FAQ

How to get started? In your code, add the following line at the top to each class where you want to use Text To Speech:
using GoodEnough.TextToSpeech;
Then use this method to make your device speak any text:
TTS.Speak("Your text");

Can I test the plugin in Unity Editor? This plugin works only on iOS or tvOS devices. You will need to make a build and start it on an iPhone, iPad or Apple TV device to hear the speech.

Does the plugin require an active Internet connection? No internet is required for the plugin to work.

How many languages are supported by the plugin? The number of supported languages depends on the device and iOS version. Go to Settings->Siri & Search->Language to check how many languages your device supports.

Is it possible to switch voices between the sentences? Yes, you can speak each sentence with a different voice.

Documentation

Synthesizing Speech

public static void Speak(string speechString)

Description
Enqueues an utterance to be spoken using DefaultParameters

Parameters
string speechString - The text to be spoken in the utterance.

Example

TTS.Speak("Hello world!");

public static void Speak(string speechString, string language)

Description
Enqueues an utterance to be spoken using a voice object for the specified language and locale.

Parameters
string speechString - The text to be spoken in the utterance.
string language - A BCP 47 code specifying language and locale for a voice.

Example

TTS.Speak("Hello world!", "en-US");

public static void Speak(string speechString, ISpeechSynthesisVoice voice)

Description
Enqueues an utterance to be spoken using a specific voice object

Parameters
string speechString - The text to be spoken in the utterance.
ISpeechSynthesisVoice voice - The voice used to speak the utterance.

Example

  var voice = TTS.GetVoiceForLanguage("en-US");
  TTS.Speak("Hello world!", voice);

public static void Speak(string speechString, SpeechUtteranceParameters speechUtteranceParameters)

Description
Enqueues an utterance to be spoken with default voice using specific parameters

Parameters
string speechString - The text to be spoken in the utterance.
SpeechUtteranceParameters speechUtteranceParameters - Parameters that affect the speech

Example

  var parameters = new SpeechUtteranceParameters();
  TTS.Speak("Hello world!", parameters);

public static void Speak(SpeechUtterance speechUtterance)

Description
Enqueues an utterance to be spoken with using specific parameters

Parameters
SpeechUtterance speechUtterance - A chunk of text to be spoken, along with parameters that affect its speech.

Example

  var utterance = new SpeechUtterance("Hello world!");
  TTS.Speak(utterance);

Controlling Speech Synthesis

public static bool Continue()

Description
Continues speech from the point at which it left off.

Return value
Returns true if speech has continued, or false otherwise.

Example

TTS.Continue();

public static bool Pause()

Description
Pauses speech at default boundary constraints.

Return value
Returns true if speech has paused, or false otherwise.

Example

TTS.Pause();

public static bool Pause(SpeechBoundary speechBoundary)

Description
Pauses speech at the specified boundary constraint.

Parameters
SpeechBoundary speechBoundary - A constant describing whether speech should pause immediately or only after finishing the word currently being spoken.

Return value
Returns true if speech has paused, or false otherwise.

Example

TTS.Pause(SpeechBoundary.Word);

public static bool Stop()

Description
Stops all speech at default boundary constraints.

Return value
Returns true if speech has stopped, or false otherwise.

Example

TTS.Stop();

public static bool Stop(SpeechBoundary speechBoundary)

Description
Stops all speech at the specified boundary constraint.

Parameters
SpeechBoundary speechBoundary - A constant describing whether speech should stop immediately or only after finishing the word currently being spoken.

Return value
Returns true if speech has stopped, or false otherwise.

Example

TTS.Stop(SpeechBoundary.Word);

Using Voices and Languages

Classes

public interface ISpeechSynthesisVoice

Description
The voice object used to speak the utterance.

Properties

string Identifier
The unique identifier for a voice object.

string Name
The name for a voice object.

