ecoute/AudioRecorder.py

59 lines
2.6 KiB
Python
Raw Normal View History

2023-05-12 14:54:28 +00:00
import custom_speech_recognition as sr
2023-05-11 00:52:52 +00:00
import pyaudiowpatch as pyaudio
2023-05-12 02:06:14 +00:00
from datetime import datetime
2023-05-11 00:52:52 +00:00
2023-05-12 02:06:14 +00:00
RECORD_TIMEOUT = 3
2023-05-11 00:52:52 +00:00
ENERGY_THRESHOLD = 1000
DYNAMIC_ENERGY_THRESHOLD = False
2023-05-13 02:42:38 +00:00
class BaseRecorder:
2023-05-13 14:36:00 +00:00
def __init__(self, source, source_name):
2023-05-11 00:52:52 +00:00
self.recorder = sr.Recognizer()
self.recorder.energy_threshold = ENERGY_THRESHOLD
self.recorder.dynamic_energy_threshold = DYNAMIC_ENERGY_THRESHOLD
2023-06-01 18:59:36 +00:00
if source is None:
raise ValueError("audio source can't be None")
2023-05-13 02:42:38 +00:00
self.source = source
self.source_name = source_name
2023-05-11 00:52:52 +00:00
2023-05-16 23:06:21 +00:00
def adjust_for_noise(self, device_name, msg):
print(f"[INFO] Adjusting for ambient noise from {device_name}. " + msg)
2023-05-11 00:52:52 +00:00
with self.source:
self.recorder.adjust_for_ambient_noise(self.source)
2023-05-16 23:06:21 +00:00
print(f"[INFO] Completed ambient noise adjustment for {device_name}.")
2023-05-11 00:52:52 +00:00
def record_into_queue(self, audio_queue):
def record_callback(_, audio:sr.AudioData) -> None:
data = audio.get_raw_data()
2023-05-13 02:42:38 +00:00
audio_queue.put((self.source_name, data, datetime.utcnow()))
2023-05-11 00:52:52 +00:00
self.recorder.listen_in_background(self.source, record_callback, phrase_time_limit=RECORD_TIMEOUT)
2023-05-13 02:42:38 +00:00
class DefaultMicRecorder(BaseRecorder):
2023-05-11 00:52:52 +00:00
def __init__(self):
2023-05-13 14:36:00 +00:00
super().__init__(source=sr.Microphone(sample_rate=16000), source_name="You")
2023-05-16 23:06:21 +00:00
self.adjust_for_noise("Default Mic", "Please make some noise from the Default Mic...")
2023-05-11 00:52:52 +00:00
2023-05-13 02:42:38 +00:00
class DefaultSpeakerRecorder(BaseRecorder):
def __init__(self):
2023-05-11 00:52:52 +00:00
with pyaudio.PyAudio() as p:
wasapi_info = p.get_host_api_info_by_type(pyaudio.paWASAPI)
2023-05-13 02:42:38 +00:00
default_speakers = p.get_device_info_by_index(wasapi_info["defaultOutputDevice"])
2023-05-11 00:52:52 +00:00
2023-05-13 02:42:38 +00:00
if not default_speakers["isLoopbackDevice"]:
2023-05-11 00:52:52 +00:00
for loopback in p.get_loopback_device_info_generator():
2023-05-13 02:42:38 +00:00
if default_speakers["name"] in loopback["name"]:
default_speakers = loopback
2023-05-11 00:52:52 +00:00
break
else:
2023-05-13 02:42:38 +00:00
print("[ERROR] No loopback device found.")
2023-05-11 00:52:52 +00:00
2023-05-13 14:36:00 +00:00
source = sr.Microphone(speaker=True,
device_index= default_speakers["index"],
sample_rate=int(default_speakers["defaultSampleRate"]),
chunk_size=pyaudio.get_sample_size(pyaudio.paInt16),
channels=default_speakers["maxInputChannels"])
super().__init__(source=source, source_name="Speaker")
2023-05-16 23:06:21 +00:00
self.adjust_for_noise("Default Speaker", "Please make or play some noise from the Default Speaker...")