Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
MoosicBox Audio Output
A cross-platform audio output abstraction layer for high-quality audio playback.
Overview
The MoosicBox Audio Output package provides:
- Cross-Platform Support: Works on Windows, macOS, and Linux
- CPAL-Based Audio Output: Uses CPAL (Cross-Platform Audio Library) for audio playback
- Professional Audio Backend Support: Optional JACK and ASIO support via CPAL
- Audio Format Encoding: Built-in support for encoding to AAC, FLAC, MP3, and Opus
- Device Management: Scan and select audio output devices
- Command-Based Control: Pause, resume, reset, and volume control via
AudioHandle(seekcommands currently return an error for CPAL outputs) - Progress Tracking: Real-time playback position tracking with callbacks
Supported Audio Backends
All audio backends are provided through CPAL (Cross-Platform Audio Library):
CPAL (Cross-Platform Audio Library)
- Platforms: Windows (WASAPI, DirectSound), macOS (Core Audio), Linux (ALSA)
- Use Case: General-purpose audio output with good cross-platform compatibility
- Default Backend: Enabled by default
JACK (JACK Audio Connection Kit)
- Platforms: Linux, macOS, Windows
- Use Case: Professional audio, low-latency applications
- Features: Real-time audio routing, minimal latency
- Enabled via:
jackfeature flag
ASIO (Audio Stream Input/Output)
- Platforms: Windows
- Use Case: Professional audio interfaces, ultra-low latency
- Requirements: ASIO-compatible audio hardware
- Enabled via:
asiofeature flag
Usage
Basic Audio Output
use ;
use AudioWrite;
use AudioBuffer;
async
Device Enumeration
use ;
async
Volume and Playback Control
use ;
async
Progress Tracking
use ;
async
Feature Flags
Audio Backends
cpal- Enable CPAL backend (cross-platform, enabled by default)jack- Enable JACK backend via CPAL (professional audio)asio- Enable ASIO backend via CPAL (Windows professional audio)
Audio Encoding Formats
aac- Support for encoding to AAC formatflac- Support for encoding to FLAC formatmp3- Support for encoding to MP3 formatopus- Support for encoding to Opus format
API Features
api- Enable API models for integration (enabled by default)openapi- Enable OpenAPI/utoipa support (enabled by default)
Default Features
The default feature enables: api, default-windows, and openapi.
The default-windows feature enables: aac, cpal, flac, mp3, and opus.
Architecture
The audio output package uses a layered architecture:
- AudioOutput: Main wrapper that handles resampling and delegates to
AudioWrite - AudioWrite: Core trait for writing audio buffers to output devices
- AudioOutputFactory: Factory pattern for creating audio outputs with specific configurations
- CpalAudioOutput: CPAL-based implementation of
AudioWrite - AudioHandle: Command-based interface for controlling playback (pause, resume, reset, volume; seek command currently returns an error for CPAL outputs)
- ProgressTracker: Tracks and reports playback progress with callbacks
Audio Specifications
The package uses Symphonia's SignalSpec for audio specifications:
use SignalSpec;
// The SignalSpec defines the audio format
let spec = SignalSpec ;
Audio quality is determined by the source material and the audio device's capabilities. The package automatically handles:
- Sample rate conversion (resampling)
- Channel configuration
- Buffer management (30-second ring buffer with 10-second initial buffering)
Platform-Specific Notes
Linux
- ALSA: Default backend via CPAL (direct hardware access)
- JACK: Optional professional audio backend (enable with
jackfeature)
# JACK is optional - install only if you need professional audio features
macOS
- Core Audio: Native macOS audio (via CPAL)
- JACK: Optional professional audio backend (enable with
jackfeature)
# JACK is optional - install only if you need professional audio features
Windows
- WASAPI: Modern Windows audio API (default via CPAL)
- DirectSound: Legacy Windows audio (fallback via CPAL)
- ASIO: Optional professional audio backend (enable with
asiofeature)
For ASIO support:
- ASIO-compatible audio hardware
- ASIO drivers from hardware manufacturer
- Enable the
asiofeature flag in Cargo.toml
Error Handling
use AudioOutputError;
match audio_output.write
Implementation Details
Buffer Management
The CPAL implementation uses a ring buffer architecture:
- Ring buffer size: 30 seconds of audio
- Initial buffering: 10 seconds before playback starts
- Purpose: Prevents audio underruns and ensures smooth playback
This approach ensures that:
- Short audio clips (< 10 seconds) start immediately on flush
- Long audio content has ample buffering to prevent crackling
- Volume changes are applied immediately in the CPAL callback
Sample Rate Handling
The package automatically handles sample rate conversion:
- Uses the
moosicbox_resamplercrate for high-quality resampling - Converts input audio to match the output device's sample rate
- Maintains audio quality during conversion
Progress Tracking
Progress tracking uses a dedicated ProgressTracker:
- Tracks consumed samples from the CPAL audio callback
- Calculates playback position based on actual output sample rate
- Triggers callbacks when position changes by ≥0.1 seconds
- Thread-safe via atomic operations
Integration with MoosicBox
The audio output package integrates with other MoosicBox components:
- moosicbox_audio_decoder: Provides decoded audio buffers via the
AudioDecodetrait - moosicbox_resampler: Handles sample rate conversion automatically
- moosicbox_player: Uses
AudioOutputfor playback - moosicbox_stream_utils: Provides streaming utilities
The AudioOutput implements the AudioDecode trait, allowing it to receive decoded audio directly from audio decoders.
Troubleshooting
Common Issues
-
No audio output:
- Run
scan_outputs().awaitto ensure devices are detected - Check system audio settings and permissions
- Verify CPAL feature is enabled
- Run
-
Audio crackling or stuttering:
- This is typically a system resource issue
- The package uses a 30-second ring buffer with 10-second initial buffering to prevent this
- Check CPU usage and system load
-
Device not found:
- Verify the device is connected and enabled in system settings
- Run
output_factories().awaitto list available devices
-
Build errors with JACK/ASIO:
- Ensure you have the required development libraries installed
- JACK requires
libjack-jackd2-devon Linux - ASIO requires ASIO SDK and drivers on Windows
Debug Logging
Enable debug logging to troubleshoot issues:
# Enable audio output debugging
RUST_LOG=moosicbox_audio_output=debug
# Enable trace-level logging for detailed information
RUST_LOG=moosicbox_audio_output=trace
Platform-Specific Troubleshooting
Linux
# List ALSA devices
# Check JACK status (if using JACK feature)
Windows
# List audio devices
macOS
# List audio devices
See Also
- MoosicBox Player - Audio playback engine that uses this library
- MoosicBox Audio Decoder - Audio format decoding
- MoosicBox Server - Server that integrates audio output