wavesurfer.js 7.8.4-beta.0 → 7.8.4-beta.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.
- package/dist/base-plugin.js +3 -1
- package/dist/decoder.js +5 -16
- package/dist/dom.js +1 -1
- package/dist/draggable.js +2 -2
- package/dist/event-emitter.js +3 -6
- package/dist/fetcher.js +37 -50
- package/dist/player.js +4 -14
- package/dist/plugins/envelope.js +355 -1
- package/dist/plugins/hover.js +106 -1
- package/dist/plugins/record.cjs +1 -1
- package/dist/plugins/record.esm.js +1 -1
- package/dist/plugins/record.js +1 -1
- package/dist/plugins/record.min.js +1 -1
- package/dist/renderer.js +88 -108
- package/dist/timer.js +1 -4
- package/dist/wavesurfer.js +76 -95
- package/dist/webaudio.js +33 -50
- package/package.json +1 -1
package/dist/plugins/hover.js
CHANGED
|
@@ -1 +1,106 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* The Hover plugin follows the mouse and shows a timestamp
|
|
3
|
+
*/
|
|
4
|
+
import BasePlugin from '../base-plugin.js';
|
|
5
|
+
import createElement from '../dom.js';
|
|
6
|
+
const defaultOptions = {
|
|
7
|
+
lineWidth: 1,
|
|
8
|
+
labelSize: 11,
|
|
9
|
+
formatTimeCallback(seconds) {
|
|
10
|
+
const minutes = Math.floor(seconds / 60);
|
|
11
|
+
const secondsRemainder = Math.floor(seconds) % 60;
|
|
12
|
+
const paddedSeconds = `0${secondsRemainder}`.slice(-2);
|
|
13
|
+
return `${minutes}:${paddedSeconds}`;
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
class HoverPlugin extends BasePlugin {
|
|
17
|
+
options;
|
|
18
|
+
wrapper;
|
|
19
|
+
label;
|
|
20
|
+
unsubscribe = () => undefined;
|
|
21
|
+
constructor(options) {
|
|
22
|
+
super(options || {});
|
|
23
|
+
this.options = Object.assign({}, defaultOptions, options);
|
|
24
|
+
// Create the plugin elements
|
|
25
|
+
this.wrapper = createElement('div', { part: 'hover' });
|
|
26
|
+
this.label = createElement('span', { part: 'hover-label' }, this.wrapper);
|
|
27
|
+
}
|
|
28
|
+
static create(options) {
|
|
29
|
+
return new HoverPlugin(options);
|
|
30
|
+
}
|
|
31
|
+
addUnits(value) {
|
|
32
|
+
const units = typeof value === 'number' ? 'px' : '';
|
|
33
|
+
return `${value}${units}`;
|
|
34
|
+
}
|
|
35
|
+
/** Called by wavesurfer, don't call manually */
|
|
36
|
+
onInit() {
|
|
37
|
+
if (!this.wavesurfer) {
|
|
38
|
+
throw Error('WaveSurfer is not initialized');
|
|
39
|
+
}
|
|
40
|
+
const wsOptions = this.wavesurfer.options;
|
|
41
|
+
const lineColor = this.options.lineColor || wsOptions.cursorColor || wsOptions.progressColor;
|
|
42
|
+
// Vertical line
|
|
43
|
+
Object.assign(this.wrapper.style, {
|
|
44
|
+
position: 'absolute',
|
|
45
|
+
zIndex: 10,
|
|
46
|
+
left: 0,
|
|
47
|
+
top: 0,
|
|
48
|
+
height: '100%',
|
|
49
|
+
pointerEvents: 'none',
|
|
50
|
+
borderLeft: `${this.addUnits(this.options.lineWidth)} solid ${lineColor}`,
|
|
51
|
+
opacity: '0',
|
|
52
|
+
transition: 'opacity .1s ease-in',
|
|
53
|
+
});
|
|
54
|
+
// Timestamp label
|
|
55
|
+
Object.assign(this.label.style, {
|
|
56
|
+
display: 'block',
|
|
57
|
+
backgroundColor: this.options.labelBackground,
|
|
58
|
+
color: this.options.labelColor,
|
|
59
|
+
fontSize: `${this.addUnits(this.options.labelSize)}`,
|
|
60
|
+
transition: 'transform .1s ease-in',
|
|
61
|
+
padding: '2px 3px',
|
|
62
|
+
});
|
|
63
|
+
// Append the wrapper
|
|
64
|
+
const container = this.wavesurfer.getWrapper();
|
|
65
|
+
container.appendChild(this.wrapper);
|
|
66
|
+
// Attach pointer events
|
|
67
|
+
container.addEventListener('pointermove', this.onPointerMove);
|
|
68
|
+
container.addEventListener('pointerleave', this.onPointerLeave);
|
|
69
|
+
container.addEventListener('wheel', this.onPointerMove);
|
|
70
|
+
this.unsubscribe = () => {
|
|
71
|
+
container.removeEventListener('pointermove', this.onPointerMove);
|
|
72
|
+
container.removeEventListener('pointerleave', this.onPointerLeave);
|
|
73
|
+
container.removeEventListener('wheel', this.onPointerLeave);
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
onPointerMove = (e) => {
|
|
77
|
+
if (!this.wavesurfer)
|
|
78
|
+
return;
|
|
79
|
+
// Position
|
|
80
|
+
const bbox = this.wavesurfer.getWrapper().getBoundingClientRect();
|
|
81
|
+
const { width } = bbox;
|
|
82
|
+
const offsetX = e.clientX - bbox.left;
|
|
83
|
+
const relX = Math.min(1, Math.max(0, offsetX / width));
|
|
84
|
+
const posX = Math.min(width - this.options.lineWidth - 1, offsetX);
|
|
85
|
+
this.wrapper.style.transform = `translateX(${posX}px)`;
|
|
86
|
+
this.wrapper.style.opacity = '1';
|
|
87
|
+
// Timestamp
|
|
88
|
+
const duration = this.wavesurfer.getDuration() || 0;
|
|
89
|
+
this.label.textContent = this.options.formatTimeCallback(duration * relX);
|
|
90
|
+
const labelWidth = this.label.offsetWidth;
|
|
91
|
+
this.label.style.transform =
|
|
92
|
+
posX + labelWidth > width ? `translateX(-${labelWidth + this.options.lineWidth}px)` : '';
|
|
93
|
+
// Emit a hover event with the relative X position
|
|
94
|
+
this.emit('hover', relX);
|
|
95
|
+
};
|
|
96
|
+
onPointerLeave = () => {
|
|
97
|
+
this.wrapper.style.opacity = '0';
|
|
98
|
+
};
|
|
99
|
+
/** Unmount */
|
|
100
|
+
destroy() {
|
|
101
|
+
super.destroy();
|
|
102
|
+
this.unsubscribe();
|
|
103
|
+
this.wrapper.remove();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
export default HoverPlugin;
|
package/dist/plugins/record.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";function i(i,t,e,s){return new(e||(e=Promise))((function(o,r){function n(i){try{d(s.next(i))}catch(i){r(i)}}function a(i){try{d(s.throw(i))}catch(i){r(i)}}function d(i){var t;i.done?o(i.value):(t=i.value,t instanceof e?t:new e((function(i){i(t)}))).then(n,a)}d((s=s.apply(i,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;class t{constructor(){this.listeners={}}on(i,t,e){if(this.listeners[i]||(this.listeners[i]=new Set),this.listeners[i].add(t),null==e?void 0:e.once){const e=()=>{this.un(i,e),this.un(i,t)};return this.on(i,e),e}return()=>this.un(i,t)}un(i,t){var e;null===(e=this.listeners[i])||void 0===e||e.delete(t)}once(i,t){return this.on(i,t,{once:!0})}unAll(){this.listeners={}}emit(i,...t){this.listeners[i]&&this.listeners[i].forEach((i=>i(...t)))}}class e extends t{constructor(i){super(),this.subscriptions=[],this.options=i}onInit(){}_init(i){this.wavesurfer=i,this.onInit()}destroy(){this.emit("destroy"),this.subscriptions.forEach((i=>i()))}}class s extends t{constructor(){super(...arguments),this.unsubscribe=()=>{}}start(){this.unsubscribe=this.on("tick",(()=>{requestAnimationFrame((()=>{this.emit("tick")}))})),this.emit("tick")}stop(){this.unsubscribe()}destroy(){this.unsubscribe()}}const o=["audio/webm","audio/wav","audio/mpeg","audio/mp4","audio/mp3"];class
|
|
1
|
+
"use strict";function i(i,t,e,s){return new(e||(e=Promise))((function(o,r){function n(i){try{d(s.next(i))}catch(i){r(i)}}function a(i){try{d(s.throw(i))}catch(i){r(i)}}function d(i){var t;i.done?o(i.value):(t=i.value,t instanceof e?t:new e((function(i){i(t)}))).then(n,a)}d((s=s.apply(i,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;class t{constructor(){this.listeners={}}on(i,t,e){if(this.listeners[i]||(this.listeners[i]=new Set),this.listeners[i].add(t),null==e?void 0:e.once){const e=()=>{this.un(i,e),this.un(i,t)};return this.on(i,e),e}return()=>this.un(i,t)}un(i,t){var e;null===(e=this.listeners[i])||void 0===e||e.delete(t)}once(i,t){return this.on(i,t,{once:!0})}unAll(){this.listeners={}}emit(i,...t){this.listeners[i]&&this.listeners[i].forEach((i=>i(...t)))}}class e extends t{constructor(i){super(),this.subscriptions=[],this.options=i}onInit(){}_init(i){this.wavesurfer=i,this.onInit()}destroy(){this.emit("destroy"),this.subscriptions.forEach((i=>i()))}}class s extends t{constructor(){super(...arguments),this.unsubscribe=()=>{}}start(){this.unsubscribe=this.on("tick",(()=>{requestAnimationFrame((()=>{this.emit("tick")}))})),this.emit("tick")}stop(){this.unsubscribe()}destroy(){this.unsubscribe()}}const o=["audio/webm","audio/wav","audio/mpeg","audio/mp4","audio/mp3"],r=i=>setTimeout(i,1e3/60);class n extends e{constructor(i){var t,e,o,r,n,a;super(Object.assign(Object.assign({},i),{audioBitsPerSecond:null!==(t=i.audioBitsPerSecond)&&void 0!==t?t:128e3,scrollingWaveform:null!==(e=i.scrollingWaveform)&&void 0!==e&&e,scrollingWaveformWindow:null!==(o=i.scrollingWaveformWindow)&&void 0!==o?o:5,continuousWaveform:null!==(r=i.continuousWaveform)&&void 0!==r&&r,renderRecordedAudio:null===(n=i.renderRecordedAudio)||void 0===n||n,mediaRecorderTimeslice:null!==(a=i.mediaRecorderTimeslice)&&void 0!==a?a:void 0})),this.stream=null,this.mediaRecorder=null,this.dataWindow=null,this.isWaveformPaused=!1,this.lastStartTime=0,this.lastDuration=0,this.duration=0,this.timer=new s,this.subscriptions.push(this.timer.on("tick",(()=>{const i=performance.now()-this.lastStartTime;this.duration=this.isPaused()?this.duration:this.lastDuration+i,this.emit("record-progress",this.duration)})))}static create(i){return new n(i||{})}renderMicStream(i){var t;const e=new AudioContext,s=e.createMediaStreamSource(i),o=e.createAnalyser();s.connect(o);const n=o.frequencyBinCount,a=new Float32Array(n);let d,c=0;this.wavesurfer&&(null!==(t=this.originalOptions)&&void 0!==t||(this.originalOptions=Object.assign({},this.wavesurfer.options)),this.wavesurfer.options.interact=!1,this.options.scrollingWaveform&&(this.wavesurfer.options.cursorWidth=0));const u=()=>{var i,t,s,h;if(this.isWaveformPaused)d=r(u);else{if(o.getFloatTimeDomainData(a),this.options.scrollingWaveform){const i=Math.floor((this.options.scrollingWaveformWindow||0)*e.sampleRate),t=Math.min(i,this.dataWindow?this.dataWindow.length+n:n),s=new Float32Array(i);if(this.dataWindow){const e=Math.max(0,i-this.dataWindow.length);s.set(this.dataWindow.slice(-t+n),e)}s.set(a,i-n),this.dataWindow=s}else if(this.options.continuousWaveform){if(!this.dataWindow){const e=this.options.continuousWaveformDuration?Math.round(60*this.options.continuousWaveformDuration):(null!==(t=null===(i=this.wavesurfer)||void 0===i?void 0:i.getWidth())&&void 0!==t?t:0)*window.devicePixelRatio;this.dataWindow=new Float32Array(e)}const e=Math.max(...a);if(c+1>this.dataWindow.length){const i=new Float32Array(2*this.dataWindow.length);i.set(this.dataWindow,0),this.dataWindow=i}this.dataWindow.set([e],c),c++}else this.dataWindow=a;if(this.wavesurfer){const i=(null!==(h=null===(s=this.dataWindow)||void 0===s?void 0:s.length)&&void 0!==h?h:0)/60;this.wavesurfer.load("",[this.dataWindow],this.options.scrollingWaveform?this.options.scrollingWaveformWindow:i).then((()=>{this.wavesurfer&&this.options.continuousWaveform&&(this.wavesurfer.setTime(this.getDuration()/1e3),this.wavesurfer.options.minPxPerSec||this.wavesurfer.setOptions({minPxPerSec:this.wavesurfer.getWidth()/this.wavesurfer.getDuration()}))})).catch((i=>{console.error("Error rendering real-time recording data:",i)}))}d=r(u)}};return u(),{onDestroy:()=>{cancelAnimationFrame(d),null==s||s.disconnect(),null==e||e.close()},onEnd:()=>{this.isWaveformPaused=!0,cancelAnimationFrame(d),this.stopMic()}}}startMic(t){return i(this,void 0,void 0,(function*(){let i;try{i=yield navigator.mediaDevices.getUserMedia({audio:!(null==t?void 0:t.deviceId)||{deviceId:t.deviceId}})}catch(i){throw new Error("Error accessing the microphone: "+i.message)}const{onDestroy:e,onEnd:s}=this.renderMicStream(i);return this.subscriptions.push(this.once("destroy",e)),this.subscriptions.push(this.once("record-end",s)),this.stream=i,i}))}stopMic(){this.stream&&(this.stream.getTracks().forEach((i=>i.stop())),this.stream=null,this.mediaRecorder=null)}startRecording(t){return i(this,void 0,void 0,(function*(){const i=this.stream||(yield this.startMic(t));this.dataWindow=null;const e=this.mediaRecorder||new MediaRecorder(i,{mimeType:this.options.mimeType||o.find((i=>MediaRecorder.isTypeSupported(i))),audioBitsPerSecond:this.options.audioBitsPerSecond});this.mediaRecorder=e,this.stopRecording();const s=[];e.ondataavailable=i=>{i.data.size>0&&s.push(i.data),this.emit("record-data-available",i.data)};const r=i=>{var t;const o=new Blob(s,{type:e.mimeType});this.emit(i,o),this.options.renderRecordedAudio&&(this.applyOriginalOptionsIfNeeded(),null===(t=this.wavesurfer)||void 0===t||t.load(URL.createObjectURL(o)))};e.onpause=()=>r("record-pause"),e.onstop=()=>r("record-end"),e.start(this.options.mediaRecorderTimeslice),this.lastStartTime=performance.now(),this.lastDuration=0,this.duration=0,this.isWaveformPaused=!1,this.timer.start(),this.emit("record-start")}))}getDuration(){return this.duration}isRecording(){var i;return"recording"===(null===(i=this.mediaRecorder)||void 0===i?void 0:i.state)}isPaused(){var i;return"paused"===(null===(i=this.mediaRecorder)||void 0===i?void 0:i.state)}isActive(){var i;return"inactive"!==(null===(i=this.mediaRecorder)||void 0===i?void 0:i.state)}stopRecording(){var i;this.isActive()&&(null===(i=this.mediaRecorder)||void 0===i||i.stop(),this.timer.stop())}pauseRecording(){var i,t;this.isRecording()&&(this.isWaveformPaused=!0,null===(i=this.mediaRecorder)||void 0===i||i.requestData(),null===(t=this.mediaRecorder)||void 0===t||t.pause(),this.timer.stop(),this.lastDuration=this.duration)}resumeRecording(){var i;this.isPaused()&&(this.isWaveformPaused=!1,null===(i=this.mediaRecorder)||void 0===i||i.resume(),this.timer.start(),this.lastStartTime=performance.now(),this.emit("record-resume"))}static getAvailableAudioDevices(){return i(this,void 0,void 0,(function*(){return navigator.mediaDevices.enumerateDevices().then((i=>i.filter((i=>"audioinput"===i.kind))))}))}destroy(){this.applyOriginalOptionsIfNeeded(),super.destroy(),this.stopRecording(),this.stopMic()}applyOriginalOptionsIfNeeded(){this.wavesurfer&&this.originalOptions&&(this.wavesurfer.setOptions(this.originalOptions),delete this.originalOptions)}}module.exports=n;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
function i(i,t,e,s){return new(e||(e=Promise))((function(o,r){function n(i){try{d(s.next(i))}catch(i){r(i)}}function a(i){try{d(s.throw(i))}catch(i){r(i)}}function d(i){var t;i.done?o(i.value):(t=i.value,t instanceof e?t:new e((function(i){i(t)}))).then(n,a)}d((s=s.apply(i,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;class t{constructor(){this.listeners={}}on(i,t,e){if(this.listeners[i]||(this.listeners[i]=new Set),this.listeners[i].add(t),null==e?void 0:e.once){const e=()=>{this.un(i,e),this.un(i,t)};return this.on(i,e),e}return()=>this.un(i,t)}un(i,t){var e;null===(e=this.listeners[i])||void 0===e||e.delete(t)}once(i,t){return this.on(i,t,{once:!0})}unAll(){this.listeners={}}emit(i,...t){this.listeners[i]&&this.listeners[i].forEach((i=>i(...t)))}}class e extends t{constructor(i){super(),this.subscriptions=[],this.options=i}onInit(){}_init(i){this.wavesurfer=i,this.onInit()}destroy(){this.emit("destroy"),this.subscriptions.forEach((i=>i()))}}class s extends t{constructor(){super(...arguments),this.unsubscribe=()=>{}}start(){this.unsubscribe=this.on("tick",(()=>{requestAnimationFrame((()=>{this.emit("tick")}))})),this.emit("tick")}stop(){this.unsubscribe()}destroy(){this.unsubscribe()}}const o=["audio/webm","audio/wav","audio/mpeg","audio/mp4","audio/mp3"];class
|
|
1
|
+
function i(i,t,e,s){return new(e||(e=Promise))((function(o,r){function n(i){try{d(s.next(i))}catch(i){r(i)}}function a(i){try{d(s.throw(i))}catch(i){r(i)}}function d(i){var t;i.done?o(i.value):(t=i.value,t instanceof e?t:new e((function(i){i(t)}))).then(n,a)}d((s=s.apply(i,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;class t{constructor(){this.listeners={}}on(i,t,e){if(this.listeners[i]||(this.listeners[i]=new Set),this.listeners[i].add(t),null==e?void 0:e.once){const e=()=>{this.un(i,e),this.un(i,t)};return this.on(i,e),e}return()=>this.un(i,t)}un(i,t){var e;null===(e=this.listeners[i])||void 0===e||e.delete(t)}once(i,t){return this.on(i,t,{once:!0})}unAll(){this.listeners={}}emit(i,...t){this.listeners[i]&&this.listeners[i].forEach((i=>i(...t)))}}class e extends t{constructor(i){super(),this.subscriptions=[],this.options=i}onInit(){}_init(i){this.wavesurfer=i,this.onInit()}destroy(){this.emit("destroy"),this.subscriptions.forEach((i=>i()))}}class s extends t{constructor(){super(...arguments),this.unsubscribe=()=>{}}start(){this.unsubscribe=this.on("tick",(()=>{requestAnimationFrame((()=>{this.emit("tick")}))})),this.emit("tick")}stop(){this.unsubscribe()}destroy(){this.unsubscribe()}}const o=["audio/webm","audio/wav","audio/mpeg","audio/mp4","audio/mp3"],r=i=>setTimeout(i,1e3/60);class n extends e{constructor(i){var t,e,o,r,n,a;super(Object.assign(Object.assign({},i),{audioBitsPerSecond:null!==(t=i.audioBitsPerSecond)&&void 0!==t?t:128e3,scrollingWaveform:null!==(e=i.scrollingWaveform)&&void 0!==e&&e,scrollingWaveformWindow:null!==(o=i.scrollingWaveformWindow)&&void 0!==o?o:5,continuousWaveform:null!==(r=i.continuousWaveform)&&void 0!==r&&r,renderRecordedAudio:null===(n=i.renderRecordedAudio)||void 0===n||n,mediaRecorderTimeslice:null!==(a=i.mediaRecorderTimeslice)&&void 0!==a?a:void 0})),this.stream=null,this.mediaRecorder=null,this.dataWindow=null,this.isWaveformPaused=!1,this.lastStartTime=0,this.lastDuration=0,this.duration=0,this.timer=new s,this.subscriptions.push(this.timer.on("tick",(()=>{const i=performance.now()-this.lastStartTime;this.duration=this.isPaused()?this.duration:this.lastDuration+i,this.emit("record-progress",this.duration)})))}static create(i){return new n(i||{})}renderMicStream(i){var t;const e=new AudioContext,s=e.createMediaStreamSource(i),o=e.createAnalyser();s.connect(o);const n=o.frequencyBinCount,a=new Float32Array(n);let d,c=0;this.wavesurfer&&(null!==(t=this.originalOptions)&&void 0!==t||(this.originalOptions=Object.assign({},this.wavesurfer.options)),this.wavesurfer.options.interact=!1,this.options.scrollingWaveform&&(this.wavesurfer.options.cursorWidth=0));const h=()=>{var i,t,s,u;if(this.isWaveformPaused)d=r(h);else{if(o.getFloatTimeDomainData(a),this.options.scrollingWaveform){const i=Math.floor((this.options.scrollingWaveformWindow||0)*e.sampleRate),t=Math.min(i,this.dataWindow?this.dataWindow.length+n:n),s=new Float32Array(i);if(this.dataWindow){const e=Math.max(0,i-this.dataWindow.length);s.set(this.dataWindow.slice(-t+n),e)}s.set(a,i-n),this.dataWindow=s}else if(this.options.continuousWaveform){if(!this.dataWindow){const e=this.options.continuousWaveformDuration?Math.round(60*this.options.continuousWaveformDuration):(null!==(t=null===(i=this.wavesurfer)||void 0===i?void 0:i.getWidth())&&void 0!==t?t:0)*window.devicePixelRatio;this.dataWindow=new Float32Array(e)}const e=Math.max(...a);if(c+1>this.dataWindow.length){const i=new Float32Array(2*this.dataWindow.length);i.set(this.dataWindow,0),this.dataWindow=i}this.dataWindow.set([e],c),c++}else this.dataWindow=a;if(this.wavesurfer){const i=(null!==(u=null===(s=this.dataWindow)||void 0===s?void 0:s.length)&&void 0!==u?u:0)/60;this.wavesurfer.load("",[this.dataWindow],this.options.scrollingWaveform?this.options.scrollingWaveformWindow:i).then((()=>{this.wavesurfer&&this.options.continuousWaveform&&(this.wavesurfer.setTime(this.getDuration()/1e3),this.wavesurfer.options.minPxPerSec||this.wavesurfer.setOptions({minPxPerSec:this.wavesurfer.getWidth()/this.wavesurfer.getDuration()}))})).catch((i=>{console.error("Error rendering real-time recording data:",i)}))}d=r(h)}};return h(),{onDestroy:()=>{cancelAnimationFrame(d),null==s||s.disconnect(),null==e||e.close()},onEnd:()=>{this.isWaveformPaused=!0,cancelAnimationFrame(d),this.stopMic()}}}startMic(t){return i(this,void 0,void 0,(function*(){let i;try{i=yield navigator.mediaDevices.getUserMedia({audio:!(null==t?void 0:t.deviceId)||{deviceId:t.deviceId}})}catch(i){throw new Error("Error accessing the microphone: "+i.message)}const{onDestroy:e,onEnd:s}=this.renderMicStream(i);return this.subscriptions.push(this.once("destroy",e)),this.subscriptions.push(this.once("record-end",s)),this.stream=i,i}))}stopMic(){this.stream&&(this.stream.getTracks().forEach((i=>i.stop())),this.stream=null,this.mediaRecorder=null)}startRecording(t){return i(this,void 0,void 0,(function*(){const i=this.stream||(yield this.startMic(t));this.dataWindow=null;const e=this.mediaRecorder||new MediaRecorder(i,{mimeType:this.options.mimeType||o.find((i=>MediaRecorder.isTypeSupported(i))),audioBitsPerSecond:this.options.audioBitsPerSecond});this.mediaRecorder=e,this.stopRecording();const s=[];e.ondataavailable=i=>{i.data.size>0&&s.push(i.data),this.emit("record-data-available",i.data)};const r=i=>{var t;const o=new Blob(s,{type:e.mimeType});this.emit(i,o),this.options.renderRecordedAudio&&(this.applyOriginalOptionsIfNeeded(),null===(t=this.wavesurfer)||void 0===t||t.load(URL.createObjectURL(o)))};e.onpause=()=>r("record-pause"),e.onstop=()=>r("record-end"),e.start(this.options.mediaRecorderTimeslice),this.lastStartTime=performance.now(),this.lastDuration=0,this.duration=0,this.isWaveformPaused=!1,this.timer.start(),this.emit("record-start")}))}getDuration(){return this.duration}isRecording(){var i;return"recording"===(null===(i=this.mediaRecorder)||void 0===i?void 0:i.state)}isPaused(){var i;return"paused"===(null===(i=this.mediaRecorder)||void 0===i?void 0:i.state)}isActive(){var i;return"inactive"!==(null===(i=this.mediaRecorder)||void 0===i?void 0:i.state)}stopRecording(){var i;this.isActive()&&(null===(i=this.mediaRecorder)||void 0===i||i.stop(),this.timer.stop())}pauseRecording(){var i,t;this.isRecording()&&(this.isWaveformPaused=!0,null===(i=this.mediaRecorder)||void 0===i||i.requestData(),null===(t=this.mediaRecorder)||void 0===t||t.pause(),this.timer.stop(),this.lastDuration=this.duration)}resumeRecording(){var i;this.isPaused()&&(this.isWaveformPaused=!1,null===(i=this.mediaRecorder)||void 0===i||i.resume(),this.timer.start(),this.lastStartTime=performance.now(),this.emit("record-resume"))}static getAvailableAudioDevices(){return i(this,void 0,void 0,(function*(){return navigator.mediaDevices.enumerateDevices().then((i=>i.filter((i=>"audioinput"===i.kind))))}))}destroy(){this.applyOriginalOptionsIfNeeded(),super.destroy(),this.stopRecording(),this.stopMic()}applyOriginalOptionsIfNeeded(){this.wavesurfer&&this.originalOptions&&(this.wavesurfer.setOptions(this.originalOptions),delete this.originalOptions)}}export{n as default};
|
package/dist/plugins/record.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
function i(i,t,e,s){return new(e||(e=Promise))((function(o,r){function n(i){try{d(s.next(i))}catch(i){r(i)}}function a(i){try{d(s.throw(i))}catch(i){r(i)}}function d(i){var t;i.done?o(i.value):(t=i.value,t instanceof e?t:new e((function(i){i(t)}))).then(n,a)}d((s=s.apply(i,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;class t{constructor(){this.listeners={}}on(i,t,e){if(this.listeners[i]||(this.listeners[i]=new Set),this.listeners[i].add(t),null==e?void 0:e.once){const e=()=>{this.un(i,e),this.un(i,t)};return this.on(i,e),e}return()=>this.un(i,t)}un(i,t){var e;null===(e=this.listeners[i])||void 0===e||e.delete(t)}once(i,t){return this.on(i,t,{once:!0})}unAll(){this.listeners={}}emit(i,...t){this.listeners[i]&&this.listeners[i].forEach((i=>i(...t)))}}class e extends t{constructor(i){super(),this.subscriptions=[],this.options=i}onInit(){}_init(i){this.wavesurfer=i,this.onInit()}destroy(){this.emit("destroy"),this.subscriptions.forEach((i=>i()))}}class s extends t{constructor(){super(...arguments),this.unsubscribe=()=>{}}start(){this.unsubscribe=this.on("tick",(()=>{requestAnimationFrame((()=>{this.emit("tick")}))})),this.emit("tick")}stop(){this.unsubscribe()}destroy(){this.unsubscribe()}}const o=["audio/webm","audio/wav","audio/mpeg","audio/mp4","audio/mp3"];class
|
|
1
|
+
function i(i,t,e,s){return new(e||(e=Promise))((function(o,r){function n(i){try{d(s.next(i))}catch(i){r(i)}}function a(i){try{d(s.throw(i))}catch(i){r(i)}}function d(i){var t;i.done?o(i.value):(t=i.value,t instanceof e?t:new e((function(i){i(t)}))).then(n,a)}d((s=s.apply(i,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;class t{constructor(){this.listeners={}}on(i,t,e){if(this.listeners[i]||(this.listeners[i]=new Set),this.listeners[i].add(t),null==e?void 0:e.once){const e=()=>{this.un(i,e),this.un(i,t)};return this.on(i,e),e}return()=>this.un(i,t)}un(i,t){var e;null===(e=this.listeners[i])||void 0===e||e.delete(t)}once(i,t){return this.on(i,t,{once:!0})}unAll(){this.listeners={}}emit(i,...t){this.listeners[i]&&this.listeners[i].forEach((i=>i(...t)))}}class e extends t{constructor(i){super(),this.subscriptions=[],this.options=i}onInit(){}_init(i){this.wavesurfer=i,this.onInit()}destroy(){this.emit("destroy"),this.subscriptions.forEach((i=>i()))}}class s extends t{constructor(){super(...arguments),this.unsubscribe=()=>{}}start(){this.unsubscribe=this.on("tick",(()=>{requestAnimationFrame((()=>{this.emit("tick")}))})),this.emit("tick")}stop(){this.unsubscribe()}destroy(){this.unsubscribe()}}const o=["audio/webm","audio/wav","audio/mpeg","audio/mp4","audio/mp3"],r=i=>setTimeout(i,1e3/60);class n extends e{constructor(i){var t,e,o,r,n,a;super(Object.assign(Object.assign({},i),{audioBitsPerSecond:null!==(t=i.audioBitsPerSecond)&&void 0!==t?t:128e3,scrollingWaveform:null!==(e=i.scrollingWaveform)&&void 0!==e&&e,scrollingWaveformWindow:null!==(o=i.scrollingWaveformWindow)&&void 0!==o?o:5,continuousWaveform:null!==(r=i.continuousWaveform)&&void 0!==r&&r,renderRecordedAudio:null===(n=i.renderRecordedAudio)||void 0===n||n,mediaRecorderTimeslice:null!==(a=i.mediaRecorderTimeslice)&&void 0!==a?a:void 0})),this.stream=null,this.mediaRecorder=null,this.dataWindow=null,this.isWaveformPaused=!1,this.lastStartTime=0,this.lastDuration=0,this.duration=0,this.timer=new s,this.subscriptions.push(this.timer.on("tick",(()=>{const i=performance.now()-this.lastStartTime;this.duration=this.isPaused()?this.duration:this.lastDuration+i,this.emit("record-progress",this.duration)})))}static create(i){return new n(i||{})}renderMicStream(i){var t;const e=new AudioContext,s=e.createMediaStreamSource(i),o=e.createAnalyser();s.connect(o);const n=o.frequencyBinCount,a=new Float32Array(n);let d,c=0;this.wavesurfer&&(null!==(t=this.originalOptions)&&void 0!==t||(this.originalOptions=Object.assign({},this.wavesurfer.options)),this.wavesurfer.options.interact=!1,this.options.scrollingWaveform&&(this.wavesurfer.options.cursorWidth=0));const h=()=>{var i,t,s,u;if(this.isWaveformPaused)d=r(h);else{if(o.getFloatTimeDomainData(a),this.options.scrollingWaveform){const i=Math.floor((this.options.scrollingWaveformWindow||0)*e.sampleRate),t=Math.min(i,this.dataWindow?this.dataWindow.length+n:n),s=new Float32Array(i);if(this.dataWindow){const e=Math.max(0,i-this.dataWindow.length);s.set(this.dataWindow.slice(-t+n),e)}s.set(a,i-n),this.dataWindow=s}else if(this.options.continuousWaveform){if(!this.dataWindow){const e=this.options.continuousWaveformDuration?Math.round(60*this.options.continuousWaveformDuration):(null!==(t=null===(i=this.wavesurfer)||void 0===i?void 0:i.getWidth())&&void 0!==t?t:0)*window.devicePixelRatio;this.dataWindow=new Float32Array(e)}const e=Math.max(...a);if(c+1>this.dataWindow.length){const i=new Float32Array(2*this.dataWindow.length);i.set(this.dataWindow,0),this.dataWindow=i}this.dataWindow.set([e],c),c++}else this.dataWindow=a;if(this.wavesurfer){const i=(null!==(u=null===(s=this.dataWindow)||void 0===s?void 0:s.length)&&void 0!==u?u:0)/60;this.wavesurfer.load("",[this.dataWindow],this.options.scrollingWaveform?this.options.scrollingWaveformWindow:i).then((()=>{this.wavesurfer&&this.options.continuousWaveform&&(this.wavesurfer.setTime(this.getDuration()/1e3),this.wavesurfer.options.minPxPerSec||this.wavesurfer.setOptions({minPxPerSec:this.wavesurfer.getWidth()/this.wavesurfer.getDuration()}))})).catch((i=>{console.error("Error rendering real-time recording data:",i)}))}d=r(h)}};return h(),{onDestroy:()=>{cancelAnimationFrame(d),null==s||s.disconnect(),null==e||e.close()},onEnd:()=>{this.isWaveformPaused=!0,cancelAnimationFrame(d),this.stopMic()}}}startMic(t){return i(this,void 0,void 0,(function*(){let i;try{i=yield navigator.mediaDevices.getUserMedia({audio:!(null==t?void 0:t.deviceId)||{deviceId:t.deviceId}})}catch(i){throw new Error("Error accessing the microphone: "+i.message)}const{onDestroy:e,onEnd:s}=this.renderMicStream(i);return this.subscriptions.push(this.once("destroy",e)),this.subscriptions.push(this.once("record-end",s)),this.stream=i,i}))}stopMic(){this.stream&&(this.stream.getTracks().forEach((i=>i.stop())),this.stream=null,this.mediaRecorder=null)}startRecording(t){return i(this,void 0,void 0,(function*(){const i=this.stream||(yield this.startMic(t));this.dataWindow=null;const e=this.mediaRecorder||new MediaRecorder(i,{mimeType:this.options.mimeType||o.find((i=>MediaRecorder.isTypeSupported(i))),audioBitsPerSecond:this.options.audioBitsPerSecond});this.mediaRecorder=e,this.stopRecording();const s=[];e.ondataavailable=i=>{i.data.size>0&&s.push(i.data),this.emit("record-data-available",i.data)};const r=i=>{var t;const o=new Blob(s,{type:e.mimeType});this.emit(i,o),this.options.renderRecordedAudio&&(this.applyOriginalOptionsIfNeeded(),null===(t=this.wavesurfer)||void 0===t||t.load(URL.createObjectURL(o)))};e.onpause=()=>r("record-pause"),e.onstop=()=>r("record-end"),e.start(this.options.mediaRecorderTimeslice),this.lastStartTime=performance.now(),this.lastDuration=0,this.duration=0,this.isWaveformPaused=!1,this.timer.start(),this.emit("record-start")}))}getDuration(){return this.duration}isRecording(){var i;return"recording"===(null===(i=this.mediaRecorder)||void 0===i?void 0:i.state)}isPaused(){var i;return"paused"===(null===(i=this.mediaRecorder)||void 0===i?void 0:i.state)}isActive(){var i;return"inactive"!==(null===(i=this.mediaRecorder)||void 0===i?void 0:i.state)}stopRecording(){var i;this.isActive()&&(null===(i=this.mediaRecorder)||void 0===i||i.stop(),this.timer.stop())}pauseRecording(){var i,t;this.isRecording()&&(this.isWaveformPaused=!0,null===(i=this.mediaRecorder)||void 0===i||i.requestData(),null===(t=this.mediaRecorder)||void 0===t||t.pause(),this.timer.stop(),this.lastDuration=this.duration)}resumeRecording(){var i;this.isPaused()&&(this.isWaveformPaused=!1,null===(i=this.mediaRecorder)||void 0===i||i.resume(),this.timer.start(),this.lastStartTime=performance.now(),this.emit("record-resume"))}static getAvailableAudioDevices(){return i(this,void 0,void 0,(function*(){return navigator.mediaDevices.enumerateDevices().then((i=>i.filter((i=>"audioinput"===i.kind))))}))}destroy(){this.applyOriginalOptionsIfNeeded(),super.destroy(),this.stopRecording(),this.stopMic()}applyOriginalOptionsIfNeeded(){this.wavesurfer&&this.originalOptions&&(this.wavesurfer.setOptions(this.originalOptions),delete this.originalOptions)}}export{n as default};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(i,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):((i="undefined"!=typeof globalThis?globalThis:i||self).WaveSurfer=i.WaveSurfer||{},i.WaveSurfer.Record=t())}(this,(function(){"use strict";function i(i,t,e,s){return new(e||(e=Promise))((function(o,r){function n(i){try{d(s.next(i))}catch(i){r(i)}}function a(i){try{d(s.throw(i))}catch(i){r(i)}}function d(i){var t;i.done?o(i.value):(t=i.value,t instanceof e?t:new e((function(i){i(t)}))).then(n,a)}d((s=s.apply(i,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;class t{constructor(){this.listeners={}}on(i,t,e){if(this.listeners[i]||(this.listeners[i]=new Set),this.listeners[i].add(t),null==e?void 0:e.once){const e=()=>{this.un(i,e),this.un(i,t)};return this.on(i,e),e}return()=>this.un(i,t)}un(i,t){var e;null===(e=this.listeners[i])||void 0===e||e.delete(t)}once(i,t){return this.on(i,t,{once:!0})}unAll(){this.listeners={}}emit(i,...t){this.listeners[i]&&this.listeners[i].forEach((i=>i(...t)))}}class e extends t{constructor(i){super(),this.subscriptions=[],this.options=i}onInit(){}_init(i){this.wavesurfer=i,this.onInit()}destroy(){this.emit("destroy"),this.subscriptions.forEach((i=>i()))}}class s extends t{constructor(){super(...arguments),this.unsubscribe=()=>{}}start(){this.unsubscribe=this.on("tick",(()=>{requestAnimationFrame((()=>{this.emit("tick")}))})),this.emit("tick")}stop(){this.unsubscribe()}destroy(){this.unsubscribe()}}const o=["audio/webm","audio/wav","audio/mpeg","audio/mp4","audio/mp3"];class
|
|
1
|
+
!function(i,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):((i="undefined"!=typeof globalThis?globalThis:i||self).WaveSurfer=i.WaveSurfer||{},i.WaveSurfer.Record=t())}(this,(function(){"use strict";function i(i,t,e,s){return new(e||(e=Promise))((function(o,r){function n(i){try{d(s.next(i))}catch(i){r(i)}}function a(i){try{d(s.throw(i))}catch(i){r(i)}}function d(i){var t;i.done?o(i.value):(t=i.value,t instanceof e?t:new e((function(i){i(t)}))).then(n,a)}d((s=s.apply(i,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;class t{constructor(){this.listeners={}}on(i,t,e){if(this.listeners[i]||(this.listeners[i]=new Set),this.listeners[i].add(t),null==e?void 0:e.once){const e=()=>{this.un(i,e),this.un(i,t)};return this.on(i,e),e}return()=>this.un(i,t)}un(i,t){var e;null===(e=this.listeners[i])||void 0===e||e.delete(t)}once(i,t){return this.on(i,t,{once:!0})}unAll(){this.listeners={}}emit(i,...t){this.listeners[i]&&this.listeners[i].forEach((i=>i(...t)))}}class e extends t{constructor(i){super(),this.subscriptions=[],this.options=i}onInit(){}_init(i){this.wavesurfer=i,this.onInit()}destroy(){this.emit("destroy"),this.subscriptions.forEach((i=>i()))}}class s extends t{constructor(){super(...arguments),this.unsubscribe=()=>{}}start(){this.unsubscribe=this.on("tick",(()=>{requestAnimationFrame((()=>{this.emit("tick")}))})),this.emit("tick")}stop(){this.unsubscribe()}destroy(){this.unsubscribe()}}const o=["audio/webm","audio/wav","audio/mpeg","audio/mp4","audio/mp3"],r=i=>setTimeout(i,1e3/60);class n extends e{constructor(i){var t,e,o,r,n,a;super(Object.assign(Object.assign({},i),{audioBitsPerSecond:null!==(t=i.audioBitsPerSecond)&&void 0!==t?t:128e3,scrollingWaveform:null!==(e=i.scrollingWaveform)&&void 0!==e&&e,scrollingWaveformWindow:null!==(o=i.scrollingWaveformWindow)&&void 0!==o?o:5,continuousWaveform:null!==(r=i.continuousWaveform)&&void 0!==r&&r,renderRecordedAudio:null===(n=i.renderRecordedAudio)||void 0===n||n,mediaRecorderTimeslice:null!==(a=i.mediaRecorderTimeslice)&&void 0!==a?a:void 0})),this.stream=null,this.mediaRecorder=null,this.dataWindow=null,this.isWaveformPaused=!1,this.lastStartTime=0,this.lastDuration=0,this.duration=0,this.timer=new s,this.subscriptions.push(this.timer.on("tick",(()=>{const i=performance.now()-this.lastStartTime;this.duration=this.isPaused()?this.duration:this.lastDuration+i,this.emit("record-progress",this.duration)})))}static create(i){return new n(i||{})}renderMicStream(i){var t;const e=new AudioContext,s=e.createMediaStreamSource(i),o=e.createAnalyser();s.connect(o);const n=o.frequencyBinCount,a=new Float32Array(n);let d,u=0;this.wavesurfer&&(null!==(t=this.originalOptions)&&void 0!==t||(this.originalOptions=Object.assign({},this.wavesurfer.options)),this.wavesurfer.options.interact=!1,this.options.scrollingWaveform&&(this.wavesurfer.options.cursorWidth=0));const c=()=>{var i,t,s,h;if(this.isWaveformPaused)d=r(c);else{if(o.getFloatTimeDomainData(a),this.options.scrollingWaveform){const i=Math.floor((this.options.scrollingWaveformWindow||0)*e.sampleRate),t=Math.min(i,this.dataWindow?this.dataWindow.length+n:n),s=new Float32Array(i);if(this.dataWindow){const e=Math.max(0,i-this.dataWindow.length);s.set(this.dataWindow.slice(-t+n),e)}s.set(a,i-n),this.dataWindow=s}else if(this.options.continuousWaveform){if(!this.dataWindow){const e=this.options.continuousWaveformDuration?Math.round(60*this.options.continuousWaveformDuration):(null!==(t=null===(i=this.wavesurfer)||void 0===i?void 0:i.getWidth())&&void 0!==t?t:0)*window.devicePixelRatio;this.dataWindow=new Float32Array(e)}const e=Math.max(...a);if(u+1>this.dataWindow.length){const i=new Float32Array(2*this.dataWindow.length);i.set(this.dataWindow,0),this.dataWindow=i}this.dataWindow.set([e],u),u++}else this.dataWindow=a;if(this.wavesurfer){const i=(null!==(h=null===(s=this.dataWindow)||void 0===s?void 0:s.length)&&void 0!==h?h:0)/60;this.wavesurfer.load("",[this.dataWindow],this.options.scrollingWaveform?this.options.scrollingWaveformWindow:i).then((()=>{this.wavesurfer&&this.options.continuousWaveform&&(this.wavesurfer.setTime(this.getDuration()/1e3),this.wavesurfer.options.minPxPerSec||this.wavesurfer.setOptions({minPxPerSec:this.wavesurfer.getWidth()/this.wavesurfer.getDuration()}))})).catch((i=>{console.error("Error rendering real-time recording data:",i)}))}d=r(c)}};return c(),{onDestroy:()=>{cancelAnimationFrame(d),null==s||s.disconnect(),null==e||e.close()},onEnd:()=>{this.isWaveformPaused=!0,cancelAnimationFrame(d),this.stopMic()}}}startMic(t){return i(this,void 0,void 0,(function*(){let i;try{i=yield navigator.mediaDevices.getUserMedia({audio:!(null==t?void 0:t.deviceId)||{deviceId:t.deviceId}})}catch(i){throw new Error("Error accessing the microphone: "+i.message)}const{onDestroy:e,onEnd:s}=this.renderMicStream(i);return this.subscriptions.push(this.once("destroy",e)),this.subscriptions.push(this.once("record-end",s)),this.stream=i,i}))}stopMic(){this.stream&&(this.stream.getTracks().forEach((i=>i.stop())),this.stream=null,this.mediaRecorder=null)}startRecording(t){return i(this,void 0,void 0,(function*(){const i=this.stream||(yield this.startMic(t));this.dataWindow=null;const e=this.mediaRecorder||new MediaRecorder(i,{mimeType:this.options.mimeType||o.find((i=>MediaRecorder.isTypeSupported(i))),audioBitsPerSecond:this.options.audioBitsPerSecond});this.mediaRecorder=e,this.stopRecording();const s=[];e.ondataavailable=i=>{i.data.size>0&&s.push(i.data),this.emit("record-data-available",i.data)};const r=i=>{var t;const o=new Blob(s,{type:e.mimeType});this.emit(i,o),this.options.renderRecordedAudio&&(this.applyOriginalOptionsIfNeeded(),null===(t=this.wavesurfer)||void 0===t||t.load(URL.createObjectURL(o)))};e.onpause=()=>r("record-pause"),e.onstop=()=>r("record-end"),e.start(this.options.mediaRecorderTimeslice),this.lastStartTime=performance.now(),this.lastDuration=0,this.duration=0,this.isWaveformPaused=!1,this.timer.start(),this.emit("record-start")}))}getDuration(){return this.duration}isRecording(){var i;return"recording"===(null===(i=this.mediaRecorder)||void 0===i?void 0:i.state)}isPaused(){var i;return"paused"===(null===(i=this.mediaRecorder)||void 0===i?void 0:i.state)}isActive(){var i;return"inactive"!==(null===(i=this.mediaRecorder)||void 0===i?void 0:i.state)}stopRecording(){var i;this.isActive()&&(null===(i=this.mediaRecorder)||void 0===i||i.stop(),this.timer.stop())}pauseRecording(){var i,t;this.isRecording()&&(this.isWaveformPaused=!0,null===(i=this.mediaRecorder)||void 0===i||i.requestData(),null===(t=this.mediaRecorder)||void 0===t||t.pause(),this.timer.stop(),this.lastDuration=this.duration)}resumeRecording(){var i;this.isPaused()&&(this.isWaveformPaused=!1,null===(i=this.mediaRecorder)||void 0===i||i.resume(),this.timer.start(),this.lastStartTime=performance.now(),this.emit("record-resume"))}static getAvailableAudioDevices(){return i(this,void 0,void 0,(function*(){return navigator.mediaDevices.enumerateDevices().then((i=>i.filter((i=>"audioinput"===i.kind))))}))}destroy(){this.applyOriginalOptionsIfNeeded(),super.destroy(),this.stopRecording(),this.stopMic()}applyOriginalOptionsIfNeeded(){this.wavesurfer&&this.originalOptions&&(this.wavesurfer.setOptions(this.originalOptions),delete this.originalOptions)}}return n}));
|
package/dist/renderer.js
CHANGED
|
@@ -1,35 +1,26 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
11
|
-
var t = {};
|
|
12
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
13
|
-
t[p] = s[p];
|
|
14
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
15
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
16
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
17
|
-
t[p[i]] = s[p[i]];
|
|
18
|
-
}
|
|
19
|
-
return t;
|
|
20
|
-
};
|
|
21
1
|
import { makeDraggable } from './draggable.js';
|
|
22
2
|
import EventEmitter from './event-emitter.js';
|
|
23
3
|
class Renderer extends EventEmitter {
|
|
4
|
+
static MAX_CANVAS_WIDTH = 8000;
|
|
5
|
+
static MAX_NODES = 10;
|
|
6
|
+
options;
|
|
7
|
+
parent;
|
|
8
|
+
container;
|
|
9
|
+
scrollContainer;
|
|
10
|
+
wrapper;
|
|
11
|
+
canvasWrapper;
|
|
12
|
+
progressWrapper;
|
|
13
|
+
cursor;
|
|
14
|
+
timeouts = [];
|
|
15
|
+
isScrollable = false;
|
|
16
|
+
audioData = null;
|
|
17
|
+
resizeObserver = null;
|
|
18
|
+
lastContainerWidth = 0;
|
|
19
|
+
isDragging = false;
|
|
20
|
+
subscriptions = [];
|
|
21
|
+
unsubscribeOnScroll;
|
|
24
22
|
constructor(options, audioElement) {
|
|
25
23
|
super();
|
|
26
|
-
this.timeouts = [];
|
|
27
|
-
this.isScrollable = false;
|
|
28
|
-
this.audioData = null;
|
|
29
|
-
this.resizeObserver = null;
|
|
30
|
-
this.lastContainerWidth = 0;
|
|
31
|
-
this.isDragging = false;
|
|
32
|
-
this.subscriptions = [];
|
|
33
24
|
this.subscriptions = [];
|
|
34
25
|
this.options = options;
|
|
35
26
|
const parent = this.parentFromOptionsContainer(options.container);
|
|
@@ -126,16 +117,15 @@ class Renderer extends EventEmitter {
|
|
|
126
117
|
}));
|
|
127
118
|
}
|
|
128
119
|
getHeight(optionsHeight, optionsSplitChannel) {
|
|
129
|
-
var _a;
|
|
130
120
|
const defaultHeight = 128;
|
|
131
|
-
const numberOfChannels =
|
|
121
|
+
const numberOfChannels = this.audioData?.numberOfChannels || 1;
|
|
132
122
|
if (optionsHeight == null)
|
|
133
123
|
return defaultHeight;
|
|
134
124
|
if (!isNaN(Number(optionsHeight)))
|
|
135
125
|
return Number(optionsHeight);
|
|
136
126
|
if (optionsHeight === 'auto') {
|
|
137
127
|
const height = this.parent.clientHeight || defaultHeight;
|
|
138
|
-
if (optionsSplitChannel
|
|
128
|
+
if (optionsSplitChannel?.every((channel) => !channel.overlay))
|
|
139
129
|
return height / numberOfChannels;
|
|
140
130
|
return height;
|
|
141
131
|
}
|
|
@@ -251,11 +241,10 @@ class Renderer extends EventEmitter {
|
|
|
251
241
|
this.setScroll(scrollStart);
|
|
252
242
|
}
|
|
253
243
|
destroy() {
|
|
254
|
-
var _a, _b;
|
|
255
244
|
this.subscriptions.forEach((unsubscribe) => unsubscribe());
|
|
256
245
|
this.container.remove();
|
|
257
|
-
|
|
258
|
-
|
|
246
|
+
this.resizeObserver?.disconnect();
|
|
247
|
+
this.unsubscribeOnScroll?.();
|
|
259
248
|
}
|
|
260
249
|
createDelay(delayMs = 10) {
|
|
261
250
|
let timeout;
|
|
@@ -489,8 +478,7 @@ class Renderer extends EventEmitter {
|
|
|
489
478
|
});
|
|
490
479
|
}
|
|
491
480
|
}
|
|
492
|
-
renderChannel(channelData,
|
|
493
|
-
var { overlay } = _a, options = __rest(_a, ["overlay"]);
|
|
481
|
+
renderChannel(channelData, { overlay, ...options }, width, channelIndex) {
|
|
494
482
|
// A container for canvases
|
|
495
483
|
const canvasContainer = document.createElement('div');
|
|
496
484
|
const height = this.getHeight(options.height, options.splitChannels);
|
|
@@ -506,60 +494,56 @@ class Renderer extends EventEmitter {
|
|
|
506
494
|
// Render the waveform
|
|
507
495
|
this.renderMultiCanvas(channelData, options, width, height, canvasContainer, progressContainer);
|
|
508
496
|
}
|
|
509
|
-
render(audioData) {
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
// Render
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
const options = Object.assign(Object.assign({}, this.options), (_a = this.options.splitChannels) === null || _a === void 0 ? void 0 : _a[i]);
|
|
546
|
-
this.renderChannel([audioData.getChannelData(i)], options, width, i);
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
|
-
else {
|
|
550
|
-
// Render a single waveform for the first two channels (left and right)
|
|
551
|
-
const channels = [audioData.getChannelData(0)];
|
|
552
|
-
if (audioData.numberOfChannels > 1)
|
|
553
|
-
channels.push(audioData.getChannelData(1));
|
|
554
|
-
this.renderChannel(channels, this.options, width, 0);
|
|
497
|
+
async render(audioData) {
|
|
498
|
+
// Clear previous timeouts
|
|
499
|
+
this.timeouts.forEach((clear) => clear());
|
|
500
|
+
this.timeouts = [];
|
|
501
|
+
// Clear the canvases
|
|
502
|
+
this.canvasWrapper.innerHTML = '';
|
|
503
|
+
this.progressWrapper.innerHTML = '';
|
|
504
|
+
// Width
|
|
505
|
+
if (this.options.width != null) {
|
|
506
|
+
this.scrollContainer.style.width =
|
|
507
|
+
typeof this.options.width === 'number' ? `${this.options.width}px` : this.options.width;
|
|
508
|
+
}
|
|
509
|
+
// Determine the width of the waveform
|
|
510
|
+
const pixelRatio = this.getPixelRatio();
|
|
511
|
+
const parentWidth = this.scrollContainer.clientWidth;
|
|
512
|
+
const scrollWidth = Math.ceil(audioData.duration * (this.options.minPxPerSec || 0));
|
|
513
|
+
// Whether the container should scroll
|
|
514
|
+
this.isScrollable = scrollWidth > parentWidth;
|
|
515
|
+
const useParentWidth = this.options.fillParent && !this.isScrollable;
|
|
516
|
+
// Width of the waveform in pixels
|
|
517
|
+
const width = (useParentWidth ? parentWidth : scrollWidth) * pixelRatio;
|
|
518
|
+
// Set the width of the wrapper
|
|
519
|
+
this.wrapper.style.width = useParentWidth ? '100%' : `${scrollWidth}px`;
|
|
520
|
+
// Set additional styles
|
|
521
|
+
this.scrollContainer.style.overflowX = this.isScrollable ? 'auto' : 'hidden';
|
|
522
|
+
this.scrollContainer.classList.toggle('noScrollbar', !!this.options.hideScrollbar);
|
|
523
|
+
this.cursor.style.backgroundColor = `${this.options.cursorColor || this.options.progressColor}`;
|
|
524
|
+
this.cursor.style.width = `${this.options.cursorWidth}px`;
|
|
525
|
+
this.audioData = audioData;
|
|
526
|
+
this.emit('render');
|
|
527
|
+
// Render the waveform
|
|
528
|
+
if (this.options.splitChannels) {
|
|
529
|
+
// Render a waveform for each channel
|
|
530
|
+
for (let i = 0; i < audioData.numberOfChannels; i++) {
|
|
531
|
+
const options = { ...this.options, ...this.options.splitChannels?.[i] };
|
|
532
|
+
this.renderChannel([audioData.getChannelData(i)], options, width, i);
|
|
555
533
|
}
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
534
|
+
}
|
|
535
|
+
else {
|
|
536
|
+
// Render a single waveform for the first two channels (left and right)
|
|
537
|
+
const channels = [audioData.getChannelData(0)];
|
|
538
|
+
if (audioData.numberOfChannels > 1)
|
|
539
|
+
channels.push(audioData.getChannelData(1));
|
|
540
|
+
this.renderChannel(channels, this.options, width, 0);
|
|
541
|
+
}
|
|
542
|
+
// Must be emitted asynchronously for backward compatibility
|
|
543
|
+
Promise.resolve().then(() => this.emit('rendered'));
|
|
559
544
|
}
|
|
560
545
|
reRender() {
|
|
561
|
-
|
|
562
|
-
(_a = this.unsubscribeOnScroll) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
546
|
+
this.unsubscribeOnScroll?.();
|
|
563
547
|
delete this.unsubscribeOnScroll;
|
|
564
548
|
// Return if the waveform has not been rendered yet
|
|
565
549
|
if (!this.audioData)
|
|
@@ -631,28 +615,24 @@ class Renderer extends EventEmitter {
|
|
|
631
615
|
this.scrollIntoView(progress, isPlaying);
|
|
632
616
|
}
|
|
633
617
|
}
|
|
634
|
-
exportImage(format, quality, type) {
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
return Promise
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
}));
|
|
653
|
-
});
|
|
618
|
+
async exportImage(format, quality, type) {
|
|
619
|
+
const canvases = this.canvasWrapper.querySelectorAll('canvas');
|
|
620
|
+
if (!canvases.length) {
|
|
621
|
+
throw new Error('No waveform data');
|
|
622
|
+
}
|
|
623
|
+
// Data URLs
|
|
624
|
+
if (type === 'dataURL') {
|
|
625
|
+
const images = Array.from(canvases).map((canvas) => canvas.toDataURL(format, quality));
|
|
626
|
+
return Promise.resolve(images);
|
|
627
|
+
}
|
|
628
|
+
// Blobs
|
|
629
|
+
return Promise.all(Array.from(canvases).map((canvas) => {
|
|
630
|
+
return new Promise((resolve, reject) => {
|
|
631
|
+
canvas.toBlob((blob) => {
|
|
632
|
+
blob ? resolve(blob) : reject(new Error('Could not export image'));
|
|
633
|
+
}, format, quality);
|
|
634
|
+
});
|
|
635
|
+
}));
|
|
654
636
|
}
|
|
655
637
|
}
|
|
656
|
-
Renderer.MAX_CANVAS_WIDTH = 8000;
|
|
657
|
-
Renderer.MAX_NODES = 10;
|
|
658
638
|
export default Renderer;
|
package/dist/timer.js
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
import EventEmitter from './event-emitter.js';
|
|
2
2
|
class Timer extends EventEmitter {
|
|
3
|
-
|
|
4
|
-
super(...arguments);
|
|
5
|
-
this.unsubscribe = () => undefined;
|
|
6
|
-
}
|
|
3
|
+
unsubscribe = () => undefined;
|
|
7
4
|
start() {
|
|
8
5
|
this.unsubscribe = this.on('tick', () => {
|
|
9
6
|
requestAnimationFrame(() => {
|