Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sound alarm when code finishes

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?

like image 619
mtigger Avatar asked May 15 '13 19:05

mtigger


4 Answers

print('\007')

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

like image 37
Josh Allemon Avatar answered Oct 21 '22 02:10

Josh Allemon


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
like image 100
Ryan Saxe Avatar answered Oct 21 '22 00:10

Ryan Saxe


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
      1000 is the duration in miliseconds

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

  • in a terminal, type 'cd /etc/modprobe.d' then 'gksudo gedit blacklist.conf'
  • comment the line that says 'blacklist pcspkr', then reboot
  • check also that the terminal preferences has the 'Terminal Bell' checked.
like image 38
Saullo G. P. Castro Avatar answered Oct 21 '22 00:10

Saullo G. P. Castro


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()
like image 20
poulter7 Avatar answered Oct 21 '22 01:10

poulter7