Recursive AI

Change audio playback pitch smoothly in C#

oneaudio is a free, open-source audio library for .NET from Recursive AI. MIT or Apache-2.0, no bundled native binaries.

A voice is one sound playing in the mixer. Start it with VariablePitch, then ramp SetPitch to change pitch and speed together.

Terminal
dotnet add package RecursiveAI.OneAudio

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

The snippet loads tone.wav from the zip, a generated two-second 440 Hz mono tone at 48 kHz.

C#
using OneAudio;
 
const int rate = 48000;
var mixer = new AudioMixer(new()
{
    SampleRate = rate, Channels = 1, MaxBlockFrames = 480
});
var clip = Audio.Load("tone.wav");
var voice = mixer.Play(new(clip) { VariablePitch = true });
if (!voice.IsValid)
    throw new InvalidOperationException("No voice.");
 
var output = new float[rate];
mixer.Render(output.AsSpan(0, rate / 4));
if (!mixer.SetPitch(voice, 1.5f, rampFrames: rate / 2))
    throw new InvalidOperationException("Command queue full.");
mixer.Render(output.AsSpan(rate / 4));
 
using (var wav = new WavEncoder(
    File.Create("glide.wav"), new(rate, 1)))
    wav.Write(output);
 
Console.WriteLine("Saved glide.wav");

Unzip the example, then dotnet run.

The snippet starts at the original pitch, then glides toward 1.5× over half a second and writes one second to glide.wav.

This example renders to a file so it runs without an audio device; to hear a mixer through your speakers, see Play an audio file.

Text
Saved glide.wav

The full program

The example counts upward zero crossings over two steady 200 ms windows, one before and one after the glide. If the command queue fills, SetPitch returns false; check it so a dropped change isn’t mistaken for a working ramp.

C#
using OneAudio;
 
const int rate = 48000;
var mixer = new AudioMixer(new()
{
    SampleRate = rate, Channels = 1, MaxBlockFrames = 480
});
var clip = Audio.Load("tone.wav");
var voice = mixer.Play(new(clip) { VariablePitch = true });
if (!voice.IsValid) throw new InvalidOperationException("No voice.");
 
// Start at 440 Hz, then glide to a multiplier of 1.5 over 0.5 seconds.
var output = new float[rate];
mixer.Render(output.AsSpan(0, rate / 4));
if (!mixer.SetPitch(voice, 1.5f, rampFrames: rate / 2))
    throw new InvalidOperationException("Command queue full.");
mixer.Render(output.AsSpan(rate / 4));
 
using (var wav = new WavEncoder(File.Create("glide.wav"), new(rate, 1)))
    wav.Write(output);
 
// Steady sections: count upward zero crossings over 0.20 s.
Console.WriteLine($"Before glide: {Frequency(0.025)} Hz");
Console.WriteLine($"After glide: {Frequency(0.775)} Hz");
Console.WriteLine("Saved glide.wav");
 
int Frequency(double start)
{
    int first = (int)(start * rate);
    int last = first + rate / 5;
    int cycles = 0;
    for (int i = first + 1; i <= last; i++)
        if (output[i - 1] <= 0 && output[i] > 0) cycles++;
    return cycles * 5;
}
Text
Before glide: 440 Hz
After glide: 660 Hz
Saved glide.wav

Variations

Glide down instead. Replace the SetPitch check in the full program:

C#
if (!mixer.SetPitch(voice, 0.75f, rampFrames: rate / 2))
    throw new InvalidOperationException("Command queue full.");
Text
Before glide: 440 Hz
After glide: 330 Hz
Saved glide.wav

The short answer

Set VariablePitch = true when you call Play, keep the returned voice handle, and call SetPitch on it while the voice is active. Use a positive rampFrames to glide to the target multiplier. At 48 kHz, 24,000 output frames is half a second. The example renders a quarter second at the original pitch before queueing the glide, so it changes a voice that is already playing.

How can I change the pitch and speed of audio?

SetPitch takes one multiplier for both. Above 1 the mixer reads through the source more quickly and the sound rises in pitch; below 1 it slows down and falls. For a tone, 1.5 moves 440 Hz to 660 Hz, as measured after the ramp. A multiplier of 2 doubles playback speed and raises pitch one octave, within the mixer’s converter ratio range (how many output frames it can produce per source frame).

A render pass is one batch of output frames that the mixer processes together. The mixer updates the glide at the start of each pass, not at every sample. The example caps each pass at 480 frames (10 ms at 48 kHz). With rampFrames: 0, the new pitch takes effect at the next pass without a glide. A same-rate voice started at pitch 1 without VariablePitch has no reserved converter and ignores later pitch changes. Reserve one up front even if you expect the first change much later. See resampling and pitch in the mixer reference.

Can you change the pitch of audio without slowing it down?

Not with this call. It changes how many source frames are consumed for each output frame, like speeding up or slowing down a recording. A pitch shifter that preserves duration is a different operation. Pitch-preserving time stretch is the opposite independent control: it changes duration without shifting the notes. If a dialogue take must still end at the same picture frame, or a song’s tempo must change without changing its key, use an algorithm designed for that job rather than the mixer’s playback multiplier.

All oneaudio recipes