alessandro trinca tornidor commited on
Commit
89dc4a6
·
1 Parent(s): 20172dc

feat: add error message in case of missing TTS voice for the selected language

Browse files
Files changed (1) hide show
  1. aip_trainer/lambdas/js.py +25 -7
aip_trainer/lambdas/js.py CHANGED
@@ -27,6 +27,7 @@ function playAudio(text, language) {
27
  let voice_idx = 0;
28
  let voice_synth = null;
29
  let synth = window.speechSynthesis;
 
30
 
31
  function setSpeech() {
32
  return new Promise(
@@ -43,17 +44,33 @@ function playAudio(text, language) {
43
  )
44
  }
45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  let s = setSpeech();
47
  s.then((voices) => {
48
- for (idx = 0; idx < voices.length; idx++) {
49
- if (voices[idx].lang.slice(0, 2) == language) {
50
- voice_synth = voices[idx];
51
- break;
52
- }
 
 
 
 
53
  }
54
-
55
  var utterThis = new SpeechSynthesisUtterance(text);
56
- utterThis.voice = voice_synth;
57
  utterThis.rate = 0.7;
58
 
59
  synth.speak(utterThis);
@@ -62,3 +79,4 @@ function playAudio(text, language) {
62
  });
63
  }
64
  """
 
 
27
  let voice_idx = 0;
28
  let voice_synth = null;
29
  let synth = window.speechSynthesis;
30
+ let voice_lang;
31
 
32
  function setSpeech() {
33
  return new Promise(
 
44
  )
45
  }
46
 
47
+ switch (language) {
48
+ case 'de':
49
+ voice_lang = 'de-DE';
50
+ break;
51
+ case 'en':
52
+ voice_lang = 'en-US';
53
+ break;
54
+ default:
55
+ console.log(`Sorry, we are out of ${expr}.`);
56
+ throw new Error(`Language ${language} not valid!`)
57
+ alert(`Language ${language} not valid!`)
58
+ }
59
+
60
  let s = setSpeech();
61
  s.then((voices) => {
62
+ let voicesSynth = voices.filter(voice => voice.lang === voice_lang);
63
+ if (voicesSynth.length === 0) {
64
+ console.error(`No voice found for language ${voice_lang}, retry for less restrictive check (startsWith)...`)
65
+ voicesSynth = voices.filter(voice => voice.lang.startsWith(language));
66
+ }
67
+ if (voicesSynth.length === 0) {
68
+ console.error(`No voice found for language ${voice_lang}/${language}, you should use the Text-To-Speech backend feature...`)
69
+ throw new Error(`No voice found for language ${voice_lang}/${language}, you should use the Text-To-Speech backend feature...`)
70
+ alert(`No voice found for language ${voice_lang}/${language}, you should use the Text-To-Speech backend feature...`)
71
  }
 
72
  var utterThis = new SpeechSynthesisUtterance(text);
73
+ utterThis.voice = voicesSynth[0];
74
  utterThis.rate = 0.7;
75
 
76
  synth.speak(utterThis);
 
79
  });
80
  }
81
  """
82
+ 1