在 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 名单:
- 音频文件格式: 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 uw vms voc vorbis vox w64 wav wavpcm wv wve xa xi
- 音频设备驱动程序:alsa、oss、ossdsp
我应该将什么作为最后一个参数传递给 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 包含录制的音频。
我正在尝试使用 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 名单:
- 音频文件格式: 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 uw vms voc vorbis vox w64 wav wavpcm wv wve xa xi
- 音频设备驱动程序:alsa、oss、ossdsp
我应该将什么作为最后一个参数传递给 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 包含录制的音频。