You've already forked MicroPythonOS
mirror of
https://github.com/m5stack/MicroPythonOS.git
synced 2026-05-20 11:51:27 -07:00
Improve recording UX
This commit is contained in:
@@ -307,13 +307,17 @@ class SoundRecorder(Activity):
|
||||
AudioFlinger.stop()
|
||||
self._is_recording = False
|
||||
|
||||
# Update UI
|
||||
self._record_button_label.set_text(lv.SYMBOL.AUDIO + " Record")
|
||||
self._record_button.set_style_bg_color(lv.theme_get_color_primary(None), 0)
|
||||
self._update_status()
|
||||
# Show "Saving..." status immediately (file finalization takes time on SD card)
|
||||
self._status_label.set_text("Saving...")
|
||||
self._status_label.set_style_text_color(lv.color_hex(0xFF8800), 0) # Orange
|
||||
|
||||
# Stop timer update
|
||||
self._stop_timer_update()
|
||||
# Disable record button while saving
|
||||
self._record_button.add_flag(lv.obj.FLAG.HIDDEN)
|
||||
|
||||
# Stop timer update but keep the elapsed time visible
|
||||
if self._timer_task:
|
||||
self._timer_task.delete()
|
||||
self._timer_task = None
|
||||
|
||||
def _on_recording_complete(self, message):
|
||||
"""Callback when recording finishes."""
|
||||
@@ -326,14 +330,17 @@ class SoundRecorder(Activity):
|
||||
"""Update UI after recording finishes (called on main thread)."""
|
||||
self._is_recording = False
|
||||
|
||||
# Update UI
|
||||
# Re-enable and reset record button
|
||||
self._record_button.remove_flag(lv.obj.FLAG.HIDDEN)
|
||||
self._record_button_label.set_text(lv.SYMBOL.AUDIO + " Record")
|
||||
self._record_button.set_style_bg_color(lv.theme_get_color_primary(None), 0)
|
||||
|
||||
# Update status and find recordings
|
||||
self._update_status()
|
||||
self._find_last_recording()
|
||||
|
||||
# Stop timer update
|
||||
self._stop_timer_update()
|
||||
# Reset timer display
|
||||
self._timer_label.set_text(self._format_timer_text(0))
|
||||
|
||||
def _start_timer_update(self):
|
||||
"""Start updating the timer display."""
|
||||
|
||||
@@ -262,9 +262,14 @@ class RecordStream:
|
||||
print(f"RecordStream: max_bytes={max_bytes}, chunk_size={chunk_size}")
|
||||
|
||||
# Open file for appending audio data (append mode to avoid seek issues)
|
||||
with open(self.file_path, 'ab') as f:
|
||||
buf = bytearray(chunk_size)
|
||||
print(f"RecordStream: Opening file for audio data...")
|
||||
t0 = time.ticks_ms()
|
||||
f = open(self.file_path, 'ab')
|
||||
print(f"RecordStream: File opened in {time.ticks_diff(time.ticks_ms(), t0)}ms")
|
||||
|
||||
buf = bytearray(chunk_size)
|
||||
|
||||
try:
|
||||
while self._keep_running and self._bytes_recorded < max_bytes:
|
||||
# Check elapsed time
|
||||
elapsed = time.ticks_diff(time.ticks_ms(), start_time)
|
||||
@@ -291,13 +296,30 @@ class RecordStream:
|
||||
if num_read > 0:
|
||||
f.write(buf[:num_read])
|
||||
self._bytes_recorded += num_read
|
||||
finally:
|
||||
# Explicitly close the file and measure time
|
||||
print(f"RecordStream: Closing audio data file...")
|
||||
t0 = time.ticks_ms()
|
||||
f.close()
|
||||
print(f"RecordStream: File closed in {time.ticks_diff(time.ticks_ms(), t0)}ms")
|
||||
|
||||
# Close the file first, then reopen to update header
|
||||
# Now reopen to update header
|
||||
# This avoids the massive delay caused by seeking backwards in a large file
|
||||
# on ESP32 with SD card (FAT filesystem buffering issue)
|
||||
print(f"RecordStream: Reopening file to update WAV header...")
|
||||
t0 = time.ticks_ms()
|
||||
f = open(self.file_path, 'r+b')
|
||||
print(f"RecordStream: File reopened in {time.ticks_diff(time.ticks_ms(), t0)}ms")
|
||||
|
||||
print(f"RecordStream: Updating WAV header with data_size={self._bytes_recorded}")
|
||||
with open(self.file_path, 'r+b') as f:
|
||||
self._update_wav_header(f, self._bytes_recorded)
|
||||
t0 = time.ticks_ms()
|
||||
self._update_wav_header(f, self._bytes_recorded)
|
||||
print(f"RecordStream: Header updated in {time.ticks_diff(time.ticks_ms(), t0)}ms")
|
||||
|
||||
print(f"RecordStream: Closing header file...")
|
||||
t0 = time.ticks_ms()
|
||||
f.close()
|
||||
print(f"RecordStream: Header file closed in {time.ticks_diff(time.ticks_ms(), t0)}ms")
|
||||
|
||||
elapsed_ms = time.ticks_diff(time.ticks_ms(), start_time)
|
||||
print(f"RecordStream: Finished recording {self._bytes_recorded} bytes ({elapsed_ms}ms)")
|
||||
|
||||
Reference in New Issue
Block a user