diff --git a/src/API.ts b/src/API.ts index 23da42a..1e6a315 100644 --- a/src/API.ts +++ b/src/API.ts @@ -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 // ============================================================= diff --git a/src/Documentation.ts b/src/Documentation.ts index 349f724..2f94e59 100644 --- a/src/Documentation.ts +++ b/src/Documentation.ts @@ -1577,6 +1577,27 @@ mod(0.25) :: sound('sine') **Note:** you can also set the _modulation index_ and the _harmonic ratio_ with the fm argument. You will have to feed both as a string: fm('2:4'). 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 = `