0Pricing
AI Prompt Engineering · درس

التحكم في SSML والتنغيم

لغة ترميز تركيب الكلام: الوقفات، والتوكيد، والسرعة، ودرجة الصوت

التحكم في SSML والتنغيم درس مجاني في AI Prompt Engineering على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في AI Prompt Engineering، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة AI Prompt Engineering 4 دروس في المجموع.

ما هي SSML؟

SSML (لغة ترميز تركيب الكلام) هي لغة تستند إلى XML وتمنحك تحكمًا دقيقًا في طريقة قراءة محركات تحويل النص إلى كلام للنص. وتدعمها Google Cloud TTS وAmazon Polly وMicrosoft Azure TTS وغيرها الكثير.

بينما يوفر النص العادي الكلمات فقط، تتيح لك SSML التحكم في الوقفات، والتأكيد، والسرعة، وطبقة الصوت، والنطق، وغير ذلك.

البنية الأساسية لـ SSML

تُغلّف جميع مستندات SSML بوسم <speak>. وداخل هذا الوسم، تمزج بين النص العادي وعناصر ترميز SSML. وتعالج محركات TTS ترميز SSML وتنتج الصوت وفقًا له.

# SSML document structure
SSML_BASIC = """
<speak>
  Welcome to the course.
  <break time="500ms"/>
  Today we will cover three topics.
  First, we will discuss SSML basics.
  <break time="300ms"/>
  Second, we will explore prosody control.
  <break time="300ms"/>
  And third, we will look at advanced features.
</speak>
"""

# Send to Google Cloud TTS
from google.cloud import texttospeech

client_tts = texttospeech.TextToSpeechClient()

input_text = texttospeech.SynthesisInput(ssml=SSML_BASIC)
voice = texttospeech.VoiceSelectionParams(
    language_code='en-US',
    ssml_gender=texttospeech.SsmlVoiceGender.NEUTRAL
)
audio_config = texttospeech.AudioConfig(
    audio_encoding=texttospeech.AudioEncoding.MP3
)
print('SSML document ready to synthesize')

عنصر break

يُدرج <break> وقفة في الكلام. استخدمه لإنشاء إيقاع طبيعي، أو فصل عناصر القائمة، أو إضافة تأثير درامي. وتقبل السمة time المللي ثانية (ms) أو الثواني (s).

# break element examples
BREAK_EXAMPLES = """
<speak>
  Are you ready?
  <break time="1s"/>
  Let us begin.

  The three rules are:
  number one,
  <break time="300ms"/>
  always test your code.
  <break time="200ms"/>
  Number two,
  <break time="300ms"/>
  write clear documentation.
  <break time="200ms"/>
  And number three,
  <break time="300ms"/>
  review before you ship.

  <break strength="x-strong"/>
  That is all for today.
</speak>
"""

# break strength values: none, x-weak, weak, medium, strong, x-strong
# These map to approximate pause durations defined by the engine
print('break time: explicit duration (e.g. 500ms)')
print('break strength: semantic pause level (weak/medium/strong)')

عنصر emphasis

يضيف <emphasis> تشديدًا إلى الكلمات، فيجعل المحرك ينطقها بصوت أعلى أو ببطء أكبر أو بطبقة صوت أعلى. استخدمه باعتدال، لأن الإفراط في التشديد يبدو غير طبيعي.

EMPHASIS_EXAMPLES = """
<speak>
  This update is
  <emphasis level="strong">critically important</emphasis>.
  Please read it carefully.

  The deadline is
  <emphasis level="moderate">this Friday</emphasis>,
  not next week.

  We
  <emphasis level="reduced">recommend</emphasis>
  enabling this feature, but it is optional.
</speak>
"""

# emphasis level values:
# strong  — much more stress (louder, slower, higher pitch)
# moderate — some additional stress (default if level omitted)
# reduced — less stress (quieter, faster)
print('Use strong for critical information')
print('Use reduced for parenthetical or secondary information')

عنصر prosody: Rate

يتحكم <prosody rate> في سرعة الكلام. أبطئ السرعة عند تقديم معلومات مهمة، وزدها عند تقديم تفاصيل ثانوية أو إخلاءات مسؤولية.

PROSODY_RATE_EXAMPLES = """
<speak>
  <prosody rate="slow">
    This is the most important thing to remember.
    Take a moment to let it sink in.
  </prosody>

  <prosody rate="medium">
    Now, for some context about how we got here.
  </prosody>

  <prosody rate="fast">
    And now a quick summary of less critical details that you
    can refer back to in the documentation.
  </prosody>
</speak>
"""

