Update audio validity checks to be based on the source (#1290)

This commit is contained in:
toasted-nutbread 2021-01-22 21:09:43 -05:00 committed by GitHub
parent 55df9dc7cd
commit a51a591c40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 19 deletions

View File

@ -53,7 +53,7 @@ class AudioDownloader {
switch (info.type) { switch (info.type) {
case 'url': case 'url':
try { try {
return await this._downloadAudioFromUrl(info.url); return await this._downloadAudioFromUrl(info.url, source);
} catch (e) { } catch (e) {
// NOP // NOP
} }
@ -192,7 +192,7 @@ class AudioDownloader {
return [{type: 'url', url}]; return [{type: 'url', url}];
} }
async _downloadAudioFromUrl(url) { async _downloadAudioFromUrl(url, source) {
const response = await this._requestBuilder.fetchAnonymous(url, { const response = await this._requestBuilder.fetchAnonymous(url, {
method: 'GET', method: 'GET',
mode: 'cors', mode: 'cors',
@ -208,22 +208,29 @@ class AudioDownloader {
const arrayBuffer = await response.arrayBuffer(); const arrayBuffer = await response.arrayBuffer();
if (!await this._isAudioBinaryValid(arrayBuffer)) { if (!await this._isAudioBinaryValid(arrayBuffer, source)) {
throw new Error('Could not retrieve audio'); throw new Error('Could not retrieve audio');
} }
return this._arrayBufferToBase64(arrayBuffer); return this._arrayBufferToBase64(arrayBuffer);
} }
async _isAudioBinaryValid(arrayBuffer) { async _isAudioBinaryValid(arrayBuffer, source) {
switch (source) {
case 'jpod101':
{
const digest = await this._arrayBufferDigest(arrayBuffer); const digest = await this._arrayBufferDigest(arrayBuffer);
switch (digest) { switch (digest) {
case 'ae6398b5a27bc8c0a771df6c907ade794be15518174773c58c7c7ddd17098906': // jpod101 invalid audio case 'ae6398b5a27bc8c0a771df6c907ade794be15518174773c58c7c7ddd17098906': // Invalid audio
return false; return false;
default: default:
return true; return true;
} }
} }
default:
return true;
}
}
async _arrayBufferDigest(arrayBuffer) { async _arrayBufferDigest(arrayBuffer) {
const hash = new Uint8Array(await crypto.subtle.digest('SHA-256', new Uint8Array(arrayBuffer))); const hash = new Uint8Array(await crypto.subtle.digest('SHA-256', new Uint8Array(arrayBuffer)));

View File

@ -51,7 +51,7 @@ class AudioSystem {
const info = infoList[j]; const info = infoList[j];
let audio; let audio;
try { try {
audio = await this.createAudioFromInfo(info); audio = await this.createAudioFromInfo(info, source);
} catch (e) { } catch (e) {
continue; continue;
} }
@ -72,11 +72,11 @@ class AudioSystem {
return this._fallbackAudio; return this._fallbackAudio;
} }
createAudio(url) { createAudio(url, source) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const audio = new Audio(url); const audio = new Audio(url);
audio.addEventListener('loadeddata', () => { audio.addEventListener('loadeddata', () => {
if (!this._isAudioValid(audio)) { if (!this._isAudioValid(audio, source)) {
reject(new Error('Could not retrieve audio')); reject(new Error('Could not retrieve audio'));
} else { } else {
resolve(audio); resolve(audio);
@ -94,10 +94,10 @@ class AudioSystem {
return new TextToSpeechAudio(text, voice); return new TextToSpeechAudio(text, voice);
} }
async createAudioFromInfo(info) { async createAudioFromInfo(info, source) {
switch (info.type) { switch (info.type) {
case 'url': case 'url':
return await this.createAudio(info.url); return await this.createAudio(info.url, source);
case 'tts': case 'tts':
return this.createTextToSpeechAudio(info.text, info.voice); return this.createTextToSpeechAudio(info.text, info.voice);
default: default:
@ -107,13 +107,20 @@ class AudioSystem {
// Private // Private
_isAudioValid(audio) { _isAudioValid(audio, source) {
switch (source) {
case 'jpod101':
{
const duration = audio.duration; const duration = audio.duration;
return ( return (
duration !== 5.694694 && // jpod101 invalid audio (Chrome) duration !== 5.694694 && // Invalid audio (Chrome)
duration !== 5.720718 // jpod101 invalid audio (Firefox) duration !== 5.720718 // Invalid audio (Firefox)
); );
} }
default:
return true;
}
}
_getTextToSpeechVoiceFromVoiceUri(voiceUri) { _getTextToSpeechVoiceFromVoiceUri(voiceUri) {
try { try {