5

任务是从录音的开始和结束按阈值去除静音。我使用这个 sox 端口到 iOS。 https://github.com/shieldlock/SoX-iPhone-Lib/

我发现命令行 sox 工具通过以下命令完成我的任务:

sox in.wav out.wav silence 1 0.1 1% reverse silence 1 0.1 1% reverse

(取自这里:http ://digitalcardboard.com/blog/2009/08/25/the-sox-of-silence/ )

但我不能像这样以 iOS lib 格式翻译它:

sox_create_effect(sox_find_effect("silence"));
args[0] = "2000", assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);

我需要为完成这项任务提供哪些参数?

4

2 回答 2

1

Since sox in.wav out.wav silence 1 0.1 1% reverse silence 1 0.1 1% reverse is a concatenation of two different command lines:

sox in.wav temp.wav silence 1 0.1 1% reverse
sox temp.wav out.wav silence 1 0.1 1% reverse

create two silence effects in your chain. Once effect trims the beginning of the file and pipes a reversed copy to a temp destination, and the next trims from the beginning of the temp and reverses it back to the finished destination.

But what arguments to pass (args)? DISCLAIMER: I have little experience and cannot test this, but I believe it should be these strings:

args[1] = "1";
args[2] = "0.1";
args[3] = "1%";
args[4] = "reverse";

e = sox_create_effect(sox_find_effect("silence"));
args[0] = "2000", assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
assert(sox_add_effect(chain, e, &inFile->signal, &tempFile->signal) == SOX_SUCCESS);
free(e);

e = sox_create_effect(sox_find_effect("silence"));
args[0] = "2000", assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
assert(sox_add_effect(chain, e, &tempFile->signal, &outFile->signal) == SOX_SUCCESS);
free(e);

sox_flow_effects(chain, NULL, NULL);
于 2014-08-10T01:04:29.257 回答
1

Sox 的文档很差。我在这里分享我的解决方案。
示例中的阈值对您来说可能太低了。适当调整参数。(确保在使用 C/C++ API 之前了解 shell 命令行选项。)

char *options[10];

// input effect. See sox's documentaton for details. 

// equivalent to 'silence 1 0.3 0.1% reverse silence 1 0.3 0.1% reverse'
options[0] = const_cast<char*>("1");
options[1] = const_cast<char*>("0.3");
options[2] = const_cast<char*>("0.1%");
for(int i = 0; i < 2; ++i) {
    // silence 1 0.3 0.1% 
    e = sox_create_effect(sox_find_effect("silence"));
    if(sox_effect_options(e, 3, options) != SOX_SUCCESS) {
        on_error("silence1 effect options error!");
    }   
    if(sox_add_effect(chain, e, &in->signal, &in->signal) != SOX_SUCCESS) {
        on_error("add effect error!");
    }
    free(e);

    // reverse
    e = sox_create_effect(sox_find_effect("reverse"));
    if(sox_effect_options(e, 0, NULL) != SOX_SUCCESS) {
        on_error("silence1 effect options error!");
    }
    if(sox_add_effect(chain, e, &in->signal, &in->signal) != SOX_SUCCESS) {
        on_error("add effect error!");
    }
    free(e);
}

// output effect
于 2015-10-20T07:32:57.920 回答