summaryrefslogtreecommitdiff
path: root/ext/mixed/js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2021-01-18 22:01:08 -0500
committerGitHub <noreply@github.com>2021-01-18 22:01:08 -0500
commit21fce9f3d98e4381f8813cf9c63410ca1dbd7f91 (patch)
tree124a510e4f208bbd7516b2eae98454400e9b4c54 /ext/mixed/js
parent85c723b85f47ff3048ba7aca46a532aa1bc44064 (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')
-rw-r--r--ext/mixed/js/api.js4
-rw-r--r--ext/mixed/js/audio-system.js60
-rw-r--r--ext/mixed/js/display-audio.js7
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';