---
title: "Write WAV to a non-seekable stream in C#"
url: https://recursiveai.org/oneaudio/cookbook/write-wav-to-a-non-seekable-stream/
description: "Write WAV to a non-seekable stream in C# without buffering the audio. Set the PCM frame count up front so OneAudio can write an exact header."
version: "1.8.0"
---

# Write WAV to a non-seekable stream 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-wav-to-a-non-seekable-stream/write-wav-to-a-non-seekable-stream.zip)

Set `TotalFrames` to write the WAV header once, before sending the audio samples. The header comes first and holds the sizes, so on a non-seekable stream you need to know those sizes up front to write an exact header.

```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 output =
    Console.OpenStandardOutput();
var format = new AudioFormat(48000, 2);
int[] pcm = [1000, -1000, 2000, -2000];
var settings = new WavEncoderSettings
{
    SampleType = NativeSampleType.PcmSigned16,
    TotalFrames = pcm.Length / format.Channels
};
using var wav =
    new WavEncoder(output, format, settings);
wav.WriteInteger(pcm);
wav.Finish();
```

The snippet writes two generated stereo frames as binary WAV to standard output. After pasting it into a console project, use `dotnet build`, then `dotnet run --no-build | cat > two-frames.wav` in Bash to save the output rather than send binary data to the terminal.

Unzip [the example](https://recursiveai.org/oneaudio/cookbook/write-wav-to-a-non-seekable-stream/write-wav-to-a-non-seekable-stream.zip), then `dotnet run`.

## The full program

The download sends the same four samples to a write-only `Stream` and reopens a separate copy of the bytes. No input file or redirect is needed to run it.

```csharp
using OneAudio;

var format = new AudioFormat(48000, 2);
int[] pcm = [1000, -1000, 2000, -2000];
using var capture = new MemoryStream();
using var output = new WriteOnly(capture);
using var wav = new WavEncoder(output, format, new()
{
    SampleType = NativeSampleType.PcmSigned16,
    TotalFrames = pcm.Length / format.Channels
}, leaveOpen: true);
wav.WriteInteger(pcm);
wav.Finish();

using var copy = new MemoryStream(capture.ToArray());
using var reader = AudioDecoder.Open(copy, new AudioCodecRegistry().AddWav());
var decoded = new int[pcm.Length];
int frames = reader.ReadInteger(decoded);
if (frames != pcm.Length / format.Channels ||
    !decoded.AsSpan().SequenceEqual(pcm))
    throw new InvalidDataException("PCM did not round-trip.");

Console.WriteLine($"Output can seek: {output.CanSeek}");
Console.WriteLine($"Frames read: {frames}");
Console.WriteLine($"Samples: {string.Join(", ", decoded)}");

// Forward writes to the capture, but expose no way to read or seek.
sealed class WriteOnly(Stream capture) : Stream
{
    public override bool CanRead => false;
    public override bool CanSeek => false;
    public override bool CanWrite => true;
    public override long Length => throw new NotSupportedException();
    public override long Position
    {
        get => throw new NotSupportedException();
        set => throw new NotSupportedException();
    }
    public override void Flush() => capture.Flush();
    public override void Write(byte[] bytes, int offset, int count) =>
        capture.Write(bytes, offset, count);
    public override void Write(ReadOnlySpan<byte> bytes) =>
        capture.Write(bytes);
    public override int Read(byte[] bytes, int offset, int count) =>
        throw new NotSupportedException();
    public override long Seek(long offset, SeekOrigin origin) =>
        throw new NotSupportedException();
    public override void SetLength(long value) =>
        throw new NotSupportedException();
}
```

```text
Output can seek: False
Frames read: 2
Samples: 1000, -1000, 2000, -2000
```

If you exceed `TotalFrames`, the write throws `InvalidOperationException`; if you finish short, `Finish()` throws the same exception. Count frames, not interleaved samples or bytes.

## Variations

**Write without a known length.** In the full program, replace the `TotalFrames` setting with:

```csharp
TotalFrames = -1
```

The full program still reads back two frames. The WAV header now uses the unknown-size marker instead of exact lengths.

**Write RF64 to the same stream.** RF64 is a WAV layout with 64-bit sizes. Keep the frame count and add `ForceRf64` to the settings in the full program:

```csharp
TotalFrames = pcm.Length / format.Channels,
ForceRf64 = true
```

The full program reads back the same two frames from the RF64 copy.

## Related recipes

- [Write a WAV file](https://recursiveai.org/oneaudio/cookbook/write-wav/)
- [Read a WAV file](https://recursiveai.org/oneaudio/cookbook/read-wav/)

## The short answer

On a writable stream with `CanSeek == false`, set `WavEncoderSettings.TotalFrames` to the number of PCM frames you’ll send. OneAudio uses the sample type and channel count to work out the data size before writing the header. Write the samples, then call `Finish()`. The stream never needs to support rewinding.

## How do you deal with a non-seekable stream in C#?

Check [`Stream.CanSeek`](https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.canseek). A write-only pipe can accept new bytes but can’t move back to ones you’ve already sent. That matters for WAV because the header comes before the audio and holds the sizes. On a seekable file the encoder can fill in those sizes at `Finish()`; on a pipe it can only write them once.

A stereo frame is a left/right pair. For the four interleaved samples in the recipe, `pcm.Length / format.Channels` is two frames. For a series of blocks, declare the total across all blocks, not the size of a block. The example captures a copy only to show what was written; the encoder doesn’t need that copy. For a real destination, pass your writable stream directly, using `leaveOpen: true` if the caller owns it.

## Can you write WAV without knowing the length?

On a non-seekable stream, leaving `TotalFrames` at `-1` writes `0xFFFFFFFF` in the RIFF and data size fields. OneAudio’s reader treats those sizes as unknown and reads the audio through the end of the stream. If the receiver requires exact header sizes, give the encoder the frame count or write to a seekable file first and send the finished file.

RF64 is different: its `ds64` chunk holds the real 64-bit sizes, so a non-seekable RF64 output needs a known frame count. Setting `ForceRf64` with an unknown count on a non-seekable stream throws `ArgumentException` at construction. See the [OneAudio encoder reference](https://recursiveai.org/oneaudio/docs/api/).
