Need help with struct and aifc modules.

sj _jones_57_ at _swbell.net
Sat Sep 4 23:57:50 EDT 2004


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





>>> sfile = "/samples/test.aiff"
>>> print get_aiff_stats( sfile)

aifc file      /samples/test.aiff
n channels     1
sample width   2
frame rate     44100
n frames       100
compression    NONE   not compressed
markers:       NONE

>>> dump_frames( sfile, 0, 11)
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



-- 
Replace underscores in email address to reply



More information about the Python-list mailing list