Gemini API का इस्तेमाल करके, लिखाई को बोली में बदलने (टीटीएस) की सुविधा


टेक्स्ट से नैचुरल और एक्सप्रेसिव स्पीच (ऑडियो) जनरेट करने के लिए, Gemini टीटीएस मॉडल (gemini-3.8-flash-tts या gemini-3.8-flash-lite-tts) का इस्तेमाल किया जा सकता है. Firebase AI Logic का इस्तेमाल करके, सीधे अपने मोबाइल या वेब ऐप्लिकेशन से यह अनुरोध किया जा सकता है. जैसे, Apple (Swift), Android (Kotlin / Java), वेब (JavaScript), Flutter (Dart) या Unity ऐप्लिकेशन.

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

इस सुविधा की मदद से, ये सुविधाएं बनाई जा सकती हैं:

  • इंटरैक्टिव स्टोरीटेलिंग और ऑडियो बुक: लंबी कहानियां सुनाएं या रोल-प्लेइंग गेम खेलें. इनमें किरदार अपनी आवाज़ें बदल सकते हैं, एक-दूसरे की बात पर ओवरलैप होने वाले बैकचैनल से प्रतिक्रिया दे सकते हैं, और अपनी भावनाओं को ज़ाहिर कर सकते हैं. जैसे, सस्पेंस में फुसफुसाना या किसी चुटकुले पर हंसना.
  • कम इंतज़ार के समय वाले वॉइस एजेंट और असिस्टेंट: एलएलएम से टेक्स्ट मिलते ही, सिंथेटिक स्पीच को स्ट्रीम करें. इससे बातचीत के दौरान, आवाज़ की क्वालिटी एक जैसी बनी रहती है.
  • कई भाषाओं में कॉन्टेंट पढ़ने और भाषा सिखाने वाले ट्यूटर: 130 से ज़्यादा भाषाओं और क्षेत्रीय लहजों में लेख, रेसिपी या स्टडी मटीरियल को ज़ोर से पढ़वाएं. इसके अलावा, एक ही बातचीत के दौरान आसानी से भाषाएं बदलें.

इस गाइड में, एक या एक से ज़्यादा स्पीकर वाली आवाज़ जनरेट करने, ऑडियो रिस्पॉन्स स्ट्रीम करने, SpeechMetadata और इनलाइन टैग की मदद से सीधे आवाज़ देने, और पहले से मौजूद, लाइब्रेरी या पसंद के मुताबिक आवाज़ों में से चुनने का तरीका बताया गया है.

सिंगल-स्पीकर कोड पर जाएं मल्टी-स्पीकर कोड पर जाएं स्ट्रीमिंग कोड पर जाएं

टीटीएस और Live API के बीच तुलना

लिखे गए शब्दों को सुनने की सुविधा (टीटीएस) और Live API, दोनों ही मॉडल कम इंतज़ार के समय वाले और बोली जनरेट करने वाले मॉडल हैं. इन्हें अलग-अलग आवाज़ों और भाषाओं में जवाब देने के लिए कॉन्फ़िगर किया जा सकता है. हालांकि, दोनों का इस्तेमाल अलग-अलग कामों के लिए किया जाता है.

  • लिखे गए शब्दों को सुनने की सुविधा (टीटीएस), एकतरफ़ा इंटरैक्शन है. इसमें अनुरोध के जवाब में ऑडियो मिलता है. जैसे, टेक्स्ट के बदले ऑडियो. इसे उन स्थितियों के लिए तैयार किया गया है जिनमें दिए गए टेक्स्ट को सटीक तरीके से पढ़ने की ज़रूरत होती है. साथ ही, स्टाइल और आवाज़ पर बारीकी से कंट्रोल किया जा सकता है. जैसे, पॉडकास्ट नरेशन, ऑडियो बुक या लेखों को तेज़ आवाज़ में पढ़ना.

  • Live API जनरेशन में, रीयल-टाइम में बोलकर बातचीत करने (बोलकर सवाल पूछना और बोलकर जवाब पाना) के लिए, दोनों तरफ़ से स्ट्रीमिंग की सुविधा उपलब्ध है. यह डाइनैमिक बातचीत के कॉन्टेक्स्ट में बेहतरीन तरीके से काम करता है. इसमें मॉडल यह तय करता है कि किस तरह की स्पीच का इस्तेमाल करना है. ध्यान दें कि Live API के नए मॉडल, वीडियो और इमेज इनपुट के साथ भी काम करते हैं.

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

इस पेज पर, सेवा देने वाली कंपनी के हिसाब से कॉन्टेंट और कोड देखने के लिए, Gemini API की सेवा देने वाली कंपनी पर क्लिक करें.

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

अपने प्रॉम्प्ट को टेस्ट करने और उनमें बदलाव करने के लिए, हमारा सुझाव है कि आप Google AI Studio का इस्तेमाल करें.

इस सुविधा के साथ काम करने वाले मॉडल

  • gemini-3.8-flash-tts
  • gemini-3.8-flash-lite-tts
टीटीएस मॉडल की तुलना

दोनों Gemini 3.x TTS मॉडल का अनुरोध स्कीमा और प्रॉम्प्ट फ़ॉर्मैट एक जैसा है. हालांकि, यहां कुछ अंतर दिए गए हैं, जिनसे आपको यह समझने में मदद मिलेगी कि हर मॉडल का इस्तेमाल कब किया जा सकता है.

सुविधा या वर्कलोड gemini-3.8-flash-tts gemini-3.8-flash-lite-tts
प्राइमरी स्ट्रेंथ स्टूडियो-ग्रेड वॉइस फ़िडेलिटी, ऐक्टिंग की बारीकियां, और अलग-अलग बोलियों की कवरेज हाई थ्रूपुट, कम इंतज़ार का समय, और कम लागत
इस्तेमाल के सबसे सही उदाहरण ऑडियो बुक, स्टूडियो नरेशन, कई स्पीकर वाले मुश्किल डायलॉग, बार-बार आने वाले वोकल-बर्स्ट टैग, मुश्किल उच्चारण, क्षेत्रीय बोलियां ज़्यादा वॉल्यूम में ऑडियो जनरेट करना, रीयल-टाइम में वॉइस एजेंट कैस्केड, तेज़ आवाज़ में सुनाने वाले यूज़र इंटरफ़ेस (यूआई) की सुविधाएं, रोज़ाना एक स्पीकर की आवाज़
इस्तेमाल की जा सकने वाली भाषाएं 132 भाषाएं 103 भाषाएं

टेक्स्ट की मदद से बोली जनरेट करना

टेक्स्ट इनपुट से, एक स्पीकर की आवाज़ या दो स्पीकर के डायलॉग में स्पीच जनरेट की जा सकती है. इसे पूरी WAV फ़ाइल (generateContent) या कम इंतज़ार के समय वाली रॉ पीसीएम ऑडियो स्ट्रीम (generateContentStream) के तौर पर जनरेट किया जा सकता है.

एक स्पीकर के साथ स्पीच जनरेट करना

इस सैंपल को आज़माने से पहले, इस गाइड का शुरू करने से पहले सेक्शन पूरा करें. इससे आपको अपना प्रोजेक्ट और ऐप्लिकेशन सेट अप करने में मदद मिलेगी.
इस सेक्शन में, आपको Gemini APIसेवा देने वाली कंपनी के लिए एक बटन पर भी क्लिक करना होगा, ताकि आपको इस पेज पर सेवा देने वाली कंपनी के हिसाब से कॉन्टेंट दिखे.

एक स्पीकर वाला ऑडियो जनरेट करने का तरीका यहां बताया गया है:

  1. अपने GenerationConfig में, responseModalities को AUDIO पर सेट करें और SpeechConfig को इन सेटिंग के साथ कॉन्फ़िगर करें:

  2. इनका इस्तेमाल करके TextPart बनाएं:

    • (ज़रूरी है) शब्द-दर-शब्द ट्रांसक्रिप्ट, जिसे बोला जाना है.

    • (ज़रूरी नहीं) style ब्यौरे के साथ SpeechMetadata (उदाहरण के लिए, "cheerful and friendly").

  3. generateContent को कॉल करें. डिफ़ॉल्ट रूप से, यूनरी अनुरोधों के जवाब में, इनलाइन डेटा के हिस्सों में पूरी WAV (audio/wav) ऑडियो फ़ाइल (16-बिट लीनियर पीसीएम, 24 किलोहर्ट्ज़, मोनो, स्टैंडर्ड 44-बाइट आरआईएफ़एफ़ हेडर के साथ) मिलती है.

Swift


import FirebaseAILogic

// Initialize the Gemini Developer API backend service.
let ai = FirebaseAI.firebaseAI(backend: .googleAI())

// Set `responseModalities` to include `.audio`.
// Configure a `SpeechConfig` with your chosen voice name (and optionally a language code).
let config = GenerationConfig(
  responseModalities: [.audio],
  speechConfig: SpeechConfig(voiceName: "Kore", languageCode: "en-US")
)

