JUCE resampler

Programming applications for making music on Linux.

Moderators: MattKingUSA, khz

Post Reply
User avatar
peter.semiletov
Established Member
Posts: 119
Joined: Thu May 11, 2023 1:09 pm
Has thanked: 25 times
Been thanked: 82 times
Contact:

JUCE resampler

Post by peter.semiletov »

Hello! I’m completely stuck with the JUCE interpolators :(
Before that I used libsamplerate and I was happy.
I need to resample the buffer of floats from sound file’s samplerate to DAW session’s sample rate.
So I have the source buffer (loaded from the reader):

Code: Select all

juce::AudioBuffer<float> *buffer = new juce::AudioBuffer<float>;

If the buffer’s samplerate != session’s samplerate I need to resample.
My steps are.

Define the resamling ratio:

Code: Select all

float ratio = (float) session_samplerate / samplerate;

Calculate the output frames (samples) count for each channels:

Code: Select all

size_t output_frames_count = ratio * length_in_samples;

length_in_samples is the length of the source buffer, in samples, for the any of the channels.

I define the new buffer to write resampled data:

Code: Select all

juce::AudioBuffer<float> * output_buffer = new juce::AudioBuffer <float> (channels, output_frames_count);

Then I process:

Code: Select all

 for (int i = 0; i < channels; i++)
      {
       juce::LinearInterpolator interpolator;

    int result = interpolator.process (ratio,
                                        buffer->getReadPointer(i),
                                        output_buffer->getWritePointer(i),
                                        output_frames_count);
}

Sometime it works, but sound sounded as doubled/repeated, sometime I get just crashes..

tseaver
Established Member
Posts: 408
Joined: Mon Mar 13, 2017 6:07 am
Has thanked: 12 times
Been thanked: 102 times

Re: JUCE resampler

Post by tseaver »

Ob. disclaimer: I haven't done serious C / C++ development for years. Nevertheless, perhaps this might help:

Code: Select all

float ratio = (float) session_samplerate / samplerate;

You might want to coerce both values before dividing, e.g.:

Code: Select all

float ratio = ((float) session_samplerate) / ((float) samplerate);

Given that you are sometimes seeing crashes, rather than just mis-resampled values, my change won't likely fix anything.

Ubuntu, Mixbus32C; acoustic blues / country / jazz
User avatar
peter.semiletov
Established Member
Posts: 119
Joined: Thu May 11, 2023 1:09 pm
Has thanked: 25 times
Been thanked: 82 times
Contact:

Re: JUCE resampler

Post by peter.semiletov »

Thank you! But it does not help :) Actually, I started to use Speex resampler instead.

SocaLabs
Established Member
Posts: 33
Joined: Sat May 09, 2020 1:38 am
Been thanked: 12 times

Re: JUCE resampler

Post by SocaLabs »

LinearInterpolator has state, so you can't recreate it every time around the loop, it needs to be a member variable, and you need one per channel.

If you are getting crashes, maybe yo are reading off the end of your input buffer. There is another version of process where you also feed in remaining number of source samples.

User avatar
peter.semiletov
Established Member
Posts: 119
Joined: Thu May 11, 2023 1:09 pm
Has thanked: 25 times
Been thanked: 82 times
Contact:

Re: JUCE resampler

Post by peter.semiletov »

Finally, I took the resampler from Speex, it works in a same way as libsamplerate )

Post Reply