Search Unity

Speech Using C#

Discussion in 'Scripting' started by Narlix, Sep 24, 2010.

  1. Narlix

    Narlix

    Joined:
    Jul 3, 2009
    Posts:
    111
    i keep getting this error. and i have no idea, what it means.


    • at slot 150: get_MMHandle (63) overrides get_MMHandle (18)
      at slot 150: set_Format (46) overrides set_Format (1)
      at slot 150: Read (47) overrides Read (2)
      at slot 150: Write (48) overrides Write (3)
      at slot 150: Seek (49) overrides Seek (4)
      at slot 150: get_Status (50) overrides get_Status (5)
      at slot 150: get_BufferInfo (51) overrides get_BufferInfo (6)
      at slot 150: get_DefaultFormat (52) overrides get_DefaultFormat (7)
      at slot 150: get_Volume (53) overrides get_Volume (8)
      at slot 150: set_Volume (54) overrides set_Volume (9)
      at slot 150: get_BufferNotifySize (55) overrides get_BufferNotifySize (10)
      at slot 150: set_BufferNotifySize (56) overrides set_BufferNotifySize (11)
      at slot 150: get_EventHandle (57) overrides get_EventHandle (12)
      at slot 150: SetState (58) overrides SetState (13)
      at slot 150: get_DeviceId (59) overrides get_DeviceId (14)
      at slot 150: set_DeviceId (60) overrides set_DeviceId (15)

    and that is just the top of it, it goes on.

    my code is

    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5. using System;
    6. using System.Collections.Generic;
    7. using System.Text;
    8. using System.Speech;
    9. using System.Speech.Recognition;
    10. using System.Speech.Synthesis;
    11. using SpeechLib;
    12.  
    13. public class AIBase : MonoBehaviour {
    14.    
    15.         public static bool Finished;
    16.  
    17.         private static SpeechRecognizer m_SpeechRecogniser = null;
    18.         private const float MinimumConfidenceLevel = 0.5f;
    19.         private const float FullyConfidentLevel = 0.75f;
    20.  
    21.         static void Awake()
    22.         {
    23.             do
    24.             {
    25.                 //string s = Console.ReadLine();
    26.            
    27.                 /*if(FoundKeyword("Exit", s))
    28.                 {
    29.                     Finished = true;
    30.                 }
    31.                 if ((FindWord("What", s) == true)  (FindWord("Name", s) == true))
    32.                 {
    33.                     Speak("My Name is Sophia.");
    34.                 }*/
    35.  
    36.                 Ask();
    37.             } while (Finished == false);
    38.         }
    39.  
    40.         static void Ask()
    41.         {
    42.  
    43.             //m_SpeechRecogniser = new SpeechRecognizer();
    44.             //m_SpeechRecogniser.LoadGrammar(new DictationGrammar());
    45.             //m_SpeechRecogniser.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(SpeechRecogniser_SpeechRecognized);
    46.  
    47.         }
    48.  
    49.         static void SpeechRecogniser_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    50.         {
    51.           //if (e.Result.Text.Length >= 1)
    52.           //  ProcessSpeech(e.Result);
    53.         }
    54.  
    55.         private static void ProcessSpeech(RecognitionResult recognitionResult)
    56.         {
    57.             //Console.WriteLine(recognitionResult.Text);
    58.             /*if (recognitionResult.Text.ToLower() == "what is your name")
    59.             {
    60.                 Speak("My name is Sofia.");
    61.             }*/
    62.             if ((FindWord("What", recognitionResult.Text.ToLower()) == true)  (FindWord("Name", recognitionResult.Text.ToLower()) == true))
    63.             {
    64.                 Speak("My Name is Sophia.");
    65.             }
    66.             if ((FindWord("What", recognitionResult.Text.ToLower()) == true)  (FindWord("Time", recognitionResult.Text.ToLower()) == true))
    67.             {
    68.                 Speak("It is Currently");
    69.             }
    70.         }
    71.  
    72.         void AskAQuestion(string Question)
    73.         {
    74.             if (FindWord("What", Question) == true)
    75.             {
    76.                 if (FindWord("Name", Question) == true)
    77.                 {
    78.                     if (FindWord("You", Question) == true)
    79.                     {
    80.                         Speak("My name is Sophia.");
    81.                     }
    82.                 }
    83.             }
    84.         }
    85.  
    86.         static void Speak(string Line)
    87.         {
    88.           SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
    89.           SpVoice Voice = new SpVoice();
    90.  
    91.           Voice.Speak(Line, SpFlags);
    92.         }
    93.  
    94.  
    95.         static bool FindWord(string find, string findin)
    96.         {
    97.           //string SearchString = "pul";
    98.           int FirstChr = -1;
    99.           FirstChr = findin.ToLower().IndexOf(find.ToLower());
    100.           if(FirstChr >= 0) {
    101.             return true;
    102.           }
    103.  
    104.           return (false);
    105.         }
    106.  
    107.         static bool FoundKeyword(string Find, string FindIn)
    108.         {
    109.           string[] Split = FindIn.Split(new Char[] { ' ' });
    110.  
    111.           //string s = Split[0].ToString() + "+" + Split[1].ToString() + "+" + Split[2].ToString() + "+" + Split[3].ToString();
    112.           //Console.WriteLine(s);
    113.           //SHOW RESULT
    114.           //MessageBox.Show(Convert.ToString(Split[0]));
    115.           //MessageBox.Show(Convert.ToString(Split[1]));
    116.           int i = 0;
    117.           //Console.WriteLine(Convert.ToString(Split[i]).ToLower());
    118.           do {
    119.             //Console.WriteLine(Split.Length.ToString());
    120.             //Console.WriteLine(i.ToString());
    121.             if (Convert.ToString(Split[i]).ToLower() == Find.ToLower())
    122.             {
    123.                 //Console.WriteLine(Convert.ToString(Split[i]).ToLower());
    124.                 return true;
    125.             }
    126.             i++;
    127.           } while(i < Split.Length);
    128.          
    129.  
    130.           return(false);
    131.         }
    132.  
    133.    
    134. }
    135.  
    if any help thanks. And yeah, i know it is the poorest ai you've ever seen, but thats not the point of the code, i want to use the speech SDK with Unity.
     
  2. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    i'm not sure if the 'System.Speech' namespace is available in Unity.
     
  3. Narlix

    Narlix

    Joined:
    Jul 3, 2009
    Posts:
    111
    i've added the link library to the asset folder. and since mono can use system.speech, so should unity.
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Not needfully
    Unity does not distribute the full mono framework only the things UT sees need for.

    In Unity 2.x / iphone 1.x its especially a really outdated mono version (1.2.5) which is on .net 2.0


    and last but not least: sound in unity is done through openal (2.5 and older) or fmod, there is no other playback type thats support nor are any os related functionality offered in this respect

    -> chance goes towards < 0 that this works and ever will work without a 3rd party plugin and standalone desktop distribution
     
  5. Narlix

    Narlix

    Joined:
    Jul 3, 2009
    Posts:
    111
    well, i guess a plugin could also be done, thanks for the info :)
     
  6. antonio_ni

    antonio_ni

    Joined:
    Oct 23, 2010
    Posts:
    2
    Hi, I also make experiments with Speech API
    I want to get SpeechSynthesis and SpeechRecognition.
    I use Speech API 5.1 on Windows XP, Unity 3.1 indie.
    This simple code for a synthesizer using peechLib.dll works. But it has not all the functionality that I want to get.
    I want to track an event ending a phrase. Add _ISpeechVoiceEvents_EndStreamEventHandler. Then Unity crash.
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System;
    4. using System.Collections;
    5. using System.Runtime.InteropServices;
    6. using SpeechLib;
    7.  
    8. public class SpLibSynthesizer : MonoBehaviour
    9. {
    10.     public string toSpeak = "Hello World";
    11.  
    12.     void Update()
    13.     {
    14.         SpLibSynt listener = new SpLibSynt();
    15.         listener.Speak(toSpeak);
    16.     }
    17. }
    18.  
    19. public class SpLibSynt
    20. {
    21.     public bool done = false;
    22.     private SpVoice Voice;
    23.  
    24.     public void Speak(string textToSpeak)
    25.     {
    26.         Voice = new SpVoice();
    27.         //Voice.EndStream += new SpeechLib._ISpeechVoiceEvents_EndStreamEventHandler(Voice_EndStream);
    28.         Voice.Speak(textToSpeak, SpeechVoiceSpeakFlags.SVSFlagsAsync);//SVSFDefault
    29.         Voice.WaitUntilDone(2000);
    30.     }
    31.  
    32.     public void Voice_EndStream(int StreamNumber, object StreamPosition)
    33.     {
    34.         done = true;
    35.         Debug.Log("Voice EndStream");
    36.     }
    37. }
    38.  
     
  7. BernieRoehl

    BernieRoehl

    Joined:
    Jun 24, 2010
    Posts:
    81
    Yes, I've encountered the same thing.

    Does anyone have any theories as to why Unity is crashing?

    I've simplified the code down to the following:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using SpeechLib;
    4.  
    5. public class TestSpeech : MonoBehaviour
    6. {
    7.     private SpVoice voice;
    8.     void Start()
    9.     {
    10.         voice = new SpVoice();
    11.         Debug.Log(new SpeechLib._ISpeechVoiceEvents_EndStreamEventHandler(Voice_EndStream));
    12.         //voice.EndStream += new SpeechLib._ISpeechVoiceEvents_EndStreamEventHandler(Voice_EndStream);
    13.         voice.Speak("Hello, world", SpeechVoiceSpeakFlags.SVSFlagsAsync);//SVSFDefault
    14.         voice.WaitUntilDone(2000);
    15.     }
    16.     public void Voice_EndStream(int StreamNumber, object StreamPosition)
    17.     {
    18.     //       Debug.Log("Voice EndStream");
    19.     }
    20. }
    If I uncomment the line that adds the handler, I get a crash at runtime.
     
    Last edited: Jan 14, 2011
  8. BernieRoehl

    BernieRoehl

    Joined:
    Jun 24, 2010
    Posts:
    81
    Even simpler example. It shows that the crash occurs not when the callback happens, but when the handler's added to the SpVoice object.


    Code (csharp):
    1. using UnityEngine;
    2. using SpeechLib;
    3.  
    4. public class TestSpeech : MonoBehaviour
    5. {
    6.     private SpVoice voice;
    7.     void Start()
    8.     {
    9.         voice = new SpVoice();
    10.         voice.EndStream += new SpeechLib._ISpeechVoiceEvents_EndStreamEventHandler(Voice_EndStream);
    11.     }
    12.     public void Voice_EndStream(int StreamNumber, object StreamPosition)
    13.     {
    14.     }
    15. }
     
  9. vdidev

    vdidev

    Joined:
    Mar 30, 2011
    Posts:
    19
    Anybody find a solution for this?
    I'm trying to get some speech recognition into my program as well, but Unity straight up crashes on this line of code

    SpeechRecognitionEngine recogEngine = new SpeechRecognitionEngine();

    no error message, nothing.

    Looking at the crash.dmp file that Unity generates, my first error is

    "ERROR: SymGetSymFromAddr64, GetLastError: 'The specified module could not be found."

    then it goes on with more "module could not be found" and "dynamic link library (DLL) initialization routine failed" errors.

    I had to manually add the System.Speech.dll file to my Unity Project. There were multiple versions on my computer, some ~270K in size, others over 600K. Only one of those .dlls would actually allow the project to compile, however, and that was the biggest of the System.Speech.dll files (at 672K or thereabouts).

    I got speech recognition up and running just fine as a WPF project. Perhaps the best solution is to send the data between programs over the network? Ugh, lame....

    If I understand correctly, System.Speech is only available starting from .NET 3.0, and Unity only has support for 2.0? Is there any way around this limitation?

    Thank you for any help!
     
  10. vdidev

    vdidev

    Joined:
    Mar 30, 2011
    Posts:
    19
    Whelp, in case anybody was wondering, you can get speech recognition running just fine if you write a standalone program that processes the speech recognition, sends it over the network, and then write a script in unity to handle the data received.

    If you plan on serializing your data, you should create a separate Class Library that defines the data type that you're sending, and reference the .dll file in your server and client applications. And of course, you should build your Class Library datatype using a .NET 2.0 target.

    I think that just about covers the problems I encountered...
     
  11. Freddy888

    Freddy888

    Joined:
    Sep 13, 2014
    Posts:
    165
    @BernieRoehl

    Hi, I know this is some time ago, but I also have the event handler problem. Did you find a solution at all please ?

    Cheers :)
     
  12. bluesyhfang

    bluesyhfang

    Joined:
    Apr 4, 2014
    Posts:
    8
    @Freddy888

    Put "Using System;" before your class should solve the event handler problem. Hope this helps.

    However, I got another even trickier problem. The console says as follows:

    Internal compiler error. See the console log for more information. output was: Unhandled Exception: System.TypeLoadException: Could not load type 'System.Runtime.CompilerServices.ReferenceAssemblyAttribute' from assembly 'System.Speech'.

    at System.MonoCustomAttrs.GetCustomAttributesBase (ICustomAttributeProvider obj, System.Type attributeType) [0x00000] in <filename unknown>:0

    at System.MonoCustomAttrs.GetCustomAttributes (ICustomAttributeProvider obj, System.Type attributeType, Boolean inherit) [0x00000] in <filename unknown>:0

    at System.Reflection.Assembly.GetCustomAttributes (System.Type attributeType, Boolean inherit) [0x00000] in <filename unknown>:0

    Do you know what's the problem here?
     
  13. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    974
    Looks like there is a ReferenceAssemblyAttribute on the library you are using. Unfortunately, this is a .net 4.0 feature, so it cannot be compiled by the Unity mono compiler which only supports .net 3.5.

    From the msdn docs, it looks like System.Speech is also a .net 4.0 feature, so this isn't surprising. Maybe I am wrong?
     
  14. William_Lee_Sims

    William_Lee_Sims

    Joined:
    Oct 11, 2014
    Posts:
    40
  15. EyePD

    EyePD

    Joined:
    Feb 26, 2016
    Posts:
    63
    Speech is now built into 5.4 (currently in beta). For more information see the Microsoft page.