Skip to content

Commit

Permalink
Merge pull request #1 from s0d3s/main
Browse files Browse the repository at this point in the history
  • Loading branch information
mlubbad authored Jun 9, 2023
2 parents 82ddd74 + fd27ea4 commit 11ea194
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 44 deletions.
79 changes: 41 additions & 38 deletions AudioRecorder.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import custom_speech_recognition as sr
import os
from datetime import datetime

try:
import pyaudiowpatch as pyaudio
except ImportError:
import pyaudio
if os.name != "nt":
import pyaudio
else:
raise

RECORD_TIMEOUT = 3
ENERGY_THRESHOLD = 1000
Expand Down Expand Up @@ -41,45 +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:
p = pyaudio.PyAudio()
try:
if os.name == "nt":
wasapi_info = p.get_host_api_info_by_type(pyaudio.paWASAPI)
default_speakers = p.get_device_info_by_index(wasapi_info["defaultOutputDevice"])
else:
codeaudio_info = p.get_host_api_info_by_type(pyaudio.paCoreAudio)
default_speakers = p.get_device_info_by_index(codeaudio_info["defaultOutputDevice"])

# 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.")

isLoopbackDevice = False
if os.name == "nt":
isLoopbackDevice = default_speakers["isLoopbackDevice"]
else:
isLoopbackDevice = default_speakers.get("isLoopbackDevice", False)
if not isLoopbackDevice:
if os.name != "nt":
for i in range(p.get_device_count()):
device_info = p.get_device_info_by_index(i)
if device_info['maxInputChannels'] > 0 and device_info['hostApi'] == p.get_default_host_api_info()['index']:
default_speakers = loopback = device_info
break
else:
print("[ERROR] No loopback device found.")

else:
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.")
finally:
p.terminate()

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...")
8 changes: 6 additions & 2 deletions AudioTranscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
import io
from datetime import timedelta
from heapq import merge

try:
import pyaudiowpatch as pyaudio
import pyaudiowpatch as pyaudio
except ImportError:
import pyaudio
if os.name != "nt":
import pyaudio
else:
raise


PHRASE_TIMEOUT = 3.05
Expand Down
7 changes: 5 additions & 2 deletions custom_speech_recognition/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,12 @@ 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 pyaudiowpatch as pyaudio
except ImportError:
import pyaudio
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__))
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +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
pyaudio==0.2.13

0 comments on commit 11ea194

Please sign in to comment.