我正在将端口音频与现有的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允许在回调中有更大的延迟,从而避免了嗡嗡声。

08-16 18:04