将音频流写入循环缓冲区但分段错误读取值
Writing audio stream to circular buffer but Segmentation error reading value
我对音频处理的概念非常新,并且具有 C++ 的基本知识,我的最终目标是创建一种测量线路输入音频谐波失真的方法;我的想法是将音频输入流写入循环缓冲区,然后从循环缓冲区读取到 FFT 函数;从 FFT 数据我可以锻炼音频失真。
到目前为止,我正在使用 Portaudio 将流式音频从其流式输入回调写入循环缓冲区;我创建了一个 Thread 来检查循环缓冲区中是否有任何数据,如果有数据,则将其读入临时缓冲区。作为一种简单测试的方法,我只是回显要从循环缓冲区读取的元素数量,这可行,但在一秒钟左右后,会为 'read elements available' (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
) 以及出现分段错误的原因,我们将不胜感激;我的想法是这可能是因为循环缓冲区读取是在一个单独的线程中完成的;然而,使用的循环缓冲区确实声明其线程安全。
main.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);
}
}
RingBuffer::readAvailable()
返回一个小的负数作为 size_t
。由于 size_t
是一个无符号类型,并且因为您在 printf
中使用了 %lu
,所以它被显示为一个巨大的 unsigned long
。 (不知何故你的输出有额外的数字。)
可能 RingBuffer
有错误。似乎假设尾部索引将始终小于或等于头部索引。我不是无锁代码中使用的内存排序约束方面的专家,但在我看来,当它读取头部和尾部以计算可用量时,这可能是一个同步问题。
我对音频处理的概念非常新,并且具有 C++ 的基本知识,我的最终目标是创建一种测量线路输入音频谐波失真的方法;我的想法是将音频输入流写入循环缓冲区,然后从循环缓冲区读取到 FFT 函数;从 FFT 数据我可以锻炼音频失真。
到目前为止,我正在使用 Portaudio 将流式音频从其流式输入回调写入循环缓冲区;我创建了一个 Thread 来检查循环缓冲区中是否有任何数据,如果有数据,则将其读入临时缓冲区。作为一种简单测试的方法,我只是回显要从循环缓冲区读取的元素数量,这可行,但在一秒钟左右后,会为 'read elements available' (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
) 以及出现分段错误的原因,我们将不胜感激;我的想法是这可能是因为循环缓冲区读取是在一个单独的线程中完成的;然而,使用的循环缓冲区确实声明其线程安全。
main.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);
}
}
RingBuffer::readAvailable()
返回一个小的负数作为 size_t
。由于 size_t
是一个无符号类型,并且因为您在 printf
中使用了 %lu
,所以它被显示为一个巨大的 unsigned long
。 (不知何故你的输出有额外的数字。)
可能 RingBuffer
有错误。似乎假设尾部索引将始终小于或等于头部索引。我不是无锁代码中使用的内存排序约束方面的专家,但在我看来,当它读取头部和尾部以计算可用量时,这可能是一个同步问题。