wavesurfer.js 7.0.0-beta.6 → 7.0.0-beta.8

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 (58) hide show
  1. package/README.md +2 -2
  2. package/dist/plugins/minimap.min.js +1 -1
  3. package/dist/plugins/regions.d.ts +1 -0
  4. package/dist/plugins/regions.js +3 -0
  5. package/dist/plugins/regions.min.js +1 -1
  6. package/dist/renderer.d.ts +2 -0
  7. package/dist/renderer.js +33 -19
  8. package/dist/wavesurfer.d.ts +9 -9
  9. package/dist/wavesurfer.js +3 -4
  10. package/dist/wavesurfer.min.js +1 -1
  11. package/package.json +5 -5
  12. package/dist/cypress/support/commands.d.ts +0 -1
  13. package/dist/cypress/support/commands.js +0 -39
  14. package/dist/cypress/support/e2e.d.ts +0 -1
  15. package/dist/cypress/support/e2e.js +0 -18
  16. package/dist/cypress.config.d.ts +0 -2
  17. package/dist/cypress.config.js +0 -10
  18. package/dist/plugins/envelope.min.cjs +0 -1
  19. package/dist/plugins/minimap.min.cjs +0 -1
  20. package/dist/plugins/multitrack.d.ts +0 -117
  21. package/dist/plugins/multitrack.js +0 -507
  22. package/dist/plugins/multitrack.min.cjs +0 -1
  23. package/dist/plugins/multitrack.min.js +0 -1
  24. package/dist/plugins/record.min.cjs +0 -1
  25. package/dist/plugins/regions.min.cjs +0 -1
  26. package/dist/plugins/spectrogram.min.cjs +0 -1
  27. package/dist/plugins/timeline.min.cjs +0 -1
  28. package/dist/src/base-plugin.d.ts +0 -13
  29. package/dist/src/base-plugin.js +0 -22
  30. package/dist/src/decoder.d.ts +0 -9
  31. package/dist/src/decoder.js +0 -48
  32. package/dist/src/event-emitter.d.ts +0 -19
  33. package/dist/src/event-emitter.js +0 -45
  34. package/dist/src/fetcher.d.ts +0 -5
  35. package/dist/src/fetcher.js +0 -7
  36. package/dist/src/player.d.ts +0 -45
  37. package/dist/src/player.js +0 -114
  38. package/dist/src/plugins/envelope.d.ts +0 -71
  39. package/dist/src/plugins/envelope.js +0 -350
  40. package/dist/src/plugins/minimap.d.ts +0 -39
  41. package/dist/src/plugins/minimap.js +0 -117
  42. package/dist/src/plugins/record.d.ts +0 -26
  43. package/dist/src/plugins/record.js +0 -124
  44. package/dist/src/plugins/regions.d.ts +0 -93
  45. package/dist/src/plugins/regions.js +0 -395
  46. package/dist/src/plugins/spectrogram-fft.d.ts +0 -9
  47. package/dist/src/plugins/spectrogram-fft.js +0 -150
  48. package/dist/src/plugins/spectrogram.d.ts +0 -69
  49. package/dist/src/plugins/spectrogram.js +0 -340
  50. package/dist/src/plugins/timeline.d.ts +0 -45
  51. package/dist/src/plugins/timeline.js +0 -162
  52. package/dist/src/renderer.d.ts +0 -41
  53. package/dist/src/renderer.js +0 -420
  54. package/dist/src/timer.d.ts +0 -11
  55. package/dist/src/timer.js +0 -19
  56. package/dist/src/wavesurfer.d.ts +0 -149
  57. package/dist/src/wavesurfer.js +0 -229
  58. package/dist/wavesurfer.min.cjs +0 -1
