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
/// Generate bounded random walk sequence
///
/// Like `random_walk()`, but constrained to stay within a specified range.
/// When the walk would exceed the bounds, it's clamped to min/max.
///
/// This is useful when you want random variation but need to guarantee
/// the values stay within musical constraints (e.g., a specific frequency range,
/// or normalized 0.0-1.0 values for parameters).
///
/// # Arguments
/// * `start` - Initial value (will be clamped to min..max if outside)
/// * `step` - Maximum step size per iteration (controls variation speed)
/// * `min` - Minimum allowed value (hard floor)
/// * `max` - Maximum allowed value (hard ceiling)
/// * `steps` - Number of steps to generate
///
/// # Returns
/// Vector of values forming a bounded random walk (all values in min..=max)
///
/// # Typical Parameters
///
/// **step size** (relative to range):
/// - **Small (5-10% of range)**: Slow, smooth exploration
/// - **Medium (15-25% of range)**: Natural variation
/// - **Large (30-50% of range)**: Jumpy, energetic movement
///
/// **Common ranges:**
/// - One octave: min=220, max=440, step=20-40
/// - Two octaves: min=220, max=880, step=40-80
/// - Normalized (0-1): min=0.0, max=1.0, step=0.05-0.15
///
/// # Recipe: Melody in Range
///
/// ```
/// use tunes::prelude::*;
/// use tunes::sequences;
///
/// let mut comp = Composition::new(Tempo::new(120.0));
///
/// // Wandering melody in 2-octave range
/// let melody = sequences::bounded_walk::generate(
/// 440.0, // Start at A4
/// 35.0, // Steps of up to 35 Hz
/// 220.0, // Min: A3
/// 880.0, // Max: A5
/// 32 // 32 notes
/// );
///
/// comp.instrument("lead", &Instrument::synth_lead())
/// .delay(Delay::new(0.375, 0.3, 0.5))
/// .notes(&melody, 0.25);
/// ```
///
/// # Recipe: Bass Line in Octave
///
/// ```
/// use tunes::prelude::*;
/// use tunes::sequences;
///
/// let mut comp = Composition::new(Tempo::new(140.0));
///
/// // Bass that stays in one octave
/// let bass = sequences::bounded_walk::generate(
/// 110.0, // A2
/// 8.0, // Small steps for smooth bass
/// 82.4, // E2 (low end)
/// 164.8, // E3 (high end)
/// 16
/// );
///
/// comp.instrument("bass", &Instrument::sub_bass())
/// .notes(&bass, 0.5);
/// ```
///
/// # Musical Applications
/// - **Constrained melodies**: Wandering pitch that stays in range
/// - **Bass lines**: Random variation within an octave
/// - **Parameter automation**: Filter, pan, volume staying in valid ranges
/// - **Scale-based melodies**: Step between scale degrees randomly
/// - **Dynamic contrast**: Volume variation within acceptable range
// ========== PRESETS ==========
/// Classic bounded walk - range -10 to 10, 32 steps
/// Narrow range - range -5 to 5, 24 steps
/// Wide range - range -20 to 20, 48 steps