Can't get scipy.io.wavfile.read() to work

10,447

You have confused SciPy's WAV module with Python's. Remove import wave, use import scipy.io.wavfile, and call scipy.io.wavfile.read.

Example:

>>> import scipy.io.wavfile
>>> FSample, samples = scipy.io.wavfile.read('myfile.wav')

SciPy's module does the job of converting from a byte string to numbers for you, unlike Python's module. See the linked docs for more details.

Share:
10,447
W. Weiss
Author by

W. Weiss

I am a physics student who discovered programming and fell in love. I don't know a lot about how to use web resources to learn more about coding and that is why I am here.

Updated on June 18, 2022

Comments

  • W. Weiss
    W. Weiss almost 2 years

    I am trying to read a .wav file into an array so that I can then plot the array and do a FFT. I got the file open with the wave module and now I am struggling. I was advised to use scipy.io.wavfile.read(filename, mmap=False) but am not having any luck. This function should do exactly what I want it to do but it isn't. I am running Python 2.7 and maybe that is it. Please help me figure out how to make this work. The code I have written is below.

    import scipy
    import wave
    harp=wave.open('/Users/williamweiss2/Desktop/Test 2/harp.wav','r')
    frames_harp=harp.getnframes()
    harp_rate,harp_data=scipy.io.wavfile.read(harp,mmap=False)
    

    This is the error I am getting when I try to run the program.

    ---> harp_rate,harp_data=scipy.io.wavfile.read(harp,mmap=False)

    AttributeError: 'module' object has no attribute 'io'

    Any help would be greatly appreciated. Thanks in advance.

  • W. Weiss
    W. Weiss almost 8 years
    OK. I am trying to change that right now. What are the differences?
  • Warren Weckesser
    Warren Weckesser almost 8 years
    The essential issue is that import scipy does not import all the scipy subpackages. If you want to use scipy.io, you have to explicitly import it, using whatever variation of import that you prefer. E.g. use import scipy.io and use scipy.io.wavfile.read, or from scipy import io and use io.wavfile.read, or from scipy.io import wavfile and use wavfile.read, etc.