Recursive AI

Seek to an exact MP3 sample in C#

oneaudio is a free, open-source audio library for .NET from Recursive AI. MIT or Apache-2.0, no bundled native binaries.

Use Seek with a decoded frame index. A frame is one time position across all channels. Try the included stereo tone.mp3.

Terminal
dotnet add package RecursiveAI.OneAudio

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OneAudio;
 
using var mp3 = Audio.Open("tone.mp3");
mp3.Seek(98765);
Console.WriteLine($"Position: {mp3.PositionFrames}");
var frame = new float[mp3.Format.Channels];
int read = mp3.Read(frame);
Console.WriteLine($"Frames read: {read}");
Text
Position: 98765
Frames read: 1

Unzip the example, then dotnet run.

At index 98,765, Read fills the buffer with one left and one right sample from the 44.1 kHz tone.

The full program

Two decoders open the same file. One seeks directly; the other reads and discards frames until it reaches the target. Both read the next 32 frames, and the program compares the bytes of the decoded floats, not just their values rounded for display.

C#
using System.Runtime.InteropServices;
using OneAudio;
 
long target = args.Length == 0 ? 98765 : long.Parse(args[0]);
using var seek = Audio.Open("tone.mp3");
using var linear = Audio.Open("tone.mp3");
int channels = seek.Format.Channels;
const int window = 32;
if (!seek.CanSeek || target < 0 || target > seek.LengthFrames - window)
    throw new ArgumentOutOfRangeException(nameof(target));
 
seek.Seek(target);
Console.WriteLine($"Decoded position: {seek.PositionFrames}");
 
var scratch = new float[4096 * channels];
while (linear.PositionFrames < target)
{
    int step = (int)Math.Min(4096, target - linear.PositionFrames);
    if (linear.Read(scratch.AsSpan(0, step * channels)) == 0)
        throw new EndOfStreamException();
}
 
var afterSeek = ReadWindow(seek);
var sequential = ReadWindow(linear);
bool equal = MemoryMarshal.AsBytes(afterSeek.AsSpan())
    .SequenceEqual(MemoryMarshal.AsBytes(sequential.AsSpan()));
Console.WriteLine($"Compared {window} frames, {channels} channels.");
Console.WriteLine($"Bit-identical: {equal}");
 
float[] ReadWindow(IAudioDecoder decoder)
{
    var samples = new float[window * channels];
    int frames = 0;
    while (frames < window)
    {
        int read = decoder.Read(samples.AsSpan(frames * channels));
        if (read == 0) throw new EndOfStreamException();
        frames += read;
    }
    return samples;
}
Text
Decoded position: 98765
Compared 32 frames, 2 channels.
Bit-identical: True

On a non-seekable input such as a pipe, Seek throws NotSupportedException; read forward instead or save the input to a seekable stream. A Read can return fewer frames than requested, so the full program keeps reading until the window is full or it reaches the end.

Variations

Seek to 750 ms. At 44,100 frames per second that’s frame 33,075. Run the full program with that frame index:

Terminal
dotnet run -- 33075
Text
Decoded position: 33075
Compared 32 frames, 2 channels.
Bit-identical: True

The short answer

Open a seekable MP3 with Audio.Open, pass a zero-based decoded frame index to Seek, then call Read. An MP3 frame is a compressed block containing many such positions. You don’t need to round to a compressed block boundary.

How do I seek to a specific time in an MP3?

Multiply seconds by Format.SampleRate and convert to a whole frame. Pick how to round based on your task: for the nearest position, round to the nearest integer; for the first frame at or after a cue, round up. Check the result against LengthFrames if it’s known. If you need a window of 32 frames, the last usable start is 32 frames before the end, not the index of the last sample. Don’t multiply the seek position by the channel count. That multiplication is for sizing or slicing the destination buffer, where left and right samples are stored next to each other.

Why doesn’t seeking to a byte offset give the same audio as decoding from the start?

Compressed frames aren’t indexed by decoded sample, and a variable-bitrate file doesn’t use the same number of bytes for each block. Finding the correct compressed frame is only part of the job: the MP3 bit reservoir can borrow data from earlier frames, and the synthesis filter needs earlier audio to rebuild its state.

The oneaudio MP3 decoder builds its own index by scanning frame headers; it doesn’t use Xing or VBRI seek tables. It starts decoding before the target to rebuild decoder state, using a 48-compressed-frame lookback to select an index entry. For files with usable gapless information in a Xing/Info header with the LAME extension, the decoder removes the reported delay and padding from the decoded timeline; without that information the decoder doesn’t trim them. Compare against a sequential read from the same decoder so both sides use the same timeline. “Bit-identical” here means the two reads of the decoded MP3 agree, not that the MP3 restored the original uncompressed recording.

All oneaudio recipes