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
Description Parameters Example
public static void Speak(string speechString)
Enqueues an utterance to be spoken using DefaultParameters
string speechString - The text to be spoken in the utterance.TTS.Speak("Hello world!");
Description Parameters Example
public static void Speak(string speechString, string language)
Enqueues an utterance to be spoken using a voice object for the specified language and locale.
string speechString - The text to be spoken in the utterance.
string language - A BCP 47 code specifying language and locale for a voice.TTS.Speak("Hello world!", "en-US");
Description Parameters Example
public static void Speak(string speechString, ISpeechSynthesisVoice voice)
Enqueues an utterance to be spoken using a specific voice object
string speechString - The text to be spoken in the utterance.
ISpeechSynthesisVoice voice - The voice used to speak the utterance.
var voice = TTS.GetVoiceForLanguage("en-US");
TTS.Speak("Hello world!", voice);
Description Parameters Example
public static void Speak(string speechString, SpeechUtteranceParameters speechUtteranceParameters)
Enqueues an utterance to be spoken with default voice using specific parameters
string speechString - The text to be spoken in the utterance.
SpeechUtteranceParameters speechUtteranceParameters - Parameters that affect the speech
var parameters = new SpeechUtteranceParameters();
TTS.Speak("Hello world!", parameters);
Description Parameters Example
public static void Speak(SpeechUtterance speechUtterance)
Enqueues an utterance to be spoken with using specific parameters
SpeechUtterance speechUtterance - A chunk of text to be spoken, along with parameters that affect its speech.
var utterance = new SpeechUtterance("Hello world!");
TTS.Speak(utterance);
Controlling Speech Synthesis
Description Return value Example
public static bool Continue()
Continues speech from the point at which it left off.
Returns true if speech has continued, or false otherwise.TTS.Continue();
Description Return value Example
public static bool Pause()
Pauses speech at default boundary constraints.
Returns true if speech has paused, or false otherwise.TTS.Pause();
Description Parameters Return value Example
public static bool Pause(SpeechBoundary speechBoundary)
Pauses speech at the specified boundary constraint.
SpeechBoundary speechBoundary - A constant describing whether speech should pause immediately or only after finishing the word currently being spoken.
Returns true if speech has paused, or false otherwise.TTS.Pause(SpeechBoundary.Word);
Description Return value Example
public static bool Stop()
Stops all speech at default boundary constraints.
Returns true if speech has stopped, or false otherwise.TTS.Stop();
Description Parameters Return value Example
public static bool Stop(SpeechBoundary speechBoundary)
Stops all speech at the specified boundary constraint.
SpeechBoundary speechBoundary - A constant describing whether speech should stop immediately or only after finishing the word currently being spoken.
Returns true if speech has stopped, or false otherwise.TTS.Stop(SpeechBoundary.Word);
Using Voices and Languages
Classes
Description Properties string Identifier string Name string Language VoiceQuality Qualitypublic interface ISpeechSynthesisVoice
The voice object used to speak the utterance.
The unique identifier for a voice object.
The name for a voice object.
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.
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.
Description Values Default = 1 Enhanced = 2public enum VoiceQuality
The speech quality for a voice object.
The lower quality version of a voice that is usually installed on the device by default.
The higher quality version of a voice that is usually downloaded by the user.
Usages
Description Parameters Return value Example
public static ISpeechSynthesisVoice GetVoiceForLanguage(string language)
Returns a voice object for the specified language and locale.
string language - A BCP 47 code specifying language and locale for a voice.
Returns null if no voice available for the specified language
var voice = TTS.GetVoiceForLanguage("en-US");
TTS.Speak("Hello world!", voice);
Description Parameters Return value Example
public static ISpeechSynthesisVoice[] GetAllVoicesForLanguage(string language)
Returns all available voice objects for the specified language and locale.
string language - A BCP 47 code specifying language and locale for a voice.
Returns an empty array if no voice available for the specified language
var voices = TTS.GetAllVoicesForLanguage("en-US");
Debug.Log("There are " + voices.Length + "voices available for en-US");
Description Return value Example
public static ReadOnlyCollection<ISpeechSynthesisVoice> AllAvailableVoices
Returns all available voices.
Returns a read-only collection of all available voices
var voices = TTS.AllAvailableVoices;
Debug.Log("There are " + voices.Count + "voices available on this device");
Description Return value Example
public static string CurrentLanguageCode
Returns the code for the user’s current locale.
Returns a string containing BCP 47 language and locale code for the user’s current locale.
var languageCode = TTS.CurrentLanguageCode;
Debug.Log("Current language code is " + languageCode);
Description Return value Example
public static string[] GetAllAvailableLanguages()
Returns all language codes (A BCP 47) for which voices are available.
Returns an array with all language codes (A BCP 47) for which voices are available.
var languages = TTS.GetAllAvailableLanguages();
Debug.Log("There are voices available in " + languages.length + " languages");
Customizing Parameters
Classes
Description Properties string SpeechString float PitchMultiplier float PreUtteranceDelay float PostUtteranceDelay float SpeechRate ISpeechSynthesisVoice Voice float Volume Constructors
public class SpeechUtterance
A chunk of text to be spoken, along with parameters that affect its speech.
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.
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).
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.
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.
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.
The voice used to speak the utterance.
The default value is null, which causes the utterance to be spoken in the default voice.
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.public SpeechUtterance(string speechString)
public SpeechUtterance(string speechString, SpeechUtteranceParameters parameters)
Description Properties float PitchMultiplier float PreUtteranceDelay float PostUtteranceDelay float SpeechRate ISpeechSynthesisVoice Voice float Volume Constructors
public class SpeechUtteranceParameters
Parameters that affect the speech.
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).
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.
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.
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.
The voice used to speak the utterance.
The default value is null, which causes the utterance to be spoken in the default voice.
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.public SpeechUtteranceParameters()
Usages
Example
Using SpeechUtterance
var speech = new SpeechUtterance("Hello World!");
speech.PitchMultiplier = 2f;
speech.Voice = TTS.GetVoiceForLanguage("en-US");
TTS.Speak(speech);
Example
Using SpeechUtteranceParameters
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
Description Values Immediate = 0 Word = 1public enum SpeechBoundary
Constraints describing when speech may be paused or stopped.
Indicates that speech should pause or stop immediately.
Indicates that speech should pause or stop after the word currently being spoken.
Default Settings
Description Example
public static SpeechUtteranceParameters DefaultParameters
Parameters used when calling Speak without custom SpeechUtteranceParameters
TTS.DefaultParameters.Voice = TTS.GetVoiceForLanguage("en-US");
TTS.DefaultParameters.PitchMultiplier = 0.5f;
TTS.DefaultParameters.Volume = 0.8f;
TTS.Speak("Hello world!");
Description Example
public static SpeechBoundary DefaultSpeechBoundaryForPause
Constraints describing when speech may be paused
TTS.DefaultSpeechBoundaryForPause = SpeechBoundary.Word;
TTS.Pause();
Description Example
public static SpeechBoundary DefaultSpeechBoundaryForStop
Constraints describing when speech may be stopped.
TTS.DefaultSpeechBoundaryForStop = SpeechBoundary.Word;
TTS.Stop();
Other Properties
Description Return value Example
public static bool IsSpeaking
A Boolean value that indicates whether the synthesizer is speaking.
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.
private void Update()
{
Debug.Log("Is speaking: " + TTS.IsSpeaking);
}
Description Return value Example
public static bool IsPaused
A Boolean value that indicates whether speech has been paused.
Returns true if the synthesizer has begun speaking an utterance and was paused using TTS.Pause; false otherwise.
private void Update()
{
Debug.Log("Is paused: " + TTS.IsPaused);
}
Description Example
public static float UtteranceDefaultSpeechRate
The default rate at which an utterance is spoken unless its rate property is changed.
TTS.DefaultParameters.SpeechRate = TTS.UtteranceDefaultSpeechRate;
Description Example
public static float UtteranceMinimumSpeechRate
The minimum allowed speech rate.
TTS.DefaultParameters.SpeechRate = TTS.UtteranceMinimumSpeechRate;
Description Example
public static float UtteranceMaximumSpeechRate
The maximum allowed speech rate.
TTS.DefaultParameters.SpeechRate = TTS.UtteranceMaximumSpeechRate;
Setting Callbacks
Delegate Types
Descriptionpublic delegate void SpeechUtteranceCallback()
void Method with no parameters.
Description Parameterspublic delegate void StringSpeechUtteranceCallback(int startIndex, int stringLength, string utteranceSpeechString)
void Method with 3 parameters: int startIndex, int stringLength, string utteranceSpeechString.
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
Description Example
public static SpeechUtteranceCallback OnSpeechCancelled;
Called when the synthesizer has resumed speaking an utterance after being paused.private void Start()
{
TTS.OnSpeechCancelled += LogOnCancelled;
}
private void LogOnCancelled()
{
Debug.Log("Utterance was cancelled");
}
Description Example
public static SpeechUtteranceCallback OnSpeechContinued
Called when the synthesizer has resumed speaking an utterance after being paused.private void Start()
{
TTS.OnSpeechContinued += LogOnContinued;
}
private void LogOnContinued()
{
Debug.Log("Utterance was unpaused");
}
Description Example
public static SpeechUtteranceCallback OnSpeechFinished
Called when the synthesizer has finished speaking an utterance.private void Start()
{
TTS.OnSpeechFinished += LogOnFinished;
}
private void LogOnFinished()
{
Debug.Log("Finished speaking an utterance");
}
Description Example
public static SpeechUtteranceCallback OnSpeechPaused
Called when the synthesizer has paused while speaking an utterance.private void Start()
{
TTS.OnSpeechPaused += LogOnPaused;
}
private void LogOnPaused()
{
Debug.Log("Utterance was paused");
}
Description Example
public static SpeechUtteranceCallback OnSpeechStarted
Called when the synthesizer has begun speaking an utterance.private void Start()
{
TTS.OnSpeechStarted += LogOnStarted;
}
private void LogOnStarted()
{
Debug.Log("Utterance was started");
}
Description Parameters Example
public static StringSpeechUtteranceCallback OnWillSpeak
Called when the synthesizer is about to speak a portion of an utterance’s speechString.
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.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);
}