Python: Making a beep noise
PythonAudioSerial PortPython Problem Overview
I'm trying to get the program to give me a beeping noise. I'm on a windows machine. I've looked at http://docs.python.org/library/winsound.html
But not sure how I can program this with a barcode scanner.
Here is my code for the serial barcode scanner.
ser = serial.Serial()
ser.baudrate = 9600
#for windows
ser.port = 2 #for COM3
ser.open()
ser.write('hello')
ser.close()
UPDATE: Since I'm annoying my co-workers with the beep. Can I get it to come through the audio jack for headphones?
Python Solutions
Solution 1 - Python
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.
Solution 2 - Python
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
Solution 3 - Python
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
Solution 4 - Python
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: https://stackoverflow.com/questions/3127977/how-to-make-the-hardware-beep-sound-in-mac-os-x-10-6
Solution 5 - Python
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 :
- the sleep value (here 0.2), depends on the length (seconds) of your default beep sound
- I choosed to use
os.system
rather thensubprocess.Popen
for simplicity (it could be bad) - the '-n' for
echo
is to have no more display - the last ';' after
sleep
is necessary for the resulting text sequence (*x) - also tested through ssh on an X term
Solution 6 - Python
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)
Solution 7 - Python
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')
Solution 8 - Python
pygame
on any platform
Using The advantage of using pygame
is that it can be made to work on any OS platform. Below example code is for GNU/Linux though.
First install the pygame
module for python3
as explained in detail here.
$ sudo pip3 install pygame
The pygame
module can play .wav
and .ogg
files from any file location. Here is an example:
#!/usr/bin/env python3
import pygame
pygame.mixer.init()
sound = pygame.mixer.Sound('/usr/share/sounds/freedesktop/stereo/phone-incoming-call.oga')
sound.play()
Solution 9 - Python
On linux: print('\007')
will make the system bell sound.
Solution 10 - Python
I have made a package for that purpose.
You can use it like this:
from pybeep.pybeep import PyVibrate, PyBeep
PyVibrate().beep()
PyVibrate().beepn(3)
PyBeep().beep()
PyBeep().beepn(3)
It depends on sox and only supports python3.
Solution 11 - Python
If you want beep from Zelda or Mario theme :
!pip install chime
import chime
chime.theme('zelda')
chime.success()
chime.warning()
chime.error()
chime.info()
chime.notify_exceptions()
1/0
Solution 12 - Python
# playsound in cross plate form, just install it with pip
# first install playsound > pip install playsound
from playsound import playsound
playsound('audio.mp3')