Skip to content

Commit

Permalink
Merge pull request #10 from GregorD1A1/dev
Browse files Browse the repository at this point in the history
Found and solved bugs with Linux install before 0.1.0 release
  • Loading branch information
Grigorij-Dudnik authored Sep 18, 2024
2 parents 1233f9e + 69cb93c commit b938748
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 deletions.
41 changes: 23 additions & 18 deletions utilities/user_input.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import sys

from pynput.keyboard import Key, Listener
from utilities.voice_utils import VoiceRecorder
from utilities.util_functions import print_formatted
import time

recorder = VoiceRecorder()

# Old try, to remove
class InputHandler():
def __init__(self):
self.output_string = ""
Expand Down Expand Up @@ -46,27 +44,34 @@ def press_interrupt(key):



def user_input_old(prompt=""):
input_class = InputHandler()
return input_class.user_input(prompt)


def user_input(prompt=""):
print_formatted(prompt + " Or use (m)icrophone to record it:", color="yellow", bold=True)
user_sentence = input()
if user_sentence == 'm':
recorder.start_recording()
def press_interrupt(key):
if key == Key.enter:
recorder.stop_recording()
print("Recording finished.\n")
return False # Stop listener
with Listener(on_press=press_interrupt, suppress=True) as listener:
listener.join()

user_sentence = recorder.transcribe_audio()
if recorder.microphone_available:
user_sentence = record_voice_message()
else:
print_formatted(
"Install 'sudo apt-get install libportaudio2' (Linux) or 'brew install portaudio' (Mac) to use microphone feature.", color="light_red"
)
user_sentence = input()

return user_sentence


def record_voice_message():
recorder.start_recording()

def press_interrupt(key):
if key == Key.enter:
recorder.stop_recording()
print("Recording finished.\n")
return False # Stop listener

with Listener(on_press=press_interrupt, suppress=True) as listener:
listener.join()

return recorder.transcribe_audio()

if __name__ == "__main__":
print(user_input("Provide your feedback."))
3 changes: 2 additions & 1 deletion utilities/util_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ def convert_images(image_paths):


def join_paths(*args):
joined = '/'.join(p.strip('/') for p in args if p)
leading_slash = '/' if args[0].startswith('/') else ''
joined = leading_slash + '/'.join(p.strip('/') for p in args)
return os.path.normpath(joined)


Expand Down
17 changes: 11 additions & 6 deletions utilities/voice_utils.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
import os
from threading import Thread
import sounddevice
import soundfile
import tempfile
import queue
import sys
from openai import OpenAI
from dotenv import load_dotenv, find_dotenv
import time
from utilities.util_functions import print_formatted

load_dotenv(find_dotenv())
openai_client = OpenAI()


class VoiceRecorder:
def __init__(self):

self.microphone_available = True
try:
import sounddevice
import soundfile
self.sounddevice = sounddevice
self.soundfile = soundfile
except OSError:
self.microphone_available = False
self.recording_queue = queue.Queue()
self.sample_rate = 44100
self.soundfile_path = tempfile.mktemp(suffix=".wav")
self.is_recording = False
self.recording_thread = None

def record(self):
with soundfile.SoundFile(self.soundfile_path, mode='x', samplerate=self.sample_rate, channels=1) as file:
with sounddevice.InputStream(samplerate=self.sample_rate, channels=1, callback=self.save_sound_callback):
with self.soundfile.SoundFile(self.soundfile_path, mode='x', samplerate=self.sample_rate, channels=1) as file:
with self.sounddevice.InputStream(samplerate=self.sample_rate, channels=1, callback=self.save_sound_callback):
print('Press Enter to finish recording')
while self.is_recording:
file.write(self.recording_queue.get())
Expand Down

0 comments on commit b938748

Please sign in to comment.