tunes
A standalone Rust library for music composition, synthesis, and audio generation with real-time, concurrent playback and control. Build complex musical pieces with an intuitive, expressive API — no runtime dependencies required. Perfect for algorithmic music, game audio, generative art, and interactive installations.
Performance: CPU synthesis measured at 100x realtime (uncached) and 18.6x realtime (cached complex compositions) on modern hardware. SIMD sample playback: 11-17x realtime with true concurrent playback (all samples playing simultaneously) - can handle 550-1,100+ concurrent samples in real-world scenarios. SIMD effects: 98.8x realtime with all effects stacked. Optional GPU acceleration available via
gpufeature - provides minimal benefit on integrated GPUs (~1.1x) but scales with discrete GPU hardware.
Features
- Music Theory: Scales, chords, patterns, progressions, and transposition
- Composition DSL: Fluent API for building musical sequences
- Sections & Arrangements: Create reusable sections (verse, chorus, bridge) and arrange them
- Synthesis: FM synthesis, Granular synthesis, Karplus Strong, additive synthesis, filter envelopes, wavetable oscillators
- Instruments: 150+ Pre-configured synthesizers, bass, pads, leads, guitars, percussion, brass, strings, woodwinds and more
- Rhythm & Drums: 100+ pre-configured drum sounds, drum grids, euclidean rhythms, 808-style synthesis, and pattern sequencing
- Effects, Automation and Filters: Delay, convolution reverb, distortion, parametric EQ, chorus, modulation, tremolo, autopan, gate, limiter, compressor (with multiband support), bitcrusher, eq, phaser, flanger, saturation, sidechaining/ducking, various filters
- Musical Patterns: Arpeggios, ornaments, tuplets, and many classical techniques and patterns built-in
- Algorithmic Sequences: 50+ algorithms, including Primes, Fib, 2^x, Markov, L-map, Collatz, Euclidean, Golden ratio, random/bounded walks, Thue-Morse, Recamán's, Van der Corput, L-System, Cantor, Shepherd, Cellular Automaton, and many more
- Tempo & Timing: Tempo changes, time signatures (3/4, 5/4, 7/8, etc.), key signatures with modal support
- Key Signatures & Modes: Major, minor, and all 7 Greek modes (Dorian, Phrygian, Lydian, etc.)
- Real-time Playback: Cross-platform audio output with concurrent mixing, live volume/pan control
- Live Audio Recording: Record from microphone or line-in to WAV files, then process with full effects pipeline
- Sample Playback: Load and play audio files (MP3, OGG, FLAC, WAV, AAC) with pitch shifting, time dilation and slicing, powered by SIMD with auto caching for efficient sample playback
- GPU Acceleration (optional
gpufeature): GPU compute shader acceleration via wgpu for synthesis and export. Transparent API integration - enable withAudioEngine::new_with_gpu(). Performance scales with GPU hardware; integrated GPUs show minimal improvement while discrete GPUs can provide significant acceleration - WebAssembly Support (optional
webfeature): Full browser support via WebAssembly - synthesis, effects, sample playback, and real-time audio in web applications. Powered by Web Audio API through cpal's wasm-bindgen integration - Streaming Audio: Memory-efficient streaming for long background music and ambience without loading entire files into RAM (native-only)
- Spatial Audio: 3D sound positioning with distance attenuation, azimuth panning, doppler effect, and listener orientation, sound cones, and occlusion for immersive game audio
- Audio Export: WAV (uncompressed), FLAC (lossless ~50-60% compression), STEM export
- MIDI Import/Export: Import Standard MIDI Files and export compositions to MIDI with proper metadata
- Live Coding: Hot-reload system - edit code and hear changes instantly
- The library includes 1547 comprehensive tests and 659 doc tests ensuring reliability and correctness.
Who this is and isn't for:
For:
learners
tinkerers
algorithmic/generative/procedural music
experimental musicians
game jammers and indie devs,
rust coders looking to play with Digital Signal Processing without having to re-implement everything from scratch
Not for:
professional producers
DAW dwellers
DSP engineers
live-repl-first musicians
PROS
rust
music theory integration
batteries included approach
composition and code first environment (rust's ide integration and your choice of ide is everything here)
high CPU performance (100x realtime synthesis uncached, 18.6x cached on complex compositions)
automatic SIMD acceleration for concurrent sample playback (11-17x realtime, 550-1,100+ concurrent samples)
automatic SIMD acceleration for effects (98.8x realtime with all effects stacked)
multi-core parallelism (automatic via Rayon)
optional GPU compute shader acceleration with transparent API
CONS
no gui or graphical elements
no "instant feedback" outside of hot-reloading segments
no external control or input (no midi in, osc or network controls) or hardware control
no plugin system
rust (not as beginner friendly as something like sonic pi)
Installation
Add this to your Cargo.toml:
[]
= "1.0.2"
# Optional: Enable GPU acceleration (requires discrete GPU for best results)
# tunes = { version = "1.0.2", features = ["gpu"] }
# Optional: Enable WebAssembly support for browser-based applications
# tunes = { version = "1.0.2", features = ["web"] }
Platform Requirements
Linux users need ALSA development libraries:
# Debian/Ubuntu
# Fedora/RHEL
macOS and Windows work out of the box with no additional dependencies.
WebAssembly requires wasm-pack for building:
See book/src/advanced/wasm.md for complete WebAssembly setup and usage guide.
Quick Start: Super simple!!
Real-time Playback
use *;
Export to WAV
use *;
Export to FLAC (Lossless Compression)
use *;
Sample Playback (Game Audio - Simple!)
use *;
With GPU Acceleration (requires gpu feature):
use *;
Advanced: Sample Playback in Compositions
For precise timing and mixing with synthesis:
use *;
MIDI Import/Export
use *;
Live Coding (Hot Reload)
# 1. Copy the template
# 2. Start live coding mode
# 3. Edit my_live.rs and save - hear changes instantly!
The live coding system watches your file and automatically:
- ✅ Recompiles when you save
- ✅ Stops the old version
- ✅ Starts playing the new version
- ✅ Shows compilation errors in real-time
Perfect for iterative composition, live performances, and experimentation!
// my_live.rs - edit and save to hear changes!
use *;
Important:
- Use
play_looping()for seamless loops without gaps - Buffer size 4096 works well for most systems (increase to 8192 or 16384 if you hear glitches)
- The live reload system will automatically stop and restart with your changes
Comparison with Other Music Programming Libraries
tunes occupies a unique position in the music programming landscape:
| Feature | SuperCollider | Sonic Pi | Leipzig | Strudel | tunes | Music21 |
|---|---|---|---|---|---|---|
| Type safety | No | No | No (Clojure) | Partial (TS) | Yes (Rust) | No |
| Real-time audio | Yes | Yes | Yes (Overtone) | Yes (Web Audio) | Yes | No |
| Sample playback | Yes | Yes | Yes (Overtone) | Yes | Yes | No |
| WebAssembly support | No | No | No | Yes (JS native) | Yes (Rust) | No |
| GPU acceleration | No | No | No | No | Yes (wgpu) | No |
| SIMD acceleration | Some | No | Via Overtone | No | Yes (11-17x) | No |
| WAV export | Yes (manual) | No | Via Overtone | No (browser) | Yes (easy) | Yes |
| FLAC export | Yes (manual) | No | No | No | Yes (easy) | No |
| MIDI import | Yes | No | No | No | Yes | Yes |
| MIDI export | Yes | No | No | No | Yes | Yes |
| Live coding | Yes | Yes | Partial | Yes | Yes | No |
| Easy to learn | No | Yes | Medium | Yes | Yes | Yes |
| No dependencies | No (needs SC) | No (needs Ruby) | No (Clojure+SC) | No (browser/Node) | Yes | No |
| Algorithmic patterns | Yes | Yes | Yes | Yes | Yes | Yes |
| Music theory | Manual | Manual | Yes | Some | Yes (built-in) | Yes |
| Standalone binary | No | No | No | No | Yes | No |
| Embeddable | No | No | No | No | Yes | No |
When to use tunes
tunes excels at:
- Building Rust applications with music generation (games, art installations, tools)
- Browser-based music applications and interactive web demos
- Algorithmic composition with type-safe APIs
- Offline music generation and batch processing
- Learning music programming without complex setup
- Prototyping musical ideas with immediate feedback
Use alternatives if you need:
- SuperCollider: Extreme synthesis flexibility and live coding ecosystem
- Sonic Pi: Classroom-ready live coding with visual feedback
- Leipzig: Functional composition with Clojure's elegance
- Strudel: Browser-based collaboration and live coding
- Music21: Academic music analysis and score manipulation
tunes' unique position
tunes is the only standalone, embeddable, type-safe music library with synthesis + sample playback that works on both native and WebAssembly. It compiles to a single binary with no runtime dependencies, making it ideal for:
- Rust game developers (Bevy, ggez, etc.)
- Browser-based music applications and web audio tools
- Desktop music applications
- Command-line music tools
- Embedded audio systems
Documentation
Run cargo doc --open to view the full API documentation with detailed examples for each module.
Testing
Examples
Run the included 100+ examples to hear the library in action:
# Sample playback (WAV file loading and playback)
# Export to WAV file
# Synthesis showcase (FM, filters, envelopes)
# Theory and scales
# Effects and effect automation (dynamic parameter changes over time)
# And many more...
Note: Use --release for examples with very complex synthesis to avoid audio underruns.
Documentation Book
📚 Comprehensive Guide Available!
Tunes includes a complete book with tutorials, examples, and in-depth comparisons with other audio libraries.
Find it at: book/ directory in the repository
To read locally:
# Install mdbook if you don't have it
# Serve the book locally
The book includes:
- 🚀 Getting Started - From first sound to algorithmic music
- 🎵 Core Concepts - Architecture, engine, mixer, composition layers
- 🎮 Game Audio Patterns - Samples, concurrent SFX, dynamic music, spatial audio
- 🎹 Synthesis & Effects - FM synthesis, granular, effects chains
- 🔬 Advanced Topics - Generators, transformations, MIDI, optimization
- ⚖️ Comparisons - Clinical, honest comparisons with Kira, Rodio, SoLoud, TidalCycles, Sonic Pi, and more
Not sure if Tunes is right for you? Check the Comparisons page for honest, technical comparisons with other libraries.
Performance & Benchmarks
Tunes is designed for exceptional performance with automatic optimizations:
Measured Performance (i5-6500 @ 3.2GHz, Intel HD 530)
CPU Performance (SIMD + Rayon):
- Uncached synthesis: 100x realtime (192 FM notes)
- Cached complex composition: 18.6x realtime
- SIMD concurrent sample playback: 11-17x realtime (25-100 samples playing simultaneously)
- Conservative concurrent capacity: 550-1,100+ samples in real-world scenarios
- SIMD effects (all stacked): 98.8x realtime
- WAV export: 12.2x realtime (124-second multi-track composition)
GPU Performance (Intel HD 530 integrated):
- Speedup: ~1.1x vs CPU (marginal improvement on integrated graphics)
- WAV export: 12.2x realtime (marginal improvement vs CPU baseline)
- Note: Integrated GPUs show minimal benefit - CPU performance already excellent
Hardware Scaling:
- Integrated GPUs show 1.0-1.2x speedup (marginal improvement)
- Discrete GPUs: Performance scales with compute capacity and memory bandwidth
- Note: Integrated GPU matches full CPU performance while using a fraction of system resources
What This Means
For a 16-bar drum pattern (192 notes, 13.6 seconds of audio):
- CPU renders in: 0.136 seconds (100x realtime)
For game audio with true concurrent samples:
- SIMD handles 50-100 samples playing simultaneously at 11-17x realtime
- Conservative estimate: 550-1,100+ concurrent samples in real-world scenarios
- This is 10-20x more than other libraries claiming "50 or dozens" of concurrent samples!
- Automatic caching and multi-core parallelism optimize performance
For effects processing:
- All SIMD effects stacked: 98.8x realtime (incredibly fast)
Automatic Optimizations
Tunes automatically applies:
- ✅ SIMD vectorization (AVX2/SSE/NEON) - 11-17x realtime concurrent sample playback, 98.8x effects
- ✅ Multi-core parallelism (Rayon) - automatic scaling across CPU cores
- ✅ Block processing (512-sample chunks) - reduces overhead
- ✅ Integer-based routing (Vec-indexed, not HashMap)
- ✅ Sample caching (LRU eviction, Arc-based sharing)
Optional GPU Acceleration
Enable with the gpu feature flag in Cargo.toml:
= { = "1.0.2", = ["gpu"] }
Transparent API (recommended):
// Automatic GPU acceleration for all operations
let engine = new_with_gpu?;
engine.export_wav?; // GPU accelerated
engine.play_mixer_realtime?; // GPU accelerated
Manual control:
let mut mixer = comp.into_mixer;
mixer.enable_gpu; // Explicit GPU enablement
Performance characteristics:
- Integrated GPUs: 1.0-1.2x speedup (minimal benefit)
- Discrete GPUs: Performance scales with hardware capabilities
- Fallback-safe: Automatically uses CPU if GPU unavailable
- Transparent: No API changes required beyond engine initialization
- Cross-platform: wgpu supports Vulkan, Metal, DX12, WebGPU
Run Benchmarks Yourself
# GPU vs CPU comparison with transparent API
# Export performance (CPU and GPU)
# Two-stage GPU pipeline demonstration
# Additional benchmarks to be found in the benches/ directory!
Comparison with Other Rust Audio Libraries
| Library | SIMD | Multi-core | GPU | CPU Performance |
|---|---|---|---|---|
| Tunes | ✅ | ✅ (Rayon) | ✅ (wgpu) | 100x realtime (uncached) |
| Kira | Unknown | No | No | ~10-30x (estimated) |
| Rodio | Unknown | No | No | ~10-20x (estimated) |
| SoLoud (C++) | ✅ | Yes | No | ~10-50x (estimated) |
Tunes is the only Rust audio library with GPU compute shader acceleration via wgpu.
Why This Matters for Games
Traditional approach:
- Pre-record all sound variations → Large asset files
- Limited variations → Repetitive audio
With Tunes:
- Generate sound variations at runtime (procedural synthesis)
- Each variation unique
- Zero disk space for variations
- 100x realtime synthesis on CPU (fast enough for most games)
- Can handle 550-1,100+ concurrent samples at realtime (10-20x more than other libraries!)
Example: Procedural game audio
- Synthesize unique sounds per enemy type
- Dynamic music that responds to gameplay
- Algorithmic sound effects with variations
- Result: Unique audio without large asset files
License
MIT OR Apache-2.0
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.