# 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:** 20\
**Page:** 1

<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 12, 2020, 2:39am UTC](https://discourse.julialang.org/t/how-to-play-a-sound-or-tone-when-a-program-ends/41239/1 "2020-06-12T02:39:48Z")

</div>

Hi, I was wondering how to set a julia program to beep or play some tones or music when a long program finally finishes running.

(Googling this question leads to recommendations for old packages, like AudioIO, which aren’t maintained and don’t install properly.)

Thanks!

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [June 12, 2020, 2:43am UTC](https://discourse.julialang.org/t/how-to-play-a-sound-or-tone-when-a-program-ends/41239/2 "2020-06-12T02:43:57Z")

</div>

Probably a little outdated of an answer, but in Tetris.jl I used [Howler.js](https://howlerjs.com/) to play a sound:

- [tetris music player](https://github.com/djsegal/Tetris.jl/blob/689827ff24e3ecc7265205380bd5e52c34ad5efd/src/setup.jl#L97-L110)

This was just javascript that got rendered to the page a la:

```julia
HTML("""
<script>
  new Howl({
    src: ["pkg/Tetris/tetris-theme.mp3"],
  });
</script>
""")

```

---

<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 12, 2020, 2:48am UTC](https://discourse.julialang.org/t/how-to-play-a-sound-or-tone-when-a-program-ends/41239/3 "2020-06-12T02:48:37Z")

</div>

I need to know how to do this in a julia program running in atom.

---

<div class="post-metadata">

**Author:** ![asinghvi17](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asinghvi17/32/8272_2.png) [@asinghvi17](https://discourse.julialang.org/u/asinghvi17)\
**Post date:** [June 12, 2020, 5:18am UTC](https://discourse.julialang.org/t/how-to-play-a-sound-or-tone-when-a-program-ends/41239/4 "2020-06-12T05:18:20Z")

</div>

You could try Alert.jl, it works pretty well for me!

---

<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 12, 2020, 5:29am UTC](https://discourse.julialang.org/t/how-to-play-a-sound-or-tone-when-a-program-ends/41239/5 "2020-06-12T05:29:00Z")

</div>

Thanks but that looks like an alert message printout (which is easy to do anyway). I’m looking for something that plays a sound that I can hear when not looking right at the computer.

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [June 12, 2020, 5:38am UTC](https://discourse.julialang.org/t/how-to-play-a-sound-or-tone-when-a-program-ends/41239/6 "2020-06-12T05:38:35Z")

</div>

This is really basic, but if you’re doing this from the REPL in a terminal, try `println("\007")`.

(Just saw you were using Atom. I don’t know whether this will work there. I confirmed it worked in a REPL on my mac.)

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [June 12, 2020, 7:11am UTC](https://discourse.julialang.org/t/how-to-play-a-sound-or-tone-when-a-program-ends/41239/7 "2020-06-12T07:11:32Z")

</div>

If you’re on a Mac, you can define spoken alerts with this Julia function:

```julia
say(what) = run(`osascript -e "say \"$(what)\""`, wait=false)

```

Then:

```julia
say("Finished - you'd better come and take a look....")

```

If it’s music you’re after, then try:

```julia
play() = run(`osascript -e "tell app \"Music\" to play"`, wait=false)

```

which will resume your music you’ve been playing in ~~iTunes~~ Music. (You’ll have to give permission for this to happen…)

---

<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 12, 2020, 7:30am UTC](https://discourse.julialang.org/t/how-to-play-a-sound-or-tone-when-a-program-ends/41239/8 "2020-06-12T07:30:40Z")

</div>

Sbromberger, the sound is nothing fancy, but it does work when executed as a program (not just from the REPL).

Cormullion, thanks, but no Mac, I’m working from 2 Windows computers (one running Win 7.0 and one running Win 10.0).

Ideally, it would be nice to take some interesting mp3 or wav soundclip I could find & download from the web, and play it at when the program ends. For now I’m repeat-playing `println("\007")` in a `while` loop.

---

<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 12, 2020, 8:13am UTC](https://discourse.julialang.org/t/how-to-play-a-sound-or-tone-when-a-program-ends/41239/9 "2020-06-12T08:13:10Z")

</div>

I am also using Windows (10) and I use a powershell script to accomplish that:

````julia
function sound_done()
    script = ```\$sound = New-Object System.Media.SoundPlayer\;
                \$sound.SoundLocation = "<sound file location>"\;
                \$sound.Play\(\)```
    run(`powershell /c $script`)
end

````

You need to have a wav audio file and then replace `<sound file location>` with the file location.

---

<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 12, 2020, 8:31am UTC](https://discourse.julialang.org/t/how-to-play-a-sound-or-tone-when-a-program-ends/41239/10 "2020-06-12T08:31:51Z")

</div>

I just looked it up, you can also use `\$sound.PlayLooping\(\)` and other commands. You can find the documentation [here](https://docs.microsoft.com/en-us/dotnet/api/system.media.soundplayer?view=dotnet-plat-ext-3.1).

---

<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 12, 2020, 6:03pm UTC](https://discourse.julialang.org/t/how-to-play-a-sound-or-tone-when-a-program-ends/41239/11 "2020-06-12T18:03:10Z")

</div>

Ludwig, I looked at your 2nd suggestion (PlayLooping), and it led to about 10 pages of documentation with no clear examples to try out (not clear to me anyway).

Trying out your first method gives me an error no matter how I try to locate or specify the file. The directory I wanted to put the wav file in had a space in the path, and using quotes or slashes to keep the space in the path name didn’t work. Then I put it in the “home” directory (according to Julia at startup), and it still didn’t work:

````julia
function sound_done()
    script = ```\$sound = New-Object System.Media.SoundPlayer\;
                \$sound.SoundLocation = "file_example_WAV_1MG.wav"\;
                \$sound.Play\(\)```
    run(`powershell /c $script`)
end

sound_done()

````

I get the error:

```julia
file_example_WAV_1MG.wav : The term 'file_example_WAV_1MG.wav' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the 
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:70
+ ... .SoundPlayer; $sound.SoundLocation = file_example_WAV_1MG.wav; $sound ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (file_example_WAV_1MG.wav:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

```

That file, `"file_example_WAV_1MG.wav"`, is a sample audio file that plays fine when I click on it in Windows 7.

---

<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 12, 2020, 6:58pm UTC](https://discourse.julialang.org/t/how-to-play-a-sound-or-tone-when-a-program-ends/41239/12 "2020-06-12T18:58:18Z")

</div>

Actually I DID finally get this working:

````julia

function Play_A_Tune(SoundFileName1)
    script = ```\$sound = New-Object System.Media.SoundPlayer\;
                \$sound.SoundLocation = $(SoundFileName1)\;
                \$sound.Play\(\)```
    run(`powershell /c $script`)
end

````

With a call like:

```julia
Play_A_Tune("C:\\JuliaPro\\file_example_WAV_1MG.wav")

```

But I still can’t get it to run the audio file from a directory path with spaces in it. For example, none of these kinds of things seem to be working:

`Play_A_Tune("C:\\Data Area\\Folder\\file_example_WAV_1MG.wav") Play_A_Tune("C:\\Data\ Area\\Folder\\file_example_WAV_1MG.wav") Play_A_Tune("C:\\"Data Area"\\Folder\\file_example_WAV_1MG.wav")`

…no matter what I’ve tried.  
(It’s possible that one problem may be that my directory path is a really long path name with many subdirectories.)

---

<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:** [June 12, 2020, 7:05pm UTC](https://discourse.julialang.org/t/how-to-play-a-sound-or-tone-when-a-program-ends/41239/13 "2020-06-12T19:05:34Z")

</div>

> [@anon94023334](#):
>
> println(“\007”)

Works on Windows.

---

<div class="post-metadata">

**Author:** ![josephmarturano](https://avatars.discourse-cdn.com/v4/letter/j/3d9bf3/32.png) [@josephmarturano](https://discourse.julialang.org/u/josephmarturano)\
**Post date:** [June 12, 2020, 7:32pm UTC](https://discourse.julialang.org/t/how-to-play-a-sound-or-tone-when-a-program-ends/41239/14 "2020-06-12T19:32:29Z")

</div>

Did you try typing `raw` before the filepath? For example:

```julia
Play_A_Tune(raw"C:\\Data Area\\Folder\\file_example_WAV_1MG.wav")

```

When I want to target a specific file on Windows, I usually `Shift+Right Click` on the file in Windows Explorer, select `Copy as Path`, and then paste the path after first typing `raw` (as 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 12, 2020, 9:09pm UTC](https://discourse.julialang.org/t/how-to-play-a-sound-or-tone-when-a-program-ends/41239/15 "2020-06-12T21:09:27Z")

</div>

Hi Joseph M., good idea trying “raw”, I didn’t know about that option, but it didn’t work.

PetrKryslUCSD, I already implemented that, see above.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [June 12, 2020, 9:18pm UTC](https://discourse.julialang.org/t/how-to-play-a-sound-or-tone-when-a-program-ends/41239/16 "2020-06-12T21:18:42Z")

</div>

> [@josephmarturano](#):
>
> `raw"C:\\Data Area\\Folder\\file_example_WAV_1MG.wav"`

That doesn’t look right to me. If you have `raw""`, then you shouldn’t need to do `\\` to get a single backslash. As written, this will put literal double backslashes into the path. I’m not sure how Windows handles that.

```julia
julia> for character in raw"C:\\Data Area"
         println(character)
       end
C
:
\
\
D
a
t
a
 
A
r
e
a

```

Edit: unless powershell also needs the `\\` to escape the backslashes itself? In which case what you’ve written is probably correct.

---

<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 12, 2020, 10:08pm UTC](https://discourse.julialang.org/t/how-to-play-a-sound-or-tone-when-a-program-ends/41239/17 "2020-06-12T22:08:51Z")

</div>

Good idea Joseph, but changing the `\\` back to `\` doesn’t fix it.

Oddly, something like `FileName = "C:\\Data Area\\Folder\\file_example_WAV_1MG.wav"` IS working for commands like `writedlm` and `readdlm`, but NOT for `Play_A_Tune` here. Strange!

---

<div class="post-metadata">

**Author:** ![josephmarturano](https://avatars.discourse-cdn.com/v4/letter/j/3d9bf3/32.png) [@josephmarturano](https://discourse.julialang.org/u/josephmarturano)\
**Post date:** [June 13, 2020, 12:39am UTC](https://discourse.julialang.org/t/how-to-play-a-sound-or-tone-when-a-program-ends/41239/18 "2020-06-13T00:39:39Z")

</div>

Right, I suspect this is related to spaces in a path with powershell ([see this discussion](https://superuser.com/questions/881019/windows-powershell-doesnt-seem-to-accept-target-paths-with-spaces/881029)). I think the following will catch Windows folders with and without spaces:

````julia
function play_a_tune2(SoundFileName1)
    script = ```\$sound = New-Object System.Media.SoundPlayer\;
                \$sound.SoundLocation = \& \"$SoundFileName1\" \;
                \$sound.Play\(\)```
    run(`powershell /c $script`)
end

````

I tested this with a .wav file with two paths on my Windows 10 machine:

```julia
play_a_tune2("C:\\folder_without_spaces\\elephant.wav")

play_a_tune2("C:\\folder with spaces\\elephant.wav")

```

I hear an elephant with both function calls. Or I could also be going crazy.

---

<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, 3:31am UTC](https://discourse.julialang.org/t/how-to-play-a-sound-or-tone-when-a-program-ends/41239/19 "2020-06-13T03:31:39Z")

</div>

Yes Joseph, that does work in both cases, thanks!

Not sure why you’re hearing elephants though, might want to get your ears checked.

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [June 13, 2020, 4:16am UTC](https://discourse.julialang.org/t/how-to-play-a-sound-or-tone-when-a-program-ends/41239/20 "2020-06-13T04:16:16Z")

</div>

```julia
using RCall

beep() = begin
    R"""
      beepr::beep()
    """
end

beep()

```

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