变速类RateTransposer的实现
回到SoundTouch类成员函数void SoundTouch::putSamples(const SAMPLETYPE *samples, uint nSamples)。定义一个SoundTouch类变量之后,通过简单地调用这个类函数,就可以实现音频的相关处理。分析一下他的调用形式,也很简单,第一个参数SAMPLETYPE *samples,指向一个以PCM编码的wave数据缓冲区,第二个参数uint nSamples,就是这个数据缓冲区包含的Sample个数,前面已经讨论过这个Sample的计算方法,这里就不再累述。
先看一下他的实现:
// Adds 'numSamples' pcs of samples from the 'samples' memory position into
// the input of the object.
void SoundTouch::putSamples(const SAMPLETYPE *samples, uint nSamples)
{
if (bSrateSet == FALSE)
{
throw std::runtime_error("SoundTouch : Sample rate not defined");
}
else if (channels == 0)
{
throw std::runtime_error("SoundTouch : Number of channels not defined");
}
#ifndef PREVENT_CLICK_AT_RATE_CROSSOVER
else if (rate <= 1.0f)
{
// transpose the rate down, output the transposed sound to tempo changer buffer
assert(output == pTDStretch);
pRateTransposer->putSamples(samples, nSamples);
pTDStretch->moveSamples(*pRateTransposer);
}
else
#endif
{
// evaluate the tempo changer, then transpose the rate up,
assert(output == pRateTransposer);
pTDStretch->putSamples(samples, nSamples);
pRateTransposer->moveSamples(*pTDStretch);
}
}
前面大致上可以看做是判断SoundTouch类初始化过程是否顺利,重点我们看一下
#ifndef PREVENT_CLICK_AT_RATE_CROSSOVER
else if (rate <= 1.0f)