How to play a sound or tone when a program ends?

AFAIK R’s beep simply sends an \a to the terminal — see @anon94023334’s much simper solution for this above.

2 Likes

I’m pretty much good on this topic, jm’s “play_a_tune2” solution above worked; the only thing I can’t seem to do is set the filepath equal to a String variable, and then call it with play_a_tune2(variable); I have to call it with play_a_tune(<whole messy file path>). (But that’s not a big problem.)

Thanks everyone!

println("\007") doesn’t work for me on Ubuntu 18.04, but after:

sudo apt install beep
sudo modprobe pcspkr

the awesome power of beep is available, assuming that you have an old fashion motherboard speaker installed:

run(`beep -l 1000`)
1 Like

Most distros don’t enable the bell events on a desktop by default. Eg for PulseAudio something like PulseAudio - ArchWiki can be used to re-enable it.

On Linux, I would just call a command line player with a path.

There are Linux commands such as spd-say:

run(`spd-say 'Stay alert!'`)

Although using this is not advisable if you are afraid of robotic voices.

1 Like

Paul, Tamas, Zlatan, I am running Julia in atom in Windows 7 & Windows 10 on my 2 computers. I don’t know how to implement any of those last 3 suggestions in Windows.

I don’t use Windows so I can’t provide specific help, but I would just search for solutions playing audio from the command line on Windows, eg

https://stackoverflow.com/questions/1569765/how-to-play-audio-file-on-windows-from-command-line

I actually tried to say, that you can replace \$sound.Play\(\) by \$sound.PlayLooping\(\) as described in the link. It allows the file to be played in a loop.
The link is from the Powershell documentation from the here used SoundPlayer class. One can look it up for further and more complex usecases.

‘wavplay’ from WAV · Julia Packages worked fine for me.

using WAV
y, fs = wavread("foo.wav")

Anywhere in your program where you want to play the sound in the file foo.wav, just add the line.

wavplay(y, fs)

Also you can try Beep.jl (GitHub - arubhardwaj/Beep.jl: Notify me, Julia! I cannot remember after executing the script!!).

1 Like

Linking related post.

Nice! But how to cut off the music being played, without closing the REPL? :smiley:

“Nice! But how to cut off the music being played, without closing the REPL?”

Step 1. Get to computer
Step 2. Turn the speaker volume to zero

1 Like

It’s indeed a better method than blocking my ears.

On macOS, I sometimes put this at the bottom of my scripts/notebooks:

if Sys.isapple()
    run(`osascript -e "tell app \"Music\" to play"`)
end

I queue up whatever music I want in the Music app. This works really well to get my attention. You can also do it the other way around, having music playing and then pausing it when done. That has an added benefit: if your computer usually goes to sleep when you have a long run, playing music prevents that from happening. But then when it’s done and the music pauses, your computer can go to sleep. Silly, but it works in a pinch.

(The first time you do this command, you might get a pop-up asking for permission to let Music be controlled in this way.)

I find that playing music consumes cycles and memory (Windows 11, not sure 'bout da Mac). I play a jingle when done…