string Language
A BCP 47 code identifying the voice’s language and locale.
The locale of a voice reflects regional variations in pronunciation or accent; for example, a voice with code en-US speaks English text with a North American accent, and a voice with code en-AU speaks English text with an Australian accent.

VoiceQuality Quality
The speech quality for a voice object.
Default - he lower quality version of a voice that is usually installed on the device by default.
Enhanced - The higher quality version of a voice that is usually downloaded by the user.

public enum VoiceQuality

Description
The speech quality for a voice object.

Values

Default = 1
The lower quality version of a voice that is usually installed on the device by default.

Enhanced = 2
The higher quality version of a voice that is usually downloaded by the user.

Usages

public static ISpeechSynthesisVoice GetVoiceForLanguage(string language)

Description
Returns a voice object for the specified language and locale.

Parameters
string language - A BCP 47 code specifying language and locale for a voice.

Return value
Returns null if no voice available for the specified language

Example

  var voice = TTS.GetVoiceForLanguage("en-US");
  TTS.Speak("Hello world!", voice);

public static ISpeechSynthesisVoice[] GetAllVoicesForLanguage(string language)

Description
Returns all available voice objects for the specified language and locale.

Parameters
string language - A BCP 47 code specifying language and locale for a voice.

Return value
Returns an empty array if no voice available for the specified language

Example

  var voices = TTS.GetAllVoicesForLanguage("en-US");
  Debug.Log("There are " + voices.Length + "voices available for en-US");

public static ReadOnlyCollection<ISpeechSynthesisVoice> AllAvailableVoices

Description
Returns all available voices.

Return value
Returns a read-only collection of all available voices

Example

  var voices = TTS.AllAvailableVoices;
  Debug.Log("There are " + voices.Count + "voices available on this device");

public static string CurrentLanguageCode

Description
Returns the code for the user’s current locale.

Return value
Returns a string containing BCP 47 language and locale code for the user’s current locale.

Example

  var languageCode = TTS.CurrentLanguageCode;
  Debug.Log("Current language code is " + languageCode);

public static string[] GetAllAvailableLanguages()

Description
Returns all language codes (A BCP 47) for which voices are available.

Return value
Returns an array with all language codes (A BCP 47) for which voices are available.

Example

  var languages = TTS.GetAllAvailableLanguages();
  Debug.Log("There are voices available in " + languages.length + " languages");

Customizing Parameters

Classes

public class SpeechUtterance

Description
A chunk of text to be spoken, along with parameters that affect its speech.

Properties

string SpeechString
The text to be spoken in the utterance.
An utterance’s text cannot be changed once it is created. To speak different text, create a new utterance.

float PitchMultiplier
The baseline pitch at which the utterance will be spoken.
The default pitch is 1.0. Allowed values are in the range from 0.5 (for lower pitch) to 2.0 (for higher pitch).

float PreUtteranceDelay
The amount of time in seconds a speech synthesizer will wait before actually speaking the utterance upon beginning to handle it.
When two or more utterances are spoken, the time between periods when either is audible will be at least the sum of the first utterance’s postUtteranceDelay and the second utterance’s preUtteranceDelay.

float PostUtteranceDelay
The amount of time in seconds a speech synthesizer will wait after the utterance is spoken before handling the next queued utterance.
When two or more utterances are spoken, the time between periods when either is audible will be at least the sum of the first utterance’s postUtteranceDelay and the second utterance’s preUtteranceDelay.

float SpeechRate
The rate at which the utterance will be spoken.
Speech rates are values in the range between UtteranceMinimumSpeechRate and UtteranceMaximumSpeechRate. Lower values correspond to slower speech, and vice versa. The default value is UtteranceDefaultSpeechRate.

ISpeechSynthesisVoice Voice
The voice used to speak the utterance.
The default value is null, which causes the utterance to be spoken in the default voice.

float Volume
The volume used when speaking the utterance.
Allowed values are in the range from 0.0 (silent) to 1.0 (loudest). The default volume is 1.0.