// Create a `GenerativeModel` instance with a model that supports speech generation.
let model = ai.generativeModel(
  modelName: "gemini-3.8-flash-tts",
  generationConfig: config
)

// Provide the verbatim text to synthesize as speech.
// Optionally provide turn-level style metadata.
let prompt = TextPart(
  "Have a wonderful day!",
  speechMetadata: SpeechMetadata(style: "cheerful and friendly")
)

// Call `generateContent` to generate the speech output.
let response = try await model.generateContent(prompt)

// Extract the WAV audio data and handle it for downstream use. For example:
for part in response.inlineDataParts {
  let wavData = part.data       // WAV audio bytes (24kHz, 1 channel, 16-bit PCM)
  let mimeType = part.mimeType  // for example: "audio/wav"
  print("Received audio data with MIME type: \(mimeType)")

  // Play the WAV audio data using your own playback helper (for example, with `AVAudioPlayer(data: wavData)`).
  playAudio(data: wavData)
}

Kotlin

Kotlin के लिए, इस एसडीके में मौजूद तरीके, सस्पेंड फ़ंक्शन हैं. इन्हें Coroutine स्कोप से कॉल करना ज़रूरी है.

// Set `responseModalities` to include `AUDIO`.
// Configure a `SpeechConfig` with your chosen voice name (and optionally a language code).
val config = generationConfig {
    responseModalities = listOf(ResponseModality.AUDIO)
    speechConfig = SpeechConfig(
        voice = Voice("Kore"),
        languageCode = "en-US"
    )
}

// Initialize the Gemini Developer API backend service.
// Create a `GenerativeModel` instance with a model that supports speech generation.
val model = Firebase.ai(backend = GenerativeBackend.googleAI())
    .generativeModel(
        modelName = "gemini-3.8-flash-tts",
        generationConfig = config
    )

// Provide the verbatim text to synthesize as speech.
// Optionally provide turn-level style metadata.
val prompt = content {
    text(
        text = "Have a wonderful day!",
        speechMetadata = SpeechMetadata(style = "cheerful and friendly")
    )
}

// Call `generateContent` to generate the speech output.
val response = model.generateContent(prompt)

// Extract the WAV audio data and handle it for downstream use. For example:
val part = response.candidates.firstOrNull()?.content?.parts?.firstOrNull()
if (part is InlineDataPart) {
    val wavData = part.inlineData  // WAV audio bytes (24kHz, 1 channel, 16-bit PCM)

    // Pass the audio data to your audio player or playback helper (which you'll need to implement).
    playAudio(wavData)
}

Java

Java के लिए, इस एसडीके में मौजूद स्ट्रीमिंग के तरीके, Reactive Streams लाइब्रेरी से Publisher टाइप दिखाते हैं.

// Set `responseModalities` to include `AUDIO`.
// Configure a `SpeechConfig` with your chosen voice name (and optionally a language code).
GenerationConfig config = new GenerationConfig.Builder()
    .setResponseModalities(Collections.singletonList(ResponseModality.AUDIO))
    .setSpeechConfig(new SpeechConfig(new Voice("Kore"), "en-US"))
    .build();

// Initialize the Gemini Developer API backend service.
// Create a `GenerativeModel` instance with a model that supports speech generation.
GenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.googleAI())
        .generativeModel("gemini-3.8-flash-tts", config);

// Use the GenerativeModelFutures Java compatibility layer.
GenerativeModelFutures model = GenerativeModelFutures.from(ai);

// Provide the verbatim text to synthesize as speech.
// Optionally provide turn-level style metadata.
Content content = new Content.Builder()
    .addText("Have a wonderful day!", new SpeechMetadata(null, "cheerful and friendly"))
    .build();
Executor executor = Executors.newSingleThreadExecutor();

// Call `generateContent` to generate the speech output.
// Extract the WAV audio data and handle it for downstream use.
ListenableFuture<GenerateContentResponse> response = model.generateContent(content);
Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
    @Override
    public void onSuccess(GenerateContentResponse result) {
        Part part = result.getCandidates().get(0).getContent().getParts().get(0);
        if (part instanceof InlineDataPart) {
            byte[] wavData = ((InlineDataPart) part).getInlineData();  // WAV audio bytes (24kHz, 1 channel, 16-bit PCM)

            // Pass the audio data to your audio player or playback helper (which you'll need to implement).
            playAudio(wavData);
        }
    }

    @Override
    public void onFailure(Throwable t) {
        t.printStackTrace();
    }
}, executor);

Web


import { initializeApp } from "firebase/app";
import { getAI, getGenerativeModel, GoogleAIBackend, ResponseModality } from "firebase/ai";

// TODO(developer): Replace with your app's Firebase configuration
const firebaseConfig = { /* ... */ };
const firebaseApp = initializeApp(firebaseConfig);

// Initialize the Gemini Developer API backend service.
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });

// Set `responseModalities` to include `AUDIO`.
// Configure a `SpeechConfig` with your chosen voice name (and optionally a language code).
const generationConfig = {
  responseModalities: [ResponseModality.AUDIO],
  speechConfig: {
    voiceConfig: { prebuiltVoiceConfig: { voiceName: "Kore" } },
    languageCode: "en-US"
  }
};

// Create a `GenerativeModel` instance with a model that supports speech generation.
const model = getGenerativeModel(ai, {
  model: "gemini-3.8-flash-tts",
  generationConfig
});

// Provide the verbatim text to synthesize as speech.
// Optionally provide turn-level style metadata.
const prompt = [
  {
    type: "text",
    text: "Have a wonderful day!",
    speechMetadata: { style: "cheerful and friendly" }
  }
];

// Call `generateContent` to generate the speech output.
const result = await model.generateContent(prompt);
const inlineDataParts = result.response.inlineDataParts();

// Extract the WAV audio data and handle it for downstream use. For example:
if (inlineDataParts?.[0]) {
  const wavBase64 = inlineDataParts[0].inlineData.data;  // WAV audio bytes (24kHz, 1 channel, 16-bit PCM)
  // Decode base64 to ArrayBuffer
  const wavBuffer = Uint8Array.from(atob(wavBase64), c => c.charCodeAt(0)).buffer;

  // The response is a complete WAV file, so the Web Audio API can decode and play it directly.
  const audioCtx = new AudioContext();
  const audioBuffer = await audioCtx.decodeAudioData(wavBuffer);
  const source = audioCtx.createBufferSource();
  source.buffer = audioBuffer;
  source.connect(audioCtx.destination);
  source.start();
}

Dart


import 'package:firebase_ai/firebase_ai.dart';

// Set `responseModalities` to include `audio`.
// Configure a `SpeechConfig` with your chosen voice name (and optionally a language code).
final config = GenerationConfig(
  responseModalities: [ResponseModalities.audio],
  speechConfig: SpeechConfig(voiceName: 'Kore', languageCode: 'en-US'),
);

// Initialize the Gemini Developer API backend service.
// Create a `GenerativeModel` instance with a model that supports speech generation.
final model = FirebaseAI.googleAI().generativeModel(
  model: 'gemini-3.8-flash-tts',
  generationConfig: config,
);

// Provide the verbatim text to synthesize as speech.
// Optionally provide turn-level style metadata.
final prompt = Content.multi([
  TextPart(
    'Have a wonderful day!',
    speechMetadata: SpeechMetadata(style: 'cheerful and friendly'),
  ),
]);

// Call `generateContent` to generate the speech output.
final response = await model.generateContent([prompt]);

// Extract the WAV audio data and handle it for downstream use. For example:
final part = response.candidates.first.content.parts.first;
if (part is InlineDataPart && part.mimeType.startsWith('audio/')) {
  final Uint8List wavData = part.bytes;  // WAV audio bytes (24kHz, 1 channel, 16-bit PCM)

  // Play the WAV audio data using your preferred audio player or helper (which you'll need to implement).
  await playAudio(wavData);
}

Unity


using Firebase.AI;

// Initialize the Gemini Developer API backend service.
var ai = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI());

// Set `responseModalities` to include `Audio`.
// Configure a `SpeechConfig` with your chosen voice name and language code.
var config = new GenerationConfig(
  responseModalities: new System.Collections.Generic.List<ResponseModality> { ResponseModality.Audio },
  speechConfig: SpeechConfig.UsePrebuiltVoice("Kore", "en-US")
);

// Create a `GenerativeModel` instance with a model that supports speech generation.
var model = ai.GetGenerativeModel(
  modelName: "gemini-3.8-flash-tts",
  generationConfig: config
);

// Provide the verbatim text to synthesize as speech.
// Optionally provide turn-level style metadata.
var prompt = new ModelContent(
  new ModelContent.TextPart(
    "Have a wonderful day!",
    new SpeechMetadata(speaker: null, style: "cheerful and friendly")
  )
);

// Call `GenerateContentAsync` to generate the speech output.
var response = await model.GenerateContentAsync(prompt);

