#![cfg_attr(not(feature = "testing"), no_std)]
#![deny(unsafe_code)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#[cfg(feature = "alloc")]
extern crate alloc;
pub mod binary;
pub mod duty;
pub mod envelope;
pub mod interrupt;
pub mod notice;
#[cfg(feature = "libm")]
pub mod output;
pub mod pitch;
pub mod text;
pub mod kit;
pub mod synth;
pub const PARAM_PAGE: u16 = 256;
pub fn set_param_id<S: Synth + ?Sized>(synth: &mut S, ch: usize, id: u16, value: u32) {
match u8::try_from(id) {
Ok(p) => synth.set_param(ch, p, value),
Err(_) => synth.set_param_hi(ch, (id - PARAM_PAGE) as u8, value),
}
}
pub fn param_id<S: Synth + ?Sized>(synth: &S, ch: usize, id: u16) -> Option<u32> {
match u8::try_from(id) {
Ok(p) => synth.param(ch, p),
Err(_) => synth.param_hi(ch, (id - PARAM_PAGE) as u8),
}
}
pub const WAVE_LEN: usize = 32;
pub trait Synth: Send {
fn channels(&self) -> usize;
fn reset(&mut self);
fn set_tone(&mut self, ch: usize, tone: u8);
fn prepare_tone(&mut self, _tone: u8) {}
fn set_wave(&mut self, ch: usize, wave: &[i8]) {
let _ = (ch, wave);
}
fn wave_partner(&self, ch: usize) -> Option<usize> {
let _ = ch;
None
}
fn set_param(&mut self, ch: usize, param: u8, value: u32);
fn set_param_hi(&mut self, ch: usize, param: u8, value: u32) {
let _ = (ch, param, value);
}
fn param_hi(&self, ch: usize, param: u8) -> Option<u32> {
let _ = (ch, param);
None
}
fn note_on(&mut self, ch: usize, key: u8, vel: u8);
fn note_off(&mut self, ch: usize);
fn set_volume(&mut self, ch: usize, vel: u8);
fn set_pitch(&mut self, ch: usize, cents_q8: i32);
fn next(&mut self, audible: u32) -> [f32; 2];
fn set_tempo(&mut self, bpm_milli: u32) {
let _ = bpm_milli;
}
fn chord_on(&mut self, ch: usize, keys: &[u8], vel: u8, legato: bool) {
if legato {
self.note_off(ch);
}
if let Some(&key) = keys.first() {
self.note_on(ch, key, vel);
}
}
fn param(&self, ch: usize, param: u8) -> Option<u32> {
let _ = (ch, param);
None
}
fn is_unpitched(&self, ch: usize) -> bool {
let _ = ch;
false
}
fn key_range(&self, ch: usize) -> (u8, u8) {
let _ = ch;
(0, 127)
}
fn tail_frames(&self) -> u32 {
0
}
fn latency_frames(&self) -> u32 {
0
}
fn set_sfx_mask(&mut self, mask: u32) {
let _ = mask;
}
fn shot_duck(&mut self) {}
fn mix_input(&mut self, frame: [f32; 2]) -> bool {
let _ = frame;
false
}
}
pub const KEY_HZ: [f64; 128] = [
8.175798915643707,
8.661957218027252,
9.177023997418988,
9.722718241315029,
10.300861153527183,
10.913382232281373,
11.562325709738575,
12.249857374429663,
12.978271799373287,
13.75,
14.567617547440307,
15.433853164253883,
16.351597831287414,
17.323914436054505,
18.354047994837977,
19.445436482630058,
20.601722307054366,
21.826764464562746,
23.12465141947715,
24.499714748859326,
25.956543598746574,
27.5,
29.13523509488062,
30.86770632850775,
32.70319566257483,
34.64782887210901,
36.70809598967594,
38.890872965260115,
41.20344461410875,
43.653528929125486,
46.2493028389543,
48.999429497718666,
51.91308719749314,
55.0,
58.27047018976124,
61.7354126570155,
65.40639132514966,
69.29565774421802,
73.41619197935188,
77.78174593052023,
82.4068892282175,
87.30705785825097,
92.4986056779086,
97.99885899543733,
103.82617439498628,
110.0,
116.54094037952248,
123.47082531403103,
130.8127826502993,
138.59131548843604,
146.8323839587038,
155.56349186104046,
164.81377845643496,
174.61411571650194,
184.9972113558172,
195.99771799087463,
207.65234878997256,
220.0,
233.08188075904496,
246.94165062806206,
261.6255653005986,
277.1826309768721,
293.6647679174076,
311.1269837220809,
329.6275569128699,
349.2282314330039,
369.9944227116344,
391.99543598174927,
415.3046975799451,
440.0,
466.1637615180899,
493.8833012561241,
523.2511306011972,
554.3652619537442,
587.3295358348151,
622.2539674441618,
659.2551138257398,
698.4564628660078,
739.9888454232688,
783.9908719634985,
830.6093951598903,
880.0,
932.3275230361799,
987.7666025122483,
1046.5022612023945,
1108.7305239074883,
1174.6590716696303,
1244.5079348883237,
1318.5102276514797,
1396.9129257320155,
1479.9776908465376,
1567.981743926997,
1661.2187903197805,
1760.0,
1864.6550460723597,
1975.533205024496,
2093.004522404789,
2217.4610478149766,
2349.31814333926,
2489.0158697766474,
2637.02045530296,
2793.825851464031,
2959.955381693075,
3135.9634878539946,
3322.437580639561,
3520.0,
3729.3100921447194,
3951.066410048992,
4186.009044809578,
4434.922095629953,
4698.63628667852,
4978.031739553295,
5274.04091060592,
5587.651702928062,
5919.91076338615,
6271.926975707989,
6644.875161279122,
7040.0,
7458.620184289437,
7902.132820097988,
8372.018089619156,
8869.844191259906,
9397.272573357044,
9956.06347910659,
10548.081821211836,
11175.303405856126,
11839.8215267723,
12543.853951415975,
];
pub fn transposed_key<C: Synth + ?Sized>(synth: &C, ch: usize, key: u8, semitones: i8) -> u8 {
transpose_key(synth, ch, key, semitones).0
}
pub fn transpose_key<C: Synth + ?Sized>(
synth: &C,
ch: usize,
key: u8,
semitones: i8,
) -> (u8, bool) {
if semitones == 0 || synth.is_unpitched(ch) {
return (key, false);
}
let (lo, hi) = synth.key_range(ch);
let lo = lo as i32;
let hi = (hi as i32).max(lo);
let mut k = key as i32 + semitones as i32;
while k > hi && k - 12 >= lo {
k -= 12;
}
while k < lo && k + 12 <= hi {
k += 12;
}
let wrapped = k != key as i32 + semitones as i32;
(k.clamp(lo, hi).clamp(0, 127) as u8, wrapped)
}
pub fn key_to_hz(key: u8) -> f64 {
KEY_HZ[(key as usize).min(127)]
}
pub fn round_u32(x: f64) -> u32 {
if x.is_nan() || x <= 0.0 {
0
} else if x >= u32::MAX as f64 {
u32::MAX
} else {
(x + 0.5) as u32
}
}
#[derive(Debug, Clone, Copy)]
pub struct DcBlocker {
x1: f32,
y1: f32,
r: f32,
}
impl DcBlocker {
pub fn new(sample_rate: u32) -> Self {
let sr = if sample_rate == 0 { 1 } else { sample_rate };
let r = 1.0 - (2.0 * core::f32::consts::PI * 20.0 / sr as f32);
DcBlocker {
x1: 0.0,
y1: 0.0,
r: r.clamp(0.5, 0.99999),
}
}
pub fn reset(&mut self) {
self.x1 = 0.0;
self.y1 = 0.0;
}
pub fn process(&mut self, x: f32) -> f32 {
let y = x - self.x1 + self.r * self.y1;
self.x1 = x;
self.y1 = y;
y
}
}
pub mod defaults {
pub const PPQ: u32 = 10080;
pub const TEMPO: u16 = 120;
pub const TEMPO_MILLI: u32 = TEMPO as u32 * 1000;
pub const MAX_TEMPO_MILLI: u32 = 999_990;
pub const MIN_TEMPO_MILLI: u32 = 1000;
pub const VOLUME: u8 = 12;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn table() {
assert_eq!(key_to_hz(69), 440.0);
assert!((key_to_hz(60) - 261.625_565_300_598_6).abs() < 1e-9);
assert_eq!(key_to_hz(200), KEY_HZ[127]);
assert_eq!(round_u32(2.5), 3);
assert_eq!(round_u32(-1.0), 0);
}
}