pyaudio-OSError: [Errno -9999] Unanticipated host error
Asked Answered
D

5

9

I just want to run a simple python audio code:

import pyaudio
import wave
import sys

CHUNK = 1024
wf = wave.open("4.wav", 'rb')
# instantiate PyAudio (1)
p = pyaudio.PyAudio()
# open stream (2)
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
                channels=wf.getnchannels(),
                rate=wf.getframerate(),
                output=True)

but I got the following error:

Traceback (most recent call last):
  File "rec2.py", line 17, in <module>
    output=True)
  File "C:\Users\Surena\Anaconda3\lib\site-packages\pyaudio.py", line 750,  in open
    stream = Stream(self, *args, **kwargs)
  File "C:\Users\Surena\Anaconda3\lib\site-packages\pyaudio.py", line 441, in __init__
    self._stream = pa.open(**arguments)
OSError: [Errno -9999] Unanticipated host error

I tried another pyaudio record too, the same error came up. I also tried uninstall pyaudio and install it again using pip install pyaudio, but it did not help. I even uninstalled anaconda3 and reinstall it.nothing changed.

what is the problem?

Demulsify answered 7/1, 2017 at 7:16 Comment(3)
The method works in my case: #43841816Sidestroke
I got the same problem and this is method works in my case: #43841816Sidestroke
Shutting off antivirus as proposed in that thread didn't work for me.Elsie
S
3

I had the same problem, and I fix it just by enable/disable the access to the microphone in Setting>Confidentiality>Microphone (on Windows 10)

Siderosis answered 29/6, 2018 at 9:11 Comment(1)
On my windows, it's "Setting>Privacy>Microphone".Parole
D
1

You need to collect additional information to understand the problem. From Portaudio docs:

PortAudio usually tries to translate error conditions into portable PaError error codes. However if an unexpected error is encountered the paUnanticipatedHostError code may be returned. In this case a further mechanism is provided to query for Host API-specific error information. If PortAudio returns paUnanticipatedHostError you can call Pa_GetLastHostErrorInfo() to retrieve a pointer to a PaHostErrorInfo structure that provides more information, including the Host API that encountered the error, a native API error code and error text.

If you have this error on Linux, most likely it is caused by incompatible sample rate you are trying to request from the driver. This value

            rate=wf.getframerate()

can be changed to 16000, 44100 and 48000 to test which rate is actually supported.

Dorinedorion answered 23/6, 2017 at 21:55 Comment(1)
I am having this issue on Win10 and tried the privacy and antivirus solutions proposed by others. Verified I am using the right input by enumerating sound devices. I'd like to try your method but can you help by explaining a bit more about how to call Pa_GetLastHostErrorInfo() ? I tried to look for documentation but didn't find how to use it.Elsie
S
0

I had same problem so i just give allow permission to microphone from setting and its work just go to setting --> Privacy --> Microphone then give the persmission to mic means on the microphone

Secretarygeneral answered 12/5, 2020 at 11:55 Comment(0)
A
0

If you are using windows 10 search for microphone privacy settings

Aldos answered 24/7, 2020 at 19:9 Comment(0)
I
0

Okay so if you using jupyter-notebook and you face this challenge. install pyaudio with homebrew(brew install pyaudio) and then restart your kernel.

Iceberg answered 9/4, 2021 at 8:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.