Overview
LibAudio handles audio data through a plugin-based architecture that supports various codecs and formats:- Loader System: Format-agnostic audio file loading
- Codec Support: FLAC, MP3, WAV, and QOA formats
- Sample Processing: Multi-channel audio with various sample formats
- Metadata Extraction: ID3 tags and embedded pictures
- Server Connection: Integration with AudioServer for playback
Core Components
Loader
The main interface for loading audio files.High-level audio file loader supporting multiple formats.Factory Methods:
create(StringView path): Load from file pathcreate(ReadonlyBytes buffer): Load from memory buffer
Audio Properties
Get the sample rate in Hz.Common values: 44100, 48000, 96000 Hz
Get the number of audio channels.Typical values: 1 (mono), 2 (stereo), 6 (5.1 surround)
Get total sample count (per channel).For a 3-second stereo file at 44.1kHz: returns 132,300
Get number of samples loaded so far.
Sample Formats
Supported PCM Formats
Supported PCM Formats
LibAudio supports various PCM sample formats:The
Sample type represents a normalized floating-point sample value.Supported Formats
FLAC (Free Lossless Audio Codec)
Lossless audio compression format loader.Features:
- Lossless compression
- Seek table support
- Embedded metadata and pictures
- Streaming decoding
LibAudio/FlacLoader.hMP3 (MPEG Audio Layer III)
Lossy audio compression format loader.Features:
- Frame-based decoding
- Variable and constant bitrate support
- ID3v2 tag parsing
- Huffman table decoding
LibAudio/MP3Loader.hWAV (Waveform Audio)
Uncompressed PCM audio format with RIFF container.QOA (Quite OK Audio)
Lightweight lossy audio compression format.Metadata Support
Audio file metadata including tags and embedded data.Contains:
- Artist, album, title information
- Track numbers and dates
- Genre and comment fields
- Custom metadata tags
Picture Data
Embedded Album Art
Embedded Album Art
LibAudio can extract embedded pictures from audio files:Picture Types:
FrontCover: Album front coverBackCover: Album back coverLeadArtist: Lead artist photoMedia: Media (e.g., CD) image- And 17 other types defined in ID3v2 spec
Seeking and Navigation
Seek Points
Efficient seeking within audio streams.Seek tables enable fast random access within compressed audio files.
Seeking Precision:
- Maximum seek point distance: 1000ms
- Seek tolerance: ±5000ms
- Loaders may provide frame-accurate seeking
Audio Server Integration
Connection Management
Server Connections
Server Connections
LibAudio provides connections to AudioServer and AudioManagerServer:Location:
ConnectionToServer.h: Audio playbackConnectionToManagerServer.h: Device management
Plugin Architecture
Base class for format-specific audio decoders.
Chunk-Based Loading
Audio is loaded in chunks for efficient streaming:
- Chunks are format-specific (FLAC frames, MP3 frames, etc.)
- Default buffer size: 8 KiB
- Loaders return multiple chunks to satisfy sample requests
- May overshoot requested sample count by small amounts
Error Handling
Audio loading and decoding errors.Location:
LibAudio/LoaderError.hGeneric Types
Sample Representation
Sample Representation
LibAudio uses normalized floating-point samples:Samples are normalized to the range [-1.0, 1.0] regardless of source format.
Encoding Support
FLAC encoding capabilities.Location:
LibAudio/FlacWriter.hUsage Example
Source Location
Directory:Userland/Libraries/LibAudio/
Key Files:
Loader.h: Main loader interfaceGenericTypes.h: Sample and common typesFlacLoader.h: FLAC decoderMP3Loader.h: MP3 decoderLoaderError.h: Error definitions