// Extract the WAV audio data and handle it for downstream use. For example:
if (response.Candidates.Count > 0) {
  foreach (var part in response.Candidates[0].Content.Parts) {
    if (part is ModelContent.InlineDataPart inlineData) {
      byte[] wavData = inlineData.Data;  // WAV audio bytes (24kHz, 1 channel, 16-bit PCM)

      // Pass the audio data to your audio player or playback helper (which you'll need to implement).
      PlayAudio(wavData);
    }
  }
}

एक से ज़्यादा स्पीकर की आवाज़ में स्पीच जनरेट करना

इस सैंपल को आज़माने से पहले, इस गाइड का शुरू करने से पहले सेक्शन पूरा करें. इससे आपको अपना प्रोजेक्ट और ऐप्लिकेशन सेट अप करने में मदद मिलेगी.
इस सेक्शन में, आपको Gemini APIसेवा देने वाली कंपनी के लिए एक बटन पर भी क्लिक करना होगा, ताकि आपको इस पेज पर सेवा देने वाली कंपनी के हिसाब से कॉन्टेंट दिखे.

एक ही अनुरोध में, दो लोगों के बीच होने वाली बातचीत को नैचुरल तरीके से जनरेट किया जा सकता है. इसके लिए, हर स्पीकर को एक अलग आवाज़ असाइन करें और डायलॉग की हर लाइन को उसके TextPart के तौर पर पास करें:

  1. एक MultiSpeakerVoiceConfig बनाएं, जो दो स्पीकर के नामों (उदाहरण के लिए, "Joe" और "Jane") को उनकी SpeakerVoiceConfig आवाज़ों से मैप करे.

  2. अपने GenerationConfig में, responseModalities को AUDIO पर सेट करें और अपने MultiSpeakerVoiceConfig (और वैकल्पिक languageCode) को SpeechConfig में पास करें.

  3. TextPart ऑब्जेक्ट का कलेक्शन या सूची दें. खास तौर पर, हर डायलॉग टर्न के लिए एक TextPart दें. हर TextPart को, SpeechMetadata को इस तरह सेट करें:

    • (ज़रूरी है) ऐसा speaker जो आपके MultiSpeakerVoiceConfig में बताए गए स्पीकर के नामों में से किसी एक से मेल खाता हो.
    • (ज़रूरी नहीं) style का ब्यौरा (उदाहरण के लिए, "cheerful and friendly").
  4. generateContent को कॉल करें. मॉडल, पूरी बातचीत को सिंथेसाइज़ करता है और WAV (audio/wav) ऑडियो फ़ाइल जनरेट करता है.

Swift


import FirebaseAILogic

// Initialize the Gemini Developer API backend service.
let ai = FirebaseAI.firebaseAI(backend: .googleAI())

// Configure a `SpeechConfig` for multiple speakers, assigning a voice to each speaker.
let multiSpeechConfig = SpeechConfig(
  multiSpeakerVoiceConfig: MultiSpeakerVoiceConfig(
    speakerVoiceConfigs: [
      SpeakerVoiceConfig(speaker: "Joe", voiceName: "Puck"),
      SpeakerVoiceConfig(speaker: "Jane", voiceName: "Kore")
    ]
  ),
  languageCode: "en-US"
)

// Set `responseModalities` to include `.audio`.
let config = GenerationConfig(
  responseModalities: [.audio],
  speechConfig: multiSpeechConfig
)

// Create a `GenerativeModel` instance with a model that supports speech generation.
let model = ai.generativeModel(
  modelName: "gemini-3.8-flash-tts",
  generationConfig: config
)

// Provide one `TextPart` per dialogue turn, specifying the speaker and optional style in `SpeechMetadata`.
let prompt: [any PartsRepresentable] = [
  TextPart(
    "How's it going today, Jane?",
    speechMetadata: SpeechMetadata(speaker: "Joe", style: "cheerful and friendly")
  ),
  TextPart(
    "Not too bad, how about you?",
    speechMetadata: SpeechMetadata(speaker: "Jane", style: "calm and relaxed")
  )
]

// Call `generateContent` to generate the multi-speaker speech output.
let response = try await model.generateContent(prompt)

// Extract the WAV audio data and handle it for downstream use. For example:
for part in response.inlineDataParts {
  let wavData = part.data       // WAV audio bytes (24kHz, 1 channel, 16-bit PCM)
  let mimeType = part.mimeType  // for example: "audio/wav"
  print("Received audio data with MIME type: \(mimeType)")

  // Play the WAV audio data using your own playback helper (for example, with `AVAudioPlayer(data: wavData)`).
  playAudio(data: wavData)
}

Kotlin

Kotlin के लिए, इस एसडीके में मौजूद तरीके, सस्पेंड फ़ंक्शन हैं. इन्हें Coroutine स्कोप से कॉल करना ज़रूरी है.

// Configure a `SpeechConfig` for multiple speakers, assigning a voice to each speaker.
val multiSpeechConfig = SpeechConfig(
    multiSpeakerVoiceConfig = MultiSpeakerVoiceConfig(
        speakerVoiceConfigs = listOf(
            SpeakerVoiceConfig(speaker = "Joe", voice = Voice("Puck")),
            SpeakerVoiceConfig(speaker = "Jane", voice = Voice("Kore"))
        )
    ),
    languageCode = "en-US"
)

// Set `responseModalities` to include `AUDIO`.
val config = generationConfig {
    responseModalities = listOf(ResponseModality.AUDIO)
    speechConfig = multiSpeechConfig
}

// Initialize the Gemini Developer API backend service.
// Create a `GenerativeModel` instance with a model that supports speech generation.
val model = Firebase.ai(backend = GenerativeBackend.googleAI())
    .generativeModel(
        modelName = "gemini-3.8-flash-tts",
        generationConfig = config
    )

// Provide one text part per dialogue turn, specifying the speaker and optional style in `SpeechMetadata`.
val prompt = content {
    text(
        text = "How's it going today, Jane?",
        speechMetadata = SpeechMetadata(speaker = "Joe", style = "cheerful and friendly")
    )
    text(
        text = "Not too bad, how about you?",
        speechMetadata = SpeechMetadata(speaker = "Jane", style = "calm and relaxed")
    )
}

// Call `generateContent` to generate the multi-speaker speech output.
val response = model.generateContent(prompt)

// Extract the WAV audio data and handle it for downstream use. For example:
val part = response.candidates.firstOrNull()?.content?.parts?.firstOrNull()
if (part is InlineDataPart) {
    val wavData = part.inlineData  // WAV audio bytes (24kHz, 1 channel, 16-bit PCM)

    // Pass the audio data to your audio player or playback helper (which you'll need to implement).
    playAudio(wavData)
}

Java

Java के लिए, इस एसडीके में मौजूद स्ट्रीमिंग के तरीके, Reactive Streams लाइब्रेरी से Publisher टाइप दिखाते हैं.

// Configure a `SpeechConfig` for multiple speakers, assigning a voice to each speaker.
MultiSpeakerVoiceConfig multiSpeakerVoiceConfig = new MultiSpeakerVoiceConfig(
    Arrays.asList(
        new SpeakerVoiceConfig("Joe", new Voice("Puck")),
        new SpeakerVoiceConfig("Jane", new Voice("Kore"))
    )
);

SpeechConfig multiSpeechConfig = new SpeechConfig(multiSpeakerVoiceConfig, "en-US");

// Set `responseModalities` to include `AUDIO`.
GenerationConfig config = new GenerationConfig.Builder()
    .setResponseModalities(Collections.singletonList(ResponseModality.AUDIO))
    .setSpeechConfig(multiSpeechConfig)
    .build();

// Initialize the Gemini Developer API backend service.
// Create a `GenerativeModel` instance with a model that supports speech generation.
GenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.googleAI())
        .generativeModel("gemini-3.8-flash-tts", config);

// Use the `GenerativeModelFutures` Java compatibility layer.
GenerativeModelFutures model = GenerativeModelFutures.from(ai);

// Provide one text part per dialogue turn, specifying the speaker and optional style in `SpeechMetadata`.
Content content = new Content.Builder()
    .addText(
        "How's it going today, Jane?",
        new SpeechMetadata("Joe", "cheerful and friendly"))
    .addText(
        "Not too bad, how about you?",
        new SpeechMetadata("Jane", "calm and relaxed"))
    .build();
Executor executor = Executors.newSingleThreadExecutor();

// Call `generateContent` to generate the multi-speaker speech output.
// Extract the WAV audio data and handle it for downstream use.
ListenableFuture<GenerateContentResponse> response = model.generateContent(content);
Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
    @Override
    public void onSuccess(GenerateContentResponse result) {
        Part part = result.getCandidates().get(0).getContent().getParts().get(0);
        if (part instanceof InlineDataPart) {
            byte[] wavData = ((InlineDataPart) part).getInlineData();  // WAV audio bytes (24kHz, 1 channel, 16-bit PCM)

            // Pass the audio data to your audio player or playback helper (which you'll need to implement).
            playAudio(wavData);
        }
    }

    @Override
    public void onFailure(Throwable t) {
        t.printStackTrace();
    }
}, executor);

