-
-
Notifications
You must be signed in to change notification settings - Fork 289
/
Playout.js
164 lines (137 loc) · 4.16 KB
/
Playout.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
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
import { FADEIN, FADEOUT, createFadeIn, createFadeOut } from "fade-maker";
function noEffects(node1, node2) {
node1.connect(node2);
}
export default class {
constructor(ac, buffer, masterGain = ac.createGain()) {
this.ac = ac;
this.gain = 1;
this.effectsGraph = noEffects;
this.masterEffectsGraph = noEffects;
this.buffer = buffer;
this.masterGain = masterGain;
this.destination = this.ac.destination;
}
applyFade(type, start, duration, shape = "logarithmic") {
if (type === FADEIN) {
createFadeIn(this.fadeGain.gain, shape, start, duration);
} else if (type === FADEOUT) {
createFadeOut(this.fadeGain.gain, shape, start, duration);
} else {
throw new Error("Unsupported fade type");
}
}
applyFadeIn(start, duration, shape = "logarithmic") {
this.applyFade(FADEIN, start, duration, shape);
}
applyFadeOut(start, duration, shape = "logarithmic") {
this.applyFade(FADEOUT, start, duration, shape);
}
isPlaying() {
return this.source !== undefined;
}
getDuration() {
return this.buffer.duration;
}
setAudioContext(ac) {
this.ac = ac;
this.destination = this.ac.destination;
}
createStereoPanner() {
if (this.ac.createStereoPanner) {
return this.ac.createStereoPanner();
}
return this.ac.createPanner();
}
setUpSource() {
this.source = this.ac.createBufferSource();
this.source.buffer = this.buffer;
let cleanupEffects;
let cleanupMasterEffects;
const sourcePromise = new Promise((resolve) => {
// keep track of the buffer state.
this.source.onended = () => {
this.source.disconnect();
this.fadeGain.disconnect();
this.volumeGain.disconnect();
this.shouldPlayGain.disconnect();
this.panner.disconnect();
// this.masterGain.disconnect();
if (cleanupEffects) cleanupEffects();
if (cleanupMasterEffects) cleanupMasterEffects();
this.source = undefined;
this.fadeGain = undefined;
this.volumeGain = undefined;
this.shouldPlayGain = undefined;
this.panner = undefined;
resolve();
};
});
this.fadeGain = this.ac.createGain();
// used for track volume slider
this.volumeGain = this.ac.createGain();
// used for solo/mute
this.shouldPlayGain = this.ac.createGain();
this.panner = this.createStereoPanner();
this.source.connect(this.fadeGain);
this.fadeGain.connect(this.volumeGain);
this.volumeGain.connect(this.shouldPlayGain);
this.shouldPlayGain.connect(this.panner);
cleanupEffects = this.effectsGraph(
this.panner,
this.masterGain,
this.ac instanceof (window.OfflineAudioContext || window.webkitOfflineAudioContext)
);
cleanupMasterEffects = this.masterEffectsGraph(
this.masterGain,
this.destination,
this.ac instanceof (window.OfflineAudioContext || window.webkitOfflineAudioContext)
);
return sourcePromise;
}
setVolumeGainLevel(level) {
if (this.volumeGain) {
this.volumeGain.gain.value = level;
}
}
setShouldPlay(bool) {
if (this.shouldPlayGain) {
this.shouldPlayGain.gain.value = bool ? 1 : 0;
}
}
setMasterGainLevel(level) {
if (this.masterGain) {
this.masterGain.gain.value = level;
}
}
setStereoPanValue(pan = 0) {
if (this.panner) {
if (this.panner.pan !== undefined) {
this.panner.pan.value = pan;
} else {
this.panner.panningModel = "equalpower";
this.panner.setPosition(pan, 0, 1 - Math.abs(pan));
}
}
}
setEffects(effectsGraph = noEffects) {
this.effectsGraph = effectsGraph;
}
setMasterEffects(effectsGraph = noEffects) {
this.masterEffectsGraph = effectsGraph;
}
/*
source.start is picky when passing the end time.
If rounding error causes a number to make the source think
it is playing slightly more samples than it has it won't play at all.
Unfortunately it doesn't seem to work if you just give it a start time.
*/
play(when, start, duration) {
this.source.start(when, start, duration);
}
stop(when = 0) {
if (this.source) {
this.source.stop(when);
}
}
}