Unity WebGL Package For Speech Synthesis
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
images
README.md

README.md

UnityWebGLSpeechSynthesis

The WebGL for Speech Synthesis package is available in the Unity Asset Store. Online documentation is available.

See Also

The WebGL for Speech Detection package is available in the Unity Asset Store. Online documentation is available.

The WebGL Speech package is available in the Unity Asset Store. Online documentation is available.

Supported Platforms

Target

The WebGL for Speech Synthesis package is created for Unity version 5.3 or better. This package was originally created for the WebGL platform and supports other platforms using a Speech Proxy. This package requires a browser with the built-in Web Speech API, like Chrome. Synthesis requires an Internet connection. Check the browser compatibility to see which browsers implemented the Speech API.

Tested Browsers

  • Chrome
  • Edge
  • Firefox
  • Safari

Changelog

1.0 - Initial creation of the project

1.1 - Added support for Speech Proxy

1.2 - Minor fixes

1.3 - Added support for speech synthesis in edit mode

1.4 - Added support for MacOS for play-mode and edit-mode

1.5 - Added example without GUI

Demos

Demo 01 Unity Speech Synthesis

Documentation

This document can be accessed in Assets/WebGLSpeechSynthesis/Readme.pdf or use the menuitem GameObject->WebGLSpeechSynthesis->Online Documentation

Sample Scenes

1 Assets/WebGLSpeechSynthesis/Scenes/Example01Synthesis - Uses WebGLSpeechSynthesisPlugin to do speech synthesis

2 Assets/WebGLSpeechSynthesis/Scenes/Example02Proxy - Uses ProxySpeechSynthesisPlugin to do speech synthesis

3 Assets/WebGLSpeechSynthesis/Scenes/Example03ProxyManagement - Management methods for launching and modifying the proxy

4 Assets/WebGLSpeechSynthesis/Scenes/Example04SbaitsoClone - Clone of a classic text to speech demo

5 Assets/WebGLSpeechSynthesis/Editor/Example05PanelSynthesis.cs - Unity editor panel for speech synthesis that works in play mode and edit mode

6 Assets/WebGLSpeechSynthesis/Scenes/Example06NoGUI - Speech synthesis example without a GUI

Modes

Synthesis modes use the same API interface other than where the instance comes from.

WebGL Mode

The WebGLSpeechSynthesisPlugin uses native synthesis only for the WebGL platform.

ISpeechSynthesisPlugin speechSynthesisPlugin = WebGLSpeechSynthesisPlugin.GetInstance();

WebGL mode requires a WebGLSpeechSynthesisPlugin gameobject in the scene which can be created from the GameObject->WebGLSpeechSynthesis->Create WebGLSpeechSynthesisPlugin menu item.

Proxy Mode

The ProxySpeechSynthesisPlugin uses a Speech Proxy to do speech synthesis for non-WebGL platforms.

ISpeechSynthesisPlugin speechSynthesisPlugin = ProxySpeechSynthesisPlugin.GetInstance();

Proxy mode requires a ProxySpeechSynthesisPlugin gameobject in the scene which can be created from the GameObject->WebGLSpeechSynthesis->Create ProxySpeechSynthesisPlugin menu item.

Also a Speech Proxy needs to be running for Proxy mode to work.

The Proxy Port is assigned by the ProxySpeechSynthesisPlugin gameobject with the inspector and needs to match the port used by the Speech Proxy.

image_6

Quick Start

