diff --git a/AudioRecorder.py b/AudioRecorder.py index 9766398..954fb8e 100644 --- a/AudioRecorder.py +++ b/AudioRecorder.py @@ -1,5 +1,5 @@ import custom_speech_recognition as sr -import pyaudiowpatch as pyaudio +import pyaudio from datetime import datetime RECORD_TIMEOUT = 3 @@ -7,7 +7,7 @@ DYNAMIC_ENERGY_THRESHOLD = False class BaseRecorder: - def __init__(self, source): + def __init__(self, source, source_name): self.recorder = sr.Recognizer() self.recorder.energy_threshold = ENERGY_THRESHOLD self.recorder.dynamic_energy_threshold = DYNAMIC_ENERGY_THRESHOLD @@ -16,6 +16,7 @@ def __init__(self, source): raise ValueError("audio source can't be None") self.source = source + self.source_name = source_name def adjust_for_noise(self, device_name, msg): print(f"[INFO] Adjusting for ambient noise from {device_name}. " + msg) @@ -26,33 +27,23 @@ def adjust_for_noise(self, device_name, msg): def record_into_queue(self, audio_queue): def record_callback(_, audio:sr.AudioData) -> None: data = audio.get_raw_data() - audio_queue.put((data, datetime.utcnow())) + audio_queue.put((self.source_name, data, datetime.utcnow())) self.recorder.listen_in_background(self.source, record_callback, phrase_time_limit=RECORD_TIMEOUT) class DefaultMicRecorder(BaseRecorder): def __init__(self): - super().__init__(source=sr.Microphone(sample_rate=16000)) + super().__init__(source=sr.Microphone(sample_rate=16000), source_name="You") self.adjust_for_noise("Default Mic", "Please make some noise from the Default Mic...") class DefaultSpeakerRecorder(BaseRecorder): def __init__(self): - with pyaudio.PyAudio() as p: - wasapi_info = p.get_host_api_info_by_type(pyaudio.paWASAPI) - default_speakers = p.get_device_info_by_index(wasapi_info["defaultOutputDevice"]) - - if not default_speakers["isLoopbackDevice"]: - for loopback in p.get_loopback_device_info_generator(): - if default_speakers["name"] in loopback["name"]: - default_speakers = loopback - break - else: - print("[ERROR] No loopback device found.") - - 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) - self.adjust_for_noise("Default Speaker", "Please make or play some noise from the Default Speaker...") \ No newline at end of file + p = pyaudio.PyAudio() + try: + # Use the default input device as the audio source + default_device_index = p.get_default_input_device_info()["index"] + source = sr.Microphone(device_index=default_device_index) + super().__init__(source=source, source_name="Speaker") + self.adjust_for_noise("Default Speaker", "Please make or play some noise from the Default Speaker...") + finally: + p.terminate() diff --git a/AudioTranscriber.py b/AudioTranscriber.py index 63f5c99..de743f2 100644 --- a/AudioTranscriber.py +++ b/AudioTranscriber.py @@ -5,7 +5,7 @@ import custom_speech_recognition as sr import io from datetime import timedelta -import pyaudiowpatch as pyaudio +import pyaudio from heapq import merge PHRASE_TIMEOUT = 3.05 diff --git a/custom_speech_recognition/__init__.py b/custom_speech_recognition/__init__.py index 1d339b0..cd324f2 100644 --- a/custom_speech_recognition/__init__.py +++ b/custom_speech_recognition/__init__.py @@ -107,7 +107,7 @@ 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 + import pyaudio except ImportError: raise AttributeError("Could not find PyAudio; check installation") from distutils.version import LooseVersion diff --git a/requirements.txt b/requirements.txt index 5c2ea6b..789150c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,6 +3,6 @@ faster-whisper Wave openai customtkinter -PyAudioWPatch +pyaudio torch>=2.2.0 --extra-index-url https://download.pytorch.org/whl/cu121 --no-cache-dir ctranslate2==3.24.0 \ No newline at end of file