diff --git a/MAVProxy/modules/mavproxy_chat/chat_voice_to_text.py b/MAVProxy/modules/mavproxy_chat/chat_voice_to_text.py index 5718d4bc06..9164f38106 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,20 @@ 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 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() + # update the recording state back to false globally + 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..ef713d1089 100644 --- a/MAVProxy/modules/mavproxy_chat/chat_window.py +++ b/MAVProxy/modules/mavproxy_chat/chat_window.py @@ -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 @@ -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 @@ -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()