Reducing a channel from wav file using libsox

心已入冬 提交于 2020-02-04 15:01:11

问题


I'm new to libsox programming, and I want to reduce a chennel from a stereo audio which named 'a.wav' then generate a mono audio 'b.wav' with the following code:

sox_format_t * in, * out; 
sox_effects_chain_t * chain;
sox_effect_t * e;
char * args[10];

sox_init();
in = sox_open_read("E:\\a.wav", NULL, NULL, NULL);
out = sox_open_write("E:\\b.wav", &in->signal, NULL, NULL, NULL, NULL);
out->signal.channels = 1;

chain = sox_create_effects_chain(&in->encoding, &out->encoding);
e = sox_create_effect(sox_find_effect("input"));
sox_add_effect(chain, e, &in->signal, &in->signal);

e = sox_create_effect(sox_find_effect("channels"));
sox_add_effect(chain, e, &in->signal, &out->signal);

e = sox_create_effect(sox_find_effect("output"));
sox_add_effect(chain, e, &in->signal, &out->signal);

sox_flow_effects(chain, NULL, NULL);
sox_delete_effects_chain(chain);
sox_close(out);
sox_close(in);
sox_format_quit();

After running the application, the mono audio 'b.wav' was generated, but the duration of the sound was half of the a.wav. Is there something wrong with my code?

Any reply will be appreciated!


回答1:


sox_add_effect() overwrites the input signal (third parameter) to describe the properties of the signal after this processing step, so that you can pass it to the next effect. However, in your case, the modified signal information is also used by the read handler, and the contents no longer match the file being read.

You’ll need to make a copy of the signal information as returned by sox_open_read(), which you can then pass as the third parameter to the sox_add_effect() calls:

sox_signalinfo_t interm_signal = in->signal;
...
sox_add_effect(chain, e, &interm_signal, &out->signal);

That’s why I warned you to look at the newest version of example3.c in the git repository, not in the released versions.



来源:https://stackoverflow.com/questions/17141679/reducing-a-channel-from-wav-file-using-libsox

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