Gemini API, टेक्स्ट इनपुट को एक या एक से ज़्यादा लोगों की आवाज़ वाले ऑडियो में बदल सकता है. इसके लिए, यह Gemini की लिखे गए शब्दों को सुनने की सुविधा (टीटीएस) का इस्तेमाल करता है. लिखे गए शब्दों को सुनने की सुविधा (टीटीएस) को कंट्रोल किया जा सकता है. इसका मतलब है कि बातचीत को स्ट्रक्चर करने के लिए, सामान्य भाषा का इस्तेमाल किया जा सकता है. साथ ही, ऑडियो की स्टाइल, उच्चारण, रफ़्तार, और टोन को गाइड किया जा सकता है.
टीटीएस की सुविधा, Live API के ज़रिए उपलब्ध कराई गई स्पीच जनरेशन की सुविधा से अलग है. इसे इंटरैक्टिव, अनस्ट्रक्चर्ड ऑडियो, और मल्टीमॉडल इनपुट और आउटपुट के लिए डिज़ाइन किया गया है. लाइव एपीआई, बातचीत के कॉन्टेक्स्ट को डाइनैमिक तरीके से समझने में बेहतर है. वहीं, Gemini API के ज़रिए टीटीएस की सुविधा, उन स्थितियों के लिए तैयार की गई है जिनमें स्टाइल और आवाज़ पर बारीकी से कंट्रोल करने के साथ-साथ, सटीक टेक्स्ट सुनाने की ज़रूरत होती है. जैसे, पॉडकास्ट या ऑडियो बुक जनरेट करना.
इस गाइड में, टेक्स्ट से एक या एक से ज़्यादा स्पीकर वाला ऑडियो जनरेट करने का तरीका बताया गया है.
शुरू करने से पहले
पक्का करें कि आपने Gemini 2.5 मॉडल के ऐसे वैरिएंट का इस्तेमाल किया हो जिसमें Gemini की टेक्स्ट-टू-स्पीच (टीटीएस) की सुविधा उपलब्ध हो. इसके बारे में साथ काम करने वाले मॉडल सेक्शन में बताया गया है. सबसे अच्छे नतीजों के लिए, यह तय करें कि आपके इस्तेमाल के हिसाब से कौनसा मॉडल सबसे सही है.
ऐप्लिकेशन बनाना शुरू करने से पहले, AI Studio में Gemini के टीटीएस मॉडल को टेस्ट करना आपके लिए फ़ायदेमंद हो सकता है.
एक व्यक्ति की आवाज़ में टीटीएस
टेक्स्ट को एक स्पीकर वाले ऑडियो में बदलने के लिए, रिस्पॉन्स मोड को "ऑडियो" पर सेट करें. इसके बाद, आवाज़ के नाम के साथ speech_config ऑब्जेक्ट पास करें.
आपको पहले से मौजूद आउटपुट की आवाज़ों में से, किसी आवाज़ का नाम चुनना होगा.
इस उदाहरण में, मॉडल से मिले आउटपुट ऑडियो को वेव फ़ाइल में सेव किया गया है:
Python
from google import genai
import wave
import base64
def wave_file(filename, pcm, channels=1, rate=24000, sample_width=2):
with wave.open(filename, "wb") as wf:
wf.setnchannels(channels)
wf.setsampwidth(sample_width)
wf.setframerate(rate)
wf.writeframes(pcm)
client = genai.Client()
interaction = client.interactions.create(
model="gemini-3.1-flash-tts-preview",
input="Say cheerfully: Have a wonderful day!",
response_format={"type": "audio"},
generation_config={
"speech_config": [
{"voice": "Kore"}
]
}
)
wave_file('out.wav', base64.b64decode(interaction.output_audio.data))
JavaScript
import {GoogleGenAI} from '@google/genai';
import wav from 'wav';
async function saveWaveFile(
filename,
pcmData,
channels = 1,
rate = 24000,
sampleWidth = 2,
) {
return new Promise((resolve, reject) => {
const writer = new wav.FileWriter(filename, {
channels,
sampleRate: rate,
bitDepth: sampleWidth * 8,
});
writer.on('finish', resolve);
writer.on('error', reject);
writer.write(pcmData);
writer.end();
});
}
async function main() {
const client = new GoogleGenAI({});
const interaction = await client.interactions.create({
model: "gemini-3.1-flash-tts-preview",
input: "Say cheerfully: Have a wonderful day!",
response_format: { type: 'audio' },
generation_config: {
speech_config: [
{ voice: 'Kore' }
]
},
});
const audioBuffer = Buffer.from(interaction.output_audio.data, 'base64');
await saveWaveFile('out.wav', audioBuffer);
}
await main();
REST
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gemini-3.1-flash-tts-preview",
"input": "Say cheerfully: Have a wonderful day!",
"response_format": {
"type": "audio"
},
"generation_config": {
"speech_config": [
{ "voice": "Kore" }
]
}
}'
जनरेट किए गए ऑडियो डेटा को वापस पाने के लिए, interaction.output_audio
प्रॉपर्टी का इस्तेमाल किया जा सकता है. यह प्रॉपर्टी, जनरेट किए गए आखिरी ऑडियो ब्लॉक को वापस लाती है. सुविधा से जुड़ी प्रॉपर्टी के बारे में ज़्यादा जानने के लिए, इंटरैक्शन की खास जानकारी देखें.
एक से ज़्यादा लोगों की आवाज़ में टीटीएस
एक से ज़्यादा स्पीकर वाले ऑडियो के लिए, आपको एक multi_speaker_voice_config ऑब्जेक्ट की ज़रूरत होगी. इसमें हर स्पीकर (दो तक) को speaker_voice_config के तौर पर कॉन्फ़िगर किया गया हो.
आपको हर speaker को उन नामों से तय करना होगा जिनका इस्तेमाल प्रॉम्प्ट में किया गया है:
Python
from google import genai
import wave
import base64
def wave_file(filename, pcm, channels=1, rate=24000, sample_width=2):
with wave.open(filename, "wb") as wf:
wf.setnchannels(channels)
wf.setsampwidth(sample_width)
wf.setframerate(rate)
wf.writeframes(pcm)
client = genai.Client()
prompt = """TTS the following conversation between Joe and Jane:
Joe: How's it going today Jane?
Jane: Not too bad, how about you?"""
interaction = client.interactions.create(
model="gemini-3.1-flash-tts-preview",
input=prompt,
response_format={"type": "audio"},
generation_config={
"speech_config": [
{"speaker": "Joe", "voice": "Kore"},
{"speaker": "Jane", "voice": "Puck"}
]
}
)
wave_file('out.wav', base64.b64decode(interaction.output_audio.data))
JavaScript
import {GoogleGenAI} from '@google/genai';
import wav from 'wav';
async function saveWaveFile(
filename,
pcmData,
channels = 1,
rate = 24000,
sampleWidth = 2,
) {
return new Promise((resolve, reject) => {
const writer = new wav.FileWriter(filename, {
channels,
sampleRate: rate,
bitDepth: sampleWidth * 8,
});
writer.on('finish', resolve);
writer.on('error', reject);
writer.write(pcmData);
writer.end();
});
}
async function main() {
const client = new GoogleGenAI({});
const prompt = `TTS the following conversation between Joe and Jane:
Joe: How's it going today Jane?
Jane: Not too bad, how about you?`;
const interaction = await client.interactions.create({
model: "gemini-3.1-flash-tts-preview",
input: prompt,
response_format: { type: 'audio' },
generation_config: {
speech_config: [
{ speaker: 'Joe', voice: 'Kore' },
{ speaker: 'Jane', voice: 'Puck' }
]
},
});
const audioBuffer = Buffer.from(interaction.output_audio.data, 'base64');
await saveWaveFile('out.wav', audioBuffer);
}
await main();
REST
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gemini-3.1-flash-tts-preview",
"input": "TTS the following conversation between Joe and Jane: Joe: Hows it going today Jane? Jane: Not too bad, how about you?",
"response_format": {
"type": "audio"
},
"generation_config": {
"speech_config": [
{ "speaker": "Joe", "voice": "Kore" },
{ "speaker": "Jane", "voice": "Puck" }
]
}
}'
प्रॉम्प्ट की मदद से, बोलने के तरीके को कंट्रोल करना
एक या एक से ज़्यादा स्पीकर के लिए, टीटीएस की स्टाइल, टोन, लहजे, और रफ़्तार को कंट्रोल किया जा सकता है. इसके लिए, नैचुरल लैंग्वेज प्रॉम्प्ट का इस्तेमाल करें. उदाहरण के लिए, एक स्पीकर वाले प्रॉम्प्ट में, यह कहा जा सकता है:
Say in an spooky whisper:
"By the pricking of my thumbs...
Something wicked this way comes"
एक से ज़्यादा स्पीकर वाले प्रॉम्प्ट में, मॉडल को हर स्पीकर का नाम और उससे जुड़ी ट्रांसक्रिप्ट दें. हर स्पीकर के लिए अलग-अलग निर्देश भी दिए जा सकते हैं:
Make Speaker1 sound tired and bored, and Speaker2 sound excited and happy:
Speaker1: So... what's on the agenda today?
Speaker2: You're never going to guess!
अपनी बात को ज़्यादा असरदार बनाने के लिए, आवाज़ का ऐसा विकल्प इस्तेमाल करें जो आपकी स्टाइल या भावना के मुताबिक हो. उदाहरण के लिए, पिछले प्रॉम्प्ट में एन्सेलडस की सांस लेने की आवाज़ से "थका हुआ" और "उबाऊ" पर ज़ोर दिया जा सकता है. वहीं, पक की तेज़ आवाज़ से "उत्साहित" और "खुश" पर ज़ोर दिया जा सकता है.
ऑडियो में बदलने के लिए प्रॉम्प्ट जनरेट करना
टीटीएस मॉडल सिर्फ़ ऑडियो आउटपुट देते हैं. हालांकि, पहले ट्रांसक्रिप्ट जनरेट करने के लिए अन्य मॉडल का इस्तेमाल किया जा सकता है. इसके बाद, उस ट्रांसक्रिप्ट को टीटीएस मॉडल को पढ़कर सुनाने के लिए भेजा जा सकता है.
Python
from google import genai
client = genai.Client()
transcript_interaction = client.interactions.create(
model="gemini-3.6-flash",
input="""Generate a short transcript around 100 words that reads
like it was clipped from a podcast by excited herpetologists.
The hosts names are Dr. Anya and Liam."""
)
transcript = transcript_interaction.output_text
tts_interaction = client.interactions.create(
model="gemini-3.1-flash-tts-preview",
input=transcript,
response_format={"type": "audio"},
generation_config={
"speech_config": [
{"speaker": "Dr. Anya", "voice": "Kore"},
{"speaker": "Liam", "voice": "Puck"}
]
}
)
JavaScript
import { GoogleGenAI } from "@google/genai";
const client = new GoogleGenAI({});
async function main() {
const transcriptInteraction = await client.interactions.create({
model: "gemini-3.6-flash",
input: "Generate a short transcript around 100 words that reads like it was clipped from a podcast by excited herpetologists. The hosts names are Dr. Anya and Liam.",
})
const ttsInteraction = await client.interactions.create({
model: "gemini-3.1-flash-tts-preview",
input: transcriptInteraction.output_text,
response_format: { type: 'audio' },
generation_config: {
speech_config: [
{ speaker: "Dr. Anya",