ItsSmudge

Speech

Jun 20th, 2020
66
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using UnityEngine.Windows.Speech;
  3. using System.Linq;
  4. using WindowsInput;
  5. using System.Collections.Generic;
  6.  
  7. public class Speech : MonoBehaviour
  8. {
  9.     InputSimulator IS;
  10.     KeywordRecognizer keywordRecognizer;
  11.     Dictionary<string, System.Action> keywords = new Dictionary<string, System.Action>();
  12.  
  13.  
  14.     public AudioClip sound;
  15.     static AudioSource audioSrc;
  16.  
  17.     void Start()
  18.     {
  19.         audioSrc = GetComponent<AudioSource>();
  20.  
  21.         IS = new InputSimulator();
  22.         keywords.Add("mute", () =>
  23.         {
  24.             simulateDiscordMute();
  25.         });
  26.  
  27.         keywords.Add("unmute", () =>
  28.         {
  29.             simulateDiscordUnmute();
  30.         });
  31.         keywords.Add("test", () =>
  32.         {
  33.             testSound();
  34.         });
  35.         keywordRecognizer = new KeywordRecognizer(keywords.Keys.ToArray());
  36.         keywordRecognizer.OnPhraseRecognized += KeywordRecognizerOnPhraseRecognized;
  37.         keywordRecognizer.Start();
  38.     }
  39.  
  40.     void KeywordRecognizerOnPhraseRecognized(PhraseRecognizedEventArgs args) {
  41.  
  42.         System.Action keywordAction;
  43.  
  44.         if (keywords.TryGetValue(args.text, out keywordAction))
  45.         {
  46.             keywordAction.Invoke();
  47.         }
  48.    
  49.     }
  50.  
  51.     private void testSound() {
  52.  
  53.         audioSrc.PlayOneShot(sound);
  54.    
  55.     }
  56.     private void simulateDiscordMute()
  57.     {
  58.         IS.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.SEPARATOR);
  59.         Debug.Log("muted");
  60.     }
  61.     private void simulateDiscordUnmute()
  62.     {
  63.         IS.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.SEPARATOR);
  64.         Debug.Log("unmuted");
  65.     }
  66. }
RAW Paste Data