تصميم الصوت

تتيح لك ميزة "تصميم الصوت" إنشاء شخصية صوتية جديدة ودائمة من وصف باللغة الطبيعية باستخدام نقطة نهاية "الأصوات" في Gemini API (POST /v1beta/voices). وبدلاً من الاقتصار على الأصوات المسبقة الإنشاء أو تسجيل الصوت المرجعي، يمكنك وصف عمر الشخصية ونبرة صوتها ولهجتها وأسلوبها الأساسي في الكلام، وستتلقّى معرّف voice_... قابل لإعادة الاستخدام يتم حفظه في مشروعك.

أسرع طريقة لتصميم أصوات مخصّصة وتجربتها وتكرارها هي استخدام استوديو تصميم الأصوات التفاعلي في Google AI Studio. يمكنك إنشاء شخصيات مخصّصة من طلبات نصية، واختبارها باستخدام نصوص برمجية نموذجية، ونسخ معرّف voice_... الناتج مباشرةً إلى الرمز البرمجي لتطبيقك.

يتوافق كل من Gemini 3.8 Flash لتحويل النص إلى كلام (gemini-3.8-flash-tts) وGemini 3.8 Flash-Lite لتحويل النص إلى كلام (gemini-3.8-flash-lite-tts) مع ميزة "تصميم الصوت".

إنشاء صوت مصمَّم

استخدِم حزمة تطوير البرامج (SDK) من Google للذكاء الاصطناعي التوليدي (google-genai 2.25.0 أو إصدار أحدث / @google/genai 2.24.0 أو إصدار أحدث) أو واجهة REST API لإنشاء صوت مخصّص من وصف نصي. بالنسبة إلى أصوات "prompted"، يعرض كل من voices.create (CreateVoice) وvoices.get (GetVoice) حقل sample_audio مخصصًا للإخراج فقط (mime_type: "audio/wav"، data بترميز base64) حتى تتمكّن من الاستماع إلى الصوت الذي تم إنشاؤه على الفور:

Python

import base64
from google import genai

client = genai.Client()

# 1. Design a custom voice persona from natural language
created_voice = client.voices.create(
    store=True,
    voice={
        "model": "gemini-3.8-flash-tts",
        "type": "prompted",
        "display_name": "Warm British Astronomer",
        "gender": "male",
        "language_code": "en-GB",
        "prompted": {
            "input": (
                "A warm, thoughtful astronomer in his late 60s with a gentle"
                " British accent, speaking with quiet wonder."
            )
        },
    },
)

print(f"Created voice ID: {created_voice.id}")

# Save the generated sample_audio preview (audio/wav) returned by CreateVoice
if created_voice.sample_audio and created_voice.sample_audio.data:
    with open("voice_preview.wav", "wb") as f:
        f.write(base64.b64decode(created_voice.sample_audio.data))

JavaScript

import * as fs from "node:fs";
import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI();

// 1. Design a custom voice persona from natural language
const createdVoice = await ai.voices.create({
  store: true,
  voice: {
    model: "gemini-3.8-flash-tts",
    type: "prompted",
    display_name: "Warm British Astronomer",
    gender: "male",
    language_code: "en-GB",
    prompted: {
      input:
        "A warm, thoughtful astronomer in his late 60s with a gentle British accent, speaking with quiet wonder.",
    },
  },
});

console.log(`Created voice ID: ${createdVoice.id}`);

// Save the generated sample_audio preview (audio/wav) returned by CreateVoice
if (createdVoice.sample_audio?.data) {
  fs.writeFileSync(
    "voice_preview.wav",
    Buffer.from(createdVoice.sample_audio.data, "base64")
  );
}

REST

curl "https://generativelanguage.googleapis.com/v1beta/voices" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H "Content-Type: application/json" \
  -X POST \
  -d '{
    "store": true,
    "voice": {
      "model": "gemini-3.8-flash-tts",
      "type": "prompted",
      "display_name": "Warm British Astronomer",
      "gender": "male",
      "language_code": "en-GB",
      "prompted": {
        "input": "A warm, thoughtful astronomer in his late 60s with a gentle British accent, speaking with quiet wonder."
      }
    }
  }' | tee created_voice.json | jq -r '.sample_audio.data' | base64 --decode > voice_preview.wav

طريقة عمل تصميم Voice

  1. إنشاء صوت موجَّه: اتّصِل بـ voices.create (POST /v1beta/voices) باستخدام type="prompted" وstore=True.
  2. تلقّي معاينة دائمة voice_id وsample_audio: تنشئ واجهة برمجة التطبيقات هوية صوتية وتخزّنها في مشروعك وتعرض رقم تعريف دائمًا (مثل voice_abc123...) مع sample_audio (mime_type: "audio/wav"، data بترميز base64) الذي يحتوي على معاينة الصوت التي تم إنشاؤها.
  3. تركيب الكلام: مرِّر voice_id في أي مكان يتم فيه قبول اسم صوت في طلبات التركيب.

تركيب الكلام باستخدام صوتك المصمَّم

بعد إنشاء صوت، مرِّر id (voice_...) إلى واجهة برمجة التطبيقات Interactions لإنشاء كلام:

Python

import base64
from google import genai

client = genai.Client()

interaction = client.interactions.create(
    model="gemini-3.8-flash-tts",
    input=[{
        "type": "user_input",
        "content": [{
            "type": "text",
            "text": (
                "Look out past the rings of Saturn. Those faint photons left"
                " their source millions of years ago."
            ),
            "annotations": [{
                "type": "speech_metadata",
                "style": "reflective and awe-inspired",
            }],
        }],
    }],
    response_format={"type": "audio"},
    generation_config={
        "speech_config": [
            {"voice": created_voice.id},
        ]
    },
)

