-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduled-source.js
73 lines (63 loc) · 1.87 KB
/
scheduled-source.js
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
import React from "react";
import RAudioNode from "./audio-node.js";
/**
* Any RAudioNode that can be scheduled to start/end is a RScheduledSource
*
* @class RScheduledSource (name)
*/
export default class RScheduledSource extends RAudioNode {
constructor(props) {
super(props);
this.readyToPlay = false;
this.playbackScheduled = false;
this.onEnded = this.onEnded.bind(this);
this.schedule = this.schedule.bind(this);
}
onEnded() {
this.playbackScheduled = false;
// Web Audio will remove the node from the graph after stopping, so reinstantiate it
this.instantiateNode();
this.connectToAllDestinations(this.props.destination, this.node);
}
schedule() {
const shouldScheduleStart =
typeof this.props.start === "number" &&
this.readyToPlay &&
!this.playbackScheduled &&
(typeof this.props.stop !== "number" ||
this.props.start < this.props.stop);
const shouldScheduleStop = typeof this.props.stop === "number";
if (shouldScheduleStart) {
this.node.start(
this.props.start || 0,
this.props.offset || 0,
this.props.duration
);
this.playbackScheduled = true;
}
if (shouldScheduleStop) {
this.node.stop(this.props.stop);
}
}
/**
Overriding this method enables sources to specify special conditions when playback should be rescheduled.
e.g. BufferSource should be rescheduled if a new buffer is provided
**/
shouldStartWithPropsChange() {
return false;
}
componentDidMount() {
super.componentDidMount();
this.schedule();
}
componentDidUpdate(prevProps, prevState) {
super.componentDidUpdate(prevProps, prevState);
if (
prevProps.start !== this.props.start ||
prevProps.stop !== this.props.stop ||
this.shouldStartWithPropsChange(prevProps, this.props)
) {
this.schedule();
}
}
}