SSML Split

Splits SSML strings into batches AWS Polly ánd Google's Text to Speech API can consume.
Based on polly-ssml-split by @oleglegun
Changes compared to polly-ssml-split
:
Added includeSSMLTagsInCounter: boolean
option to count characters based on the complete SSML tag and not just the included text characters.
For example:
<speak><p>some text</p></speak>
The default behaviour would count that as 9 characters, which is fine for AWS Polly, but not for Google's Text to Speech API.
With includeSSMLTagsInCounter: true
it will be count as 31 characters, just like Google's Text to Speech API counts it.
Usage:
npm install ssml-split --save
const ssmlSplit = require('ssml-split')
const options = {
softLimit: 2500,
hardLimit: 5000,
includeSSMLTagsInCounter: true
}
ssmlSplit.configure(options)
const batches = ssmlSplit.split('<speak>your long text here</speak>')
API Documentation
Options
AWS
const options = {
softLimit: 1500,
hardLimit: 3000,
includeSSMLTagsInCounter: false
}
Google
const options = {
softLimit: 2500,
hardLimit: 5000,
includeSSMLTagsInCounter: true
}
You can tweak the softLimit
to see what works for you. I suggest you keep the hardLimit
at the limitation limit of the respective API.
About
The polly-ssml-split by @oleglegun library already handles splitting of SSML correctly for AWS Polly, but wasn't working properly for Google's Text to Speech. So I just modified the package to fit my needs.
By adding the option includeSSMLTagsInCounter
to include the SSML tag characters in the calculation on when to split the SSML, makes the library also work with Google's Text to Speech API.
This package should prevent you from seeing this error when using Google's Text to Speech API:
INVALID_ARGUMENT: 5000 characters limit exceeded.
Source
https://cloud.google.com/text-to-speech/pricing?hl=en
Note that Speech Synthesis Markup Language (SSML) tags are included in the character count for billing purposes. For example, this input counts as 79 characters, including the SSML tags, newlines, and spaces:
<speak>
<say-as interpret-as="cardinal">12345</say-as> and one more
</speak>