How do I play sound in a notebook (preferably Pluto)?

I want to build a synthesizer in Julia, and I want to play the output inside the notebook, like this example for Jupyter+Python: IPython Cookbook - 11.7. Creating a sound synthesizer in the Notebook
I have searched how to play sound in Julia, and all of the libraries are either unmaintained (AudioIO.jl) or segfaults (PortAudio.jl)

4 Likes

There’s also JuliaMusic - eg things like https://github.com/JuliaMusic/Mplay.jl - as well as JuliaAudio. (The difference between music and audio is a question for another day… :smiley:)

3 Likes

The problem with Mplay is that its only a MIDI player, it doesn’t generate and play sounds by itself. And JuliaAudio doesn’t have any package that works with Julia 1.4.1 (JACKAudio.jl is deprecated, and PortAudio.jl does not officially support Julia 1.0 and above, and if I try to use it anyway, it segfaults).

And all of them plays the audio directly, it isn’t like Python notebooks where there is a play button in the notebook and the audio is playing through the browser

1 Like

Yes, it’s not all there at present, unfortunately.

But I noticed this on Twitter, so something is possible…

1 Like

The thing is that in that linked twitter thread, Julia controls Pure Data that actually generates and plays the sound, so again its not Julia that plays the sound, unlike Matlab play() and sound() functions.

To work around this I write the array into a file, then I run sox that reads from the file and plays the sound. Its a little bit round about, but it works perfectly both in REPL and Pluto

1 Like

If any of you find a Julia package that has basic types for waveforms and it does not play audio when you use it in Pluto, let me know! We can make this work pretty easily – I just need to add the audio MIME type.
fonsvdplas@gmail.com

2 Likes

This would be very nice, especially if it could be served online.

The easy way I found to do this was using PlutoUI

using WAV, PlutoUI

begin
	wavwrite(Int.(trunc.(y*2^15)), "audio.wav", Fs=sr, nbits=16)
	md"""$(LocalResource("audio.wav"))"""
end

somehow it doesn’t work if the raw Float data is written in wavwrite, so I needed to convert to Int16

1 Like