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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
//! Hénon Map - 2D chaotic attractor
//!
//! The Hénon map is a discrete-time dynamical system that exhibits chaotic behavior.
//! Unlike 1D maps like the logistic map, it uses two coupled equations creating a
//! 2D phase space with a strange attractor.
//!
//! The equations are:
//! - x_n+1 = 1 - a * x_n^2 + y_n
//! - y_n+1 = b * x_n
//!
//! Classic parameters (a=1.4, b=0.3) produce the famous Hénon attractor.
//!
//! # Musical Application
//! - Creates complex, non-repetitive melodies with structure
//! - Two output streams (x and y) can drive different musical parameters
//! - Natural variation between ordered and chaotic regions
//! - Excellent for evolving ambient textures
//! - Pair with `normalize()` to map to frequency ranges
//! - Use with `map_to_scale()` for melodic sequences
//!
//! # Parameters
//! - `a`: Controls the nonlinearity (typical: 1.4, range: 0.0-1.5)
//! - `b`: Controls the coupling (typical: 0.3, range: 0.0-0.4)
//! - Higher `a` values increase chaos
//!
//! # Example
//! ```
//! use tunes::sequences::henon_map;
//!
//! // Generate 64 points from the classic Hénon attractor
//! let (x_vals, y_vals) = henon_map::generate(1.4, 0.3, 0.1, 0.1, 64);
//!
//! // Use x values for melody (map to frequency range manually)
//! let melody: Vec<f32> = x_vals.iter()
//! .map(|&x| 220.0 + (x + 1.5) / 3.0 * (880.0 - 220.0))
//! .collect();
//!
//! // Use y values for rhythm or timbre changes
//! let dynamics: Vec<f32> = y_vals.iter()
//! .map(|&y| 0.3 + (y + 1.5) / 3.0 * (1.0 - 0.3))
//! .collect();
//! ```
/// Generate a sequence using the Hénon map
///
/// Returns two sequences (x_values, y_values) from iterating the 2D Hénon map.
/// Both sequences exhibit chaotic behavior with the classic parameters.
///
/// # Arguments
/// * `a` - Nonlinearity parameter (typical: 1.4, try range 0.8-1.5)
/// * `b` - Coupling parameter (typical: 0.3, try range 0.2-0.4)
/// * `x0` - Initial x value (typical: 0.1)
/// * `y0` - Initial y value (typical: 0.1)
/// * `n` - Number of iterations to generate
///
/// # Returns
/// Tuple of (x_values, y_values), each containing n points
///
/// # Typical Parameters
/// - **a = 1.4, b = 0.3**: Classic Hénon attractor (strongly recommended)
/// - **a = 1.2, b = 0.25**: Less chaotic, more periodic
/// - **a = 1.3, b = 0.3**: Moderate chaos
/// - **x0, y0**: Usually 0.1, 0.1 (avoid 0.0, 0.0)
///
/// # Recipe: Dual-Stream Melody
/// ```
/// use tunes::prelude::*;
/// use tunes::sequences;
///
/// let mut comp = Composition::new(Tempo::new(130.0));
///
/// // Generate classic Hénon attractor
/// let (x_vals, y_vals) = sequences::henon_map::generate(1.4, 0.3, 0.1, 0.1, 32);
///
/// // Use x for melody
/// let melody = sequences::map_to_scale_f32(
/// &x_vals,
/// &sequences::Scale::minor_pentatonic(),
/// D4,
/// 2
/// );
///
/// // Use y for counter-melody (different scale position)
/// let counter = sequences::map_to_scale_f32(
/// &y_vals,
/// &sequences::Scale::minor_pentatonic(),
/// A4,
/// 2
/// );
///
/// comp.instrument("henon_lead", &Instrument::synth_lead())
/// .notes(&melody, 0.25);
///
/// comp.instrument("henon_counter", &Instrument::pluck())
/// .notes(&counter, 0.25);
/// ```
///
/// # Example
/// ```
/// use tunes::sequences::henon_map;
///
/// // Classic Hénon attractor parameters
/// let (x_vals, y_vals) = henon_map::generate(1.4, 0.3, 0.1, 0.1, 100);
/// assert_eq!(x_vals.len(), 100);
/// assert_eq!(y_vals.len(), 100);
///
/// // Different parameters create different patterns
/// let (x2, y2) = henon_map::generate(1.2, 0.25, 0.0, 0.0, 50);
/// ```
/// Generate only the x-coordinate sequence from the Hénon map
///
/// Convenience function when you only need one dimension of output.
///
/// # Arguments
/// * `a` - Nonlinearity parameter
/// * `b` - Coupling parameter
/// * `x0` - Initial x value
/// * `y0` - Initial y value
/// * `n` - Number of iterations
///
/// # Returns
/// Vector of x values
///
/// # Example
/// ```
/// use tunes::sequences::henon_x;
///
/// let melody = henon_x(1.4, 0.3, 0.1, 0.1, 32);
/// assert_eq!(melody.len(), 32);
/// ```
/// Generate only the y-coordinate sequence from the Hénon map
///
/// Convenience function when you only need one dimension of output.
///
/// # Arguments
/// * `a` - Nonlinearity parameter
/// * `b` - Coupling parameter
/// * `x0` - Initial x value
/// * `y0` - Initial y value
/// * `n` - Number of iterations
///
/// # Returns
/// Vector of y values
///
/// # Example
/// ```
/// use tunes::sequences::henon_y;
///
/// let rhythm = henon_y(1.4, 0.3, 0.1, 0.1, 32);
/// assert_eq!(rhythm.len(), 32);
/// ```
// ============================================================================
// PRESETS - Ready-to-use Hénon map configurations
// ============================================================================
/// Classic Hénon attractor (a=1.4, b=0.3)
///
/// The famous Hénon attractor parameters discovered by Michel Hénon.
/// Produces the classic chaotic butterfly-like pattern.
///
/// # Arguments
/// * `n` - Number of points to generate
///
/// # Returns
/// Tuple of (x_values, y_values)
///
/// # Example
/// ```
/// use tunes::sequences::henon_classic;
///
/// let (x, y) = henon_classic(64);
/// assert_eq!(x.len(), 64);
/// assert_eq!(y.len(), 64);
/// ```
/// Classic Hénon attractor - x values only
///
/// Convenience function returning only the x-coordinate from the classic attractor.
///
/// # Arguments
/// * `n` - Number of values to generate
///
/// # Example
/// ```
/// use tunes::sequences::henon_classic_x;
///
/// let melody = henon_classic_x(32);
/// assert_eq!(melody.len(), 32);
/// ```
/// Classic Hénon attractor - y values only
///
/// Convenience function returning only the y-coordinate from the classic attractor.
///
/// # Arguments
/// * `n` - Number of values to generate
///
/// # Example
/// ```
/// use tunes::sequences::henon_classic_y;
///
/// let rhythm = henon_classic_y(32);
/// assert_eq!(rhythm.len(), 32);
/// ```
/// Mild Hénon map with less chaotic behavior
///
/// Uses a=1.2, b=0.25 for gentler, more periodic patterns.
/// Good for subtle melodic movement with some structure.
///
/// # Arguments
/// * `n` - Number of points to generate
///
/// # Returns
/// Tuple of (x_values, y_values)
///
/// # Example
/// ```
/// use tunes::sequences::henon_mild;
///
/// let (x, y) = henon_mild(32);
/// assert_eq!(x.len(), 32);
/// ```
/// Intense Hénon map with stronger chaos
///
/// Uses a=1.35, b=0.35 for more intense chaotic behavior.
/// Creates more dramatic, unpredictable patterns.
///
/// # Arguments
/// * `n` - Number of points to generate
///
/// # Returns
/// Tuple of (x_values, y_values)
///
/// # Example
/// ```
/// use tunes::sequences::henon_intense;
///
/// let (x, y) = henon_intense(64);
/// assert_eq!(x.len(), 64);
/// ```