audio notes

Jump to navigationJump to search

Make audio test files using Sox

The different synth types are sine, square, triangle, sawtooth, trapezium, exp, [white]noise, tpdfnoise, pinknoise, brownnoise, pluck.

Play a 3 second sine wave tone at a given frequency (440 Hz in this example).

    play -n synth 3 sine 440

Play each string of a 6-string guitar in standard tuning

for note in E2 A2 D3 G3 B3 E4; do
    play -n synth 3 pluck $note

Constant tone mixed with a swept tone

play --bits=16 -n synth 5 sine 1000 synth 4 sine mix 100-1000 channels 1 gain -3
play --bits=16 -n synth 9 sine 1000 synth 2 sine mix 1-2000 synth 2 sine mix 2000-1 channels 1 gain -3
# Same thing saved to a file:
sox --bits=16 -n test-sound.wav synth 9 sine 1000 synth 2 sine mix 1-2000 synth 2 sine mix 2000-1 channels 1 gain -3

Almost play a tune

for note in C3 F4 A4 F4 C3 F4 A4 F4 C3 F4 F4 F4 E4 D4 C3; do
    play -n synth 0.25 pluck $note

Remove/reduce background noise and hiss from an audio file

This is a two-step process; although, it can be run as a pipeline. First you need to analyze the audio to build up a profile of the noise. You want to sample a range that features only the background noise you want to remove. Typically you can sample the first 1 second of an audio file. This doesn't always work, but it mostly works.

sox audio_recording.wav -n trim 0 1 noiseprof | play audio_recording.wav noisered - 0.2

Trim silent gaps from audio

This removes silent sections from the beginning, middle, and end. Useful for compressing long audio logs that may contain many long pauses.

sox audio_recording.wav silence_removed.wav silence 1 0.1 1% -1 0.5 1%

Create a spectrogram (sonogram, FFT, etc.) of an audio file

spectrogram spectrograph sonogram sonograph spectral plot spectrum FFT Fourier Transform

The rate 6k option will narrow the frequency range view to the band most sensitive for human hearing. This cuts off frequencies above 3 kHz (half the sample rate of 6k). If you want the full frequency range then leave off the rate 6k option.

The -n is the NULL file option. This simply tells Sox that we don't want to actually create a new sound file. We are just analyzing the input file.

sox audio_recording.wav -n rate 6k spectrogram -t "Spectrogram of audio_recording.wav" -o spectrogram_20150531.png
# For a white background use '-l' option:
sox audio_recording.wav -n rate 6k spectrogram -l -t "Spectrogram of audio_recording.wav" -o spectrogram_20150531.png

Record audio from the microphone

Sox is probably the most universal tool for recording, manipulating, and playing back sound.

Record audio using Sox

Sox works on Linux and OS X (through Brew).

Simple stereo recording:

rec --channels 2 audio_recording.wav

Using Sox (this also works on OS X). This splits on silent gaps. This records audio. It does not start recording until it detects sound. It splits the audio separated by 2 seconds of silence into separate files. It stops recording after 10 seconds of silence. You may set the 0:10 to 1:00 to choose 1 minute instead of 10 seconds.

rec -V3 -p \
 | sox -p -p silence 1 0.50 0.1% 1 0:10 0.1% \
 | sox -p audio_recording.wav silence 1 0.50 0.1% 1 2.0 0.1% : newfile : restart

Linux Only

arecord -vv -fdat audio_recording.wav 

Playback audio OX X using Sox

Note that where an output filename is require you may substitute -d or -t coreaudio (for Mac OS X). These seem to be equivalent. The -d option seems to be the more general purpose style since it will automatically pick the correct sound output on a Mac and Linux.

Both examples below play audio and both will automatically detect the audio stream type. The play command is the easier to remember version. You may have special reasons for wanting to use the sox command alternative.

play audio_recording.wav
cat audio_recording.wav | sox - -t coreaudio

Play noise

These are all equivalent using /dev/urandom.

# From a file or device file.
sox -t raw -r 44100 -b 16 -e unsigned-integer /dev/urandom -d
sox -t raw -r 44100 -b 16 -e unsigned-integer /dev/urandom -t coreaudio
# Using a pipe...
cat /dev/urandom | sox -t raw -r 44100 -b 16 -e unsigned-integer - -d
cat /dev/urandom | sox -t raw -r 44100 -b 16 -e unsigned-integer - -t coreaudio

This uses Sox's built-in noise generator:

play --null synth whitenoise

This sounds best:

play --channels 2 --null --show-progress synth 01:00 brownnoise band -n 400 499 tremolo 0.1 70 reverb 19 bass -11 treble -1 vol 12dB repeat 19