< BACKMake Note | BookmarkCONTINUE >
152015024128143245168232148039199167010047123209178152124239215162148044238005059246049208

Working with Sounds

Python has many modules that can provide audio support for your programs by allowing you to listen to your favorite audio CDs and read/write audio files (such as .wav, .aifc, and so on). Next, I present some of the most important modules. However, keep in mind that other modules exist that are not mentioned here.

winsound Module

The winsound module implements an interface that grants access to the sound-playing environment provided by Windows Platforms. This module is able to play wave sound files (.wav).

This module implements the function PlaySound, which has the following syntax: PlaySound(sound, flags).

						
>>> import winsound
>>> winsound.PlaySound(r'C:\WINNT\Media\tada.wav', winsound.SND_FILENAME)

					

The following flag constants, which are also defined by this module, can be used as bitwise arguments to the PlaySound function:

SND_FILENAME—   The sound is a wave filename.

SND_ALIAS—   The sound is a control panel sound association name.

SND_LOOP—   This plays the sound repeatedly; must also specify SND_ASYNC.

SND_MEMORY—   The sound is a memory image of a wave file.

SND_PURGE—   This stops all instances of the specified sound.

SND_ASYNC—   The PlaySound returns immediately.

SND_NODEFAULT—   This does not play a default beep if the sound cannot be found.

SND_NOSTOP—   This does not interrupt any sounds currently playing.

SND_NOWAIT—   This returns immediately if the sound driver is busy.

Tip

Before going further in this topic, let me present a small introduction about audio concepts that is applicable for the understanding of the next couple of modules.

Audio files have a number of parameters that describe the audio data. The sampling rate or frame rate is the number of times per second the sound is sampled. The number of channels indicate whether the audio is mono, stereo, or quadro. Each frame consists of one sample per channel. The sample size is the size in bytes of each sample. Thus a frame consists of nchannels*samplesize bytes, and a second's worth of audio consists of nchannels*samplesize*framerate bytes.

For example, CD quality audio has a sample size of two bytes (16 bits), uses two channels (stereo), and has a frame rate of 44,100 frames/second. This gives a frame size of 4 bytes (2*2), and a second's worth occupies 2*2*44100 bytes, that is, 176,400 bytes.



sndhdr Module

The sndhdr module is a collection of routines that help recognize sound files.

						
>>> import sndhdr
>>> audioinfo = sndhdr.what("c:\windows\media\start.wav")
('wav', 22050, 2, -1, 4)

					

The function sndhdr.whathdr() recognizes various types of sound file headers as it understands almost all headers that SOX can decode. The function sndhdr.what() calls sndhdr.whathdr(), and the return tuple contains the following items, in this order:

  • file type (as SOX understands it)

  • sampling rate (0 if unknown or hard to decode)

  • number of channels (0 if unknown or hard to decode)

  • number of frames in the file (-1 if unknown or hard to decode)

  • number of bits/sample; 'U' for U-LAW, or 'A' for A-LAW

If the file doesn't have a recognizable type, it returns None; and if the file can't be opened, IOError is raised.

To compute the total time, divide the number of frames by the sampling rate (a frame contains a sample for each channel).

wave Module

This module enables you to read, parse, and create wave (.wav) files where file is either the name of a file or an open file pointer. The open file pointer must have methods read(), seek(), and close(). When the setpos() and rewind() methods are not used, the seek() method is not necessary. This function returns an instance of a class with the following public methods:

Table 9.1. Public Methods Exposed by the wave Module for an Instance of a Class That Can Read from a File
Public Method Description
getnchannels() Returns the number of audio channels (1 for mono, 2 for stereo).
getsampwidth() Returns sample width in bytes.
getframerate() Returns sampling frequency.
getnframes() Returns number of audio frames.
getcomptype() Returns compression type ('NONE' for linear samples).
getcompname() Returns human-readable version of compression type ('not compressed' linear samples)
getparams() Returns a tuple consisting of all the previous in the order shown.
getmarkers() Returns None (for compatibility with the aifc module).
getmark(id) Raises an error because the mark does not exist (for compatibility with the aifc module).
readframes(n) Returns at most n frames of audio.
rewind() Rewinds to the beginning of the audio stream.
setpos(pos) Seeks to the specified position.
tell() Returns the current position.
close() Closes the instance (makes it unusable).

The position returned by tell() and the position given to setpos() are compatible and have nothing to do with the actual position in the file. The close() method is called automatically when the class instance is destroyed.

The syntax for writing wave files is f = wave.open(file, 'w')where file is either the name of a file or an open file pointer. The open file pointer must have methods write(), tell(), seek(), and close(). This function returns an instance of a class with the following public methods:

Table 9.2. Public Methods Exposed by the wave Module for an Instance of a Class That Can Write to a File
Public Method Description
setnchannels(n) Sets the number of channels.
setsampwidth(n) Sets the sample width.
setframerate(n) Sets the frame rate.
setnframes(n) Sets the number of frames.
setcomptype(type, name) Sets the compression type and the human-readable compression type.
setparams(tuple) Sets all parameters at once.
tell() Returns current position in output file.
writeframesraw(data) Writes audio frames without patching up the file header.
writeframes(data) Writes audio frames and patches up the file header.
close() Patches up the file header and closes the output file.

You should set the parameters before the first writeframesraw or writeframes. The total number of frames does not need to be set, but when it is set to the correct value, the header does not have to be patched up. It is best to first set all parameters, perhaps possibly the compression type, and then write audio frames using writeframesraw. When all frames have been written, either call writeframes('') or close() to patch up the sizes in the header. The close() method is called automatically when the class instance is destroyed.

						
>>> import wave
>>> audio = wave.open('c:\\windows\\media\\tada.wav', 'r')
>>> audio.getnchannels()
2
>>> audio.getsampwidth()
2
>>> audio.getframerate()
22050
>>> audio.getnframes()
42752
				
					

