How to get byte data from audio file (must be as signed int / bytes) in Flutter/Dart like AudioInputStream gives you in java How to get byte data from audio file (must be as signed int / bytes) in Flutter/Dart like AudioInputStream gives you in java dart dart

How to get byte data from audio file (must be as signed int / bytes) in Flutter/Dart like AudioInputStream gives you in java


Let's say your WAV header is 74 bytes long. (It will vary according to the number of sections, so really you need to parse it to determine that. But for any one source of WAV files it will often be the same number - use a hex dump to determine the offset of the data block plus 4.)

(By parsing the header you can find out other things like the sample rate and whether it's mono or stereo, etc.)

Then, if bytes is the Uint8List, you need bytes.buffer.asInt16List(74). This means: interpret the buffer backing the bytes as signed shorts, but starting at offset 74 - after the header.

  var dataOffset = 74; // parse the WAV header or determine from a hex dump  var bytes = await file.readAsBytes();  var shorts = bytes.buffer.asInt16List(dataOffset);  print(shorts[0]); // the first sample of audio  print(shorts.length); // the number of audio samples