端口音频导致大声嗡嗡声 50% 的测试

Port Audio causing Loud Buzzing 50% of Tests

我正在将端口音频与现有的 MacOSX 应用程序集成。到目前为止,这是我的代码:

static int coreAudioCallback( const void *inputBuffer, void *outputBuffer,
                             unsigned long framesPerBuffer,
                             const PaStreamCallbackTimeInfo* timeInfo,
                             PaStreamCallbackFlags statusFlags,
                             void *userData ){
    /*(V3dAudioListener * pCallingListener = (V3dAudioListener*)userData;
    Aquila::SampleType *out = (Aquila::SampleType*)outputBuffer;
    pCallingListener->updateEQVals(out, framesPerBuffer);*/

    if (statusFlags & paInputOverflow) {
        printf("Input underflow");
    }

    if(statusFlags & paInputOverflow){
        printf("Input overflow");
    }
    if (statusFlags & paOutputUnderflowed) {
        printf("Output underflowed");
    }
    if (statusFlags & paOutputOverflow) {
        printf("Output overflow");
    }
    return 0;
}

void V3dAudioListener::start(){
    PaError error = Pa_Initialize();
    if(error != paNoError){
        throw std::invalid_argument("Failed to initialize port audio.");
    };

    PaStream *stream;
    /* Open an audio I/O stream. */
    error = Pa_OpenDefaultStream( &stream,
                                 0,          /* no input channels */
                                 2,          /* stereo output */
                                 paFloat32,  /* 32 bit floating point output */
                                 _sampleRate,
                                 _framesPerBuffer,        /* frames per buffer, i.e. the number
                                                           of sample frames that PortAudio will
                                                           request from the callback. Many apps
                                                           may want to use
                                                           paFramesPerBufferUnspecified, which
                                                           tells PortAudio to pick the best,
                                                           possibly changing, buffer size.*/
                                 coreAudioCallback, /* this is your callback function */
                                 this); /*This is a pointer that will be passed to
                                         your callback*/
    if( error != paNoError ){
        throw std::invalid_argument("Failed to set up port audio stream.");
    }

    error = Pa_StartStream(stream);
    if( error != paNoError ){
        throw std::invalid_argument("Failed to start port audio stream.");
    }

}

void V3dAudioListener::stop(){
    int error = Pa_Terminate();
    if(error != paNoError){
        throw std::invalid_argument("Failed to terminate port audio.");
    };
}

大约一半的时间代码是 运行,它会从输出音频设备产生很大的嗡嗡声。嗡嗡声不会在每个 运行 期间开始或结束,而是在初始启动时出现或不出现。是什么原因造成的?是否需要最小延迟以避免缓冲区溢出?在回调中放置一个 sleep(1) 似乎可以解决问题,除非在回调中完成额外的工作。

其他信息: OSX版本:10.11.3 _sampleRate:44100 _framesPerBuffer:256

解决方案是增加我们传递给输出流的 _framesPerBuffer 值。值 1024 允许在回调中有更大的延迟,从而防止嗡嗡声。