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

Forum
Links

  Tutorial chapter
 

The example below shows how to convert compressed audio files into the format suitable for writing onto regular Audio CD. It may be also useful if you want to manipulate your audio data in the WAV editing software.

 
 
In order to keep size of your audio files small, there are number of compressing techniques known. In some cases it can make your original CD smaller by 10 times or less. Every technique has its own pluses and minuses and most of them lose quality. It is all up to you to determine which format you want to use.

In order for you to play compressed file you have to decompress it so the soundcard can understand what sound to make. In most cases the format of the audio file can be determined by file's extension. It could be mp3, ogg, wma or anything else.

Basically PyMedia chooses which decoder to use based on extension you pass, thus you need to know file's extension to be able to decode it.
 
 
Let's say you know which file type you have. Let it be mp3 file for a sake of simplicity
import pymedia.audio.acodec as acodec
import pymedia.muxer as muxer
dm= muxer.Demuxer( 'mp3' )

You can always ask PyMedia about all known extensions like this:
print muxer.extensions
 
 
Before you can decode data in a file it has to be demuxed (demultiplexed). Many formats such as m4a, wma etc have some additional information in a file besides the compressed data. And we have to deal with it. Demuxer parse compressed data and return list of compressed strings which can be processed by decoder.
f= open( 'YOU FILENAME', 'rb' )
s= f.read( 200000 )
frames= dm.parse( s )

 
 
Once the file is read, we can get more information about the file's content in dm.streams. Ususally in audio files we can find only 1 stream. For mp3 file it is always true.
frame= frames[ 0 ]
dec= acodec.Decoder( dm.streams[ 0 ] )
 
 
Once decoder is opened, all you need to do is just pass the parsed compressed data to it and it'll return it uncompressed
r= dec.decode( frame[ 1 ] )

Now. As you know from the previous section, every sound should have parameters like sampling frequency, channels number and sample width. PyMedia returns that information to you in a acodec.Frame object, which is the result of decode() function. You can get those parameters by:
print r.sample_rate, r.channels

The sound data itself is stored in the data property of acodec.Frame object returned by decode() function. Let's save it as a WAV file:
import wave
fw= wave.open( 'test.wav', 'wb' )
fw.setparams( (r.channels, 2, r.sample_rate, 0, 'NONE','') )
snd.writeframes( r.data )

Simple huh ? Bear with me for another simple session.
 
 
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