stamhaney
stamhaney

Reputation: 1312

How to mix two wav files using LibSoX

I am trying to mix two wav files using LibSoX on Windows. I can do this by using sox from the command line using the following command:

sox -m f1.wav f2.wav out.wav

However I want to do this programmatically using a C/C++ function and linking with LibSoX. I have built LibSoX successfully and have tried out the sample programs which do not cover a "mix" of two audio files.

Has someone done this earlier? It would be great if you can give a code snippet or at least some pointers to do this using LibSoX calls

Upvotes: 1

Views: 1782

Answers (2)

Ryan Francesconi
Ryan Francesconi

Reputation: 1006

Though this is an old question, I came across this same problem recently. In order to use the sox.c main method from your code, you'll need to reset all the globals after a call is made. sox.c was intended to run once then exit. To do that you can add a function like below. Then you can rename main, sox_main_private and things will basically work to use the command line calls directly building your argv array manually by calling sox_main()

// reset the globals so that multiple calls can be made to sox_main
static void cleanup_globals(void)
{
    file_count = 0;
    input_count = 0;
    output_count = 0;
    current_input = 0;

    input_wide_samples = 0;
    read_wide_samples = 0;
    output_samples = 0;
    input_eof = sox_false;
    output_eof = sox_false;
    user_abort = sox_false;
    user_skip = sox_false;
    user_restart_eff = sox_false;
    success = 0;

    files = NULL;
    user_efftab = NULL;
    user_efftab_size = 0;
    effects_chain = NULL;
    save_output_eff = NULL;
    user_effargs_size = NULL;  /* array: size of user_effargs for each chain */
    nuser_effects = NULL;  /* array: number of effects in each chain */
    current_eff_chain = 0;
    eff_chain_count = 0;
    very_first_effchain = sox_true;
    effects_filename = NULL;
    play_rate_arg = NULL;
    norm_level = NULL;
}

int sox_main(int argc, char *argv[])
{
#if DEBUG
    printf("sox_main:\n");
    int i = 0;
    while (i < argc) {
        printf("%s ", argv[i]);
        i++;
    }
    printf("\n");
#endif
    sox_main_private(argc, argv);
    cleanup_globals();
    return 0;
}

Upvotes: 0

stamhaney
stamhaney

Reputation: 1312

Want to share the solution (workaround) which I made for the above issue. Basically, mixing is not exposed as an exported function call. Mixing can be done via the command line of course, so there could be two solutions: 1) spawning a process (sox.exe) via the program and 2) via libsox. I needed a libsox based solution as the function which was going to call the mixing function, could not spawn a process (constraint). So I moved the main function functionality from sox to a new method in libsox which I exported :) . So now I can pass the same command line switches to my function, and get the job done using libsox! This would be a "workaround" ideally, till Chris Bagwell exposes the mixing (and other missing) functionality from libsox.

Upvotes: 2

Related Questions