Python Sound ("Bell")

I would like the python program to alert me when it completed its task by creating a beep. I am currently using import os and then using a command line speech program to say "Process complete." Most likely, this is a simple โ€œcallโ€.

I know that there is a function that can be used in Cocoa, NSBeep , but I do not think this has anything to do with this.

I tried too

 print('\a') 

but it didnโ€™t work.

I am using a Mac if you cannot tell from my cocoa comment so that this can help.

+48
python terminal audio macos
Aug 17 '08 at 21:33
source share
5 answers

You tried:

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

This works for me here on Mac OS 10.5

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

 print('\a') 

(You just need single quotes around a sequence of characters).

+66
Aug 17 '08 at 21:46
source share

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

 from AppKit import NSBeep NSBeep() 

to play a system warning.

+10
Aug 29 '08 at 15:47
source share

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

 $ sudo apt-get install python-pygame 

Then in the program write the following:

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

With pygame, you have many customization options that you can experiment with further.

+7
May 24 '11 at 12:14
source share

I had to disable the "Silence terminal bell" option in my active terminal profile in iTerm for print('\a') . Apparently, it worked fine by default in the terminal.

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

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

Carbon modules do not have documentation, so I had to use help(Carbon.Snd) to find out what features are available. This seems to be a direct interface to Carbon, so the docs in Apple Developer Connection are likely to help.

+5
Aug 17 '08 at 21:56
source share

Based on Barry Wark's answer ... NSBeep() from AppKit works great, but also makes the terminal / application icon in the taskbar jump. A few additional lines with NSSound() avoid this and make it possible to use a different 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() 

The standard sound files can be found through the command line locate /System/Library/Sounds/*.aiff file used by NSBeep() seems to be '/System/Library/Sounds/Funk.aiff'

+2
Oct 14 '17 at 9:37 on
source share



All Articles