将音频流写入循环缓冲区但分段错误读取值

Posted

技术标签:

【中文标题】将音频流写入循环缓冲区但分段错误读取值【英文标题】:Writing audio stream to circular buffer but Segmentation error reading value 【发布时间】:2021-06-05 11:03:04 【问题描述】:

我非常新的音频处理概念和 C++ 的基本知识,我的最终目标是创建一种方法来测量线路输入音频的谐波失真;我的想法是将音频输入流写入循环缓冲区,然后从循环缓冲区读取到 FFT 函数;从 FFT 数据中我可以锻炼音频失真。

到目前为止,我正在使用 Portaudio 将流式音频从其流式输入回调写入循环缓冲区;我创建了一个 Thread 来检查循环缓冲区中是否有任何数据,如果有数据,则将其读入临时缓冲区。作为一种简单测试的方法,我只是在回显要从循环缓冲区中读取的元素数量,这是可行的,但是大约一秒钟后,会为“可用的读取元素”(18446744073709542400) 产生不正确的值接着是分段错误;示例输出和我的代码如下:

+Read available 0
Stream started
+Read available 8192
>>dataIndex: 8192
+Read available 2048
>>dataIndex: 10240
+Read available 0
+Read available 9216
>>dataIndex: 19456
+Read available 18446744073709542400
Segmentation fault: 11

在理解上述非常大的读取值 (18446744073709542400) 和分段错误的原因方面提供任何帮助;我的想法是可能是因为循环缓冲区读取是在单独的线程中完成的;但是使用的循环缓冲区确实声明其线程安全。

ringbuffer.hpp

ma​​in.cpp

#include <array>
#include <stdio.h>
#include <thread>
#include "ringbuffer.hpp"
#include "portaudio.h"


/* #define SAMPLE_RATE  (17932) // Test failure to open with this value. */
#define SAMPLE_RATE  (44100)
#define FRAMES_PER_BUFFER (512)
#define NUM_SECONDS     (1)
#define NUM_CHANNELS    (2)
#define NUM_WRITES_PER_BUFFER   (4)
#define DITHER_FLAG     (paDitherOff)
//#define DITHER_FLAG     (0)


/* Select sample format. */
#if 1
#define PA_SAMPLE_TYPE  paFloat32
typedef float SAMPLE;
#define SAMPLE_SILENCE  (0.0f)
#define PRINTF_S_FORMAT "%.8f"
#elif 1
#define PA_SAMPLE_TYPE  paInt16
typedef short SAMPLE;
#define SAMPLE_SILENCE  (0)
#define PRINTF_S_FORMAT "%d"
#elif 0
#define PA_SAMPLE_TYPE  paInt8
typedef char SAMPLE;
#define SAMPLE_SILENCE  (0)
#define PRINTF_S_FORMAT "%d"
#else
#define PA_SAMPLE_TYPE  paUInt8
typedef unsigned char SAMPLE;
#define SAMPLE_SILENCE  (128)
#define PRINTF_S_FORMAT "%d"
#endif


static unsigned long int rbs_min(unsigned long int a, unsigned long int b)

    return (a < b) ? a : b;


typedef struct

    unsigned long int frameIndex;
    unsigned long int dataIndex;
    unsigned long int maxFrameIndex;
    int threadSyncFlag;
    SAMPLE *sampleData;
    const SAMPLE* buff;
    Ringbuffer<const SAMPLE*, 65536> ringBuffer;
    void *threadHandle;

paTestData;


static int cons(void* ptr) 
    unsigned long int ra_i;

    paTestData* pData = (paTestData*)ptr;

    /* Mark thread started */
    pData->threadSyncFlag = 0;

    while(1) 

        ra_i = pData->ringBuffer.readAvailable();
        printf("+Read available %lu\n",ra_i);

        if ( (pData->dataIndex <= 65536) )
        
            if (! pData->ringBuffer.isEmpty()) 
                pData->dataIndex += pData->ringBuffer.readBuff(&pData->buff,ra_i);
                printf(">>dataIndex: %lu\n",pData->dataIndex);
            


         else
        
            break;
        

        Pa_Sleep(100);

    

    pData->threadSyncFlag = 0;

    return 0;



