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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
use block::Block;
use block::Tick;
use block::FRAMES_PER_BLOCK_USIZE;
use node::BlockInfo;

#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub enum ParamType {
    Frequency,
    Detune,
    Gain,
    Q,
    Pan,
    PlaybackRate,
    Position(ParamDir),
    Forward(ParamDir),
    Up(ParamDir),
    Orientation(ParamDir),
    Offset,
}

#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub enum ParamDir {
    X,
    Y,
    Z,
}

/// An AudioParam.
///
/// https://webaudio.github.io/web-audio-api/#AudioParam
pub struct Param {
    val: f32,
    kind: ParamRate,
    events: Vec<AutomationEvent>,
    current_event: usize,
    event_start_time: Tick,
    event_start_value: f32,
    /// Cache of inputs from connect()ed nodes
    blocks: Vec<Block>,
    /// The value of all connect()ed inputs mixed together, for this frame
    block_mix_val: f32,
    /// If true, `blocks` has been summed together into a single block
    summed: bool,
    dirty: bool,
}

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum ParamRate {
    /// Value is held for entire block
    KRate,
    /// Value is updated each frame
    ARate,
}

impl Param {
    pub fn new(val: f32) -> Self {
        Param {
            val,
            kind: ParamRate::ARate,
            events: vec![],
            current_event: 0,
            event_start_time: Tick(0),
            event_start_value: val,
            blocks: Vec::new(),
            block_mix_val: 0.,
            summed: false,
            dirty: false,
        }
    }

    pub fn new_krate(val: f32) -> Self {
        Param {
            val,
            kind: ParamRate::KRate,
            events: vec![],
            current_event: 0,
            event_start_time: Tick(0),
            event_start_value: val,
            blocks: Vec::new(),
            block_mix_val: 0.,
            summed: false,
            dirty: false,
        }
    }

    /// Update the value of this param to the next
    ///
    /// Invariant: This should be called with monotonically increasing
    /// ticks, and Tick(0) should never be skipped.
    ///
    /// Returns true if anything changed
    pub fn update(&mut self, block: &BlockInfo, tick: Tick) -> bool {
        let mut changed = self.dirty;
        self.dirty = false;
        if tick.0 == 0 {
            self.summed = true;
            if let Some(first) = self.blocks.pop() {
                // first sum them together
                // https://webaudio.github.io/web-audio-api/#dom-audionode-connect-destinationparam-output
                let block = self
                    .blocks
                    .drain(..)
                    .fold(first, |acc, block| acc.sum(block));
                self.blocks.push(block);
            }
        } else if self.kind == ParamRate::KRate {
            return changed;
        }

        // Even if the timeline does nothing, it's still possible
        // that there were connected inputs, so we should not
        // directly return `false` after this point, instead returning
        // `changed`
        changed |= if let Some(block) = self.blocks.get(0) {
            // store to be summed with `val` later
            self.block_mix_val = block.data_chan_frame(tick.0 as usize, 0);
            true
        } else {
            false
        };

        if self.events.len() <= self.current_event {
            return changed;
        }

        let current_tick = block.absolute_tick(tick);
        let mut current_event = &self.events[self.current_event];

        // move to next event if necessary
        // XXXManishearth k-rate events may get skipped over completely by this
        // method. Firefox currently doesn't support these, however, so we can
        // handle those later
        loop {
            let mut move_next = false;
            if let Some(done_time) = current_event.done_time() {
                // If this event is done, move on
                if done_time < current_tick {
                    move_next = true;
                }
            } else if let Some(next) = self.events.get(self.current_event + 1) {
                // this event has no done time and we must run it till the next one
                // starts
                if let Some(start_time) = next.start_time() {
                    // if the next one is ready to start, move on
                    if start_time <= current_tick {
                        move_next = true;
                    }
                } else {
                    // If we have a next event with no start time and
                    // the current event has no done time, this *has* to be because
                    // the current event is SetTargetAtTime and the next is a Ramp
                    // event. In this case we skip directly to the ramp assuming
                    // the SetTarget is ready to start (or has started already)
                    if current_event.time() <= current_tick {
                        move_next = true;
                    } else {
                        // This is a SetTarget event before its start time, ignore
                        return changed;
                    }
                }
            }
            if move_next {
                self.current_event += 1;
                self.event_start_value = self.val;
                self.event_start_time = current_tick;
                if let Some(next) = self.events.get(self.current_event + 1) {
                    current_event = next;
                    // may need to move multiple times
                    continue;
                } else {
                    return changed;
                }
            }
            break;
        }

        current_event.run(
            &mut self.val,
            current_tick,
            self.event_start_time,
            self.event_start_value,
        )
    }

    pub fn value(&self) -> f32 {
        // the data from connect()ed audionodes is first mixed
        // together in update(), and then mixed with the actual param value
        // https://webaudio.github.io/web-audio-api/#dom-audionode-connect-destinationparam-output
        self.val + self.block_mix_val
    }

