Starting to support arrays and tuples for inout
This commit is contained in:
38
oxydsp-dsp/src/units.rs
Normal file
38
oxydsp-dsp/src/units.rs
Normal file
@ -0,0 +1,38 @@
|
||||
use std::f64::consts::PI;
|
||||
|
||||
use crate::map;
|
||||
|
||||
// Represents digital frequency
|
||||
#[derive(Clone, Copy, PartialEq, PartialOrd)]
|
||||
pub struct DigitalFrequency(pub usize);
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, PartialOrd)]
|
||||
pub struct Phase(pub DigitalFrequency);
|
||||
|
||||
impl DigitalFrequency
|
||||
{
|
||||
pub fn from_rad(radians_per_sample: f64) -> Self
|
||||
{
|
||||
// Frequnecy wraps arround : Going at 2 pi radians per second
|
||||
// Is like not oscillating at all
|
||||
let f = radians_per_sample.rem_euclid(radians_per_sample);
|
||||
|
||||
// Then we map the [0, 2*PI] range into the 0 to usize range
|
||||
DigitalFrequency(map(f, 0., 2. * PI, 0., usize::MAX as f64).floor() as usize)
|
||||
}
|
||||
|
||||
pub fn from_time_frequency(hertz: f64, sample_rate: f64) -> Self
|
||||
{
|
||||
Self::from_rad(map(hertz, 0., sample_rate, 0., 2. * PI))
|
||||
}
|
||||
|
||||
pub fn as_rad(&self) -> f64
|
||||
{
|
||||
map(self.0 as f64, 0., usize::MAX as f64, 0., 2. * PI)
|
||||
}
|
||||
|
||||
pub fn as_time_frequency(&self, sample_rate: f64) -> f64
|
||||
{
|
||||
map(self.0 as f64, 0., usize::MAX as f64, 0., sample_rate)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user