54

I would like to be able to play a sound file in a ipython notebook. My aim is to be able to listen to the results of different treatments applied to a sound directly from within the notebook. Is this possible? If yes, what is the best solution to do so?

5 Answers 5

109

The previous answer is pretty old. You can use IPython.display.Audio now. Like this:

import IPython IPython.display.Audio("my_audio_file.mp3") 

Note that you can also process any type of audio content, and pass it to this function as a numpy array.

If you want to display multiple audio files, use the following:

IPython.display.display(IPython.display.Audio("my_audio_file.mp3")) IPython.display.display(IPython.display.Audio("my_audio_file.mp3")) 
Sign up to request clarification or add additional context in comments.

1 Comment

Worth noting that if you want the sound to actually play automatically, then add the autoplay=True parameter. Like this: IPython.display.Audio("my_audio_file.mp3", autoplay=True)
13

A small example that might be relevant : http://nbviewer.org/5507501/the%20sound%20of%20hydrogen.ipynb

it should be possible to avoid gooing through external files by base64 encoding as for PNG/jpg...

6 Comments

Thanks for your answer, I don't manage using external files. (despite changing path, I get WARNING:root:404 GET /files/440.wav (127.0.0.1) ). I'll have a look at how to avoid the use of external file.
Hum, weird, have you change CWD ? when you run the notebook, is the 440.wav file created ?
Sorry for the delay. I was using an old version of ipython. Everything works great now. Having player directly in notebook is very neat to play around with sounds
Btw, I updated (a few hours ago) to avoid going through the file system : nbviewer.ipython.org/urls/raw.github.com/Carreau/posts/master/… happy coincidence :-)
@Matt: why do you need to reimplement scipy.io.wavfile.write? It accepts string buffer already... I.e. buffer = StringIO.StringIO();wavfile.write(buffer, sample_rate, samples)
|
4

The code:

import IPython IPython.display.Audio("my_audio_file.mp3") 

may give an error of "Invalid Source" in IE11, try in other browsers it should work fine.

Comments

3

If the sound you are looking for could be also a "Text-to-Speech", I would like to mention that every time a start some long process in the background, I queue the execution of a cell like this too:

from IPython.display import clear_output, display, HTML, Javascript display(Javascript(""" var msg = new SpeechSynthesisUtterance(); msg.text = "Process completed!"; window.speechSynthesis.speak(msg); """)) 

You can change the text you want to hear with msg.text.

Comments

0

The other available answers added an HTML element which I disliked, so I created the ringbell, which gets you both play a custom sound as such:

from ringbell import RingBell RingBell( sample = "path/to/sample.wav", minimum_execution_time = 0, verbose = True ) 

and it also gets you a one-lines to play a bell when a cell execution takes more than 1 minute (or a custom amount of time for that matter) or is fails with an exception:

import ringbell.auto 

You can install this package from PyPI:

pip install ringbell 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.