लिखाई को बोली में बदलने की सुविधा

Gemini API, टेक्स्ट इनपुट को एक या एक से ज़्यादा लोगों की आवाज़ वाले ऑडियो में बदल सकता है. इसके लिए, यह लिखाई को बोली में बदलने (टीटीएस) की सुविधा का इस्तेमाल करता है. लिखे गए शब्दों को सुनने की सुविधा (टीटीएस) को कंट्रोल किया जा सकता है. इसका मतलब है कि बातचीत को बेहतर बनाने के लिए, सामान्य भाषा का इस्तेमाल किया जा सकता है. साथ ही, ऑडियो की स्टाइल, उच्चारण, गति, और टोन को कंट्रोल किया जा सकता है.

टीटीएस की सुविधा, Live API के ज़रिए उपलब्ध कराई गई स्पीच जनरेशन की सुविधा से अलग है. इसे इंटरैक्टिव, अनस्ट्रक्चर्ड ऑडियो, और मल्टीमॉडल इनपुट और आउटपुट के लिए डिज़ाइन किया गया है. लाइव एपीआई, बातचीत के बदलते संदर्भों में बेहतर काम करता है. वहीं, Gemini API के ज़रिए टीटीएस की सुविधा, उन स्थितियों के लिए तैयार की गई है जिनमें सटीक टेक्स्ट सुनाने की ज़रूरत होती है. साथ ही, स्टाइल और आवाज़ पर बारीकी से कंट्रोल करने की ज़रूरत होती है. जैसे, पॉडकास्ट या ऑडियो बुक जनरेट करना.

इस गाइड में, टेक्स्ट से एक या एक से ज़्यादा स्पीकर की आवाज़ जनरेट करने का तरीका बताया गया है.

शुरू करने से पहले

पक्का करें कि आपने Gemini 2.5 मॉडल के ऐसे वैरिएंट का इस्तेमाल किया हो जिसमें टेक्स्ट को बोली में बदलने (टीटीएस) की सुविधा उपलब्ध हो. इसके बारे में इन मॉडल के साथ काम करता है सेक्शन में बताया गया है. सबसे अच्छे नतीजे पाने के लिए, यह देखें कि आपके इस्तेमाल के हिसाब से कौनसा मॉडल सबसे सही है.

ऐप्लिकेशन बनाना शुरू करने से पहले, AI Studio में Gemini 2.5 के टीटीएस मॉडल को आज़माना आपके लिए फ़ायदेमंद हो सकता है.

एक व्यक्ति की आवाज़ में लिखाई को बोली में बदलने की सुविधा

टेक्स्ट को एक स्पीकर वाले ऑडियो में बदलने के लिए, रिस्पॉन्स मोड को "ऑडियो" पर सेट करें. इसके बाद, VoiceConfig सेट किए गए SpeechConfig ऑब्जेक्ट को पास करें. आपको पहले से मौजूद आउटपुट की आवाज़ों में से किसी एक आवाज़ का नाम चुनना होगा.

इस उदाहरण में, मॉडल से मिले आउटपुट ऑडियो को वेव फ़ाइल में सेव किया गया है:

Python

from google import genai
from google.genai import types
import wave

# Set up the wave file to save the output:
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()

response = client.models.generate_content(
   model="gemini-2.5-flash-preview-tts",
   contents="Say cheerfully: Have a wonderful day!",
   config=types.GenerateContentConfig(
      response_modalities=["AUDIO"],
      speech_config=types.SpeechConfig(
         voice_config=types.VoiceConfig(
            prebuilt_voice_config=types.PrebuiltVoiceConfig(
               voice_name='Kore',
            )
         )
      ),
   )
)

data = response.candidates[0].content.parts[0].inline_data.data

file_name='out.wav'
wave_file(file_name, data) # Saves the file to current directory

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 ai = new GoogleGenAI({});

   const response = await ai.models.generateContent({
      model: "gemini-2.5-flash-preview-tts",
      contents: [{ parts: [{ text: 'Say cheerfully: Have a wonderful day!' }] }],
      config: {
            responseModalities: ['AUDIO'],
            speechConfig: {
               voiceConfig: {
                  prebuiltVoiceConfig: { voiceName: 'Kore' },
               },
            },
      },
   });

   const data = response.candidates?.[0]?.content?.parts?.[0]?.inlineData?.data;
   const audioBuffer = Buffer.from(data, 'base64');

   const fileName = 'out.wav';
   await saveWaveFile(fileName, audioBuffer);
}
await main();

REST

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-preview-tts:generateContent" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -X POST \
  -H "Content-Type: application/json" \
  -d '{
        "contents": [{
          "parts":[{
            "text": "Say cheerfully: Have a wonderful day!"
          }]
        }],
        "generationConfig": {
          "responseModalities": ["AUDIO"],
          "speechConfig": {
            "voiceConfig": {
              "prebuiltVoiceConfig": {
                "voiceName": "Kore"
              }
            }
          }
        },
        "model": "gemini-2.5-flash-preview-tts",
    }' | jq -r '.candidates[0].content.parts[0].inlineData.data' | \
          base64 --decode >out.pcm
