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 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:

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

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! :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()),
                    channels=wf.getnchannels(),
                    rate=wf.getframerate(),
                    output=True)

And mine

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.

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

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.