with open("designed_voice.wav", "wb") as f:
    f.write(base64.b64decode(interaction.output_audio.data))

JavaScript

import * as fs from "node:fs";
import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI();

const interaction = await ai.interactions.create({
  model: "gemini-3.8-flash-tts",
  input: [{
    type: "user_input",
    content: [{
      type: "text",
      text: "Look out past the rings of Saturn. Those faint photons left their source millions of years ago.",
      annotations: [{
        type: "speech_metadata",
        style: "reflective and awe-inspired",
      }],
    }],
  }],
  response_format: { type: "audio" },
  generation_config: {
    speech_config: [
      { voice: createdVoice.id },
    ],
  },
});

fs.writeFileSync("designed_voice.wav", Buffer.from(interaction.output_audio.data, "base64"));

REST

curl "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H "Content-Type: application/json" \
  -X POST \
  -d '{
    "model": "gemini-3.8-flash-tts",
    "input": [{
      "type": "user_input",
      "content": [{
        "type": "text",
        "text": "Look out past the rings of Saturn. Those faint photons left their source millions of years ago.",
        "annotations": [{
          "type": "speech_metadata",
          "style": "reflective and awe-inspired"
        }]
      }]
    }],
    "response_format": {"type": "audio"},
    "generation_config": {
      "speech_config": [
        {"voice": "voice_YOUR_DESIGNED_VOICE_ID"}
      ]
    }
  }'

إدارة أصواتك

يمكنك في أي وقت إدراج الأصوات المخزّنة وفلترتها وفحصها وحذفها باستخدام Voices API (راجِع مكتبة الأصوات الموسّعة والفلترة للاطّلاع على جميع مَعلمات الفلترة).

  • حدود التخزين ومدة البقاء (TTL): يبلغ الحد الأقصى للأصوات التي تحتفظ بالحالة (store=True، والتي تتم مشاركتها بين الأصوات التي تم إنشاؤها بناءً على طلب والأصوات المنسوخة) 200 صوت لكل مشروع، وتبلغ مدة البقاء (TTL) سنة واحدة.
  • معلومات التوفّر الخاصة بـ sample_audio: يتم ملء sample_audio (mime_type: "audio/wav"، data بترميز base64) باستخدام voices.create() (CreateVoice) وvoices.get() (GetVoice) لأصوات "prompted". للحفاظ على حجم صغير للبيانات، لا تتضمّن voices.list() (ListVoices) sample_audio (ويتم إلغاء ضبط sample_audio لصوتَي "replicated" و"prebuilt").

Python

from google import genai

client = genai.Client()

# List stored prompted voices in your project filtered by language
response = client.voices.list(
    type_=["prompted"],
    language_code=["en-US", "en-GB"],
)
for voice in response.voices or []:
    print(voice.id, voice.display_name, voice.type)

# Retrieve a specific voice by ID
voice_details = client.voices.get(id=created_voice.id)

# Delete a stored custom voice
client.voices.delete(id=created_voice.id)

JavaScript

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

const ai = new GoogleGenAI();

// List stored prompted voices in your project filtered by language
const response = await ai.voices.list({
  type: ["prompted"],
  language_code: ["en-US", "en-GB"],
});
for (const voice of response.voices ?? []) {
  console.log(voice.id, voice.display_name, voice.type);
}

// Retrieve a specific voice by ID
const voiceDetails = await ai.voices.get(createdVoice.id);

// Delete a stored custom voice
await ai.voices.delete(createdVoice.id);

REST

# List stored prompted voices filtered by language
curl -G "https://generativelanguage.googleapis.com/v1beta/voices" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  --data-urlencode "type=prompted" \
  --data-urlencode "language_code=en-US" \
  --data-urlencode "language_code=en-GB"

# Retrieve a specific voice by ID
curl "https://generativelanguage.googleapis.com/v1beta/voices/voice_YOUR_DESIGNED_VOICE_ID" \
  -H "x-goog-api-key: $GEMINI_API_KEY"

# Delete a stored custom voice
curl -X DELETE "https://generativelanguage.googleapis.com/v1beta/voices/voice_YOUR_DESIGNED_VOICE_ID" \
  -H "x-goog-api-key: $GEMINI_API_KEY"

أفضل الممارسات لتوجيه الطلبات من أجل تصميم Voice

  • وضع السمات الصوتية الدائمة في تصميم Voice، وليس في style: حدِّد الخصائص الثابتة، مثل العمر والجنس والنبرة والملمس الصوتي واللكنة الإقليمية، عند إنشاء الصوت في voices.create.
  • استخدِم speech_metadata.style للتعبير عن المشاعر في مواقف معيّنة: بعد إنشاء صوتك المخصّص، استخدِم طلبات قصيرة style (مثل "whispered urgently" أو "cheerful and energetic") لتوجيه الأداء التمثيلي خطوة بخطوة بدون تغيير هوية المتحدث الأساسية.
  • استخدِم وصفًا دقيقًا وموجزًا: يؤدي الوصف الواضح المكوّن من جملة أو جملتين (مثل "مذيعة رياضية نشيطة في الثلاثينيات من عمرها تتحدث بلكنة أمريكية وسط غربية خفيفة") إلى نتائج أفضل وأكثر اتساقًا من الفقرات المتناقضة أو الطويلة جدًا.

الخطوات التالية