1 Switch to the WebGL platform in `Build Settings image_2

2 Create one WebGLSpeechSynthesisPlugin GameObject in the scene with the menu GameObject->WebGLSpeechSynthesis->Create WebGLSpeechSynthesisPlugion image_3

3 (Optional) You may need a voices dropdown in your UI, use the menuitem GameObject->WebGLSpeechSynthesis->Create Voices Dropdown image_4

4 At this point you should have a scene with the WebGLSpeechSynthesisPlugin, and (optionally) a voices dropdown added to the canvas.

image_5

5 Create a custom MonoBehaviour script to use the WebGLSpeechSynthesis API

6 Add a using statement to get access to the WebGLSpeechSynthesis namespace

using UnityWebGLSpeechSynthesis;

Speech Synthesis Plugin Quick Setup

7 Add a reference for WebGLSpeechSynthesisPlugin to the script

/// <summary>
/// Reference to the plugin
/// </summary>
private ISpeechSynthesisPlugin _mSpeechSynthesisPlugin = null;

8 In the start event check if the plugin is available.

// Use this for initialization
IEnumerator Start()
{
    // get singleton instance
    _mSpeechSynthesisPlugin = WebGLSpeechSynthesisPlugin.GetInstance();
    if (null == _mSpeechSynthesisPlugin)
    {
        Debug.LogError("WebGL Speech Synthesis Plugin is not set!");
        yield break;
    }

    // wait for proxy to become available
    while (!_mSpeechSynthesisPlugin.IsAvailable())
    {
        yield return null;
    }
}

Speak Quick Setup

9 Add a field to hold the utterance that will be spoken

/// <summary>
/// Reference to the utterance which holds the voice and text to speak
/// </summary>
private SpeechSynthesisUtterance _mSpeechSynthesisUtterance = null;

10 Create an instance of SpeechSynthesisUtterance

// Create an instance of SpeechSynthesisUtterance
_mSpeechSynthesisPlugin.CreateSpeechSynthesisUtterance((utterance) =>
{
    //Debug.LogFormat("Utterance created: {0}", utterance._mReference);
    _mSpeechSynthesisUtterance = utterance;
});

11 Speak the utterance

// Cancel if already speaking
_mSpeechSynthesisPlugin.Cancel();

// Set the text that will be spoken
_mSpeechSynthesisPlugin.SetText(_mSpeechSynthesisUtterance, _mInputField.text);

// Use the plugin to speak the utterance
_mSpeechSynthesisPlugin.Speak(_mSpeechSynthesisUtterance);

Voice Selection Quick Setup

12 Add a field to hold the available voices

/// <summary>
/// Reference to the supported voices
/// </summary>
private VoiceResult _mVoiceResult = null;

13 Use the plugin to get the available voices

_mSpeechSynthesisPlugin.GetVoices((voiceResult) =>
{
    _mVoiceResult = voiceResult;
});

14 Select the desired voice from the voice result

if (null != _mVoiceResult &&
    null != _mVoiceResult.voices)
{
    for (int i = 0; i < _mVoiceResult.voices.Length; ++i)
    {
        Voice voice = _mVoiceResult.voices[i];
        if (null == voice)
        {
            continue;
        }
        // select voice by display name
        if (!string.IsNullOrEmpty(voice.display))
        {
            options.Add(voice.display);
        }
        // select voice by name
        else if (!string.IsNullOrEmpty(voice.name))
        {
            options.Add(voice.name);
        }
    }
}

15 Set the voice on the utterance

_mSpeechSynthesisPlugin.SetVoice(_mSpeechSynthesisUtterance, voice);

16 Set text on the utterance and call Speak

// Set the text that will be spoken
_mSpeechSynthesisPlugin.SetText(_mSpeechSynthesisUtterance, text);

// Use the plugin to speak the utterance
_mSpeechSynthesisPlugin.Speak(_mSpeechSynthesisUtterance);

Proxy Management

17 Launch the Speech Proxy

// get the singleton instance
_mSpeechSynthesisPlugin = ProxySpeechSynthesisPlugin.GetInstance();

// check the reference to the plugin
if (null != _mSpeechSynthesisPlugin)
{
    // launch the proxy
    _mSpeechSynthesisPlugin.ManagementLaunchProxy();
}

18 Set Proxy Port

int port = 83;
_mSpeechSynthesisPlugin.ManagementSetProxyPort(port);

19 Open Browser Tab

_mSpeechSynthesisPlugin.ManagementOpenBrowserTab();

20 Close Browser Tab

_mSpeechSynthesisPlugin.ManagementCloseBrowserTab();

21 Close Proxy

_mSpeechSynthesisPlugin.ManagementCloseProxy();

Scenes

Example01 - Speech Synthesis

The scene is located at Assets/WebGLSpeechSynthesis/Scenes/Example01Synthesis.unity

The example source is located at Assets/WebGLSpeechSynthesis/Scripts/Example01Synthesis.cs.

image_1

Example02 - Proxy Synthesis

The scene is located at Assets/WebGLSpeechSynthesis/Scenes/Example02Proxy.unity

The example source is located at Assets/WebGLSpeechSynthesis/Scripts/Example02Proxy.cs.

The example code is nearly identical to the non-proxy example, except for getting the synthesis instance from ProxySpeechSynthesisPlugin.

// get the singleton instance
_mSpeechSynthesisPlugin = ProxySpeechSynthesisPlugin.GetInstance();

Example03 - Proxy Management

The scene is located at Assets/WebGLSpeechSynthesis/Scenes/Example03ProxyManagement.unity.

The example source is located at Assets/WebGLSpeechSynthesis/Scripts/Example03ProxyManagement.cs.

image_7

Example04 - Sbaitso Clone

The scene is located at Assets/WebGLSpeechSynthesis/Scenes/Example04SbaitsoClone.unity.

The example source is located at Assets/WebGLSpeechSynthesis/Scripts/Example04SbaitsoClone.cs.

The AI is controlled from Assets/WebGLSpeechSynthesis/Scripts/AISbaitso.cs which is a port from JAVA.

The example is a clone of the classic Dr. Sbaitso Demo that was bundled with Sound Blaster Pro Audio Cards which showcased text to speech in the 1990s.

image_8

Example05 - Panel Synthesis

The editor panel script is located at Assets/WebGLSpeechSynthesis/Editor/Example05PanelSynthesis.cs and is activated via the Window->WebGLSpeechSynthesis->Open Example05PanelSynthesis menu item.

The example panel shows speech synthesis working in edit and play modes.

The panel example uses the EditorProxySpeechSynthesisPlugin to proxy the speech api in edit mode.

Example06 - No GUI

The scene is located at Assets/WebGLSpeechSynthesis/Scenes/Example06NoGUI.unity

The example source is located at Assets/WebGLSpeechSynthesis/Scripts/Example06NoGUI.cs.

Support

Send questions and/or feedback to the support@theylovegames.com email.