Socket
Socket
Sign inDemoInstall

tts-narrator

Package Overview
Dependencies
129
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    tts-narrator

Generate narration with Text-To-Speech technology


Version published
Weekly downloads
23
decreased by-28.12%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

tts-narrator

Generate narration with Text-To-Speech technology

Version Downloads/week CI

The input is a script file in YAML format. Example script files can be found under test/fixtures.

The script file is structured in this way:

  • settings: script settings
    • voice: voice settings at script level
  • chapters: array of chapter
    • each chapter can contain settings and multiple sections:
      • settings: chapter level voice settings that can override script level voice settings
      • sections: array of sections
        • each section can contain settings and multiple paragraphs:
          • settings: section level voice settings that can override upper level voice settings
          • paragraphs: array of paragraphs
            • each paragraph can contain settings and text:
              • settings: paragraph level voice settings that can override upper level voice settings
              • text: text content that needs to be converted into audio

The text field of a paragraph can be pure text, or an SSML fragment. Multi-line strings are supported.

It has command line flags --play and --no-play to control whether generated MP3s should be played back. This feature is supported by a dev dependency node-speaker. If you use it as a library in your project, and would like to have the play back capability, you need to install node-speaker as a dependency in your project.

When running on MacOS, to avoid illegal hardware instruction issue, try npm i speaker --mpg123-backend=openal

CLI

To use the CLI as an NPM package, you need to install it with all its optional dependencies, like this:

npm i -g --include=optional tts-narrator
USAGE
  $ tts-narrator   FILE[-h] [-v] [-d] [-s azure] [-k <value>]
    [--subscription-key-env <value>] [-r <value>] [-p] [-i] [-o] [--dry-run]
    [--ssml | -q] [--chapters <value>] [--sections <value>]

ARGUMENTS
  FILE  path to the script file (.yml)

FLAGS
  -d, --debug                         output debug information
  -h, --help                          Show help
  -i, --interactive                   wait for key press before entering each
                                      section
  -k, --subscription-key=<value>      Azure Speech service subscription key
  -o, --overwrite                     always overwrite previously generated
                                      audio files
  -p, --[no-]play                     play generated audio
  -q, --quiet                         output warn and error information only
  -r, --region=<value>                region of the text-to-speech service
  -s, --service=<option>              text-to-speech service to use
                                      <options: azure>
  -v, --version                       Show CLI version
      --chapters=<value>              list of chapters to process, examples:
                                      "1-10,13,15", "4-"
      --dry-run                       don't try to generate or play audio
      --sections=<value>              list of sections to process, examples:
                                      "1-10,13,15", "5-"
      --ssml                          display generated SSML
      --subscription-key-env=<value>  Name of the environment variable that
                                      holds the subscription key

DESCRIPTION
  Generate narration with Text-To-Speech technology

EXAMPLES
  $ tts-narrator myscript.yml --play --interactive --service azure --subscription-key-env TTS_SUBSCRIPTION_KEY --region australiaeast

  $ tts-narrator ./test/fixtures/script3.yml -s azure --ssml -r australiaeast --subscription-key-env=TTS_SUB_KEY  --no-play --interactive -d

  $ tts-narrator ./test/fixtures/script3.yml -s azure -r australiaeast --subscription-key-env=TTS_SUB_KEY --quiet

  $ tts-narrator ./test/fixtures/script3.yml

API

To use the NPM package as a dependency, you just need to install it normally, like this:

npm i tts-narrator

Example:

const ttsService = new AzureTtsService(...);
const ttsNarrator = new TtsNarrator(ttsService, './output-folder');
const script = await loadScript('./my-script.yml');
await ttsNarrator.narrate(script);
console.log(`One of the generated audio file is: ${script.chapters[0].sections[0].paragraphs[0].audioFilePath}`);

tts-narrator

Modules

Classes

Class: AzureTtsService

azure-tts-service.AzureTtsService

Hierarchy
Constructors
constructor

new AzureTtsService()

Overrides

BaseTtsService.constructor

Properties
PropertyDescription
Protected Optional options: Omit<AzureAudioGenerationOptions, "outputFilePath">
Methods
buildSpeakStartTag

Protected buildSpeakStartTag(voiceSettings): string

Parameters
NameType
voiceSettingsVoiceSettings
Returns

string

Overrides

BaseTtsService.buildSpeakStartTag


buildVoiceStartTag

Protected buildVoiceStartTag(voiceSettings): string

Parameters
NameType
voiceSettingsVoiceSettings
Returns

