-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpeechInteractionCORE.cs
86 lines (72 loc) · 2.65 KB
/
SpeechInteractionCORE.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq; // Voice recognition dictionary
using UnityEngine.Windows.Speech; // Courtesy of https://lightbuzz.com/speech-recognition-unity/
[RequireComponent(typeof(AudioSource))]
public class SpeechInteractionCORE : MonoBehaviour
{
[Header("Array Spaghetti")] // Speech recognition system - Related arrays [ALL SHOULD MATCH YOU THICK BASTARD]
[SerializeField] private string[] _Keywords; // Array with keywords (should match TMP text)
[SerializeField] private bool[] _HasBeenCalled; // Default is false
[SerializeField] private GameObject[] _KeyLines; // TMP game object array
[SerializeField] private AudioClip[] _VoiceClips; // List of audio clips
// Speech recognition system - The rest
[Header("Voice Recog Settings")]
public ConfidenceLevel _Confidence = ConfidenceLevel.Low;
protected PhraseRecognizer _Recognizer;
protected string _WordString = "word";
[Header("Other")] // External stuffs
[SerializeField] private AudioSource _AudioSource;
// Non interfaced data
private bool _IsReactEnable = false;
private void Start()
{
// Keyword Recognizer
if (_Keywords != null)
{
_Recognizer = new KeywordRecognizer(_Keywords, _Confidence);
_Recognizer.OnPhraseRecognized += Recognizer_OnPhraseRecognized;
_Recognizer.Start();
}
}
private void Recognizer_OnPhraseRecognized(PhraseRecognizedEventArgs args)
{
_WordString = args.text;
if (_IsReactEnable == true)
{
int _IndexRetrVal = System.Array.IndexOf(_Keywords, _WordString);
if (_HasBeenCalled[_IndexRetrVal] == false)
{
_KeyLines[_IndexRetrVal].SetActive(false);
_AudioSource.PlayOneShot(_VoiceClips[_IndexRetrVal]);
_HasBeenCalled[_IndexRetrVal] = true;
}
_WordString = "NullValue"; // Prevents an infinite loop (Is there even one in this occasion??)
}
}
// Trigger stuffs
private void OnTriggerEnter(Collider col)
{
if (col.gameObject.tag == "Player")
{
_IsReactEnable = true;
}
}
private void OnTriggerExit(Collider col)
{
if (col.gameObject.tag == "Player")
{
_IsReactEnable = false;
}
}
private void OnApplicationQuit()
{
if (_Recognizer != null && _Recognizer.IsRunning)
{
Debug.Log("<color=red>Voice Recognizer is OFF</color>");
_Recognizer.OnPhraseRecognized -= Recognizer_OnPhraseRecognized; // Is this necessary?
_Recognizer.Stop();
}
}
}