-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBufferPlayer.js
189 lines (160 loc) · 4.54 KB
/
BufferPlayer.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
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
/**
* Copyright (C) 2022, BBC R&D
* This source code is licensed under the GPL license found in the LICENSE file in this repository.
*/
import bbcat from '@bbc/audio-orchestration-bbcat-js';
import Player from './Player';
class BufferPlayer extends Player {
constructor(audioContext, mediaUrl) {
super(audioContext);
/** @private */
this.mediaUrl = mediaUrl;
/** @private */
this.buffer = null;
/** @private */
this.source = null;
/** @private */
this.when = 0;
/** @private */
this.offset = 0;
/** @private */
this.preparePromise = null;
}
/**
* Prepare the player by downloading and decoding the audio if necessary.
*
* @returns {Promise<AudioBuffer>}
*/
prepare() {
// If we have a valid buffer, immediately resolve to that.
if (this.buffer !== null) {
return Promise.resolve(this.buffer);
}
// If we are already waiting for the download to finish, return the pending promise.
if (this.preparePromise !== null) {
return this.preparePromise;
}
// Otherwise, initiate a new loader and return a promise to the decoded buffer.
const loader = new bbcat.core.AudioLoader(this.audioContext);
this.preparePromise = loader.load([
this.mediaUrl,
]).then((decodedBuffers) => {
[this.buffer] = decodedBuffers;
return this.buffer;
}).then((buffer) => {
this._outputs = [...Array(this.buffer.numberOfChannels).keys()]
.map(() => this.audioContext.createGain());
return buffer;
}).then((buffer) => {
this.state = 'ready';
return buffer;
});
return this.preparePromise;
}
/**
* @param {number} when
* @param {number} offset
*
* @returns {Promise<BufferPlayer>}
*/
play(when = this.audioContext.currentTime, offset = this.offset) {
return this.prepare().then((buffer) => {
// console.debug(`bufferPlayer.play() when: ${when} offset: ${offset}`);
// check that offset is valid
if (offset < 0 || offset > this.buffer.duration) {
throw new Error('offset must be >= 0 and < buffer.duration.');
}
// if we already have a playing source, stop it.
if (this.source !== null && this.state === 'playing') {
try {
this.source.stop();
this.state = 'ready';
} catch (e) {
console.warn('bufferPlayer.pause():', e);
}
}
// save new start time and offset.
this.when = when;
this.offset = offset;
this.source = this.audioContext.createBufferSource();
this.source.buffer = buffer;
this.connectOutputs();
this.source.onended = (e) => {
// if it hasn't been replaced already, update the player state.
// set offset to current position to handle pause/resume.
if (e.target === this.source) {
this.offset = this.currentTime;
this.state = 'ready';
}
};
this.source.start(when, offset);
this.state = 'playing';
return this;
});
}
connectOutputs() {
const splitter = this.audioContext.createChannelSplitter(this._outputs.length);
this.source.connect(splitter);
this._outputs.forEach((output, i) => {
splitter.connect(output, i);
});
}
/**
* Pauses the player.
*
* @returns {Promise} resolving when the stop has been scheduled.
*/
pause() {
if (this.source !== null && this.state === 'playing') {
try {
this.source.stop();
this.state = 'ready';
} catch (e) {
console.warn('bufferPlayer.pause():', e);
}
}
return Promise.resolve();
}
/**
* Get the current content time, the progress of the player.
*
* @returns {number}
*/
get currentTime() {
if (this.source === null || this.buffer === null) {
return 0;
}
if (this.state === 'ready') {
return this.offset;
}
const currentTime = this.audioContext.currentTime - (this.when - this.offset);
return Math.max(this.offset, Math.min(currentTime, this.buffer.duration));
}
/**
* Get the current playback rate (0 = paused, 1 = playing)
*
* @returns {number}
*/
get playbackRate() {
if (this.state === 'playing') {
return 1;
}
return 0;
}
/**
* get the default buffering delay for this type of player.
*
* @returns {number}
*/
/* eslint-disable-next-line class-methods-use-this */
get defaultBufferingDelay() {
return 0;
}
get duration() {
if (this.buffer === null) {
return 0;
}
return this.buffer.duration;
}
}
export default BufferPlayer;