Splits SSML strings into batches AWS Polly ánd Google's Text to Speech API can consume.
- Splits your large SSML into batches AWS Polly and Google's Text to Speech API can consume.
- Makes sure you stay below the API character limitations by configuring a
- Creates the least possible batch size to limit your requests to the Text to Speech API's.
- Will split text at the nearest
;or space. Can be configured.
- Sanitizes your SSML by removing new lines, excessive white spaces, and empty tags, resulting in less characters used.
- Uses TypeScript so you can enjoy the type safety and documentation that comes with it.
- Installation - Walk through how to install SSML Split.
- Usage - Read how SSML Split works with the available options.
- Recommended Options - Use these options to get started quickly.
- Contributing - Become familiar with how to contribute back to SSML Split
- Code of Conduct - Be a good citizen by following these repository rules
Install the package with:
npm install ssml-split --save
Import the package and set the options. Use the
.split() method to split your SSML string. 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:
||Set to which synthesizer you are using. Useful for when you use
||The amount of characters the script will start trying to break-up your SSML in multiple parts. You can tweak this number to see what works for you.|
||The amount of characters the script should stay below for maximum size per SSML part. If any batch size goes above this, the script will error. This hard limit is the character limit of the AWS or Google API you are using.|
||Characters that can be used as split markers for plain text.|
By using the option
synthesizer: 'google' the library will include counting SSML tags characters to determine the best possible split moment. This makes the library also work with Google's Text to Speech API.
The default behaviour would count that as 9 characters, which is fine for AWS Polly, but not for Google's Text to Speech API.
synthesizer: 'google' it will be count as 31 characters, just like Google's Text to Speech API counts it.
This should prevent you from seeing this error when using Google's Text to Speech API:
INVALID_ARGUMENT: 5000 characters limit exceeded.
By adding the option
breakParagraphsAboveHardLimit: true you allow the script to break up large paragraphs by removing the
<p> and replacing the
</p> with a
<break strength="x-strong" /> for AWS or
<break strength="x-weak" /> for Google. Which results in the same pause. This allows the library to properly split large paragraphs.
Using this option will result in 20 more characters, per paragraph, to your usage when using Google's Text to Speech API.
If you work with large paragraphs and you do not use this option, you might run into errors like
SSML tag appeared to be too long.
Using this option is recommended when you have SSML length that goes above the
synthesizer: 'aws'softLimit: 2000hardLimit: 3000 // AWS Polly limitationbreakParagraphsAboveHardLimit: true // optional, but recommended when you have large <p>'s
synthesizer: 'google'softLimit: 4000hardLimit: 5000 // Google Text to Speech API limitationbreakParagraphsAboveHardLimit: true // optional, but recommended when you have large <p>'s
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.
Changes compared to
synthesizeroption to count characters based on the complete SSML tag and not just the included text characters. Which is required if you work with Google's Text to Speech API.
- Rewrote the library to use Typescript, so you get correct type checking in your Typescript project.
- Removed the
.configuremethod and use the class constructor method for it instead.
breakParagraphsAboveHardLimitoptions to break up large paragraphs by removing the
<p>and replacing the
<break strength="x-strong" />for AWS or
<break strength="x-weak" />for Google. Which results in the same pause. This allows the library to properly split the paragraph and to send less batches to the text to speech API's.
- Added more tests using Jest.
Any contribution is appreciated! Please read our CONTRIBUTING.md on how to contribute.
Use a test-driven approach when developing new features or fixing bugs.
$ npm install$ npm run dev
Run tests on file change:
$ npm test:watch
$ npm run build
Run all tests:
$ npm test