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
/// Generate logistic map sequence (chaos theory)
///
/// The logistic map is a simple equation that exhibits complex chaotic behavior:
/// `x(n+1) = r * x(n) * (1 - x(n))`
///
/// Originally used to model population growth, it demonstrates how simple deterministic
/// systems can produce seemingly random, unpredictable behavior - the foundation of chaos theory.
///
/// The behavior dramatically changes based on the parameter `r`:
/// - **r < 1.0**: Population dies out (converges to 0)
/// - **r < 3.0**: Converges to a stable fixed point
/// - **3.0 < r < 3.57**: Oscillates between multiple values (period doubling)
/// - **r > 3.57**: Chaotic behavior (appears random but is deterministic)
/// - **r ≈ 3.828**: Extreme chaos
///
/// # Arguments
/// * `r` - Growth rate parameter (typically 0.0 to 4.0)
/// * `initial` - Starting value (typically 0.0 to 1.0, often 0.5)
/// * `n` - Number of iterations to generate
///
/// # Returns
/// Vector of values in range 0.0 to 1.0 (except edge cases where it may converge to 0)
///
/// # Typical Parameters
///
/// **r (chaos parameter):**
/// - **r = 2.5**: Stable, converges to fixed point (calm background music)
/// - **r = 3.0**: Barely stable (subtle variation)
/// - **r = 3.2**: Period-2 oscillation (moderate tension)
/// - **r = 3.5**: Period-4 oscillation (increasing complexity)
/// - **r = 3.6**: Complex oscillation (dramatic music)
/// - **r = 3.7**: Onset of chaos (boss fight intro)
/// - **r = 3.9**: Full chaos (intense action, combat)
/// - **r = 4.0**: Maximum chaos (extreme situations)
///
/// **initial:** Usually 0.5 (mid-range), but any value in (0, 1) works
///
/// # Recipe: Chaotic Melody in Scale
///
/// ```
/// use tunes::prelude::*;
/// use tunes::sequences;
///
/// let mut comp = Composition::new(Tempo::new(140.0));
///
/// // Generate chaotic values
/// let chaos = sequences::logistic_map::generate(3.9, 0.5, 32);
///
/// // Map to D minor pentatonic scale
/// let melody = sequences::map_to_scale_f32(
/// &chaos,
/// &sequences::Scale::minor_pentatonic(),
/// D4,
/// 2 // 2 octaves
/// );
///
/// comp.instrument("chaos_lead", &Instrument::synth_lead())
/// .delay(Delay::new(0.375, 0.3, 0.5))
/// .notes(&melody, 0.25);
/// ```
///
/// # Recipe: Dynamic Intensity Control
///
/// ```
/// use tunes::sequences;
///
/// // Map game state to music intensity
/// fn generate_for_intensity(intensity: f32, n: usize) -> Vec<f32> {
/// // intensity: 0.0 (calm) to 1.0 (chaotic)
/// let r = 2.5 + intensity * 1.5; // Maps to r=2.5-4.0
/// let chaos = sequences::logistic_map::generate(r, 0.5, n);
/// sequences::normalize(
/// &chaos.iter().map(|&x| (x * 100.0) as u32).collect::<Vec<_>>(),
/// 220.0,
/// 880.0
/// )
/// }
///
/// // Calm exploration
/// let calm_melody = generate_for_intensity(0.0, 16); // r=2.5, stable
///
/// // Boss fight
/// let intense_melody = generate_for_intensity(1.0, 32); // r=4.0, chaos!
/// ```
///
/// # Musical Applications
/// - **Dynamic complexity**: Map game state to r parameter for intensity control
/// - **Melodic variation**: Chaotic but deterministic pitch sequences
/// - **Rhythm patterns**: Use values to determine hit probabilities
/// - **Texture density**: More enemies → higher r → more chaotic/dense music
/// - **Filter sweeps**: Smooth but unpredictable parameter changes
///
/// # Chaos Control
/// The logistic map is perfect for game audio because you can smoothly transition
/// from stable (calm) to chaotic (intense) music by adjusting the `r` parameter
/// based on gameplay state (enemy count, health, proximity to danger, etc.).
// ========== PRESETS ==========
/// Ordered behavior (r=2.5) - converges to fixed point
/// Period-2 oscillation (r=3.2) - alternates between two values
/// Edge of chaos (r=3.56995) - onset of chaos, very interesting
/// Chaotic (r=3.9) - fully chaotic, unpredictable
/// Classic chaotic (r=3.7) - balanced chaos