首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在C中使用libsox来改变声音文件的速度?

如何在C中使用libsox来改变声音文件的速度?
EN

Stack Overflow用户
提问于 2018-12-01 02:10:17
回答 1查看 480关注 0票数 1

我开发了C代码,用Ubuntu使用以下示例代码上的libsox库更改声音文件(3到10秒ogg文件)的速度(增加ogg文件)。使用以下示例代码(值> 1)的得到输出文件(用截断的声音),但是对于tempo (值< 1),我得到了正确的输出文件(所有语音示例)。

代码语言:javascript
复制
#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;
}

在这里,我使用了speed=1.5,并且尝试了所有大于一个的不同值。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-25 22:35:41

这与src/example0.c类似。我认为这个示例中存在一个错误,它将& in ->signal作为对sox_add_effect()的每个调用的两个参数。

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

看看src/example6.c以及它如何使用变量interim_signal作为输入和输出信号。

大致如下:

代码语言:javascript
复制
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);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53567179

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档