NAV Navbar
Python Go

TTS as a Service gRPC API

The Nuance TTS (Text to Speech) service is powered by the Nuance Vocalizer for Cloud (NVC) engine, which synthesizes speech from text. NVC works with Nuance Vocalizer for Enterprise (NVE) and Nuance voice packs to generate speech.

Nuance TTS as a service lets you request speech synthesis using gRPC from NVC engines running on Nuance-hosted machines. It works with a voice in your language and locale, with choices of gender and age.

The gRPC protocol provided by NVC allows a client application to request speech synthesis services in all the programming languages supported by gRPC.

gRPC is an open source RPC (remote procedure call) software used to create services. It uses HTTP/2 for transport, and protocol buffers to define the structure of the application. NVC supports Protocol Buffers version 3, also known as proto3.

Version: v1

This release supports version v1 of the gRPC protocol. See gRPC setup to download the proto file and get started.

Upgrading to v1

Generate client stubs from the new proto file

$ cd v1 [go to directory containing v1 proto file]

$ python3 -m grpc_tools.protoc --proto_path=./ --python_out=./ --grpc_python_out=./ nuance_tts.proto

$ ls -1 nuance_t*.py
nuance_tts_pb2_grpc.py
nuance_tts_pb2.py

Generate client stubs from the new proto file

$ protoc -I ./src/v1/ ./src/v1/nuance_tts.proto --go_out=plugins=grpc:./src/v1

$ ls -1 v1/nuance_t*.py
nuance_tts.pb.go

In client app, change Input for plain text, remove input type

OLD
request.input.type = "text/plain;charset=utf-8"
request.input.body = "This is my input text."

-> NEW V1
request.input.text.text = "This is my input text."

For text with control sequences, change Input, remove input type

OLD
request.input.type = "text/plain;charset=utf-8"
request.input.body = "This is my input text, including \\!\\pause=500\\ control codes."

-> NEW V1
request.input.tokenized_sequence.tokens.extend([
    Token(text="This is my input text, including "), 
    Token(control_code=ControlCode(key="pause", value="500")),
    Token(text="control codes.")
])

For SSML, change Input, remove input type

OLD
request.input.type = "text/plain;charset=utf-8"
request.input.body = '<?xml version="1.0"?><speak xmlns="http://www.w3.org/2001/10/synthesis" 
xml:lang="en-US" version="1.0"><prosody volume="10">I can speak rather quietly,</prosody>
<prosody volume="90">But also very loudly.</prosody></speak>’

-> NEW V1
request.input.ssml.text = '<?xml version="1.0"?><speak xmlns="http://www.w3.org/2001/10/synthesis" 
xml:lang="en-US" version="1.0"><prosody volume="10">I can speak rather quietly,</prosody>
<prosody volume="90">But also very loudly.</prosody></speak>'

Change opus field if used

OLD
audio_format=AudioFormat(opus=OggOpus(OggOpus fields))

-> NEW V1
audio_format=AudioFormat(opus=Opus(raw Opus fields)) 
audio_format=AudioFormat(ogg_opus=OggOpus(Ogg Opus fields))

Rename and change type

speaking_rate_percentage (uint) -> speaking_rate_factor (float)
EnumSSMLValidation -> EnumSSMLValidationMode

Consolidate download parameters

OLD
message DownloadParameters {
  oneof optional_download_parameter_max_age {uint32 max_age = 1;}
  oneof optional_download_parameter_max_stale {uint32 max_stale = 2;}
} 

-> NEW V1
message DownloadParameters {
  ...
  map<string, string> headers = 1; 
  ...
}  

To upgrade to the v1 protocol from v1beta1, you need to regenerate your programming-language stub files from the new proto files, then make small adjustments to your client application.

First regenerate your client stubs from the new proto files, as described in gRPC setup.

  1. Download the gRPC proto file here. We recommend you make a new directory for the v1 files.

  2. Use gRPC tools to generate the client stubs from the proto file.

  3. Notice the new client stub files.

Then adjust your client application for the changes made to the protocol in v1.

Prerequisites from Mix

Before developing your TTS gRPC application, you need a Nuance Mix project. This project provides credentials to run your application against the Nuance-hosted NVC engine.

  1. Create a Mix project and model: see Mix.nlu workflow to:

    • Create a Mix project.

    • Create, train, and build a model in the project. If you are using other Nuance "as a service" products (such as Krypton or NLU), you may use the same Mix project for NVC. Your project must include a model even though it is not needed for your NVC application.

    • Create and deploy an application configuration for the project.

  2. Generate a "secret" and client ID of your Mix project: see Mix.dashboard Obtain authentication for services. Later you will use these credentials to request an authorization token to run your application.

  3. Learn the URL to call the NLU service: see Mix.dashboard Accessing a runtime service.

gRPC setup

Download proto file

nuance_tts.proto

Install gRPC for programming language

$ python3 -m pip install --upgrade pip
$ python3 -m pip install grpcio
$ python3 -m pip install grpcio-tools
$ go get google.golang.org/grpc
$ go get github.com/akamensky/argparse
$ go get github.com/youpy/go-wav

Generate client stubs

$ python3 -m grpc_tools.protoc --proto_path=./ --python_out=./ --grpc_python_out=./ nuance_tts.proto

$ ls -1 nuance_t*.py
nuance_tts_pb2_grpc.py
nuance_tts_pb2.py
$ protoc -I ./src/v1/ ./src/v1/nuance_tts.proto --go_out=plugins=grpc:./src/v1

$ ls -1 v1/nuance_t*.py
nuance_tts.pb.go

The basic steps in using the NVC gRPC protocol are:

  1. Download the gRPC proto file here: nuance_tts.proto. This proto3 file contains a generic version of the functions or classes that can request speech synthesis from an NVC engine.

  2. Install gRPC for the programming language of your choice, including C++, Java, Python, Go, Ruby, C#, Node.js, and others. See gRPC Documentation for a complete list and instructions on using gRPC with each one.

  3. Generate client stub files in your programming language from the proto files using gRPC protoc. Depending on your programming language, the stubs may consist of one file or multiple files.

    These stub files contain the methods and fields from the proto files as implemented in your programming language. You will consult the stubs in conjunction with the proto files.

  4. Write your client app, referencing the functions or classes in the client stub files. See Client app development for details and a scenario.

  5. Run your client app to request synthesis. See Sample applications.

Client app development

The gRPC protocol for NVC lets you create a client application for synthesizing text and obtaining information about available voices. This section describes how to implement the basic functionality of NVC in the context of a Python or Go application. For the complete applications, see Sample applications.

The essential tasks are illustrated in the following high-level sequence flow.

Sequence flow

Step 1: Generate token

A Linux script requests the token and saves it in a variable

#!/bin/bash 