Web


import { initializeApp } from "firebase/app";
import { getAI, getGenerativeModel, GoogleAIBackend, ResponseModality } from "firebase/ai";

// TODO(developer): Replace with your app's Firebase configuration
const firebaseConfig = { /* ... */ };
const firebaseApp = initializeApp(firebaseConfig);

// Initialize the Gemini Developer API backend service.
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });

// Set `responseModalities` to include `AUDIO`.
// Configure a `SpeechConfig` for multiple speakers, assigning a voice to each speaker.
const generationConfig = {
  responseModalities: [ResponseModality.AUDIO],
  speechConfig: {
    multiSpeakerVoiceConfig: {
      speakerVoiceConfigs: [
        { speaker: "Joe", voiceConfig: { prebuiltVoiceConfig: { voiceName: "Puck" } } },
        { speaker: "Jane", voiceConfig: { prebuiltVoiceConfig: { voiceName: "Kore" } } }
      ]
    },
    languageCode: "en-US"
  }
};

// Create a `GenerativeModel` instance with a model that supports speech generation.
const model = getGenerativeModel(ai, {
  model: "gemini-3.8-flash-tts",
  generationConfig
});

// Provide one `TextPart` per dialogue turn, specifying the speaker and optional style in `speechMetadata`.
const prompt = [
  {
    type: "text",
    text: "How's it going today, Jane?",
    speechMetadata: { speaker: "Joe", style: "cheerful and friendly" }
  },
  {
    type: "text",
    text: "Not too bad, how about you?",
    speechMetadata: { speaker: "Jane", style: "calm and relaxed" }
  }
];

// Call `generateContent` to generate the multi-speaker speech output.
const result = await model.generateContent(prompt);
const inlineDataParts = result.response.inlineDataParts();

// Extract the WAV audio data and handle it for downstream use. For example:
if (inlineDataParts?.[0]) {
  const wavBase64 = inlineDataParts[0].inlineData.data;  // WAV audio bytes (24kHz, 1 channel, 16-bit PCM)
  // Decode base64 to ArrayBuffer
  const wavBuffer = Uint8Array.from(atob(wavBase64), c => c.charCodeAt(0)).buffer;

  // The response is a complete WAV file, so the Web Audio API can decode and play it directly.
  const audioCtx = new AudioContext();
  const audioBuffer = await audioCtx.decodeAudioData(wavBuffer);
  const source = audioCtx.createBufferSource();
  source.buffer = audioBuffer;
  source.connect(audioCtx.destination);
  source.start();
}

Dart


import 'package:firebase_ai/firebase_ai.dart';

// Configure a `SpeechConfig` for multiple speakers, assigning a voice to each speaker.
final multiSpeechConfig = SpeechConfig.multiSpeaker(
  multiSpeakerVoiceConfig: MultiSpeakerVoiceConfig(
    speakerVoiceConfigs: [
      SpeakerVoiceConfig(speaker: 'Joe', voiceName: 'Puck'),
      SpeakerVoiceConfig(speaker: 'Jane', voiceName: 'Kore'),
    ],
  ),
  languageCode: 'en-US',
);

// Set `responseModalities` to include `audio`.
final config = GenerationConfig(
  responseModalities: [ResponseModalities.audio],
  speechConfig: multiSpeechConfig,
);

// Initialize the Gemini Developer API backend service.
// Create a `GenerativeModel` instance with a model that supports speech generation.
final model = FirebaseAI.googleAI().generativeModel(
  model: 'gemini-3.8-flash-tts',
  generationConfig: config,
);

// Provide one `TextPart` per dialogue turn, specifying the speaker and optional style in `SpeechMetadata`.
final prompt = Content.multi([
  TextPart(
    "How's it going today, Jane?",
    speechMetadata: SpeechMetadata(speaker: 'Joe', style: 'cheerful and friendly'),
  ),
  TextPart(
    'Not too bad, how about you?',
    speechMetadata: SpeechMetadata(speaker: 'Jane', style: 'calm and relaxed'),
  ),
]);

// Call `generateContent` to generate the multi-speaker speech output.
final response = await model.generateContent([prompt]);

// Extract the WAV audio data and handle it for downstream use. For example:
final part = response.candidates.first.content.parts.first;
if (part is InlineDataPart && part.mimeType.startsWith('audio/')) {
  final Uint8List wavData = part.bytes;  // WAV audio bytes (24kHz, 1 channel, 16-bit PCM)

  // Play the WAV audio data using your preferred audio player or helper (which you'll need to implement).
  await playAudio(wavData);
}

Unity


using Firebase.AI;

// Initialize the Gemini Developer API backend service.
var ai = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI());

// Configure a `SpeechConfig` for multiple speakers, assigning a voice to each speaker.
var multiSpeakerVoiceConfig = new MultiSpeakerVoiceConfig(
  new System.Collections.Generic.List<SpeakerVoiceConfig> {
    SpeakerVoiceConfig.UsePrebuiltVoice("Joe", "Puck"),
    SpeakerVoiceConfig.UsePrebuiltVoice("Jane", "Kore")
  }
);

var multiSpeechConfig = SpeechConfig.UseMultiSpeakerVoice(multiSpeakerVoiceConfig, "en-US");

// Set `responseModalities` to include `Audio`.
var config = new GenerationConfig(
  responseModalities: new System.Collections.Generic.List<ResponseModality> { ResponseModality.Audio },
  speechConfig: multiSpeechConfig
);

// Create a `GenerativeModel` instance with a model that supports speech generation.
var model = ai.GetGenerativeModel(
  modelName: "gemini-3.8-flash-tts",
  generationConfig: config
);

// Provide one `TextPart` per dialogue turn, specifying the speaker and optional style in `SpeechMetadata`.
var prompt = new ModelContent(
  new ModelContent.TextPart(
    "How's it going today, Jane?",
    new SpeechMetadata(speaker: "Joe", style: "cheerful and friendly")
  ),
  new ModelContent.TextPart(
    "Not too bad, how about you?",
    new SpeechMetadata(speaker: "Jane", style: "calm and relaxed")
  )
);

// Call `GenerateContentAsync` to generate the multi-speaker speech output.
var response = await model.GenerateContentAsync(prompt);

// Extract the WAV audio data and handle it for downstream use. For example:
if (response.Candidates.Count > 0) {
  foreach (var part in response.Candidates[0].Content.Parts) {
    if (part is ModelContent.InlineDataPart inlineData) {
      byte[] wavData = inlineData.Data;  // WAV audio bytes (24kHz, 1 channel, 16-bit PCM)

      // Pass the audio data to your audio player or playback helper (which you'll need to implement).
      PlayAudio(wavData);
    }
  }
}

जवाब को स्ट्रीम करें

इस सैंपल को आज़माने से पहले, इस गाइड का शुरू करने से पहले सेक्शन पूरा करें. इससे आपको अपना प्रोजेक्ट और ऐप्लिकेशन सेट अप करने में मदद मिलेगी.
इस सेक्शन में, आपको Gemini APIसेवा देने वाली कंपनी के लिए एक बटन पर भी क्लिक करना होगा, ताकि आपको इस पेज पर सेवा देने वाली कंपनी के हिसाब से कॉन्टेंट दिखे.

ऑडियो जनरेट होने में लगने वाले समय (टीटीएफ़ए) को कम करने और रीयल-टाइम में ऑडियो चलाने के लिए, ऑडियो रिस्पॉन्स को जनरेट होते ही स्ट्रीम किया जा सकता है. इसके लिए, पूरी क्लिप के जनरेट होने का इंतज़ार करने की ज़रूरत नहीं है. स्ट्रीमिंग की सुविधा, एक स्पीकर और एक से ज़्यादा स्पीकर वाले कॉन्फ़िगरेशन, दोनों के साथ काम करती है.

स्पीच को स्ट्रीम करने के लिए, generateContent के बजाय generateContentStream को कॉल करें. यूनरी अनुरोधों (जो WAV फ़ाइल दिखाते हैं) के उलट, स्ट्रीमिंग अनुरोधों से बिना हेडर वाले रॉ 16-बिट साइन किए गए लिटिल-एंडियन पीसीएम चंक (audio/l16, 24 किलोहर्ट्ज़, मोनो) मिलते हैं. इससे हर चंक को सीधे अपने ऑडियो आउटपुट बफ़र में जोड़ा जा सकता है.

Swift


import FirebaseAILogic

// Initialize the Gemini Developer API backend service.
let ai = FirebaseAI.firebaseAI(backend: .googleAI())

// Set `responseModalities` to include `.audio`.
// Configure a `SpeechConfig` with your chosen voice name (and optionally a language code).
let config = GenerationConfig(
  responseModalities: [.audio],
  speechConfig: SpeechConfig(voiceName: "Kore")
)

// Create a `GenerativeModel` instance with a model that supports speech generation.
let model = ai.generativeModel(
  modelName: "gemini-3.8-flash-tts",
  generationConfig: config
)

