diff --git a/AudioRecorder.py b/AudioRecorder.py index 8c09227..7b6d944 100644 --- a/AudioRecorder.py +++ b/AudioRecorder.py @@ -1,7 +1,15 @@ import custom_speech_recognition as sr -import pyaudiowpatch as pyaudio +import os from datetime import datetime +try: + import pyaudiowpatch as pyaudio +except ImportError: + if os.name != "nt": + import pyaudio + else: + raise + RECORD_TIMEOUT = 3 ENERGY_THRESHOLD = 1000 DYNAMIC_ENERGY_THRESHOLD = False @@ -37,23 +45,44 @@ def __init__(self): 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: + + # Different implementations of obtaining the info dict of a default speaker, for different platforms + if os.name == "nt": + def _get_default_speaker(self): + # Requires PyAudioWPatch >= 0.2.12.6 + with pyaudio.PyAudio() as p: + try: + # Get loopback of default WASAPI speaker + return p.get_default_wasapi_loopback() + + except OSError: + print("[ERROR] Looks like WASAPI is not available on the system.") + + except LookupError: print("[ERROR] No loopback device found.") + + else: + def _get_default_speaker(self): + # At the moment, recording from speakers is only available under Windows + # raise NotImplementedError("Recording from speakers is only available under Windows") + + # As far as I understand, now the code style does not provide + # for error handling - only print them. + print("[ERROR] Recording from speakers is only available under Windows.") + + + def __init__(self): + default_speaker = self._get_default_speaker() + + if not default_speaker: + print("[ERROR] Something went wrong while trying to get the default speakers.") + super().__init__(source=sr.Microphone(sample_rate=16000), source_name="Speaker") + return source = sr.Microphone(speaker=True, - device_index= default_speakers["index"], - sample_rate=int(default_speakers["defaultSampleRate"]), + device_index=default_speaker["index"], + sample_rate=int(default_speaker["defaultSampleRate"]), chunk_size=pyaudio.get_sample_size(pyaudio.paInt16), - channels=default_speakers["maxInputChannels"]) + channels=default_speaker["maxInputChannels"]) super().__init__(source=source, source_name="Speaker") - self.adjust_for_noise("Default Speaker", "Please make or play some noise from the Default Speaker...") \ No newline at end of file + self.adjust_for_noise("Default Speaker", "Please make or play some noise from the Default Speaker...") diff --git a/AudioTranscriber.py b/AudioTranscriber.py index b37eae8..bf245b8 100644 --- a/AudioTranscriber.py +++ b/AudioTranscriber.py @@ -7,9 +7,17 @@ import custom_speech_recognition as sr import io from datetime import timedelta -import pyaudiowpatch as pyaudio from heapq import merge +try: + import pyaudiowpatch as pyaudio +except ImportError: + if os.name != "nt": + import pyaudio + else: + raise + + PHRASE_TIMEOUT = 3.05 MAX_PHRASES = 10 @@ -112,4 +120,4 @@ def clear_transcript_data(self): self.audio_sources["Speaker"]["last_sample"] = bytes() self.audio_sources["You"]["new_phrase"] = True - self.audio_sources["Speaker"]["new_phrase"] = True \ No newline at end of file + self.audio_sources["Speaker"]["new_phrase"] = True diff --git a/custom_speech_recognition/__init__.py b/custom_speech_recognition/__init__.py index 1d339b0..d5694c9 100644 --- a/custom_speech_recognition/__init__.py +++ b/custom_speech_recognition/__init__.py @@ -109,7 +109,10 @@ def get_pyaudio(): try: import pyaudiowpatch as pyaudio except ImportError: - raise AttributeError("Could not find PyAudio; check installation") + if os.name != "nt": + import pyaudio + else: + raise 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__)) diff --git a/requirements.txt b/requirements.txt index 78f1554..5cb9913 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,6 +3,7 @@ openai-whisper==20230314 Wave==0.0.2 openai==0.27.6 customtkinter==5.1.3 -PyAudioWPatch==0.2.12.5 +PyAudioWPatch==0.2.12.6; sys_platform == 'win32' +pyaudio==0.2.13; sys_platform != 'win32' --extra-index-url https://download.pytorch.org/whl/cu117 -torch \ No newline at end of file +torch