use super::*;
pub(super) const FMX_OPS_MAX: usize = 6;
const STATE_OPS: usize = 6;
#[derive(Clone, Copy)]
pub(super) struct Algo {
mods_in: [u8; STATE_OPS],
carriers: u8,
fb_op: u8,
order: [u8; STATE_OPS],
}
const fn algo(mods_in: [u8; STATE_OPS], carriers: u8, fb_op: u8, order: [u8; STATE_OPS]) -> Algo {
Algo {
mods_in,
carriers,
fb_op,
order,
}
}
const ALGOS_2: [Algo; 2] = [
algo([0b10, 0, 0, 0, 0, 0], 0b01, 1, [1, 0, 0, 0, 0, 0]),
algo([0, 0, 0, 0, 0, 0], 0b11, 0, [1, 0, 0, 0, 0, 0]),
];
const ALGOS_3: [Algo; 4] = [
algo([0b010, 0b100, 0, 0, 0, 0], 0b001, 2, [2, 1, 0, 0, 0, 0]),
algo([0b110, 0, 0, 0, 0, 0], 0b001, 1, [2, 1, 0, 0, 0, 0]),
algo([0, 0b100, 0, 0, 0, 0], 0b011, 2, [2, 1, 0, 0, 0, 0]),
algo([0, 0, 0, 0, 0, 0], 0b111, 0, [2, 1, 0, 0, 0, 0]),
];
const ALGOS_4: [Algo; 8] = [
algo(
[0b0010, 0b0100, 0b1000, 0, 0, 0],
0b0001,
3,
[3, 2, 1, 0, 0, 0],
),
algo([0b0010, 0b1100, 0, 0, 0, 0], 0b0001, 3, [3, 2, 1, 0, 0, 0]),
algo([0b1010, 0b0100, 0, 0, 0, 0], 0b0001, 3, [3, 2, 1, 0, 0, 0]),
algo([0b0110, 0, 0b1000, 0, 0, 0], 0b0001, 3, [3, 2, 1, 0, 0, 0]),
algo([0b0010, 0, 0b1000, 0, 0, 0], 0b0101, 3, [3, 2, 1, 0, 0, 0]),
algo(
[0b1000, 0b1000, 0b1000, 0, 0, 0],
0b0111,
3,
[3, 2, 1, 0, 0, 0],
),
algo([0, 0, 0b1000, 0, 0, 0], 0b0111, 3, [3, 2, 1, 0, 0, 0]),
algo([0; 6], 0b1111, 3, [3, 2, 1, 0, 0, 0]),
];
const fn from_graph(carriers: &[u8], edges: &[(u8, u8)]) -> Algo {
let mut mods_in = [0u8; STATE_OPS];
let mut carrier_mask = 0u8;
let mut top = 1u8;
let mut fb = 0u8;
let mut i = 0;
while i < carriers.len() {
carrier_mask |= 1 << (carriers[i] - 1);
if carriers[i] > top {
top = carriers[i];
}
i += 1;
}
let mut k = 0;
while k < edges.len() {
let (from, to) = edges[k];
mods_in[(to - 1) as usize] |= 1 << (from - 1);
if from > top {
top = from;
}
if from - 1 > fb {
fb = from - 1;
}
k += 1;
}
let mut order = [0u8; STATE_OPS];
let mut n = 0;
while n < top as usize {
order[n] = top - 1 - n as u8;
n += 1;
}
Algo {
mods_in,
carriers: carrier_mask,
fb_op: fb,
order,
}
}
const fn from_graph_fb(carriers: &[u8], edges: &[(u8, u8)], fb_op: u8) -> Algo {
let a = from_graph(carriers, edges);
Algo {
fb_op: fb_op - 1,
..a
}
}
const ALGOS_5: [Algo; 8] = [
from_graph(&[1], &[(5, 4), (4, 3), (3, 2), (2, 1)]),
from_graph(&[1, 3], &[(2, 1), (5, 4), (4, 3)]),
from_graph(&[1, 4], &[(3, 2), (2, 1), (5, 4)]),
from_graph(&[1, 3], &[(2, 1), (5, 3), (4, 3)]),
from_graph(&[1, 3, 5], &[(2, 1), (4, 3)]),
from_graph(&[1], &[(5, 1), (4, 1), (3, 1), (2, 1)]),
from_graph(&[1, 2, 3, 4], &[(5, 4)]),
from_graph(&[1, 2, 3, 4, 5], &[]),
];
const ALGOS_6: [Algo; 32] = [
from_graph(&[1], &[(6, 5), (5, 4), (4, 3), (3, 2), (2, 1)]),
from_graph(&[1], &[(6, 4), (5, 4), (4, 3), (3, 2), (2, 1)]),
from_graph(&[1], &[(6, 5), (5, 4), (4, 2), (3, 2), (2, 1)]),
from_graph(&[1], &[(6, 5), (5, 4), (4, 3), (3, 1), (2, 1)]),
from_graph(&[1], &[(6, 3), (5, 3), (4, 3), (3, 2), (2, 1)]),
from_graph(&[1], &[(6, 2), (5, 2), (4, 2), (3, 2), (2, 1)]),
from_graph(&[1], &[(6, 1), (5, 1), (4, 1), (3, 1), (2, 1)]),
from_graph(&[1, 2], &[(6, 5), (5, 4), (4, 3), (3, 2)]),
from_graph(&[1, 3], &[(2, 1), (6, 5), (5, 4), (4, 3)]),
from_graph(&[1, 4], &[(3, 2), (2, 1), (6, 5), (5, 4)]),
from_graph(&[1, 5], &[(4, 3), (3, 2), (2, 1), (6, 5)]),
from_graph(&[1, 6], &[(5, 4), (4, 3), (3, 2), (2, 1)]),
from_graph(&[1, 3, 5], &[(2, 1), (4, 3), (6, 5)]),
from_graph(&[1, 2, 3], &[(6, 5), (5, 4), (4, 3)]),
from_graph(&[1, 3], &[(2, 1), (6, 4), (5, 4), (4, 3)]),
from_graph(&[1, 4], &[(3, 2), (2, 1), (6, 4), (5, 4)]),
from_graph(&[1, 3], &[(2, 1), (6, 3), (5, 3), (4, 3)]),
from_graph(&[1, 2], &[(6, 2), (5, 2), (4, 2), (3, 2)]),
from_graph(&[1, 2, 3], &[(6, 3), (5, 3), (4, 3)]),
from_graph(&[1, 2, 3, 4], &[(6, 5), (5, 4)]),
from_graph(&[1, 2, 3, 4, 5], &[(6, 5), (6, 4), (6, 3), (6, 2), (6, 1)]),
from_graph(&[1, 2, 3, 4], &[(6, 4), (5, 4)]),
from_graph(&[1, 2, 3, 5], &[(6, 5), (4, 3)]),
from_graph(&[1, 2, 4, 5], &[(3, 2), (6, 5)]),
from_graph(&[1, 2, 3, 4, 5, 6], &[]),
from_graph(&[1, 2, 3, 4, 5], &[(6, 5)]),
from_graph(&[1, 2, 3, 4], &[(6, 4), (5, 3)]),
from_graph(&[1, 2, 3], &[(4, 1), (5, 2), (6, 3)]),
from_graph(&[1, 3], &[(2, 1), (6, 5), (5, 3), (4, 3)]),
from_graph(&[1, 2], &[(6, 5), (5, 4), (4, 1), (3, 2)]),
from_graph(&[1, 4], &[(2, 1), (3, 1), (6, 5), (5, 4)]),
from_graph(&[1, 2], &[(6, 5), (5, 4), (4, 3), (3, 1)]),
];
const ALGOS_DX7: [Algo; 32] = [
from_graph_fb(&[1, 3], &[(2, 1), (6, 5), (5, 4), (4, 3)], 6),
from_graph_fb(&[1, 3], &[(2, 1), (6, 5), (5, 4), (4, 3)], 2),
from_graph_fb(&[1, 4], &[(3, 2), (2, 1), (6, 5), (5, 4)], 6),
from_graph_fb(&[1, 4], &[(3, 2), (2, 1), (6, 5), (5, 4)], 6),
from_graph_fb(&[1, 3, 5], &[(2, 1), (4, 3), (6, 5)], 6),
from_graph_fb(&[1, 3, 5], &[(2, 1), (4, 3), (6, 5)], 6),
from_graph_fb(&[1, 3], &[(2, 1), (6, 5), (5, 3), (4, 3)], 6),
from_graph_fb(&[1, 3], &[(2, 1), (6, 5), (5, 3), (4, 3)], 4),
from_graph_fb(&[1, 3], &[(2, 1), (6, 5), (5, 3), (4, 3)], 2),
from_graph_fb(&[1, 4], &[(3, 2), (2, 1), (6, 4), (5, 4)], 3),
from_graph_fb(&[1, 4], &[(3, 2), (2, 1), (6, 4), (5, 4)], 6),
from_graph_fb(&[1, 3], &[(2, 1), (6, 3), (5, 3), (4, 3)], 2),
from_graph_fb(&[1, 3], &[(2, 1), (6, 3), (5, 3), (4, 3)], 6),
from_graph_fb(&[1, 3], &[(2, 1), (4, 3), (6, 4), (5, 4)], 6),
from_graph_fb(&[1, 3], &[(2, 1), (4, 3), (6, 4), (5, 4)], 2),
from_graph_fb(&[1], &[(2, 1), (3, 1), (5, 1), (4, 3), (6, 5)], 6),
from_graph_fb(&[1], &[(2, 1), (3, 1), (5, 1), (4, 3), (6, 5)], 2),
from_graph_fb(&[1], &[(2, 1), (3, 1), (4, 1), (5, 4), (6, 5)], 3),
from_graph_fb(&[1, 4, 5], &[(3, 2), (2, 1), (6, 4), (6, 5)], 6),
from_graph_fb(&[1, 2, 4], &[(3, 1), (3, 2), (5, 4), (6, 4)], 3),
from_graph_fb(&[1, 2, 4, 5], &[(3, 1), (3, 2), (6, 4), (6, 5)], 3),
from_graph_fb(&[1, 3, 4, 5], &[(2, 1), (6, 3), (6, 4), (6, 5)], 6),
from_graph_fb(&[1, 2, 4, 5], &[(3, 2), (6, 4), (6, 5)], 6),
from_graph_fb(&[1, 2, 3, 4, 5], &[(6, 3), (6, 4), (6, 5)], 6),
from_graph_fb(&[1, 2, 3, 4, 5], &[(6, 4), (6, 5)], 6),
from_graph_fb(&[1, 2, 4], &[(3, 2), (5, 4), (6, 4)], 6),
from_graph_fb(&[1, 2, 4], &[(3, 2), (5, 4), (6, 4)], 3),
from_graph_fb(&[1, 3, 6], &[(2, 1), (4, 3), (5, 4)], 5),
from_graph_fb(&[1, 2, 3, 5], &[(4, 3), (6, 5)], 6),
from_graph_fb(&[1, 2, 3, 6], &[(4, 3), (5, 4)], 5),
from_graph_fb(&[1, 2, 3, 4, 5], &[(6, 5)], 6),
from_graph_fb(&[1, 2, 3, 4, 5, 6], &[], 6),
];
const fn algos_6_all() -> [Algo; 64] {
let mut all = [ALGOS_6[0]; 64];
let mut i = 0;
while i < 32 {
all[i] = ALGOS_6[i];
all[32 + i] = ALGOS_DX7[i];
i += 1;
}
all
}
const ALGOS_6_ALL: [Algo; 64] = algos_6_all();
pub(crate) fn dx7_carrier_mask(algorithm: u8) -> u8 {
ALGOS_DX7[(algorithm as usize).min(31)].carriers
}
fn pick(ops: usize, algo: usize) -> &'static Algo {
let table: &[Algo] = match ops {
0..=2 => &ALGOS_2,
3 => &ALGOS_3,
4 => &ALGOS_4,
5 => &ALGOS_5,
_ => &ALGOS_6_ALL,
};
&table[algo.min(table.len() - 1)]
}
#[derive(Clone, Copy)]
pub(super) struct FmxState {
phase: [[f32; STATE_OPS]; FM_MAX_UNISON],
fb: [f32; FM_MAX_UNISON],
pub(super) add: AddState,
}
impl FmxState {
pub(super) const fn new() -> FmxState {
FmxState {
phase: [[0.0; STATE_OPS]; FM_MAX_UNISON],
fb: [0.0; FM_MAX_UNISON],
add: AddState::new(),
}
}
}
fn op_param(n: usize, k: u8) -> usize {
param::FMX_OP_BASE as usize + n * param::FMX_OP_PARAMS as usize + k as usize
}
pub(super) fn fmx(
params: &[u32; param::COUNT],
state: &mut FmxState,
dt: f32,
md: &Mods,
) -> (f32, f32) {
let ops = (params[param::FMX_OPS as usize] as usize).clamp(2, FMX_OPS_MAX);
let alg = pick(ops, params[param::FMX_ALGO as usize] as usize);
let tau = core::f32::consts::TAU;
let p = |i: usize| params[i] as f32;
let mut ratio = [1.0f32; STATE_OPS];
let mut index = [0.0f32; STATE_OPS];
let mut amp = [0.0f32; STATE_OPS];
let mut det_cents = [0.0f32; STATE_OPS];
let lfo_depth = if md.has(LFO_FM) {
md.sum[LFO_FM] * 5.0
} else {
0.0
};
for n in 0..ops {
ratio[n] = p(op_param(n, param::FMX_RATIO)) / 100.0;
let mut level = p(op_param(n, param::FMX_LEVEL));
let env = p(op_param(n, param::FMX_ENV));
if env > 0.0 {
let tau_s = (p(op_param(n, param::FMX_DECAY)) * 0.01).max(0.01);
level += env * m::exp(-md.t / tau_s);
}
index[n] = (level / 10.0 + lfo_depth).max(0.0);
amp[n] = level / 100.0;
det_cents[n] = p(op_param(n, param::FMX_DET)) - 100.0;
}
let fb_amount = p(param::FMX_FB as usize) / 100.0 * FM_FB_MAX;
let uni = (params[param::FM_UNI as usize] as usize).clamp(1, FM_MAX_UNISON);
let uni_cents = p(param::FM_DET as usize) * 0.6;
let drift = p(param::FM_DRIFT as usize) / 100.0 * FM_DRIFT_CENTS;
let width = p(param::WIDTH as usize) / 100.0;
let carriers = alg.carriers.count_ones().max(1) as f32;
let carrier_norm = 1.0 / carriers.sqrt();
let (mut l, mut r) = (0.0, 0.0);
for v in 0..uni {
let (spread, pan, mix) = supersaw_voice(uni, v);
let mut cents = spread * uni_cents;
if drift > 0.0 {
cents += drift * fm_drift(v, md.t);
}
let dtv = if cents == 0.0 {
dt
} else {
dt * m::exp2(cents / 1200.0)
};
let mut out = [0.0f32; STATE_OPS];
for &o in &alg.order[..ops] {
let n = o as usize;
let mut input = 0.0;
for j in 0..ops {
if alg.mods_in[n] & (1 << j) != 0 {
input += index[j] * out[j];
}
}
if n == alg.fb_op as usize && fb_amount > 0.0 {
input += fb_amount * state.fb[v];
}
out[n] = m::sin(state.phase[v][n] * tau + input);
if n == alg.fb_op as usize {
state.fb[v] = (state.fb[v] + out[n]) * 0.5;
}
}
let mut sum = 0.0;
for n in 0..ops {
if alg.carriers & (1 << n) != 0 {
sum += out[n] * amp[n];
}
let step = if det_cents[n] == 0.0 {
dtv * ratio[n]
} else {
dtv * ratio[n] * m::exp2(det_cents[n] / 1200.0)
};
dsp::advance(&mut state.phase[v][n], step.min(0.49));
}
let x = sum * carrier_norm * mix;
let pan = pan * width;
l += x * (1.0 - pan).min(1.0);
r += x * (1.0 + pan).min(1.0);
}
if uni == 1 {
(l, r)
} else {
let norm = 1.0 / (uni as f32).sqrt();
(l * norm, r * norm)
}
}