CLIENT_ID="appID%3ANMDPTRIAL_your_name_nuance_com_20190919T190532565840"
SECRET="appID%3ANMDPTRIAL_your_name_nuance_com_20190919T190532565840"
export MY_TOKEN="`curl https://auth.crt.nuance.com/oauth2/token 
-d 'grant_type=client_credentials' -d 'scope=asr dialog nlu tts' 
-d "client_id=$CLIENT_ID" -d "client_secret=$SECRET" 
| python -m json.tool | python -c 'import sys, json; print(json.load(sys.stdin)["access_token"])'`"

./client.py --serverUrl 'tts.api.nuance.com:443' --token $MY_TOKEN 
--secure --saveAudio --saveAudioinWave  

For the Go app, set the client ID and secret in a config file, config.json

{
    "client_id": "appID:<Provide Your Mix App Id>",
    "client_secret": "<Provide Your Mix Client Secret>",
    "token_url": "https://auth.crt.nuance.com/oauth2/token"
}

Nuance Mix uses the OAuth 2.0 protocol for authentication. The client application must provide an access token to be able to access the NVC runtime service. The token expires after a short period of time so must be regenerated frequently.

Your client application uses the client ID and secret from the Mix Dashboard (see Prerequisites from Mix) to generate an authentication token from the Mix Authentication Service, available at the following URL:

auth.crt.nuance.com/oauth2/token

The token may be generated in several ways, for example:

Step 2: Authenticate and connect

Create the connection to the TTS service using the token

def create_channel():
    call_credentials = None
    channel = None

    if args.token:
        log.debug("Adding CallCredentials with token %s" % args.token)
        call_credentials = grpc.access_token_call_credentials(args.token)
    . . . 
    channel_credentials = grpc.ssl_channel_credentials(root_certificates=root_certificates, private_key=private_key, certificate_chain=certificate_chain)
        if call_credentials is not None:
            channel_credentials = grpc.composite_channel_credentials(channel_credentials, call_credentials)
        channel = grpc.secure_channel(args.serverUrl, credentials=channel_credentials, options=[('grpc.max_receive_message_length', args.maxReceiveSizeMB * 1024 * 1024)])
    else:
        log.debug("Creating insecure gRPC channel")
        channel = grpc.insecure_channel(args.serverUrl, options=[('grpc.max_receive_message_length', args.maxReceiveSizeMB * 1024 * 1024)])

    return channel

Collect the service URL (server) and credentials (configFile) in tts_client.go

func main() {

    // collect arguments
    parser := argparse.NewParser("tts_client", "Use Nuance TTS to vocalize text")
    server := parser.String("s", "server", &argparse.Options{
        Default: "tts.api.nuance.com:443",
        Help:    "server host:port",
    })
    . . . 
    configFile := parser.String("c", "configFile", &argparse.Options{
        Default: "config.json",
        Help:    "config file containing client credentials (client_id and client_secret)",

Then call authenticate.go to generate and validate the token using the values from config.json

package main

import (
    "encoding/json"
    "errors"
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
    "net/url"
    "os"
    "strings"
    "time"
)

const (
    TokenCache  = "token.cache"
    TokenMaxAge = 59 // minutes
    GrantType   = "client_credentials"
    Scope       = "tts"
)

type Token struct {
    AccessToken string `json:"access_token"`
    ExpiresIn   int    `json:"expires_in"`
    Scope       string `json:"scope"`
    TokenType   string `json:"bearer"`
}

func (t *Token) String(pretty bool) string {
    var str []byte
    var err error

    if pretty {
        str, _ = json.MarshalIndent(t, "", "  ")
    } else {
        str, _ = json.Marshal(t)
    }

    if err != nil {
        log.Printf("Error marshalling token to json: %s", err)
    }

    return string(str)
}

type Authenticator struct {
    config Config
    token  *Token
}

func (a *Authenticator) generateToken() (*Token, error) {
    a.token = nil

    body := strings.NewReader(fmt.Sprintf("grant_type=%s&scope=%s", GrantType, Scope))
    req, err := http.NewRequest("POST", a.config.TokenURL, )
    if err != nil {
        return nil, err
    }

    req.SetBasicAuth(url.QueryEscape(a.config.ClientID), url.QueryEscape(a.config.ClientSecret))
    req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

    resp, err := http.DefaultClient.Do(req)
    if err != nil {
        return nil, err
    }
    defer resp.Body.Close()

    if resp.StatusCode < 200 || resp.StatusCode >= 300 {
        return nil, errors.New(resp.Status)
    }

    bodyBytes, _ := ioutil.ReadAll(resp.Body)
    t := &Token{}
    err = json.Unmarshal(bodyBytes, t)
    if err != nil {
        return nil, err
    }

    a.token = t
    return a.token, nil
}

func (a *Authenticator) isTokenValid() bool {

    // Is token cached?
    info, err := os.Stat(TokenCache)
    if err != nil {
        return false
    }

    // Can token be read from file?
    source, err := ioutil.ReadFile(TokenCache)
    if err != nil {
        return false
    }

    // Are contents of token valid?
    t := &Token{}
    err = json.Unmarshal(source, t)
    if err != nil || len(t.AccessToken) == 0 {
        return false
    }

    // Has token expired?
    lapsed := time.Since(info.ModTime())
    if lapsed > (TokenMaxAge * time.Minute) {
        return false
    }

    // All tests passed
    a.token = t
    return true
}

func (a *Authenticator) cacheToken() {
    outputJSON, err := json.MarshalIndent(a.token, "", "  ")
    if err != nil {
        log.Printf("Failed to cache token: %v", err)
        return
    }

    err = ioutil.WriteFile(TokenCache, outputJSON, 0644)
    if err != nil {
        log.Printf("Failed to cache token: %v", err)
    }

    return
}

func (a *Authenticator) Authenticate() (*Token, error) {
    if a.isTokenValid() {
        return a.token, nil
    }

    if _, err := a.generateToken(); err != nil {
        return nil, err
    }

    a.cacheToken()
    return a.token, nil
}

func NewAuthenticator(config Config) *Authenticator {
    a := &Authenticator{
        config: config,
    }
    return a
}

The app creates a secure gRPC channel and authenticates itself to the TTS service by providing the URL of the hosted service and an access token. The URL of the service is tts.api.nuance.com:443

In both these examples, the service URL is passed to the application as an argument.

There are several ways to handle the token that authenticates the application to the TTS service. The code samples show two methods:

Step 3: Import functions

Import functions from stubs

from nuance_tts_pb2 import * 
from nuance_tts_pb2_grpc import *
import (
    . . . 
    pb "./v1"
)

The app imports all functions from the NVC client stubs that were generated in gRPC setup.

Do not edit these stub files.

Step 4: Set synthesis parameters

User sets synthesis parameters in flow.py

# Synthesis request
request = SynthesisRequest()

request.voice.name = "Evan"
request.voice.model = "enhanced"

pcm = PCM(sample_rate_hz=22050)
request.audio_params.audio_format.pcm.CopyFrom(pcm)

request.audio_params.volume_percentage = 80
request.audio_params.speaking_rate_factor = 1.0
request.audio_params.audio_chunk_duration_ms = 2000

request.input.text.text = "This is a test. A very simple test."

request.event_params.send_log_events = True

Users sets synthesis parameters in params.json

{
    "voice": {
      "name": "Zoe-sc",
      "model": "enhanced"
    },
    "audio_params": {
      "audio_format": {
        "AudioFormat": {
          "Pcm": {
            "sample_rate_hz": 22050
          }
        }
      }
    },
    "input": {
      "InputData": {
          "Text": {
              "Text": "Hello world. This is a test."
          }
       }
    },
    "event_params": {
      "send_sentence_marker_events": false,
      "send_word_marker_events": false
    }
  }

The app uses parameters to define how the voice synthesis should be performed. Some synthesis parameters are:

In the Python application, these parameters are set in an input file named flow.py. In the Go application, they are set in a params.json file.

In the next step, these parameters are added to a SynthesisRequest, ready to send to NVC to generate the synthesized speech.

See SynthesisRequest for other parameters that affect synthesis.

Step 5: Request synthesis

Add the items in flow.py to the SynthesisRequest

def run_one_file(file, list_of_requests):
    with create_channel() as channel:
        grpc_client = SynthesizerStub(channel=channel)
        log.info("Running file [%s]" % file)
        log.debug(list_of_requests)

        thread_context.num_synthesis = 0
        thread_context.file = os.path.basename(file)

        for request in list_of_requests:
            if isinstance(request, GetVoicesRequest):
                send_get_voices_request(grpc_client, request)
            elif isinstance(request, SynthesisRequest):
                if args.sendUnary:
                    send_unary_synthesis_request(grpc_client, request)
                else:
                    send_synthesis_request(grpc_client, request)
            elif isinstance(request, (int, float)):
                log.info("Waiting for {} seconds".format(request))
                time.sleep(request)
        log.info("Done running file [%s]" % file)

Read params.json and construct parameters

    paramsFile := parser.String("p", "paramsFile", &argparse.Options{
        Default: "params.json",
        Help:    "file containing tts parameters to customize TTS behavior",
    })
    . . . 
    {
        parameters := initializeSynthesisRequest(*paramsFile, textInput)
        jout, _ := json.MarshalIndent(parameters, "", "  ")
        log.Printf("parameters: %v", string(jout))
        synthesize(ctx, client, parameters, *outFile)
    }

The app defines a SynthesisRequest, including the parameters collected from an input file.

Step 6: Call client stub

Call main client stub

def run_one_file(file, list_of_requests):
    with create_channel() as channel:
        grpc_client = SynthesizerStub(channel=channel)
        log.info("Running file [%s]" % file)
        log.debug(list_of_requests)
    client := pb.NewSynthesizerClient(conn)
    ctx, cancel := CreateChannelContext(&token.AccessToken)

The app must include the location of the NVC service, the authentication token, and where the audio is obtained. See Step 2: Authenticate and connect.

Using this information, the app calls a client stub function or class. In some languages, this stub is defined in the generated client files: in Python it is named SynthesizerStub and in Go it is SynthesizerClient. In other languages, such as Java, you must create your own stub.

Step 7: Receive results

Receive results from NVC

    responses = grpc_client.Synthesize(
        request=request, metadata=metadata)

    for response in responses:
        if response.HasField("audio"):
            log.info("Received audio: %d bytes" % len(response.audio))
            if not received_first_audio_chunk:
                received_first_audio_chunk = True
                latency = time.monotonic() - start
                log.info("First chunk latency: {} seconds".format(latency))
                global total_first_chunk_latency
                total_first_chunk_latency = total_first_chunk_latency + latency
                log.info("Average first-chunk latency (over {} synthesis requests): {} seconds".format(
                    total_synthesis, total_first_chunk_latency/(total_synthesis)))

            if args.saveAudio:
                if args.saveAudioinWave:
                    if request.audio_params.audio_format.HasField("ogg_opus") or request.audio_params.audio_format.HasField("opus"):
                        log.warn("Cannot set to wav format for OggOpus and Opus")
                    else:
                        currentaudiolen += len(response.audio)
                        waveheader = genHeader(sampleRate,bitsPerSample,channels,currentaudiolen,audioformat)
                        audio_file.seek(0,0)
                        audio_file.write(waveheader)
                        audio_file.seek(0,2)
                if audio_file:
                    audio_file.write(response.audio)
            if args.saveAudioChunks:
                if request.audio_params.audio_format.HasField("ogg_opus"):
                    log.warn("Cannot save separate audio chunks for Ogg Opus, ignoring")
                else:
                    num_chunks = num_chunks + 1
                    chunk_file_name = "%s_i%d_s%d_c%d.%s" % (
                        thread_context.file, num_iterations, thread_context.num_synthesis, num_chunks, extension)
                    chunk_audio_file = open(chunk_file_name, "wb")
                    if args.saveAudioinWave:
                        #Adding wav header before writing to audio file
                        waveheader = genHeader(sampleRate,bitsPerSample,channels,len(response.audio),audioformat)
                        chunk_audio_file.write(waveheader)
                    chunk_audio_file.write(response.audio)
                    chunk_audio_file.close()
                    log.info("Wrote audio chunk to %s" % chunk_file_name)
        elif response.HasField("events"):
            log.info("Received events")
            log.info(text_format.MessageToString(response.events))
        else:
            if response.status.code == 200:
                log.info("Received status response: SUCCESS")
            else:
                log.error("Received status response: FAILED")
                log.error("Code: {}, Message: {}".format(response.status.code, response.status.message))
                log.error('Error: {}'.format(response.status.details))

    if args.saveAudio and audio_file:
        audio_file.close()
        log.info("Wrote audio to %s" % audio_file_name)
    outFile := parser.String("o", "outFile", &argparse.Options{
        Required: false,
        Help:     "file to save audio to",
    })
    . . . 
    {
        parameters := initializeSynthesisRequest(*paramsFile, textInput)
        jout, _ := json.MarshalIndent(parameters, "", "  ")
        log.Printf("parameters: %v", string(jout))
        synthesize(ctx, client, parameters, *outFile)
    }

Finally the app receives the results from the NVC engine, in these examples saving the synthesized speech to a file.

In the Python app, the user requests an audio file on the command line with --SaveAudio (for a *.pcm file) or --SaveAudio --SaveAudioinWave (for a *.wav file). The filename is generated by the application, for example flow.py_i1_s1.wav.

This example shows a standard (streamed) synthesis response. For unary synthesis, see the complete sample Python app and Streamed vs. unary response.

In the Go app, the user sets the name of the audio file with the outFile argument.

Get voices

Each SynthesisRequest must include a voice name and model. If you need to know which voices and models are available, issue a GetVoicesRequest.

Python get voices

The client.py app includes a GetVoices request and response

def send_get_voices_request(grpc_client, request):
    log.info("Sending GetVoices request")

    client_span = None
    get_voices_span = None
    metadata = []

    if args.jaeger:
        log.debug("Injecting Jaeger span context into request")
        client_span = tracer.start_span("Client.gRPC")
        get_voices_span = tracer.start_span(
            "Client.GetVoices", child_of=client_span)
        carrier = dict()
        tracer.inject(get_voices_span.context,
                      opentracing.propagation.Format.TEXT_MAP, carrier)
        metadata.append(('uber-trace-id', carrier['uber-trace-id']))

    response = grpc_client.GetVoices(request=request, metadata=metadata)

    log.info(text_format.MessageToString(response))

    if get_voices_span:
        get_voices_span.finish()
    if client_span:
        client_span.finish()

In the sample Python application, client.py returns the voices available from the NVC server. You specify the GetVoices parameters in flow.py, for example to request just one voice.

# GetVoices request
request = GetVoicesRequest()
request.voice.name = "Evan"

This returns the information about that voice:

2020-03-10 11:48:32,377 INFO : Sending GetVoices request
2020-03-10 11:48:32,696 INFO : voices {
  name: "Evan"
  model: "enhanced"
  language: "en-us"
  gender: MALE
  sample_rate_hz: 22050
  language_tlw: "enu"
}

Or flow.py can request just voices from a specific language

# GetVoices request
request = GetVoicesRequest()
request.voice.language = "en-us"

This returns information about all American English voices.

2020-03-10 12:16:19,897 INFO : Sending GetVoices request
2020-03-10 12:16:20,065 INFO : voices {
  name: "Evan"
  model: "enhanced"
  language: "en-us"
  gender: MALE
  sample_rate_hz: 22050
  language_tlw: "enu"
}
voices {
  name: "Nathan"
  model: "enhanced"
  language: "en-us"
  gender: MALE
  sample_rate_hz: 22050
  language_tlw: "enu"
}
voices {
  name: "Zoe-Sc"
  model: "enhanced"
  language: "en-us"
  gender: FEMALE
  sample_rate_hz: 22050
  language_tlw: "enu"
}

If you want to know all voices available on the server, omit all GetVoices parameters in flow.py.

# GetVoices request
request = GetVoicesRequest()

This returns information about all voices.

2020-03-10 11:50:52,860 INFO : Sending GetVoices request
2020-03-10 11:50:53,220 INFO : voices {
  name: "Evan"
  model: "enhanced"
  language: "en-us"
  gender: MALE
  sample_rate_hz: 22050
  language_tlw: "enu"
}
voices {
  name: "Nathan"
  model: "enhanced"
  language: "en-us"
  gender: MALE
  sample_rate_hz: 22050
  language_tlw: "enu"
}
voices {
  name: "Zoe-Sc"
  model: "enhanced"
  language: "en-us"
  gender: FEMALE
  sample_rate_hz: 22050
  language_tlw: "enu"
}
voices {
  name: "Serena"
  model: "enhanced"
  language: "en-gb"
  gender: FEMALE
  sample_rate_hz: 22050
  language_tlw: "eng"
}
. . .
voices {
  name: "Paulina-Ml"
  model: "enhanced"
  language: "es-us"
  gender: FEMALE
  sample_rate_hz: 22050
  language_tlw: "spm"
}

Go get voices

The Go app returns available voices when the -v option is set on the command line

$ go run ./src -h
usage: tts_client [-h|--help] [-s|--server "<value>"] [-i|--textInput
                "<value>"] [-o|--outFile "<value>"] [-c|--configFile
                "<value>"] [-p|--paramsFile "<value>"] [-v|--getVoices]
                Use Nuance  TTS to vocalize text
Arguments:
-h  --help        Print help information
-s  --server      server host:port. Default: tts.api.nuance.com:443
-i  --textInput   text input
-o  --outFile     file to save audio to
-c  --configFile  config file containing client credentials (client_id and
                    client_secret). Default: config.json
-p  --paramsFile  file containing tts parameters to customize TTS behavior.
                    Default: params.json
-v  --getVoices   Specify this flag to retrieve available voices

The tts_client.go app includes a function that performs a basic GetVoicesRequest

func getVoices(ctx context.Context, client pb.SynthesizerClient) {

    req := &pb.GetVoicesRequest{}
    resp, err := client.GetVoices(ctx, req)
    if err != nil {
        log.Printf("Error getting voices: %s", err)
        return
    }

    out, _ := json.MarshalIndent(resp, "", "  ")
    log.Printf("Voices: %s", string(out))
}

In the Go example, GetVoicesRequest is also available as part of the main application. Specify -v (or --getVoices) to retrieve available voices.

$ go run ./src -v
2019/11/01 11:31:29 Voices: {
"voices": [
    {
    "name": "Evan",
    "model": "enhanced",
    "language": "en-us"
    },
    {
    "name": "Zoe-Sc",
    "model": "enhanced",
    "language": "en-us"
    }
]
}

Sample applications

This section contains sample client applications for NVC.

Sample Python app

The client.py application and its input file, flow.py, support all input types and a unary synthesis response

import argparse
import sys
import time
import logging
import grpc
import os
from importlib.machinery import SourceFileLoader
import threading
from google.protobuf import text_format

from nuance_tts_pb2 import * 
from nuance_tts_pb2_grpc import *

thread_context = threading.local()
total_first_chunk_latency = 0
total_synthesis = 0

args = None

# Generates the .wav file header for a given set of parameters
def genHeader(sampleRate, bitsPerSample, channels, datasize, formattype):
    o = bytes("RIFF",'ascii')                                               # (4byte) Marks file as RIFF
    o += (datasize + 36).to_bytes(4,'little')                               # (4byte) File size in bytes excluding this and RIFF marker
    o += bytes("WAVE",'ascii')                                              # (4byte) File type
    o += bytes("fmt ",'ascii')                                              # (4byte) Format Chunk Marker
    o += (16).to_bytes(4,'little')                                          # (4byte) Length of above format data
    o += (formattype).to_bytes(2,'little')                                  # (2byte) Format type (1 - PCM)
    o += (channels).to_bytes(2,'little')                                    # (2byte) Will always be 1 for TTS
    o += (sampleRate).to_bytes(4,'little')                                  # (4byte) 
    o += (sampleRate * channels * bitsPerSample // 8).to_bytes(4,'little')  # (4byte)
    o += (channels * bitsPerSample // 8).to_bytes(2,'little')               # (2byte)
    o += (bitsPerSample).to_bytes(2,'little')                               # (2byte)
    o += bytes("data",'ascii')                                              # (4byte) Data Chunk Marker
    o += (datasize).to_bytes(4,'little')                                    # (4byte) Data size in bytes

    return o

def send_get_voices_request(grpc_client, request):
    log.info("Sending GetVoices request")

    client_span = None
    get_voices_span = None
    metadata = []

    if args.jaeger:
        log.debug("Injecting Jaeger span context into request")
        client_span = tracer.start_span("Client.gRPC")
        get_voices_span = tracer.start_span(
            "Client.GetVoices", child_of=client_span)
        carrier = dict()
        tracer.inject(get_voices_span.context,
                      opentracing.propagation.Format.TEXT_MAP, carrier)
        metadata.append(('uber-trace-id', carrier['uber-trace-id']))

    response = grpc_client.GetVoices(request=request, metadata=metadata)

    log.info(text_format.MessageToString(response))

    if get_voices_span:
        get_voices_span.finish()
    if client_span:
        client_span.finish()

def send_synthesis_request(grpc_client, request, metadata=None):
    log.info("Sending Synthesis request")

    audio_file = None
    audio_file_name = ""
    extension = ""
    waveheader = ""
    sampleRate  = 0
    bitsPerSample = 0
    channels = 1 
    audioformat = 0
    currentaudiolen = 0
    num_chunks = 0
    metadata = []
    client_span = None
    synthesis_span = None
    received_first_audio_chunk = False
    call_credentials = None

    global total_synthesis
    total_synthesis = total_synthesis + 1

    global args

    thread_context.num_synthesis = thread_context.num_synthesis + 1

    if args.saveAudio or args.saveAudioChunks:
        if request.audio_params.audio_format.HasField("pcm"):
            extension = "pcm"
            sampleRate = request.audio_params.audio_format.pcm.sample_rate_hz
            bitsPerSample = 16
            audioformat = 1
        elif request.audio_params.audio_format.HasField("alaw"):
            extension = "alaw"
            bitsPerSample = 8
            sampleRate = 8000
            audioformat = 6
        elif request.audio_params.audio_format.HasField("ulaw"):
            extension = "ulaw"
            bitsPerSample = 8
            sampleRate = 8000
            audioformat = 7
        elif request.audio_params.audio_format.HasField("ogg_opus"):
            extension = "ogg"
        elif request.audio_params.audio_format.HasField("opus"):
            extension = "opus"

        if args.saveAudio:
            if args.saveAudioinWave:
                if request.audio_params.audio_format.HasField("ogg_opus") or request.audio_params.audio_format.HasField("opus"):
                    log.warn("Cannot set to wav format for OggOpus and Opus, ignoring")
                else:
                    extension = "wav"

            if request.audio_params.audio_format.HasField("opus"):
                log.warn("Cannot save whole audio for Opus, ignoring.")
            else:
                audio_file_name = "%s_i%d_s%d.%s" % (
                thread_context.file, num_iterations, thread_context.num_synthesis, extension)
                audio_file = open(audio_file_name, "wb")

        if args.saveAudioChunks:
            if args.saveAudioinWave:
                if request.audio_params.audio_format.HasField("ogg_opus") or request.audio_params.audio_format.HasField("opus"):
                    log.warn("Cannot set to wav format for OggOpus and Opus, ignoring")
                else:
                    extension = "wav"

    if args.nmaid:
        metadata.append(('x-nuance-client-id', args.nmaid))

    if args.jaeger:
        log.debug("Injecting Jaeger span context into request")
        client_span = tracer.start_span("Client.gRPC")
        synthesis_span = tracer.start_span(
            "Client.Synthesize", child_of=client_span)
        carrier = dict()
        tracer.inject(synthesis_span.context,
                      opentracing.propagation.Format.TEXT_MAP, carrier)
        metadata.append(('uber-trace-id', carrier['uber-trace-id']))

    start = time.monotonic()

    responses = grpc_client.Synthesize(
        request=request, metadata=metadata)

    for response in responses:
        if response.HasField("audio"):
            log.info("Received audio: %d bytes" % len(response.audio))
            if not received_first_audio_chunk:
                received_first_audio_chunk = True
                latency = time.monotonic() - start
                log.info("First chunk latency: {} seconds".format(latency))
                global total_first_chunk_latency
                total_first_chunk_latency = total_first_chunk_latency + latency
                log.info("Average first-chunk latency (over {} synthesis requests): {} seconds".format(
                    total_synthesis, total_first_chunk_latency/(total_synthesis)))

            if args.saveAudio:
                if args.saveAudioinWave:
                    if request.audio_params.audio_format.HasField("ogg_opus") or request.audio_params.audio_format.HasField("opus"):
                        log.warn("Cannot set to wav format for OggOpus and Opus")
                    else:
                        currentaudiolen += len(response.audio)
                        waveheader = genHeader(sampleRate,bitsPerSample,channels,currentaudiolen,audioformat)
                        audio_file.seek(0,0)
                        audio_file.write(waveheader)
                        audio_file.seek(0,2)
                if audio_file:
                    audio_file.write(response.audio)
            if args.saveAudioChunks:
                if request.audio_params.audio_format.HasField("ogg_opus"):
                    log.warn("Cannot save separate audio chunks for Ogg Opus, ignoring")
                else:
                    num_chunks = num_chunks + 1
                    chunk_file_name = "%s_i%d_s%d_c%d.%s" % (
                        thread_context.file, num_iterations, thread_context.num_synthesis, num_chunks, extension)
                    chunk_audio_file = open(chunk_file_name, "wb")
                    if args.saveAudioinWave:
                        #Adding wav header before writing to audio file
                        waveheader = genHeader(sampleRate,bitsPerSample,channels,len(response.audio),audioformat)
                        chunk_audio_file.write(waveheader)
                    chunk_audio_file.write(response.audio)
                    chunk_audio_file.close()
                    log.info("Wrote audio chunk to %s" % chunk_file_name)
        elif response.HasField("events"):
            log.info("Received events")
            log.info(text_format.MessageToString(response.events))
        else:
            if response.status.code == 200:
                log.info("Received status response: SUCCESS")
            else:
                log.error("Received status response: FAILED")
                log.error("Code: {}, Message: {}".format(response.status.code, response.status.message))
                log.error('Error: {}'.format(response.status.details))

    if args.saveAudio and audio_file:
        audio_file.close()
        log.info("Wrote audio to %s" % audio_file_name)

    if synthesis_span:
        synthesis_span.finish()
    if client_span:
        client_span.finish()

def send_unary_synthesis_request(grpc_client, request, metadata=None):
    log.info("Sending Unary Synthesis request")

    audio_file = None
    audio_file_name = ""
    extension = ""
    waveheader = ""
    sampleRate  = 0
    bitsPerSample = 0
    channels = 1 
    audioformat = 0
    currentaudiolen = 0
    num_chunks = 0
    metadata = []
    client_span = None
    synthesis_span = None
    received_first_audio_chunk = False
    call_credentials = None

    global total_synthesis
    total_synthesis = total_synthesis + 1

    global args

    thread_context.num_synthesis = thread_context.num_synthesis + 1

    if args.saveAudio or args.saveAudioChunks:
        if request.audio_params.audio_format.HasField("pcm"):
            extension = "pcm"
            sampleRate = request.audio_params.audio_format.pcm.sample_rate_hz
            bitsPerSample = 16
            audioformat = 1
        elif request.audio_params.audio_format.HasField("alaw"):
            extension = "alaw"
            bitsPerSample = 8
            sampleRate = 8000
            audioformat = 6
        elif request.audio_params.audio_format.HasField("ulaw"):
            extension = "ulaw"
            bitsPerSample = 8
            sampleRate = 8000
            audioformat = 7
        elif request.audio_params.audio_format.HasField("ogg_opus"):
            extension = "ogg"
        else:
            extension = "opus"

        if args.saveAudio:
            if args.saveAudioinWave:
                if request.audio_params.audio_format.HasField("ogg_opus") or request.audio_params.audio_format.HasField("opus"):
                    log.warn("Cannot set to wav format for Ogg Opus, ignoring")
                else:
                    extension = "wav"

            if request.audio_params.audio_format.HasField("opus"):
                log.warn("Cannot save whole audio for Opus, ignoring")
            else:
                audio_file_name = "%s_i%d_s%d.%s" % (
                thread_context.file, num_iterations, thread_context.num_synthesis, extension)
                audio_file = open(audio_file_name, "wb")

    if args.nmaid:
        metadata.append(('x-nuance-client-id', args.nmaid))

    if args.jaeger:
        log.debug("Injecting Jaeger span context into request")
        client_span = tracer.start_span("Client.gRPC")
        synthesis_span = tracer.start_span(
            "Client.Synthesize", child_of=client_span)
        carrier = dict()
        tracer.inject(synthesis_span.context,
                      opentracing.propagation.Format.TEXT_MAP, carrier)
        metadata.append(('uber-trace-id', carrier['uber-trace-id']))

    start = time.monotonic()

    response = grpc_client.UnarySynthesize(
        request=request, metadata=metadata)

    if response.status.code == 200:
        log.info("Received audio: %d bytes" % len(response.audio))
        if not received_first_audio_chunk:
            received_first_audio_chunk = True
            latency = time.monotonic() - start
            log.info("First chunk latency: {} seconds".format(latency))
            global total_first_chunk_latency
            total_first_chunk_latency = total_first_chunk_latency + latency
            log.info("Average first-chunk latency (over {} synthesis requests): {} seconds".format(
                total_synthesis, total_first_chunk_latency/(total_synthesis)))

        if args.saveAudio:
            if args.saveAudioinWave:
                if request.audio_params.audio_format.HasField("ogg_opus") or request.audio_params.audio_format.HasField("opus"):
                    log.warn("Cannot save wave format for Opus, ignoring")
                else:
                    currentaudiolen += len(response.audio)
                    waveheader = genHeader(sampleRate,bitsPerSample,channels,currentaudiolen,audioformat)
                    audio_file.seek(0,0)
                    audio_file.write(waveheader)
                    audio_file.seek(0,2)
            if audio_file:
                audio_file.write(response.audio)
        if args.saveAudioChunks:
            if request.audio_params.audio_format.HasField("ogg_opus"):
                log.warn("Cannot save separate audio chunks for Opus, ignoring")
            else:
                num_chunks = num_chunks + 1
                chunk_file_name = "%s_i%d_s%d_c%d.%s" % (
                    thread_context.file, num_iterations, thread_context.num_synthesis, num_chunks, extension)
                chunk_audio_file = open(chunk_file_name, "wb")
                if args.saveAudioinWave:
                    #Adding wav header before writing to audio file
                    waveheader = genHeader(sampleRate,bitsPerSample,channels,len(response.audio),audioformat)
                    chunk_audio_file.write(waveheader)
                chunk_audio_file.write(response.audio)
                chunk_audio_file.close()
                log.info("Wrote audio chunk to %s" % chunk_file_name)
        if response.HasField("events"):
            log.info("Received events")
            log.info(text_format.MessageToString(response.events))
        log.info("Received status response: SUCCESS")
    else:
        if response.HasField("events"):
            log.info("Received events")
            log.info(text_format.MessageToString(response.events))
        log.error("Received status response: FAILED")
        log.error("Code: {}, Message: {}".format(response.status.code, response.status.message))
        log.error('Error: {}'.format(response.status.details))

    if args.saveAudio and audio_file:
        audio_file.close()
        log.info("Wrote audio to %s" % audio_file_name)

    if synthesis_span:
        synthesis_span.finish()
    if client_span:
        client_span.finish()

def parse_args():
    global args
    parser = argparse.ArgumentParser(
        prog="client.py",
        usage="%(prog)s [-options]",
        add_help=False,
        formatter_class=lambda prog: argparse.HelpFormatter(
            prog, max_help_position=45, width=100)
    )

    options = parser.add_argument_group("options")
    options.add_argument("-h", "--help", action="help",
                         help="Show this help message and exit")
    options.add_argument("--nmaid", nargs="?", help=argparse.SUPPRESS)
    options.add_argument("--token", nargs="?", help=argparse.SUPPRESS)
    options.add_argument("-f", "--files", metavar="file", nargs="+",
                         help="List of flow files to execute sequentially, default=['flow.py']", default=['flow.py'])
    options.add_argument("-p", "--parallel", action="store_true",
                         help="Run each flow in a separate thread.")
    options.add_argument("-i", "--iterations", metavar="num", nargs="?",
                         help="Number of times to run the list of files, default=1", default=1, type=int)
    options.add_argument("-s", "--serverUrl", metavar="url", nargs="?",
                         help="NVC server URL, default=localhost:8080", default='localhost:8080')
    options.add_argument("--secure", action="store_true",
                         help="Connect to the server using a secure gRPC channel.")
    options.add_argument("--rootCerts",  metavar="file", nargs="?",
                         help="Root certificates when using secure channel.")
    options.add_argument("--privateKey",  metavar="file", nargs="?",
                         help="Certificate private key when using secure channel.")
    options.add_argument("--certChain",  metavar="file", nargs="?",
                         help="Certificate chain when using secure channel.")
    options.add_argument("--saveAudio", action="store_true",
                         help="Save audio to disk")
    options.add_argument("--saveAudioChunks", action="store_true",
                         help="Save each individual audio chunk to disk")
    options.add_argument("--saveAudioinWave", action="store_true",
                         help="Save each audio to disk in WAVE format")                      
    options.add_argument("--jaeger", metavar="addr", nargs="?", const='udp://localhost:6831',
                         help="Send UDP opentrace spans, default addr=udp://localhost:6831")
    options.add_argument("--sendUnary", action="store_true",
                         help="Receive a single response of audio instead of streams of audio")
    options.add_argument("--maxReceiveSizeMB", metavar="megabytes", nargs="?", help="Maximum length of gRPC server response in megabytes, default=50 MB", default=50, type=int)

    args = parser.parse_args()

def initialize_tracing():
    if args.jaeger:
        print("Enabling Jaeger traces")
        global opentracing
        import opentracing
        import jaeger_client

        from urllib.parse import urlparse
        agent_addr = urlparse(args.jaeger)
        if not agent_addr.netloc:
            raise Exception(
                "invalid jaeger agent address: {}".format(args.jaeger))
        if not agent_addr.hostname:
            raise Exception(
                "missing hostname in jaeger agent address: {}".format(args.jaeger))
        if not agent_addr.port:
            raise Exception(
                "missing port in jaeger agent address: {}".format(args.jaeger))
        tracer_config = {
            'sampler': {
                'type': 'const',
                'param': 1,
            },
            'local_agent': {
                'reporting_host': agent_addr.hostname,
                'reporting_port': agent_addr.port
            },
            'logging': True
        }
        config = jaeger_client.Config(
            config=tracer_config, service_name='NVCClient', validate=True)
        global tracer
        tracer = config.initialize_tracer()

def create_channel():
    call_credentials = None
    channel = None

    if args.token:
        log.debug("Adding CallCredentials with token %s" % args.token)
        call_credentials = grpc.access_token_call_credentials(args.token)

    if args.secure:
        log.debug("Creating secure gRPC channel")
        root_certificates = None
        certificate_chain = None
        private_key = None
        if args.rootCerts:
            log.debug("Adding root certs")
            root_certificates = open(args.rootCerts, 'rb').read()
        if args.certChain:
            log.debug("Adding cert chain")
            certificate_chain = open(args.certChain, 'rb').read()
        if args.privateKey:
            log.debug("Adding private key")
            private_key = open(args.privateKey, 'rb').read()

        channel_credentials = grpc.ssl_channel_credentials(root_certificates=root_certificates, private_key=private_key, certificate_chain=certificate_chain)
        if call_credentials is not None:
            channel_credentials = grpc.composite_channel_credentials(channel_credentials, call_credentials)
        channel = grpc.secure_channel(args.serverUrl, credentials=channel_credentials, options=[('grpc.max_receive_message_length', args.maxReceiveSizeMB * 1024 * 1024)])
    else:
        log.debug("Creating insecure gRPC channel")
        channel = grpc.insecure_channel(args.serverUrl, options=[('grpc.max_receive_message_length', args.maxReceiveSizeMB * 1024 * 1024)])

    return channel

def run_one_file(file, list_of_requests):
    with create_channel() as channel:
        grpc_client = SynthesizerStub(channel=channel)
        log.info("Running file [%s]" % file)
        log.debug(list_of_requests)

        thread_context.num_synthesis = 0
        thread_context.file = os.path.basename(file)

        for request in list_of_requests:
            if isinstance(request, GetVoicesRequest):
                send_get_voices_request(grpc_client, request)
            elif isinstance(request, SynthesisRequest):
                if args.sendUnary:
                    send_unary_synthesis_request(grpc_client, request)
                else:
                    send_synthesis_request(grpc_client, request)
            elif isinstance(request, (int, float)):
                log.info("Waiting for {} seconds".format(request))
                time.sleep(request)
        log.info("Done running file [%s]" % file)

def run():
    parse_args()

    log_level = logging.DEBUG
    global log
    log = logging.getLogger('')
    logging.basicConfig(
        format='%(asctime)s %(levelname)-5s: %(message)s', level=log_level)

    initialize_tracing()

    for i in range(args.iterations):
        global num_iterations
        num_iterations = i + 1
        log.info("Iteration #{}".format(num_iterations))
        threads = []
        for file in args.files:
            absolute_path = os.path.abspath(file)
            module_name = os.path.splitext(absolute_path)[0]
            module = SourceFileLoader(module_name, absolute_path).load_module()

            # module = importlib.import_module(basename)
            if module.list_of_requests == None:
                raise Exception(
                        "Error importing [%s]: variable list_of_requests not defined" % file)
            if args.parallel:
                log.info("Running flows in parallel")
                thread = threading.Thread(target=run_one_file, args=[file, module.list_of_requests])
                threads.append(thread)
                thread.start()
            else:
                run_one_file(file, module.list_of_requests)
        for thread in threads:
            thread.join()
        log.info("Iteration #{} complete".format(num_iterations))

    if total_synthesis > 0:
        log.info("Average first-chunk latency (over {} synthesis requests): {} seconds".format(total_synthesis, total_first_chunk_latency/(total_synthesis)))

    if args.jaeger:
        tracer.close()
        # Need to give time to tracer to flush the spans: https://github.com/jaegertracing/jaeger-client-python/issues/50
        time.sleep(2)
    print("Done")

if __name__ == '__main__':
    run()

This Python app, client.py, includes three types of request and response.

Input file flow.py

The main application file, client.py (shown at the right) calls one or more input files, by default flow.py, to execute the requests.

from nuance_tts_pb2 import * 
from nuance_tts_pb2_grpc import *  
 
list_of_requests = []
 
# GetVoices request
request = GetVoicesRequest()
request.voice.name = "Evan"
 
# Add request to list
list_of_requests.append(request)
 
# Synthesis request
request = SynthesisRequest()
 
request.voice.name = "Evan"
request.voice.model = "enhanced"
 
pcm = PCM(sample_rate_hz=22050)
request.audio_params.audio_format.pcm.CopyFrom(pcm)
 
request.audio_params.volume_percentage = 80
request.audio_params.speaking_rate_factor = 1.0
request.audio_params.audio_chunk_duration_ms = 2000
 
request.input.text.text = "This is a test. A very simple test."
 
request.event_params.send_log_events = True
 
# Optionally specify a user_id 
# request.user_id = "MyApplicationUser"
 
#Add request to list
list_of_requests.append(request)

See Get voices for information about the GetVoices request and response available in this application.

Different input types

To provide SSML input, replace the request.input.text.text line with request.input.ssml.text. This example is synthesized as: "I can speak rather quietly, BUT ALSO VERY LOUDLY."

request.input.ssml.text = '<?xml version="1.0"?><speak 
xmlns="http://www.w3.org/2001/10/synthesis" xml:lang="en-US" version="1.0">
<prosody volume="10">I can speak rather quietly,</prosody>
<prosody volume="90">But also very loudly.</prosody></speak>'

For a tokenized sequence, replace the request.input.text.text line with request.input.tokenized_sequence.tokens.extend([...]). This example is s synthesized as: "The time and date is: ten o'clock, May twenty-sixth, two thousand twenty. My phone number is: one, eight hundred, six eight eight, zero zero six eight."

request.input.tokenized_sequence.tokens.extend([
Token(text="The time and date is."),
Token(control_code=ControlCode(key="tn", value="time")),
Token(text="10:00"),
Token(control_code=ControlCode(key="pause", value="300")),
Token(control_code=ControlCode(key="tn", value="date")),
Token(text="05/26/2020"),
Token(control_code=ControlCode(key="pause", value="300")),
Token(text="My phone number is."),
Token(control_code=ControlCode(key="tn", value="phone")),
Token(text="1-800-688-0068")
])

Run Python app

A simple way to run this app is to create a script file that generates the authentication token and runs the app against the NVC server.

#!/bin/bash 
 
CLIENT_ID="appID%3ANMDPTRIAL_your_name_nuance_com_20190919T190532565840"
SECRET="appID%3ANMDPTRIAL_your_name_nuance_com_20190919T190532565840"
export MY_TOKEN="`curl https://auth.crt.nuance.com/oauth2/token 
-d 'grant_type=client_credentials' -d 'scope=asr dialog nlu tts' 
-d "client_id=$CLIENT_ID" -d "client_secret=$SECRET" 
| python -m json.tool | python -c 'import sys, json; print(json.load(sys.stdin)["access_token"])'`"
 
./client.py --serverUrl 'tts.api.nuance.com:443' --token $MY_TOKEN 
--secure --saveAudio --saveAudioinWave

Many parameters are set in the input file, flow.py, but others may be passed as arguments to the application. To see the arguments, run client.py with the -h option.

$ ./client.py -h
usage: client.py [-options]
 
options:
  -h, --help                   Show this help message and exit
  -f file [file ...], --files file [file ...]
                               List of files to execute sequentially, default flow.py
  -p, --parallel               Run each flow in a separate thread.
  -i [num], --iterations [num] Number of times to run the list of files, default 1
  -s [url], --serverUrl [url]  NVC server URL, default localhost:8080
  --secure                     Connect to the server using a secure gRPC channel
  --rootCerts [file]           Root certificates when using secure channel
  --privateKey [file]          Certificate private key when using secure channel
  --certChain [file]           Certificate chain when using secure channel
  --saveAudio                  Save audio to disk
  --saveAudioChunks            Save each individual audio chunk to disk
  --saveAudioinWave            Save each audio to disk in WAVE format
  --jaeger [addr]              Send UDP opentrace spans, default udp://localhost:6831
  --sendUnary                  Receive a single response of audio 
  --maxReceiveSizeMB [mb]      Maximum length of gRPC server response in MB, default 50

Sample Go app

The Go sample app consists of two config files and three application functions

The config.json file contains your authentication values

{
    "client_id": "appID:<Provide Your Mix App Id>",
    "client_secret": "<Provide Your Mix Client Secret>",
    "token_url": "https://auth.crt.nuance.com/oauth2/token"
}

The params.json file contains your synthesis and other parameters

{
    "voice": {
      "name": "Zoe-sc",
      "model": "enhanced"
    },
    "audio_params": {
      "audio_format": {
        "AudioFormat": {
          "Pcm": {
            "sample_rate_hz": 22050
          }
        }
      }
    },
    "input": {
      "InputData": {
          "Text": {
              "Text": "Hello world. This is a test."
          }
       }
    },
    "event_params": {
      "send_sentence_marker_events": false,
      "send_word_marker_events": false
    }
  }

The src/tts_client.go file is the main application file

package main

import (
    "context"
    "crypto/tls"
    "encoding/json"
    "fmt"
    "io"
    "io/ioutil"
    "log"
    "os"
    "time"

    "google.golang.org/grpc"
    "google.golang.org/grpc/credentials"
    "google.golang.org/grpc/metadata"

    pb "./v1"

    "github.com/akamensky/argparse"
)

func CreateChannelContext(token *string) (context.Context, context.CancelFunc) {
    ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
    ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+*token)
    return ctx, cancel
}

func getVoices(ctx context.Context, client pb.SynthesizerClient) {

    req := &pb.GetVoicesRequest{}
    resp, err := client.GetVoices(ctx, req)
    if err != nil {
        log.Printf("Error getting voices: %s", err)
        return
    }

    out, _ := json.MarshalIndent(resp, "", "  ")
    log.Printf("Voices: %s", string(out))
}

func synthesize(ctx context.Context, client pb.SynthesizerClient, params *pb.SynthesisRequest, pcmFile string) {
    stream, err := client.Synthesize(ctx, params)
    if err != nil {
        log.Fatalf("%v.RouteChat(_) = _, %v", client, err)
    }
    waitc := make(chan struct{})
    go func() {
        bytesWritten := 0
        var ttsAudioOut *os.File

        for {
            resp, err := stream.Recv()
            if err == io.EOF {
                // read done.
                close(waitc)
                return
            }
            if err != nil {
                log.Fatalf("Failed to receive a response : %v", err)
            }
            switch response := resp.Response.(type) {
            case *pb.SynthesisResponse_Status:
                log.Printf("received status")
                out, _ := json.MarshalIndent(response.Status, "", "  ")
                log.Printf("%s", string(out))
            case *pb.SynthesisResponse_Events:
                log.Printf("received events")
                out, _ := json.MarshalIndent(response.Events, "", "  ")
                log.Printf("%s", string(out))
            case *pb.SynthesisResponse_Audio:
                log.Printf("received audio")
                if bytesWritten == 0 {
                    ttsAudioOut, _ = os.Create(pcmFile)
                }
                n, _ := ttsAudioOut.Write(response.Audio)
                bytesWritten += n
            }
        }
    }()
    <-waitc
}

func setInputData(req *pb.SynthesisRequest, textInput *string) {

    // If text input has been provided, overwrite any text provided in the params file
    if textInput != nil && len(*textInput) > 0 {
        req.Input.InputData = &pb.Input_Body{
            Text: &pb.Text{
                Text: *textInput,
            }
        }
    }
}

func initializeSynthesisRequest(paramFile string, textInput *string) *pb.SynthesisRequest {

    // Default voice parameters
    voice := &pb.Voice{
        Name:  "Evan",
        Model: "enhanced",
    }

    // Default audio parameters (22kHz PCM)
    format := &pb.AudioParameters{
        AudioFormat: &pb.AudioFormat{
            AudioFormat: &pb.AudioFormat_Pcm{
                Pcm: &pb.PCM{
                    SampleRateHz: 22050,
                },
            },
        },
    }

    // Default text to synthesize
    input := &pb.Input{
        InputData: &pb.Input_Body{}, // needs to be initialized for json.Marshal to work
    }

    req := &pb.SynthesisRequest{
        Voice:       voice,
        AudioParams: format,
        Input:       input,
    }

    // If textInput has been provided, this overwrites InputData in the params file
    defer setInputData(req, textInput)

    source, err := ioutil.ReadFile(paramFile)
    if err != nil {
        log.Printf("Error reading params file: %s", err)
        return req
    }

    err = json.Unmarshal(source, req)
    if err != nil {
        log.Printf("Error parsing params file: %s", err)
        return req
    }

    return req
}

func main() {

    // Collect arguments
    parser := argparse.NewParser("tts_client", "Use Nuance TTS to vocalize text")
    server := parser.String("s", "server", &argparse.Options{
        Default: "tts.api.nuance.com:443",
        Help:    "server host:port",
    })
    textInput := parser.String("i", "textInput", &argparse.Options{
        Required: false,
        Help:     "text input",
    })
    outFile := parser.String("o", "outFile", &argparse.Options{
        Required: false,
        Help:     "file to save audio to",
    })
    configFile := parser.String("c", "configFile", &argparse.Options{
        Default: "config.json",
        Help:    "config file containing client credentials (client_id and client_secret)",
    })
    paramsFile := parser.String("p", "paramsFile", &argparse.Options{
        Default: "params.json",
        Help:    "file containing tts parameters to customize TTS behavior",
    })
    voices := parser.Flag("v", "getVoices", &argparse.Options{
        Help: "Specify this flag to retrieve available voices",
    })
    err := parser.Parse(os.Args)
    if err != nil {
        fmt.Print(parser.Usage(err))
        os.Exit(1)
    }

    // Import the user's Mix Beta credentials
    config, err := NewConfig(*configFile)
    if err != nil {
        log.Fatalf("Error importing user credentials: %v", err)
        os.Exit(1)
    }

    // Authenticate the user's credentials
    auth := NewAuthenticator(*config)
    token, err := auth.Authenticate()
    if err != nil {
        log.Fatalf("Error authenticating to Mix: %v", err)
        os.Exit(1)
    }

    // Connect to TTS service
    creds := credentials.NewTLS(&tls.Config{})
    if err != nil {
        log.Fatalf("Failed to create TLS credentials %v", err)
        os.Exit(1)
    }
    conn, err := grpc.Dial(*server, grpc.WithTransportCredentials(creds))
    if err != nil {
        log.Fatalf("fail to dial: %v", err)
        os.Exit(1)
    }
    defer conn.Close()

    // Run a synthesis request
    client := pb.NewSynthesizerClient(conn)
    ctx, cancel := CreateChannelContext(&token.AccessToken)
    defer cancel()
    if *voices {
        getVoices(ctx, client)
    } else {
        parameters := initializeSynthesisRequest(*paramsFile, textInput)
        jout, _ := json.MarshalIndent(parameters, "", "  ")
        log.Printf("parameters: %v", string(jout))
        synthesize(ctx, client, parameters, *outFile)
    }
}

The src/authenticate.go file authenticates the app to the TTS service. See Step 2: Authenticate and connect

The src/config.go file handles the authentication values in config.json

package main

import (
    "encoding/json"
    "io/ioutil"
    "log"
)

type Config struct {
    ClientID     string `json:"client_id"`
    ClientSecret string `json:"client_secret"`
    TokenURL     string `json:"token_url"`
}

func (c *Config) LoadConfig(configFile string) (*Config, error) {
    source, err := ioutil.ReadFile(configFile)
    if err != nil {
        return nil, err
    }
    err = json.Unmarshal(source, c)
    if err != nil {
        return nil, err
    }

    return c, nil
}

func (c *Config) String(pretty bool) string {
    var str []byte
    var err error

    if pretty {
        str, _ = json.MarshalIndent(c, "", "  ")
    } else {
        str, _ = json.Marshal(c)
    }

    if err != nil {
        log.Printf("Error marshalling config to json: %s", err)
    }

    return string(str)
}

func NewConfig(configFile string) (*Config, error) {
    config := &Config{}
    _, err := config.LoadConfig(configFile)

    return config, err
}

This Go application consists of these files:

Run Go app

This runs the app with the help option to see the values that you may pass to the application.

$ go run ./src -h
usage: tts_client [-h|--help] [-s|--server "<value>"] [-i|--textInput "<value>"] 
                  [-o|--outFile "<value>"] [-c|--configFile "<value>"] 
                  [-p|--paramsFile "<value>"] [-v|--getVoices]
 
                  Use Nuance Mix TTS to vocalize text
 
Arguments:
-h  --help                    Print help information
-s  --server <server>         Server host:port. Default: tts.api.nuance.com:443
-i  --textInput <textInput>   Text input
-o  --outFile <outFile>       File to save audio to. Default: out.pcm
-c  --configFile <configFile> Config file containing client credentials. Default:  config.json
-p  --paramsFile <paramsFile> File containing TTS parameters. Default: params.json
-v  --getVoices               Specify this flag to retrieve available voices

This runs the app with a SynthesisRequest, generating the audio in out.pcm.

$ go run ./src  -o out.pcm -i "This is a test using params dot json to specify Zoe as the voice"
2019/11/01 11:33:32 parameters: {
"voice": {
    "name": "Zoe-sc",
    "model": "enhanced"
},
"audio_params": {
    "audio_format": {
    "AudioFormat": {
        "Pcm": {
        "sample_rate_hz": 22050
        }
    }
    }
},
"input": {
    "InputData": {
        "Text": 
            "Text": "This is a test using the parameters file to specify Zoe as the voice"
    }
},
"event_params": {}
}
2019/11/01 11:33:33 received audio
2019/11/01 11:33:34 received audio
2019/11/01 11:33:34 received audio
2019/11/01 11:33:34 received audio
2019/11/01 11:33:34 received audio
2019/11/01 11:33:34 received audio
2019/11/01 11:33:34 received audio
2019/11/01 11:33:34 received audio
2019/11/01 11:33:34 received status
2019/11/01 11:33:34 {
"code": 200,
"message": "OK"
}

Reference topics

This section provides more information about topics in the gRPC API.

Status codes

Code Message Indicates
200 Success Synthesis completed successfully.
400 Bad request A malformed or unsupported client request was rejected.
403 Forbidden A restricted voice was requested but you are not authorized to use it.
500 Internal server error An unknown error has occurred on the server.
502 Resource error An error has occurred with a synthesis resource.

Streamed vs. unary response

One request, two possible responses

service Synthesizer {
  rpc Synthesize(SynthesisRequest) returns (stream SynthesisResponse) {} 
  rpc UnarySynthesize(SynthesisRequest) returns (UnarySynthesisResponse {}
. . .
message SynthesisRequest { 
  Voice voice = 1;  
  AudioParameters audio_params = 2; 
  Input input = 3;   
  EventParameters event_params = 4;  
  map<string, string> client_data = 5; 
}

message SynthesisResponse {
  oneof response {
    Status status = 1;   
    Events events = 2;   
    bytes audio = 3;     
  }
}

message UnarySynthesisResponse {  
  Status status = 1;   
  Events events = 2;   
  bytes audio = 3;     
}

NVC offers two types of synthesis response: a streamed response available in SynthesisResponse and a non-streamed response in UnarySynthesisResponse.

The request is the same in both cases: SynthesisRequest specifies a voice, the input text to synthesize, and optional parameters. The response can be either:

See Sample Python app for an application that includes the unary response, activated by a command line flag.

Defaults

The proto file provides the following defaults for messages in SynthesisRequest. Mandatory fields are shown in bold.

                                   
Items in SynthesisRequest Default
    voice (Voice)  
    name Mandatory, e.g. 'Evan'
    model Mandatory, e.g. 'enhanced'
    language Voice's default language
    age_group (EnumAgeGroup) ADULT
    gender (EnumGender) ANY
    audio_params (AudioParameters)  
    audio_format (AudioFormat) PCM 22.5kHz
    volume_percentage 80
    speaking_rate_factor 1.0
    audio_chunk_duration_ms 20000 (20 seconds)
    target_audio_length_ms 0, meaning no maximum duration
    disable_early_emission False: Send audio segments as soon as possible
    input (Input)  
    text (Text) Mandatory: one of text, ssml, or tokenized_sequence
    ssml (SSML)
    tokenized_sequence (TokenizedSequence)
    escape_sequence \! and <ESC>
    resources (SynthesisResource)  
      type (EnumResourceType) USER_DICTIONARY
    ssml_validation_mode (EnumSSMLValidationMode) STRICT
    lid_params (LanguageIdentificationParameters)
      disable False: LID is turned on
      languages Blank, meaning use all available languages
      always_use_ highest_confidence False: Use highest language with any confidence score
    download_params (DownloadParameters)  
      headers Blank
      refuse_cookies False: Accept cookies
      request_timeout_ms NVC server default, usually 30000 (30 seconds)
    event_params (EventParameters)  
    send_sentence_marker_events False: Do not send
    send_word_marker_events False: Do not send
    send_phoneme_marker_events False: Do not send
    send_bookmark_marker_events False: Do not send
    send_paragraph_marker_events False: Do not send
    send_visemes False: Do not send
    send_log_events False: Do not send
    suppress_input False: Include text and URIs in logs
    client_data Blank
    user_id Blank

Synthesis resources

Once you have experimented with basic synthesis, you can add external resources such as user dictionaries and tuning data.

External resources are not currently supported in Nuance-hosted NVC, but you may include inline dictionaries with SynthesisResource - body.

User dictionary

Source user dictionary

[Header]
Language = ENU
[SubHeader]
Content = EDCT_CONTENT_BROAD_NARROWS
Representation = EDCT_REPR_SZZ_STRING
[Data]
zero // #'zi.R+o&U#
addr // #'@.dR+Es#
adm // #@d.'2mI.n$.'stR+e&I.S$n#
[SubHeader]
Content=EDCT_CONTENT_ORTHOGRAPHIC
Representation=EDCT_REPR_SZ_STRING
[Data]
Info      Information
IT        "Information Technology"
DLL       "Dynamic Link Library"
A-level   "advanced level"
Afr       africa
Acc       account
TEL       telephone
Anon      anonymous
AP        "associated press" 

Compiled dictionary referenced in flow.py

request.input.text.text = "I need to find a DLL."

user_dict = SynthesisResource()
user_dict.type = EnumResourceType.USER_DICTIONARY
user_dict.body = open('/path/to/user_dictionary.dcb', 'rb').read()
request.input.resources.extend([user_dict])

A user dictionary alters the default pronunciation of words spoken by NVC. For example, you can define the pronunciation of words from foreign languages, expand special acronyms, and tune the pronunciation of words with unusual spelling.

The sample dictionary shown at the right includes the pronunciation of "zero," the expansion and pronunciation of "addr" and "adm," plus the expansion of several abbreviated words and acronyms.

You may use this dictionary in NVC by referencing it with SynthesisResource - body.

  1. Compile the source dictionary using Nuance Vocalizer studio or its conversion tool, dictcpl. In this example, the resulting compiled file is user_dictionary.dcb.

  2. Reference user_dictionary.dcb in flow.py, which serves as input to the sample client.py application. See the example at the right for the SynthesisResource code that reads the dictionary as a local file.

  3. Run client.py. The audio output is: "I need to find a dynamic link library."

Input to synthesize

Plain text input

Input - Text - text "This is the text to synthesize. It contains plain text only and no control codes."

SSML input

Input - SSML - text - '<?xml version=”1.0”?>
<speak xmlns="http://www.w3.org/2001/10/synthesis" xml:lang="en-US" version="1.0">
This is my normal speaking volume. 
<prosody volume="-50%">I can instead speak rather quietly,</prosody>
<prosody volume="+50%">or also very loudly.</prosody></speak>'

Tokenized sequence input

Input - TokenizedSequence -   +

   Token - text "My name is"  
   Token - ControlCode (key="PAUSE", value="300")  
   Token - text "Jeremiah Jones"  

You provide the text for NVC to synthesize in one of three ways using the Input message:

SSML tags

Generic example

<?xml version="1.0"?>
<speak xmlns="http://www.w3.org/2001/10/synthesis" xml:lang="en-US" version="1.0">
Text before control code. 
<prosody volume="10">Text following or affected by control code.</prosody> 
</speak>

SSML tags may be included when using the input type Input - SSML. These tags indicate how the text segments within the tag should be spoken.

See Control codes to accomplish the same type of control in tokenized sequence input.

NVC supports the following SSML elements and attributes in SSML input. For details about these items, see the W3C specification Speech Synthesis Markup Language Specification Version 1.0 – W3C Recommendation 7 September 2004. Note that NVC does not support all SSML elements and attributes listed in the W3C specification.

xml version

An XML declaration, specifying the XML version, 1.0.

speak

xml declaration and speak

<?xml version="1.0"?>
<speak xmlns="http://www.w3.org/2001/10/synthesis" xml:lang="en-US" version="1.0">
Input text and tags</speak>

The root SSML element. Required. It contains the required attributes, xml:lang and version, and encloses text to be synthesized along with these optional elements: break, mark, p, prosody, say-as, and s.

audio

Inserts a digital audio recording at the current location. Not currently supported in Nuance-hosted NVC.

break

break

His name is <break time="300ms"/> Michael.

Tom lives in New York City. So does John. He\'s at 180 Park Ave. <break strength="none"/> Room 24

Controls pausing between words, overriding the default breaks based on punctuation in the text. The break tag has two optional attributes:

lexicon

Supports loading user dictionaries, rulesets, and so on. Not currently supported in Nuance-hosted NVC.

mark

mark

This bookmark <mark name="bookmark1"/> marks a reference point. 
Another <mark name="bookmark2"/> does the same.

Inserts a bookmark that is returned in the results. The value can be any string.

paragraph or p

paragraph or p

<p>Welcome to Vocalizer.</p>
<p>Vocalizer is a state-of-the-art text to speech system.</p>

Indicates a paragraph break. A paragraph break is equivalent to break strength="x-strong".

prosody

A collection of elements that specify intonation in the generated voice. You may combine multiple elements within the same prosody tag.

prosody - rate

prosody - rate

This is my normal speaking rate. 
<prosody rate="+50%"> But I can speed up the rate.</prosody>
<prosody rate="-25%">Or I can slow it down.</prosody>

Sets the speaking rate as a keyword, a number (0-100), or a relative percentage (+/-n%). The keywords are:

prosody - timbre

prosody - timbre

This is the normal timbre of my voice. 
<prosody timbre="young"> I can sound a bit younger. </prosody> 
<prosody timbre="old" rate="-10%"> Or older and hopefully wiser. </prosody>

Changes the speaking voice to sound bigger and older (lower values) or smaller and younger (higher values). Not supported for all languages. The value is a keyword, a number (50-200, default is 100), or a relative percentage (+/-n%). The keywords are:

prosody - volume

prosody - volume

This is my normal speaking volume. 
<prosody volume="-50%">I can also speak rather quietly,</prosody> 
<prosody volume="+50%"> or also very loudly.</prosody>

Changes the speaking volume. The value is a keyword, a number (0-100), or a relative percentage (+/-n%). The keywords are: silent, x-soft, soft, medium (default), loud, or x-loud.

say-as

say-as

<say-as interpret-as="address">Apt. 7-12, 28 N. Whitney St., Saint Augustine Beach, FL 32084-6715</say-as>

<say-as interpret-as="currency">12USD</say-as>

<say-as interpret-as="date">11/21/2020</say-as>

<say-as interpret-as="name">Care Telecom Ltd</say-as>

<say-as interpret-as="number">1343455</say-as>

<say-as interpret-as="ordinal">12th</say-as>

<say-as interpret-as="phone">1-800-688-0068</say-as>

<say-as interpret-as="raw">app.</say-as>

<say-as interpret-as="sms">CU :-)</say-as>

<say-as interpret-as="spell" format="alphanumeric">a34y - 347</say-as>

<say-as interpret-as="spell" format="strict">a34y - 347</say-as>

<say-as interpret-as="state">FL</say-as>

<say-as interpret-as="streetname">Emerson Rd.</say-as>

<say-as interpret-as="streetnumber">11001-11010</say-as>

<say-as interpret-as="time">10:00</say-as>

<say-as interpret-as="zip">01803</say-as>

Controls how to say specific types of text, using the interpret-as attribute to specify a value and (in some cases) a format. A wide range of input is accepted for most values. The values are:

sentence or s

sentence or s

<s>The wind was a torrent of darkness, among the gusty trees</s>
<s>The moon was a ghostly galleon, tossed upon cloudy seas</s>

Indicates a sentence break. A sentence break is equivalent to break strength="strong".

Control codes

Tokenized sequence structure

SynthesisRequest - Input - TokenizedSequence - 
    Token - text "Text before control code"
    Token - ControlCode (key="code name", value="code value")
    Token - text "Text following or affected by control code"

Generic example

request.input.tokenized_sequence.tokens.extend ([
    Token (text = "Text before control code"),
    Token (control_code=ControlCode (key="code name", value="code value")),
    Token (text = "Text following or affected by control code")
])

Control codes, sometimes known as control sequences, may be included in the input text when using the input type Input - TokenizedSequence. These codes indicate how the text segments following the code should be spoken.

See SSML tags to accomplish the same types of control in SSML input.

Nuance supports the following control codes and values in TokenizedSequence.

audio

Inserts a digital audio recording at the current location. Not currently supported in Nuance-hosted NVC.

eos

eos

Token - text "Tom lives in the U.S."  
Token - ControlCode (key="eos", value="1") 
Token - text "So does John. 180 Park Ave."
Token - ControlCode (key="eos", value="0")
Token - text "Room 24"

Controls end-of-sentence detection. Values are:

To disable automatic end-of-sentence detection for a block of text, use readmode explicit_eos.

lang

lang

Token - text "The name of the song is. " 
Token - ControlCode (key="lang", value="unknown")
Token - text "Mon pays, c'est l'hiver."
Token - ControlCode (key="lang", value="normal")
Token - text "It's a famous Quebec song."

Labels text identified as from an unknown language. Values are:

The control sequence lang unknown labels all the text from that position up to a lang normal or the end of the input as being from an unknown language. NVC then uses its language identification feature on a sentence-by-sentence basis within that region, where for each sentence it determines the language, then switch the synthesis voice to a voice for that language if necessary. The synthesis voice is restored to the original voice at the next lang normal or the end of the synthesis request.

See LanguageIdentificationParameters.

Language identification is only supported for a limited set of languages.

mrk

mrk

Token - ControlCode (key="mrk", value="important")
Token - text "This is an important point"

Inserts a bookmark that is returned in the results. The value can be any name.

pause

pause

Token - text "My name is"  
Token - ControlCode (key="pause", value="300")  
Token - text "Jeremiah Jones"

Inserts a pause of a specified duration in milliseconds. Values from 1 to 65,535.

para

para

Token - text "Introduction to Vocalizer"
Token - ControlCode (key="para")
Token - text "Vocalizer is a state-of-the-art text-to-speech system."

Indicates a paragraph break and implies a sentence break. The difference between this and eos 1 (end of sentence) is that this triggers the delivery of a paragraph mark event.

rate

rate

Token - text "I can"
Token - ControlCode (key="rate", value="75")
Token - text "speed up the rate"
Token - ControlCode (key="rate", value="25")
Token - text "or slow it down"

Sets the speaking rate as a percentage of the default speaking rate. Values are from 1 to 100, with 50 as the default rate.

readmode

readmode

Token - ControlCode (key="readmode", value="sent")
Token - text "Please buy green apples. You can also get pears."
Token - ControlCode (key="readmode", value="char")
Token - text "Apples"
Token - ControlCode (key="readmode", value="word")
Token - text "Please buy green apples."
Token - ControlCode (key="readmode", value="line")
Token - text "Bananas. Low-fat milk. Whole wheat flour."
Token - ControlCode (key="readmode", value="explicit_eos")
Token - text "Bananas. Low-fat milk. Whole wheat flour."

Changes the reading mode from sentence mode (the default) to specialized modes. Values are the modes:

Return to readmode sent after the specialized readme.

rst

Token - ControlCode (key="vol", value="10")
Token - text "The volume is set to a low value"
Token - ControlCode (key="rst")
Token - text "Now it is reset to its default value"

rst

Resets all codes to the default values.

spell

Token - ControlCode (key="tn", value="spell")
Token - ControlCode (key="spell", value="200")
Token - text "a134b"
Token - ControlCode (key="tn", value="normal")

spell

For tn - spell, sets the inter-character pause in milliseconds. Values are from 1 to 65535.

timbre

timbre

Token - ControlCode (key="timbre", value="180")
Token - text "I can sound quite young"
Token - ControlCode (key="timbre", value="50")
Token - text "Or I can sound old and maybe wise."
Token - ControlCode (key="tn", value="normal")

Changes the speaking voice to sound bigger and older (lower values) or smaller and younger (higher values). Values are between 50 and 200, and 100 is typical.

Not supported for all languages.

tn

tn - address

Token - ControlCode (key="tn", value="address")
Token - text "Apt. 7-12, 28 N. Whitney St., Saint Augustine Beach, FL 32084-6715
Token - ControlCode (key="tn", value="normal")  

Full name and address

Token - ControlCode (key="tn", value="name")  
Token - text "Aardvark & Sons Co. Inc."
Token - ControlCode (key="tn", value="address")
Token - text "123 E. Forest Ave., Portland, ME 04103
Token - ControlCode (key="tn", value="normal")  

Guides text normalization. Values are the different types of text. After applying the normalization mode, apply another code or return to normal.

tn - address

Provides optimal reading for complete postal addresses.

Do not include the name portion of the address to avoid undesired expansions of name-specific abbreviations. Instead, include the name in a separate tn - name section prior to the tn - address.

For example, the full name and address at the right is read as: "Aardvark and Sons Company Incorporated, one two three East Forest Avenue, Portland, Maine, zero four one zero three."

tn - alphanumeric

An alias of tn - spell:alphanumeric.

tn - boolean

tn - boolean

Token - ControlCode (key="tn", value="boolean")  
Token - text = "true"
Token - ControlCode (key="tn", value="normal")  

Reads boolean values (true, false, yes, no) by spelling them out. This example spells out "T R U E."

tn - cardinal

An alias of tn - number.

tn - characters

tn - currency

Token - ControlCode (key="tn", value="currency")  
Token - text = "123.45USD"
Token - ControlCode (key="tn", value="normal")  

An alias of tn - spell:alphanumeric.

tn - currency

Reads text as currency. For example, "123.45USD" is read as "one hundred twenty three U S dollars and forty five cents."

tn - date

tn - date

Token - ControlCode (key="tn", value="date")  
Token - text = "11/21/1984"
Token - ControlCode (key="tn", value="normal")  

Reads text as a date. For example, "11/21/1984" is read as "November twenty-first, nineteen eighty four."

The precise output is determined by the voice, and ambiguous dates are interpreted according to the conventions of the voice's locale. For example, "05/12/2020" is read by an American English voice as "May twelfth two thousand twenty" and by a British English voice as "the fifth of December two thousand and twenty."

tn - digits

An alias for number.

tn - name

Token - ControlCode (key="tn", value="name")  
Token - text = "Care Telecom Ltd"

tn - name

Gives correct reading of names.

tn - number

tn - number

Token - ControlCode (key="tn", value="number")  
Token - text = "1343455"
Token - ControlCode (key="tn", v="normal")  

Reads the text as cardinal numbers, or digits. For example "1343455" is read as "one million three hundred forty three thousand four hundred and fifty five."

tn - normal

tn - ordinal

Token - ControlCode (key="tn", value="ordinal")  
Token - text "12th"
Token - ControlCode (key="tn", value="normal")  

Returns to generic normalization following a text fragment that is normalized in a special way. All the examples in this tn section include tn - normal following the specific normalization segment.

tm - ordinal

Reads positional numbers such as 1st, 2nd, 3rd, and so on.

tn - phone

Token - ControlCode (key="tn", value="phone")  
Token - text = "1-H800-688-0068"
Token - ControlCode (key="tn", value="normal")  

tn - phone

Reads telephone numbers. For example, "1-800-688-0068" is read as "One, eight hundred, six eight eight, zero zero six eight."

tn - raw

Token - ControlCode (key="tn", value="raw")  
Token - text = "app."
Token - ControlCode (key="tn", value="normal")  

tn - raw

Provides a literal reading of the text, such as blocking undesired abbreviation expansion. It operates principally on the abbreviations and acronyms but may impact the surrounding text as well.

tn - sms

tn - sms

Token - ControlCode (key="tn", value="sms")  
Token - text = "ttyl, James, :-)"
Token - ControlCode (key="tn", value="normal")  

Gives short message service (SMS) reading. For example, "ttyl, James, :-)" is read as "Talk to you later, James, smiley happy."

tn - spell:alphanumeric

tn - spell:alphanumeric

Token - ControlCode (key="tn", value="spell:alphanumeric")  
Token - text = "a34y - 347"
Token - ControlCode (key="tn", value="normal") 

Spells out all alphabetic and numeric characters, but does not read white space, special characters, and punctuation marks. For example, "a34y - 347" is pronounced "A three four Y, three four seven."

For both types of spell normalization, characters with accents are indicated. For example: "café" is spoken as "C A F E acute" and "Abc" is spoken as "Upper case A B C."

tn - spell:strict

tn - spell:strict

Token - ControlCode (key="tn", value="spell:strict")  
Token - text = "a34y - 347"
Token - ControlCode (key="tn", value="normal") 

Spells out all characters, including white space, special characters, and punctuation marks.

For example, "a34y - 347" is pronounced "A three four Y, space hyphen space, three four seven."

tn - state

tn - state

Token - ControlCode (key="tn", value="state")  
Token - text "FL"
Token - ControlCode (key="tn", value="normal") 

Expands and pronounces state, city, and province names and abbreviations. Not supported for all languages.

tn - streetname

tn - streetname

Token - ControlCode (key="tn", value="streetname")  
Token - text = "Emerson Rd."
Token - ControlCode (key="tn", value="normal") 

Reads street names and abbreviations. Not supported for all languages.

tn - telephone

tn - time

Token - ControlCode (key="tn", value="time")  
Token - text = "10:00"
Token - ControlCode (key="tn", value="normal") 

An alias of tn - phone.

tn - time

Gives a time of day reading. For example, 10:00 is pronounced "ten o'clock."

tn - zip

Token - ControlCode (key="tn", value="zip")  
Token - text = "01803"
Token - ControlCode (key="tn", value="normal") 

tn- zip

Reads US zip codes. Supported for American English only.

voice (control code)

voice

Token - ControlCode (key="voice", value="samantha")
Token - text "Hello, this is Samantha. "
Token - ControlCode (key="voice", value="tom")
Token - text "Hello, this is Tom."

Changes the speaking voice, which also forces a sentence break. Values are the voices within the request.

vol

vol

Token - text "I can"
Token - ControlCode (key="vol", value="10")
Token - text "speak rather quietly,"
Token - ControlCode (key="vol", value="90")
Token - text "but also very loudly."

Changes the volume as a percentage of maximum volume. Values are from 0 (silent) to 100 (maximum volume). The default is typically 80.

wait

wait

Token - ControlCode (key="wait", value="2")
Token - text "There will be a short wait period after this sentence."  
Token - ControlCode (key="wait", value="9") 
Token - text "This sentence will be followed by a long wait. Did you notice the difference?"

The end-of-sentence pause duration. Values are from 0 to 9, where the pause is 200 milliseconds multiplied by the value.

gRPC API

NVC provides a protocol buffer file, nuance_tts.proto, to define Nuance's TTS service for gRPC. This file contains the building blocks of your voice synthesis applications.

Once you have transformed the proto file into functions and classes in your programming language using gRPC tools, you can call these functions from your application to set parameters, request synthesis, and receive the resulting audio.

See Client app development for scenarios and sample apps in Python and Go. For other languages, consult the gRPC and Protocol Buffer documentation:

Field names in proto and stub files

In this section, the names of the fields are shown as they appear in the proto files. To see how they are generated in your programming language, consult your generated files. For example:

Proto file Python Go Java
audio_params audio_params AudioParams audioParams or getAudioParams
language language Language language or getLanguage

For details about how proto file terms are transformed in the different programming languages, see the Protocol Buffers documentation for:

Proto file structure

Structure of nuance_tts.proto

Synthesizer
    Get Voices
        GetVoicesRequest
        GetVoicesResponse
    Synthesize
        SynthesisRequest
        SynthesisResponse
    UnarySynthesize
        SynthesisRequest
        UnarySynthesisResponse

GetVoicesRequest / GetVoicesResponse
    Voice
        EnumAgeGroup
        EnumGender
        voice fields

SynthesisRequest
    Voice
        voice fields
    AudioParameters
        audio parm fields
        AudioFormat
            audio format fields
            OggOpus | Opus
                Opus fields
                EnumVariableBitrate
    Input
        Text
        SSML
        TokenizedSequence
        SynthesisResource
            resource fields
            EnumResourceType
        EnumSSMLValidationMode
        LanguageIdentificationParameters
        DownloadParameters
    EventParameters
        event parm fields
    client_data
    user_id

SynthesisResponse
    Status
    Events
        Event
    audio

UnarySynthesisResponse
    Status
    Events
        Event
    audio

The proto file defines a Synthesizer service with three RPC methods: GetVoices, Synthesize, and UnarySynthesize. Details about each component are referenced by name within the proto file.

This is the structure of the GetVoices request and response:

Proto files: GetVoices

And this shows the Synthesize and UnarySynthesize request and response:

Proto files: Synthesize

Synthesizer

The Synthesizer service offers these functionalities:

Name Request Type Response Type
GetVoices GetVoicesRequest GetVoicesResponse
Synthesize SynthesisRequest SynthesisResponse stream
UnarySynthesize SynthesisRequest UnarySynthesisResponse

GetVoicesRequest

Get voices request/response code

def create_get_voices_request(name=None, model=None, language=None, sample_rate=0):
    request = nuance_tts_pb2.GetVoicesRequest()
    if name:
        request.voice.name = name
    if model:
        request.voice.model = model
    if language:
        request.voice.language = language
    request.voice.sample_rate_hz=sample_rate
. . .     
        for voice in response.voices:
            print("Voice: %s, Model: %s, Language: %s, Sample rate: %d" % (voice.name, voice.model, voice.language, voice.sample_rate_hz))
func getVoices(ctx context.Context, client pb.SynthesizerClient) {

    req := &pb.GetVoicesRequest{}
    resp, err := client.GetVoices(ctx, req)
    if err != nil {
        log.Printf("Error getting voices: %s", err)
        return
    }

    out, _ := json.MarshalIndent(resp, "", "  ")
    log.Printf("Voices: %s", string(out))
}

Sample response

Voice: Evan, Model: enhanced, Language: en-us, Sample rate: 22050
Voice: Zoe-Sc, Model: enhanced, Language: en-us, Sample rate: 22050                
2019/11/01 11:31:29 Voices: {
"voices": [
    {
    "name": "Evan",
    "model": "enhanced",
    "language": "en-us"
    },
    {
    "name": "Zoe-Sc",
    "model": "enhanced",
    "language": "en-us"
    }
]
}

Input message for message for Synthesizer - GetVoices, to query voices available to the client. (Currently available voices are also listed in Speech-to-Text (TTS) voices.) See Get voices for more examples.

Field Type Description
voice Voice Optionally filter the voices to retrieve, e.g. set language to en-US to return only American English voices.

Voice

Input or output message for voices. When sent as input:

When received as output in GetVoicesResponse, it returns the list of available voices.

Field Type Description
name string The voice's name, e.g. 'Evan'. Mandatory for SynthesisRequest.
model string The voice's quality model, e.g. 'enhanced' or 'standard'. Mandatory for SynthesisRequest.
language string IETF language code, e.g. 'en-US'. Some voices may support multiple languages. Default is the voice's default language.
age_group EnumAgeGroup Used only in GetVoicesRequest and GetVoicesResponse, to search for adult or child voices. Ignored otherwise.
gender EnumGender Used only in GetVoicesRequest and GetVoicesResponse, to search for voices with a certain gender. Ignored otherwise.
sample_rate_hz uint32 Used only in GetVoicesRequest and GetVoicesResponse, to search for a certain native sample rate. Ignored otherwise.
language_tlw string Used only in GetVoicesRequest and GetVoicesResponse. Three-letter language code (e.g. 'enu' for American English) for configuring language identification in Input.
restricted bool Used only in GetVoicesResponse, to identify restricted voices (restricted: true). These are custom voices available only to specific customers. Default is false, meaning the voice is public.

EnumAgeGroup

Input field for GetVoicesRequest or output field for GetVoicesResponse, specifying whether the voice uses its adult or child version, if available. Included in Voice.

Name Number Description
ADULT 0 Adult voice. Default for GetVoicesRequest.
CHILD 1 Child voice.

EnumGender

Input field for GetVoicesRequest or output field for GetVoicesResponse, specifying gender for voices that support multiple genders. Included in Voice.

Name Number Description
ANY 0 Any gender voice. Default for GetVoicesRequest.
MALE 1 Male voice.
FEMALE 2 Female voice.
NEUTRAL 3 Neutral gender voice.

GetVoicesResponse

Output message for Synthesizer - GetVoices. Includes a list of voices that matched the input criteria, if any.

Field Type Description
voices Voice Repeated. Voices and characteristics returned.

SynthesisRequest

Synthesis request

def create_synthesis_request(name, model, body, uri, sample_rate, send_log_events=False):
    request = nuance_tts_pb2.SynthesisRequest()

    request.voice.name = name
    request.voice.model = model

    pcm = nuance_tts_pb2.PCM(sample_rate_hz=sample_rate)
    request.audio_params.audio_format.pcm.CopyFrom(pcm)

    if body:
        request.input.text.text = body
    elif uri:
        request.input.text.uri = uri
    elif ssml:
        request.input.ssml.text = ssml
    else:
        raise RuntimeError("Input text or uri must be defined.")

    request.event_params.send_log_events = send_log_events

    return request
func initializeSynthesisRequest(paramFile string, textInput *string) *pb.SynthesisRequest {

    // Default voice parameters
    voice := &pb.Voice{
        Name:  "Evan",
        Model: "enhanced",
    }

    // Default audio parameters (22kHz PCM)
    format := &pb.AudioParameters{
        AudioFormat: &pb.AudioFormat{
            AudioFormat: &pb.AudioFormat_Pcm{
                Pcm: &pb.PCM{
                    SampleRateHz: 22050,
                },
            },
        },
    }

    // Default text to synthesize
    input := &pb.Input{
        Type:      "text/plain;charset=utf-8",
        InputData: &pb.Input_Body{}, // needs to be initialized for json.Marshal to work
    }

    req := &pb.SynthesisRequest{
        Voice:       voice,
        AudioParams: format,
        Input:       input,
    }

Input message for Synthesizer - Synthesize. Specifies input text, audio parameters, and events to subscribe to, in exchange for synthesized audio.

Field Type Description
voice Voice The voice to use for audio synthesis. Mandatory.
audio_params AudioParameters Output audio parameters, such as encoding and volume.
input Input Input text to synthesize, tuning data, etc. Mandatory.
event_params EventParameters Markers and other info to include in server events returned during synthesis.
client_data string,string Repeated. Client-supplied key-value pairs to inject into the call log.
user_id string Identifies a specific user within the application.

AudioParameters

Input message for audio-related parameters during synthesis, including encoding, volume, and audio length. Included in SynthesisRequest.

Field Type Description
audio_format AudioFormat Audio encoding. Default PCM 22.5kHz.
volume_percentage uint32 Volume amplitude, from 0 to 100. Default 80.
speaking_rate_factor float Speaking rate, from 0 to 2.0. Default 1.0.
audio_chunk_duration_ms uint32 Maximum duration, in ms, of an audio chunk delivered to the client, from 1 to 60000. Default is 20000 (20 seconds). When this parameter is large enough (for example, 20 or 30 seconds), each audio chunk contains an audible segment surrounded by silence.
target_audio_length_ms uint32 Maximum duration, in ms, of synthesized audio. When greater than 0, the server stops ongoing synthesis at the first sentence end, or silence, closest to the value.
disable_early_emission bool By default, audio segments are emitted as soon as possible, even if they are not audible. This behavior may be disabled.

AudioFormat

Input message for audio encoding of synthesized text. Included in AudioParameters.

Field Type Description
pcm PCM Signed 16-bit little endian PCM, 8kHz or 16kHz. Default.
alaw ALaw G.711 A-law, 8kHz.
ulaw ULaw G.711 Mu-law, 8kHz.
ogg_opus OggOpus Ogg Opus, 8kHz,16kHz, or 24 kHz.
opus Opus Opus, 8kHz, 16kHZ, or 24 kHz. The audio will be sent one Opus packet at a time.

PCM

Input message defining PCM sample rate. Included in AudioFormat.

Field Type Description
sample_rate_hz uint32 Output sample rate: 8000, 16000, 22050 (default), 24000.

ALaw

Input message defining A-law audio format. Included in AudioFormat. G.711 audio formats are set to 8kHz.

ULaw

Input message defining Mu-law audio format. Included in AudioFormat. G.711 audio formats are set to 8kHz.

OggOpus

Input message defining Ogg Opus output rate. Included in AudioFormat.

Field Type Description
sample_rate_hz uint32 Output sample rate. Supported values: 8000, 16000, 24000 Hz.
bit_rate_bps uint32 Valid range is 500 to 256000 bps. Default 28000 bps.
max_frame_duration_ms float Opus frame size, in ms: 2.5, 5, 10, 20, 40, 60. Default 20.
complexity uint32 Computational complexity. A complexity of 0 means the codec default.
vbr EnumVariableBitrate Variable bitrate. On by default.

Opus

Input message defining Opus output rate. Included in AudioFormat.

Field Type Description
sample_rate_hz uint32 Output sample rate. Supported values: 8000, 16000, 24000 Hz.
bit_rate_bps uint32 Valid range is 500 to 256000 bps. Default 28000 bps.
max_frame_duration_ms float Opus frame size, in ms: 2.5, 5, 10, 20, 40, 60. Default 20.
complexity uint32 Computational complexity. A complexity of 0 means the codec default.
vbr EnumVariableBitrate Variable bitrate. On by default.

EnumVariableBitrate

Settings for variable bitrate. Included in OggOpus and Opus. Turned on by default.

Name Number Description
VARIABLE_BITRATE_ON 0 Use variable bitrate. Default.
VARIABLE_BITRATE_OFF 1 Do not use variable bitrate.
VARIABLE_BITRATE_CONSTRAINED 2 Use constrained variable bitrate.

Input

Input message containing text to synthesize and synthesis parameters, including tuning data, etc. Included in SynthesisRequest. The type of input may be plain text, SSML, or a sequence of plain text and Nuance control codes.

Field Type Description
text Text Plain text input.
ssml SSML SSML input, including text and SSML elements.
tokenized_sequence TokenizedSequence Sequence of text and Nuance control codes.
resources SynthesisResource Repeated. Synthesis resources (user dictionaries, rulesets, etc.) to tune synthesized audio. Default blank.
lid_params LanguageIdentification Parameters LID parameters.
download_params DownloadParameters Remote file download parameters.

Text

Plain text input

request.input.text.text = "Your coffee will be ready in 5 minutes"

Input message for synthesizing plain text. The encoding must be UTF-8.

Field Type Description
text string Plain input text in UTF-8 encoding.
uri string Remote URI to the plain input text. Not currently supported in Nuance-hosted NVC.

SSML

SSML input

request.input.ssml.text = '<?xml version="1.0"?><speak xmlns="http://www.w3.org/2001/10/synthesis" 
xml:lang=”en-US” version="1.0"><prosody volume="10">I can speak rather quietly,</prosody>
<prosody volume="90">But also very loudly.</prosody></speak>'

Input message for synthesizing SSML input. See SSML tags for a list of supported elements and examples.

Field Type Description
text string SSML input text and elements.
uri string Remote URI to the SSML input text. Not currently supported in Nuance-hosted NVC.
ssml_validation_mode EnumSSMLValidationMode SSML validation mode. Default STRICT.

EnumSSMLValidationMode

SSML validation mode when using SSML input. Included in SSML. Strict by default but can be relaxed.

Name Number Description
STRICT 0 Strict SSL validation. Default.
WARN 1 Give warning only.
NONE 2 Do not validate.

TokenizedSequence

Tokenized sequence added in input file, flow.py

request.input.tokenized_sequence.tokens.extend([
  Token(control_code=ControlCode(key="vol", value="10")),
  Token(text="I can speak rather quietly,"), 
  Token(control_code=ControlCode(key="vol", value="90")),
  Token(text="but also very loudly.")
])

Input message for synthesizing a sequence of plain text and Nuance control codes.

Field Type Description
tokens Token Repeated. Sequence of text and control codes.

Token

The unit when using TokenizedSequence for input. Each token can be either plain text or a Nuance control code. See Control codes for a list of supported codes and examples.

Field Type Description
text string Plain input text.
control_code ControlCode Nuance control code.

ControlCode

Nuance control code that specifies how text should be spoken, similarly to SSML.

Field Type Description
key string Name of the control code, e.g. 'pause'
value string Value of the control code.

SynthesisResource

Input message specifying the type of file to tune the synthesized output and its location or contents. Included in Input.

Field Type Description
type EnumResourceType Resource type, e.g. user dictionary, etc. Default USER_DICTIONARY.
uri string URI to the remote resource. Currently disabled.
body bytes For EnumResourceType USER_DICTIONARY, the contents of the file. See User dictionary for an example.

EnumResourceType

The type of synthesis resource to tune the output. Included in SynthesisResource. User dictionaries provide custom pronunciations, rulesets apply search-and-replace rules to input text, and ActivePrompt databases help tune synthesized audio under certain conditions, using Nuance Vocalizer Studio.

Only USER_DICTIONARY is currently supported in Nuance-hosted NVC, with the dictionary specified inline.

Name Number Description
USER_DICTIONARY 0 User dictionary (application/edct-bin-dictionary). Default.
TEXT_USER_RULESET 1 Text user ruleset (application/x-vocalizer-rettt+text).
BINARY_USER_RULESET 2 Binary user ruleset (application/x-vocalizer-rettt+bin).
ACTIVEPROMPT_DB 3 ActivePrompt database (application/x-vocalizer/activeprompt-db).
ACTIVEPROMPT_DB_AUTO 4 ActivePrompt database with automatic insertion (application/x-vocalizer/activeprompt-db;mode=automatic).
SYSTEM_DICTIONARY 5 Nuance system dictionary (application/sdct-bin-dictionary).

LanguageIdentificationParameters

Input message controlling the language identifier. Included in Input. The language identifier runs on input blocks labeled with the control code lang unknown or SSML xml:lang="unknown". The language identifier automatically restricts the matched languages to the installed voices. This limits the permissible languages, and also sets the order of precedence (first to last) when they have equal confidence scores.

Field Type Description
disable bool Whether to disable language identification. Turned on by default.
languages string Repeated. List of three-letter language codes (e.g. enu, frc, spm) to restrict language identification results, in order of precedence. Use GetVoicesRequest - Voice - language_tlw to obtain the three-letter codes. Default blank.
always_use_highest_confidence bool If enabled, language identification always chooses the language with the highest confidence score, even if the score is low. Default false, meaning use language with any confidence.

DownloadParameters

Input message containing parameters for remote file download, whether for input text (Input.uri) or a SynthesisResource (SynthesisResource.uri). Included in Input.

Field Type Description
headers string,string Repeated. HTTP header name,value pairs to include in outgoing requests. Supported headers: max_age, max_stale.
request_timeout_ms uint32 Request timeout in ms. Default (0) means server default, usually 30000 (30 seconds).
refuse_cookies bool Whether to disable cookies. By default, HTTP requests accept cookies.

EventParameters

Input message that defines event subscription parameters. Included in SynthesisRequest. Events that are requested are sent throughout the SynthesisResponse stream, when generated. Marker events can send events as certain parts of the synthesized audio are reached, for example, at the end of a word, sentence, or user-defined bookmark.

Log events are produced throughout a synthesis request for events such as a voice loaded by the server or an audio chunk being ready to send.

Field Type Description
send_sentence_marker_events bool Sentence marker. Default: do not send.
send_word_marker_events bool Word marker. Default: do not send.
send_phoneme_marker_events bool Phoneme marker. Default: do not send.
send_bookmark_marker_events bool Bookmark marker. Default: do not send.
send_paragraph_marker_events bool Paragraph marker. Default: do not send.
send_visemes bool Lipsync information. Default: do not send.
send_log_events bool Whether to log events during synthesis. By default, logging is turned off.
suppress_input bool Whether to omit input text and URIs from log events. By default, these items are included.

SynthesisResponse

Response to synthesis request

       try:
            if args.output_audio_file:
                audio_file = open(args.output_audio_file, "wb")
            for response in stream_in:
                if response.HasField("audio"):
                    print("Received audio: %d bytes" % len(response.audio))
                    if(audio_file):
                        audio_file.write(response.audio)
                elif response.HasField("events"):
                    print("Received events")
                    print(text_format.MessageToString(response.events))
                else:
                    if response.status.code == 200:
                        print("Received status response: SUCCESS")
                    else:
                        print("Received status response: FAILED")
                        print("Code: {}, Message: {}".format(response.status.code, response.status.message))
                        print('Error: {}'.format(response.status.details))
for {
            resp, err := stream.Recv()
            if err == io.EOF {
                // read done.
                close(waitc)
                return
            }
            if err != nil {
                log.Fatalf("Failed to receive a response : %v", err)
            }
            switch response := resp.Response.(type) {
            case *pb.SynthesisResponse_Status:
                log.Printf("received status")
                out, _ := json.MarshalIndent(response.Status, "", "  ")
                log.Printf("%s", string(out))
            case *pb.SynthesisResponse_Events:
                log.Printf("received events")
                out, _ := json.MarshalIndent(response.Events, "", "  ")
                log.Printf("%s", string(out))
            case *pb.SynthesisResponse_Audio:
                log.Printf("received audio")
                if bytesWritten == 0 {
                    ttsAudioOut, _ = os.Create(pcmFile)
                }
                n, _ := ttsAudioOut.Write(response.Audio)
                bytesWritten += n
            }
        }

Response example

Received audio: 36636 bytes
Received audio: 15188 bytes
Received audio: 5432 bytes
Received audio: 13258 bytes
Received audio: 36294 bytes
Received audio: 41650 bytes
Received audio: 24678 bytes
Received audio: 42446 bytes
Received status response: SUCCESS
2019/11/01 11:33:33 received audio
2019/11/01 11:33:34 received audio
2019/11/01 11:33:34 received audio
2019/11/01 11:33:34 received audio
2019/11/01 11:33:34 received audio
2019/11/01 11:33:34 received audio
2019/11/01 11:33:34 received audio
2019/11/01 11:33:34 received audio
2019/11/01 11:33:34 received status
2019/11/01 11:33:34 {
"code": 200,
"message": "OK"

The Synthesizer - Synthesize RPC call returns a stream of SynthesisResponse messages. (See UnarySynthesisResponse for a non-streamed response.) Each response contains one of:

Field Type Description
status Status A status response, indicating completion or failure of the request.
events Events A list of events. See EventParameters for details.
audio bytes The latest audio buffer.

Status

Output message containing a status response, indicating completion or failure of a Synthesize call. Included in SynthesisResponse and UnarySynthesisResponse.

Field Type Description
code uint32 HTTP-style return code: 200, 4xx, or 5xx as appropriate.
message string Brief description of the status.
details string Longer description if available.

Events

Output message defining a container for a list of events. This container is needed because oneof does not allow repeated parameters in Protobuf. Included in SynthesisResponse and UnarySynthesisResponse.

Field Type Description
events Event Repeated. One or more events.

Event

Output message defining an event message. Included in Events. See EventParameters for details.

Field Type Description
name string Either "Markers" or the name of the event in the case of a Log Event.
values string,string Repeated. Key-value data relevant to the current event.

UnarySynthesisResponse

The Synthesizer - UnarySynthesize RPC call returns a single UnarySynthesisResponse message. It is similar to SynthesisResponse but includes all the information instead of a single type of response. The response contains:

Field Type Description
status Status A status response, indicating completion or failure of the request.
events Events A list of events. See EventParameters for details.
audio bytes Audio buffer of the synthesized text.

Scalar value types

The data types in the proto files are mapped to equivalent types in the generated client stub files.

Proto Notes C++ Java Python
double double double float
float float float float
int32 Uses variable-length encoding. Inefficient for encoding negative numbers. If your field is likely to have negative values, use sint32 instead. int32 int int
int64 Uses variable-length encoding. Inefficient for encoding negative numbers. If your field is likely to have negative values, use sint64 instead. int64 long int/long
uint32 Uses variable-length encoding. uint32 int int/long
uint64 Uses variable-length encoding. uint64 long int/long
sint32 Uses variable-length encoding. Signed int value. These encode negative numbers more efficiently than regular int32s. int32 int int
sint64 Uses variable-length encoding. Signed int value. These encode negative numbers more efficiently than regular int64s. int64 long int/long
fixed32 Always four bytes. More efficient than uint32 if values are often greater than 2^28. uint32 int int
fixed64 Always eight bytes. More efficient than uint64 if values are often greater than 2^56. uint64 long int/long
sfixed32 Always four bytes. int32 int int
sfixed64 Always eight bytes. int64 long int/long
bool bool boolean boolean
string A string must always contain UTF-8 encoded or 7-bit ASCII text. string String str/unicode
bytes May contain any arbitrary sequence of bytes. string ByteString str

Change log

2020-07-??

These changes were made to the TTS API:

2020-06-24

The TTS v1beta1 protocol is deprecated: it is currently being monitored and may be removed in the near future. If you are using v1beta1, we recommend that you upgrade to v1.

2020-05-31

These changes were made to the API and documentation:

2020-04-30

These changes were made to the documentation:

2020-03-31

These changes were made to the API and documentation:

v1beta1 v1
message Input {
  string type = 1;
  oneof input_data {
    string uri = 2;
    string body = 3;
  bytes body_as_bytes = 4;
  }
  string escape_sequence = 5;
}
message Input {
  oneof input_data {
    Text text = 1;
    SSML ssml = 2;
    TokenizedSequence tokenized_sequence = 3;
  }
}

message Text {}
message SSML {}
message TokenizedSequence {}
message Token {}
message ControlCode {}

2020-02-19

These changes were made to the API and documentation:

2020-01-22

These changes were made to the API and documentation:

2019-12-18

These changes were made to the TTSaaS gRPC API documentation:

2019-12-02

These changes were made to the TTSaaS gRPC API documentation:

2019-11-15

Below are changes made to the TTSaaS gRPC API documentation since the initial Beta release: