Solve Unanticipated Host Error Python pyaudio

67 Views Asked by At

When I attempt to use the pyaudio library to record an audio file I get an error. The error message is:

lProject\test.py", line 11, in
stream = p.open(
^^^^^^^
File "C:\Users\lenny\AppData\Local\Programs\Python\Python311\Lib\site-packages\pyaudio_init_.py", line 639, in open
stream = PyAudio.Stream(self, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lenny\AppData\Local\Programs\Python\Python311\Lib\site-   packages\pyaudio_init_.py", line 441, in init
self._stream = pa.open(**arguments)
^^^^^^^^^^^^^^^^^^^^
OSError: [Errno -9999] Unanticipated host error

The code I am trying to run is:

import pyaudio
import wave

FRAMES_PER_BUFFER = 512
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000

p = pyaudio.PyAudio()

stream = p.open(
    format=FORMAT,
    channels=CHANNELS,
    rate=RATE,
    input=True,
    frames_per_buffer=FRAMES_PER_BUFFER

)

print("Start recording")

seconds = 5
frames = []

for i in range(0, int(RATE/FRAMES_PER_BUFFER*seconds)):
    data = stream.read(FRAMES_PER_BUFFER)
    frames.append(data, FRAMES_PER_BUFFER)


stream.stop_stream()
stream.close()

p.terminate()

obj = wave.open("output.wav", "wb")
obj.setnchannels(CHANNELS)
obj.setsampwidth(p.get_sample_size)
obj.setframerate(RATE)
obj.writeframes(b"".join(frames))
obj.close()

The script is meant to just simply record audio then save it as output.wav

I am using windows 11 with python 3.11.5 and pyaudio 0.2.13

Can someone please help?

0

There are 0 best solutions below