Change the output-to-input frame ratio without discarding audio history between blocks.
dotnet add package RecursiveAI.OneAudioRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneAudio; var resampler = AudioResampler.Create( 1, ResamplerQuality.Polyphase, 1.0); resampler.SetRatio(1.02); var buffer = new float[1024]; resampler.Process(new float[480], buffer, out int used, out int written); int total = written, tail; while ((tail = resampler.Flush(buffer)) > 0) total += tail; Console.WriteLine($"{used} in, {total} out");
Unzip the example, then dotnet run.
Prints:
480 in, 490 outThe full program
The snippet at the top converts 480 mono frames of silence at ratio 1.02 and flushes the tail. The full program generates a 440 Hz tone in four blocks, changes from ratio 1.0 to 1.02 halfway through, and measures adjacent output samples.
using OneAudio; var resampler = AudioResampler.Create(1, ResamplerQuality.Polyphase, 1.0, new ResamplerOptions { MinRatio = 0.9, MaxRatio = 1.1 }); var input = new float[480]; var output = new float[1024]; int consumed = 0, produced = 0; float previous = 0; double maxStep = 0; for (int block = 0; block < 4; block++) { for (int i = 0; i < input.Length; i++) input[i] = (float)(0.25 * Math.Sin(2 * Math.PI * 440 * (block * 480 + i) / 48000)); resampler.SetRatio(block < 2 ? 1.0 : 1.02); int offset = 0; while (offset < input.Length) { resampler.Process(input.AsSpan(offset), output, out int used, out int written); offset += used; consumed += used; Observe(written); } } int tail; while ((tail = resampler.Flush(output)) != 0) Observe(tail); Console.WriteLine($"Input frames: {consumed}"); Console.WriteLine($"Output frames: {produced}"); Console.WriteLine(FormattableString.Invariant($"Largest adjacent step: {maxStep:F4}")); void Observe(int count) { for (int i = 0; i < count; i++) { if (produced + i > 0) maxStep = Math.Max(maxStep, Math.Abs(output[i] - previous)); previous = output[i]; } produced += count; }
dotnet run prints:
Input frames: 1920 Output frames: 1941 Largest adjacent step: 0.0582
A non-finite, nonpositive or out-of-range ratio throws ArgumentOutOfRangeException.
Variations
Restrict the range at creation, then handle an invalid change:
using OneAudio; var resampler = AudioResampler.Create( 1, ResamplerQuality.Linear, 1.0, new ResamplerOptions { MinRatio = 0.9, MaxRatio = 1.1 }); try { resampler.SetRatio(1.2); } catch (ArgumentOutOfRangeException) { Console.WriteLine("Ratio is outside the configured range."); }
Ratio is outside the configured range.Related recipes
The short answer
Create an IResampler, feed interleaved float PCM through Process, and call SetRatio between blocks. Keep the same instance so the filter history and read position survive the change. After the final input, call Flush until it returns zero. The ratio means output rate divided by input rate, not a codec bitrate or a compression setting.
The example uses mono audio, so one float is one frame. For stereo, one frame contains two floats. This distinction matters when advancing through a partially consumed buffer: the returned counts are frames, not array indexes in samples.
How do I write a streaming variable-rate audio resampler in C#?
The full program above supplies the complete loop. Process reports input frames consumed and output frames written. Re-offer any unconsumed input on the next call; do not discard it just because the output buffer filled. Size buffers with MaxOutputFrames at the current ratio when you need an upper bound. For multiple channels, multiply frame counts by Channels when slicing interleaved arrays.
The example starts at ratio 1.0 and changes to 1.02 after two input blocks. It checks total consumption and measures the largest adjacent-sample step, including across block boundaries. That number describes this generated tone, not a general guarantee about audible clicks. The interface documentation explicitly warns that extreme jumps may click even though filter state remains continuous.
Filter delay is compensated: at a fixed ratio, output frame m represents input time m divided by the ratio. That does not mean every output frame is immediately available. The filter needs look-ahead, exposed through LatencyFrames, before it can calculate a sample. Flush emits the buffered tail as if silence followed the input, then resets the converter. Call it at the end of a signal, not after every input block.
What does a variable audio resampler ratio change?
SetRatio changes sample-rate conversion for subsequent calls. It is not a time-stretch API that promises to preserve pitch. The library describes it as suitable for bending pitch; playback behavior also depends on the rate at which the resulting frames are consumed.
Configure MinRatio and MaxRatio when creating the resampler. They both constrain later changes and size preallocated state. The documented defaults are 0.25 and 16. In polyphase modes, memory grows as the minimum ratio decreases, so choose a range that reflects the changes your application actually needs.
The pinned docs describe ratio changes as continuous in read position. They also advise changes of a few percent per call rather than per frame: a sufficiently large cutoff change below ratio 1 redesigns the polyphase coefficient bank. Process, SetRatio, Flush and Reset allocate nothing, but the instance is not thread-safe. Keep control changes and processing serialized on the same execution path. Use Reset when seeking to an unrelated signal; it discards history rather than preserving continuity.
Source: oneaudio resampling documentation.