# rate values:
# x-slow, slow, medium (default), fast, x-fast
# Or percentage: rate="75%" (75% of normal speed)
# Or absolute: rate="200 words per minute"
print('Slow: for emphasis, complex information, or pauses for thought')
print('Fast: for disclaimers, secondary info, rapid listing')

عنصر prosody: Pitch

يضبط <prosody pitch> التردد الأساسي للصوت. استخدمه للإشارة إلى تغيرات النبرة، مثل الأسئلة أو الحماس أو المحتوى الجاد.

PROSODY_PITCH_EXAMPLES = """
<speak>
  <prosody pitch="high" rate="medium">
    Exciting news! We just launched a brand new feature!
  </prosody>

  <prosody pitch="low" rate="slow">
    Unfortunately, this service will be discontinued.
    We apologize for any inconvenience.
  </prosody>

  <prosody pitch="+20%">
    Did you know that our users save three hours per week on average?
  </prosody>

  <prosody pitch="-15%">
    Please review the terms and conditions carefully.
  </prosody>
</speak>
"""

# pitch values:
# x-low, low, medium, high, x-high
# Or relative: +20%, -15%
# Or semitones: +2st, -4st
print('High pitch: excitement, questions, announcements')
print('Low pitch: serious, cautionary, or somber content')

عنصر say-as

يخبر <say-as> محرك TTS بكيفية تفسير النص، سواء أكان تاريخًا أم رقم هاتف أم عملة أم أحرفًا وغير ذلك. وهذا هو الحل الموثوق للتعامل مع الأرقام والقيم الخاصة.

SAY_AS_EXAMPLES = """
<speak>
  Your appointment is on
  <say-as interpret-as="date" format="mdy">01/15/2025</say-as>.

  Call us at
  <say-as interpret-as="telephone">1-800-555-1234</say-as>.

  Your confirmation code is
  <say-as interpret-as="characters">XK7T9</say-as>.

  The total is
  <say-as interpret-as="currency" language="en-US">$47.50</say-as>.

  This is version
  <say-as interpret-as="characters">2.3.1</say-as>
  of the software.
</speak>
"""

# interpret-as values:
# characters  — spell out each character
# cardinal    — number as cardinal ("forty-seven")
# ordinal     — "forty-seventh"
# fraction    — "three halves"
# date        — format string controls order
# telephone   — phone number formatting
# currency    — monetary value with currency name
print('say-as is the most reliable way to control number pronunciation')

عنصر phoneme

يوفر <phoneme> نطقًا صوتيًا صريحًا للكلمات التي يخطئ محرك TTS في نطقها باستمرار، مثل أسماء العلامات التجارية أو المصطلحات التقنية أو الكلمات الأجنبية.

PHONEME_EXAMPLES = """
<speak>
  Welcome to
  <phoneme alphabet="ipa" ph="ent.ro.pi">Entropiq</phoneme>,
  the leading analytics platform.

  Our CEO,
  <phoneme alphabet="ipa" ph="joo.serf">Josef</phoneme>,
  will present the results.

  This API uses
  <phoneme alphabet="ipa" ph="kwer.i">GraphQL</phoneme>
  for data fetching.
</speak>
"""

# IPA (International Phonetic Alphabet) is the most precise
# x-sampa is an alternative ASCII-friendly phonetic alphabet
# Use an IPA converter tool to find the right phonemes:
# - https://tophonetics.com
# - Dictionary.com pronunciation guides use IPA
print('Use phoneme for brand names, technical terms, proper nouns')
print('Test with multiple phoneme values until it sounds right')

SSML مع Amazon Polly

يدعم Amazon Polly معيار SSML، إضافة إلى الامتدادات الخاصة به. ويختلف استخدام واجهة برمجة التطبيقات قليلًا عن Google Cloud TTS، لكن ترميز SSML نفسه واحد.

import boto3

polly = boto3.client('polly', region_name='us-east-1')

SSML_CONTENT = """
<speak>
  <prosody rate="slow" pitch="low">
    Welcome to our quarterly earnings call.
  </prosody>
  <break time="1s"/>
  We are pleased to report
  <emphasis level="strong">record revenue</emphasis>
  of
  <say-as interpret-as="currency" language="en-US">$4200000</say-as>
  this quarter.
</speak>
"""

response = polly.synthesize_speech(
    Text=SSML_CONTENT,
    TextType='ssml',     # Tell Polly this is SSML
    OutputFormat='mp3',
    VoiceId='Joanna',    # US English female voice
)

if 'AudioStream' in response:
    with open('output.mp3', 'wb') as f:
        f.write(response['AudioStream'].read())
    print('Audio saved to output.mp3')

إنشاء SSML باستخدام LLMs

