From e330fea4a53ba845868ff5643a6cb2a25b16dbe6 Mon Sep 17 00:00:00 2001 From: adityaomar3 Date: Sat, 10 Aug 2024 22:38:08 +0530 Subject: [PATCH] chat: push to record Co-authored-by: Randy Mackay --- .../mavproxy_chat/chat_voice_to_text.py | 16 +++++++-------- MAVProxy/modules/mavproxy_chat/chat_window.py | 20 ++++++++++++++++++- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/MAVProxy/modules/mavproxy_chat/chat_voice_to_text.py b/MAVProxy/modules/mavproxy_chat/chat_voice_to_text.py index 5718d4bc06..53aea49f5d 100644 --- a/MAVProxy/modules/mavproxy_chat/chat_voice_to_text.py +++ b/MAVProxy/modules/mavproxy_chat/chat_voice_to_text.py @@ -5,7 +5,6 @@ AP_FLAKE8_CLEAN ''' -import time try: import pyaudio # install using, "sudo apt-get install python3-pyaudio" @@ -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 @@ -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) diff --git a/MAVProxy/modules/mavproxy_chat/chat_window.py b/MAVProxy/modules/mavproxy_chat/chat_window.py index 00e66c7cd0..be04592db5 100644 --- a/MAVProxy/modules/mavproxy_chat/chat_window.py +++ b/MAVProxy/modules/mavproxy_chat/chat_window.py @@ -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) @@ -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) @@ -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() @@ -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()