process static method

Float32List process(
  1. Float32List input, {
  2. required int origFreq,
  3. required int newFreq,
  4. int lowpassFilterWidth = 6,
  5. double rolloff = 0.99,
  6. ResamplingMethod method = ResamplingMethod.sincHann,
  7. double? beta,
})

Resamples a single block of audio from origFreq to newFreq.

This is a convenience one-shot API:

  • creates a native resampling context,
  • processes input,
  • returns a newly allocated output buffer,
  • destroys the native context before returning.

lowpassFilterWidth controls the interpolation filter width. Higher values can improve quality at the cost of more computation.

rolloff sets the low-pass cutoff as a fraction of the Nyquist rate.

method selects the windowing/resampling kernel. If method is ResamplingMethod.sincKaiser, beta controls the Kaiser window shape.

Throws a StateError if the native resampling context cannot be created.

Implementation

static Float32List process(
  Float32List input, {
  required int origFreq,
  required int newFreq,
  int lowpassFilterWidth = 6,
  double rolloff = 0.99,
  ResamplingMethod method = ResamplingMethod.sincHann,
  double? beta,
}) {
  final L = input.length;
  final ctx = yl_resample_ctx_create(
    origFreq,
    newFreq,
    lowpassFilterWidth,
    rolloff,
    method.value,
    beta ?? 0,
  );
  if (ctx == ffi.nullptr) {
    throw StateError("Failed to create resampling context");
  }

  ffi.Pointer<ffi.Float> inPtr = ffi.nullptr;
  ffi.Pointer<ffi.Float> outPtr = ffi.nullptr;
  try {
    final outLen = yl_resample_out_len_ffi(origFreq, newFreq, L);
    inPtr = calloc<ffi.Float>(L);
    outPtr = calloc<ffi.Float>(outLen);
    inPtr.asTypedList(L).setAll(0, input);
    // double errorSqrt = yl_lpc_ctx_process_frame(ctx, inPtr, L, 0, outPtr);
    yl_resample_ctx_process(ctx, inPtr, L, outPtr);

    final out = Float32List(outLen);
    out.setAll(0, outPtr.asTypedList(outLen));
    return out;
  } finally {
    if (inPtr != ffi.nullptr) calloc.free(inPtr);
    if (outPtr != ffi.nullptr) calloc.free(outPtr);
    yl_resample_ctx_destroy(ctx);
  }
}