Set LoopStartFrame and LoopEndFrame to play an intro once, then repeat just the section between the markers.
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, [0f, 1f, 0f, -1f, 1f]); var voice = mixer.Play(new PlayParams(clip) { Loop = true, LoopStartFrame = 1, LoopEndFrame = 4 }); if (!voice.IsValid) throw new InvalidOperationException(); var pcm = new float[10]; mixer.Render(pcm); Console.WriteLine(string.Join(", ", pcm));
The example builds its clip from five samples. For an existing WAV or MP3, pass its path to Audio.Load to decode it into an AudioClip.
The mono clip starts at frame 0, then repeats frames 1, 2, and 3. The final 1 in the input array, at frame 4, is outside the loop.
0, 1, 0, -1, 1, 0, -1, 1, 0, -1If Play returns an invalid handle, the start request was not queued.
To hear the loop on your speakers, see Play an audio file in C# for device output.
Unzip the example, then dotnet run.
Variations
To repeat the entire clip, replace the marker lines inside PlayParams (leave Loop = true):
LoopStartFrame = 0, LoopEndFrame = -1
0, 1, 0, -1, 1, 0, 1, 0, -1, 1Related recipes
The short answer
On a decoded AudioClip, set PlayParams.Loop = true, then supply LoopStartFrame and LoopEndFrame in the clip’s own frame numbers. The voice starts at frame zero and wraps to the start marker as soon as it reaches the end marker. The start is inclusive and the end is exclusive. To include the last frame of a clip, use the clip’s frame count as the end, or use -1.
How do you play the intro once and loop from a sample position?
Set the start marker after the intro. If a clip’s intro is 12,000 frames and the loop body is the next 36,000, use start 12000 and end 48000. Playback starts at zero, reaches frame 47999, then goes back to frame 12000; it doesn’t jump directly to the loop marker when the voice starts. Validate 0 <= start < end <= clip.Frames before accepting two markers from an editor. For example, the pinned clip reader uses zero when the supplied start is at or beyond the end, rather than rejecting it.
A frame contains one sample for each channel. At 48 kHz, one second is frame 48000 in either mono or stereo. That frame is at index 96000 in a stereo interleaved array, but the marker is still 48000. Use the rate of the clip handed to the mixer, not the mixer rate or an index into the interleaved sample array. If you resampled the clip before playing it, convert the markers to the new clip’s rate.
How do you make an audio loop seamless?
Listen to the loop boundary, not just the repeated section. A jump in the waveform at the seam can produce a click. Adjust the markers or edit the clip so the end joins the start smoothly. A frame-exact wrap ensures the mixer jumps at the chosen point; it does not blend the join.
This recipe loops an in-memory clip. If you stream a longer recording, set loop to true and pass loopStartFrame and loopEndFrame in decoder frames to the StreamingSource constructor. The PlayParams loop fields demonstrated here are for clips.