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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
//! Special effects.
use core::cmp::Ordering;
use core::iter::{self, Peekable, FromIterator};
use arrayvec::ArrayVec;

use lazy_static::lazy_static;

pub const MIXER_REG: u8 = 7;
pub const VOL_A_REG: u8 = 8;
pub const VOL_B_REG: u8 = 9;
pub const VOL_C_REG: u8 = 10;
pub const ENV_PER_FINE_REG: u8 = 11;
pub const ENV_PER_COARSE_REG: u8 = 12;
pub const ENV_REG: u8 = 13;

/// The timer type, used by all of the special effects.
#[derive(Debug, Default, Clone, Copy)]
struct Timer {
    current: f32,
    step: f32
}

/// The `Sync Buzzer` effect writes periodically into the AY/YM register 13 a set up shape value,
/// which resets the chipset's internal volume envelope control timer.
#[derive(Debug, Default, Clone, Copy)]
pub struct SyncBuzzer {
    timer: Timer,
    shape: u8,
    active: bool
}

/// The `SID voice` effect modulates the channel's volume alternating between 0 and some set up value.
#[derive(Debug, Default, Clone, Copy)]
pub struct SidVoice {
    timer: Timer,
    vol: u8,
    cur: bool,
    active: bool
}

/// The `Sinus SID` effect modulates the channel's volume, by applying the scaled sinusoid shape with
/// the period of 8 samples, with the set up amplitude.
#[derive(Debug, Default, Clone, Copy)]
pub struct SinusSid {
    timer: Timer,
    amplitude: u8,
    phase: u8,
    active: bool
}

/// The `DIGI-DRUM` effect modulates the channel's volume level, by applying to it 4-bit sample values.
#[derive(Debug, Default, Clone, Copy)]
pub struct DigiDrum {
    timer: Timer,
    cur: usize,
    end: usize
}

#[derive(Debug)]
struct TimerIter<'a> {
    timer: &'a mut Timer,
    limit: f32
}

pub(super) struct Mixer<I: Iterator> {
    iters: ArrayVec<Peekable<I>, 4>
}

const SINUS_SID_PERIOD: usize = 8;
const SINUS_SID_MASK: usize = SINUS_SID_PERIOD - 1;

lazy_static! {
    static ref SINUS_SID: [u8;SINUS_SID_PERIOD] = {
        use core::f32::consts::PI;
        let mut sinus_sid = [0u8;SINUS_SID_PERIOD];
        for (n, p) in sinus_sid.iter_mut().enumerate() {
            let x = 2.0 * PI * n as f32 / SINUS_SID_PERIOD as f32;
            *p = ((x.cos() * 0.5 + 0.5) * 255.0).round() as u8;
        }
        sinus_sid
    };
}

#[inline(always)]
fn sinus_sid(phase: usize, vol: u16) -> u8 {
    ((SINUS_SID[phase & SINUS_SID_MASK] as u16 * vol + 127) / 255) as u8
}

impl<'a> TimerIter<'a> {
    #[inline]
    fn new(timer: &'a mut Timer, limit: f32) -> TimerIter<'a> {
        TimerIter { timer, limit }
    }

    #[inline]
    fn force_end(&mut self) {
        self.timer.current = self.limit;
    }
}

impl<'a> Iterator for TimerIter<'a> {
    type Item = f32;

    fn next(&mut self) -> Option<f32> {
        let Timer { current, step } = *self.timer;
        if current < self.limit {
            self.timer.current = current + step;
            return Some(current)
        }
        self.timer.current -= self.limit;
        None
    }
}

impl Timer {
    fn reset(&mut self) {
        self.current = 0.0;
    }

    fn set_step(&mut self, step: f32) {
        assert!(step > f32::EPSILON);
        self.step = step;
    }

    /// Returns the number of ticks
    fn fast_forward(&mut self, limit: f32) -> f32 {
        let step = self.step;
        if step >= f32::EPSILON {
            let rest = limit - self.current;
            self.current = step - (rest % step);
            return (rest / step).trunc()
        }
        0.0
    }
}

impl SyncBuzzer {
    pub fn stop(&mut self) {
        self.active = false;
    }

    pub fn start(&mut self, shape: u8, step: f32) {
        self.timer.set_step(step);
        self.shape = shape & 0x0f;
        self.active = true;
    }

    pub fn iter_frame<'a>(&'a mut self, limit: f32) -> Option<impl Iterator<Item=(f32, u8, u8)> + 'a> {
        if self.active {
            let shape = self.shape;
            return Some(
                TimerIter::new(&mut self.timer, limit)
                           .map(move |ts| (ts, ENV_REG, shape) )
            )
        }
        None
    }
}

impl SidVoice {
    pub fn is_active(&self) -> bool {
        self.active
    }

    pub fn stop(&mut self) {
        self.active = false;
    }

    pub fn reset(&mut self) {
        self.timer.reset();
        self.cur = false;
    }

    pub fn start(&mut self, vol: u8, step: f32) {
        self.timer.set_step(step);
        self.vol = vol;
        self.active = true;
    }

