Python: Making a beep noise

Question:

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?

Asked By: Marc Brigham

||

Answers:

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.

Answered By: CyanRook

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

Answered By: jforberg

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
Answered By: user285594

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.

Answered By: qed

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 then subprocess.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
Answered By: s4mdf0o1

The cross-platform way:

import time
import sys
for i in range(1,6):
    sys.stdout.write('ra{i}'.format(i=i))
    sys.stdout.flush()
    time.sleep(1)
sys.stdout.write('n')

Thanks to c z:

print(end='a')
Answered By: FooBar167

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

Answered By: c z

Using pygame on any platform

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()
Answered By: Serge Stroobandt

On linux: print('07') will make the system bell sound.

Answered By: Rodrigo
# playsound in cross plate form, just install it with pip
#  first install playsound > pip install playsound
from playsound import playsound
playsound('audio.mp3')
Answered By: Wester king

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)
Answered By: Brad123

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
Answered By: GuilLabs
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.