extrectWaveform function

Future<List<double>> extrectWaveform(
  1. File audioFile
)

Extracts the waveform from an audio file and normalizes the amplitude values.

audioFile - The audio file from which the waveform is to be extracted. Returns a list of normalized waveform values (amplitude) between 0.0 and 1.0.

Implementation

Future<List<double>> extrectWaveform(File audioFile) async {
  try {
    // Define an output file for the waveform data.
    final outputFile = File('${audioFile.path}_waveform.wave');

    // Extract the waveform data from the audio file asynchronously.
    final waveformStream = JustWaveform.extract(
      audioInFile: audioFile,
      waveOutFile: outputFile,
    );

    // Wait for the waveform extraction to complete and get the waveform data.
    final waveform = await waveformStream.last;

    // Normalize the waveform amplitude between 0.0 and 1.0 and return the data.
    return waveform.waveform!.data.map((e) => e.toDouble() / 32768.0).toList();
  } catch (e) {
    // Log any errors that occur during the extraction process.
    log(e.toString());
    // Return an empty list if there is an error.
    return [];
  }
}