This package creates Speech Synthesis Markup Language (SSML) using the builder pattern.
Tired of creating SSML using string concatenation or worring about special characters like '&' ? This project aims to eliminate all these headaches by providing a clean and easy to use API. In addition to making SSML easier to create, this library is fully unit-tested to ensure things work as expected.
Whether you're building an Amazon Alexa Skill using the older version of the JavaScript SDK or the new one, this library is compatible. See the examples in the lower portion of this documentation.
npm install ssml-builder --save
var Speech = require('ssml-builder');
var speech = new Speech();
speech.say('Hello')
.pause('1s')
.say('fellow Alexa developers')
.pause('500ms')
.say('Testing phone numbers')
.sayAs({
word: "+1-377-777-1888",
interpret: "telephone"
});
var speechOutput = speech.ssml(true);
this.emit(':tell', speechOutput);
Note: In this example, the SSML is not surrounded by <speak/> because we passed 'true' into the ssml(boolean) method. This is intentional to work with the new SDK due to their current design.
'Hello <break time='1s'/> fellow Alexa developers <break time='500ms'/> Testing phone numbers <say-as interpret-as='telephone'>+1-377-777-1888</say-as>'
var Speech = require('ssml-builder');
var speech = new Speech();
speech.say('Hello')
.pause('1s')
.say('fellow Alexa developers');
var speechOutput = speech.toObject();
response.tell(speechOutput);
{
"type": "SSML",
"speech": "<speak>Hello <break time='1s'/> fellow Alexa developers</speak>"
}
When using Amazon specific tags, like whisper, you will need to import and use the AmazonSpeech object directly.
var AmazonSpeech = require('ssml-builder/amazon_speech');
var speech = new AmazonSpeech();
speech.say('Hello')
.pause('1s')
.whisper('I can see you when you are sleeping')
.pause('500ms')
.say('Is your phone number still')
.sayAs({
word: "+1-377-777-1888",
interpret: "telephone"
});
var ssml = speech.ssml();
speech.whisper('I can see you when you are sleeping');
speech.audio('https://carfu.com/audio/carfu-welcome.mp3');
speech.pause('500ms')
.say('you hear this after a 500 millisecond pause')
.pause('2s')
.say('you heard this after a 2 second pause');
speech.emphasis('strong', 'phrase will be strong');
speech.emphasis('moderate', 'phrase will be moderate');
speech.emphasis('reduced', 'phrase will be reduced');
speech.prosody({rate: 'slow'}, 'say slow');
speech.prosody({rate: 'fast'}, 'say fast');
speech.prosody({rate: '120%'}, 'increase the rate of speech by 20%');
speech.prosody({rate: '35%'}, 'decrease the rate of speech by 35%');
speech.prosody({pitch: 'medium'}, 'set pitch to medium');
speech.prosody({pitch: 'x-high'}, 'set pitch to extra high');
speech.prosody({pitch: '+20%'}, 'increase the pitch by 20%');
speech.prosody({pitch: '-10%'}, 'decrease the pitch by 10%');
speech.prosody({volume: 'soft'}, 'set volume to soft');
speech.prosody({volume: 'loud'}, 'set volume to loud');
speech.prosody({volume: '+2db'}, 'increase volume by 2db');
speech.prosody({volume: '-3db'}, 'decrease volume by 3db');
speech.paragraph('phrase will be said with extra strong breaks before and after itself');
speech.sentence('phrase will be said with strong breaks before and after itself');
speech.phoneme('ipa', "pɪˈkɑːn", 'pecan');
speech.phoneme('x-sampa', "fr\oU.z@n", 'frozen');
speech.say('this will be said');
speech.sayAs({word: '12345', interpret: 'digits'});
speech.sayAs({word: 'usa', interpret: 'characters'});
speech.sayAs({word: '5553329939', interpret: 'telephone'});
speech.sub('magnesium', 'Mg');
speech.partOfSpeech({word: 'record', role: 'amazon:VB'});
speech.partOfSpeech({word: 'record', role: 'amazon:NN'});