forked from ispikit/ispikit-unity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIspikit.cs
277 lines (266 loc) · 10.4 KB
/
Ispikit.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
using System;
using System.Timers;
// This is boilerplate code to start coding using the Ispikit Unity plugin
// On iOS, it assumes it belongs to a "GameObject" game object
[RequireComponent (typeof (AudioSource))]
public class Ispikit : MonoBehaviour {
// Below are the available plugin calls for all supported platforms
#if UNITY_IOS
[DllImport("__Internal")]
private static extern int startInitialization(string callbackGameObjectName, string callbackMethodName);
[DllImport("__Internal")]
private static extern int setPlaybackDoneCallback(string callbackGameObjectName, string callbackMethodName);
[DllImport("__Internal")]
private static extern int setResultCallback(string callbackGameObjectName, string callbackMethodName);
[DllImport("__Internal")]
private static extern int setCompletionCallback(string callbackGameObjectName, string callbackMethodName);
[DllImport("__Internal")]
private static extern int setNewWordsCallback(string callbackGameObjectName, string callbackMethodName);
[DllImport("__Internal")]
private static extern int setNewAudioCallback(string callbackGameObjectName, string callbackMethodName);
[DllImport("__Internal")]
private static extern int startRecording(string sentences);
[DllImport("__Internal")]
private static extern int stopRecording();
[DllImport("__Internal")]
private static extern int setStrictness(int strictness);
[DllImport("__Internal")]
private static extern int startPlayback();
[DllImport("__Internal")]
private static extern int stopPlayback();
[DllImport("__Internal")]
private static extern int addWord(string word, string pronunciation);
#elif UNITY_ANDROID
public delegate void initCallbackDelegate(int n);
public delegate void resultCallbackDelegate(int score, int speed, string words);
public delegate void completionCallbackDelegate(int completion);
public delegate void newWordsCallbackDelegate(string words);
public delegate void newAudioCallbackDelegate(int volume, string pitch, string waveform);
public delegate void playbackDoneCallbackDelegate();
[DllImport("upal")]
private static extern int startInitialization(initCallbackDelegate icb, string path);
[DllImport("upal")]
private static extern int setPlaybackDoneCallback(playbackDoneCallbackDelegate pcb);
[DllImport("upal")]
private static extern int setResultCallback(resultCallbackDelegate rcb);
[DllImport("upal")]
private static extern int setCompletionCallback(completionCallbackDelegate ccb);
[DllImport("upal")]
private static extern int setNewWordsCallback(newWordsCallbackDelegate nwcb);
[DllImport("upal")]
private static extern int setNewAudioCallback(newAudioCallbackDelegate nacb);
[DllImport("upal")]
private static extern int startRecording(string sentences);
[DllImport("upal")]
private static extern int stopRecording();
[DllImport("upal")]
private static extern int setStrictness(int strictness);
[DllImport("upal")]
private static extern int startPlayback();
[DllImport("upal")]
private static extern int stopPlayback();
[DllImport("upal")]
private static extern int addWord(string word, string pronunciation);
#elif UNITY_STANDALONE_OSX
public delegate void initCallbackDelegate(int n);
public delegate void resultCallbackDelegate(int score, int speed, string words);
public delegate void completionCallbackDelegate(int completion);
public delegate void newWordsCallbackDelegate(string words);
public delegate void newAudioCallbackDelegate(int volume, string pitch, string waveform);
public delegate void playbackDoneCallbackDelegate();
[DllImport("ispikit")]
private static extern int startInitialization(initCallbackDelegate icb, string path);
[DllImport("ispikit")]
private static extern int setPlaybackDoneCallback(playbackDoneCallbackDelegate pcb);
[DllImport("ispikit")]
private static extern int setResultCallback(resultCallbackDelegate rcb);
[DllImport("ispikit")]
private static extern int setCompletionCallback(completionCallbackDelegate ccb);
[DllImport("ispikit")]
private static extern int setNewWordsCallback(newWordsCallbackDelegate nwcb);
[DllImport("ispikit")]
private static extern int setNewAudioCallback(newAudioCallbackDelegate nacb);
[DllImport("ispikit")]
private static extern int startRecording(string sentences);
[DllImport("ispikit")]
private static extern int stopRecording();
[DllImport("ispikit")]
private static extern int setStrictness(int strictness);
[DllImport("ispikit")]
private static extern int startPlayback();
[DllImport("ispikit")]
private static extern int stopPlayback();
[DllImport("ispikit")]
private static extern int addWord(string word, string pronunciation);
#endif
private static System.Timers.Timer timer;
// We need to keep this information
// for desktop platforms otherwise the timer
// keep running in the background
private static bool stopped;
void Awake()
{
Debug.Log ("About to initialize plugin");
stopped = false;
#if UNITY_IOS
string gameObjectName = "GameObject";
string callbackName = "initCallback";
// When calling the startInitialization function, we provide the name
// of the Game Object and Callback for when init is done
Debug.Log (startInitialization(gameObjectName, callbackName));
#elif UNITY_ANDROID || UNITY_STANDALONE_OSX
Debug.Log (startInitialization(new initCallbackDelegate( this.initCallback ), Application.persistentDataPath));
#endif
}
void StopAll()
{
Debug.Log ("Stopping all");
stopped = true;
stopRecording ();
stopPlayback ();
}
void OnApplicationQuit()
{
StopAll ();
}
void Start () {
}
void Update () {
}
#if UNITY_IOS
public void initCallback(string status) {
#elif UNITY_ANDROID || UNITY_STANDALONE_OSX
public void initCallback(int status) {
#endif
// This is for when plugin is initialized, status should be "0"
// if successful
Debug.Log ("Plugin initialization done");
Debug.Log (status);
// We now register all callbacks
#if UNITY_IOS
setPlaybackDoneCallback ("GameObject", "playbackDoneCallback");
setResultCallback ("GameObject", "resultCallback");
setCompletionCallback ("GameObject", "completionCallback");
setNewWordsCallback ("GameObject", "newWordsCallback");
setNewAudioCallback ("GameObject", "newAudioCallback");
#elif UNITY_ANDROID || UNITY_STANDALONE_OSX
Debug.Log ("About to wire callbacks");
setResultCallback (new resultCallbackDelegate( this.resultCallback ));
setCompletionCallback (new completionCallbackDelegate( this.completionCallback ));
setNewWordsCallback (new newWordsCallbackDelegate( this.newWordsCallback ));
setNewAudioCallback (new newAudioCallbackDelegate( this.newAudioCallback ));
setPlaybackDoneCallback (new playbackDoneCallbackDelegate( this.playbackDoneCallback ));
#endif
// We then start recording, with three possible inputs: "first", "second", and "third"
startRecording ("first,second,third");
Debug.Log ("Starting recording");
// Three seconds later, we wiĺl stop recording in the provided onRecordingDone function
if (stopped)
return;
timer = new System.Timers.Timer (3000);
timer.Elapsed += onRecordingDone;
timer.AutoReset = false;
timer.Enabled = true;
}
public static void playbackDoneCallback() {
// This is called once playback is done. It will start another timer
// after which recording will start again.
Debug.Log ("Playback Done");
if (stopped)
return;
timer = new System.Timers.Timer (1000);
timer.Elapsed += onRestarting;
timer.AutoReset = false;
timer.Enabled = true;
}
private static void onRecordingDone(object source, ElapsedEventArgs e) {
// This just stops recording. In the background, analysis will start
// and result callback will be called once done.
Debug.Log ("Stopping recording");
stopRecording ();
}
#if UNITY_IOS
public void resultCallback(string status) {
#elif UNITY_ANDROID || UNITY_STANDALONE_OSX
public static void resultCallback(int score, int speed, string words) {
#endif
// Callback when result is available, a few seconds after stopRecording, typically.
// See docs on how to parse the result.
Debug.Log ("Result");
#if UNITY_IOS
Debug.Log (status);
#elif UNITY_ANDROID || UNITY_STANDALONE_OSX
Debug.Log (score);
Debug.Log (speed);
Debug.Log (words);
#endif
// Starts replaying the userś voice after one second
if (stopped)
return;
timer = new System.Timers.Timer (1000);
timer.Elapsed += onStartPlayback;
timer.AutoReset = false;
timer.Enabled = true;
}
private static void onStartPlayback(object source, ElapsedEventArgs e) {
// This just starts playing back previously recorded audio
// once audio is played back, it calls back using the provide callback
// function
Debug.Log ("Starting playback");
startPlayback();
}
private static void onRestarting(object source, ElapsedEventArgs e) {
// This is a function to be called by a timer to start another recognition
// This boilerplate application will continue looping indefinitely through
// recording - analysis - playback cycles
Debug.Log ("Starting recording again");
if (stopped)
return;
startRecording ("I am learning English,hello goodbye,one two three four");
timer = new System.Timers.Timer (3000);
timer.Elapsed += onRecordingDone;
timer.AutoReset = false;
timer.Enabled = true;
}
#if UNITY_IOS
public void completionCallback(string status) {
#elif UNITY_ANDROID || UNITY_STANDALONE_OSX
public static void completionCallback(int completion) {
#endif
// This callback is called during analysis
// Status is between "0" and "100", it is the percentage of completion of
// analysis, it can be used to display a progress bar.
Debug.Log ("Completion");
#if UNITY_IOS
Debug.Log (status);
#elif UNITY_ANDROID || UNITY_STANDALONE_OSX
Debug.Log (completion);
#endif
}
public static void newWordsCallback(string words) {
// This callback comes during recording, it gives the words recognized
// see docs on how to parse the string
Debug.Log ("New words");
Debug.Log (words);
}
#if UNITY_IOS
public void newAudioCallback(string status) {
#elif UNITY_ANDROID || UNITY_STANDALONE_OSX
public static void newAudioCallback(int volume, string pitch, string waveform) {
#endif
// This callback also comes during recording, it gives data about the recording
// that can be used for UI effects: audio volume, pitch and waveform
// see docs on how to parse it
Debug.Log ("New audio data");
#if UNITY_IOS
Debug.Log (status);
#elif UNITY_ANDROID || UNITY_STANDALONE_OSX
Debug.Log (volume);
Debug.Log (pitch);
Debug.Log (waveform);
#endif
}
}