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. ashjack

    ashjack

    Joined:
    Jun 9, 2013
    Posts:
    44
    How do I make it say a string at a certain time? So for example when the character says something (chatbot), what has been said is read out immediately afterwards.
     
  2. g0tNoodles

    g0tNoodles

    Joined:
    Nov 28, 2010
    Posts:
    193
    You could use speak to file if you want it to read from an external file. Otherwise you need to use the 'Say(string ttospeak);' function. When your chatbot is ready to speak just run that.

    Something like this:

    Code (csharp):
    1. if(chatbotSpeak == true)
    2. {
    3.      Say("Hello, I am ChatBot");
    4. }
    5.  
    6. or
    7. public string chatBotText = "Hello, I am ChatBot";
    8.  
    9. if(chatbotSpeak == true)
    10. {
    11.      Say(chatBotText);
    12. }
    13.  
    That should work ok but I am not able to test it as I am on OSX atm. If it doesn't work, post your code here and people will be more likely to help.
     
  3. ashjack

    ashjack

    Joined:
    Jun 9, 2013
    Posts:
    44
    Do I put this in a seperate script, or in VoiceSpeaker.cs?
     
  4. ashjack

    ashjack

    Joined:
    Jun 9, 2013
    Posts:
    44
    Ok, I tried it from another script(C#) And I get the following error: Assets/My Scripts/ChatBot.cs(80,21): error CS0122: `VoiceSpeaker.Say(string)' is inaccessible due to its protection level

    Here is my script:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ChatBot : MonoBehaviour {
    5.    
    6.     //Friendship level. -5 = Nemesis -4 = Hated -3 = Very Disliked -2 = Disliked -1 = Bad First Impression 0 = Stranger, 1 = Acquaintance, 2 = A Bit Friendly, 3 = Friend, 4 = Good Friend, 5 = Very Good Friend, 6 = Best Friend, 7 = Gf/Bf, 8 = Partner
    7.     //Mood. 0 = Furious 1 = Angry 2 = Depressed 3 = Sad 4 = Embarrassed 5 = Bored 6 = Excited 7 = Happy 8 = Elated 9 = OK
    8.     public int friendship;
    9.     public int mood;
    10.     public int randomgreeting;
    11.     public string greeting;
    12.     public VoiceSpeaker npc;
    13.  
    14.  
    15.        
    16.    
    17.     private void OnMouseDown() {
    18.    
    19.     if(friendship == 0)
    20.         {
    21.             randomgreeting = (Random.Range(0,12));
    22.         }
    23.    
    24.     if(randomgreeting == 0  mood == 9)
    25.         {
    26.             greeting = "Hello.";
    27.         }
    28.     else if(randomgreeting == 1  mood == 9)
    29.         {
    30.             greeting = "Uh, hi.";
    31.         }
    32.     else if(randomgreeting == 2  mood == 9)
    33.         {
    34.             greeting = "Yes?";
    35.         }
    36.     else if(randomgreeting == 3  mood == 9)
    37.         {
    38.             greeting = "Huh?";
    39.         }
    40.     else if(randomgreeting == 4  mood == 9)
    41.         {
    42.             greeting = "Are you talking to me?";
    43.            
    44.         }
    45.     else if(randomgreeting == 5  mood == 9)
    46.         {
    47.             greeting = "Hi?";
    48.         }
    49.     else if(randomgreeting == 6  mood == 9)
    50.         {
    51.             greeting = "Hi, I guess...";
    52.         }
    53.     else if(randomgreeting == 7  mood == 9)
    54.         {
    55.             greeting = "Hello, I guess...";
    56.         }
    57.     else if(randomgreeting == 8  mood == 9)
    58.         {
    59.             greeting = "Hey, I guess...";
    60.         }
    61.     else if(randomgreeting == 9  mood == 9)
    62.         {
    63.             greeting = "Hello?";
    64.         }
    65.     else if(randomgreeting == 10  mood == 9)
    66.         {
    67.             greeting = "Hey?";
    68.         }
    69.     else if(randomgreeting == 11  mood == 9)
    70.         {
    71.             greeting = "Uh, Hello.";
    72.         }
    73.     else if(randomgreeting == 12  mood == 9)
    74.         {
    75.             greeting = "Uh, Hey.";
    76.         }
    77.        
    78.        
    79.    
    80.         npc.Say(greeting);
    81.    
    82.        
    83.        
    84.        
    85.         if(friendship == 1)
    86.         {
    87.             randomgreeting = (Random.Range(0,12));
    88.         }
    89.  
    90.     if(randomgreeting == 0  mood == 9)
    91.         {
    92.             greeting = "Hello";
    93.         }
    94.     else if(randomgreeting == 1  mood == 9)
    95.         {
    96.             greeting = "Hi.";
    97.         }
    98.     else if(randomgreeting == 2  mood == 9)
    99.         {
    100.             greeting = "Hey.";
    101.         }
    102.     else if(randomgreeting == 3  mood == 9)
    103.         {
    104.             greeting = "Hey there.";
    105.         }
    106.     else if(randomgreeting == 4  mood == 9)
    107.         {
    108.             greeting = "Hi there.";
    109.            
    110.         }
    111.     else if(randomgreeting == 5  mood == 9)
    112.         {
    113.             greeting = "How are you?";
    114.         }
    115.     else if(randomgreeting == 6  mood == 9)
    116.         {
    117.             greeting = "Hi " + name + "!";
    118.         }
    119.     else if(randomgreeting == 7  mood == 9)
    120.         {
    121.             greeting = "Hello, I guess...";
    122.         }
    123.     else if(randomgreeting == 8  mood == 9)
    124.         {
    125.             greeting = "Hey, I guess...";
    126.         }
    127.     else if(randomgreeting == 9  mood == 9)
    128.         {
    129.             greeting = "Hello?";
    130.         }
    131.     else if(randomgreeting == 10  mood == 9)
    132.         {
    133.             greeting = "Hey?";
    134.         }
    135.     else if(randomgreeting == 11  mood == 9)
    136.         {
    137.             greeting = "Uh, Hello.";
    138.         }
    139.     else if(randomgreeting == 12  mood == 9)
    140.         {
    141.             greeting = "Uh, Hey.";
    142.         }
    143.     }
    144.    
    145.    
    146.    
    147.     // Update is called once per frame
    148.     void Update () {
    149.    
    150.    
    151.    
    152.  
    153.    
    154.     }
    155. }
    156.  
     
    Last edited: Oct 31, 2013
  5. g0tNoodles

    g0tNoodles

    Joined:
    Nov 28, 2010
    Posts:
    193
    I'll have a look at it when I get to my computer and I'll let you know what I found/did so you can see how it works. It'll be in a couple of hours!
     
  6. g0tNoodles

    g0tNoodles

    Joined:
    Nov 28, 2010
    Posts:
    193
    Ok, this is my VoiceSpeaker script:

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. using System;
    4.  
    5. using System.Collections;
    6.  
    7. using System.Runtime.InteropServices;
    8.  
    9.  
    10.  
    11. public class VoiceSpeaker : MonoBehaviour
    12.  
    13. {
    14.  
    15.     [DllImport ("Voice_speaker.dll", EntryPoint="VoiceAvailable")] private static extern int    VoiceAvailable();
    16.  
    17.     [DllImport ("Voice_speaker.dll", EntryPoint="InitVoice")]      private static extern void   InitVoice();
    18.  
    19.     [DllImport ("Voice_speaker.dll", EntryPoint="WaitUntilDone")]  private static extern int    WaitUntilDone(int millisec);
    20.  
    21.     [DllImport ("Voice_speaker.dll", EntryPoint="FreeVoice")]      private static extern void   FreeVoice();
    22.  
    23.     [DllImport ("Voice_speaker.dll", EntryPoint="GetVoiceCount")]  private static extern int    GetVoiceCount();
    24.  
    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.  
    29.     [DllImport ("Voice_speaker.dll", EntryPoint="Say")]            private static extern void   Say(string ttospeak);
    30.  
    31.     [DllImport ("Voice_speaker.dll", EntryPoint="SayAndWait")]     private static extern void   SayAndWait(string ttospeak);
    32.  
    33.     [DllImport ("Voice_speaker.dll", EntryPoint="SpeakToFile")]    private static extern int    SpeakToFile(string filename, string ttospeak);
    34.  
    35.     [DllImport ("Voice_speaker.dll", EntryPoint="GetVoiceState")]  private static extern int    GetVoiceState();
    36.  
    37.     [DllImport ("Voice_speaker.dll", EntryPoint="GetVoiceVolume")] private static extern int    GetVoiceVolume();
    38.  
    39.     [DllImport ("Voice_speaker.dll", EntryPoint="SetVoiceVolume")] private static extern void   SetVoiceVolume(int volume);
    40.  
    41.     [DllImport ("Voice_speaker.dll", EntryPoint="GetVoiceRate")]   private static extern int    GetVoiceRate();
    42.  
    43.     [DllImport ("Voice_speaker.dll", EntryPoint="SetVoiceRate")]   private static extern void   SetVoiceRate(int rate);
    44.  
    45.     [DllImport ("Voice_speaker.dll", EntryPoint="PauseVoice")]     private static extern void   PauseVoice();
    46.  
    47.     [DllImport ("Voice_speaker.dll", EntryPoint="ResumeVoice")]    private static extern void   ResumeVoice();
    48.  
    49.     public int voice_nb = 0;
    50.    
    51.     public void Speak(string talking)
    52.     {
    53.         if( VoiceAvailable()>0 )
    54.         {
    55.             InitVoice(); // init the engine
    56.  
    57.             SetVoice(voice_nb); // 0 to voiceCount - 1
    58.  
    59.             Say(talking);
    60.         }
    61.     }
    62.  }
    And then this is how I called it from another script:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GameguideForTheDeaf : MonoBehaviour {
    5.    
    6.     public string deafplayerHelp;
    7.     public VoiceSpeaker talk;
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.         talk.Speak(deafplayerHelp);
    12.     }
    13.    
    14.     // Update is called once per frame
    15.     void Update () {
    16.     }
    17. }
    Forgive me for any bad naming of variables as I did do most of this at about 3am after a day at Uni =[. Let me know how you get on and if you need any more help!
     
  7. g0tNoodles

    g0tNoodles

    Joined:
    Nov 28, 2010
    Posts:
    193
    VERY IMPORTANT! From my experience with this tool, you need to make sure that you save everything, exit Unity (and Mono Dev) and then restart Unity before you test. If you don't do this and it crashes, you will lose any editor changes (not script) that you made and will have to redo them. This happens no matter how many times you save your project and/or scene.

    Basically, what you need to do is; go to your VoiceSpeaker script, add in a method with the name Speak (what whatever you want) e.g

    Just for future reference, you may not need to use InitVoice(); every time you call the method but I left it because it worked. See what happens. Worst case scenario is that you need to add it back in.
    Code (csharp):
    1. void Speak (string textToSay)
    2. {
    3.    if(VoiceAvailable() > 0)
    4.    {
    5.       InitVoice();
    6.       SetVoice(voice nb);
    7.       Say(textToSay);
    8.    }
    9. }
    Then within the script you posted, change this
    Code (csharp):
    1. npc.Say(greeting);
    to this

    Code (csharp):
    1. npc.Speak(greeting);
     
    Last edited: Oct 31, 2013
  8. ashjack

    ashjack

    Joined:
    Jun 9, 2013
    Posts:
    44
    I did exactly what you said, and now I get this error:

    NullReferenceException: Object reference not set to an instance of an object
    ChatBot.OnMouseDown () (at Assets/My Scripts/ChatBot.cs:80)
    UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32, Int32)

    The only difference to my chatbot.cs is that I changed npc.Say to npc.Speak, and I used your version of VoiceSpeaker.cs instead of the original.
     
    Last edited: Nov 1, 2013
  9. g0tNoodles

    g0tNoodles

    Joined:
    Nov 28, 2010
    Posts:
    193
    Have you dragged the gameobject with your chatbot.cs script attached to the npc variable in the editor/inspector?
     
  10. ashjack

    ashjack

    Joined:
    Jun 9, 2013
    Posts:
    44
    I hadn't, but I have now. I saved and closed unity(monodev wasn't up) and reopened it, and when I clicked on the NPC, unity crashed. I keep trying to save and close, then open and test, but every time I test, it still crashes.
     
    Last edited: Nov 1, 2013
  11. g0tNoodles

    g0tNoodles

    Joined:
    Nov 28, 2010
    Posts:
    193
    OK, that means that something is returning null. Try calling a string without your random assignment. Just type in npc.Speak("Hello"); and see what happens.

    But it seems like it has something more to do with the omousedown event. Try putting an input event on the update method and call what I said above. If that works then try not putting private infront on void onmousedown.
     
  12. ashjack

    ashjack

    Joined:
    Jun 9, 2013
    Posts:
    44
    Ok, I changed the chatbot script to this:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ChatBot : MonoBehaviour {
    5.    
    6.     //Friendship level. -5 = Nemesis -4 = Hated -3 = Very Disliked -2 = Disliked -1 = Bad First Impression 0 = Stranger, 1 = Acquaintance, 2 = A Bit Friendly, 3 = Friend, 4 = Good Friend, 5 = Very Good Friend, 6 = Best Friend, 7 = Gf/Bf, 8 = Partner
    7.     //Mood. 0 = Furious 1 = Angry 2 = Depressed 3 = Sad 4 = Embarrassed 5 = Bored 6 = Excited 7 = Happy 8 = Elated 9 = OK
    8.     public int friendship;
    9.     public int mood;
    10.     public int randomgreeting;
    11.     public string greeting;
    12.     public VoiceSpeaker npc;
    13.  
    14.  
    15.        
    16.    
    17.     void Update() {
    18.        
    19.     if(Input.GetKeyUp(KeyCode.Space)){
    20.    
    21.     if(friendship == 0)
    22.         {
    23.             randomgreeting = (Random.Range(0,12));
    24.         }
    25.    
    26.     if(randomgreeting == 0  mood == 9)
    27.         {
    28.             greeting = "Hello.";
    29.         }
    30.     else if(randomgreeting == 1  mood == 9)
    31.         {
    32.             greeting = "Uh, hi.";
    33.         }
    34.     else if(randomgreeting == 2  mood == 9)
    35.         {
    36.             greeting = "Yes?";
    37.         }
    38.     else if(randomgreeting == 3  mood == 9)
    39.         {
    40.             greeting = "Huh?";
    41.         }
    42.     else if(randomgreeting == 4  mood == 9)
    43.         {
    44.             greeting = "Are you talking to me?";
    45.            
    46.         }
    47.     else if(randomgreeting == 5  mood == 9)
    48.         {
    49.             greeting = "Hi?";
    50.         }
    51.     else if(randomgreeting == 6  mood == 9)
    52.         {
    53.             greeting = "Hi, I guess...";
    54.         }
    55.     else if(randomgreeting == 7  mood == 9)
    56.         {
    57.             greeting = "Hello, I guess...";
    58.         }
    59.     else if(randomgreeting == 8  mood == 9)
    60.         {
    61.             greeting = "Hey, I guess...";
    62.         }
    63.     else if(randomgreeting == 9  mood == 9)
    64.         {
    65.             greeting = "Hello?";
    66.         }
    67.     else if(randomgreeting == 10  mood == 9)
    68.         {
    69.             greeting = "Hey?";
    70.         }
    71.     else if(randomgreeting == 11  mood == 9)
    72.         {
    73.             greeting = "Uh, Hello.";
    74.         }
    75.     else if(randomgreeting == 12  mood == 9)
    76.         {
    77.             greeting = "Uh, Hey.";
    78.         }
    79.        
    80.        
    81.    
    82.         npc.Speak("greeting");
    83.    
    84.        
    85.        
    86.        
    87.         if(friendship == 1)
    88.         {
    89.             randomgreeting = (Random.Range(0,12));
    90.         }
    91.  
    92.     if(randomgreeting == 0  mood == 9)
    93.         {
    94.             greeting = "Hello";
    95.         }
    96.     else if(randomgreeting == 1  mood == 9)
    97.         {
    98.             greeting = "Hi.";
    99.         }
    100.     else if(randomgreeting == 2  mood == 9)
    101.         {
    102.             greeting = "Hey.";
    103.         }
    104.     else if(randomgreeting == 3  mood == 9)
    105.         {
    106.             greeting = "Hey there.";
    107.         }
    108.     else if(randomgreeting == 4  mood == 9)
    109.         {
    110.             greeting = "Hi there.";
    111.            
    112.         }
    113.     else if(randomgreeting == 5  mood == 9)
    114.         {
    115.             greeting = "How are you?";
    116.         }
    117.     else if(randomgreeting == 6  mood == 9)
    118.         {
    119.             greeting = "Hi " + name + "!";
    120.         }
    121.     else if(randomgreeting == 7  mood == 9)
    122.         {
    123.             greeting = "Hello, I guess...";
    124.         }
    125.     else if(randomgreeting == 8  mood == 9)
    126.         {
    127.             greeting = "Hey, I guess...";
    128.         }
    129.     else if(randomgreeting == 9  mood == 9)
    130.         {
    131.             greeting = "Hello?";
    132.         }
    133.     else if(randomgreeting == 10  mood == 9)
    134.         {
    135.             greeting = "Hey?";
    136.         }
    137.     else if(randomgreeting == 11  mood == 9)
    138.         {
    139.             greeting = "Uh, Hello.";
    140.         }
    141.     else if(randomgreeting == 12  mood == 9)
    142.         {
    143.             greeting = "Uh, Hey.";
    144.         }
    145.     }
    146.     }
    147.    
    148.    
    149.  
    150.    
    151. }
    152.  
    It works, but as soon as I remove the speech marks and make it say the string, it gives up and crashes.
     
  13. g0tNoodles

    g0tNoodles

    Joined:
    Nov 28, 2010
    Posts:
    193
    Ok, do you know for a fact that the string is being set to something? You could try sayin:
    if(greeting != null)
    {
    npc.Speak(greeting);
    }
    else
    npc.Speak("no string set");

    That should run it properly or tell you you don't have a string set.
     
  14. ashjack

    ashjack

    Joined:
    Jun 9, 2013
    Posts:
    44
    It still crashes, even with your code.
     
  15. g0tNoodles

    g0tNoodles

    Joined:
    Nov 28, 2010
    Posts:
    193
    Is a string actually being set? I'll create a quick project later and see how that goes. If it works fine, I'll send it to you.
     
  16. ashjack

    ashjack

    Joined:
    Jun 9, 2013
    Posts:
    44
    Ok, thanks!
     
  17. g0tNoodles

    g0tNoodles

    Joined:
    Nov 28, 2010
    Posts:
    193
    Ok, I have attached a .rar of my test project for you. Just open it up and run it and it should work fine. I have made some comments in the ChatBot.cs script for you to have a look at.

    I made mistakes when answering your questions and have tried to make sure I have explained them in the comments; I apologise for this. If you need any more help, private message me!
     

    Attached Files:

  18. fabio1955

    fabio1955

    Joined:
    Nov 11, 2009
    Posts:
    72
    Unity (4.3 Window 7) crushes if you set voice_nb>0 in the inspector even if you have more than one voice (I got 6 voices installed). Any suggestion?
    OK: SOLVED: Voice #1 was badly installed
     
    Last edited: Nov 14, 2013
  19. g0tNoodles

    g0tNoodles

    Joined:
    Nov 28, 2010
    Posts:
    193
    fabio, can you post the code you're using?
     
  20. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,649
    @g0tNoodles
    Thanks for the hot-line.. ;)
     
  21. g0tNoodles

    g0tNoodles

    Joined:
    Nov 28, 2010
    Posts:
    193
    May as well pass on the small amount of knowledge I have on the subject ;D
     
  22. fabio1955

    fabio1955

    Joined:
    Nov 11, 2009
    Posts:
    72
    @ g0tNoodles: I am using just your code posted one week ago (the rar file)...
     
  23. g0tNoodles

    g0tNoodles

    Joined:
    Nov 28, 2010
    Posts:
    193
    I'm glad that I am able to give back to the community in some way!
     
  24. fabio1955

    fabio1955

    Joined:
    Nov 11, 2009
    Posts:
    72
    Do you have suggestions for replacing the default voice Anna on a Windows 7 64?
     
  25. g0tNoodles

    g0tNoodles

    Joined:
    Nov 28, 2010
    Posts:
    193
    I'll be honest, I've never actually tried to change the voice before so I have no idea...
     
  26. DigitalAdam

    DigitalAdam

    Joined:
    Jul 18, 2007
    Posts:
    1,202
    Is there an easy way to have your system "speak" whatever is in a text field?
     
    Last edited: Nov 21, 2013
  27. g0tNoodles

    g0tNoodles

    Joined:
    Nov 28, 2010
    Posts:
    193
    Do you mean the GUI text field or in the inspector?
     
  28. DigitalAdam

    DigitalAdam

    Joined:
    Jul 18, 2007
    Posts:
    1,202
    Exactly. Is there a way for the text to speech to read whatever is in the GUI text field; whether that's hand entered (say you type something in and press enter) or something is automatically generated to display there?

    I'm a designer, not a coder, so any help would greatly be appreciated!
     
  29. g0tNoodles

    g0tNoodles

    Joined:
    Nov 28, 2010
    Posts:
    193
    I will have to do a test and get back to you on that one! Will put the result up asap!
     
  30. DigitalAdam

    DigitalAdam

    Joined:
    Jul 18, 2007
    Posts:
    1,202
    That would be awesome, thank you!
     
  31. g0tNoodles

    g0tNoodles

    Joined:
    Nov 28, 2010
    Posts:
    193
    Hey adamz, I have built on to my previous test project and added a new scene for your question. The project is attached to this message. I am hoping it is what you want, if not, let me know and I will try to help more!
     

    Attached Files:

  32. DigitalAdam

    DigitalAdam

    Joined:
    Jul 18, 2007
    Posts:
    1,202
    This is fantastic, thanks! I have a script that uses AIML and I want to see if I can hear it's responses based on your code.
     
  33. g0tNoodles

    g0tNoodles

    Joined:
    Nov 28, 2010
    Posts:
    193
    Never used that before and don't really know anything about it but give it a go and see what happens. If you know the problem but are not sure how to sort it let me know and I will do what I can for you dude.
     
  34. DigitalAdam

    DigitalAdam

    Joined:
    Jul 18, 2007
    Posts:
    1,202
    Actually your code in combination with mine worked out great, so thanks!

    AIML = Artificial Intelligence Markup Language. Its a language for the computer to understand what you type. So if I ask it "hows is your day?", it can respond realistically. Here is an example of what I'm trying to achieve. FaceFX is a plugin that drive the facial animation, and Pandora Bots runs the AIML on their servers to give the bot simulated life. You might have to wait a moment or two for the bot to respond once you enter a question in the text field and hit 'enter'.
     
  35. g0tNoodles

    g0tNoodles

    Joined:
    Nov 28, 2010
    Posts:
    193
    Glad it works for you! That looks quite good. Is the speed it takes to reply something that can be sped up or is that just the way it is?
     
  36. DigitalAdam

    DigitalAdam

    Joined:
    Jul 18, 2007
    Posts:
    1,202
    I think the delay is because it's being submitted to a server, analyzed with a response, and then submitted back to the browser, that's why it takes a few moments. The system I'm running will be local, so it should be instantaneous or pretty close to that.

    Now I need to find an asset that will sync my bone driven character with the text response/voice response. FaceFX is too expensive for me to utilize. Anyways I'm halfway there thanks to you!
     
  37. g0tNoodles

    g0tNoodles

    Joined:
    Nov 28, 2010
    Posts:
    193
    I'm sure you will find something out there =]. All the best though!
     
  38. Murkas

    Murkas

    Joined:
    Jul 24, 2013
    Posts:
    7
    At first: good work :)
    I modified your code a bit and nearly everything works great. I wanted to use this as a 3D sound, but it seems, that it is not possible atm. So I thought about using the SpeakToFile function, but the result sounded a bit creepy, until I realized, the stuff spoken to the file does not use the language, I told the programm to use (via SetVoice). Is there any solution for this?
     
  39. g0tNoodles

    g0tNoodles

    Joined:
    Nov 28, 2010
    Posts:
    193
    Is there any chance you could post your code (and the file you're trying to read from)? I've not used SpeakToFile before and I am not sure what is happening.
     
  40. Murkas

    Murkas

    Joined:
    Jul 24, 2013
    Posts:
    7
    You did'nt understand SpeakToFile. SpeakToFile reads the given text and saves it as a file.
    Instead of
    Code (csharp):
    1. Say("Something to say");
    you use
    Code (csharp):
    1. SpeakToFile("test.wav","Something to say");
    Then you can go to your assets folder (editor) or to your main folder (standalone) and play the soundfile.
    But as I already said, it does not use the language I set to use.
     
  41. OneManArmy3D

    OneManArmy3D

    Joined:
    Jun 2, 2011
    Posts:
    191
    $voice.png


    Just a modified version of script. If you install second language, you can switch voices + change speed in runtime.

    Code (csharp):
    1.  using UnityEngine;
    2.         using System;
    3.         using System.Collections;
    4.         using System.Runtime.InteropServices;
    5.          
    6.         public class VoiceSpeaker : MonoBehaviour
    7.         {
    8.             [DllImport ("Voice_speaker.dll", EntryPoint="VoiceAvailable")] private static extern int    VoiceAvailable();
    9.             [DllImport ("Voice_speaker.dll", EntryPoint="InitVoice")]      private static extern void   InitVoice();
    10.             [DllImport ("Voice_speaker.dll", EntryPoint="WaitUntilDone")]  private static extern int    WaitUntilDone(int millisec);
    11.             [DllImport ("Voice_speaker.dll", EntryPoint="FreeVoice")]      private static extern void   FreeVoice();
    12.             [DllImport ("Voice_speaker.dll", EntryPoint="GetVoiceCount")]  private static extern int    GetVoiceCount();
    13.      
    14.             // Unity V4.x.x
    15.             [DllImport ("Voice_speaker.dll", EntryPoint="GetVoiceName")]   private static extern IntPtr GetVoiceName(int index);
    16.             //  other Unity version
    17.             // [DllImport ("Voice_speaker.dll", EntryPoint="GetVoiceName")]   private static extern string GetVoiceName(int index);
    18.      
    19.             [DllImport ("Voice_speaker.dll", EntryPoint="SetVoice")]       private static extern void   SetVoice(int index);
    20.             [DllImport ("Voice_speaker.dll", EntryPoint="Say")]            private static extern void   Say(string ttospeak);
    21.             [DllImport ("Voice_speaker.dll", EntryPoint="SayAndWait")]     private static extern void   SayAndWait(string ttospeak);
    22.             [DllImport ("Voice_speaker.dll", EntryPoint="SpeakToFile")]    private static extern int    SpeakToFile(string filename, string ttospeak);
    23.             [DllImport ("Voice_speaker.dll", EntryPoint="GetVoiceState")]  private static extern int    GetVoiceState();
    24.             [DllImport ("Voice_speaker.dll", EntryPoint="GetVoiceVolume")] private static extern int    GetVoiceVolume();
    25.             [DllImport ("Voice_speaker.dll", EntryPoint="SetVoiceVolume")] private static extern void   SetVoiceVolume(int volume);
    26.             [DllImport ("Voice_speaker.dll", EntryPoint="GetVoiceRate")]   private static extern int    GetVoiceRate();
    27.             [DllImport ("Voice_speaker.dll", EntryPoint="SetVoiceRate")]   private static extern void   SetVoiceRate(int rate);
    28.             [DllImport ("Voice_speaker.dll", EntryPoint="PauseVoice")]     private static extern void   PauseVoice();
    29.             [DllImport ("Voice_speaker.dll", EntryPoint="ResumeVoice")]    private static extern void   ResumeVoice();
    30.          
    31.             public int voice_nb = 0;
    32.             public string text;
    33.             public string stringToEdit = "Hello World";
    34.             public int value = 1;
    35.          
    36.             void Speach ()
    37.             {
    38.                 if( VoiceAvailable()>0 )
    39.                 {
    40.                     InitVoice(); // init the engine
    41.                    
    42.                     if (voice_nb > GetVoiceCount()) voice_nb = 0;
    43.                     if (voice_nb < 0) voice_nb = 0;
    44.      
    45.                     SetVoice(voice_nb); // 0 to voiceCount - 1
    46.                     SetVoiceRate(value);
    47.                    
    48.                     Say(text);
    49.                
    50.                 }
    51.                 Application.Quit();
    52.             }
    53.            
    54.             void OnDisable()
    55.             {
    56.                 if( VoiceAvailable()>0 )
    57.                 {
    58.                     FreeVoice();
    59.                 }
    60.             }
    61.            
    62.             void OnGUI(){
    63.                 GUILayout.BeginArea(new Rect(Screen.width/2 - 260, Screen.height/2 - 125, 570, 250), "", "Window");
    64.                    
    65.                     stringToEdit = GUI.TextArea(new Rect(10, 10,550, 200), stringToEdit);
    66.                    
    67.                     //Change voice
    68.                     if (GUI.Button(new Rect(10, 213, 100, 30), "Voice1")){
    69.                         voice_nb = 0;
    70.                         text = "voice changed to Default ";
    71.                         Speach();
    72.                     }
    73.                     //Change voice
    74.                     if (GUI.Button(new Rect(110, 213, 100, 30), "Voice2")){
    75.                         voice_nb = 1;
    76.                         text = "voice changed to Emily  ";
    77.                         Speach();
    78.                     }
    79.  
    80.                     //speach speed
    81.                     if (GUI.Button(new Rect(230, 213, 30, 30), "<")){
    82.                         if(value > -5){
    83.                             value --;
    84.                         }
    85.                     }
    86.                     GUI.Button(new Rect(260, 213, 50, 30), value.ToString());
    87.                     if (GUI.Button(new Rect(310, 213, 30, 30), ">")){
    88.                         if(value < 5){
    89.                             value ++;
    90.                         }
    91.                     }
    92.                    
    93.                     //Clear text from text area
    94.                     GUI.color = Color.cyan;
    95.                     if (GUI.Button(new Rect(360, 213, 100, 30), "Clear")){
    96.                         text = "";
    97.                         stringToEdit = "";
    98.                     }
    99.                     //Say text
    100.                     GUI.color = Color.green;
    101.                     if (GUI.Button(new Rect(460, 213, 100, 30), "Say")){
    102.                         text = stringToEdit;
    103.                         if(text != "") Speach();
    104.                     }
    105.                 GUILayout.EndArea();
    106.             }
    107.         }
     
  42. Shukerullah

    Shukerullah

    Joined:
    Mar 15, 2013
    Posts:
    97
    What platforms it supports?
     
  43. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,649
    It's a native DLL for Win32/64.

    It use the default system voice.
     
    Last edited: Jun 7, 2015
  44. g0tNoodles

    g0tNoodles

    Joined:
    Nov 28, 2010
    Posts:
    193
    Very nice, OneMan!
     
  45. jiangjiang

    jiangjiang

    Joined:
    Feb 17, 2014
    Posts:
    6
    hello,everyone.i want to kown if it can speak chinese?i am trying to write a project of TTS for chinese and i need help!
     
  46. SunnyShark

    SunnyShark

    Joined:
    Mar 15, 2014
    Posts:
    1
    It works fine with English.but incorrect for Chinese.
    How could I solve it?!Please.
     
  47. Yodzilla

    Yodzilla

    Joined:
    Mar 21, 2013
    Posts:
    48
    Thanks for this neat dll and script! Works great here!
     
  48. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,649
    You're welcome..
     
  49. DigitalAdam

    DigitalAdam

    Joined:
    Jul 18, 2007
    Posts:
    1,202
    Is it possible or has anyone been able to take the speech output and convert it to an audio file?
     
  50. ng_min_teck

    ng_min_teck

    Joined:
    Apr 30, 2014
    Posts:
    33
    Is it possible to just place Voice_speaker.dll at project file and path to system32/SysWOW64?