Need help with struct and aifc modules.

Discussion in 'Python' started by sj, Sep 5, 2004.

  1. sj

    sj Guest

    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
    sj, Sep 5, 2004
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Fogelklou
    Replies:
    36
    Views:
    1,373
    Chris Fogelklou
    Apr 20, 2004
  2. Kelvin Chu

    AIFC (Python and audio files.)

    Kelvin Chu, Jul 14, 2003, in forum: Python
    Replies:
    3
    Views:
    493
    Dennis Lee Bieber
    Jul 16, 2003
  3. Replies:
    3
    Views:
    1,086
  4. Tzury Bar Yochay
    Replies:
    4
    Views:
    570
    John Machin
    Sep 21, 2008
  5. Noob
    Replies:
    25
    Views:
    1,471
    Nick Keighley
    Dec 9, 2009
Loading...

Share This Page