Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chat: push to record #1435

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 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,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
rmackay9 marked this conversation as resolved.
Show resolved Hide resolved

# 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