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

    Function isValidPitch

    • Validates if a string represents a valid pitch value.

      Checks if the provided string matches any of the accepted pitch formats for SSML prosody elements that control voice pitch.

      Valid formats:

      • Frequency: 200Hz, 150Hz (absolute frequency in Hertz)
      • Percentage change: +10%, -5% (relative change from baseline)
      • Named values: x-low, low, medium, high, x-high

      The regex pattern matches:

      • Optional +/- sign
      • Digits with optional decimal
      • Required unit suffix: either 'Hz' or '%'

      Note: Semitone changes (e.g., +2st) are also valid in SSML but not validated by this function. Consider extending if needed.

      Parameters

      • value: string

        The string value to validate

      Returns boolean

      true if the value is a valid pitch format, false otherwise

      // Valid pitch values
      console.log(isValidPitch('high')); // true - named value
      console.log(isValidPitch('x-low')); // true - named value
      console.log(isValidPitch('200Hz')); // true - absolute frequency
      console.log(isValidPitch('+10%')); // true - percentage increase
      console.log(isValidPitch('-5.5%')); // true - percentage decrease
      console.log(isValidPitch('150.5Hz')); // true - decimal frequency

      // Invalid pitch values
      console.log(isValidPitch('very-high')); // false - invalid named value
      console.log(isValidPitch('200')); // false - missing unit
      console.log(isValidPitch('200hz')); // false - lowercase 'hz'
      console.log(isValidPitch('+2st')); // false - semitones not handled

      // Use in validation
      function setPitch(pitch: string) {
      if (!isValidPitch(pitch)) {
      throw new Error(`Invalid pitch format: ${pitch}`);
      }
      // Valid pitch format
      }