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

**URL:** <https://discourse.julialang.org/t/how-to-play-a-sound-or-tone-when-a-program-ends/41239>\
**Category:** General Usage\
**Tags:** sound\
**Created:** [June 12, 2020, 2:39am UTC](https://discourse.julialang.org/t/how-to-play-a-sound-or-tone-when-a-program-ends/41239 "2020-06-12T02:39:48Z")\
**Posts on this page:** 15\
**Page:** 2

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [June 13, 2020, 7:33am UTC](https://discourse.julialang.org/t/how-to-play-a-sound-or-tone-when-a-program-ends/41239/21 "2020-06-13T07:33:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![CosmoProf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cosmoprof/32/15516_2.png) [@CosmoProf](https://discourse.julialang.org/u/CosmoProf)\
**Post date:** [June 13, 2020, 2:40pm UTC](https://discourse.julialang.org/t/how-to-play-a-sound-or-tone-when-a-program-ends/41239/22 "2020-06-13T14:40:18Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)\
**Post date:** [June 14, 2020, 10:17am UTC](https://discourse.julialang.org/t/how-to-play-a-sound-or-tone-when-a-program-ends/41239/23 "2020-06-14T10:17:41Z")

</div>

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

```julia
sudo apt install beep
sudo modprobe pcspkr

```

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

```julia
run(`beep -l 1000`)

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [June 14, 2020, 11:31am UTC](https://discourse.julialang.org/t/how-to-play-a-sound-or-tone-when-a-program-ends/41239/24 "2020-06-14T11:31:22Z")

</div>

Most distros don’t enable the bell events on a desktop by default. Eg for PulseAudio something like [PulseAudio - ArchWiki](https://wiki.archlinux.org/index.php/PulseAudio#X11_Bell_Events) can be used to re-enable it.

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

---

<div class="post-metadata">

**Author:** ![anon37204545](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon37204545](https://discourse.julialang.org/u/anon37204545)\
**Post date:** [June 14, 2020, 2:54pm UTC](https://discourse.julialang.org/t/how-to-play-a-sound-or-tone-when-a-program-ends/41239/25 "2020-06-14T14:54:47Z")

</div>

There are Linux commands such as `spd-say`:

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

```

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

---

<div class="post-metadata">

**Author:** ![CosmoProf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cosmoprof/32/15516_2.png) [@CosmoProf](https://discourse.julialang.org/u/CosmoProf)\
**Post date:** [June 14, 2020, 3:50pm UTC](https://discourse.julialang.org/t/how-to-play-a-sound-or-tone-when-a-program-ends/41239/26 "2020-06-14T15:50:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [June 14, 2020, 5:46pm UTC](https://discourse.julialang.org/t/how-to-play-a-sound-or-tone-when-a-program-ends/41239/27 "2020-06-14T17:46:09Z")

</div>

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](https://stackoverflow.com/questions/1569765/how-to-play-audio-file-on-windows-from-command-line)

---

<div class="post-metadata">

**Author:** ![ludwig-austermann](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ludwig-austermann/32/11093_2.png) [@ludwig-austermann](https://discourse.julialang.org/u/ludwig-austermann)\
**Post date:** [June 17, 2020, 1:07am UTC](https://discourse.julialang.org/t/how-to-play-a-sound-or-tone-when-a-program-ends/41239/28 "2020-06-17T01:07:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cesquerre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cesquerre/32/205867_2.png) [@cesquerre](https://discourse.julialang.org/u/cesquerre)\
**Post date:** [December 19, 2023, 11:51am UTC](https://discourse.julialang.org/t/how-to-play-a-sound-or-tone-when-a-program-ends/41239/29 "2023-12-19T11:51:49Z")

</div>

‘wavplay’ from [WAV · Julia Packages](https://juliapackages.com/p/wav) worked fine for me.

```julia
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.

```julia
wavplay(y, fs)

```

Also you can try Beep.jl ([GitHub - arubhardwaj/Beep.jl: Notify me, Julia! I cannot remember after executing the script!!](https://github.com/arubhardwaj/Beep.jl/tree/main)).

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 19, 2023, 2:57pm UTC](https://discourse.julialang.org/t/how-to-play-a-sound-or-tone-when-a-program-ends/41239/30 "2023-12-19T14:57:52Z")

</div>

Linking [related post](https://discourse.julialang.org/t/is-there-something-so-i-can-play-a-noise-when-julia-finished-running-like-i-have/55107).

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [July 2, 2024, 7:29am UTC](https://discourse.julialang.org/t/how-to-play-a-sound-or-tone-when-a-program-ends/41239/31 "2024-07-02T07:29:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [July 3, 2024, 1:50am UTC](https://discourse.julialang.org/t/how-to-play-a-sound-or-tone-when-a-program-ends/41239/32 "2024-07-03T01:50:17Z")

</div>

“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

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [July 3, 2024, 4:23pm UTC](https://discourse.julialang.org/t/how-to-play-a-sound-or-tone-when-a-program-ends/41239/33 "2024-07-03T16:23:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tuckermcclure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tuckermcclure/32/1187_2.png) [@tuckermcclure](https://discourse.julialang.org/u/tuckermcclure)\
**Post date:** [August 3, 2024, 12:27am UTC](https://discourse.julialang.org/t/how-to-play-a-sound-or-tone-when-a-program-ends/41239/34 "2024-08-03T00:27:20Z")

</div>

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

```julia
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.)

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [August 3, 2024, 1:47am UTC](https://discourse.julialang.org/t/how-to-play-a-sound-or-tone-when-a-program-ends/41239/35 "2024-08-03T01:47:35Z")

</div>

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

[Previous page](https://discourse.julialang.org/t/how-to-play-a-sound-or-tone-when-a-program-ends/41239.md?page=1)
