Simple Python struct issue

Carlo DiCelico carlo.dicelico at gmail.com
Fri Oct 2 18:35:11 CEST 2009


On Oct 2, 12:15 pm, Simon Forman <sajmik... at gmail.com> wrote:
> On Fri, Oct 2, 2009 at 12:07 PM, Carlo DiCelico
>
>
>
>
>
> <carlo.dicel... at gmail.com> wrote:
> > I saw an article on O'Reilly about using NumPy and Dislin to analyze
> > and visualize WAV files. It's a really fantastic article but was a
> > little out of date. I updated the script to work with the newer
> > modules &etc but am still having trouble getting it working.
>
> > The line
>
> > temp[i,:] = array(struct.unpack("%dB"%(fft_length), tempb),Float) -
> > 128.0
>
> > always returns the same error: "Traceback (most recent call last):
> >  File "pysono.py", line 31, in <module>
> >   temp[i,:] = array(struct.unpack("%dB"%(fft_length),tempb),float) -
> > 128.0
> > struct.error: unpack requires a string argument of length 256" when I
> > do python pysono.py test.wav 256
>
> > I'm sure it's probably something simple but I just can't see what it
> > is!
>
> > Here's the original code:http://onlamp.com/python/2001/01/31/graphics/pysono.py
>
> > Thanks!
>
> In:
>
> struct.unpack("%dB" % (fft_length), tempb)
>
> tempb is not length 256.
>
> Also, note that (foo) is the same as just foo.  To create a tuple of
> length 1 you must say (foo,)
>
> HTH,
> ~Simon

I'm sorry, I'm not sure what you're referring to when you say "Also,
note that (foo) is the same as just foo.  To create a tuple of length
1 you must say (foo,)". The 256 is passed into the script as an
argument and then assigned to the variable fft_length, which you can
see in that line. So, whatever value I pass in comes out in the error.
What you're saying is that the error happens because tempb isn't the
length of any of those values (i.e., 128, 256, 512, etc)? (1) How
could I determine the length of tempb? and (2) It's doing this ->
tempb = fp.readframes(fft_length); right before doing the struct.unpack
(); could the splitting of the frames into frames of length fft_length
be causing this error?

Thanks for the help!
Carlo



More information about the Python-list mailing list