mandnyc / ssml-builder

Apache License 2.0
162 stars 38 forks source link

ssml-builder

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.

Installation

npm install ssml-builder --save

Features

Code Example for the new Alexa SDK

see link to the new Alexa SDK https://github.com/alexa/alexa-skills-kit-sdk-for-nodejs

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);

The above code will produce the following SSML

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>'

Code Example for the old Alexa SDK

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);

The above code will produce the following object

  { 
    "type": "SSML",
    "speech": "<speak>Hello <break time='1s'/> fellow Alexa developers</speak>"
  }

Amazon SSML specific tags

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();

Tag Examples

amazon:effect

speech.whisper('I can see you when you are sleeping');

audio

speech.audio('https://carfu.com/audio/carfu-welcome.mp3');

break

speech.pause('500ms')
      .say('you hear this after a 500 millisecond pause')
      .pause('2s')
      .say('you heard this after a 2 second pause');

emphasis

speech.emphasis('strong', 'phrase will be strong');
speech.emphasis('moderate', 'phrase will be moderate');
speech.emphasis('reduced', 'phrase will be reduced');

prosody

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');

p

speech.paragraph('phrase will be said with extra strong breaks before and after itself');

s

speech.sentence('phrase will be said with strong breaks before and after itself');

phoneme

speech.phoneme('ipa', "pɪˈkɑːn", 'pecan');
speech.phoneme('x-sampa', "fr\oU.z@n", 'frozen');

speak

speech.say('this will be said');

say-as

speech.sayAs({word: '12345', interpret: 'digits'});
speech.sayAs({word: 'usa', interpret: 'characters'});
speech.sayAs({word: '5553329939', interpret: 'telephone'});

sub

speech.sub('magnesium', 'Mg');

w

speech.partOfSpeech({word: 'record', role: 'amazon:VB'});
speech.partOfSpeech({word: 'record', role: 'amazon:NN'});