Need help with struct and aifc modules.

S

sj

Im trying to read sample values from aiff files but can not seem to get the
unpacking right. Ths following code reads n frames from a sample file and
prints the amplitudes to the terminal. In the call to struct.unpack 'h' is
the only formating string I can use without getting the message
"struct.error: unpack str size does not match format"

However the output is wrong. The test file contains a single cycle sine
wave which is positive for the first 50 samples. What am I doing wrong?



import aifc
import struct
import string

def dump_frames( file, start=0, end=None):
fobj = aifc.open( file, 'r')
fobj.setpos( start)
if not end:
end = fobj.getnframes()
index = start
while index < end:
raw = fobj.readframes(1)
val = struct.unpack("h", raw)[0]
print "Frame ", string.rjust( str(index), 8), " : ",
print "Value ",string.rjust( str(val), 12)
index += 1





aifc file /samples/test.aiff
n channels 1
sample width 2
frame rate 44100
n frames 100
compression NONE not compressed
markers: NONE
Frame 0 : Value 0
Frame 1 : Value 14343
Frame 2 : Value 26638
Frame 3 : Value -30187
Frame 4 : Value -27108
Frame 5 : Value -31453
Frame 6 : Value 20778
Frame 7 : Value -3536
Frame 8 : Value 25143
Frame 9 : Value -26051
Frame 10 : Value -27581
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top