SSML Builder Documentation - v1.0.1
    Preparing search index...

    Class AudioElement

    SSML element for embedding external audio files in speech synthesis.

    The <audio> element allows you to insert pre-recorded audio files into the synthesized speech output. This is useful for adding sound effects, music, pre-recorded messages, or any other audio content that should be played as part of the speech. If the audio file is unavailable or cannot be played, the optional fallback text will be spoken instead.

    The audio element supports common audio formats like MP3 and WAV. The audio file must be publicly accessible via HTTPS for cloud-based speech services to retrieve and play it.

    // Basic audio element with fallback text
    const audio = new AudioElement(
    'https://example.com/sound.mp3',
    'Sound effect here'
    );
    audio.render();
    // Output: <audio src="https://example.com/sound.mp3">Sound effect here</audio>

    // Audio without fallback
    const music = new AudioElement('https://example.com/music.mp3');
    music.render();
    // Output: <audio src="https://example.com/sound.mp3"></audio>

    // Use with SSMLBuilder
    const ssml = new SSMLBuilder({ lang: 'en-US' })
    .voice('en-US-AvaNeural')
    .text('And now, a sound effect: ')
    .audio('https://example.com/bell.mp3', 'Ding!')
    .build();
    • The audio file must be accessible via HTTPS (not HTTP) for security reasons
    • Supported formats typically include MP3, WAV, and other common audio formats
    • The audio plays in its entirety unless interrupted
    • If the audio file cannot be loaded, the fallback text is spoken using the current voice
    • The audio element can be nested within voice, paragraph, and sentence elements
    • Consider file size and network latency when using external audio files

    Hierarchy (View Summary)

    Index

    Constructors

    Methods

    Constructors

    • Creates a new AudioElement instance.

      Parameters

      • src: string

        The URL of the audio file to play. Must be a publicly accessible HTTPS URL. Supported formats include MP3, WAV, and other common audio formats. The URL should be properly encoded if it contains special characters.

      • OptionalfallbackText: string

        Optional text to speak if the audio file is unavailable or cannot be played. This text will be spoken using the current voice settings. Special XML characters in the text will be automatically escaped. If not provided and the audio fails, there will be silence.

      Returns AudioElement

      // With fallback text for accessibility
      const soundEffect = new AudioElement(
      'https://cdn.example.com/sounds/notification.mp3',
      'Notification sound'
      );

      // Without fallback (silent if audio fails)
      const backgroundMusic = new AudioElement(
      'https://cdn.example.com/music/intro.mp3'
      );

      // With special characters in fallback text (automatically escaped)
      const alert = new AudioElement(
      'https://example.com/alert.mp3',
      'Alert: "System" & security check'
      );

    Methods

    • Protected

      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 &amp; (must be escaped first to avoid double-escaping)
      • < becomes &lt; (prevents opening of unintended tags)
      • > becomes &gt; (prevents closing of unintended tags)
      • " becomes &quot; (prevents breaking out of attribute values)
      • ' becomes &apos; (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.

      Parameters

      • text: string

        The text content to escape

      Returns string

      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 &amp; &quot;world&quot; &lt;script&gt;
      return `<text>${this.escapeXml(this.text)}</text>`;
      }
      }

      // Edge cases handled correctly
      this.escapeXml('5 < 10 & 10 > 5');
      // Returns: '5 &lt; 10 &amp; 10 &gt; 5'

      this.escapeXml('She said "Hello"');
      // Returns: 'She said &quot;Hello&quot;'

      this.escapeXml("It's a test");
      // Returns: 'It&apos;s a test'

      // Prevents XML injection
      this.escapeXml('</voice><voice name="evil">');
      // Returns: '&lt;/voice&gt;&lt;voice name=&quot;evil&quot;&gt;'
    • Renders the audio element as an SSML XML string.

      Generates the <audio> element with the source URL and optional fallback text. If fallback text is provided, it is automatically escaped to prevent XML injection and ensure valid XML output. The fallback text is placed inside the audio element and will only be spoken if the audio file cannot be loaded or played.

      Returns string

      The XML string representation of the audio element in the format: <audio src="url">fallback text</audio> or <audio src="url"></audio> if no fallback text is provided

      // With fallback text
      const audio1 = new AudioElement(
      'https://example.com/sound.mp3',
      'Ding sound'
      );
      console.log(audio1.render());
      // Output: <audio src="https://example.com/sound.mp3">Ding sound</audio>

      // Without fallback text
      const audio2 = new AudioElement('https://example.com/music.mp3');
      console.log(audio2.render());
      // Output: <audio src="https://example.com/music.mp3"></audio>

      // With special characters in fallback (automatically escaped)
      const audio3 = new AudioElement(
      'https://example.com/alert.mp3',
      'Alert & "Warning"'
      );
      console.log(audio3.render());
      // Output: <audio src="https://example.com/alert.mp3">Alert &amp; &quot;Warning&quot;</audio>