---
title: "Decode FLAC to PCM frames in C#"
url: https://recursiveai.org/oneaudio/cookbook/decode-flac-to-pcm-frames/
description: "Decode a FLAC file to integer PCM in C#. Read interleaved frames into a reusable buffer without loading the whole waveform at once."
version: "1.8.0"
---

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

[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/decode-flac-to-pcm-frames/decode-flac-to-pcm-frames.zip)

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.

```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;

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](https://recursiveai.org/oneaudio/cookbook/decode-flac-to-pcm-frames/decode-flac-to-pcm-frames.zip) 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:

```csharp
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
```

## Related recipes

- [Read a FLAC file in C#](https://recursiveai.org/oneaudio/cookbook/read-flac/)
- [Decode audio in blocks](https://recursiveai.org/oneaudio/cookbook/streaming-decode/)
- [Encode raw FLAC frames in C#](https://recursiveai.org/oneaudio/cookbook/encode-flac-frames-in-memory/)

---

## 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](https://www.rfc-editor.org/rfc/rfc9639.html#section-4) 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](https://recursiveai.org/oneaudio/docs/api/#flac) 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.