    pub fn iter_frame<'a>(
            &'a mut self,
            limit: f32,
            reg: u8
        ) -> Option<impl Iterator<Item=(f32, u8, u8)> + 'a>
    {
        if self.active {
            let vol = self.vol;
            let cur = &mut self.cur;
            return Some(
                TimerIter::new(&mut self.timer, limit).map(move |ts| {
                    let res = *cur;
                    *cur = !res;
                    let v = if res { 0 } else { vol };
                    (ts, reg, v)
                })
            )
        }
        if self.timer.fast_forward(limit) as u32 & 1 == 1 {
            self.cur = !self.cur;
        }
        None
    }
}

impl DigiDrum {
    pub fn is_active(&self) -> bool {
        self.cur < self.end
    }

    pub fn stop(&mut self) {
        self.end = 0;
    }

    pub fn start(&mut self, start: usize, end: usize, step: f32) {
        self.timer.reset();
        self.timer.set_step(step);
        self.cur = start;
        self.end = end;
    }

    pub fn iter_frame<'a, 'b: 'a>(
            &'a mut self,
            limit: f32,
            reg: u8,
            dd_samples: &'b [u8],
            end_vol: u8
        ) -> Option<impl Iterator<Item=(f32, u8, u8)> + 'a>
    {
        let end = self.end;
        let cur = &mut self.cur;
        if *cur < end {
            let mut samples = dd_samples[*cur..end].iter();
            let mut timer = TimerIter::new(&mut self.timer, limit);
            return Some(iter::from_fn(move || {
                match timer.next() {
                    Some(ts) => {
                        match samples.next() {
                            Some(vol) => Some((ts, reg, *vol)),
                            None => {
                                timer.force_end();
                                Some((ts, reg, end_vol))
                            }
                        }
                    }
                    None => {
                        *cur = end - samples.len();
                        None
                    }
                }
            }))
        }
        None
    }
}

impl SinusSid {
    pub fn is_active(&self) -> bool {
        self.active
    }

    pub fn stop(&mut self) {
        self.active = false;
    }

    pub fn start(&mut self, amplitude: u8, step: f32) {
        self.timer.set_step(step);
        self.amplitude = amplitude;
        self.active = true;
    }

    pub fn iter_frame<'a>(
            &'a mut self,
            limit: f32,
            reg: u8,
        ) -> Option<impl Iterator<Item=(f32, u8, u8)> + 'a>
    {
        if self.active {
            let amplitude = self.amplitude as u16;
            let phase = &mut self.phase;
            return Some(
                TimerIter::new(&mut self.timer, limit).map(move |ts| {
                    let ph = *phase;
                    let v = sinus_sid(ph as usize, amplitude);
                    *phase = (ph + 1) & SINUS_SID_MASK as u8;
                    (ts, reg, v as u8)
                })
            )
        }
        None
    }

}

impl<I: Iterator> Mixer<I> {
    pub(super) fn push(&mut self, iter: I) {
        self.iters.push(iter.peekable())
    }
}

impl<I> Iterator for Mixer<I>
    where I: Iterator<Item=(f32, u8, u8)>,
{
    type Item = (f32, u8, u8);

    fn next(&mut self) -> Option<Self::Item> {
        if let Some((pos, ..)) = self.iters.iter_mut().map(Peekable::peek).enumerate()
                                     .min_by(|(_, a), (_, b)|
            match (a, b) {
                (Some((ta, ..)), Some((tb, ..))) => ta.partial_cmp(tb).unwrap(),
                (Some(..), None) => Ordering::Less,
                (None, Some(..)) => Ordering::Greater,
                (None, None) => Ordering::Equal
            })
        {
            self.iters[pos].next()
        }
        else {
            None
        }
    }
}

pub(super) fn iter_select<'a, A: Iterator<Item=(f32, u8, u8)>,
                       B: Iterator<Item=(f32, u8, u8)>,
                       C: Iterator<Item=(f32, u8, u8)>>(
        (it_a, it_b, it_c): &'a mut (Option<A>, Option<B>, Option<C>)
    ) -> Option<&'a mut dyn Iterator<Item=(f32, u8, u8)>>
{
    if let Some(it) = it_a.as_mut() {
        Some(it)
    }
    else if let Some(it) = it_b.as_mut() {
        Some(it)
    }
    else if let Some(it) = it_c.as_mut() {
        Some(it)
    }
    else {
        None
    }
}

impl<I> FromIterator<I> for Mixer<I>
    where I: Iterator<Item=(f32, u8, u8)>
{
    fn from_iter<T: IntoIterator<Item = I>>(
            iter: T
        ) -> Self
    {
        let iters = iter.into_iter().map(Iterator::peekable).collect();
        Mixer { iters }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn sinus_sid_works() {
        assert_eq!(SINUS_SID_PERIOD, 8);
        use core::f32::consts::PI;
        for vol in 0..16 {
            println!("vol: {}", vol);
            for n in 0..SINUS_SID_PERIOD {
                let x = 2.0 * PI * n as f32 / SINUS_SID_PERIOD as f32;
                let y = x.cos() * 0.5 + 0.5;
                let v0 = (y * vol as f32).round() as u8;
                let v1 = sinus_sid(n as usize, vol);
                println!("{:02}: {:02} {:02} {:03} {:.8} {:.8}", n, v0, v1, SINUS_SID[n as usize], y, x);
                assert_eq!(v0, v1);
            }
        }
    }
}