import { TTSClient, VoiceStyle } from '@lokutor/sdk';
const audioCtx = new AudioContext();
let nextTime = audioCtx.currentTime;
const client = new TTSClient({
apiKey: import.meta.env.VITE_LOKUTOR_API_KEY,
});
await client.synthesize({
text: 'Hello from Lokutor.',
voice: VoiceStyle.F1,
onAudio: (pcm16) => {
const int16 = new Int16Array(pcm16.buffer, pcm16.byteOffset, pcm16.length / 2);
const float32 = new Float32Array(int16.length);
for (let i = 0; i < int16.length; i++) float32[i] = int16[i] / 32768;
const buffer = audioCtx.createBuffer(1, float32.length, 44100);
buffer.getChannelData(0).set(float32);
const source = audioCtx.createBufferSource();
source.buffer = buffer;
source.connect(audioCtx.destination);
source.start(nextTime);
nextTime += buffer.duration;
},
});
console.log('Playback complete');