Stack Overflow Asked on February 18, 2021
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
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:
What should I pass to the sox_open_read function as the last parameter to use a microphone as an input?
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();
}
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.
Correct answer by Chris on February 18, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP