Python: making a sound

I am trying to get the program to beep. I am on a window machine. I looked at http://docs.python.org/library/winsound.html

But I don’t know how I can program this with a barcode scanner.

Here is my code for a serial barcode scanner.

ser = serial.Serial() ser.baudrate = 9600 #for windows ser.port = 2 #for COM3 ser.open() ser.write('hello') ser.close() 

UPDATE: Because I annoy my employees with a beep. Can I get it through the headphone audio jack?

+77
python audio serial-port
Jun 30 2018-11-15T00:
source share
7 answers

On Windows, if you just want to make the computer sound an alarm:

 import winsound frequency = 2500 # Set Frequency To 2500 Hertz duration = 1000 # Set Duration To 1000 ms == 1 second winsound.Beep(frequency, duration) 

winsound.Beep() can be used wherever you want a beep.

+113
Jun 30 '11 at 15:53
source share

The cross-platform way to do this is print '\a' . This will send the ASCII Bell character to stdout and, hopefully, will generate a beep (a for "alert"). Please note that many modern terminal emulators provide the ability to ignore bell characters.

Since you are on Windows, you will be happy to hear that Windows has its own (attach itself) Beep API, which allows you to send audio signals of arbitrary length and pitch. Note that this solution is for Windows only, so you should probably prefer print '\a' if you really don't care about Hertz and milliseconds.

The Beep API is accessed through the winsound module: http://docs.python.org/library/winsound.html

+113
Jun 30 '11 at 15:59
source share

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 
+18
Sep 24 '13 at 14:25
source share

I was looking for the same thing, but for the Linux shell.

The topic led me to answer, -thanks -

Perhaps a more pythonic manner:

 import os beep = lambda x: os.system("echo -n '\a';sleep 0.2;" * x) beep(3) 

Notes:

  • the value of sleep (here 0.2) depends on the length (seconds) of your default sound signal
  • I decided to use os.system and not subprocess.Popen for simplicity (this may be bad)
  • '-n' for echo should no longer be displayed
  • last ';' after sleep necessary for the resulting text sequence (* x)
  • also verified via ssh on the X member
+12
Nov 03 '15 at 15:00
source share

I made a package for this purpose.

You can use it as follows:

 from pybeep.pybeep import PyVibrate, PyBeep PyVibrate().beep() PyVibrate().beepn(3) PyBeep().beep() PyBeep().beepn(3) 

It depends on sox and only supports python3.

+4
Dec 22 '14 at 13:24
source share

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') 
+3
Jul 13 '17 at 11:36
source share

There is a Windows answer and a Debian answer, so here's a Mac :

This assumes that you are just looking for a quick way to make a custom alert sound, not specifically the piezoelectric signal that you get on Windows:

 os.system( "say beep" ) 

Disclaimer: you can replace os.system a subprocess module call if you are worried that someone has cracked your beep.

+3
Dec 05 '18 at 16:10
source share



All Articles