Save audio stream / Uint8List data in file flutter

6,366

I have added a WAVE/RIFF Header before writing my bytes into the file which gives the metadata to bytes and file.

I have 16 BIT PCM audio bytes with MONO channel in my stream. I am appending those bytes in a list of bytes when I am receiving the bytes.

List<int> recordedData = [];
recordedData.addAll(value);

Now I have all the recorded bytes on my list. After stopping my recorder I called the below function. Which takes all the data and sample rate for recorded. in my case its 44100.

await save(recordedData, 44100);

Future<void> save(List<int> data, int sampleRate) async {
    File recordedFile = File("/storage/emulated/0/recordedFile.wav");

    var channels = 1;

    int byteRate = ((16 * sampleRate * channels) / 8).round();

    var size = data.length;

    var fileSize = size + 36;

    Uint8List header = Uint8List.fromList([
      // "RIFF"
      82, 73, 70, 70,
      fileSize & 0xff,
      (fileSize >> 8) & 0xff,
      (fileSize >> 16) & 0xff,
      (fileSize >> 24) & 0xff,
      // WAVE
      87, 65, 86, 69,
      // fmt
      102, 109, 116, 32,
      // fmt chunk size 16
      16, 0, 0, 0,
      // Type of format
      1, 0,
      // One channel
      channels, 0,
      // Sample rate
      sampleRate & 0xff,
      (sampleRate >> 8) & 0xff,
      (sampleRate >> 16) & 0xff,
      (sampleRate >> 24) & 0xff,
      // Byte rate
      byteRate & 0xff,
      (byteRate >> 8) & 0xff,
      (byteRate >> 16) & 0xff,
      (byteRate >> 24) & 0xff,
      // Uhm
      ((16 * channels) / 8).round(), 0,
      // bitsize
      16, 0,
      // "data"
      100, 97, 116, 97,
      size & 0xff,
      (size >> 8) & 0xff,
      (size >> 16) & 0xff,
      (size >> 24) & 0xff,
      ...data
    ]);
    return recordedFile.writeAsBytesSync(header, flush: true);
  }

And it's creating the playable WAV file.

Share:
6,366
Dhalloo
Author by

Dhalloo

Simply Coding :D

Updated on December 19, 2022

Comments

  • Dhalloo
    Dhalloo over 1 year

    I am getting Uint8List from recorder plugin for Android and iOS both. I want to write the data in a local playable audio file whenever I am getting the data in my stream subscription of a mic. Is there any possible way to write the data?

    Currently, I am storing the data like

    recordedFile.writeAsBytesSync(recordedData, flush: true);
    

    It's writing data to file but not able to play from the file storage. But also if I read the same file and gives it's bytes to plugin it's playing the same buffer.

    • FilipeOS
      FilipeOS almost 4 years
      Did you managed to do this?
    • Dhalloo
      Dhalloo almost 4 years
      @FilipeOS Yes. I have to add some header and it's allowed me to write a playable audio file
    • Uttam Panchasara
      Uttam Panchasara over 3 years
      @Dhalloo May I know which plugin are using, which is returning bytes?
    • Dhalloo
      Dhalloo over 3 years
      @UttamPanchasara I have used flutter sound plugin to fetch the bytes and play from the bytes.
    • who-aditya-nawandar
      who-aditya-nawandar over 2 years
      @Dhalloo Can you share the code please; how is flutter_sound returning bytes?
    • Dhalloo
      Dhalloo over 2 years
      @who-aditya-nawandar plugin is self-descriptive now. If you look into example
  • FilipeOS
    FilipeOS almost 4 years
    When I do await save(_micChunks, 44100); I get The argument type List<Uint8List> can't be assigned to List<int>. I'm using plugin sound_stream
  • Dhalloo
    Dhalloo almost 4 years
    Uint8List is extending to List<int> only. So in your case, as you have List<Uint8List> try with List<List<int>>
  • FilipeOS
    FilipeOS almost 4 years
    If I change List<int> recordedData = []; to List<List<int>> data = _micChunks; the last piece of code on header ...data say a List<int> cannot be assigned to int
  • Dhalloo
    Dhalloo almost 4 years
    can you share any link of the package or plugin that you used so I can guide you accordingly
  • FilipeOS
    FilipeOS almost 4 years
  • Dhalloo
    Dhalloo almost 4 years
    I am also using the same.
  • FilipeOS
    FilipeOS almost 4 years
    And can you help me? It's saving file but not playing at all.. Gives error github.com/CasperPas/flutter-sound-stream/issues/…
  • Dhalloo
    Dhalloo almost 4 years
    @FilipeOS how can I help you in this issue?
  • Uttam Panchasara
    Uttam Panchasara over 3 years
    @Dhalloo I have same scenario as your only difference is I'm continually receiving audio bytes (real-time), I want to convert that raw bytes to text, any idea? Rate = 44100, channel = Mono
  • Ali Esfandiari
    Ali Esfandiari about 3 years
    I recorded my audio by flutter webrtc, but after adding header I only receive noise.