// Provide the verbatim text to synthesize as speech.
// Optionally provide turn-level style metadata.
let prompt = TextPart(
  "Tell me a story about a brave knight.",
  speechMetadata: SpeechMetadata(style: "warm, engaging audiobook narrator")
)

// Call `generateContentStream` to stream the speech output as it is generated.
let responseStream = try model.generateContentStream(prompt)

// Extract the raw PCM audio chunks and queue them for playback:
for try await chunk in responseStream {
  for part in chunk.inlineDataParts {
    let pcmChunk = part.data  // Headerless raw PCM bytes (24kHz, 1 channel, 16-bit)

    // Append the audio chunk to your audio queue/buffer for playback (using a helper you implement).
    appendAudioChunk(pcmChunk)
  }
}

Kotlin

Kotlin के लिए, इस एसडीके में मौजूद तरीके, सस्पेंड फ़ंक्शन हैं. इन्हें Coroutine स्कोप से कॉल करना ज़रूरी है.

// Set `responseModalities` to include `AUDIO`.
// Configure a `SpeechConfig` with your chosen voice name (and optionally a language code).
val config = generationConfig {
    responseModalities = listOf(ResponseModality.AUDIO)
    speechConfig = SpeechConfig(voice = Voice("Kore"))
}

// Initialize the Gemini Developer API backend service.
// Create a `GenerativeModel` instance with a model that supports speech generation.
val model = Firebase.ai(backend = GenerativeBackend.googleAI())
    .generativeModel(
        modelName = "gemini-3.8-flash-tts",
        generationConfig = config
    )

// Provide the verbatim text to synthesize as speech.
// Optionally provide turn-level style metadata.
val prompt = content {
    text(
        text = "Tell me a story about a brave knight.",
        speechMetadata = SpeechMetadata(style = "warm, engaging audiobook narrator")
    )
}

// Call `generateContentStream` to stream the speech output as it is generated.
// Extract the raw PCM audio chunks and queue them for playback.
model.generateContentStream(prompt).collect { chunk ->
    val part = chunk.candidates.firstOrNull()?.content?.parts?.firstOrNull()
    if (part is InlineDataPart) {
        val pcmChunk = part.inlineData  // Headerless raw PCM bytes (24kHz, 1 channel, 16-bit)

        // Append the audio chunk to your audio queue/buffer for playback (using a helper you implement).
        appendAudioChunk(pcmChunk)
    }
}

Java

Java के लिए, इस एसडीके में मौजूद स्ट्रीमिंग के तरीके, Reactive Streams लाइब्रेरी से Publisher टाइप दिखाते हैं.

// Set `responseModalities` to include `AUDIO`.
// Configure a `SpeechConfig` with your chosen voice name (and optionally a language code).
GenerationConfig config = new GenerationConfig.Builder()
    .setResponseModalities(Collections.singletonList(ResponseModality.AUDIO))
    .setSpeechConfig(new SpeechConfig(new Voice("Kore")))
    .build();

// Initialize the Gemini Developer API backend service.
// Create a `GenerativeModel` instance with a model that supports speech generation.
GenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.googleAI())
        .generativeModel("gemini-3.8-flash-tts", config);

// Use the `GenerativeModelFutures` Java compatibility layer.
GenerativeModelFutures model = GenerativeModelFutures.from(ai);

// Provide the verbatim text to synthesize as speech.
// Optionally provide turn-level style metadata.
Content content = new Content.Builder()
    .addText(
        "Tell me a story about a brave knight.",
        new SpeechMetadata(null, "warm, engaging audiobook narrator"))
    .build();

// Call `generateContentStream` to stream the speech output as it is generated.
Publisher<GenerateContentResponse> streamingResponse =
    model.generateContentStream(content);

// Extract the raw PCM audio chunks and queue them for playback.
streamingResponse.subscribe(new Subscriber<GenerateContentResponse>() {
  @Override
  public void onSubscribe(Subscription s) {
      s.request(Long.MAX_VALUE);
  }

  @Override
  public void onNext(GenerateContentResponse chunk) {
      Part part = chunk.getCandidates().get(0).getContent().getParts().get(0);
      if (part instanceof InlineDataPart) {
          byte[] pcmChunk = ((InlineDataPart) part).getInlineData();  // Headerless raw PCM bytes (24kHz, 1 channel, 16-bit)

          // Append the audio chunk to your audio queue/buffer for playback (using a helper you implement).
          appendAudioChunk(pcmChunk);
      }
  }

  @Override
  public void onComplete() {
      // Audio stream complete.
  }

  @Override
  public void onError(Throwable t) {
      t.printStackTrace();
  }
});

Web


import { initializeApp } from "firebase/app";
import { getAI, getGenerativeModel, GoogleAIBackend, ResponseModality } from "firebase/ai";

// TODO(developer): Replace with your app's Firebase configuration
const firebaseConfig = { /* ... */ };
const firebaseApp = initializeApp(firebaseConfig);

// Initialize the Gemini Developer API backend service.
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });

// Set `responseModalities` to include `AUDIO`.
// Configure a `SpeechConfig` with your chosen voice name (and optionally a language code).
const generationConfig = {
  responseModalities: [ResponseModality.AUDIO],
  speechConfig: {
    voiceConfig: { prebuiltVoiceConfig: { voiceName: "Kore" } }
  }
};

// Create a `GenerativeModel` instance with a model that supports speech generation.
const model = getGenerativeModel(ai, {
  model: "gemini-3.8-flash-tts",
  generationConfig
});

// Provide the verbatim text to synthesize as speech.
// Optionally provide turn-level style metadata.
const prompt = [
  {
    type: "text",
    text: "Tell me a story about a brave knight.",
    speechMetadata: { style: "warm, engaging audiobook narrator" }
  }
];

// Call `generateContentStream` to stream the speech output as it is generated.
const result = await model.generateContentStream(prompt);

// Extract the raw PCM audio chunks and queue them for playback:
const playbackQueue = [];
for await (const chunk of result.stream) {
  const inlineDataParts = chunk.inlineDataParts();
  if (inlineDataParts?.[0]) {
    const pcmBase64 = inlineDataParts[0].inlineData.data;  // Headerless raw PCM bytes (24kHz, 1 channel, 16-bit)
    const pcmBuffer = Uint8Array.from(atob(pcmBase64), c => c.charCodeAt(0)).buffer;

    // Append the audio chunk to your audio queue/buffer for playback.
    // In a real app, start playing chunks as they arrive (rather than waiting for
    // the whole stream to finish) to get the low-latency benefit of streaming.
    playbackQueue.push(pcmBuffer);
  }
}

// To play back an array of PCM buffers in sequence, pass them to your playback helper (which you'll need to write).
processPlaybackQueue(playbackQueue);

Dart


import 'package:firebase_ai/firebase_ai.dart';

// Set `responseModalities` to include `audio`.
// Configure a `SpeechConfig` with your chosen voice name (and optionally a language code).
final config = GenerationConfig(
  responseModalities: [ResponseModalities.audio],
  speechConfig: SpeechConfig(voiceName: 'Kore'),
);

// Initialize the Gemini Developer API backend service.
// Create a `GenerativeModel` instance with a model that supports speech generation.
final model = FirebaseAI.googleAI().generativeModel(
  model: 'gemini-3.8-flash-tts',
  generationConfig: config,
);

// Provide the verbatim text to synthesize as speech.
// Optionally provide turn-level style metadata.
final prompt = Content.multi([
  TextPart(
    'Tell me a story about a brave knight.',
    speechMetadata: SpeechMetadata(style: 'warm, engaging audiobook narrator'),
  ),
]);

// Call `generateContentStream` to stream the speech output as it is generated.
final responseStream = model.generateContentStream([prompt]);

// Extract the raw PCM audio chunks and queue them for playback:
await for (final chunk in responseStream) {
  final part = chunk.candidates.first.content.parts.first;
  if (part is InlineDataPart && part.mimeType.startsWith('audio/')) {
    final Uint8List pcmChunk = part.bytes;  // Headerless raw PCM bytes (24kHz, 1 channel, 16-bit)

    // Append the audio chunk to your audio queue/buffer for playback (using a helper you implement).
    appendAudioChunk(pcmChunk);
  }
}

Unity


using System.Collections.Generic;
using System.Linq;
using Firebase.AI;

// Initialize the Gemini Developer API backend service.
var ai = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI());

// Set `ResponseModalities` to include `Audio`.
// Configure a `SpeechConfig` with your chosen voice name (and optionally a language code).
var config = new GenerationConfig(
    responseModalities: new List<ResponseModality> { ResponseModality.Audio },
    speechConfig: SpeechConfig.UsePrebuiltVoice("Kore")
);

// Create a `GenerativeModel` instance with a model that supports speech generation.
var model = ai.GetGenerativeModel(
    modelName: "gemini-3.8-flash-tts",
    generationConfig: config
);

