All files / src/utils text-to-speech.ts

5.26% Statements 1/19
100% Branches 0/0
0% Functions 0/1
5.26% Lines 1/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 221x                                          
import {Predictions} from "@aws-amplify/predictions";
import type {VoiceId} from "@aws-sdk/client-polly";
 
export async function textToSpeech(text: string, voice: VoiceId): Promise<HTMLAudioElement> {
    const speech = await Predictions.convert({
        textToSpeech: {
            source: {
                text: text,
            },
            voiceId: voice,
        },
    });
    const audioStream = speech.audioStream;
    const audioBlob = new Blob([audioStream], {
        type: "audio/mp3",
    });
    const audioURL = URL.createObjectURL(audioBlob);
    const audio = new Audio(audioURL);
    await audio.play();
    return audio;
}