Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'Audio & Video' started by IvanDonets, Sep 25, 2017.

  1. IvanDonets

    IvanDonets

    Joined:
    Feb 19, 2014
    Posts:
    117
    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:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. //using SpeechLib;
    5. using UnityEngine.UI;
    6.  
    7. using UnityEngine.Windows.Speech;
    8.  
    9. public class UnityWinTTS : MonoBehaviour {
    10.     public UnityEngine.UI.Text txt;
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.        
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update () {
    19.         if (Input.GetKeyDown(KeyCode.Space))
    20.         {
    21.             ButtonPress();
    22.         }
    23.  
    24.     }
    25.  
    26.     public void ButtonPress()
    27.     {
    28.         SpVoice voice;
    29.         voice = new SpVoice();
    30.         voice.Speak("Hello.");
    31.         voice.Speak(txt.text);
    32.        
    33.  
    34.        
    35.  
    36.     }
    37.  
    38.  
    39. }
    40.  
    9. 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 ;-) 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?
     

    Attached Files:

  2. Joshua_McDonald

    Joshua_McDonald

    Joined:
    Jan 2, 2018
    Posts:
    1
    Hey text23d

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

    Did you manage to find a solution?
     
  3. deviant-dev

    deviant-dev

    Joined:
    Nov 18, 2017
    Posts:
    5
    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

    text23d, I was able to add that .DLL to your Windows10 test project and get access to the Microsoft.Speech.Synthesis namespace.

    I updated your script to try it out:
    Code (CSharp):
    1. using Microsoft.Speech.Synthesis;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. public class UnityWinTTS : MonoBehaviour
    6. {
    7.     [SerializeField] private Text _text;
    8.  
    9.     public void ButtonPress()
    10.     {
    11.         SpeechSynthesizer synth = new SpeechSynthesizer();
    12.         synth.Speak(_text.text);
    13.     }
    14. }
    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. ¯\_(ツ)_/¯
     

    Attached Files:

    Last edited: Jan 12, 2018
    migupta likes this.
  4. migupta

    migupta

    Joined:
    Jan 23, 2018
    Posts:
    11
    where did you add the dll file
     
  5. deviant-dev

    deviant-dev

    Joined:
    Nov 18, 2017
    Posts:
    5
    You can put it anywhere in the Assets folder and Unity will pick it up. I believe I put it in `Assets/Plugins`
     
  6. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,850
    Anybody get this working without crashes? Looks promising.
     
  7. OldManWoofa

    OldManWoofa

    Joined:
    Feb 1, 2017
    Posts:
    6
    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.
     
  8. pbsulin

    pbsulin

    Joined:
    Oct 15, 2018
    Posts:
    1
    I thank you for the info but is there a specific computer to do this?
     
  9. OldManWoofa

    OldManWoofa

    Joined:
    Feb 1, 2017
    Posts:
    6
    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.
     
  10. frankadimcosta

    frankadimcosta

    Joined:
    Jan 14, 2015
    Posts:
    203
    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 !!!
     
  11. Deftsoft1

    Deftsoft1

    Joined:
    May 11, 2017
    Posts:
    3
    its working thanks !!

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

    Any one have the solution
     
  12. rubenbr83

    rubenbr83

    Joined:
    May 31, 2021
    Posts:
    2
    nobody?