38 lines
888 B
TypeScript
38 lines
888 B
TypeScript
import { TTSModule, TTSResponse } from '../tts';
|
|
|
|
import { VoicesManager, Communicate } from 'edge-tts-universal';
|
|
|
|
class AzureTTS implements TTSModule {
|
|
private voices: Array<string> | undefined = undefined;
|
|
|
|
public name: string = 'Azure';
|
|
|
|
async getVoices(): Promise<Array<string> | undefined> {
|
|
if (!this.voices) {
|
|
const voiceMgr = await VoicesManager.create();
|
|
const voiceQuery = await voiceMgr.find({});
|
|
|
|
this.voices = voiceQuery.map((voice) => voice.ShortName);
|
|
}
|
|
|
|
return this.voices;
|
|
}
|
|
|
|
async generate(voice: string, text: string): Promise<TTSResponse> {
|
|
const comm = new Communicate(text, {
|
|
voice: voice
|
|
});
|
|
|
|
const buffers: Buffer[] = [];
|
|
for await (const chunk of comm.stream()) {
|
|
if (chunk.type === 'audio' && chunk.data) {
|
|
buffers.push(chunk.data);
|
|
}
|
|
}
|
|
|
|
return { data: Buffer.concat(buffers) };
|
|
}
|
|
}
|
|
|
|
export default new AzureTTS();
|