Constructors

public SpeechUtterance(string speechString)
public SpeechUtterance(string speechString, SpeechUtteranceParameters parameters)

public class SpeechUtteranceParameters

Description
Parameters that affect the speech.

Properties

float PitchMultiplier
The baseline pitch at which the utterance will be spoken.
The default pitch is 1.0. Allowed values are in the range from 0.5 (for lower pitch) to 2.0 (for higher pitch).

float PreUtteranceDelay
The amount of time in seconds a speech synthesizer will wait before actually speaking the utterance upon beginning to handle it.
When two or more utterances are spoken, the time between periods when either is audible will be at least the sum of the first utterance’s postUtteranceDelay and the second utterance’s preUtteranceDelay.

float PostUtteranceDelay
The amount of time in seconds a speech synthesizer will wait after the utterance is spoken before handling the next queued utterance.
When two or more utterances are spoken, the time between periods when either is audible will be at least the sum of the first utterance’s postUtteranceDelay and the second utterance’s preUtteranceDelay.

float SpeechRate
The rate at which the utterance will be spoken.
Speech rates are values in the range between UtteranceMinimumSpeechRate and UtteranceMaximumSpeechRate. Lower values correspond to slower speech, and vice versa. The default value is UtteranceDefaultSpeechRate.

ISpeechSynthesisVoice Voice
The voice used to speak the utterance.
The default value is null, which causes the utterance to be spoken in the default voice.

float Volume
The volume used when speaking the utterance.
Allowed values are in the range from 0.0 (silent) to 1.0 (loudest). The default volume is 1.0.

Constructors

public SpeechUtteranceParameters()

Usages

Using SpeechUtterance

Example

    var speech = new SpeechUtterance("Hello World!");
    speech.PitchMultiplier = 2f;
    speech.Voice = TTS.GetVoiceForLanguage("en-US");
    TTS.Speak(speech);

Using SpeechUtteranceParameters

Example

    var parameters = new SpeechUtteranceParameters();
    parameters.Voice = TTS.GetVoiceForLanguage("en-US");
    parameters.SpeechRate = TTS.UtteranceMaximumSpeechRate;
    parameters.PostUtteranceDelay = 0.3f;
var speech = new SpeechUtterance("Hello World!", parameters); TTS.Speak(speech);
var anotherSpeech = new SpeechUtterance("Hello to you too!", parameters); TTS.Speak(anotherSpeech);

Modifying Default Behaviour

Classes

public enum SpeechBoundary

Description
Constraints describing when speech may be paused or stopped.

Values

Immediate = 0
Indicates that speech should pause or stop immediately.

Word = 1
Indicates that speech should pause or stop after the word currently being spoken.

Default Settings

public static SpeechUtteranceParameters DefaultParameters

Description
Parameters used when calling Speak without custom SpeechUtteranceParameters

Example

    TTS.DefaultParameters.Voice = TTS.GetVoiceForLanguage("en-US");
    TTS.DefaultParameters.PitchMultiplier = 0.5f;
    TTS.DefaultParameters.Volume = 0.8f;
    TTS.Speak("Hello world!");

public static SpeechBoundary DefaultSpeechBoundaryForPause

Description
Constraints describing when speech may be paused

Example

    TTS.DefaultSpeechBoundaryForPause = SpeechBoundary.Word;
    TTS.Pause();

public static SpeechBoundary DefaultSpeechBoundaryForStop

Description
Constraints describing when speech may be stopped.

Example

    TTS.DefaultSpeechBoundaryForStop = SpeechBoundary.Word;
    TTS.Stop();

Other Properties

public static bool IsSpeaking

Description
A Boolean value that indicates whether the synthesizer is speaking.

Return value
Returns true if the synthesizer is speaking or has utterances enqueued to speak, even if it is currently paused. Returns false if the synthesizer has finished speaking all utterances in its queue or if it has not yet been given an utterance to speak.

