Creates a new ExpressAsElement instance.
The text content to express with the specified style. This text will be spoken with the emotional or stylistic expression. Special XML characters (&, <, >, ", ') are automatically escaped to ensure valid XML output and prevent injection attacks.
Configuration object for the expression style
Configuration options for express-as elements (Azure-specific).
Controls emotional expression and speaking styles for neural voices that support these features. Allows for nuanced emotional delivery and role-playing scenarios.
Optional
role?: ExpressAsRoleAge and gender role for voice modification.
Simulates different speaker characteristics. Only supported by certain voices.
Emotional or speaking style to apply. (Required)
The available styles depend on the voice being used. Common categories include emotions (cheerful, sad, angry), professional styles (newscast, customerservice), and special effects (whispering, shouting).
Optional
styledegree?: stringIntensity of the style expression.
Controls how strongly the style is applied. Range: "0.01" (minimal) to "2" (double intensity)
// Simple emotional expression
const cheerful = new ExpressAsElement(
'What a beautiful day!',
{ style: 'cheerful' }
);
// With adjusted intensity
const veryExcited = new ExpressAsElement(
'This is incredible!',
{ style: 'excited', styledegree: '1.5' }
);
// Professional styles
const newsReader = new ExpressAsElement(
'Breaking news just in...',
{ style: 'newscast-formal' }
);
// Customer service tone
const support = new ExpressAsElement(
'How may I help you today?',
{ style: 'customerservice' }
);
// With role for character voices
const childNarrator = new ExpressAsElement(
'And they lived happily ever after!',
{ style: 'narration-relaxed', role: 'Girl' }
);
// Special effects
const whisper = new ExpressAsElement(
'This is a secret',
{ style: 'whispering' }
);
Protected
escapeProtected
Escapes special XML characters in text content to ensure valid XML output.
This method replaces XML special characters with their corresponding entity references to prevent XML parsing errors and potential security issues (XML injection). It should be used whenever inserting user-provided or dynamic text content into XML elements.
The following characters are escaped:
&
becomes &
(must be escaped first to avoid double-escaping)<
becomes <
(prevents opening of unintended tags)>
becomes >
(prevents closing of unintended tags)"
becomes "
(prevents breaking out of attribute values)'
becomes '
(prevents breaking out of attribute values)This method is marked as protected
so it's only accessible to classes that extend
SSMLElement, ensuring proper encapsulation while allowing all element implementations
to use this essential functionality.
The text content to escape
The text with all special XML characters properly escaped
// In a render method implementation
class TextElement extends SSMLElement {
private text: string = 'Hello & "world" <script>';
render(): string {
// Escapes to: Hello & "world" <script>
return `<text>${this.escapeXml(this.text)}</text>`;
}
}
// Edge cases handled correctly
this.escapeXml('5 < 10 & 10 > 5');
// Returns: '5 < 10 & 10 > 5'
this.escapeXml('She said "Hello"');
// Returns: 'She said "Hello"'
this.escapeXml("It's a test");
// Returns: 'It's a test'
// Prevents XML injection
this.escapeXml('</voice><voice name="evil">');
// Returns: '</voice><voice name="evil">'
Renders the express-as element as an SSML XML string.
Generates the Azure-specific <mstts:express-as>
element with the required style
attribute and optional styledegree and role attributes. Text content is automatically
escaped to prevent XML injection and ensure valid output. Only includes optional
attributes if their values are defined, keeping the XML clean.
The XML string representation of the express-as element in the format:
<mstts:express-as style="value" [styledegree="value"] [role="value"]>text</mstts:express-as>
// Basic style only
const expr1 = new ExpressAsElement('Hello!', { style: 'cheerful' });
console.log(expr1.render());
// Output: <mstts:express-as style="cheerful">Hello!</mstts:express-as>
// With style degree
const expr2 = new ExpressAsElement('Wow!', {
style: 'excited',
styledegree: '2'
});
console.log(expr2.render());
// Output: <mstts:express-as style="excited" styledegree="2">Wow!</mstts:express-as>
// With role
const expr3 = new ExpressAsElement('Story time', {
style: 'narration-relaxed',
role: 'OlderAdultMale'
});
console.log(expr3.render());
// Output: <mstts:express-as style="narration-relaxed" role="OlderAdultMale">Story time</mstts:express-as>
// All attributes with escaped text
const expr4 = new ExpressAsElement('Terms & "Conditions"', {
style: 'serious',
styledegree: '1.2',
role: 'YoungAdultFemale'
});
console.log(expr4.render());
// Output: <mstts:express-as style="serious" styledegree="1.2" role="YoungAdultFemale">Terms & "Conditions"</mstts:express-as>
SSML element for applying emotional expression and speaking styles (Azure-specific).
The
<mstts:express-as>
element enables neural voices to express emotions and speaking styles beyond the default neutral voice. This Azure Speech Service specific feature allows for more natural and engaging speech synthesis by simulating various emotional states, professional styles, and character roles. The element significantly enhances the expressiveness of synthesized speech for scenarios like audiobooks, virtual assistants, and interactive applications.Not all neural voices support all styles, and the quality of expression varies by voice and language. The express-as element can only be used with neural voices that support style features.
Example
Remarks
See