#[cfg(feature = "apu2a03")]
use super::apu2a03;
#[cfg(feature = "dmg")]
use super::dmg;
#[cfg(feature = "scc")]
use super::scc;
#[cfg(feature = "sfx")]
use super::sfx;
#[cfg(feature = "sfx-hd")]
use super::sfx_hd;
#[cfg(feature = "sn76489")]
use super::sn76489;
#[cfg(feature = "va")]
use super::va;
use super::{SynthConfig, SynthSpec, AY_3_8912, BASIC, BUZZER, YM2149};
use crate::synth::Synth;
pub const BUILTIN: &[&SynthSpec] = &[&BASIC, &AY_3_8912, &YM2149, &BUZZER];
pub static ALL: &[&SynthSpec] = &[
&BASIC,
&AY_3_8912,
&YM2149,
&BUZZER,
#[cfg(feature = "sn76489")]
&sn76489::SN76489,
#[cfg(feature = "apu2a03")]
&apu2a03::APU_2A03,
#[cfg(feature = "scc")]
&scc::SCC,
#[cfg(feature = "dmg")]
&dmg::DMG,
#[cfg(feature = "va")]
&va::VA,
#[cfg(feature = "sfx")]
&sfx::SFX,
#[cfg(feature = "sfx-hd")]
&sfx_hd::SFX_HD,
];
static FROZEN: std::sync::OnceLock<&'static [&'static SynthSpec]> = std::sync::OnceLock::new();
pub(super) fn frozen() -> bool {
FROZEN.get().is_some()
}
pub fn all() -> &'static [&'static SynthSpec] {
FROZEN.get_or_init(|| {
let extra = super::derived::EXTRA
.lock()
.map(|e| e.clone())
.unwrap_or_default();
if extra.is_empty() {
ALL
} else {
let mut v = ALL.to_vec();
v.extend(extra);
Box::leak(v.into_boxed_slice())
}
})
}
pub fn options() -> crate::CompileOptions<'static> {
crate::CompileOptions {
synths: all(),
..crate::CompileOptions::default()
}
}
pub fn compile(src: &str) -> (Option<crate::Song>, Vec<crate::Diagnostic>) {
crate::compile_with(src, &options())
}
pub fn player(song: std::sync::Arc<crate::Song>, sample_rate: u32) -> crate::Player {
match find(all(), &song.synth.name) {
Some(spec) => crate::Player::with_spec(song, sample_rate, spec),
None => crate::Player::new(song, sample_rate),
}
}
pub fn render(song: &crate::Song, sample_rate: u32, loops: u32) -> Vec<[f32; 2]> {
let synth = create(all(), &song.synth, sample_rate, song.tracks.len());
crate::render_to_vec_looped_with(song, sample_rate, synth, loops)
}
pub fn find<'a>(synths: &[&'a SynthSpec], name: &str) -> Option<&'a SynthSpec> {
synths.iter().copied().find(|c| c.matches(name))
}
pub fn create_builtin(config: &SynthConfig, sample_rate: u32, channels: usize) -> Box<dyn Synth> {
create(BUILTIN, config, sample_rate, channels)
}
pub fn create(
synths: &[&SynthSpec],
config: &SynthConfig,
sample_rate: u32,
channels: usize,
) -> Box<dyn Synth> {
let spec = find(synths, &config.name).unwrap_or(&BASIC);
(spec.create)(config, sample_rate, channels)
}