gpt4 book ai didi

c - 如何在 C 中使用 libsox 更改声音文件的速度?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:33:33 27 4
gpt4 key购买 nike

我在 Ubuntu 上使用 libsox 库开发了 C 代码来改变声音文件(3 到 10 秒 ogg 文件)的速度(增加/减少)using following sample code .使用 tempo(value > 1) 我得到输出文件(带有截断的语音)但是对于 tempo(值 < 1)我得到正确的输出文件(带有所有语音样本)。

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

/*
* Reads input file, applies effects, stores in output file.
*executable file:example, input fie:audio.ogg ,output file:tempo.ogg
E.g. ./example audio.ogg tempo.ogg
*/
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];

assert(argc == 3);

/* All libSoX applications must start by initialising the SoX library */
assert(sox_init() == SOX_SUCCESS);

/* Open the input file (with default parameters) */
assert(in = sox_open_read(argv[1], NULL, NULL, NULL));

/* Open the output file; we must specify the output signal characteristics.
* Since we are using only simple effects, they are the same as the input
* file characteristics */
assert(out = sox_open_write(argv[2], &in->signal, NULL, NULL, NULL, NULL));

/* Create an effects chain; some effects need to know about the input
* or output file encoding so we provide that information here */
chain = sox_create_effects_chain(&in->encoding, &out->encoding);

/* The first effect in the effect chain must be something that can source
* samples; in this case, we use the built-in handler that inputs
* data from an audio file */
e = sox_create_effect(sox_find_effect("input"));
args[0] = (char *)in, assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
/* This becomes the first `effect' in the chain */
assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);
free(e);

/* Create the `tempo' effect, and initialise it with the desired parameters: */
e = sox_create_effect(sox_find_effect("tempo"));
args[0] = "1.5", assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
/* Add the effect to the end of the effects processing chain: */
assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);
free(e);
/* The last effect in the effect chain must be something that only consumes
* samples; in this case, we use the built-in handler that outputs
* data to an audio file */
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, &in->signal, &in->signal) == SOX_SUCCESS);
free(e);

/* Flow samples through the effects processing chain until EOF is reached */
sox_flow_effects(chain, NULL, NULL);

/* All done; tidy up: */
sox_delete_effects_chain(chain);
sox_close(out);
sox_close(in);
sox_quit();
return 0;
}

在这里我使用了 tempo speed=1.5 并且我尝试了所有大于 1 的不同值。谁能告诉我我必须做哪些改变才能获得适当增加的节奏声音文件?

最佳答案

这看起来类似于 src/example0.c。我认为此示例中存在一个错误,它会将 &in->signal 作为两个参数传递给每次调用 sox_add_effect()。

我相信这会用效果的输出配置覆盖 in 配置。

查看 src/example6.c 以及它如何使用变量 interim_signal 作为输入和 &out->signal 作为输出。

大致:

sox_signalinfo_t interm_signal; /* @ intermediate points in the chain. */
...

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

/* Create the `vol' effect, and initialise it with the desired parameters: */
e = sox_create_effect(sox_find_effect("vol"));
args[0] = "3dB", assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
/* Add the effect to the end of the effects processing chain: */
assert(sox_add_effect(chain, e, &interm_signal, &out->signal) == SOX_SUCCESS);
free(e);

关于c - 如何在 C 中使用 libsox 更改声音文件的速度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53567179/

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