savePowerSpectrogramPng function

Future<void> savePowerSpectrogramPng(
  1. String wavPath,
  2. String pngPath, {
  3. int sampleRate = 22050,
  4. int nFft = 2048,
  5. int? hopLength,
  6. double topDb = 80.0,
  7. String title = 'Power Spectrogram',
  8. int plotWidth = 1000,
  9. int plotHeight = 400,
  10. bool useNearest = true,
})

Generates a Power spectrogram (linear frequency) from a WAV file and saves it as a PNG.

wavPath: Input audio file. pngPath: Output image file.

Implementation

Future<void> savePowerSpectrogramPng(
  String wavPath,
  String pngPath, {
  int sampleRate = 22050,
  int nFft       = 2048,
  int? hopLength,
  double topDb   = 80.0,
  String title   = 'Power Spectrogram',
  int plotWidth  = 1000,
  int plotHeight = 400,
  bool useNearest = true,
}) async {
  final hop = hopLength ?? nFft ~/ 4;

  final audio = await loadAudio(wavPath, sampleRate);

  // STFT returns [Frames][FrequencyBins]
  final power = calculateSpectrogram(audio.samples, fftSize: nFft, hopSize: hop);

  final powerDb = powerToDb(power, topDb: topDb);

  // Transpose to [FrequencyBins][Frames] for the visualization logic
  final transposed = _transpose(powerDb);

  await saveSpectrogramImage(
    transposed, pngPath,
    sampleRate: sampleRate,
    hopLength: hop,
    fMin: 0.0,
    fMax: sampleRate / 2.0,
    topDb: topDb,
    title: title,
    plotWidth: plotWidth,
    plotHeight: plotHeight,
    useNearest: useNearest,
    isMel: false,
  );
}