renderInterleavedInt16 method

void renderInterleavedInt16(
  1. ArrayInt16 destination, {
  2. int offset = 0,
  3. int? length,
})
Renders the waveform as a stereo interleaved signal with 16-bit quantization. The audio renderer. The destination buffer.

Implementation

void renderInterleavedInt16(ArrayInt16 destination, {int offset = 0, int? length})
{
    if (destination.bytes.lengthInBytes % 4 != 0)
    {
        throw "Invalid destination length";
    }

    int sampleCount = 0;

    if (length != null) {
      sampleCount = length;
    } else {
      sampleCount = destination.bytes.lengthInBytes ~/ 4;
      sampleCount -= offset;
    }

    List<double> left = List<double>.filled(sampleCount, 0);
    List<double> right = List<double>.filled(sampleCount, 0);

    render(left, right);

    for (var t = 0; t < sampleCount; t++)
    {
        int sampleLeft = (32768 * left[t]).toInt();
        var sampleRight = (32768 * right[t]).toInt();

        // these get automaticall casted to shorts in ArrayInt16[]
        destination[offset + t * 2 + 0] = sampleLeft;
        destination[offset + t * 2 + 1] = sampleRight;
    }
}