I am an interdisciplinary researcher investigating how technology can be used to monitor biodiversity, in particular using bioacoustic and ecoacoustic approaches.
GitHub Profile | CV | Media
Bioacoustic and Ecoacoustic Data in Audiovisual Core
Good practice guidelines for long-term ecoacoustic monitoring in the UK
05/12/2024 - NHM x Natural England
08/11/2024 - Digital Dimensions of Nature Recovery
04/10/2024 - Soundings: The River
Some thoughts on:
This is a collection of small techniques for manipulating audio data on the command line. They have been compiled over a number of years from my work on bioacoustic and ecoacoustic datsets, although they should prove useful to anyone who works with audio files.
Most of these should work on other Unix-like systems such as macOS.
Unless you are unusually curious, the best way to use this guide is by seaching (Ctrl+F
) in your web browser for what you are trying to achieve.
The two most common command line tools are Sound eXchange (sox) and FFmpeg, which share some basic functionality. These are widely availabe on Unix-like operating systems (and even Windows). FFmpeg is primarily a tool for video (which is often a useful tool for the acoustician) although it can also be used to manipulate audio.
sudo apt install sox
First install homebrew.
brew install sox
sudo apt install ffmpeg
First install homebrew.
brew install ffmpeg
Both sox and ffmpeg come with extensive documentation.
man sox
man ffmpeg
sox --info file
soxi file
sox file.mp3 -n stat
The play
command is provided by sox
.
play in_file
play in_file gain -n
sox stereo.wav -c 1 mono.wav
sox stereo.wav mono.wav channels
ffmpeg -i stero.wav -ac 1 mono.wav
sox in_file.wav out_left.wav remix 1
sox in_file.wav out_right.wav remix 2
sox -M left_file_ right_file_ stereo_file
sox -m in_file_1 in_file_2 in_file_3 out_file
# Extract channels 2, 4, and 5
sox in_file out_file remix 2 4 5
ffmpeg -i high.wav -ar 44100 low.wav
sox high.wav -r 44100 low.wav
sox high.wav low.wav rate 44100
sox high.wav -r 48k low.wav
sox high.wav low.wav rate 48k
sox in_file -C 256 output.mp3
ffmpeg -i high.wav -c:a pcm_s16le low.wav
Source files in raw
, output files in wav
, convert to 16 bit 44.1kHz Wave file.
for f in raw/*;
do
ffmpeg -i "${f}" -vn -c:a pcm_s16le -ar 44100 "wav/${f#raw/}.wav";
done
ffmpeg in_file.wav out_file.mp3
sox in_file.wav out_file.mp3
sox in_file out_file norm
sox in_file out_file norm -0.1
sox -v 2.0 quiet.wav loud.wav
sox -v 0.5 loud.wav quiet.wav
Split a file into n second chunks
sox in_file out_file trim 0 n : newfile : restart
sox in_file out_file trim 0 n
sox in_file out_file trim 0 -n
sox in_file_1 in_file_2 in_file_3 out_file
This uses sox to concatenate any matching wave files in a directory.
Note that the wav file format is limited to 4GB due to a 32-bit header.
# Delete temp.wav if it exists
if test -f temp.wav; then
rm temp.wav
fi
# Delete out.wav if it exists
if test -f out.wav; then
rm out.wav
fi
# List Wave files and sort by name
ls *.wav | sort -n | while read l;
do
echo "$l"
if [ ! -f temp.wav ]
then
cp $l temp.wav
else
sox temp.wav $l out.wav
rm temp.wav
cp out.wav temp.wav
fi
done
#n can be a decimal value
sox in.wav out.wav speed n
sox -n output.wav synth 1 noise
sox -n output.wav synth 1 pinknoise
sox -n output.wav synth 1 sine 440
sox -n swept-sine.wav synth 10 sine 2/20000
sox -n -r 48000 dirac.wav synth 1s square pad 0 47999s
sox in_file out_file reverse
sox in_file -n spectrogram -o out_file.png
Set the output format of ffmpeg to sox
.
ffmpeg -hide_banner -i <infile> -f sox - | sox -p <outfile> trim 0 10
-p
provides a conveninet shortcut for setting the format using -t sox -
.
sox <infile> -p trim 10 20 | sox -p <outfile> trim 0 10
sox <infile> -p trim 10 20 | sox -p -p trim 0 10 | sox -p <outfile> trim 5 6
Install the sox MP3 handler
Ubuntu
sudo apt-get install libsox-fmt-mp3