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
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")) 1 Comment
autoplay=True parameter. Like this: IPython.display.Audio("my_audio_file.mp3", autoplay=True)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
buffer = StringIO.StringIO();wavfile.write(buffer, sample_rate, samples)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
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