Play a sound in Jupyter Notebook

I want my Jupyter notebook to play a sound when a cell is done, what is the easiest way to do this?

long_running_function()
play_sound()

Haven’t tried myself, but if you have e.g. an IOStream of an mp3, you might be
able to just display it with the appropriate mimetype. Something like:

open("mymp3.mp3", "r") do f
    display("audio/mpeg", f)
end

Don’t have access to a working jupyter installation at the moment, so I can’t
try it out. Let me know if that works.

That would probably just give you a widget with the audio file, though maybe there’s a way to set an auto-play flag on it or something?

If the notebook is running on the same machine as Julia, you can use PortAudio/LibSndFile to load the sound and play it, though things are a bit of a mess right now with respect to the 1.0 update. You’ll need to use master for most of the JuliaAudio packages.

that uses the julia process, right? In my case that process runs on another machine.

What I thought it would do is send the jupyter process a bytestream for the mp3 file and the mimetype associated with the bytestream. The notebook then chooses how to display it itself. But I think @ssfrr is correct that it will give a widget rather than an auto-play. I know that jupyter itself has an option to trigger auto-playing of audio, but I don’t know how to do that through IJulia.

open("http://audiosoundclips.com/wp-content/uploads/2011/12/Bleep4.mp3", "r") do f
    display("audio/mpeg", f)
end

also complains about not being able to open the file.

Well you’re trying to open a URL with something that is only looking at your local file-system. You’re gonna need an http lib if you want to play something from a website.

You probably need to:

  • use WebIO.jl (and host sound file in /assets)
  • add some javascript that plays music (and handle it with Julia)

?

using WebIO
playsound() = dom"script"("var audio = new Audio('http://audiosoundclips.com/wp-content/uploads/2011/12/Bleep4.mp3');audio.play();")

playsound()
3 Likes

Found this thread actually looking for how to have a player widget on a notebook, ended up with this after a python example:

using WAV
wavwrite(0.4*rand(2000, 2), "example2.wav", Fs=44100.0)

audioplayer(filepath) = """
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Simple Test</title>
    </head>
    
    <body>
    <audio controls="controls" style="width:600px" >
      <source src="$filepath" type="audio/wav" />
      Your browser does not support the audio element.
    </audio>
    </body>
    """

display("text/html", audioplayer("example2.wav"))
1 Like