Callysto.ca Banner

Open in Callysto

Including music and sounds

Music and sounds on a computer are stored as data files, usually with the file types .wav or .mp3. You can upload such a file onto your Jupyter hub account, and then access it with HTML code or a call to Python tools.

We give four examples to show the basics of how this works.

Example 1 - A local .wav file using html

We have uploaded a short music file onto this repo, called KolnShort.wav (a clip from Keith Jarret’s Koln concert).

A few lines of html code will create a tool on the screen, click to play.

%%html
<audio controls>
  <source src="KolnShort.wav" type="audio/wav">
Your browser does not support the audio element.
</audio>

Example 2 - A remote .wav file using html

You can also access a .wav stored online, and play it. Here we grab a short clip from an online resource at Stanford.

A few lines of html code will create a tool on the screen, click to play.

%%html
<audio controls>
  <source src="https://ccrma.stanford.edu/workshops/mir2014/audio/T37-vibraphone-8k.wav" type="audio/wav">
Your browser does not support the audio element.
</audio>

Example 3 - Python code to play a .wav sound

We can import a tool and play the sound within Python code, avoiding the HTML magic above. It works about the same.

from IPython.display import Audio  ## only import once per notebook

Audio(data='KolnShort.wav')

Example 4 - Python code to play a remote .wav sound

We can also use Python code to play a clip of sound from an online resource. It works about the same as the last example.

Audio(data='https://ccrma.stanford.edu/workshops/mir2014/audio/T37-vibraphone-8k.wav')

If you want to dive deeper into music and sound using Python on the computer, a good resource is here: https://musicinformationretrieval.com/ipython_audio.html

Callysto.ca License