7pm: Terminal Tunes: Creating Music with Sox

"No Stairway, denied!", As evening settles in, it's time for a touch of creativity and fun. In this article, we will explore how to harness the power of your Terminal to generate simple beep sounds. While we won't be spinning Top 40 hits or recreating Stairway to heaven, this guide offers an entertaining introduction to auditory output via the Terminal, demonstrating just a fragment of its vast potential.

What does this article replace?

This article provides a playful diversion from standard GUI-based sound output tools. Instead of interacting with an application interface, you'll be experimenting with simple auditory outputs directly from your Terminal!

How long will it take to set up?

The process of installing Homebrew and Sox should take about 15 minutes. Writing and running the script could take another 10 minutes, depending on your comfort level with coding.

2 out of 5 stars

Difficulty Rating:

Scripting music from your terminal instead of using a traditional music creation tool? This task has a difficulty level of 2 out of 5!

Step 1: Install Sox

We will use sox, a powerful command-line tool for converting and playing audio files. Install it using Homebrew with:

brew install sox

Confirm that sox was correctly installed by typing the following command in the terminal:

sox --version

It should print the installed version of sox.

Step 2: Create the script

Open a text editor and paste the following bash script:

#!/bin/bash
# Smoke on the Water main riff
play -n synth 0.3 sin 392 vol 0.1
sleep 0.1
play -n synth 0.3 sin 466.16 vol 0.1
sleep 0.1
play -n synth 0.3 sin 523.25 vol 0.1
sleep 0.4
play -n synth 0.3 sin 392 vol 0.1
sleep 0.1
play -n synth 0.3 sin 466.16 vol 0.1
sleep 0.1
play -n synth 0.3 sin 523.25 vol 0.1
sleep 0.1
play -n synth 0.3 sin 466.16 vol 0.1
sleep 0.4
play -n synth 0.3 sin 392 vol 0.1
sleep 0.1
play -n synth 0.3 sin 466.16 vol 0.1
sleep 0.1
play -n synth 0.3 sin 523.25 vol 0.1
sleep 0.1
play -n synth 0.3 sin 466.16 vol 0.1
sleep 0.1
play -n synth 0.3 sin 392 vol 0.1

Save the file with a .sh extension, for example, smokeonthewater.sh.

Understanding the Code

Let's take a moment to understand what the code is doing:

  • play: This is a command provided by sox. It's used to play any audio files or sounds defined by other parameters in the command.
  • -n: This flag stands for "null file" - this means that sox is not playing an audio file but rather a sound we're defining.
  • synth 0.3 sin 392: The synth parameter generates a synthetic tone. 0.3 is the duration of the sound in seconds. sin 392 creates a sine wave at 392 Hz, which corresponds to the musical note G4.
  • vol 0.1: This sets the volume level of the sound. The range is from 0 (silent) to 1 (maximum volume).
  • sleep 0.1: This is not a sox command but a general Unix command that pauses execution for a specified number of seconds. In this case, it's used to create a brief rest between notes.

So, play -n synth 0.3 sin 392 vol 0.1 plays a G4 note for 0.3 seconds at a low volume, and sleep 0.1 creates a pause after the note has played.

Understanding Musical Notes and Octaves

In the world of music, a note is a symbol representing a musical sound. Each note corresponds to a certain frequency of sound wave vibration. The human ear can interpret these vibrations as different pitches. For instance, the note A4 represents a sound wave that vibrates at 440 Hz (cycles per second), which is perceived as a particular pitch by the human ear.

Here are some examples of musical notes and their corresponding frequencies:

  • C4 (Middle C): 261.63 Hz
  • E4: 329.63 Hz
  • D4: 293.66 Hz
  • F4: 349.23 Hz
  • G4: 392.00 Hz
  • A4: 440.00 Hz
  • B4: 493.88 Hz
  • C5 (One octave above Middle C): 523.25 Hz

The number following the note letter represents the octave in which that note resides. This is part of a system called "Scientific Pitch Notation."

  • C4 represents Middle C, the C note in the fourth octave on a piano.
  • D4 is the D note in the same octave.
  • C5 would be the C note one octave higher than Middle C.
  • B3 would be the B note one octave lower than Middle C.

The concept of octaves is a fundamental aspect of music theory. In music, moving up one "octave" doubles the frequency. So if A4 is 440 Hz, then A5 would be 880 Hz. Conversely, moving down an octave halves the frequency. So A3 would be 220 Hz.

Step 3: Make the script executable

Open the Terminal and navigate to the location where you saved the file using the cd command. For instance, if you saved it in your Documents folder, you can get there with:

cd ~/Documents

Then, make the script executable with the chmod command:

chmod +x smokeonthewater.sh

Step 4: Run the script

Now, you can run the script with:

./smokeonthewater.sh

Remember to replace smokeonthewater.sh with the actual name of your script. You should now hear a simple rendition of "Smoke on the Water"'s main riff played through your system's audio output.


You've just created and played a piece of music using your Terminal. You're not just working with code; you're using it as a creative tool. We're excited to see what musical masterpiece you'll script next!