0

I'm trying to read data from a .wav file and store it in an array as such:

out = struct.unpack_from("%dh" % num_frames * num_channels, frames)
self.data = array(out)

When I try this I get the error:

self.data = array(out)
TypeError: must be char, not tuple

What's causing this error? Are all the values in out not the same type? Shouldn't they all be negative or positive integers?

Nanor
  • 2,400
  • 6
  • 35
  • 66

1 Answers1

1

If you're using the array class, the constructor takes two arguments, the first being a character representing a type code.

So you want

from array import array
out = <...>
self.data = array('b', out)

Or one of the other type codes listed in the docs instead of 'b'.

Or you can use numpy.array, in which case your code should work as-is:

from numpy import array
out = <...>
self.data = array(out)
Andrew Magee
  • 6,506
  • 4
  • 35
  • 58
  • It worked. I used `'h'` instead of `'b'`. It worked previously without using the first argument as outlined in the top rated answer of this post: http://stackoverflow.com/questions/2063284/what-is-the-easiest-way-to-read-wav-files-using-python-summary?lq=1 – Nanor Feb 22 '15 at 22:42
  • 1
    Hmm, maybe they are talking about `numpy.array`? http://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html – Andrew Magee Feb 22 '15 at 22:48
  • Ah, that's what it was! Everything is back to normal now. Phew. – Nanor Feb 22 '15 at 22:50