How naudio/nlayer works
Project type and purpose
• NLayer is a fully-managed, stand-alone MP3-to-WAV decoder library written in C#. It provides two deliverables:
a) A core decoder (in the NLayer folder) that implements MPEG frame parsing and sample decoding.
b) An NAudio integration layer (in NLayer.NAudioSupport) that lets you plug NLayer into the NAudio playback/conversion pipeline.
• There is also a small WinForms “TestApp” showing how to consume the decoder in a GUI.
File structure
• Top-level projects:
• NLayer (core decoder)
– Decoder folder: implements each stage of MP3 decoding (bit reservoir, frame parsing, Huffman, layer-specific decoders, VBR headers, etc.)
– Public façade: MpegFile, MpegFrameDecoder, IMpegFrame
• NLayer.NAudioSupport
– Wrappers (Mp3FrameDecompressor, ManagedMpegStream) that implement NAudio’s FrameDecompressorBuilder pattern
• TestApp
– A Windows Forms application that references both NLayer and NAudio to demonstrate real-time playback or conversion
• Key config files: two .csproj files (core and NAudio support), a solution file, and a small test project.
Readme insights
• Shows simple API: construct new MpegFile(...) then ReadSamples(...) for raw PCM floats
• Illustrates NAudio usage: build an Mp3FileReader by supplying our decompressor builder, then feed into waveOut for playback
Components
a. External Actor/Client
– TestApp (or any consumer application)
b. NLayer Core
– File I/O / Stream Reader (MpegStreamReader)
– Frame Decoder (MpegFrameDecoder, layer-specific decoders, bit reservoir, Huffman, VBRInfo)
– Public API (MpegFile, IMpegFrame)
c. NAudio Integration Layer
– Mp3FrameDecompressor / ManagedMpegStream classes implementing NAudio interfaces
d. Optional External Service
– NAudio library (for playback, file conversion)
e. Underlying MP3 file (source) and WAV/PCM samples (output)
Relationships and data flow
• A directional flow:
1) Consumer App → (calls) MpegFile constructor → passes MP3 file stream
2) MpegFile → uses MpegStreamReader to read raw MPEG frames
3) MpegStreamReader → hands off to MpegFrameDecoder → pipeline of layer decoders (LayerI/II/III) → Huffman → bit reservoir → VBR/ID3 parsing
4) Decoder → outputs float[] PCM samples
5) (If using NAudioSupport) Mp3FrameDecompressor wraps the MpegFile pipeline and implements NAudio’s FrameDecompressorBuilder → feeds samples into Mp3FileReader → waveOut
Architectural patterns and design principles
• Layered architecture: separate I/O, decoding pipeline, public façade, and integration layer
• Dependency-inversion for NAudio integration: the core does not reference NAudio; instead NAudioSupport references both NLayer and NAudio
• Single-responsibility: each decoder class handles a specific MP3 layer or function (Huffman, reservoir, ID3, etc.)
Elements
• Through each Decoder stage → PCM Samples
• From Samples → TestApp (or WaveOut via NAudio)
• Core decoder (e.g. green)
• Integration layer (e.g. blue)
• External libraries (e.g. orange)