Summary
Waveform data parsed from generated files contains impossible min/max pairs where the minimum value is greater than the maximum value, indicating data corruption during parsing.
Description
When extracting waveform data using JustWaveform.extract() and then parsing the resulting file, the waveform data contains corrupted min/max pairs. For example:
// Example of corrupted data where min > max
[0, 4410, 0, 15092, 0, -3, 3, -3, 3, -3, ...]
In the above data, we can see pairs like 0, -3 where the first value (min) should be ≤ the second value (max), but instead we have 0 > -3, which is impossible for properly aligned min/max pairs.
Root Cause
The bug is located in the parse method in lib/just_waveform.dart at line 78:
|
final data = flags == 0 |
|
? Int16List.view(bytes, headerLength ~/ 2) |
|
: Int8List.view(bytes, headerLength); |
The issue is that headerLength ~/ 2 evaluates to 10 (since headerLength = 20), which means the waveform data is being read starting from byte 10 instead of byte 20. This causes the parser to read from the middle of the header rather than after the header, completely misaligning all the min/max pairs.
Fix
Change line 78 from:
Int16List.view(bytes, headerLength ~/ 2)
To:
Int16List.view(bytes, headerLength)
The Int16List.view constructor expects the offset parameter to be in bytes, not in number of shorts. Since the header is 20 bytes long, we should start reading the waveform data from byte offset 20, not 10.
Summary
Waveform data parsed from generated files contains impossible min/max pairs where the minimum value is greater than the maximum value, indicating data corruption during parsing.
Description
When extracting waveform data using
JustWaveform.extract()and then parsing the resulting file, the waveform data contains corrupted min/max pairs. For example:In the above data, we can see pairs like
0, -3where the first value (min) should be ≤ the second value (max), but instead we have0 > -3, which is impossible for properly aligned min/max pairs.Root Cause
The bug is located in the
parsemethod inlib/just_waveform.dartat line 78:just_waveform/lib/just_waveform.dart
Lines 77 to 79 in 2a82cb0
The issue is that
headerLength ~/ 2evaluates to10(sinceheaderLength = 20), which means the waveform data is being read starting from byte 10 instead of byte 20. This causes the parser to read from the middle of the header rather than after the header, completely misaligning all the min/max pairs.Fix
Change line 78 from:
To:
The
Int16List.viewconstructor expects the offset parameter to be in bytes, not in number of shorts. Since the header is 20 bytes long, we should start reading the waveform data from byte offset 20, not 10.