string

Inherited from

BaseTtsService.buildVoiceStartTag


generateAudio

generateAudio(ssml, options): Promise<any>

Parameters
NameType
ssmlstring
optionsAzureAudioGenerationOptions | Pick<AzureAudioGenerationOptions, "outputFilePath">
Returns

Promise<any>

Overrides

BaseTtsService.generateAudio


generateSSML

generateSSML(paragraph): Promise<string>

Parameters
NameType
paragraphNarrationParagraph
Returns

Promise<string>

Inherited from

BaseTtsService.generateSSML


generateSsmlWithoutValidation

Protected generateSsmlWithoutValidation(paragraph): Object

Parameters
NameType
paragraphNarrationParagraph
Returns

Object

NameType
lineOffsetnumber
ssmlstring
Inherited from

BaseTtsService.generateSsmlWithoutValidation


validateXML

Protected validateXML(xml, lineOffset): void

Parameters
NameType
xmlstring
lineOffsetnumber
Returns

void

Inherited from

BaseTtsService.validateXML

Class: NarrationChapter

narration-script.NarrationChapter

Implements
Constructors
constructor

new NarrationChapter(chapter, index, script)

Parameters
NameType
chapterChapter
indexnumber
scriptNarrationScript
Properties
PropertyDescription
Protected chapter: Chapter
index: number
script: NarrationScript
sections: NarrationSection[]Implementation of

Chapter.sections
Accessors
key

get key(): string

Returns

string

Implementation of

Chapter.key


settings

get settings(): VoiceSettings

Returns

VoiceSettings

Implementation of

Chapter.settings

Methods
getSectionByKey

getSectionByKey(key): undefined | NarrationSection

Parameters
NameType
keystring
Returns

undefined | NarrationSection

Class: NarrationParagraph

narration-script.NarrationParagraph

Implements
Constructors
constructor

new NarrationParagraph(paragraph, index, section, chapter, script)

Parameters
NameType
paragraphParagraph
indexnumber
sectionNarrationSection
chapterNarrationChapter
scriptNarrationScript
Properties
PropertyDescription
Optional audioFilePath: stringPath of the generated audio file. Only for in-memory processing, not supposed to be stored in file.
chapter: NarrationChapter
index: number
Protected paragraph: Paragraph
script: NarrationScript
section: NarrationSection
Accessors
key

get key(): string

Returns

string

Implementation of

Paragraph.key


settings

get settings(): VoiceSettings

Returns

VoiceSettings

Implementation of

Paragraph.settings


text

get text(): string

Returns

string

Implementation of

Paragraph.text

Class: NarrationScript

narration-script.NarrationScript

Implements
Constructors
constructor

new NarrationScript(script, scriptFilePath)

Parameters
NameType
scriptScript
scriptFilePathstring
Properties
PropertyDescription
chapters: NarrationChapter[]Implementation of

Script.chapters
Protected script: Script
scriptFilePath: string
Accessors
settings

get settings(): ScriptSettings

Returns

ScriptSettings

Implementation of

Script.settings

Methods
export

export(): Script

Returns

Script


getChapterByKey

getChapterByKey(key): undefined | NarrationChapter

Parameters
NameType
keystring
Returns

undefined | NarrationChapter

Class: NarrationSection

narration-script.NarrationSection

Implements
Constructors
constructor

new NarrationSection(section, index, chapter, script)

Parameters
NameType
sectionSection
indexnumber
chapterNarrationChapter
scriptNarrationScript
Properties
PropertyDescription
chapter: NarrationChapter
index: number
paragraphs: NarrationParagraph[]Implementation of

Section.paragraphs
script: NarrationScript
Protected section: Section
Accessors
key

get key(): string

Returns

string

Implementation of

Section.key


settings

get settings(): VoiceSettings

Returns

VoiceSettings

Implementation of

Section.settings

Class: ScriptProcessor

script-processor.ScriptProcessor

Hierarchy
Constructors
constructor

new ScriptProcessor(scriptFilePath, flags, cliConsole?)

