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

Text to Speech [Dll] for Windows Desktop.

Discussion in 'Scripting' started by ZJP, Jul 31, 2010.

  1. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,649
    Hi,

    Unity (Indie and Pro) can speak now. Sorry, only for Windows standalone ;)

    Need a Voice Recognition solution? Try this

    ********************************************************************************************************
    EDIT 05/01/18 NEW VERSION HERE......

    Your are free to include this DLL in any app or project you want.
    You are free to modify and change anything you want. Yes, even the copyright and my name. :p


    ********************************************************************************************************

    The "VoiceSpeaker.CS" file

    Code (csharp):
    1.  
    2. // Voice Speaker  (c) ZJP
    3. //
    4. // Windows 32B >> Copy 'Voice_speaker.dll' in windows\system32 folder
    5. // Windows 64B >> Copy 'Voice_speaker.dll' in windows\SysWOW64 folder
    6. // Remember to release "Voice_speaker.dll" with your final project. It will be placed in the same folder as the EXE
    7. //
    8.     // Voice Speaker  (c) ZJP //
    9.     using UnityEngine;
    10.     using System;
    11.     using System.Collections;
    12.     using System.Runtime.InteropServices;
    13.  
    14.     public class VoiceSpeaker : MonoBehaviour
    15.     {
    16.         [DllImport ("Voice_speaker.dll", EntryPoint="VoiceAvailable")] private static extern int    VoiceAvailable();
    17.         [DllImport ("Voice_speaker.dll", EntryPoint="InitVoice")]      private static extern void   InitVoice();
    18.         [DllImport ("Voice_speaker.dll", EntryPoint="WaitUntilDone")]  private static extern int    WaitUntilDone(int millisec);
    19.         [DllImport ("Voice_speaker.dll", EntryPoint="FreeVoice")]      private static extern void   FreeVoice();
    20.         [DllImport ("Voice_speaker.dll", EntryPoint="GetVoiceCount")]  private static extern int    GetVoiceCount();
    21.  
    22.         // Unity V4.x.x
    23.         [DllImport ("Voice_speaker.dll", EntryPoint="GetVoiceName")]   private static extern IntPtr GetVoiceName(int index);
    24.         //  other Unity version
    25.         // [DllImport ("Voice_speaker.dll", EntryPoint="GetVoiceName")]   private static extern string GetVoiceName(int index);
    26.  
    27.         [DllImport ("Voice_speaker.dll", EntryPoint="SetVoice")]       private static extern void   SetVoice(int index);
    28.         [DllImport ("Voice_speaker.dll", EntryPoint="Say")]            private static extern void   Say(string ttospeak);
    29.         [DllImport ("Voice_speaker.dll", EntryPoint="SayAndWait")]     private static extern void   SayAndWait(string ttospeak);
    30.         [DllImport ("Voice_speaker.dll", EntryPoint="SpeakToFile")]    private static extern int    SpeakToFile(string filename, string ttospeak);
    31.         [DllImport ("Voice_speaker.dll", EntryPoint="GetVoiceState")]  private static extern int    GetVoiceState();
    32.         [DllImport ("Voice_speaker.dll", EntryPoint="GetVoiceVolume")] private static extern int    GetVoiceVolume();
    33.         [DllImport ("Voice_speaker.dll", EntryPoint="SetVoiceVolume")] private static extern void   SetVoiceVolume(int volume);
    34.         [DllImport ("Voice_speaker.dll", EntryPoint="GetVoiceRate")]   private static extern int    GetVoiceRate();
    35.         [DllImport ("Voice_speaker.dll", EntryPoint="SetVoiceRate")]   private static extern void   SetVoiceRate(int rate);
    36.         [DllImport ("Voice_speaker.dll", EntryPoint="PauseVoice")]     private static extern void   PauseVoice();
    37.         [DllImport ("Voice_speaker.dll", EntryPoint="ResumeVoice")]    private static extern void   ResumeVoice();
    38.  
    39.         public int voice_nb = 0;
    40.  
    41.         void Start ()
    42.         {
    43.             if( VoiceAvailable()>0 )
    44.             {
    45.                 InitVoice(); // init the engine
    46.          
    47.                 if (voice_nb > GetVoiceCount()) voice_nb = 0;
    48.                 if (voice_nb < 0) voice_nb = 0;
    49.  
    50.                 // Unity V4.x.x *******************************************
    51.                 IntPtr pStr = GetVoiceName(voice_nb);
    52.                 string str = Marshal.PtrToStringAnsi(pStr);
    53.                 Debug.Log ("Voice name : "+str); // Voice Name
    54.                 // Unity V4.x.x *******************************************
    55.  
    56.                 //Debug.Log ("Voice name : "+GetVoiceName(voice_nb)); // Voice Name other Unity version
    57.  
    58.                 Debug.Log ("Number of voice : "+GetVoiceCount()); // Number of voice
    59.        
    60.                 SetVoice(voice_nb); // 0 to voiceCount - 1
    61.                 Debug.Log ("Voice Rate : "+GetVoiceRate());
    62.                 SetVoiceRate(2);
    63.        
    64.                 //Debug.Log ("Voice name : "+GetVoiceName(voice_nb));
    65.                 Say("All system nominal. Engine, online, weapons, online.. We are ready. 9.,.8.,.7.,.6.,.5.,.4.,.3.,.2.,.1.,.0.,. Go,.Take off");
    66.                 // Say("Tout les systèmes sont opérationnels. Moteurs, en ligne. Armement, en ligne. Nous sommes prêt. 9.,.8.,.7.,.6.,.5.,.4.,.3.,.2.,.1.,.0.,. .Décollage" );
    67.             }
    68.             Application.Quit();
    69.         }
    70.  
    71.         void OnDisable()
    72.         {
    73.             if( VoiceAvailable()>0 )
    74.             {
    75.                 FreeVoice();
    76.             }
    77.         }
    78.     }
    79.  
    Have fun,

    JP


    Edit :
    Do not forget : executing Windows\sysWOW64\speech\SpeechUX\SAPI.cpl brings up a Window that displays all of the 32 bit Voices and the current single 64 bit Voice "Anna".




    RealSpeak Solo de Nuance (Scansoft). Free Voice

    (French - Virgine) http://www.mta.alainlapierre.com/res/RSSolo4FrenchVirginie.zip
    (German) - Steffi) http://www.mta.alainlapierre.com/res/RSSolo4GermanSteffi.zip
    (Italia) - Silvia) http://www.mta.alainlapierre.com/res/RSSolo4ItalianSilvia.zip
    (Spanish) - Isabel) http://www.mta.alainlapierre.com/res/RSSolo4SpanishIsabel.zip
    (English) - Emily) http://www.mta.alainlapierre.com/res/RSSolo4UKEnglishEmily.zip
    (Us - Jennifer ) http://udm4.com/Windows/RealSpeak_Solo_Direc-391899

    EDIT 12-18-15.
    NEW VERSION (WITH SOURCES) HERE......
     

    Attached Files:

    Last edited: Jan 3, 2018
  2. KITT

    KITT

    Joined:
    Jul 17, 2009
    Posts:
    221
  3. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,649
    TX :)
     
  4. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,649
    Hi,

    .CS file updated with news functions in post one.

    JP
     
  5. Kokumo

    Kokumo

    Joined:
    Jul 23, 2010
    Posts:
    416
    Bien ahí pibe!!!... good work!!!
    There is a voice known as "Jorge Loquendo"... of course, from Loquendo company. It is possible to assign that voice to the voice system?

    Greets
     
  6. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,649
  7. Kokumo

    Kokumo

    Joined:
    Jul 23, 2010
    Posts:
    416
    It's very impresive... Jorge's voice (spanish) it one of the best.
    I will markup this thread :D

    Thanks for your work!
     
  8. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,649
    Hi,

    The French voices are amazing too. But I have not seen prices. :cry:

    JP
     
  9. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,649
    Public apology to Unity Techology for having treated them as "pettiness" for disabling the "System.Runtime.InteropServices" allowing access to native DLLs.
    It works again. Thanks guys ;) ;)

    JP
     
  10. Adamo

    Adamo

    Joined:
    Dec 17, 2010
    Posts:
    55
    Dear ZJP,

    Thank you for sharing the DLL and the C# code.
    That was very easy to use.
    Is there also some other utility/tool to achive lips sync to "Say" (said/spoken words)?

    Thanks!

    -Adamo
     
  11. Furfire

    Furfire

    Joined:
    Feb 27, 2011
    Posts:
    23
    Is there a way to implement TTS for 64 bit windows running unity as well?
     
  12. psydack

    psydack

    Joined:
    Sep 18, 2009
    Posts:
    8
    This beatiful. ROFL.

    Can you make manual like, change voice and tips.
    This is awesome.

    Thanks for share man.

    It's make my day cool!
     
    Last edited: Apr 27, 2011
  13. elfelipe

    elfelipe

    Joined:
    Apr 6, 2009
    Posts:
    1
    Hi!! Thanks for the great work with this DLL... I've been experiencing some performance problems (the words are not played fluently) in computers with limited hardware capabilities while playing text with more than 6 or 7 words.

    Is there something I must consider when using your functions? Or things I could do for increasing performance.

    I first get all the voices installed with GetVoiceCount() and GetVoiceName(int index)
    Then when I want to play some text, I set the voice with SetVoice(int index) and after that I call the Say(string ttospeak) function with the text I want.

    If someone with the same problem could help me or point me in some direction, I would really appreciate it.
     
  14. georgedim

    georgedim

    Joined:
    Sep 14, 2011
    Posts:
    3
    Dear JZP,

    I would find your code very useful since I am developing a project involving TTS. Yet I seem unable to get Unity to see the dll. I copy it as you suggested to the system32 folder (and I even tried to copy it in my assets folder). Yet when I try to run your example script I get a DllNotFoundException.

    I am quite a new user of Unity so forgive me if I am asking somthing that I should know :)

    Thanks again for the code

    ...

    Oops, got the solution. For people who are on a Win 7 x64 system. Don't copy the dll in the system32 forlder but in the SysWOW64 one.
     
    Last edited: Sep 14, 2011
  15. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Any chance of this working on ios?
     
  16. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,649
    Nice. Thx for the tip ;)

    No. Sorry :(

    JP
     
    Last edited: Sep 15, 2011
  17. BlinD

    BlinD

    Joined:
    Oct 26, 2011
    Posts:
    3
    where is the file, a necessary licenses?

    Is there a guide?
     
  18. nofosu1

    nofosu1

    Joined:
    Jan 13, 2011
    Posts:
    73
    Very nice, ill have to check it out when i get home.
     
  19. KyleStaves

    KyleStaves

    Joined:
    Nov 4, 2009
    Posts:
    821
    Has anyone worked with this recently? It works for me, but crashes Unity about 80% of the time (working great the other 20%) upon hitting "Play."
     
  20. PAEvenson

    PAEvenson

    Joined:
    Nov 12, 2009
    Posts:
    47
    I am currently using it. You need to make sure you call Init and set the voice. Also if you Free the voice you will need to Init again and set the voice again.
     
  21. Alanimator

    Alanimator

    Joined:
    Sep 25, 2012
    Posts:
    1
    i .......have NO idea what to do here .

    i placed the 32 but dll in the correct folder and created the C# script but i don't know what to do from there . what should i do ?
     
  22. rahuxx

    rahuxx

    Joined:
    May 8, 2009
    Posts:
    537
    How to improve it that it works on mobile platform as they also support plugins?
     
  23. busuzima

    busuzima

    Joined:
    Dec 11, 2012
    Posts:
    2
    Hi guys, I've put the dll in the right folder, attached the ZJP's sample script to the main camera and pressed "Play". After that Unity just crashes down and the Unity Bug Reporter pops up. I tried to set the voice_nb to 1, -1... the same. Btw, I'm a noob in programming, so when this question sounds stupid, please don't be angry. I would appreciate any ideas to solve my problem.

    Regards, Arthur!
     
  24. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,649
    Hi,

    Win32 or Win64?

    Tested on Unity 3.5.6 without pb.

    It's a Windows DLL. Sorry
     
  25. felix_berninger

    felix_berninger

    Joined:
    Aug 19, 2011
    Posts:
    30
    Hi same Problem here. Unity4 x64 Win7 placed dll in right directory and attatched the script. Unity simply crashes.

    when i look at the log it says that it can't find the dll.
     
    Last edited: Dec 14, 2012
  26. g0tNoodles

    g0tNoodles

    Joined:
    Nov 28, 2010
    Posts:
    193
    @xfelalx You need to make sure that it is returning something when you hit play. As an example, if I leave my text box in the editor empty it will crash. As soon as I enter anything, it works. The same goes for a lot of the other functions, like volume and so on.

    I do have a problem though, I cannot get it to speak when I have built my project. It works fine in Unity itself but as soon as I play my build, there is nothing. Do I need to enable something or put a file in my play folder?

    Edit: To make sure the TTS works in my build I have set the architecture to x86 only, not x86_x64.
     
    Last edited: Feb 3, 2013
  27. rastilion

    rastilion

    Joined:
    Dec 18, 2012
    Posts:
    6
    Steps to get this working for me:

    1: Place DLL in SysWOW64
    2: Run Unity, attach script
    3: Run Scene, wait for Unity to crash
    4: Activate debugger from crash dialog
    5: Run scene while / after debugger is done -> Text plays

    Restarting Unity after TTS works will cause Unity to crash again after pressing Play until the debugger is active again.

    Anyone have any idea how to get around the debugger?
     
  28. g0tNoodles

    g0tNoodles

    Joined:
    Nov 28, 2010
    Posts:
    193
    rastilion, when you press play (without the debugger) how are you setting the text in your code?
     
  29. spspider

    spspider

    Joined:
    Jul 29, 2012
    Posts:
    3
    how you can use it? please help me attach to my project? or do simply working project.
     
  30. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,649
    Hi,

    Seems not to work with Unity V4. Why? (Mono?) I do not know. :(
     
    Last edited: Mar 3, 2013
  31. g0tNoodles

    g0tNoodles

    Joined:
    Nov 28, 2010
    Posts:
    193
    ZJP, it works fine for me in Unity 4.x (free). I have most of it working how I would like it to apart from the WaitUntilDone feature which seems to pause everything in the game until it has done speaking and the allows animations, input etc to continue.
     
  32. Keerati

    Keerati

    Joined:
    Mar 4, 2013
    Posts:
    1
    I got the crash problem, and I found that it comes from the function
    GetVoiceName(voice_nb)

    I simply remove this function from the code, and it works now.
     
  33. Seth-Bergman

    Seth-Bergman

    Joined:
    Jul 4, 2012
    Posts:
    4
    Confirmed.. removed both calls to GetVoiceName() (on lines 38 and 39), and everything else works great
     
  34. g0tNoodles

    g0tNoodles

    Joined:
    Nov 28, 2010
    Posts:
    193
    With regards to Keerati and Seth; do you both find that the game is smoother when running without that function? For me, I get a bit of lag before the speech occurs. It may be the way I am implementing it but I am not sure.
     
  35. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,649
    It's the pb. Thx for the tip.
    I've update the original post.

    RealSpeak Solo de Nuance (Scansoft). Free Voice

    (French - Virgine) http://www.mta.alainlapierre.com/res/RSSolo4FrenchVirginie.zip
    (German) - Steffi) http://www.mta.alainlapierre.com/res/RSSolo4GermanSteffi.zip
    (Italia) - Silvia) http://www.mta.alainlapierre.com/res/RSSolo4ItalianSilvia.zip
    (Spanish) - Isabel) http://www.mta.alainlapierre.com/res/RSSolo4SpanishIsabel.zip
    English) - Emily) http://www.mta.alainlapierre.com/res/RSSolo4UKEnglishEmily.zip


    JP
     
    Last edited: Mar 6, 2013
  36. g0tNoodles

    g0tNoodles

    Joined:
    Nov 28, 2010
    Posts:
    193
    How do you use the say and wait function? For what I need, I need the game to speak and then wait for a second or wait for some input from the user.

    Any help would be appreciated.
     
  37. Seth-Bergman

    Seth-Bergman

    Joined:
    Jul 4, 2012
    Posts:
    4
    You can simply use:

    SayAndWait("Please enter some sort of input");
    showInputBox = true; //or whatever

    void OnGUI(){
    if(showInputBox)
    ....etc
    }

    In other words, the next line is reached once the speech is complete.

    for further assistance try the answers forum
     
    Last edited: Mar 8, 2013
  38. g0tNoodles

    g0tNoodles

    Joined:
    Nov 28, 2010
    Posts:
    193
    Well, I got it to work but the game stops completely so it can finish speaking, all but the music in my game. Like I have said, I'm not sure if this happens to everyone else or just me.
     
  39. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,649
    Last edited: Mar 22, 2013
  40. HsinChien

    HsinChien

    Joined:
    Nov 19, 2012
    Posts:
    22
    I try it with Chinese tts, but it seems the string got converted wrong and the engine speak incorrect word. Is it possible to use Chinese tts? Thanks!
     

    Attached Files:

    Last edited: Jul 30, 2013
  41. ashjack

    ashjack

    Joined:
    Jun 9, 2013
    Posts:
    44
    Hi there! Is it possible to make the voice act like a 3D sound?
     
  42. tataparcs

    tataparcs

    Joined:
    Aug 26, 2012
    Posts:
    3
    Regarding of Text to Speech [Dll] for Win32, how could I put play/pause/stop button? I don't want it to autoplay because it is one of the requirements on our project. Thank you for the reply sir! God Bless!
     
  43. g0tNoodles

    g0tNoodles

    Joined:
    Nov 28, 2010
    Posts:
    193
    All you need to do is set up and call the pause function from another script in in the same one but at some gui stuff to it
     
  44. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,649
    +1
     
  45. lancer

    lancer

    Joined:
    Aug 1, 2013
    Posts:
    231
    Hey there ZJP,

    I'm trying to use this in my project and keep getting this error:

    DllNotFoundException: Voice_speaker.dll
    VoiceSpeaker.Start () (at Assets/Scripts/Speech/VoiceSpeaker.cs:50)

    I downloaded the .zip in your first post and dropped the .dll into the assets folder. Is there something I'm doing wrong?
     
  46. lancer

    lancer

    Joined:
    Aug 1, 2013
    Posts:
    231
    Never Mind,

    I dropped the .dll into the SysWOW64 folder and it worked.
    I recommend that you add to your first post with the instruction that you have to do that.
     
  47. g0tNoodles

    g0tNoodles

    Joined:
    Nov 28, 2010
    Posts:
    193
    Lancer, there is a note in the first post about where to put the .dll, you just didn't read it.
     
  48. lancer

    lancer

    Joined:
    Aug 1, 2013
    Posts:
    231
    I did read it, the way it was typed made it sound like he was saying to copy a file from one of those directories, not put it in.
     
  49. lancer

    lancer

    Joined:
    Aug 1, 2013
    Posts:
    231
    hey ZJP,

    How do I slow the voice down? I've set the voice rate to 1 and tried 0 and it is still too fast.
     
  50. lancer

    lancer

    Joined:
    Aug 1, 2013
    Posts:
    231
    Oh, got it,

    I thought that -1 would make the voice talk backwards, but it just slowed it down more.