STFT constructor

STFT({
  1. required int nFft,
  2. int? winLen,
  3. required int hop,
  4. StftWindow window = StftWindow.hann,
  5. bool meanSubtract = false,
  6. bool center = false,
})

Creates a reusable native STFT context.

The instance can be used in streaming mode via push and flush, or in whole-buffer mode via process. The same context also exposes scalar spectrum variants through pushSpec, flushSpec, and processSpec.

Implementation

factory STFT({
  required int nFft,
  int? winLen,
  required int hop,
  StftWindow window = StftWindow.hann,
  bool meanSubtract = false,
  bool center = false,
}) {
  final actualWinLen = winLen ?? nFft;
  if (nFft <= 0) {
    throw ArgumentError.value(nFft, 'nFft', 'must be > 0');
  }
  if (actualWinLen <= 0 || actualWinLen > nFft) {
    throw ArgumentError.value(
      actualWinLen,
      'winLen',
      'must be > 0 and <= nFft',
    );
  }
  if (hop <= 0) {
    throw ArgumentError.value(hop, 'hop', 'must be > 0');
  }

  final cfg = calloc<yl_stft_cfg>();
  try {
    cfg.ref
      ..n_fft = nFft
      ..win_len = actualWinLen
      ..hop = hop
      ..win = window.value
      ..mean_subtract = meanSubtract ? 1 : 0
      ..center = center ? 1 : 0;

    final handle = yl_stft_create(cfg);
    if (handle == ffi.nullptr) {
      throw StateError(
        'yl_stft_create(nFft: $nFft, winLen: $actualWinLen, hop: $hop) returned nullptr',
      );
    }

    return STFT._(
      nFft: yl_stft_nfft(handle),
      winLen: yl_stft_win_len(handle),
      hop: yl_stft_hop(handle),
      bins: yl_stft_bins(handle),
      window: window,
      meanSubtract: meanSubtract,
      center: center,
      handle: handle,
    );
  } finally {
    calloc.free(cfg);
  }
}