标签归档:alarm

代码完成时发出声音警报

问题:代码完成时发出声音警报

我处于这样一种情况,我的代码要花很长时间才能运行,并且我不想一直盯着它,但想知道何时完成。

完成后,如何使(Python)代码听起来像是“警报”?我当时正在考虑使其在代码末尾时播放.wav文件…

这甚至是可行的想法吗?如果是这样,我该怎么办?

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?


回答 0

在Windows上

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

其中freq是频率(单位为Hz),持续时间以毫秒为单位。

在Linux和Mac上

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

为了使用此示例,您必须安装sox

在Debian / Ubuntu / Linux Mint上,在终端中运行以下命令:

sudo apt install sox

在Mac上,在终端(使用macports)中运行以下命令:

sudo port install sox

在Mac上的演讲

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

在Linux上的演讲

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

您需要speech-dispatcher在Ubuntu中安装该软件包(或在其他发行版中安装相应的软件包):

sudo apt install speech-dispatcher

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

回答 1

 print('\007')

播放铃声

 print('\007')

plays the bell sound


回答 2

这个似乎在Windows和Linux *上都可以使用(根据这个问题):

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

beep()

在Windows中,可以放在末尾:

import winsound
winsound.Beep(500, 1000)

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

要在Linux上工作,您可能需要执行以下操作(根据QO的评论):

  • 在终端中,键入“ cd /etc/modprobe.d”,然后键入“ gksudo gedit blacklist.conf”
  • 评论说“ blacklist pcspkr”的行,然后重新启动
  • 还要检查终端首选项是否已选中“ Terminal Bell”。

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.

回答 3

可以使用ubuntu语音调度程序:

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

ubuntu speech dispatcher can be used:

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

回答 4

我假设您想要标准的系统铃声,而不想关心频率和持续时间等,您只想要标准的Windows铃声。

import winsound
winsound.MessageBeep()

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

回答 5

Kuchi的答案在OS X Yosemite(10.10.1)上对我不起作用。我确实找到了afplay命令(在这里),您可以从Python调用该命令。无论是否启用了终端音频铃,并且没有第三方库,此方法都有效。

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

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

回答 6

请参阅:Python声音(“铃声”)
当我想做同样的事情时,这对我有帮助。
所有学分归gbc

引用:

你有没有尝试过 :

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

在Mac OS 10.5上适合我

实际上,我认为您的原始尝试也可以进行一些修改:

print('\a')

(您只需要在字符序列周围加上单引号)。

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


回答 7

为什么要完全使用python?您可能会忘记删除它,并将其检入存储库。只需使用&&和另一个命令运行python命令以运行警报。

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

或将函数放入.bashrc中。我在这里使用apython,但是您可以覆盖“ python”

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

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
}

回答 8

可以通过以下代码来完成:

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

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

回答 9

import subprocess

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

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