c - 使用 PortAudio 录音 : Pa_GetStreamReadAvailable not work?

标签 c linux ubuntu real-time portaudio

我使用 C 和 Linux 制作实时数据处理应用程序(使用 fftw3 库的频谱分析仪)。我的输入数据目前取自硬件音频线路输入。我使用 PortAudio 库与硬件对话。 (目前我使用 PortAudio 的回调)。我选择 Portaudio,因为它有很多录音示例。 RtAudio,虽然可以提供较低的延迟,但不幸的是,它是用 CPP 编写的,而不是 C(所以我有多个可移植性问题)。 (我应该尝试其他包装器吗?有没有直接的方法来捕获声音缓冲区,有例子吗?)。

除非 DFT 计算花费的时间足以用新数据填充音频缓冲区,否则我的工作设置很好。因此数据在系统的某个地方停留和积累,音频输入和图像之间的延迟发生并增加。在频谱分析中,不可能“丢弃”数据。所以我唯一能做的就是警告用户 CPU 马力低。但是这里我有问题。

Pa_GetStreamReadAvailable 函数用来显示有多少未读数据可用。但这对我来说根本不起作用。我准备了一个简单的例子,主要基于文件 www.kfr.co.il/files/speed_photo/complete.c

#include <sys/ioctl.h>
#include <linux/parport.h>
#include <linux/ppdev.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <portaudio.h>

/* #define SAMPLE_RATE  (17932) // Test failure to open with this value. */
#define SAMPLE_RATE  (44100)
#define FRAMES_PER_BUFFER (1024)
#define NUM_SECONDS     (5)
#define NUM_CHANNELS    (2)
/* #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

int running = 1;

void signalHandler(int sig)
{
   running = 0;
}


/*******************************************************************/
int main(void);
int main(void)
{
    printf("Initializing PortAudio...\n");
    PaStreamParameters inputParameters, outputParameters;
    PaStream *stream;
    PaError err;
    SAMPLE *recordedSamples;
    int i;
    int maxFrames;
    int numSamples;
    int numBytes;
    SAMPLE max, average, val;

    // Set ctrl-c handler
    signal(SIGINT, signalHandler);

    //totalFrames = NUM_SECONDS * SAMPLE_RATE; /* Record for a few seconds. */
    maxFrames = SAMPLE_RATE*1;
    numSamples = maxFrames * NUM_CHANNELS;

    numBytes = numSamples * sizeof(SAMPLE);
    recordedSamples = (SAMPLE *) malloc( numBytes );
    if( recordedSamples == NULL )
    {
        printf("Could not allocate record array.\n");
        exit(1);
    }
    for( i=0; i<numSamples; i++ ) recordedSamples[i] = 0;

    err = Pa_Initialize();
    if( err != paNoError ) goto error;

    inputParameters.device = Pa_GetDefaultInputDevice(); /* default input device */
    if (inputParameters.device == paNoDevice) {
      fprintf(stderr,"Error: No default input device.\n");
      goto error;
    }
    inputParameters.channelCount = NUM_CHANNELS;
    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 */
              NULL, /* no callback, use blocking API */
              NULL ); /* no callback, so no callback userData */
    if( err != paNoError ) goto error;

    printf("Starting!\n\n");

    printf("Numbers should increasing:\n");

    err = Pa_StartStream( stream );
    if( err != paNoError ) goto error;

    Pa_ReadStream(stream, recordedSamples, maxFrames);
    i = 1;
    while (i<8)
    {
        long toRead = Pa_GetStreamReadAvailable(stream);
        printf("%ld %d\n", toRead, maxFrames);
        if (toRead > maxFrames)
            toRead = maxFrames;
        err = Pa_ReadStream(stream, recordedSamples, toRead);
        if( err != paNoError ) goto error;

        //  Here is place for heavy calculations,
        // they can be longer than time needed for filling one buffer.
        // (So data, awaiting for processing, should be (and really is)
        // accumulated somewhere in system/OS buffer.)
        //  Emulate big delays:
        usleep(i*1000000);
        i++;
    }

    printf("Stopping PortAudio...\n");
    err = Pa_CloseStream( stream );
    if( err != paNoError ) goto error;

    free( recordedSamples );

    Pa_Terminate();
    return 0;

error:
    Pa_Terminate();
    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 ) );
    return -1;
}

我预计打印输出中的数字会增加,但我的结果显然是错误的:

598 44100
3071 44100
3071 44100
3071 44100
3071 44100
3071 44100
3071 44100

使用“Pa_OpenDefaultStream”代替“Pa_OpenStream”给出其他错误数字 (8191)。 我哪里错了?

或者它是 PA 中的错误,但可以肯定的是,在提交错误报告之前,我更愿意先询问。 谢谢。

附言将 PA 库回归到以前的版本(用于测试)是不可能的,我无法在现代 Ubuntu 中用它编译这个例子。

最佳答案

我不清楚这里有一个错误(除了你在你的盒子上做一个 FFT 花费的时间太长)。

通常音频子系统有少量缓冲区(在您的情况下看起来像 3 3072 是 3 * 1024,您将其设置为 FRAMES_PER_BUFFER,2 是另一个常见值),如果您未能跟上,它只会丢弃来自最近最少填充的缓冲区,没有不断增长的音频缓冲区。

您有责任及时将数据从这些缓冲区中复制出来,并在 ram 或磁盘上进行缓冲,这是您需要为您的应用程序做的事情。

我有点惊讶现代机器在音频速率下进行 1024 点 FFT 有任何问题。

问候,丹。

关于c - 使用 PortAudio 录音 : Pa_GetStreamReadAvailable not work?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37470911/

相关文章:

C程序打不开文件

c - 将 fork 与 setjmp/longjmp 结合使用

linux - shell map 是否限制 key 格式?

linux - Shell 脚本在 Ubuntu 中成功但在 Amazon Linux 2 中失败?

ubuntu - Microsoft Excel Writer 步骤意外错误 - Pentaho Data Integration 4.4.0

oracle - 在 Linux 的 Windows 子系统上安装 Oracle Instant Client

c - 如何找到二叉搜索树第 x 层的节点数(有限制)

javascript - 用 javascript 编写的 FD_SET 和 FD_ISSET 宏

linux - "qemu-system-riscv: command not found"

c - 如何通过指针访问全局定义的 typedef 结构?