Skip to content

NAudio 3 support, and the groundwork for a 3.0 release - #21

Merged
markheath merged 8 commits into
masterfrom
claude/naudio-3-compatibility-9so9qq
Sep 4, 2026
Merged

markheath merged 8 commits into
masterfrom
claude/naudio-3-compatibility-9so9qq

Conversation

@markheath

Copy link
Copy Markdown
Contributor

Makes NAudio.Vorbis work with NAudio 3, fixes a long-standing decode hang, and adds the things the repo was missing before it can cut a release: tests, CI, a release pipeline and a test harness.

Fixes #20, fixes #16, closes #17.

NAudio 3 support (#20)

NAudio 3 changed IWaveProvider.Read / ISampleProvider.Read to take a single Span<T>, so the 1.5.0 assembly fails to load against it with TypeLoadException: Method 'Read' in type 'NAudio.Vorbis.VorbisWaveReader' does not have an implementation. It also dropped .NET Standard 2.0 for net9.0.

Both are recompile-required breaks, so this is a clean-cut major: 3.x targets NAudio 3 on net9.0, and NAudio 2 users stay on 1.5.0.

  • VorbisSampleProvider implements ISampleProvider.Read(Span<float>). NVorbis needs a whole number of sample frames but a span caller can pass any length, so the count is trimmed here rather than relying on callers.
  • VorbisWaveReader implements Read(Span<byte>) with the byte[] overload forwarding to it, matching NAudio 3's built-in readers. The caller's buffer is reinterpreted as floats so the decoder writes straight into it, which removes a [ThreadStatic] scratch buffer and a copy per read.
  • The (buffer, offset, count) overloads stay as forwarders, so existing calling code still compiles.
  • Removed the long-obsolete IsParameterChange, ClearParameterChange and ContainerOverheadBits.

The decode hang (#16)

Reading with a buffer larger than the samples remaining can leave NVorbis's decode state with a negative valid length. Its Read loop then satisfies neither the refill branch nor the copy branch, so it spins forever without consuming input or producing output (NVorbis#40). Stream.CopyTo sizes its buffer from Length, so its final read always over-reads — which is how the reporter hit it.

Confirmed against the file attached to the issue: reads 1–9 return normally and read #10, the one that reaches past the last sample, never returns. It reproduces on 0.10.4 and 0.10.5 alike; NVorbis fixed the decoder itself in the 1.0.0 line.

The spin is inside NVorbis's own loop, so nothing downstream can recover once it starts — the fix has to be not to trigger it. Read now clamps to what the stream has left. TotalSamples is only knowable when the container can seek, which is the same condition NVorbis's own fix uses, so forward-only sources are left unclamped; a source constructed with allowSeek: true that turns out not to seek stops being asked after the first NotSupportedException. The reporter's program now decodes the file in 155ms instead of hanging.

A forward-only read of such a file still hangs, because there is no length to clamp to. That needs NVorbis 1.0.

NVorbis 0.10.5 (#17)

Strictly better than 0.10.4: it fixes Comments throwing InvalidCastException on every file, and turns a silently-wrong seek into a loud one on files whose first header page has granule −1.

Tests

54 xunit tests over both read paths, position and seeking, tags, stream ownership and forward-only sources. Test audio is generated from synthesised tones rather than lifted from a recording, so the corpus carries no third-party rights; NAudio.Vorbis.Tests/tools regenerates it.

short-granule.ogg is our own stereo file with the last page's granule position pulled back and the page CRC recomputed, so its stated length understates its contents — a minimal reproduction of #16. Reverting the guard fails those two tests on a watchdog rather than hanging the run; that was checked both ways.

Known NVorbis limitations

Writing the seek tests turned up two more 0.10.5 defects, both fixed in the 1.0.0 line and neither ours to fix:

  • seeking into the final page can throw GranulePos mismatch when the last page's granule does not line up with the packets on it — the ordinary shape of an encoder's trailing partial page (NVorbis#39)
  • seeking back to the start does not always land on the first sample: the position reads back as 0 but decoding resumes elsewhere

The tests assert strictly where 0.10.5 is correct and only what holds on both versions where it is not, each noting what to tighten on the NVorbis 1.0 bump. They are in the README so they are not rediscovered as NAudio.Vorbis bugs.

Releases stay on the stable 0.10.5, because a stable package should not carry a prerelease dependency (NU5104). The suite passes against 0.10.5 and 1.0.0-rc.2.

Infrastructure

  • build.yml — builds and tests every PR and push on windows-latest, plus a continue-on-error job against the NVorbis 1.0 prerelease so we know when it drifts.
  • release.yml — modelled on NLayer. Tag-driven finals, dispatch-driven previews, notes from RELEASE_NOTES.md, NuGet trusted publishing (OIDC). Specific to this repo: an nvorbis_version input so a preview can be published against the NVorbis rc without changing the default, and a check that refuses to cut a final release against a prerelease NVorbis.
  • TestApp — WinForms, plays through WasapiPlayer with a position bar that tracks playback and seeks when dragged. Failed seeks go to the status line rather than taking the app down, since seeing which files misbehave is the point. Reads and seeks share a lock, because otherwise repositioning during playback races the playback thread inside the decoder.
  • Strong naming — new in 3.0, matching NAudio and NLayer. NVorbis is unsigned in every version, so CS8002 is suppressed for that reference; .NET Core dropped strong-name verification, so it has no effect on net9.0.
  • Directory.Build.props / .targets for version and package metadata, SourceLink, symbol packages, and an overridable NVorbisVersion.
  • The public API is now fully XML-documented, and the build runs with TreatWarningsAsErrors.

Verification

dotnet build and dotnet test are clean against both NVorbis 0.10.5 and 1.0.0-rc.2, and both the stable and preview dotnet pack paths were run and their nupkg contents checked.

Two things I could not verify here: the WinForms harness has not been run — this environment has no Windows and no display, so it is compile-verified only, and the click-and-drag pass is still needed. And the workflows have not executed; they are validated as YAML and modelled closely on NLayer's, but the first real run is the test.


Generated by Claude Code

claude added 8 commits August 31, 2026 21:25
NAudio 3 changed IWaveProvider.Read / ISampleProvider.Read to take a single
Span<T> instead of buffer/offset/count, so the existing assembly fails to load
against it with "Method 'Read' in type 'NAudio.Vorbis.VorbisWaveReader' does
not have an implementation". NAudio 3 also drops .NET Standard 2.0 in favour
of .NET 9, so this is a clean-cut major version: NAudio.Vorbis 3.x targets
NAudio 3 / net9.0, and NAudio 2 users stay on 1.5.0.

- VorbisSampleProvider implements ISampleProvider.Read(Span<float>). NVorbis
  requires a whole number of sample frames but a Span caller is free to pass
  any length, so the count is trimmed here rather than relying on callers.
- VorbisWaveReader implements Read(Span<byte>) with the byte[] overload
  forwarding to it, matching NAudio 3's built-in readers. The caller's buffer
  is reinterpreted as floats so the decoder writes straight into it, which
  removes the [ThreadStatic] conversion buffer and a copy per read.
- The float[]/offset/count overloads are kept on both types as thin
  forwarders, so existing calling code still compiles.
- Removed the long-obsolete IsParameterChange, ClearParameterChange and
  ContainerOverheadBits members.
- Updated NVorbis to 0.10.5, which fixes VorbisWaveReader.Comments throwing
  an InvalidCastException from NVorbis TagData.All on every file.

Verified by decoding the NVorbis test corpus through all three read paths
(byte[], Span<byte> and Span<float>) and checking they produce identical
samples, plus format, length, seek and comment metadata.

Fixes #20
Closes #17

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019ysTjCRRPXjVbtSvMuNKZ1
Reading with a buffer larger than the samples remaining can leave NVorbis's
decode state with a negative valid length. Its Read loop then satisfies
neither the refill branch nor the copy branch, so it spins forever without
consuming input or producing output (NVorbis#40). The reported case is a
Stream.CopyTo, which sizes its buffer from Length and so always over-reads on
the final call, but any caller reading past the end can hit it.

The spin is inside NVorbis's own loop, so nothing downstream can recover once
it starts - the fix has to be not to trigger it. Read now clamps the count to
the samples the stream has left. TotalSamples is only knowable when the
container can seek, which is the same condition NVorbis's own fix for this
uses, so forward-only sources are left reading unclamped as before; a source
constructed with allowSeek: true that turns out not to support it stops being
asked after the first NotSupportedException.

On files whose granule timeline overstates the length this makes no
difference. On files that understate it, the tail is now clamped to the
stated end rather than over-emitting past it, which matches what NVorbis 1.0
does for the same files.

Verified with the file attached to the issue: the reporter's program decodes
it in 155ms rather than hanging. The NVorbis test corpus decodes byte for
byte identically to before through every read path, seekable and
forward-only alike.

Note this is a mitigation, not the root cause. A forward-only read of a file
like this one still hangs, because there is no length to clamp to. NVorbis
fixed the decoder itself in the 1.0.0 line; NVorbis 0.10.5 does not have it.

Fixes #16

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019ysTjCRRPXjVbtSvMuNKZ1
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019ysTjCRRPXjVbtSvMuNKZ1
37 tests over the reader and the sample provider: format and duration, the
four Read overloads agreeing sample for sample, whole-frame handling for
buffers that are not a multiple of the channel count, position and seeking,
tags, stream ownership on dispose, and forward-only sources.

Test audio is generated from synthesised sine tones rather than lifted from
a recording, so the corpus carries no third-party rights; tools/ has the
script that regenerates it.

OverReadRegressionTests covers issue #16. short-granule.ogg is our own
sine-stereo.ogg with the last page's granule position pulled back (page CRC
recomputed), so the stream's stated length understates its contents and a
read that reaches the end runs past it - the condition that sends the NVorbis
decode loop into its permanent spin. Reverting the guard in
VorbisSampleProvider.Read fails those two tests rather than hanging the run,
because each is bounded by a watchdog.

Project layout moves onto the conventions NLayer uses: Directory.Build.props
carries the version and shared package metadata, SourceLink and symbol
packages are on, and Directory.Build.targets packs the README and icon and
can load release notes from a file at pack time.

The NVorbis version is now an overridable property, so a preview can be built
against a newer NVorbis without editing the repo:

    dotnet build -p:NVorbisVersion=1.0.0-rc.2

Public members on VorbisWaveReader that had no XML documentation now have it,
so the package ships complete IntelliSense and the build stays clean with
TreatWarningsAsErrors and GenerateDocumentationFile both on.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019ysTjCRRPXjVbtSvMuNKZ1
Seeking is the part of this wrapper most likely to be wrong, and it is what
the WinForms harness exercises by hand, so it needs assertions of its own.

Two NVorbis 0.10.5 defects turned up while writing these, both fixed in the
1.0.0 line and neither ours to fix:

 - seeking back to the start does not always land on the first sample - the
   position reads back as 0 but decoding resumes elsewhere (visible on the
   mono file, not the stereo one)
 - seeking into the final page can throw "GranulePos mismatch" when the last
   page's granule does not line up with the packets on it, which is the
   ordinary shape of an encoder's trailing partial page (NVorbis#39)

The tests assert strictly where 0.10.5 is correct and, where it is not,
assert only what holds on both versions, each saying in a comment what to
tighten once the NVorbis dependency moves to 1.0. The suite passes against
0.10.5 and against 1.0.0-rc.2.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019ysTjCRRPXjVbtSvMuNKZ1
Everything here follows the shape NLayer already uses, so the two repos work
the same way.

build.yml builds and tests on every pull request and push to master, on
windows-latest so the WinForms harness is covered too. A second job builds
against the NVorbis 1.0 prerelease; it is allowed to fail, since it tracks
something we do not ship and should not block a PR when NVorbis publishes a
new rc.

release.yml publishes to NuGet via trusted publishing (OIDC), so there is no
API key to rotate. A v* tag cuts a final release and a manual run cuts a
preview, with release notes lifted from the matching RELEASE_NOTES.md section
in both cases. Two things are specific to this repo: an nvorbis_version input
that builds a preview against a different NVorbis, and a check that refuses to
cut a *final* release against a prerelease NVorbis, since a stable package
should not carry a prerelease dependency. Between them, a preview on the
NVorbis 1.0 rc can be published without changing the repo default.

TestApp plays a file through WasapiPlayer with a position bar that tracks
playback and seeks when dragged, which is the only practical way to exercise
repositioning. Seeking is the least reliable part of the decoder today, so
failures land in the status line instead of taking the app down - seeing which
files misbehave is the point. Reads and seeks share a lock, because otherwise
repositioning during playback races the playback thread inside the decoder and
the corruption looks like a decoder bug.

The README covers NAudio 3 and .NET 9, leads with WasapiPlayer, and documents
the NVorbis limitations we cannot fix from here so they are not rediscovered
as NAudio.Vorbis bugs. Every code sample in it was compiled.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019ysTjCRRPXjVbtSvMuNKZ1
NAudio and NLayer are both strong-named and NAudio.Vorbis was not, so consumers
signing their own assemblies had to make an exception for this one. 3.0 is the
release to fix that in: the identity change it causes costs nothing on top of a
major that already requires a new target framework and a recompile against
NAudio 3.

The key is checked in at the repo root, as it is in the other two repos. For
open-source strong naming the private half is not a secret - it establishes
assembly identity rather than protecting anything - and a key nobody else can
build with would be worse than no key.

No version of NVorbis is strong-named, including the 1.0 prereleases, so
referencing it from a signed assembly raises CS8002, which TreatWarningsAsErrors
turns into a build failure. It is suppressed with a note: .NET Framework refused
to load an unsigned reference from a signed assembly, but .NET Core and later
dropped strong-name verification, so on net9.0 there is nothing to fix.
NAudio.Core is signed.

The library, tests and harness all come out with public key token
f2a2dcb8253cf711, and the suite passes against NVorbis 0.10.5 and 1.0.0-rc.2.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019ysTjCRRPXjVbtSvMuNKZ1
NAudio is already on .slnx and this repo's .sln dated from 2019. The XML
format is diffable and mergeable, and drops the GUIDs and the per-project
configuration matrix that nobody edited by hand.

`dotnet sln migrate` carried over x64 and x86 platforms from the old file;
none of these projects build for either, so the generated <Configurations>
block goes and the defaults apply, matching NAudio's own .slnx. A Solution
Items folder puts the README, release notes, build props and workflows where
they can be opened from the IDE.

.slnx needs the 9.0.200 SDK or later, so global.json pins that floor - an
older SDK now fails with something that says so - and rolls forward, so .NET
10 and later are fine. CI installs 9.0.x, comfortably past it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019ysTjCRRPXjVbtSvMuNKZ1
@markheath

Copy link
Copy Markdown
Contributor Author

Have completed testing on Windows of the TestApp with various ogg files and playback and repositioning are working as expected with NAudio 3

@markheath
markheath merged commit 2f3603c into master Sep 4, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TypeLoadException after updating NAudio to 3.0.1 Update to NVorbis v0.10.5 Infinite loop while converting an ogg

2 participants