use std::fmt;
pub const LAYERS: usize = 4;
pub const LAYER_PARAMS: usize = 12;
pub const COUNT: usize = LAYERS * LAYER_PARAMS + 6;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ParamDef {
pub name: &'static str,
pub min: u16,
pub max: u16,
pub default: u16,
}
pub mod layer {
pub const SRC: usize = 0;
pub const LEVEL: usize = 1;
pub const PITCH: usize = 2;
pub const PEND: usize = 3;
pub const PTIME: usize = 4;
pub const ATTACK: usize = 5;
pub const DECAY: usize = 6;
pub const DELAY: usize = 7;
pub const FTYPE: usize = 8;
pub const CUT: usize = 9;
pub const CEND: usize = 10;
pub const RES: usize = 11;
}
pub mod master {
use super::{LAYERS, LAYER_PARAMS};
pub const DRIVE: usize = LAYERS * LAYER_PARAMS;
pub const REV: usize = DRIVE + 1;
pub const REV_SIZE: usize = DRIVE + 2;
pub const LENGTH: usize = DRIVE + 3;
pub const VOL: usize = DRIVE + 4;
pub const PAN: usize = DRIVE + 5;
}
pub mod source {
pub const OFF: u16 = 0;
pub const SINE: u16 = 1;
pub const TRIANGLE: u16 = 2;
pub const SAW: u16 = 3;
pub const SQUARE: u16 = 4;
pub const NOISE: u16 = 5;
pub const METAL: u16 = 6;
}
pub const fn layer_param(l: usize, p: usize) -> usize {
l * LAYER_PARAMS + p
}
const fn d(name: &'static str, min: u16, max: u16, default: u16) -> ParamDef {
ParamDef {
name,
min,
max,
default,
}
}
pub const PARAMS: [ParamDef; COUNT] = [
d("l1_src", 0, 6, 0),
d("l1_level", 0, 100, 80),
d("l1_pitch", 0, 127, 60),
d("l1_pend", 0, 127, 60),
d("l1_ptime", 1, 2000, 100),
d("l1_attack", 0, 1000, 0),
d("l1_decay", 1, 5000, 300),
d("l1_delay", 0, 1000, 0),
d("l1_ftype", 0, 2, 0),
d("l1_cut", 0, 135, 135),
d("l1_cend", 0, 135, 135),
d("l1_res", 0, 100, 0),
d("l2_src", 0, 6, 0),
d("l2_level", 0, 100, 80),
d("l2_pitch", 0, 127, 60),
d("l2_pend", 0, 127, 60),
d("l2_ptime", 1, 2000, 100),
d("l2_attack", 0, 1000, 0),
d("l2_decay", 1, 5000, 300),
d("l2_delay", 0, 1000, 0),
d("l2_ftype", 0, 2, 0),
d("l2_cut", 0, 135, 135),
d("l2_cend", 0, 135, 135),
d("l2_res", 0, 100, 0),
d("l3_src", 0, 6, 0),
d("l3_level", 0, 100, 80),
d("l3_pitch", 0, 127, 60),
d("l3_pend", 0, 127, 60),
d("l3_ptime", 1, 2000, 100),
d("l3_attack", 0, 1000, 0),
d("l3_decay", 1, 5000, 300),
d("l3_delay", 0, 1000, 0),
d("l3_ftype", 0, 2, 0),
d("l3_cut", 0, 135, 135),
d("l3_cend", 0, 135, 135),
d("l3_res", 0, 100, 0),
d("l4_src", 0, 6, 0),
d("l4_level", 0, 100, 80),
d("l4_pitch", 0, 127, 60),
d("l4_pend", 0, 127, 60),
d("l4_ptime", 1, 2000, 100),
d("l4_attack", 0, 1000, 0),
d("l4_decay", 1, 5000, 300),
d("l4_delay", 0, 1000, 0),
d("l4_ftype", 0, 2, 0),
d("l4_cut", 0, 135, 135),
d("l4_cend", 0, 135, 135),
d("l4_res", 0, 100, 0),
d("drive", 0, 100, 0),
d("rev", 0, 100, 0),
d("rev_size", 0, 100, 50),
d("length", 10, 400, 100),
d("vol", 0, 100, 70),
d("pan", 0, 16, 8),
];
pub fn param_index(name: &str) -> Option<usize> {
PARAMS.iter().position(|p| p.name == name)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Category {
Laser = 0,
Blaster = 1,
Explosion = 2,
Impact = 3,
Pickup = 4,
Click = 5,
Confirm = 6,
Whoosh = 7,
Charge = 8,
Zap = 9,
}
const PRESET_TEXT: [&str; 10] = [
"%l1_src=3 %l1_level=80 %l1_pitch=100 %l1_pend=64 %l1_ptime=180 %l1_decay=220 %l1_cut=120 %l1_cend=82 %l1_res=30 \
%l2_src=4 %l2_level=45 %l2_pitch=112 %l2_pend=76 %l2_ptime=150 %l2_decay=160 \
%l3_src=5 %l3_level=25 %l3_decay=40 %l3_ftype=1 %l3_cut=100 %l3_cend=100 \
%drive=20 %rev=20",
"%l1_src=5 %l1_level=90 %l1_decay=120 %l1_cut=110 %l1_cend=80 \
%l2_src=1 %l2_level=90 %l2_pitch=76 %l2_pend=40 %l2_ptime=60 %l2_decay=150 \
%l3_src=4 %l3_level=40 %l3_pitch=96 %l3_pend=60 %l3_ptime=40 %l3_decay=50 \
%drive=40 %rev=25",
"%l1_src=5 %l1_level=100 %l1_attack=5 %l1_decay=1800 %l1_cut=100 %l1_cend=60 %l1_res=10 \
%l2_src=1 %l2_level=100 %l2_pitch=48 %l2_pend=28 %l2_ptime=400 %l2_decay=900 \
%l3_src=5 %l3_level=60 %l3_decay=150 %l3_ftype=2 %l3_cut=105 %l3_cend=105 %l3_res=20 \
%l4_src=5 %l4_level=35 %l4_delay=120 %l4_decay=1200 %l4_ftype=2 %l4_cut=90 %l4_cend=70 \
%drive=50 %rev=40 %rev_size=70",
"%l1_src=1 %l1_level=100 %l1_pitch=60 %l1_pend=36 %l1_ptime=50 %l1_decay=250 \
%l2_src=5 %l2_level=70 %l2_decay=60 %l2_ftype=2 %l2_cut=100 %l2_cend=100 %l2_res=20 \
%l3_src=6 %l3_level=25 %l3_pitch=84 %l3_pend=84 %l3_decay=120 %l3_ftype=1 %l3_cut=96 %l3_cend=96 \
%drive=30 %rev=15",
"%l1_src=2 %l1_level=70 %l1_pitch=84 %l1_pend=84 %l1_decay=250 \
%l2_src=1 %l2_level=80 %l2_pitch=91 %l2_pend=91 %l2_delay=70 %l2_decay=400 \
%l3_src=1 %l3_level=30 %l3_pitch=103 %l3_pend=103 %l3_delay=70 %l3_decay=300 \
%rev=35",
"%l1_src=5 %l1_level=70 %l1_decay=15 %l1_ftype=2 %l1_cut=108 %l1_cend=108 %l1_res=30 \
%l2_src=1 %l2_level=50 %l2_pitch=96 %l2_pend=96 %l2_decay=30 \
%rev=5 %vol=100",
"%l1_src=1 %l1_level=80 %l1_pitch=79 %l1_pend=79 %l1_decay=180 \
%l2_src=1 %l2_level=80 %l2_pitch=86 %l2_pend=86 %l2_delay=80 %l2_decay=300 \
%l3_src=2 %l3_level=30 %l3_pitch=98 %l3_pend=98 %l3_delay=80 %l3_decay=200 \
%rev=20",
"%l1_src=5 %l1_level=100 %l1_attack=250 %l1_decay=300 %l1_ftype=2 %l1_cut=70 %l1_cend=110 %l1_res=40 \
%l2_src=5 %l2_level=50 %l2_attack=200 %l2_decay=250 %l2_delay=40 %l2_ftype=2 %l2_cut=60 %l2_cend=100 %l2_res=30 \
%rev=30 %vol=100",
"%l1_src=3 %l1_level=70 %l1_pitch=48 %l1_pend=84 %l1_ptime=1200 %l1_attack=900 %l1_decay=400 %l1_cut=80 %l1_cend=125 \
%l2_src=4 %l2_level=40 %l2_pitch=55 %l2_pend=91 %l2_ptime=1200 %l2_attack=900 %l2_decay=400 \
%l3_src=1 %l3_level=60 %l3_pitch=36 %l3_pend=60 %l3_ptime=1200 %l3_attack=800 %l3_decay=400 \
%drive=15 %rev=30",
"%l1_src=4 %l1_level=70 %l1_pitch=108 %l1_pend=60 %l1_ptime=80 %l1_decay=140 \
%l2_src=6 %l2_level=50 %l2_pitch=90 %l2_pend=90 %l2_decay=100 %l2_ftype=1 %l2_cut=100 %l2_cend=100 \
%l3_src=5 %l3_level=40 %l3_decay=70 %l3_ftype=2 %l3_cut=110 %l3_cend=110 %l3_res=60 \
%drive=35",
];
impl Category {
pub const ALL: [Category; 10] = [
Category::Laser,
Category::Blaster,
Category::Explosion,
Category::Impact,
Category::Pickup,
Category::Click,
Category::Confirm,
Category::Whoosh,
Category::Charge,
Category::Zap,
];
pub fn from_index(n: u8) -> Option<Category> {
Self::ALL.get(n as usize).copied()
}
pub fn name(self) -> &'static str {
match self {
Category::Laser => "laser",
Category::Blaster => "blaster",
Category::Explosion => "explosion",
Category::Impact => "impact",
Category::Pickup => "pickup",
Category::Click => "click",
Category::Confirm => "confirm",
Category::Whoosh => "whoosh",
Category::Charge => "charge",
Category::Zap => "zap",
}
}
pub fn from_name(name: &str) -> Option<Category> {
Self::ALL.iter().copied().find(|c| c.name() == name)
}
pub fn preset(self) -> HdParams {
let mut p = HdParams::neutral();
p.apply(PRESET_TEXT[self as usize])
.expect("プリセットの中身は正しい");
p
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct HdParams {
values: [u16; COUNT],
}
impl Default for HdParams {
fn default() -> Self {
Category::Laser.preset()
}
}
impl HdParams {
pub const fn neutral() -> Self {
let mut values = [0u16; COUNT];
let mut i = 0;
while i < COUNT {
values[i] = PARAMS[i].default;
i += 1;
}
HdParams { values }
}
pub fn get(&self, i: usize) -> u16 {
self.values.get(i).copied().unwrap_or(0)
}
pub fn layer(&self, l: usize, p: usize) -> u16 {
self.get(layer_param(l, p))
}
pub fn set(&mut self, i: usize, v: u32) {
if let (Some(slot), Some(def)) = (self.values.get_mut(i), PARAMS.get(i)) {
*slot = v.clamp(def.min as u32, def.max as u32) as u16;
}
}
pub fn set_by_name(&mut self, name: &str, v: u32) -> bool {
match param_index(name) {
Some(i) => {
self.set(i, v);
true
}
None => false,
}
}
pub fn values(&self) -> &[u16; COUNT] {
&self.values
}
pub fn length_samples(&self, sample_rate: u32) -> u32 {
let mut ms = 0u64;
for l in 0..LAYERS {
if self.layer(l, layer::SRC) == source::OFF || self.layer(l, layer::LEVEL) == 0 {
continue;
}
let t = self.layer(l, layer::DELAY) as u64
+ self.layer(l, layer::ATTACK) as u64
+ self.layer(l, layer::DECAY) as u64;
ms = ms.max(t);
}
let ms = ms * self.get(master::LENGTH) as u64 / 100 + 2;
(ms * sample_rate as u64 / 1000).max(1) as u32
}
fn apply<'a>(&mut self, text: &'a str) -> Result<(), ParseError<'a>> {
for tok in text.split_whitespace() {
if let Some(n) = tok.strip_prefix('@') {
let c = n
.parse::<u8>()
.ok()
.and_then(Category::from_index)
.ok_or(ParseError::BadTone(tok))?;
*self = c.preset();
} else if let Some(kv) = tok.strip_prefix('%') {
let (k, v) = kv.split_once('=').ok_or(ParseError::BadToken(tok))?;
let i = param_index(k).ok_or(ParseError::UnknownParam(tok))?;
let v: u32 = v.parse().map_err(|_| ParseError::BadValue(tok))?;
let def = PARAMS[i];
if v < def.min as u32 || v > def.max as u32 {
return Err(ParseError::OutOfRange(tok));
}
self.values[i] = v as u16;
} else {
return Err(ParseError::BadToken(tok));
}
}
Ok(())
}
pub fn parse(text: &str) -> Result<HdParams, ParseError<'_>> {
let mut p = HdParams::default();
p.apply(text)?;
Ok(p)
}
pub fn write_text(&self, base: Category, w: &mut impl fmt::Write) -> fmt::Result {
write!(w, "@{}", base as u8)?;
let b = base.preset();
for (i, def) in PARAMS.iter().enumerate() {
if self.values[i] != b.values[i] {
write!(w, " %{}={}", def.name, self.values[i])?;
}
}
Ok(())
}
pub fn nearest_category(&self) -> Category {
let mut best = Category::Laser;
let mut best_n = usize::MAX;
for c in Category::ALL {
let p = c.preset();
let n = (0..COUNT)
.filter(|&i| p.values[i] != self.values[i])
.count();
if n < best_n {
best = c;
best_n = n;
}
}
best
}
pub fn to_text(&self) -> String {
let mut s = String::new();
let _ = self.write_text(self.nearest_category(), &mut s);
s
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParseError<'a> {
BadTone(&'a str),
UnknownParam(&'a str),
BadValue(&'a str),
OutOfRange(&'a str),
BadToken(&'a str),
}
impl crate::text::Localized for ParseError<'_> {
fn text_key(&self) -> &'static str {
match self {
ParseError::BadTone(_) => "err-hd-params-bad-tone",
ParseError::UnknownParam(_) => "err-hd-params-unknown-param",
ParseError::BadValue(_) => "err-hd-params-bad-value",
ParseError::OutOfRange(_) => "err-hd-params-out-of-range",
ParseError::BadToken(_) => "err-hd-params-bad-token",
}
}
fn text_args(&self, f: &mut dyn FnMut(&'static str, crate::text::TextArg<'_>)) {
let (ParseError::BadTone(t)
| ParseError::UnknownParam(t)
| ParseError::BadValue(t)
| ParseError::OutOfRange(t)
| ParseError::BadToken(t)) = self;
f("word", crate::text::TextArg::Str(t));
}
fn catalogs(&self) -> &'static [crate::text::Catalog] {
crate::synth::sfx_hd::text_table::CATALOGS
}
}
impl core::fmt::Display for ParseError<'_> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
crate::text::write_localized(f, self, crate::text::DEFAULT_LANG)
}
}
impl std::error::Error for ParseError<'_> {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn presets_parse_and_names_are_unique() {
for c in Category::ALL {
let _ = c.preset();
}
for (i, a) in PARAMS.iter().enumerate() {
assert_eq!(param_index(a.name), Some(i));
assert!(a.default >= a.min && a.default <= a.max, "{}", a.name);
}
assert_eq!(PARAMS[master::DRIVE].name, "drive");
assert_eq!(PARAMS[layer_param(3, layer::RES)].name, "l4_res");
}
#[test]
fn text_round_trips() {
let mut p = Category::Impact.preset();
p.set(layer_param(1, layer::DECAY), 90);
assert_eq!(p.to_text(), "@3 %l2_decay=90");
assert_eq!(HdParams::parse(&p.to_text()).unwrap(), p);
assert_eq!(HdParams::parse("@0 x"), Err(ParseError::BadToken("x")));
}
}