use pikopiko_runtime::synth::ay::{AyConfig, AySynth, AyVariant, Pan};
use super::{
hz_to_key, OptionInfo, OptionKind, ParamInfo, Scope, SynthConfig, SynthSpec, ToneInfo,
};
const TONES: &[ToneInfo] = &[ToneInfo { id: 0 }, ToneInfo { id: 1 }, ToneInfo { id: 2 }];
const PARAMS: &[ParamInfo] = &[
ParamInfo {
name: "env",
min: 0,
max: 1,
default: 0,
scope: Scope::Track,
},
ParamInfo {
name: "env_shape",
min: 0,
max: 15,
default: 0,
scope: Scope::Synth,
},
ParamInfo {
name: "env_period",
min: 1,
max: 65535,
default: 1024,
scope: Scope::Synth,
},
ParamInfo {
name: "noise",
min: 0,
max: 31,
default: 0,
scope: Scope::Synth,
},
ParamInfo {
name: "pan",
min: 0,
max: 16,
default: 8,
scope: Scope::Track,
},
];
const OPTIONS_AY: &[OptionInfo] = &[
OptionInfo {
name: "clock",
kind: OptionKind::Range {
min: 100_000,
max: 10_000_000,
},
default: "1773400",
},
OptionInfo {
name: "pan",
kind: OptionKind::Choice(&["mono", "abc", "acb"]),
default: "mono",
},
];
const OPTIONS_YM: &[OptionInfo] = &[
OptionInfo {
name: "clock",
kind: OptionKind::Range {
min: 100_000,
max: 10_000_000,
},
default: "2000000",
},
OptionInfo {
name: "pan",
kind: OptionKind::Choice(&["mono", "abc", "acb"]),
default: "mono",
},
];
fn key_range(cfg: &SynthConfig) -> (u8, u8) {
let clock = cfg.get_u32("clock").unwrap_or(1_773_400) as f64;
let lo = hz_to_key(clock / (16.0 * 4095.0)).ceil().clamp(0.0, 127.0) as u8;
let hi = hz_to_key(clock / 16.0).floor().clamp(0.0, 127.0) as u8;
(lo, hi)
}
pub const AY_3_8912: SynthSpec = SynthSpec {
texts: crate::text_table::CATALOGS,
name: "ay-3-8912",
aliases: &["ay-3-8910", "ay-3-8913", "ay8910", "ay8912", "ay"],
max_tracks: 3,
polyphony: 1,
tones: TONES,
default_tone: 0,
unpitched_tones: &[1],
duty_tones: false,
wave_memory: false,
params: PARAMS,
options: OPTIONS_AY,
presets: &[],
knobs: &[],
tone_files: super::tones::chip::AY_FILES,
check_sample: None,
key_range,
create: |cfg, sr, n| Box::new(AySynth::new(ay_config(AyVariant::Ay, cfg), sr, n)),
};
pub const YM2149: SynthSpec = SynthSpec {
texts: crate::text_table::CATALOGS,
name: "ym2149",
aliases: &["ym2149f"],
max_tracks: 3,
polyphony: 1,
tones: TONES,
default_tone: 0,
unpitched_tones: &[1],
duty_tones: false,
wave_memory: false,
params: PARAMS,
options: OPTIONS_YM,
presets: &[],
knobs: &[],
tone_files: &[],
check_sample: None,
key_range,
create: |cfg, sr, n| Box::new(AySynth::new(ay_config(AyVariant::Ym, cfg), sr, n)),
};
pub fn ay_config(variant: AyVariant, cfg: &SynthConfig) -> AyConfig {
let base = match variant {
AyVariant::Ay => AyConfig::ay(),
AyVariant::Ym => AyConfig::ym(),
};
AyConfig {
variant,
clock_hz: cfg.get_u32("clock").unwrap_or(base.clock_hz),
pan: match cfg.get("pan") {
Some("abc") => Pan::Abc,
Some("acb") => Pan::Acb,
_ => Pan::Mono,
},
}
}