Set StartOffsetFrame to skip the start of an in-memory audio clip. Multiply seconds by the clip’s sample rate: 0.5 s at 48 kHz is frame 24,000.
dotnet add package RecursiveAI.OneAudioRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneAudio; var format = new AudioFormat(48000, 1); var mixer = new AudioMixer(new() { SampleRate = 48000, Channels = 1 }); var clip = new AudioClip(format, [0.125f, 0.25f, 0.5f, 0.75f]); var voice = mixer.Play(new(clip) { StartOffsetFrame = 2 }); if (!voice.IsValid) throw new InvalidOperationException( "No voice."); var output = new float[4]; mixer.Render(output); foreach (float sample in output) { string text = FormattableString .Invariant($"{sample:F2}"); Console.WriteLine(text); }
A negative offset throws ArgumentOutOfRangeException. A streaming source accepts offset 0, but Play throws ArgumentException for a positive offset on that source.
Unzip the example, then dotnet run.
The clip contains four generated mono frames. The mixer starts at frame 2, plays the last two frames, and fills the rest of the output block with silence. No file or audio device is needed.
0.50 0.75 0.00 0.00
Variations
Start exactly at the loop end. Replace StartOffsetFrame = 2 inside the Play initializer with these settings. The loop covers frames 1 and 2; it doesn’t include frame 3.
Loop = true, LoopStartFrame = 1, LoopEndFrame = 3, StartOffsetFrame = 3
0.25 0.50 0.25 0.50
Start one frame later on the mixer clock. Add StartAtFrame in the same initializer. On this fresh mixer, the first output frame is silent; the clip still starts at frame 2.
StartOffsetFrame = 2, StartAtFrame = 1
0.00 0.50 0.75 0.00
Related recipes
The short answer
Set PlayParams.StartOffsetFrame when you call AudioMixer.Play with an AudioClip. This is a zero-based position in the source clip, so 2 begins on the third frame. It sets the position for the new voice without cutting samples out of the clip. You can play the same clip from different positions on separate calls.
How do you start audio at a specific time in C#?
Multiply the starting time in seconds by the clip’s sample rate, then choose a whole frame. At 48,000 Hz, a start time of half a second is frame 24,000. A frame contains one sample per channel at that position, so the offset is 24,000 for a stereo clip too, not 48,000. Use the source rate even if the mixer runs at a different rate or you set Pitch to change playback speed.
Inside the Play initializer, for an offset of half the clip’s sample rate in frames:
StartOffsetFrame = format.SampleRate / 2With the four-frame clip above, that skips the whole clip and prints four zeros. With a clip longer than half a second, it starts at the half-second point (rounded down to a whole frame). To delay when the sound starts instead, set StartAtFrame, which uses the mixer’s output clock. You can set both to schedule a cue that starts partway through the source.
What happens if the start position is at the end of the audio?
A one-shot at or beyond the clip’s frame count plays nothing. A loop with a nonempty body wraps at or past its exclusive end. For the recipe’s loop [1, 3), offset 3 wraps to frame 1 and offset 4 wraps to frame 2. An offset before the loop start plays the intro before repeating the loop body; it isn’t moved forward to the loop start.
For streaming audio, leave StartOffsetFrame at 0. If the decoder supports seeking, seek it before creating the streaming source. The mixer’s clip offset isn’t a general seek control for a stream or for a voice that’s already playing. See the OneAudio API reference for the clip and streaming source APIs.