npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

google-tts-narrator

v0.2.1

Published

A Google Text-to-Speech Narrator

Downloads

9

Readme

Google TTS Narrator

npm version Coverage Status NPM

Use Google Cloud Text-to-Speech to convert SSML (or other types, see below) text into speeches.

English | 日本語

Setup

  • Create or setup a Google Cloud project
  • Make sure you account's billing is enabled (4 million characters are free each month)
  • Enable the Cloud Text-to-Speech API
  • Download Account key(it is a JSON file)
  • Set GOOGLE_APPLICATION_CREDENTIALS="/FULL/PATH/TO/ACCOUNT_KET.json" to

View here for more Infomations

Install

As a module

npm i google-tts-narrator

As a cli app

npx google-tts-narrator --from my_scenario.ssml.pug --to my_scenario_sounds/

Scenario File

SSML(XML)

Google Cloud Text-to-Speech only allowed ONE <speak></speak> for each call, this library will analyze your SSML file and call for each <speak></speak> elements with different configs.

You need to set audioConfig and voice attributes for each dialogue, which could found & tested here and here.

<speak audioConfig='{"audioEncoding": "MP3", "pitch": 0, "speakingRate": 1}'
  voice='{"languageCode": "en-US", "name": "en-US-Wavenet-G"}'>
  The <say-as interpret-as="characters">SSML</say-as>standard
  <break time="1s"/>is defined by the
  <sub alias="World Wide Web Consortium">W3C</sub>.
</speak>

<speak audioConfig='{"audioEncoding": "MP3", "pitch": 0, "speakingRate": 1}'
  voice='{"languageCode": "en-ES", "name": "es-ES-Standard-C", "ssmlGender": "MALE"}'>
  ¡Hola<break time="200ms"/>mundo!
</speak>

Upper scenario file will convert to 2 sound tracks:
demo_1.mp3 demo_2.mp3

PUG(was Jade)

Pug is an indent style HTML template DSL(Domain-Specific Language), it can also compiled to XML files.

SSML upper also could written like below

speak(
  audioconfig='{"audioEncoding": "MP3", "pitch": 0, "speakingRate": 1}'
  voice='{"languageCode": "en-US", "name": "en-US-Wavenet-G"}')
  | The #[say-as(interpret-as='characters') SSML] standard #[break(time='1s')/]
  | is defined by the #[sub(alias='World Wide Web Consortium') W3C].

speak(
  audioconfig='{"audioEncoding": "MP3", "pitch": 0, "speakingRate": 1}'
  voice='{"languageCode": "en-ES", "name": "es-ES-Standard-C", "ssmlGender": "MALE"}')
  | ¡Hola#[break(time='200ms')/] mundo!

Even could separate speakers' settings from scripts, it will made dialogue script more smoothly.
[Warning] Only Cli mode can deal with separated pug files!

//- _speaker.pug
mixin enSpeaker(audioConfig = {}, voice = {})
  -
    audioConfig = Object.assign({
      speakingRate: 1.12, audioEncoding: 'MP3',
    }, audioConfig)
    voice = Object.assign({
      languageCode: 'en-US', ssmlGender: 'MALE',
    }, voice)
  speak(audioConfig=audioConfig voice=voice)
    block

mixin jaSpeaker(audioConfig = {}, voice = {})
  -
    audioConfig = Object.assign({
      pitch: 1.6, speakingRate: 1.12,
      audioEncoding: 'MP3',
    }, audioConfig)
    voice = Object.assign({
      languageCode: 'ja-JP', name: 'ja-JP-Wavenet-A',
    }, voice)
  speak(audioConfig=audioConfig voice=voice)
    block
//- include_demo.pug
include _speaker.pug

+enSpeaker Hello!
+jaSpeaker こんにちは!
+enSpeaker Goodbye!
+jaSpeaker さようなら!

YAML(JSON)

You may also create an array of Text-to-Speech params in JSON or YAML

YAML

- &staff # Setup staff
  audioConfig:
    audioEncoding: MP3
    pitch: 1.6
    speakingRate: 1.07
  voice:
    languageCode: ja-JP
    name: ja-JP-Wavenet-A
  input:
    text: いらっしゃいませ。
- &customer # Setup customer
  audioConfig:
    audioEncoding: MP3
    pitch: 1.6
    speakingRate: 1.07
  voice:
    languageCode: ja-JP
    name: ja-JP-Wavenet-D
  input:
    text: チーズバーガーをください。
-
  <<: *staff
  input:
    ssml: |-
      <speak>
        かしこまりました。お召し上がりですか?<break time="300ms"/>お持ち帰りですか?
      </speak>
-
  <<: *customer
  input:
    text: 持ち帰ります。

JSON

[
  {
    "voice": {
      "languageCode": "en-US",
      "ssmlGender": "NEUTRAL"
    },
    "audioConfig": {
      "audioEncoding": "MP3"
    },
    "input": {
      "text": "Hello, world!"
    }
  },
  {
    "voice": {
      "languageCode": "en-ES",
      "name": "es-ES-Standard-C"
    },
    "audioConfig": {
      "audioEncoding": "MP3"
    },
    "input": {
      "text": "¡Hola, mundo!"
    }
  }
]

Cli

Make sure GOOGLE_APPLICATION_CREDENTIALS is set, and Cloud Text-to-Speech API permission enabled.

Run command below to generate sound tracks.

npx google-tts-narrator -s PATH/TO/FILE

System will guess type by your scenario file's extension, and create a directory for sound tracks if not given.

Options:
  -s, --from     Your Scenario File Path                              [required]
  -d, --to       Target Directory Path
  -t, --type     Scenario File Type, One of [pug|xml|yaml|json]
  -h, --help     Show help                                             [boolean]
  -v, --version  Show version number                                   [boolean]

API

Make sure GOOGLE_APPLICATION_CREDENTIALS is set, or set credentials manually.

const Narrator = require('google-tts-narrator');
const options = { credentials: require('/PATH/TO/credentials.json') }

const scenario = fs.readFileSync('PATH/TO/scenario.pug', 'utf-8');
const gen = Narrator.yieldSpeeches(scenario, 'pug', options);

...

API mode only support single file scenario

Generate Style

Use Generate Style API to create sounds step by step, it will save the costs.

const fs = require('fs');
const Narrator = require('google-tts-narrator');

const scenario = fs.readFileSync('PATH/TO/scenario.pug', 'utf-8');
const gen = Narrator.yieldSpeeches(scenario, 'pug');

(async () => {
  for await (let speach of gen) {
    const { step } = speach.narratorInfo;
    fs.writeFileSync(`PATH/TO/DIR/${step}.mp3`, speach.audioContent, 'binary');
  }
})();

Async/Await Style

Classical async/await style also can be used to batch create sound tracks.

const fs = require('fs');
const Narrator = require('google-tts-narrator');

const scenario = fs.readFileSync('PATH/TO/scenario.pug', 'utf-8');

(async () => {
  const speaches = await Narrator.fetchSpeeches();
  speaches.forEach((speach, i) => {
    fs.writeFileSync(`PATH/TO/DIR/${i}.mp3`, speach.audioContent, 'binary');
  })
})();

Special Thanks

Ms Rie Ishikawa of Rose Ladies School provides all Japanese dialogue samples for kindness.
All copyrights of Japanese dialogue samples belongs to Rose Ladies School. Rie Ishikawa.

LICENSE

MIT(Except Japanese Dialogue Samples)