use super::*;
pub(super) type Ints = Interrupt<32, 64>;
pub(super) struct Routed {
pub(super) inner: Box<dyn Synth>,
pub(super) ints: Option<Box<Ints>>,
}
macro_rules! route {
($self:ident, $name:ident ( $($arg:expr),* )) => {
match $self.ints.as_deref_mut() {
Some(i) => i.bgm(&mut *$self.inner).$name($($arg),*),
None => $self.inner.$name($($arg),*),
}
};
}
impl Synth for Routed {
fn channels(&self) -> usize {
self.inner.channels()
}
fn reset(&mut self) {
route!(self, reset())
}
fn set_tone(&mut self, ch: usize, tone: u8) {
route!(self, set_tone(ch, tone))
}
fn prepare_tone(&mut self, tone: u8) {
self.inner.prepare_tone(tone)
}
fn set_wave(&mut self, ch: usize, wave: &[i8]) {
route!(self, set_wave(ch, wave))
}
fn set_param(&mut self, ch: usize, param: u8, value: u32) {
route!(self, set_param(ch, param, value))
}
fn set_param_hi(&mut self, ch: usize, param: u8, value: u32) {
route!(self, set_param_hi(ch, param, value))
}
fn param_hi(&self, ch: usize, param: u8) -> Option<u32> {
self.inner.param_hi(ch, param)
}
fn note_on(&mut self, ch: usize, key: u8, vel: u8) {
route!(self, note_on(ch, key, vel))
}
fn note_off(&mut self, ch: usize) {
route!(self, note_off(ch))
}
fn set_volume(&mut self, ch: usize, vel: u8) {
route!(self, set_volume(ch, vel))
}
fn set_pitch(&mut self, ch: usize, cents_q8: i32) {
route!(self, set_pitch(ch, cents_q8))
}
fn next(&mut self, audible: u32) -> [f32; 2] {
route!(self, next(audible))
}
fn set_tempo(&mut self, bpm_milli: u32) {
self.inner.set_tempo(bpm_milli)
}
fn chord_on(&mut self, ch: usize, keys: &[u8], vel: u8, legato: bool) {
route!(self, chord_on(ch, keys, vel, legato))
}
fn tail_frames(&self) -> u32 {
self.inner.tail_frames()
}
fn latency_frames(&self) -> u32 {
self.inner.latency_frames()
}
fn param(&self, ch: usize, param: u8) -> Option<u32> {
self.inner.param(ch, param)
}
fn set_sfx_mask(&mut self, mask: u32) {
self.inner.set_sfx_mask(mask)
}
fn shot_duck(&mut self) {
self.inner.shot_duck()
}
fn mix_input(&mut self, frame: [f32; 2]) -> bool {
route!(self, mix_input(frame))
}
}
pub(super) struct SfxEntry {
pub(super) data: Box<[u8]>,
pub(super) tracks: u8,
pub(super) tone: u8,
}
pub(super) type SfxSeq<'a> = Sequencer<'a, MAX_SFX_TRACKS>;
impl Player {
pub fn sfx_active(&self) -> bool {
self.sfx_playing.iter().any(Option::is_some)
}
pub fn is_sfx_playing(&self, id: SfxId) -> bool {
self.sfx_playing.contains(&Some(id.0))
}
pub fn register_sfx(&mut self, sfx: &Song) -> Result<SfxId, SfxError> {
if sfx.synth.name != self.song.synth.name {
return Err(SfxError::SynthMismatch);
}
if sfx.tracks.is_empty() || sfx.tracks.len() > MAX_SFX_TRACKS {
return Err(SfxError::BadTracks);
}
if self.sfx.len() >= MAX_REGISTERED_SFX {
return Err(SfxError::Full);
}
super::announce_tones(self.synth.inner.as_mut(), sfx);
let data = crate::export::binary::encode(sfx)
.map_err(|_| SfxError::Encode)?
.into_boxed_slice();
let seq = SfxSeq::new(&data, self.sample_rate)
.map_err(|_| SfxError::Encode)?
.unbind();
if self.synth.ints.is_none() {
let mut ints = Box::new(Ints::new());
for &(id, default, wide) in &self.param_info {
ints.set_param_info(id, default, wide);
}
ints.set_default_tone(self.song.default_tone);
self.synth.ints = Some(ints);
}
let id = SfxId(self.sfx.len() as u8);
self.sfx.push(SfxEntry {
data,
tracks: sfx.tracks.len() as u8,
tone: sfx.default_tone,
});
self.sfx_seqs.push(Some(seq));
Ok(id)
}
pub fn play_sfx(
&mut self,
id: SfxId,
channels: SfxChannels,
priority: u8,
looping: bool,
) -> Result<(), SfxError> {
let r = self.try_play_sfx(id, channels, priority, looping);
if r.is_err() {
self.notices.push(Notice::SfxRejected(id));
}
r
}
pub(super) fn try_play_sfx(
&mut self,
id: SfxId,
channels: SfxChannels,
priority: u8,
looping: bool,
) -> Result<(), SfxError> {
let e = id.0 as usize;
let entry = self.sfx.get(e).ok_or(SfxError::Unknown)?;
let (tracks, tone) = (entry.tracks as usize, entry.tone);
let Some(ints) = self.synth.ints.as_deref_mut() else {
return Err(SfxError::Unknown);
};
let slot = match self.sfx_playing.iter().position(|p| *p == Some(id.0)) {
Some(s) => s,
None => self
.sfx_playing
.iter()
.position(Option::is_none)
.ok_or(SfxError::Busy)?,
};
let replaced = ints
.start(
&mut *self.synth.inner,
slot,
tracks,
channels,
priority,
tone,
)
.map_err(|err| match err {
interrupt::SfxError::Busy => SfxError::Busy,
_ => SfxError::BadChannels,
})?;
for i in 0..MAX_SFX {
if replaced & (1 << i) != 0 {
if let Some(old) = self.sfx_playing[i].take() {
self.notices.push(Notice::SfxFinished(SfxId(old)));
}
}
}
if let Some(seq) = self.sfx_seqs[e].as_mut() {
seq.restart();
seq.set_looping(looping);
}
self.sfx_playing[slot] = Some(id.0);
Ok(())
}
pub fn stop_sfx(&mut self, id: SfxId) {
if let Some(slot) = self.sfx_playing.iter().position(|p| *p == Some(id.0)) {
if let Some(ints) = self.synth.ints.as_deref_mut() {
ints.stop(&mut *self.synth.inner, slot);
}
self.sfx_playing[slot] = None;
self.notices.push(Notice::SfxFinished(id));
}
}
pub fn stop_all_sfx(&mut self) {
for slot in 0..MAX_SFX {
if let Some(id) = self.sfx_playing[slot] {
self.stop_sfx(SfxId(id));
}
}
}
pub(super) fn step_sfx(&mut self, sfx: &mut [Option<(usize, SfxSeq<'_>)>; MAX_SFX]) {
let Some(ints) = self.synth.ints.as_deref_mut() else {
return;
};
for (slot, bound) in sfx.iter_mut().enumerate() {
let Some(id) = self.sfx_playing[slot] else {
continue;
};
let Some((e, seq)) = bound.as_mut() else {
continue;
};
if *e != id as usize {
continue;
}
if !seq.step(&mut ints.sfx(&mut *self.synth.inner, slot)) {
ints.stop(&mut *self.synth.inner, slot);
self.sfx_playing[slot] = None;
self.notices.push(Notice::SfxFinished(SfxId(id)));
}
}
}
pub(super) fn audible_mask(&self) -> u32 {
match self.solo {
Some(s) if s < 32 => 1 << s,
Some(_) => 0,
None => {
let mut m = 0u32;
for (i, muted) in self.muted.iter().enumerate().take(32) {
if !muted {
m |= 1 << i;
}
}
m
}
}
}
}