pub const TRIG_SIGNAL_LENGTH_MS: f32 = 2.0;
pub const TRIG_LOW_THRES: f32 = 0.25;
pub const TRIG_HIGH_THRES: f32 = 0.5;
#[derive(Debug, Clone, Copy)]
pub struct GateSignal {
ms_per_sample: f32,
ms_count: f32,
}
impl GateSignal {
pub fn new() -> Self {
Self { ms_per_sample: 1000.0 / 44100.0, ms_count: 0.0 }
}
pub fn reset(&mut self) {
self.ms_count = 0.0;
}
pub fn set_sample_rate(&mut self, srate: f32) {
self.ms_per_sample = 1000.0 / srate;
}
#[inline]
pub fn trigger(&mut self) {
self.ms_count = 0.0001;
}
#[inline]
pub fn next(&mut self, length_ms: f32) -> f32 {
if self.ms_count > 0.0 {
self.ms_count += self.ms_per_sample;
if (self.ms_count - 0.0001) > length_ms {
self.ms_count = 0.0;
}
1.0
} else {
0.0
}
}
}
impl Default for GateSignal {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone, Copy)]
pub struct TrigSignal {
length: u32,
scount: u32,
}
impl TrigSignal {
pub fn new() -> Self {
Self { length: ((44100.0 * TRIG_SIGNAL_LENGTH_MS) / 1000.0).ceil() as u32, scount: 0 }
}
pub fn reset(&mut self) {
self.scount = 0;
}
pub fn set_sample_rate(&mut self, srate: f32) {
self.length = ((srate * TRIG_SIGNAL_LENGTH_MS) / 1000.0).ceil() as u32;
self.scount = 0;
}
#[inline]
pub fn trigger(&mut self) {
self.scount = self.length;
}
#[inline]
pub fn next(&mut self) -> f32 {
if self.scount > 0 {
self.scount -= 1;
1.0
} else {
0.0
}
}
}
impl Default for TrigSignal {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone, Copy)]
pub struct ChangeTrig {
ts: TrigSignal,
last: f32,
}
impl ChangeTrig {
pub fn new() -> Self {
Self {
ts: TrigSignal::new(),
last: -100.0, }
}
pub fn reset(&mut self) {
self.ts.reset();
self.last = -100.0;
}
pub fn set_sample_rate(&mut self, srate: f32) {
self.ts.set_sample_rate(srate);
}
#[inline]
pub fn next(&mut self, inp: f32) -> f32 {
if (inp - self.last).abs() > std::f32::EPSILON {
self.ts.trigger();
self.last = inp;
}
self.ts.next()
}
}
impl Default for ChangeTrig {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone, Copy)]
pub struct Trigger {
triggered: bool,
}
impl Trigger {
pub fn new() -> Self {
Self { triggered: false }
}
#[inline]
pub fn reset(&mut self) {
self.triggered = false;
}
#[inline]
pub fn check_trigger(&mut self, input: f32) -> bool {
if self.triggered {
if input <= TRIG_LOW_THRES {
self.triggered = false;
}
false
} else if input > TRIG_HIGH_THRES {
self.triggered = true;
true
} else {
false
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct CustomTrigger {
triggered: bool,
low_thres: f32,
high_thres: f32,
}
impl CustomTrigger {
pub fn new(low_thres: f32, high_thres: f32) -> Self {
Self { triggered: false, low_thres, high_thres }
}
pub fn set_threshold(&mut self, low_thres: f32, high_thres: f32) {
self.low_thres = low_thres;
self.high_thres = high_thres;
}
#[inline]
pub fn reset(&mut self) {
self.triggered = false;
}
#[inline]
pub fn check_trigger(&mut self, input: f32) -> bool {
if self.triggered {
if input <= self.low_thres {
self.triggered = false;
}
false
} else if input > self.high_thres {
self.triggered = true;
true
} else {
false
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct TriggerPhaseClock {
clock_phase: f64,
clock_inc: f64,
prev_trigger: bool,
clock_samples: u32,
}
impl TriggerPhaseClock {
pub fn new() -> Self {
Self { clock_phase: 0.0, clock_inc: 0.0, prev_trigger: true, clock_samples: 0 }
}
#[inline]
pub fn reset(&mut self) {
self.clock_samples = 0;
self.clock_inc = 0.0;
self.prev_trigger = true;
self.clock_samples = 0;
}
#[inline]
pub fn sync(&mut self) {
self.clock_phase = 0.0;
}
#[inline]
pub fn next_phase(&mut self, clock_limit: f64, trigger_in: f32) -> f64 {
if self.prev_trigger {
if trigger_in <= TRIG_LOW_THRES {
self.prev_trigger = false;
}
} else if trigger_in > TRIG_HIGH_THRES {
self.prev_trigger = true;
if self.clock_samples > 0 {
self.clock_inc = 1.0 / (self.clock_samples as f64);
}
self.clock_samples = 0;
}
self.clock_samples += 1;
self.clock_phase += self.clock_inc;
self.clock_phase = self.clock_phase % clock_limit;
self.clock_phase
}
}
#[derive(Debug, Clone, Copy)]
pub struct TriggerSampleClock {
prev_trigger: bool,
clock_samples: u32,
counter: u32,
}
impl TriggerSampleClock {
pub fn new() -> Self {
Self { prev_trigger: true, clock_samples: 0, counter: 0 }
}
#[inline]
pub fn reset(&mut self) {
self.clock_samples = 0;
self.counter = 0;
}
#[inline]
pub fn next(&mut self, trigger_in: f32) -> u32 {
if self.prev_trigger {
if trigger_in <= TRIG_LOW_THRES {
self.prev_trigger = false;
}
} else if trigger_in > TRIG_HIGH_THRES {
self.prev_trigger = true;
self.clock_samples = self.counter;
self.counter = 0;
}
self.counter += 1;
self.clock_samples
}
}