euclidean-rhythm
A Rust library for generating Euclidean rhythms using Bjorklund's algorithm.
Euclidean rhythms are mathematical patterns that distribute pulses as evenly as possible across a given number of steps. These patterns appear in traditional music from around the world and are popular in electronic music production.
Features
- Generate Euclidean rhythm patterns with any number of steps and pulses
- Rotate patterns to different starting positions
- Convert patterns to string representations for visualization
- Fast, lightweight implementation with no dependencies
- Well-tested with property-based tests
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Usage
Basic Pattern Generation
use euclidean;
// Generate a tresillo rhythm (3 pulses in 8 steps)
let pattern = euclidean;
// Result: [true, false, false, true, false, false, true, false]
Pattern Visualization
use ;
let pattern = euclidean;
println!;
// Output: x..x..x.
// Or use different characters
println!;
// Output: 10010010
Pattern Rotation
use euclidean;
// Rotate the pattern by 2 steps
let pattern = euclidean;
// Result: [false, true, false, false, true, false, true, false]
// Rotation wraps around (10 % 8 = 2)
let pattern2 = euclidean;
assert_eq!;
Musical Examples
The library generates well-known rhythmic patterns from various musical traditions:
use ;
// Tresillo - Common in Latin music (Cuba, Haiti)
let tresillo = euclidean;
println!;
// Output: x..x..x.
// West African bell pattern
let bell = euclidean;
println!;
// Output: x.xx.xx.
// Persian rhythm
let persian = euclidean;
println!;
// Output: x..x.x..x.x.
// Bossa Nova rhythm
let bossa = euclidean;
println!;
// Output: x..x.x.x..x.x.x.
Algorithm
This library implements Bjorklund's algorithm, which generates maximally even distributions of pulses. The algorithm works by:
- Starting with separate groups of pulses and rests
- Repeatedly pairing groups together
- Concatenating pairs until a single pattern emerges
This produces rhythms where pulses are distributed as evenly as possible, which is why these patterns sound natural and appear in traditional music worldwide.
API Reference
euclidean(steps: u8, pulses: u8, rotation: u8) -> Vec<bool>
Generates a Euclidean rhythm pattern.
Parameters:
steps- Total number of steps in the pattern (must be > 0)pulses- Number of pulses to distribute (must be ≤ steps)rotation- Number of positions to rotate the pattern (wraps with modulo)
Returns:
- A
Vec<bool>wheretruerepresents a pulse andfalserepresents a rest
Panics:
- Panics if
steps == 0orpulses > steps
pattern_to_string(pattern: &[bool], pulse_char: char, rest_char: char) -> String
Converts a boolean pattern to a string representation.
Parameters:
pattern- The pattern to convertpulse_char- Character to represent pulses (true values)rest_char- Character to represent rests (false values)
Returns:
- A
Stringwith the pattern visualized using the specified characters
Testing
Run the test suite:
The library includes:
- Unit tests for known musical patterns
- Property-based tests (528 test cases)
- Edge case tests (empty patterns, full patterns, rotation)
- Panic tests for invalid inputs
References
- The Euclidean Algorithm Generates Traditional Musical Rhythms by Godfried Toussaint
- Bjorklund's Algorithm
License
This project is released into the public domain. See LICENSE for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.