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#.
dotnet add package RecursiveAI.OneAudioRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
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.
0, 0, 1, -1, 0Queue 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, 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:
mixer.Render(new float[4]); var voice = mixer.Play(new PlayParams(clip) { StartAtFrame = mixer.TimelineFrames + 2 });
0, 0, 1, -1, 0The 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
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 for separate render and playback counters.