Why is 4 added to length when constructing a SamplerSound?

Hi there,

In the Sampler constructor below (found in Sampler.cpp) could someone explain why the reading of the buffer involves length + 4 ? what is this 4 value ? why not just pass in length as is? Many thanks in advance.

SamplerSound::SamplerSound (const String& soundName,
                            AudioFormatReader& source,
                            const BigInteger& notes,
                            int midiNoteForNormalPitch,
                            double attackTimeSecs,
                            double releaseTimeSecs,
                            double maxSampleLengthSeconds)
    : name (soundName),
      sourceSampleRate (source.sampleRate),
      midiNotes (notes),
      midiRootNote (midiNoteForNormalPitch)
    if (sourceSampleRate > 0 && source.lengthInSamples > 0)
        length = jmin ((int) source.lengthInSamples,
                       (int) (maxSampleLengthSeconds * sourceSampleRate));

        data.reset (new AudioBuffer<float> (jmin (2, (int) source.numChannels), length + 4));

        source.read (data.get(), 0, length + 4, 0, true, true);

        params.attack  = static_cast<float> (attackTimeSecs);
        params.release = static_cast<float> (releaseTimeSecs);
1 Like

just guessing here, but I think there is some sort of interpolation going on which needs 4 samples (lagrange maybe). So it copies the first 4 samples as the last samples to keep the lookup code easy. not sure though!
anyway, magic numbers should be avoided, that “4” should have a name!

1 Like

I guessed that too, a bit odd though as it would be safer to limit the memory read and return 0.

true! Most samples will contain fades but who knows!