Exemple #1
0
void snd_reset_samplerate_conv(int rec_or_stream)
{
    int error;
    
    if (rec_or_stream == SND_STREAM)
    { 
        if (srconv_state_stream != NULL)
        {
            src_delete(srconv_state_stream);
            srconv_state_stream = NULL;
        }

        srconv_state_stream = src_new(cfg.audio.resample_mode, cfg.audio.channel, &error);
        if (srconv_state_stream == NULL)
        {
            print_info(_("ERROR: Could not initialize samplerate converter"), 0);
        }
    }

    if (rec_or_stream == SND_REC)
    { 
        if (srconv_state_record != NULL)
        {
            src_delete(srconv_state_record);
            srconv_state_record = NULL;
        }


        srconv_state_record = src_new(cfg.audio.resample_mode, cfg.audio.channel, &error);
        if (srconv_state_record == NULL)
        {
            print_info(_("ERROR: Could not initialize samplerate converter"), 0);
        }
    }
}
Exemple #2
0
static void
zero_input_test (int converter)
{	SRC_DATA data ;
	SRC_STATE *state ;
	float out [100] ;
	int error ;

	printf ("    %s (%-26s) ........ ", __func__, src_get_name (converter)) ;
	fflush (stdout) ;

	if ((state = src_new (converter, 1, &error)) == NULL)
	{	printf ("\n\nLine %d : src_new failed : %s.\n\n", __LINE__, src_strerror (error)) ;
		exit (1) ;
		} ;

	data.data_in = (float *) 0xdeadbeef ;
	data.input_frames = 0 ;
	data.data_out = out ;
	data.output_frames = ARRAY_LEN (out) ;
	data.end_of_input = 0 ;
	data.src_ratio = 1.0 ;

	if ((error = src_process (state, &data)))
	{	printf ("\n\nLine %d : src_new failed : %s.\n\n", __LINE__, src_strerror (error)) ;
		exit (1) ;
		} ;

	state = src_delete (state) ;

	puts ("ok") ;
} /* zero_input_test */
Exemple #3
0
int jack_card_open_w(JackCard *obj,int bits,int stereo,int rate)
{
  int channels = stereo + 1, bsize, err;
  obj->write.init = TRUE;
  if (jack_init(obj) != 0) return -1;

  obj->write.rate = rate;
  obj->sample_size = bits / 8;
  obj->frame_size = channels * obj->sample_size;
  bsize = BSIZE;
  obj->write.frames = bsize / 2;
  SND_CARD(obj)->bsize = bsize;
  SND_CARD(obj)->flags |= SND_CARD_FLAGS_OPENED;
  obj->write.channels = channels;
  if ((obj->write.src_state = src_new (SRC_SINC_FASTEST, channels, &err)) == NULL)
    g_warning("Error while initializing the samplerate converter: %s", src_strerror(err));
  obj->write.data.src_ratio = (double)obj->rate / (double)rate;
  obj->write.data.data_in = malloc(obj->write.frames*sizeof(float));
  obj->write.data.end_of_input = 0;
  obj->write.data.output_frames = (long)((double)obj->write.frames*obj->write.data.src_ratio);
  obj->write.data.data_out = malloc(obj->write.data.output_frames*sizeof(float));
  if (!obj->write.buffer)
    obj->write.buffer = jack_ringbuffer_create(WRITEBUFFERSIZE);
  obj->write.can_process = TRUE;
  obj->can_process = TRUE;
  obj->write.open = TRUE;
  obj->write.init = FALSE;
  return 0;
}
Exemple #4
0
static int OpenResampler (vlc_object_t *obj)
{
    filter_t *filter = (filter_t *)obj;

    /* Only float->float */
    if (filter->fmt_in.audio.i_format != VLC_CODEC_FL32
     || filter->fmt_out.audio.i_format != VLC_CODEC_FL32
    /* No channels remapping */
     || filter->fmt_in.audio.i_physical_channels
                                  != filter->fmt_out.audio.i_physical_channels
     || filter->fmt_in.audio.i_original_channels
                                  != filter->fmt_out.audio.i_original_channels)
        return VLC_EGENERIC;

    int type = var_InheritInteger (obj, "src-converter-type");
    int channels = aout_FormatNbChannels (&filter->fmt_in.audio);
    int err;

    SRC_STATE *s = src_new (type, channels, &err);
    if (s == NULL)
    {
        msg_Err (obj, "cannot initialize resampler: %s", src_strerror (err));
        return VLC_EGENERIC;
    }

    filter->p_sys = (filter_sys_t *)s;
    filter->pf_audio_filter = Resample;
    return VLC_SUCCESS;
}
static int pcm_src_init(void *obj, snd_pcm_rate_info_t *info)
{
	struct rate_src *rate = obj;
	int err;

	if (! rate->state || rate->channels != info->channels) {
		if (rate->state)
			src_delete(rate->state);
		rate->channels = info->channels;
		rate->state = src_new(rate->converter, rate->channels, &err);
		if (! rate->state)
			return -EINVAL;
	}

	rate->ratio = (double)info->out.rate / (double)info->in.rate;

	free(rate->src_buf);
	rate->src_buf = malloc(sizeof(float) * rate->channels * info->in.period_size);
	free(rate->dst_buf);
	rate->dst_buf = malloc(sizeof(float) * rate->channels * info->out.period_size);
	if (! rate->src_buf || ! rate->dst_buf) {
		pcm_src_free(rate);
		return -ENOMEM;
	}

	rate->data.data_in = rate->src_buf;
	rate->data.data_out = rate->dst_buf;
	rate->data.src_ratio = rate->ratio;
	rate->data.end_of_input = 0;

	return 0;
}
Exemple #6
0
SDLAudioDevice::SDLAudioDevice(IBus *bus)
  : _state(src_new(SRC_LINEAR, 1, &_error))
  , _bus(bus)
{

  memset(_in.data(), 0, BUFFER_SIZE * sizeof(float));
  memset(_out.data(), 0, BUFFER_SIZE * sizeof(float));

  SDL_InitSubSystem(SDL_INIT_AUDIO);

  SDL_AudioSpec obtained, desired;
  SDL_zero(desired);

  desired.freq = 44800;
  desired.format = AUDIO_S16LSB;
  desired.channels = 1;
  desired.samples = AUDIO_BUFFER_SIZE * 2;
  desired.callback = audio_callback;
  desired.userdata = this;

  _device = SDL_OpenAudioDevice(NULL, 0, &desired, &obtained, 0); // FIXME: handle different configurations
  if (_device == 0) {
    std::cerr << "Couldn't open audio device: " << SDL_GetError() << "\n";
    throw 0;
  } else {
    std::cout << obtained.freq << '\n';
  }

}
Exemple #7
0
int VPOutPluginDummy::init(VPlayer *v, VPBuffer *in)
{
    DBG("Dummy:init");
    owner = v;
    bin = in;

    out_srate = 48000;
    in_srate = bin->srate;
    int rerr;
    rs = src_new(SRC_SINC_FASTEST, bin->chans, &rerr);
    if (!rs){
        DBG("SRC error"<<rerr);
        return -1;
    }

    rd.src_ratio = (out_srate*1.0d)/(in_srate*1.0d);
    out_frames = (VPBUFFER_FRAMES*rd.src_ratio)*2;
    out_buf = (float *)malloc(out_frames*sizeof(float)*bin->chans);
    DBG("target rate "<<out_srate);
    work = true;
    paused = false;
    pause_check = false;

    FULL_MEMORY_BARRIER;

    worker = new std::thread((void(*)(void*))worker_run, this);
    DBG("Dummy thread made");
    return 0;
}
Exemple #8
0
static gboolean
xmms_vocoder_init (xmms_xform_t *xform)
{
	xmms_vocoder_data_t *priv;
	xmms_config_property_t *config;

	g_return_val_if_fail (xform, FALSE);

	priv = g_new0 (xmms_vocoder_data_t, 1);
	priv->winsize = 2048;
	priv->channels = xmms_xform_indata_get_int (xform, XMMS_STREAM_TYPE_FMT_CHANNELS);
	priv->bufsize = priv->winsize * priv->channels;

	priv->iobuf = g_malloc (priv->bufsize * sizeof (gint16));
	priv->procbuf = g_malloc (priv->bufsize * sizeof (pvocoder_sample_t));
	priv->resbuf = g_malloc (priv->bufsize * sizeof (gfloat));
	priv->outbuf = g_string_new (NULL);

	priv->pvoc = pvocoder_init (priv->winsize, priv->channels);
	g_return_val_if_fail (priv->pvoc, FALSE);

	priv->resampler = src_new (SRC_LINEAR, priv->channels, NULL);
	g_return_val_if_fail (priv->resampler, FALSE);

	xmms_xform_private_data_set (xform, priv);

	config = xmms_xform_config_lookup (xform, "enabled");
	g_return_val_if_fail (config, FALSE);
	xmms_config_property_callback_set (config, xmms_vocoder_config_changed, priv);
	priv->enabled = !!xmms_config_property_get_int (config);

	config = xmms_xform_config_lookup (xform, "speed");
	g_return_val_if_fail (config, FALSE);
	xmms_config_property_callback_set (config, xmms_vocoder_config_changed, priv);
	priv->speed = (gfloat) xmms_config_property_get_int (config) / 100.0;

	config = xmms_xform_config_lookup (xform, "pitch");
	g_return_val_if_fail (config, FALSE);
	xmms_config_property_callback_set (config, xmms_vocoder_config_changed, priv);
	priv->pitch = 100.0 / (gfloat) xmms_config_property_get_int (config);

	config = xmms_xform_config_lookup (xform, "attack_detection");
	g_return_val_if_fail (config, FALSE);
	xmms_config_property_callback_set (config, xmms_vocoder_config_changed, priv);
	priv->attack_detection = !!xmms_config_property_get_int (config);

	pvocoder_set_scale (priv->pvoc, priv->speed * priv->pitch);
	pvocoder_set_attack_detection (priv->pvoc, priv->attack_detection);

	priv->resdata.data_in = NULL;
	priv->resdata.input_frames = 0;
	priv->resdata.data_out = priv->resbuf;
	priv->resdata.output_frames = priv->winsize;
	priv->resdata.src_ratio = priv->pitch;
	priv->resdata.end_of_input = 0;

	xmms_xform_outdata_type_copy (xform);

	return TRUE;
}
HRESULT CSampleRateConverter::SetupConversion()
{
  m_nFrameSize = m_pInputFormat->Format.nBlockAlign;

  m_dSampleRateRation = (double)m_pOutputFormat->Format.nSamplesPerSec / (double)m_pInputFormat->Format.nSamplesPerSec;

  if (m_pSrcState)
    m_pSrcState = src_delete(m_pSrcState);

  int error = 0;
  m_pSrcState = src_new(m_pSettings->m_nResamplingQuality, m_pInputFormat->Format.nChannels, &error);

  m_llFramesInput = 0;
  m_llFramesOutput = 0;

  // TODO better error handling
  if (error != 0)
    return S_FALSE;

  Log("CSampleRateConverter::SetupConversion");
  LogWaveFormat(m_pInputFormat, "Input format    ");
  LogWaveFormat(m_pOutputFormat, "Output format   ");

  return S_OK;
}
Exemple #10
0
SampleChannel::SampleChannel(int bufferSize, char side)
	: Channel          (CHANNEL_SAMPLE, STATUS_EMPTY, side, bufferSize),
		frameRewind      (-1),
		wave             (NULL),
		tracker          (0),
		begin            (0),
		end              (0),
		pitch            (gDEFAULT_PITCH),
		boost            (1.0f),
		mode             (DEFAULT_CHANMODE),
		qWait	           (false),
		fadeinOn         (false),
		fadeinVol        (1.0f),
		fadeoutOn        (false),
		fadeoutVol       (1.0f),
		fadeoutTracker   (0),
		fadeoutStep      (DEFAULT_FADEOUT_STEP),
		key              (0),
	  readActions      (true),
	  midiInReadActions(0x0),
	  midiInPitch      (0x0)
{
	rsmp_state = src_new(SRC_LINEAR, 2, NULL);
	pChan      = (float *) malloc(kernelAudio::realBufsize * 2 * sizeof(float));
}
Exemple #11
0
void alloc_ports( int n_capture, int n_playback ) {

    int port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
    int chn;
    jack_port_t *port;
    char buf[32];

    capture_ports = NULL;
    for (chn = 0; chn < n_capture; chn++)
    {
	snprintf (buf, sizeof(buf) - 1, "capture_%u", chn+1);

	port = jack_port_register (client, buf,
		JACK_DEFAULT_AUDIO_TYPE,
		port_flags, 0);

	if (!port)
	{
	    printf( "jacknet_client: cannot register port for %s", buf);
	    break;
	}

	capture_srcs = jack_slist_append( capture_srcs, src_new( 4-samplerate_quality, 1, NULL ) );
	capture_ports = jack_slist_append (capture_ports, port);
    }

    port_flags = JackPortIsInput;

    playback_ports = NULL;
    for (chn = 0; chn < n_playback; chn++)
    {
	snprintf (buf, sizeof(buf) - 1, "playback_%u", chn+1);

	port = jack_port_register (client, buf,
		JACK_DEFAULT_AUDIO_TYPE,
		port_flags, 0);

	if (!port)
	{
	    printf( "jacknet_client: cannot register port for %s", buf);
	    break;
	}

	playback_srcs = jack_slist_append( playback_srcs, src_new( 4-samplerate_quality, 1, NULL ) );
	playback_ports = jack_slist_append (playback_ports, port);
    }
}
Exemple #12
0
unsigned int AmAudio::downMix(unsigned int size)
{
  unsigned int s = size;
  if(fmt->channels == 2){
    stereo2mono(samples.back_buffer(),(unsigned char*)samples,s);
    samples.swap();
  }

#ifdef USE_LIBSAMPLERATE 
  if (fmt->rate != SYSTEM_SAMPLERATE) {
    if (!resample_state) {
      int src_error;
      // for better quality but more CPU usage, use SRC_SINC_ converters
      resample_state = src_new(SRC_LINEAR, 1, &src_error);
      if (!resample_state) {
	ERROR("samplerate initialization error: ");
      }
    }

    if (resample_state) {
      if (resample_buf_samples + PCM16_B2S(s) > PCM16_B2S(AUDIO_BUFFER_SIZE) * 2) {
	WARN("resample input buffer overflow! (%d)\n",
	     resample_buf_samples + PCM16_B2S(s));
      } else {
	signed short* samples_s = (signed short*)(unsigned char*)samples;
	src_short_to_float_array(samples_s, &resample_in[resample_buf_samples], PCM16_B2S(s));
	resample_buf_samples += PCM16_B2S(s);
      }
      
      SRC_DATA src_data;
      src_data.data_in = resample_in;
      src_data.input_frames = resample_buf_samples;
      src_data.data_out = resample_out;
      src_data.output_frames = PCM16_B2S(AUDIO_BUFFER_SIZE);
      src_data.src_ratio = (double)SYSTEM_SAMPLERATE / (double)fmt->rate;
      src_data.end_of_input = 0;

      int src_err = src_process(resample_state, &src_data);
      if (src_err) {
	DBG("resample error: '%s'\n", src_strerror(src_err));
      }else {
	signed short* samples_s = (signed short*)(unsigned char*)samples;
	src_float_to_short_array(resample_out, samples_s, src_data.output_frames_gen);
	s = PCM16_S2B(src_data.output_frames_gen);

	if (resample_buf_samples !=  (unsigned int)src_data.input_frames_used) {
	  memmove(resample_in, &resample_in[src_data.input_frames_used], 
		  (resample_buf_samples - src_data.input_frames_used) * sizeof(float));
	}
	resample_buf_samples = resample_buf_samples - src_data.input_frames_used;
      }
    }
  }
#endif
 

  return s;
}
void* decOpen(
	const char* inputurl,
	size_t* bitspersample,
	size_t* channels,
	size_t* samplerate)
{
	int i;

	for(i = 0; decoders[i].open != NULL ; i++)
	{
		int err;
		void* data;
		dec_data* ret;

		if((data = decoders[i].open(
			inputurl,
			bitspersample, 
			channels,
			samplerate,
			&err)) == NULL)
		{
			if(err != INVALID_FORMAT)
				return NULL;
			else
				continue;
		}
 	
		if((ret = (dec_data*)malloc(sizeof(dec_data))) == NULL)
		{
			decoders[i].close(data);
			return NULL;
		}

		ret->decoder = &decoders[i];
		ret->data    = data;
	
		if((ret->converter = src_new(SRC_LINEAR, audio_channels, &err)) == NULL)
		{
			LOG(L"libsamplerate: " MB_FORMAT "\n", src_strerror(err));
	
			decoders[i].close(data);
			free(ret);
			
			return NULL;
		}
		
		ret->ratio = ((double)audio_samplerate / (double)*samplerate);
		src_set_ratio((SRC_STATE*)ret->converter, ret->ratio);
		
		ret->samplerate = *samplerate;
		ret->channels   = *channels;
		
		return ret;
	}

	return NULL;
}
Exemple #14
0
static int init_src(void) {
    int err;
    if (fancy_resampling)
        src = src_new(SRC_SINC_MEDIUM_QUALITY, 2, &err);
    else
        src = NULL;

    return err;
}
Exemple #15
0
unsigned int AmLibSamplerateResamplingState::resample(unsigned char* samples, unsigned int s, double ratio)
{
  DBG("resampling packet of size %d with ratio %f", s, ratio);
  if (!resample_state) {
    int src_error;
    // for better quality but more CPU usage, use SRC_SINC_ converters
    resample_state = src_new(SRC_LINEAR, 1, &src_error);
    if (!resample_state) {
      ERROR("samplerate initialization error: ");
    }
  }

  if (resample_state) {
    if (resample_buf_samples + PCM16_B2S(s) > PCM16_B2S(AUDIO_BUFFER_SIZE) * 2) {
      WARN("resample input buffer overflow! (%lu)\n", resample_buf_samples + PCM16_B2S(s));
    } else if (resample_out_buf_samples + (PCM16_B2S(s) * ratio) + 20 > PCM16_B2S(AUDIO_BUFFER_SIZE)) {
      WARN("resample: possible output buffer overflow! (%lu)\n", (resample_out_buf_samples + (size_t) ((PCM16_B2S(s) * ratio)) + 20));
    } else {
      signed short* samples_s = (signed short*)samples;
      src_short_to_float_array(samples_s, &resample_in[resample_buf_samples], PCM16_B2S(s));
      resample_buf_samples += PCM16_B2S(s);
    }

    SRC_DATA src_data;
    src_data.data_in = resample_in;
    src_data.input_frames = resample_buf_samples;
    src_data.data_out = &resample_out[resample_out_buf_samples];
    src_data.output_frames = PCM16_B2S(AUDIO_BUFFER_SIZE);
    src_data.src_ratio = ratio;
    src_data.end_of_input = 0;

    int src_err = src_process(resample_state, &src_data);
    if (src_err) {
      DBG("resample error: '%s'\n", src_strerror(src_err));
    }else {
      signed short* samples_s = (signed short*)(unsigned char*)samples;
      resample_out_buf_samples += src_data.output_frames_gen;
      s *= ratio;
      src_float_to_short_array(resample_out, samples_s, PCM16_B2S(s));
      DBG("resample: output_frames_gen = %ld", src_data.output_frames_gen);

      if (resample_buf_samples !=  (unsigned int)src_data.input_frames_used) {
	memmove(resample_in, &resample_in[src_data.input_frames_used],
		(resample_buf_samples - src_data.input_frames_used) * sizeof(float));
      }
      resample_buf_samples = resample_buf_samples - src_data.input_frames_used;

      if (resample_out_buf_samples != s) {
	memmove(resample_out, &resample_out[PCM16_B2S(s)], (resample_out_buf_samples - PCM16_B2S(s)) * sizeof(float));
      }
      resample_out_buf_samples -= PCM16_B2S(s);
    }
  }

  DBG("resample: output size is %d", s);
  return s;
}
Exemple #16
0
//***************************************************************************
Kwave::RateConverter::RateConverter()
    :Kwave::SampleSource(), m_ratio(1.0), m_converter(0),
     m_converter_in(), m_converter_out()
{
    int error = 0;
    m_converter = src_new(SRC_SINC_MEDIUM_QUALITY, 1, &error);
    Q_ASSERT(m_converter);
    if (!m_converter) qWarning("creating converter failed: '%s",
	src_strerror(error));
}
SamplerateConverter::SamplerateConverter (int freq) : _maxFreq(freq)
{
    int err;
    _src_state = src_new (SRC_LINEAR, 1, &err);

    _samples = (freq * 20) / 1000; // start with 20 ms buffers

    _floatBufferIn = new float32[_samples];
    _floatBufferOut = new float32[_samples];
}
Exemple #18
0
int LV2convolv::resample_read_presets (const float *in, unsigned int in_frames, const int sample_rate, float **buf, unsigned int *n_ch, unsigned int *n_sp)
{
	float resample_ratio = 1.0;

	if (n_ch) *n_ch = PRESETS_CH;
	if (n_sp) *n_sp = in_frames;

	if (sample_rate != PRESETS_SAMPLERATE) {
		fprintf(stderr, "convolution: samplerate mismatch preset:%d host:%d\n", PRESETS_SAMPLERATE, sample_rate);
		resample_ratio = (float) sample_rate / (float) PRESETS_SAMPLERATE;
	}

	if (buf) {
		const size_t frames_in = PRESETS_CH * in_frames;
		const size_t frames_out = PRESETS_CH * ceil(in_frames * resample_ratio);
		*buf = (float*) malloc(frames_out*sizeof(float));
		
		float *iin;
		if (resample_ratio != 1.0) {
			iin = (float*)malloc(frames_in * sizeof(float));
			memcpy(iin, in, frames_in * sizeof(float));
		} else {
			memcpy(*buf, in, frames_in * sizeof(float));
		}

		if (!*buf) {
			fprintf (stderr, "convolution: memory allocation failed for IR audio-file buffer.\n");
			return (-2);
		}

		if (resample_ratio != 1.0) {
			VERBOSE_printf("convolution: resampling IR %ld -> %ld [frames * channels].\n",
					(long int) frames_in,
					(long int) frames_out);
			SRC_STATE* src_state = src_new(SRC_QUALITY, PRESETS_CH, NULL);
			SRC_DATA src_data;

			src_data.input_frames  = in_frames;
			src_data.output_frames = in_frames * resample_ratio;
			src_data.end_of_input  = 1;
			src_data.src_ratio     = resample_ratio;
			src_data.input_frames_used = 0;
			src_data.output_frames_gen = 0;
			src_data.data_in       = iin;
			src_data.data_out      = *buf;
			src_process(src_state, &src_data);
			VERBOSE_printf("convolution: resampled IR  %ld -> %ld [frames * channels].\n",
					src_data.input_frames_used * PRESETS_CH,
					src_data.output_frames_gen * PRESETS_CH);
			if (n_sp) *n_sp = (unsigned int) src_data.output_frames_gen;
			free(iin);
		}
	}
	return (0);
}
Exemple #19
0
ResampleSRC::ResampleSRC(int c)
    : Resample(c)
    , state(NULL)
{
	state = new SRC_STATE *[c];
	for(int i = 0; i < c; ++i) {
		int error;
		state[i] = src_new(SRC_ZERO_ORDER_HOLD,1,&error);
		if(!state[i]) post("src init error %i",error);
	}
}
void CDVDPlayerResampler::CheckResampleBuffers(int channels)
{
  int error;
  if (channels != m_nrchannels)
  {
    Clean();

    m_nrchannels = channels;
    m_converter = src_new(m_quality, m_nrchannels, &error);
  }
}
SamplerateConverter::SamplerateConverter(int freq) : floatBufferIn_(0),
    floatBufferOut_(0), samples_(0), maxFreq_(freq), src_state_(0)
{
    int err;
    src_state_ = src_new(SRC_LINEAR, 1, &err);

    samples_ = (freq * 20) / 1000; // start with 20 ms buffers

    floatBufferIn_ = new float[samples_];
    floatBufferOut_ = new float[samples_];
}
Exemple #22
0
void Resample::configure() {
  int quality = parameter("quality").toInt();
  Real factor = parameter("outputSampleRate").toReal() / parameter("inputSampleRate").toReal();

  if (_state) src_delete(_state);
  int nChannels = 1;
  _state = src_new(quality, nChannels, &_errorCode);

  _data.src_ratio = factor;

  reset();
}
Exemple #23
0
  void play(){
    int num_channels=getSourceNumChannels();

    if(isinitialized==false || num_channels==0)
      return;

    if(isusingjack==false && audioDeviceManager.getCurrentAudioDevice()==NULL)
      return;

    stop();

    GUI_newprocess(source_init);
    //source_init();
    //fprintf(stderr,"GUI_newprocess finished\n");

#if 0      
    if(isusingjack==false){
      if(audioDeviceManager.getOutputChannels().countNumberOfSetBits()!=getSourceNumChannels()){
	if(num_channels==1)
	  num_channels=2;
	outchannels=new BitArray(0);
	for(int lokke=0;lokke<num_channels;lokke++)
	  outchannels->setBit(lokke);
	audioDeviceManager.setAudioDevice(
					  audioDeviceManager.getAvailableAudioDeviceNames()[0],
					  1024,
					  R,
					  new BitArray(0),
					  outchannels,
					false);
      }
    }
#endif  

    {
      int error;
      for(int i=0;i<num_src_states;i++)
	src_delete(src_states[i]);
      free(src_states);
      
      src_states=(SRC_STATE**)malloc(sizeof(SRC_STATE*)*num_channels);
      
      for(int ch=0;ch<num_channels;ch++)
	src_states[ch]=src_new(SRC_QUALITY,1,&error);
    }

    pleasestop=false;
    jp_playpos=0;
    mustrunonemore=false;
    jp_isplaying=true;
    isreadingdata=true;
  }
