# Resampling an AudioSampleBuffer

**URL:** https://forum.juce.com/t/resampling-an-audiosamplebuffer/14287
**Category:** Audio Plugins
**Created:** [February 10, 2015, 2:42am UTC](https://forum.juce.com/t/resampling-an-audiosamplebuffer/14287 "2015-02-10T02:42:03Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![lapsang](https://avatars.discourse-cdn.com/v4/letter/l/3be4f8/32.png) [@lapsang](https://forum.juce.com/u/lapsang)
#### Post date: [February 10, 2015, 2:42am UTC](https://forum.juce.com/t/resampling-an-audiosamplebuffer/14287/1 "2015-02-10T02:42:03Z")

</div>

Is there a "simple" way to resample an AudioSampleBuffer? I tried playing with&nbsp;ResamplingAudioSource but I couldn't really figure out how to use it for a fixed buffer (not live).&nbsp;

What should I do or where should I look?

&nbsp;

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)
#### Post date: [February 10, 2015, 9:16am UTC](https://forum.juce.com/t/resampling-an-audiosamplebuffer/14287/2 "2015-02-10T09:16:31Z")

</div>

There's also the&nbsp;LagrangeInterpolator, which is a bit more low-level, if that's what you're looking for?

---

<div class="post-metadata">

### Author: ![lapsang](https://avatars.discourse-cdn.com/v4/letter/l/3be4f8/32.png) [@lapsang](https://forum.juce.com/u/lapsang)
#### Post date: [February 10, 2015, 3:24pm UTC](https://forum.juce.com/t/resampling-an-audiosamplebuffer/14287/3 "2015-02-10T15:24:00Z")

</div>

Thanks for the reply. I basically need to load a IR sample in a AudioSampleBuffer and stretch it according to the host samplerate and a stretch factor.

I tried the LagrangeInterpolator but there's something wrong with my implementation as it doesn't do exactly what it should (or at least what I think it should do).&nbsp;

When then I use the new buffer for the convolution if the stretch is \< 1 it looks like the sample is slower (while it should be faster) and usually when it's \> 1 i get strange repetitions only on left channel (and sometimes a huge click/pop and no audio anymore).

I am probably overthinking here, but I just need to change the speed of this AudioSampleBuffer according to the host samplerate and a stretch factor.

&nbsp;

---

<div class="post-metadata">

### Author: ![lapsang](https://avatars.discourse-cdn.com/v4/letter/l/3be4f8/32.png) [@lapsang](https://forum.juce.com/u/lapsang)
#### Post date: [February 15, 2015, 12:27am UTC](https://forum.juce.com/t/resampling-an-audiosamplebuffer/14287/4 "2015-02-15T00:27:33Z")

</div>

\*bump\*

Any help with this?

&nbsp;

---

<div class="post-metadata">

### Author: ![DaveH](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daveh/32/4924_2.png) [@DaveH](https://forum.juce.com/u/DaveH)
#### Post date: [September 15, 2017, 5:41pm UTC](https://forum.juce.com/t/resampling-an-audiosamplebuffer/14287/5 "2017-09-15T17:41:12Z")

</div>

Here’s my interpretation of how to use the LagrangeInterpolator, but unfortunately it crashes with an exception on the second channel, any idea why? I really need this to work.  
waveBuffer (AudioSampleBuffer) is the destination buffer that’s in the class…

```
void Buffer::fillBuffer(AudioFormatReader *reader, double currentSampleRate)
 {
	AudioSampleBuffer temp;

	waveBuffer.clear();
	temp.clear();
	
	double ratio = currentSampleRate / reader->sampleRate;

	temp.setSize((int)reader->numChannels, (int)reader->lengthInSamples);
	waveBuffer.setSize((int)reader->numChannels, (int)((double)reader->lengthInSamples * ratio));

	reader->read(&temp, 0, (int)reader->lengthInSamples, 0, true, true);

	ScopedPointer<LagrangeInterpolator> resampler = new LagrangeInterpolator();

	const float **inputs = temp.getArrayOfReadPointers();
	float **outputs = waveBuffer.getArrayOfWritePointers();
	for (int c = 0; c < waveBuffer.getNumChannels(); c++)
	{
		resampler->reset();
		resampler->process(ratio, inputs[c], outputs[c], waveBuffer.getNumSamples());
	}
}
```

---

<div class="post-metadata">

### Author: ![DaveH](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daveh/32/4924_2.png) [@DaveH](https://forum.juce.com/u/DaveH)
#### Post date: [September 15, 2017, 7:35pm UTC](https://forum.juce.com/t/resampling-an-audiosamplebuffer/14287/6 "2017-09-15T19:35:18Z")

</div>

OK I’ve discovered the ratio was 1/ the correct value but it still crashes when down-sampling, it seems to work fine when up-sampling. Is the LagrangeAlgorithm code broken?  
The ‘in’ gets unreadable here:-

```
 else
    {
        for (int i = numOut; --i >= 0;)
        {
            while (pos < actualRatio)
            {
                pushInterpolationSample (lastInputSamples, *in++);
                pos += 1.0;
            }

            pos -= actualRatio;
            *out++ = InterpolatorType::valueAtOffset (lastInputSamples, jmax (0.0f, 1.0f - (float) pos));
        }
    }
```

---

<div class="post-metadata">

### Author: ![DaveH](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daveh/32/4924_2.png) [@DaveH](https://forum.juce.com/u/DaveH)
#### Post date: [September 15, 2017, 8:16pm UTC](https://forum.juce.com/t/resampling-an-audiosamplebuffer/14287/7 "2017-09-15T20:16:21Z")

</div>

OK I’ve got it now, there is no filtering in the Lagrange code but ho-hum, here’s the new version:  
\*Note I now divide the incoming length by the ratio.

```
AudioSampleBuffer temp;

sofar = 0;
waveBuffer.clear();
temp.clear();

double ratio = reader->sampleRate / hostSampleRate;

temp.setSize((int)reader->numChannels, (int)reader->lengthInSamples);
waveBuffer.setSize((int)reader->numChannels, (int)(((double)reader->lengthInSamples) / ratio));

reader->read(&temp, 0, (int)reader->lengthInSamples, 0, true, true);

ScopedPointer<LagrangeInterpolator> resampler = new LagrangeInterpolator();

const float **inputs = temp.getArrayOfReadPointers();
float **outputs = waveBuffer.getArrayOfWritePointers();
for (int c = 0; c < waveBuffer.getNumChannels(); c++)
{
	resampler->reset();
	resampler->process(ratio, inputs[c], outputs[c], waveBuffer.getNumSamples());
}
```

---

<div class="post-metadata">

### Author: ![WilliamkWusik](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/williamkwusik/32/3821_2.png) [@WilliamkWusik](https://forum.juce.com/u/WilliamkWusik)
#### Post date: [August 5, 2019, 7:52pm UTC](https://forum.juce.com/t/resampling-an-audiosamplebuffer/14287/8 "2019-08-05T19:52:54Z")

</div>

Just saw this today. 😉 The code works great, BUT, for each channel you need its own Lagrange object.

`if you're resampling multiple channels, make sure each one uses its own object.`

---

<div class="post-metadata">

### Author: ![DaveH](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daveh/32/4924_2.png) [@DaveH](https://forum.juce.com/u/DaveH)
#### Post date: [August 10, 2019, 9:32am UTC](https://forum.juce.com/t/resampling-an-audiosamplebuffer/14287/9 "2019-08-10T09:32:59Z")

</div>

Yes of course. 🙂
