1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//! Structures for mixing or combining different signals together.

use crate::prelude::*;
use std::cell::UnsafeCell;

/// Combines two [`smp::Mono`] signals into a [`smp::Stereo`] signal. One signal plays on each
/// channel.
pub struct Stereo<X: Signal<Sample = smp::Mono>, Y: Signal<Sample = smp::Mono>>(pub X, pub Y);

impl<X: Signal<Sample = smp::Mono>, Y: Signal<Sample = smp::Mono>> Stereo<X, Y> {
    /// Initializes a new [`Stereo`].
    pub const fn new(sgn1: X, sgn2: Y) -> Self {
        Self(sgn1, sgn2)
    }
}

impl<Z: Signal<Sample = smp::Mono> + Clone> Stereo<Z, Z> {
    /// Duplicates a [`smp::Mono`] signal.
    pub fn dup(sgn: Z) -> Self {
        Self(sgn.clone(), sgn)
    }
}

impl<X: Signal<Sample = smp::Mono>, Y: Signal<Sample = smp::Mono>> Signal for Stereo<X, Y> {
    type Sample = smp::Stereo;

    fn get(&self) -> Self::Sample {
        smp::Stereo(self.0.get().0, self.1.get().0)
    }
}

impl<X: SignalMut<Sample = smp::Mono>, Y: SignalMut<Sample = smp::Mono>> SignalMut
    for Stereo<X, Y>
{
    fn advance(&mut self) {
        self.0.advance();
        self.1.advance();
    }

    fn retrigger(&mut self) {
        self.0.retrigger();
        self.1.retrigger();
    }
}

impl<X: Done<Sample = smp::Mono>, Y: Done<Sample = smp::Mono>> Done for Stereo<X, Y> {
    fn is_done(&self) -> bool {
        self.0.is_done() && self.1.is_done()
    }
}

impl<X: Stop<Sample = smp::Mono>, Y: Stop<Sample = smp::Mono>> Stop for Stereo<X, Y> {
    fn stop(&mut self) {
        self.0.stop();
        self.1.stop();
    }
}

impl<X: Panic<Sample = smp::Mono>, Y: Panic<Sample = smp::Mono>> Panic for Stereo<X, Y> {
    fn panic(&mut self) {
        self.0.panic();
        self.1.panic();
    }
}

/// Adds two signals together.
///
/// If you want to mix more signals together (e.g. an entire song), it might be easier to manually
/// add the samples instead.
pub struct Mix<X: Signal, Y: Signal<Sample = X::Sample>>(pub X, pub Y);

impl<X: Signal, Y: Signal<Sample = X::Sample>> Mix<X, Y> {
    /// Initializes a new [`Mix`].
    pub const fn new(x: X, y: Y) -> Self {
        Self(x, y)
    }
}

impl<X: Signal, Y: Signal<Sample = X::Sample>> Signal for Mix<X, Y> {
    type Sample = X::Sample;

    fn get(&self) -> Self::Sample {
        self.0.get() + self.1.get()
    }
}

impl<X: SignalMut, Y: SignalMut<Sample = X::Sample>> SignalMut for Mix<X, Y> {
    fn advance(&mut self) {
        self.0.advance();
        self.1.advance();
    }

    fn retrigger(&mut self) {
        self.0.retrigger();
        self.1.retrigger();
    }
}

impl<X: Done, Y: Done<Sample = X::Sample>> Done for Mix<X, Y> {
    fn is_done(&self) -> bool {
        self.0.is_done() && self.1.is_done()
    }
}

impl<X: Stop, Y: Stop<Sample = X::Sample>> Stop for Mix<X, Y> {
    fn stop(&mut self) {
        self.0.stop();
        self.1.stop();
    }
}

impl<X: Panic, Y: Panic<Sample = X::Sample>> Panic for Mix<X, Y> {
    fn panic(&mut self) {
        self.0.panic();
        self.1.panic();
    }
}

/// The function that duplicates a [`smp::Mono`] sample in both channels.
#[derive(Clone, Copy, Debug, Default)]
pub struct Dup;

impl Map for Dup {
    type Input = smp::Mono;
    type Output = smp::Stereo;

    fn eval(&self, x: smp::Mono) -> smp::Stereo {
        x.duplicate()
    }
}

/// Duplicates a [`smp::Mono`] signal to create a [`Stereo`] signal.
pub type Duplicate<S> = eff::MapSgn<S, Dup>;

impl<S: Signal<Sample = smp::Mono>> Duplicate<S> {
    /// Duplicates a [`smp::Mono`] signal in both channels.
    pub const fn new_dup(sgn: S) -> Self {
        Self::new(sgn, Dup)
    }
}

