Reading raw audio data

during my experiments with audio analysis i fought a bit with reading audio data. in case someone else tries to do the same. here’s an example of how to get the raw data into an array.

i’m currently writing on more complex stuff but that’s not yet polished up. so please be patient.

from panda3d.core import MovieAudio,Filename 
from struct import unpack

myAudio = MovieAudio("mytest")
myAudioCursor = myAudio.get(Filename("test.ogg")).open()
print "Source:",myAudioCursor.getSource().getFilename()
print "Number of Channels:",myAudioCursor.audioChannels()
print "Sample rate:" ,myAudioCursor.audioRate()
print "Can seek:",myAudioCursor.canSeek()
print "Can seek fast:" , myAudioCursor.canSeekFast() 
print "Length:" ,myAudioCursor.length()
print "Ready?:" ,myAudioCursor.ready()
print "Current sample:",myAudioCursor.tell()

numFrames = int(myAudioCursor.length()*myAudioCursor.audioRate()) # aproximately number of frames
numChannels = myAudioCursor.audioChannels()

#reading out the entire file at once and stuff it into an array
rawdata=myAudioCursor.readSamples(numFrames) #read the raw-data, signed shorts little endian 
data = unpack( "<"+("h"*numFrames*numChannels),rawdata ) #unpack the rawdata into a list of int's

#for mono audio the data is ok like that. for stereo the 1st value is left channel, 2nd value right, 3 is left again.. etc.
#values range from somewhere between -2^15 to 2^15 .. at least theoretically.