Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Continuous mode not working #148

Closed
wants to merge 40 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
9fde685
Update README.md for Transcribe
vivekuppal Jun 29, 2023
1483fea
Merge pull request #1 from vivekuppal/vu-readme-updates
vivekuppal Jun 29, 2023
391d728
Allow usage without a valid OPEN API key. (#2)
vivekuppal Jun 29, 2023
ab4245d
Update README.md (#3)
vivekuppal Jun 29, 2023
ebb6f2f
Allow user to choose model. Add arguments to main file.
vivekuppal Jun 29, 2023
8f5a595
Code clean up, add linting. (#4)
vivekuppal Jun 30, 2023
59d5c91
UI Text Chronology (#5)
vivekuppal Jun 30, 2023
f772bb8
Update readme with Enhancements. Allow copy of text from UI window. R…
vivekuppal Jun 30, 2023
87a38b1
Save conversation to text. (#9)
vivekuppal Jun 30, 2023
65d6dcf
Add Contextual Information to Responses (#11)
vivekuppal Jun 30, 2023
d1b3c45
Allow users to pause audio transcription. Change the default for gett…
vivekuppal Jul 3, 2023
cfca51a
Update main.py (#15)
abhinavuppal1 Jul 11, 2023
152bad3
Code reorg to separate UI code (#16)
vivekuppal Jul 12, 2023
addf17f
Add support for multiple languages (#18)
vivekuppal Jul 12, 2023
e5cda88
Easy install for non developers on windows (#20)
vivekuppal Jul 18, 2023
9896c1c
Disabled winrar UI (#22)
Adarsha-gg Jul 18, 2023
901501b
When using API, we do not need to specify language, absorb the lang p…
vivekuppal Jul 18, 2023
bd48b61
Language combo fix (#26)
Adarsha-gg Jul 19, 2023
7c9ca88
Added gdrive (#27)
Adarsha-gg Jul 19, 2023
2429c97
Allow usage of API Key in installed version of Transcribe (#28)
vivekuppal Jul 19, 2023
12ef846
updated the drive link (#30)
Adarsha-gg Jul 20, 2023
4be26c7
Add a duration class to easily measure the time taken for an operatio…
vivekuppal Jul 21, 2023
6e53b31
--api option was not working correctly (#34)
vivekuppal Jul 21, 2023
bd42b8c
Initial unit tests for the speech recognition library (#36)
vivekuppal Jul 24, 2023
af87eff
user reported defect fixes. (#39)
vivekuppal Jul 26, 2023
26cfaad
Optimize LLM usage (#40)
vivekuppal Jul 26, 2023
f8d5857
Bug fixes for exceptions observed during usage. Add further plumbing …
vivekuppal Jul 27, 2023
1356a78
Add logging infrastructure (#42)
vivekuppal Jul 27, 2023
a1cc48b
Get Response from LLM on demand (#44)
vivekuppal Jul 28, 2023
ea5f392
Models from open ai site (#43)
Adarsha-gg Jul 28, 2023
b4e03a4
List all active devices (#45)
vivekuppal Aug 1, 2023
85d09ed
Allow user to select input, output audio devices (#48)
vivekuppal Aug 21, 2023
28d1e9a
Disable mic speaker selectively (#49)
vivekuppal Aug 23, 2023
e48bdb8
Add Audio Response for LLM generated content (#50)
vivekuppal Aug 27, 2023
6baa77f
Update, upload latest binaries (#54)
Adarsha-gg Aug 30, 2023
fa55416
Multiturn prompts, bug fixes (#55)
vivekuppal Sep 5, 2023
ce5a1e1
Allow enable/disable speaker and microphone from UI (#56)
Adarsha-gg Sep 6, 2023
e445856
Update gdrive link (#58)
Adarsha-gg Sep 7, 2023
b50f58c
Bring readme up to date with current functionality. Describe content …
vivekuppal Sep 8, 2023
a7ea2cc
Continuous mode broke after updates to the UI.
vivekuppal Sep 8, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
max-line-length = 120
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
__pycache__/
*.wav
keys.py
.venv/
.venv/
venv
output
tiny.pt
Transcribe.log
223 changes: 209 additions & 14 deletions AudioRecorder.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,254 @@
from datetime import datetime
from abc import abstractmethod
import custom_speech_recognition as sr
import pyaudiowpatch as pyaudio
from datetime import datetime
import app_logging as al

RECORD_TIMEOUT = 3
ENERGY_THRESHOLD = 1000
DYNAMIC_ENERGY_THRESHOLD = False

root_logger = al.get_logger()


# https://people.csail.mit.edu/hubert/pyaudio/docs/#id6
driver_type = {
-1: 'Not actually an audio device',
0: 'Still in development',
1: 'DirectSound (Windows only)',
2: 'Multimedia Extension (Windows only)',
3: 'Steinberg Audio Stream Input/Output',
4: 'SoundManager (OSX only)',
5: 'CoreAudio (OSX only)',
7: 'Open Sound System (Linux only)',
8: 'Advanced Linux Sound Architecture (Linux only)',
9: 'Open Audio Library',
10: 'BeOS Sound System',
11: 'Windows Driver Model (Windows only)',
12: 'JACK Audio Connection Kit',
13: 'Windows Vista Audio stack architecture'
}


def print_detailed_audio_info(print_func=print):
"""
Print information about Host APIs and devices,
using `print_func`.

:param print_func: Print function(or wrapper)
:type print_func: function
:rtype: None
"""
print_func("\n|", "~ Audio Drivers on this machine ~".center(20), "|\n")
header = f" ^ #{'INDEX'.center(7)}#{'DRIVER TYPE'.center(13)}#{'DEVICE COUNT'.center(15)}#{'NAME'.center(5)}"
print_func(header)
print_func("-"*len(header))
py_audio = pyaudio.PyAudio()
for host_api in py_audio.get_host_api_info_generator():
print_func(
(
f" » "
f"{('['+str(host_api['index'])+']').center(8)}|"
f"{str(host_api['type']).center(13)}|"
f"{str(host_api['deviceCount']).center(15)}|"
f" {host_api['name']}"
)
)

print_func("\n\n\n|", "~ Audio Devices on this machine ~".center(20), "|\n")
header = f" ^ #{'INDEX'.center(7)}# HOST API INDEX #{'LOOPBACK'.center(10)}#{'NAME'.center(5)}"
print_func(header)
print_func("-"*len(header))
for device in py_audio.get_device_info_generator():
print_func(
(
f" » "
f"{('['+str(device['index'])+']').center(8)}"
f"{str(device['hostApi']).center(16)}"
f" {str(device['isLoopbackDevice']).center(10)}"
f" {device['name']}"
)
)

# Below statements are useful to view all available fields in the
# driver and device list
# Do not remove these statements from here
# print('Windows Audio Drivers')
# for host_api_info_gen in py_audio.get_host_api_info_generator():
# print(host_api_info_gen)

# print('Windows Audio Devices')
# for device_info_gen in py_audio.get_device_info_generator():
# print(device_info_gen)


class BaseRecorder:
"""Base class for Speaker, Microphone classes
"""
def __init__(self, source, source_name):
root_logger.info(BaseRecorder.__name__)
self.recorder = sr.Recognizer()
self.recorder.energy_threshold = ENERGY_THRESHOLD
self.recorder.dynamic_energy_threshold = DYNAMIC_ENERGY_THRESHOLD
# Determines if this device is being used for transcription
self.enabled = True

if source is None:
raise ValueError("audio source can't be None")

self.source = source
self.source_name = source_name

@abstractmethod
def get_name(self):
"""Get the name of this device
"""

def enable(self):
"""Enable transcription from this device
"""
self.enabled = True

def disable(self):
"""Disable transcription from this device
"""
self.enabled = False

def adjust_for_noise(self, device_name, msg):
root_logger.info(BaseRecorder.adjust_for_noise.__name__)
print(f"[INFO] Adjusting for ambient noise from {device_name}. " + msg)
with self.source:
self.recorder.adjust_for_ambient_noise(self.source)
print(f"[INFO] Completed ambient noise adjustment for {device_name}.")

def record_into_queue(self, audio_queue):
def record_callback(_, audio:sr.AudioData) -> None:
data = audio.get_raw_data()
audio_queue.put((self.source_name, data, datetime.utcnow()))
def record_callback(_, audio: sr.AudioData) -> None:
if self.enabled:
data = audio.get_raw_data()
audio_queue.put((self.source_name, data, datetime.utcnow()))

self.recorder.listen_in_background(self.source, record_callback,
phrase_time_limit=RECORD_TIMEOUT)

self.recorder.listen_in_background(self.source, record_callback, phrase_time_limit=RECORD_TIMEOUT)

class DefaultMicRecorder(BaseRecorder):
class MicRecorder(BaseRecorder):
"""Encapsultes the Microphone device audio input
"""
def __init__(self):
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...")
root_logger.info(MicRecorder.__name__)
with pyaudio.PyAudio() as py_audio:
# WASAPI is windows specific
wasapi_info = py_audio.get_host_api_info_by_type(pyaudio.paWASAPI)
self.device_index = wasapi_info["defaultInputDevice"]
default_mic = py_audio.get_device_info_by_index(self.device_index)

self.device_info = default_mic

source = sr.Microphone(device_index=default_mic["index"],
sample_rate=int(default_mic["defaultSampleRate"]),
channels=default_mic["maxInputChannels"]
)
self.source = source
super().__init__(source=source, source_name="You")
print(f'[INFO] Listening to sound from Microphone: {self.get_name()} ')
# This line is commented because in case of non default microphone it can occasionally take
# several minutes to execute, thus delaying the start of the application.
# self.adjust_for_noise("Default Mic", "Please make some noise from the Default Mic...")

def get_name(self):
return f'#{self.device_index} - {self.device_info["name"]}'

def set_device(self, index: int):
"""Set active device based on index.
"""
root_logger.info(MicRecorder.set_device.__name__)
with pyaudio.PyAudio() as py_audio:
self.device_index = index
mic = py_audio.get_device_info_by_index(self.device_index)

self.device_info = mic

class DefaultSpeakerRecorder(BaseRecorder):
source = sr.Microphone(device_index=mic["index"],
sample_rate=int(mic["defaultSampleRate"]),
channels=mic["maxInputChannels"]
)
self.source = source
print(f'[INFO] Listening to sound from Microphone: {self.get_name()} ')
self.adjust_for_noise("Mic", "Please make some noise from the chosen Mic...")


class SpeakerRecorder(BaseRecorder):
"""Encapsultes the Speaer device audio input
"""
def __init__(self):
root_logger.info(SpeakerRecorder.__name__)
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"])

self.device_index = wasapi_info["defaultOutputDevice"]
default_speakers = p.get_device_info_by_index(self.device_index)

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


self.device_info = default_speakers

source = sr.Microphone(speaker=True,
device_index= default_speakers["index"],
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")
self.adjust_for_noise("Default Speaker", "Please make or play some noise from the Default Speaker...")
print(f'[INFO] Listening to sound from Speaker: {self.get_name()} ')
self.adjust_for_noise("Default Speaker",
"Please play sound from Default Speaker...")

def get_name(self):
return f'#{self.device_index} - {self.device_info["name"]}'

def set_device(self, index: int):
"""Set active device based on index.
"""
root_logger.info(SpeakerRecorder.set_device.__name__)
with pyaudio.PyAudio() as p:
self.device_index = index
speakers = p.get_device_info_by_index(self.device_index)

if not speakers["isLoopbackDevice"]:
for loopback in p.get_loopback_device_info_generator():
if speakers["name"] in loopback["name"]:
speakers = loopback
break
else:
print("[ERROR] No loopback device found.")

self.device_info = speakers

source = sr.Microphone(speaker=True,
device_index=speakers["index"],
sample_rate=int(speakers["defaultSampleRate"]),
chunk_size=pyaudio.get_sample_size(pyaudio.paInt16),
channels=speakers["maxInputChannels"])
self.source = source
print(f'[INFO] Listening to sound from Speaker: {self.get_name()} ')
self.adjust_for_noise("Speaker",
f"Please play sound from selected Speakers {self.get_name()}...")


if __name__ == "__main__":
print_detailed_audio_info()
# Below statements are useful to view all available fields in the
# default Input Device.
# Do not delete these lines
# with pyaudio.PyAudio() as p:
# wasapi_info = p.get_host_api_info_by_type(pyaudio.paWASAPI)
# print(wasapi_info)

# with pyaudio.PyAudio() as p:
# wasapi_info = p.get_host_api_info_by_type(pyaudio.paWASAPI)
# default_mic = p.get_device_info_by_index(wasapi_info["defaultInputDevice"])
# print(default_mic)
Loading