Skip to main content
LibAudio provides comprehensive audio file loading, decoding, and encoding capabilities for SerenityOS. It supports multiple audio formats and offers a unified interface for audio stream processing.

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.
class
High-level audio file loader supporting multiple formats.
Factory Methods:
  • create(StringView path): Load from file path
  • create(ReadonlyBytes buffer): Load from memory buffer

Audio Properties

method
Get the sample rate in Hz.
Common values: 44100, 48000, 96000 Hz
method
Get the number of audio channels.
Typical values: 1 (mono), 2 (stereo), 6 (5.1 surround)
method
Get total sample count (per channel).
For a 3-second stereo file at 44.1kHz: returns 132,300
method
Get number of samples loaded so far.

Sample Formats

LibAudio supports various PCM sample formats:
The Sample type represents a normalized floating-point sample value.

Supported Formats

FLAC (Free Lossless Audio Codec)

class
Lossless audio compression format loader.Features:
  • Lossless compression
  • Seek table support
  • Embedded metadata and pictures
  • Streaming decoding
Location: LibAudio/FlacLoader.h

MP3 (MPEG Audio Layer III)

class
Lossy audio compression format loader.Features:
  • Frame-based decoding
  • Variable and constant bitrate support
  • ID3v2 tag parsing
  • Huffman table decoding
Location: LibAudio/MP3Loader.h

WAV (Waveform Audio)

Uncompressed PCM audio format with RIFF container.

QOA (Quite OK Audio)

Lightweight lossy audio compression format.

Metadata Support

struct
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

LibAudio can extract embedded pictures from audio files:
Picture Types:
  • FrontCover: Album front cover
  • BackCover: Album back cover
  • LeadArtist: Lead artist photo
  • Media: Media (e.g., CD) image
  • And 17 other types defined in ID3v2 spec

Seeking and Navigation

Seek Points

class
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

LibAudio provides connections to AudioServer and AudioManagerServer:
Location:
  • ConnectionToServer.h: Audio playback
  • ConnectionToManagerServer.h: Device management

Plugin Architecture

class
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

enum
Audio loading and decoding errors.
Location: LibAudio/LoaderError.h

Generic Types

LibAudio uses normalized floating-point samples:
Samples are normalized to the range [-1.0, 1.0] regardless of source format.

Encoding Support

class
FLAC encoding capabilities.
Location: LibAudio/FlacWriter.h

Usage Example

Source Location

Directory: Userland/Libraries/LibAudio/ Key Files:
  • Loader.h: Main loader interface
  • GenericTypes.h: Sample and common types
  • FlacLoader.h: FLAC decoder
  • MP3Loader.h: MP3 decoder
  • LoaderError.h: Error definitions