코드 완료시 소리 경보
내 코드를 실행하는 데 시간이 오래 걸리고 항상 쳐다보고 싶지 않지만 언제 완료되는지 알고 싶습니다.
(파이썬) 코드를 소리가 "알람"으로 만들려면 어떻게해야합니까? 코드 끝에 도달했을 때 .wav 파일을 재생하려고 생각하고있었습니다 ...
이것이 가능한 아이디어 일까? 그렇다면 어떻게 할 수 있습니까?
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
.
데비안 / 우분투 / 리눅스 민트의 터미널에서 이것을 실행하십시오 :
sudo apt install sox
Mac의 경우 터미널에서이를 실행하십시오 (macports 사용).
sudo port install sox
Mac에서의 연설
import os
os.system('say "your program has finished"')
리눅스 연설
import os
os.system('spd-say "your program has finished"')
speech-dispatcher
우분투 (또는 다른 배포판의 해당 패키지)에 패키지 를 설치해야합니다 :
sudo apt install speech-dispatcher
print('\007')
벨 소리를 재생합니다
이것은 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'이라고 적힌 줄에 주석을 달고 재부팅하십시오.
- 터미널 환경 설정에 '터미널 벨'이 있는지 확인하십시오.
우분투 음성 디스패처를 사용할 수 있습니다 :
import subprocess
subprocess.call(['speech-dispatcher']) #start speech dispatcher
subprocess.call(['spd-say', '"your process has finished"'])
쿠치의 답변 은 OS X Yosemite (10.10.1)에서 작동하지 않았습니다. 파이썬에서 호출 할 수 있는 afplay
명령 ( here )을 찾았습니다 . 이것은 터미널 가청 벨의 활성화 여부에 관계없이 작동하며 타사 라이브러리가 없습니다.
import os
os.system('afplay /System/Library/Sounds/Sosumi.aiff')
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()
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
}
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')
import subprocess
subprocess.call(['D:\greensoft\TTPlayer\TTPlayer.exe', "E:\stridevampaclip.mp3"])
참고URL : https://stackoverflow.com/questions/16573051/sound-alarm-when-code-finishes
'Programing' 카테고리의 다른 글
DisplayNameAttribute 지역화 (0) | 2020.07.15 |
---|---|
xcodebuild는 스키마를 포함하지 않는다고 말합니다. (0) | 2020.07.15 |
페이지가로드되는 동안 VueJS 구문을 숨기려면 어떻게합니까? (0) | 2020.07.15 |
회사 이름은 어디에 설정합니까? (0) | 2020.07.15 |
IBus 문제 해결-1.5.11 이전의 IBus는 입력 문제를 일으킬 수 있습니다 (0) | 2020.07.15 |