Sound alarm when code finishes

前端 未结 11 780
离开以前
离开以前 2021-01-29 17:42

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

相关标签:
11条回答
  • 2021-01-29 18:09

    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
    
    0 讨论(0)
  • 2021-01-29 18:10

    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
    }
    
    0 讨论(0)
  • 2021-01-29 18:14

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

    0 讨论(0)
  • 2021-01-29 18:18

    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')
    
    0 讨论(0)
  • 2021-01-29 18:21

    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.
    0 讨论(0)
  • 2021-01-29 18:23
    print('\007')
    

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

    0 讨论(0)
提交回复
热议问题