use crate::binary::Sequencer;
use crate::Synth;
pub const MAX_SFX: usize = 4;
pub const MAX_SFX_TRACKS: usize = 4;
const PARAM_IDS: usize = 256;
const CHORD_MAX: usize = 8;
mod bgm;
mod channels;
mod sfx;
mod shadow;
pub use bgm::BgmSynth;
pub use channels::{SfxChannels, SfxError};
pub use sfx::SfxSynth;
use shadow::{Bits, ParamTable, Shadow, Slot};
#[derive(Debug, Clone)]
pub struct Interrupt<const CH: usize, const K: usize> {
shadow: [Shadow<K>; CH],
owner: [u8; CH],
synth_wide: Bits,
global: ParamTable<K>,
global_initial: ParamTable<K>,
defaults: ParamTable<K>,
default_tone: Option<u8>,
held: Bits,
slots: [Slot<K>; MAX_SFX],
wave_seq: u32,
}
impl<const CH: usize, const K: usize> Default for Interrupt<CH, K> {
fn default() -> Self {
Self::new()
}
}
impl<const CH: usize, const K: usize> Interrupt<CH, K> {
pub const fn new() -> Self {
Interrupt {
shadow: [Shadow::EMPTY; CH],
owner: [0; CH],
synth_wide: Bits::EMPTY,
global: ParamTable::EMPTY,
global_initial: ParamTable::EMPTY,
defaults: ParamTable::EMPTY,
default_tone: None,
held: Bits::EMPTY,
slots: [Slot::EMPTY; MAX_SFX],
wave_seq: 0,
}
}
pub fn set_param_info(&mut self, id: u8, default: u32, synth_wide: bool) {
self.defaults.put(id, default);
if synth_wide {
self.synth_wide.set(id);
} else {
self.synth_wide.clear(id);
}
}
pub fn set_default_tone(&mut self, tone: u8) {
self.default_tone = Some(tone);
}
pub fn borrowed(&self) -> u32 {
let mut m = 0u32;
for (i, o) in self.owner.iter().enumerate().take(32) {
if *o != 0 {
m |= 1 << i;
}
}
m
}
pub fn is_active(&self, slot: usize) -> bool {
self.slots.get(slot).is_some_and(|s| s.active)
}
pub fn any_active(&self) -> bool {
self.slots.iter().any(|s| s.active)
}
pub fn audible(&self, bgm_audible: u32) -> u32 {
let b = self.borrowed();
(bgm_audible & !b) | b
}
pub fn bgm<'c, C: Synth + ?Sized>(&'c mut self, synth: &'c mut C) -> BgmSynth<'c, C, CH, K> {
BgmSynth { synth, ints: self }
}
pub fn sfx<'c, C: Synth + ?Sized>(
&'c mut self,
synth: &'c mut C,
slot: usize,
) -> SfxSynth<'c, C, CH, K> {
SfxSynth {
synth,
ints: self,
slot: slot.min(MAX_SFX - 1),
}
}
pub fn clear(&mut self) {
self.shadow = [Shadow::EMPTY; CH];
self.owner = [0; CH];
self.global = ParamTable::EMPTY;
self.global_initial = ParamTable::EMPTY;
self.held = Bits::EMPTY;
self.slots = [Slot::EMPTY; MAX_SFX];
self.wave_seq = 0;
}
fn initial_of(&self, c: usize, id: u8) -> Option<u32> {
if self.synth_wide.has(id) {
self.global_initial.get(id)
} else {
self.shadow[c].initial.get(id)
}
.or_else(|| self.defaults.get(id))
}
pub fn reset_bgm<C: Synth + ?Sized>(&mut self, synth: &mut C) {
let n = synth.channels().min(CH);
for c in 0..n {
let sh = self.shadow[c];
self.shadow[c] = Shadow::EMPTY;
if self.owner[c] != 0 {
continue;
}
synth.note_off(c);
synth.set_pitch(c, 0);
if let Some(t) = self.default_tone {
synth.set_tone(c, t);
}
for (id, _) in sh.params.iter() {
if let Some(v) = sh.initial.get(id).or_else(|| self.defaults.get(id)) {
synth.set_param(c, id, v);
}
}
self.shadow[c].initial = sh.initial;
}
let global = self.global;
self.global = ParamTable::EMPTY;
for (id, _) in global.iter() {
if self.held.has(id) {
continue;
}
if let Some(v) = self
.global_initial
.get(id)
.or_else(|| self.defaults.get(id))
{
synth.set_param(0, id, v);
}
}
}
pub fn start<C: Synth + ?Sized>(
&mut self,
synth: &mut C,
slot: usize,
tracks: usize,
channels: SfxChannels,
priority: u8,
tone: u8,
) -> Result<u8, SfxError> {
if slot >= MAX_SFX {
return Err(SfxError::BadSlot);
}
let n = synth.channels().min(CH).min(32);
let map = channels.resolve(tracks, n).ok_or(SfxError::BadChannels)?;
let mut replace = 0u8;
for &ch in &map[..tracks] {
let o = self.owner[ch as usize];
if o != 0 && o as usize - 1 != slot {
let other = &self.slots[o as usize - 1];
if other.priority > priority {
return Err(SfxError::Busy);
}
replace |= 1 << (o - 1);
}
}
let mut new_mask = 0u32;
for &ch in &map[..tracks] {
new_mask |= 1 << ch;
}
for i in 0..MAX_SFX {
if replace & (1 << i) != 0 || (i == slot && self.slots[i].active) {
self.release_slot(synth, i, new_mask);
}
}
let s = &mut self.slots[slot];
*s = Slot {
active: true,
priority,
map,
tracks: tracks as u8,
touched: Bits::EMPTY,
touched_track: Bits::EMPTY,
saved: [ParamTable::EMPTY; MAX_SFX_TRACKS],
saved_global: ParamTable::EMPTY,
};
for &ch in &map[..tracks] {
let c = ch as usize;
self.owner[c] = slot as u8 + 1;
synth.note_off(c);
synth.set_pitch(c, 0);
synth.set_tone(c, tone);
let sh = self.shadow[c];
for (id, _) in sh.params.iter() {
if let Some(v) = self.initial_of(c, id) {
synth.set_param(c, id, v);
}
}
}
synth.set_sfx_mask(self.borrowed());
Ok(replace)
}
pub fn stop<C: Synth + ?Sized>(&mut self, synth: &mut C, slot: usize) {
if slot < MAX_SFX && self.slots[slot].active {
self.release_slot(synth, slot, 0);
synth.set_sfx_mask(self.borrowed());
}
}
pub fn stop_all<C: Synth + ?Sized>(&mut self, synth: &mut C) {
for i in 0..MAX_SFX {
self.stop(synth, i);
}
}
fn release_slot<C: Synth + ?Sized>(&mut self, synth: &mut C, slot: usize, keep: u32) {
let s = self.slots[slot];
self.slots[slot] = Slot::EMPTY;
for (t, &ch) in s.map[..s.tracks as usize].iter().enumerate() {
let c = ch as usize;
if self.owner[c] != slot as u8 + 1 {
continue;
}
self.owner[c] = 0;
if keep & (1 << c) == 0 {
self.restore_channel(synth, c, &s.touched_track, &s.saved[t]);
} else {
self.restore_params(synth, c, &s.touched_track, &s.saved[t]);
}
}
for id in 0..=u8::MAX {
if !s.touched.has(id) {
continue;
}
if self.slots.iter().any(|o| o.active && o.touched.has(id)) {
continue;
}
self.held.clear(id);
let v = self
.global
.get(id)
.or_else(|| s.saved_global.get(id))
.or_else(|| self.defaults.get(id));
if let Some(v) = v {
synth.set_param(0, id, v);
}
}
}
fn restore_channel<C: Synth + ?Sized>(
&mut self,
synth: &mut C,
c: usize,
touched: &Bits,
saved: &ParamTable<K>,
) {
let sh = self.shadow[c];
synth.note_off(c);
if sh.wave_before_tone {
if let Some(w) = sh.wave {
synth.set_wave(c, &w);
}
}
if let Some(t) = sh.tone.or(self.default_tone) {
synth.set_tone(c, t);
}
if !sh.wave_before_tone {
if let Some(w) = sh.wave {
synth.set_wave(c, &w);
}
}
if let Some(p) = synth.wave_partner(c) {
if let Some(ps) = self.shadow.get(p).copied() {
if ps.wave_seq > sh.wave_seq {
match (ps.wave, ps.tone) {
(Some(w), _) if !ps.wave_before_tone || ps.tone.is_none() => {
synth.set_wave(c, &w)
}
(_, Some(t)) => synth.set_tone(c, t),
_ => {}
}
}
}
}
self.restore_params(synth, c, touched, saved);
synth.set_pitch(c, sh.pitch);
if sh.on {
if sh.nkeys > 1 {
synth.chord_on(c, &sh.keys[..sh.nkeys as usize], sh.vel, false);
} else {
synth.note_on(c, sh.keys[0], sh.vel);
}
}
}
fn restore_params<C: Synth + ?Sized>(
&self,
synth: &mut C,
c: usize,
touched: &Bits,
saved: &ParamTable<K>,
) {
let sh = &self.shadow[c];
for (id, v) in sh.params.iter() {
synth.set_param(c, id, v);
}
for (id, v) in saved.iter() {
if touched.has(id) && sh.params.get(id).is_none() {
synth.set_param(c, id, v);
}
}
for (id, v) in self.defaults.iter() {
if !self.synth_wide.has(id)
&& touched.has(id)
&& sh.params.get(id).is_none()
&& saved.get(id).is_none()
{
synth.set_param(c, id, v);
}
}
}
pub fn render<C: Synth + ?Sized, const N: usize>(
&mut self,
synth: &mut C,
bgm: &mut Sequencer<'_, N>,
sfx: &mut [&mut dyn SfxSource],
out: &mut [[f32; 2]],
) {
let volume = bgm.volume();
for frame in out.iter_mut() {
if bgm.is_paused() {
*frame = [0.0, 0.0];
continue;
}
let bgm_on = bgm.step(&mut self.bgm(synth));
for (i, s) in sfx.iter_mut().enumerate().take(MAX_SFX) {
if !self.slots[i].active {
continue;
}
let alive = s.step(&mut self.sfx(synth, i));
if !alive {
self.stop(synth, i);
}
}
if !bgm_on && !self.any_active() {
*frame = [0.0, 0.0];
continue;
}
let [l, r] = synth.next(self.audible(bgm.audible()));
*frame = [(l * volume).clamp(-1.0, 1.0), (r * volume).clamp(-1.0, 1.0)];
}
}
fn owned_by_bgm(&self, ch: usize) -> bool {
self.owner.get(ch).is_none_or(|o| *o == 0)
}
}
pub trait SfxSource {
fn step(&mut self, synth: &mut dyn Synth) -> bool;
}
impl<const N: usize> SfxSource for Sequencer<'_, N> {
fn step(&mut self, synth: &mut dyn Synth) -> bool {
self.is_paused() || Sequencer::step(self, synth)
}
}