Example

  private void Update()
  {
      Debug.Log("Is speaking: " + TTS.IsSpeaking);
  }

public static bool IsPaused

Description
A Boolean value that indicates whether speech has been paused.

Return value
Returns true if the synthesizer has begun speaking an utterance and was paused using TTS.Pause; false otherwise.

Example

  private void Update()
  {
      Debug.Log("Is paused: " + TTS.IsPaused);
  }

public static float UtteranceDefaultSpeechRate

Description
The default rate at which an utterance is spoken unless its rate property is changed.

Example

  TTS.DefaultParameters.SpeechRate = TTS.UtteranceDefaultSpeechRate;

public static float UtteranceMinimumSpeechRate

Description
The minimum allowed speech rate.

Example

  TTS.DefaultParameters.SpeechRate = TTS.UtteranceMinimumSpeechRate;

public static float UtteranceMaximumSpeechRate

Description
The maximum allowed speech rate.

Example

  TTS.DefaultParameters.SpeechRate = TTS.UtteranceMaximumSpeechRate;

Setting Callbacks

Delegate Types

public delegate void SpeechUtteranceCallback()

Description
void Method with no parameters.

public delegate void StringSpeechUtteranceCallback(int startIndex, int stringLength, string utteranceSpeechString)

Description
void Method with 3 parameters: int startIndex, int stringLength, string utteranceSpeechString.

Parameters
int startIndex - The start index of the spoken part of the utterance string.
int stringLength - The number of characters in the spoken part of the utterance string.
string utteranceSpeechString - The utterance currently being spoken.

Callbacks

public static SpeechUtteranceCallback OnSpeechCancelled;

Description
Called when the synthesizer has resumed speaking an utterance after being paused.

Example

private void Start()
{
    TTS.OnSpeechCancelled += LogOnCancelled;
}

private void LogOnCancelled()
{
    Debug.Log("Utterance was cancelled");
}

public static SpeechUtteranceCallback OnSpeechContinued

Description
Called when the synthesizer has resumed speaking an utterance after being paused.

Example

private void Start()
{
    TTS.OnSpeechContinued += LogOnContinued;
}

private void LogOnContinued()
{
    Debug.Log("Utterance was unpaused");
}

public static SpeechUtteranceCallback OnSpeechFinished

Description
Called when the synthesizer has finished speaking an utterance.

Example

private void Start()
{
    TTS.OnSpeechFinished += LogOnFinished;
}

private void LogOnFinished()
{
    Debug.Log("Finished speaking an utterance");
}

public static SpeechUtteranceCallback OnSpeechPaused

Description
Called when the synthesizer has paused while speaking an utterance.

Example

private void Start()
{
    TTS.OnSpeechPaused += LogOnPaused;
}

private void LogOnPaused()
{
    Debug.Log("Utterance was paused");
}

public static SpeechUtteranceCallback OnSpeechStarted

Description
Called when the synthesizer has begun speaking an utterance.

Example

private void Start()
{
    TTS.OnSpeechStarted += LogOnStarted;
}

private void LogOnStarted()
{
    Debug.Log("Utterance was started");
}

public static StringSpeechUtteranceCallback OnWillSpeak

Description
Called when the synthesizer is about to speak a portion of an utterance’s speechString.

Parameters
int startIndex - The start index of the spoken part of the utterance string.
int stringLength - The number of characters in the spoken part of the utterance string.
string utteranceSpeechString - The utterance currently being spoken.

Example

private void Start()
{
    TTS.OnWillSpeak += LogOnWillSpeak;
}

private void LogOnWillSpeak(int startIndex, int stringLength, string utteranceSpeechString)
{
    var partOfSpring = utteranceSpeechString.Substring(startIndex, stringLength);
    Debug.Log("Will speak: " + partOfSpring + " from the text: " + utteranceSpeechString);
}