为什么使用 memcpy 复制到 portaudio 输入缓冲区会产生噪音?

Why does copying to portaudio input buffer with memcpy is creating noise?

我只是想在回调函数中复制 portaudio 的输入缓冲区。那是我的回调:

static int recordCallback( const void *inputBuffer, void *outputBuffer,
                           unsigned long framesPerBuffer,
                           const PaStreamCallbackTimeInfo* timeInfo,
                           PaStreamCallbackFlags statusFlags,
                           void *userData )
{
   
    paTestData *data = (paTestData*)userData;
    const SAMPLE *rptr = (const SAMPLE*)inputBuffer;
    SAMPLE *wptr = &data->recordedSamples[data->frameIndex * NUM_CHANNELS];
    long framesToCalc;
    long i;
    int finished;
    unsigned long framesLeft = data->maxFrameIndex - data->frameIndex;
    memcpy(&circularBuffer[data->write], inputBuffer, 1024); // Assuming samplesPerFrame = FRAME_SIZE
    data->write = data->write + 1024;
    (void) outputBuffer; /* Prevent unused variable warnings. */
    (void) timeInfo;
    (void) statusFlags;
    (void) userData;

    if( framesLeft < framesPerBuffer )
    {
        framesToCalc = framesLeft;
        finished = paComplete;
    }
    else
    {
        framesToCalc = framesPerBuffer;
        finished = paContinue;
    }
    data->frameIndex += framesToCalc;
    return finished;
}

这是来自 portaudio 示例的原始回调:

static int recordCallback( const void *inputBuffer, void *outputBuffer,
                               unsigned long framesPerBuffer,
                               const PaStreamCallbackTimeInfo* timeInfo,
                               PaStreamCallbackFlags statusFlags,
                               void *userData )
    {
     
        paTestData *data = (paTestData*)userData;
        const SAMPLE *rptr = (const SAMPLE*)inputBuffer;
        SAMPLE *wptr = &data->recordedSamples[data->frameIndex * NUM_CHANNELS];
        long framesToCalc;
        long i;
        int finished;
        unsigned long framesLeft = data->maxFrameIndex - data->frameIndex;
    
        (void) outputBuffer; /* Prevent unused variable warnings. */
        (void) timeInfo;
        (void) statusFlags;
        (void) userData;
    
        if( framesLeft < framesPerBuffer )
        {
            framesToCalc = framesLeft;
            finished = paComplete;
        }
        else
        {
            framesToCalc = framesPerBuffer;
            finished = paContinue;
        }
    
        if( inputBuffer == NULL )
        {
            for( i=0; i<framesToCalc; i++ )
            {
                *wptr++ = SAMPLE_SILENCE;  /* left */
                if( NUM_CHANNELS == 2 ) *wptr++ = SAMPLE_SILENCE;  /* right */
            }
        }
        else
        {
            for( i=0; i<framesToCalc; i++ )
            {
                *wptr++ = *rptr++;  /* left */
                if( NUM_CHANNELS == 2 ) *wptr++ = *rptr++;  /* right */
            }
        }
        data->frameIndex += framesToCalc;
       

 return finished;
}

我只是使用 memcpy 而不是迭代指针。但是我录制的声音有很多噪音。我无法弄清楚问题所在。也许,我错过了频道(2 个频道),但我不确定。你有什么想法吗?

您可以从 here

获取完整代码

该方法是正确的,但我无法正确指定帧大小

而不是这个:

memcpy(&circularBuffer[data->write], inputBuffer, 1024); // 1024 = FRAME_SIZE

这样会更好:

memcpy(&circularBuffer[data->write], inputBuffer, 1024*sizeof(float)); // Assuming 1024 = FRAME_SIZE

因为:

typedef float SAMPLE; 是 4 个字节。