gpt4 book ai didi

c++ - 使用 libSox 链接多个效果并读取输出数据的正确方法

转载 作者:太空狗 更新时间:2023-10-29 23:11:07 24 4
gpt4 key购买 nike

我正在尝试以编程方式对 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;

我的问题是:

  1. 我是否正确设置了效果链?

  2. 如何读取内存中产生的音频样本?

    如果我使用速度值 < 1,我会从输出中获得正确数量的样本(在 audio_buffer 中),但是如果我将其更改为 1.2,我突然会得到非常少量的样本,如果我使用该值则为 0 1.0。我想知道我的链配置或从输出中读取的数据是否存在错误?这是我第一次使用 libsox,我尝试遵循示例,但我被困在这里。

预先感谢您的帮助!

谢谢!

最佳答案

  1. 你的效果链没问题,你可以通过将它写入文件来检查它是否提供了正确的输出缓冲区 - 只需在你的代码中替换这一行:

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);

  1. 关于内存输出缓冲区问题 - 我研究了 libsox 代码,它的内存缓冲区处理似乎存在错误。作为一种解决方法,我建议您在读取 ​​output 缓冲区之前添加 output->olength = 0;,然后它似乎可以正常工作。

因此,您的代码将如下所示:

...
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

时使用解决方法

关于c++ - 使用 libSox 链接多个效果并读取输出数据的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51908577/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com