0

For now, I have two txt files in my assets. One contains data in UInt8 format and another in UInt16 format. Both of them are of the same wav file. I need to create a wav file from the UInt16 file and UInt8 file is just for references.

Wav file from the Uint8 file

I am using the code below to create a wav file. It works. But this is not what I need.

var bytes = await rootBundle.loadString('assets/wav/audio_01_Uint8.txt');
List<int> bytesListString = await (bytes.split(', '));
for (String s in bytesListString) {
  bytesListInt.add(int.parse(s));
}
Uint8List bytesUInt8 = Uint8List.fromList(bytesListInt);
await File(tempPath + '/audio_01_Uint8.wav')
      .writeAsBytes(bytesUInt8);

Wav file from the Uint16 file

I am using the same process as for the UInt16 file but this doesn't create a playable wave file. It says that the file is empty.

var bytes = await rootBundle.loadString('assets/wav/audio_01_Uint16.txt');
List<int> bytesListString = await (bytes.split(', '));
for (String s in bytesListString) {
  bytesListInt.add(int.parse(s));
}
Uint16List bytesUInt16 = Uint16List.fromList(bytesListInt);
await File(tempPath + '/audio_01_Uint16.wav')
      .writeAsBytes(bytesUInt16);

Later, I will receive the data in UInt16 so I cannot use UInt8 to create a file. So, how can I create a wav file from the UInt16 data.

  • What are you saving as the WAV file header? – Abion47 Jul 18 '21 at 08:17
  • Also, have you verified what the output of the second file is as a `WAV` file? – Abion47 Jul 18 '21 at 08:19
  • The txt file contains the header as well. The Uint8 has first 44 data as header but Uint16 has the first only 22 data as header. Wav file is created but it says that the file is empty while playing the file. – Shree K Adhikari Jul 18 '21 at 17:22
  • "The WAV file is created" Yes, but have you opened it up with a hex editor or something and verified it is identical to the UInt8 version? – Abion47 Jul 18 '21 at 17:48
  • I read the file by putting it in the projects assets folder. I can read the file as Uint8 but the values are completely different from the original Uint8 txt file also, the length is half from the original one. And, the header is totally wrong. – Shree K Adhikari Jul 19 '21 at 12:59
  • Then the problem is your use of `writeAsByes`. It is truncating every UInt16 and only using the lower 8 bits. To save a `UInt16List`, you need to first convert it into a `UInt8List`. – Abion47 Jul 19 '21 at 16:14

0 Answers0