@@ -1,150 +0,0 @@
1
- // @ts-nocheck
2
- /**
3
- * Calculate FFT - Based on https://github.com/corbanbrook/dsp.js
4
- *
5
- * @param {Number} bufferSize Buffer size
6
- * @param {Number} sampleRate Sample rate
7
- * @param {Function} windowFunc Window function
8
- * @param {Number} alpha Alpha channel
9
- */
10
- export default function FFT(bufferSize, sampleRate, windowFunc, alpha) {
11
- this.bufferSize = bufferSize;
12
- this.sampleRate = sampleRate;
13
- this.bandwidth = (2 / bufferSize) * (sampleRate / 2);
14
- this.sinTable = new Float32Array(bufferSize);
15
- this.cosTable = new Float32Array(bufferSize);
16
- this.windowValues = new Float32Array(bufferSize);
17
- this.reverseTable = new Uint32Array(bufferSize);
18
- this.peakBand = 0;
19
- this.peak = 0;
20
- var i;
21
- switch (windowFunc) {
22
- case 'bartlett':
23
- for (i = 0; i < bufferSize; i++) {
24
- this.windowValues[i] = (2 / (bufferSize - 1)) * ((bufferSize - 1) / 2 - Math.abs(i - (bufferSize - 1) / 2));
25
- }
26
- break;
27
- case 'bartlettHann':
28
- for (i = 0; i < bufferSize; i++) {
29
- this.windowValues[i] =
30
- 0.62 - 0.48 * Math.abs(i / (bufferSize - 1) - 0.5) - 0.38 * Math.cos((Math.PI * 2 * i) / (bufferSize - 1));
31
- }
32
- break;
33
- case 'blackman':
34
- alpha = alpha || 0.16;
35
- for (i = 0; i < bufferSize; i++) {
36
- this.windowValues[i] =
37
- (1 - alpha) / 2 -
38
- 0.5 * Math.cos((Math.PI * 2 * i) / (bufferSize - 1)) +
39
- (alpha / 2) * Math.cos((4 * Math.PI * i) / (bufferSize - 1));
40
- }
41
- break;
42
- case 'cosine':
43
- for (i = 0; i < bufferSize; i++) {
44
- this.windowValues[i] = Math.cos((Math.PI * i) / (bufferSize - 1) - Math.PI / 2);
45
- }
46
- break;
47
- case 'gauss':
48
- alpha = alpha || 0.25;
49
- for (i = 0; i < bufferSize; i++) {
50
- this.windowValues[i] = Math.pow(Math.E, -0.5 * Math.pow((i - (bufferSize - 1) / 2) / ((alpha * (bufferSize - 1)) / 2), 2));
51
- }
52
- break;
53
- case 'hamming':
54
- for (i = 0; i < bufferSize; i++) {
55
- this.windowValues[i] = 0.54 - 0.46 * Math.cos((Math.PI * 2 * i) / (bufferSize - 1));
56
- }
57
- break;
58
- case 'hann':
59
- case undefined:
60
- for (i = 0; i < bufferSize; i++) {
61
- this.windowValues[i] = 0.5 * (1 - Math.cos((Math.PI * 2 * i) / (bufferSize - 1)));
62
- }
63
- break;
64
- case 'lanczoz':
65
- for (i = 0; i < bufferSize; i++) {
66
- this.windowValues[i] =
67
- Math.sin(Math.PI * ((2 * i) / (bufferSize - 1) - 1)) / (Math.PI * ((2 * i) / (bufferSize - 1) - 1));
68
- }
69
- break;
70
- case 'rectangular':
71
- for (i = 0; i < bufferSize; i++) {
72
- this.windowValues[i] = 1;
73
- }
74
- break;
75
- case 'triangular':
76
- for (i = 0; i < bufferSize; i++) {
77
- this.windowValues[i] = (2 / bufferSize) * (bufferSize / 2 - Math.abs(i - (bufferSize - 1) / 2));
78
- }
79
- break;
80
- default:
81
- throw Error("No such window function '" + windowFunc + "'");
82
- }
83
- var limit = 1;
84
- var bit = bufferSize >> 1;
85
- var i;
86
- while (limit < bufferSize) {
87
- for (i = 0; i < limit; i++) {
88
- this.reverseTable[i + limit] = this.reverseTable[i] + bit;
89
- }
90
- limit = limit << 1;
91
- bit = bit >> 1;
92
- }
93
- for (i = 0; i < bufferSize; i++) {
94
- this.sinTable[i] = Math.sin(-Math.PI / i);
95
- this.cosTable[i] = Math.cos(-Math.PI / i);
96
- }
97
- this.calculateSpectrum = function (buffer) {
98
- // Locally scope variables for speed up
99
- var bufferSize = this.bufferSize, cosTable = this.cosTable, sinTable = this.sinTable, reverseTable = this.reverseTable, real = new Float32Array(bufferSize), imag = new Float32Array(bufferSize), bSi = 2 / this.bufferSize, sqrt = Math.sqrt, rval, ival, mag, spectrum = new Float32Array(bufferSize / 2);
100
- var k = Math.floor(Math.log(bufferSize) / Math.LN2);
101
- if (Math.pow(2, k) !== bufferSize) {
102
- throw 'Invalid buffer size, must be a power of 2.';
103
- }
104
- if (bufferSize !== buffer.length) {
105
- throw ('Supplied buffer is not the same size as defined FFT. FFT Size: ' +
106
- bufferSize +
107
- ' Buffer Size: ' +
108
- buffer.length);
109
- }
110
- var halfSize = 1, phaseShiftStepReal, phaseShiftStepImag, currentPhaseShiftReal, currentPhaseShiftImag, off, tr, ti, tmpReal;
111
- for (var i = 0; i < bufferSize; i++) {
112
- real[i] = buffer[reverseTable[i]] * this.windowValues[reverseTable[i]];
113
- imag[i] = 0;
114
- }
115
- while (halfSize < bufferSize) {
116
- phaseShiftStepReal = cosTable[halfSize];
117
- phaseShiftStepImag = sinTable[halfSize];
118
- currentPhaseShiftReal = 1;
119
- currentPhaseShiftImag = 0;
120
- for (var fftStep = 0; fftStep < halfSize; fftStep++) {
121
- var i = fftStep;
122
- while (i < bufferSize) {
123
- off = i + halfSize;
124
- tr = currentPhaseShiftReal * real[off] - currentPhaseShiftImag * imag[off];
125
- ti = currentPhaseShiftReal * imag[off] + currentPhaseShiftImag * real[off];
126
- real[off] = real[i] - tr;
127
- imag[off] = imag[i] - ti;
128
- real[i] += tr;
129
- imag[i] += ti;
130
- i += halfSize << 1;
131
- }
132
- tmpReal = currentPhaseShiftReal;
133
- currentPhaseShiftReal = tmpReal * phaseShiftStepReal - currentPhaseShiftImag * phaseShiftStepImag;
134
- currentPhaseShiftImag = tmpReal * phaseShiftStepImag + currentPhaseShiftImag * phaseShiftStepReal;
135
- }
136
- halfSize = halfSize << 1;
137
- }
138
- for (var i = 0, N = bufferSize / 2; i < N; i++) {
139
- rval = real[i];
140
- ival = imag[i];
141
- mag = bSi * sqrt(rval * rval + ival * ival);
142
- if (mag > this.peak) {
143
- this.peakBand = i;
144
- this.peak = mag;
145
- }
146
- spectrum[i] = mag;
147
- }
148
- return spectrum;
149
- };
150
- }
@@ -1,69 +0,0 @@
1
- /**
2
- * Spectrogram plugin
3
- *
4
- * Render a spectrogram visualisation of the audio.
5
- *
6
- * @author Pavel Denisov (https://github.com/akreal)
7
- * @see https://github.com/wavesurfer-js/wavesurfer.js/pull/337
8
- *
9
- * @example
10
- * // ... initialising wavesurfer with the plugin
11
- * var wavesurfer = WaveSurfer.create({
12
- * // wavesurfer options ...
13
- * plugins: [
14
- * SpectrogramPlugin.create({
15
- * // plugin options ...
16
- * })
17
- * ]
18
- * });
19
- */
20
- import BasePlugin from '../base-plugin.js';
21
- export type SpectrogramPluginOptions = {
22
- /** Selector of element or element in which to render */
23
- container: string | HTMLElement;
24
- /** Number of samples to fetch to FFT. Must be a power of 2. */
25
- fftSamples?: number;
26
- /** Height of the spectrogram view in CSS pixels */
27
- height?: number;
28
- /** Set to true to display frequency labels. */
29
- labels?: boolean;
30
- /** Size of the overlapping window. Must be < fftSamples. Auto deduced from canvas size by default. */
31
- noverlap?: number;
32
- /** The window function to be used. */
33
- windowFunc?: 'bartlett' | 'bartlettHann' | 'blackman' | 'cosine' | 'gauss' | 'hamming' | 'hann' | 'lanczoz' | 'rectangular' | 'triangular';
34
- /** Some window functions have this extra value. (Between 0 and 1) */
35
- alpha?: number;
36
- /** Min frequency to scale spectrogram. */
37
- frequencyMin?: number;
38
- /** Max frequency to scale spectrogram. Set this to samplerate/2 to draw whole range of spectrogram. */
39
- frequencyMax?: number;
40
- /**
41
- * A 256 long array of 4-element arrays. Each entry should contain a float between 0 and 1 and specify r, g, b, and alpha.
42
- * Each entry should contain a float between 0 and 1 and specify r, g, b, and alpha.
43
- */
44
- colorMap?: number[][];
45
- };
46
- export type SpectrogramPluginEvents = {
47
- ready: [];
48
- click: [relativeX: number];
49
- };
50
- export default class SpectrogramPlugin extends BasePlugin<SpectrogramPluginEvents, SpectrogramPluginOptions> {
51
- static create(options?: SpectrogramPluginOptions): SpectrogramPlugin;
52
- utils: {
53
- style: (el: HTMLElement, styles: Record<string, string>) => CSSStyleDeclaration & Record<string, string>;
54
- };
55
- constructor(options: SpectrogramPluginOptions);
56
- onInit(): void;
57
- destroy(): void;
58
- createWrapper(): void;
59
- _wrapperClickHandler(event: any): void;
60
- createCanvas(): void;
61
- render(): void;
62
- drawSpectrogram: (frequenciesData: any) => void;
63
- getFrequencies(callback: any): void;
64
- loadFrequenciesData(url: any): Promise<void>;
65
- freqType(freq: any): string | number;
66
- unitType(freq: any): "KHz" | "Hz";
67
- loadLabels(bgFill: any, fontSizeFreq: any, fontSizeUnit: any, fontType: any, textColorFreq: any, textColorUnit: any, textAlign: any, container: any): void;
68
- resample(oldMatrix: any): Uint8Array[];
69
- }
@@ -1,340 +0,0 @@
1
- /**
2
- * Spectrogram plugin
3
- *
4
- * Render a spectrogram visualisation of the audio.
5
- *
6
- * @author Pavel Denisov (https://github.com/akreal)
7
- * @see https://github.com/wavesurfer-js/wavesurfer.js/pull/337
8
- *
9
- * @example
10
- * // ... initialising wavesurfer with the plugin
11
- * var wavesurfer = WaveSurfer.create({
12
- * // wavesurfer options ...
13
- * plugins: [
14
- * SpectrogramPlugin.create({
15
- * // plugin options ...
16
- * })
17
- * ]
18
- * });
19
- */
20
- // @ts-nocheck
21
- import BasePlugin from '../base-plugin.js';
22
- import FFT from './spectrogram-fft.js';
23
- export default class SpectrogramPlugin extends BasePlugin {
24
- static create(options) {
25
- return new SpectrogramPlugin(options || {});
26
- }
27
- utils = {
28
- style: (el, styles) => Object.assign(el.style, styles),
29
- };
30
- constructor(options) {
31
- super(options);
32
- this.frequenciesDataUrl = options.frequenciesDataUrl;
33
- this.container =
34
- 'string' == typeof options.container ? document.querySelector(options.container) : options.container;
35
- if (options.colorMap) {
36
- if (options.colorMap.length < 256) {
37
- throw new Error('Colormap must contain 256 elements');
38
- }
39
- for (let i = 0; i < options.colorMap.length; i++) {
40
- const cmEntry = options.colorMap[i];
41
- if (cmEntry.length !== 4) {
42
- throw new Error('ColorMap entries must contain 4 values');
43
- }
44
- }
45
- this.colorMap = options.colorMap;
46
- }
47
- else {
48
- this.colorMap = [];
49
- for (let i = 0; i < 256; i++) {
50
- const val = (255 - i) / 256;
51
- this.colorMap.push([val, val, val, 1]);
52
- }
53
- }
54
- this.fftSamples = options.fftSamples || 512;
55
- this.height = options.height || this.fftSamples / 2;
56
- this.noverlap = options.noverlap;
57
- this.windowFunc = options.windowFunc;
58
- this.alpha = options.alpha;
59
- this.channels = 1;
60
- // Getting file's original samplerate is difficult(#1248).
61
- // So set 12kHz default to render like wavesurfer.js 5.x.
62
- this.frequencyMin = options.frequencyMin || 0;
63
- this.frequencyMax = options.frequencyMax || 0;
64
- this.createWrapper();
65
- this.createCanvas();
66
- }
67
- onInit() {
68
- this.container = this.container || this.wavesurfer.getWrapper();
69
- this.container.appendChild(this.wrapper);
70
- if (this.wavesurfer.options.fillParent) {
71
- this.utils.style(this.wrapper, {
72
- width: '100%',
73
- overflowX: 'hidden',
74
- overflowY: 'hidden',
75
- });
76
- }
77
- this.width = this.wavesurfer.getWrapper().offsetWidth;
78
- this.subscriptions.push(this.wavesurfer.on('redraw', () => this.render()));
79
- }
80
- destroy() {
81
- this.unAll();
82
- this.wavesurfer.un('ready', this._onReady);
83
- this.wavesurfer.un('redraw', this._onRender);
84
- this.wavesurfer = null;
85
- this.util = null;
86
- this.options = null;
87
- if (this.wrapper) {
88
- this.wrapper.remove();
89
- this.wrapper = null;
90
- }
91
- }
92
- createWrapper() {
93
- this.wrapper = document.createElement('div');
94
- this.utils.style(this.wrapper, {
95
- display: 'block',
96
- position: 'relative',
97
- userSelect: 'none',
98
- webkitUserSelect: 'none',
99
- height: `${this.height * this.channels}px`,
100
- });
101
- // if labels are active
102
- if (this.options.labels) {
103
- const labelsEl = document.createElement('canvas');
104
- labelsEl.classList.add('spec-labels');
105
- this.utils.style(labelsEl, {
106
- position: 'absolute',
107
- zIndex: 9,
108
- width: '55px',
109
- height: '100%',
110
- });
111
- this.wrapper.appendChild(labelsEl);
112
- this.labelsEl = labelsEl;
113
- }
114
- this.wrapper.addEventListener('click', this._onWrapperClick);
115
- }
116
- _wrapperClickHandler(event) {
117
- event.preventDefault();
118
- const relX = 'offsetX' in event ? event.offsetX : event.layerX;
119
- this.emit('click', relX / this.width || 0);
120
- }
121
- createCanvas() {
122
- const canvas = document.createElement('canvas');
123
- this.wrapper.appendChild(canvas);
124
- this.spectrCc = canvas.getContext('2d');
125
- this.utils.style(canvas, {
126
- position: 'absolute',
127
- left: 0,
128
- top: 0,
129
- width: '100%',
130
- height: '100%',
131
- zIndex: 4,
132
- });
133
- this.canvas = canvas;
134
- }
135
- render() {
136
- this.canvas.width = this.width;
137
- this.canvas.height = this.height;
138
- if (this.frequenciesDataUrl) {
139
- this.loadFrequenciesData(this.frequenciesDataUrl);
140
- }
141
- else {
142
- this.getFrequencies(this.drawSpectrogram);
143
- }
144
- this.loadLabels('rgba(68,68,68,0.5)', '12px', '10px', '', '#fff', '#f7f7f7', 'center', '#specLabels');
145
- }
146
- drawSpectrogram = (frequenciesData) => {
147
- if (!isNaN(frequenciesData[0][0])) {
148
- // data is 1ch [sample, freq] format
149
- // to [channel, sample, freq] format
150
- frequenciesData = [frequenciesData];
151
- }
152
- const spectrCc = this.spectrCc;
153
- const height = this.height;
154
- const width = this.width;
155
- const freqFrom = this.buffer.sampleRate / 2;
156
- const freqMin = this.frequencyMin;
157
- const freqMax = this.frequencyMax;
158
- if (!spectrCc) {
159
- return;
160
- }
161
- for (let c = 0; c < frequenciesData.length; c++) {
162
- // for each channel
163
- const pixels = this.resample(frequenciesData[c]);
164
- const imageData = new ImageData(width, height);
165
- for (let i = 0; i < pixels.length; i++) {
166
- for (let j = 0; j < pixels[i].length; j++) {
167
- const colorMap = this.colorMap[pixels[i][j]];
168
- const redIndex = ((height - j) * width + i) * 4;
169
- imageData.data[redIndex] = colorMap[0] * 255;
170
- imageData.data[redIndex + 1] = colorMap[1] * 255;
171
- imageData.data[redIndex + 2] = colorMap[2] * 255;
172
- imageData.data[redIndex + 3] = colorMap[3] * 255;
173
- }
174
- }
175
- // scale and stack spectrograms
176
- createImageBitmap(imageData).then((renderer) => {
177
- spectrCc.drawImage(renderer, 0, height * (1 - freqMax / freqFrom), // source x, y
178
- width, (height * (freqMax - freqMin)) / freqFrom, // source width, height
179
- 0, height * c, // destination x, y
180
- width, height);
181
- });
182
- }
183
- this.emit('ready');
184
- };
185
- getFrequencies(callback) {
186
- const fftSamples = this.fftSamples;
187
- const buffer = this.wavesurfer.getDecodedData();
188
- const channels = this.channels;
189
- this.frequencyMax = this.frequencyMax || buffer.sampleRate / 2;
190
- if (!buffer)
191
- return;
192
- this.buffer = buffer;
193
- // This may differ from file samplerate. Browser resamples audio.
194
- const sampleRate = buffer.sampleRate;
195
- const frequencies = [];
196
- let noverlap = this.noverlap;
197
- if (!noverlap) {
198
- const uniqueSamplesPerPx = buffer.length / this.canvas.width;
199
- noverlap = Math.max(0, Math.round(fftSamples - uniqueSamplesPerPx));
200
- }
201
- const fft = new FFT(fftSamples, sampleRate, this.windowFunc, this.alpha);
202
- for (let c = 0; c < channels; c++) {
203
- // for each channel
204
- const channelData = buffer.getChannelData(c);
205
- const channelFreq = [];
206
- let currentOffset = 0;
207
- while (currentOffset + fftSamples < channelData.length) {
208
- const segment = channelData.slice(currentOffset, currentOffset + fftSamples);
209
- const spectrum = fft.calculateSpectrum(segment);
210
- const array = new Uint8Array(fftSamples / 2);
211
- let j;
212
- for (j = 0; j < fftSamples / 2; j++) {
213
- array[j] = Math.max(-255, Math.log10(spectrum[j]) * 45);
214
- }
215
- channelFreq.push(array);
216
- // channelFreq: [sample, freq]
217
- currentOffset += fftSamples - noverlap;
218
- }
219
- frequencies.push(channelFreq);
220
- // frequencies: [channel, sample, freq]
221
- }
222
- callback(frequencies, this);
223
- }
224
- loadFrequenciesData(url) {
225
- return fetch(url)
226
- .then((response) => response.json())
227
- .then(this.drawSpectrogram);
228
- }
229
- freqType(freq) {
230
- return freq >= 1000 ? (freq / 1000).toFixed(1) : Math.round(freq);
231
- }
232
- unitType(freq) {
233
- return freq >= 1000 ? 'KHz' : 'Hz';
234
- }
235
- loadLabels(bgFill, fontSizeFreq, fontSizeUnit, fontType, textColorFreq, textColorUnit, textAlign, container) {
236
- const frequenciesHeight = this.height;
237
- bgFill = bgFill || 'rgba(68,68,68,0)';
238
- fontSizeFreq = fontSizeFreq || '12px';
239
- fontSizeUnit = fontSizeUnit || '10px';
240
- fontType = fontType || 'Helvetica';
241
- textColorFreq = textColorFreq || '#fff';
242
- textColorUnit = textColorUnit || '#fff';
243
- textAlign = textAlign || 'center';
244
- container = container || '#specLabels';
245
- const bgWidth = 55;
246
- const getMaxY = frequenciesHeight || 512;
247
- const labelIndex = 5 * (getMaxY / 256);
248
- const freqStart = this.frequencyMin;
249
- const step = (this.frequencyMax - freqStart) / labelIndex;
250
- // prepare canvas element for labels
251
- const ctx = this.labelsEl.getContext('2d');
252
- const dispScale = window.devicePixelRatio;
253
- this.labelsEl.height = this.height * this.channels * dispScale;
254
- this.labelsEl.width = bgWidth * dispScale;
255
- ctx.scale(dispScale, dispScale);
256
- if (!ctx) {
257
- return;
258
- }
259
- for (let c = 0; c < this.channels; c++) {
260
- // for each channel
261
- // fill background
262
- ctx.fillStyle = bgFill;
263
- ctx.fillRect(0, c * getMaxY, bgWidth, (1 + c) * getMaxY);
264
- ctx.fill();
265
- let i;
266
- // render labels
267
- for (i = 0; i <= labelIndex; i++) {
268
- ctx.textAlign = textAlign;
269
- ctx.textBaseline = 'middle';
270
- const freq = freqStart + step * i;
271
- const label = this.freqType(freq);
272
- const units = this.unitType(freq);
273
- const yLabelOffset = 2;
274
- const x = 16;
275
- let y;
276
- if (i == 0) {
277
- y = (1 + c) * getMaxY + i - 10;
278
- // unit label
279
- ctx.fillStyle = textColorUnit;
280
- ctx.font = fontSizeUnit + ' ' + fontType;
281
- ctx.fillText(units, x + 24, y);
282
- // freq label
283
- ctx.fillStyle = textColorFreq;
284
- ctx.font = fontSizeFreq + ' ' + fontType;
285
- ctx.fillText(label, x, y);
286
- }
287
- else {
288
- y = (1 + c) * getMaxY - i * 50 + yLabelOffset;
289
- // unit label
290
- ctx.fillStyle = textColorUnit;
291
- ctx.font = fontSizeUnit + ' ' + fontType;
292
- ctx.fillText(units, x + 24, y);
293
- // freq label
294
- ctx.fillStyle = textColorFreq;
295
- ctx.font = fontSizeFreq + ' ' + fontType;
296
- ctx.fillText(label, x, y);
297
- }
298
- }
299
- }
300
- }
301
- resample(oldMatrix) {
302
- const columnsNumber = this.width;
303
- const newMatrix = [];
304
- const oldPiece = 1 / oldMatrix.length;
305
- const newPiece = 1 / columnsNumber;
306
- let i;
307
- for (i = 0; i < columnsNumber; i++) {
308
- const column = new Array(oldMatrix[0].length);
309
- let j;
310
- for (j = 0; j < oldMatrix.length; j++) {
311
- const oldStart = j * oldPiece;
312
- const oldEnd = oldStart + oldPiece;
313
- const newStart = i * newPiece;
314
- const newEnd = newStart + newPiece;
315
- const overlap = oldEnd <= newStart || newEnd <= oldStart
316
- ? 0
317
- : Math.min(Math.max(oldEnd, newStart), Math.max(newEnd, oldStart)) -
318
- Math.max(Math.min(oldEnd, newStart), Math.min(newEnd, oldStart));
319
- let k;
320
- /* eslint-disable max-depth */
321
- if (overlap > 0) {
322
- for (k = 0; k < oldMatrix[0].length; k++) {
323
- if (column[k] == null) {
324
- column[k] = 0;
325
- }
326
- column[k] += (overlap / newPiece) * oldMatrix[j][k];
327
- }
328
- }
329
- /* eslint-enable max-depth */
330
- }
331
- const intColumn = new Uint8Array(oldMatrix[0].length);
332
- let m;
333
- for (m = 0; m < oldMatrix[0].length; m++) {
334
- intColumn[m] = column[m];
335
- }
336
- newMatrix.push(intColumn);
337
- }
338
- return newMatrix;
339
- }
340
- }
@@ -1,45 +0,0 @@
1
- /**
2
- * The Timeline plugin adds timestamps and notches under the waveform.
3
- */
4
- import BasePlugin from '../base-plugin.js';
5
- export type TimelinePluginOptions = {
6
- /** The height of the timeline in pixels, defaults to 20 */
7
- height?: number;
8
- /** HTML container for the timeline, defaults to wavesufer's container */
9
- container?: HTMLElement;
10
- /** Pass 'beforebegin' to insert the timeline on top of the waveform */
11
- insertPosition?: InsertPosition;
12
- /** The duration of the timeline in seconds, defaults to wavesurfer's duration */
13
- duration?: number;
14
- /** Interval between ticks in seconds */
15
- timeInterval?: number;
16
- /** Interval between numeric labels */
17
- primaryLabelInterval?: number;
18
- /** Interval between secondary numeric labels */
19
- secondaryLabelInterval?: number;
20
- /** Custom inline style to apply to the container */
21
- style?: Partial<CSSStyleDeclaration> | string;
22
- };
23
- declare const defaultOptions: {
24
- height: number;
25
- };
26
- export type TimelinePluginEvents = {
27
- ready: [];
28
- };
29
- declare class TimelinePlugin extends BasePlugin<TimelinePluginEvents, TimelinePluginOptions> {
30
- private timelineWrapper;
31
- protected options: TimelinePluginOptions & typeof defaultOptions;
32
- constructor(options?: TimelinePluginOptions);
33
- static create(options?: TimelinePluginOptions): TimelinePlugin;
34
- /** Called by wavesurfer, don't call manually */
35
- onInit(): void;
36
- /** Unmount */
37
- destroy(): void;
38
- private initTimelineWrapper;
39
- private formatTime;
40
- private defaultTimeInterval;
41
- private defaultPrimaryLabelInterval;
42
- private defaultSecondaryLabelInterval;
43
- private initTimeline;
44
- }
45
- export default TimelinePlugin;