Home > OS >  combine permutations of .wav files
combine permutations of .wav files

Time:03-16

I am trying to combine permutations of some .wav files.
There's 6 variations of 4 instruments. Each generated track should have one of each instrument. If my math is right, there should be 24 unique permutations.
The files are named like:

beat_1.wav, beat_2.wav ...
bass_1.wav, bass_2.wav ...
chord_1.wav, chord_2.wav ...
melody_1.wav, melody_2.wav ...

I've tried to combine them with

sox -m {beat,bass,chord,melody}_{1..6}.wav out_{1..24}.wav

but regardless of what range of values I use for the out_n.wav file, sox gives this error immediately:

sox FAIL formats: can't open input file `out_23.wav': No such file or directory

The number in out_23.wav is always one lower than whatever range I specify.

I'm open to using tools other than sox and bash, provided I can generate all the tracks in one command/program (I don't want to do it by hand in Audacity, for example).

CodePudding user response:

If you replace sox with echo you will see the command you constructed is not really permutating the way you want:

$ echo sox -m {beat,bash,chord,melody}_{1..6}.wav out_{1..24}.wav
sox -m beat_1.wav beat_2.wav beat_3.wav beat_4.wav beat_5.wav beat_6.wav bash_1.wav bash_2.wav bash_3.wav bash_4.wav bash_5.wav bash_6.wav chord_1.wav chord_2.wav chord_3.wav chord_4.wav chord_5.wav chord_6.wav melody_1.wav melody_2.wav melody_3.wav melody_4.wav melody_5.wav melody_6.wav out_1.wav out_2.wav out_3.wav out_4.wav out_5.wav out_6.wav out_7.wav out_8.wav out_9.wav out_10.wav out_11.wav out_12.wav out_13.wav out_14.wav out_15.wav out_16.wav out_17.wav out_18.wav out_19.wav out_20.wav out_21.wav out_22.wav out_23.wav out_24.wav

So what we see is there are 24 combinations for your input as required, but, it is also supplying 24 outputs on the same line, which, according to the documentation of sox, all inputs are treated as input except for the last, so, files out_1.wav ... out23.wav will also be treated as input not outputs. So, you have a logic problem.

If you want to permutate through all 24 combinations, one at a time, I recommend a for loop, e.g.

i=0
for f in {beat,bass,chord,melody}_{1..6}.wav
do
  ((i  ))
  echo "Input: " $f "Output: out_${i}.wav"
done

Which outputs:

Input:  beat_1.wav Output: out_1.wav
Input:  beat_2.wav Output: out_2.wav
Input:  beat_3.wav Output: out_3.wav
Input:  beat_4.wav Output: out_4.wav
Input:  beat_5.wav Output: out_5.wav
Input:  beat_6.wav Output: out_6.wav
Input:  bass_1.wav Output: out_7.wav
Input:  bass_2.wav Output: out_8.wav
Input:  bass_3.wav Output: out_9.wav
Input:  bass_4.wav Output: out_10.wav
Input:  bass_5.wav Output: out_11.wav
Input:  bass_6.wav Output: out_12.wav
Input:  chord_1.wav Output: out_13.wav
Input:  chord_2.wav Output: out_14.wav
Input:  chord_3.wav Output: out_15.wav
Input:  chord_4.wav Output: out_16.wav
Input:  chord_5.wav Output: out_17.wav
Input:  chord_6.wav Output: out_18.wav
Input:  melody_1.wav Output: out_19.wav
Input:  melody_2.wav Output: out_20.wav
Input:  melody_3.wav Output: out_21.wav
Input:  melody_4.wav Output: out_22.wav
Input:  melody_5.wav Output: out_23.wav
Input:  melody_6.wav Output: out_24.wav
  • Related