Creates a new BackgroundAudioElement instance.
Configuration object for the background audio element
Configuration options for background audio.
Defines audio that plays throughout the entire SSML document, behind the spoken content. Useful for adding music or ambient sounds.
Optional
fadein?: stringDuration of the fade-in effect at the start.
Gradually increases volume from 0 to the specified volume level. Specified in milliseconds (ms) or seconds (s).
Optional
fadeout?: stringDuration of the fade-out effect at the end.
Gradually decreases volume from the specified level to 0. Specified in milliseconds (ms) or seconds (s).
URL of the background audio file. (Required)
Requirements:
Optional
volume?: stringVolume level of the background audio.
Can be specified as:
// Minimal configuration
const simple = new BackgroundAudioElement({
src: 'https://cdn.example.com/ambient.mp3'
});
// With volume control
const withVolume = new BackgroundAudioElement({
src: 'https://cdn.example.com/music.mp3',
volume: '0.3' // 30% volume
});
// Full configuration with fades
const full = new BackgroundAudioElement({
src: 'https://cdn.example.com/soundtrack.mp3',
volume: '0.5', // 50% volume
fadein: '3000ms', // 3-second fade in
fadeout: '2000ms' // 2-second fade out
});
// For podcast/narration
const podcast = new BackgroundAudioElement({
src: 'https://example.com/podcast-intro.mp3',
volume: '-10dB', // Reduced by 10 decibels
fadein: '5s', // Slow 5-second fade in
fadeout: '3s' // 3-second fade out
});
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 background audio element as an SSML XML string.
Generates the Azure-specific <mstts:backgroundaudio>
element with the specified
audio source and optional volume/fade settings. This element is self-closing and
does not contain any child elements. The attributes are only included if their
corresponding option values are provided.
The XML string representation of the background audio element in the format:
<mstts:backgroundaudio src="url" [volume="value"] [fadein="duration"] [fadeout="duration"]/>
// Minimal output
const basic = new BackgroundAudioElement({
src: 'https://example.com/music.mp3'
});
console.log(basic.render());
// Output: <mstts:backgroundaudio src="https://example.com/music.mp3"/>
// With volume
const withVol = new BackgroundAudioElement({
src: 'https://example.com/music.mp3',
volume: '0.5'
});
console.log(withVol.render());
// Output: <mstts:backgroundaudio src="https://example.com/music.mp3" volume="0.5"/>
// Full configuration
const full = new BackgroundAudioElement({
src: 'https://example.com/music.mp3',
volume: '0.3',
fadein: '2000ms',
fadeout: '1500ms'
});
console.log(full.render());
// Output: <mstts:backgroundaudio src="https://example.com/music.mp3" volume="0.3" fadein="2000ms" fadeout="1500ms"/>
SSML element for adding background audio to the entire speech output (Azure-specific).
The
<mstts:backgroundaudio>
element plays audio continuously throughout the entire SSML document, behind all spoken content. This is useful for adding ambient music, sound effects, or any audio atmosphere that enhances the listening experience. The background audio automatically loops if it's shorter than the speech duration and is trimmed if longer.This is an Azure Speech Service specific extension and requires the mstts namespace. Only one background audio element can be used per SSML document, and it must be placed as a direct child of the
<speak>
element, before any<voice>
elements.Example
Remarks
See
https://docs.microsoft.com/azure/cognitive-services/speech-service/speech-synthesis-markup#add-background-audio Background Audio Documentation