diff options
author | Stefan Vuković <stefanvukovic44@gmail.com> | 2024-07-01 11:51:10 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-01 09:51:10 +0000 |
commit | 9261a70a564034badf2c2b8c6859c46b295adb3d (patch) | |
tree | 1517f37d5d368db2104fa0b5d38677cb65705803 /ext/js | |
parent | f243608010746b8268f8658b40bd38bc4e337a98 (diff) |
make non-ja audio work without configuration (#1152)
* add audio source for wiktionary format filenames on commons
* allow region codes
* fix files being saved in anki as mp3
* wip
* refactor _getAvailableAudioSourceTypes
* update docs
* wording
* single line list
* fix no anki audio
Diffstat (limited to 'ext/js')
-rw-r--r-- | ext/js/display/display-audio.js | 30 | ||||
-rw-r--r-- | ext/js/media/audio-downloader.js | 28 | ||||
-rw-r--r-- | ext/js/pages/settings/audio-controller.js | 34 |
3 files changed, 74 insertions, 18 deletions
diff --git a/ext/js/display/display-audio.js b/ext/js/display/display-audio.js index bb30c944..4fcabf06 100644 --- a/ext/js/display/display-audio.js +++ b/ext/js/display/display-audio.js @@ -166,16 +166,15 @@ export class DisplayAudio { * @param {import('display').EventArgument<'optionsUpdated'>} details */ _onOptionsUpdated({options}) { - const {enabled, autoPlay, volume, sources} = options.audio; + const { + general: {language}, + audio: {enabled, autoPlay, volume, sources}, + } = options; this._autoPlay = enabled && autoPlay; this._playbackVolume = Number.isFinite(volume) ? Math.max(0, Math.min(1, volume / 100)) : 1; /** @type {Set<import('settings').AudioSourceType>} */ - const requiredAudioSources = new Set([ - 'jpod101', - 'jpod101-alternate', - 'jisho', - ]); + const requiredAudioSources = this._getRequiredAudioSources(language); /** @type {Map<string, import('display-audio').AudioSource[]>} */ const nameMap = new Map(); this._audioSources.length = 0; @@ -188,11 +187,28 @@ export class DisplayAudio { } const data = document.documentElement.dataset; - data.audioEnabled = `${enabled && sources.length > 0}`; + data.audioEnabled = enabled.toString(); this._cache.clear(); } + /** + * @param {string} language + * @returns {Set<import('settings').AudioSourceType>} + */ + _getRequiredAudioSources(language) { + return language === 'ja' ? + new Set([ + 'jpod101', + 'jpod101-alternate', + 'jisho', + ]) : + new Set([ + 'lingua-libre', + 'wiktionary', + ]); + } + /** */ _onContentClear() { this._entriesToken = {}; diff --git a/ext/js/media/audio-downloader.js b/ext/js/media/audio-downloader.js index 99ca1dfd..17911c3e 100644 --- a/ext/js/media/audio-downloader.js +++ b/ext/js/media/audio-downloader.js @@ -90,7 +90,8 @@ export class AudioDownloader { */ async downloadTermAudio(sources, preferredAudioIndex, term, reading, idleTimeout, languageSummary) { const errors = []; - for (const source of sources) { + const requiredAudioSources = this._getRequiredAudioSources(languageSummary.iso, sources); + for (const source of [...sources, ...requiredAudioSources]) { let infoList = await this.getTermAudioInfoList(source, term, reading, languageSummary); if (typeof preferredAudioIndex === 'number') { infoList = (preferredAudioIndex >= 0 && preferredAudioIndex < infoList.length ? [infoList[preferredAudioIndex]] : []); @@ -116,6 +117,31 @@ export class AudioDownloader { // Private /** + * @param {string} language + * @param {import('audio').AudioSourceInfo[]} sources + * @returns {import('audio').AudioSourceInfo[]} + */ + _getRequiredAudioSources(language, sources) { + /** @type {Set<import('settings').AudioSourceType>} */ + const requiredSources = language === 'ja' ? + new Set([ + 'jpod101', + 'jpod101-alternate', + 'jisho', + ]) : + new Set([ + 'lingua-libre', + 'wiktionary', + ]); + + for (const {type} of sources) { + requiredSources.delete(type); + } + + return [...requiredSources].map((type) => ({type, url: '', voice: ''})); + } + + /** * @param {string} url * @param {string} base * @returns {string} diff --git a/ext/js/pages/settings/audio-controller.js b/ext/js/pages/settings/audio-controller.js index b89ae2ba..defef16e 100644 --- a/ext/js/pages/settings/audio-controller.js +++ b/ext/js/pages/settings/audio-controller.js @@ -47,6 +47,8 @@ export class AudioController extends EventDispatcher { this._voiceTestTextInput = querySelectorNotNull(document, '#text-to-speech-voice-test-text'); /** @type {import('audio-controller').VoiceInfo[]} */ this._voices = []; + /** @type {string} */ + this._language = 'ja'; } /** @type {import('./settings-controller.js').SettingsController} */ @@ -121,12 +123,18 @@ export class AudioController extends EventDispatcher { * @param {import('settings-controller').EventArgument<'optionsChanged'>} details */ _onOptionsChanged({options}) { + const { + general: {language}, + audio: {sources}, + } = options; + + this._language = language; + for (const entry of this._audioSourceEntries) { entry.cleanup(); } this._audioSourceEntries = []; - const {sources} = options.audio; for (let i = 0, ii = sources.length; i < ii; ++i) { this._createAudioSourceEntry(i, sources[i]); } @@ -216,15 +224,7 @@ export class AudioController extends EventDispatcher { * @returns {import('settings').AudioSourceType} */ _getUnusedAudioSourceType() { - /** @type {import('settings').AudioSourceType[]} */ - const typesAvailable = [ - 'jpod101', - 'jpod101-alternate', - 'jisho', - 'lingua-libre', - 'wiktionary', - 'custom', - ]; + const typesAvailable = this._getAvailableAudioSourceTypes(); for (const type of typesAvailable) { if (!this._audioSourceEntries.some((entry) => entry.type === type)) { return type; @@ -233,6 +233,20 @@ export class AudioController extends EventDispatcher { return typesAvailable[0]; } + /** + * @returns {import('settings').AudioSourceType[]} + */ + _getAvailableAudioSourceTypes() { + /** @type {import('settings').AudioSourceType[]} */ + const generalAudioSources = ['lingua-libre', 'wiktionary', 'text-to-speech', 'custom']; + if (this._language === 'ja') { + /** @type {import('settings').AudioSourceType[]} */ + const japaneseAudioSources = ['jpod101', 'jpod101-alternate', 'jisho']; + return [...japaneseAudioSources, ...generalAudioSources]; + } + return generalAudioSources; + } + /** */ async _addAudioSource() { const type = this._getUnusedAudioSourceType(); |