Text-to-speech (Windows) (Success and failure)

I was looking for free TTS Asset (MS Windows version), but didn’t find one. I tried to make own asset and it worked on Windows Vista

=Success Story 1. TTS Unity Asset on Windows Vista works (you can do it yourself)=
Unity3d can’t use Speech API, because it is based on COM, not on .NET Framework. And somehow I found out that some Interop DLL can be created which will be like a “bridge” between Unity3d and Speech API.
Here is how I made TTS work in Unity in Windows Vista (that’s on my old Laptop)

  1. I started MS Visual Studio, created new C# project.
  2. Clicked menu “Project” → Add reference
  3. In COM tab I select Speech API DLL file: MS Speech Object Library (c:\Windows\System32\Speech\Common\sapi.dll)
  4. Click OK
  5. Visual Studio generated Interop.SpeechLib.dll in one of folders of C# project.
  6. I created new Unity3d project
  7. Into it’s Assets folder I copied a generated Interop.SpeechLib.dll
  8. Made new C# file with content:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//using SpeechLib;
using UnityEngine.UI;

using UnityEngine.Windows.Speech;

public class UnityWinTTS : MonoBehaviour {
    public UnityEngine.UI.Text txt;

    // Use this for initialization
    void Start () {
       
    }
   
    // Update is called once per frame
    void Update () {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            ButtonPress();
        }

    }

    public void ButtonPress()
    {
        SpVoice voice;
        voice = new SpVoice();
        voice.Speak("Hello.");
        voice.Speak(txt.text);
       

       

    }


}
  1. i created a Input Field and Button on Canvas. Button runs ButtonPress function on its OnClick event.
    10. I pressed run and on Button click Unity3d spoke “Hello” and text in Input-Field :wink: Wow. I was surprised, but Text-to-speech within Unity3d works on my Vista (Unity3d 4.7)

=Failure Story 2. TTS Unity Asset on Windows 10 doesn’t work (try yourself)=
On my desktop computer with Windows 10 (64-bit, Unity3d v2017) I walked the same steps as in Story 1, VS generated a DLL, I copied it into Assets folder, but Unity3d doesn’t speak on Button press.

I receive an error:
error CS0246: The type or namespace name `SpeechLib’ could not be found. Are you missing an assembly reference?

I uploaded two ZIPs:

  1. Win 10 Unity TTS project (Assets folder) with Speech API Interop DLL version 5.4
  2. Vista Interop DLL (SAPI v5.3)

Anyone can help make this work on Windows 10?

3232555–248135–Windows10 Unity TSS Effort (Assets).zip (62 KB)
3232555–248137–Interop.SpeechLib (vista).zip (55.5 KB)

Hey text23d

Thanks for posting.
I’m currently having the same issue.

Did you manage to find a solution?

I’ve been looking for a similar solution. I got a bit farther, but not with much luck. I was able to download Microsoft’s Speech Platform v11.

Inside that is a Microsoft Speech .DLL here:
C:\Program Files\Microsoft SDKs\Speech\v11.0\Assembly\Microsoft.Speech.dll

@IvanDonets , I was able to add that .DLL to your Windows10 test project and get access to the [Microsoft.Speech.Synthesis](https://msdn.microsoft.com/en-us/library/jj572477(v=office.14).aspx) namespace.

I updated your script to try it out:

using Microsoft.Speech.Synthesis;
using UnityEngine;
using UnityEngine.UI;

public class UnityWinTTS : MonoBehaviour
{
    [SerializeField] private Text _text;

    public void ButtonPress()
    {
        SpeechSynthesizer synth = new SpeechSynthesizer();
        synth.Speak(_text.text);
    }
}

But when I press the button, Unity crashes. I’m assuming I need to properly wrap the .DLL somehow, but that’s a bit over my head. It’s also possible I need to set some special settings on the editor, but I have no idea what exactly.

I attached my own failed attempt in case it helps someone. ¯_(ツ)_/¯

3351327–262186–Windows10 Unity TSS Effort v2 (Assets).zip (231 KB)

1 Like

where did you add the dll file

You can put it anywhere in the Assets folder and Unity will pick it up. I believe I put it in Assets/Plugins

Anybody get this working without crashes? Looks promising.

The solution to the OP’s problem is really very simple. Uncomment the line “using SpeechLib;” The SpeechLib library is the reference mentioned in the error message.

I thank you for the info but is there a specific computer to do this?

That particular method will only work on a windows machine. I use the latest build of Win 10 and that method works fine. You may occassionally get errors about the interop.speechlib.dll but they can be ignored. Just uncomment the using speechlib.

The “SAPI UNITY DLL & UNITY PLUGINS” solution works !!! TNX !!!
But it misses the SKIP command.
It is useful to stop (not pausing) the current speaking. Can you implement it ? TNX !!!

its working thanks !!

while it works unity pauses evrything
i think SPvoice is calling in main thread

Any one have the solution

nobody?