|
| 1 | +use crate::{ |
| 2 | + sig::{ |
| 3 | + Buf, Sig, SigBoxed, SigCtx, SigSampleIntoBufT, SigShared, SigT, |
| 4 | + sig_boxed, sig_shared, |
| 5 | + }, |
| 6 | + stereo::{Channel, Stereo, StereoPair}, |
| 7 | +}; |
| 8 | +use std::sync::{Arc, RwLock}; |
| 9 | + |
| 10 | +/// Private inner type of the `Cell_` type. |
| 11 | +struct Unshared<T: Clone> { |
| 12 | + sig_boxed: Arc<RwLock<SigBoxed<T>>>, |
| 13 | + buf: Vec<T>, |
| 14 | +} |
| 15 | + |
| 16 | +impl<T: Clone> Clone for Unshared<T> { |
| 17 | + fn clone(&self) -> Self { |
| 18 | + Self { |
| 19 | + sig_boxed: Arc::clone(&self.sig_boxed), |
| 20 | + buf: Vec::new(), |
| 21 | + } |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +impl<T: Clone> Unshared<T> { |
| 26 | + fn new<S>(sig: S) -> Self |
| 27 | + where |
| 28 | + S: SigT<Item = T> + Sync + Send + 'static, |
| 29 | + { |
| 30 | + Unshared { |
| 31 | + sig_boxed: Arc::new(RwLock::new(sig_boxed(sig).0)), |
| 32 | + buf: Vec::new(), |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + fn set<S>(&self, sig: S) |
| 37 | + where |
| 38 | + S: SigT<Item = T> + Sync + Send + 'static, |
| 39 | + { |
| 40 | + *self.sig_boxed.write().unwrap() = sig_boxed(sig).0; |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +impl<T: Clone> SigT for Unshared<T> { |
| 45 | + type Item = T; |
| 46 | + |
| 47 | + fn sample(&mut self, ctx: &SigCtx) -> impl Buf<Self::Item> { |
| 48 | + let mut unlocked = self.sig_boxed.write().unwrap(); |
| 49 | + unlocked.sample_into_buf(ctx, &mut self.buf); |
| 50 | + &self.buf |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +impl<T: Clone> SigSampleIntoBufT for Unshared<T> { |
| 55 | + type Item = T; |
| 56 | + |
| 57 | + fn sample_into_buf(&mut self, ctx: &SigCtx, buf: &mut Vec<Self::Item>) { |
| 58 | + self.sig_boxed.write().unwrap().sample_into_buf(ctx, buf); |
| 59 | + } |
| 60 | + |
| 61 | + fn sample_into_slice( |
| 62 | + &mut self, |
| 63 | + ctx: &SigCtx, |
| 64 | + stride: usize, |
| 65 | + offset: usize, |
| 66 | + out: &mut [Self::Item], |
| 67 | + ) { |
| 68 | + self.sig_boxed |
| 69 | + .write() |
| 70 | + .unwrap() |
| 71 | + .sample_into_slice(ctx, stride, offset, out); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +pub fn cell_<S>(sig: S) -> Sig<Cell_<S::Item>> |
| 76 | +where |
| 77 | + S: SigT + Sync + Send + 'static, |
| 78 | +{ |
| 79 | + Sig(Cell_::new(sig)) |
| 80 | +} |
| 81 | + |
| 82 | +#[derive(Clone)] |
| 83 | +pub struct Cell_<T: Clone>(Sig<SigShared<Unshared<T>>>); |
| 84 | + |
| 85 | +impl<T: Clone> SigT for Cell_<T> { |
| 86 | + type Item = T; |
| 87 | + |
| 88 | + fn sample(&mut self, ctx: &SigCtx) -> impl Buf<Self::Item> { |
| 89 | + self.0.sample(ctx) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +impl<T: Clone> Cell_<T> { |
| 94 | + pub fn new<S>(sig: S) -> Self |
| 95 | + where |
| 96 | + S: SigT<Item = T> + Sync + Send + 'static, |
| 97 | + { |
| 98 | + Self(sig_shared(Unshared::new(sig))) |
| 99 | + } |
| 100 | + |
| 101 | + pub fn set<S>(&self, sig: S) |
| 102 | + where |
| 103 | + S: SigT<Item = T> + Sync + Send + 'static, |
| 104 | + { |
| 105 | + self.0.0.with_inner_mut(|unshared| unshared.set(sig)); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +impl<T: Clone> Sig<Cell_<T>> { |
| 110 | + pub fn set<S>(&self, sig: S) |
| 111 | + where |
| 112 | + S: SigT<Item = T> + Sync + Send + 'static, |
| 113 | + { |
| 114 | + self.0.set(sig); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +impl<T: Clone> SigSampleIntoBufT for Cell_<T> { |
| 119 | + type Item = T; |
| 120 | + |
| 121 | + fn sample_into_buf(&mut self, ctx: &SigCtx, buf: &mut Vec<Self::Item>) { |
| 122 | + self.0.sample_into_buf(ctx, buf); |
| 123 | + } |
| 124 | + |
| 125 | + fn sample_into_slice( |
| 126 | + &mut self, |
| 127 | + ctx: &SigCtx, |
| 128 | + stride: usize, |
| 129 | + offset: usize, |
| 130 | + out: &mut [Self::Item], |
| 131 | + ) { |
| 132 | + self.0.sample_into_slice(ctx, stride, offset, out); |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +/// A container for a signal yielding a type `T` whose contentents (a signal) can be changed to a |
| 137 | +/// different signal later. The type of the signal is erased (`T` is the type _yielded_ by the |
| 138 | +/// signal) so signals of different types may be placed into the same cell. Call its `set` method |
| 139 | +/// to update the signal contained in the cell. |
| 140 | +pub type Cell<T> = Sig<Cell_<T>>; |
| 141 | +pub type CellF32 = Cell<f32>; |
| 142 | + |
| 143 | +pub fn cell<T>( |
| 144 | + initial_sig: impl SigT<Item = T> + Sync + Send + 'static, |
| 145 | +) -> Cell<T> |
| 146 | +where |
| 147 | + T: Clone, |
| 148 | +{ |
| 149 | + cell_(initial_sig) |
| 150 | +} |
| 151 | + |
| 152 | +pub fn cell_f32( |
| 153 | + initial_sig: impl SigT<Item = f32> + Sync + Send + 'static, |
| 154 | +) -> CellF32 { |
| 155 | + cell_(initial_sig) |
| 156 | +} |
| 157 | + |
| 158 | +/// Create a new signal cell initialized with a constant signal yielding the default value of the |
| 159 | +/// type `T`. |
| 160 | +pub fn cell_default<T>() -> Cell<T> |
| 161 | +where |
| 162 | + T: SigT<Item = T> + Clone + Default + Sync + Send + 'static, |
| 163 | +{ |
| 164 | + cell(T::default()) |
| 165 | +} |
| 166 | + |
| 167 | +impl<T> Stereo<Cell<T>, Cell<T>> |
| 168 | +where |
| 169 | + T: Clone, |
| 170 | +{ |
| 171 | + pub fn set<F, S>(&self, mut f: F) |
| 172 | + where |
| 173 | + S: SigT<Item = T> + Sync + Send + 'static, |
| 174 | + F: FnMut() -> S, |
| 175 | + { |
| 176 | + self.as_ref().with(|s| s.set(f())); |
| 177 | + } |
| 178 | + |
| 179 | + pub fn set_channel<F, S>(&self, mut f: F) |
| 180 | + where |
| 181 | + S: SigT<Item = T> + Sync + Send + 'static, |
| 182 | + F: FnMut(Channel) -> S, |
| 183 | + { |
| 184 | + self.as_ref().with_channel(|channel, s| s.set(f(channel))); |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +impl Stereo<Cell<f32>, Cell<f32>> { |
| 189 | + pub fn with_volume<V>(self, volume: V) -> Self |
| 190 | + where |
| 191 | + V: SigT<Item = f32> + Sync + Send + 'static, |
| 192 | + { |
| 193 | + let out = StereoPair::new_fn(|| cell(0.)); |
| 194 | + let volume = sig_shared(volume); |
| 195 | + self.set_channel(|channel| out.get(channel).clone() * volume.clone()); |
| 196 | + out |
| 197 | + } |
| 198 | +} |
0 commit comments