use super::{text_key, Knob, SynthConfig, ToneFile};
use crate::synth::Synth;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ToneInfo {
pub id: u8,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
pub enum Scope {
Track,
Synth,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ParamInfo {
pub name: &'static str,
pub min: u32,
pub max: u32,
pub default: u32,
pub scope: Scope,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OptionKind {
Range { min: u32, max: u32 },
Choice(&'static [&'static str]),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct OptionInfo {
pub name: &'static str,
pub kind: OptionKind,
pub default: &'static str,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SynthPreset {
pub name: &'static str,
pub kind: &'static str,
pub body: &'static str,
}
impl SynthPreset {
pub fn macro_line(&self) -> String {
format!("${} = {}", self.name, self.body)
}
}
pub type CheckSample = fn(&str, &[u8]) -> Result<(), crate::compiler::diagnostic::Message>;
pub struct SynthSpec {
pub name: &'static str,
pub aliases: &'static [&'static str],
pub texts: &'static [crate::text::Catalog],
pub max_tracks: usize,
pub polyphony: u8,
pub tones: &'static [ToneInfo],
pub default_tone: u8,
pub unpitched_tones: &'static [u8],
pub duty_tones: bool,
pub wave_memory: bool,
pub params: &'static [ParamInfo],
pub options: &'static [OptionInfo],
pub presets: &'static [SynthPreset],
pub knobs: &'static [Knob],
pub tone_files: &'static [ToneFile],
pub check_sample: Option<CheckSample>,
pub key_range: fn(&SynthConfig) -> (u8, u8),
pub create: fn(&SynthConfig, u32, usize) -> Box<dyn Synth>,
}
impl core::fmt::Debug for SynthSpec {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("SynthSpec")
.field("name", &self.name)
.finish()
}
}
impl SynthSpec {
pub fn text(&self, key: &str, lang: &str) -> &'static str {
match crate::text::find(self.texts, lang, key) {
Some([crate::text::Seg::Lit(t)]) => t,
_ => "",
}
}
pub fn description(&self, lang: &str) -> &'static str {
self.text(&text_key::synth(self.name), lang)
}
pub fn tone_description(&self, id: u8, lang: &str) -> &'static str {
self.text(&text_key::tone(self.name, id), lang)
}
pub fn param_description(&self, name: &str, lang: &str) -> &'static str {
self.text(&text_key::param(self.name, name), lang)
}
pub fn option_description(&self, name: &str, lang: &str) -> &'static str {
self.text(&text_key::option(self.name, name), lang)
}
pub fn preset_description(&self, name: &str, lang: &str) -> &'static str {
self.text(&self.preset_text_key(name), lang)
}
pub fn preset_text_key(&self, name: &str) -> String {
if self
.tone_files
.iter()
.any(|f| f.tones().any(|(n, _)| n == name))
{
text_key::library(self.name, name)
} else {
text_key::preset(self.name, name)
}
}
pub fn knob_label(&self, name: &str, lang: &str) -> &'static str {
self.text(&text_key::knob(self.name, name), lang)
}
pub fn knob_description(&self, name: &str, lang: &str) -> &'static str {
self.text(&text_key::knob_help(self.name, name), lang)
}
pub fn knob(&self, name: &str) -> Option<&'static Knob> {
self.knobs.iter().find(|k| k.name == name)
}
pub fn is_unpitched(&self, tone: u8) -> bool {
self.unpitched_tones.contains(&tone)
}
pub fn library_description(&self, name: &str, lang: &str) -> &'static str {
self.text(&text_key::library(self.name, name), lang)
}
pub fn library_prefix(&self) -> &'static str {
self.aliases
.iter()
.copied()
.fold(self.name, |a, b| if b.len() < a.len() { b } else { a })
}
pub fn tone_file(&self, name: &str) -> Option<&'static ToneFile> {
self.tone_files.iter().find(|f| f.name == name)
}
pub fn preset(&self, name: &str) -> Option<&'static SynthPreset> {
self.presets.iter().find(|p| p.name == name)
}
pub fn has_tone(&self, tone: u8) -> bool {
self.tones.iter().any(|t| t.id == tone)
}
pub fn param_index(&self, name: &str) -> Option<usize> {
self.params.iter().position(|p| p.name == name)
}
pub fn matches(&self, name: &str) -> bool {
self.name == name || self.aliases.contains(&name)
}
}