【问题标题】:C++ alsa api bad recorded qualityC ++ alsa api不良记录质量
【发布时间】:2021-01-15 02:30:31
【问题描述】:

我正在尝试在 linux 上使用 alsa api 录制麦克风,但结果是奇怪的声音,例如冻结的故障机器人。记录 UDP 协议发送到 pcm 播放器端点的 pcm 数据。

        char* device = "default";
        unsigned int rate = 44100;
        unsigned int channels = 2;
        snd_pcm_uframes_t frames{};
        snd_pcm_t* capture_handle{};
        snd_pcm_hw_params_t* hw_params{};

        if (snd_pcm_open(&capture_handle, device, SND_PCM_STREAM_CAPTURE, 0) < 0)
            throw new std::runtime_error{ "Can't open device for capture" };

        if (snd_pcm_hw_params_malloc(&hw_params) < 0)
            throw new std::runtime_error{ "Can't allocate hw parameters structure" };

        if (snd_pcm_hw_params_any(capture_handle, hw_params) < 0)
            throw new std::runtime_error{ "Can't initialize parameters structure" };

        if (snd_pcm_hw_params_set_access(capture_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED) < 0)
            throw new std::runtime_error{ "Can't set access parameter" };

        if (snd_pcm_hw_params_set_format(capture_handle, hw_params, SND_PCM_FORMAT_S16_LE) < 0)
            throw new std::runtime_error{ "Can't set access parameter" };

        if (snd_pcm_hw_params_set_rate_near(capture_handle, hw_params, &rate, 0) < 0)
            throw new std::runtime_error{ "Can't set rate" };

        if (snd_pcm_hw_params_set_channels(capture_handle, hw_params, channels) < 0)
            throw new std::runtime_error{ "Can't set channels count" };

        frames = 32;
        if (snd_pcm_hw_params_set_period_size_near(capture_handle, hw_params, &frames, 0))
            throw new std::runtime_error{ "Can't set period size" };

        if (snd_pcm_hw_params(capture_handle, hw_params) < 0)
            throw new std::runtime_error{ "Can't set parameters" };

        snd_pcm_hw_params_free(hw_params);

        if (snd_pcm_prepare(capture_handle) < 0)
            throw new std::runtime_error{ "Can't prepare capture device" };

        if (snd_pcm_hw_params_get_period_size(hw_params, &frames, 0) < 0)
            throw new std::runtime_error{ "Can't get frames count" };

        const unsigned int bufSize = frames * channels * 2;
        unsigned int buf[bufSize];
        while (true) {
            if (snd_pcm_readi(capture_handle, &buf[0], bufSize) != bufSize)
                throw new std::runtime_error{ "Can't read from buffer" };
            
            if (connectable != nullptr)
                connectable->sendData(buf, bufSize);
        }

        snd_pcm_close(capture_handle);

示例结果:https://voca.ro/1m3zDAmdW5cc

【问题讨论】:

  • 当您使用 ALSA 的 arecord 实用程序以相同的参数进行录制时,您是否观察到相同的情况?
  • arecord 工具效果很好。我测试它: arecord -f S16_LE -d 10 -r 44100 --device="default" -c 2 rec.wav。

标签: c++ linux alsa libalsa


【解决方案1】:

所以问题是因为我没有指定周期时间,周期大小。在期间操作之后,我需要检索实际值。我在缓冲区大小公式中有错误。固定代码:

...
 frames = periodSize;
        if (snd_pcm_hw_params_set_period_size_near(captureHandle, hwParams, &frames, 0) < 0)
            throw new std::runtime_error{ "Can't set period" };

        if (snd_pcm_hw_params_set_period_time_near(captureHandle, hwParams, const_cast<unsigned int*>(&framesTime), 0) < 0)
            throw new std::runtime_error{ "Can't set period time" };
...
snd_pcm_hw_params_get_rate(hwParams, &actualSampleRate, 0);

snd_pcm_hw_params_get_period_size(hwParams, &frames, 0);

snd_pcm_hw_params_get_period_time(hwParams, &actualPeriodTime, 0);

snd_pcm_hw_params_get_channels(hwParams, &actualChannels);
...
bufferSize = (frames * channels * snd_pcm_format_physical_width(SND_PCM_FORMAT_S16_LE)) / 8;
...
snd_pcm_readi(captureHandle, buffer, frames);
...
connectable->sendData(buffer, bufferSize);

【讨论】:

    猜你喜欢
    • 2014-04-01
    • 2013-01-18
    • 2015-04-09
    • 1970-01-01
    • 2022-01-17
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多