See All Titles |
![]() ![]() Working with SoundsPython 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 ModuleThe 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:
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 ModuleThe 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:
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 ModuleThis 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: 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: 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 ModuleThe 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 ModuleThe 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/.
|
© 2002, O'Reilly & Associates, Inc. |