Skip to content

Instantly share code, notes, and snippets.

@mecab
Last active May 25, 2022 16:33
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mecab/a14a761d76a9136a96466b0fc4c2acc3 to your computer and use it in GitHub Desktop.
Save mecab/a14a761d76a9136a96466b0fc4c2acc3 to your computer and use it in GitHub Desktop.
document.addEventListener('onEventReceived', function(obj) {
const synth = window.speechSynthesis;
if (obj.detail.command === 'PRIVMSG') {
var utter = new SpeechSynthesisUtterance(obj.detail.body);
utter.voice = synth.getVoices().filter(v => v.lang === 'ja-JP')[0];
utter.rate = 1;
utter.pitch = 1;
synth.speak(utter);
}
});
@kimitaka
Copy link

What kind of license is applied for this code?

@mecab
Copy link
Author

mecab commented Mar 18, 2020

I release above code under CC0.
( https://creativecommons.org/publicdomain/zero/1.0/deed )

@kimitaka
Copy link

I see. Thank you.

@mecab
Copy link
Author

mecab commented Aug 22, 2020

If it doesn't work use the following instead;
(Released under CC0)

window.addEventListener('message', function(e) {
  const data = e.data || {};
  if (data.type !== 'item')
    return;
  const message = data.message || {};
  const obj = { detail: { command: message.command, body: message.body, from: message.from }};
  
  const synth = window.speechSynthesis;
  if (obj.detail.command === 'PRIVMSG') {
    var utter = new SpeechSynthesisUtterance(obj.detail.body);
    utter.voice = synth.getVoices().filter(v => v.lang === 'ja-JP')[0];
    utter.rate = 1;
    utter.pitch = 1;
    synth.speak(utter);
  }
});

EDIT: Applied above to the gist. Previous version can be seen at https://gist.github.com/mecab/a14a761d76a9136a96466b0fc4c2acc3/e93bca2e683b30ca2ccbb03702972be20734012f

@yuichitanakadesu
Copy link

Thank you for releasing this amazing code under CC0.
When I implement this codes into Chat Box in Streamlabs, it reads out each comment three times repeatedly.
Do you know how to fix it?

@mecab
Copy link
Author

mecab commented May 25, 2022

@yuichitanakadesu Sorry for the very late reply. I got some reports that recent OBS seems to read comments by itself (i.e., the browser inside OBS can use speech API), I haven't confirmed yet though. In this case, you won't need the external browser anymore so please just close it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment