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.
https://github.com/llfbandit/record/blob/fa10b0f9281daa26f170c11e259b8b96009feae9/record/lib/src/record.dart#L267C1-L278C4
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.view with a non-zero byte offset
- Call
convertBytesToInt16 with 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;
}
Package version
record: ^6.1.1
Environment
Describe the bug
The helper method
convertBytesToInt16returns incorrect PCM16 sample values when the providedUint8Listis a view with a non-zerooffsetInBytes.https://github.com/llfbandit/record/blob/fa10b0f9281daa26f170c11e259b8b96009feae9/record/lib/src/record.dart#L267C1-L278C4
This happens because
ByteData.view(bytes.buffer)ignoresbytes.offsetInBytesand 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
Uint8List.viewwith a non-zero byte offsetconvertBytesToInt16with that viewMinimal reproducible example:
Improvement Suggestion: