---
title: "Pan audio left and right in C#"
url: https://recursiveai.org/oneaudio/cookbook/pan-audio-between-speakers/
description: "Use OneAudio to pan a mono clip between the left and right speakers in C#. Set the starting pan, move the same voice, and inspect the stereo output."
version: "1.8.0"
---

# Pan audio left and right in C#

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

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneAudio) · [GitHub](https://github.com/recursive-ai-oss/oneaudio-csharp) · [Docs](https://recursiveai.org/oneaudio/docs/) · [Download example (.zip)](https://recursiveai.org/oneaudio/cookbook/pan-audio-between-speakers/pan-audio-between-speakers.zip)

Set `Pan` to `-1` for left or `1` for right. Change it with `SetPan` while the clip plays.

```shell
dotnet add package RecursiveAI.OneAudio
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using OneAudio;

var mixer = new AudioMixer();
var mono = new AudioFormat(48000, 1);
var clip = new AudioClip(mono, [1f, 1f]);
var voice = mixer.Play(
    new PlayParams(clip) { Pan = -1f });
if (!voice.IsValid)
    throw new InvalidOperationException();

var stereo = new float[2];
mixer.Render(stereo);
PrintFrame();
if (!mixer.SetPan(voice, 1f, 0))
    throw new InvalidOperationException();
mixer.Render(stereo);
PrintFrame();

void PrintFrame() => Console.WriteLine(
    FormattableString.Invariant(
        $"L={stereo[0]:F3} R={stereo[1]:F3}"));
```

The output shows the first frame on the left and the next frame on the right. Each render fills one stereo frame in left, right order.

```text
L=1.000 R=0.000
L=0.000 R=1.000
```

An invalid voice handle means `Play` could not queue the clip; `SetPan` returns `false` if its command cannot be queued.

To use your own recording or hear the mix on your speakers, see [Play an audio file in C#](https://recursiveai.org/oneaudio/cookbook/play-audio-file/).

Unzip [the example](https://recursiveai.org/oneaudio/cookbook/pan-audio-between-speakers/pan-audio-between-speakers.zip), then `dotnet run`.

## Variations

Start in the center instead. Replace the `new PlayParams` line inside `mixer.Play` with this line:

```csharp
    new PlayParams(clip) { Pan = 0f });
```

```text
L=0.707 R=0.707
L=0.000 R=1.000
```

**Sweep from left to right.** At 48 kHz, a 4,800-frame ramp moves the voice from left to right in a tenth of a second.

Replace the `var clip` line with a clip long enough for the first frame and the whole sweep:

```csharp
var samples = new float[4801];
Array.Fill(samples, 1f);
var clip = new AudioClip(mono, samples);
```

Replace the `SetPan` call and second render with this code to print the midpoint and end of the sweep:

```csharp
if (!mixer.SetPan(voice, 1f, 4800))
    throw new InvalidOperationException();
var sweep = new float[4800 * 2];
mixer.Render(sweep);
sweep.AsSpan(2399 * 2, 2).CopyTo(stereo);
PrintFrame();
sweep.AsSpan(^2).CopyTo(stereo);
PrintFrame();
```

```text
L=1.000 R=0.000
L=0.500 R=0.500
L=-0.000 R=1.000
```

## Related recipes

- [Mix audio in C#](https://recursiveai.org/oneaudio/cookbook/mix-audio/)
- [Play an audio file in C#](https://recursiveai.org/oneaudio/cookbook/play-audio-file/)

## The short answer

Route a mono `AudioClip` through a stereo `AudioMixer` with `PlayParams.Pan` set to `-1f` for left, `0f` for center, or `1f` for right. To move the same sound while it plays, keep the `VoiceHandle` and call `SetPan` on it. You don’t have to decode the clip again or restart it.

## How do you make audio move from left to right?

The example calls `SetPan` with a third argument of `0`, which means zero ramp frames. That makes the jump obvious: one rendered frame is on the left, and the next is on the right. For an audible sweep in a game or an app, give the pan change a nonzero ramp (in mixer frames), or omit that argument to use `MixerOptions.SmoothingMilliseconds`. At 48,000 frames per second, 4,800 frames is a tenth of a second. Use a clip long enough to cover the ramp; the two-frame clip here is only for inspecting the channel values.

Call `SetPan` on the control side, not in the audio render callback. The mixer reads queued commands at the start of each render pass. Keep the handle for the playing voice, not just a reference to the clip: several voices can play the same clip and each can have its own pan. Once the voice is gone, the handle is stale and the mixer ignores changes to it.

## Is panning only left and right?

Here the pan control acts on the front left and right pair, with positions in between the two extremes. The [channel mapping docs](https://recursiveai.org/oneaudio/docs/api/) describe constant-power panning for mono: a full-scale mono sample at center becomes about `0.707` in each output channel. Squaring those two values and adding them gives approximately the same total as one channel at `1.0`; this describes the signal, not the listener’s perceived loudness in every room.

For a stereo source, `Pan` is a balance control. Hard right turns down the left channel; it does not copy the recording’s left channel to the right. Use the original mono sound if you want the whole sound to move as one. The mixer’s channel mapping is not 3D positioning: the pan value does not specify distance or an angle behind the listener.
