1#![cfg_attr(not(feature = "testing"), no_std)]
28#![deny(unsafe_code)]
29#![cfg_attr(docsrs, feature(doc_cfg))]
31
32#[cfg(feature = "alloc")]
33extern crate alloc;
34
35pub mod binary;
36pub mod duty;
37pub mod envelope;
38pub mod interrupt;
39pub mod notice;
40#[cfg(feature = "libm")]
41pub mod output;
42pub mod pitch;
43pub mod text;
44
45pub mod kit;
46pub mod synth;
47
48pub const PARAM_PAGE: u16 = 256;
51
52pub fn set_param_id<S: Synth + ?Sized>(synth: &mut S, ch: usize, id: u16, value: u32) {
54 match u8::try_from(id) {
55 Ok(p) => synth.set_param(ch, p, value),
56 Err(_) => synth.set_param_hi(ch, (id - PARAM_PAGE) as u8, value),
57 }
58}
59
60pub fn param_id<S: Synth + ?Sized>(synth: &S, ch: usize, id: u16) -> Option<u32> {
62 match u8::try_from(id) {
63 Ok(p) => synth.param(ch, p),
64 Err(_) => synth.param_hi(ch, (id - PARAM_PAGE) as u8),
65 }
66}
67
68pub const WAVE_LEN: usize = 32;
70
71pub trait Synth: Send {
74 fn channels(&self) -> usize;
76 fn reset(&mut self);
78 fn set_tone(&mut self, ch: usize, tone: u8);
80
81 fn prepare_tone(&mut self, _tone: u8) {}
86
87 fn set_wave(&mut self, ch: usize, wave: &[i8]) {
93 let _ = (ch, wave);
94 }
95
96 fn wave_partner(&self, ch: usize) -> Option<usize> {
104 let _ = ch;
105 None
106 }
107 fn set_param(&mut self, ch: usize, param: u8, value: u32);
109 fn set_param_hi(&mut self, ch: usize, param: u8, value: u32) {
113 let _ = (ch, param, value);
114 }
115 fn param_hi(&self, ch: usize, param: u8) -> Option<u32> {
117 let _ = (ch, param);
118 None
119 }
120 fn note_on(&mut self, ch: usize, key: u8, vel: u8);
122 fn note_off(&mut self, ch: usize);
123 fn set_volume(&mut self, ch: usize, vel: u8);
127 fn set_pitch(&mut self, ch: usize, cents_q8: i32);
133 fn next(&mut self, audible: u32) -> [f32; 2];
136 fn set_tempo(&mut self, bpm_milli: u32) {
141 let _ = bpm_milli;
142 }
143 fn chord_on(&mut self, ch: usize, keys: &[u8], vel: u8, legato: bool) {
150 if legato {
151 self.note_off(ch);
152 }
153 if let Some(&key) = keys.first() {
154 self.note_on(ch, key, vel);
155 }
156 }
157 fn param(&self, ch: usize, param: u8) -> Option<u32> {
162 let _ = (ch, param);
163 None
164 }
165 fn is_unpitched(&self, ch: usize) -> bool {
168 let _ = ch;
169 false
170 }
171 fn key_range(&self, ch: usize) -> (u8, u8) {
174 let _ = ch;
175 (0, 127)
176 }
177 fn tail_frames(&self) -> u32 {
181 0
182 }
183 fn latency_frames(&self) -> u32 {
188 0
189 }
190 fn set_sfx_mask(&mut self, mask: u32) {
195 let _ = mask;
196 }
197 fn shot_duck(&mut self) {}
201 fn mix_input(&mut self, frame: [f32; 2]) -> bool {
206 let _ = frame;
207 false
208 }
209}
210
211pub const KEY_HZ: [f64; 128] = [
213 8.175798915643707,
214 8.661957218027252,
215 9.177023997418988,
216 9.722718241315029,
217 10.300861153527183,
218 10.913382232281373,
219 11.562325709738575,
220 12.249857374429663,
221 12.978271799373287,
222 13.75,
223 14.567617547440307,
224 15.433853164253883,
225 16.351597831287414,
226 17.323914436054505,
227 18.354047994837977,
228 19.445436482630058,
229 20.601722307054366,
230 21.826764464562746,
231 23.12465141947715,
232 24.499714748859326,
233 25.956543598746574,
234 27.5,
235 29.13523509488062,
236 30.86770632850775,
237 32.70319566257483,
238 34.64782887210901,
239 36.70809598967594,
240 38.890872965260115,
241 41.20344461410875,
242 43.653528929125486,
243 46.2493028389543,
244 48.999429497718666,
245 51.91308719749314,
246 55.0,
247 58.27047018976124,
248 61.7354126570155,
249 65.40639132514966,
250 69.29565774421802,
251 73.41619197935188,
252 77.78174593052023,
253 82.4068892282175,
254 87.30705785825097,
255 92.4986056779086,
256 97.99885899543733,
257 103.82617439498628,
258 110.0,
259 116.54094037952248,
260 123.47082531403103,
261 130.8127826502993,
262 138.59131548843604,
263 146.8323839587038,
264 155.56349186104046,
265 164.81377845643496,
266 174.61411571650194,
267 184.9972113558172,
268 195.99771799087463,
269 207.65234878997256,
270 220.0,
271 233.08188075904496,
272 246.94165062806206,
273 261.6255653005986,
274 277.1826309768721,
275 293.6647679174076,
276 311.1269837220809,
277 329.6275569128699,
278 349.2282314330039,
279 369.9944227116344,
280 391.99543598174927,
281 415.3046975799451,
282 440.0,
283 466.1637615180899,
284 493.8833012561241,
285 523.2511306011972,
286 554.3652619537442,
287 587.3295358348151,
288 622.2539674441618,
289 659.2551138257398,
290 698.4564628660078,
291 739.9888454232688,
292 783.9908719634985,
293 830.6093951598903,
294 880.0,
295 932.3275230361799,
296 987.7666025122483,
297 1046.5022612023945,
298 1108.7305239074883,
299 1174.6590716696303,
300 1244.5079348883237,
301 1318.5102276514797,
302 1396.9129257320155,
303 1479.9776908465376,
304 1567.981743926997,
305 1661.2187903197805,
306 1760.0,
307 1864.6550460723597,
308 1975.533205024496,
309 2093.004522404789,
310 2217.4610478149766,
311 2349.31814333926,
312 2489.0158697766474,
313 2637.02045530296,
314 2793.825851464031,
315 2959.955381693075,
316 3135.9634878539946,
317 3322.437580639561,
318 3520.0,
319 3729.3100921447194,
320 3951.066410048992,
321 4186.009044809578,
322 4434.922095629953,
323 4698.63628667852,
324 4978.031739553295,
325 5274.04091060592,
326 5587.651702928062,
327 5919.91076338615,
328 6271.926975707989,
329 6644.875161279122,
330 7040.0,
331 7458.620184289437,
332 7902.132820097988,
333 8372.018089619156,
334 8869.844191259906,
335 9397.272573357044,
336 9956.06347910659,
337 10548.081821211836,
338 11175.303405856126,
339 11839.8215267723,
340 12543.853951415975,
341];
342
343pub fn transposed_key<C: Synth + ?Sized>(synth: &C, ch: usize, key: u8, semitones: i8) -> u8 {
350 transpose_key(synth, ch, key, semitones).0
351}
352
353pub fn transpose_key<C: Synth + ?Sized>(
356 synth: &C,
357 ch: usize,
358 key: u8,
359 semitones: i8,
360) -> (u8, bool) {
361 if semitones == 0 || synth.is_unpitched(ch) {
362 return (key, false);
363 }
364 let (lo, hi) = synth.key_range(ch);
365 let lo = lo as i32;
366 let hi = (hi as i32).max(lo);
367 let mut k = key as i32 + semitones as i32;
368 while k > hi && k - 12 >= lo {
369 k -= 12;
370 }
371 while k < lo && k + 12 <= hi {
372 k += 12;
373 }
374 let wrapped = k != key as i32 + semitones as i32;
375 (k.clamp(lo, hi).clamp(0, 127) as u8, wrapped)
376}
377
378pub fn key_to_hz(key: u8) -> f64 {
380 KEY_HZ[(key as usize).min(127)]
381}
382
383pub fn round_u32(x: f64) -> u32 {
385 if x.is_nan() || x <= 0.0 {
386 0
387 } else if x >= u32::MAX as f64 {
388 u32::MAX
389 } else {
390 (x + 0.5) as u32
391 }
392}
393
394#[derive(Debug, Clone, Copy)]
396pub struct DcBlocker {
397 x1: f32,
398 y1: f32,
399 r: f32,
400}
401
402impl DcBlocker {
403 pub fn new(sample_rate: u32) -> Self {
404 let sr = if sample_rate == 0 { 1 } else { sample_rate };
405 let r = 1.0 - (2.0 * core::f32::consts::PI * 20.0 / sr as f32);
406 DcBlocker {
407 x1: 0.0,
408 y1: 0.0,
409 r: r.clamp(0.5, 0.99999),
410 }
411 }
412
413 pub fn reset(&mut self) {
414 self.x1 = 0.0;
415 self.y1 = 0.0;
416 }
417
418 pub fn process(&mut self, x: f32) -> f32 {
419 let y = x - self.x1 + self.r * self.y1;
420 self.x1 = x;
421 self.y1 = y;
422 y
423 }
424}
425
426pub mod defaults {
428 pub const PPQ: u32 = 10080;
431 pub const TEMPO: u16 = 120;
433 pub const TEMPO_MILLI: u32 = TEMPO as u32 * 1000;
435 pub const MAX_TEMPO_MILLI: u32 = 999_990;
437 pub const MIN_TEMPO_MILLI: u32 = 1000;
439 pub const VOLUME: u8 = 12;
440}
441
442#[cfg(test)]
443mod tests {
444 use super::*;
445
446 #[test]
447 fn table() {
448 assert_eq!(key_to_hz(69), 440.0);
449 assert!((key_to_hz(60) - 261.625_565_300_598_6).abs() < 1e-9);
450 assert_eq!(key_to_hz(200), KEY_HZ[127]);
451 assert_eq!(round_u32(2.5), 3);
452 assert_eq!(round_u32(-1.0), 0);
453 }
454}