Skip to content

Commit ddf8656

Browse files
Improve recording UX
1 parent cb05fc4 commit ddf8656

File tree

2 files changed

+44
-15
lines changed

2 files changed

+44
-15
lines changed

internal_filesystem/apps/com.micropythonos.soundrecorder/assets/sound_recorder.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -307,13 +307,17 @@ def _stop_recording(self):
307307
AudioFlinger.stop()
308308
self._is_recording = False
309309

310-
# Update UI
311-
self._record_button_label.set_text(lv.SYMBOL.AUDIO + " Record")
312-
self._record_button.set_style_bg_color(lv.theme_get_color_primary(None), 0)
313-
self._update_status()
310+
# Show "Saving..." status immediately (file finalization takes time on SD card)
311+
self._status_label.set_text("Saving...")
312+
self._status_label.set_style_text_color(lv.color_hex(0xFF8800), 0) # Orange
314313

315-
# Stop timer update
316-
self._stop_timer_update()
314+
# Disable record button while saving
315+
self._record_button.add_flag(lv.obj.FLAG.HIDDEN)
316+
317+
# Stop timer update but keep the elapsed time visible
318+
if self._timer_task:
319+
self._timer_task.delete()
320+
self._timer_task = None
317321

318322
def _on_recording_complete(self, message):
319323
"""Callback when recording finishes."""
@@ -326,14 +330,17 @@ def _recording_finished(self, message):
326330
"""Update UI after recording finishes (called on main thread)."""
327331
self._is_recording = False
328332

329-
# Update UI
333+
# Re-enable and reset record button
334+
self._record_button.remove_flag(lv.obj.FLAG.HIDDEN)
330335
self._record_button_label.set_text(lv.SYMBOL.AUDIO + " Record")
331336
self._record_button.set_style_bg_color(lv.theme_get_color_primary(None), 0)
337+
338+
# Update status and find recordings
332339
self._update_status()
333340
self._find_last_recording()
334341

335-
# Stop timer update
336-
self._stop_timer_update()
342+
# Reset timer display
343+
self._timer_label.set_text(self._format_timer_text(0))
337344

338345
def _start_timer_update(self):
339346
"""Start updating the timer display."""

internal_filesystem/lib/mpos/audio/stream_record.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,14 @@ def record(self):
262262
print(f"RecordStream: max_bytes={max_bytes}, chunk_size={chunk_size}")
263263

264264
# Open file for appending audio data (append mode to avoid seek issues)
265-
with open(self.file_path, 'ab') as f:
266-
buf = bytearray(chunk_size)
265+
print(f"RecordStream: Opening file for audio data...")
266+
t0 = time.ticks_ms()
267+
f = open(self.file_path, 'ab')
268+
print(f"RecordStream: File opened in {time.ticks_diff(time.ticks_ms(), t0)}ms")
267269

270+
buf = bytearray(chunk_size)
271+
272+
try:
268273
while self._keep_running and self._bytes_recorded < max_bytes:
269274
# Check elapsed time
270275
elapsed = time.ticks_diff(time.ticks_ms(), start_time)
@@ -291,13 +296,30 @@ def record(self):
291296
if num_read > 0:
292297
f.write(buf[:num_read])
293298
self._bytes_recorded += num_read
294-
295-
# Close the file first, then reopen to update header
299+
finally:
300+
# Explicitly close the file and measure time
301+
print(f"RecordStream: Closing audio data file...")
302+
t0 = time.ticks_ms()
303+
f.close()
304+
print(f"RecordStream: File closed in {time.ticks_diff(time.ticks_ms(), t0)}ms")
305+
306+
# Now reopen to update header
296307
# This avoids the massive delay caused by seeking backwards in a large file
297308
# on ESP32 with SD card (FAT filesystem buffering issue)
309+
print(f"RecordStream: Reopening file to update WAV header...")
310+
t0 = time.ticks_ms()
311+
f = open(self.file_path, 'r+b')
312+
print(f"RecordStream: File reopened in {time.ticks_diff(time.ticks_ms(), t0)}ms")
313+
298314
print(f"RecordStream: Updating WAV header with data_size={self._bytes_recorded}")
299-
with open(self.file_path, 'r+b') as f:
300-
self._update_wav_header(f, self._bytes_recorded)
315+
t0 = time.ticks_ms()
316+
self._update_wav_header(f, self._bytes_recorded)
317+
print(f"RecordStream: Header updated in {time.ticks_diff(time.ticks_ms(), t0)}ms")
318+
319+
print(f"RecordStream: Closing header file...")
320+
t0 = time.ticks_ms()
321+
f.close()
322+
print(f"RecordStream: Header file closed in {time.ticks_diff(time.ticks_ms(), t0)}ms")
301323

302324
elapsed_ms = time.ticks_diff(time.ticks_ms(), start_time)
303325
print(f"RecordStream: Finished recording {self._bytes_recorded} bytes ({elapsed_ms}ms)")

0 commit comments

Comments
 (0)