使用libsox更改音频文件的音高而不更改速度

问题描述 投票:2回答:1

我开发了一个简单的应用程序,可以使用libsox(使用this example)更改音频文件的音高。这是我的代码。它与2个输入参数一起使用-输入文件路径和输出文件路径:

#include <sox.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>

int main(int argc, char * argv[])
{
    static sox_format_t * in, * out; /* input and output files */
    sox_effects_chain_t * chain;
    sox_effect_t * e;
    char * args[10];
    sox_signalinfo_t interm_signal; /* @ intermediate points in the chain. */
    sox_encodinginfo_t out_encoding = {
        SOX_ENCODING_SIGN2,
        16,
        0,
        sox_option_default,
        sox_option_default,
        sox_option_default,
        sox_false
    };
    sox_signalinfo_t out_signal = {
        16000,
        1,
        0,
        0,
        NULL
    };

assert(argc == 3);
assert(sox_init() == SOX_SUCCESS);
assert(in = sox_open_read(argv[1], NULL, NULL, NULL));
assert(out = sox_open_write(argv[2], &out_signal, &out_encoding, NULL, NULL, NULL));

chain = sox_create_effects_chain(&in->encoding, &out->encoding);

interm_signal = in->signal; /* NB: deep copy */

e = sox_create_effect(sox_find_effect("input"));
args[0] = (char *)in; 
assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
assert(sox_add_effect(chain, e, &interm_signal, &in->signal) == SOX_SUCCESS);
free(e);

e = sox_create_effect(sox_find_effect("pitch"));
args[0] = "1000";
assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
assert(sox_add_effect(chain, e, &interm_signal, &out->signal) == SOX_SUCCESS);
free(e);

e = sox_create_effect(sox_find_effect("output"));
args[0] = (char *)out;
assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
assert(sox_add_effect(chain, e, &interm_signal, &out->signal) == SOX_SUCCESS);
free(e);

sox_flow_effects(chain, NULL, NULL);

sox_delete_effects_chain(chain);
sox_close(out);
sox_close(in);
sox_quit();

return 0;
}

但是我从上面的代码中得到的结果是一个节奏改变的文件。以下是输入和输出的详细信息:

Input File     : 'input.wav'
Channels       : 1
Sample Rate    : 16000
Precision      : 16-bit
Duration       : 00:00:11.87 = 189921 samples ~ 890.255 CDDA sectors

Input File     : 'output.wav'
Channels       : 1
Sample Rate    : 16000
Precision      : 16-bit
Duration       : 00:00:21.15 = 338401 samples ~ 1586.25 CDDA sectors

另一件事,Sox应用程序运行正常。

sox input.wav output_app.wav pitch 1000

它生成与输入持续时间相同的文件:

Input File     : 'output_app.wav'
Channels       : 1
Sample Rate    : 16000
Precision      : 16-bit
Duration       : 00:00:11.87 = 189921 samples ~ 890.255 CDDA sectors

这里有人遇到同样的问题吗?还是我需要向sox_effect提供其他任何选项以使此效果正常工作?

c sox libsox
1个回答
0
投票

经过一段时间的搜索,感谢this。我发现,为了保持音频速度,必须在pitch effect之后的效果链中添加rate effect

© www.soinside.com 2019 - 2024. All rights reserved.