| Crates.io | tunes |
| lib.rs | tunes |
| version | 1.1.0 |
| created_at | 2025-11-02 23:57:48.593415+00 |
| updated_at | 2025-11-28 02:22:59.785884+00 |
| description | A music composition, synthesis, and audio generation library |
| homepage | https://github.com/sqrew/tunes |
| repository | https://github.com/sqrew/tunes |
| max_upload_size | |
| id | 1913730 |
| size | 4,490,595 |
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 20.0x realtime (cached complex compositions) on decade old hardware. SIMD sample playback: 1000+ with true concurrent playback (all samples playing simultaneously)** - can handle 500-1500+ concurrent samples in real-world scenarios. Optional GPU acceleration available via
gpufeature - provides minimal benefit on integrated GPUs (~1.0x on i5 6500) but scales with discrete GPU hardware.
gpu feature): GPU compute shader acceleration via wgpu for synthesis and export. Transparent API integration - enable with AudioEngine::new_with_gpu(). Performance scales with GPU hardware; integrated GPUs show minimal improvement while discrete GPUs can provide significant accelerationweb feature): 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 integrationThe library includes 1547 comprehensive tests and 659 doc tests ensuring reliability and correctness.
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
rust making music is cool
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 and automatic simd (100x realtime synthesis uncached, 20.0x cached on complex compositions)
multi-core parallelism (automatic via Rayon)
optional GPU compute shader acceleration with transparent API
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)
Add this to your Cargo.toml:
[dependencies]
tunes = "1.1.0"
# Optional: Enable GPU acceleration (requires discrete GPU for best results)
# tunes = { version = "1.1.0", features = ["gpu"] }
# Optional: Enable WebAssembly support for browser-based applications
# tunes = { version = "1.1.0", features = ["web"] }
Linux users need ALSA development libraries:
# Debian/Ubuntu
sudo apt install libasound2-dev
# Fedora/RHEL
sudo dnf install alsa-lib-devel
macOS and Windows work out of the box with no additional dependencies.
WebAssembly requires wasm-pack for building:
cargo install wasm-pack
wasm-pack build --target web --features web
See book/src/advanced/wasm.md for complete WebAssembly setup and usage guide.
use tunes::prelude::*;
fn main() -> Result<(), anyhow::Error> {
let engine = AudioEngine::new()?;
let mut comp = Composition::new(Tempo::new(120.0));
let eighth = comp.tempo().eighth_note(); //getting this value to use in the next example
// This is where you can do everything.
// Notes and chords can be input as floats with frequencies in hz or using or by prelude constants
// Durations can be input as a duration of seconds as a float or using durations inferred by tempo
comp.instrument("piano", &Instrument::electric_piano())
.note(&[C4], 0.5) //plays a c4 for half a second
.note(&[280.0], eighth); //plays 280.0 hz note for half a second
//continue chaining methods after the second note if you want.
engine.play_mixer(&comp.into_mixer())?;
Ok(())
}
use tunes::prelude::*;
fn main() -> Result<(), anyhow::Error> {
let engine = AudioEngine::new()?;
// That's it! Play samples with automatic caching and SIMD acceleration
engine.play_sample("explosion.wav"); // Loads once, caches, plays with SIMD
engine.play_sample("footstep.wav"); // Loads once, caches
engine.play_sample("footstep.wav"); // Instant! Uses cache, SIMD playback
play_sample!(engine, "sample.wav"); // Works the same but has compile time path resolution and runtime startup file validation
// All samples play concurrently with automatic mixing
Ok(())
}
use tunes::prelude::*;
fn main() -> Result<(), anyhow::Error> {
let engine = AudioEngine::new()?;
let mut comp = Composition::new(Tempo::new(120.0));
// Create a melody with instruments and effects
comp.instrument("lead", &Instrument::synth_lead())
.filter(Filter::low_pass(1200.0, 0.6))
.notes(&[C4, E4, G4, C5], 0.5);
// Export to WAV (sample rate inferred from engine)
let mut mixer = comp.into_mixer();
engine.export_wav(&mut mixer, "my_song.wav")?;
// Or export to FLAC (lossless compression)
engine.export_flac(&mut mixer, "my_song.flac")?;
Ok(())
}
use tunes::prelude::*;
fn main() -> Result<(), anyhow::Error> {
// Export: Create and export a composition to MIDI
let mut comp = Composition::new(Tempo::new(120.0));
comp.instrument("melody", &Instrument::synth_lead())
.notes(&[C4, E4, G4, C5], 0.5);
comp.track("drums")
.drum_grid(16, 0.125)
.kick(&[0, 4, 8, 12])
.snare(&[4, 12]);
let mixer = comp.into_mixer();
mixer.export_midi("song.mid")?;
// Import: Load a MIDI file and render to audio
let engine = AudioEngine::new()?;
let mut imported = Mixer::import_midi("song.mid")?;
engine.export_wav(&mut imported, "output.wav")?;
// Or play it directly
engine.play_mixer(&imported)?;
Ok(())
}
# 1. Copy the template
cp templates/live_template.rs my_live.rs
# 2. Start live coding mode
cargo run --bin tunes-live -- my_live.rs
# 3. Edit my_live.rs and save - hear changes instantly!
The live coding system watches your file and automatically:
Perfect for iterative composition and experimentation!
// my_live.rs - edit and save to hear changes!
use tunes::prelude::*;
fn main() -> anyhow::Result<()> {
let mut comp = Composition::new(Tempo::new(140.0));
comp.track("drums")
.drum_grid(16, 0.125)
.kick(&[0, 4, 8, 12]);
// Try changing notes here and saving!
comp.instrument("lead", &Instrument::synth_lead())
.notes(&[C4, E4, G4], 0.25);
let mixer = comp.into_mixer();
// 4096 samples = ~93ms latency - good balance for live coding
let engine = AudioEngine::with_buffer_size(4096)?;
// Start looping playback
let loop_id = engine.play_looping(&mixer)?;
// Keep program running (live reload will restart)
loop {
std::thread::sleep(std::time::Duration::from_millis(100));
}
}
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
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 | 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/export | Yes | No | No | No | Yes | Yes |
| No dependencies | No (needs SC) | No (needs Ruby) | No (Clojure+SC) | No (browser/Node) | Yes | No |
| 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 |
tunestunes excels at:
Use alternatives if you need:
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:
Run cargo doc --open to view the full API documentation with detailed examples for each module.
cargo test
Run the included 100+ examples to hear the library in action:
# Sample playback (WAV file loading and playback)
cargo run --release --example sample_playback_demo
# Export to WAV file
cargo run --release --example wav_export_demo
# Synthesis showcase (FM, filters, envelopes)
cargo run --release --example synthesis_demo
# Theory and scales
cargo run --example theory_demo
# Effects and effect automation (dynamic parameter changes over time)
cargo run --example effects_showcase
cargo run --example automation_demo
# And many more...
cargo run -- example example-name-here
Note: Use --release for examples with very complex synthesis to avoid audio underruns.
📚 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
cargo install mdbook
# Serve the book locally
cd book
mdbook serve --open
The book includes:
Not sure if Tunes is right for you? Check the Comparisons page for honest, technical comparisons with other libraries.
Tunes is designed for exceptional performance with automatic optimizations:
CPU Performance (SIMD + Rayon):
For game audio with true concurrent samples:
SIMD handles 50-100 samples playing simultaneously at 15x realtime
Conservative estimate: 1000+ 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
Tunes automatically applies:
Enable with the gpu feature flag in Cargo.toml:
tunes = { version = "1.1.0", features = ["gpu"] }
Automatic GPU acceleration (recommended):
// GPU automatically enabled for all export/render operations
let engine = AudioEngine::new_with_gpu()?;
let mut mixer = comp.into_mixer();
engine.export_wav(&mut mixer, "output.wav")?; // GPU accelerated
engine.export_flac(&mut mixer, "output.flac")?; // GPU accelerated
engine.play_mixer_realtime(&mixer)?; // GPU accelerated
Manual GPU control (per-mixer):
let engine = AudioEngine::new()?; // CPU-only engine
let mut mixer = comp.into_mixer();
mixer.enable_gpu(); // Enable GPU for this mixer
engine.export_wav(&mut mixer, "output.wav")?; // Uses GPU
mixer.disable_gpu(); // Disable GPU for this mixer
engine.export_wav(&mut mixer, "output2.wav")?; // Uses CPU
Performance characteristics:
GPU Performance (Intel HD 530 integrated (decade old integrated gpu)):
# CPU performance on concurrent sample playback
cargo bench --bench realistic_game_audio
# Export performance (CPU and GPU)
cargo bench --bench export_speed --features gpu
# Additional benchmarks to be found in the benches/ directory!
MIT OR Apache-2.0
Contributions are welcome! Please feel free to submit a Pull Request.