يمكنك استخدام LLM لتحويل النص العادي إلى نصوص كلام مشروحة بعلامات SSML. ويتيح لك ذلك كتابة المحتوى بصورة طبيعية ثم معالجته لاحقًا للحصول على أفضل إخراج من TTS.

import anthropic

client = anthropic.Anthropic(api_key='sk-ant-...')

SSML_GENERATION_PROMPT = (
    'Convert the following text into an SSML document for Google Cloud TTS.\n\n'
    'Rules:\n'
    '- Wrap the entire output in <speak> tags\n'
    '- Add <break time="500ms"/> between major points\n'
    '- Add <emphasis level="strong"> around key terms or critical information\n'
    '- Use <prosody rate="slow"> for important warnings or summaries\n'
    '- Use <say-as interpret-as="date"> for all dates\n'
    '- Use <say-as interpret-as="telephone"> for phone numbers\n'
    '- Return only the SSML, no explanation\n\n'
    'Text: {text}'
)

def text_to_ssml(text):
    prompt = SSML_GENERATION_PROMPT.format(text=text)
    r = client.messages.create(
        model='claude-opus-4-5',
        max_tokens=2000,
        messages=[{'role': 'user', 'content': prompt}]
    )
    return r.content[0].text

التحقق من SSML واختباره

تؤدي SSML غير الصحيحة إلى أخطاء في واجهة TTS البرمجية، أو تجعل النظام يعود إلى نطق وسوم XML الخام بصوت عالٍ. احرص دائمًا على التحقق من صحة SSML قبل إرسالها إلى بيئة الإنتاج.

import xml.etree.ElementTree as ET

def validate_ssml(ssml_string):
    """
    Basic SSML validation: checks XML is well-formed
    and has a <speak> root element.
    """
    try:
        root = ET.fromstring(ssml_string)
        if root.tag != 'speak':
            return False, 'Root element must be <speak>'

        # Check for common misuse patterns
        warnings = []
        for elem in root.iter():
            if elem.tag == 'break' and 'time' not in elem.attrib and 'strength' not in elem.attrib:
                warnings.append('<break> has no time or strength attribute')

        return True, warnings if warnings else 'Valid'

    except ET.ParseError as e:
        return False, f'XML parse error: {e}'

# Test
valid_ssml = '<speak>Hello <break time="500ms"/> world.</speak>'
bad_ssml = '<speak>Hello <break> world.</speak>'  # break not self-closed

print(validate_ssml(valid_ssml))
print(validate_ssml(bad_ssml))

اختبار المعرفة: say-as في SSML

ما الغرض الأساسي من عنصر SSML <say-as>؟

مراجعة: التحكم في SSML وProsody

تمنح SSML تحكمًا دقيقًا في مخرجات TTS. وتشمل العناصر الأساسية: <break>، للوقفات المحددة بالوقت أو بدرجة القوة؛ و<emphasis>، لمستويات التشديد: strong/moderate/reduced؛ و<prosody rate>، لسرعة الكلام؛ و<prosody pitch>، لتردد الصوت؛ و<say-as>، للتفسير الدلالي للأرقام والتواريخ وأرقام الهواتف؛ و<phoneme>، للنطق الصريح باستخدام IPA. يدعم كل من Google Cloud TTS وAmazon Polly SSML مع مجموعة الوسوم الأساسية نفسها. استخدم LLMs لإنشاء SSML تلقائيًا من النص العادي، وتحقق دائمًا من سلامة بنية XML قبل الإرسال إلى بيئة الإنتاج.

الأسئلة الشائعة

هل درس «التحكم في SSML والتنغيم» مجاني؟

نعم — نص درس «التحكم في SSML والتنغيم» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة AI Prompt Engineering، انتقل إلى CoddyKit PRO. تتضمن دورة AI Prompt Engineering 4 دروس في المجموع.

ماذا ستتعلم في «التحكم في SSML والتنغيم»؟

لغة ترميز تركيب الكلام: الوقفات، والتوكيد، والسرعة، ودرجة الصوت تتمرن على AI Prompt Engineering مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ AI Prompt Engineering؟

لا تُشترط خبرة سابقة. AI Prompt Engineering على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.

كم من الوقت يستغرق درس «التحكم في SSML والتنغيم»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس AI Prompt Engineering هذا؟

نعم. كل درس في AI Prompt Engineering يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. أنماط مطالبات TTS للكلام الطبيعي
  2. التحكم في SSML والتنغيم
  3. تصميم شخصية الذكاء الاصطناعي الصوتية
  4. الوكلاء متعددو الوسائط للصوت والنص
← العودة إلى AI Prompt Engineering