SoundTouch library messes up the ending when pitch-shifting

315 Views Asked by At

I'm using the SoundTouch library to pitch-shift some audio files. Everything works well, except the last few hundred milliseconds of the new audio file are not like the original file. Here is the original file: enter image description here

And here's what I get after pitch-shifting: enter image description here

As you can see the ending is not right. It's like there was nothing there in the original file, when there certainly is.

Here's the code I'm using:

int generateFile(WavInFile *file, SoundTouch *st, string fileName, int semitones)
{
  const bool speech = true; 

SAMPLETYPE samples[BUFF_SIZE];

WavOutFile *out = new WavOutFile(fileName.c_str(), (int)file->getSampleRate(), (int)file->getNumBits(), (int)file->getNumChannels());

int nChannels = (int)file->getNumChannels();

assert(nChannels > 0);

int num, nSamples;
int buffSizeSamples = BUFF_SIZE / nChannels;

st->setSampleRate((int)file->getSampleRate());
st->setChannels(nChannels);
st->setPitchSemiTones(semitones);

if (!speech)
{
    st->setSetting(SETTING_USE_QUICKSEEK, 0);
    st->setSetting(SETTING_USE_AA_FILTER, 0);
}
else
{
    st->setSetting(SETTING_USE_QUICKSEEK, 0);
    st->setSetting(SETTING_SEQUENCE_MS, 40);
    st->setSetting(SETTING_SEEKWINDOW_MS, 15);
    st->setSetting(SETTING_OVERLAP_MS, 8);
}



while (file->eof() == 0)
{
    num = file->read(samples, BUFF_SIZE);

    nSamples = num / (int)file->getNumChannels();

    st->putSamples(samples, nSamples);

    do 
    {
        nSamples = st->receiveSamples(samples, buffSizeSamples);
        out->write(samples, nSamples * nChannels);
    } while (nSamples != 0);
}


st->flush();

do 
{
    nSamples = st->receiveSamples(samples, buffSizeSamples);
    out->write(samples, nSamples * nChannels);
} while (nSamples != 0);

delete out;

return 0;

}

And yes, I delete WavInFile *file later in my code. So my question is- Why is SoundTouch doing this and how can I fix it?

Also I cannot simply cut the wrong part of the new audio file because I'm generating hundreds of files this way so cutting every single one of them would be...

0

There are 0 best solutions below