#![no_std]
use core::cmp;
use core::marker::PhantomData;
use core::ops;
pub mod comp;
pub mod gen;
pub mod noise;
#[cfg(feature = "num-traits")]
pub mod num;
pub trait Signal {
type Type;
fn next(&mut self) -> Self::Type;
#[inline]
fn add<S>(self, other: S) -> comp::Add<Self, S>
where
Self: Sized,
S: Signal,
Self::Type: ops::Add<S::Type>,
{
comp::Add {
left: self,
right: other,
}
}
#[inline]
fn sub<S>(self, other: S) -> comp::Sub<Self, S>
where
Self: Sized,
S: Signal,
Self::Type: ops::Sub<S::Type>,
{
comp::Sub {
left: self,
right: other,
}
}
#[inline]
fn mul<S>(self, other: S) -> comp::Mul<Self, S>
where
Self: Sized,
S: Signal,
Self::Type: ops::Mul<S::Type>,
{
comp::Mul {
left: self,
right: other,
}
}
#[inline]
fn div<S>(self, other: S) -> comp::Div<Self, S>
where
Self: Sized,
S: Signal,
Self::Type: ops::Div<S::Type>,
{
comp::Div {
left: self,
right: other,
}
}
#[inline]
fn rem<S>(self, other: S) -> comp::Rem<Self, S>
where
Self: Sized,
S: Signal,
Self::Type: ops::Rem<S::Type>,
{
comp::Rem {
left: self,
right: other,
}
}
#[inline]
fn bitand<S>(self, other: S) -> comp::BitAnd<Self, S>
where
Self: Sized,
S: Signal,
Self::Type: ops::BitAnd<S::Type>,
{
comp::BitAnd {
left: self,
right: other,
}
}
#[inline]
fn bitor<S>(self, other: S) -> comp::BitOr<Self, S>
where
Self: Sized,
S: Signal,
Self::Type: ops::BitOr<S::Type>,
{
comp::BitOr {
left: self,
right: other,
}
}
#[inline]
fn bitxor<S>(self, other: S) -> comp::BitXor<Self, S>
where
Self: Sized,
S: Signal,
Self::Type: ops::BitXor<S::Type>,
{
comp::BitXor {
left: self,
right: other,
}
}
#[inline]
fn shr<S>(self, other: S) -> comp::Shr<Self, S>
where
Self: Sized,
S: Signal,
Self::Type: ops::Shr<S::Type>,
{
comp::Shr {
left: self,
right: other,
}
}
#[inline]
fn shl<S>(self, other: S) -> comp::Shl<Self, S>
where
Self: Sized,
S: Signal,
Self::Type: ops::Shl<S::Type>,
{
comp::Shl {
left: self,
right: other,
}
}
#[inline]
fn neg(self) -> comp::Neg<Self>
where
Self: Sized,
Self::Type: ops::Neg,
{
comp::Neg { signal: self }
}
#[inline]
fn min<S>(self, other: S) -> comp::Min<Self, S>
where
Self: Sized,
S: Signal<Type = Self::Type>,
Self::Type: cmp::Ord,
{
comp::Min { a: self, b: other }
}
#[inline]
fn max<S>(self, other: S) -> comp::Max<Self, S>
where
Self: Sized,
S: Signal<Type = Self::Type>,
Self::Type: cmp::Ord,
{
comp::Max { a: self, b: other }
}
#[inline]
fn clamp<Min, Max>(self, min: Min, max: Max) -> comp::Clamp<Self, Min, Max>
where
Self: Sized,
Min: Signal<Type = Self::Type>,
Max: Signal<Type = Self::Type>,
Self::Type: cmp::Ord,
{
comp::Clamp {
signal: self,
min,
max,
}
}
#[inline]
fn map<F, T>(self, map: F) -> comp::Map<Self, F>
where
Self: Sized,
F: FnMut(Self::Type) -> T,
{
comp::Map { signal: self, map }
}
#[inline]
fn filter<F>(self, predicate: F) -> comp::Filter<Self, F>
where
Self: Sized,
F: FnMut(&Self::Type) -> bool,
{
comp::Filter {
signal: self,
predicate,
}
}
#[inline]
fn filter_map<F, T>(self, predicate: F) -> comp::FilterMap<Self, F>
where
Self: Sized,
F: FnMut(Self::Type) -> Option<T>,
{
comp::FilterMap {
signal: self,
predicate,
}
}
#[inline]
fn cloned<'a, T>(self) -> comp::Cloned<Self>
where
Self: Sized + Signal<Type = &'a T>,
T: 'a + Clone,
{
comp::Cloned { signal: self }
}
#[inline]
fn inspect<F>(self, inspect: F) -> comp::Inspect<Self, F>
where
Self: Sized,
F: FnMut(&Self::Type),
{
comp::Inspect {
signal: self,
inspect,
}
}
#[inline]
fn zip<S>(self, other: S) -> comp::Zip<Self, S>
where
Self: Sized,
S: Signal,
{
comp::Zip { a: self, b: other }
}
#[inline]
fn into<T>(self) -> comp::Into<Self, T>
where
Self: Sized,
Self::Type: Into<T>,
{
comp::Into {
signal: self,
_target: PhantomData,
}
}
#[inline]
fn skip(self, n: usize) -> comp::Skip<Self>
where
Self: Sized,
{
comp::Skip { signal: self, n }
}
#[inline]
fn into_iter(self) -> comp::IntoIter<Self>
where
Self: Sized,
{
comp::IntoIter { signal: self }
}
#[inline]
#[cfg(any(feature = "num-libm", feature = "num-std"))]
fn ease(mut self, amount: Self::Type) -> num::Ease<Self>
where
Self: Sized,
Self::Type: num_traits::Float,
{
num::Ease {
prev: self.next(),
signal: self,
inv_amount: num_traits::Float::recip(amount + num_traits::One::one()),
}
}
#[inline]
#[cfg(any(feature = "num-libm", feature = "num-std"))]
fn lerp<S>(self, other: S, amount: Self::Type) -> num::Lerp<Self, S>
where
Self: Sized,
S: Signal<Type = Self::Type>,
Self::Type: num_traits::Float,
{
num::Lerp {
a: self,
b: other,
t: amount,
}
}
#[inline]
#[cfg(feature = "num-traits")]
fn wrapping_add<S, RefL, RefR, T>(self, other: S) -> num::WrappingAdd<T, Self, S>
where
T: num_traits::WrappingAdd,
RefL: AsRef<T>,
RefR: AsRef<T>,
Self: Sized + Signal<Type = RefL>,
S: Signal<Type = RefR>,
{
num::WrappingAdd {
left: self,
right: other,
_marker: core::marker::PhantomData,
}
}
#[inline]
#[cfg(feature = "num-traits")]
fn wrapping_sub<S, RefL, RefR, T>(self, other: S) -> num::WrappingSub<T, Self, S>
where
T: num_traits::WrappingSub,
RefL: AsRef<T>,
RefR: AsRef<T>,
Self: Sized + Signal<Type = RefL>,
S: Signal<Type = RefR>,
{
num::WrappingSub {
left: self,
right: other,
_marker: core::marker::PhantomData,
}
}
#[inline]
#[cfg(feature = "num-traits")]
fn wrapping_mul<S, RefL, RefR, T>(self, other: S) -> num::WrappingMul<T, Self, S>
where
T: num_traits::WrappingMul,
RefL: AsRef<T>,
RefR: AsRef<T>,
Self: Sized + Signal<Type = RefL>,
S: Signal<Type = RefR>,
{
num::WrappingMul {
left: self,
right: other,
_marker: core::marker::PhantomData,
}
}
#[inline]
#[cfg(feature = "num-traits")]
fn wrapping_shr<S, RefL, T>(self, other: S) -> num::WrappingShr<T, Self, S>
where
T: num_traits::WrappingShr,
RefL: AsRef<T>,
Self: Sized + Signal<Type = RefL>,
S: Signal<Type = u32>,
{
num::WrappingShr {
left: self,
right: other,
_marker: core::marker::PhantomData,
}
}
#[inline]
#[cfg(feature = "num-traits")]
fn wrapping_shl<S, RefL, T>(self, other: S) -> num::WrappingShl<T, Self, S>
where
T: num_traits::WrappingShl,
RefL: AsRef<T>,
Self: Sized + Signal<Type = RefL>,
S: Signal<Type = u32>,
{
num::WrappingShl {
left: self,
right: other,
_marker: core::marker::PhantomData,
}
}
#[inline]
#[cfg(feature = "num-traits")]
fn saturating_add<S, RefL, RefR, T>(self, other: S) -> num::SaturatingAdd<T, Self, S>
where
T: num_traits::SaturatingAdd,
RefL: AsRef<T>,
RefR: AsRef<T>,
Self: Sized + Signal<Type = RefL>,
S: Signal<Type = RefR>,
{
num::SaturatingAdd {
left: self,
right: other,
_marker: core::marker::PhantomData,
}
}
#[inline]
#[cfg(feature = "num-traits")]
fn saturating_sub<S, RefL, RefR, T>(self, other: S) -> num::SaturatingSub<T, Self, S>
where
T: num_traits::SaturatingSub,
RefL: AsRef<T>,
RefR: AsRef<T>,
Self: Sized + Signal<Type = RefL>,
S: Signal<Type = RefR>,
{
num::SaturatingSub {
left: self,
right: other,
_marker: core::marker::PhantomData,
}
}
#[inline]
#[cfg(feature = "num-traits")]
fn saturating_mul<S, RefL, RefR, T>(self, other: S) -> num::SaturatingMul<T, Self, S>
where
T: num_traits::SaturatingMul,
RefL: AsRef<T>,
RefR: AsRef<T>,
Self: Sized + Signal<Type = RefL>,
S: Signal<Type = RefR>,
{
num::SaturatingMul {
left: self,
right: other,
_marker: core::marker::PhantomData,
}
}
#[inline]
#[cfg(feature = "num-traits")]
fn checked_add<S, RefL, RefR, T>(self, other: S) -> num::CheckedAdd<T, Self, S>
where
T: num_traits::CheckedAdd,
RefL: AsRef<T>,
RefR: AsRef<T>,
Self: Sized + Signal<Type = RefL>,
S: Signal<Type = RefR>,
{
num::CheckedAdd {
left: self,
right: other,
_marker: core::marker::PhantomData,
}
}
#[inline]
#[cfg(feature = "num-traits")]
fn checked_sub<S, RefL, RefR, T>(self, other: S) -> num::CheckedSub<T, Self, S>
where
T: num_traits::CheckedSub,
RefL: AsRef<T>,
RefR: AsRef<T>,
Self: Sized + Signal<Type = RefL>,
S: Signal<Type = RefR>,
{
num::CheckedSub {
left: self,
right: other,
_marker: core::marker::PhantomData,
}
}
#[inline]
#[cfg(feature = "num-traits")]
fn checked_mul<S, RefL, RefR, T>(self, other: S) -> num::CheckedMul<T, Self, S>
where
T: num_traits::CheckedMul,
RefL: AsRef<T>,
RefR: AsRef<T>,
Self: Sized + Signal<Type = RefL>,
S: Signal<Type = RefR>,
{
num::CheckedMul {
left: self,
right: other,
_marker: core::marker::PhantomData,
}
}
#[inline]
#[cfg(feature = "num-traits")]
fn checked_shr<S, RefL, T>(self, other: S) -> num::CheckedShr<T, Self, S>
where
T: num_traits::CheckedShr,
RefL: AsRef<T>,
Self: Sized + Signal<Type = RefL>,
S: Signal<Type = u32>,
{
num::CheckedShr {
left: self,
right: other,
_marker: core::marker::PhantomData,
}
}
#[inline]
#[cfg(feature = "num-traits")]
fn checked_shl<S, RefL, T>(self, other: S) -> num::CheckedShl<T, Self, S>
where
T: num_traits::CheckedShr,
RefL: AsRef<T>,
Self: Sized + Signal<Type = RefL>,
S: Signal<Type = u32>,
{
num::CheckedShl {
left: self,
right: other,
_marker: core::marker::PhantomData,
}
}
#[inline]
#[cfg(feature = "num-traits")]
fn inv(self) -> num::Inv<Self>
where
Self: Sized,
Self::Type: num_traits::Inv,
{
num::Inv { signal: self }
}
#[inline]
#[cfg(feature = "num-traits")]
fn mul_add<M, A>(self, mul: M, add: A) -> num::MulAdd<Self, M, A>
where
Self: Sized,
M: Signal,
A: Signal,
Self::Type: num_traits::MulAdd<M::Type, A::Type>,
{
num::MulAdd {
signal: self,
mul,
add,
}
}
#[inline]
#[cfg(feature = "num-traits")]
fn pow<E>(self, exp: E) -> num::Pow<Self, E>
where
Self: Sized,
E: Signal,
Self::Type: num_traits::Pow<E::Type>,
{
num::Pow { signal: self, exp }
}
#[inline]
#[cfg(any(feature = "num-std", feature = "num-libm"))]
fn interpolate<F>(mut self, length: usize, interpolate: F) -> num::Interpolate<Self, F>
where
Self: Sized,
Self::Type: num_traits::Float + num_traits::FromPrimitive,
F: FnMut(Self::Type, Self::Type, Self::Type) -> Self::Type,
{
num::Interpolate {
a: self.next(),
b: self.next(),
signal: self,
length,
count: 0,
interpolate,
}
}
}
#[inline]
pub fn constant<T>(value: T) -> gen::Const<T>
where
T: Clone,
{
gen::Const { value }
}
#[inline]
pub fn default<T>() -> gen::Const<T>
where
T: Clone + Default,
{
gen::Const {
value: T::default(),
}
}
#[inline]
pub fn gen<F, T>(gen: F) -> gen::Gen<F>
where
F: FnMut() -> T,
{
gen::Gen { gen }
}
#[inline]
pub fn successors<T, F>(first_value: T, successor: F) -> gen::Successors<T, F>
where
F: FnMut(&T) -> T,
{
gen::Successors {
state: first_value,
successor,
}
}
#[inline]
pub fn cycle<I>(iter: I) -> gen::Cycle<I::IntoIter>
where
I: IntoIterator,
I::IntoIter: Clone,
{
let iter = iter.into_iter();
gen::Cycle {
orig: iter.clone(),
iter,
}
}
#[inline]
#[cfg(any(feature = "num-libm", feature = "num-std"))]
pub fn oscillator<S, F, T>(phase: num::Phase<S>, waveform: F) -> num::Oscillator<S, F>
where
S: Signal,
S::Type: num_traits::Float,
F: FnMut(S::Type) -> T,
{
num::Oscillator { phase, waveform }
}
#[inline]
#[cfg(any(feature = "num-libm", feature = "num-std"))]
pub fn sine<S>(steps: S) -> num::Sine<S>
where
S: Signal,
S::Type: num_traits::Float + num_traits::FloatConst,
{
num::Sine {
phase: num::Phase::new(steps, num_traits::One::one()),
}
}
#[inline]
#[cfg(any(feature = "num-libm", feature = "num-std"))]
pub fn triangle<S>(steps: S) -> num::Triangle<S>
where
S: Signal,
S::Type: num_traits::Float + num_traits::FloatConst,
{
num::Triangle {
phase: num::Phase::new(steps, num_traits::One::one()),
}
}
#[inline]
#[cfg(any(feature = "num-libm", feature = "num-std"))]
pub fn sawtooth<S>(steps: S) -> num::Sawtooth<S>
where
S: Signal,
S::Type: num_traits::Float + num_traits::FloatConst,
{
num::Sawtooth {
phase: num::Phase::new(steps, num_traits::One::one()),
}
}
#[inline]
#[cfg(any(feature = "num-libm", feature = "num-std"))]
pub fn square<S>(steps: S) -> num::Square<S>
where
S: Signal,
S::Type: num_traits::Float + num_traits::FloatConst,
{
num::Square {
phase: num::Phase::new(steps, num_traits::One::one()),
}
}
#[inline]
#[cfg(feature = "rand")]
pub fn random_complete<R, D, T>(rng: R, dist: D) -> noise::Random<R, D, T>
where
R: rand::Rng,
D: rand::distributions::Distribution<T>,
{
noise::Random {
rng,
dist,
_marker: core::marker::PhantomData,
}
}
#[inline]
#[cfg(feature = "rand-std")]
pub fn random<T>() -> noise::Random<rand::rngs::ThreadRng, rand::distributions::Standard, T> {
noise::Random {
rng: rand::rngs::ThreadRng::default(),
dist: rand::distributions::Standard,
_marker: core::marker::PhantomData,
}
}
#[inline]
#[cfg(any(feature = "num-std", feature = "num-libm"))]
pub fn simplex<S>(steps: S) -> noise::Simplex<S>
where
S: Signal<Type = f64>,
{
noise::Simplex::default(steps)
}