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

    Class PhonemeElement

    SSML element for specifying exact phonetic pronunciation of text.

    The <phoneme> element provides precise control over pronunciation by specifying the phonetic transcription of words using standard phonetic alphabets. This is essential for proper names, technical terms, foreign words, or any text that the speech synthesizer might pronounce incorrectly by default. The element ensures consistent and accurate pronunciation across different voices and languages.

    The phoneme element supports multiple phonetic alphabets including IPA (International Phonetic Alphabet), SAPI (Microsoft Speech API), and UPS (Universal Phone Set), allowing developers to choose the most appropriate system for their needs.

    // IPA pronunciation
    const phoneme = new PhonemeElement('schedule', {
    alphabet: 'ipa',
    ph: 'ˈʃɛdjuːl' // British pronunciation
    });
    phoneme.render();
    // Output: <phoneme alphabet="ipa" ph="ˈʃɛdjuːl">schedule</phoneme>

    // SAPI pronunciation
    const sapi = new PhonemeElement('Azure', {
    alphabet: 'sapi',
    ph: 'ae zh er'
    });

    // Use with SSMLBuilder
    const ssml = new SSMLBuilder({ lang: 'en-US' })
    .voice('en-US-AvaNeural')
    .text('The word ')
    .phoneme('tomato', { alphabet: 'ipa', ph: 'təˈmeɪtoʊ' })
    .text(' has different pronunciations.')
    .build();
    • The phoneme element can only contain text and no other elements
    • The text content is what appears visually, while the ph attribute determines pronunciation
    • Different voices may support different phonetic alphabets
    • IPA is the most universally supported alphabet
    • SAPI is optimized for Microsoft voices
    • Special XML characters in text are automatically escaped
    • Incorrect phonetic transcriptions may result in unexpected pronunciation

    Hierarchy (View Summary)

    Index

    Constructors

    Methods

    Constructors

    • Creates a new PhonemeElement instance.

      Parameters

      • text: string

        The text content to be pronounced. This is the written form that appears in the document. Special XML characters (&, <, >, ", ') are automatically escaped. The actual pronunciation is determined by the ph attribute.

      • options: PhonemeOptions

        Configuration for phonetic pronunciation

        Configuration options for phoneme elements.

        Provides exact phonetic pronunciation using standard phonetic alphabets. Essential for proper names, technical terms, or words with ambiguous pronunciation.

        • alphabet: PhonemeAlphabet

          Phonetic alphabet used for transcription. (Required)

          Available alphabets:

          • ipa: International Phonetic Alphabet (universal standard)
          • sapi: Microsoft SAPI phonemes (English-focused)
          • ups: Universal Phone Set (Microsoft's unified system)
          "ipa"
          
          "sapi"
          
        • ph: string

          Phonetic transcription of the word. (Required)

          The exact phonetic representation in the specified alphabet. Must be valid according to the chosen alphabet's rules.

          "ˈʃɛdjuːl" - IPA for "schedule" (British)
          
          "s k eh jh uw l" - SAPI for "schedule" (American)
          

      Returns PhonemeElement

      // IPA for international use
      const ipaExample = new PhonemeElement('schedule', {
      alphabet: 'ipa',
      ph: 'ˈskɛdʒuːl' // American pronunciation
      });

      // SAPI for Microsoft voices
      const sapiExample = new PhonemeElement('SQL', {
      alphabet: 'sapi',
      ph: 'eh s k y uw eh l'
      });

      // Technical terms
      const techTerm = new PhonemeElement('Kubernetes', {
      alphabet: 'ipa',
      ph: 'kuːbərˈnetiːz'
      });

      // Disambiguating homographs
      const present = new PhonemeElement('read', {
      alphabet: 'ipa',
      ph: 'riːd' // Present tense, not past tense 'rɛd'
      });

      // Foreign names
      const name = new PhonemeElement('Nguyen', {
      alphabet: 'ipa',
      ph: 'ŋwiən'
      });

      // Company/product names
      const product = new PhonemeElement('iPhone', {
      alphabet: 'sapi',
      ph: 'ay f ow n'
      });

    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 phoneme element as an SSML XML string.

      Generates the <phoneme> element with the alphabet and ph attributes specifying the phonetic transcription. The text content is automatically escaped to prevent XML injection and ensure valid output. The rendered element instructs the speech synthesizer to use the specified phonetic pronunciation instead of its default text-to-phoneme conversion.

      Returns string

      The XML string representation of the phoneme element in the format: <phoneme alphabet="alphabet" ph="transcription">text</phoneme>

      // IPA example
      const ipa = new PhonemeElement('schedule', {
      alphabet: 'ipa',
      ph: 'ˈʃɛdjuːl'
      });
      console.log(ipa.render());
      // Output: <phoneme alphabet="ipa" ph="ˈʃɛdjuːl">schedule</phoneme>

      // SAPI example
      const sapi = new PhonemeElement('Azure', {
      alphabet: 'sapi',
      ph: 'ae zh er'
      });
      console.log(sapi.render());
      // Output: <phoneme alphabet="sapi" ph="ae zh er">Azure</phoneme>

      // With special characters (automatically escaped)
      const special = new PhonemeElement('AT&T', {
      alphabet: 'sapi',
      ph: 'ey t ih n t ih'
      });
      console.log(special.render());
      // Output: <phoneme alphabet="sapi" ph="ey t ih n t ih">AT&amp;T</phoneme>

      // UPS example
      const ups = new PhonemeElement('hello', {
      alphabet: 'ups',
      ph: 'H EH L OW'
      });
      console.log(ups.render());
      // Output: <phoneme alphabet="ups" ph="H EH L OW">hello</phoneme>