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
/// Generate prime numbers sequence
///
/// Prime numbers are integers greater than 1 that have no divisors other than 1 and themselves.
/// The sequence starts: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47...
///
/// Primes have fascinated mathematicians for millennia and create interesting musical patterns
/// because they resist regular subdivision - they're inherently "unpredictable" while still
/// being deterministic. This makes them excellent for creating rhythms and melodies that
/// feel organic and non-mechanical.
///
/// Properties that make primes useful in music:
/// - **Irregular spacing**: No obvious pattern, but not random
/// - **Avoid common factors**: Create polyrhythms that rarely align
/// - **Mathematical beauty**: Deterministic but complex
/// - **Ancient mystery**: Used in composition since medieval times
///
/// # Arguments
/// * `n` - Number of prime numbers to generate
///
/// # Returns
/// Vector containing the first n prime numbers: [2, 3, 5, 7, 11, 13, 17, ...]
///
/// # Typical Values
/// - **n = 5-10**: Short irregular patterns (good for rhythmic hits)
/// - **n = 10-15**: Medium complexity (melodic sequences)
/// - **n = 20-30**: Long evolving patterns (structural organization)
///
/// # Recipe: Non-Repetitive Rhythm
/// ```
/// use tunes::prelude::*;
/// use tunes::sequences;
///
/// let mut comp = Composition::new(Tempo::new(140.0));
///
/// // Use primes for irregular but deterministic hit pattern
/// let primes = sequences::primes::generate(10);
/// let rhythm: Vec<usize> = primes.iter().map(|&p| (p % 16) as usize).collect();
///
/// comp.track("prime_perc")
/// .drum_grid(16, 0.125, |g| g
/// .sound(DrumType::Kick, &rhythm));
/// ```
///
/// # Examples
/// ```
/// use tunes::sequences;
///
/// let primes = sequences::primes::generate(10);
/// assert_eq!(primes, vec![2, 3, 5, 7, 11, 13, 17, 19, 23, 29]);
///
/// // Use for rhythmic patterns that avoid repetition
/// # use tunes::prelude::*;
/// # let mut comp = Composition::new(Tempo::new(140.0));
/// let prime_rhythm = sequences::primes::generate(8);
/// let hits = sequences::normalize(&prime_rhythm, 0.0, 16.0);
/// for &hit_time in &hits {
/// comp.track("prime_kicks")
/// .at(hit_time)
/// .note(&[110.0], 0.1);
/// }
///
/// // Use for melodic intervals (semitone jumps)
/// let intervals = sequences::primes::generate(12);
/// let pitches = sequences::normalize(&intervals, 200.0, 800.0);
/// comp.track("prime_melody").notes(&pitches, 0.25);
/// ```
///
/// # Musical Applications
/// - **Polyrhythms**: Primes create patterns that rarely align (3 against 5, 7 against 11)
/// - **Non-repetitive rhythms**: Use primes for hit positions to avoid obvious patterns
/// - **Melodic contours**: Prime-based intervals create interesting, unpredictable melodies
/// - **Phrase lengths**: Prime-numbered bar counts (5-bar, 7-bar, 11-bar phrases)
/// - **Harmonic ratios**: Prime number ratios create inharmonic/dissonant timbres
/// - **Form and structure**: Section lengths using primes (13 bars, 17 bars, 23 bars)
///
/// # Why Primes Matter in Music
/// Composers like Olivier Messiaen used prime numbers extensively to create rhythms that
/// "never repeat" within practical performance time. The lack of common factors means
/// patterns layer in complex, non-obvious ways - perfect for generative music that needs
/// to sound structured but not mechanical.
/// Helper function to check if a number is prime
// ========== PRESETS ==========
/// Short prime sequence (8 primes) - good for rhythmic patterns
/// Returns: [2, 3, 5, 7, 11, 13, 17, 19]
/// Medium prime sequence (12 primes) - balanced for melodies
/// Returns: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
/// Classic prime sequence (16 primes) - good variety
/// Long prime sequence (24 primes) - extensive patterns
/// Polyrhythm primes - first 6 primes for layering rhythms
/// Returns: [2, 3, 5, 7, 11, 13] - perfect for polyrhythmic complexity