Audio system improvements (#1268)

* Simplify API

* Move fallback audio
This commit is contained in:
toasted-nutbread 2021-01-17 23:05:15 -05:00 committed by GitHub
parent d21de9df00
commit 887150e012
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 18 deletions

View File

@ -22,9 +22,7 @@
class AudioController { class AudioController {
constructor(settingsController) { constructor(settingsController) {
this._settingsController = settingsController; this._settingsController = settingsController;
this._audioSystem = new AudioSystem({ this._audioSystem = new AudioSystem(false);
cacheSize: 0
});
this._audioSourceContainer = null; this._audioSourceContainer = null;
this._audioSourceAddButton = null; this._audioSourceAddButton = null;
this._audioSourceEntries = []; this._audioSourceEntries = [];

View File

@ -18,12 +18,13 @@
/* global /* global
* CacheMap * CacheMap
* TextToSpeechAudio * TextToSpeechAudio
* api
*/ */
class AudioSystem { class AudioSystem {
constructor({getAudioInfo, cacheSize=32}) { constructor(useCache) {
this._cache = new CacheMap(cacheSize); this._cache = new CacheMap(useCache ? 32 : 0);
this._getAudioInfo = getAudioInfo; this._fallbackAudio = null;
} }
prepare() { prepare() {
@ -81,6 +82,13 @@ class AudioSystem {
throw new Error('Could not create audio'); throw new Error('Could not create audio');
} }
getFallbackAudio() {
if (this._fallbackAudio === null) {
this._fallbackAudio = new Audio('/mixed/mp3/button.mp3');
}
return this._fallbackAudio;
}
createAudio(url) { createAudio(url) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const audio = new Audio(url); const audio = new Audio(url);
@ -105,6 +113,10 @@ class AudioSystem {
// Private // Private
async _getAudioInfo(source, expression, reading, details) {
return await api.getDefinitionAudioInfo(source, expression, reading, details);
}
_isAudioValid(audio) { _isAudioValid(audio) {
const duration = audio.duration; const duration = audio.duration;
return ( return (

View File

@ -46,10 +46,7 @@ class Display extends EventDispatcher {
this._options = null; this._options = null;
this._index = 0; this._index = 0;
this._audioPlaying = null; this._audioPlaying = null;
this._audioFallback = null; this._audioSystem = new AudioSystem(true);
this._audioSystem = new AudioSystem({
getAudioInfo: this._getAudioInfo.bind(this)
});
this._styleNode = null; this._styleNode = null;
this._eventListeners = new EventListenerCollection(); this._eventListeners = new EventListenerCollection();
this._setContentToken = null; this._setContentToken = null;
@ -1234,10 +1231,7 @@ class Display extends EventDispatcher {
({audio, index} = await this._audioSystem.createDefinitionAudio(sources, expression, reading, {textToSpeechVoice, customSourceUrl})); ({audio, index} = await this._audioSystem.createDefinitionAudio(sources, expression, reading, {textToSpeechVoice, customSourceUrl}));
info = `From source ${1 + index}: ${sources[index]}`; info = `From source ${1 + index}: ${sources[index]}`;
} catch (e) { } catch (e) {
if (this._audioFallback === null) { audio = this._audioSystem.getFallbackAudio();
this._audioFallback = new Audio('/mixed/mp3/button.mp3');
}
audio = this._audioFallback;
info = 'Could not find audio'; info = 'Could not find audio';
} }
@ -1569,10 +1563,6 @@ class Display extends EventDispatcher {
return {type, expression, reading}; return {type, expression, reading};
} }
async _getAudioInfo(source, expression, reading, details) {
return await api.getDefinitionAudioInfo(source, expression, reading, details);
}
async _setOptionsContextIfDifferent(optionsContext) { async _setOptionsContextIfDifferent(optionsContext) {
if (deepEqual(this._optionsContext, optionsContext)) { return; } if (deepEqual(this._optionsContext, optionsContext)) { return; }
await this.setOptionsContext(optionsContext); await this.setOptionsContext(optionsContext);