-
Notifications
You must be signed in to change notification settings - Fork 288
Closed
Description
Package version
record: ^6.1.1
Environment
- OS: Windows 11
- Platform: Flutter (platform-independent issue)
Describe the bug
The helper method convertBytesToInt16 returns incorrect PCM16 sample values when the provided Uint8List is a view with a non-zero offsetInBytes.
This happens because ByteData.view(bytes.buffer) ignores bytes.offsetInBytes and always reads from the beginning of the underlying buffer. In audio streaming scenarios, buffers are often provided as views, which leads to misaligned samples.
To Reproduce
- Create a
Uint8List.viewwith a non-zero byte offset - Call
convertBytesToInt16with that view - Observe incorrect sample values
Minimal reproducible example:
final bigBuffer = Uint8List.fromList([
0x01, 0x00, // 1
0x02, 0x00, // 2
0x03, 0x00, // 3
0x04, 0x00, // 4
]);
// View containing only samples [3, 4]
final view = Uint8List.view(bigBuffer.buffer, 4, 4);
// Expected: [3, 4]
// Actual: [1, 2]
final result = convertBytesToInt16(view);Improvement Suggestion:
/// Converts a [Uint8List] of bytes to a [List<int>] of signed 16-bit integers.
///
/// [endian] specifies the byte order (default: [Endian.little]).
/// Throws [ArgumentError] if `bytes.length` is not even.
List<int> convertBytesToInt16(Uint8List bytes, [Endian endian = Endian.little]) {
if (bytes.length % 2 != 0) {
throw ArgumentError('Input byte length must be even.');
}
// Ensure correct byte offset and length are used
final byteData = ByteData.sublistView(bytes);
final values = List<int>.filled(bytes.length ~/ 2, 0);
for (var i = 0; i < values.length; i++) {
values[i] = byteData.getInt16(i * 2, endian);
}
return values;
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels