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?
This question is related to
python
audio
serial-port
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')
pygame
on any platformThe 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()
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)
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.
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)On linux: print('\007')
will make the system bell sound.
# playsound in cross plate form, just install it with pip
# first install playsound > pip install playsound
from playsound import playsound
playsound('audio.mp3')
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
Source: Stackoverflow.com