Skip to content

Commit e91a5ec

Browse files
committed
add docs
1 parent 3b4ab7a commit e91a5ec

2 files changed

Lines changed: 111 additions & 1 deletion

File tree

‎README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# rhythms
2+
3+
A WIP prototype for rhythmic pattern generation in Rust with `no_std` support.

‎src/lib.rs‎

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
//! A WIP prototype for rhythmic pattern generation in Rust with `no_std` support.
2+
13
#![no_std]
24

35
use smallvec::SmallVec;
46

7+
/// The main building block for pattern generation
58
#[derive(Debug, Clone)]
69
pub struct Pattern {
710
steps: SmallVec<[bool; 64]>,
@@ -10,14 +13,39 @@ pub struct Pattern {
1013
}
1114

1215
impl Pattern {
13-
16+
/// Returns a pattern with given length, number of pulses and rotation
17+
///
18+
/// # Arguments
19+
///
20+
/// * `length` - Total number of steps
21+
/// * `pulses` - The number of pulses
22+
/// * `rotation` - Number of rotation steps. Polarity represents direction
23+
///
24+
/// # Examples
25+
///
26+
/// ```
27+
/// use rhythms::Pattern;
28+
/// let pattern = Pattern::new(8, 4, -1);
29+
/// ```
1430
pub fn new(length: usize, pulses: usize, rotation: isize) -> Self {
1531
let mut pattern = Pattern::with_length(length);
1632
pattern.pulses(pulses);
1733
pattern.rotate(rotation);
1834
pattern
1935
}
2036

37+
/// Returns a pattern with given length
38+
///
39+
/// # Arguments
40+
///
41+
/// * `length` - Total number of steps
42+
///
43+
/// # Examples
44+
///
45+
/// ```
46+
/// use rhythms::Pattern;
47+
/// let pattern = Pattern::with_length(8);
48+
/// ```
2149
pub fn with_length(length: usize) -> Self {
2250
let mut steps = SmallVec::new();
2351
for _ in 0..length {
@@ -30,6 +58,18 @@ impl Pattern {
3058
}
3159
}
3260

61+
/// Returns a pattern based on a boolean slice
62+
///
63+
/// # Arguments
64+
///
65+
/// * `slice` - A boolean slice holding the initial pattern
66+
///
67+
/// # Examples
68+
///
69+
/// ```
70+
/// use rhythms::Pattern;
71+
/// let pattern = Pattern::from_slice(&[true, false, true, false]);
72+
/// ```
3373
pub fn from_slice(slice: &[bool]) -> Self {
3474
Self {
3575
steps: SmallVec::from_slice(slice),
@@ -38,6 +78,23 @@ impl Pattern {
3878
}
3979
}
4080

81+
/// Updates the current pattern with a number of pulses, using an abstraction based on
82+
/// Bjorklund's Euclidean algorithm.
83+
///
84+
/// # Arguments
85+
///
86+
/// * `pulses` - Total number of pulses
87+
///
88+
/// # Examples
89+
///
90+
/// ```
91+
/// use rhythms::Pattern;
92+
/// let mut pattern = Pattern::with_length(8);
93+
/// pattern.pulses(2);
94+
/// // or
95+
/// let mut pattern = Pattern::new(8, 4, 0);
96+
/// pattern.pulses(2);
97+
/// ```
4198
pub fn pulses(&mut self, pulses: usize) -> &mut Self {
4299
if pulses == 0 {
43100
return self
@@ -66,6 +123,24 @@ impl Pattern {
66123
self
67124
}
68125

126+
/// Rotates the current pattern
127+
///
128+
/// # Arguments
129+
///
130+
/// * `rotation` - Number of rotation steps. Polarity represents direction
131+
///
132+
/// # Examples
133+
///
134+
/// ```
135+
/// use rhythms::Pattern;
136+
/// let mut pattern = Pattern::with_length(8);
137+
/// pattern.pulses(2);
138+
/// pattern.rotate(-1);
139+
/// // or
140+
/// let mut pattern = Pattern::new(8, 4, 0);
141+
/// pattern.pulses(2);
142+
/// pattern.rotate(3);
143+
/// ```
69144
pub fn rotate(&mut self, rotation: isize) -> &mut Self {
70145
if rotation.is_positive() {
71146
self.steps.rotate_right(rotation as usize);
@@ -75,10 +150,33 @@ impl Pattern {
75150
self
76151
}
77152

153+
/// Returns a boolean slice reprensenting the pattern
154+
///
155+
/// # Examples
156+
///
157+
/// ```
158+
/// use rhythms::Pattern;
159+
/// let pattern = Pattern::new(8, 2, 0);
160+
/// let slice = pattern.as_slice();
161+
/// ```
78162
pub fn as_slice(&self) -> &[bool] {
79163
self.steps.as_slice()
80164
}
81165

166+
/// Returns the state of a step
167+
///
168+
/// # Arguments
169+
///
170+
/// * `step` - Step identifiyer. Range starts at 0
171+
///
172+
/// # Examples
173+
///
174+
/// ```
175+
/// use rhythms::Pattern;
176+
/// let mut pattern = Pattern::new(8, 4, 0);
177+
/// let first_step = pattern.step(0);
178+
/// let third_step = pattern.step(2);
179+
/// ```
82180
pub fn step(&self, step: usize) -> Option<bool> {
83181
if step < self.steps.len() {
84182
Some(self.steps[step])
@@ -87,6 +185,15 @@ impl Pattern {
87185
}
88186
}
89187

188+
/// Returns length of current pattern
189+
///
190+
/// # Examples
191+
///
192+
/// ```
193+
/// use rhythms::Pattern;
194+
/// let pattern = Pattern::new(8, 2, 0);
195+
/// let length = pattern.len();
196+
/// ```
90197
pub fn len(&self) -> usize {
91198
self.steps.len()
92199
}

0 commit comments

Comments
 (0)