Clone a voice
Upload a recording and use the new voice to generate speech.
1. Upload your recording
This example uploads sample.wav and names the voice narrator.
import { readFile } from "node:fs/promises";
const form = new FormData();
form.set("file", new Blob([await readFile("sample.wav")], { type: "audio/wav" }), "sample.wav");
form.set("name", "narrator");
form.set("provider", "soniox");
form.set("consent", "true");
const response = await fetch("https://api.allmodels.io/v1/custom-voices", {
method: "POST",
headers: { Authorization: `Bearer ${process.env.ALLMODELS_API_KEY}` },
body: form
});
if (!response.ok) throw new Error(await response.text());
const result = await response.json();import os
import requests
with open("sample.wav", "rb") as audio:
response = requests.post(
"https://api.allmodels.io/v1/custom-voices",
headers={"Authorization": f"Bearer {os.environ['ALLMODELS_API_KEY']}"},
files={"file": ("sample.wav", audio, "audio/wav")},
data={"name": "narrator", "provider": "soniox", "consent": "true"},
)
response.raise_for_status()
result = response.json()curl https://api.allmodels.io/v1/custom-voices \
-H "Authorization: Bearer $ALLMODELS_API_KEY" \
-F "file=@sample.wav" \
-F "name=narrator" \
-F "provider=soniox" \
-F "consent=true" \
--output cloned-voice.jsonFor best results:
- Get the speaker's consent before cloning their voice.
- Record only one speaker for one to two minutes.
- Have the speaker talk naturally about a familiar subject. Avoid scripts and recitals.
- Record in a quiet room without music, background noise, or other voices.
- Keep the microphone at a fixed distance and the volume consistent. Avoid echo and distortion.
- Use WAV when possible. Check the selected provider's requirements for other formats and size limits.
2. Copy the voice ID
The response includes the voice ID in id. In this example it is AM:@narrator.
const voiceId = result.id;
console.log(voiceId);voice_id = result["id"]
print(voice_id)VOICE_ID=$(jq -r '.id' cloned-voice.json)
echo "$VOICE_ID"3. Generate speech
Use the voice ID in the voice field:
import { writeFile } from "node:fs/promises";
const speech = await fetch("https://api.allmodels.io/oai/audio/speech", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.ALLMODELS_API_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
model: "soniox/tts-rt-v2",
voice: voiceId,
input: "Hello from my cloned voice.",
response_format: "mp3"
})
});
if (!speech.ok) throw new Error(await speech.text());
await writeFile("speech.mp3", Buffer.from(await speech.arrayBuffer()));speech = requests.post(
"https://api.allmodels.io/oai/audio/speech",
headers={"Authorization": f"Bearer {os.environ['ALLMODELS_API_KEY']}"},
json={
"model": "soniox/tts-rt-v2",
"voice": voice_id,
"input": "Hello from my cloned voice.",
"response_format": "mp3",
},
)
speech.raise_for_status()
with open("speech.mp3", "wb") as output:
output.write(speech.content)curl https://api.allmodels.io/oai/audio/speech \
-H "Authorization: Bearer $ALLMODELS_API_KEY" \
-H "Content-Type: application/json" \
--data "{\"model\":\"soniox/tts-rt-v2\",\"voice\":\"$VOICE_ID\",\"input\":\"Hello from my cloned voice.\",\"response_format\":\"mp3\"}" \
--output speech.mp3The generated audio is saved as speech.mp3.
See the custom voice API reference for provider requirements and the text-to-speech guide for more ways to generate speech.
