我试着用斯坦福的STK做一些实时的可波合成。我正在使用FMVoices to类1FMVoices.html,并试图在下面定义的回调例程中使用它。
int tick( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
double streamTime, RtAudioStreamStatus status, void *dataPointer )
{
FMVoices *FM = (FMVoices *) dataPointer;
register StkFloat *samples = (StkFloat *) outputBuffer;
for ( unsigned int i=0; i<nBufferFrames; i++ )
*samples++ = FM->tick();
return 0;
}我认为问题在于最后一个参数的类型。我得到一个运行时错误:"0xC0000005:执行位置0x000000001的访问冲突。“现在,这种回调应该是为其他STK乐器编写的,比如单簧管,甚至是FileLoop类,但是FMVoices有一些奇怪的地方。该对象被传递给openStream (它处理特定于平台的实时输出),作为一个指向void的指针。当系统的音频缓冲区已满时,会自动调用回调。实现此功能并为其他工具工作的代码片段如下所示:
int main()
{
// Set the global sample rate before creating class instances.
Stk::setSampleRate( 44100.0 );
RtAudio dac;
Instrmnt * instrument_FM;
int nFrames = 10000;
try {
instrument_FM = new FMVoices;
}
catch ( StkError & ) {
goto cleanup;
}
instrument_FM->setFrequency(440.0);
// Figure out how many bytes in an StkFloat and setup the RtAudio stream.
RtAudio::StreamParameters parameters;
parameters.deviceId = dac.getDefaultOutputDevice();
parameters.nChannels = 1;
RtAudioFormat format = ( sizeof(StkFloat) == 8 ) ? RTAUDIO_FLOAT64 : RTAUDIO_FLOAT32;
unsigned int bufferFrames = RT_BUFFER_SIZE;
try {
dac.openStream( ¶meters, NULL, format, (unsigned int)Stk::sampleRate(), &bufferFrames, &tick, (void *)&instrument_FM);
}
catch ( RtError &error ) {
error.printMessage();
Sleep(1000);
goto cleanup;}
nFrames的大小似乎没有影响。在我看来,这些类型的错误通常来自于引用指向空的指针。
发布于 2013-12-13 05:52:54
问题是,您正在获取指针的地址,并将其传递到openStream中。
// pointer to instrument
Instrmnt * instrument_FM;
// snip ...
// &instrument_FM is a pointer to a pointer! i.e. Instrmnt **
dac.openStream( ¶meters, /* other params */, (void *)&instrument_FM)最快的解决方案就是去掉该行中的&。
现在有一些关于C++的注释,以及对您的代码的更多修正。代码看起来像是C和Java的混合体,并打开了许多陷阱,其中一个导致了您的问题。
FMVoices。使用堆栈就像使用RtAudio dac一样。deleteFMVoices instrument_FM;
try/catch来进行清理,因为C++具有在作用域结束时触发并传播错误的析构函数。delete和清理操作。
goto中使用C++,它真的不需要。(与C语言不同,在C中,它可以用于清理)。
static_cast<>和reinterpret_cast<>,而不是C风格的。
以下是修改后的代码:
int main()
{
// Set the global sample rate before creating class instances.
Stk::setSampleRate( 44100.0 );
RtAudio dac;
FMVoices instrument_FM;
instrument_FM.setFrequency(440.0);
// Figure out how many bytes in an StkFloat and setup the RtAudio stream.
RtAudio::StreamParameters parameters;
parameters.deviceId = dac.getDefaultOutputDevice();
parameters.nChannels = 1;
RtAudioFormat format = ( sizeof(StkFloat) == 8 ) ? RTAUDIO_FLOAT64 : RTAUDIO_FLOAT32;
unsigned int bufferFrames = RT_BUFFER_SIZE;
// didn't get rid of this try since you want to print the error message.
try {
// note here i need the ampersand &, because instrument_FM is on the stack
dac.openStream( ¶meters, NULL, format, static_cast<unsigned int>(Stk::sampleRate()), &bufferFrames, &tick, reinterpret_cast<void*>(&instrument_FM));
}
catch ( RtError& error ) {
error.printMessage();
}
}https://stackoverflow.com/questions/20559530
复制相似问题