---
title: "Schedule audio playback at a sample frame in C#"
url: https://recursiveai.org/oneaudio/cookbook/schedule-audio-at-exact-sample-frame/
description: "Schedule a clip at an exact mixer frame in C# using OneAudio’s StartAtFrame. Render the result offline and see where the clip starts in the buffer."
version: "1.8.0"
---

# Schedule audio playback at a sample frame 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/schedule-audio-at-exact-sample-frame/schedule-audio-at-exact-sample-frame.zip)

Set `StartAtFrame` on the mixer’s clock instead of waiting on the control thread. This example renders to memory; to hear the mix on speakers, see [Play an audio file in C#](https://recursiveai.org/oneaudio/cookbook/play-audio-file/).

```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(
    new MixerOptions { Channels = 1 });
var format = new AudioFormat(48000, 1);
var clip = new AudioClip(format, [1f, -1f]);
var voice = mixer.Play(new PlayParams(clip)
{
    StartAtFrame = 2
});
if (!voice.IsValid)
    throw new InvalidOperationException();

var pcm = new float[5];
mixer.Render(pcm);
Console.WriteLine(string.Join(", ", pcm));
```

The first two frames are silent. The clip starts at frame index `2`, plays two frames, and leaves the final frame silent.

```text
0, 0, 1, -1, 0
```

Queue a future start before rendering reaches it. If the requested frame is already past, the voice starts at the next render pass instead.

Unzip [the example](https://recursiveai.org/oneaudio/cookbook/schedule-audio-at-exact-sample-frame/schedule-audio-at-exact-sample-frame.zip), then `dotnet run`.

## Variations

To schedule relative to the current mixer position, replace the `var voice = mixer.Play(...)` statement with this version. It advances the offline mixer by four frames first, so the absolute start becomes frame `6`:

```csharp
mixer.Render(new float[4]);
var voice = mixer.Play(new PlayParams(clip)
{
    StartAtFrame = mixer.TimelineFrames + 2
});
```

```text
0, 0, 1, -1, 0
```

The samples are the same because the clip still starts two frames into the printed buffer, which now covers absolute frames `4` through `8`.

## 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

Set `PlayParams.StartAtFrame` to the desired absolute frame on the `AudioMixer` timeline before calling `Play`. `TimelineFrames` tells you how many frames the mixer has already rendered. For a start one second from that position at 48 kHz, add 48,000. The offset is in mixer frames, not clip frames or individual samples in a stereo buffer.

## How do you schedule sample-accurate audio playback?

Queue the start ahead of time and keep rendering. The mixer can begin the voice inside a render pass; it doesn’t have to wait for the next block boundary. The example renders five frames at once yet starts at the third frame.

For events spaced at fixed intervals, pick a future origin on the mixer timeline and add each event’s frame offset to that origin. Don’t sleep until each event is due and call `Play` without a start frame: then the start depends on when the control thread wakes and when the render side reads the queue. On a running mixer the audio thread can advance while you prepare a command, so give it enough lead time. A valid handle tells you the request was queued, not that it reached the mixer before the requested frame.

## How do you calculate audio frames from time?

Multiply the offset in seconds by `mixer.SampleRate` and round to the nearest whole frame when you need the nearest representable start. At 48 kHz, 250 milliseconds is 12,000 frames. Stereo still has 12,000 frames in that interval; it just has two samples per frame. The input clip’s sample rate doesn’t change the target position on the mixer clock.

This is timing within the rendered mix, not a guarantee about when sound comes out of a speaker. The example renders straight to memory with no limiter. A mixer configured with a lookahead limiter delays its output by `LatencyFrames`; if you send the mix to a device, rendered data can also be queued before it plays. See the [mixer and device reference](https://recursiveai.org/oneaudio/docs/api/) for separate render and playback counters.
