Creates a new AudioDurationElement instance.
The target duration for the audio output. Must be specified in seconds (e.g., '10s') or milliseconds (e.g., '5000ms'). The speech synthesizer will adjust the speaking rate to fit this duration. Valid range typically 0.5x to 2x the natural duration.
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 audio duration element as an SSML XML string.
Generates the Azure-specific <mstts:audioduration>
element with the specified
duration value. This element is self-closing and does not contain any child elements.
The XML string representation of the audio duration element in the format:
<mstts:audioduration value="duration"/>
const element = new AudioDurationElement('15s');
console.log(element.render());
// Output: <mstts:audioduration value="15s"/>
// Different duration formats
const ms = new AudioDurationElement('10000ms');
console.log(ms.render());
// Output: <mstts:audioduration value="10000ms"/>
const seconds = new AudioDurationElement('7.5s');
console.log(seconds.render());
// Output: <mstts:audioduration value="7.5s"/>
SSML element for controlling the total audio duration (Azure-specific).
The
<mstts:audioduration>
element adjusts the speech rate to make the audio output fit within a specified duration. This is useful for synchronizing speech with video, animations, or when you need precise timing control. The speech synthesizer will automatically speed up or slow down the speech to match the target duration.This is an Azure Speech Service specific extension and requires the mstts namespace. If the specified duration is shorter than the natural speech duration, the speech will be sped up. If longer, the speech will be slowed down.
Example
Remarks
See