getPitchFromAudioData method

Future<double> getPitchFromAudioData(
  1. Uint8List audioData
)

Implementation

Future<double> getPitchFromAudioData(Uint8List audioData) async {
  _accumulatedData.addAll(audioData);

  const int bytesPerSample = 2;
  double pitch = 0;
  int neededBytes = NeomGeneratorConstants.neededSamples * bytesPerSample;

  while (_accumulatedData.length >= neededBytes) {
    // Extraemos los primeros neededBytes
    final chunk = _accumulatedData.sublist(0, neededBytes);
    // Los removemos del acumulado para postearior analisis del buffer
    _accumulatedData.removeRange(0, neededBytes);

    final pitchDetectorDart = PitchDetector(
      audioSampleRate: NeomGeneratorConstants.sampleRate.toDouble(),
      bufferSize: NeomGeneratorConstants.neededSamples,
    );

    try {
      final chunkAsUint8List = Uint8List.fromList(chunk);

      PitchDetectorResult pitchResult = await pitchDetectorDart.getPitchFromIntBuffer(chunkAsUint8List);
      pitch = pitchResult.pitch.roundToDouble();
    } catch (e, st) {
      NeomErrorLogger.recordError(e, st, module: 'neom_sound', operation: 'getPitchFromAudioData');
    }
  }

  return pitch;
}