Sep-19-2020, 01:18 PM
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.
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 errorNote: The wave file is sampled at 96Khz.