static unsigned NextPowerOf2(unsigned val)

    val--;
    val = (val >> 1) | val;
    val = (val >> 2) | val;
    val = (val >> 4) | val;
    val = (val >> 8) | val;
    val = (val >> 16) | val;
    return ++val;


/* This routine will be called by the PortAudio engine when audio is needed.
** It may be called at interrupt level on some machines so don't do anything
** that could mess up the system like calling malloc() or free().
*/
static int recordCallback( const void *inputBuffer, void *outputBuffer,
                           unsigned long framesPerBuffer,
                           const PaStreamCallbackTimeInfo* timeInfo,
                           PaStreamCallbackFlags statusFlags,
                           void *userData )


    paTestData *data = (paTestData*)userData;

    unsigned long int elementsWriteable = data->ringBuffer.writeAvailable();

    unsigned long int elementsToWrite = rbs_min( elementsWriteable, (unsigned long int)(framesPerBuffer * NUM_CHANNELS) );

    SAMPLE *rptr = (SAMPLE*)inputBuffer;


    (void) outputBuffer; /* Prevent unused variable warnings. */
    (void) timeInfo;
    (void) statusFlags;
    (void) userData;

    data->frameIndex += data->ringBuffer.writeBuff( &rptr, elementsToWrite);

    return paContinue;





PaError pa_term(PaError err) 
    Pa_Terminate();
    if( err != paNoError )
    
        fprintf( stderr, "An error occured while using the portaudio stream\n" );
        fprintf( stderr, "Error number: %d\n", err );
        fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
        err = 1;          // Always return 0 or 1, but no other return codes.
    
    return err;



int main(void);

int main(void) 

    PaStreamParameters  inputParameters,
                        outputParameters;
    PaStream*           stream;
    PaError             err = paNoError;
    paTestData          data = 0;
    unsigned            delayCntr;
    unsigned long int   totalFrames;
    unsigned            numSamples;
    unsigned            numBytes;

    data.dataIndex = 0;

    err = Pa_Initialize();
    if( err != paNoError )
    
        pa_term(err);
    

    inputParameters.device = Pa_GetDefaultInputDevice(); /* default input device */
    if (inputParameters.device == paNoDevice) 
        printf("Error: No default input device.\n");
        pa_term(paDeviceUnavailable);
    

    inputParameters.channelCount = 2;                    /* stereo input */
    inputParameters.sampleFormat = PA_SAMPLE_TYPE;
    inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency;
    inputParameters.hostApiSpecificStreamInfo = NULL;

    /* Record some audio. -------------------------------------------- */
    err = Pa_OpenStream(
              &stream,
              &inputParameters,
              NULL,                  /* &outputParameters, */
              SAMPLE_RATE,
              FRAMES_PER_BUFFER,
              paClipOff,      /* we won't output out of range samples so don't bother clipping them */
              recordCallback,
              &data );
    if( err != paNoError )
    
        pa_term(err);
    

    // Start stream logging thread
    std::thread first (cons, &data);


    err = Pa_StartStream( stream );
    if( err != paNoError )
    
        pa_term(err);
    

    printf("Stream started\n");

    while(!paNoError) 
        Pa_Sleep(1);
    

    err = Pa_CloseStream( stream );
    if( err != paNoError )
    
        pa_term(err);
    


【问题讨论】:

【参考方案1】:

RingBuffer::readAvailable() 返回一个小的负数作为size_t。因为size_t 是一个无符号类型并且因为您在printf 中使用%lu,所以它被显示为好像它是一个巨大的unsigned long。 (不知何故,你的输出有多余的数字。)

RingBuffer 可能有错误。似乎假设尾部索引将始终小于或等于头部索引。我不是无锁代码中使用的内存排序约束方面的专家,但在我看来,当它读取头部和尾部以计算可用数量时,它可能是一个同步问题。

【讨论】:

以上是关于将音频流写入循环缓冲区但分段错误读取值的主要内容,如果未能解决你的问题,请参考以下文章

捕获从端口音频写入输出音频设备的音频输出

将流写入缓冲区对象

访问共享内存时出现分段错误

通过 TCP 套接字将音频写入服务器

由于无效写入导致的分段错误

循环遍历存储在 C 中缓冲区中的数据