---
title: "Loop audio between two sample positions in C#"
url: https://recursiveai.org/oneaudio/cookbook/loop-audio-between-sample-markers/
description: "Loop part of an audio clip in C# with OneAudio. Choose an inclusive start frame and an exclusive end frame, and render the intro followed by the loop."
version: "1.8.0"
---

# Loop audio between two sample positions 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/loop-audio-between-sample-markers/loop-audio-between-sample-markers.zip)

Set `LoopStartFrame` and `LoopEndFrame` to play an intro once, then repeat just the section between the markers.

```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,
    [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.

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

If `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#](https://recursiveai.org/oneaudio/cookbook/play-audio-file/) for device output.

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

## Variations

To repeat the entire clip, replace the marker lines inside `PlayParams` (leave `Loop = true`):

```csharp
    LoopStartFrame = 0,
    LoopEndFrame = -1
```

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

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

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](https://github.com/recursive-ai-oss/oneaudio-csharp/blob/b7bedf09e6f32687a7cb58f4452ef85e88f3b4c9/src/OneAudio/AudioSources.cs#L89-L103) 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](https://www.soundonsound.com/techniques/making-seamless-sample-loops-your-hard-disk-recorder). 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](https://github.com/recursive-ai-oss/oneaudio-csharp/blob/b7bedf09e6f32687a7cb58f4452ef85e88f3b4c9/src/OneAudio/AudioSources.cs#L145-L152). The `PlayParams` loop fields demonstrated here are for clips.
