user3151828
user3151828

Reputation: 363

Scipy: Trying to write wav file, AttributeError: 'list' object has no attribute 'dtype'

I am using Anaconda3 and SciPy to try to write a wav file using an array:

wavfile.write("/Users/Me/Desktop/C.wav", 1000, array)

(I don't know how many samples per second, I'm planning on playing around with that, I'm betting on 1000 however)

array returns an array of 3000 integers, so the file would last 3 seconds.

However it gives me this error when trying to run:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-21-ce3a8d3e4b4b> in <module>()
----> 1 wavfile.write("/Users/Me/Desktop/C.wav", 1000, fin)

/Users/Me/anaconda/lib/python3.4/site-packages/scipy/io/wavfile.py in write(filename, rate, data)
213 
214     try:
--> 215         dkind = data.dtype.kind
216         if not (dkind == 'i' or dkind == 'f' or (dkind == 'u' and data.dtype.itemsize == 1)):
217             raise ValueError("Unsupported data type '%s'" %         data.dtype)

AttributeError: 'list' object has no attribute 'dtype'

Upvotes: 3

Views: 8480

Answers (2)

Jeff
Jeff

Reputation: 471

I would like to add a bit of information in reply to user3151828's comment. I opened a file comprised of 32 bit signed float values, audio data not formatted as a proper wave file, and created a normal Python list and then converted it to a numpy array as Oliver W. states to do and printed the results.

import numpy as np
import os
import struct
file = open('audio.npa', 'rb')
i = 0
datalist = []
for i in range(4):
    data = file.read(4)
    s = struct.unpack('f', data)
    datalist.append(s)
numpyarray = np.array(datalist)
print('datalist, normal python array is: ', datalist, '/n')
print('numpyarray is: ', numpyarray)

The output is:

datalist, normal python list is:  [(-0.000152587890625,), (-0.005126953125,), (-0.010284423828125,), (-0.009796142578125,)]

numpyarray is:
[[-0.00015259]
[-0.00512695]
[-0.01028442]
[-0.00979614]]

So, there is the difference between the two.

Upvotes: 0

Oliver W.
Oliver W.

Reputation: 13459

You are passing write an ordinary python list, which does not have an attribute called dtype (you can get that info by studying the error message). The documentation of scipy.io.wavfile clearly states you should pass it a numpy array:

Definition: wavfile.write(filename, rate, data)

Docstring:

Write a numpy array as a WAV file

You can convert your ordinary python list to a numpy array like so:

import numpy as np
arr = np.array(array)

Upvotes: 5

Related Questions