The string value to validate
true
if the value is a valid volume format, false
otherwise
// Valid volume values
console.log(isValidVolume('50')); // true - percentage
console.log(isValidVolume('75%')); // true - percentage with %
console.log(isValidVolume('+10dB')); // true - positive decibels
console.log(isValidVolume('-5.5dB')); // true - negative decibels
console.log(isValidVolume('loud')); // true - named value
console.log(isValidVolume('x-soft')); // true - named value
// Invalid volume values
console.log(isValidVolume('150%')); // false - over 100%
console.log(isValidVolume('very-loud')); // false - invalid named value
console.log(isValidVolume('10db')); // false - lowercase 'db'
// Use in validation
function setVolume(volume: string) {
if (!isValidVolume(volume)) {
throw new Error(`Invalid volume format: ${volume}`);
}
// Valid volume format
}
https://docs.microsoft.com/azure/cognitive-services/speech-service/speech-synthesis-markup#adjust-prosody Prosody Volume Documentation
Validates if a string represents a valid volume value.
Checks if the provided string matches any of the accepted volume formats for SSML prosody and background audio elements.
Valid formats:
+10dB
,-5dB
,0dB
(can be positive or negative)50
,50%
,100
,100%
(0-100)silent
,x-soft
,soft
,medium
,loud
,x-loud
The regex patterns match: