การควบคุม SSML และท่วงทำนองเสียง
ภาษามาร์กอัปสังเคราะห์เสียงพูด: การเว้นจังหวะ การเน้น อัตราการพูด และระดับเสียง
การควบคุม SSML และท่วงทำนองเสียง เป็นบทเรียน AI Prompt Engineering ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน AI Prompt Engineering และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส AI Prompt Engineering มีบทเรียนทั้งหมด 4 บทเรียน
SSML คืออะไร
SSML (ภาษามาร์กอัปสังเคราะห์เสียง) คือภาษาที่อิง XML ซึ่งช่วยให้คุณควบคุมวิธีที่ระบบแปลงข้อความเป็นเสียงอ่านข้อความได้อย่างละเอียด SSML รองรับโดย 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: อัตรา
<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: ระดับเสียง
<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 มาตรฐาน รวมถึงส่วนขยายเฉพาะของ Polly การใช้งาน API แตกต่างจาก 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 ด้วย LLM
คุณสามารถใช้ 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 ที่มีรูปแบบไม่ถูกต้องทำให้เกิดข้อผิดพลาดของ API ของ 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> (ระดับการเน้นเสียง: เข้ม/ปานกลาง/ลดลง), <prosody rate> (ความเร็วในการพูด), <prosody pitch> (ความถี่ของเสียง), <say-as> (การตีความเชิงความหมายของตัวเลข วันที่ และหมายเลขโทรศัพท์) และ <phoneme> (การออกเสียงด้วย IPA อย่างชัดเจน) ทั้ง Google Cloud TTS และ Amazon Polly รองรับ SSML ด้วยชุดแท็กหลักเดียวกัน ใช้ LLM เพื่อสร้าง SSML โดยอัตโนมัติจากข้อความธรรมดา และตรวจสอบว่า XML มีรูปแบบถูกต้องก่อนส่งไปใช้งานจริงเสมอ
เรียนรู้ AI Prompt Engineering ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 53
- บทเรียน
- 199
คำถามที่พบบ่อย
บทเรียน “การควบคุม SSML และท่วงทำนองเสียง” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การควบคุม SSML และท่วงทำนองเสียง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส AI Prompt Engineering ให้อัปเกรดเป็น CoddyKit PRO คอร์ส AI Prompt Engineering มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การควบคุม SSML และท่วงทำนองเสียง”
ภาษามาร์กอัปสังเคราะห์เสียงพูด: การเว้นจังหวะ การเน้น อัตราการพูด และระดับเสียง คุณปฏิบัติ AI Prompt Engineering ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน AI Prompt Engineering หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน AI Prompt Engineering บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การควบคุม SSML และท่วงทำนองเสียง” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน AI Prompt Engineering นี้ได้ไหม
ได้ บทเรียน AI Prompt Engineering ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- รูปแบบพรอมต์ TTS สำหรับเสียงพูดที่เป็นธรรมชาติ
- การควบคุม SSML และท่วงทำนองเสียง
- การออกแบบบุคลิกของปัญญาประดิษฐ์เสียงพูด
- ตัวแทนเสียงและข้อความแบบหลายรูปแบบ