diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2021-01-18 22:01:08 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-18 22:01:08 -0500 |
commit | 21fce9f3d98e4381f8813cf9c63410ca1dbd7f91 (patch) | |
tree | 124a510e4f208bbd7516b2eae98454400e9b4c54 /ext/mixed/js/audio-system.js | |
parent | 85c723b85f47ff3048ba7aca46a532aa1bc44064 (diff) |
Audio system refactoring (#1275)
* Simplify details
* Simplify audio creation
* Return an array of sources instead of a single item
* Use sourceIndex instead of index
* Rename APIs
* Return more info about the source
* Return source instead of sourceIndex
Diffstat (limited to 'ext/mixed/js/audio-system.js')
-rw-r--r-- | ext/mixed/js/audio-system.js | 60 |
1 files changed, 25 insertions, 35 deletions
diff --git a/ext/mixed/js/audio-system.js b/ext/mixed/js/audio-system.js index 19c85690..f42fd657 100644 --- a/ext/mixed/js/audio-system.js +++ b/ext/mixed/js/audio-system.js @@ -36,47 +36,30 @@ class AudioSystem { eventListeners.addEventListener(speechSynthesis, 'voiceschanged', onVoicesChanged, false); } - async createDefinitionAudio(sources, expression, reading, details) { + async createExpressionAudio(sources, expression, reading, details) { const key = [expression, reading]; const cacheValue = this._cache.get(key); if (typeof cacheValue !== 'undefined') { - const {audio, source} = cacheValue; - const index = sources.indexOf(source); - if (index >= 0) { - return {audio, index}; - } + return cacheValue; } for (let i = 0, ii = sources.length; i < ii; ++i) { const source = sources[i]; - const info = await this._getAudioInfo(source, expression, reading, details); - if (info === null) { continue; } - - let audio; - try { - switch (info.type) { - case 'url': - { - const {details: {url}} = info; - audio = await this.createAudio(url); - } - break; - case 'tts': - { - const {details: {text, voice}} = info; - audio = this.createTextToSpeechAudio(text, voice); - } - break; - default: - throw new Error(`Unsupported type: ${info.type}`); + const infoList = await await api.getExpressionAudioInfoList(source, expression, reading, details); + for (let j = 0, jj = infoList.length; j < jj; ++j) { + const info = infoList[j]; + let audio; + try { + audio = await this.createAudioFromInfo(info); + } catch (e) { + continue; } - } catch (e) { - continue; - } - this._cache.set(key, {audio, source}); - return {audio, index: i}; + const result = {audio, source, infoList, infoListIndex: j}; + this._cache.set(key, result); + return result; + } } throw new Error('Could not create audio'); @@ -111,12 +94,19 @@ class AudioSystem { return new TextToSpeechAudio(text, voice); } - // Private - - async _getAudioInfo(source, expression, reading, details) { - return await api.getDefinitionAudioInfo(source, expression, reading, details); + async createAudioFromInfo(info) { + switch (info.type) { + case 'url': + return await this.createAudio(info.url); + case 'tts': + return this.createTextToSpeechAudio(info.text, info.voice); + default: + throw new Error(`Unsupported type: ${info.type}`); + } } + // Private + _isAudioValid(audio) { const duration = audio.duration; return ( |