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
/// Generate Thue-Morse sequence (fair division sequence)
///
/// The Thue-Morse sequence is a binary sequence with remarkable fairness properties.
/// It's constructed by starting with 0, then repeatedly appending the bitwise complement:
/// - Start: `0`
/// - Append complement: `0 1`
/// - Append complement: `0 1 1 0`
/// - Append complement: `0 1 1 0 1 0 0 1`
/// - Continue...
///
/// This sequence has fascinating properties:
/// - **No three consecutive identical blocks** (avoids repetition)
/// - **Fairest possible coin flip sequence** (equal distribution)
/// - **Self-similar** (contains itself at different scales)
/// - **Appears in chemistry** (protein folding), music, and computer science
///
/// Named after mathematicians Axel Thue (1906) and Marston Morse (1921).
///
/// # Arguments
/// * `n` - Number of terms to generate
///
/// # Returns
/// Vector of 0s and 1s forming the Thue-Morse sequence
///
/// # Typical Values
/// - **n = 8-16**: Short patterns (accent patterns, simple alternation)
/// - **n = 16-32**: Medium patterns (drum programming, rhythm)
/// - **n = 32-64**: Long patterns (extended sequences, formal structure)
/// - Powers of 2 (8, 16, 32, 64) align with the sequence's natural structure
///
/// # Recipe: Non-Repetitive Drum Pattern
/// ```
/// use tunes::prelude::*;
/// use tunes::sequences;
///
/// let mut comp = Composition::new(Tempo::new(128.0));
///
/// // Extract hit positions from Thue-Morse
/// let tm = sequences::thue_morse::generate(32);
/// let hits: Vec<usize> = tm.iter()
/// .enumerate()
/// .filter(|(_, &v)| v == 1)
/// .map(|(i, _)| i)
/// .collect();
///
/// comp.track("tm_drums")
/// .drum_grid(32, 0.125, |g| g
/// .sound(DrumType::Kick, &hits)
/// .sound(DrumType::HiHatClosed, &sequences::euclidean::generate(16, 32))); // Layer with Euclidean
/// ```
///
/// # Examples
/// ```
/// use tunes::sequences;
///
/// let tm = sequences::thue_morse::generate(16);
/// // [0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0]
///
/// // Use as rhythm pattern (0 = rest, 1 = hit)
/// # use tunes::prelude::*;
/// # let mut comp = Composition::new(Tempo::new(120.0));
/// let pattern: Vec<usize> = sequences::thue_morse::generate(32)
/// .iter()
/// .enumerate()
/// .filter(|(_, &v)| v == 1)
/// .map(|(i, _)| i)
/// .collect();
///
/// comp.track("thue_drums")
/// .drum_grid(32, 0.125, |g| g
/// .sound(DrumType::Kick, &pattern));
///
/// // Use for parameter switching
/// let tm_seq = sequences::thue_morse::generate(8);
/// for (i, &val) in tm_seq.iter().enumerate() {
/// let freq = if val == 0 { 440.0 } else { 554.37 };
/// comp.track("alternating").note(&[freq], 0.25);
/// }
/// ```
///
/// # Musical Applications
/// - **Non-repetitive rhythms**: Creates patterns that don't sound mechanical
/// - **Timbral alternation**: Switch between two instruments/sounds fairly
/// - **Accent patterns**: Alternate strong/weak beats without predictability
/// - **Chord voicings**: Alternate between two chord inversions
/// - **Stereo panning**: Fair left/right distribution
/// - **Minimalist composition**: Used by composers like Tom Johnson
///
/// # Why It Matters for Music
/// The Thue-Morse sequence avoids the monotony of simple alternation (0,1,0,1,...)
/// while maintaining perfect fairness. It sounds organic and interesting without
/// being truly random - ideal for generative music that needs structure but
/// wants to avoid repetitive patterns.
// ========== PRESETS ==========
/// Short Thue-Morse (16 elements)
/// Classic Thue-Morse (32 elements)
/// Extended Thue-Morse (64 elements)