Python Forum
PyAudio [Errorno -9999] Unanticipated Host Error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PyAudio [Errorno -9999] Unanticipated Host Error
#1
Hello,

I hope everyone is doing well. I have been using pyaudio for quite some time now, however, recently it stopped working and throwing me an OSERROR, I don't specifically remember changing anything. The relevant part of the script is reproduced below for testing purpose along with the error that I am receiving.

Any help will be greatly appreciated.

import pyaudio
import wave
import threading
import struct


# Monkey patch wave library
def _new_read_fmt_chunk(self, chunk):    
    WAVE_FORMAT_EXTENSIBLE = 65534
    WAVE_FORMAT_PCM = 0x0001
    try:
        wFormatTag, self._nchannels, self._framerate, dwAvgBytesPerSec, wBlockAlign = struct.unpack_from('<HHLLH',
                                                                                                         chunk.read(14))
    except struct.error:
        raise EOFError from None
    if wFormatTag == WAVE_FORMAT_PCM or wFormatTag == WAVE_FORMAT_EXTENSIBLE:
        try:
            sampwidth = struct.unpack_from('<H', chunk.read(2))[0]
        except struct.error:
            raise EOFError from None
        self._sampwidth = (sampwidth + 7) // 8
        if not self._sampwidth:
            raise Error('bad sample width')
    else:
        raise Error('unknown format: %r' % (wFormatTag,))
    if not self._nchannels:
        raise Error('bad # of channels')
    self._framesize = self._nchannels * self._sampwidth
    self._comptype = 'NONE'
    self._compname = 'not compressed'
wave.Wave_read._read_fmt_chunk = _new_read_fmt_chunk


class BackGroundMusic:
    def __init__(self):
        self.p = pyaudio.PyAudio()
        self.enabled = False

    def __del__(self):
        self.p.terminate()

    def play(self, bgm_file):
        if bgm_file[-4:].lower() != '.wav':
            return
        self.enabled = True
        threading.Thread(target=self.background_thread, args=(bgm_file, )).start()

    def background_thread(self, bgm_file):
        chunk = 1024
        with wave.open(bgm_file, 'rb') as wf:
            stream = self.p.open(format=self.p.get_format_from_width(wf.getsampwidth()), channels=wf.getnchannels(),
                                 rate=wf.getframerate(), output=True)
            while self.enabled:
                data = wf.readframes(chunk)
                if not data:
                    wf.rewind()
                stream.write(data)
            stream.close()

    def stop(self):
        self.enabled = False


x = BackGroundMusic()
bgm_file = r'C:\file_path\music_file.wav'
x.play(bgm_file)
Error:
Exception in thread Thread-1: Traceback (most recent call last): File "C:\Program Files (x86)\Python38-32\lib\threading.py", line 932, in _bootstrap_inner self.run() File "C:\Program Files (x86)\Python38-32\lib\threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "C:/Users/xxxx/PycharmProjects/hm_API/hm_API/music/__init__.py", line 50, in background_thread stream = self.p.open(format=self.p.get_format_from_width(wf.getsampwidth()), channels=wf.getnchannels(), File "C:\Users\xxxx\AppData\Roaming\Python\Python38\site-packages\pyaudio.py", line 750, in open stream = Stream(self, *args, **kwargs) File "C:\Users\xxxx\AppData\Roaming\Python\Python38\site-packages\pyaudio.py", line 441, in __init__ self._stream = pa.open(**arguments) OSError: [Errno -9999] Unanticipated host error
Note: The wave file is sampled at 96Khz.
Reply
#2
Took your code, only change I made was the name of the file, and it played just fine. I'd do the usuals - reboot, make sure the file plays in a different player.
Reply
#3
Thank you for the response. however, unfortunately rebooting, using a different file etc doesn't solve the problem, i have tried by reinstalling pyaudio too but the error still persists
Reply
#4
Looking around, I have found the following possibilities:
Unsupported bit rate (make sure you try files in different and common bit rates, like 44.1K and 48K)
Microphone privacy settings - enable and disable and see if makes a difference (even though you are not using, several people have reported this to cause the error)

Make sure your settings are supported.
import pyaudio
soundObj = pyaudio.PyAudio()

# Learn what your OS+Hardware can do
defaultCapability = soundObj.get_default_host_api_info()
print defaultCapability

# See if you can make it do what you want
isSupported = soundObj.is_format_supported(input_format=pyaudio.paInt8, input_channels=1, rate=22050, input_device=0)
print isSupported
Credit where it is due, I got that from Stack Overflow
Reply
#5
Thank you for the help, however unfortunately, following your advice did not resolve the issue. I believe something has gotten messed up in my portaudio binary. I tried removing and reinstalling it but still no dice. Can someone recommend a simpler sound library with synthesizer support?. The library that does not implement threading on its own will be recommended.
Reply
#6
pygame has audio support. The following will play an mp3 file. It will also play midi notes.
import pygame.mixer

pygame.mixer.init()
pygame.mixer.music.load('V.mp3')
pygame.mixer.music.play(0)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pyaudio seems to randomly halt input. elpidiovaldez5 2 312 Jan-22-2024, 09:07 AM
Last Post: elpidiovaldez5
  where to host my python script tomtom 1 1,233 Feb-09-2022, 06:45 AM
Last Post: ndc85430
  Failing to connect to a host with WMI tester_V 6 4,280 Aug-10-2021, 06:25 PM
Last Post: tester_V
  Not getting response from pyaudio OceansBlue 1 2,584 Jul-03-2021, 06:22 AM
Last Post: OceansBlue
  help with PyAudio Leo12143 1 1,888 Jan-18-2021, 09:56 AM
Last Post: DT2000
  pyAudio playing variable Talking2442 3 2,959 Dec-01-2020, 06:20 PM
Last Post: Talking2442
  Pyaudio Souls99 7 3,464 Oct-05-2020, 04:06 PM
Last Post: Larz60+
  PyAudio Buffer Data Calculation MclarenF1 0 2,100 Aug-21-2020, 10:55 AM
Last Post: MclarenF1
  PyAudio throwing Input overflowed anthares 3 4,689 Jun-14-2020, 03:37 PM
Last Post: anthares
  Using pyaudio to stop recording under certain sound threshold Twanski94 2 6,301 Jun-13-2020, 11:35 AM
Last Post: Twanski94

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020