-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
speech_recognition_patch.py
55 lines (47 loc) · 2.37 KB
/
speech_recognition_patch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from abc import ABC
import types
import speech_recognition as sr
class Microphone(sr.Microphone, ABC):
def __init__(self, device_index=None, sample_rate=None, chunk_size=1024):
types.MethodType(self.get_pyaudio, super())
#sr.Microphone.get_pyaudio = self.get_pyaudio
super().__init__(device_index=device_index, sample_rate=sample_rate, chunk_size=chunk_size)
def __enter__(self):
assert self.stream is None, "This audio source is already inside a context manager"
self.audio = self.pyaudio_module.PyAudio()
try:
device = self.audio.get_device_info_by_index(self.device_index)
print(f"Using Device: {device['name']} (defaultSampleRate={device['defaultSampleRate']}, maxInputChannels={device['maxInputChannels']})")
# channels = self.audio.get_device_info_by_index(self.device_index)['maxInputChannels']
channels = 1
is_input = True
# @todo: Fix for "OSError: [Errno -9997] Invalid sample rate" with loopback devices
#if device['isLoopbackDevice']:
# self.SAMPLE_RATE = int(device['defaultSampleRate'])
self.stream = sr.Microphone.MicrophoneStream(
self.audio.open(
input_device_index=self.device_index, channels=channels,
format=self.format, rate=self.SAMPLE_RATE, frames_per_buffer=self.CHUNK,
input=is_input, # stream is an input stream
# output=not is_input
)
)
except Exception:
self.audio.terminate()
raise
return self
@staticmethod
def get_pyaudio():
"""
Imports the pyaudio module and checks its version. Throws exceptions if pyaudio can't be found or a wrong version is installed
"""
try:
import pyaudiowpatch as pyaudio
except ImportError:
raise AttributeError("Could not find PyAudio; check installation")
from distutils.version import LooseVersion
if LooseVersion(pyaudio.__version__) < LooseVersion("0.2.11"):
raise AttributeError("PyAudio 0.2.11 or later is required (found version {})".format(pyaudio.__version__))
return pyaudio
class Recognizer(sr.Recognizer, ABC):
__init__ = sr.Recognizer.__init__