Skip to content

Commit b90a2eb

Browse files
adityaomar3rmackay9
andcommitted
chat: push to record
Co-authored-by: Randy Mackay <[email protected]>
1 parent 1b716ea commit b90a2eb

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

MAVProxy/modules/mavproxy_chat/chat_voice_to_text.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
AP_FLAKE8_CLEAN
66
'''
77

8-
import time
98

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

17+
# initializing the global list to keep and update the stop_recording state
18+
stop_recording = [False]
19+
1820

1921
class chat_voice_to_text():
2022
def __init__(self):
21-
# initialise OpenAI connection
23+
# initialise variables
2224
self.client = None
2325
self.assistant = None
2426

@@ -53,22 +55,18 @@ def record_audio(self):
5355
print("chat: failed to connect to microphone")
5456
return None
5557

56-
# calculate time recording should stop
57-
curr_time = time.time()
58-
time_stop = curr_time + 5
59-
6058
# record until specified time
6159
frames = []
62-
while curr_time < time_stop:
60+
# while curr_time < time_stop and not self.stop_recording:
61+
while not stop_recording[0]:
6362
data = stream.read(1024)
6463
frames.append(data)
65-
curr_time = time.time()
6664

6765
# Stop and close the stream
6866
stream.stop_stream()
6967
stream.close()
7068
p.terminate()
71-
69+
stop_recording[0] = False
7270
# Save audio file
7371
wf = wave.open("recording.wav", "wb")
7472
wf.setnchannels(1)

MAVProxy/modules/mavproxy_chat/chat_window.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ def __init__(self, mpstate, wait_for_command_ack_fn):
5858

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

@@ -121,7 +123,7 @@ def apikey_close_button_click(self, event):
121123

122124
# record button clicked
123125
def record_button_click(self, event):
124-
# run record_button_click_execute in a new thread
126+
print("record button clicked")
125127
th = Thread(target=self.record_button_click_execute, args=(event,))
126128
th.start()
127129

@@ -146,6 +148,19 @@ def record_button_click_execute(self, event):
146148
self.set_status_text("sending text to assistasnt")
147149
self.send_text_to_assistant()
148150

151+
# record button pushed
152+
def record_button_pushed(self, event):
153+
# run record_button_click_execute in a new thread
154+
self.set_status_text("recording button pressed")
155+
self.record_button_click(event)
156+
157+
# record button released
158+
def record_button_released(self, event):
159+
# Run when mous click is released
160+
self.set_status_text("recording button released")
161+
# set the stop_recording status to True
162+
chat_voice_to_text.stop_recording[0] = True
163+
149164
# cancel button clicked
150165
def cancel_button_click(self, event):
151166
self.chat_openai.cancel_run()

0 commit comments

Comments
 (0)