【问题标题】:PulseAudio API - No Microphone SignalPulseAudio API - 没有麦克风信号
【发布时间】:2013-02-15 20:22:18
【问题描述】:

我正在尝试使用 PulseAudio“实时”捕获麦克风信号。该程序是用 C 语言编写的,并为此使用 PulseAudio Simple API。不幸的是,我请求的音频缓冲区不包含任何信号。要么我的代码有问题,要么设备源无法识别。我使用 gstreamer 的 pulsesrc 和 pulsesink 在我的程序之外运行了一些测试,这些测试有效。我还测试了以下同样有效的命令:

parec -d alsa_input.usb-041e_30d3_121023000184-00-U0x41e0x30d3.analog-mono | sox -t raw -r 44100 -sLb 16 -c 2 - /home/roos/Arbeitsfläche/pulsetest.wav

在我的第二张卡上它也有效:

parec -d alsa_input.usb-Creative_Technology_Ltd_Sound_Blaster_X-Fi_Go__Pro_00173634-00-Pro_1.analog-stereo | sox -t raw -r 44100 -sLb 16 -c 2 - /home/roos/Arbeitsfläche/pulsetest.wav

这是应该可以工作的代码:

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <math.h>

#include <pulse/simple.h>
#include <pulse/error.h>

#define BUFSIZE 1024
#define SAMPLE_BITS 16

/* A simple routine calling UNIX write() in a loop */
static ssize_t loop_write(int fd, const uint8_t *data, size_t size) 
{
    int i = 0;
    for (i = 0; i < size; i += 2)
    {
        // put two bytes into one __signed__ integer
        int16_t val = data[i] + ((uint32_t)data[i+1] << 8);
        printf("%d", val);
    }

        return size;
}

int main(int argc, char*argv[]) 
{
    char *device = "alsa_output.usb-Creative_Technology_Ltd_Sound_Blaster_X-Fi_Go__Pro_00173634-00-Pro_1.analog-stereo";

    // The sample type to use
    static const pa_sample_spec ss = {
    .format = PA_SAMPLE_S16LE,
    .rate = 44100,
    .channels = 2
    };
    pa_simple *s = NULL;
    int ret = 1;
    int error;

    // Create the recording stream 
    if (!(s = pa_simple_new(NULL, argv[0], PA_STREAM_RECORD, NULL, "record", &ss, NULL, NULL, &error))) {
    fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
    goto finish;
    }

    for (;;) {
    uint8_t buf[BUFSIZE];

    // Record some data ...
    if (pa_simple_read(s, buf, sizeof(buf), &error) < 0) {
        fprintf(stderr, __FILE__": pa_simple_read() failed: %s\n", pa_strerror(error));
        goto finish;
    }

    // And write it to STDOUT
    if (loop_write(STDOUT_FILENO, buf, sizeof(buf)) != sizeof(buf)) {
        fprintf(stderr, __FILE__": write() failed: %s\n", strerror(errno));
        goto finish;
    }
    }

    ret = 0;

finish:

    if (s)
    pa_simple_free(s);

    return 0;
}

“ssize_t loop_write”方法接收缓冲区,因为它是一个 16 位小端字节数组,我将两个字节组合成一个 16 位整数。这意味着幅度(由变量“val”表示)应该在 0 和 32768 之间。但到目前为止,它都是 0。所以我有两个主要问题是设备源(imo 似乎更有可能)和我的转换为一个整数值。

您对此有什么建议吗?提前谢谢!

编辑/更新:好的,我不知道我做了什么 - 但如果我通过特定设备,我现在会收到以下消息:

pa_simple_new() failed: Connection refused

当我传递 NULL 时,它适用于默认声卡。仍在使用我之前描述的命令行命令。有什么线索吗?

【问题讨论】:

    标签: c linux audio real-time pulseaudio


    【解决方案1】:

    好的,刚刚想通了——我犯了一个非常愚蠢的错误。

    我将设备传递给第一个函数参数而不是第四个。

    这样做

    if (!(s = pa_simple_new(NULL, argv[0], PA_STREAM_RECORD, device, "record", &ss, NULL, NULL, &error)))
    

    而不是这个

    if (!(s = pa_simple_new(device, argv[0], PA_STREAM_RECORD, NULL, "record", &ss, NULL, NULL, &error)))
    

    【讨论】:

      猜你喜欢
      • 2015-04-22
      • 1970-01-01
      • 2019-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-26
      • 2010-12-22
      相关资源
      最近更新 更多