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

    Class ExpressAsElement

    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.

    // Basic emotional expression
    const happy = new ExpressAsElement('I am so happy!', { style: 'cheerful' });
    happy.render();
    // Output: <mstts:express-as style="cheerful">I am so happy!</mstts:express-as>

    // With intensity control
    const excited = new ExpressAsElement('Amazing news!', {
    style: 'excited',
    styledegree: '2'
    });

    // With role-playing
    const childVoice = new ExpressAsElement('Once upon a time...', {
    style: 'narration-relaxed',
    role: 'Girl'
    });

    // Use with SSMLBuilder
    const ssml = new SSMLBuilder({ lang: 'en-US' })
    .voice('en-US-AvaNeural')
    .expressAs('Welcome to customer service!', {
    style: 'customerservice'
    })
    .build();
    • This is an Azure Speech Service specific extension requiring the mstts namespace
    • Not all neural voices support all styles - check voice documentation for compatibility
    • The express-as element can contain text and other inline elements
    • Style expression affects the entire enclosed content
    • Special XML characters in text are automatically escaped
    • Can be nested within voice, paragraph, and sentence elements
    • Cannot be nested within another express-as element

    Hierarchy (View Summary)

    Index

    Constructors

    Methods

    Constructors

    • Creates a new ExpressAsElement instance.

      Parameters

      • text: string

        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.

      • options: ExpressAsOptions

        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.

        • Optionalrole?: ExpressAsRole

          Age and gender role for voice modification.

          Simulates different speaker characteristics. Only supported by certain voices.

          "Girl"
          
          "OlderAdultMale"
          
          "YoungAdultFemale"
          

          ExpressAsRole type for full list

        • style: ExpressAsStyle

          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).

          "cheerful"
          
          "newscast-formal"
          
          "whispering"
          

          ExpressAsStyle type for full list

        • Optionalstyledegree?: string

          Intensity of the style expression.

          Controls how strongly the style is applied. Range: "0.01" (minimal) to "2" (double intensity)

          "1"
          
          "0.5" - Half intensity
          
          "1.5" - 50% more intense
          
          "2" - Maximum intensity
          

      Returns ExpressAsElement

      // 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' }
      );

    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 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.

      Returns string

      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 &amp; &quot;Conditions&quot;</mstts:express-as>