我正在尝试以编程的方式使用libSox应用很少的效果,目前我无法理解我是否做得对。例如,我需要应用速度并获得效果,并在缓冲区中读取产生的音频以供进一步处理。这些文档非常稀少,谷歌搜索也没有结果。这是我的代码:
sox_format_t* input = sox_open_read("<file.wav>", NULL, NULL, NULL);
//sox_format_t* out;
sox_format_t* output = sox_open_memstream_write(&buffer, &buffer_size,
&input->signal, &input->encoding, "raw", NULL);
//assert(output = sox_open_write("/home/egor/hello_processed.wav", &input->signal, NULL, NULL, NULL, NULL));
sox_effects_chain_t* chain = sox_create_effects_chain(&input->encoding, &output->encoding);
char* sox_args[10];
//input effect
sox_effect_t* e = sox_create_effect(sox_find_effect("input"));
sox_args[0] = (char*)input;
assert(sox_effect_options(e, 1, sox_args) == SOX_SUCCESS);
assert(sox_add_effect(chain, e, &input->signal, &input->signal) ==
SOX_SUCCESS);
free(e);
e = sox_create_effect(sox_find_effect("tempo"));
std::string tempo_str = "1.01";
sox_args[0] = (char*)tempo_str.c_str();
assert(sox_effect_options(e, 1, sox_args) == SOX_SUCCESS);
assert(sox_add_effect(chain, e, &input->signal,&input->signal) ==
SOX_SUCCESS);
free(e);
e = sox_create_effect(sox_find_effect("output"));
sox_args[0] = (char*)output;
assert(sox_effect_options(e, 1, sox_args) == SOX_SUCCESS);
assert(sox_add_effect(chain, e, &input->signal, &input->signal) ==
SOX_SUCCESS);
free(e);
sox_flow_effects(chain, NULL, NULL);
static const size_t maxSamples=4096;
sox_sample_t samples[maxSamples];
std::vector<sox_sample_t> audio_buffer;
for (size_t r; 0 != (r=sox_read(output,samples,maxSamples));)
for(int i=0;i<r ;i++)
audio_buffer.push_back(samples[i]);
std::cout << audio_buffer.size() << std::endl;我的问题是:
提前感谢您的帮助!
谢谢!
发布于 2018-08-28 23:02:15
sox_format_t* output = sox_open_memstream_write(&buffer, &buffer_size, &input->signal, &input->encoding, "raw", NULL);
对此:
sox_format_t* output = sox_open_write("2.wav", &input->signal, &input->encoding, "raw", NULL, NULL);
libsox代码,它的内存缓冲区处理似乎有一个缺陷。作为一种解决办法,我建议您在读取output->olength = 0;缓冲区之前添加output,然后它似乎正确工作。所以,您的代码将如下所示:
...
if (std::stof(tempo_str) >= 1.0) { // use workaround only if tempo >= 1.0
output->olength = 0;
}
std::vector<sox_sample_t> audio_buffer;
for (size_t r; 0 != (r=sox_read(output,samples,maxSamples));)
for(int i=0;i<r ;i++)
audio_buffer.push_back(samples[i]);
...UPD:仅在tempo >= 1.0时使用解决方案
https://stackoverflow.com/questions/51908577
复制相似问题