RfftPlan constructor

RfftPlan(
  1. int n
)

Creates a reusable r2c FFT plan for transform length n.

Throws ArgumentError if n is not positive. Throws StateError if native plan creation fails.

Implementation

factory RfftPlan(int n) {
  if (n <= 0) throw ArgumentError.value(n, 'n', 'must be > 0');

  final plan = yl_fft_plan_r2c_create(n);
  if (plan == ffi.nullptr) {
    throw StateError('yl_fft_plan_r2c_create($n) returned nullptr');
  }

  final bins = yl_rfft_bins(n);

  ffi.Pointer<ffi.Float>? inputPtr, rePtr, imPtr, specPtr;
  try {
    inputPtr = calloc<ffi.Float>(n);
    rePtr = calloc<ffi.Float>(bins);
    imPtr = calloc<ffi.Float>(bins);
    specPtr = calloc<ffi.Float>(bins);

    return RfftPlan._(
      n,
      bins,
      _RfftPlanResource(plan, inputPtr, rePtr, imPtr, specPtr),
    );
  } catch (_) {
    if (inputPtr != null) calloc.free(inputPtr);
    if (rePtr != null) calloc.free(rePtr);
    if (imPtr != null) calloc.free(imPtr);
    if (specPtr != null) calloc.free(specPtr);
    yl_fft_plan_destroy(plan);
    rethrow;
  }
}