From 887150e01257af0b5deb472263de116cac7d0c1c Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 17 Jan 2021 23:05:15 -0500 Subject: Audio system improvements (#1268) * Simplify API * Move fallback audio --- ext/mixed/js/audio-system.js | 18 +++++++++++++++--- ext/mixed/js/display.js | 14 ++------------ 2 files changed, 17 insertions(+), 15 deletions(-) (limited to 'ext/mixed') diff --git a/ext/mixed/js/audio-system.js b/ext/mixed/js/audio-system.js index 7ae8efdc..19c85690 100644 --- a/ext/mixed/js/audio-system.js +++ b/ext/mixed/js/audio-system.js @@ -18,12 +18,13 @@ /* global * CacheMap * TextToSpeechAudio + * api */ class AudioSystem { - constructor({getAudioInfo, cacheSize=32}) { - this._cache = new CacheMap(cacheSize); - this._getAudioInfo = getAudioInfo; + constructor(useCache) { + this._cache = new CacheMap(useCache ? 32 : 0); + this._fallbackAudio = null; } prepare() { @@ -81,6 +82,13 @@ class AudioSystem { throw new Error('Could not create audio'); } + getFallbackAudio() { + if (this._fallbackAudio === null) { + this._fallbackAudio = new Audio('/mixed/mp3/button.mp3'); + } + return this._fallbackAudio; + } + createAudio(url) { return new Promise((resolve, reject) => { const audio = new Audio(url); @@ -105,6 +113,10 @@ class AudioSystem { // Private + async _getAudioInfo(source, expression, reading, details) { + return await api.getDefinitionAudioInfo(source, expression, reading, details); + } + _isAudioValid(audio) { const duration = audio.duration; return ( diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index 3c7deefe..0b0236da 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -46,10 +46,7 @@ class Display extends EventDispatcher { this._options = null; this._index = 0; this._audioPlaying = null; - this._audioFallback = null; - this._audioSystem = new AudioSystem({ - getAudioInfo: this._getAudioInfo.bind(this) - }); + this._audioSystem = new AudioSystem(true); this._styleNode = null; this._eventListeners = new EventListenerCollection(); this._setContentToken = null; @@ -1234,10 +1231,7 @@ class Display extends EventDispatcher { ({audio, index} = await this._audioSystem.createDefinitionAudio(sources, expression, reading, {textToSpeechVoice, customSourceUrl})); info = `From source ${1 + index}: ${sources[index]}`; } catch (e) { - if (this._audioFallback === null) { - this._audioFallback = new Audio('/mixed/mp3/button.mp3'); - } - audio = this._audioFallback; + audio = this._audioSystem.getFallbackAudio(); info = 'Could not find audio'; } @@ -1569,10 +1563,6 @@ class Display extends EventDispatcher { return {type, expression, reading}; } - async _getAudioInfo(source, expression, reading, details) { - return await api.getDefinitionAudioInfo(source, expression, reading, details); - } - async _setOptionsContextIfDifferent(optionsContext) { if (deepEqual(this._optionsContext, optionsContext)) { return; } await this.setOptionsContext(optionsContext); -- cgit v1.2.3