How to use soxlib for iOS to remove start and end silence

会有一股神秘感。 提交于 2019-12-04 08:14:54

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

Sox's documentation is poor. I share my solution here.
The threshhold in the example may be too low for you. Adjust the parameters as appropriate. (Make sure you understand the shell command line options before you use the C/C++ API.)

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!