Skip to content

Commit d2f80db

Browse files
stream_record.py: add periodic flushing
1 parent ddf8656 commit d2f80db

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

internal_filesystem/lib/mpos/audio/stream_record.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,13 @@ def record(self):
259259
start_time = time.ticks_ms()
260260
sample_offset = 0 # For sine wave phase continuity
261261

262-
print(f"RecordStream: max_bytes={max_bytes}, chunk_size={chunk_size}")
262+
# Flush every ~2 seconds of audio (64KB at 16kHz 16-bit mono)
263+
# This spreads out the filesystem write overhead
264+
flush_interval_bytes = 64 * 1024
265+
bytes_since_flush = 0
266+
last_flush_time = start_time
267+
268+
print(f"RecordStream: max_bytes={max_bytes}, chunk_size={chunk_size}, flush_interval={flush_interval_bytes}")
263269

264270
# Open file for appending audio data (append mode to avoid seek issues)
265271
print(f"RecordStream: Opening file for audio data...")
@@ -296,9 +302,19 @@ def record(self):
296302
if num_read > 0:
297303
f.write(buf[:num_read])
298304
self._bytes_recorded += num_read
305+
bytes_since_flush += num_read
306+
307+
# Periodic flush to spread out filesystem overhead
308+
if bytes_since_flush >= flush_interval_bytes:
309+
t0 = time.ticks_ms()
310+
f.flush()
311+
flush_time = time.ticks_diff(time.ticks_ms(), t0)
312+
print(f"RecordStream: Flushed {bytes_since_flush} bytes in {flush_time}ms")
313+
bytes_since_flush = 0
314+
last_flush_time = time.ticks_ms()
299315
finally:
300316
# Explicitly close the file and measure time
301-
print(f"RecordStream: Closing audio data file...")
317+
print(f"RecordStream: Closing audio data file (remaining {bytes_since_flush} bytes)...")
302318
t0 = time.ticks_ms()
303319
f.close()
304320
print(f"RecordStream: File closed in {time.ticks_diff(time.ticks_ms(), t0)}ms")

0 commit comments

Comments
 (0)