wavesurfer.js 6.5.1 → 7.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (132) hide show
  1. package/.eslintrc.json +24 -0
  2. package/.prettierrc +8 -0
  3. package/README.md +26 -117
  4. package/dist/base-plugin.d.ts +10 -0
  5. package/dist/base-plugin.js +13 -0
  6. package/dist/decoder.d.ts +10 -0
  7. package/dist/decoder.js +43 -0
  8. package/dist/event-emitter.d.ts +11 -0
  9. package/dist/event-emitter.js +21 -0
  10. package/dist/fetcher.d.ts +4 -0
  11. package/dist/fetcher.js +17 -0
  12. package/dist/index.d.ts +92 -0
  13. package/dist/index.js +166 -0
  14. package/dist/player-webaudio.d.ts +8 -0
  15. package/dist/player-webaudio.js +31 -0
  16. package/dist/player.d.ts +16 -0
  17. package/dist/player.js +40 -0
  18. package/dist/plugins/regions.d.ts +48 -0
  19. package/dist/plugins/regions.js +183 -0
  20. package/dist/renderer.d.ts +34 -0
  21. package/dist/renderer.js +193 -0
  22. package/dist/timer.d.ts +10 -0
  23. package/dist/timer.js +17 -0
  24. package/examples/audio.ogg +0 -0
  25. package/examples/bars.js +19 -0
  26. package/examples/basic.js +8 -0
  27. package/examples/gradient.js +28 -0
  28. package/examples/regions.js +63 -0
  29. package/examples/video.js +19 -0
  30. package/examples/webaudio.js +14 -0
  31. package/package.json +19 -99
  32. package/src/base-plugin.ts +20 -0
  33. package/src/decoder.ts +41 -0
  34. package/src/event-emitter.ts +35 -0
  35. package/src/fetcher.ts +7 -0
  36. package/src/index.ts +252 -0
  37. package/src/player-webaudio.ts +34 -0
  38. package/src/player.ts +50 -0
  39. package/src/plugins/regions.ts +240 -0
  40. package/src/renderer.ts +250 -0
  41. package/src/timer.ts +27 -0
  42. package/tsconfig.json +105 -0
  43. package/tutorial/index.html +47 -0
  44. package/tutorial/src/editor.js +70 -0
  45. package/tutorial/src/init.js +66 -0
  46. package/tutorial/src/url.js +25 -0
  47. package/tutorial/style.css +211 -0
  48. package/yarn-error.log +1049 -0
  49. package/LICENSE +0 -29
  50. package/dist/plugin/wavesurfer.cursor.js +0 -418
  51. package/dist/plugin/wavesurfer.cursor.js.map +0 -1
  52. package/dist/plugin/wavesurfer.cursor.min.js +0 -7
  53. package/dist/plugin/wavesurfer.cursor.min.js.map +0 -1
  54. package/dist/plugin/wavesurfer.elan.js +0 -381
  55. package/dist/plugin/wavesurfer.elan.js.map +0 -1
  56. package/dist/plugin/wavesurfer.elan.min.js +0 -7
  57. package/dist/plugin/wavesurfer.elan.min.js.map +0 -1
  58. package/dist/plugin/wavesurfer.markers.js +0 -520
  59. package/dist/plugin/wavesurfer.markers.js.map +0 -1
  60. package/dist/plugin/wavesurfer.markers.min.js +0 -7
  61. package/dist/plugin/wavesurfer.markers.min.js.map +0 -1
  62. package/dist/plugin/wavesurfer.mediasession.js +0 -181
  63. package/dist/plugin/wavesurfer.mediasession.js.map +0 -1
  64. package/dist/plugin/wavesurfer.mediasession.min.js +0 -7
  65. package/dist/plugin/wavesurfer.mediasession.min.js.map +0 -1
  66. package/dist/plugin/wavesurfer.microphone.js +0 -466
  67. package/dist/plugin/wavesurfer.microphone.js.map +0 -1
  68. package/dist/plugin/wavesurfer.microphone.min.js +0 -7
  69. package/dist/plugin/wavesurfer.microphone.min.js.map +0 -1
  70. package/dist/plugin/wavesurfer.minimap.js +0 -440
  71. package/dist/plugin/wavesurfer.minimap.js.map +0 -1
  72. package/dist/plugin/wavesurfer.minimap.min.js +0 -7
  73. package/dist/plugin/wavesurfer.minimap.min.js.map +0 -1
  74. package/dist/plugin/wavesurfer.playhead.js +0 -300
  75. package/dist/plugin/wavesurfer.playhead.js.map +0 -1
  76. package/dist/plugin/wavesurfer.playhead.min.js +0 -7
  77. package/dist/plugin/wavesurfer.playhead.min.js.map +0 -1
  78. package/dist/plugin/wavesurfer.regions.js +0 -1335
  79. package/dist/plugin/wavesurfer.regions.js.map +0 -1
  80. package/dist/plugin/wavesurfer.regions.min.js +0 -7
  81. package/dist/plugin/wavesurfer.regions.min.js.map +0 -1
  82. package/dist/plugin/wavesurfer.spectrogram.js +0 -736
  83. package/dist/plugin/wavesurfer.spectrogram.js.map +0 -1
  84. package/dist/plugin/wavesurfer.spectrogram.min.js +0 -7
  85. package/dist/plugin/wavesurfer.spectrogram.min.js.map +0 -1
  86. package/dist/plugin/wavesurfer.timeline.js +0 -649
  87. package/dist/plugin/wavesurfer.timeline.js.map +0 -1
  88. package/dist/plugin/wavesurfer.timeline.min.js +0 -7
  89. package/dist/plugin/wavesurfer.timeline.min.js.map +0 -1
  90. package/dist/wavesurfer-html-init.js +0 -381
  91. package/dist/wavesurfer-html-init.js.map +0 -1
  92. package/dist/wavesurfer-html-init.min.js +0 -7
  93. package/dist/wavesurfer-html-init.min.js.map +0 -1
  94. package/dist/wavesurfer.js +0 -6145
  95. package/dist/wavesurfer.js.map +0 -1
  96. package/dist/wavesurfer.min.js +0 -7
  97. package/dist/wavesurfer.min.js.map +0 -1
  98. package/src/drawer.canvasentry.js +0 -427
  99. package/src/drawer.js +0 -413
  100. package/src/drawer.multicanvas.js +0 -617
  101. package/src/html-init.js +0 -241
  102. package/src/mediaelement-webaudio.js +0 -77
  103. package/src/mediaelement.js +0 -443
  104. package/src/peakcache.js +0 -127
  105. package/src/plugin/cursor/index.js +0 -350
  106. package/src/plugin/elan/index.js +0 -307
  107. package/src/plugin/markers/index.js +0 -436
  108. package/src/plugin/mediasession/index.js +0 -94
  109. package/src/plugin/microphone/index.js +0 -415
  110. package/src/plugin/minimap/index.js +0 -403
  111. package/src/plugin/playhead/index.js +0 -226
  112. package/src/plugin/regions/index.js +0 -478
  113. package/src/plugin/regions/region.js +0 -875
  114. package/src/plugin/spectrogram/fft.js +0 -222
  115. package/src/plugin/spectrogram/index.js +0 -522
  116. package/src/plugin/timeline/index.js +0 -600
  117. package/src/util/absMax.js +0 -16
  118. package/src/util/clamp.js +0 -11
  119. package/src/util/fetch.js +0 -222
  120. package/src/util/frame.js +0 -13
  121. package/src/util/get-id.js +0 -22
  122. package/src/util/index.js +0 -14
  123. package/src/util/max.js +0 -16
  124. package/src/util/min.js +0 -16
  125. package/src/util/observer.js +0 -144
  126. package/src/util/orientation.js +0 -98
  127. package/src/util/prevent-click.js +0 -19
  128. package/src/util/request-animation-frame.js +0 -15
  129. package/src/util/silence-mode.js +0 -42
  130. package/src/util/style.js +0 -16
  131. package/src/wavesurfer.js +0 -1797
  132. package/src/webaudio.js +0 -779
