use crate::synth::{ParamInfo, Scope, Synth, SynthSpec, ToneInfo};
use crate::synth::sfx_hd::engine::{Limiter, Track};
use crate::synth::sfx_hd::params::{Category, HdParams, COUNT, PARAMS};
const MAX_TRACKS: usize = 8;
const MAX_TAIL_SECS: u32 = 10;
const fn build_params() -> [ParamInfo; COUNT] {
let mut out = [ParamInfo {
name: "",
min: 0,
max: 0,
default: 0,
scope: Scope::Track,
}; COUNT];
let mut i = 0;
while i < COUNT {
let d = PARAMS[i];
out[i] = ParamInfo {
name: d.name,
min: d.min as u32,
max: d.max as u32,
default: d.default as u32,
scope: Scope::Track,
};
i += 1;
}
out
}
const SYNTH_PARAMS: [ParamInfo; COUNT] = build_params();
const fn t(id: u8) -> ToneInfo {
ToneInfo { id }
}
const TONES: [ToneInfo; 10] = [t(0), t(1), t(2), t(3), t(4), t(5), t(6), t(7), t(8), t(9)];
pub const SFX_HD: SynthSpec = SynthSpec {
texts: crate::synth::sfx_hd::text_table::CATALOGS,
name: "sfx-hd",
aliases: &[],
max_tracks: MAX_TRACKS,
polyphony: 1,
tones: &TONES,
default_tone: 0,
unpitched_tones: &[],
duty_tones: false,
wave_memory: false,
params: &SYNTH_PARAMS,
options: &[],
presets: crate::synth::sfx_hd::presets::PRESETS,
knobs: &[],
tone_files: crate::synth::tones::sfx_hd::FILES,
check_sample: None,
key_range: |_| (0, 127),
create: |_, sr, n| Box::new(SfxHdSynth::new(sr, n)),
};
struct Channel {
params: HdParams,
track: Track,
key: u8,
cents_q8: i32,
vel: u8,
}
impl Channel {
fn offset(&self) -> f32 {
(self.key as f32 - 60.0) + self.cents_q8 as f32 / 25_600.0
}
}
pub struct SfxHdSynth {
sample_rate: u32,
presets: [HdParams; 10],
channels: Vec<Channel>,
limiter: Limiter,
}
impl SfxHdSynth {
pub fn new(sample_rate: u32, channels: usize) -> Self {
let sample_rate = sample_rate.max(1);
let presets = Category::ALL.map(Category::preset);
SfxHdSynth {
sample_rate,
presets,
channels: (0..channels.min(MAX_TRACKS))
.map(|_| Channel {
params: presets[0],
track: Track::new(sample_rate),
key: 60,
cents_q8: 0,
vel: 15,
})
.collect(),
limiter: Limiter::new(sample_rate),
}
}
}
impl Synth for SfxHdSynth {
fn channels(&self) -> usize {
self.channels.len()
}
fn reset(&mut self) {
for c in &mut self.channels {
c.params = self.presets[0];
c.track.reset();
c.key = 60;
c.cents_q8 = 0;
c.vel = 15;
}
self.limiter.reset();
}
fn set_tone(&mut self, ch: usize, tone: u8) {
if let (Some(c), Some(p)) = (self.channels.get_mut(ch), self.presets.get(tone as usize)) {
c.params = *p;
}
}
fn set_param(&mut self, ch: usize, param: u8, value: u32) {
if let Some(c) = self.channels.get_mut(ch) {
c.params.set(param as usize, value);
}
}
fn param(&self, ch: usize, param: u8) -> Option<u32> {
let i = param as usize;
if i >= COUNT {
return None;
}
self.channels.get(ch).map(|c| c.params.get(i) as u32)
}
fn note_on(&mut self, ch: usize, key: u8, vel: u8) {
if let Some(c) = self.channels.get_mut(ch) {
c.key = key;
c.vel = vel;
let off = c.offset();
c.track.start(&c.params, off);
}
}
fn note_off(&mut self, _ch: usize) {
}
fn set_volume(&mut self, ch: usize, vel: u8) {
if let Some(c) = self.channels.get_mut(ch) {
c.vel = vel;
}
}
fn set_pitch(&mut self, ch: usize, cents_q8: i32) {
if let Some(c) = self.channels.get_mut(ch) {
c.cents_q8 = cents_q8;
let off = c.offset();
c.track.voice.set_offset(off);
}
}
fn next(&mut self, audible: u32) -> [f32; 2] {
let mut out = [0.0f32; 2];
for (i, c) in self.channels.iter_mut().enumerate() {
let gain = c.vel.min(15) as f32 / 15.0;
let s = c.track.next(gain, audible & (1 << i) != 0);
out[0] += s[0];
out[1] += s[1];
}
self.limiter.process(out)
}
fn tail_frames(&self) -> u32 {
let longest = self
.channels
.iter()
.map(|c| c.track.remaining())
.max()
.unwrap_or(0);
longest.min(self.sample_rate * MAX_TAIL_SECS)
}
}