Question

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!

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top