/
SttEngine.cs
executable file
·878 lines (817 loc) · 38 KB
/
SttEngine.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
/*
* Copyright (c) 2016 Samsung Electronics Co., Ltd 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.
*/
using System;
using System.Runtime.InteropServices;
using static Interop.SttEngine;
namespace Tizen.Uix.SttEngine
{
/// <summary>
/// Enumeration for the audio types.
/// </summary>
/// <since_tizen> 4 </since_tizen>
public enum AudioType
{
/// <summary>
/// Signed 16-bit audio type, little endian.
/// </summary>
PcmS16Le = 0,
/// <summary>
/// Unsigned 8-bit audio type.
/// </summary>
PcmU8
};
/// <summary>
/// Enumeration for the results.
/// </summary>
/// <since_tizen> 4 </since_tizen>
public enum ResultEvent
{
/// <summary>
/// Event when either the full matched or the final result is delivered.
/// </summary>
FinalResult = 0,
/// <summary>
/// Event when the partial matched result is delivered.
/// </summary>
PartialResult,
/// <summary>
/// Event when the recognition has failed.
/// </summary>
Error
};
/// <summary>
/// Enumeration for the result time.
/// </summary>
/// <since_tizen> 4 </since_tizen>
public enum TimeEvent
{
/// <summary>
/// Event when the token is beginning type.
/// </summary>
Beginning = 0,
/// <summary>
/// Event when the token is middle type.
/// </summary>
Middle = 1,
/// <summary>
/// Event when the token is end type.
/// </summary>
End = 2
};
/// <summary>
/// Enumeration for the speech status.
/// </summary>
/// <since_tizen> 4 </since_tizen>
public enum SpeechStatus
{
/// <summary>
/// Beginning point of the speech is detected.
/// </summary>
BeginningPointDetected = 0,
/// <summary>
/// End point of the speech is detected.
/// </summary>
EndPointDetected
};
/// <summary>
/// Enumeration for representing the result message.
/// </summary>
/// <since_tizen> 4 </since_tizen>
public enum ResultMessage
{
/// <summary>
/// No error.
/// </summary>
None,
/// <summary>
/// Recognition failed because the speech started too soon.
/// </summary>
TooSoon,
/// <summary>
/// Recognition failed because the speech is too short.
/// </summary>
TooShort,
/// <summary>
/// Recognition failed because the speech is too long.
/// </summary>
TooLong,
/// <summary>
/// Recognition failed because the speech is too quiet to listen.
/// </summary>
TooQuiet,
/// <summary>
/// Recognition failed because the speech is too loud to listen.
/// </summary>
TooLoud,
/// <summary>
/// Recognition failed because the speech is too fast to listen.
/// </summary>
TooFast
};
/// <summary>
/// Enumeration for the error values that can occur.
/// </summary>
/// <since_tizen> 4 </since_tizen>
public enum Error
{
/// <summary>
/// Successful, no error.
/// </summary>
None = ErrorCode.None,
/// <summary>
/// Out of memory.
/// </summary>
OutOfMemory = ErrorCode.OutOfMemory,
/// <summary>
/// I/O error.
/// </summary>
IoError = ErrorCode.IoError,
/// <summary>
/// Invalid parameter.
/// </summary>
InvalidParameter = ErrorCode.InvalidParameter,
/// <summary>
/// Network down (Out of network).
/// </summary>
NetworkDown = ErrorCode.NetworkDown,
/// <summary>
/// Invalid state.
/// </summary>
InvalidState = ErrorCode.InvalidState,
/// <summary>
/// Invalid language.
/// </summary>
InvalidLanguage = ErrorCode.InvalidLanguage,
/// <summary>
/// Operation failed.
/// </summary>
OperationFailed = ErrorCode.OperationFailed,
/// <summary>
/// Not supported feature of the current engine.
/// </summary>
NotSupportedFeature = ErrorCode.NotSupportedFeature,
/// <summary>
/// Not supported.
/// </summary>
NotSupported = ErrorCode.NotSupported,
/// <summary>
/// Permission denied.
/// </summary>
PermissionDenied = ErrorCode.PermissionDenied,
/// <summary>
/// Recording timed out.
/// </summary>
RecordingTimedOut = ErrorCode.RecordingTimedOut
};
/// <summary>
/// This class represents the STT Engine, which has to be inherited to make the engine.
/// </summary>
/// <since_tizen> 4 </since_tizen>
public abstract class Engine
{
private CallbackStructGCHandle _callbackStructGCHandle = new CallbackStructGCHandle();
private PrivateDataSetCb _privateDataSetCb;
private Action<string> _privateDatacallback;
private PrivateDataRequestedCb _privateDataRequestedCb;
private OutAction<string> _privateDataRequestedCallback;
private static Engine _engine;
private IntPtr _structIntPtrHandle;
/// <summary>
/// An action with 2 input parameters returning an error.
/// </summary>
/// <typeparam name="T">Generic type for parameter 1.</typeparam>
/// <param name="a">The input parameter 1.</param>
/// <param name="b">The input parameter 2.</param>
/// <returns>Error value.</returns>
/// <since_tizen> 4 </since_tizen>
public delegate Error Action<T>(T a, T b);
/// <summary>
/// An action with 2 out parameters returning an error.
/// </summary>
/// <typeparam name="T">Generic type for parameter 1.</typeparam>
/// <param name="a">The input parameter 1.</param>
/// <param name="b">The input parameter 2.</param>
/// <returns>Error value.</returns>
/// <since_tizen> 4 </since_tizen>
public delegate Error OutAction<T>(T a, out T b);
/// <summary>
/// Called when the STT engine provides the time stamp of result to the engine service user.
/// This callback function is implemented by the engine service user. Therefore, the engine developer does NOT have to implement this callback function.
/// </summary>
/// <param name="index">The result index.</param>
/// <param name="resultEvent">The token event.</param>
/// <param name="text">The result text.</param>
/// <param name="startTime">The time started speaking the result text.</param>
/// <param name="endTime">The time finished speaking the result text.</param>
/// <param name="userData">The user data.</param>
/// <returns>true to continue with the next iteration of the loop, false to break out of the loop.</returns>
/// <precondition>SendResult() should be called.</precondition>
/// <since_tizen> 4 </since_tizen>
public delegate bool ResultTime(int index, TimeEvent resultEvent, string text, long startTime, long endTime, IntPtr userData);
/// <summary>
/// Called when the STT engine informs the engine service user about the whole supported language list.
/// This callback function is implemented by the engine service user. Therefore, the engine developer does NOT have to implement this callback function.
/// </summary>
/// <param name="language">The language is specified as an ISO 3166 alpha-2 two letter country-code
/// followed by an ISO 639-1 for the two-letter language code.
/// For example, "ko_KR" for Korean, "en_US" for American English.</param>
/// <param name="userData">The user data.</param>
/// <returns>true to continue with the next iteration of the loop, false to break out of the loop.</returns>
/// <precondition>ForEachSupportedLanguages() should be called.</precondition>
/// <since_tizen> 4 </since_tizen>
public delegate bool SupportedLanguages(string language, IntPtr userData);
/// <summary>
/// Called when the engine service user requests the basic information of the STT engine.
/// </summary>
/// <remarks>
/// In order to upload the engine at Tizen Appstore, both a service application and a UI application are necessary. Therefore, engineSetting must be transferred to the engine service user.
/// </remarks>
/// <param name="engineUuid">UUID of the engine.</param>
/// <param name="engineName">Name of the engine.</param>
/// <param name="engineSetting">The engine setting application (UI app)'s ID.</param>
/// <param name="useNetwork">A variable for checking whether the network is used or not.</param>
/// <returns>
/// The following error codes can be returned:
/// 1. None
/// 2. OperationFailed
/// 3. InvalidParameter
/// </returns>
/// <since_tizen> 4 </since_tizen>
public abstract Error GetInformation(out string engineUuid, out string engineName, out string engineSetting, out bool useNetwork);
/// <summary>
/// Called when the engine service user initializes the STT engine.
/// This callback function is called by the engine service user to request for the STT engine to be started.
/// </summary>
/// <returns>
/// The following error codes can be returned:
/// 1. None
/// 2. InvalidParameter
/// 3. InvalidState
/// 4. OperationFailed
/// </returns>
/// <since_tizen> 4 </since_tizen>
public abstract Error Initialize();
/// <summary>
/// Called when the engine service user deinitializes the STT engine.
/// This callback function is called by the engine service user to request for the STT engine to be deinitialized.
/// </summary>
/// <returns>
/// The following error codes can be returned:
/// 1. None
/// 2. InvalidState
/// </returns>
/// <since_tizen> 4 </since_tizen>
public abstract Error Deinitialize();
/// <summary>
/// Called when the engine service user gets the whole supported language list.
/// </summary>
/// <remarks>
/// In this function, the engine service user's callback function 'SupportedLanguages' is invoked repeatedly for getting all the supported languages
/// and user_data must be transferred to 'SupportedLanguages'. If 'SupportedLanguages' returns false, it should be stopped to call 'SupportedLanguages'.
/// </remarks>
/// <param name="callback">The callback function,</param>
/// <param name="userData">The user data, which must be passed to the callback delegate 'SupportedLanguages'.</param>
/// <returns>
/// The following error codes can be returned:
/// 1. None
/// 2. InvalidState
/// 3. InvalidParameter
/// </returns>
/// <postcondition>
/// This callback function invokes SupportedLanguages repeatedly for getting the supported languages.
/// </postcondition>
/// <since_tizen> 4 </since_tizen>
public abstract Error ForEachSupportedLanguages(SupportedLanguages callback, IntPtr userData);
/// <summary>
/// Called when the engine service user checks whether the corresponding language is valid or not in the STT engine.
/// </summary>
/// <param name="language">The language is specified as an ISO 3166 alpha-2 two letter country-code followed by an ISO 639-1 for the two-letter language code.
/// For example, "ko_KR" for Korean, "en_US" for American English.</param>
/// <param name="isValid">A variable for checking whether the corresponding language is valid or not. true to be valid, false to be invalid.</param>
/// <returns>
/// The following error codes can be returned:
/// 1. None
/// 2. InvalidParameter
/// </returns>
/// <since_tizen> 4 </since_tizen>
public abstract Error IsValidLanguage(string language, out bool isValid);
/// <summary>
/// Called when the engine service user checks whether the STT engine supports silence detection.
/// </summary>
/// <returns>true to support silence detection, false not to support silence detection.</returns>
/// <since_tizen> 4 </since_tizen>
public abstract bool SupportSilenceDetection();
/// <summary>
/// Called when the engine service user checks whether the STT engine supports the corresponding recognition type.
/// </summary>
/// <param name="type">The type for recognition, "stt.recognition.type.FREE", or "stt.recognition.type.FREE.PARTIAL".</param>
/// <param name="isSupported">A variable for checking whether the STT engine supports the corresponding recognition type.
/// true to support the recognition type, false not to support the recognition type.</param>
/// <returns>
/// The following error codes can be returned:
/// 1. None
/// 2. InvalidParameter
/// </returns>
/// <since_tizen> 4 </since_tizen>
public abstract Error SupportRecognitionType(string type, out bool isSupported);
/// <summary>
/// Called when the engine service user gets the proper recording format of the STT engine.
/// The recording format is used for creating the recorder.
/// </summary>
/// <param name="types">The format used by the recorder.</param>
/// <param name="rate">The sample rate used by the recorder.</param>
/// <param name="channels">The number of channels used by the recorder.</param>
/// <returns>
/// The following error codes can be returned:
/// 1. None
/// 2. InvalidState
/// </returns>
/// <since_tizen> 4 </since_tizen>
public abstract Error GetRecordingFormat(out AudioType types, out int rate, out int channels);
/// <summary>
/// Called when the engine service user sets the silence detection.
/// If the engine service user sets this option as 'TRUE', the STT engine will detect the silence (EPD) and send the callback event about it.
/// </summary>
/// <param name="isSet">A variable for setting the silence detection. true to detect the silence, false not to detect the silence.</param>
/// <returns>
/// The following error codes can be returned:
/// 1. None
/// 2. InvalidState
/// 3. NotSupportedFeature
/// </returns>
/// <since_tizen> 4 </since_tizen>
public abstract Error SetSilenceDetection(bool isSet);
/// <summary>
/// Called when the engine service user requests for the STT engine to check whether the application agreed the usage of the STT engine.
/// This callback function is called when the engine service user requests for the STT engine to check the application's agreement about using the engine.
/// According to the need, the engine developer can provide some user interfaces to check the agreement.
/// </summary>
/// <param name="appid">The Application ID</param>
/// <param name="isAgreed">A variable for checking whether the application agreed to use the STT engine or not. true to agree, false to disagree.</param>
/// <returns>
/// The following error codes can be returned:
/// 1. None
/// 2. InvalidState
/// 3. NotSupportedFeature
/// </returns>
/// <since_tizen> 4 </since_tizen>
public abstract Error CheckAppAgreed(string appid, out bool isAgreed);
/// <summary>
/// Called when the engine service user checks whether STT engine needs the application's credential.
/// </summary>
/// <returns>true if the STT engine needs the application's credential, otherwise false.</returns>
/// <since_tizen> 4 </since_tizen>
public abstract bool NeedAppCredential();
/// <summary>
/// Called when the engine service user gets the result time information(stamp).
/// </summary>
/// <remarks>
/// In this function, the engine service user's callback delegate 'ResultTime' is invoked repeatedly for sending the time information to the engine service user
/// and user_data must be transferred to 'ResultTime'. If 'ResultTime' returns false, it should be stopped to call 'ResultTime'.
/// timeInfo is transferred from SendResult. The type of timeInfo is up to the STT engine developer.
/// </remarks>
/// <param name="timeInfo">The time information.</param>
/// <param name="callback">The callback function.</param>
/// <param name="userData">The user data, which must be passed to the callback function ResultTime.</param>
/// <returns>
/// The following error codes can be returned:
/// 1. None
/// 2. InvalidState
/// 3. InvalidParameter
/// </returns>
/// <precondition>
/// SendResult will invoke this function.
/// </precondition>
/// <postcondition>
/// This function invokes the ResultTime repeatedly for getting the result time information.
/// </postcondition>
/// <since_tizen> 4 </since_tizen>
public abstract Error ForEachResultTime(IntPtr timeInfo, ResultTime callback, IntPtr userData);
/// <summary>
/// Called when the engine service user starts to recognize the recording data.
/// In this callback function, the STT engine must transfer the recognition result and userData to the engine service user using SendResult().
/// Also, if the STT engine needs the application's credential, it sets the credential granted to the application.
/// </summary>
/// <param name="language">The language is specified as an ISO 3166 alpha-2 two letter country-code followed by an ISO 639-1 for the two-letter language code.
/// For example, "ko_KR" for Korean, "en_US" for American English.</param>
/// <param name="type">The recognition type, "stt.recognition.type.FREE", or "stt.recognition.type.FREE.PARTIAL".</param>
/// <param name="appid">The application ID.</param>
/// <param name="credential">The credential granted to the application.</param>
/// <param name="userData">The user data to be passed to the callback function.</param>
/// <returns>
/// The following error codes can be returned:
/// 1. None
/// 2. InvalidState
/// 3. InvalidParameter
/// 4. InvalidLanguage
/// 5. OperationFailed
/// 6. NetworkDown
/// </returns>
/// <precondition>
/// The engine is not in recognition processing.
/// </precondition>
/// <since_tizen> 4 </since_tizen>
public abstract Error Start(string language, string type, string appid, string credential, IntPtr userData);
/// <summary>
/// Called when the engine service user sets and sends the recording data for speech recognition.
/// This callback function is called by the engine service user to send the recording data to the STT engine. The engine receives the recording data and uses for speech recognition.
/// This function should be returned immediately after recording data copy.
/// </summary>
/// <param name="data">The recording data.</param>
/// <param name="length">The length of the recording data.</param>
/// <returns>
/// The following error codes can be returned:
/// 1. None
/// 2. InvalidState
/// 3. InvalidParameter
/// 4. OperationFailed
/// </returns>
/// <precondition>
/// Start should succeed.</precondition>
/// <postcondition>
/// If the engine supports partial result, SendResult() should be invoked.</postcondition>
/// <since_tizen> 4 </since_tizen>
public abstract Error SetRecordingData(string data, uint length);
/// <summary>
/// Called when the engine service user stops to recognize the recording data.
/// This callback function is called by the engine service user to stop recording and to get the recognition result.
/// </summary>
/// <returns>The following error codes can be returned:
/// 1. None
/// 2. InvalidState
/// 3. OperationFailed
/// 4. NetworkDown
/// </returns>
/// <precondition>
/// Start should succeed.</precondition>
/// <postcondition>
/// After processing of the engine, SendResult() should be invoked.</postcondition>
/// <since_tizen> 4 </since_tizen>
public abstract Error Stop();
/// <summary>
/// Called when the engine service user cancels to recognize the recording data.
/// This callback function is called by the engine service user to cancel to recognize the recording data. Also, when starting the recorder is failed, this function is called.
/// </summary>
/// <returns>
/// The following error codes can be returned:
/// 1. None
/// 2. InvalidState
/// </returns>
/// <precondition>The STT engine is in recognition processing or recording.</precondition>
/// <since_tizen> 4 </since_tizen>
public abstract Error Cancel();
/// <summary>
/// Public constructor.
/// </summary>
/// <feature>
/// http://tizen.org/feature/speech.recognition
/// http://tizen.org/feature/microphone
/// </feature>
/// <since_tizen> 4 </since_tizen>
public Engine()
{
_engine = this;
}
/// <summary>
/// Main function for the Speech-To-Text (STT) engine.
/// This function is the main function for operating the STT engine.
/// </summary>
/// <privilege>
/// http://tizen.org/privilege/recorder
/// </privilege>
/// <feature>
/// http://tizen.org/feature/speech.recognition
/// http://tizen.org/feature/microphone
/// </feature>
/// <remarks>
/// ServiceAppMain should be used for working the engine after this function.
/// </remarks>
/// <param name="argc">The number of arguments.</param>
/// <param name="argv">The arguments array.</param>
/// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
/// <exception cref="NotSupportedException">Thrown in case of not supported.</exception>
/// <exception cref="InvalidOperationException">Thrown in case of an operation failure.</exception>
/// <since_tizen> 4 </since_tizen>
public void EngineMain(int argc, string[] argv)
{
_callbackStructGCHandle.CallbackStruct.version = 1;
_callbackStructGCHandle.CallbackStruct.getInfo = _getInfoCb;
_callbackStructGCHandle.CallbackStruct.initialize = Initialize;
_callbackStructGCHandle.CallbackStruct.deinitialize = _deinitializeCb;
_callbackStructGCHandle.CallbackStruct.supportedLanaguge = ForEachSupportedLanguages;
_callbackStructGCHandle.CallbackStruct.validLanaguage = _isValidLanguageCb;
_callbackStructGCHandle.CallbackStruct.silence = SupportSilenceDetection;
_callbackStructGCHandle.CallbackStruct.recognitionType = _supportRecognitionTypeCb;
_callbackStructGCHandle.CallbackStruct.recordingFormat = GetRecordingFormat;
_callbackStructGCHandle.CallbackStruct.resultTime = ForEachResultTime;
_callbackStructGCHandle.CallbackStruct.silenceDetection = SetSilenceDetection;
_callbackStructGCHandle.CallbackStruct.start = Start;
_callbackStructGCHandle.CallbackStruct.recordingData = SetRecordingData;
_callbackStructGCHandle.CallbackStruct.stop = Stop;
_callbackStructGCHandle.CallbackStruct.cancel = Cancel;
_callbackStructGCHandle.CallbackStruct.checkAppAgreed = _checkAppAgreedCb;
_callbackStructGCHandle.CallbackStruct.needAppCredential = NeedAppCredential;
_structIntPtrHandle = Marshal.AllocHGlobal(Marshal.SizeOf(_callbackStructGCHandle.CallbackStruct));
Marshal.StructureToPtr<RequestCallbackStruct>(_callbackStructGCHandle.CallbackStruct, _structIntPtrHandle, false);
Error error = STTEMain(argc, argv, _structIntPtrHandle);
if (error != Error.None)
{
Log.Error(LogTag, "STTEMain Failed with error " + error);
throw ExceptionFactory.CreateException((ErrorCode)error);
}
Log.Info(LogTag, "After STTEMain");
}
/// <summary>
/// Sends the recognition result to the engine service user.
/// </summary>
/// <feature>
/// http://tizen.org/feature/speech.recognition
/// http://tizen.org/feature/microphone
/// </feature>
/// <remarks>
/// This API is used in SetRecordingData() and Stop(), when the STT engine sends the recognition result to the engine service user.
/// This function is called in the following situations: 1) After Stop() is called, 2) The end point of speech is detected from recording, or 3) Partial result is occurred.
/// The recognition result must be transferred to the engine service user through this function. Also, the timeInfo must be transferred to ForEachResultTime().
/// The type of timeInfo is up to the STT engine developer.
/// </remarks>
/// <param name="resultEvent">The result event.</param>
/// <param name="type">The recognition type, "stt.recognition.type.FREE", or "stt.recognition.type.FREE.PARTIAL".</param>
/// <param name="result">Result texts.</param>
/// <param name="resultCount">Result text count.</param>
/// <param name="msg">Engine message.</param>
/// <param name="timeInfo">The time information.</param>
/// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
/// <exception cref="NotSupportedException">Thrown in case of not supported.</exception>
/// <exception cref="InvalidOperationException">Thrown in case of an operation failure.</exception>
/// <precondition>
/// The EngineMain function should be invoked before this function is called. Stop will invoke this function.
/// </precondition>
/// <postcondition>
/// This function invokes ForEachResultTime
/// </postcondition>
/// <since_tizen> 4 </since_tizen>
public void SendResult(ResultEvent resultEvent, string type, string[] result, int resultCount, ResultMessage msg, IntPtr timeInfo)
{
if ((result != null) && (result.Length != 0))
{
string message = "stt.result.message.none";
switch (msg)
{
case ResultMessage.None:
message = "stt.result.message.none";
break;
case ResultMessage.TooFast:
message = "stt.result.message.error.too.fast";
break;
case ResultMessage.TooLong:
message = "stt.result.message.error.too.long";
break;
case ResultMessage.TooLoud:
message = "stt.result.message.error.too.loud";
break;
case ResultMessage.TooQuiet:
message = "stt.result.message.error.too.quiet";
break;
case ResultMessage.TooShort:
message = "stt.result.message.error.too.short";
break;
case ResultMessage.TooSoon:
message = "stt.result.message.error.too.soon";
break;
}
Error error = STTESendResult(resultEvent, type, result, resultCount, message, timeInfo, IntPtr.Zero);
if (error != Error.None)
{
Log.Error(LogTag, "STTESendResult Failed with error " + error);
throw ExceptionFactory.CreateException((ErrorCode)error);
}
}
else
{
throw new ArgumentNullException("result", "is null or empty");
}
}
/// <summary>
/// Sends the error to the engine service user.
/// </summary>
/// <feature>
/// http://tizen.org/feature/speech.recognition
/// http://tizen.org/feature/microphone
/// </feature>
/// <param name="error">The error reason.</param>
/// <param name="msg">The error message.</param>
/// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
/// <exception cref="NotSupportedException">Thrown in case of not supported.</exception>
/// <exception cref="InvalidOperationException">Thrown in case of an operation failure.</exception>
/// <precondition>
/// The main function should be invoked before this function is called.
/// </precondition>
/// <since_tizen> 4 </since_tizen>
public void SendError(Error error, string msg)
{
Error err = STTESendError(error, msg);
if (err != Error.None)
{
Log.Error(LogTag, "SendError Failed with error " + err);
throw ExceptionFactory.CreateException((ErrorCode)error);
}
}
/// <summary>
/// Sends the speech status to the engine service user when the STT engine notifies the change of the speech status.
/// </summary>
/// <feature>
/// http://tizen.org/feature/speech.recognition
/// http://tizen.org/feature/microphone
/// </feature>
/// <remarks>
/// This API is invoked when the STT engine wants to notify the change of the speech status anytime. Note that this API can be invoked for recognizing the speech.
/// </remarks>
/// <param name="status">SpeechStatus</param>
/// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
/// <exception cref="NotSupportedException">Thrown in case of not supported.</exception>
/// <exception cref="InvalidOperationException">Thrown in case of an operation failure.</exception>
/// <precondition>
/// The main function should be invoked before this function is called. The Start() and SetRecordingData() will invoke this function.
/// </precondition>
/// <since_tizen> 4 </since_tizen>
public void SendSpeechStatus(SpeechStatus status)
{
Error error = STTESendSpeechStatus(status, IntPtr.Zero);
if (error != Error.None)
{
Log.Error(LogTag, "SendSpeechStatus Failed with error " + error);
throw ExceptionFactory.CreateException((ErrorCode)error);
}
}
/// <summary>
/// Sets a callback function for setting the private data.
/// </summary>
/// <privilege>
/// http://tizen.org/privilege/recorder
/// </privilege>
/// <feature>
/// http://tizen.org/feature/speech.recognition
/// http://tizen.org/feature/microphone
/// </feature>
/// <param name="callback">
/// Called when the STT engine receives the private data from the engine service user.
/// This callback function is called when the engine service user sends the private data to the STT engine.
/// In Parameters:
/// a = Key -- The key field of private data
/// b = data -- The data field of private data
/// The following error codes can be returned:
/// 1. None
/// 2. InvalidParameter
/// 3. OperationFailed
/// </param>
/// <exception cref="ArgumentException">Thrown in case of an invalid parameter.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
/// <exception cref="NotSupportedException">Thrown in case of not supported.</exception>
/// <exception cref="InvalidOperationException">Thrown in case of an operation failure.</exception>
/// <precondition>
/// The main function should be invoked before this function is called.
/// </precondition>
/// <since_tizen> 4 </since_tizen>
public void SetPrivateDataSetDelegate(Action<string> callback)
{
if (null == callback)
{
Log.Error(LogTag, "callback is null");
throw ExceptionFactory.CreateException(ErrorCode.InvalidParameter);
}
_privateDatacallback = callback;
_privateDataSetCb = (string key, string data) =>
{
return _privateDatacallback.Invoke(key, data);
};
Error error = STTESetPrivateDataSetCb(_privateDataSetCb);
if (error != Error.None)
{
Log.Error(LogTag, "SetPrivateDataSetDelegate Failed with error " + error);
throw ExceptionFactory.CreateException((ErrorCode)error);
}
}
/// <summary>
/// Sets a callback delegate for requesting the private data.
/// </summary>
/// <privilege>
/// http://tizen.org/privilege/recorder
/// </privilege>
/// <feature>
/// http://tizen.org/feature/speech.recognition
/// http://tizen.org/feature/microphone
/// </feature>
/// <param name="callback">The callback function.
/// Called when the STT engine provides the engine service user with the private data.
/// This callback function is called when the engine service user gets the private data from the STT engine.
/// Out parameters:
/// a = Key -- The key field of private data
/// b = data -- The data field of private data
/// The following error codes can be returned:
/// 1. None
/// 2. InvalidParameter
/// 3. OperationFailed
/// </param>
/// <exception cref="ArgumentException">Thrown in case of an invalid parameter.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
/// <exception cref="NotSupportedException">Thrown in case of not supported.</exception>
/// <exception cref="InvalidOperationException">Thrown in case of an operation failure.</exception>
/// <precondition>
/// The main function should be invoked before this function is called.
/// </precondition>
/// <since_tizen> 4 </since_tizen>
public void SetPrivateDataRequestedDelegate(OutAction<string> callback)
{
if (null == callback)
{
Log.Error(LogTag, "callback is null");
throw ExceptionFactory.CreateException(ErrorCode.InvalidParameter);
}
_privateDataRequestedCallback = callback;
_privateDataRequestedCb = (string key, out string data) =>
{
return _privateDataRequestedCallback.Invoke(key, out data);
};
Error error = STTESetPrivateDataRequestedCb(_privateDataRequestedCb);
if (error != Error.None)
{
Log.Error(LogTag, "SetPrivateDataRequestedDelegate Failed with error " + error);
throw ExceptionFactory.CreateException((ErrorCode)error);
}
}
private IsValidLanguageCb _isValidLanguageCb = (string langauge, out byte isValid) =>
{
bool valid;
Error err = _engine.IsValidLanguage(langauge, out valid);
if (true == valid)
{
isValid = 1;
}
else
{
isValid = 0;
}
return err;
};
private CheckAppAgreedCb _checkAppAgreedCb = (string appid, out byte isAgreed) =>
{
bool agreed;
Error err = _engine.CheckAppAgreed(appid, out agreed);
if (true == agreed)
{
isAgreed = 1;
}
else
{
isAgreed = 0;
}
return err;
};
private SupportRecognitionTypeCb _supportRecognitionTypeCb = (string type, out byte isSupported) =>
{
bool supported;
Error err = _engine.SupportRecognitionType(type, out supported);
if (true == supported)
{
isSupported = 1;
}
else
{
isSupported = 0;
}
return err;
};
private GetInfoCb _getInfoCb = (out string engineUuid, out string engineName, out string engineSetting, out byte useNetwork) =>
{
bool network;
Error err = _engine.GetInformation(out engineUuid, out engineName, out engineSetting, out network);
if (true == network)
{
useNetwork = 1;
}
else
{
useNetwork = 0;
}
return err;
};
private DeinitializeCb _deinitializeCb = () =>
{
Marshal.FreeHGlobal(_engine._structIntPtrHandle);
return _engine.Deinitialize();
};
}
}