use super::*;
pub struct BgmSynth<'c, C: Synth + ?Sized, const CH: usize, const K: usize> {
pub(super) synth: &'c mut C,
pub(super) ints: &'c mut Interrupt<CH, K>,
}
impl<C: Synth + ?Sized, const CH: usize, const K: usize> Synth for BgmSynth<'_, C, CH, K> {
fn channels(&self) -> usize {
self.synth.channels()
}
fn reset(&mut self) {
if self.ints.any_active() {
self.ints.reset_bgm(&mut *self.synth);
} else {
self.synth.reset();
self.ints.clear();
}
}
fn set_tone(&mut self, ch: usize, tone: u8) {
self.ints.wave_seq = self.ints.wave_seq.wrapping_add(1).max(1);
if let Some(s) = self.ints.shadow.get_mut(ch) {
s.tone = Some(tone);
s.wave_before_tone = s.wave.is_some();
s.wave_seq = self.ints.wave_seq;
}
if self.ints.owned_by_bgm(ch) {
self.synth.set_tone(ch, tone);
}
}
fn set_wave(&mut self, ch: usize, wave: &[i8]) {
if let Some(s) = self.ints.shadow.get_mut(ch) {
let mut w = [0i8; crate::WAVE_LEN];
for (d, v) in w.iter_mut().zip(wave) {
*d = *v;
}
s.wave = Some(w);
s.wave_before_tone = false;
s.wave_seq = self.ints.wave_seq.wrapping_add(1).max(1);
self.ints.wave_seq = s.wave_seq;
}
if self.ints.owned_by_bgm(ch) {
self.synth.set_wave(ch, wave);
}
}
fn wave_partner(&self, ch: usize) -> Option<usize> {
self.synth.wave_partner(ch)
}
fn set_param(&mut self, ch: usize, param: u8, value: u32) {
if self.ints.synth_wide.has(param) {
if self.ints.global.get(param).is_none()
&& self.ints.global_initial.get(param).is_none()
{
if let Some(v) = self.synth.param(ch, param) {
self.ints.global_initial.put(param, v);
}
}
self.ints.global.put(param, value);
if !self.ints.held.has(param) {
self.synth.set_param(ch, param, value);
}
return;
}
if let Some(s) = self.ints.shadow.get_mut(ch) {
if s.params.get(param).is_none() && s.initial.get(param).is_none() {
if let Some(v) = self.synth.param(ch, param) {
s.initial.put(param, v);
}
}
s.params.put(param, value);
}
if self.ints.owned_by_bgm(ch) {
self.synth.set_param(ch, param, value);
}
}
fn param(&self, ch: usize, param: u8) -> Option<u32> {
self.synth.param(ch, param)
}
fn set_param_hi(&mut self, ch: usize, param: u8, value: u32) {
if self.ints.owned_by_bgm(ch) {
self.synth.set_param_hi(ch, param, value);
}
}
fn param_hi(&self, ch: usize, param: u8) -> Option<u32> {
self.synth.param_hi(ch, param)
}
fn note_on(&mut self, ch: usize, key: u8, vel: u8) {
if let Some(s) = self.ints.shadow.get_mut(ch) {
s.on = true;
s.keys[0] = key;
s.nkeys = 1;
s.vel = vel;
}
if self.ints.owned_by_bgm(ch) {
self.synth.note_on(ch, key, vel);
}
}
fn note_off(&mut self, ch: usize) {
if let Some(s) = self.ints.shadow.get_mut(ch) {
s.on = false;
}
if self.ints.owned_by_bgm(ch) {
self.synth.note_off(ch);
}
}
fn set_volume(&mut self, ch: usize, vel: u8) {
if let Some(s) = self.ints.shadow.get_mut(ch) {
if s.on {
s.vel = vel;
}
}
if self.ints.owned_by_bgm(ch) {
self.synth.set_volume(ch, vel);
}
}
fn set_pitch(&mut self, ch: usize, cents_q8: i32) {
if let Some(s) = self.ints.shadow.get_mut(ch) {
s.pitch = cents_q8;
}
if self.ints.owned_by_bgm(ch) {
self.synth.set_pitch(ch, cents_q8);
}
}
fn next(&mut self, audible: u32) -> [f32; 2] {
let mask = self.ints.audible(audible);
self.synth.next(mask)
}
fn set_tempo(&mut self, bpm_milli: u32) {
self.synth.set_tempo(bpm_milli);
}
fn chord_on(&mut self, ch: usize, keys: &[u8], vel: u8, legato: bool) {
if let Some(s) = self.ints.shadow.get_mut(ch) {
let n = keys.len().min(CHORD_MAX);
s.keys[..n].copy_from_slice(&keys[..n]);
s.nkeys = n as u8;
s.on = n > 0;
s.vel = vel;
}
if self.ints.owned_by_bgm(ch) {
self.synth.chord_on(ch, keys, vel, legato);
}
}
fn shot_duck(&mut self) {
self.synth.shot_duck()
}
fn mix_input(&mut self, frame: [f32; 2]) -> bool {
self.synth.mix_input(frame)
}
fn tail_frames(&self) -> u32 {
self.synth.tail_frames()
}
fn latency_frames(&self) -> u32 {
self.synth.latency_frames()
}
}