More CLI stuff with audio files

OK - so - I wanted to concatenate several WAV files into one large one (for a ringtone for my alarm)…

Issue I was having - most of them were (output of soxi $FILENAME) :

Sample Rate : 48000
Precision : 24-bit

But two of them were 44100 and 16-bit and sox refused to generate any output.

So - I had to resample those two :
sox -r 48000 -e unsigne -b 24 -c 1 $FILE outie/$FILE

Then concatenate :
ffmpeg -i *.wav -filter_complex '[0:0][1:0][2:0][3:0]concat=n=4:v=0:a=1[out]' -map '[out]' ../cl4p-out.wav

I’m pretty much writing this by way of a “howto to refer to in future” when it happens again :smiley:

6 Likes

Note that rate-shifting with sox -r <rate> will change the pitch and duration of the audio. To resample without audible changes, you should use the rate effect, like so:

sox infile.wav outfile.wav rate -v 48k

-v gives the highest-quality resampling; see sox --help-effect rate for details on all the available options.

Also, it’s a teeny bit weird that you resampled with sox, but then used ffmpeg for concatenation. sox can concatenate just fine, and in fact if you want to concatenate-in a single resampled audio clip with a list of others already at the target rate, you can do that by using sox in a pipeline:

sox infile.wav -t wav - rate -v 48k \
  | sox --combine concatenate \
    48k_infile1.wav -t wav - 48k_infile2.wav \
    outfile.wav

You always have to give the -t <type> of piped output streams, and sometimes of piped input streams as well. (It’s always safer to explicitly specify, just in case.) The above would make the resampled audio clip the middle of 3 concatenated files.

(But if you have to resample more than one audio clip, you’ll need to write the resampled clips to files, then use those in a sox --combine concatenate command. You can’t apply the rate effect in the middle of a pipeline, since having a 44.1k input file on the command line is incompatible with using --combine concatenate in that same command.)

2 Likes

Actually, you could avoid writing any of the resampled files to disk, by instead doing the concatenation in multiple steps, and writing the partial assembly to disk at the end of each step, to be picked up at the start of the next step. i.e.:

$ sox infileB_441k.wav -t wav - rate -v 48k \
    | sox --combine concatenate \
      infileA_48k.wav -t wav - infileC_48k.wav \
      partial_partsA-C.wav
$ sox infileD_441k.wav -t wav - rate -v 48k \
    | sox --combine concatenate \
      partial_partsA-C.wav -t wav - \
      infileE_48k.wav \
      final_partsA-E_output.wav
3 Likes

That’s what I intended to do (use sox to concat) - but got impatient and found the ffmpeg method on Stack Overflow or somewhere - and it worked - I don’t usually revisit stuff that worked - because the goal was to upload a single mp3 file to my phone, as a wake-up alarm - 'cause for the last 15 years or so - my ringtone has been “Misirlou” by Dick Dale and the Deltones (most people call it “that Pulp Fiction song” - but its actually an old old Greek love song about a Greek living in Egypt who falls in love with a Muslim girl - Dick Dale was of Lebanese descent) - and I couldn’t use that for my alarm 'cause I’d think it was work calling me in the wee hours…

Anyway - just hit another issue today. Got an ISO image of an Audio CD (not prepared to admit where or how I obtained it) that wouldn’t mount (e.g. mount -o loop file.iso /mnt/ISO).

Discovered it was created using SACD.

So - off to github - get the source and build it :

git clone https://github.com/Sound-Linux-More/sacd.git
cd sacd
make
sudo make install

then :
sacd -p -s -i file.iso -o outie

And folder “outie” now has a WAV file for each track… Just gotta convert them to FLAC with ffmpeg…

'cause I’m lazy - I’ll probably just co-opt my quick and dirty “convert folder of
FLAC files to mp3 in a new folder” script…

1 Like

That script I wrote to convert a folder of FLAC files to mp3 - didn’t work when I tried to change it to do “folder of WAV files to FLAC”… and script too complex for me to figure out so I gave up and just did this instead :

for F in *.wav ; do ffmpeg -i $F -compression_level 6 "${F%.*}".flac ; done

1 Like

ffmpeg is definitely a jack of all trades.

2 Likes

The Swiss Army Knife of CLI media utilities… Lot of Windows users use it as well…

1 Like