2019-12-01 20:30:04 +00:00
|
|
|
/*
|
2021-01-01 19:50:41 +00:00
|
|
|
* Copyright (C) 2019-2021 Yomichan Authors
|
2019-12-01 20:30:04 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2020-01-01 17:00:31 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2019-12-01 20:30:04 +00:00
|
|
|
*/
|
|
|
|
|
2020-03-11 02:30:36 +00:00
|
|
|
/* global
|
|
|
|
* AudioSystem
|
|
|
|
*/
|
2019-12-01 20:30:04 +00:00
|
|
|
|
2021-05-30 16:41:19 +00:00
|
|
|
class AudioController extends EventDispatcher {
|
2021-05-27 00:40:53 +00:00
|
|
|
constructor(settingsController, modalController) {
|
2021-05-30 16:41:19 +00:00
|
|
|
super();
|
2020-05-30 13:33:13 +00:00
|
|
|
this._settingsController = settingsController;
|
2021-05-27 00:40:53 +00:00
|
|
|
this._modalController = modalController;
|
2021-01-23 03:10:27 +00:00
|
|
|
this._audioSystem = new AudioSystem();
|
2020-05-29 23:56:38 +00:00
|
|
|
this._audioSourceContainer = null;
|
|
|
|
this._audioSourceAddButton = null;
|
|
|
|
this._audioSourceEntries = [];
|
2021-05-30 16:41:19 +00:00
|
|
|
this._voiceTestTextInput = null;
|
|
|
|
this._voices = [];
|
2020-05-29 23:56:38 +00:00
|
|
|
}
|
2019-12-01 20:30:04 +00:00
|
|
|
|
2021-05-28 02:16:34 +00:00
|
|
|
get settingsController() {
|
|
|
|
return this._settingsController;
|
|
|
|
}
|
|
|
|
|
|
|
|
get modalController() {
|
|
|
|
return this._modalController;
|
|
|
|
}
|
|
|
|
|
2020-05-29 23:56:38 +00:00
|
|
|
async prepare() {
|
2020-09-26 17:41:26 +00:00
|
|
|
this._audioSystem.prepare();
|
2019-12-01 20:30:04 +00:00
|
|
|
|
2021-05-30 16:41:19 +00:00
|
|
|
this._voiceTestTextInput = document.querySelector('#text-to-speech-voice-test-text');
|
2020-10-10 16:11:33 +00:00
|
|
|
this._audioSourceContainer = document.querySelector('#audio-source-list');
|
|
|
|
this._audioSourceAddButton = document.querySelector('#audio-source-add');
|
2020-05-29 23:56:38 +00:00
|
|
|
this._audioSourceContainer.textContent = '';
|
|
|
|
|
|
|
|
this._audioSourceAddButton.addEventListener('click', this._onAddAudioSource.bind(this), false);
|
|
|
|
|
2021-04-11 03:55:11 +00:00
|
|
|
this._audioSystem.on('voiceschanged', this._updateTextToSpeechVoices.bind(this), false);
|
2020-10-10 16:11:33 +00:00
|
|
|
this._updateTextToSpeechVoices();
|
|
|
|
|
|
|
|
document.querySelector('#text-to-speech-voice-test').addEventListener('click', this._onTestTextToSpeech.bind(this), false);
|
2020-05-30 13:33:13 +00:00
|
|
|
|
|
|
|
this._settingsController.on('optionsChanged', this._onOptionsChanged.bind(this));
|
|
|
|
|
2020-05-31 01:54:38 +00:00
|
|
|
const options = await this._settingsController.getOptions();
|
|
|
|
this._onOptionsChanged({options});
|
2020-05-29 23:56:38 +00:00
|
|
|
}
|
|
|
|
|
2021-05-28 02:16:34 +00:00
|
|
|
async removeSource(entry) {
|
|
|
|
const {index} = entry;
|
|
|
|
this._audioSourceEntries.splice(index, 1);
|
|
|
|
entry.cleanup();
|
|
|
|
for (let i = index, ii = this._audioSourceEntries.length; i < ii; ++i) {
|
|
|
|
this._audioSourceEntries[i].index = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
await this._settingsController.modifyProfileSettings([{
|
|
|
|
action: 'splice',
|
|
|
|
path: 'audio.sources',
|
|
|
|
start: index,
|
|
|
|
deleteCount: 1,
|
|
|
|
items: []
|
|
|
|
}]);
|
|
|
|
}
|
|
|
|
|
2021-05-30 16:41:19 +00:00
|
|
|
getVoices() {
|
|
|
|
return this._voices;
|
|
|
|
}
|
|
|
|
|
|
|
|
setTestVoice(voice) {
|
|
|
|
this._voiceTestTextInput.dataset.voice = voice;
|
|
|
|
}
|
|
|
|
|
2020-05-29 23:56:38 +00:00
|
|
|
// Private
|
2019-12-01 20:30:04 +00:00
|
|
|
|
2020-05-31 01:54:38 +00:00
|
|
|
_onOptionsChanged({options}) {
|
2021-05-28 02:16:34 +00:00
|
|
|
for (const entry of this._audioSourceEntries) {
|
|
|
|
entry.cleanup();
|
2020-05-30 13:33:13 +00:00
|
|
|
}
|
2021-05-28 02:16:34 +00:00
|
|
|
this._audioSourceEntries = [];
|
2020-05-30 13:33:13 +00:00
|
|
|
|
2021-05-28 02:16:34 +00:00
|
|
|
const {sources} = options.audio;
|
|
|
|
for (let i = 0, ii = sources.length; i < ii; ++i) {
|
|
|
|
this._createAudioSourceEntry(i, sources[i]);
|
2020-05-30 13:33:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-28 02:16:34 +00:00
|
|
|
_onAddAudioSource() {
|
|
|
|
this._addAudioSource();
|
|
|
|
}
|
|
|
|
|
2020-10-10 16:11:33 +00:00
|
|
|
_onTestTextToSpeech() {
|
|
|
|
try {
|
2021-05-30 16:41:19 +00:00
|
|
|
const text = this._voiceTestTextInput.value || '';
|
|
|
|
const voiceUri = this._voiceTestTextInput.dataset.voice;
|
2020-10-10 16:11:33 +00:00
|
|
|
const audio = this._audioSystem.createTextToSpeechAudio(text, voiceUri);
|
|
|
|
audio.volume = 1.0;
|
|
|
|
audio.play();
|
|
|
|
} catch (e) {
|
|
|
|
// NOP
|
|
|
|
}
|
2020-05-29 23:56:38 +00:00
|
|
|
}
|
2019-12-06 03:36:59 +00:00
|
|
|
|
2020-05-29 23:56:38 +00:00
|
|
|
_updateTextToSpeechVoices() {
|
2020-10-10 16:11:33 +00:00
|
|
|
const voices = (
|
|
|
|
typeof speechSynthesis !== 'undefined' ?
|
|
|
|
[...speechSynthesis.getVoices()].map((voice, index) => ({
|
|
|
|
voice,
|
|
|
|
isJapanese: this._languageTagIsJapanese(voice.lang),
|
|
|
|
index
|
|
|
|
})) :
|
|
|
|
[]
|
|
|
|
);
|
2020-05-29 23:56:38 +00:00
|
|
|
voices.sort(this._textToSpeechVoiceCompare.bind(this));
|
2021-05-30 16:41:19 +00:00
|
|
|
this._voices = voices;
|
|
|
|
this.trigger('voicesUpdated');
|
2019-12-01 20:30:04 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 23:56:38 +00:00
|
|
|
_textToSpeechVoiceCompare(a, b) {
|
2020-10-10 16:11:33 +00:00
|
|
|
if (a.isJapanese) {
|
|
|
|
if (!b.isJapanese) { return -1; }
|
2020-05-29 23:56:38 +00:00
|
|
|
} else {
|
2020-10-10 16:11:33 +00:00
|
|
|
if (b.isJapanese) { return 1; }
|
2020-05-29 23:56:38 +00:00
|
|
|
}
|
|
|
|
|
2020-10-10 16:11:33 +00:00
|
|
|
if (a.voice.default) {
|
|
|
|
if (!b.voice.default) { return -1; }
|
2020-05-29 23:56:38 +00:00
|
|
|
} else {
|
2020-10-10 16:11:33 +00:00
|
|
|
if (b.voice.default) { return 1; }
|
2020-05-29 23:56:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return a.index - b.index;
|
|
|
|
}
|
2019-12-01 20:30:04 +00:00
|
|
|
|
2020-05-29 23:56:38 +00:00
|
|
|
_languageTagIsJapanese(languageTag) {
|
|
|
|
return (
|
2020-06-13 14:18:44 +00:00
|
|
|
languageTag.startsWith('ja_') ||
|
2020-05-29 23:56:38 +00:00
|
|
|
languageTag.startsWith('ja-') ||
|
|
|
|
languageTag.startsWith('jpn-')
|
|
|
|
);
|
|
|
|
}
|
2019-12-01 20:30:04 +00:00
|
|
|
|
2021-05-30 16:41:19 +00:00
|
|
|
_createAudioSourceEntry(index, source) {
|
2021-05-28 02:16:34 +00:00
|
|
|
const node = this._settingsController.instantiateTemplate('audio-source');
|
2021-05-30 16:41:19 +00:00
|
|
|
const entry = new AudioSourceEntry(this, index, source, node);
|
2021-05-28 02:16:34 +00:00
|
|
|
this._audioSourceEntries.push(entry);
|
|
|
|
this._audioSourceContainer.appendChild(node);
|
|
|
|
entry.prepare();
|
|
|
|
}
|
|
|
|
|
|
|
|
_getUnusedAudioSourceType() {
|
|
|
|
const typesAvailable = [
|
2020-05-29 23:56:38 +00:00
|
|
|
'jpod101',
|
|
|
|
'jpod101-alternate',
|
|
|
|
'jisho',
|
|
|
|
'custom'
|
|
|
|
];
|
2021-05-28 02:16:34 +00:00
|
|
|
for (const type of typesAvailable) {
|
|
|
|
if (!this._audioSourceEntries.some((entry) => entry.type === type)) {
|
|
|
|
return type;
|
2020-05-29 23:56:38 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-28 02:16:34 +00:00
|
|
|
return typesAvailable[0];
|
2020-05-29 23:56:38 +00:00
|
|
|
}
|
|
|
|
|
2021-05-28 02:16:34 +00:00
|
|
|
async _addAudioSource() {
|
|
|
|
const type = this._getUnusedAudioSourceType();
|
2021-05-30 16:41:19 +00:00
|
|
|
const source = {type, url: '', voice: ''};
|
2021-05-28 02:16:34 +00:00
|
|
|
const index = this._audioSourceEntries.length;
|
2021-05-30 16:41:19 +00:00
|
|
|
this._createAudioSourceEntry(index, source);
|
2020-05-31 01:54:38 +00:00
|
|
|
await this._settingsController.modifyProfileSettings([{
|
|
|
|
action: 'splice',
|
|
|
|
path: 'audio.sources',
|
|
|
|
start: index,
|
2021-05-28 02:16:34 +00:00
|
|
|
deleteCount: 0,
|
2021-05-30 16:41:19 +00:00
|
|
|
items: [source]
|
2020-05-31 01:54:38 +00:00
|
|
|
}]);
|
|
|
|
}
|
2021-05-28 02:16:34 +00:00
|
|
|
}
|
2020-05-31 01:54:38 +00:00
|
|
|
|
2021-05-28 02:16:34 +00:00
|
|
|
class AudioSourceEntry {
|
2021-05-30 16:41:19 +00:00
|
|
|
constructor(parent, index, source, node) {
|
2021-05-28 02:16:34 +00:00
|
|
|
this._parent = parent;
|
|
|
|
this._index = index;
|
2021-05-30 16:41:19 +00:00
|
|
|
this._type = source.type;
|
|
|
|
this._url = source.url;
|
|
|
|
this._voice = source.voice;
|
2021-05-28 02:16:34 +00:00
|
|
|
this._node = node;
|
|
|
|
this._eventListeners = new EventListenerCollection();
|
2021-05-30 16:41:19 +00:00
|
|
|
this._typeSelect = null;
|
|
|
|
this._urlInput = null;
|
|
|
|
this._voiceSelect = null;
|
2020-05-29 23:56:38 +00:00
|
|
|
}
|
|
|
|
|
2021-05-28 02:16:34 +00:00
|
|
|
get index() {
|
|
|
|
return this._index;
|
|
|
|
}
|
|
|
|
|
|
|
|
set index(value) {
|
|
|
|
this._index = value;
|
2019-12-01 20:30:04 +00:00
|
|
|
}
|
2019-12-12 02:15:51 +00:00
|
|
|
|
2021-05-28 02:16:34 +00:00
|
|
|
get type() {
|
|
|
|
return this._type;
|
|
|
|
}
|
|
|
|
|
|
|
|
prepare() {
|
2021-05-30 16:41:19 +00:00
|
|
|
this._updateTypeParameter();
|
|
|
|
|
2021-05-28 02:16:34 +00:00
|
|
|
const menuButton = this._node.querySelector('.audio-source-menu-button');
|
2021-05-30 16:41:19 +00:00
|
|
|
this._typeSelect = this._node.querySelector('.audio-source-type-select');
|
|
|
|
this._urlInput = this._node.querySelector('.audio-source-parameter-container[data-field=url] .audio-source-parameter');
|
|
|
|
this._voiceSelect = this._node.querySelector('.audio-source-parameter-container[data-field=voice] .audio-source-parameter');
|
2021-05-28 02:16:34 +00:00
|
|
|
|
2021-05-30 16:41:19 +00:00
|
|
|
this._typeSelect.value = this._type;
|
|
|
|
this._urlInput.value = this._url;
|
2020-05-29 23:56:38 +00:00
|
|
|
|
2021-05-30 16:41:19 +00:00
|
|
|
this._eventListeners.addEventListener(this._typeSelect, 'change', this._onTypeSelectChange.bind(this), false);
|
|
|
|
this._eventListeners.addEventListener(this._urlInput, 'change', this._onUrlInputChange.bind(this), false);
|
|
|
|
this._eventListeners.addEventListener(this._voiceSelect, 'change', this._onVoiceSelectChange.bind(this), false);
|
2021-05-28 02:16:34 +00:00
|
|
|
this._eventListeners.addEventListener(menuButton, 'menuOpen', this._onMenuOpen.bind(this), false);
|
|
|
|
this._eventListeners.addEventListener(menuButton, 'menuClose', this._onMenuClose.bind(this), false);
|
2021-05-30 16:41:19 +00:00
|
|
|
this._eventListeners.on(this._parent, 'voicesUpdated', this._onVoicesUpdated.bind(this));
|
|
|
|
this._onVoicesUpdated();
|
2020-05-29 23:56:38 +00:00
|
|
|
}
|
|
|
|
|
2021-05-28 02:16:34 +00:00
|
|
|
cleanup() {
|
|
|
|
if (this._node.parentNode !== null) {
|
|
|
|
this._node.parentNode.removeChild(this._node);
|
|
|
|
}
|
|
|
|
this._eventListeners.removeAllEventListeners();
|
2020-10-18 17:18:57 +00:00
|
|
|
}
|
|
|
|
|
2021-05-28 02:16:34 +00:00
|
|
|
// Private
|
|
|
|
|
2021-05-30 16:41:19 +00:00
|
|
|
_onVoicesUpdated() {
|
|
|
|
const voices = this._parent.getVoices();
|
|
|
|
|
|
|
|
const fragment = document.createDocumentFragment();
|
|
|
|
|
|
|
|
let option = document.createElement('option');
|
|
|
|
option.value = '';
|
|
|
|
option.textContent = 'None';
|
|
|
|
fragment.appendChild(option);
|
|
|
|
|
|
|
|
for (const {voice} of voices) {
|
|
|
|
option = document.createElement('option');
|
|
|
|
option.value = voice.voiceURI;
|
|
|
|
option.textContent = `${voice.name} (${voice.lang})`;
|
|
|
|
fragment.appendChild(option);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._voiceSelect.textContent = '';
|
|
|
|
this._voiceSelect.appendChild(fragment);
|
|
|
|
this._voiceSelect.value = this._voice;
|
|
|
|
}
|
|
|
|
|
|
|
|
_onTypeSelectChange(e) {
|
|
|
|
this._setType(e.currentTarget.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onUrlInputChange(e) {
|
|
|
|
this._setUrl(e.currentTarget.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onVoiceSelectChange(e) {
|
|
|
|
this._setVoice(e.currentTarget.value);
|
2021-05-28 02:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_onMenuOpen(e) {
|
2021-05-27 00:40:53 +00:00
|
|
|
const {menu} = e.detail;
|
|
|
|
|
|
|
|
let hasHelp = false;
|
2021-05-28 02:16:34 +00:00
|
|
|
switch (this._type) {
|
2021-05-27 00:40:53 +00:00
|
|
|
case 'custom':
|
|
|
|
case 'custom-json':
|
2021-05-30 16:41:19 +00:00
|
|
|
case 'text-to-speech':
|
|
|
|
case 'text-to-speech-reading':
|
2021-05-27 00:40:53 +00:00
|
|
|
hasHelp = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
menu.bodyNode.querySelector('.popup-menu-item[data-menu-action=help]').hidden = !hasHelp;
|
|
|
|
}
|
|
|
|
|
2021-05-28 02:16:34 +00:00
|
|
|
_onMenuClose(e) {
|
2021-01-20 01:52:57 +00:00
|
|
|
switch (e.detail.action) {
|
2021-05-27 00:40:53 +00:00
|
|
|
case 'help':
|
2021-05-28 02:16:34 +00:00
|
|
|
this._showHelp(this._type);
|
2021-05-27 00:40:53 +00:00
|
|
|
break;
|
2020-10-18 17:18:57 +00:00
|
|
|
case 'remove':
|
2021-05-28 02:16:34 +00:00
|
|
|
this._parent.removeSource(this);
|
2020-10-18 17:18:57 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-05-29 23:56:38 +00:00
|
|
|
}
|
2021-05-27 00:40:53 +00:00
|
|
|
|
2021-05-28 02:16:34 +00:00
|
|
|
async _setType(value) {
|
|
|
|
this._type = value;
|
2021-05-30 16:41:19 +00:00
|
|
|
this._updateTypeParameter();
|
|
|
|
await this._parent.settingsController.setProfileSetting(`audio.sources[${this._index}].type`, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
async _setUrl(value) {
|
|
|
|
this._url = value;
|
|
|
|
await this._parent.settingsController.setProfileSetting(`audio.sources[${this._index}].url`, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
async _setVoice(value) {
|
|
|
|
this._voice = value;
|
|
|
|
await this._parent.settingsController.setProfileSetting(`audio.sources[${this._index}].voice`, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
_updateTypeParameter() {
|
|
|
|
let field = null;
|
|
|
|
switch (this._type) {
|
|
|
|
case 'custom':
|
|
|
|
case 'custom-json':
|
|
|
|
field = 'url';
|
|
|
|
break;
|
|
|
|
case 'text-to-speech':
|
|
|
|
case 'text-to-speech-reading':
|
|
|
|
field = 'voice';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for (const node of this._node.querySelectorAll('.audio-source-parameter-container')) {
|
|
|
|
node.hidden = (field === null || node.dataset.field !== field);
|
|
|
|
}
|
2021-05-28 02:16:34 +00:00
|
|
|
}
|
|
|
|
|
2021-05-27 00:40:53 +00:00
|
|
|
_showHelp(type) {
|
|
|
|
switch (type) {
|
|
|
|
case 'custom':
|
|
|
|
this._showModal('audio-source-help-custom');
|
|
|
|
break;
|
|
|
|
case 'custom-json':
|
|
|
|
this._showModal('audio-source-help-custom-json');
|
|
|
|
break;
|
2021-05-30 16:41:19 +00:00
|
|
|
case 'text-to-speech':
|
|
|
|
case 'text-to-speech-reading':
|
|
|
|
this._parent.setTestVoice(this._voice);
|
|
|
|
this._showModal('audio-source-help-text-to-speech');
|
|
|
|
break;
|
2021-05-27 00:40:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_showModal(name) {
|
2021-05-28 02:16:34 +00:00
|
|
|
this._parent.modalController.getModal(name).setVisible(true);
|
2021-05-27 00:40:53 +00:00
|
|
|
}
|
2019-12-12 02:15:51 +00:00
|
|
|
}
|