236

I am in a situation where my code takes extremely long to run and I don't want to be staring at it all the time but want to know when it is done.

How can I make the (Python) code sort of sound an "alarm" when it is done? I was contemplating making it play a .wav file when it reaches the end of the code...

Is this even a feasible idea? If so, how could I do it?

7

13 Answers 13

387

On Windows

import winsound duration = 1000 # milliseconds freq = 440 # Hz winsound.Beep(freq, duration) 

Where freq is the frequency in Hz and the duration is in milliseconds.

On Linux and Mac

import os duration = 1 # seconds freq = 440 # Hz os.system('play -nq -t alsa synth {} sine {}'.format(duration, freq)) 

In order to use this example, you must install sox.

On Debian / Ubuntu / Linux Mint, run this in your terminal:

sudo apt install sox 

On Mac, run this in your terminal (using macports):

sudo port install sox 

Speech on Mac

import os os.system('say "your program has finished"') 

Speech on Linux

import os os.system('spd-say "your program has finished"') 

You need to install the speech-dispatcher package in Ubuntu (or the corresponding package on other distributions):

sudo apt install speech-dispatcher 
Sign up to request clarification or add additional context in comments.

10 Comments

right You have to download it, I'll give you the link, let me find it...I just posted another one for macs that will actually speak to you, so it can tell you when it's done
thanks! do you happen to know if linux has one that will speak to you too?
mine says that say is not found, even though i've installed sox and play works. but i've found an alternative. import os os.system('espeak "your program has finished"')
@CharlieParker, if you look at the full answer, the first option is only for windows users. The others state how to on other environments, which includes OSX
TIL I can make my computer talk using the os package! So awesome!!
|
54
print('\007') 

Plays the bell sound on Linux. Plays the error sound on Windows 10.

4 Comments

This doesn't appear to work through ipython/Jupyter
does not work on VS code nor Jupyter notebooks on win10
Does not work on my computer (Linux Mint 20) !
Does not seem to work on Mac either.
45

This one seems to work on both Windows and Linux* (from this question):

def beep(): print("\a") beep() 

In Windows, can put at the end:

import winsound winsound.Beep(500, 1000) 

where 500 is the frequency in Herz and 1000 is the duration in miliseconds

To work on Linux, you may need to do the following (from QO's comment):

  • In a terminal, run cd /etc/modprobe.d then gksudo gedit blacklist.conf to edit /etc/modprobe.d/blacklist.conf as root.
  • Comment out the line that says blacklist pcspkr, then reboot.
  • Also check that the terminal preferences has the Terminal Bell option checked.

7 Comments

thanks! just to add, may need to turn on the terminal bell sound in ubuntu gnome for it to work.
@mtigger Could you please explain how to do that, so that we can update the answer... (or you can update)
in a terminal, type 'cd /etc/modprobe.d' then 'gksudo gedit blacklist.conf'. uncomment the line that says 'blacklist pcspkr', then reboot. check also that the terminal preferences has the 'Terminal Bell' checked.
how to check "that the terminal preferences has the 'Terminal Bell' checked" in ubuntu 13?
"\a" worked for me on the Ubuntu subsystem for Windows, thanks!!
|
25

I'm assuming you want the standard system bell, and don't want to concern yourself with frequencies and durations etc., you just want the standard windows bell.

import winsound winsound.MessageBeep() 

Comments

17

ubuntu speech dispatcher can be used:

import subprocess subprocess.call(['speech-dispatcher']) #start speech dispatcher subprocess.call(['spd-say', '"your process has finished"']) 

3 Comments

but, starting to play when process finished.
that's what the intended behaviour is, you want the sound when the process finishes
you can add -w argument to wait while spd-say finishes the phrase
15

Kuchi's answer didn't work for me on OS X Yosemite (10.10.1). I did find the afplay command (here), which you can just call from Python. This works regardless of whether the Terminal audible bell is enabled and without a third-party library.

import os os.system('afplay /System/Library/Sounds/Sosumi.aiff') 

Comments

11

Why use python at all? You might forget to remove it and check it into a repository. Just run your python command with && and another command to run to do the alerting.

python myscript.py && notify-send 'Alert' 'Your task is complete' && paplay /usr/share/sounds/freedesktop/stereo/suspend-error.oga 

or drop a function into your .bashrc. I use apython here but you could override 'python'

function apython() { /usr/bin/python $* notify-send 'Alert' "python $* is complete" paplay /usr/share/sounds/freedesktop/stereo/suspend-error.oga } 

1 Comment

great, it works. also with os.system("paplay sound-alarm.oga")
6

See: Python Sound ("Bell")
This helped me when i wanted to do the same.
All credits go to gbc

Quote:

Have you tried :

import sys sys.stdout.write('\a') sys.stdout.flush() 

That works for me here on Mac OS 10.5

Actually, I think your original attempt works also with a little modification:

print('\a') 

(You just need the single quotes around the character sequence).

Comments

6

It can be done by code as follows:

import time time.sleep(10) #Set the time for x in range(60): time.sleep(1) print('\a') 

Comments

3

A bit more to your question.

I used gTTS package to generate audio from text and then play that audio using Playsound when I was learning webscraping and created a coursera downloader(only free courses).

text2speech = gTTS("Your course " + course_name + " is downloaded to " + downloads + ". Check it fast.") text2speech.save("temp.mp3") winsound.Beep(2500, 1000) playsound("temp.mp3") 

Comments

3

I created a python module that reminds developers on Telegram application after code execution. It might be more helpful than making alarm sound.

Pypi link: https://pypi.org/project/devreminder/

Github link: https://github.com/cagataygulten/devreminder

Example:

In [1]>> from devreminder import DevReminder import time In [2]>> remind = DevReminder(1932126911,False,0) In [3]>> remind.me("Example") time.sleep(6) 

Output:

An example of reminder message

Please follow README file for more information.

Comments

3

On macOS there's a dead simple way to do it.

python file.py && say done 

Comments

2
import subprocess subprocess.call(['D:\greensoft\TTPlayer\TTPlayer.exe', "E:\stridevampaclip.mp3"]) 

1 Comment

You should add some comments or other explanation for this code to explain how it works and why it answers the question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.