diff options
Diffstat (limited to 'ext/mixed/js')
-rw-r--r-- | ext/mixed/js/api.js | 4 | ||||
-rw-r--r-- | ext/mixed/js/audio-system.js | 60 | ||||
-rw-r--r-- | ext/mixed/js/display-audio.js | 7 |
3 files changed, 31 insertions, 40 deletions
diff --git a/ext/mixed/js/api.js b/ext/mixed/js/api.js index 433a52e2..03e58f5e 100644 --- a/ext/mixed/js/api.js +++ b/ext/mixed/js/api.js @@ -97,8 +97,8 @@ const api = (() => { return this._invoke('suspendAnkiCardsForNote', {noteId}); } - getDefinitionAudioInfo(source, expression, reading, details) { - return this._invoke('getDefinitionAudioInfo', {source, expression, reading, details}); + getExpressionAudioInfoList(source, expression, reading, details) { + return this._invoke('getExpressionAudioInfoList', {source, expression, reading, details}); } downloadDefinitionAudio(sources, expression, reading, details) { 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 ( diff --git a/ext/mixed/js/display-audio.js b/ext/mixed/js/display-audio.js index c423446e..0cd8a625 100644 --- a/ext/mixed/js/display-audio.js +++ b/ext/mixed/js/display-audio.js @@ -117,9 +117,10 @@ class DisplayAudio { let audio; let info; try { - let index; - ({audio, index} = await this._audioSystem.createDefinitionAudio(sources, expression, reading, {textToSpeechVoice, customSourceUrl})); - info = `From source ${1 + index}: ${sources[index]}`; + let source; + ({audio, source} = await this._audioSystem.createExpressionAudio(sources, expression, reading, {textToSpeechVoice, customSourceUrl})); + const sourceIndex = sources.indexOf(source); + info = `From source ${1 + sourceIndex}: ${source}`; } catch (e) { audio = this._audioSystem.getFallbackAudio(); info = 'Could not find audio'; |