Johnzo
Johnzo

Reputation: 112

How to mix audio files using SoX C library

Mixing audio files is simple with SoX from the command line with "sox -m ..." but I'm trying to find to the exact same thing with the C library and I can't find out how to do it anywhere. Is it even possible?

Upvotes: 1

Views: 2389

Answers (1)

Michał Górny
Michał Górny

Reputation: 19293

Well, it is possible but the library won't help you with that.

Looking through the sox.c code, you can notice that the actual mixing code is in a static function:

for (ws = 0; ws < olen; ++ws) { /* wide samples */
  if (combine_method == sox_mix || combine_method == sox_mix_power) {
    for (s = 0; s < effp->in_signal.channels; ++s, ++p) { /* sum samples */
      *p = 0;
      for (i = 0; i < input_count; ++i)
        if (ws < z->ilen[i] && s < files[i]->ft->signal.channels) {
          /* Cast to double prevents integer overflow */
          double sample = *p + (double)z->ibuf[i][ws * files[i]->ft->signal.channels + s];
          *p = SOX_ROUND_CLIP_COUNT(sample, mixing_clips);
        }
    }
/* [...] */

So, if you really want to use sox, you can use it for file input/output, but the mixing you will need to do yourself.

Upvotes: 1

Related Questions