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
/// Generate sequences from the Lorenz attractor
///
/// The Lorenz system is a famous 3D continuous chaotic system that creates the iconic
/// "butterfly" pattern. Unlike discrete maps (logistic, tent, etc.), the Lorenz attractor
/// produces smooth, flowing trajectories perfect for melodic lines and parameter automation.
///
/// The system is defined by three coupled differential equations:
/// ```text
/// dx/dt = σ(y - x)
/// dy/dt = x(ρ - z) - y
/// dz/dt = xy - βz
/// ```
///
/// Classic parameters (Lorenz 1963):
/// - σ (sigma) = 10: Prandtl number
/// - ρ (rho) = 28: Rayleigh number
/// - β (beta) = 8/3: Geometric factor
///
/// These create the iconic butterfly attractor with two lobes.
///
/// # Arguments
/// * `sigma` - Prandtl number (typical: 10.0)
/// * `rho` - Rayleigh number (typical: 28.0, chaos at ρ > 24.74)
/// * `beta` - Geometric factor (typical: 8.0/3.0 ≈ 2.667)
/// * `initial` - Starting point (x, y, z). Try (1.0, 1.0, 1.0)
/// * `dt` - Time step for integration (typical: 0.01)
/// * `steps` - Number of points to generate
///
/// # Returns
/// Vec of (x, y, z) coordinates tracing the attractor's path
///
/// # Examples
/// ```
/// use tunes::sequences;
///
/// // Classic Lorenz butterfly
/// let path = sequences::lorenz_attractor::generate(10.0, 28.0, 8.0/3.0, (1.0, 1.0, 1.0), 0.01, 100);
///
/// // Extract x coordinates for melody and normalize to frequency range
/// let x_vals: Vec<f32> = path.iter().map(|(x, _, _)| *x).collect();
/// let melody = sequences::normalize_f32(&x_vals, 220.0, 880.0);
///
/// // Use y for volume automation
/// let y_vals: Vec<f32> = path.iter().map(|(_, y, _)| *y).collect();
/// let volumes_norm = sequences::normalize_f32(&y_vals, 0.3, 1.0);
///
/// // Create evolving melody with automation
/// # use tunes::prelude::*;
/// # let mut comp = Composition::new(Tempo::new(120.0));
/// for i in 0..melody.len().min(10) {
/// comp.track("lorenz")
/// .at(i as f32 * 0.25)
/// .volume(volumes_norm[i])
/// .note(&[melody[i]], 0.2);
/// }
/// ```
///
/// # Musical Applications
/// - **Smooth melodies**: Continuous flow without jumps
/// - **Parameter automation**: X/Y/Z → pitch/volume/filter
/// - **Binaural effects**: X → left ear, Y → right ear phase
/// - **Ambient textures**: Slow-moving, never-repeating patterns
/// - **Modulation sources**: Drive LFOs, tremolo, vibrato depth
/// - **Spatial movement**: Map to stereo pan + reverb send
///
/// # Typical Parameters
/// - **Classic chaos**: σ=10.0, ρ=28.0, β=8.0/3.0, dt=0.01 (strongly recommended)
/// - **Mild chaos**: σ=10.0, ρ=25.0, β=8.0/3.0, dt=0.01
/// - **Complex**: σ=10.0, ρ=35.0, β=8.0/3.0, dt=0.01
/// - **initial**: (1.0, 1.0, 1.0) works well, try (0.1, 0.0, 0.0) for different path
///
/// # Recipe: Flowing Melody in Scale
/// ```
/// use tunes::prelude::*;
/// use tunes::sequences;
///
/// let mut comp = Composition::new(Tempo::new(100.0));
///
/// // Generate butterfly attractor (skip first 100 for transient)
/// let path = sequences::lorenz_butterfly(132); // 32 notes + 100 skip
/// let path_stable = &path[100..]; // Use settled portion
///
/// // Extract x coordinates
/// let x_vals: Vec<f32> = path_stable.iter().map(|(x, _, _)| *x).collect();
///
/// // Map to minor pentatonic
/// let melody = sequences::map_to_scale_f32(
/// &x_vals,
/// &sequences::Scale::minor_pentatonic(),
/// A4,
/// 2
/// );
///
/// comp.instrument("lorenz_melody", &Instrument::synth_lead())
/// .reverb(Reverb::new(0.6, 0.6, 0.5))
/// .notes(&melody, 0.3);
/// ```
///
/// # Parameter Exploration
/// - **σ < 10**: Less chaotic, more predictable orbits
/// - **10 < ρ < 24.74**: Stable fixed points
/// - **ρ = 28**: Classic butterfly chaos
/// - **ρ > 28**: More complex attractors
/// - **β variations**: Change lobe symmetry
///
/// # Tips
/// - Use dt=0.01 for smooth paths (smaller = smoother but slower)
/// - Discard first ~100 steps (transient before settling on attractor)
/// - Normalize coordinates to musical ranges (they span roughly -20 to 20)
/// - Try different initial conditions for different trajectories
/// Generate a Lorenz attractor with classic parameters
///
/// Convenience function using the standard Lorenz parameters (σ=10, ρ=28, β=8/3)
/// that produce the iconic butterfly attractor.
///
/// # Arguments
/// * `steps` - Number of points to generate
///
/// # Returns
/// Vec of (x, y, z) coordinates with first 100 transient steps removed
///
/// # Examples
/// ```
/// use tunes::sequences;
///
/// // Get 500 points on the butterfly attractor
/// let butterfly = sequences::lorenz_butterfly(500);
///
/// // X coordinates for melody (range approximately -20 to 20)
/// let x_vals: Vec<f32> = butterfly.iter().map(|(x, _, _)| *x).collect();
/// ```
// ========== PRESETS ==========
/// Classic Lorenz attractor (sigma=10, rho=28, beta=8/3)
/// Extended Lorenz - more points for detailed structure
/// Fast Lorenz - larger time step