/// A reference to another signal.
///
/// This can be used as a simple and efficient way to "clone" a signal, in order to use its output
/// across various other signals.
///
/// Note that due to Rust's aliasing rules, once a [`Ref<S>`] is created, the original signal can't
/// be modified until it's dropped. For this same reason, [`Ref<S>`] does not implement
/// [`SignalMut`], even if `S` does. The only way to "advance" a signal is to re-build it for each
/// sample. If this restriction is unacceptable, [`Cell`] can be used to provide interior
/// mutability.
///
/// ## Examples
///
/// In this example, we apply two different distortion effects to a single sine wave, and play them
/// in both ears. The left ear should be noticeably louder.
///
/// ```
/// # use pointillism::prelude::*;
/// // The original signal.
/// let mut signal = gen::Loop::new(crv::Sin, unt::Freq::from_raw_default(unt::RawFreq::A3));
///
/// pointillism::create(
///     "examples/routing.wav",
///     unt::Time::from_sec_default(3.0), unt::SampleRate::default(),
///     |_| {
///         // Thanks to `Ref`, we're able to re-use our signal.
///         // However, we need to re-define it for every sample.
///         let sgn1 = eff::PwMapSgn::inf_clip(rtn::Ref::new(&signal));
///         let sgn2 = eff::PwMapSgn::cubic(rtn::Ref::new(&signal));
///         let stereo = rtn::Stereo::new(sgn1, sgn2);
///
///         // However, we must manually advance them.
///         let res = stereo.get();
///         signal.advance();
///         res
///     }
/// )
/// .unwrap();
/// ```
///
/// The next example rewrites our previous code in terms of [`Cell`]. If our wrappers had non-zero
/// cost, this would most likely give a noticeable improvement in performance.
///
/// ```
/// # use pointillism::prelude::*;
/// // The original signal.
/// let signal = gen::Loop::new(crv::Sin, unt::Freq::from_raw_default(unt::RawFreq::A3));
/// let cell = rtn::Cell::new(signal);
///
/// // Thanks to `Ref`, we're able to re-use our signal.
/// // And thanks to `Cell`, we only need to define our mix once.
/// let sgn1 = eff::PwMapSgn::inf_clip(rtn::Ref::new(&cell));
/// let sgn2 = eff::PwMapSgn::cubic(rtn::Ref::new(&cell));
/// let stereo = rtn::Stereo::new(sgn1, sgn2);
///
/// pointillism::create(
///     "examples/routing_cell.wav",
///     unt::Time::from_sec_default(3.0), unt::SampleRate::default(),
///     |_| {
///         // The `advance` method here uses interior mutability.
///         let res = stereo.get();
///         cell.advance();
///         res
///     }
/// )
/// .expect("IO error!");
/// ```
pub struct Ref<'a, S: Signal>(pub &'a S);

impl<'a, S: Signal> Ref<'a, S> {
    /// Initializes a new [`Ref`].
    pub const fn new(sgn: &'a S) -> Self {
        Self(sgn)
    }
}

impl<'a, S: Signal> Signal for Ref<'a, S> {
    type Sample = S::Sample;

    fn get(&self) -> S::Sample {
        self.0.get()
    }
}

impl<'a, S: Done> Done for Ref<'a, S> {
    fn is_done(&self) -> bool {
        self.0.is_done()
    }
}

/// A wrapper around [`UnsafeCell`], which allows us to reference a signal that might be modified.
///
/// For safety reasons, we don't allow access to the pointer `&mut S`. Instead, the signal must be
/// read using the [`Signal`] methods, and modified using [`Cell::modify`].
pub struct Cell<S: Signal>(UnsafeCell<S>);

impl<S: Signal> Cell<S> {
    /// Initializes a new [`Cell`].
    pub const fn new(sgn: S) -> Self {
        Self(UnsafeCell::new(sgn))
    }

    /// Modify the signal through the function.
    ///
    /// Although this should be safe, note that it's subject to the same pitfalls as the nightly
    /// [`Cell::update`](https://github.com/rust-lang/rust/issues/50186#issuecomment-593233850).
    /// Particularly, you should avoid nested calls.
    pub fn modify<F: FnMut(&mut S)>(&self, mut func: F) {
        // Safety: within this scope, this is an exclusive reference.
        func(unsafe { &mut *self.0.get() });
    }
}

impl<S: SignalMut> Cell<S> {
    /// Advances the signal. Note that this only requires `&self`.
    pub fn advance(&self) {
        self.modify(SignalMut::advance);
    }

    /// Gets the next sample and advances the state of the signal. Note that this only requires
    /// `&self`.
    pub fn next(&self) -> S::Sample {
        self.advance();
        self._get()
    }
}

/// If we own `&mut Cell<S>`, it must be the only reference.
impl<S: Signal> AsMut<S> for Cell<S> {
    fn as_mut(&mut self) -> &mut S {
        self.0.get_mut()
    }
}

impl<S: Signal> Signal for Cell<S> {
    type Sample = S::Sample;

    fn get(&self) -> Self::Sample {
        // Safety: within this scope, this is an exclusive reference.
        (unsafe { &*self.0.get() })._get()
    }
}

impl<S: Done> Done for Cell<S> {
    fn is_done(&self) -> bool {
        // Safety: within this scope, this is an exclusive reference.
        (unsafe { &*self.0.get() }).is_done()
    }
}