// Provide the verbatim text to synthesize as speech.
// Optionally provide turn-level style metadata.
var prompt = new ModelContent(
    new ModelContent.TextPart(
        "Tell me a story about a brave knight.",
        new SpeechMetadata(speaker: null, style: "warm, engaging audiobook narrator")
    )
);

// Call `GenerateContentStreamAsync` to stream the speech output as it is generated.
var responseStream = model.GenerateContentStreamAsync(prompt);

// Extract the raw PCM audio chunks and queue them for playback:
await foreach (var response in responseStream)
{
    var audioParts = response.Candidates.FirstOrDefault().Content.Parts
                            .OfType<ModelContent.InlineDataPart>();

    foreach (var part in audioParts)
    {
        byte[] pcmChunk = part.Data; // Headerless raw PCM bytes (24kHz, 1 channel, 16-bit)

        // Append the audio chunk to your audio queue/buffer for playback (using a helper you implement).
        AppendAudioChunk(pcmChunk);
    }
}



मेटाडेटा और इनलाइन टैग की मदद से, बोलकर जवाब देने की सुविधा को कंट्रोल करना

Gemini 3.x TTS मॉडल, हर TextPart में मौजूद text को हूबहू ट्रांसक्रिप्ट के तौर पर मानते हैं, ताकि उसे ज़ोर से बोला जा सके. टेक्स्ट स्ट्रिंग में, "Say cheerfully:" जैसे निर्देश न लिखें. ऐसा करने पर, हो सकता है कि मॉडल उन शब्दों को ज़ोर से पढ़कर सुनाए.

इन तरीकों का इस्तेमाल करके, वोकल परफ़ॉर्मेंस को डायरेक्ट किया जा सकता है:

टर्न-लेवल style और इनलाइन वोकल टैग

परफ़ॉर्मेंस के निर्देशों को स्कोप के हिसाब से, SpeechMetadata और इनलाइन ऐंगल-ब्रैकेट टैग (<...>) के बीच बांटें.

दायरा कहां लगाएं इससे क्या कंट्रोल किया जाता है उदाहरण
टर्न-लेवल
(पूरे टर्न के दौरान बरकरार रहता है)
SpeechMetadata.style भावना, व्यक्तित्व, टोन, उतार-चढ़ाव, पिच, और बोलने की कुल रफ़्तार "cheerful and friendly",
"whispered, nervous",
"speaking rapidly",
"out of breath",
"sarcastic"
Point-in-time
(localized to a specific moment in the sentence)
<...> का इस्तेमाल करके, TextPart टेक्स्ट में इनलाइन बातचीत के अलावा, इंसान की आवाज़ में अचानक होने वाले बदलाव, सांस लेने की आवाज़, पॉज़ या कुछ समय के लिए आवाज़ में होने वाले बदलाव "Wait... <short pause> did you hear that? <gasp> Someone's at the door."

बोलने की रफ़्तार, रुककर बोलना, पिच, और ज़ोर देना

  • विराम चिह्न और एलिप्सिस: ट्रांसक्रिप्ट में कॉमा, डैश (--), और एलिप्सिस (...) का इस्तेमाल करें, ताकि वाक्यांश सामान्य लगें और हिचकिचाहट का पता चले.

  • साफ़ तौर पर दिखने वाले पॉज़ टैग: <short pause> या <long pause> को इनलाइन में उस जगह पर डालें जहां स्पीकर को रुकना चाहिए:

    Hold on, let me think... <short pause> Alright, I've got it.
    
  • बोलने की रफ़्तार और पिच: SpeechMetadata में style सेट करें, ताकि टर्न के लिए कुल रफ़्तार, पिच या उतार-चढ़ाव को कंट्रोल किया जा सके. उदाहरण के लिए, "speaking rapidly and excitedly", "speaking slowly" या "monotone and flat". अगर मोनोलॉग के बीच में भावना या रफ़्तार बदलती है, तो स्क्रिप्ट को अलग-अलग TextPart टर्न में बांटें. हर टर्न का अपना style होना चाहिए.

  • शब्दों पर ज़ोर देना: ट्रांसक्रिप्ट में कुछ शब्दों को कैपिटल लेटर में लिखें, ताकि उन पर ज़ोर दिया जा सके:

    It was a VERY long day <sigh> ... nobody listens anymore.
    

वोकल बर्स्ट और बोली के अलावा अन्य आवाज़ें

ऐंगल ब्रैकेट (<...>) का इस्तेमाल करके, इंसानों की आवाज़ों को इनलाइन शामिल करें. इन्हें ठीक उसी शब्द के बाद शामिल करें जहां आवाज़ सुनाई देनी चाहिए. बिना आवाज़ वाले साउंड इफ़ेक्ट के बजाय, इंसान की आवाज़ का इस्तेमाल करें. यहां इस्तेमाल करने के लिए सुझाए गए टैग दिए गए हैं:

<argh> <exhales> <heavy breath> <scream> <snort>
<breath> <gasp> <hiss> <short pause> <sob>
<cackle> <giggle> <laugh> /
<laughter>
<shout> <throat-clearing>
<cheer> <groan> <long pause> <shriek> <tsk>
<chuckle> /
<chuckles>
<growl> <moan> <sigh> /
<sighs>
<whimper>
<cough> <grr> <pant> <sneeze> <whispers> /
<whispering>
<cry> <grunt> <pff> /
<phew>
<snicker> <yawn>

बैकचैनल और ओवरलैप होने वाली स्पीच

एक से ज़्यादा लोगों के बीच होने वाली बातचीत में, सुनने वाले व्यक्ति की छोटी प्रतिक्रियाओं को पाइप वर्णों (|reaction|) में रैप किया जा सकता है. इसके लिए, बोलने वाले व्यक्ति की ट्रांसक्रिप्ट में पाइप वर्णों का इस्तेमाल करें. जो स्पीकर नहीं बोल रहा है वह बिना अपनी बारी का इंतज़ार किए, बातचीत के बीच में नैचुरल तरीके से बोल सकता है:

  • श्रोता के छोटे बैकचैनल:

    • पहला टर्न (speaker: "Joe"): "So the launch is Thursday |oh hmm| Are we actually ready?"
    • दूसरा टर्न (speaker: "Jane"): "Ready enough |oh really?| The last blocker cleared this morning."
  • एक साथ कई आवाज़ें: एक साथ कई आवाज़ें जनरेट करने के लिए, एक से ज़्यादा पाइप सेगमेंट का इस्तेमाल किया जा सकता है. यह सुविधा gemini-3.8-flash-tts के साथ सबसे अच्छी तरह काम करती है. जैसे:

    • पहला टर्न (speaker: "Joe"): "Let's surprise him on three |ok| ready?"
    • दूसरा टर्न (speaker: "Jane"): "one. two. three. |happy| happy |birthday| birthday!"

