1.0.2 • Published 3 years ago

ts-ssml-builder v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

ssml-builder

Description

Speech Synthesis Markup Language (SSML) is an XML-based markup language for speech synthesis applications.

This library supports the creation of SSML.

Install

npm install ts-ssml-builder

Usage

import { SSMLBuilder } from 'ts-ssml-builder';

const builder = new SSMLBuilder();
const ssml = builder
    .paragraph()
        .raw('This is sample')
        .break({ time: '10s' })
        .audio( {src: 'audio.mp4' } )
        .up()
    .up()
    .ssml();
console.log( ssml )
/** 
 * Output: String
 * <speak><p >This is sample<break time="10s" /><audio src="audio.mp4"></audio></p></speak>
*/

API

interface ISsmlBuilder {
    ssml(): string;
    raw( value: string, useEscape?: boolean ): Builder;
    sayAs( option: interfaces.ISayAs ): Builder;
    break( option?: Partial<interfaces.IBreak> ): Builder;
    sub( option: interfaces.ISub ): Builder;
    audio( option: interfaces.IAudio ): Builder; // Wrap tag
    sentence(): Builder; // Wrap tag
    paragraph(): Builder; // Wrap tag
    prosody( option: interfaces.IProsody ): Builder;
    emphasis( option?: interfaces.IEmphasis ): Builder; // Wrap tag
    up(): Builder;
};

export class Builder implements ISsmlBuilder { /** */}

Wrap tag( sentence, paragraph... ) needs to explicitly call up() when closing a tag.

NameWrap tag?Description
rawNoYou can add text and insert custom tags. useEscape is default true. 
sayAsNoInsert the say-as tag.
breakNoInsert the break tag.
subNoInsert the sub tag.
audioYesInsert the audio tag.
sentenceYesInsert the s tag.
paragraphYesInsert the p tag.
prosodyNoInsert the prosody tag.
emphasisYesInsert the emphasis tag.

API Params

The audio and prosody can be set to any attribute.

type InterpretAs = 'characters' | 'spell-out' | 'cardinal' | 'number' | 'address'
    | 'ordinal' | 'digits' | 'fraction' | 'unit' | 'date' | 'time' | 'telephone' 
    | 'interjection' | 'expletive'
interface ISayAs {
    interpretAs: InterpretAs;
    word: string;
    format?: string;
    detail?: string;
};

type BreakStrength = 'none' | 'x-weak' | 'weak' | 'medium' | 'strong' | 'x-strong'

interface IBreak {
    strength: BreakStrength;
    time: string;
};

interface ISub {
    alias: string;
    word: string;
};

interface IAudio {
    src: string;
    [ key: string ] : string;
};
interface IProsody {
    word: string;
    [ key: string ] : string;
};
export type EmphasisLevel = 'strong' | 'moderate' | 'none' | 'reduced'
export interface IEmphasis {
    level: EmphasisLevel;
};

Escape

Applies to all word values of the above API parameters and when useEscape of API:raw is true.

Therefore, if you use custom elements in raw, useEscape should be set to false.

/* Escape processing */
escapedLines = escapedLines.replace(/&/g, '&amp;');
escapedLines = escapedLines.replace(/"/g, '&quot;');
escapedLines = escapedLines.replace(/</g, '&lt;');
escapedLines = escapedLines.replace(/>/g, '&gt;');

Feature

Licence

MIT