Generating using the API
Voice snippets can be generated programmatically using the generateVoice API.
This operation streams either audio/mpeg
or audio/wav
(depending on the selected voice) binary data which minimizes playout latency.
This API should be used directly in your client application code. Since your Gabber API key needs to be kept secret, you
will need to authenticate with a usage token generated from your backend with a voice_synthesis_seconds
limit set.
- Typescript
const usageToken = await fetchUsageTokenFromYourBackend();
const response = await fetch('https://api.gabber.ai/v1/voice/generate', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${usageToken}`
},
body: JSON.stringify({
text: 'Hello, world!',
voice: '<gabber-voice-id>',
})
});
// Decode and playback the audio using WebAudio
const audioContext = new AudioContext();
buffer = await audioContext.decodeAudioData(arrayBuffer);
const source = audioContext.createBufferSource();
source.buffer = buffer;
source.connect(audioContext.destination);
source.start(0);
source.onended = () => {
console.log('Playback finished');
};