forked from watson-developer-cloud/unity-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpeechToTextWidget.cs
More file actions
215 lines (191 loc) · 5.94 KB
/
Copy pathSpeechToTextWidget.cs
File metadata and controls
215 lines (191 loc) · 5.94 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
* Copyright 2015 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
//#define ENABLE_DEBUGGING
using IBM.Watson.DeveloperCloud.DataTypes;
using IBM.Watson.DeveloperCloud.Services.SpeechToText.v1;
using IBM.Watson.DeveloperCloud.Logging;
using UnityEngine;
using UnityEngine.UI;
using IBM.Watson.DeveloperCloud.Utilities;
#pragma warning disable 414
namespace IBM.Watson.DeveloperCloud.Widgets
{
/// <summary>
/// SpeechToText Widget that wraps the SpeechToText service.
/// </summary>
public class SpeechToTextWidget : Widget
{
#region Inputs
[SerializeField]
private Input m_AudioInput = new Input("Audio", typeof(AudioData), "OnAudio");
[SerializeField]
private Input m_LanguageInput = new Input("Language", typeof(LanguageData), "OnLanguage");
#endregion
#region Outputs
[SerializeField]
private Output m_ResultOutput = new Output(typeof(SpeechToTextData), true);
#endregion
#region Private Data
private SpeechToText m_SpeechToText = new SpeechToText();
[SerializeField]
private Text m_StatusText = null;
[SerializeField]
private bool m_DetectSilence = true;
[SerializeField]
private float m_SilenceThreshold = 0.03f;
[SerializeField]
private bool m_WordConfidence = false;
[SerializeField]
private bool m_TimeStamps = false;
[SerializeField]
private int m_MaxAlternatives = 1;
[SerializeField]
private bool m_EnableContinous = false;
[SerializeField]
private bool m_EnableInterimResults = false;
[SerializeField]
private Text m_Transcript = null;
[SerializeField, Tooltip("Language ID to use in the speech recognition model.")]
private string m_Language = "en-US";
#endregion
#region Public Properties
/// <summary>
/// This property starts or stop's this widget listening for speech.
/// </summary>
public bool Active
{
get { return m_SpeechToText.IsListening; }
set
{
if (value && !m_SpeechToText.IsListening)
{
m_SpeechToText.DetectSilence = m_DetectSilence;
m_SpeechToText.EnableWordConfidence = m_WordConfidence;
m_SpeechToText.EnableTimestamps = m_TimeStamps;
m_SpeechToText.SilenceThreshold = m_SilenceThreshold;
m_SpeechToText.MaxAlternatives = m_MaxAlternatives;
m_SpeechToText.EnableContinousRecognition = m_EnableContinous;
m_SpeechToText.EnableInterimResults = m_EnableInterimResults;
m_SpeechToText.OnError = OnError;
m_SpeechToText.StartListening(OnRecognize);
if (m_StatusText != null)
m_StatusText.text = "LISTENING";
}
else if (!value && m_SpeechToText.IsListening)
{
m_SpeechToText.StopListening();
if (m_StatusText != null)
m_StatusText.text = "READY";
}
}
}
#endregion
#region Widget Interface
/// <exclude />
protected override string GetName()
{
return "SpeechToText";
}
#endregion
#region Event handlers
/// <summary>
/// Button handler to toggle the active state of this widget.
/// </summary>
public void OnListenButton()
{
Active = !Active;
}
/// <exclude />
protected override void Start()
{
base.Start();
if (m_StatusText != null)
m_StatusText.text = "READY";
if (!m_SpeechToText.GetModels(OnGetModels))
Log.Error("SpeechToTextWidget", "Failed to request models.");
}
private void OnDisable()
{
if (Active)
Active = false;
}
private void OnError(string error)
{
Active = false;
if (m_StatusText != null)
m_StatusText.text = "ERROR: " + error;
}
private void OnAudio(Data data)
{
if (!Active)
Active = true;
m_SpeechToText.OnListen((AudioData)data);
}
private void OnLanguage(Data data)
{
LanguageData language = data as LanguageData;
if (language == null)
throw new WatsonException("Unexpected data type");
if (!string.IsNullOrEmpty(language.Language))
{
m_Language = language.Language;
if (!m_SpeechToText.GetModels(OnGetModels))
Log.Error("SpeechToTextWidget", "Failed to rquest models.");
}
}
private void OnGetModels(Model[] models)
{
if (models != null)
{
Model bestModel = null;
foreach (var model in models)
{
if (model.language.StartsWith(m_Language)
&& (bestModel == null || model.rate > bestModel.rate))
{
bestModel = model;
}
}
if (bestModel != null)
{
Log.Status("SpeechToTextWidget", "Selecting Recognize Model: {0} ", bestModel.name);
m_SpeechToText.RecognizeModel = bestModel.name;
}
}
}
private void OnRecognize(SpeechRecognitionEvent result)
{
m_ResultOutput.SendData(new SpeechToTextData(result));
if (result != null && result.results.Length > 0)
{
if (m_Transcript != null)
m_Transcript.text = "";
foreach (var res in result.results)
{
foreach (var alt in res.alternatives)
{
string text = alt.transcript;
if (m_Transcript != null)
m_Transcript.text += string.Format("{0} ({1}, {2:0.00})\n",
text, res.final ? "Final" : "Interim", alt.confidence);
}
}
}
}
#endregion
}
}