playBuffer method

void playBuffer(
  1. Uint8List audioData
)

Implementation

void playBuffer(Uint8List audioData) async {
  // TODO: use nicer way of reading buff_size chunks out of audioData
  final bufferCount = (audioData.length ~/ buff_size);
  for (var j = 0; j <= bufferCount; j++) {
    for (var i = 0; i < buff_size; i++) {
      final index = i + (j * buff_size);
      if (index > audioData.length - 1) {
        break;
      }
      final b = audioData[index];
      audioFramesBuffer[i] = b;
    }

    var frames = -EAGAIN;
    while (frames == -EAGAIN) {
      frames = alsa.snd_pcm_writei(pcmHandlePtr.value,
          audioFramesBuffer.cast<Void>(), framesPtr.value);

      await Future.delayed(Duration(microseconds: 0));
    }

    if (frames == -EPIPE) {
      _printDebug('XRUN.'); // should client get callback for this?
      alsa.snd_pcm_prepare(pcmHandlePtr.value);
    }
    if (frames > 0 && frames != buff_size / 4) {
      _printDebug('Short write, expected: ${buff_size / 4}, wrote: $frames');
    }
    if (frames < 0) {
      throw Exception(
          "ERROR [$frames]. Can't write to PCM device. ${alsa.snd_strerror(frames).cast<Utf8>().toDartString()}");
    }
  }
}