C++ C++;alsa api不良记录质量

C++ C++;alsa api不良记录质量,c++,linux,alsa,libalsa,C++,Linux,Alsa,Libalsa,我试图在linux上用ALSAAPI录制麦克风,但结果是奇怪的声音像冰冻的glitchy机器人。记录的pcm数据通过UDP协议发送到pcm播放器端点 char* device = "default"; unsigned int rate = 44100; unsigned int channels = 2; snd_pcm_uframes_t frames{}; snd_pcm_t* captur

我试图在linux上用ALSAAPI录制麦克风,但结果是奇怪的声音像冰冻的glitchy机器人。记录的pcm数据通过UDP协议发送到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);
char*device=“默认”;
无符号整数率=44100;
无符号整数通道=2;
snd_pcm_uframes_t frames{};
snd_pcm_t*capture_handle{};
snd_pcm_hw_params_t*hw_params{};
if(snd_pcm_打开(&capture_句柄,设备,snd_pcm_流_捕获,0)<0)
抛出新std::runtime_错误{“无法打开设备进行捕获”};
如果(snd_pcm_hw_参数_malloc(&hw_参数)<0)
抛出新的std::runtime_错误{“无法分配hw参数结构”};
如果(snd_pcm_hw_params_any(capture_handle,hw_params)<0)
抛出新的std::runtime_错误{“无法初始化参数结构”};
if(snd_pcm_hw_参数设置_访问(捕获_句柄、hw_参数、snd_pcm_访问_RW_交错)<0)
抛出新std::runtime_错误{“无法设置访问参数”};
if(snd_pcm_hw_参数_设置_格式(捕获_句柄、hw_参数、snd_pcm_格式_S16_LE)<0)
抛出新std::runtime_错误{“无法设置访问参数”};
如果(snd_pcm_hw_params_set_rate_near(捕获句柄、hw_params和速率,0)<0)
抛出新std::runtime_错误{“无法设置速率”};
中频(snd_pcm_hw_参数_设置_通道(捕捉_手柄,hw_参数,通道)<0)
抛出新std::runtime_错误{“无法设置通道计数”};
帧=32;
if(snd_pcm_hw_参数_设置_周期_大小_接近(捕捉_句柄,hw_参数和帧,0))
抛出新std::runtime_错误{“无法设置时段大小”};
如果(snd_pcm_hw_参数(捕获句柄,hw_参数)<0)
抛出新std::runtime_错误{“无法设置参数”};
snd_pcm_硬件参数_free(硬件参数);
如果(snd\U pcm\U准备(捕获\U句柄)<0)
抛出新std::runtime_错误{“无法准备捕获设备”};
if(snd_pcm_hw_params_get_period_size(hw_params,&frames,0)<0)
抛出新std::runtime_错误{“无法获取帧计数”};
const unsigned int bufSize=帧*通道*2;
无符号整数buf[bufSize];
while(true){
if(snd\u pcm\u readi(捕获\u句柄和buf[0],bufSize)!=bufSize)
抛出新std::runtime_错误{“无法从缓冲区读取”};
如果(可连接!=nullptr)
可连接->发送数据(buf,bufSize);
}
snd_pcm_关闭(捕获_手柄);

示例结果:

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

...
 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);
。。。
帧=周期大小;
如果(snd_pcm_hw_参数设置_周期_大小_近(captureHandle,hwParams和frames,0)<0)
抛出新std::runtime_错误{“无法设置时段”};
如果(snd_pcm_hw_params_set_period_time_near(captureHandle,hwParams,const_cast(&Framesime),0)<0)
抛出新std::runtime_错误{“无法设置时段时间”};
...
snd_pcm_hw_params_get_rate(hwParams和实际采样率,0);
snd_pcm_hw_params_get_period_size(hwParams和frames,0);
snd_pcm_hw_params_get_period_time(hwParams和actual periodidtime,0);
snd_pcm_硬件参数_get_通道(硬件参数和实际通道);
...
缓冲区大小=(帧*通道*snd_pcm_格式_物理_宽度(snd_pcm_格式_S16_LE))/8;
...
snd_pcm_readi(captureHandle、缓冲器、帧);
...
可连接->发送数据(缓冲区、缓冲区大小);

使用ALSA的arecord实用程序记录相同的参数时,您是否观察到了相同的情况?arecord工具工作正常。我用:arecord-fs16_LE-d10-r44100--device=“default”-c2rec.wav测试它。