Recursive AI

Decode FLAC to PCM frames 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 ReadInteger to get PCM sample values from FLAC; a frame is one sample per channel. The example ZIP includes steps.flac, which has six stereo frames.

Terminal
dotnet add package RecursiveAI.OneAudio

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

C#
using OneAudio;
 
using var audio = Audio.Open("steps.flac");
int channels = audio.Format.Channels;
int[] pcm = new int[2 * channels];
int frames;
while ((frames = audio.ReadInteger(pcm)) > 0)
    Console.WriteLine(string.Join(", ", pcm[..(frames * channels)]));

Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.

Text
100, -100, 200, -200
300, -300, 400, -400
500, -500, 600, -600

The sample is 48 kHz, 16-bit stereo. Its left channel contains 100, 200, 300, 400, 500, 600, and the right channel contains their negatives. Each output line holds two frames in left, right, left, right order. Two frames per read makes the example easy to see; for regular audio use a larger buffer, such as 1,024 frames times the channel count.

A corrupt FLAC frame can raise AudioFormatException after earlier frames have already been returned.

Variations

Read floats instead of integers. Use Read with a float[]. This reads the first frame of the same sample and checks that the two channels have opposite signs:

C#
using OneAudio;
 
using var audio = Audio.Open("steps.flac");
float[] frame = new float[audio.Format.Channels];
if (audio.Read(frame) > 0)
    Console.WriteLine(frame[0] > 0 && frame[1] < 0);
Text
True

The short answer

Open the FLAC file with Audio.Open and read it in a loop with ReadInteger. Size your buffer in frames times channels, but count the return value in frames. Consume only frames * channels values on each pass, and stop when the decoder returns zero. There’s no need to save a WAV file to get at the PCM values.

Is PCM the same as FLAC?

No. PCM is the series of sample values; FLAC compresses audio losslessly so those values can be recovered. A FLAC frame is a compressed block. A PCM frame, in this API, is one instant of decoded audio with one value per channel. The decoder reads compressed blocks internally but you can ask for a buffer of two PCM frames, 1,024, or another size without matching the boundaries in the file. See the FLAC specification for the definitions used by the format.

For stereo, an array with four elements can hold two PCM frames. For mono, the same array can hold four. In a processing loop, pass the valid range to the next step before reusing the same buffer. On the last successful call, there may be fewer frames than the buffer can hold; values beyond the returned count can be leftovers from the previous read.

How do I decode FLAC to integer PCM instead of floats?

Use ReadInteger when your next step takes integer PCM. In the example, the values printed for 16-bit FLAC are the original signed samples. For depths that aren’t a whole number of bytes, the FLAC decoder docs specify that integer samples are left-shifted to the whole-byte width in Format.BitsPerSample. For example, a 12-bit FLAC reports 16-bit container values; read FlacDecoder.BitsPerSample for the original depth. Don’t assume all integer samples use 16-bit scale just because your destination API takes shorts.

Use Read when your mixer or filter takes floats. That path scales using the file’s actual depth, with full scale at about −1 to +1. Both calls return frames rather than array elements. Neither call writes PCM bytes to disk; a file or device sink still needs its own sample format, channel count and sample rate.

All oneaudio recipes