fix(protocol): update extra chunk fetching logic to stop at silence detection

This commit is contained in:
2026-04-23 18:28:14 -04:00
parent ecd980d345
commit fa887b85d9
2 changed files with 21 additions and 16 deletions
+20 -15
View File
@@ -652,13 +652,12 @@ class MiniMateProtocol:
# and primes the device to return a valid footer in the termination # and primes the device to return a valid footer in the termination
# response. Without it, termination returns an empty ack with no # response. Without it, termination returns an empty ack with no
# footer bytes (confirmed 2026-04-23 from HxD comparison). # footer bytes (confirmed 2026-04-23 from HxD comparison).
# Download extra chunks until we find the footer chunk (contains # Download extra chunks until we hit post-event silence (all-FF
# 0x0e 0x08 near its end) or hit the extra_chunks_after_metadata # ADC values) or the cap. The device returns the footer in the
# cap. The footer bytes are embedded in the last meaningful data # termination response only when we stop at the right point —
# chunk, NOT in the termination response. For short events (1-sec) # right after the last real data chunk, before silence starts.
# the footer is in extra chunk 1. For longer events it is in a # Silence detection: >80% of payload bytes are 0xFF.
# later chunk. Confirmed 2026-04-23 from BW file comparison. log.debug("5A A5[%d] metadata found — fetching extra chunks until silence",
log.debug("5A A5[%d] metadata found — fetching extra chunks until footer found",
chunk_num) chunk_num)
for _extra_n in range(extra_chunks_after_metadata): for _extra_n in range(extra_chunks_after_metadata):
chunk_num += 1 chunk_num += 1
@@ -667,20 +666,26 @@ class MiniMateProtocol:
self._send(build_5a_frame(_BULK_CHUNK_OFFSET, params)) self._send(build_5a_frame(_BULK_CHUNK_OFFSET, params))
try: try:
extra = self._recv_one(expected_sub=rsp_sub, timeout=10.0) extra = self._recv_one(expected_sub=rsp_sub, timeout=10.0)
log.debug("5A A5[%d] extra chunk page_key=0x%04X data_len=%d has_footer=%s", payload = extra.data[7:] # skip 7-byte frame header
chunk_num, extra.page_key, len(extra.data), ff_ratio = payload.count(0xFF) / max(len(payload), 1)
b'\x0e\x08' in extra.data[-50:]) is_silence = ff_ratio > 0.8
log.debug(
"5A A5[%d] extra chunk page_key=0x%04X data_len=%d "
"ff_ratio=%.2f silence=%s",
chunk_num, extra.page_key, len(extra.data),
ff_ratio, is_silence,
)
if extra.page_key == 0x0000: if extra.page_key == 0x0000:
if include_terminator: if include_terminator:
frames_data.append(extra) frames_data.append(extra)
return frames_data return frames_data
frames_data.append(extra) if is_silence:
# Stop as soon as we find the footer marker in the tail # Don't include the silence chunk — terminate here.
# of this chunk — the body is complete. # The termination response will contain the footer.
if b'\x0e\x08' in extra.data[-50:]: log.debug("5A A5[%d] silence detected — stopping before this chunk",
log.debug("5A A5[%d] footer marker found — stopping extra chunks",
chunk_num) chunk_num)
break break
frames_data.append(extra)
except TimeoutError: except TimeoutError:
log.debug("5A extra chunk %d timed out — end of stream", _extra_n + 1) log.debug("5A extra chunk %d timed out — end of stream", _extra_n + 1)
break break
+1 -1
View File
@@ -894,7 +894,7 @@ def device_event_blastware_file(
rectime = float(info.compliance_config.record_time or 1.0) rectime = float(info.compliance_config.record_time or 1.0)
except (AttributeError, TypeError, ValueError): except (AttributeError, TypeError, ValueError):
pass pass
extra_chunks = max(4, int(rectime * 4) + 4) extra_chunks = max(4, int(rectime * 6) + 8) # generous cap; silence detection stops early
log.info("blastware_file: rectime=%.1fs → extra_chunks_cap=%d", rectime, extra_chunks) log.info("blastware_file: rectime=%.1fs → extra_chunks_cap=%d", rectime, extra_chunks)
events = client.get_events( events = client.get_events(
full_waveform=False, full_waveform=False,