Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speech synthesis #6

Open
MzHub opened this issue Apr 10, 2022 · 1 comment
Open

Speech synthesis #6

MzHub opened this issue Apr 10, 2022 · 1 comment

Comments

@MzHub
Copy link

MzHub commented Apr 10, 2022

I saw speech synthesis has been suggested. I realize it would be a lot of work, and that browser extensions may be a better solution.

That said, I wanted to let you know, that the speech synthesis part itself is very simple. I've added this to all my tiny Japanese learning web apps.

let speak;
{
  // Speech synthesis
  const synth = window.speechSynthesis;
  let voices = synth.getVoices();
  speechSynthesis.onvoiceschanged = () => { voices = synth.getVoices() };
  speak = function (text) {
    let selectedVoice;
    for (const voice of voices) {
      if (voice.name.toLowerCase().includes('japan') || voice.lang.toLowerCase().includes('ja-jp')) {
        selectedVoice = voice;
      }
    }
    const utterance = new SpeechSynthesisUtterance(text);
    utterance.voice = selectedVoice;
    utterance.pitch = 1;
    utterance.rate = 1;
    synth.speak(utterance);
  };
}

Usage:

speak('こんにちは');

If you end up using it, you might want to add a check for whether Japanese voice was found, and make it do nothing if not. Currently it will default to whatever is the default voice, which is not nice for a public project.

Either way, cool project! And feel free to ignore this, just wanted to let you know in case it ends up being useful.

EDIT: So something like this:

    let selectedVoice;
    let hasJapaneseVoice = false;
    for (const voice of voices) {
      if (voice.name.toLowerCase().includes('japan') || voice.lang.toLowerCase().includes('ja-jp')) {
        selectedVoice = voice;
        hasJapaneseVoice = true;
        break;
      }
    }
    if ( ! hasJapaneseVoice) {
      return;
    }
@DrDru
Copy link
Owner

DrDru commented Apr 11, 2022

Thanks a lot for the kind word and for the suggestion. I bear that in mind and will play with it. It mind be useful in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants