Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Playing a sound in a ipython notebook

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?

like image 930
user1436340 Avatar asked Apr 26 '13 17:04

user1436340


People also ask

Can Librosa play audio?

Playing a real sound Of course, we can also play back real recorded sounds in the same way. Your browser does not support the audio element.

How do I play a WAV file in Python?

python-sounddevice In order to play WAV files, numpy and soundfile need to be installed, to open WAV files as NumPy arrays. The line containing sf. read() extracts the raw audio data, as well as the sampling rate of the file as stored in its RIFF header, and sounddevice.


2 Answers

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")) 
like image 123
Oriol Nieto Avatar answered Sep 18 '22 14:09

Oriol Nieto


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

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

like image 40
Matt Avatar answered Sep 21 '22 14:09

Matt