The string value to validate
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
}
https://docs.microsoft.com/azure/cognitive-services/speech-service/speech-synthesis-markup#adjust-prosody Prosody Pitch Documentation
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:
200Hz
,150Hz
(absolute frequency in Hertz)+10%
,-5%
(relative change from baseline)x-low
,low
,medium
,high
,x-high
The regex pattern matches:
Note: Semitone changes (e.g.,
+2st
) are also valid in SSML but not validated by this function. Consider extending if needed.