---
title: "Write AAC ADTS frames in C#"
url: https://recursiveai.org/oneaudio/cookbook/write-raw-aac-adts-stream/
description: "Write an AAC-LC stream as ADTS frames instead of M4A. Convert a WAV in one call or write each packet directly to a stream in C#."
version: "1.7.2"
---

# Write AAC ADTS 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/write-raw-aac-adts-stream/write-raw-aac-adts-stream.zip)

Convert the included `tone.wav` to `.aac` for ADTS: a header on each AAC frame, no MP4 wrapper.

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

Audio.Transcode("tone.wav", "tone.aac");
Console.WriteLine("Wrote tone.aac.");
```

```text
Wrote tone.aac.
```

Unzip [the example](https://recursiveai.org/oneaudio/cookbook/write-raw-aac-adts-stream/write-raw-aac-adts-stream.zip), then `dotnet run`.

## The full program

The file conversion above picks ADTS from the `.aac` extension. The sample is a 440 Hz mono tone at 48 kHz. The program below uses the same WAV but writes the frames itself, so you can send them to any writable stream. The encoder writes AAC-LC and accepts mono or stereo at supported rates from 8 to 48 kHz.

```csharp
using OneAudio;
using OneAudio.Aac;

var tone = Audio.Load("tone.wav");
var settings = new AacEncoderSettings { Adts = true };
var encoder = new AacEncoder(48000, 1, settings);
using var output = File.Create("tone.aac");

int packets = 0;
for (int offset = 0; offset < tone.Frames;)
{
    offset += encoder.Write(tone.Samples.Span[offset..]);
    Drain();
}
encoder.Finish();
Drain();
Console.WriteLine($"Wrote {packets} ADTS frames to tone.aac.");

void Drain()
{
    while (encoder.TryReadPacket(out var packet))
    {
        output.Write(packet.Data.Span);
        packets++;
    }
}
```

```text
Wrote 6 ADTS frames to tone.aac.
```

`Write` returns the number of PCM frames it consumed, not the number of packets it produced. Advance by that count. Drain after each write and again after `Finish`, which pads and flushes the tail. An unsupported source format needs conversion to a supported rate and channel count before low-level encoding.

## Variations

**Choose ADTS when the output name doesn’t end in `.aac`.** Replace the `Transcode` call at the top:

```csharp
Audio.Transcode("tone.wav", "tone.bin",
    new TranscodeOptions { Format = AudioFileFormat.AacAdts });
```

**Encode stereo PCM.** Load a 48 kHz stereo WAV instead of the mono sample and pass `2` as the channel count to `AacEncoder`. In the full program’s loop, replace the `Write` line with this one: each PCM frame occupies two interleaved samples.

```csharp
offset += encoder.Write(tone.Samples.Span[(offset * 2)..]);
```

## Related recipes

- [Encode AAC in C#](https://recursiveai.org/oneaudio/cookbook/encode-aac/)
- [Decode AAC in C#](https://recursiveai.org/oneaudio/cookbook/decode-aac/)

---

## The short answer

Convert PCM audio to `.aac` with `Audio.Transcode`, or set `Format = AudioFileFormat.AacAdts` explicitly. If you’re encoding PCM a block at a time, give `AacEncoder` the `Adts = true` setting and write each `packet.Data` to your destination. No M4A writer is involved. In this encoder, ADTS adds a seven-byte header to each AAC access unit. Without that setting, the packet holds the compressed AAC payload without the ADTS header.

## What’s the difference between AAC ADTS and M4A?

AAC is the codec: it defines how the audio is compressed. ADTS (Audio Data Transport Stream) is framing for that compressed data. The header marks where a frame starts and signals details such as sample rate and frame length. An ADTS stream is a sequence of those frames. It isn’t headerless AAC; if your receiver expects bare AAC access units and configuration supplied separately, don’t enable ADTS.

M4A is an audio-only use of an MP4 container. The AAC can have the same profile and bitrate as the ADTS version, but it’s packaged with track information rather than an ADTS header on each packet. Choose the framing your receiver expects. Renaming a file doesn’t add or remove those headers. The [AAC decoder format reference](https://learn.microsoft.com/en-us/windows/win32/medfound/aac-decoder) distinguishes headerless AAC from ADTS; the [oneaudio encoder reference](https://recursiveai.org/oneaudio/docs/api/#aac-lc-encoding-flac-packets-and-the-mp4-writer) describes the packet setting and the MP4 writer.

## Does ADTS preserve the exact number of input samples?

Not by itself. The encoder works in hops of 1,024 PCM frames. It looks one hop ahead, so a write may consume input without making a packet available yet. The decoded stream has 1,024 frames of priming at the start, and `Finish` adds silence to fill out the last incomplete hop if necessary. In this example the 4,800-frame WAV results in six AAC packets: five hops of input and padding, plus priming. Packet count times 1,024 is therefore not the original length.

If you control both ends of an ADTS stream and need an exact cut, keep the original frame count with the stream and account for the encoder’s priming when decoding. If you want the file itself to carry that timing, the library’s M4A writer records the intended range in an MP4 edit list and an `iTunSMPB` tag. A player has to honor the timing metadata for the trim to take effect.
