use {Duplex, Frame, Sample, Vec, Rc, VecDeque};
use rate;
use core;
impl<I> Signal for I where I: Iterator, I::Item: Frame {}
pub trait Signal: Iterator + Sized
where Self::Item: Frame,
{
#[inline]
fn add_amp<S>(self, other: S) -> AddAmp<Self, S>
where S: Signal,
S::Item: Frame<Sample=<<Self::Item as Frame>::Sample as Sample>::Signed,
NumChannels=<Self::Item as Frame>::NumChannels>,
{
AddAmp {
a: self,
b: other,
}
}
#[inline]
fn mul_amp<S>(self, other: S) -> MulAmp<Self, S>
where S: Signal,
S::Item: Frame<Sample=<<Self::Item as Frame>::Sample as Sample>::Float,
NumChannels=<Self::Item as Frame>::NumChannels>,
{
MulAmp {
a: self,
b: other,
}
}
#[inline]
fn offset_amp(self, offset: <<Self::Item as Frame>::Sample as Sample>::Signed)
-> OffsetAmp<Self>
{
OffsetAmp {
signal: self,
offset: offset,
}
}
#[inline]
fn scale_amp(self, amp: <<Self::Item as Frame>::Sample as Sample>::Float) -> ScaleAmp<Self> {
ScaleAmp {
signal: self,
amp: amp,
}
}
#[inline]
fn offset_amp_per_channel<F>(self, amp_frame: F) -> OffsetAmpPerChannel<Self, F>
where F: Frame<Sample=<<Self::Item as Frame>::Sample as Sample>::Signed,
NumChannels=<Self::Item 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 F: Frame<Sample=<<Self::Item as Frame>::Sample as Sample>::Float,
NumChannels=<Self::Item as Frame>::NumChannels>,
{
ScaleAmpPerChannel {
signal: self,
amp_frame: amp_frame,
}
}
fn mul_hz<I>(self, mul_per_frame: I) -> MulHz<Self, I>
where I: Iterator<Item=f64>,
{
MulHz {
signal: rate::Converter::scale_playback_hz(self, 1.0),
mul_per_frame: mul_per_frame,
}
}
fn from_hz_to_hz(self, source_hz: f64, target_hz: f64) -> rate::Converter<Self> {
rate::Converter::from_hz_to_hz(self, source_hz, target_hz)
}
fn scale_hz(self, multi: f64) -> rate::Converter<Self> {
rate::Converter::scale_playback_hz(self, multi)
}
fn delay(self, n_frames: usize) -> Delay<Self> {
Delay {
signal: self,
n_frames: n_frames,
}
}
fn to_samples(self) -> ToSamples<Self> {
ToSamples {
signal: self,
current_frame: None,
}
}
fn clip_amp(self, thresh: <<Self::Item as Frame>::Sample as Sample>::Signed) -> ClipAmp<Self> {
ClipAmp {
signal: self,
thresh: thresh,
}
}
fn bus(self) -> Bus<Self> {
Bus {
node: Rc::new(core::cell::RefCell::new(SharedNode {
signal: self,
buffers: vec![VecDeque::new()],
})),
}
}
}
#[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 FromInterleavedSamples<I, F>
where I: Iterator,
I::Item: Sample,
F: Frame<Sample=I::Item>,
{
samples: I,
frame: core::marker::PhantomData<F>,
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Rate {
hz: f64,
}
#[derive(Clone)]
pub struct ConstHz {
step: f64,
}
#[derive(Clone)]
pub struct Hz<I> {
hz: I,
last_step_size: f64,
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,
S::Item: Frame,
{
signal: S,
offset: <<S::Item as Frame>::Sample as Sample>::Signed,
}
#[derive(Clone)]
pub struct ScaleAmp<S>
where S: Signal,
S::Item: Frame,
{
signal: S,
amp: <<S::Item 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>
where S: Signal,
S::Item: Frame,
{
signal: rate::Converter<S>,
mul_per_frame: M,
}
#[derive(Clone)]
pub struct Delay<S> {
signal: S,
n_frames: usize,
}
pub struct ToSamples<S>
where S: Signal,
S::Item: Frame,
{
signal: S,
current_frame: Option<<S::Item as Frame>::Channels>,
}
#[derive(Clone)]
pub struct ClipAmp<S>
where S: Signal,
S::Item: Frame,
{
signal: S,
thresh: <<S::Item as Frame>::Sample as Sample>::Signed,
}
pub struct Bus<S>
where S: Signal,
S::Item: Frame,
{
node: Rc<core::cell::RefCell<SharedNode<S>>>,
}
struct SharedNode<S>
where S: Signal,
S::Item: Frame,
{
signal: S,
buffers: Vec<VecDeque<S::Item>>,
}
pub struct Output<S>
where S: Signal,
S::Item: Frame,
{
idx: usize,
node: Rc<core::cell::RefCell<SharedNode<S>>>,
}
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_interleaved_samples<I, F>(samples: I) -> FromInterleavedSamples<I, F>
where I: Iterator,
I::Item: Sample,
F: Frame<Sample=I::Item>,
{
FromInterleavedSamples {
samples: samples,
frame: core::marker::PhantomData,
}
}
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<F> Iterator for Equilibrium<F>
where F: Frame,
{
type Item = F;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
Some(F::equilibrium())
}
}
impl<F> DoubleEndedIterator for Equilibrium<F>
where F: Frame,
{
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
Some(F::equilibrium())
}
}
impl<G, F> Iterator for Gen<G, F>
where G: Fn() -> F,
{
type Item = F;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
Some((self.gen)())
}
}
impl<G, F> Iterator for GenMut<G, F>
where G: FnMut() -> F,
{
type Item = F;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
Some((self.gen_mut)())
}
}
impl<I, F> Iterator for FromInterleavedSamples<I, F>
where I: Iterator,
I::Item: Sample,
F: Frame<Sample=I::Item>,
{
type Item = F;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
F::from_samples(&mut self.samples)
}
}
impl Rate {
pub fn const_hz(self, hz: f64) -> ConstHz {
ConstHz { step: hz / self.hz }
}
pub fn hz<I>(self, init: f64, hz: I) -> Hz<I>
where I: Iterator<Item=f64>,
{
Hz {
hz: hz,
last_step_size: init / self.hz,
rate: self,
}
}
}
impl<I> Hz<I>
where I: Iterator<Item=f64>,
{
#[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<I> Step for Hz<I>
where I: Iterator<Item=f64>,
{
#[inline]
fn step(&mut self) -> f64 {
match self.hz.next() {
Some(hz) => {
self.last_step_size = hz / self.rate.hz;
hz
},
None => self.last_step_size,
}
}
}
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<I> Iterator for Hz<I>
where I: Iterator<Item=f64>,
{
type Item = f64;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
Some(self.step())
}
}
impl Iterator for ConstHz {
type Item = f64;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
Some(self.step())
}
}
impl<S> Iterator for Phase<S>
where S: Step,
{
type Item = [f64; 1];
#[inline]
fn next(&mut self) -> Option<Self::Item> {
Some([self.next_phase()])
}
}
impl<S> Iterator for Sine<S>
where S: Step,
{
type Item = [f64; 1];
#[inline]
fn next(&mut self) -> Option<Self::Item> {
const PI_2: f64 = core::f64::consts::PI * 2.0;
let phase = self.phase.next_phase();
Some([super::ops::f64::sin(PI_2 * phase)])
}
}
impl<S> Iterator for Saw<S>
where S: Step,
{
type Item = [f64; 1];
#[inline]
fn next(&mut self) -> Option<Self::Item> {
let phase = self.phase.next_phase();
Some([phase * -2.0 + 1.0])
}
}
impl<S> Iterator for Square<S>
where S: Step,
{
type Item = [f64; 1];
#[inline]
fn next(&mut self) -> Option<Self::Item> {
let phase = self.phase.next_phase();
Some([if phase < 0.5 { 1.0 } else { -1.0 }])
}
}
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 Iterator for Noise {
type Item = [f64; 1];
#[inline]
fn next(&mut self) -> Option<Self::Item> {
Some([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> Iterator for NoiseSimplex<S>
where S: Step,
{
type Item = [f64; 1];
#[inline]
fn next(&mut self) -> Option<Self::Item> {
Some([self.next_sample()])
}
}
#[inline]
fn zipped_size_hint<A, B>(a: &A, b: &B) -> (usize, Option<usize>)
where A: Iterator,
B: Iterator,
{
let (a_lower, a_upper) = a.size_hint();
let (b_lower, b_upper) = b.size_hint();
let lower = core::cmp::min(a_lower, b_lower);
let upper = match (a_upper, b_upper) {
(Some(a), Some(b)) => Some(core::cmp::min(a, b)),
(Some(a), None) => Some(a),
(None, Some(b)) => Some(b),
(None, None) => None,
};
(lower, upper)
}
impl<A, B> Iterator for AddAmp<A, B>
where A: Signal,
B: Signal,
A::Item: Frame,
B::Item: Frame<Sample=<<A::Item as Frame>::Sample as Sample>::Signed,
NumChannels=<A::Item as Frame>::NumChannels>,
{
type Item = A::Item;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.a.next().and_then(|a_f| self.b.next().map(|b_f| a_f.add_amp(b_f)))
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
zipped_size_hint(&self.a, &self.b)
}
}
impl<A, B> ExactSizeIterator for AddAmp<A, B>
where AddAmp<A, B>: Iterator,
A: ExactSizeIterator,
B: ExactSizeIterator,
{
#[inline]
fn len(&self) -> usize {
core::cmp::min(self.a.len(), self.b.len())
}
}
impl<A, B> Iterator for MulAmp<A, B>
where A: Signal,
B: Signal,
A::Item: Frame,
B::Item: Frame<Sample=<<A::Item as Frame>::Sample as Sample>::Float,
NumChannels=<A::Item as Frame>::NumChannels>,
{
type Item = A::Item;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.a.next().and_then(|a_f| self.b.next().map(|b_f| a_f.mul_amp(b_f)))
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
zipped_size_hint(&self.a, &self.b)
}
}
impl<A, B> ExactSizeIterator for MulAmp<A, B>
where MulAmp<A, B>: Iterator,
A: ExactSizeIterator,
B: ExactSizeIterator,
{
#[inline]
fn len(&self) -> usize {
core::cmp::min(self.a.len(), self.b.len())
}
}
impl<S> Iterator for ScaleAmp<S>
where S: Signal,
S::Item: Frame,
{
type Item = S::Item;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.signal.next().map(|f| f.scale_amp(self.amp))
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.signal.size_hint()
}
}
impl<S> ExactSizeIterator for ScaleAmp<S>
where S: Signal + ExactSizeIterator,
S::Item: Frame,
{
#[inline]
fn len(&self) -> usize {
self.signal.len()
}
}
impl<S, F> Iterator for ScaleAmpPerChannel<S, F>
where S: Signal,
S::Item: Frame,
F: Frame<Sample=<<S::Item as Frame>::Sample as Sample>::Float,
NumChannels=<S::Item as Frame>::NumChannels>,
{
type Item = S::Item;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.signal.next().map(|f| f.mul_amp(self.amp_frame))
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.signal.size_hint()
}
}
impl<S, F> ExactSizeIterator for ScaleAmpPerChannel<S, F>
where ScaleAmpPerChannel<S, F>: Iterator,
S: ExactSizeIterator,
{
#[inline]
fn len(&self) -> usize {
self.signal.len()
}
}
impl<S> Iterator for OffsetAmp<S>
where S: Signal,
S::Item: Frame,
{
type Item = S::Item;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.signal.next().map(|f| f.offset_amp(self.offset))
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.signal.size_hint()
}
}
impl<S> ExactSizeIterator for OffsetAmp<S>
where S: Signal + ExactSizeIterator,
S::Item: Frame,
{
#[inline]
fn len(&self) -> usize {
self.signal.len()
}
}
impl<S, F> Iterator for OffsetAmpPerChannel<S, F>
where S: Signal,
S::Item: Frame,
F: Frame<Sample=<<S::Item as Frame>::Sample as Sample>::Signed,
NumChannels=<S::Item as Frame>::NumChannels>,
{
type Item = S::Item;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.signal.next().map(|f| f.add_amp(self.amp_frame))
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.signal.size_hint()
}
}
impl<S, F> ExactSizeIterator for OffsetAmpPerChannel<S, F>
where OffsetAmpPerChannel<S, F>: Iterator,
S: ExactSizeIterator,
{
#[inline]
fn len(&self) -> usize {
self.signal.len()
}
}
impl<S, M> Iterator for MulHz<S, M>
where S: Signal,
S::Item: Frame,
<S::Item as Frame>::Sample: Duplex<f64>,
M: Iterator<Item=f64>,
{
type Item = S::Item;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.mul_per_frame.next().and_then(|mul| {
self.signal.set_playback_hz_scale(mul);
self.signal.next()
})
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(1, None)
}
}
impl<S> Iterator for Delay<S>
where S: Signal,
S::Item: Frame,
{
type Item = S::Item;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
if self.n_frames > 0 {
self.n_frames -= 1;
Some(Frame::equilibrium())
} else {
self.signal.next()
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let (lower, upper) = self.signal.size_hint();
(lower + self.n_frames, upper.map(|n| n + self.n_frames))
}
}
impl<S> ExactSizeIterator for Delay<S>
where Delay<S>: Iterator,
S: ExactSizeIterator,
{
#[inline]
fn len(&self) -> usize {
self.signal.len() + self.n_frames
}
}
impl<S> Iterator for ToSamples<S>
where S: Signal,
S::Item: Frame,
{
type Item = <S::Item as Frame>::Sample;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
loop {
if let Some(ref mut frame) = self.current_frame {
if let Some(channel) = frame.next() {
return Some(channel);
}
}
self.current_frame = match self.signal.next() {
Some(frame) => Some(frame.channels()),
None => return None,
};
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let (lower, upper) = self.signal.size_hint();
let current_frame = self.current_frame.as_ref().map(|chans| chans.size_hint());
let n_channels = <S::Item as Frame>::n_channels();
let lower = lower * n_channels + current_frame.map(|sh| sh.0).unwrap_or(0);
let upper = upper.and_then(|upper| {
let current_upper = match current_frame.map(|sh| sh.1) {
None => 0,
Some(None) => return None,
Some(Some(n)) => n,
};
Some(upper * n_channels + current_upper)
});
(lower, upper)
}
}
impl<S> Clone for ToSamples<S>
where S: Signal + Clone,
S::Item: Frame,
<S::Item as Frame>::Channels: Clone,
{
#[inline]
fn clone(&self) -> Self {
ToSamples {
signal: self.signal.clone(),
current_frame: self.current_frame.clone(),
}
}
}
impl<S> ExactSizeIterator for ToSamples<S>
where ToSamples<S>: Iterator,
S: ExactSizeIterator,
S::Item: Frame,
<S::Item as Frame>::Channels: ExactSizeIterator,
{
#[inline]
fn len(&self) -> usize {
self.signal.len() * <S::Item as Frame>::n_channels()
+ self.current_frame.as_ref().map(|f| f.len()).unwrap_or(0)
}
}
impl<S> Iterator for ClipAmp<S>
where S: Signal,
S::Item: Frame,
{
type Item = S::Item;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.signal.next().map(|f| f.map(|s| {
let s: <<S::Item 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()
}))
}
}
impl<S> Bus<S>
where S: Signal,
S::Item: Frame,
{
#[inline]
pub fn send(&self) -> Output<S> {
let idx = self.node.borrow().buffers.len();
self.node.borrow_mut().buffers.push(VecDeque::new());
Output {
idx: idx,
node: self.node.clone(),
}
}
}
impl<S> SharedNode<S>
where S: Signal,
S::Item: Frame,
{
#[inline]
fn next_frame(&mut self, idx: usize) -> Option<S::Item> {
loop {
match self.buffers[idx].pop_front() {
Some(frame) => return Some(frame),
None => match self.signal.next() {
Some(frame) => {
for buffer in self.buffers.iter_mut() {
buffer.push_back(frame);
}
},
None => return None,
}
}
}
}
#[inline]
fn pending_frames(&self, idx: usize) -> usize {
self.buffers[idx].len()
}
}
impl<S> Output<S>
where S: Signal,
S::Item: Frame,
{
#[inline]
pub fn pending_frames(&self) -> usize {
self.node.borrow().pending_frames(self.idx)
}
}
impl<S> Iterator for Output<S>
where S: Signal,
S::Item: Frame,
{
type Item = S::Item;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.node.borrow_mut().next_frame(self.idx)
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let node = self.node.borrow();
let (lower, upper) = node.signal.size_hint();
let n = node.buffers[self.idx].len();
(lower + n, upper.map(|upper| upper + n))
}
}