Exemplo n.º 1
0
void _dumb_init_cubic(void)
{
	static int done = 0;
	if (done) return;

	resampler_init();

	done = 1;
}
Exemplo n.º 2
0
//USRP initialisation
bool usrp_init (const char *rx_subdev_par, const char *tx_subdev_par)
{
    uhd::device_addr_t hint;
    hint["type"] = "usrp1";

    //discover the usrps and print the results
    uhd::device_addrs_t device_addrs = uhd::device::find(hint);

    if (device_addrs.size() == 0){
        std::cerr << "*** FATAL: No UHD devices Found" << std::endl;
        return false;
    }

    for (size_t i = 0; i < device_addrs.size(); i++){
        std::cout << "--------------------------------------------------" << std::endl;
        std::cout << "-- UHD Device " << i << std::endl;
        std::cout << "--------------------------------------------------" << std::endl;
        std::cout << device_addrs[i].to_pp_string() << std::endl << std::endl;
        
        usrp.tx_enabled = false;        

        // USRP create
        usrp.sdev = uhd::usrp::multi_usrp::make(device_addrs[i]);
        //Lock mboard clocks
        usrp.sdev->set_clock_config(uhd::clock_config_t::internal(), 0);        
        
        //std::cerr << boost::format("USRP Subdev specs: %s\n") % subdev_specs << std::endl;
        usrp_set_subdev_args(rx_subdev_par, tx_subdev_par);            
        
        //set the USRP rx standard sample rate 256000 for minimum bandwidth.
        usrp.sdev->set_rx_rate(USRP_RX_SAMPLERATE);
        //std::cout << boost::format("USRP RX Rate: %f Msps") % (usrp.sdev->get_rx_rate()/1e6) << std::endl; 
        //set the USRP tx standard sample rate 320000 - USRP interpolation is automatic
        usrp.sdev->set_tx_rate(USRP_TX_SAMPLERATE);               
        //std::cout << boost::format("USRP TX Rate: %f Msps") % (usrp.sdev->get_tx_rate()/1e6) << std::endl; 

        usrp_set_frequency(DEFAULT_RX_FREQUENCY); //TEMPORARY      
        sleep(1); //allow for some setup time
        
        usrp.dev = usrp.sdev->get_device();
        MAX_USRP_RX_BUFFER = usrp.dev->get_max_recv_samps_per_packet();
        MAX_USRP_TX_BUFFER = usrp.dev->get_max_send_samps_per_packet();
        
        ZERO_CPX_VECTOR = std::vector<std::complex<float> >(MAX_USRP_RX_BUFFER, CPX_ZERO);
                
        setup_rx_queue();
        setup_tx_queue();
        
        pthread_mutex_init(&RECEIVER_CYCLE_lock, NULL );
        
        resampler_init();

        return true;
    }
    return false;
}
Exemplo n.º 3
0
	USFPlayer(const std::string &fileName) {  	
		usf_state = new usf_loader_state;
		usf_state->emu_state = malloc( usf_get_state_size() );
		usf_clear( usf_state->emu_state );
		sample_rate = 0;
			
		char temp[fileName.length()+1];
		strcpy(temp, fileName.c_str());

		LOGD("Trying to load USF %s", string(temp));
					
		if ( psf_load( temp, &psf_file_system, 0x21, usf_loader, usf_state, usf_info, usf_state, 1 ) < 0 )
			throw player_exception();

		usf_set_hle_audio(usf_state->emu_state, 1);
		
		PSFFile psf { fileName };
		if(psf.valid()) {
			auto &tags = psf.tags();

			int seconds = psf.songLength();

			setMeta("composer", tags["artist"],
				"sub_title", tags["title"],
				"game", tags["game"],
				"format", "Nintendo 64",
				"length", seconds
			);
		}

		usf_set_compare( usf_state->emu_state, usf_state->enable_compare );
		usf_set_fifo_full( usf_state->emu_state, usf_state->enable_fifo_full );

		const char *err = usf_render(usf_state->emu_state, 0, 0, &sample_rate);
		if(err)
			LOGD("ERROR %s", err);
		LOGD("######### RATE %d", sample_rate);
		resampler_init();
		for(auto &r : resampler) {
			r = resampler_create();
			resampler_set_quality(r, RESAMPLER_QUALITY_CUBIC);
			resampler_set_rate(r,  (float)sample_rate / 44100.0);
			//resampler_set_rate(r,  44100.0 / (float)sample_rate);
			resampler_clear(r);
		}
	}
