1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/// Generate rhythm patterns using the Circle Map (Arnol'd Tongue)
///
/// The circle map is a one-dimensional chaotic map specifically designed to model
/// phase-locking phenomena in oscillators. It's perfect for generating rhythms that
/// transition smoothly between regular locked patterns and chaotic variations.
///
/// The map is defined as:
/// ```text
/// θ_{n+1} = (θ_n + Ω - (K/2π)sin(2πθ_n)) mod 1
/// ```
///
/// Where:
/// - θ (theta) is the angle/phase on the unit circle [0, 1)
/// - Ω (omega) is the driving frequency ratio
/// - K is the coupling strength (0 = pure rotation, higher = more locking)
///
/// # The Arnol'd Tongue
///
/// The circle map exhibits "mode-locking" where for certain (Ω, K) combinations,
/// the system locks into rational ratios. These regions form triangular wedges
/// called "Arnol'd tongues" in parameter space.
///
/// Key behaviors:
/// - **K = 0**: Pure rotation by Ω (perfectly periodic)
/// - **0 < K < 1**: Quasi-periodic, smooth rotation with slight perturbation
/// - **K = 1**: Critical point, mode-locking boundaries
/// - **K > 1**: Strong mode-locking, chaotic between locked regions
///
/// # Arguments
/// * `omega` - Driving frequency ratio (0.0 to 1.0). Try 0.3, 0.5, 0.618 (golden ratio)
/// * `k` - Coupling strength (0.0 to 2.0). 0=rotation, 1=critical, >1=strong locking
/// * `initial` - Starting phase angle (0.0 to 1.0)
/// * `iterations` - Number of iterations to generate
///
/// # Returns
/// Vec of phase angles in [0, 1) representing positions on the unit circle
///
/// # Examples
/// ```
/// use tunes::sequences;
///
/// // Pure rotation (K=0) - perfectly periodic
/// let rotation = sequences::circle_map::generate(0.25, 0.0, 0.5, 16);
/// // Creates 1:4 rhythm (hits every 4 steps)
///
/// // Critical coupling (K=1) - interesting mode-locking
/// let critical = sequences::circle_map::generate(0.333, 1.0, 0.5, 24);
/// // Creates 1:3 patterns with slight variation
///
/// // High coupling (K=2) - complex rhythms
/// let complex = sequences::circle_map::generate(0.618, 2.0, 0.0, 32);
/// // Golden ratio creates non-repeating but structured rhythms
///
/// // Convert to rhythm hits (trigger when crossing threshold)
/// let hits = sequences::circle_map_to_hits(0.4, 1.5, 0.0, 16, 0.5);
/// # use tunes::prelude::*;
/// # let mut comp = Composition::new(Tempo::new(140.0));
/// comp.track("circle_rhythm")
/// .drum_grid(16, 0.25, |g| g
/// .sound(DrumType::Kick, &hits));
/// ```
///
/// # Musical Applications
/// - **Polyrhythmic patterns**: Omega as rational fractions (1/3, 2/5, etc.)
/// - **Metric modulation**: Smoothly transition between time feels
/// - **Phasing effects**: Two circle maps slightly out of sync
/// - **Groove generation**: K controls "humanization" vs rigidity
/// - **Hocket rhythms**: Multiple voices with complementary circle maps
/// - **Rhythmic fractals**: Self-similar patterns across time scales
///
/// # Parameter Tips
/// - **Omega = p/q**: Creates p:q polyrhythm when K is small
/// - **Omega = φ (0.618)**: Golden ratio, maximally irrational (never locks)
/// - **K = 0**: Perfect click track
/// - **K ≈ 0.5**: Slight groove variation
/// - **K ≈ 1.0**: On edge of chaos, interesting "almost locked" feel
/// - **K ≈ 2.0**: Complex but deterministic chaos
///
/// # Advanced: Finding Mode-Locked Regions
/// For a given omega, mode-locking occurs at specific K values.
/// The tongue for p:q ratio is centered approximately at K = 1.
/// Convert circle map phases to rhythm hits using threshold crossing
///
/// Generates a boolean rhythm pattern by triggering whenever the phase
/// crosses a threshold value. This is the most common way to convert
/// continuous circle map output to discrete rhythm events.
///
/// # Arguments
/// * `omega` - Driving frequency ratio
/// * `k` - Coupling strength
/// * `initial` - Starting phase
/// * `iterations` - Number of steps
/// * `threshold` - Trigger threshold (0.0 to 1.0, typically 0.5)
///
/// # Returns
/// Vec of step indices where hits occur (empty if no hits)
///
/// # Examples
/// ```
/// use tunes::sequences;
///
/// // 3:8 polyrhythm with slight variation
/// let hits = sequences::circle_map_to_hits(0.375, 1.0, 0.0, 16, 0.5);
/// // Returns indices like [0, 3, 5, 8, 11, 13] (approximately)
///
/// // Golden ratio rhythm (never repeats)
/// let golden_hits = sequences::circle_map_to_hits(0.618, 1.5, 0.0, 32, 0.5);
/// ```
/// Generate complementary circle map rhythm (hocket pattern)
///
/// Creates a second rhythm that fills the gaps of the first rhythm,
/// perfect for creating call-and-response or hocket patterns.
///
/// # Arguments
/// * `omega` - Driving frequency ratio
/// * `k` - Coupling strength
/// * `initial` - Starting phase
/// * `iterations` - Number of steps
/// * `threshold` - Trigger threshold
///
/// # Returns
/// Tuple of (primary_hits, complement_hits)
///
/// # Examples
/// ```
/// use tunes::sequences;
///
/// // Create kick/snare hocket
/// let (kick_hits, snare_hits) = sequences::circle_map_hocket(0.4, 1.5, 0.0, 16, 0.5);
///
/// # use tunes::prelude::*;
/// # let mut comp = Composition::new(Tempo::new(130.0));
/// comp.track("hocket")
/// .drum_grid(16, 0.25, |g| g
/// .sound(DrumType::Kick, &kick_hits)
/// .sound(DrumType::Snare, &snare_hits));
/// ```
// ========== PRESETS ==========
/// Ordered rhythm - low chaos (k=0.5), 16 iterations
/// Edge of chaos - medium chaos (k=1.0), 24 iterations
/// Chaotic rhythm - high chaos (k=2.0), 32 iterations
/// Unstable - very high chaos (k=3.0), 24 iterations