[python] 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?

This question is related to python alarm audio

The answer is


ubuntu speech dispatcher can be used:

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

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()

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.

import subprocess

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

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')

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")

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')

print('\007')

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


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


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
}