    pub fn set_rate(&mut self, rate: ParamRate) {
        self.kind = rate;
    }

    pub(crate) fn insert_event(&mut self, event: AutomationEvent) {
        if let AutomationEvent::SetValue(val) = event {
            self.val = val;
            self.event_start_value = val;
            self.dirty = true;
            return;
        }

        let time = event.time();

        let result = self.events.binary_search_by(|e| e.time().cmp(&time));
        // XXXManishearth this should handle overlapping events
        let idx = match result {
            Ok(idx) => idx,
            Err(idx) => idx,
        };

        // XXXManishearth this isn't quite correct, this
        // doesn't handle cases for when this lands inside a running
        // event
        if let Some(is_hold) = event.cancel_event() {
            self.events.truncate(idx);
            if !is_hold {
                // If we cancelled the current event, reset
                // the value to what it was before
                if self.current_event >= self.events.len() {
                    self.val = self.event_start_value;
                }
            }
            // don't actually insert the event
            return;
        }
        self.events.insert(idx, event);
        // XXXManishearth handle inserting events with a time before that
        // of the current one
    }

    pub(crate) fn add_block(&mut self, block: Block) {
        debug_assert!(block.chan_count() == 1);
        // summed only becomes true during a node's process() call,
        // but add_block is called during graph traversal before processing,
        // so if summed is true that means we've moved on to the next block
        // and should clear our inputs
        if self.summed {
            self.blocks.clear();
        }
        self.blocks.push(block)
    }

    /// Flush an entire block of values into a buffer
    ///
    /// Only for use with AudioListener.
    ///
    /// Invariant: `block` must be a FRAMES_PER_BLOCK length array filled with silence
    pub(crate) fn flush_to_block(&mut self, info: &BlockInfo, block: &mut [f32]) {
        // common case
        if self.current_event >= self.events.len() && self.blocks.is_empty() {
            if self.val != 0. {
                for tick in 0..(FRAMES_PER_BLOCK_USIZE) {
                    // ideally this can use some kind of vectorized memset()
                    block[tick] = self.val;
                }
            }
        // if the value is zero, our buffer is already zeroed
        } else {
            for tick in 0..(FRAMES_PER_BLOCK_USIZE) {
                self.update(info, Tick(tick as u64));
                block[tick] = self.val;
            }
        }
    }
}

#[derive(Clone, Copy, Eq, PartialEq, Debug)]
pub enum RampKind {
    Linear,
    Exponential,
}

#[derive(Clone, PartialEq, Debug)]
/// https://webaudio.github.io/web-audio-api/#dfn-automation-event
pub(crate) enum AutomationEvent {
    SetValue(f32),
    SetValueAtTime(f32, Tick),
    RampToValueAtTime(RampKind, f32, Tick),
    SetTargetAtTime(f32, Tick, /* time constant, units of Tick */ f64),
    SetValueCurveAtTime(
        Vec<f32>,
        /* start time */ Tick,
        /* duration */ Tick,
    ),
    CancelAndHoldAtTime(Tick),
    CancelScheduledValues(Tick),
}

#[derive(Clone, PartialEq, Debug)]
/// An AutomationEvent that uses times in s instead of Ticks
pub enum UserAutomationEvent {
    SetValue(f32),
    SetValueAtTime(f32, /* time */ f64),
    RampToValueAtTime(RampKind, f32, /* time */ f64),
    SetTargetAtTime(f32, f64, /* time constant, units of s */ f64),
    SetValueCurveAtTime(Vec<f32>, /* start time */ f64, /* duration */ f64),
    CancelAndHoldAtTime(f64),
    CancelScheduledValues(f64),
}

impl UserAutomationEvent {
    pub(crate) fn to_event(self, rate: f32) -> AutomationEvent {
        match self {
            UserAutomationEvent::SetValue(val) => AutomationEvent::SetValue(val),
            UserAutomationEvent::SetValueAtTime(val, time) => {
                AutomationEvent::SetValueAtTime(val, Tick::from_time(time, rate))
            }
            UserAutomationEvent::RampToValueAtTime(kind, val, time) => {
                AutomationEvent::RampToValueAtTime(kind, val, Tick::from_time(time, rate))
            }
            UserAutomationEvent::SetValueCurveAtTime(values, start, duration) => {
                AutomationEvent::SetValueCurveAtTime(
                    values,
                    Tick::from_time(start, rate),
                    Tick::from_time(duration, rate),
                )
            }
            UserAutomationEvent::SetTargetAtTime(val, start, tau) => {
                AutomationEvent::SetTargetAtTime(
                    val,
                    Tick::from_time(start, rate),
                    tau * rate as f64,
                )
            }
            UserAutomationEvent::CancelScheduledValues(t) => {
                AutomationEvent::CancelScheduledValues(Tick::from_time(t, rate))
            }
            UserAutomationEvent::CancelAndHoldAtTime(t) => {
                AutomationEvent::CancelAndHoldAtTime(Tick::from_time(t, rate))
            }
        }
    }
}

