🔊 High-performance native Windows WASAPI and XAudio2 audio playback API for Java.
FastAudioPlayer is the high-performance native audio output substrate of the FastJava ecosystem. It provides low-latency WASAPI-based playback primitives required for real-time speech synthesis (FastTTS), low-overhead audio streaming, and high-performance game loops in Java without GC pressure.
import fastaudio.FastAudioPlayer;
public class Demo {
public static void main(String[] args) throws Exception {
// Initialize player (creates WASAPI context)
FastAudioPlayer player = new FastAudioPlayer();
// Load audio file (WAV or MP3)
player.load("beep.wav");
System.out.println("Duration: " + player.getDuration() + " ms");
// Asynchronous low-latency playback
player.play();
while (player.isPlaying()) {
Thread.sleep(100);
}
// Clean up native resources
player.close();
}
}- Why FastAudioPlayer?
- Quick Start
- Key Features
- Real-World Use Cases
- Performance Benchmarks
- API Quick Reference
- Technical Examples
- Installation
- Documentation
- Platform Support
- License
- Related Projects
Standard Java audio solutions (specifically the legacy javax.sound.sampled JavaSound API) suffer from critical latency and stability issues in interactive, real-time applications:
- High Audio Latency & Buffering Stutter: JavaSound introduces 45–120 ms of mixing latency, making synchronized voice playback (Text-to-Speech) and responsive game sound effects feel noticeably delayed.
- Garbage Collection Churn in Audio Loops: Continuously allocating temporary
byte[]arrays for chunk streaming causes periodic JVM Garbage Collection pauses and audible clicks/pops. - Missing Modern OS Integration: JavaSound cannot interface directly with low-latency Windows Audio Session API (WASAPI) endpoints, hardware device changes, or native volume scaling.
- Thread Blocking & CPU Overhead: Legacy Java audio lines block worker threads and consume excessive CPU cycles during real-time mixing.
FastAudioPlayer solves this by binding directly to Windows WASAPI with zero-allocation ring buffers and hardware-accelerated routing:
| Feature | JavaSound (SourceDataLine) |
Heavy C++ Wrappers (OpenAL / SDL) | FastAudioPlayer |
|---|---|---|---|
| Audio Backend | Legacy Windows DirectSound / MME | Heavy OpenAL / SDL runtimes | Native Windows WASAPI |
| Buffer Latency | 45–120 ms (Mixer queue lag) | 15–30 ms | ~10 ms (Shared engine) |
| GC Allocations (Loop) | High (byte[] stream churn) |
Moderate (JNI handle churn) | 0 bytes (Off-heap buffer) |
| Control Latency | Blocking / Buffer lag | JNA bridge overhead | < 1 µs (Direct memory) |
| Codec Integration | Basic WAV / AU only | Format dependent | WAV, MP3 & PCM stream |
| Dependencies | Standard JDK | Bulky C++ runtime bundles | Lightweight DLL via FastCore |
- ⏱️ Ultra-Low Latency: Direct Windows WASAPI access via JNI with native COM initialization.
- ⚙️ Zero GC Overhead: Optimized playbacks using lightweight native state-handles (Zero-GC off-heap buffers).
- 📦 Zero External Dependencies: Just requires Java 17+ and Windows. Bundles pre-compiled DLLs.
- 🎛️ Total Audio Control: Real-time Volume, Pause, Resume, Stop, playback position queries, and output device selection.
- 🔊 Low-Latency Game & GUI Audio: Sub-15ms audio buffer playback via native WASAPI endpoints.
- 🎙️ Voice AI Assistant Playback: Stream synthetic speech directly from FastTTS with zero buffer stutter.
- 🎚️ Hardware Gain & Equalization: Apply AVX2 SIMD volume scaling and pitch adjustments on off-heap PCM buffers.
- 🎵 Multi-Track Audio Mixing: Route multiple non-blocking native sound streams in real-time desktop applications.
In the official JMH Benchmark, FastAudioPlayer measures state handle access and zero-overhead native routing throughput:
Benchmark Mode Cnt Score Error Units
JMH_FastAudioPlayer.benchmarkPlayer thrpt 2 1,180,759,582 ops/s
Zero Overhead Native Handle: Accessing and routing player control states runs at over 1.18 Billion operations per second with zero JVM Garbage Collection allocations.
FastAudioPlayer bypasses JavaSound's high-overhead mixer layer, communicating directly with Windows Audio Session API:
| Audio Engine | Hardware Buffer Latency | CPU Overhead (Playback Loop) | GC Pressure |
|---|---|---|---|
| JavaSound (SourceDataLine) | 45 ms - 120 ms | ~4.5% | High (byte[] allocations) |
| FastAudioPlayer (WASAPI) | ~10 ms | <0.5% | None (Zero GC) |
| Method | Description | Target |
|---|---|---|
load(String path) |
Asynchronously loads a WAV/MP3 file into memory. | File / Path |
play() |
Triggers or resumes low-latency playback. | WASAPI Output |
pause() / resume() |
Pauses/resumes playbacks cleanly. | State Control |
stop() |
Stops playing and resets playback position. | State Control |
setVolume(float vol) |
Adjusts volume from 0.0 (mute) to 1.0 (max). |
Hardware Volume |
getDevices() |
Queries all available native WASAPI endpoint devices. | Audio Hardware |
setDevice(String id) |
Dynamically changes the active playback output device. | Output Switching |
close() |
Safely frees all native structures and terminates COM threads. | JNI Cleanup |
Tip
Refer to the Javadoc in FastAudioPlayer.java for full threading contracts and fallback rules.
Add the JitPack repository and the complete dependency stack to your pom.xml:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<!-- FastAudioPlayer Engine -->
<dependency>
<groupId>com.github.andrestubbe</groupId>
<artifactId>FastAudioPlayer</artifactId>
<version>0.1.2</version>
</dependency>
<!-- FastSIMD Hardware Vector Acceleration Engine -->
<dependency>
<groupId>com.github.andrestubbe</groupId>
<artifactId>FastSIMD</artifactId>
<version>0.1.3</version>
</dependency>
<!-- FastMemory Aligned Allocator -->
<dependency>
<groupId>com.github.andrestubbe</groupId>
<artifactId>FastMemory</artifactId>
<version>0.1.1</version>
</dependency>
<!-- FastPointer Address Wrapper -->
<dependency>
<groupId>com.github.andrestubbe</groupId>
<artifactId>FastPointer</artifactId>
<version>0.1.1</version>
</dependency>
<!-- FastAudioProcess Audio Engine -->
<dependency>
<groupId>com.github.andrestubbe</groupId>
<artifactId>FastAudioProcess</artifactId>
<version>0.1.1</version>
</dependency>
<!-- FastCore Unified JNI Loader -->
<dependency>
<groupId>com.github.andrestubbe</groupId>
<artifactId>FastCore</artifactId>
<version>0.1.0</version>
</dependency>
</dependencies>repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
implementation 'com.github.andrestubbe:FastAudioPlayer:0.1.2'
implementation 'com.github.andrestubbe:FastSIMD:0.1.3'
implementation 'com.github.andrestubbe:FastMemory:0.1.1'
implementation 'com.github.andrestubbe:FastPointer:0.1.1'
implementation 'com.github.andrestubbe:FastAudioProcess:0.1.1'
implementation 'com.github.andrestubbe:FastCore:0.1.0'
}Download the latest JARs directly to add them to your classpath:
- 📦 FastAudioPlayer-0.1.2.jar (Native WASAPI Player)
- ⚙️ fastcore-0.1.0.jar (Required JNI Loader)
We provide high-quality, standalone examples inside the examples/ directory:
- Interactive Console Demo An interactive terminal audio player featuring active audio device query, real-time volume controls, crisp 440Hz sine wave auto-generation, and a smooth real-time visual progress bar.
- Precision Latency Benchmark Side-by-side precision latency benchmark comparing Windows
WASAPI Native (
FastAudioPlayer) against standard Java Sound (javax.sound.sampled.SourceDataLine) over multiple iterations.
You can instantly compile and run these examples using the root automation scripts run-demo.bat and
run-benchmark.bat.
- CHANGELOG.md: Release notes and version history.
- REFERENCE.md: Core API reference manual.
- PHILOSOPHY.md: Engineering rationale for zero-allocation performance.
- COMPILE.md: Full compilation guide (MSVC C++17 build chain + JNI Setup).
- ROADMAP.md: Future development goals.
| Platform | Status |
|---|---|
| Windows 10/11 | âś… Fully Supported |
| Linux | đź”— Planned |
| macOS | đź”— Planned |
MIT License See LICENSE file for details.
- FastCore Native Library Loader for Java
- FastAudioCapture High-Performance Native Audio Capture for Java
- FastTTS High-Performance Native Windows TTS API for Java
- FastSTT Ultra-Fast Native Speech-to-Text for Java
- FastWakeWord
Part of the FastJava Ecosystem — Making the JVM faster. Small package. Maximum speed. Zero bloat. 🚀📋