aifc Module

The aifc module, which stands for Audio Interchange File Format, is devoted to audio file access (reading/writing) in the AIFF and AIFC formats. This module has some functionality that only works on IRIX systems, but it partially works fine on Windows systems, as well.

						
>>> dev = aifc.open("test.aifc", "w")
>>> dev.setframerate(22050)
>>> dev.setsampwidth(2)
>>> dev.setnchannels(2)
>>> dev.writeframes('123456787654321'*20000)
>>> dev.close()
				
					

Note that, the method aifc.writeframes() is equivalent to the audiodev.Audiodev.writeframesraw. Both methods write data to the output file, and they can only be called after the audio file parameters have been set.

You can hear the file that is generated by using the QuickTime Player on Macintosh systems, or the MediaPlayer on Windows systems.

audiodev Module

The audiodev module provides a generic interface for audio output, which is used by Macintoshes, the SGI UNIX(IRIX) and SunOS/Solaris platforms. Note that there is a module called linuxaudiodev specific for Linux systems.

						
>>> import audiodev, aifc
>>> afile = aifc.open("test.aifc", "r")
>>> dev = audiodev.AudioDev()
>>> dev.setoutrate(afile.getframerate())
>>> dev.setsampwidth(afile.getsampwidth())
>>> dev.setnchannels(afile.getnchannels())
>>> data = afile.getsampwidth()*afile.getnchannels()*afile.getframerate()
>>> while 1:
…       frames = afile.readframes(data)
…       if not data:
…           break
…       dev.writeframes(frames)
…
>>>

					

The setoutrate() method defines the frequency rate of the sound wave; in this case, it is set to 22.05Khz.

The setsampwidth() method defines the sample width in number of bytes.

The setnchannels() method establishes the number of channels that we want to use. The previous example defines that we want to hear the sound in stereo.

The previous modules are all part of the standard distribution. Now, I will talk about some third-party modules.

The PythonWare Sound Toolkit (PST) reads sound files in different formats, and plays them on a variety of hardware platforms. Similar to Python itself, the PythonWare Sound Toolkit is copyrighted but can be used without a fee. This also applies to commercial applications. The current release reads AU, VOC, and WAV files, and plays them on Windows and Sun boxes.

For more information and download, visit the Web page:

						
					http://www.pythonware.com/products/pst/index.htm
				
					

The following link is an interesting resource that provides a Python package that plays audio CDs on your Linux system:

						
					ftp://starship.python.net/pub/crew/amk/unmaintained/linux-cd.tgz
				
					

If you are really interested in playing around with audio CDs, you'd better check the CDDB module. CDDB.py provides an easy way for Python programs to fetch track and disc information on audio CDs. This information is acquired from CDDB, a very large online database of track listings and other information on audio CDs. Included is a C extension module to enable Python to read track listings from audio CDs under Linux, FreeBSD, Solaris, and Win32. The interface to this extension module is portable and is intended to be ported to other operational systems easily.

You can check it out at http://csl.cse.ucsc.edu/~ben/python/.


Last updated on 1/30/2002
Python Developer's Handbook, © 2002 Sams Publishing

< BACKMake Note | BookmarkCONTINUE >

Index terms contained in this section

AIFC (Audio Interchange File Format)
aifc module 2nd
aifc.writeframes() method
arguments
     bitwise
            PlaySound function 2nd
audio 2nd 3rd 4th 5th 6th
Audio Interchange File Format (AIFC)
audiodev module 2nd
bitwise arguments
      PlaySound function 2nd
channels
      audio files
close() method 2nd
constants
     flag
            bitwise arguments, PlaySound function 2nd
creating
      wave files
files
     wave
            writing
flag constants
      bitwise arguments, PlaySound function 2nd
formats
      Audio Interchange File (AIFC)
frame rate
frames
      audio files
functions
     PlaySound
            bitwise arguments 2nd
      sndhdr.whathdr()
      sndhdrwhat()
getcompname() method
getcomptype() method
getframerate() method
getmark(id) method
getmarkers() method
getnchannels() method
getnframes() method
getparams() method
getsampwidth() method
methods
      aifc.writeframes()
      close() 2nd
      getcompname()
      getcomptype()
      getframerate()
      getmark(id)
      getmarkers()
      getnchannels()
      getnframes()
      getparams()
      getsampwidth()
      readframes(n)
      rewind()
      setcomptype(type, name)
      setframerate(n)
      setnchannels(n)
      setnframes(n)
      setparams(tuple)
      setpos(pos)
      setsampwidth(n)
      tell() 2nd
      wave module 2nd
      writeframes(data)
      writeframesraw(data)
modules
      aifc 2nd
      audiodev 2nd
      sndhdr
      wave 2nd
      winsound 2nd
PlaySound function
      bitwise arguments 2nd
readframes(n) method
rewind() method
sampling rate
setcomptype(type, name) method
setframerate(n) method
setnchannels(n) method
setnframes(n) method
setparams(tuple) method
setpos(pos) method
setsampwidth(n) method
sndhdr module
sndhdr.whathdr() function
sndhdrwhat() function
sounds 2nd 3rd 4th 5th 6th
syntax
      writing wave files
tell() method 2nd
wave files
      writing
wave module 2nd
winsound module 2nd
writeframes(data) method
writeframesraw(data) method
writing
      wave files

© 2002, O'Reilly & Associates, Inc.