aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js/backend.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-03-10 19:20:34 -0400
committerGitHub <noreply@github.com>2020-03-10 19:20:34 -0400
commit36c55f0b17e7c2697543edc38e444d01da4f4a5c (patch)
treebeb7cb0baf66bf5d0f07919153c6586790345f33 /ext/bg/js/backend.js
parent7541517d8084b50e054393f3b4fa6d4630ad012e (diff)
parent0cbf427ab50061b48c9027e63e9ee8a209946d37 (diff)
Merge pull request #401 from toasted-nutbread/audio-refactor
Audio refactor
Diffstat (limited to 'ext/bg/js/backend.js')
-rw-r--r--ext/bg/js/backend.js57
1 files changed, 48 insertions, 9 deletions
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js
index d0d53a36..349fb4eb 100644
--- a/ext/bg/js/backend.js
+++ b/ext/bg/js/backend.js
@@ -21,9 +21,8 @@ conditionsTestValue, profileConditionsDescriptor
handlebarsRenderDynamic
requestText, requestJson, optionsLoad
dictConfigured, dictTermsSort, dictEnabledSet
-audioGetUrl, audioInject
jpConvertReading, jpDistributeFuriganaInflected, jpKatakanaToHiragana
-AnkiNoteBuilder, AudioSystem, Translator, AnkiConnect, AnkiNull, Mecab, BackendApiForwarder, JsonSchema, ClipboardMonitor*/
+AnkiNoteBuilder, AudioSystem, AudioUriBuilder, Translator, AnkiConnect, AnkiNull, Mecab, BackendApiForwarder, JsonSchema, ClipboardMonitor*/
class Backend {
constructor() {
@@ -36,6 +35,7 @@ class Backend {
this.optionsSchema = null;
this.defaultAnkiFieldTemplates = null;
this.audioSystem = new AudioSystem({getAudioUri: this._getAudioUri.bind(this)});
+ this.audioUriBuilder = new AudioUriBuilder();
this.optionsContext = {
depth: 0,
url: window.location.href
@@ -67,7 +67,7 @@ class Backend {
['noteView', this._onApiNoteView.bind(this)],
['templateRender', this._onApiTemplateRender.bind(this)],
['commandExec', this._onApiCommandExec.bind(this)],
- ['audioGetUrl', this._onApiAudioGetUrl.bind(this)],
+ ['audioGetUri', this._onApiAudioGetUri.bind(this)],
['screenshotGet', this._onApiScreenshotGet.bind(this)],
['forward', this._onApiForward.bind(this)],
['frameInformationGet', this._onApiFrameInformationGet.bind(this)],
@@ -434,12 +434,11 @@ class Backend {
const templates = this.defaultAnkiFieldTemplates;
if (mode !== 'kanji') {
- await audioInject(
+ await this._audioInject(
definition,
options.anki.terms.fields,
options.audio.sources,
- optionsContext,
- this.audioSystem
+ optionsContext
);
}
@@ -514,9 +513,9 @@ class Backend {
return this._runCommand(command, params);
}
- async _onApiAudioGetUrl({definition, source, optionsContext}) {
+ async _onApiAudioGetUri({definition, source, optionsContext}) {
const options = this.getOptions(optionsContext);
- return await audioGetUrl(definition, source, options);
+ return await this.audioUriBuilder.getUri(definition, source, options);
}
_onApiScreenshotGet({options}, sender) {
@@ -772,7 +771,36 @@ class Backend {
}
const options = this.getOptions(optionsContext);
- return await audioGetUrl(definition, source, options);
+ return await this.audioUriBuilder.getUri(definition, source, options);
+ }
+
+ async _audioInject(definition, fields, sources, optionsContext) {
+ let usesAudio = false;
+ for (const fieldValue of Object.values(fields)) {
+ if (fieldValue.includes('{audio}')) {
+ usesAudio = true;
+ break;
+ }
+ }
+
+ if (!usesAudio) {
+ return true;
+ }
+
+ try {
+ const expressions = definition.expressions;
+ const audioSourceDefinition = Array.isArray(expressions) ? expressions[0] : definition;
+
+ const {uri} = await this.audioSystem.getDefinitionAudio(audioSourceDefinition, sources, {tts: false, optionsContext});
+ const filename = this._createInjectedAudioFileName(audioSourceDefinition);
+ if (filename !== null) {
+ definition.audio = {url: uri, filename};
+ }
+
+ return true;
+ } catch (e) {
+ return false;
+ }
}
async _injectScreenshot(definition, fields, screenshot) {
@@ -815,6 +843,17 @@ class Backend {
return handlebarsRenderDynamic(template, data);
}
+ _createInjectedAudioFileName(definition) {
+ const {reading, expression} = definition;
+ if (!reading && !expression) { return null; }
+
+ let filename = 'yomichan';
+ if (reading) { filename += `_${reading}`; }
+ if (expression) { filename += `_${expression}`; }
+ filename += '.mp3';
+ return filename;
+ }
+
static _getTabUrl(tab) {
return new Promise((resolve) => {
chrome.tabs.sendMessage(tab.id, {action: 'getUrl'}, {frameId: 0}, (response) => {