# Error in opening a wav file

**URL:** <https://discuss.python.org/t/error-in-opening-a-wav-file/39090>\
**Category:** Python Help\
**Tags:** help, new-users\
**Created:** [November 19, 2023, 3:00am UTC](https://discuss.python.org/t/error-in-opening-a-wav-file/39090 "2023-11-19T03:00:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Monkeybanana](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/monkeybanana/32/15980_2.png) [@Monkeybanana](https://discuss.python.org/u/Monkeybanana)\
**Post date:** [November 19, 2023, 3:00am UTC](https://discuss.python.org/t/error-in-opening-a-wav-file/39090/1 "2023-11-19T03:00:21Z")

</div>

I am trying to make a program that will display a fft and waveform of a wav file as it plays. For some reason, python is giving me this error:

> Traceback (most recent call last):  
> File “C:\Users\palla\PycharmProjects\Python1\My Personal Projects\Science\_Fair\_2023-2024\FTT try 2.py”, line 38, in   
> data = stream.read(CHUNK)  
> ^^^^^^^^^^^^^^^^^^  
> File “C:\Users\palla\AppData\Local\Programs\Python\Python311\Lib\site-packages\pyaudio\__init_\_.py”, line 568, in read  
> raise IOError(“Not input stream”,  
> OSError: [Errno Not input stream] -9975

Do you know why this is happenning? Here is my code:

```auto
import numpy as np #importing Numpy with an alias np
import pyaudio as pa
import struct
import matplotlib.pyplot as plt
import wave

wf = wave.open(input("Path>> "), 'rb')

CHUNK = 1024 * 1

p = pa.PyAudio()

stream = p.open(format=
    p.get_format_from_width(wf.getsampwidth()),
    channels=wf.getnchannels(),
    rate=wf.getframerate(),
    output=True,
    frames_per_buffer=CHUNK
)

fig, (ax,ax1) = plt.subplots(2)
x_fft = np.linspace(0, RATE, CHUNK)
x = np.arange(0,2*CHUNK,2)
line, = ax.plot(x, np.random.rand(CHUNK),'r')
line_fft, = ax1.semilogx(x_fft, np.random.rand(CHUNK), 'b')
ax.set_ylim(-32000,32000)
ax.ser_xlim = (0,CHUNK)
ax1.set_xlim(20,RATE/2)
ax1.set_ylim(0,1)
fig.show()

while True:
    data = stream.read(CHUNK)
    dataInt = struct.unpack(str(CHUNK) + 'h', data)
    line.set_ydata(dataInt)
    line_fft.set_ydata(np.abs(np.fft.fft(dataInt))*2/(11000*CHUNK))
    fig.canvas.draw()
    fig.canvas.flush_events()

```

I have also noticed that the pyplot window opens as expected, then closes. After this the program gives me the error and stops.  
Please help.

Thanks,  
Monkeybanana

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [November 19, 2023, 3:18am UTC](https://discuss.python.org/t/error-in-opening-a-wav-file/39090/2 "2023-11-19T03:18:57Z")

</div>

Judging from this page `https://people.csail.mit.edu/hubert/pyaudio/docs/` you should be reading from `wf`.

When you look it, there’s nothing in the call `p.open(...)` that tells `p` where the data comes from, but only what it looks like, so it’s not surprising that it’s not working! 🙂

---

<div class="post-metadata">

**Author:** ![Monkeybanana](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/monkeybanana/32/15980_2.png) [@Monkeybanana](https://discuss.python.org/u/Monkeybanana)\
**Post date:** [November 19, 2023, 3:30am UTC](https://discuss.python.org/t/error-in-opening-a-wav-file/39090/3 "2023-11-19T03:30:58Z")

</div>

Can you tell me how to fix? I started learning python only like 3 months ago…  
Also, I don’t think that that is the problem. The code from the website:

```auto
format = p.get_format_from_width(wf.getsampwidth()),
                    channels=wf.getnchannels(),
                    rate=wf.getframerate(),
                    output=True)

```

And mine

```auto
format=
    p.get_format_from_width(wf.getsampwidth()),
    channels=wf.getnchannels(),
    rate=wf.getframerate(),
    output=True,
    frames_per_buffer=CHUNK

```

are identical, if you exclude the frames\_per\_buffer.

---

<div class="post-metadata">

**Author:** ![kknechtel](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kknechtel](https://discuss.python.org/u/kknechtel)\
**Post date:** [November 19, 2023, 7:10am UTC](https://discuss.python.org/t/error-in-opening-a-wav-file/39090/4 "2023-11-19T07:10:12Z")

</div>

First, make sure that you understand that PyAudio is for listening to a microphone or sending audio to a speaker. It is not for reading and writing audio files. The standard library `wave` already takes care of working with audio files in the WAV format. “reading” from a `pyaudio.PyAudio` means that you want to record the sound from a microphone in real time.

If you want to play the sound at the same time that you are processing the sound data in your program, then you want to read from `wf`, and _write to_ `stream` (writing whatever it was that you read from `wf`). The code reports an error because you tried to read from `stream` instead, but it is an output stream (suitable for playing the sound, and specified that way using `output=True`).

> [@Monkeybanana](#):
>
> The code from the website:

You missed the point. What you are showing there is the code that you use to create `stream`. The problem is in the loop at the end of the code, when you try to _use_ the `stream`. That is why, in the error message, it shows you that line (`data = stream.read(CHUNK)`) and underlines part of it with `^` symbols. You need to `.write` here, not `.read`. Also, you cannot just write a number like `CHUNK` to the stream. You need to write actual data. To get that data, you should `.read` it from `wf` each time through the loop.

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [November 19, 2023, 2:40pm UTC](https://discuss.python.org/t/error-in-opening-a-wav-file/39090/5 "2023-11-19T14:40:38Z")

</div>

That’s not the part it’s complaining about.

It’s complaining about:

`data = stream.read(CHUNK)`

where the website has:

`wf.readframes(CHUNK)`

As Karl said, you’re trying to read from an output stream instead of from the file.
