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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//! Voice management for polyphonic synthesis.
//!
//! Provides voice allocation, stealing, and per-voice state tracking
//! for polyphonic instruments.
use serde::{Deserialize, Serialize};
/// Polyphony mode.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum PolyMode {
/// Polyphonic — each note gets its own voice.
Poly,
/// Monophonic — only one note at a time, retrigger on new note.
Mono,
/// Legato — monophonic but glides to new note without retriggering.
Legato,
}
/// Voice stealing strategy when all voices are in use.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum StealMode {
/// Steal the oldest active voice.
Oldest,
/// Steal the quietest active voice.
Quietest,
/// Steal the voice with the lowest note.
Lowest,
/// Do not steal — ignore new notes when full.
None,
}
/// State of a single voice.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Voice {
/// Whether this voice is currently active (producing sound).
active: bool,
/// The MIDI note number this voice is playing (0-127).
pub note: u8,
/// Velocity (0.0 to 1.0).
pub velocity: f32,
/// Age counter — incremented each sample when active. Used for oldest-steal.
age: u64,
/// Current amplitude (for quietest-steal heuristic).
pub amplitude: f32,
/// Per-note pitch bend (MIDI 2.0, in semitones).
pub pitch_bend: f32,
/// Per-note pressure / aftertouch (0.0 to 1.0).
pub pressure: f32,
/// Per-note brightness (MIDI 2.0 CC74, 0.0 to 1.0).
pub brightness: f32,
}
impl Voice {
/// Returns whether this voice is currently active (producing sound).
#[inline]
#[must_use]
pub fn is_active(&self) -> bool {
self.active
}
/// Returns the age counter for this voice.
#[inline]
#[must_use]
pub fn age(&self) -> u64 {
self.age
}
}
impl Default for Voice {
fn default() -> Self {
Self {
active: false,
note: 0,
velocity: 0.0,
age: 0,
amplitude: 0.0,
pitch_bend: 0.0,
pressure: 0.0,
brightness: 0.5,
}
}
}
/// Manages voice allocation and stealing for polyphonic instruments.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VoiceManager {
/// Voice pool.
pub(crate) voices: Vec<Voice>,
/// Maximum polyphony.
max_voices: usize,
/// Polyphony mode.
pub poly_mode: PolyMode,
/// Voice stealing mode.
pub steal_mode: StealMode,
}
impl VoiceManager {
/// Create a new voice manager.
///
/// `max_voices` is clamped to 1..128.
#[must_use]
pub fn new(max_voices: usize, poly_mode: PolyMode, steal_mode: StealMode) -> Self {
let n = max_voices.clamp(1, 128);
Self {
voices: (0..n).map(|_| Voice::default()).collect(),
max_voices: n,
poly_mode,
steal_mode,
}
}
/// Allocate a voice for a new note. Returns the voice index.
///
/// In Poly mode: finds a free voice or steals one.
/// In Mono/Legato mode: always uses voice 0.
#[must_use]
pub fn note_on(&mut self, note: u8, velocity: f32) -> Option<usize> {
match self.poly_mode {
PolyMode::Mono | PolyMode::Legato => {
let v = &mut self.voices[0];
v.active = true;
v.note = note;
v.velocity = velocity;
v.age = 0;
Some(0)
}
PolyMode::Poly => {
// First: find a free voice
if let Some(idx) = self.voices.iter().position(|v| !v.active) {
let v = &mut self.voices[idx];
v.active = true;
v.note = note;
v.velocity = velocity;
v.age = 0;
return Some(idx);
}
// All voices busy — steal
let idx = self.find_steal_target()?;
let v = &mut self.voices[idx];
v.active = true;
v.note = note;
v.velocity = velocity;
v.age = 0;
Some(idx)
}
}
}
/// Release a voice by note number.
///
/// Returns the index of the released voice, or `None` if not found.
pub fn note_off(&mut self, note: u8) -> Option<usize> {
if let Some(idx) = self.voices.iter().position(|v| v.active && v.note == note) {
self.voices[idx].active = false;
Some(idx)
} else {
None
}
}
/// Find a voice to steal based on the current steal mode.
fn find_steal_target(&self) -> Option<usize> {
match self.steal_mode {
StealMode::None => None,
StealMode::Oldest => self
.voices
.iter()
.enumerate()
.filter(|(_, v)| v.active)
.max_by_key(|(_, v)| v.age)
.map(|(i, _)| i),
StealMode::Quietest => self
.voices
.iter()
.enumerate()
.filter(|(_, v)| v.active)
.min_by(|(_, a), (_, b)| {
a.amplitude
.partial_cmp(&b.amplitude)
.unwrap_or(std::cmp::Ordering::Equal)
})
.map(|(i, _)| i),
StealMode::Lowest => self
.voices
.iter()
.enumerate()
.filter(|(_, v)| v.active)
.min_by_key(|(_, v)| v.note)
.map(|(i, _)| i),
}
}
/// Advance age counters for all active voices (call once per sample or per buffer).
pub fn tick(&mut self) {
for v in &mut self.voices {
if v.active {
v.age = v.age.saturating_add(1);
}
}
}
/// Returns the number of currently active voices.
#[must_use]
pub fn active_count(&self) -> usize {
self.voices.iter().filter(|v| v.active).count()
}
/// Returns a shared reference to the voice pool.
#[inline]
#[must_use]
pub fn voices(&self) -> &[Voice] {
&self.voices
}
/// Returns a mutable reference to a voice by index, or `None` if out of bounds.
#[inline]
pub fn voice_mut(&mut self, index: usize) -> Option<&mut Voice> {
self.voices.get_mut(index)
}
/// Returns the maximum polyphony.
#[must_use]
pub fn max_voices(&self) -> usize {
self.max_voices
}
/// Release all voices.
pub fn all_notes_off(&mut self) {
for v in &mut self.voices {
v.active = false;
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_note_on_off() {
let mut vm = VoiceManager::new(4, PolyMode::Poly, StealMode::Oldest);
let idx = vm.note_on(60, 0.8).unwrap();
assert_eq!(vm.active_count(), 1);
assert_eq!(vm.voices()[idx].note, 60);
vm.note_off(60);
assert_eq!(vm.active_count(), 0);
}
#[test]
fn test_poly_fill_and_steal() {
let mut vm = VoiceManager::new(2, PolyMode::Poly, StealMode::Oldest);
let _ = vm.note_on(60, 0.8);
vm.tick();
let _ = vm.note_on(64, 0.7);
vm.tick();
assert_eq!(vm.active_count(), 2);
// Third note should steal oldest
let idx = vm.note_on(67, 0.9).unwrap();
assert_eq!(vm.voices()[idx].note, 67);
assert_eq!(vm.active_count(), 2);
}
#[test]
fn test_steal_none() {
let mut vm = VoiceManager::new(1, PolyMode::Poly, StealMode::None);
let _ = vm.note_on(60, 0.8);
assert!(vm.note_on(64, 0.7).is_none());
}
#[test]
fn test_mono_mode() {
let mut vm = VoiceManager::new(4, PolyMode::Mono, StealMode::Oldest);
let _ = vm.note_on(60, 0.8);
let _ = vm.note_on(64, 0.7);
// Mono always uses voice 0
assert_eq!(vm.voices()[0].note, 64);
assert_eq!(vm.active_count(), 1);
}
#[test]
fn test_all_notes_off() {
let mut vm = VoiceManager::new(4, PolyMode::Poly, StealMode::Oldest);
let _ = vm.note_on(60, 0.8);
let _ = vm.note_on(64, 0.7);
vm.all_notes_off();
assert_eq!(vm.active_count(), 0);
}
#[test]
fn test_serde_roundtrip() {
let mut vm = VoiceManager::new(4, PolyMode::Poly, StealMode::Oldest);
let _ = vm.note_on(60, 0.8);
let json = serde_json::to_string(&vm).unwrap();
let back: VoiceManager = serde_json::from_str(&json).unwrap();
assert_eq!(vm.active_count(), back.active_count());
assert_eq!(vm.max_voices(), back.max_voices());
}
}