Using the Bell Character to Make Beep Sound in Python One of the simplest ways to generate a beep sound is to use the bell character i-e '\a' within the print statement. This method does not require any extra package to import. This method does not work on every terminal or operating system.
Open a Python shell by typing the python command in your terminal. Import the os library using import os in your Python shell. Run the os. system() function and pass the "beep -f 2000 -l 1500" string command to run the beep command on your Linux system with 2000 Hz frequency and 1500 ms duration.
On Windows, if you want to just make the computer make a beep sound:
import winsound
frequency = 2500 # Set Frequency To 2500 Hertz
duration = 1000 # Set Duration To 1000 ms == 1 second
winsound.Beep(frequency, duration)
The winsound.Beep()
can be used wherever you want the beep to occur.
The cross-platform way to do this is to print('\a')
. This will send the ASCII Bell character to stdout, and will hopefully generate a beep (a for 'alert'). Note that many modern terminal emulators provide the option to ignore bell characters.
Since you're on Windows, you'll be happy to hear that Windows has its own (brace yourself) Beep API, which allows you to send beeps of arbitrary length and pitch. Note that this is a Windows-only solution, so you should probably prefer print('\a')
unless you really care about Hertz and milliseconds.
The Beep API is accessed through the winsound module: http://docs.python.org/library/winsound.html
Linux.
$ apt-get install beep
$ python
>>> os.system("beep -f 555 -l 460")
OR
$ beep -f 659 -l 460 -n -f 784 -l 340 -n -f 659 -l 230 -n -f 659 -l 110 -n -f 880 -l 230 -n -f 659 -l 230 -n -f 587 -l 230 -n -f 659 -l 460 -n -f 988 -l 340 -n -f 659 -l 230 -n -f 659 -l 110 -n -f 1047-l 230 -n -f 988 -l 230 -n -f 784 -l 230 -n -f 659 -l 230 -n -f 988 -l 230 -n -f 1318 -l 230 -n -f 659 -l 110 -n -f 587 -l 230 -n -f 587 -l 110 -n -f 494 -l 230 -n -f 740 -l 230 -n -f 659 -l 460
There's a Windows answer, and a Debian answer, so here's a Mac one:
This assumes you're just here looking for a quick way to make a customisable alert sound, and not specifically the piezeoelectric beep you get on Windows:
os.system( "say beep" )
Disclaimer: You can replace os.system
with a call to the subprocess
module if you're worried about someone hacking on your beep code.
See: How to make the hardware beep sound in Mac OS X 10.6
I was searching for the same but for Linux shell.
The topic brought me to an answer, -thanks-
Maybe more pythonic manner :
import os
beep = lambda x: os.system("echo -n '\a';sleep 0.2;" * x)
beep(3)
Notes :
os.system
rather then subprocess.Popen
for simplicity (it could be bad)echo
is to have no more displaysleep
is necessary for the resulting text sequence (*x)I found this library to be helpful: Install beepy,
pip install beepy
There are 6 different sound options, you can see details here: https://pypi.org/project/beepy/
Code snip to listen to all the sounds:
import beepy as beep
for ii in range(1,7):
beep.beep(ii)
The cross-platform way:
import time
import sys
for i in range(1,6):
sys.stdout.write('\r\a{i}'.format(i=i))
sys.stdout.flush()
time.sleep(1)
sys.stdout.write('\n')
Thanks to c z:
print(end='\a')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With