在 c/c++ 中使用 sox 进行录制

Recording using sox in c/c++

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

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

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

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

我猜这是因为我没有传递最后一个参数:文件类型和 sox 试图找到名称为 'default'.
的文件 Sox 名单:

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

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

#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 包含录制的音频。