use {BTreeMap, Duplex, Frame, Sample, Rc, VecDeque, Box};
use core;
use core::cell::RefCell;
use envelope;
use interpolate::{Converter, Interpolator};
use ring_buffer;
use rms;
pub trait Signal {
type Frame: Frame;
fn next(&mut self) -> Self::Frame;
#[inline]
fn is_exhausted(&self) -> bool {
false
}
fn map<M, F>(self, map: M) -> Map<Self, M, F>
where
Self: Sized,
M: FnMut(Self::Frame) -> F,
F: Frame,
{
Map {
signal: self,
map: map,
frame: core::marker::PhantomData,
}
}
fn zip_map<O, M, F>(self, other: O, map: M) -> ZipMap<Self, O, M, F>
where
Self: Sized,
M: FnMut(Self::Frame, O::Frame) -> F,
O: Signal,
F: Frame,
{
ZipMap {
this: self,
map: map,
other: other,
frame: core::marker::PhantomData,
}
}
#[inline]
fn add_amp<S>(self, other: S) -> AddAmp<Self, S>
where
Self: Sized,
S: Signal,
S::Frame: Frame<
Sample = <<Self::Frame as Frame>::Sample as Sample>::Signed,
NumChannels = <Self::Frame as Frame>::NumChannels,
>,
{
AddAmp { a: self, b: other }
}
#[inline]
fn mul_amp<S>(self, other: S) -> MulAmp<Self, S>
where
Self: Sized,
S: Signal,
S::Frame: Frame<
Sample = <<Self::Frame as Frame>::Sample as Sample>::Float,
NumChannels = <Self::Frame as Frame>::NumChannels,
>,
{
MulAmp { a: self, b: other }
}
#[inline]
fn offset_amp(
self,
offset: <<Self::Frame as Frame>::Sample as Sample>::Signed,
) -> OffsetAmp<Self>
where
Self: Sized,
{
OffsetAmp {
signal: self,
offset: offset,
}
}
#[inline]
fn scale_amp(self, amp: <<Self::Frame as Frame>::Sample as Sample>::Float) -> ScaleAmp<Self>
where
Self: Sized,
{
ScaleAmp {
signal: self,
amp: amp,
}
}
#[inline]
fn offset_amp_per_channel<F>(self, amp_frame: F) -> OffsetAmpPerChannel<Self, F>
where
Self: Sized,
F: Frame<
Sample = <<Self::Frame as Frame>::Sample as Sample>::Signed,
NumChannels = <Self::Frame as Frame>::NumChannels,
>,
{
OffsetAmpPerChannel {
signal: self,
amp_frame: amp_frame,
}
}
#[inline]
fn scale_amp_per_channel<F>(self, amp_frame: F) -> ScaleAmpPerChannel<Self, F>
where
Self: Sized,
F: Frame<
Sample = <<Self::Frame as Frame>::Sample as Sample>::Float,
NumChannels = <Self::Frame as Frame>::NumChannels,
>,
{
ScaleAmpPerChannel {
signal: self,
amp_frame: amp_frame,
}
}
fn mul_hz<M, I>(self, interpolator: I, mul_per_frame: M) -> MulHz<Self, M, I>
where
Self: Sized,
M: Signal<Frame = [f64; 1]>,
I: Interpolator,
{
MulHz {
signal: Converter::scale_playback_hz(self, interpolator, 1.0),
mul_per_frame: mul_per_frame,
}
}
fn from_hz_to_hz<I>(self, interpolator: I, source_hz: f64, target_hz: f64) -> Converter<Self, I>
where
Self: Sized,
I: Interpolator,
{
Converter::from_hz_to_hz(self, interpolator, source_hz, target_hz)
}
fn scale_hz<I>(self, interpolator: I, multi: f64) -> Converter<Self, I>
where
Self: Sized,
I: Interpolator,
{
Converter::scale_playback_hz(self, interpolator, multi)
}
fn delay(self, n_frames: usize) -> Delay<Self>
where
Self: Sized,
{
Delay {
signal: self,
n_frames: n_frames,
}
}
fn into_interleaved_samples(mut self) -> IntoInterleavedSamples<Self>
where
Self: Sized,
{
let first = self.next().channels();
IntoInterleavedSamples {
signal: self,
current_frame: first,
}
}
fn clip_amp(self, thresh: <<Self::Frame as Frame>::Sample as Sample>::Signed) -> ClipAmp<Self>
where
Self: Sized,
{
ClipAmp {
signal: self,
thresh: thresh,
}
}
fn inspect<F>(self, inspect: F) -> Inspect<Self, F>
where
Self: Sized,
F: FnMut(&Self::Frame),
{
Inspect {
signal: self,
inspect: inspect,
}
}
fn fork<S>(self, ring_buffer: ring_buffer::Bounded<S>) -> Fork<Self, S>
where
Self: Sized,
S: ring_buffer::SliceMut<Element=Self::Frame>,
{
assert!(ring_buffer.is_empty());
let shared = ForkShared {
signal: self,
ring_buffer: ring_buffer,
pending: Fork::<Self, S>::B,
};
Fork { shared: RefCell::new(shared) }
}
fn bus(self) -> Bus<Self>
where
Self: Sized,
{
Bus::new(self, BTreeMap::new())
}
fn take(self, n: usize) -> Take<Self>
where
Self: Sized,
{
Take { signal: self, n: n }
}
fn until_exhausted(self) -> UntilExhausted<Self>
where
Self: Sized,
{
UntilExhausted {
signal: self,
}
}
fn buffered<S>(self, ring_buffer: ring_buffer::Bounded<S>) -> Buffered<Self, S>
where
Self: Sized,
S: ring_buffer::Slice<Element = Self::Frame> + ring_buffer::SliceMut,
{
Buffered {
signal: self,
ring_buffer: ring_buffer,
}
}
fn rms<S>(self, ring_buffer: ring_buffer::Fixed<S>) -> Rms<Self, S>
where
Self: Sized,
S: ring_buffer::Slice<Element = <Self::Frame as Frame>::Float> + ring_buffer::SliceMut,
{
Rms {
signal: self,
rms: rms::Rms::new(ring_buffer),
}
}
fn detect_envelope<D>(
self,
detector: envelope::Detector<Self::Frame, D>,
) -> DetectEnvelope<Self, D>
where
Self: Sized,
D: envelope::Detect<Self::Frame>,
{
DetectEnvelope {
signal: self,
detector: detector,
}
}
fn by_ref(&mut self) -> &mut Self
where
Self: Sized,
{
self
}
}
pub fn lift<I, F, S>(iter: I, f: F) -> UntilExhausted<S>
where
I: IntoIterator,
I::Item: Frame,
F: FnOnce(FromIterator<I::IntoIter>) -> S,
S: Signal<Frame = I::Item>,
{
let iter = iter.into_iter();
let signal = from_iter(iter);
let new_signal = f(signal);
new_signal.until_exhausted()
}
#[derive(Clone)]
pub struct Equilibrium<F> {
frame: core::marker::PhantomData<F>,
}
#[derive(Clone)]
pub struct Gen<G, F> {
gen: G,
frame: core::marker::PhantomData<F>,
}
#[derive(Clone)]
pub struct GenMut<G, F> {
gen_mut: G,
frame: core::marker::PhantomData<F>,
}
#[derive(Clone)]
pub struct Map<S, M, F> {
signal: S,
map: M,
frame: core::marker::PhantomData<F>,
}
#[derive(Clone)]
pub struct ZipMap<S, O, M, F> {
this: S,
other: O,
map: M,
frame: core::marker::PhantomData<F>,
}
#[derive(Clone)]
pub struct FromIterator<I>
where
I: Iterator,
{
iter: I,
next: Option<I::Item>,
}
#[derive(Clone)]
pub struct FromInterleavedSamplesIterator<I, F>
where
I: Iterator,
I::Item: Sample,
F: Frame<Sample = I::Item>,
{
samples: I,
next: Option<F>,
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Rate {
hz: f64,
}
#[derive(Clone)]
pub struct ConstHz {
step: f64,
}
#[derive(Clone)]
pub struct Hz<S> {
hz: S,
rate: Rate,
}
#[derive(Clone)]
pub struct Phase<S> {
step: S,
next: f64,
}
#[derive(Clone)]
pub struct Sine<S> {
phase: Phase<S>,
}
#[derive(Clone)]
pub struct Saw<S> {
phase: Phase<S>,
}
#[derive(Clone)]
pub struct Square<S> {
phase: Phase<S>,
}
#[derive(Clone)]
pub struct Noise {
seed: u64,
}
#[derive(Clone)]
pub struct NoiseSimplex<S> {
phase: Phase<S>,
}
#[derive(Clone)]
pub struct AddAmp<A, B> {
a: A,
b: B,
}
#[derive(Clone)]
pub struct MulAmp<A, B> {
a: A,
b: B,
}
#[derive(Clone)]
pub struct OffsetAmp<S>
where
S: Signal,
{
signal: S,
offset: <<S::Frame as Frame>::Sample as Sample>::Signed,
}
#[derive(Clone)]
pub struct ScaleAmp<S>
where
S: Signal,
{
signal: S,
amp: <<S::Frame as Frame>::Sample as Sample>::Float,
}
#[derive(Clone)]
pub struct OffsetAmpPerChannel<S, F> {
signal: S,
amp_frame: F,
}
#[derive(Clone)]
pub struct ScaleAmpPerChannel<S, F> {
signal: S,
amp_frame: F,
}
#[derive(Clone)]
pub struct MulHz<S, M, I>
where
S: Signal,
I: Interpolator,
{
signal: Converter<S, I>,
mul_per_frame: M,
}
#[derive(Clone)]
pub struct Delay<S> {
signal: S,
n_frames: usize,
}
#[derive(Clone)]
pub struct Inspect<S, F> {
signal: S,
inspect: F,
}
pub struct IntoInterleavedSamples<S>
where
S: Signal,
{
signal: S,
current_frame: <S::Frame as Frame>::Channels,
}
pub struct IntoInterleavedSamplesIterator<S>
where
S: Signal,
{
samples: IntoInterleavedSamples<S>,
}
#[derive(Clone)]
pub struct UntilExhausted<S>
where
S: Signal,
{
signal: S,
}
#[derive(Clone)]
pub struct ClipAmp<S>
where
S: Signal,
{
signal: S,
thresh: <<S::Frame as Frame>::Sample as Sample>::Signed,
}
#[derive(Clone)]
pub struct Fork<S, D> {
shared: RefCell<ForkShared<S, D>>,
}
#[derive(Clone)]
struct ForkShared<S, D> {
signal: S,
ring_buffer: ring_buffer::Bounded<D>,
pending: bool,
}
impl<S, D> Fork<S, D> {
const A: bool = true;
const B: bool = false;
pub fn by_rc(self) -> (BranchRcA<S, D>, BranchRcB<S, D>) {
let Fork { shared } = self;
let shared_fork = Rc::new(shared);
let a = BranchRcA { shared_fork: shared_fork.clone() };
let b = BranchRcB { shared_fork: shared_fork };
(a, b)
}
pub fn by_ref(&mut self) -> (BranchRefA<S, D>, BranchRefB<S, D>) {
let Fork { ref shared } = *self;
let a = BranchRefA { shared_fork: shared };
let b = BranchRefB { shared_fork: shared };
(a, b)
}
}
macro_rules! define_branch {
($TRc:ident, $TRef:ident, $SELF:ident, $OTHER:ident) => {
pub struct $TRc<S, D> {
shared_fork: Rc<RefCell<ForkShared<S, D>>>,
}
pub struct $TRef<'a, S: 'a, D: 'a> {
shared_fork: &'a RefCell<ForkShared<S, D>>,
}
impl<S, D> Signal for $TRc<S, D>
where
S: Signal,
D: ring_buffer::SliceMut<Element=S::Frame>,
{
type Frame = S::Frame;
fn next(&mut self) -> Self::Frame {
let mut fork = self.shared_fork.borrow_mut();
if fork.pending == Fork::<S, D>::$SELF {
if let Some(frame) = fork.ring_buffer.pop() {
return frame;
}
fork.pending = Fork::<S, D>::$OTHER;
}
let frame = fork.signal.next();
fork.ring_buffer.push(frame);
frame
}
}
impl<'a, S, D> Signal for $TRef<'a, S, D>
where
S: 'a + Signal,
D: 'a + ring_buffer::SliceMut<Element=S::Frame>,
{
type Frame = S::Frame;
fn next(&mut self) -> Self::Frame {
let mut fork = self.shared_fork.borrow_mut();
if fork.pending == Fork::<S, D>::$SELF {
if let Some(frame) = fork.ring_buffer.pop() {
return frame;
}
fork.pending = Fork::<S, D>::$OTHER;
}
let frame = fork.signal.next();
fork.ring_buffer.push(frame);
frame
}
}
impl<S, D> $TRc<S, D>
where
D: ring_buffer::Slice,
D::Element: Copy,
{
pub fn pending_frames(&self) -> usize {
let fork = self.shared_fork.borrow();
if fork.pending == Fork::<S, D>::$SELF {
fork.ring_buffer.len()
} else {
0
}
}
}
impl<'a, S, D> $TRef<'a, S, D>
where
D: ring_buffer::Slice,
D::Element: Copy,
{
pub fn pending_frames(&self) -> usize {
let fork = self.shared_fork.borrow();
if fork.pending == Fork::<S, D>::$SELF {
fork.ring_buffer.len()
} else {
0
}
}
}
};
}
define_branch!(BranchRcA, BranchRefA, A, B);
define_branch!(BranchRcB, BranchRefB, B, A);
pub struct Bus<S>
where
S: Signal,
{
node: Rc<core::cell::RefCell<SharedNode<S>>>,
}
struct SharedNode<S>
where
S: Signal,
{
signal: S,
buffer: VecDeque<S::Frame>,
frames_read: BTreeMap<usize, usize>,
next_key: usize,
}
pub struct Output<S>
where
S: Signal,
{
key: usize,
node: Rc<core::cell::RefCell<SharedNode<S>>>,
}
#[derive(Clone)]
pub struct Take<S>
where
S: Signal,
{
signal: S,
n: usize,
}
#[derive(Clone)]
pub struct Buffered<S, D> {
signal: S,
ring_buffer: ring_buffer::Bounded<D>,
}
pub struct BufferedFrames<'a, D: 'a> {
ring_buffer: &'a mut ring_buffer::Bounded<D>,
}
#[derive(Clone)]
pub struct Rms<S, D>
where
S: Signal,
D: ring_buffer::Slice<Element = <S::Frame as Frame>::Float>,
{
signal: S,
rms: rms::Rms<S::Frame, D>,
}
#[derive(Clone)]
pub struct DetectEnvelope<S, D>
where
S: Signal,
D: envelope::Detect<S::Frame>,
{
signal: S,
detector: envelope::Detector<S::Frame, D>,
}
pub fn equilibrium<F>() -> Equilibrium<F>
where
F: Frame,
{
Equilibrium { frame: core::marker::PhantomData }
}
pub fn gen<G, F>(gen: G) -> Gen<G, F>
where
G: Fn() -> F,
F: Frame,
{
Gen {
gen: gen,
frame: core::marker::PhantomData,
}
}
pub fn gen_mut<G, F>(gen_mut: G) -> GenMut<G, F>
where
G: FnMut() -> F,
F: Frame,
{
GenMut {
gen_mut: gen_mut,
frame: core::marker::PhantomData,
}
}
pub fn from_iter<I>(frames: I) -> FromIterator<I::IntoIter>
where
I: IntoIterator,
I::Item: Frame,
{
let mut iter = frames.into_iter();
let next = iter.next();
FromIterator {
iter: iter,
next: next,
}
}
pub fn from_interleaved_samples_iter<I, F>(
samples: I,
) -> FromInterleavedSamplesIterator<I::IntoIter, F>
where
I: IntoIterator,
I::Item: Sample,
F: Frame<Sample = I::Item>,
{
let mut samples = samples.into_iter();
let next = Frame::from_samples(&mut samples);
FromInterleavedSamplesIterator {
samples: samples,
next: next,
}
}
pub fn phase<S>(step: S) -> Phase<S>
where
S: Step,
{
Phase {
step: step,
next: 0.0,
}
}
pub fn rate(hz: f64) -> Rate {
Rate { hz: hz }
}
pub fn sine<S>(phase: Phase<S>) -> Sine<S> {
Sine { phase: phase }
}
pub fn saw<S>(phase: Phase<S>) -> Saw<S> {
Saw { phase: phase }
}
pub fn square<S>(phase: Phase<S>) -> Square<S> {
Square { phase: phase }
}
pub fn noise(seed: u64) -> Noise {
Noise { seed: seed }
}
pub fn noise_simplex<S>(phase: Phase<S>) -> NoiseSimplex<S> {
NoiseSimplex { phase: phase }
}
impl<'a, S> Signal for &'a mut S
where
S: Signal + ?Sized,
{
type Frame = S::Frame;
#[inline]
fn next(&mut self) -> Self::Frame {
(**self).next()
}
#[inline]
fn is_exhausted(&self) -> bool {
(**self).is_exhausted()
}
}
impl<S> Signal for Box<S>
where
S: Signal + ?Sized,
{
type Frame = S::Frame;
#[inline]
fn next(&mut self) -> Self::Frame {
(**self).next()
}
#[inline]
fn is_exhausted(&self) -> bool {
(**self).is_exhausted()
}
}
impl<I> Signal for FromIterator<I>
where
I: Iterator,
I::Item: Frame,
{
type Frame = I::Item;
#[inline]
fn next(&mut self) -> Self::Frame {
match self.next.take() {
Some(frame) => {
self.next = self.iter.next();
frame
},
None => Frame::equilibrium(),
}
}
#[inline]
fn is_exhausted(&self) -> bool {
self.next.is_none()
}
}
impl<I, F> Signal for FromInterleavedSamplesIterator<I, F>
where
I: Iterator,
I::Item: Sample,
F: Frame<Sample = I::Item>,
{
type Frame = F;
#[inline]
fn next(&mut self) -> Self::Frame {
match self.next.take() {
Some(frame) => {
self.next = F::from_samples(&mut self.samples);
frame
},
None => F::equilibrium(),
}
}
#[inline]
fn is_exhausted(&self) -> bool {
self.next.is_none()
}
}
impl<F> Signal for Equilibrium<F>
where
F: Frame,
{
type Frame = F;
#[inline]
fn next(&mut self) -> Self::Frame {
F::equilibrium()
}
}
impl<G, F> Signal for Gen<G, F>
where
G: Fn() -> F,
F: Frame,
{
type Frame = F;
#[inline]
fn next(&mut self) -> Self::Frame {
(self.gen)()
}
}
impl<G, F> Signal for GenMut<G, F>
where
G: FnMut() -> F,
F: Frame,
{
type Frame = F;
#[inline]
fn next(&mut self) -> Self::Frame {
(self.gen_mut)()
}
}
impl<S, M, F> Signal for Map<S, M, F>
where
S: Signal,
M: FnMut(S::Frame) -> F,
F: Frame,
{
type Frame = F;
#[inline]
fn next(&mut self) -> Self::Frame {
(self.map)(self.signal.next())
}
fn is_exhausted(&self) -> bool {
self.signal.is_exhausted()
}
}
impl<S, O, M, F> Signal for ZipMap<S, O, M, F>
where
S: Signal,
O: Signal,
M: FnMut(S::Frame, O::Frame) -> F,
F: Frame,
{
type Frame = F;
#[inline]
fn next(&mut self) -> Self::Frame {
(self.map)(self.this.next(), self.other.next())
}
fn is_exhausted(&self) -> bool {
self.this.is_exhausted() || self.other.is_exhausted()
}
}
impl<S> Signal for Hz<S>
where
S: Signal<Frame = [f64; 1]>,
{
type Frame = [f64; 1];
#[inline]
fn next(&mut self) -> Self::Frame {
[self.step()]
}
#[inline]
fn is_exhausted(&self) -> bool {
self.hz.is_exhausted()
}
}
impl Signal for ConstHz {
type Frame = [f64; 1];
#[inline]
fn next(&mut self) -> Self::Frame {
[self.step()]
}
}
impl<S> Signal for Phase<S>
where
S: Step,
{
type Frame = [f64; 1];
#[inline]
fn next(&mut self) -> Self::Frame {
[self.next_phase()]
}
}
impl<S> Signal for Sine<S>
where
S: Step,
{
type Frame = [f64; 1];
#[inline]
fn next(&mut self) -> Self::Frame {
const PI_2: f64 = core::f64::consts::PI * 2.0;
let phase = self.phase.next_phase();
[super::ops::f64::sin(PI_2 * phase)]
}
}
impl<S> Signal for Saw<S>
where
S: Step,
{
type Frame = [f64; 1];
#[inline]
fn next(&mut self) -> Self::Frame {
let phase = self.phase.next_phase();
[phase * -2.0 + 1.0]
}
}
impl<S> Signal for Square<S>
where
S: Step,
{
type Frame = [f64; 1];
#[inline]
fn next(&mut self) -> Self::Frame {
let phase = self.phase.next_phase();
[if phase < 0.5 { 1.0 } else { -1.0 }]
}
}
impl Rate {
pub fn const_hz(self, hz: f64) -> ConstHz {
ConstHz { step: hz / self.hz }
}
pub fn hz<S>(self, hz: S) -> Hz<S>
where
S: Signal<Frame = [f64; 1]>,
{
Hz {
hz: hz,
rate: self,
}
}
}
impl<S> Hz<S>
where
S: Signal<Frame = [f64; 1]>,
{
#[inline]
pub fn phase(self) -> Phase<Self> {
phase(self)
}
#[inline]
pub fn sine(self) -> Sine<Self> {
self.phase().sine()
}
#[inline]
pub fn saw(self) -> Saw<Self> {
self.phase().saw()
}
#[inline]
pub fn square(self) -> Square<Self> {
self.phase().square()
}
#[inline]
pub fn noise_simplex(self) -> NoiseSimplex<Self> {
self.phase().noise_simplex()
}
}
impl ConstHz {
#[inline]
pub fn phase(self) -> Phase<Self> {
phase(self)
}
#[inline]
pub fn sine(self) -> Sine<Self> {
self.phase().sine()
}
#[inline]
pub fn saw(self) -> Saw<Self> {
self.phase().saw()
}
#[inline]
pub fn square(self) -> Square<Self> {
self.phase().square()
}
#[inline]
pub fn noise_simplex(self) -> NoiseSimplex<Self> {
self.phase().noise_simplex()
}
}
pub trait Step {
fn step(&mut self) -> f64;
}
impl Step for ConstHz {
#[inline]
fn step(&mut self) -> f64 {
self.step
}
}
impl<S> Step for Hz<S>
where
S: Signal<Frame = [f64; 1]>,
{
#[inline]
fn step(&mut self) -> f64 {
let hz = self.hz.next()[0];
hz / self.rate.hz
}
}
impl<S> Phase<S>
where
S: Step,
{
#[inline]
pub fn next_phase_wrapped_to(&mut self, rem: f64) -> f64 {
let phase = self.next;
self.next = (self.next + self.step.step()) % rem;
phase
}
#[inline]
pub fn next_phase(&mut self) -> f64 {
self.next_phase_wrapped_to(1.0)
}
#[inline]
pub fn sine(self) -> Sine<S> {
sine(self)
}
#[inline]
pub fn saw(self) -> Saw<S> {
saw(self)
}
#[inline]
pub fn square(self) -> Square<S> {
square(self)
}
#[inline]
pub fn noise_simplex(self) -> NoiseSimplex<S> {
noise_simplex(self)
}
}
impl Noise {
#[inline]
pub fn next_sample(&mut self) -> f64 {
fn noise_1(seed: u64) -> f64 {
const PRIME_1: u64 = 15_731;
const PRIME_2: u64 = 789_221;
const PRIME_3: u64 = 1_376_312_589;
let x = (seed << 13) ^ seed;
1.0 -
(x.wrapping_mul(x.wrapping_mul(x).wrapping_mul(PRIME_1).wrapping_add(
PRIME_2,
)).wrapping_add(PRIME_3) & 0x7fffffff) as f64 / 1_073_741_824.0
}
let noise = noise_1(self.seed);
self.seed += 1;
noise
}
}
impl Signal for Noise {
type Frame = [f64; 1];
#[inline]
fn next(&mut self) -> Self::Frame {
[self.next_sample()]
}
}
impl<S> NoiseSimplex<S>
where
S: Step,
{
#[inline]
pub fn next_sample(&mut self) -> f64 {
const TWO_POW_SIXTEEN: f64 = 65_536.0;
let phase = self.phase.next_phase_wrapped_to(TWO_POW_SIXTEEN);
fn simplex_noise_1d(x: f64) -> f64 {
const PERM: [u8; 256] = [
151,
160,
137,
91,
90,
15,
131,
13,
201,
95,
96,
53,
194,
233,
7,
225,
140,
36,
103,
30,
69,
142,
8,
99,
37,
240,
21,
10,
23,
190,
6,
148,
247,
120,
234,
75,
0,
26,
197,
62,
94,
252,
219,
203,
117,
35,
11,
32,
57,
177,
33,
88,
237,
149,
56,
87,
174,
20,
125,
136,
171,
168,
68,
175,
74,
165,
71,
134,
139,
48,
27,
166,
77,
146,
158,
231,
83,
111,
229,
122,
60,
211,
133,
230,
220,
105,
92,
41,
55,
46,
245,
40,
244,
102,
143,
54,
65,
25,
63,
161,
1,
216,
80,
73,
209,
76,
132,
187,
208,
89,
18,
169,
200,
196,
135,
130,
116,
188,
159,
86,
164,
100,
109,
198,
173,
186,
3,
64,
52,
217,
226,
250,
124,
123,
5,
202,
38,
147,
118,
126,
255,
82,
85,
212,
207,
206,
59,
227,
47,
16,
58,
17,
182,
189,
28,
42,
223,
183,
170,
213,
119,
248,
152,
2,
44,
154,
163,
70,
221,
153,
101,
155,
167,
43,
172,
9,
129,
22,
39,
253,
19,
98,
108,
110,
79,
113,
224,
232,
178,
185,
112,
104,
218,
246,
97,
228,
251,
34,
242,
193,
238,
210,
144,
12,
191,
179,
162,
241,
81,
51,
145,
235,
249,
14,
239,
107,
49,
192,
214,
31,
181,
199,
106,
157,
184,
84,
204,
176,
115,
121,
50,
45,
127,
4,
150,
254,
138,
236,
205,
93,
222,
114,
67,
29,
24,
72,
243,
141,
128,
195,
78,
66,
215,
61,
156,
180,
];
fn hash(i: i64) -> u8 {
PERM[(i as u8) as usize]
}
fn grad(hash: i64, x: f64) -> f64 {
let h = hash & 0x0F;
let mut grad = 1.0 + (h & 7) as f64;
if (h & 8) != 0 {
grad = -grad;
}
grad * x
}
let i0 = super::ops::f64::floor(x) as i64;
let i1 = i0 + 1;
let x0 = x - i0 as f64;
let x1 = x0 - 1.0;
let mut t0 = 1.0 - x0 * x0;
t0 *= t0;
let n0 = t0 * t0 * grad(hash(i0) as i64, x0);
let mut t1 = 1.0 - x1 * x1;
t1 *= t1;
let n1 = t1 * t1 * grad(hash(i1) as i64, x1);
0.395 * (n0 + n1)
}
simplex_noise_1d(phase)
}
}
impl<S> Signal for NoiseSimplex<S>
where
S: Step,
{
type Frame = [f64; 1];
#[inline]
fn next(&mut self) -> Self::Frame {
[self.next_sample()]
}
}
impl<A, B> Signal for AddAmp<A, B>
where
A: Signal,
B: Signal,
B::Frame: Frame<Sample=<<A::Frame as Frame>::Sample as Sample>::Signed,
NumChannels=<A::Frame as Frame>::NumChannels>,
{
type Frame = A::Frame;
#[inline]
fn next(&mut self) -> Self::Frame {
self.a.next().add_amp(self.b.next())
}
#[inline]
fn is_exhausted(&self) -> bool {
self.a.is_exhausted() || self.b.is_exhausted()
}
}
impl<A, B> Signal for MulAmp<A, B>
where
A: Signal,
B: Signal,
B::Frame: Frame<Sample=<<A::Frame as Frame>::Sample as Sample>::Float,
NumChannels=<A::Frame as Frame>::NumChannels>,
{
type Frame = A::Frame;
#[inline]
fn next(&mut self) -> Self::Frame {
self.a.next().mul_amp(self.b.next())
}
#[inline]
fn is_exhausted(&self) -> bool {
self.a.is_exhausted() || self.b.is_exhausted()
}
}
impl<S> Signal for ScaleAmp<S>
where
S: Signal,
{
type Frame = S::Frame;
#[inline]
fn next(&mut self) -> Self::Frame {
self.signal.next().scale_amp(self.amp)
}
#[inline]
fn is_exhausted(&self) -> bool {
self.signal.is_exhausted()
}
}
impl<S, F> Signal for ScaleAmpPerChannel<S, F>
where
S: Signal,
F: Frame<Sample=<<S::Frame as Frame>::Sample as Sample>::Float,
NumChannels=<S::Frame as Frame>::NumChannels>,
{
type Frame = S::Frame;
#[inline]
fn next(&mut self) -> Self::Frame {
self.signal.next().mul_amp(self.amp_frame)
}
#[inline]
fn is_exhausted(&self) -> bool {
self.signal.is_exhausted()
}
}
impl<S> Signal for OffsetAmp<S>
where
S: Signal,
{
type Frame = S::Frame;
#[inline]
fn next(&mut self) -> Self::Frame {
self.signal.next().offset_amp(self.offset)
}
#[inline]
fn is_exhausted(&self) -> bool {
self.signal.is_exhausted()
}
}
impl<S, F> Signal for OffsetAmpPerChannel<S, F>
where
S: Signal,
F: Frame<Sample=<<S::Frame as Frame>::Sample as Sample>::Signed,
NumChannels=<S::Frame as Frame>::NumChannels>,
{
type Frame = S::Frame;
#[inline]
fn next(&mut self) -> Self::Frame {
self.signal.next().add_amp(self.amp_frame)
}
#[inline]
fn is_exhausted(&self) -> bool {
self.signal.is_exhausted()
}
}
impl<S, M, I> Signal for MulHz<S, M, I>
where
S: Signal,
<S::Frame as Frame>::Sample: Duplex<f64>,
M: Signal<Frame = [f64; 1]>,
I: Interpolator<Frame = S::Frame>,
{
type Frame = S::Frame;
#[inline]
fn next(&mut self) -> Self::Frame {
let mul = self.mul_per_frame.next()[0];
self.signal.set_playback_hz_scale(mul);
self.signal.next()
}
#[inline]
fn is_exhausted(&self) -> bool {
self.signal.is_exhausted() || self.mul_per_frame.is_exhausted()
}
}
impl<S> Signal for Delay<S>
where
S: Signal,
{
type Frame = S::Frame;
#[inline]
fn next(&mut self) -> Self::Frame {
if self.n_frames > 0 {
self.n_frames -= 1;
Self::Frame::equilibrium()
} else {
self.signal.next()
}
}
#[inline]
fn is_exhausted(&self) -> bool {
self.n_frames == 0 && self.signal.is_exhausted()
}
}
impl<S, F> Signal for Inspect<S, F>
where
S: Signal,
F: FnMut(&S::Frame),
{
type Frame = S::Frame;
#[inline]
fn next(&mut self) -> Self::Frame {
let out = self.signal.next();
(self.inspect)(&out);
out
}
#[inline]
fn is_exhausted(&self) -> bool {
self.signal.is_exhausted()
}
}
impl<S> IntoInterleavedSamples<S>
where
S: Signal,
{
#[inline]
pub fn next_sample(&mut self) -> <S::Frame as Frame>::Sample {
loop {
match self.current_frame.next() {
Some(channel) => return channel,
None => self.current_frame = self.signal.next().channels(),
}
}
}
#[inline]
pub fn into_iter(self) -> IntoInterleavedSamplesIterator<S> {
IntoInterleavedSamplesIterator { samples: self }
}
}
impl<S> Iterator for IntoInterleavedSamplesIterator<S>
where
S: Signal,
{
type Item = <S::Frame as Frame>::Sample;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
Some(self.samples.next_sample())
}
}
impl<S> Iterator for UntilExhausted<S>
where
S: Signal,
{
type Item = S::Frame;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
if self.signal.is_exhausted() {
return None;
}
Some(self.signal.next())
}
}
impl<S> Clone for IntoInterleavedSamples<S>
where
S: Signal + Clone,
<S::Frame as Frame>::Channels: Clone,
{
#[inline]
fn clone(&self) -> Self {
IntoInterleavedSamples {
signal: self.signal.clone(),
current_frame: self.current_frame.clone(),
}
}
}
impl<S> Clone for IntoInterleavedSamplesIterator<S>
where
S: Signal,
IntoInterleavedSamples<S>: Clone,
{
#[inline]
fn clone(&self) -> Self {
IntoInterleavedSamplesIterator { samples: self.samples.clone() }
}
}
impl<S> Signal for ClipAmp<S>
where
S: Signal,
{
type Frame = S::Frame;
#[inline]
fn next(&mut self) -> Self::Frame {
let f = self.signal.next();
f.map(|s| {
let s: <<S::Frame as Frame>::Sample as Sample>::Signed = s.to_sample();
if s > self.thresh {
self.thresh
} else if s < -self.thresh {
-self.thresh
} else {
s
}.to_sample()
})
}
#[inline]
fn is_exhausted(&self) -> bool {
self.signal.is_exhausted()
}
}
impl<S> Bus<S>
where
S: Signal,
{
fn new(signal: S, frames_read: BTreeMap<usize, usize>) -> Self {
Bus {
node: Rc::new(core::cell::RefCell::new(SharedNode {
signal: signal,
buffer: VecDeque::new(),
frames_read: frames_read,
next_key: 0,
})),
}
}
#[inline]
pub fn send(&self) -> Output<S> {
let mut node = self.node.borrow_mut();
let key = node.next_key;
node.next_key = node.next_key.wrapping_add(1);
let num_frames = node.buffer.len();
node.frames_read.insert(key, num_frames);
Output {
key: key,
node: self.node.clone(),
}
}
}
impl<S> SharedNode<S>
where
S: Signal,
{
fn next_frame(&mut self, key: usize) -> S::Frame {
let num_frames = self.buffer.len();
let frames_read = self.frames_read.remove(&key).expect(
"no frames_read for Output",
);
let frame = if frames_read < num_frames {
self.buffer[frames_read]
} else {
let frame = self.signal.next();
self.buffer.push_back(frame);
frame
};
let least_frames_read = !self.frames_read.values().any(|&other_frames_read| {
other_frames_read <= frames_read
});
let new_frames_read = if least_frames_read {
self.buffer.pop_front();
for other_frames_read in self.frames_read.values_mut() {
*other_frames_read -= 1;
}
frames_read
} else {
frames_read + 1
};
self.frames_read.insert(key, new_frames_read);
frame
}
#[inline]
fn pending_frames(&self, key: usize) -> usize {
self.buffer.len() - self.frames_read[&key]
}
fn drop_output(&mut self, key: usize) {
self.frames_read.remove(&key);
let least_frames_read = self.frames_read.values().fold(self.buffer.len(), |a, &b| {
core::cmp::min(a, b)
});
if least_frames_read > 0 {
for frames_read in self.frames_read.values_mut() {
*frames_read -= least_frames_read;
}
for _ in 0..least_frames_read {
self.buffer.pop_front();
}
}
}
}
impl<S> Output<S>
where
S: Signal,
{
#[inline]
pub fn pending_frames(&self) -> usize {
self.node.borrow().pending_frames(self.key)
}
}
impl<S> Signal for Output<S>
where
S: Signal,
{
type Frame = S::Frame;
#[inline]
fn next(&mut self) -> Self::Frame {
self.node.borrow_mut().next_frame(self.key)
}
#[inline]
fn is_exhausted(&self) -> bool {
let node = self.node.borrow();
node.pending_frames(self.key) == 0 && node.signal.is_exhausted()
}
}
impl<S> Drop for Output<S>
where
S: Signal,
{
fn drop(&mut self) {
self.node.borrow_mut().drop_output(self.key)
}
}
impl<S> Iterator for Take<S>
where
S: Signal,
{
type Item = S::Frame;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
if self.n == 0 {
return None;
}
self.n -= 1;
Some(self.signal.next())
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.n, Some(self.n))
}
}
impl<S> ExactSizeIterator for Take<S>
where
S: Signal,
{
#[inline]
fn len(&self) -> usize {
self.n
}
}
impl<S, D> Buffered<S, D>
where
S: Signal,
D: ring_buffer::Slice<Element = S::Frame> + ring_buffer::SliceMut,
{
pub fn next_frames(&mut self) -> BufferedFrames<D> {
let Buffered {
ref mut signal,
ref mut ring_buffer,
} = *self;
if ring_buffer.len() == 0 {
for _ in 0..ring_buffer.max_len() {
ring_buffer.push(signal.next());
}
}
BufferedFrames { ring_buffer: ring_buffer }
}
pub fn into_parts(self) -> (S, ring_buffer::Bounded<D>) {
let Buffered {
signal,
ring_buffer,
} = self;
(signal, ring_buffer)
}
}
impl<S, D> Signal for Buffered<S, D>
where
S: Signal,
D: ring_buffer::Slice<Element=S::Frame> + ring_buffer::SliceMut,
{
type Frame = S::Frame;
fn next(&mut self) -> Self::Frame {
let Buffered { ref mut signal, ref mut ring_buffer } = *self;
loop {
match ring_buffer.pop() {
Some(frame) => return frame,
None => for _ in 0..ring_buffer.max_len() {
ring_buffer.push(signal.next());
},
}
}
}
fn is_exhausted(&self) -> bool {
self.ring_buffer.len() == 0 && self.signal.is_exhausted()
}
}
impl<'a, D> Iterator for BufferedFrames<'a, D>
where
D: ring_buffer::SliceMut,
D::Element: Copy,
{
type Item = D::Element;
fn next(&mut self) -> Option<Self::Item> {
self.ring_buffer.pop()
}
}
impl<S, D> Rms<S, D>
where
S: Signal,
D: ring_buffer::Slice<Element = <S::Frame as Frame>::Float> + ring_buffer::SliceMut,
{
pub fn next_squared(&mut self) -> <Self as Signal>::Frame {
self.rms.next_squared(self.signal.next())
}
pub fn into_parts(self) -> (S, rms::Rms<S::Frame, D>) {
let Rms {
signal,
rms,
} = self;
(signal, rms)
}
}
impl<S, D> Signal for Rms<S, D>
where
S: Signal,
D: ring_buffer::Slice<Element = <S::Frame as Frame>::Float>
+ ring_buffer::SliceMut,
{
type Frame = <S::Frame as Frame>::Float;
fn next(&mut self) -> Self::Frame {
self.rms.next(self.signal.next())
}
fn is_exhausted(&self) -> bool {
self.signal.is_exhausted()
}
}
impl<S, D> DetectEnvelope<S, D>
where
S: Signal,
D: envelope::Detect<S::Frame>,
{
pub fn set_attack_frames(&mut self, frames: f32) {
self.detector.set_attack_frames(frames);
}
pub fn set_release_frames(&mut self, frames: f32) {
self.detector.set_release_frames(frames);
}
pub fn into_parts(self) -> (S, envelope::Detector<S::Frame, D>) {
let DetectEnvelope { signal, detector } = self;
(signal, detector)
}
}
impl<S, D> Signal for DetectEnvelope<S, D>
where
S: Signal,
D: envelope::Detect<S::Frame>,
{
type Frame = D::Output;
fn next(&mut self) -> Self::Frame {
self.detector.next(self.signal.next())
}
fn is_exhausted(&self) -> bool {
self.signal.is_exhausted()
}
}