diff --git a/tts_frontend/src/TtsService.ts b/tts_frontend/src/TtsService.ts new file mode 100644 index 0000000000..cfd86d578f --- /dev/null +++ b/tts_frontend/src/TtsService.ts @@ -0,0 +1,49 @@ +import Howl from 'howler'; + +/// Requests to the backend. +class TtsRequest { + text: String + speaker: String + + constructor(text: String) { + this.text = text; + // LJSpeech dataset from Keith Ito & LibriVox, spoken by Linda Johnson. + this.speaker = 'linda'; + } +} + +class TtsResponse { + text: string + howl: Howl + + constructor(text: string, howl: Howl) { + this.text = text; + this.howl = howl; + } +} + +let makeTtsRequest = (sentence: string): Promise => { + const request = new TtsRequest(sentence); + const url = `/tts`; + + return fetch(url, { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json', + }, + body: JSON.stringify(request), + }) + .then(res => res.blob()) + .then(blob => { + const url = window.URL.createObjectURL(blob); + const howlSound = new Howl.Howl({ + src: [url], + format: 'wav', + }); + + return new TtsResponse(sentence, howlSound); + }); +} + +export {TtsRequest, TtsResponse, makeTtsRequest}; \ No newline at end of file