gpt4 book ai didi

c - PortAudio 对连续输入流的实时音频处理

转载 作者:太空狗 更新时间:2023-10-29 16:10:31 26 4
gpt4 key购买 nike

我正在使用 PortAudio 来实现实时音频处理。

我的首要任务是连续采集mic数据,提供100个样本进行处理(each FRAME = 100 samples at a time)到其他一些处理线程。

这是我的回调,每次连续收集 100 个样本 -

static int paStreamCallback( const void* input, void* output,
unsigned long samplesPerFrame,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void* userData ) {

paTestData *data = (paTestData*) userData;
const SAMPLE *readPtr = (const SAMPLE*)input; // Casting input read to valid input type SAMPLE (float)
unsigned long totalSamples = TOTAL_NUM_OF_SAMPLES ; // totalSamples = 100 here

(void) output;
(void) timeInfo;
(void) statusFlags;

static int count = 1;

if(data->sampleIndex < count * samplesPerFrame){
data->recordedSample[data->sampleIndex] = *readPtr;
data->sampleIndex++;
}

else if(data->sampleIndex == count * samplesPerFrame){

processSampleFrame(data->recordedSample);
count++;
finished = paContinue;
}

return finished;
}

我的`主要功能-

int main(void){

// Some Code here

data.sampleIndex = 0;
data.frameIndex = 1;

numBytes = TOTAL_NUM_OF_SAMPLES * sizeof(SAMPLE);
data.recordedSample = (SAMPLE*)malloc(numBytes);

for(i=0; i < TOTAL_NUM_OF_SAMPLES; i++)
data.recordedSample[i] = 0;

// Open Stream Initialization

err = Pa_StartStream(stream);

/* Recording audio here..Speak into the MIC */
printf("\nRecording...\n");
fflush(stdout);

while((err = Pa_IsStreamActive(stream)) == 1)
Pa_Sleep(10);

if(err < 0)
goto done;

err = Pa_CloseStream(stream);

// Some more code here
}

将 100 个样本的每一帧发送到 processSampleFrame。

void processSampleFrame(SAMPLE *singleFrame){

// Free buffer of this frame
// Processing sample frame here
}

挑战在于我需要实现一种方式来显示时间 processSampleFrame正在处理 sample ,我的callBack应该活跃并继续获取下一个 Frame of 100 samples和(可能更多取决于 processSampleFrame 的处理时间)。

此外,缓冲区应该能够在将帧传递给 processSampleFrame 后尽快释放自己。 .

编辑 2:

我尝试用 PaUtilRingBuffer 来实现那PortAudio提供。这是我的回调

printf("Inside callback\n");
paTestData *data = (paTestData*) userData;

ring_buffer_size_t elementsWritable = PaUtil_GetRingBufferWriteAvailable(&data->ringBuffer);
ring_buffer_size_t elementsToWrite = rbs_min(elementsWritable, (ring_buffer_size_t)(samplesPerFrame * numChannels));

const SAMPLE *readPtr = (const SAMPLE*)input;
printf("Sample ReadPtr = %.8f\n", *readPtr);
(void) output; // Preventing unused variable warnings
(void) timeInfo;
(void) statusFlags;

data->frameIndex += PaUtil_WriteRingBuffer(&data->ringBuffer, readPtr, elementsToWrite);

return paContinue;

主要:

int main(void){

paTestData data; // Object of paTestData structure

fflush(stdout);

data.frameIndex = 1;

long numBytes = TOTAL_NUM_OF_SAMPLES * LIMIT;
data.ringBufferData = (SAMPLE*)PaUtil_AllocateMemory(numBytes);
if(PaUtil_InitializeRingBuffer(&data.ringBuffer, sizeof(SAMPLE), ELEMENTS_TO_WRITE, data.ringBufferData) < 0){
printf("Failed to initialise Ring Buffer.\n");
goto done;

err = Pa_StartStream(stream);

/* Recording audio here..Speak into the MIC */
printf("\nRecording samples\n");
fflush(stdout);

while((err = Pa_IsStreamActive(stream)) == 1)
Pa_Sleep(10);

if(err < 0)
goto done;

err = Pa_CloseStream(stream);

// Error Handling here
}

PaTestData结构:

typedef struct{

SAMPLE *ringBufferData;
PaUtilRingBuffer ringBuffer;
unsigned int frameIndex;
}
paTestData;

由于无法使用任何 free,成功获取分配的空间后,我面临同样的段错误问题在callback根据 PortAudio 的建议文档。

我在哪里可以释放分配给处理线程的帧缓冲区。可能是一种获取线程同步的方法在这里真的很有用。感谢您的帮助。

最佳答案

处理音频输入的示例代码:

#define FRAME_SIZE                 1024
#define CIRCULAR_BUFFER_SIZE (FRAME_SIZE * 4)

float buffer[CIRCULAR_BUFFER_SIZE];

typedef struct {
int read, write;
float vol;
} paData;

static int paStreamCallback(const void* input, void* output,
unsigned long samplesPerFrame,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void* userData) {

paData *data = (paData *)userData;
// Write input buffer to our buffer: (memcpy function is fine, just a for loop of writing data
memcpy(&buffer[write], input, samplesPerFrame); // Assuming samplesPerFrame = FRAME_SIZE

// Increment write/wraparound
write = (write + FRAME_SIZE) % CIRCULAR_BUFFER_SIZE;

// dummy algorithm for processing blocks:
processAlgorithm(&buffer[read]);

// Write output
float *dummy_buffer = &buffer[read]; // just for easy increment
for (int i = 0; i < samplesPerFrame; i++) {
// Mix audio input and processed input; 50% mix
output[i] = input[i] * 0.5 + dummy_buffer[i] * 0.5;
}

// Increment read/wraparound
read = (read + FRAME_SIZE) % CIRCULAR_BUFFER_SIZE;

return paContinue;
}

int main(void) {
// Initialize code here; any memory allocation needs to be done here! default buffers to 0 (silence)
// initialize paData too; write = 0, read = 3072
// read is 3072 because I'm thinking about this like a delay system.
}

对此持保留态度;这样做显然是更好的方法,但它可以用作起点。

关于c - PortAudio 对连续输入流的实时音频处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44645466/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com