Python Sound (“Bell”)

I'd like to have a python program alert me when it has completed its task by making a beep noise. Currently, I use import os and then use a command line speech program to say "Process complete." I much rather it be a simple "bell."

I know that there's a function that can be used in Cocoa apps, NSBeep, but I don't think that has much anything to do with this.

I've also tried

print('\a')

but that didn't work.

I'm using a Mac, if you couldn't tell by my Cocoa comment, so that may help.

17.08.2008 21:33:39
import os; os.system('say "Beer time."'); print('\a\a\a')
line break 27.08.2015 22:41:47
the question is answered but... you do need quotation marks for special control characters they are still represented as strings print('\a')
Danilo 18.06.2018 14:09:47
Does not seem to be working for me on Mojave
acjay 14.02.2019 15:49:24
6 ОТВЕТОВ
РЕШЕНИЕ

Have you tried :

import sys
sys.stdout.write('\a')
sys.stdout.flush()

That works for me here on Mac OS 10.5

Actually, I think your original attempt works also with a little modification:

print('\a')

(You just need the single quotes around the character sequence).

72
17.08.2008 21:46:02
@kecske it's common to disable the audible-bell in terminal configs, which would mean this "works", but makes no noise (err, just noticed your comment was posted about 9 months ago)
dbr 6.08.2012 22:06:49
Works on Windows XP as well (in a console app).
martineau 15.05.2013 19:26:45
I'm seeing no difference between using double quotes or single quotes, in Python3. Also, a slight improvement which avoids printing a newline after the bell character, and forces a flush without an additional statement (the flush is needed as we've disabled the newline): print('\a', end='', flush=True)
Max Barraclough 18.01.2020 14:50:55

I had to turn off the "Silence terminal bell" option in my active Terminal Profile in iTerm for print('\a') to work. It seemed to work fine by default in Terminal.

You can also use the Mac module Carbon.Snd to play the system beep:

>>> import Carbon.Snd
>>> Carbon.Snd.SysBeep(1)
>>> 

The Carbon modules don't have any documentation, so I had to use help(Carbon.Snd) to see what functions were available. It seems to be a direct interface onto Carbon, so the docs on Apple Developer Connection probably help.

5
17.08.2008 21:56:10

If you have PyObjC (the Python - Objective-C bridge) installed or are running on OS X 10.5's system python (which ships with PyObjC), you can do

from AppKit import NSBeep
NSBeep()

to play the system alert.

10
29.08.2008 15:47:05

I tried the mixer from the pygame module, and it works fine. First install the module:

$ sudo apt-get install python-pygame

Then in the program, write this:

from pygame import mixer
mixer.init() #you must initialize the mixer
alert=mixer.Sound('bell.wav')
alert.play()

With pygame you have a lot of customization options, which you may additionally experiment with.

9
24.05.2011 12:14:54
sudo pip3 install pygame for python3 as explained here.
Serge Stroobandt 5.10.2019 09:29:51

Building on Barry Wark's answer... NSBeep() from AppKit works fine, but also makes the terminal/app icon in the taskbar jump. A few extra lines with NSSound() avoids that and gives the opportunity to use another sound:

from AppKit import NSSound
#prepare sound:
sound = NSSound.alloc()
sound.initWithContentsOfFile_byReference_('/System/Library/Sounds/Ping.aiff', True)
#rewind and play whenever you need it:
sound.stop() #rewind
sound.play()

Standard sound files can be found via commandline locate /System/Library/Sounds/*.aiff The file used by NSBeep() seems to be '/System/Library/Sounds/Funk.aiff'

2
14.10.2017 09:46:37

With the 'beep' function from the beepy module you have the sounds mentioned on the screenshot below.

From beepy module you can choose your kind of sound

0
9.04.2020 05:23:57