0

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...

Stefan Dimeski
  • 438
  • 4
  • 16
  • Why are you casting to `int` all over the place? Why not use the correct type so there is no need to cast? – PaulMcKenzie Jun 21 '16 at 12:13
  • That's how they use it in their SoundStretch main.cpp file. Also I'm casting to `int` because `WavInFile` stores everything as `unsigned int` and `WavOutFile` expects `int` as parameter in constructor. Not necessary conversion but I don't think that's causing my problem. – Stefan Dimeski Jun 21 '16 at 12:23

0 Answers0