melSpectrogram function

Future<List<Float64List>> melSpectrogram(
  1. String filePath, {
  2. int sampleRate = 22050,
  3. int nFft = 2048,
  4. int? hopLength,
  5. int nMels = 128,
  6. double fMin = 0.0,
  7. double? fMax,
  8. double topDb = 80.0,
})

Loads a WAV file and computes its Mel-frequency spectrogram in decibels (dB).

This is a high-level convenience function that combines audio loading, Short-Time Fourier Transform (STFT), Mel filterbank application, and power-to-dB conversion.

Returns a 2D list (list of frames, where each frame is a list of Mel bins).

Matches the behavior of librosa.feature.melspectrogram followed by librosa.power_to_db.

Implementation

Future<List<Float64List>> melSpectrogram(
  String filePath, {
  int sampleRate = 22050,
  int nFft       = 2048,
  int? hopLength,
  int nMels      = 128,
  double fMin    = 0.0,
  double? fMax,
  double topDb   = 80.0,
}) async {
  final audio      = await loadAudio(filePath, sampleRate);
  final powerSpec  = calculateSpectrogram(
    audio.samples,
    fftSize: nFft,
    hopSize: hopLength,
  );
  final filterbank = createMelFilterbank(
    sampleRate: sampleRate,
    nFft: nFft,
    nMels: nMels,
    fMin: fMin,
    fMax: fMax,
  );
  final melSpec    = applyMelFilterbank(powerSpec, filterbank);
  return powerToDb(melSpec, topDb: topDb);
}