0

I have a plain text file containing a list of WAV files:

0001.wav 0002.wav
0003.wav 0004.wav
0005.wav 0006.wav 00007.wav 0008.wav
0009.wav
0010.wav
0011.wav 0012.wav 0013.wav
0014.wav
0015.wav

These same files are in the same folder as the plain text file.

According to How to merge several audio files using sox, I can use sox -m <filename> <filename> <filename> to merge any files together in sox. How can I use sox to merge all of the files listed on the same lines into one file, with the output files named in sequence, e.g.:

  • 0001.wav 0002.wav → m0001.wav
  • 0003.wav 0004.wav → m0002.wav
  • 0005.wav 0006.wav 00007.wav 0008.wav → m0003.wav
Community
  • 1
  • 1
Village
  • 22,513
  • 46
  • 122
  • 163

2 Answers2

1

You should be able to to do this with:

#!/bin/bash

filename=$1
i=1

while IFS=" " read -r -a line
do
     sox ${line[@]} m$(printf "%04d" $i).wav
     let i+=1
done < $filename

Here we read the file given as the first paramenter to the script into an array line, where IFS=" " says to use a space for the delimeter, if you are using tabs you should change this to IFS="\t".

${line[@]} expands the array of input file names.

m$(printf "%04d" $i).wav says to specify a file name of the format mXXXX.wav, where printf is used to get the formatting.

i is the variable you iterate for the output file names.

imp25
  • 2,327
  • 16
  • 23
1

I wanted to see if it was possible to do this in one line. I think I have it here.

cat <file> | xargs -L 1 printf "%s\n" | xargs -I {} sox {} m%4n.wav gain 0 : newfile

cat will read your file ( replace <file> with your filename) and pipe into xargs, which will treat each line of the file as space separated arguments.

Use print to print each arg on a new line.

Use xargs again to recombine the arguments (each on a new line), using the -I argument so we can put the arguments at the front of thesox command and pass additonal arguments (you can't use -I and -L at the same time, otherwise you'd only need one xargs).

Use the sox gain filter set to 0 so we can chain the pseudo newfile filter to create a new file each time with the pattern m####.wav

Edit: nevermind, this doesn't quite work. It's still sending each file as an individual argument. Maybe someone with more command line-fu can do better. The script approach is probably more readable anyhow.

nynexman4464
  • 1,760
  • 1
  • 17
  • 19