在 c/c++ 中使用 sox 进行录制 [英] Recording using sox in c/c++

查看:45
本文介绍了在 c/c++ 中使用 sox 进行录制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 C/C++ 中的麦克风和 sox 库录制声音.

I am trying to record sound using microphone and sox library in C/C++.

sox_open_read("default", &_input->signal, NULL, NULL)

我正在尝试使用默认输入设备.我收到错误

I am trying to use default input device. I am getting the error

formats: can't open input file `default': No such file or directory

我猜这是因为我没有传递最后一个参数:filetype 和 sox 试图找到一个名为default"的文件.
袜队名单:

Which I guess is caused because I did not pass the last argument: filetype and sox tries to find a file with name 'default'.
Sox lists:

  • 音频文件格式: 8svx aif aifc aiff aiffc al amb au avr caf cdda cdr cvs cvsd cvu dat dvms f32 f4 f64 f8 fap flac fssd gsm gsrt hcom htk ima matpclu l matpc10mat5 maud mp2 mp3 nist ogg opus paf prc pvf raw s1 s16 s2 s24 s3 s32 s4 s8 sb sd2 sds sf sl sln smp snd sndfile sndr sndt sou sox tmuxuxu2u32uxu2uxu32uxu2uxu36w64 wav wavpcm wv wve xa xi
  • 音频设备驱动程序:alsa、oss、ossdsp
  • Audio file formats: 8svx aif aifc aiff aiffc al amb au avr caf cdda cdr cvs cvsd cvu dat dvms f32 f4 f64 f8 fap flac fssd gsm gsrt hcom htk ima ircam la lpc lpc10 lu mat mat4 mat5 maud mp2 mp3 nist ogg opus paf prc pvf raw s1 s16 s2 s24 s3 s32 s4 s8 sb sd2 sds sf sl sln smp snd sndfile sndr sndt sou sox sph sw txw u1 u16 u2 u24 u3 u32 u4 u8 ub ul uw vms voc vorbis vox w64 wav wavpcm wv wve xa xi
  • audio devices drivers: alsa, oss, ossdsp

我应该将什么作为使用麦克风作为输入的最后一个参数传递给 sox_open_read 函数?

What should I pass to the sox_open_read function as the last parameter to use a microphone as an input?

推荐答案

作为用于麦克风输入的 sox_open_read 函数的最后一个参数,应传递其中一个音频设备驱动程序.就我而言,它是alsa".
示例:

As the last parameter to sox_open_read function for microphone input, one of the audio devices drivers should be passed. In my case, it is 'alsa'.
Example:

#include <sox.h>
#include <memory>

sox_signalinfo_t _intermediateSignal;
sox_format_t* input;
sox_format_t* output;
sox_effects_chain_t* effectsChain;

void addEffect(std::string effectName, sox_format_t* options) {
    std::unique_ptr<sox_effect_t> effect(sox_create_effect(sox_find_effect(effectName.c_str())));
    char *args[] = {reinterpret_cast<char *>(options)};
    sox_effect_options(effect.get(), 1, args);
    sox_add_effect(effectsChain, effect.get(), &_intermediateSignal, &input->signal);
}

int main() {
    if (sox_init() != SOX_SUCCESS)
        throw std::runtime_error("Could not initialise SOX.");

    input = sox_open_read("default", NULL, NULL, "alsa");
    output = sox_open_write("recorded.wav", &input->signal, NULL, NULL, NULL, NULL);
    if (!input || !output)
        throw std::runtime_error("SOX I/O error");

    _intermediateSignal = input->signal;

    effectsChain = sox_create_effects_chain(&input->encoding, &output->encoding);

    if (!effectsChain)
        throw std::runtime_error("SOX could not initialize effects chain.");

    addEffect("input", input);
    addEffect("output", output);

    sox_flow_effects(effectsChain, NULL, NULL);
    sox_quit();
}

这个例子永远不会完成,因为 sox_flow_effects 调用会阻止执行.一旦程序被 ctrl+c 杀死,recorded.wav 包含录制的音频.

This example will never finish as sox_flow_effects call blocks the execution. Once the program is killed with ctrl+c, recorded.wav contains recorded audio.

这篇关于在 c/c++ 中使用 sox 进行录制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