wavesurfer.js 6.5.1 → 7.0.0-alpha.0

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,415 +0,0 @@
1
- /**
2
- * @typedef {Object} MicrophonePluginParams
3
- * @property {MediaStreamConstraints} constraints The constraints parameter is a
4
- * MediaStreamConstaints object with two members: video and audio, describing
5
- * the media types requested. Either or both must be specified.
6
- * @property {number} bufferSize=4096 The buffer size in units of sample-frames.
7
- * If specified, the bufferSize must be one of the following values: `256`,
8
- * `512`, `1024`, `2048`, `4096`, `8192`, `16384`
9
- * @property {number} numberOfInputChannels=1 Integer specifying the number of
10
- * channels for this node's input. Values of up to 32 are supported.
11
- * @property {number} numberOfOutputChannels=1 Integer specifying the number of
12
- * channels for this node's output.
13
- * @property {?boolean} deferInit Set to true to manually call
14
- * `initPlugin('microphone')`
15
- */
16
-
17
- /**
18
- * Visualize microphone input in a wavesurfer instance.
19
- *
20
- * @implements {PluginClass}
21
- * @extends {Observer}
22
- * @example
23
- * // es6
24
- * import MicrophonePlugin from 'wavesurfer.microphone.js';
25
- *
26
- * // commonjs
27
- * var MicrophonePlugin = require('wavesurfer.microphone.js');
28
- *
29
- * // if you are using <script> tags
30
- * var MicrophonePlugin = window.WaveSurfer.microphone;
31
- *
32
- * // ... initialising wavesurfer with the plugin
33
- * var wavesurfer = WaveSurfer.create({
34
- * // wavesurfer options ...
35
- * plugins: [
36
- * MicrophonePlugin.create({
37
- * // plugin options ...
38
- * })
39
- * ]
40
- * });
41
- */
42
- export default class MicrophonePlugin {
43
- /**
44
- * Microphone plugin definition factory
45
- *
46
- * This function must be used to create a plugin definition which can be
47
- * used by wavesurfer to correctly instantiate the plugin.
48
- *
49
- * @param {MicrophonePluginParams} params parameters use to initialise the plugin
50
- * @return {PluginDefinition} an object representing the plugin
51
- */
52
- static create(params) {
53
- return {
54
- name: 'microphone',
55
- deferInit: params && params.deferInit ? params.deferInit : false,
56
- params: params,
57
- instance: MicrophonePlugin
58
- };
59
- }
60
-
61
- constructor(params, ws) {
62
- this.params = params;
63
- this.wavesurfer = ws;
64
-
65
- this.active = false;
66
- this.paused = false;
67
- this.browser = this.detectBrowser();
68
- this.reloadBufferFunction = e => this.reloadBuffer(e);
69
-
70
- // cross-browser getUserMedia
71
- const promisifiedOldGUM = (
72
- constraints,
73
- successCallback,
74
- errorCallback
75
- ) => {
76
- // get a hold of getUserMedia, if present
77
- const getUserMedia =
78
- navigator.getUserMedia ||
79
- navigator.webkitGetUserMedia ||
80
- navigator.mozGetUserMedia ||
81
- navigator.msGetUserMedia;
82
- // Some browsers just don't implement it - return a rejected
83
- // promise with an error to keep a consistent interface
84
- if (!getUserMedia) {
85
- return Promise.reject(
86
- new Error('getUserMedia is not implemented in this browser')
87
- );
88
- }
89
- // otherwise, wrap the call to the old navigator.getUserMedia with
90
- // a Promise
91
- return new Promise((successCallback, errorCallback) => {
92
- getUserMedia.call(
93
- navigator,
94
- constraints,
95
- successCallback,
96
- errorCallback
97
- );
98
- });
99
- };
100
- // Older browsers might not implement mediaDevices at all, so we set an
101
- // empty object first
102
- if (navigator.mediaDevices === undefined) {
103
- navigator.mediaDevices = {};
104
- }
105
- // Some browsers partially implement mediaDevices. We can't just assign
106
- // an object with getUserMedia as it would overwrite existing
107
- // properties. Here, we will just add the getUserMedia property if it's
108
- // missing.
109
- if (navigator.mediaDevices.getUserMedia === undefined) {
110
- navigator.mediaDevices.getUserMedia = promisifiedOldGUM;
111
- }
112
- this.constraints = this.params.constraints || {
113
- video: false,
114
- audio: true
115
- };
116
- this.bufferSize = this.params.bufferSize || 4096;
117
- this.numberOfInputChannels = this.params.numberOfInputChannels || 1;
118
- this.numberOfOutputChannels = this.params.numberOfOutputChannels || 1;
119
-
120
- this._onBackendCreated = () => {
121
- // wavesurfer's AudioContext where we'll route the mic signal to
122
- this.micContext = this.wavesurfer.backend.getAudioContext();
123
- };
124
- }
125
-
126
- init() {
127
- this.wavesurfer.on('backend-created', this._onBackendCreated);
128
- if (this.wavesurfer.backend) {
129
- this._onBackendCreated();
130
- }
131
- }
132
-
133
- /**
134
- * Destroy the microphone plugin.
135
- */
136
- destroy() {
137
- // make sure the buffer is not redrawn during
138
- // cleanup and demolition of this plugin.
139
- this.paused = true;
140
-
141
- this.wavesurfer.un('backend-created', this._onBackendCreated);
142
- this.stop();
143
- }
144
-
145
- /**
146
- * Allow user to select audio input device, e.g. microphone, and
147
- * start the visualization.
148
- */
149
- start() {
150
- navigator.mediaDevices
151
- .getUserMedia(this.constraints)
152
- .then(data => this.gotStream(data))
153
- .catch(data => this.deviceError(data));
154
- }
155
-
156
- /**
157
- * Pause/resume visualization.
158
- */
159
- togglePlay() {
160
- if (!this.active) {
161
- // start it first
162
- this.start();
163
- } else {
164
- // toggle paused
165
- this.paused = !this.paused;
166
-
167
- if (this.paused) {
168
- this.pause();
169
- } else {
170
- this.play();
171
- }
172
- }
173
- }
174
-
175
- /**
176
- * Play visualization.
177
- */
178
- play() {
179
- this.paused = false;
180
-
181
- this.connect();
182
- }
183
-
184
- /**
185
- * Pause visualization.
186
- */
187
- pause() {
188
- this.paused = true;
189
-
190
- // disconnect sources so they can be used elsewhere
191
- // (eg. during audio playback)
192
- this.disconnect();
193
- }
194
-
195
- /**
196
- * Stop the device stream and remove any remaining waveform drawing from
197
- * the wavesurfer canvas.
198
- */
199
- stop() {
200
- if (this.active) {
201
- // stop visualization and device
202
- this.stopDevice();
203
-
204
- // empty last frame
205
- this.wavesurfer.empty();
206
- }
207
- }
208
-
209
- /**
210
- * Stop the device and the visualization.
211
- */
212
- stopDevice() {
213
- this.active = false;
214
-
215
- // stop visualization
216
- this.disconnect();
217
-
218
- // stop stream from device
219
- if (this.stream && this.stream.getTracks) {
220
- this.stream.getTracks().forEach(stream => stream.stop());
221
- }
222
- }
223
-
224
- /**
225
- * Connect the media sources that feed the visualization.
226
- */
227
- connect() {
228
- if (this.stream !== undefined) {
229
- // Create a local buffer for data to be copied to the Wavesurfer buffer for Edge
230
- if (this.browser.browser === 'edge') {
231
- this.localAudioBuffer = this.micContext.createBuffer(
232
- this.numberOfInputChannels,
233
- this.bufferSize,
234
- this.micContext.sampleRate
235
- );
236
- }
237
-
238
- // Create an AudioNode from the stream.
239
- this.mediaStreamSource = this.micContext.createMediaStreamSource(
240
- this.stream
241
- );
242
-
243
- this.levelChecker = this.micContext.createScriptProcessor(
244
- this.bufferSize,
245
- this.numberOfInputChannels,
246
- this.numberOfOutputChannels
247
- );
248
- this.mediaStreamSource.connect(this.levelChecker);
249
-
250
- this.levelChecker.connect(this.micContext.destination);
251
- this.levelChecker.onaudioprocess = this.reloadBufferFunction;
252
- }
253
- }
254
-
255
- /**
256
- * Disconnect the media sources that feed the visualization.
257
- */
258
- disconnect() {
259
- if (this.mediaStreamSource !== undefined) {
260
- this.mediaStreamSource.disconnect();
261
- }
262
-
263
- if (this.levelChecker !== undefined) {
264
- this.levelChecker.disconnect();
265
- this.levelChecker.onaudioprocess = undefined;
266
- }
267
-
268
- if (this.localAudioBuffer !== undefined) {
269
- this.localAudioBuffer = undefined;
270
- }
271
- }
272
-
273
- /**
274
- * Redraw the waveform.
275
- *
276
- * @param {object} event Audioprocess event
277
- */
278
- reloadBuffer(event) {
279
- if (!this.paused) {
280
- this.wavesurfer.empty();
281
-
282
- if (this.browser.browser === 'edge') {
283
- // copy audio data to a local audio buffer,
284
- // from https://github.com/audiojs/audio-buffer-utils
285
- let channel, l;
286
- for (
287
- channel = 0,
288
- l = Math.min(
289
- this.localAudioBuffer.numberOfChannels,
290
- event.inputBuffer.numberOfChannels
291
- );
292
- channel < l;
293
- channel++
294
- ) {
295
- this.localAudioBuffer
296
- .getChannelData(channel)
297
- .set(event.inputBuffer.getChannelData(channel));
298
- }
299
-
300
- this.wavesurfer.loadDecodedBuffer(this.localAudioBuffer);
301
- } else {
302
- this.wavesurfer.loadDecodedBuffer(event.inputBuffer);
303
- }
304
- }
305
- }
306
-
307
- /**
308
- * Audio input device is ready.
309
- *
310
- * @param {MediaStream} stream The microphone's media stream.
311
- */
312
- gotStream(stream) {
313
- this.stream = stream;
314
- this.active = true;
315
-
316
- // start visualization
317
- this.play();
318
-
319
- // notify listeners
320
- this.fireEvent('deviceReady', stream);
321
- }
322
-
323
- /**
324
- * Device error callback.
325
- *
326
- * @param {string} code Error message
327
- */
328
- deviceError(code) {
329
- // notify listeners
330
- this.fireEvent('deviceError', code);
331
- }
332
-
333
- /**
334
- * Extract browser version out of the provided user agent string.
335
- * @param {!string} uastring userAgent string.
336
- * @param {!string} expr Regular expression used as match criteria.
337
- * @param {!number} pos position in the version string to be returned.
338
- * @return {!number} browser version.
339
- */
340
- extractVersion(uastring, expr, pos) {
341
- const match = uastring.match(expr);
342
- return match && match.length >= pos && parseInt(match[pos], 10);
343
- }
344
-
345
- /**
346
- * Browser detector.
347
- * @return {object} result containing browser, version and minVersion
348
- * properties.
349
- */
350
- detectBrowser() {
351
- // Returned result object.
352
- const result = {};
353
- result.browser = null;
354
- result.version = null;
355
- result.minVersion = null;
356
-
357
- // Non supported browser.
358
- if (typeof window === 'undefined' || !window.navigator) {
359
- result.browser = 'Not a supported browser.';
360
- return result;
361
- }
362
-
363
- if (navigator.mozGetUserMedia) {
364
- // Firefox
365
- result.browser = 'firefox';
366
- result.version = this.extractVersion(
367
- navigator.userAgent,
368
- /Firefox\/(\d+)\./,
369
- 1
370
- );
371
- result.minVersion = 31;
372
- return result;
373
- } else if (navigator.webkitGetUserMedia) {
374
- // Chrome/Chromium/Webview/Opera
375
- result.browser = 'chrome';
376
- result.version = this.extractVersion(
377
- navigator.userAgent,
378
- /Chrom(e|ium)\/(\d+)\./,
379
- 2
380
- );
381
- result.minVersion = 38;
382
- return result;
383
- } else if (
384
- navigator.mediaDevices &&
385
- navigator.userAgent.match(/Edge\/(\d+).(\d+)$/)
386
- ) {
387
- // Edge
388
- result.browser = 'edge';
389
- result.version = this.extractVersion(
390
- navigator.userAgent,
391
- /Edge\/(\d+).(\d+)$/,
392
- 2
393
- );
394
- result.minVersion = 10547;
395
- return result;
396
- } else if (
397
- window.RTCPeerConnection &&
398
- navigator.userAgent.match(/AppleWebKit\/(\d+)\./)
399
- ) {
400
- // Safari
401
- result.browser = 'safari';
402
- result.minVersion = 11;
403
- result.version = this.extractVersion(
404
- navigator.userAgent,
405
- /AppleWebKit\/(\d+)\./,
406
- 1
407
- );
408
- return result;
409
- }
410
-
411
- // Non supported browser default.
412
- result.browser = 'Not a supported browser.';
413
- return result;
414
- }
415
- }