use super::*;
pub trait Modulator {
fn modulate(self, node_id: u32, param: u32, low: f32, high: f32);
}
pub struct LinearModulator {
pub start_at: Time,
pub end_at: Time,
}
impl Modulator for LinearModulator {
fn modulate(self, node_id: u32, param: u32, low: f32, high: f32) {
unsafe {
bindings::mod_linear(node_id, param, low, high, self.start_at.0, self.end_at.0);
}
}
}
pub struct HoldModulator {
pub time: Time,
}
impl Modulator for HoldModulator {
fn modulate(self, node_id: u32, param: u32, low: f32, high: f32) {
unsafe {
bindings::mod_hold(node_id, param, low, high, self.time.0);
}
}
}
pub struct AdsrModulator {
pub attack: Time,
pub decay: Time,
pub sustain: Time,
pub sustain_level: f32,
pub release: Time,
}
impl Modulator for AdsrModulator {
fn modulate(self, node_id: u32, param: u32, low: f32, high: f32) {
unsafe {
bindings::mod_adsr(
node_id,
param,
low,
high,
self.attack.0,
self.decay.0,
self.sustain.0,
self.sustain_level,
self.release.0,
);
}
}
}
pub struct SineModulator {
pub freq: Freq,
}
impl Modulator for SineModulator {
fn modulate(self, node_id: u32, param: u32, low: f32, high: f32) {
unsafe {
bindings::mod_sine(node_id, param, self.freq.0, low, high);
}
}
}
pub struct SquareModulator {
pub period: Time,
}
impl Modulator for SquareModulator {
fn modulate(self, node_id: u32, param: u32, low: f32, high: f32) {
unsafe {
bindings::mod_square(node_id, param, low, high, self.period.0);
}
}
}
pub struct SawtoothModulator {
pub period: Time,
}
impl Modulator for SawtoothModulator {
fn modulate(self, node_id: u32, param: u32, low: f32, high: f32) {
unsafe {
bindings::mod_sawtooth(node_id, param, low, high, self.period.0);
}
}
}
mod bindings {
#[link(wasm_import_module = "audio")]
unsafe extern "C" {
pub(super) unsafe fn mod_linear(
node_id: u32,
param: u32,
start: f32,
end: f32,
start_at: u32,
end_at: u32,
);
pub(super) unsafe fn mod_hold(id: u32, param: u32, low: f32, high: f32, time: u32);
pub(super) unsafe fn mod_sine(id: u32, param: u32, freq: f32, low: f32, high: f32);
pub(super) unsafe fn mod_square(id: u32, param: u32, low: f32, high: f32, period: u32);
pub(super) unsafe fn mod_sawtooth(id: u32, param: u32, low: f32, high: f32, period: u32);
pub(super) unsafe fn mod_adsr(
id: u32,
param: u32,
low: f32,
high: f32,
attack: u32,
decay: u32,
sustain: u32,
sustain_level: f32,
release: u32,
);
}
}