Exemple #24
0
SampleBuffer::handleState::handleState( bool _varying_pitch, int interpolation_mode ) :
	m_frameIndex( 0 ),
	m_varyingPitch( _varying_pitch ),
	m_isBackwards( false )
{
	int error;
	m_interpolationMode = interpolation_mode;
	
	if( ( m_resamplingData = src_new( interpolation_mode, DEFAULT_CHANNELS, &error ) ) == NULL )
	{
		qDebug( "Error: src_new() failed in sample_buffer.cpp!\n" );
	}
}
Exemple #25
0
int freedv_create() {
    pthread_mutex_init(&mutex, NULL);

    int src_error;
    insrc1 = src_new(SRC_SINC_FASTEST, 1, &src_error);

    fdmdv = fdmdv_create();
    codec2 = codec2_create(CODEC2_MODE_1400);
    output_buf =
        (short*)malloc(2*sizeof(short)*codec2_samples_per_frame(codec2));

    return 1;
}
Exemple #26
0
void Resampler::resample(const AudioBuffer &dataIn, AudioBuffer &dataOut)
{
    const double inputFreq = dataIn.getSampleRate();
    const double outputFreq = dataOut.getSampleRate();
    const double sampleFactor = outputFreq / inputFreq;

    if (sampleFactor == 1.0)
        return;

    const size_t nbFrames = dataIn.frames();
    const size_t nbChans = dataIn.channels();

    if (nbChans != format_.nb_channels) {
        // change channel num if needed
        int err;
        src_delete(src_state_);
        src_state_ = src_new(SRC_LINEAR, nbChans, &err);
        format_.nb_channels = nbChans;
        DEBUG("SRC channel number changed.");
    }
    if (nbChans != dataOut.channels()) {
        DEBUG("Output buffer had the wrong number of channels (in: %d, out: %d).", nbChans, dataOut.channels());
        dataOut.setChannelNum(nbChans);
    }

    size_t inSamples = nbChans * nbFrames;
    size_t outSamples = inSamples * sampleFactor;

    // grow buffer if needed
    floatBufferIn_.resize(inSamples);
    floatBufferOut_.resize(outSamples);
    scratchBuffer_.resize(outSamples);

    SRC_DATA src_data;
    src_data.data_in = floatBufferIn_.data();
    src_data.data_out = floatBufferOut_.data();
    src_data.input_frames = nbFrames;
    src_data.output_frames = nbFrames * sampleFactor;
    src_data.src_ratio = sampleFactor;
    src_data.end_of_input = 0; // More data will come

    dataIn.interleaveFloat(floatBufferIn_.data());

    src_process(src_state_, &src_data);

    /*
    TODO: one-shot deinterleave and float-to-short conversion
    */
    src_float_to_short_array(floatBufferOut_.data(), scratchBuffer_.data(), outSamples);
    dataOut.deinterleave(scratchBuffer_.data(), src_data.output_frames, nbChans);
}
Exemple #27
0
JNIEXPORT jint JNICALL Java_org_sipdroid_media_file_AudioFile_nresample(JNIEnv* env, jobject obj, jdouble ratio, jshortArray inBuffer, jshortArray outBuffer){
	if(ready){
		pthread_mutex_lock(&lock);
		// initialize converter
		if(converter == NULL){
			int error;
			converter = src_new(SRC_SINC_MEDIUM_QUALITY, 1, &error);
			if(converter == NULL){
				__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "unable to initialize sample rate converter: %s", src_strerror(error));
				pthread_mutex_unlock(&lock);
				return -1;
			}
		}
		// prepare buffers
		jint input_len = env->GetArrayLength(inBuffer);
		float* fl_inBuffer = (float*) malloc(input_len * sizeof(float));
		short* sh_inBuffer = env->GetShortArrayElements(inBuffer, NULL);
		src_short_to_float_array(sh_inBuffer, fl_inBuffer, input_len);
		env->ReleaseShortArrayElements(inBuffer, sh_inBuffer, 0);

		jint output_len = env->GetArrayLength(outBuffer);
		float* fl_outBuffer = (float*) malloc(sizeof(float) * output_len);

		SRC_DATA src_data;
		src_data.data_in = fl_inBuffer;
		src_data.input_frames = (long) input_len;
		src_data.data_out = fl_outBuffer;
		src_data.output_frames = (long) output_len;
		src_data.src_ratio = (double) 1/ratio;
		src_data.end_of_input = 0;

		// resample
		int error;
		if ((error = src_process(converter, &src_data)) >= 0){
			// convert output to float and write to outBuffer
			short* sh_outBuffer = env->GetShortArrayElements(outBuffer, NULL);
			src_float_to_short_array(src_data.data_out, sh_outBuffer, src_data.output_frames_gen);
			env->ReleaseShortArrayElements(outBuffer, sh_outBuffer, 0);
		}
		else{
			__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "resampling error: %s", src_strerror(error));
		}

		free(fl_outBuffer);
		free(fl_inBuffer);

		pthread_mutex_unlock(&lock);
		return src_data.output_frames_gen;
	}
	return 0;
}
Exemple #28
0
SampleBuffer::handleState::handleState( bool _varying_pitch ) :
	m_frameIndex( 0 ),
	m_varyingPitch( _varying_pitch )
{
	int error;
	if( ( m_resamplingData = src_new(/*
		( engine::mixer()->highQuality() == true ) ?
					SRC_SINC_FASTEST :*/
					SRC_LINEAR,
					DEFAULT_CHANNELS, &error ) ) == NULL )
	{
		printf( "Error: src_new() failed in sample_buffer.cpp!\n" );
	}
}
Exemple #29
0
void
Resampler::setFormat(AudioFormat format)
{
    format_ = format;
    samples_ = (format.nb_channels * format.sample_rate * 20) / 1000; // start with 20 ms buffers
    floatBufferIn_.resize(samples_);
    floatBufferOut_.resize(samples_);
    scratchBuffer_.resize(samples_);

    if (src_state_ != nullptr)
        src_delete(src_state_);

    int err;
    src_state_ = src_new(SRC_LINEAR, format.nb_channels, &err);
}
aubio_resampler_t *
new_aubio_resampler (smpl_t ratio, uint_t type)
{
  aubio_resampler_t *s = AUBIO_NEW (aubio_resampler_t);
  int error = 0;
  s->stat = src_new (type, 1, &error);  /* only one channel */
  if (error) {
    AUBIO_ERR ("Failed creating resampler: %s\n", src_strerror (error));
    del_aubio_resampler(s);
    return NULL;
  }
  s->proc = AUBIO_NEW (SRC_DATA);
  s->ratio = ratio;
  return s;
}