/
ToneBufferSource.ts
253 lines (222 loc) · 6.67 KB
/
ToneBufferSource.ts
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
import { connect } from "../../core/context/ToneAudioNode";
import { Param } from "../../core/context/Param";
import { ToneAudioBuffer } from "../../core/context/ToneAudioBuffer";
import { GainFactor, Positive, Seconds, Time } from "../../core/type/Units";
import { defaultArg, optionsFromArguments } from "../../core/util/Defaults";
import { noOp } from "../../core/util/Interface";
import { isDefined } from "../../core/util/TypeCheck";
import { assert } from "../../core/util/Debug";
import { OneShotSource, OneShotSourceCurve, OneShotSourceOptions } from "../OneShotSource";
import { EQ, GTE, LT } from "../../core/util/Math";
export type ToneBufferSourceCurve = OneShotSourceCurve;
export interface ToneBufferSourceOptions extends OneShotSourceOptions {
url: string | AudioBuffer | ToneAudioBuffer;
curve: ToneBufferSourceCurve;
playbackRate: Positive;
fadeIn: Time;
fadeOut: Time;
loopStart: Time;
loopEnd: Time;
loop: boolean;
onload: () => void;
onerror: (error: Error) => void;
}
/**
* Wrapper around the native BufferSourceNode.
* @category Source
*/
export class ToneBufferSource extends OneShotSource<ToneBufferSourceOptions> {
readonly name: string = "ToneBufferSource";
/**
* The oscillator
*/
private _source = this.context.createBufferSource();
protected _internalChannels = [this._source];
/**
* The frequency of the oscillator
*/
readonly playbackRate: Param<"positive">;
/**
* The private instance of the buffer object
*/
private _buffer: ToneAudioBuffer;
/**
* indicators if the source has started/stopped
*/
private _sourceStarted = false;
private _sourceStopped = false;
/**
* @param url The buffer to play or url to load
* @param onload The callback to invoke when the buffer is done playing.
*/
constructor(url?: ToneAudioBuffer | AudioBuffer | string, onload?: () => void);
constructor(options?: Partial<ToneBufferSourceOptions>);
constructor() {
super(optionsFromArguments(ToneBufferSource.getDefaults(), arguments, ["url", "onload"]));
const options = optionsFromArguments(ToneBufferSource.getDefaults(), arguments, ["url", "onload"]);
connect(this._source, this._gainNode);
this._source.onended = () => this._stopSource();
/**
* The playbackRate of the buffer
*/
this.playbackRate = new Param({
context: this.context,
param: this._source.playbackRate,
units: "positive",
value: options.playbackRate,
});
// set some values initially
this.loop = options.loop;
this.loopStart = options.loopStart;
this.loopEnd = options.loopEnd;
this._buffer = new ToneAudioBuffer(options.url, options.onload, options.onerror);
this._internalChannels.push(this._source);
}
static getDefaults(): ToneBufferSourceOptions {
return Object.assign(OneShotSource.getDefaults(), {
url: new ToneAudioBuffer(),
loop: false,
loopEnd: 0,
loopStart: 0,
onload: noOp,
onerror: noOp,
playbackRate: 1,
});
}
/**
* The fadeIn time of the amplitude envelope.
*/
get fadeIn(): Time {
return this._fadeIn;
}
set fadeIn(t: Time) {
this._fadeIn = t;
}
/**
* The fadeOut time of the amplitude envelope.
*/
get fadeOut(): Time {
return this._fadeOut;
}
set fadeOut(t: Time) {
this._fadeOut = t;
}
/**
* The curve applied to the fades, either "linear" or "exponential"
*/
get curve(): ToneBufferSourceCurve {
return this._curve;
}
set curve(t) {
this._curve = t;
}
/**
* Start the buffer
* @param time When the player should start.
* @param offset The offset from the beginning of the sample to start at.
* @param duration How long the sample should play. If no duration is given, it will default to the full length of the sample (minus any offset)
* @param gain The gain to play the buffer back at.
*/
start(time?: Time, offset?: Time, duration?: Time, gain: GainFactor = 1): this {
assert(this.buffer.loaded, "buffer is either not set or not loaded");
const computedTime = this.toSeconds(time);
// apply the gain envelope
this._startGain(computedTime, gain);
// if it's a loop the default offset is the loopstart point
if (this.loop) {
offset = defaultArg(offset, this.loopStart);
} else {
// otherwise the default offset is 0
offset = defaultArg(offset, 0);
}
// make sure the offset is not less than 0
let computedOffset = Math.max(this.toSeconds(offset), 0);
// start the buffer source
if (this.loop) {
// modify the offset if it's greater than the loop time
const loopEnd = this.toSeconds(this.loopEnd) || this.buffer.duration;
const loopStart = this.toSeconds(this.loopStart);
const loopDuration = loopEnd - loopStart;
// move the offset back
if (GTE(computedOffset, loopEnd)) {
computedOffset = ((computedOffset - loopStart) % loopDuration) + loopStart;
}
// when the offset is very close to the duration, set it to 0
if (EQ(computedOffset, this.buffer.duration)) {
computedOffset = 0;
}
}
// this.buffer.loaded would have return false if the AudioBuffer was undefined
this._source.buffer = this.buffer.get() as AudioBuffer;
this._source.loopEnd = this.toSeconds(this.loopEnd) || this.buffer.duration;
if (LT(computedOffset, this.buffer.duration)) {
this._sourceStarted = true;
this._source.start(computedTime, computedOffset);
}
// if a duration is given, schedule a stop
if (isDefined(duration)) {
let computedDur = this.toSeconds(duration);
// make sure it's never negative
computedDur = Math.max(computedDur, 0);
this.stop(computedTime + computedDur);
}
return this;
}
protected _stopSource(time?: Seconds): void {
if (!this._sourceStopped && this._sourceStarted) {
this._sourceStopped = true;
this._source.stop(this.toSeconds(time));
this._onended();
}
}
/**
* If loop is true, the loop will start at this position.
*/
get loopStart(): Time {
return this._source.loopStart;
}
set loopStart(loopStart: Time) {
this._source.loopStart = this.toSeconds(loopStart);
}
/**
* If loop is true, the loop will end at this position.
*/
get loopEnd(): Time {
return this._source.loopEnd;
}
set loopEnd(loopEnd: Time) {
this._source.loopEnd = this.toSeconds(loopEnd);
}
/**
* The audio buffer belonging to the player.
*/
get buffer(): ToneAudioBuffer {
return this._buffer;
}
set buffer(buffer: ToneAudioBuffer) {
this._buffer.set(buffer);
}
/**
* If the buffer should loop once it's over.
*/
get loop(): boolean {
return this._source.loop;
}
set loop(loop: boolean) {
this._source.loop = loop;
if (this._sourceStarted) {
this.cancelStop();
}
}
/**
* Clean up.
*/
dispose(): this {
super.dispose();
this._source.onended = null;
this._source.disconnect();
this._buffer.dispose();
this.playbackRate.dispose();
return this;
}
}