fetch('https://api.tryhamsa.com/v1/realtime/tts-stream', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Token YOUR_API_KEY'
},
body: JSON.stringify({ speaker: 'Ali', dialect: 'pls', text: 'Hello, world!', mulaw: false })
})
.then(response => {
const reader = response.body.getReader();
const chunks = [];
function read() {
reader.read().then(({ done, value }) => {
if (done) {
// Concatenate chunks and play entire stream
const blob = new Blob(chunks, { type: 'audio/wav' });
const url = URL.createObjectURL(blob);
const audio = new Audio(url);
audio.play();
return;
}
chunks.push(value);
read();
});
}
read();
})
.catch(console.error);"00000000: 52 49 46 46 FF FF FF FF 57 41 56 45 66 6D 74 20 "{
"code": 123,
"message": "<string>"
}{
"code": 123,
"message": "<string>"
}{
"code": 123,
"message": "<string>"
}Generate Streamed Text to Speech File Data
From the user’s perspective, this is a standard request. In the response, we include specific headers: ‘Transfer-Encoding’ is set to ‘chunked’ to enable streaming, ‘Connection’ is set to ‘keep-alive’ to maintain the connection, and ‘Content-Type’ is set to ‘audio/wav’ to indicate the media type. These headers allow the client to stream audio data from the server in real time. Important Note: after collecting the chunks, you need to add the wav header manually to the data. If you wish to get a wav header, please use the Generate TTS File Data API.
fetch('https://api.tryhamsa.com/v1/realtime/tts-stream', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Token YOUR_API_KEY'
},
body: JSON.stringify({ speaker: 'Ali', dialect: 'pls', text: 'Hello, world!', mulaw: false })
})
.then(response => {
const reader = response.body.getReader();
const chunks = [];
function read() {
reader.read().then(({ done, value }) => {
if (done) {
// Concatenate chunks and play entire stream
const blob = new Blob(chunks, { type: 'audio/wav' });
const url = URL.createObjectURL(blob);
const audio = new Audio(url);
audio.play();
return;
}
chunks.push(value);
read();
});
}
read();
})
.catch(console.error);"00000000: 52 49 46 46 FF FF FF FF 57 41 56 45 66 6D 74 20 "{
"code": 123,
"message": "<string>"
}{
"code": 123,
"message": "<string>"
}{
"code": 123,
"message": "<string>"
}Authorizations
Pass the API key in the Authorization header, You need to put Token keyword before the API key. e.g. 'Authorization: Token '
Body
The text to be generated as an aduio file.
"مرحباً بكم في جميعاً في همسة!"
The speaker (voice) name. Pick a voice that matches the chosen dialect — see the dialect field for voice examples per dialect. You can also pass the UUID of a custom cloned voice, but you must first preload it via the Preload Cloned Voice endpoint.
"Amjad"
The dialect to synthesize, identified by its country/region code. Voice examples per dialect:
pls(Palestinian) — Amjad, Layanegy(Egyptian) — Mariam, Samirsyr(Syrian) — Dalal, Maisirq(Iraqi) — Lyali, Fatmajor(Jordanian) — Lana, Jasemleb(Lebanese) — Carla, Majdksa(Saudi) — Hiba, Fahduae(Emirati) — Salma, Dimabah(Bahraini) — Mazen, Rubaqat(Qatari) — Deema, Faisalkuw(Kuwaiti) — Mai, Hatemoma(Omani) — Aisha, Jabermsa(Modern Standard Arabic / Fusha) — Salem, Tamimar-sa(Arabic – Gulf) — Khalid, Rahmaen(English) — Emma, James
pls, egy, syr, irq, jor, leb, ksa, uae, bah, qat, kuw, oma, msa, ar-sa, en "pls"
Whether to use 'Mu-Law' algorithm in voice signal processing or not.
false
Output sample rate of the PCM audio. Defaults to 16k. Only applies to PCM output — cannot be combined with mulaw (mu-law output is always 8 kHz).
8k, 16k "16k"
Controls how expressive the generated speech sounds. 0 is flat and monotone, 1 is the natural default, and 2 is highly expressive.
0 <= x <= 21
Response
Streamed Successful Response - Chunked
The response is of type file.
Was this page helpful?