Samples Tuning

What's the best way of tuning a sample in SamplerSound? I'm using several SamplerSounds and I need to tune each one individually and in real time (both tune/finetune). I copied the SamplerSound/SamplerVoice classes and played a bit with them, changing the pitchRatio worked for all sounds (but every now and then it crashes in the interpolation). I guess I can use an array but still there will be problem in the interpolation, don't know why. 

I also tried changing the samplerate but everytime the sound stops and I need something in real time.

Thanks.

With a lot of caveats (see the comments) you can do something like this....

template <typename FloatType>

FloatType cubicInterpolation(const FloatType y0, const FloatType y1,

                             const FloatType y2, const FloatType y3,

                             const FloatType alpha)

{

    const FloatType alpha2 = alpha * alpha;


    const FloatType a0 = y3 - y2 - y0 + y1;

    const FloatType a1 = y0 - y1 - a0;

    const FloatType a2 = y2 - y0;

    const FloatType a3 = y1;

    

    return (a0 * alpha * alpha2) + (a1 * alpha2) + (a2 * alpha) + a3;

}

   
 /* This function is not efficient. */

    const float getSampleWithCubicInterpolation(int chan, double pos) {

        const int posFloor = pos;

        const float alpha = pos - posFloor;

        const float * sdata = channelData[chan];

        const int maxSamples = buffer->getNumSamples();


        if (posFloor == 0)

        {

            return sdata[0];

        }

        else

        if (posFloor >= (maxSamples - 3))

        {

            return sdata[maxSamples-1]; /* This is not right. */

        }

        else

        {

            return cubicInterpolation(sdata[posFloor-1], sdata[posFloor], sdata[posFloor+1], sdata[posFloor+2], alpha);

        }

    }

Thanks for the reply. I'm not really sure what I should do with that code. Should I replace the linear interpolation in the juce class? 

Also still trying to figure out the best way of handling this for multiple sounds.

Ah - maybe I"m solving a problem you've not got - that wasn't a useful reply from me.

So, to solve for samplerSound you need to control this calcuation: 

        pitchRatio = pow (2.0, (midiNoteNumber - sound->midiRootNote) / 12.0)

                        * sound->sourceSampleRate / getSampleRate();

I don't immediately see a way of doing that without re-writing portions of SamplerSound.  So perhaps take it as a template and make your own version. Which you've already done.  And it causes a crash when you modify pitchRatio.

Now there's a test immediately after the pitchRatio is used: 

            if (sourceSamplePosition > playingSound->length)

            {

                stopNote (false);

                break;

            }

Which should stop you reading past  the end of the buffer - the most likely reason for the crash.  Jules seems to have included some padding in the buffer to stop the edge condition crashing. 

I think you need to look into your crash with the debugger, and ignore my previous hasty reply!   Do you have valid values for pos, for example, when it crashes.

 

Oh! For some stupid reason I moved the lenght test BEFORE changing the pitchRatio. I didn't realize it. Thanks for the input.

I also solved the pitch for each sound using an array with something like this for the index: playingSound->getName().getIntValue()

So I'm basically using the sound name as index, not very elegant but it works.