कंसिस्टेंसी बनाए रखने के सबसे असरदार तरीके

  • पहले style के बिना टेस्ट करें. इसके बाद, style में छोटे-छोटे बदलाव करें: ज़्यादातर अनुरोध, style फ़ील्ड को खाली रखने पर नैचुरल लगते हैं. सिर्फ़ तब style छोटा वाक्यांश (जैसे, "casual, friendly" या "reflective and awe-inspired") जोड़ें, जब किसी टर्न में डिलीवरी में खास बदलाव की ज़रूरत हो. साथ ही, जब आपको एक जैसी बेसलाइन चाहिए, तो टर्न में उसी छोटी स्ट्रिंग का फिर से इस्तेमाल करें.

  • ट्रांसक्रिप्ट में सामान्य बोलचाल की भाषा का इस्तेमाल करें: ट्रांसक्रिप्ट में, बातचीत के दौरान इस्तेमाल होने वाले छोटे शब्द और सामान्य रूप से इस्तेमाल होने वाले शब्द (जैसे, "Oh, uh, yeah I think... hm, that's interesting") शामिल करें.

  • लेगसी [Audio Profile] ब्लॉक और मेटा-निर्देशों से बचें: पर्सोना के बारे में बताने वाले लंबे पैराग्राफ़, [Director's Notes] ब्लॉक या "maintain identical timbre" जैसे मेटा-निर्देशों से आवाज़ में बदलाव होने की संभावना बढ़ जाती है. इसके बजाय, Extended Voice Library से मिलती-जुलती आवाज़ चुनें या वॉइस डिज़ाइन की मदद से, किसी किरदार के लिए हमेशा इस्तेमाल की जा सकने वाली आवाज़ बनाएं.

  • style में, आवाज़ की ऐसी विशेषताओं को बदलने की कोशिश न करें जिन्हें बदला नहीं जा सकता: आवाज़ की उम्र, लिंग या स्थायी लहजे को बदलने के लिए, SpeechMetadata.style का इस्तेमाल न करें. इसके बजाय, ऐसी आवाज़ चुनें या डिज़ाइन करें जिसमें ये विशेषताएं हों.



इस्तेमाल की जा सकने वाली आवाज़ें

Gemini 3.x TTS मॉडल में आवाज़ सेट करने के तीन तरीके हैं:

सभी आवाज़ें एक से ज़्यादा भाषाओं में उपलब्ध हैं. इसका मतलब है कि इस्तेमाल की जा सकने वाली किसी भी भाषा में स्पीच जनरेट करने के लिए, एक ही आवाज़ का इस्तेमाल किया जा सकता है. साथ ही, स्पीकर की पहचान को एक जैसा बनाए रखा जा सकता है. उदाहरण के लिए, अगर आपने आवाज़ को पहले से मौजूद Kore आवाज़ पर सेट किया है और स्पैनिश, हिन्दी, और वियतनामी भाषा में प्रॉम्प्ट भेजे हैं, तो जवाबों में इन सभी भाषाओं में Kore आवाज़ का इस्तेमाल किया जाएगा. पहले से तैयार की गई 30 मुख्य आवाज़ें, अलग-अलग कामों के लिए इस्तेमाल की जाने वाली बहुभाषी आवाज़ें हैं. वहीं, एक्सटेंडेड वॉइस लाइब्रेरी की आवाज़ें और कस्टम आवाज़ें, किसी मुख्य स्थान या क्षेत्रीय लहजे से जुड़ी होती हैं. इसका मतलब है कि वे अब भी अन्य भाषाएं बोल सकते हैं. हालांकि, वे अपनी मुख्य भाषा में सबसे ज़्यादा नैचुरल लगते हैं.

पहले से मौजूद मुख्य आवाज़ें

Gemini टीटीएस मॉडल, पहले से मौजूद 30 मुख्य आवाज़ों के साथ काम करते हैं. जैसे, Kore, Puck, Zephyr, Charon, और Fenrir. हर आवाज़ की अपनी अलग खासियत होती है. SpeechConfig में आवाज़ का नाम पास करें. इसके बारे में इस पेज पर पहले दिए गए सिंगल-स्पीकर और मल्टी-स्पीकर के उदाहरणों में बताया गया है.

एक्सटेंडेड वॉइस लाइब्रेरी में पहले से मौजूद आवाज़ें

पहले से मौजूद 30 आवाज़ों के अलावा, Extended Voice Library में पहले से सेट की गई 2,000 से ज़्यादा आवाज़ें उपलब्ध हैं. इनमें अलग-अलग भाषाओं, क्षेत्रीय लहजों, उम्र की सीमाओं, किरदारों, और इस्तेमाल के उदाहरणों (जैसे, ऑडियो बुक, ग्राहक सहायता एजेंट, और समाचार पढ़ने वाले लोग) के हिसाब से आवाज़ें शामिल हैं.

अपने ऐप्लिकेशन में Extended Voice Library की किसी भी आवाज़ का इस्तेमाल करने का तरीका यहां बताया गया है:

  1. इनमें से किसी एक विकल्प का इस्तेमाल करके, आवाज़ें ब्राउज़ करें और उनकी झलक देखें:

  2. SpeechConfig में आवाज़ के नाम के तौर पर, लाइब्रेरी की आवाज़ की id स्ट्रिंग पास करें. यह इस पेज पर पहले दिए गए सिंगल-स्पीकर के उदाहरण से मिलता-जुलता है.

कस्टम वॉइस: वॉइस डिज़ाइन और वॉइस रेप्लिकेशन

Gemini 3.x TTS मॉडल में, पसंद के मुताबिक आवाज़ें भी इस्तेमाल की जा सकती हैं. इसके लिए, इनमें से कोई एक विकल्प चुनें:

  • वॉइस डिज़ाइन (झलक): स्पीकर की उम्र, आवाज़ की क्वालिटी, लहजे, और व्यक्तित्व के बारे में नैचुरल लैंग्वेज में जानकारी देकर, अपनी पसंद के मुताबिक आवाज़ जनरेट करें. इसके लिए, आपको किसी रेफ़रंस ऑडियो की ज़रूरत नहीं होगी. सेव किया गया वॉइस आईडी (voice_...) दिखाता है.

  • वॉइस रेप्लिकेशन (झलक): किसी स्पीकर की आवाज़, टोन, और बोलने के अंदाज़ को रेप्लिकेट करें. इसके लिए, रेफ़रंस रिकॉर्डिंग और सहमति देने वाली रिकॉर्डिंग का इस्तेमाल करें. यह प्रोजेक्ट में सेव किया गया वॉइस आईडी (voice_...) या स्टेटलेस, ऐप्लिकेशन से मैनेज किया जाने वाला वॉइस रेप्लिकेशन पासकोड (voicekey_...) दिखाता है.

Firebase AI Logic ऐप्लिकेशन में कस्टम आवाज़ का इस्तेमाल करना

कस्टम आवाज़ें बनाना और उन्हें मैनेज करना, एडमिन या सर्वर-साइड ऑपरेशन है. इसलिए, Firebase AI Logic क्लाइंट एसडीके, आवाज़ों को मैनेज करने वाले एंडपॉइंट (CreateVoice, ListVoices, DeleteVoice) को सीधे तौर पर एक्सपोज़ नहीं करते.

इसके बजाय, पसंद के मुताबिक आवाज़ का इस्तेमाल करने के लिए, आपको ये काम करने होंगे:

  1. इनमें से किसी एक विकल्प का इस्तेमाल करके, अपनी पसंद के मुताबिक़ आवाज़ बनाएं:

  2. voice_<id> या voicekey_<id> स्ट्रिंग को SpeechConfig में आवाज़ के नाम के तौर पर पास करें. यह इस पेज पर पहले दिए गए सिंगल-स्पीकर के उदाहरण से मिलता-जुलता है.

कस्टम आवाज़ों का इस्तेमाल करते समय, इन ज़रूरी शर्तों का ध्यान रखें:

  • जगह (Agent Platform Gemini API (formerly Vertex AI)): पसंद के मुताबिक बनाई गई आवाज़ें (voice_... और voicekey_...), सिर्फ़ global जगह पर काम करती हैं. Firebase AI Logic का इस्तेमाल करने पर, यह जगह डिफ़ॉल्ट रूप से सेट होती है.

  • एक स्पीकर वाले अनुरोध: MultiSpeakerVoiceConfig सिर्फ़ पहले से मौजूद मुख्य आवाज़ों के साथ काम करता है. कई किरदारों वाले डायलॉग में, कस्टम डिज़ाइन की गई या कॉपी की गई आवाज़ों का इस्तेमाल करने के लिए, हर किरदार की आवाज़ को अलग-अलग सिंगल-स्पीकर अनुरोध में सिंथेसाइज़ करें.

  • सेव करके रखना और समयसीमा खत्म होना: सेव की गई कस्टम आवाज़ें (voice_...) एक साल तक इस्तेमाल न करने पर मिट जाती हैं. हालांकि, सिंथेसिस का हर अनुरोध, एक साल तक सेव करके रखने के टाइमर को रीसेट कर देता है. स्टेटलेस वॉइस रेप्लिकेशन कुंजियां (voicekey_...), बनाए जाने के सात दिन बाद मिट जाती हैं.

यह सुविधा इन भाषाओं में काम करती है

Gemini 3.x TTS के मॉडल, आपके इनपुट टेक्स्ट से भाषा का अपने-आप पता लगा लेते हैं. इसमें, एक से ज़्यादा स्पीकर वाले डायलॉग में, अलग-अलग टर्न के दौरान प्रॉम्प्ट की भाषा बदलने पर भी भाषा का पता लगाया जा सकता है.

  • Gemini 3.8 Flash TTS (gemini-3.8-flash-tts): यह 132 भाषाओं में उपलब्ध है.
  • Gemini 3.8 Flash‑Lite TTS (gemini-3.8-flash-lite-tts): यह 103 भाषाओं में उपलब्ध है.

(ज़रूरी नहीं) भाषा कोड को साफ़ तौर पर सेट करना

अगर आपको टेक्स्ट को किसी खास भाषा में बदलने की सुविधा को सीमित करना है, तो अपने SpeechConfig में BCP-47 languageCode पास किया जा सकता है:



ऑडियो आउटपुट के फ़ॉर्मैट

InlineDataPart में डिफ़ॉल्ट ऑडियो फ़ॉर्मैट इस बात पर निर्भर करता है कि आपने यूनेरी या स्ट्रीमिंग का अनुरोध किया है:

अनुरोध का तरीका डिफ़ॉल्ट तौर पर दिखाया जाने वाला एमआईएमई टाइप ऑडियो फ़ॉर्मैट की जानकारी
यूनरी
(generateContent)
audio/wav स्टैंडर्ड 44-बाइट RIFF हेडर वाली पूरी WAV फ़ाइल (16-बिट लीनियर पीसीएम, 24 किलोहर्ट्ज़, मोनो, लिटिल-एंडियन).

इसे स्टैंडर्ड प्लैटफ़ॉर्म के ऑडियो प्लेयर से सीधे तौर पर चलाया जा सकता है. जैसे, Apple प्लैटफ़ॉर्म पर AVAudioPlayer, वेब ऑडियो decodeAudioData या फ़ाइल-आधारित प्लेयर.

स्ट्रीमिंग
(generateContentStream)
audio/l16 हेडरलेस रॉ 16-बिट साइन किया गया लिटिल-एंडियन पीसीएम चंक 24 किलोहर्ट्ज़, मोनो पर.

इसे कम इंतज़ार के समय वाली बफ़र सूची और हर चंक के फ़ाइल हेडर के बिना स्ट्रिंग जोड़ने की प्रोसेस के लिए डिज़ाइन किया गया है.



सीमाएं और ज़रूरी शर्तें

Gemini 3.x TTS मॉडल इस्तेमाल करते समय, इन सीमाओं और ज़रूरी शर्तों का ध्यान रखें:

  • सिर्फ़ टेक्स्ट इनपुट, सिर्फ़ ऑडियो आउटपुट: टीटीएस मॉडल सिर्फ़ टेक्स्ट इनपुट (TextPart) स्वीकार करते हैं. साथ ही, responseModalities को AUDIO पर सेट करना ज़रूरी है.

  • सिर्फ़ शब्द-दर-शब्द सिंथेसिस: मॉडल, दी गई ट्रांसक्रिप्ट को शब्द-दर-शब्द पढ़ते हैं और बातचीत वाले टेक्स्ट जवाब जनरेट नहीं करते.

  • टोकन की सीमाएँ: हर अनुरोध के लिए, ज़्यादा से ज़्यादा 8,192 इनपुट टोकन और 16,384 आउटपुट टोकन इस्तेमाल किए जा सकते हैं.

  • एक से ज़्यादा स्पीकर के लिए सीमा: MultiSpeakerVoiceConfig के लिए दो स्पीकर ज़रूरी हैं. साथ ही, यह सिर्फ़ पहले से मौजूद मुख्य आवाज़ों के साथ काम करता है. एक से ज़्यादा स्पीकर वाले अनुरोध में मौजूद हर TextPart में, कॉन्फ़िगर किए गए स्पीकर में से किसी एक से मैच होने वाला SpeechMetadata.speaker शामिल होना चाहिए.

  • काम न करने वाले पैरामीटर और तरीके:

    • Firebase AI Logic एसडीके, डिफ़ॉल्ट ऑडियो आउटपुट फ़ॉर्मैट को बदलने के लिए, GenerationConfig में responseFormat को सेट करने की सुविधा अभी नहीं देते.
    • systemInstruction, temperature, topP, topK, candidateCount, presencePenalty या frequencyPenalty को सेट नहीं किया जा सकता. ऐसा करने पर, अनुरोध पूरा नहीं हो पाता और 400 INVALID_ARGUMENT गड़बड़ी का मैसेज दिखता है.
    • टीटीएस मॉडल के लिए, countTokens को कॉल नहीं किया जा सकता. जवाब में, टोकन की संख्या usageMetadata में दिखती है.
  • जगह (Agent Platform Gemini API (formerly Vertex AI)): कस्टम आवाज़ों (voice_... और voicekey_...) के लिए, global जगह की जानकारी देना ज़रूरी है. Firebase AI Logic का इस्तेमाल करने पर, यह जगह की डिफ़ॉल्ट जानकारी होती है.



gemini-3.1-flash-tts-preview से माइग्रेट करें

gemini-3.8-flash-tts और gemini-3.8-flash-lite-tts, दोनों एक ही अनुरोध स्ट्रक्चर का इस्तेमाल करते हैं. अपने वर्कलोड के लिए सबसे सही मॉडल चुनने के लिए, मॉडल की तुलना करने वाली टेबल देखें. इसके बाद, अपने कोड को इस तरह अपडेट करें:

क्षेत्रफल gemini-3.1-flash-tts-preview (लेगसी) gemini-3.8-flash-tts और
gemini-3.8-flash-lite-tts
डिलीवरी का स्टाइल और डायरेक्शन प्रॉम्प्ट टेक्स्ट में एम्बेड किया गया हो. उदाहरण के लिए:
"Say cheerfully: ...",
[Audio Profile],
[Director's Notes]
हर TextPart को SpeechMetadata(style: "...") में पास किया गया

TextPart टेक्स्ट को हूबहू ट्रांसक्रिप्ट के मुताबिक रखें.

एक से ज़्यादा स्पीकर वाला डायलॉग इनलाइन "Speaker: ..." प्रीफ़िक्स वाली सिंगल टेक्स्ट स्ट्रिंग हर डायलॉग टर्न के लिए एक TextPart, जिसमें हर TextPart पर SpeechMetadata(speaker: "...", style: "...") हो
इनलाइन वोकल टैग स्क्वेयर ब्रैकेट. उदाहरण के लिए,
[sigh] या [laughs]
ऐंगल ब्रैकेट. उदाहरण के लिए,
<sigh> या <laugh> या <short pause>

बोलने में होने वाली रुकावटों, जैसे कि "uhm" को सीधे टेक्स्ट में लिखें.

डिफ़ॉल्ट यूनेरी ऑडियो फ़ॉर्मैट (generateContent) बिना हेडर वाला रॉ 16-बिट PCM (audio/pcm, 24 किलोहर्ट्ज़, मोनो) पूरी WAV फ़ाइल (audio/wav, 16-बिट PCM, 24 किलोहर्ट्ज़, मोनो, 44-बाइट RIFF हेडर के साथ)

स्ट्रीमिंग (generateContentStream) हेडरलेस रॉ 16-बिट पीसीएम (audio/l16) ही रहती है.

सिस्टम के निर्देश, तापमान, टॉप-P, टॉप-K अनदेखा किया गया 400 INVALID_ARGUMENT गड़बड़ी की वजह से अस्वीकार किया गया. अपने मॉडल कॉन्फ़िगरेशन से इन पैरामीटर को हटाएं.

माइग्रेशन चेकलिस्ट

  • Firebase AI Logic SDK के नए वर्शन पर अपग्रेड करें और अपने मॉडल का नाम बदलकर gemini-3.8-flash-tts या gemini-3.8-flash-lite-tts करें.

  • स्टाइल से जुड़े निर्देशों को SpeechMetadata.style में ले जाएं: अपनी प्रॉम्प्ट स्ट्रिंग से "Say cheerfully:" प्रीफ़िक्स, [Audio Profile], [Scene], और [Director's Notes] ब्लॉक हटाएं, ताकि मॉडल उन्हें ज़ोर से न पढ़े. इसके बजाय, SpeechMetadata.style में स्टाइल के बारे में कम शब्दों में जानकारी दें.

  • एक से ज़्यादा स्पीकर वाली स्क्रिप्ट को अलग-अलग TextPart टर्न में बांटना: "Joe: ... \n Jane: ..." टेक्स्ट ब्लॉक को TextPart ऑब्जेक्ट के कलेक्शन/सूची से बदलें. इसमें हर टर्न के लिए SpeechMetadata.speaker सेट किया जाता है.

  • [...] टैग को <...> टैग में बदलें: स्क्वेयर ब्रैकेट वाले ऑडियो टैग (जैसे, [whispers], [sighs], [laughs]) को ऐंगल ब्रैकेट वाले वोकल टैग (जैसे, <whispers>, <sigh>, <laugh>, <short pause>) से बदलें.

  • यूनरी ऑडियो प्लेबैक को अपडेट करना: generateContent अब बिना हेडर वाली रॉ पीसीएम फ़ाइल के बजाय, पूरी WAV फ़ाइल (44-बाइट वाले RIFF हेडर के साथ) दिखाता है. इसलिए, यूनरी रिस्पॉन्स में WAV हेडर को मैन्युअल तरीके से जोड़ने वाले किसी भी कोड को हटा दें. इसके अलावा, अगर Android के AudioTrack या Unity के AudioClip जैसे लो-लेवल बफ़र एपीआई में रॉ पीसीएम सैंपल फ़ीड किए जा रहे हैं, तो 44-बाइट वाले हेडर को छोड़ दें.

  • ऐसे कॉन्फ़िगरेशन पैरामीटर हटाएं जो काम नहीं करते: टीटीएस GenerativeModel कॉन्फ़िगरेशन से temperature, topP, topK, candidateCount, presencePenalty, frequencyPenalty, और systemInstruction हटाएं.



तुम और क्या कर सकती हो?

अन्य सुविधाएं आज़माना

कॉन्टेंट जनरेट करने की सुविधा को कंट्रोल करने का तरीका जानें

आपके पास प्रॉम्प्ट और मॉडल कॉन्फ़िगरेशन के साथ एक्सपेरिमेंट करने का विकल्प भी है. इसके अलावा, Google AI Studio का इस्तेमाल करके, जनरेट किया गया कोड स्निपेट भी पाया जा सकता है.

इस्तेमाल किए जा सकने वाले मॉडल के बारे में ज़्यादा जानें

अलग-अलग कामों के लिए उपलब्ध मॉडल, उनके कोटा, और कीमत के बारे में जानें.


Firebase AI Logic के साथ अपने अनुभव के बारे में सुझाव/राय देना या शिकायत करना