# pcmToWaveBuffer method

Future<Uint8List> pcmToWaveBuffer({

1. required Uint8List inputBuffer,
2. int numChannels = 1,
3. int sampleRate = 16000,
4. Codec codec = fs_codec.Codec.pcm16,

})

Convert a raw PCM buffer to a WAVE buffer.

Adds a WAVE header in front of the PCM data It adds a `Wave` envelop in front of the PCM buffer, so that the file can be played back with `startPlayerFromBuffer()`.

Note: the parameters `numChannels` and `sampleRate` are mandatory, and must match the actual PCM data. See here a discussion about `Raw PCM` and `WAVE` file format.

## Parameters

- inputBuffer: is your input buffer
- numChannels: is the number of channels of your file
- sampleRate is the sample rate of your data
- codec is either fs_codec.Codec.pcm16 or fs_codec.Codec.pcmFloat32

## Return

The Uint8List containing your wave buffer

## Example

    FlutterSoundHelper().pcmToWave (
        inputFile: 'foo.pcm',
        outputFile: 'foo.wav',
        numChannels: 2 // stereo
        samplerate: 48000,
        codec: fs_codec.Codec.pcm16
     );

## Implementation

    Future<Uint8List> pcmToWaveBuffer({
      required Uint8List inputBuffer,
      int numChannels = 1,
      int sampleRate = 16000,
      fs_codec.Codec codec = fs_codec.Codec.pcm16,
      //int bitsPerSample,
    }) async {
      if (codec != fs_codec.Codec.pcm16 && codec != fs_codec.Codec.pcmFloat32) {
        throw (Exception('Bad codec'));
      }
      var size = inputBuffer.length;
      var header = WaveHeader(
        codec == fs_codec.Codec.pcm16
            ? WaveHeader.formatInt
            : WaveHeader.formatFloat,
        numChannels,
        sampleRate,
        codec == fs_codec.Codec.pcm16
            ? 16
            : 32, // 16 bits per sample for int16. 32 bits per sample for float32
        size, // total number of bytes
      );

      var buffer = <int>[];
      StreamController controller = StreamController<List<int>>();
      var sink = controller.sink as StreamSink<List<int>>;
      var stream = controller.stream as Stream<List<int>>;
      stream.listen((e) {
        var x = e.toList();
        buffer.addAll(x);
      });
      header.write(sink);
      sink.add(inputBuffer);
      await sink.close();
      await controller.close();
      return Uint8List.fromList(buffer);
    }