home  |  docs  |  features  |  forum  |  contact  search  |  site map 
  PyMedia
Features
News
  PyMedia
PyCar
  Documentation
Mailing List
FAQ

Forum
Links

  Tutorial chapter
 

This small tutorial describes basic objects that allow you to play sound on all supported platforms. In many ways it is very similar to ossaudiodev module which is standard module for Python on many Unix based systems.

 
 
Basically a WAV file can contain many different formats of audio data, but in 99% it contains raw audio data. Meaning that it is not compressed and does not require any processing when playing through the sound card.

The only thing that needs to be read is the WAV header which contains very basic information about the audio data such as( I mention those PyMedia can use ):
  • Sampling frequency( 8Khz, 11Khz, 22Khz, 44Khz etc )
  • Sample size( 2 bytes or 1 byte )
  • Sample format( signed, unsigned )
  • Channels number( 1 or 2 )
In order to play a WAV file correctly these parameters needs to be read and set up in PyMedia.
 
 
Parsing wave header is a simple thing using wave module:
import time, wave, pymedia.audio.sound as sound
f= wave.open( 'YOUR FILE NAME', 'rb' )
sampleRate= f.getframerate()
channels= f.getnchannels()

Unfortunately wave module does not return the format of audio data. But you can safely assume it'll always be pymedia.audio.sound.AFMT_S16_LE
format= sound.AFMT_S16_LE
 
 
After all the parameters are known, playing a WAV file is just as simple as code below:
snd= sound.Output( sampleRate, channels, format )
s= f.readframes( 300000 )
snd.play( s )

The biggest trick for newbies is that snd.play() will return before playing of the chunk ends. It is so called 'asynchronous' mode of playing audio.
If you want to wait until everything is finished, add this line:
while snd.isPlaying(): time.sleep( 0.05 )

That's it. Now you know how to play simple WAV files with PyMedia.
 
 
Click here to get the fully functional example.
 
SourceForge.net Logo home | docs | features
contact | News | Mailing List
Forum
 
 
 

2004 PyMedia© - opens up multimedia for everyone

Webmaster