Skip to content

Instantly share code, notes, and snippets.

@ideoforms
Last active November 7, 2024 11:43
Show Gist options
  • Save ideoforms/d64143e2bad16b18de6e97b91de494fd to your computer and use it in GitHub Desktop.
Save ideoforms/d64143e2bad16b18de6e97b91de494fd to your computer and use it in GitHub Desktop.
sox cheat sheet
################################################################################
# sox cheat sheet
################################################################################
# Example commands for the sox command-line audio processing tool,
# for manipulating or batch processing audio files.
################################################################################
# Daniel Jones <[email protected]>
################################################################################
################################################################################
#### GENERAL USAGE AND HELP
################################################################################
# General usage.
sox [general_flags] [input_flags][infile] [output_flags][outfile] [effects...]
# Get help.
sox
# Deeper commentary on each function and effect.
man sox
# Help on arguments for a particular effect.
sox --help-effect reverb
################################################################################
#### GETTING INFORMATION
################################################################################
# Display general information (samplerate, bit depth, duration, ...)
sox --info input.wav
soxi input.wav
# Process the audio contents to calculate properties such as RMS.
sox input.wav -n stats
################################################################################
#### CONVERTING BETWEEN FILE FORMATS
################################################################################
# sox automatically infers file type from the extension.
# Convert to 8kHz, 1-channel wav.
sox input.wav -r 8000 -c 1 output.wav
# Convert to mp3.
sox input.wav output.mp3
# Convert to mp3 with specified bitrate in kbps.
sox input.wav -C 256 output.mp3
# Convert from a .raw file of known format to a wav file.
sox -r 44100 -e signed-integer -b 16 raw-audio.raw raw-audio.wav
################################################################################
#### SYNTHESIZING AUDIO
################################################################################
# Generate 1 second of white noise.
sox -n output.wav synth 1 noise
# Generate 1 second of pink noise.
sox -n output.wav synth 1 pinknoise
# Generate a 1-second sine tone.
sox -n output.wav synth 1 sine 440
# Generate a 10-second exponential sine sweep
sox -n -r 44100 sine-sweep.wav synth 10 sine 40/20000
# Generate a Dirac impulse, padded to 1 second
sox -n -r 44100 impulse.wav synth 1s square pad 0 44099s
################################################################################
#### PLAYING AUDIO
################################################################################
# Play an audio file through the default system audio output.
play input.wav
# Attenuate audio playback
play input.wav gain -12
# Play synthesized audio.
play -n synth sine 440 trim 0 1 gain -12
# Play a streaming audio source from the web
play -t mp3 https://infraordinary.fiveradiostations.com:8443/stream
################################################################################
#### RECORDING AUDIO
################################################################################
# These examples are specifically for the macOS coreaudio driver
# List available audio input devices
sox -V -t coreaudio null -n 2>&1 | grep "Found Audio" | cut -d'"' -f2
# Replace "MacBook Pro Microphone" with the audio input device you wish to use
sox -t coreaudio "MacBook Pro Microphone" recording.wav
################################################################################
#### COMBINING AUDIO
################################################################################
# Combine two files by concatenation.
sox a.wav b.wav c.wav concatenated.wav
# Combine two files by mixing their contents.
sox -m a.wav b.wav c.wav mixed.wav
# Merge two mono files into a two-channel stereo file
sox -M left.wav right.wav stereo.wav
################################################################################
#### MODIFYING AUDIO
################################################################################
# Reduce level by 12dB
sox speech.wav output.wav gain -12
# Crop to the first 1 second of the file.
sox speech.wav output.wav trim 0 1
# Reverse the contents.
sox speech.wav output.wav reverse
# Normalise the contents to 0dBFS.
sox speech.wav output.wav norm
# Equaliser (-6dB @ 100Hz, -24dB @ 8000Hz)
sox speech.wav output.wav bass -6 100 treble -24 8000
# Add room modelling reverb.
sox speech.wav output.wav reverb 50 50 100
# Trim digital silence from start and end.
sox input.wav trimmed/output.wav silence 1 0.1 0 1 0.1 0
# Extract one channel from a multichannel file (numbering is from 1)
sox stereo.wav right.wav remix 2
# Extract the first, third and fifth channels from a multichannel file
sox multichannel.wav subset.wav remix 1 3 5
################################################################################
#### VISUALISATION
################################################################################
# Generate a spectrogram (output to spectrogram.png)
sox speech.wav -n spectrogram
@Gahlord
Copy link

Gahlord commented Apr 27, 2023

You are my hero. Thank you for making this!

@ed-xmos
Copy link

ed-xmos commented Aug 21, 2023

I love sox, but the options are hard to fathom. This is VERY helpful - Thanks!

@SimZhou
Copy link

SimZhou commented Jul 24, 2024

Great!!!! Thanks!!

@turbulentie
Copy link

turbulentie commented Sep 27, 2024

You can update your cheatsheet with:

  • Make 35secs preview of audiofile from start:

sox input.wav output.wav trim 0 00:35

sox -n dtmf-1.wav synth 0.1 sine 697 sine 1209 channels 1
sox -n dtmf-2.wav synth 0.1 sine 697 sine 1336 channels 1
sox -n dtmf-3.wav synth 0.1 sine 697 sine 1477 channels 1
sox -n dtmf-4.wav synth 0.1 sine 770 sine 1209 channels 1
sox -n dtmf-5.wav synth 0.1 sine 770 sine 1336 channels 1
sox -n dtmf-6.wav synth 0.1 sine 770 sine 1477 channels 1
sox -n dtmf-7.wav synth 0.1 sine 852 sine 1209 channels 1
sox -n dtmf-8.wav synth 0.1 sine 852 sine 1336 channels 1
sox -n dtmf-9.wav synth 0.1 sine 852 sine 1477 channels 1

sox -n dtmf-0.wav synth 0.1 sine 941 sine 1209 channels 1
sox -n dtmf-star.wav synth 0.1 sine 941 sine 1336 channels 1
sox -n dtmf-pound.wav synth 0.1 sine 941 sine 1477 channels 1

sox -n dtmf-A.wav synth 0.1 sine 697 sine 1633 channels 1
sox -n dtmf-B.wav synth 0.1 sine 770 sine 1633 channels 1
sox -n dtmf-C.wav synth 0.1 sine 852 sine 1633 channels 1
sox -n dtmf-D.wav synth 0.1 sine 941 sine 1633 channels 1
   
sox -n dtmf-us-busy.wav synth 10 sine 480 sine 620 channels 1
sox -n dtmf-rbt-US.wav synth 10 sine 440 sine 480 channels 1
sox -n dtmf-uk-us-dialtone.wav synth 11 sine 350 sine 440 channels 1
sox -n dtmf-uk-busy.wav synth 10 sine 400 channels 1
sox -n dtmf-uk-ringback.wav synth 10 sine 450 channels 1
sox -n dtmf-eur-dialtone.wav synth 10 sine 425 channels 1
sox -n dtmf-eur-busy.wav synth 10 sine 425 channels 1
sox -n dtmf-eur-ringback.wav synth 10 sine 425 channels 1
  • Converting PCM to PDM (DSD64):

sox input.wav output.dsf rate -v 2822400 sdm -f sdm-8

  • Converting PCM to PDM (DSD128):
    sox input.wav output.dsf rate -v 5644800 sdm -f clans-4

Note: Two last commands require SoX with DSD support

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment