forked from bbc/peaks.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
external-player.html
283 lines (234 loc) · 8.34 KB
/
external-player.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Peaks.js Demo Page</title>
<style>
body {
font-family: 'Helvetica neue', Helvetica, Arial, sans-serif;
}
#titles, #waveform-container {
margin: 24px auto;
width: 1000px;
}
#zoomview-container, #overview-container {
margin: 0 0 24px 0;
line-height: 0;
-moz-box-shadow: 3px 3px 20px #919191;
-webkit-box-shadow: 3px 3px 20px #919191;
box-shadow: 3px 3px 20px #919191;
}
#zoomview-container {
height: 200px;
}
#overview-container {
height: 85px;
}
#demo-controls {
margin: 0 auto 24px auto;
width: 1000px;
display: flex;
align-items: center;
}
#demo-controls button {
background: #fff;
border: 1px solid #919191;
cursor: pointer;
}
#audio {
flex: 0 0 30%;
}
#controls {
flex: 1;
margin-left: 1em;
}
#seek-time {
width: 4em;
}
.log {
margin: 0 auto 24px auto;
width: 1000px;
}
table {
width: 100%;
}
table th {
text-align: left;
}
table th, table td {
padding: 0.5em;
}
.hide {
display: none;
}
</style>
</head>
<body>
<div id="titles">
<h1>Peaks.js</h1>
<p>
Peaks.js is a JavaScript library that allows you to display and
interaction with audio waveforms in the browser.
</p>
<p>
It was developed by <a href="https://www.bbc.co.uk/rd">BBC R&D</a>
to allow audio editors to make accurate clippings of audio content.
You can read more about the project
<a href="https://waveform.prototyping.bbc.co.uk/">here</a>.
</p>
<h2>Demo pages</h2>
<p>
The following pages demonstrate various configuration options:
</p>
<p>
<a href="index.html">Precomputed Waveform Data</a> |
<a href="webaudio.html">Web Audio API</a> |
<a href="zoomable-waveform.html">Single Zoomable Waveform</a> |
<a href="overview-waveform.html">Single Fixed Waveform</a> |
<a href="cue-events.html">Cue Events</a> |
<a href="set-source.html">Changing the Media URL</a> |
<a href="multi-channel.html">Multi-Channel Waveform</a> |
<a href="custom-markers.html">Custom Point and Segment Markers</a> |
External Audio Player
</p>
<h2>Demo: External Player</h2>
<p>
This demo shows how to use an external player for audio playback with Peaks.js.
A Tone.js player is set up in the example.
</p>
</div>
<div id="waveform-container">
<div id="zoomview-container"></div>
<div id="overview-container"></div>
</div>
<div id="demo-controls">
<div id="controls">
<button data-action="play">Play</button>
<button data-action="pause">Pause</button>
<input type="text" id="seek-time" value="0.0">
<button data-action="seek">Seek</button>
<button data-action="zoom-in">Zoom in</button>
<button data-action="zoom-out">Zoom out</button>
<input type="text" id="segment-start" value="2.0">
<input type="text" id="segment-end" value="5.0">
<button data-action="play-segment">Play segment</button>
</div>
</div>
<script src="peaks.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tone/13.8.25/Tone.js"></script>
<script>
(async function(Peaks) {
const audioContext = Tone.context;
// Load audio file into audio buffer
fetch("TOL_6min_720p_download.mp3")
.then(function(response) {
return response.arrayBuffer();
})
.then(function(buffer) {
return audioContext.decodeAudioData(buffer);
})
.then(function(audioBuffer) {
const player = {
externalPlayer: new Tone.Player(audioBuffer),
eventEmitter: null,
init: function(eventEmitter) {
this.eventEmitter = eventEmitter;
this.externalPlayer.sync();
this.externalPlayer.start();
Tone.connectSeries(this.externalPlayer, Tone.Master);
eventEmitter.emit('player.canplay');
Tone.Transport.scheduleRepeat(() => {
var time = this.getCurrentTime();
eventEmitter.emit('player.timeupdate', time);
if (time >= this.getDuration()) {
Tone.Transport.stop();
}
}, 0.25);
},
destroy: function() {
Tone.context.dispose();
this.externalPlayer = null;
this.eventEmitter = null;
},
play: function() {
Tone.Transport.start(
Tone.now(),
this.getCurrentTime()
);
this.eventEmitter.emit('player.play', this.getCurrentTime());
},
pause: function() {
Tone.Transport.pause();
this.eventEmitter.emit('player.pause', this.getCurrentTime());
},
isPlaying: function() {
return Tone.Transport.state === "started";
},
seek: function(time) {
Tone.Transport.seconds = time;
this.eventEmitter.emit('player.seeked', this.getCurrentTime());
this.eventEmitter.emit('player.timeupdate', this.getCurrentTime());
},
isSeeking: function() {
return false;
},
getCurrentTime: function() {
return this.externalPlayer.buffer.toSeconds(Tone.Transport.position);
},
getDuration: function() {
return this.externalPlayer.buffer.duration;
}
};
var options = {
containers: {
zoomview: document.getElementById('zoomview-container'),
overview: document.getElementById('overview-container')
},
player: player,
webAudio: {
audioBuffer: audioBuffer,
scale: 128,
multiChannel: false
},
keyboard: true,
showPlayheadTime: true,
zoomLevels: [128, 256, 512, 1024, 2048, 4096]
};
Peaks.init(options, function(err, peaksInstance) {
if (err) {
console.error(err.message);
return;
}
console.log('Peaks instance ready');
document.querySelector('[data-action="play"]').addEventListener('click', function() {
peaksInstance.player.play();
});
document.querySelector('[data-action="pause"]').addEventListener('click', function() {
peaksInstance.player.pause();
});
document.querySelector('button[data-action="seek"]').addEventListener('click', function(event) {
var time = document.getElementById('seek-time').value;
var seconds = parseFloat(time);
if (!Number.isNaN(seconds)) {
peaksInstance.player.seek(seconds);
}
});
document.querySelector('[data-action="zoom-in"]').addEventListener('click', function() {
peaksInstance.zoom.zoomIn();
});
document.querySelector('[data-action="zoom-out"]').addEventListener('click', function() {
peaksInstance.zoom.zoomOut();
});
document.querySelector('[data-action="play-segment"]').addEventListener('click', function () {
var start = document.getElementById('segment-start').value;
var startInSeconds = parseFloat(start);
var end = document.getElementById('segment-end').value;
var endInSeconds = parseFloat(end);
peaksInstance.player.playSegment({ startTime: startInSeconds, endTime: endInSeconds, editable: true });
});
});
});
})(peaks);
</script>
</body>
</html>