Parameters
NameType
scriptFilePathstring
flagsObject & FlagOutput & {}
cliConsole?LineLogger<(message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void>
Properties
PropertyDescription
Protected _chalk: undefined | null | Chalk & ChalkFunction & {}
Protected _prompts: undefined | null | typeof prompts
Protected _script: NarrationScript
Protected audioGenerationOptions: undefined | Omit<AudioGenerationOptions, "outputFilePath">
Protected chapterRange: undefined | MultiRange
Protected cliConsole: LineLogger<(message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void>
Protected flags: Object & FlagOutput & {}
Protected scriptFilePath: string
Protected sectionRange: undefined | MultiRange
Protected ttsService: TtsService
Accessors
chalk

Protected get chalk(): undefined | null | typeof prompts

chalk, or null caused by library not available

Returns

undefined | null | typeof prompts


prompts

Protected get prompts(): undefined | null | typeof prompts

prompts function, or null caused by library not available

Returns

undefined | null | typeof prompts


script

get script(): NarrationScript

Returns

NarrationScript

Methods
determineAudioFilePath

Protected determineAudioFilePath(ssmlHash, _paragraph): Promise<string>

Parameters
NameType
ssmlHashstring
_paragraphNarrationParagraph
Returns

Promise<string>


hash

Protected hash(ssml, _paragraph): string

Parameters
NameType
ssmlstring
_paragraphNarrationParagraph
Returns

string


initialiseTtsServiceIfNeeded

Protected initialiseTtsServiceIfNeeded(): Promise<void>

Returns

Promise<void>


loadScriptIfNeeded

Protected loadScriptIfNeeded(): Promise<void>

Returns

Promise<void>


parseRanges

Protected parseRanges(): void

Returns

void


processGeneratedAudioFile

Protected processGeneratedAudioFile(audioFilePath): Promise<string>

Parameters
NameType
audioFilePathstring
Returns

Promise<string>


run

run(reconstructedCommandLine?): Promise<void>

Parameters
NameType
reconstructedCommandLine?string
Returns

Promise<void>


runWithoutCatch

runWithoutCatch(reconstructedCommandLine?): Promise<void>

Parameters
NameType
reconstructedCommandLine?string
Returns

Promise<void>

Class: TtsNarrator

tts-narrator.TtsNarrator

Class for generating narration. Instance of this class can be used to generate narration audio for scripts by calling the narrate(...) method.

Example

const ttsService = new AzureTtsService(...);
const ttsNarrator = new TtsNarrator(ttsService, './output-folder');
const script = await loadScript('./my-script.yml');
await ttsNarrator.narrate(script);
console.log(`One of the generated audio file is: ${script.chapters[0].sections[0].paragraphs[0].audioFilePath}`);
Hierarchy
Constructors
constructor

new TtsNarrator(ttsService, audioFileFolder, options?, cliConsole?)

Constructor

Parameters
NameTypeDefault valueDescription
ttsServiceTtsServiceundefinedThe TTS service to be used for generating audio
audioFileFolderstringundefinedThe folder that generated audio files will be placed
options?Partial<Object & FlagOutput & {}>undefinedOptional settings
cliConsoleLineLogger<(message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void>silentLoggerOptional logger
Overrides

ScriptProcessor.constructor

Properties
PropertyDescription
Protected _chalk: undefined | null | Chalk & ChalkFunction & {}Inherited from

ScriptProcessor._chalk
Protected _prompts: undefined | null | typeof promptsInherited from

ScriptProcessor._prompts
Protected _script: NarrationScriptInherited from

ScriptProcessor._script
Protected audioFileFolder: stringThe folder that generated audio files will be placed
Protected audioGenerationOptions: undefined | Omit<AudioGenerationOptions, "outputFilePath">Inherited from

ScriptProcessor.audioGenerationOptions
Protected chapterRange: undefined | MultiRangeInherited from

ScriptProcessor.chapterRange
Protected cliConsole: LineLogger<(message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void>Inherited from

ScriptProcessor.cliConsole
Protected flags: Object & FlagOutput & {}Inherited from

ScriptProcessor.flags
Protected scriptFilePath: stringInherited from

ScriptProcessor.scriptFilePath
Protected sectionRange: undefined | MultiRangeInherited from

ScriptProcessor.sectionRange
Protected ttsService: TtsServiceInherited from

ScriptProcessor.ttsService
Accessors
chalk

Protected get chalk(): undefined | null | typeof prompts

chalk, or null caused by library not available

Returns

undefined | null | typeof prompts

Inherited from

ScriptProcessor.chalk


prompts

Protected get prompts(): undefined | null | typeof prompts

prompts function, or null caused by library not available

Returns

undefined | null | typeof prompts

Inherited from

ScriptProcessor.prompts


script

get script(): NarrationScript

Returns

NarrationScript

Inherited from

ScriptProcessor.script

Methods
determineAudioFilePath

Protected determineAudioFilePath(ssmlHash, _paragraph): Promise<string>

Parameters
NameType
ssmlHashstring
_paragraphNarrationParagraph
Returns

Promise<string>

Overrides

ScriptProcessor.determineAudioFilePath


hash

Protected hash(ssml, _paragraph): string

Parameters
NameType
ssmlstring
_paragraphNarrationParagraph
Returns

string

Inherited from

ScriptProcessor.hash


initialiseTtsServiceIfNeeded

Protected initialiseTtsServiceIfNeeded(): Promise<void>

Returns

Promise<void>

Inherited from

ScriptProcessor.initialiseTtsServiceIfNeeded


loadScriptIfNeeded

Protected loadScriptIfNeeded(): Promise<void>

Returns

Promise<void>

Inherited from

ScriptProcessor.loadScriptIfNeeded


narrate

narrate(script): Promise<void>

Generate narration for the script

Parameters
NameTypeDescription
scriptNarrationScriptthe input script which will also be modified for recording audioFilePath
Returns

Promise<void>

nothing


parseRanges

Protected parseRanges(): void

Returns

void

Inherited from

ScriptProcessor.parseRanges


processGeneratedAudioFile

Protected processGeneratedAudioFile(audioFilePath): Promise<string>

Parameters
NameType
audioFilePathstring
Returns

Promise<string>

Inherited from

ScriptProcessor.processGeneratedAudioFile


run

run(reconstructedCommandLine?): Promise<void>

Parameters
NameType
reconstructedCommandLine?string
Returns

Promise<void>

Inherited from

ScriptProcessor.run


runWithoutCatch

runWithoutCatch(reconstructedCommandLine?): Promise<void>

Parameters
NameType
reconstructedCommandLine?string
Returns

Promise<void>

Inherited from

ScriptProcessor.runWithoutCatch

Class: export=

tts-narrator-cli.export=

Hierarchy
  • Command

    export=

Constructors
constructor

new export=(argv, config)

Parameters
NameType
argvstring[]
configConfig
Inherited from

Command.constructor

Properties
PropertyDescription
Static args: ObjectType declaration

Static description: string = 'Generate narration with Text-To-Speech technology'Overrides

Command.description
Static examples: string[]Overrides

Command.examples
Static flags: ObjectType declaration

Static id: string = ' 'Overrides

Command.id
Methods
run

run(): Promise<void>

Returns

Promise<void>

Overrides

Command.run

Class: BaseTtsService

tts-service.BaseTtsService

Hierarchy
Implements
Constructors
constructor

new BaseTtsService()

Methods
buildSpeakStartTag

Protected buildSpeakStartTag(voiceSettings): string

Parameters
NameType
voiceSettingsVoiceSettings
Returns

string


buildVoiceStartTag

Protected buildVoiceStartTag(voiceSettings): string

Parameters
NameType
voiceSettingsVoiceSettings
Returns

string


generateAudio

generateAudio(_ssml, _options): Promise<void>

Parameters
NameType
_ssmlstring
_optionsAudioGenerationOptions
Returns

Promise<void>

Implementation of

TtsService.generateAudio


generateSSML

generateSSML(paragraph): Promise<string>

Parameters
NameType
paragraphNarrationParagraph
Returns

Promise<string>

Implementation of

TtsService.generateSSML


generateSsmlWithoutValidation

Protected generateSsmlWithoutValidation(paragraph): Object

Parameters
NameType
paragraphNarrationParagraph
Returns

Object

NameType
lineOffsetnumber
ssmlstring

validateXML

Protected validateXML(xml, lineOffset): void

Parameters
NameType
xmlstring
lineOffsetnumber
Returns

void

Enums

Enumeration: TtsServiceType

tts-service.TtsServiceType

Enumeration Members
Azure

Azure = "azure"

Interfaces

Interface: AzureAudioGenerationOptions

azure-tts-service.AzureAudioGenerationOptions

Hierarchy
Properties
PropertyDescription
outputFilePath: stringInherited from

AudioGenerationOptions.outputFilePath
Optional serviceRegion: string
Optional subscriptionKey: string

Interface: Chapter

narration-script.NarrationScriptFile.Chapter

Implemented by
Properties
PropertyDescription
Optional key: string
sections: Section[]
Optional settings: VoiceSettings

Interface: Paragraph

narration-script.NarrationScriptFile.Paragraph

Implemented by
Properties
PropertyDescription
Optional key: string
Optional settings: VoiceSettings
text: string

Interface: Script

narration-script.NarrationScriptFile.Script

Implemented by
Properties
PropertyDescription
chapters: Chapter[]
settings: ScriptSettings

Interface: Section

narration-script.NarrationScriptFile.Section

Implemented by
Properties
PropertyDescription
Optional key: string
paragraphs: Paragraph[]
Optional settings: VoiceSettings

Interface: ScriptSettings

narration-script.ScriptSettings

Properties
PropertyDescription
Optional service: Azure
Optional voice: VoiceSettings

Interface: VoiceSettings

narration-script.VoiceSettings

Properties
PropertyDescription
Optional language: string
Optional name: string

Interface: AudioGenerationOptions

tts-service.AudioGenerationOptions

Hierarchy
Properties
PropertyDescription
outputFilePath: string

Interface: TtsService

tts-service.TtsService

Implemented by
Methods
generateAudio

generateAudio(ssml, options): Promise<void>

Parameters
NameType
ssmlstring
optionsAudioGenerationOptions
Returns

Promise<void>


generateSSML

generateSSML(paragraph): Promise<string>

Parameters
NameType
paragraphNarrationParagraph
Returns

Promise<string>

Modules

Module: audio-utils

Functions
getAudioFileDuration

getAudioFileDuration(filePath): Promise<number>

Parameters
NameType
filePathstring
Returns

Promise<number>


playMp3File

playMp3File(filePath, infoLogger): Promise<void>

Parameters
NameType
filePathstring
infoLogger(msg: string) => void
Returns

Promise<void>

Module: azure-tts-service

Classes
Interfaces

Module: index

References
AudioGenerationOptions

Re-exports AudioGenerationOptions


AzureAudioGenerationOptions

Re-exports AzureAudioGenerationOptions


AzureTtsService

Re-exports AzureTtsService


BaseTtsService

Re-exports BaseTtsService


NarrationChapter

Re-exports NarrationChapter


NarrationParagraph

Re-exports NarrationParagraph


NarrationScript

Re-exports NarrationScript


NarrationScriptFile

Re-exports NarrationScriptFile


NarrationSection

Re-exports NarrationSection


ScriptProcessor

Re-exports ScriptProcessor


ScriptSettings

Re-exports ScriptSettings


TtsNarrator

Re-exports TtsNarrator


TtsService

Re-exports TtsService


TtsServiceType

Re-exports TtsServiceType


VoiceSettings

Re-exports VoiceSettings


getAudioFileDuration

Re-exports getAudioFileDuration


loadScript

Re-exports loadScript


playMp3File

Re-exports playMp3File


saveScript

Re-exports saveScript

Namespace: NarrationScriptFile

narration-script.NarrationScriptFile

Interfaces

Module: narration-script

Namespaces
Classes
Interfaces
Functions
loadScript

loadScript(scriptFilePath): Promise<NarrationScript>

Parameters
NameType
scriptFilePathstring
Returns

Promise<NarrationScript>


saveScript

saveScript(script): Promise<void>

Parameters
NameType
scriptNarrationScript
Returns

Promise<void>

saveScript(script, scriptFilePath): Promise<void>

Parameters
NameType
scriptScript
scriptFilePathstring
Returns

Promise<void>

Module: script-processor

Classes

Module: script-processor-flags

Type Aliases
ScriptProcessorFlags

Ƭ ScriptProcessorFlags: CommandOptions<{ flags: typeof scriptProcessorFlags }>["flags"]

Variables
scriptProcessorFlags

Const scriptProcessorFlags: Object

CLI flags that are required/used by the ScriptProcessor.

Type declaration
NameType
chaptersOptionFlag<undefined | string, CustomOptions>
debugBooleanFlag<boolean>
dry-runBooleanFlag<boolean>
interactiveBooleanFlag<boolean>
overwriteBooleanFlag<boolean>
playBooleanFlag<boolean>
quietBooleanFlag<boolean>
regionOptionFlag<undefined | string, CustomOptions>
sectionsOptionFlag<undefined | string, CustomOptions>
serviceOptionFlag<undefined | string, CustomOptions>
ssmlBooleanFlag<boolean>
subscription-keyOptionFlag<undefined | string, CustomOptions>
subscription-key-envOptionFlag<undefined | string, CustomOptions>

Module: tts-narrator

Classes

Module: tts-narrator-cli

Classes

Module: tts-service

Enumerations
Classes
Interfaces

Keywords

FAQs

Last updated on 14 May 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc