0

I have filelists that look like this:

S134_001.wav
S134_002.wav
S134_003.wav
S149_001.wav
S149_002.wav
S149_003.wav
S16_001.wav
S16_002.wav
S16_003.wav
S16_004.wav
S16_005.wav
S16_006.wav
S272_001.wav
S272_002.wav
S272_003.wav
S272_004.wav
S272_005.wav
S272_006.wav
S272_007.wav
S374_001.wav
S396_001.wav
S92_001.wav

And I want to merge S134_001.wav, S134_002.wav, S134_003.wav into S134.wav; S149_001.wav, S149_002.wav and S149_003.wav into S149.wav; and so on ( preferably using sox ).

I also want to delete the original files.

How do I achieve that? Bash is the preferred solution, but any programming language will do. Thanks.

Ruan
  • 65
  • 1
  • 1
  • 9
  • Note: in `sox` terminology, `merge` means to overlay audio files, whereas `concatenate` means to sequentially append one file after another. – Stewart Macdonald Oct 01 '18 at 07:25

1 Answers1

1

You can use a for loop like this:

for f in *_001.wav
do
    pre=${f%%_001.wav}
    # use either cat or sox or whatever works:
    #cat "${pre}_"*.wav > "${pre}.wav"
    sox -m "${pre}_"*.wav "${pre}.wav"
    #rm -rf "${pre}_"*.wav
done
  • Here we iterate over the *_001.wav files and
  • derive the prefix from the file
  • and then concat the files with the same prefix into a new file.
  • If everything works, you can remove the comment before the rm command.
Lars Fischer
  • 9,135
  • 3
  • 26
  • 35
  • Also `.wav` looks be some sort of audio format, merging with `cat` doesn't actually makes sense, might need some other tool for the same. – Inian Dec 31 '16 at 15:27
  • 1
    @Inian It iterates only over the set of `*_001.wav` getting the `*` part into `$pre`. and then concats `${pre}_*.wav` into a new file named `${pre}.wav`, so all files with a common prefix are handled in one loop step and merged into a new file named after their common prefix. If the files where splitted with `split` using `cat` makes sense. If the file should be treated differently, the command has to be tweaked and this snippet is a template for that tweaking. – Lars Fischer Dec 31 '16 at 15:33
  • Ahh! that makes sense for the first part, but still `cat` doesn't make sense here, refer OP's point about using `sox` for contcat-ing the files "preferably using sox" , am not sure about the behaviour of audio stream of data using `cat` – Inian Dec 31 '16 at 15:35
  • 1
    @Inian seems like you could do something like `sox -m "${pre}_"*.wav "${pre}.wav"` instead of cat. I am no expert in sox. – Lars Fischer Dec 31 '16 at 15:45
  • The files were splitted using sox. The `cat` solution didn't work, as I didn't use `split`. – Ruan Dec 31 '16 at 16:04
  • `sox -m "${pre}_"*.wav "${pre}.wav"` didn't work either. In the results, you hear audio files overlapping, as if I had actually merged the files over one another. – Ruan Dec 31 '16 at 16:21
  • @Ruan Sorry I am no sox user at all. I only created a list of empty files named like in your question and build a for loop to loop over them. What happen if you use sox without the `-m`switch? – Lars Fischer Dec 31 '16 at 16:32
  • If I use sox without the `-m` switch, all I get is one single file which is several times larger than the original audio file ( 1,3GB larger to be precise ) and its contents will be only the first audio file in the series ( that is, S0_001.wav ). – Ruan Dec 31 '16 at 16:56
  • By the way, that is the same thing that happens when I use the `cat` command given here. – Ruan Dec 31 '16 at 16:56
  • @Ruan Here is the example for concatenate files from the sox manpage: `sox -m music.mp3 voice.wav mixed.flac`. I dont know what is happening with your files. Perhaps you can find further info in the sox documentation. The man page speaks about requirements in the files like same samplerates, etc. – Lars Fischer Dec 31 '16 at 17:15