Skip to content

Commit

Permalink
chat: push to record
Browse files Browse the repository at this point in the history
Co-authored-by: Randy Mackay <[email protected]>
  • Loading branch information
adityaomar3 and rmackay9 committed Aug 15, 2024
1 parent 1b716ea commit 9d07612
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
16 changes: 7 additions & 9 deletions MAVProxy/modules/mavproxy_chat/chat_voice_to_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
AP_FLAKE8_CLEAN
'''

import time

try:
import pyaudio # install using, "sudo apt-get install python3-pyaudio"
Expand All @@ -15,10 +14,13 @@
print("chat: failed to import pyaudio, wave or openai. See https://ardupilot.org/mavproxy/docs/modules/chat.html")
exit()

# initializing the global list to keep and update the stop_recording state
stop_recording = [False]


class chat_voice_to_text():
def __init__(self):
# initialise OpenAI connection
# initialise variables
self.client = None
self.assistant = None

Expand Down Expand Up @@ -53,22 +55,18 @@ def record_audio(self):
print("chat: failed to connect to microphone")
return None

# calculate time recording should stop
curr_time = time.time()
time_stop = curr_time + 5

# record until specified time
frames = []
while curr_time < time_stop:
# while curr_time < time_stop and not self.stop_recording:
while not stop_recording[0]:
data = stream.read(1024)
frames.append(data)
curr_time = time.time()

# Stop and close the stream
stream.stop_stream()
stream.close()
p.terminate()

stop_recording[0] = False
# Save audio file
wf = wave.open("recording.wav", "wb")
wf.setnchannels(1)
Expand Down
21 changes: 14 additions & 7 deletions MAVProxy/modules/mavproxy_chat/chat_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ def __init__(self, mpstate, wait_for_command_ack_fn):

# add a record button
self.record_button = wx.Button(self.frame, id=-1, label="Rec", size=(75, 25))
self.frame.Bind(wx.EVT_BUTTON, self.record_button_click, self.record_button)
self.record_button.Bind(wx.EVT_LEFT_DOWN, self.record_button_pushed, self.record_button)
self.record_button.Bind(wx.EVT_LEFT_UP, self.record_button_released, self.record_button)
self.horiz_sizer.Add(self.record_button, proportion=0, flag=wx.ALIGN_TOP | wx.ALL, border=5)

# add an input text box
Expand Down Expand Up @@ -119,12 +120,6 @@ def apikey_set_button_click(self, event):
def apikey_close_button_click(self, event):
self.apikey_frame.Hide()

# record button clicked
def record_button_click(self, event):
# run record_button_click_execute in a new thread
th = Thread(target=self.record_button_click_execute, args=(event,))
th.start()

# record button clicked
def record_button_click_execute(self, event):
# record audio
Expand All @@ -146,6 +141,18 @@ def record_button_click_execute(self, event):
self.set_status_text("sending text to assistasnt")
self.send_text_to_assistant()

# record button pushed
def record_button_pushed(self, event):
# run record_button_click_execute in a new thread
th = Thread(target=self.record_button_click_execute, args=(event,))
th.start()

# record button released
def record_button_released(self, event):
# Run when mouse click is released
# set the stop_recording status to True
chat_voice_to_text.stop_recording[0] = True

# cancel button clicked
def cancel_button_click(self, event):
self.chat_openai.cancel_run()
Expand Down

0 comments on commit 9d07612

Please sign in to comment.