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 <rmackay9@yahoo.com>
  • Loading branch information
adityaomar3 and rmackay9 committed Aug 10, 2024
1 parent e1aaf6a commit e330fea
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 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
20 changes: 19 additions & 1 deletion MAVProxy/modules/mavproxy_chat/chat_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ def __init__(self, mpstate, wait_for_command_ack_fn):
# keep reference to mpstate
self.mpstate = mpstate

# keep the recording state
self.isRecording = False

# create chat_openai object
self.chat_openai = chat_openai.chat_openai(self.mpstate, self.set_status_text, self.append_chat_replies,
wait_for_command_ack_fn)
Expand Down Expand Up @@ -58,6 +61,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.record_button.Bind(wx.EVT_LEFT_DOWN, self.record_button_pushed)
self.record_button.Bind(wx.EVT_LEFT_UP, self.record_button_released)
self.frame.Bind(wx.EVT_BUTTON, self.record_button_click, self.record_button)
self.horiz_sizer.Add(self.record_button, proportion=0, flag=wx.ALIGN_TOP | wx.ALL, border=5)

Expand Down Expand Up @@ -121,7 +126,7 @@ def apikey_close_button_click(self, event):

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

Expand All @@ -146,6 +151,19 @@ 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
self.set_status_text("recording button pressed")
self.record_button_click(event)

# record button released
def record_button_released(self, event):
# Run when mous click is released
self.set_status_text("recording button 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 e330fea

Please sign in to comment.