wavesurfer.js 7.0.0-beta.5 → 7.0.0-beta.6
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.
- package/README.md +1 -5
- package/dist/cypress/support/commands.d.ts +1 -0
- package/dist/cypress/support/commands.js +39 -0
- package/dist/cypress/support/e2e.d.ts +1 -0
- package/dist/cypress/support/e2e.js +18 -0
- package/dist/cypress.config.d.ts +2 -0
- package/dist/cypress.config.js +10 -0
- package/dist/decoder.d.ts +1 -1
- package/dist/draggable.d.ts +1 -0
- package/dist/draggable.js +57 -0
- package/dist/plugins/envelope.js +4 -25
- package/dist/plugins/envelope.min.js +1 -1
- package/dist/plugins/minimap.min.js +1 -1
- package/dist/plugins/regions.js +13 -51
- package/dist/plugins/regions.min.js +1 -1
- package/dist/plugins/timeline.min.js +1 -1
- package/dist/renderer.d.ts +1 -0
- package/dist/renderer.js +64 -66
- package/dist/src/base-plugin.d.ts +13 -0
- package/dist/src/base-plugin.js +22 -0
- package/dist/src/decoder.d.ts +9 -0
- package/dist/src/decoder.js +48 -0
- package/dist/src/event-emitter.d.ts +19 -0
- package/dist/src/event-emitter.js +45 -0
- package/dist/src/fetcher.d.ts +5 -0
- package/dist/src/fetcher.js +7 -0
- package/dist/src/player.d.ts +45 -0
- package/dist/src/player.js +114 -0
- package/dist/src/plugins/envelope.d.ts +71 -0
- package/dist/src/plugins/envelope.js +350 -0
- package/dist/src/plugins/minimap.d.ts +39 -0
- package/dist/src/plugins/minimap.js +117 -0
- package/dist/src/plugins/record.d.ts +26 -0
- package/dist/src/plugins/record.js +124 -0
- package/dist/src/plugins/regions.d.ts +93 -0
- package/dist/src/plugins/regions.js +395 -0
- package/dist/src/plugins/spectrogram-fft.d.ts +9 -0
- package/dist/src/plugins/spectrogram-fft.js +150 -0
- package/dist/src/plugins/spectrogram.d.ts +69 -0
- package/dist/src/plugins/spectrogram.js +340 -0
- package/dist/src/plugins/timeline.d.ts +45 -0
- package/dist/src/plugins/timeline.js +162 -0
- package/dist/src/renderer.d.ts +41 -0
- package/dist/src/renderer.js +420 -0
- package/dist/src/timer.d.ts +11 -0
- package/dist/src/timer.js +19 -0
- package/dist/src/wavesurfer.d.ts +149 -0
- package/dist/src/wavesurfer.js +229 -0
- package/dist/wavesurfer.d.ts +5 -1
- package/dist/wavesurfer.js +2 -8
- package/dist/wavesurfer.min.js +1 -1
- package/package.json +4 -1
|
@@ -0,0 +1,69 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,340 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
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;
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Timeline plugin adds timestamps and notches under the waveform.
|
|
3
|
+
*/
|
|
4
|
+
import BasePlugin from '../base-plugin.js';
|
|
5
|
+
const defaultOptions = {
|
|
6
|
+
height: 20,
|
|
7
|
+
};
|
|
8
|
+
class TimelinePlugin extends BasePlugin {
|
|
9
|
+
timelineWrapper;
|
|
10
|
+
options;
|
|
11
|
+
constructor(options) {
|
|
12
|
+
super(options || {});
|
|
13
|
+
this.options = Object.assign({}, defaultOptions, options);
|
|
14
|
+
this.timelineWrapper = this.initTimelineWrapper();
|
|
15
|
+
}
|
|
16
|
+
static create(options) {
|
|
17
|
+
return new TimelinePlugin(options);
|
|
18
|
+
}
|
|
19
|
+
/** Called by wavesurfer, don't call manually */
|
|
20
|
+
onInit() {
|
|
21
|
+
if (!this.wavesurfer) {
|
|
22
|
+
throw Error('WaveSurfer is not initialized');
|
|
23
|
+
}
|
|
24
|
+
const container = this.options.container ?? this.wavesurfer.getWrapper();
|
|
25
|
+
if (this.options.insertPosition) {
|
|
26
|
+
;
|
|
27
|
+
(container.firstElementChild || container).insertAdjacentElement(this.options.insertPosition, this.timelineWrapper);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
container.appendChild(this.timelineWrapper);
|
|
31
|
+
}
|
|
32
|
+
if (this.options.duration) {
|
|
33
|
+
this.initTimeline(this.options.duration);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
this.subscriptions.push(this.wavesurfer.on('redraw', () => {
|
|
37
|
+
this.initTimeline(this.wavesurfer?.getDuration() || 0);
|
|
38
|
+
}));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/** Unmount */
|
|
42
|
+
destroy() {
|
|
43
|
+
this.timelineWrapper.remove();
|
|
44
|
+
super.destroy();
|
|
45
|
+
}
|
|
46
|
+
initTimelineWrapper() {
|
|
47
|
+
const div = document.createElement('div');
|
|
48
|
+
div.setAttribute('part', 'timeline');
|
|
49
|
+
return div;
|
|
50
|
+
}
|
|
51
|
+
formatTime(seconds) {
|
|
52
|
+
if (seconds / 60 > 1) {
|
|
53
|
+
// calculate minutes and seconds from seconds count
|
|
54
|
+
const minutes = Math.floor(seconds / 60);
|
|
55
|
+
seconds = Math.round(seconds % 60);
|
|
56
|
+
const paddedSeconds = `${seconds < 10 ? '0' : ''}${seconds}`;
|
|
57
|
+
return `${minutes}:${paddedSeconds}`;
|
|
58
|
+
}
|
|
59
|
+
const rounded = Math.round(seconds * 1000) / 1000;
|
|
60
|
+
return `${rounded}`;
|
|
61
|
+
}
|
|
62
|
+
// Return how many seconds should be between each notch
|
|
63
|
+
defaultTimeInterval(pxPerSec) {
|
|
64
|
+
if (pxPerSec >= 25) {
|
|
65
|
+
return 1;
|
|
66
|
+
}
|
|
67
|
+
else if (pxPerSec * 5 >= 25) {
|
|
68
|
+
return 5;
|
|
69
|
+
}
|
|
70
|
+
else if (pxPerSec * 15 >= 25) {
|
|
71
|
+
return 15;
|
|
72
|
+
}
|
|
73
|
+
return Math.ceil(0.5 / pxPerSec) * 60;
|
|
74
|
+
}
|
|
75
|
+
// Return the cadence of notches that get labels in the primary color.
|
|
76
|
+
defaultPrimaryLabelInterval(pxPerSec) {
|
|
77
|
+
if (pxPerSec >= 25) {
|
|
78
|
+
return 10;
|
|
79
|
+
}
|
|
80
|
+
else if (pxPerSec * 5 >= 25) {
|
|
81
|
+
return 6;
|
|
82
|
+
}
|
|
83
|
+
else if (pxPerSec * 15 >= 25) {
|
|
84
|
+
return 4;
|
|
85
|
+
}
|
|
86
|
+
return 4;
|
|
87
|
+
}
|
|
88
|
+
// Return the cadence of notches that get labels in the secondary color.
|
|
89
|
+
defaultSecondaryLabelInterval(pxPerSec) {
|
|
90
|
+
if (pxPerSec >= 25) {
|
|
91
|
+
return 5;
|
|
92
|
+
}
|
|
93
|
+
else if (pxPerSec * 5 >= 25) {
|
|
94
|
+
return 2;
|
|
95
|
+
}
|
|
96
|
+
else if (pxPerSec * 15 >= 25) {
|
|
97
|
+
return 2;
|
|
98
|
+
}
|
|
99
|
+
return 2;
|
|
100
|
+
}
|
|
101
|
+
initTimeline(duration) {
|
|
102
|
+
const pxPerSec = this.timelineWrapper.scrollWidth / duration;
|
|
103
|
+
const timeInterval = this.options.timeInterval ?? this.defaultTimeInterval(pxPerSec);
|
|
104
|
+
const primaryLabelInterval = this.options.primaryLabelInterval ?? this.defaultPrimaryLabelInterval(pxPerSec);
|
|
105
|
+
const secondaryLabelInterval = this.options.secondaryLabelInterval ?? this.defaultSecondaryLabelInterval(pxPerSec);
|
|
106
|
+
const isTop = this.options.insertPosition === 'beforebegin';
|
|
107
|
+
const timeline = document.createElement('div');
|
|
108
|
+
timeline.setAttribute('style', `
|
|
109
|
+
height: ${this.options.height}px;
|
|
110
|
+
overflow: hidden;
|
|
111
|
+
display: flex;
|
|
112
|
+
justify-content: space-between;
|
|
113
|
+
align-items: ${isTop ? 'flex-start' : 'flex-end'};
|
|
114
|
+
font-size: ${this.options.height / 2}px;
|
|
115
|
+
white-space: nowrap;
|
|
116
|
+
`);
|
|
117
|
+
if (isTop) {
|
|
118
|
+
const topStyle = `
|
|
119
|
+
position: absolute;
|
|
120
|
+
top: 0;
|
|
121
|
+
left: 0;
|
|
122
|
+
right: 0;
|
|
123
|
+
z-index: 2;
|
|
124
|
+
`;
|
|
125
|
+
timeline.setAttribute('style', timeline.getAttribute('style') + topStyle);
|
|
126
|
+
}
|
|
127
|
+
if (typeof this.options.style === 'string') {
|
|
128
|
+
timeline.setAttribute('style', timeline.getAttribute('style') + this.options.style);
|
|
129
|
+
}
|
|
130
|
+
else if (typeof this.options.style === 'object') {
|
|
131
|
+
Object.assign(timeline.style, this.options.style);
|
|
132
|
+
}
|
|
133
|
+
const notchEl = document.createElement('div');
|
|
134
|
+
notchEl.setAttribute('style', `
|
|
135
|
+
width: 1px;
|
|
136
|
+
height: 50%;
|
|
137
|
+
display: flex;
|
|
138
|
+
flex-direction: column;
|
|
139
|
+
justify-content: ${isTop ? 'flex-start' : 'flex-end'};
|
|
140
|
+
overflow: visible;
|
|
141
|
+
border-left: 1px solid currentColor;
|
|
142
|
+
opacity: 0.25;
|
|
143
|
+
`);
|
|
144
|
+
for (let i = 0; i < duration; i += timeInterval) {
|
|
145
|
+
const notch = notchEl.cloneNode();
|
|
146
|
+
const isPrimary = (Math.round(i * 100) / 100) % primaryLabelInterval === 0;
|
|
147
|
+
const isSecondary = (Math.round(i * 100) / 100) % secondaryLabelInterval === 0;
|
|
148
|
+
if (isPrimary || isSecondary) {
|
|
149
|
+
notch.style.height = '100%';
|
|
150
|
+
notch.style.textIndent = '3px';
|
|
151
|
+
notch.textContent = this.formatTime(i);
|
|
152
|
+
if (isPrimary)
|
|
153
|
+
notch.style.opacity = '1';
|
|
154
|
+
}
|
|
155
|
+
timeline.appendChild(notch);
|
|
156
|
+
}
|
|
157
|
+
this.timelineWrapper.innerHTML = '';
|
|
158
|
+
this.timelineWrapper.appendChild(timeline);
|
|
159
|
+
this.emit('ready');
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
export default TimelinePlugin;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import EventEmitter from './event-emitter.js';
|
|
2
|
+
import type { WaveSurferOptions } from './wavesurfer.js';
|
|
3
|
+
type RendererEvents = {
|
|
4
|
+
click: [relativeX: number];
|
|
5
|
+
drag: [relativeX: number];
|
|
6
|
+
scroll: [relativeStart: number, relativeEnd: number];
|
|
7
|
+
render: [];
|
|
8
|
+
};
|
|
9
|
+
declare class Renderer extends EventEmitter<RendererEvents> {
|
|
10
|
+
private static MAX_CANVAS_WIDTH;
|
|
11
|
+
private options;
|
|
12
|
+
private container;
|
|
13
|
+
private scrollContainer;
|
|
14
|
+
private wrapper;
|
|
15
|
+
private canvasWrapper;
|
|
16
|
+
private progressWrapper;
|
|
17
|
+
private cursor;
|
|
18
|
+
private timeouts;
|
|
19
|
+
private isScrolling;
|
|
20
|
+
private audioData;
|
|
21
|
+
private resizeObserver;
|
|
22
|
+
private isDragging;
|
|
23
|
+
constructor(options: WaveSurferOptions);
|
|
24
|
+
private initEvents;
|
|
25
|
+
private initDrag;
|
|
26
|
+
private initHtml;
|
|
27
|
+
setOptions(options: WaveSurferOptions): void;
|
|
28
|
+
getWrapper(): HTMLElement;
|
|
29
|
+
getScroll(): number;
|
|
30
|
+
destroy(): void;
|
|
31
|
+
private createDelay;
|
|
32
|
+
private convertColorValues;
|
|
33
|
+
private renderSingleCanvas;
|
|
34
|
+
private renderWaveform;
|
|
35
|
+
render(audioData: AudioBuffer): void;
|
|
36
|
+
reRender(): void;
|
|
37
|
+
zoom(minPxPerSec: number): void;
|
|
38
|
+
private scrollIntoView;
|
|
39
|
+
renderProgress(progress: number, isPlaying?: boolean): void;
|
|
40
|
+
}
|
|
41
|
+
export default Renderer;
|