152.html ( File view )
- By 2010-08-21
- View(s):12
- Download(s):0
- Point(s): 1
Safari | Python Developer's Handbook -> Working with Sounds
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:
Sponsored links
Need
1 point
Your Point(s)
Your Point isn't enough. Get point immediately by PayPal More(Debit card / Credit card / PayPal Credit / Online Banking) |