Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a folder full of .wav audio files.

Some are stereo, most are mono splits.

The mono split pairs are all named foo bar track.L.wav and foo bar track.R.wav

I can use the command line tool sox to combine a mono pair into 1 stereo track like this:

sox -M track1.L.wav track1.R.wav track1.Stereo.wav

where the first 2 files are the mono pairs, and the third is the output stereo file.

This is great, but I'd like to have a script that will automatically find all the mono pairs and combine them into stereo files.

I.e., I need it to find all files which have the same name except for the .L. and .R. before the extension, and run sox on them, outputting to a new file with the same name without the L/R suffix.

For example, if my folder contains these files:

track1.L.wav
track2.L.wav
track3.L.wav
track4.L.wav
track1.R.wav
track2.R.wav
track3.R.wav
track4.R.wav
track6.wav
track7.wav

I need to run these commands:

sox -M track1.L.wav track1.R.wav track1.Stereo.wav
sox -M track2.L.wav track2.R.wav track2.Stereo.wav
sox -M track3.L.wav track3.R.wav track3.Stereo.wav
sox -M track4.L.wav track4.R.wav track4.Stereo.wav

Here's where I am so far:

for file in ./*.L.wav;
do 
    file2=`echo $file | sed 's_\(.*\).L.wav_\1.R.wav_'`;
    out=`echo $file | sed 's_\(.*\).L.wav_\1.STEREO.wav_'`;
    echo $file - $file2 - $out;
done

That works, but when I replace the echo line with

sox -M $file $file2 $out;

it doesn't work; spaces in the filenames cause it to fail.

share|improve this question

2 Answers 2

Going from sgmart's suggestion, I got it solved:

for file1 in ./*.L.wav; do 
  file2=`echo $file1 | sed 's_\(.*\).L.wav_\1.R.wav_'`;
  out=`echo $file1 | sed 's_\(.*\).L.wav_\1.STEREO.wav_'`;
  sox -MS "$file1" "$file2" "$out";
  mv "$file1" mono; mv "$file2" mono;
done

Needed "'s around the variables.

And I added a bit to move the processed files to a 'mono' subfolder.

share|improve this answer

Try with this from a GNU/Linux console:

for file in ./*L*wav;
do
    for file2 in ./*R*wav;
    do
        $out = "Stereo"
        sox -M $file $file2 $file-$out.wav;
    done;
done

NOTE: "./" represent the music directory where your files are.

share|improve this answer
    
sox needs 3 args: the 2 files, plus the output file. I need the output file name to be the name of the first 2 without the L or R –  Ze'ev Oct 4 '12 at 22:52
    
Sorry, this does not work; it does not match up the matching files. It just returns every file with an "L", followed by every other file, recursively. –  Ze'ev Oct 4 '12 at 22:56
    
Yeah, I am sorry; I forgot the third argument –  sgmart Oct 4 '12 at 22:58
    
I updated the post to clarify; your code still doesn't line up matching pairs. Also, ./*.L.wav works better... –  Ze'ev Oct 4 '12 at 23:07
    
The second for statement needs to somehow find files that match the * from the first for –  Ze'ev Oct 4 '12 at 23:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.