Use different sample rate for the Audio Plug in


Hi, I am creating a Audio Plug in which will read a file from the path specified by the user and puts the data in the buffer.
I am using the plug in host application for loading this DLL.
I want to run the process block of my plug in as the same sample rate as that of opened file. But I am not able to do it :frowning:

Currently sample rate given by the device is 44100hz and block size is 441.
My test file is 32000Hz. I want the process block to be called at the rate of 32000 ( sample rate of the wav file i loaded).

Please tell me how to accomplish.


You can’t. The hosts decides which sample-rate is used. You have to resample the audio-data.


host means the device or the plug in host?
I am guessing that 44100 is coming from the device rather than host?
It picks the default sample rate of the device and sets in to the plg in graph. Am I right?


Well depends on how host/device interact.
As plugin you “see” only what current-host tells you.


Juce has a great interpolator for resampling. You maybe start with this one:

speed ratio should be something like this: sampleSampleRate / hostSampleRate


Ok If I have to re-sample the Audio how can I do it?

If I create a Plug-in for resampling the data, still the processBlock of the resampling plug in is called at 44100Hz.
The resampled data wont be in required 32000Hz reight?

Can you please let me know how to achieve this?


I want to Decimate the signal rather than interpolate.
There is also ResamplingAudioSource which will help me in doing decimate.


Your plugin is required to deliver the sample data sampled with the frequency your host is operating on, in your case 44100 Samples/sec.
If your plugin is not using the data coming from the host but creates (or loads) sample data, it is your / your plugins responsibility to take care of, that the data is in the right sample rate. You don’t have to do anything, but then your sound is played back with the wrong speed, sounds like mickey mouse.
So read a block of data, which is shorter by 44100/32000 and resample (=interpolate) it to fill one buffer. This is the data your plugin puts into the AudioBuffer of processBlock.
To resample you can use the LagrangeInterpolator patrickkunz proposed, or you check out the ResamplingAudioSource, as you use the AudioSource for reading anyway…


I never heard of decimate in that context, but if you mean you simply drop samples, than consider that this might not improve sound quality…
And furthermore if you want to play back 32kHz PCM data with a 44.1kHz sound device, you don’t need less samples but more. This is why resampling is the right approach for you…


I think you can use the interpolator for down and upsampling. You maybe need to pre-filter the signal before you interpolate if you want good quality. You don’t know the host sample rate and simple decimation only with pre-filtering maybe don’t lead to good results if you have odd ratios.



As far I know processBlock method is called for every 10ms.

Let me explain you what I am going to do

  1. Create a file reader plug in. Which will read the file and gives the data at the sample rate of the wav file loaded.
  2. So I need to fill the Incoming AudioSampleBuffer with the samples from wav file.
  3. Create one more plug in to resample the 32k data into 8k data. (explained later)
  4. Use this data as Input to my other plug in which does the processing. (I need 8k data as input to my algorithm)

Is this right?


You can’t change the sample rate inside a plugin. You have always the same number of input and output samples.


I know that there are re sampling plug ins. How does they work?


It sounds to me, that you are actually not creating a plugin but rather some sort of custom host?

There is something called AudioProcessorGraph. Here you can combine several steps of processing. But I didn’t use that, so maybe somebody else should chime in, if you have questions how to use it.

But to explain: the application that is feeding the AudioDevice operates with exactly one sample rate. everything else needs to process it’s data so that the produced output matches the device’s setup.
Usually the host picks up that speed and expects all it’s plugins to deliver it’s output in that sample rate. So you should design your processing that it can cope with that, e.g. by resampling it’s audiodata.

BTW. usually a plugin picks up the audio data from the host, so the host has already resampled the data for you.
If you deliver audio data from other sources, then you are creating a synth?