1pub const MAX_TRACK_VOLUME: u8 = 15;
14
15const HIT_DECAY_SECS: f32 = 0.25;
17const RELEASE_DECAY_SECS: f32 = 0.2;
19const MAX_REST_SECS: f32 = 3600.0;
21const LEVEL_FALL_SECS: f32 = 0.18;
23
24#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
26pub struct TrackSample {
27 pub sounding: bool,
29 pub key: u8,
31 pub volume: u8,
33 pub note_ons: u32,
35 pub chord_len: u8,
37 pub chord_pcs: u16,
39 pub note_offs: u32,
41}
42
43#[derive(Debug, Clone, Copy, PartialEq, Default)]
45pub struct TrackView {
46 pub level: f32,
48 pub hit: f32,
50 pub key: u8,
52 pub sounding: bool,
54 pub jump: i8,
58 pub chord_len: u8,
60 pub chord_pcs: u16,
62 pub release: f32,
64 pub rest_secs: f32,
66}
67
68#[derive(Debug, Clone, Default)]
70pub struct TrackBoard {
71 views: Vec<TrackView>,
72 last_ons: Vec<u32>,
73 last_offs: Vec<u32>,
74 last_keys: Vec<Option<u8>>,
75 primed: bool,
76}
77
78impl TrackBoard {
79 pub fn new() -> Self {
81 Self::default()
82 }
83
84 pub fn update(&mut self, samples: &[TrackSample], dt: f32) {
89 if !(dt.is_finite() && dt > 0.0) {
90 return;
91 }
92 let changed_len = samples.len() != self.views.len();
93 self.views.resize(samples.len(), TrackView::default());
94 let first = !self.primed || changed_len;
95 let fall = (-dt / LEVEL_FALL_SECS).exp();
96 let decay = (-dt / HIT_DECAY_SECS).exp();
97 let release_decay = (-dt / RELEASE_DECAY_SECS).exp();
98 for (i, s) in samples.iter().enumerate() {
99 let v = &mut self.views[i];
100 let target = if s.sounding {
101 s.volume.min(MAX_TRACK_VOLUME) as f32 / MAX_TRACK_VOLUME as f32
102 } else {
103 0.0
104 };
105 v.level = if target >= v.level {
106 target
107 } else {
108 target + (v.level - target) * fall
109 };
110 if v.level < 0.002 {
111 v.level = 0.0;
112 }
113 v.hit *= decay;
114 if v.hit < 0.002 {
115 v.hit = 0.0;
116 }
117 v.release *= release_decay;
118 if v.release < 0.002 {
119 v.release = 0.0;
120 }
121 let new_note = !first && self.last_ons.get(i).is_some_and(|&n| n != s.note_ons);
122 if new_note {
123 v.hit = 1.0;
124 v.jump = match self.last_keys.get(i).copied().flatten() {
126 Some(prev) => (s.key as i16 - prev as i16).clamp(-127, 127) as i8,
127 None => 0,
128 };
129 }
130 if !first && self.last_offs.get(i).is_some_and(|&n| n != s.note_offs) {
131 v.release = 1.0;
132 }
133 v.rest_secs = if s.sounding {
134 0.0
135 } else {
136 (v.rest_secs + dt).min(MAX_REST_SECS)
137 };
138 v.key = s.key;
139 v.sounding = s.sounding;
140 v.chord_len = s.chord_len;
141 v.chord_pcs = s.chord_pcs;
142 }
143 self.last_ons.clear();
144 self.last_ons.extend(samples.iter().map(|s| s.note_ons));
145 self.last_offs.clear();
146 self.last_offs.extend(samples.iter().map(|s| s.note_offs));
147 self.last_keys.clear();
149 self.last_keys
150 .extend(samples.iter().map(|s| (s.note_ons != 0).then_some(s.key)));
151 self.primed = true;
152 }
153
154 pub fn tracks(&self) -> &[TrackView] {
156 &self.views
157 }
158
159 pub fn reset(&mut self) {
161 *self = Self::default();
162 }
163}
164
165#[cfg(test)]
166mod tests {
167 use super::*;
168
169 fn s(sounding: bool, volume: u8, ons: u32) -> TrackSample {
170 TrackSample {
171 sounding,
172 key: 60,
173 volume,
174 note_ons: ons,
175 ..TrackSample::default()
176 }
177 }
178
179 const DT: f32 = 1.0 / 60.0;
180
181 #[test]
182 fn first_update_does_not_flash_but_new_notes_do() {
183 let mut b = TrackBoard::new();
184 b.update(&[s(true, 15, 7)], DT);
185 assert_eq!(b.tracks()[0].hit, 0.0, "溜まっていた回数では光らない");
186 assert_eq!(b.tracks()[0].level, 1.0);
187 b.update(&[s(true, 15, 8)], DT);
188 assert_eq!(b.tracks()[0].hit, 1.0);
189 b.update(&[s(true, 15, 8)], DT);
190 let h = b.tracks()[0].hit;
191 assert!(h < 1.0 && h > 0.5, "{h}");
192 for _ in 0..120 {
193 b.update(&[s(true, 15, 8)], DT);
194 }
195 assert_eq!(b.tracks()[0].hit, 0.0);
196 }
197
198 #[test]
199 fn wraparound_counts_as_a_new_note() {
200 let mut b = TrackBoard::new();
201 b.update(&[s(true, 8, u32::MAX)], DT);
202 b.update(&[s(true, 8, 0)], DT);
203 assert_eq!(b.tracks()[0].hit, 1.0);
204 }
205
206 #[test]
207 fn level_rises_instantly_and_falls_smoothly() {
208 let mut b = TrackBoard::new();
209 b.update(&[s(true, 15, 0)], DT);
210 assert_eq!(b.tracks()[0].level, 1.0);
211 b.update(&[s(false, 0, 0)], DT);
212 let l = b.tracks()[0].level;
213 assert!(l > 0.5 && l < 1.0, "{l}");
214 for _ in 0..200 {
215 b.update(&[s(false, 0, 0)], DT);
216 }
217 assert_eq!(b.tracks()[0].level, 0.0);
218 assert!(!b.tracks()[0].sounding);
219 }
220
221 #[test]
222 fn volume_is_clamped_and_track_count_follows_input() {
223 let mut b = TrackBoard::new();
224 b.update(&[s(true, 200, 0), s(true, 0, 0)], DT);
225 assert_eq!(b.tracks().len(), 2);
226 assert_eq!(b.tracks()[0].level, 1.0);
227 assert_eq!(b.tracks()[1].level, 0.0);
228 b.update(&[s(true, 5, 3)], DT);
229 assert_eq!(b.tracks().len(), 1);
230 assert_eq!(b.tracks()[0].hit, 0.0, "数が変わった直後は光らない");
231 b.update(&[], DT);
232 assert!(b.tracks().is_empty());
233 }
234
235 #[test]
236 fn bad_dt_changes_nothing() {
237 let mut b = TrackBoard::new();
238 b.update(&[s(true, 15, 0)], 0.0);
239 b.update(&[s(true, 15, 0)], f32::NAN);
240 assert!(b.tracks().is_empty());
241 b.update(&[s(true, 15, 0)], DT);
242 b.reset();
243 assert!(b.tracks().is_empty());
244 }
245
246 #[test]
247 fn jump_is_the_key_change_from_the_previous_note() {
248 let mut b = TrackBoard::new();
249 let note = |key: u8, ons: u32| TrackSample {
250 sounding: true,
251 key,
252 volume: 15,
253 note_ons: ons,
254 ..TrackSample::default()
255 };
256 b.update(&[note(60, 1)], DT);
257 assert_eq!(b.tracks()[0].jump, 0, "最初の音は動きなし");
258 b.update(&[note(67, 2)], DT);
259 assert_eq!(b.tracks()[0].jump, 7);
260 b.update(&[note(67, 2)], DT);
261 assert_eq!(b.tracks()[0].jump, 7, "新しい音がなければそのまま");
262 b.update(&[note(55, 3)], DT);
263 assert_eq!(b.tracks()[0].jump, -12);
264 b.update(&[note(255, 4)], DT);
265 assert_eq!(b.tracks()[0].jump, 127, "大きすぎる動きは止める");
266 }
267
268 #[test]
269 fn release_flashes_when_a_note_ends_even_if_short() {
270 let mut b = TrackBoard::new();
271 let mk = |sounding: bool, offs: u32| TrackSample {
272 sounding,
273 key: 60,
274 volume: 8,
275 note_ons: 1,
276 note_offs: offs,
277 ..TrackSample::default()
278 };
279 b.update(&[mk(true, 0)], DT);
280 assert_eq!(b.tracks()[0].release, 0.0);
281 b.update(&[mk(false, 1)], DT);
283 assert_eq!(b.tracks()[0].release, 1.0);
284 for _ in 0..120 {
285 b.update(&[mk(false, 1)], DT);
286 }
287 assert_eq!(b.tracks()[0].release, 0.0);
288 }
289
290 #[test]
291 fn rest_secs_counts_silence_and_resets_on_sound() {
292 let mut b = TrackBoard::new();
293 let mk = |sounding: bool| TrackSample {
294 sounding,
295 volume: 8,
296 ..TrackSample::default()
297 };
298 b.update(&[mk(true)], 0.5);
299 assert_eq!(b.tracks()[0].rest_secs, 0.0);
300 b.update(&[mk(false)], 0.5);
301 b.update(&[mk(false)], 0.25);
302 assert!((b.tracks()[0].rest_secs - 0.75).abs() < 1e-6);
303 b.update(&[mk(true)], 0.5);
304 assert_eq!(b.tracks()[0].rest_secs, 0.0);
305 }
306
307 #[test]
308 fn chord_shape_is_passed_through() {
309 let mut b = TrackBoard::new();
310 b.update(
311 &[TrackSample {
312 sounding: true,
313 volume: 15,
314 chord_len: 3,
315 chord_pcs: 1 | 1 << 4 | 1 << 7,
316 ..TrackSample::default()
317 }],
318 DT,
319 );
320 let v = b.tracks()[0];
321 assert_eq!(v.chord_len, 3);
322 assert_eq!(v.chord_pcs, 0b1001_0001);
323 }
324}