Search code examples
audiomakefilesox

"sox --combine merge": how to limit the mixed output length to the shorter of the two inputs?


I am using SoX command line tool on Linux inside a Makefile to interleave two raw (float 32 bit) input audio files into one file:


make_combine: 
    sox \
    --bits 32 --channels 1 --rate 48000 signal_1.f32 \
    --bits 32 --channels 1 --rate 48000 signal_2.f32 \
    --type raw --channels 2 --combine merge signal_mixed.f32

I ran into problems when signal_1 and signal_2 are different length. How would I limit the mixed output to shorter of the two inputs?


Solution

  • Use soxi -s to find the shortest file, e.g.:

    samps=$(soxi -s signal_1.f32 signal_2.f32 | sort -n | head -n1)
    

    Then use the trim effect to shorten the files, e.g. (untested):

    sox --combine merge \
        "| sox signal_1.f32 -p trim 0 ${samps}s" \
        "| sox signal_2.f32 -p trim 0 ${samps}s" \
        signal_mixed.f32
    

    Note: If you want me to test it, provide some sample data.