Add speech synthesis

This commit is contained in:
2023-08-30 18:40:34 +03:00
parent 43eff8bda2
commit e8071493f8
2 changed files with 39 additions and 0 deletions

View File

@ -1287,6 +1287,24 @@ export class UserAPI {
abs = Math.abs;
// =============================================================
// Speed synthesis
// =============================================================
speak = (text: string, index: number): void => {
/*
* Speaks the given text using the browser's speech synthesis API.
* @param text - The text to speak
* @param index - The index of the voice to use
*/
const synth = window.speechSynthesis;
synth.cancel();
const utterance = new SpeechSynthesisUtterance(text);
utterance.voice = speechSynthesis.getVoices()[index];
synth.speak(utterance);
}
// =============================================================
// Trivial functions
// =============================================================

View File

@ -1577,6 +1577,27 @@ mod(0.25) :: sound('sine')
**Note:** you can also set the _modulation index_ and the _harmonic ratio_ with the <icode>fm</icode> argument. You will have to feed both as a string: <icode>fm('2:4')</icode>. If you only feed one number, only the _modulation index_ will be updated.
# Speech synthesis
Topos can also speak using [Web Speec API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Speech_API)!
${makeExample(
"Hello world!",
`
mod(4) :: speak("Hello world!")
`,
false
)}
${makeExample(
"Speak with a different voice",
`
mod(2) :: speak("Topos!",irand(0,25))
`,
false
)}
`;
const about: string = `