# You may need to install ffmpeg.
ffmpeg -f s16le -ar 24000 -ac 1 -i out.pcm out.wav

एक से ज़्यादा आवाज़ों में लिखाई को बोली में बदलने की सुविधा

एक से ज़्यादा स्पीकर के लिए ऑडियो चलाने के लिए, आपको MultiSpeakerVoiceConfig ऑब्जेक्ट की ज़रूरत होगी. इसमें हर स्पीकर (दो तक) को SpeakerVoiceConfig के तौर पर कॉन्फ़िगर किया गया हो. आपको हर speaker को उन नामों से तय करना होगा जिनका इस्तेमाल प्रॉम्प्ट में किया गया है:

Python

from google import genai
from google.genai import types
import wave

# Set up the wave file to save the output:
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?"""

response = client.models.generate_content(
   model="gemini-2.5-flash-preview-tts",
   contents=prompt,
   config=types.GenerateContentConfig(
      response_modalities=["AUDIO"],
      speech_config=types.SpeechConfig(
         multi_speaker_voice_config=types.MultiSpeakerVoiceConfig(
            speaker_voice_configs=[
               types.SpeakerVoiceConfig(
                  speaker='Joe',
                  voice_config=types.VoiceConfig(
                     prebuilt_voice_config=types.PrebuiltVoiceConfig(
                        voice_name='Kore',
                     )
                  )
               ),
               types.SpeakerVoiceConfig(
                  speaker='Jane',
                  voice_config=types.VoiceConfig(
                     prebuilt_voice_config=types.PrebuiltVoiceConfig(
                        voice_name='Puck',
                     )
                  )
               ),
            ]
         )
      )
   )
)

data = response.candidates[0].content.parts[0].inline_data.data

file_name='out.wav'
wave_file(file_name, data) # Saves the file to current directory

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 ai = 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 response = await ai.models.generateContent({
      model: "gemini-2.5-flash-preview-tts",
      contents: [{ parts: [{ text: prompt }] }],
      config: {
            responseModalities: ['AUDIO'],
            speechConfig: {
               multiSpeakerVoiceConfig: {
                  speakerVoiceConfigs: [
                        {
                           speaker: 'Joe',
                           voiceConfig: {
                              prebuiltVoiceConfig: { voiceName: 'Kore' }
                           }
                        },
                        {
                           speaker: 'Jane',
                           voiceConfig: {
                              prebuiltVoiceConfig: { voiceName: 'Puck' }
                           }
                        }
                  ]
               }
            }
      }
   });

   const data = response.candidates?.[0]?.content?.parts?.[0]?.inlineData?.data;
   const audioBuffer = Buffer.from(data, 'base64');

   const fileName = 'out.wav';
   await saveWaveFile(fileName, audioBuffer);
}

await main();

REST

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-preview-tts:generateContent" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -X POST \
  -H "Content-Type: application/json" \
  -d '{
  "contents": [{
    "parts":[{
      "text": "TTS the following conversation between Joe and Jane:
                Joe: Hows it going today Jane?
                Jane: Not too bad, how about you?"
    }]
  }],
  "generationConfig": {
    "responseModalities": ["AUDIO"],
    "speechConfig": {
      "multiSpeakerVoiceConfig": {
        "speakerVoiceConfigs": [{
            "speaker": "Joe",
            "voiceConfig": {
              "prebuiltVoiceConfig": {
                "voiceName": "Kore"
              }
            }
          }, {
            "speaker": "Jane",
            "voiceConfig": {
              "prebuiltVoiceConfig": {
                "voiceName": "Puck"
              }
            }
          }]
      }
    }
  },
  "model": "gemini-2.5-flash-preview-tts",
}' | jq -r '.candidates[0].content.parts[0].inlineData.data' | \
    base64 --decode > out.pcm
# You may need to install ffmpeg.
ffmpeg -f s16le -ar 24000 -ac 1 -i out.pcm out.wav

प्रॉम्प्ट की मदद से, बोलने के तरीके को कंट्रोल करना

आम बोलचाल की भाषा में दिए गए प्रॉम्प्ट का इस्तेमाल करके, स्टाइल, टोन, लहजे, और बोलने की रफ़्तार को कंट्रोल किया जा सकता है. ऐसा एक और एक से ज़्यादा स्पीकर वाले टीटीएस, दोनों के लिए किया जा सकता है. उदाहरण के लिए, एक स्पीकर वाले प्रॉम्प्ट में कहा जा सकता है:

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
from google.genai import types

client = genai.Client()

transcript = client.models.generate_content(
   model="gemini-2.0-flash",
   contents="""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.""").text

response = client.models.generate_content(
   model="gemini-2.5-flash-preview-tts",
   contents=transcript,
   config=types.GenerateContentConfig(
      response_modalities=["AUDIO"],
      speech_config=types.SpeechConfig(
         multi_speaker_voice_config=types.MultiSpeakerVoiceConfig(
            speaker_voice_configs=[
               types.SpeakerVoiceConfig(
                  speaker='Dr. Anya',
                  voice_config=types.VoiceConfig(
                     prebuilt_voice_config=types.PrebuiltVoiceConfig(
                        voice_name='Kore',
                     )
                  )
               ),
               types.SpeakerVoiceConfig(
                  speaker='Liam',
                  voice_config=types.VoiceConfig(
                     prebuilt_voice_config=types.PrebuiltVoiceConfig(
                        voice_name='Puck',
                     )
                  )
               ),
            ]
         )
      )
   )
)

# ...Code to stream or save the output

JavaScript

import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({});

async function main() {

const transcript = await ai.models.generateContent({
   model: "gemini-2.0-flash",
   contents: "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 response = await ai.models.generateContent({
   model: "gemini-2.5-flash-preview-tts",
   contents: transcript,
   config: {
      responseModalities: ['AUDIO'],
      speechConfig: {
         multiSpeakerVoiceConfig: {
            speakerVoiceConfigs: [
                   {
                     speaker: "Dr. Anya",
                     voiceConfig: {
                        prebuiltVoiceConfig: {voiceName: "Kore"},
                     }
                  },
                  {
                     speaker: "Liam",
                     voiceConfig: {
                        prebuiltVoiceConfig: {voiceName: "Puck"},
                    }
                  }
                ]
              }
            }
      }
  });
}
// ..JavaScript code for exporting .wav file for output audio

await main();

आवाज़ के विकल्प

TTS मॉडल, voice_name फ़ील्ड में आवाज़ के इन 30 विकल्पों के साथ काम करते हैं:

Zephyr -- Bright Puck -- Upbeat Charon -- जानकारी देने वाला
Kore -- Firm Fenrir -- Excitable Leda -- यूथफ़ुल
Orus -- कंपनी Aoede -- Breezy Callirrhoe -- ईज़ी-गोइंग
ऑटोनो -- तेज रोशनी Enceladus -- Breathy Iapetus -- Clear
Umbriel -- Easy-going Algieba -- Smooth Despina -- Smooth
Erinome -- Clear Algenib -- ग्रैवली Rasalgethi -- जानकारी देने वाला
Laomedeia -- अपबीट Achernar -- सॉफ़्ट Alnilam -- Firm
Schedar -- Even Gacrux -- मैच्योर Pulcherrima -- Forward
Achird -- Friendly Zubenelgenubi -- कैज़ुअल Vindemiatrix -- जेंटल
Sadachbia -- Lively Sadaltager -- Knowledgeable Sulafat -- Warm

AI Studio में जाकर, बोलकर इस्तेमाल करने लायक सभी विकल्प सुने जा सकते हैं.

इस्तेमाल की जा सकने वाली भाषाएं

टीटीएस मॉडल, इनपुट की भाषा का पता अपने-आप लगा लेते हैं. ये 24 भाषाओं में काम करते हैं:

भाषा BCP-47 कोड भाषा BCP-47 कोड
ऐरेबिक (मिस्र) ar-EG जर्मन (जर्मनी) de-DE
अंग्रेज़ी (यूएस) en-US स्पैनिश (अमेरिका) es-US
फ़्रेंच (फ़्रांस) fr-FR हिन्दी (भारत) hi-IN
इंडोनेशियन (इंडोनेशिया) id-ID इटैलियन (इटली) it-IT
जैपनीज़ (जापान) ja-JP कोरियन (कोरिया) ko-KR
पॉर्चुगीज़ (ब्राज़ील) pt-BR रशियन (रूस) ru-RU
डच (नीदरलैंड्स) nl-NL पोलिश (पोलैंड) pl-PL
थाई (थाईलैंड) th-TH टर्किश (तुर्किये) tr-TR
वियतनामीज़ (वियतनाम) vi-VN रोमानियाई (रोमानिया) ro-RO
यूक्रेनियाई (यूक्रेन) uk-UA बांग्ला (बांग्लादेश) bn-BD
अंग्रेज़ी (भारत) en-IN और hi-IN का बंडल मराठी (भारत) mr-IN
तमिल (भारत) ta-IN तेलुगू (भारत) te-IN

इन मॉडल के साथ काम करता है

मॉडल एक व्यक्ति बोल रहा है मल्टीस्पीकर
Gemini 2.5 Flash Preview TTS ✔️ ✔️
Gemini 2.5 Pro Preview TTS ✔️ ✔️

सीमाएं

  • टीटीएस मॉडल, सिर्फ़ टेक्स्ट इनपुट ले सकते हैं और ऑडियो आउटपुट जनरेट कर सकते हैं.
  • TTS सेशन के लिए, कॉन्टेक्स्ट विंडो की सीमा 32 हज़ार टोकन होती है.
  • भाषा से जुड़ी सहायता के लिए, भाषाएं सेक्शन देखें.

आगे क्या करना है