我已经在 portaudio 中做了一些淡入或淡出效果,但这不能正常工作。
我的回调函数:
int SoundEngine::patestCallback(const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo, PaStreamCallbackFlags statusFlags, void *userData)
{
paData *callData = (paData*)userData;
float *out = (float*)outputBuffer;
float sample;
unsigned long i;
(void) timeInfo; /* Prevent unused variable warnings. */
(void) statusFlags;
(void) inputBuffer;
for( i=0; i < framesPerBuffer; i++ )
{
sample = callData->sine[callData->phase++];
*out++ = sample;
if( callData->phase >= SE_TABLE_SIZE ) callData->phase -= SE_TABLE_SIZE;
}
return paContinue;
}
我发现对于整个流,通过应用这样的东西很容易制作淡出效果:
if(callData->fadeFlag == true)
{
callData->toneCountdown--;
sample *= (-cos(((float)(callData->toneCountdown - (callData->toneLength - callData->fadeOut)) / (float)callData->fadeOut) * M_PI) + 1.) * .5;
}
但这对我不起作用。如果我现在有多个声音,所有声音都会消失。目前我正在使用上面写的回调函数。callData->sine
开头只包含 0 不产生任何声音。我通过调用成员函数来添加声音playSound()
。我在单线程中执行此操作 - 所以我可以同时使用很多这些功能 - 发出多种声音。工作功能如何playSound
?它是:
void SoundEngine::playSound(int instrument, int freq, int duration)
{
if(instrument == 0)
{
for(int j=0; j<20; j++)
{
for(int i=0; i<SE_TABLE_SIZE; i++)
{
data.sine[i] += 0.01*sin(2 * M_PI * data.soundPitch[freq] * ((double)i/(double)SAMPLE_RATE));
}
}
Pa_Sleep(duration);
for(int j=0; j<20; j++)
{
for(int i=0; i<SE_TABLE_SIZE; i++)
{
data.sine[i] -= 0.01*sin(2 * M_PI * data.soundPitch[freq] * ((double)i/(double)SAMPLE_RATE));
}
}
}
}
您现在可能会问:“那个人在这里做了什么?”。我知道这不是编码的杰作,但我是声音编程的新手,所以我正在努力学习它。现在我只想让它像它应该工作的那样工作。
函数 playSound() 运行良好,但淡入/淡出在淡入淡出时会产生一些噪音。淡化效果是由for(int j=0; j<20; j++)
逐渐增大我的声音产生的。我认为修改data.sine
数据会引起这些噪音。当我减慢淡入淡出效果时 - 它工作得很好,但太慢了。我需要快速淡入/淡出效果来消除声音开始和结束时的噪音。
有人可以帮我将淡入/淡出效果应用于我的示例吗?