Error in opening a wav file

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”, line 38, in
data =
File “C:\Users\palla\AppData\Local\Programs\Python\Python311\Lib\site-packages\”, 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:

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

wf ="Path>> "), 'rb')

CHUNK = 1024 * 1

p = pa.PyAudio()

stream =

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.ser_xlim = (0,CHUNK)

while True:
    data =
    dataInt = struct.unpack(str(CHUNK) + 'h', data)

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.


Judging from this page you should be reading from wf.

When you look it, there’s nothing in the call that tells p where the data comes from, but only what it looks like, so it’s not surprising that it’s not working! :slight_smile:

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:

format = p.get_format_from_width(wf.getsampwidth()),

And mine


are identical, if you exclude the frames_per_buffer.

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).

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 = 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.

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

It’s complaining about:

data =

where the website has:


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