const PROGRESS_INTERVAL: number = 100; export default class HtmlAudioModule { isLoading: () => boolean = () => false; playing: boolean = false; progressMs: number = 0; playbackIndex: number = 0; _audio: HTMLAudioElement = null; _playbackStart: number = 0; _playing: Promise = null; _scrolling: number = 0; base64Data: string[] = []; _bufferData: AudioBuffer[] = []; // { // forceStopPlayback: action, // handlePlaybackEnded: action, // nextPlayback: action, // pausePlayback: action, // playbackIndex: observable, // playing: observable, // playSession: action, // prevPlayback: action, // progressMs: observable, // progressRatio: computed, // scrollIndicator: action, // setPlaying: action, // setSensorData: action, // stopPlayback: action, // } constructor(config) { Object.keys(config).forEach((key: string) => { this[key] = config[key]; }); } setBase64Data = (base64Data: string[]) => { this.handlePlaybackEnded(); this.base64Data = base64Data; } setBufferData = (bufferData: AudioBuffer[]) => { this.handlePlaybackEnded(); this._bufferData = bufferData; } setLoadingHandler(isLoading: () => boolean): void { this.isLoading = isLoading; } togglePlayback = () => { if (this.playing) { this.pausePlayback(); } else { this.playSession(0); } } nextPlayback = () => { if (this.base64Data && this.playbackIndex < this.base64Data.length - 1) { this.forceStopPlayback(); this.progressMs = 0; this.playbackIndex++; } } prevPlayback = () => { if (this.playbackIndex > 0) { this.forceStopPlayback(); this.progressMs = 0; this.playbackIndex--; } } setPlaying = (playing: boolean) => { this.playing = playing; } playSession = (bt: number = 0) => { if (this._audio && this._audio.paused) { this._audio.play(); this.playing = true; } else { const playbackStart: number = performance.now(); this._playbackStart = playbackStart; const ready: Promise = this.playing || this.isLoading() ? this.stopPlayback() : Promise.resolve(); return ready.then(() => { const base64WavFile: string = this.base64Data[this.playbackIndex]; if (!base64WavFile || this._playbackStart !== playbackStart) { return null; } this.setPlaying(true); this._audio = new Audio('data:audio/wav;base64,' + base64WavFile); this._audio.onplaying = () => this.handlePlaybackStarted(bt); this._audio.onended = this.handlePlaybackEnded; this._audio.onpause = this.handlePlaybackPaused; this._audio.onerror = this.handlePlaybackError; this._playing = this._audio.play().catch(this.handlePlaybackError); }); } } forceStopPlayback = () => { this._playbackStart = 0; if (this._audio) { this._audio.src = ''; } return Promise.resolve(this.handlePlaybackEnded()); } pausePlayback = () => { this._audio.pause(); this.playing = false; } stopPlayback = () => { if (this._audio) { this._audio.src = ''; } return Promise.resolve(this.handlePlaybackEnded()); } handlePlaybackPaused = () => { this.stopIndicator(); } handlePlaybackEnded = () => { this.playing = false; this.stopIndicator(); this._audio = null; this.progressMs = 0; } stopIndicator = () => { window.clearInterval(this._scrolling); this._scrolling = null; } handlePlaybackStarted = (bt?: number) => { this.scrollIndicator(bt); } scrollIndicator = (bt?: number) => { if (bt) { this.progressMs = bt / 1000; } let now: number = performance.now(); const scroll: VoidFunction = () => { const newNow: number = performance.now(); const delta: number = newNow - now; this.progressMs += delta; now = newNow; }; if (this._scrolling) { this.stopIndicator(); } this._scrolling = window.setInterval(scroll, PROGRESS_INTERVAL); } handlePlaybackError = (e: any) => { this.stopPlayback(); } get progressRatio(): number { const sessionDurationMs = (this._audio) ? this._audio.duration * 1000 : 1; return this.progressMs / (sessionDurationMs); } get base64Mode(): boolean { return !!this.base64Data.length; } downloadParametersBase64 = () => ({ download: `${this.playbackIndex}.wav`, href: 'data:audio/wav;base64,' + this.base64Data[this.playbackIndex], }) }