Exemplo n.º 4
0
Arquivo: stream.c Projeto: EQ4/musicd
bool stream_transcode(stream_t *stream, codec_type_t codec_type, int bitrate)
{
  int result;
  enum AVCodecID dst_codec_id;
  AVCodec *dst_codec = NULL;
  AVCodecContext *decoder = NULL, *encoder = NULL;
  enum AVSampleFormat dst_sample_fmt;
  int dst_sample_rate;
  resampler_t *resampler = NULL;

  if (codec_type == CODEC_TYPE_MP3) {
    dst_codec_id = AV_CODEC_ID_MP3;
  } else if (codec_type == CODEC_TYPE_OGG_VORBIS) {
    dst_codec_id = AV_CODEC_ID_VORBIS;
  } else if (codec_type == CODEC_TYPE_FLAC) {
    dst_codec_id = AV_CODEC_ID_FLAC;
  } else if (codec_type == CODEC_TYPE_AAC) {
    dst_codec_id = AV_CODEC_ID_AAC;
  } else if (codec_type == CODEC_TYPE_OPUS) {
    dst_codec_id = AV_CODEC_ID_OPUS;
  } else {
    musicd_log(LOG_ERROR, "stream", "unsupported encoder requested");
    return false;;
  }

  dst_codec = avcodec_find_encoder(dst_codec_id);
  if (!dst_codec) {
    musicd_log(LOG_ERROR, "stream", "requested encoder not found");
    return false;
  }

  decoder = stream->src_ctx->streams[0]->codec;

  /* Try to figure out common sample format to skip format conversion */
  decoder->request_sample_fmt =
    find_common_sample_fmt(stream->src_codec->sample_fmts,
                           dst_codec->sample_fmts);

  result = avcodec_open2(decoder, stream->src_codec, NULL);
  if (result < 0) {
    musicd_log(LOG_ERROR, "stream", "can't open decoder: %s",
               strerror(AVUNERROR(result)));
    goto fail;
  }

  dst_sample_fmt = find_sample_fmt(decoder->sample_fmt,
                                   dst_codec->sample_fmts);
  dst_sample_rate = find_sample_rate(decoder->sample_rate,
                                     dst_codec->supported_samplerates);

  /** @todo FIXME Hard-coded values. */
  if (bitrate < 64000 || bitrate > 320000) {
    bitrate = 196000;
  }

  if (decoder->channel_layout == 0) {
    decoder->channel_layout = av_get_default_channel_layout(decoder->channels);
  }

  encoder = avcodec_alloc_context3(dst_codec);
  encoder->sample_rate = dst_sample_rate;
  encoder->channels = decoder->channels;
  encoder->sample_fmt = dst_sample_fmt;
  encoder->channel_layout = decoder->channel_layout;
  encoder->bit_rate = bitrate;

  result = avcodec_open2(encoder, dst_codec, NULL);
  if (result < 0) {
    musicd_log(LOG_ERROR, "stream", "can't open encoder: %s",
               strerror(AVUNERROR(result)));
    goto fail;
  }

  if (decoder->channel_layout != encoder->channel_layout ||
      decoder->sample_fmt != encoder->sample_fmt ||
      decoder->sample_rate != encoder->sample_rate) {

    resampler = resampler_alloc();
    av_opt_set_int(resampler, "in_channel_layout", decoder->channel_layout, 0);
    av_opt_set_int(resampler, "out_channel_layout", encoder->channel_layout, 0);
    av_opt_set_int(resampler, "in_sample_rate", decoder->sample_rate, 0);
    av_opt_set_int(resampler, "out_sample_rate", encoder->sample_rate, 0);
    av_opt_set_int(resampler, "in_sample_fmt", decoder->sample_fmt, 0);
    av_opt_set_int(resampler, "out_sample_fmt", encoder->sample_fmt, 0);

    result = resampler_init(resampler);
    if (result < 0) {
      musicd_log(LOG_ERROR, "stream", "can't open resampler: %s",
               strerror(AVUNERROR(result)));
      goto fail;
    }
    musicd_log(LOG_DEBUG, "stream",
               "resample: ch:%d(%d) rate:%d fmt:%d -> ch:%d(%d) rate:%d fmt:%d",
               decoder->channel_layout, decoder->channels,
               decoder->sample_rate, decoder->sample_fmt,
               encoder->channel_layout, encoder->channels,
               encoder->sample_rate, encoder->sample_fmt);
  }

  stream->decoder = decoder;
  stream->encoder = encoder;
  stream->dst_codec = dst_codec;
  stream->dst_codec_type = codec_type;
  stream->resampler = resampler;
  format_from_av(encoder, &stream->format);

  stream->src_buf = av_audio_fifo_alloc(encoder->sample_fmt, encoder->channels, encoder->frame_size);

  stream->decode_frame = avcodec_alloc_frame();

  if (stream->resampler) {
    /* The buffer will be allocated dynamically */
    stream->resample_frame = avcodec_alloc_frame();
  }

  int buf_size = av_samples_get_buffer_size(NULL, stream->encoder->channels,
                                                  stream->encoder->frame_size,
                                                  stream->encoder->sample_fmt, 0);
  stream->encode_buf = av_mallocz(buf_size);
  stream->encode_frame = avcodec_alloc_frame();
  stream->encode_frame->nb_samples = stream->encoder->frame_size;
  avcodec_fill_audio_frame(stream->encode_frame,
                           stream->encoder->channels,
                           stream->encoder->sample_fmt,
                           stream->encode_buf, buf_size, 0);
  return true;

fail:
  if (decoder) {
    avcodec_close(decoder);
  }
  if (encoder) {
    avcodec_close(encoder);
    av_free(encoder);
  }
  if (resampler) {
    resampler_free(&resampler);
  }
  return false;
}