artcraft/old_code/voder-vocodes-1.0/frontends/admin/src/TtsService.ts

54 lines
1.2 KiB
TypeScript

import Howl from 'howler';
import ApiConfig from './ApiConfig';
/// Requests to the backend.
class TtsRequest {
text: String
speaker: String
// Which model to use
melgan_model?: Number
arpabet_tacotron_model?: Number
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, apiConfig: ApiConfig): Promise<TtsResponse> => {
const request = new TtsRequest(sentence);
const url = apiConfig.getEndpoint('/advanced_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};