impl AutomationEvent {
    /// The time of the event used for ordering
    pub fn time(&self) -> Tick {
        match *self {
            AutomationEvent::SetValueAtTime(_, tick) => tick,
            AutomationEvent::SetValueCurveAtTime(_, start, _) => start,
            AutomationEvent::RampToValueAtTime(_, _, tick) => tick,
            AutomationEvent::SetTargetAtTime(_, start, _) => start,
            AutomationEvent::CancelAndHoldAtTime(t) => t,
            AutomationEvent::CancelScheduledValues(tick) => tick,
            AutomationEvent::SetValue(..) => {
                unreachable!("SetValue should never appear in the timeline")
            }
        }
    }

    pub fn done_time(&self) -> Option<Tick> {
        match *self {
            AutomationEvent::SetValueAtTime(_, tick) => Some(tick),
            AutomationEvent::RampToValueAtTime(_, _, tick) => Some(tick),
            AutomationEvent::SetValueCurveAtTime(_, start, duration) => Some(start + duration),
            AutomationEvent::SetTargetAtTime(..) => None,
            AutomationEvent::CancelAndHoldAtTime(t) => Some(t),
            AutomationEvent::CancelScheduledValues(..) | AutomationEvent::SetValue(..) => {
                unreachable!("CancelScheduledValues/SetValue should never appear in the timeline")
            }
        }
    }

    pub fn start_time(&self) -> Option<Tick> {
        match *self {
            AutomationEvent::SetValueAtTime(_, tick) => Some(tick),
            AutomationEvent::RampToValueAtTime(..) => None,
            AutomationEvent::SetValueCurveAtTime(_, start, _) => Some(start),
            AutomationEvent::SetTargetAtTime(_, start, _) => Some(start),
            AutomationEvent::CancelAndHoldAtTime(t) => Some(t),
            AutomationEvent::CancelScheduledValues(..) | AutomationEvent::SetValue(..) => {
                unreachable!("CancelScheduledValues/SetValue should never appear in the timeline")
            }
        }
    }

    /// Returns Some if it's a cancel event
    /// the boolean is if it's CancelAndHold
    pub fn cancel_event(&self) -> Option<bool> {
        match *self {
            AutomationEvent::CancelAndHoldAtTime(..) => Some(true),
            AutomationEvent::CancelScheduledValues(..) => Some(false),
            _ => None,
        }
    }

    /// Update a parameter based on this event
    ///
    /// Returns true if something changed
    pub fn run(
        &self,
        value: &mut f32,
        current_tick: Tick,
        event_start_time: Tick,
        event_start_value: f32,
    ) -> bool {
        if let Some(start_time) = self.start_time() {
            if start_time > current_tick {
                // The previous event finished and we advanced to this
                // event, but it's not started yet. Return early
                return false;
            }
        }

        match *self {
            AutomationEvent::SetValueAtTime(val, time) => {
                if current_tick == time {
                    *value = val;
                    true
                } else {
                    false
                }
            }
            AutomationEvent::RampToValueAtTime(kind, val, time) => {
                let progress =
                    (current_tick - event_start_time).0 as f32 / (time - event_start_time).0 as f32;
                match kind {
                    RampKind::Linear => {
                        *value = event_start_value + (val - event_start_value) * progress;
                    }
                    RampKind::Exponential => {
                        let ratio = val / event_start_value;
                        if event_start_value == 0. || ratio < 0. {
                            if time == current_tick {
                                *value = val;
                            } else {
                                *value = event_start_value;
                            }
                        } else {
                            *value = event_start_value * (ratio).powf(progress);
                        }
                    }
                }
                true
            }
            AutomationEvent::SetTargetAtTime(val, start, tau) => {
                let exp = -((current_tick - start) / tau);
                *value = val + (event_start_value - val) * exp.exp() as f32;
                true
            }
            AutomationEvent::SetValueCurveAtTime(ref values, start, duration) => {
                let progress = ((((current_tick.0 as f32) - (start.0 as f32)) as f32)
                    / (duration.0 as f32)) as f32;
                debug_assert!(progress >= 0.);
                let n = values.len() as f32;
                let k_float = ((n - 1.) * progress) as f32;
                let k = k_float.floor();
                if (k + 1.) < n {
                    let progress = k_float - k;
                    *value =
                        values[k as usize] * (1. - progress) + values[(k + 1.) as usize] * progress;
                } else {
                    *value = values[(n - 1.) as usize];
                }
                true
            }
            AutomationEvent::CancelAndHoldAtTime(..) => false,
            AutomationEvent::CancelScheduledValues(..) | AutomationEvent::SetValue(..) => {
                unreachable!("CancelScheduledValues/SetValue should never appear in the timeline")
            }
        }
    }
}