@@ -1,403 +0,0 @@
1
- /*eslint no-console: ["error", { allow: ["warn"] }] */
2
- /**
3
- * @typedef {Object} MinimapPluginParams
4
- * @desc Extends the `WavesurferParams` wavesurfer was initialised with
5
- * @property {?string|HTMLElement} container CSS selector or HTML element where
6
- * the map should be rendered. By default it is simply appended
7
- * after the waveform.
8
- * @property {?boolean} deferInit Set to true to manually call
9
- * `initPlugin('minimap')`
10
- */
11
-
12
- /**
13
- * Renders a smaller version waveform as a minimap of the main waveform.
14
- *
15
- * @implements {PluginClass}
16
- * @extends {Observer}
17
- * @example
18
- * // es6
19
- * import MinimapPlugin from 'wavesurfer.minimap.js';
20
- *
21
- * // commonjs
22
- * var MinimapPlugin = require('wavesurfer.minimap.js');
23
- *
24
- * // if you are using <script> tags
25
- * var MinimapPlugin = window.WaveSurfer.minimap;
26
- *
27
- * // ... initialising wavesurfer with the plugin
28
- * var wavesurfer = WaveSurfer.create({
29
- * // wavesurfer options ...
30
- * plugins: [
31
- * MinimapPlugin.create({
32
- * // plugin options ...
33
- * })
34
- * ]
35
- * });
36
- */
37
- export default class MinimapPlugin {
38
- /**
39
- * Minimap plugin definition factory
40
- *
41
- * This function must be used to create a plugin definition which can be
42
- * used by wavesurfer to correctly instantiate the plugin.
43
- *
44
- * @param {MinimapPluginParams} params parameters use to initialise the plugin
45
- * @return {PluginDefinition} an object representing the plugin
46
- */
47
- static create(params) {
48
- return {
49
- name: 'minimap',
50
- deferInit: params && params.deferInit ? params.deferInit : false,
51
- params: params,
52
- staticProps: {},
53
- instance: MinimapPlugin
54
- };
55
- }
56
-
57
- constructor(params, ws) {
58
- this.params = Object.assign(
59
- {},
60
- ws.params,
61
- {
62
- showRegions: false,
63
- regionsPluginName: params.regionsPluginName || 'regions',
64
- showOverview: false,
65
- overviewBorderColor: 'green',
66
- overviewBorderSize: 2,
67
- // the container should be different
68
- container: false,
69
- height: Math.max(Math.round(ws.params.height / 4), 20)
70
- },
71
- params,
72
- {
73
- scrollParent: false,
74
- fillParent: true
75
- }
76
- );
77
- // if container is a selector, get the element
78
- if (typeof params.container === 'string') {
79
- const el = document.querySelector(params.container);
80
- if (!el) {
81
- console.warn(
82
- `Wavesurfer minimap container ${params.container} was not found! The minimap will be automatically appended below the waveform.`
83
- );
84
- }
85
- this.params.container = el;
86
- }
87
- // if no container is specified add a new element and insert it
88
- if (!params.container) {
89
- this.params.container = ws.util.style(
90
- document.createElement('minimap'),
91
- {
92
- display: 'block'
93
- }
94
- );
95
- }
96
- this.drawer = new ws.Drawer(this.params.container, this.params);
97
- this.wavesurfer = ws;
98
- this.util = ws.util;
99
- this.cleared = false;
100
- this.renderEvent = 'redraw';
101
- this.overviewRegion = null;
102
- this.regionsPlugin = this.wavesurfer[this.params.regionsPluginName];
103
-
104
- this.drawer.createWrapper();
105
- this.createElements();
106
- let isInitialised = false;
107
-
108
- // ws ready event listener
109
- this._onShouldRender = () => {
110
- // only bind the events in the first run
111
- if (!isInitialised) {
112
- this.bindWavesurferEvents();
113
- this.bindMinimapEvents();
114
- isInitialised = true;
115
- }
116
- // if there is no such element, append it to the container (below
117
- // the waveform)
118
- if (!document.body.contains(this.params.container)) {
119
- ws.container.insertBefore(this.params.container, null);
120
- }
121
-
122
- if (this.regionsPlugin && this.params.showRegions) {
123
- this.drawRegions();
124
- }
125
- this.render();
126
- };
127
-
128
- this._onAudioprocess = currentTime => {
129
- this.drawer.progress(this.wavesurfer.backend.getPlayedPercents());
130
- };
131
-
132
- // ws seek event listener
133
- this._onSeek = () =>
134
- this.drawer.progress(ws.backend.getPlayedPercents());
135
-
136
- // event listeners for the overview region
137
- this._onScroll = e => {
138
- if (!this.draggingOverview) {
139
- const orientedTarget = this.util.withOrientation(e.target, this.wavesurfer.params.vertical);
140
- this.moveOverviewRegion(orientedTarget.scrollLeft / this.ratio);
141
- }
142
- };
143
- this._onMouseover = e => {
144
- if (this.draggingOverview) {
145
- this.draggingOverview = false;
146
- }
147
- };
148
- let prevWidth = 0;
149
- this._onResize = ws.util.debounce(() => {
150
- if (prevWidth != this.drawer.wrapper.clientWidth) {
151
- prevWidth = this.drawer.wrapper.clientWidth;
152
- this.render();
153
- this.drawer.progress(
154
- this.wavesurfer.backend.getPlayedPercents()
155
- );
156
- }
157
- });
158
- this._onLoading = percent => {
159
- if (percent >= 100) {
160
- this.cleared = false;
161
- return;
162
- }
163
- if (this.cleared === true) {
164
- return;
165
- }
166
- const len = this.drawer.getWidth();
167
- this.drawer.drawPeaks([0], len, 0, len);
168
- this.cleared = true;
169
- };
170
- this._onZoom = e => {
171
- this.render();
172
- };
173
- this.wavesurfer.on('zoom', this._onZoom);
174
- }
175
-
176
- init() {
177
- if (this.wavesurfer.isReady) {
178
- this._onShouldRender();
179
- }
180
- this.wavesurfer.on(this.renderEvent, this._onShouldRender);
181
- }
182
-
183
- destroy() {
184
- window.removeEventListener('resize', this._onResize, true);
185
- window.removeEventListener('orientationchange', this._onResize, true);
186
- this.wavesurfer.drawer.wrapper.removeEventListener(
187
- 'mouseover',
188
- this._onMouseover
189
- );
190
- this.wavesurfer.un(this.renderEvent, this._onShouldRender);
191
- this.wavesurfer.un('seek', this._onSeek);
192
- this.wavesurfer.un('scroll', this._onScroll);
193
- this.wavesurfer.un('audioprocess', this._onAudioprocess);
194
- this.wavesurfer.un('zoom', this._onZoom);
195
- this.wavesurfer.un('loading', this._onLoading);
196
- this.drawer.destroy();
197
- this.overviewRegion = null;
198
- this.unAll();
199
- }
200
-
201
- drawRegions() {
202
- this.regions = {};
203
-
204
- this.wavesurfer.on('region-created', region => {
205
- this.regions[region.id] = region;
206
- this.drawer.wrapper && this.renderRegions();
207
- });
208
-
209
- this.wavesurfer.on('region-updated', region => {
210
- this.regions[region.id] = region;
211
- this.drawer.wrapper && this.renderRegions();
212
- });
213
-
214
- this.wavesurfer.on('region-removed', region => {
215
- delete this.regions[region.id];
216
- this.drawer.wrapper && this.renderRegions();
217
- });
218
- }
219
-
220
- renderRegions() {
221
- const regionElements = this.drawer.wrapper.querySelectorAll('region');
222
- let i;
223
- for (i = 0; i < regionElements.length; ++i) {
224
- this.drawer.wrapper.removeChild(regionElements[i]);
225
- }
226
-
227
- Object.keys(this.regions).forEach(id => {
228
- const region = this.regions[id];
229
- const width =
230
- this.getWidth() *
231
- ((region.end - region.start) / this.wavesurfer.getDuration());
232
- const left =
233
- this.getWidth() *
234
- (region.start / this.wavesurfer.getDuration());
235
- const regionElement = this.util.style(
236
- document.createElement('region'),
237
- {
238
- height: 'inherit',
239
- backgroundColor: region.color,
240
- width: width + 'px',
241
- left: left + 'px',
242
- display: 'block',
243
- position: 'absolute'
244
- }
245
- );
246
- regionElement.classList.add(id);
247
- this.drawer.wrapper.appendChild(regionElement);
248
- });
249
- }
250
-
251
- createElements() {
252
- this.drawer.createElements();
253
- if (this.params.showOverview) {
254
- this.overviewRegion = this.util.withOrientation(
255
- this.drawer.wrapper.appendChild(document.createElement('overview')),
256
- this.wavesurfer.params.vertical
257
- );
258
-
259
- this.util.style(
260
- this.overviewRegion,
261
- {
262
- top: 0,
263
- bottom: 0,
264
- width: '0px',
265
- display: 'block',
266
- position: 'absolute',
267
- cursor: 'move',
268
- border:
269
- this.params.overviewBorderSize +
270
- 'px solid ' +
271
- this.params.overviewBorderColor,
272
- zIndex: 2,
273
- opacity: this.params.overviewOpacity
274
- }
275
- );
276
- }
277
- }
278
-
279
- bindWavesurferEvents() {
280
- window.addEventListener('resize', this._onResize, true);
281
- window.addEventListener('orientationchange', this._onResize, true);
282
- this.wavesurfer.on('audioprocess', this._onAudioprocess);
283
- this.wavesurfer.on('seek', this._onSeek);
284
- this.wavesurfer.on('loading', this._onLoading);
285
- if (this.params.showOverview) {
286
- this.wavesurfer.on('scroll', this._onScroll);
287
- this.wavesurfer.drawer.wrapper.addEventListener(
288
- 'mouseover',
289
- this._onMouseover
290
- );
291
- }
292
- }
293
-
294
- bindMinimapEvents() {
295
- const positionMouseDown = {
296
- clientX: 0,
297
- clientY: 0
298
- };
299
- let relativePositionX = 0;
300
- let seek = true;
301
-
302
- // the following event listeners will be destroyed by using
303
- // this.unAll() and nullifying the DOM node references after
304
- // removing them
305
- if (this.params.interact) {
306
- this.drawer.wrapper.addEventListener('click', event => {
307
- this.fireEvent('click', event, this.drawer.handleEvent(event));
308
- });
309
-
310
- this.on('click', (event, position) => {
311
- if (seek) {
312
- this.drawer.progress(position);
313
- this.wavesurfer.seekAndCenter(position);
314
- } else {
315
- seek = true;
316
- }
317
- });
318
- }
319
-
320
- if (this.params.showOverview) {
321
- this.overviewRegion.domElement.addEventListener('mousedown', e => {
322
- const event = this.util.withOrientation(e, this.wavesurfer.params.vertical);
323
- this.draggingOverview = true;
324
- relativePositionX = event.layerX;
325
- positionMouseDown.clientX = event.clientX;
326
- positionMouseDown.clientY = event.clientY;
327
- });
328
-
329
- this.drawer.wrapper.addEventListener('mousemove', e => {
330
- if (this.draggingOverview) {
331
- const event = this.util.withOrientation(e, this.wavesurfer.params.vertical);
332
- this.moveOverviewRegion(
333
- event.clientX -
334
- this.drawer.container.getBoundingClientRect().left -
335
- relativePositionX
336
- );
337
- }
338
- });
339
-
340
- this.drawer.wrapper.addEventListener('mouseup', e => {
341
- const event = this.util.withOrientation(e, this.wavesurfer.params.vertical);
342
- if (
343
- positionMouseDown.clientX - event.clientX === 0 &&
344
- positionMouseDown.clientX - event.clientX === 0
345
- ) {
346
- seek = true;
347
- this.draggingOverview = false;
348
- } else if (this.draggingOverview) {
349
- seek = false;
350
- this.draggingOverview = false;
351
- }
352
- });
353
- }
354
- }
355
-
356
- render() {
357
- const len = this.drawer.getWidth();
358
- const peaks = this.wavesurfer.backend.getPeaks(len, 0, len);
359
- this.drawer.drawPeaks(peaks, len, 0, len);
360
- this.drawer.progress(this.wavesurfer.backend.getPlayedPercents());
361
-
362
- if (this.params.showOverview) {
363
- //get proportional width of overview region considering the respective
364
- //width of the drawers
365
- this.ratio = this.wavesurfer.drawer.width / this.drawer.width;
366
- this.waveShowedWidth = this.wavesurfer.drawer.width / this.ratio;
367
- this.waveWidth = this.wavesurfer.drawer.width;
368
- this.overviewWidth = this.drawer.container.offsetWidth / this.ratio;
369
- this.overviewPosition = 0;
370
- this.moveOverviewRegion(
371
- this.wavesurfer.drawer.wrapper.scrollLeft / this.ratio
372
- );
373
- this.util.style(this.overviewRegion, {
374
- width: this.overviewWidth + 'px'
375
- });
376
- }
377
- }
378
-
379
- moveOverviewRegion(pixels) {
380
- if (pixels < 0) {
381
- this.overviewPosition = 0;
382
- } else if (
383
- pixels + this.overviewWidth <
384
- this.drawer.container.offsetWidth
385
- ) {
386
- this.overviewPosition = pixels;
387
- } else {
388
- this.overviewPosition =
389
- this.drawer.container.offsetWidth - this.overviewWidth;
390
- }
391
- this.util.style(this.overviewRegion, {
392
- left: this.overviewPosition + 'px'
393
- });
394
- if (this.draggingOverview) {
395
- this.wavesurfer.drawer.wrapper.scrollLeft =
396
- this.overviewPosition * this.ratio;
397
- }
398
- }
399
-
400
- getWidth() {
401
- return this.drawer.width / this.params.pixelRatio;
402
- }
403
- }
@@ -1,226 +0,0 @@
1
-
2
- /**
3
- * The playhead plugin separates the notion of the currently playing position from
4
- * a 'play-start' position. Having a playhead enables a listening pattern
5
- * (commonly found in DAWs) that involves listening to a section of a track
6
- * repeatedly, rather than listening to an entire track in a linear fashion.
7
- *
8
- * @implements {PluginClass}
9
- *
10
- * @example
11
- * import PlayheadPlugin from 'wavesurfer.playhead.js';
12
- *
13
- * // if you are using <script> tags
14
- * var PlayheadPlugin = window.WaveSurfer.playhead;
15
- *
16
- * // ... initialising wavesurfer with the plugin
17
- * var wavesurfer = WaveSurfer.create({
18
- * // wavesurfer options ...
19
- * plugins: [
20
- * PlayheadPlugin.create({
21
- * movePlayheadOnSeek: true,
22
- * drawPlayhead: false,
23
- * movePlayheadOnPause: false
24
- * })
25
- * ]
26
- * });
27
- */
28
-
29
- const DEFAULT_FILL_COLOR = '#CF2F00';
30
-
31
- export default class PlayheadPlugin {
32
- /**
33
- * @typedef {Object} PlayheadPluginParams
34
- * @property {?boolean} draw=true Draw the playhead as a triangle/line
35
- * @property {?boolean} moveOnSeek=true Seeking (via clicking) while playing moves the playhead
36
- * @property {?boolean} returnOnPause=true Pausing the track returns the seek position to the playhead
37
- */
38
-
39
- /**
40
- * Playhead plugin definition factory
41
- *
42
- * This function must be used to create a plugin definition which can be
43
- * used by wavesurfer to correctly instantiate the plugin.
44
- *
45
- * @param {PlayheadPluginParams} params parameters use to initialise the plugin
46
- * @since 5.0.0
47
- * @return {PluginDefinition} an object representing the plugin
48
- */
49
- static create(params) {
50
- return {
51
- name: 'playhead',
52
- deferInit: params && params.deferInit ? params.deferInit : false,
53
- params: params,
54
- instance: PlayheadPlugin
55
- };
56
- }
57
-
58
- constructor(params, ws) {
59
- this.params = params;
60
- this.options = {};
61
-
62
- ['draw', 'moveOnSeek', 'returnOnPause'].forEach(opt => {
63
- if (opt in params) {
64
- this.options[opt] = params[opt];
65
- } else {
66
- this.options[opt] = true;
67
- }
68
- });
69
-
70
-
71
- this.wavesurfer = ws;
72
- this.util = ws.util;
73
- this.style = this.util.style;
74
- this.markerWidth = 21;
75
- this.markerHeight = 16;
76
- this.playheadTime = 0;
77
- this.unFuns = [];
78
-
79
- this._onResize = () => {
80
- this._updatePlayheadPosition();
81
- };
82
-
83
- this._onReady = () => {
84
- this.wrapper = this.wavesurfer.drawer.wrapper;
85
- this._updatePlayheadPosition();
86
- };
87
- }
88
-
89
- _onBackendCreated() {
90
- this.wrapper = this.wavesurfer.drawer.wrapper;
91
-
92
- if (this.options.draw) {
93
- this._createPlayheadElement();
94
- window.addEventListener('resize', this._onResize, true);
95
- window.addEventListener('orientationchange', this._onResize, true);
96
-
97
- this.wavesurferOn('zoom', this._onResize);
98
- }
99
-
100
- this.wavesurferOn('pause', () => {
101
- if ( this.options.returnOnPause ) {
102
- this.wavesurfer.setCurrentTime(this.playheadTime);
103
- }
104
- });
105
-
106
- this.wavesurferOn('seek', () => {
107
- if ( this.options.moveOnSeek ) {
108
- this.playheadTime = this.wavesurfer.getCurrentTime();
109
- this._updatePlayheadPosition();
110
- }
111
- });
112
-
113
- this.playheadTime = this.wavesurfer.getCurrentTime();
114
- }
115
-
116
- wavesurferOn(ev, fn) {
117
- let ret = this.wavesurfer.on(ev, fn);
118
- this.unFuns.push(() => {
119
- this.wavesurfer.un(ev, fn);
120
- });
121
- return ret;
122
- }
123
-
124
- init() {
125
- if (this.wavesurfer.isReady) {
126
- this._onBackendCreated();
127
- this._onReady();
128
- } else {
129
- let r;
130
-
131
- this.wavesurfer.once('ready', () => this._onReady());
132
- this.wavesurfer.once('backend-created', () => this._onBackendCreated());
133
- }
134
- }
135
-
136
- destroy() {
137
- this.unFuns.forEach(f => f());
138
- this.unFuns = [];
139
-
140
- this.wrapper.removeChild(this.element);
141
-
142
- window.removeEventListener('resize', this._onResize, true);
143
- window.removeEventListener('orientationchange', this._onResize, true);
144
- }
145
-
146
- setPlayheadTime(time) {
147
- this.playheadTime = time;
148
-
149
- if (!this.wavesurfer.isPlaying()) {
150
- this.wavesurfer.setCurrentTime(time);
151
- }
152
- this._updatePlayheadPosition();
153
- }
154
-
155
- _createPointerSVG() {
156
- const svgNS = 'http://www.w3.org/2000/svg';
157
-
158
- const el = document.createElementNS(svgNS, 'svg');
159
- const path = document.createElementNS(svgNS, 'path');
160
-
161
- el.setAttribute('viewBox', '0 0 33 30');
162
- path.setAttribute('d', 'M16.75 31 31.705 5.566A3 3 0 0 0 29.146 1H4.354a3 3 0 0 0-2.56 4.566L16.75 31z');
163
- path.setAttribute('stroke', '#979797');
164
- path.setAttribute('fill', DEFAULT_FILL_COLOR);
165
-
166
- el.appendChild(path);
167
-
168
- this.style(el, {
169
- width: this.markerWidth + 'px',
170
- height: this.markerHeight + 'px',
171
- cursor: 'pointer',
172
- 'z-index': 5
173
- });
174
- return el;
175
- }
176
-
177
- _createPlayheadElement() {
178
- const el = document.createElement('playhead');
179
- el.className = 'wavesurfer-playhead';
180
-
181
- this.style(el, {
182
- position: 'absolute',
183
- height: '100%',
184
- display: 'flex',
185
- 'flex-direction': 'column'
186
- });
187
-
188
- const pointer = this._createPointerSVG();
189
- el.appendChild(pointer);
190
-
191
- pointer.addEventListener('click', e => {
192
- e.stopPropagation();
193
- this.wavesurfer.setCurrentTime(this.playheadTime);
194
- });
195
-
196
- const line = document.createElement('div');
197
- this.style(line, {
198
- 'flex-grow': 1,
199
- 'margin-left': (this.markerWidth / 2 - 0.5) + 'px',
200
- background: 'black',
201
- width: '1px',
202
- opacity: 0.1
203
- });
204
-
205
- el.appendChild(line);
206
-
207
- this.element = el;
208
- this.wrapper.appendChild(el);
209
- }
210
-
211
- _updatePlayheadPosition() {
212
- if (!this.element) {
213
- return;
214
- }
215
-
216
- const duration = this.wavesurfer.getDuration();
217
- const elementWidth =
218
- this.wavesurfer.drawer.width /
219
- this.wavesurfer.params.pixelRatio;
220
-
221
- const positionPct = this.playheadTime / duration;
222
- this.style(this.element, {
223
- left: ((elementWidth * positionPct) - (this.markerWidth / 2)) + 'px'
224
- });
225
- }
226
- }