Giter VIP home page Giter VIP logo

text-to-speech's Introduction

Text-To-Speech using Google API

This script will allow you to synthesize text into an audio file using the Google Text-to-Speech API.

Please note, that while this script is open-source and free, the Google Text-to-Speech API is not. Also there are "Standard" voices which sound decent, as well as more premium "WaveNet" voices which if you select will cost you more. See Google's Pricing page.

Installation

  1. This script is PHP-based so you will need PHP (CLI) and Composer.

Clone this git repo and in a terminal run:

composer install
  1. Next you will need to generate a Google API credentials JSON file. Follow Steps 1-4 here: https://cloud.google.com/text-to-speech/docs/quickstart-client-libraries

  2. Save the .json file you downloaded in Step 4 into this code directory as creds.json.

  3. Now you should be ready to run it. Open a terminal and run:

php tts.php "Hello world!"
  1. Confirm that it doesn't complain about any errors. If not, it should have told you that it created a mp3 file in the current directory.

Command-Line Options

There are several command-line options that allow you to configure the results.

Here's a list of the command-line arguments that the script supports:

php tts.php "text to speak" [output_filename] [rate] [voice] [format] [profile]

Note 1: The square brackets around parameters in the example above represent optional arguments.
Note 2: Any of the parameters after text to speak can be left blank to select the default, but you must use quotes to reserve the spot. For example, if you want to change the format without changing the rate or voice you can run:

php tts "text to speak" myfile.wav "" "" wav
Option Description
"text to speak" or "-" Text that you wish to synthesize into audio. If you wish to pipe text into the script please use a dash "-" instead. (Quotes are not necessary)
output_filename The name of the file you would like it to create. If left blank, the default filename format is:
YYYY-MM-DD_HH-MM-SS.mp3.
rate This is the speed of playback. The default is 1.5 (meaning 1.5 times faster than normal speaking speed) but this is adjustable between 0.25 and 4.0.
voice The name of a supported Google API voice. The default is en-US-Standard-I. For a full list, refer to https://cloud.google.com/text-to-speech/docs/voices.
format The output file format you wish to use. The options are: wav, mp3, or ogg. The default is mp3. (Note: it is possible to specify a different format than the output_filename above so to avoid confusion, keep these in sync.)
profile The sound profile of the target device you wish to use. The default is handset-class-device. For a full list, refer to: https://cloud.google.com/text-to-speech/docs/audio-profiles

Full syntax example:

php tts.php "This text will be synthesized to a wav file" my-text.wav 1.25 en-US-Standard-C wav medium-bluetooth-speaker-class-device

Options for Passing in Text to be Synthesized

To pipe in content, use a single dash as the first argument. Run:

echo "text to speak" | php tts.php - [output_filename] [rate] [voice] [format] [profile]

Text size constraints

The amount of text the command-line can receive varies from 32k to 2mb+. That said, you may hit this limit or you may prefer to pass in a reference to a file. Use the following syntax to accomplish this.

To pass in the contents of a file, use a single dash as the first argument. Run one of:

cat /path/to/file | php tts.php - [output_filename] [rate] [voice] [format] [profile]

or

php tts.php - [output_filename] [rate] [voice] [format] [profile] </path/to/file

Optional execution syntax

If you are using a Linux/Unix-based prompt this script can be run directly without invoking PHP on the command line every time.

So instead of running:

php tts.php

You can instead run:

./tts.php

Note: The default permissions from a git clone should've left the tts.php file executable. If it is no longer executable, run:

chmod u+x tts.php

text-to-speech's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.