Review Uix.InputMethod/SttEngine/TtsEngine API cs files
[platform/core/csapi/tizenfx.git] / src / Tizen.Uix.TtsEngine / Tizen.Uix.TtsEngine / TtsEngine.cs
1 /*
2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 using System;
18 using System.Runtime.InteropServices;
19 using static Interop.TtsEngine;
20
21 namespace Tizen.Uix.TtsEngine
22 {
23     /// <summary>
24     /// Enumeration for the audio types.
25     /// </summary>
26     /// <since_tizen> 4 </since_tizen>
27     public enum AudioType
28     {
29         /// <summary>
30         /// Signed 16-bit audio type.
31         /// </summary>
32         RawS16 = 0,
33         /// <summary>
34         /// Unsigned 8-bit audio type.
35         /// </summary>
36         RawU8,
37         /// <summary>
38         /// Maximum value.
39         /// </summary>
40         Max
41     };
42
43     /// <summary>
44     /// Enumeration for the results.
45     /// </summary>
46     /// <since_tizen> 4 </since_tizen>
47     public enum ResultEvent
48     {
49         /// <summary>
50         /// Event when the voice synthesis fails.
51         /// </summary>
52         Fail = -1,
53         /// <summary>
54         /// Event when the sound data is the first data by the callback function.
55         /// </summary>
56         Start = 1,
57         /// <summary>
58         /// Event when the next sound data exists, not the first and the last.
59         /// </summary>
60         Continue = 2,
61         /// <summary>
62         /// Event when the sound data is the last data or the sound data is the only result.
63         /// </summary>
64         Finish = 3
65     };
66
67     /// <summary>
68     /// Enumeration for the voice types.
69     /// </summary>
70     /// <since_tizen> 4 </since_tizen>
71     public enum VoiceType
72     {
73         /// <summary>
74         /// Male voice type.
75         /// </summary>
76         Male = 1,
77         /// <summary>
78         /// Female voice type.
79         /// </summary>
80         Female = 2,
81         /// <summary>
82         /// Child's voice type.
83         /// </summary>
84         Child = 3
85     }
86
87     /// <summary>
88     /// Enumeration for the error values that can occur.
89     /// </summary>
90     /// <since_tizen> 4 </since_tizen>
91     public enum Error
92     {
93         /// <summary>
94         /// Successful, no error.
95         /// </summary>
96         None = ErrorCode.None,
97         /// <summary>
98         /// Out of memory.
99         /// </summary>
100         OutOfMemory = ErrorCode.OutOfMemory,
101         /// <summary>
102         /// I/O error.
103         /// </summary>
104         IoError = ErrorCode.IoError,
105         /// <summary>
106         /// Invalid parameter.
107         /// </summary>
108         InvalidParameter = ErrorCode.InvalidParameter,
109         /// <summary>
110         /// Network down (Out of network).
111         /// </summary>
112         NetworkDown = ErrorCode.NetworkDown,
113         /// <summary>
114         /// Invalid state.
115         /// </summary>
116         InvalidState = ErrorCode.InvalidState,
117         /// <summary>
118         /// Invalid voice.
119         /// </summary>
120         InvalidVoice = ErrorCode.InvalidVoice,
121         /// <summary>
122         /// Operation failed.
123         /// </summary>
124         OperationFailed = ErrorCode.OperationFailed,
125         /// <summary>
126         /// Not supported feature of the current engine.
127         /// </summary>
128         NotSupportedFeature = ErrorCode.NotSupportedFeature,
129         /// <summary>
130         /// Not supported.
131         /// </summary>
132         NotSupported = ErrorCode.NotSupported,
133         /// <summary>
134         /// Permission denied.
135         /// </summary>
136         PermissionDenied = ErrorCode.PermissionDenied
137     };
138
139     /// <summary>
140     /// This class represents the TTS engine, which has to be inherited to make the engine.
141     /// </summary>
142     /// <since_tizen> 4 </since_tizen>
143     public abstract class Engine
144     {
145         private CallbackStructGCHandle _callbackStructGCHandle = new CallbackStructGCHandle();
146         private PrivateDataSetCb _privateDataSetCb;
147         private Action<string> _privateDatacallback;
148         private PrivateDataRequestedCb _privateDataRequestedCb;
149         private OutAction<string> _privateDataRequestedCallback;
150         private static Engine _engine;
151         private IntPtr _structIntPtrHandle;
152
153         /// <summary>
154         /// An action with 2 input parameters returning an error.
155         /// </summary>
156         /// <typeparam name="T">Generic type for parameter 1.</typeparam>
157         /// <param name="a">The input parameter 1.</param>
158         /// <param name="b">The input parameter 2.</param>
159         /// <returns>Error value.</returns>
160         /// <since_tizen> 4 </since_tizen>
161         public delegate Error Action<T>(T a, T b);
162
163         /// <summary>
164         /// An action with 2 out parameters returning an error.
165         /// </summary>
166         /// <typeparam name="T">Generic type for parameter 1.</typeparam>
167         /// <param name="a">The input parameter 1.</param>
168         /// <param name="b">The input parameter 2.</param>
169         /// <returns>Error value.</returns>
170         /// <since_tizen> 4 </since_tizen>
171         public delegate Error OutAction<T>(T a, out T b);
172
173         /// <summary>
174         /// Called when the TTS engine informs the engine service user about whole supported language and voice type list.
175         /// This callback function is implemented by the engine service user, therefore, the engine developer does NOT have to implement this callback function. 
176         /// </summary>
177         /// <remarks>
178         /// This callback function is called by ForEachSupportedVoices() to inform the whole supported voice list. userData must be transferred from ForEachSupportedVoices().
179         /// </remarks>
180         /// <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.
181         /// For example, "ko_KR" for Korean, "en_US" for American English.</param>
182         /// <param name="type">The voice type.</param>
183         /// <param name="userData">The user data passed from ForEachSupportedVoices().</param>
184         /// <returns>true to continue with the next iteration of the loop, false to break out of the loop.</returns>
185         /// <precondition>ForEachSupportedVoices() will invoke this callback function.</precondition>
186         /// <since_tizen> 4 </since_tizen>
187         public delegate bool SupportedVoice(string language, VoiceType type, IntPtr userData);
188
189         /// <summary>
190         /// Called when the engine service user starts to synthesize a voice, asynchronously.
191         /// </summary>
192         /// <remarks>
193         /// In this callback function, the TTS engine must transfer the synthesized result to the engine service user using SendResult().
194         /// Also, if the TTS engine needs the application's credential, it can set the credential granted to the application.
195         /// </remarks>
196         /// <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.
197         /// For example, "ko_KR" for Korean, "en_US" for American English.</param>
198         /// <param name="type">The voice type.</param>
199         /// <param name="text">Texts.</param>
200         /// <param name="speed">The speed of speaking.</param>
201         /// <param name="appid">The application ID.</param>
202         /// <param name="credential">The credential granted to the application.</param>
203         /// <param name="userData">The user data which must be passed to the SendResult() function.</param>
204         /// <returns>
205         /// The following error codes can be returned:
206         /// 1. None
207         /// 2. InvalidState
208         /// 3. InvalidParameter
209         /// 4. InvalidVoice
210         /// 5. OperationFailed
211         /// 6. NetworkDown
212         /// 7. PermissionDenied
213         /// </returns>
214         /// <postcondition>This function invokes SendResult().</postcondition>
215         /// <since_tizen> 4 </since_tizen>
216         public abstract Error StartSynthesis(string language, int type, string text, int speed, string appid, string credential, IntPtr userData);
217
218         /// <summary>
219         /// Called when the engine service user requests the basic information of the TTS engine.
220         /// </summary>
221         /// <remarks>
222         /// The allocated engineUuid, engineName, and engineSetting will be released internally.
223         /// In order to upload the engine at Tizen Appstore, both a service application and a UI application are necessary.
224         /// Therefore, engineSetting must be transferred to the engine service user.
225         /// </remarks>
226         /// <param name="engineUuid">UUID of the engine.</param>
227         /// <param name="engineName">Name of the engine.</param>
228         /// <param name="engineSetting">The engine setting application (UI application)'s ID.</param>
229         /// <param name="useNetwork">The status for using the network.</param>
230         /// <returns>
231         /// The following error codes can be returned:
232         /// 1. None
233         /// 2. InvalidState
234         /// </returns>
235         /// <since_tizen> 4 </since_tizen>
236         public abstract Error GetInformation(out string engineUuid, out string engineName, out string engineSetting, out bool useNetwork);
237
238         /// <summary>
239         /// Called when the engine service user initializes the TTS engine.
240         /// </summary>
241         /// <returns>
242         /// The following error codes can be returned:
243         /// 1. None
244         /// 2. InvalidState
245         /// 3. NotSupportedFeature
246         /// 4. PermissionDenied
247         /// </returns>
248         /// <since_tizen> 4 </since_tizen>
249         public abstract Error Initialize();
250
251         /// <summary>
252         /// Called when the engine service user deinitializes the TTS engine.
253         /// </summary>
254         /// <remarks>
255         /// NOTE that the engine may be terminated automatically. When this callback function is invoked, the release of the resources is necessary.
256         /// </remarks>
257         /// <returns>
258         /// The following error codes can be returned:
259         /// 1. None
260         /// 2. InvalidState
261         /// </returns>
262         /// <since_tizen> 4 </since_tizen>
263         public abstract Error Deinitialize();
264
265         /// <summary>
266         /// Called when the engine service user gets the whole supported voice list.
267         /// </summary>
268         /// <remarks>
269         /// In this function, the engine service user's callback function 'SupportedVoice()' is invoked repeatedly for getting all the supported voices,
270         /// and userData must be transferred to 'SupportedVoice()'. If 'SupportedVoice()' returns false, it should be stopped to call 'SupportedVoice()'.</remarks>
271         /// <param name="callback">The callback function.</param>
272         /// <param name="userData">The user data which must be passed to SupportedVoice().</param>
273         /// <returns>
274         /// The following error codes can be returned:
275         /// 1. None
276         /// 2. OperationFailed
277         /// </returns>
278         /// <postcondition>This callback function invokes SupportedVoice() repeatedly for getting all the supported voices.</postcondition>
279         /// <since_tizen> 4 </since_tizen>
280         public abstract Error ForEachSupportedVoices(SupportedVoice callback, IntPtr userData);
281
282         /// <summary>
283         /// Called when the engine service user checks whether the voice is valid or not in the TTS engine.
284         /// </summary>
285         /// <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.
286         /// For example, "ko_KR" for Korean, "en_US" for American English.</param>
287         /// <param name="type">The voice type.</param>
288         /// <param name="isValid">A variable for checking whether the corresponding voice is valid or not. true to be valid, false to be invalid.</param>
289         /// <returns>
290         /// The following error codes can be returned:
291         /// 1. None
292         /// 2. InvalidParameter
293         /// </returns>
294         /// <since_tizen> 4 </since_tizen>
295         public abstract Error IsValidVoice(string language, int type, out bool isValid);
296
297         /// <summary>
298         /// Called when the engine service user sets the default pitch of the TTS engine.
299         /// </summary>
300         /// <param name="pitch">The default pitch.</param>
301         /// <returns>
302         /// The following error codes can be returned:
303         /// 1. None
304         /// 2. InvalidState
305         /// 3. OperationFailed
306         /// 4. InvalidParameter
307         /// </returns>
308         /// <since_tizen> 4 </since_tizen>
309         public abstract Error SetPitch(int pitch);
310
311         /// <summary>
312         /// Called when the engine service user requests to load the corresponding voice type for the first time.
313         /// </summary>
314         /// <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.
315         /// For example, "ko_KR" for Korean, "en_US" for American English.</param>
316         /// <param name="type">The voice type.</param>
317         /// <returns>
318         /// The following error codes can be returned:
319         /// 1. None
320         /// 2. InvalidState
321         /// 3. OperationFailed
322         /// 4. InvalidVoice
323         /// 5. InvalidParameter
324         /// 6. OutOfMemory
325         /// </returns>
326         /// <since_tizen> 4 </since_tizen>
327         public abstract Error LoadVoice(string language, int type);
328
329         /// <summary>
330         /// Called when the engine service user requests to unload the corresponding voice type or to stop using voice.
331         /// </summary>
332         /// <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.
333         /// For example, "ko_KR" for Korean, "en_US" for American English.</param>
334         /// <param name="type">The voice type.</param>
335         /// <returns>
336         /// The following error codes can be returned:
337         /// 1. None
338         /// 2. InvalidState
339         /// 3. OperationFailed
340         /// 4. InvalidVoice
341         /// 5. InvalidParameter
342         /// </returns>
343         /// <since_tizen> 4 </since_tizen>
344         public abstract Error UnloadVoice(string language, int type);
345
346         /// <summary>
347         /// Called when the engine service user requests for the TTS engine to check whether the application agreed the usage of the TTS engine.
348         /// This callback function is called when the engine service user requests for the TTS engine to check the application's agreement about using the the engine.
349         /// According to the need, the engine developer can provide some user interfaces to check the agreement.
350         /// </summary>
351         /// <remarks>
352         /// If the TTS engine developer does not want to check the agreement, the developer needs to return proper values as isAgreed in accordance with the intention.
353         /// true if the developer regards that every application agreed the usage of the engine, false if the developer regards that every application disagreed.
354         /// NOTE that, however, there may be any legal issue unless the developer checks the agreement.
355         /// Therefore, we suggest that the engine developers should provide a function to check the agreement.
356         /// </remarks>
357         /// <param name="appid">The application ID.</param>
358         /// <param name="isAgreed">A variable for checking whether the application agreed to use the TTS engine or not. true to agree, false to disagree.</param>
359         /// <returns>
360         /// The following error codes can be returned:
361         /// 1. None
362         /// 2. InvalidState
363         /// 3. NotSupportedFeature
364         /// 4. InvalidParameter
365         /// </returns>
366         /// <since_tizen> 4 </since_tizen>
367         public abstract Error CheckAppAgreed(string appid, out bool isAgreed);
368
369         /// <summary>
370         /// Called when the engine service user checks whether the TTS engine needs the application's credentials.
371         /// </summary>
372         /// <returns>true if TTS engine needs the application's credentials, otherwise false. </returns>
373         /// <since_tizen> 4 </since_tizen>
374         public abstract bool NeedAppCredential();
375
376         /// <summary>
377         /// Called when the engine service user cancels to synthesize a voice.
378         /// </summary>
379         /// <returns>
380         /// The following error codes can be returned:
381         /// 1. None
382         /// 2. InvalidState
383         /// </returns>
384         /// <precondition>
385         /// StartSynthesis should be performed.
386         /// </precondition>
387         /// <since_tizen> 4 </since_tizen>
388         public abstract Error CancelSynthesis();
389
390         /// <summary>
391         /// Public constructor.
392         /// </summary>
393         /// <feature>
394         /// http://tizen.org/feature/speech.synthesis
395         /// </feature>
396         /// <since_tizen> 4 </since_tizen>
397         public Engine()
398         {
399             _engine = this;
400         }
401
402         /// <summary>
403         /// Main function for the Text-To-Speech (TTS) engine.
404         /// This function is the main function for operating the TTS engine.
405         /// </summary>
406         /// <feature>
407         /// http://tizen.org/feature/speech.synthesis
408         /// </feature>
409         /// <remarks>
410         /// ServiceAppMain should be used for working the engine after this function.
411         /// </remarks>
412         /// <param name="argc">The argument count (original).</param>
413         /// <param name="argv">The argument (original).</param>
414         /// <exception cref="NotSupportedException">Thrown in case of not supported.</exception>
415         /// <exception cref="InvalidOperationException">Thrown in case of operation failure.</exception>
416         /// <since_tizen> 4 </since_tizen>
417         public void EngineMain(int argc, string[] argv)
418         {
419             _callbackStructGCHandle.CallbackStruct.version = 1;
420             _callbackStructGCHandle.CallbackStruct.getInfo = _getInfoCb;
421             _callbackStructGCHandle.CallbackStruct.initialize = Initialize;
422             _callbackStructGCHandle.CallbackStruct.deinitialize = _deinitializeCb;
423             _callbackStructGCHandle.CallbackStruct.supportedVoice = ForEachSupportedVoices;
424             _callbackStructGCHandle.CallbackStruct.validVoice = IsValidVoice;
425             _callbackStructGCHandle.CallbackStruct.pitch = SetPitch;
426             _callbackStructGCHandle.CallbackStruct.loadVoice = LoadVoice;
427             _callbackStructGCHandle.CallbackStruct.unloadVoice = UnloadVoice;
428             _callbackStructGCHandle.CallbackStruct.startSynthesis = _startSynthesisCb;
429             _callbackStructGCHandle.CallbackStruct.cancelSynthesis = CancelSynthesis;
430             _callbackStructGCHandle.CallbackStruct.checkAppAgreed = CheckAppAgreed;
431             _callbackStructGCHandle.CallbackStruct.needAppCredential = NeedAppCredential;
432             _structIntPtrHandle = Marshal.AllocHGlobal(Marshal.SizeOf(_callbackStructGCHandle.CallbackStruct));
433             Marshal.StructureToPtr<RequestCallbackStruct>(_callbackStructGCHandle.CallbackStruct, _structIntPtrHandle, false);
434             Error error = TTSEMain(argc, argv, _structIntPtrHandle);
435             if (error != Error.None)
436             {
437                 Log.Error(LogTag, "TTSEMain Failed with error " + error);
438                 throw ExceptionFactory.CreateException((ErrorCode)error);
439             }
440
441             Log.Info(LogTag, "After TTSEMain");
442         }
443
444         /// <summary>
445         /// Gets the speed range from the Tizen platform.
446         /// </summary>
447         /// <feature>
448         /// http://tizen.org/feature/speech.synthesis
449         /// </feature>
450         /// <remarks>
451         /// This API is used when the TTS engine wants to get the speed range from the Tizen platform.
452         /// </remarks>
453         /// <param name="min">The minimum speed value.</param>
454         /// <param name="normal">The normal speed value.</param>
455         /// <param name="max">The maximum speed value.</param>
456         /// <exception cref="NotSupportedException">Thrown in case of not supported.</exception>
457         /// <exception cref="InvalidOperationException">Thrown in case of operation failure.</exception>
458         /// <since_tizen> 4 </since_tizen>
459         public void GetSpeedRange(out int min, out int normal, out int max)
460         {
461             Error error = TTSEGetSpeedRange(out min, out normal, out max);
462             if (error != Error.None)
463             {
464                 Log.Error(LogTag, "TTSEGetSpeedRange Failed with error " + error);
465                 throw ExceptionFactory.CreateException((ErrorCode)error);
466             }
467
468         }
469
470         /// <summary>
471         /// Gets the pitch range from the Tizen platform.
472         /// </summary>
473         /// <feature>
474         /// http://tizen.org/feature/speech.synthesis
475         /// </feature>
476         /// <remarks>
477         /// This API is used when the TTS engine wants to get the pitch range from the Tizen platform.
478         /// </remarks>
479         /// <param name="min">The minimum pitch value.</param>
480         /// <param name="normal">The normal pitch value.</param>
481         /// <param name="max">The maximum pitch value.</param>
482         /// <exception cref="NotSupportedException">Thrown in case of not supported.</exception>
483         /// <exception cref="InvalidOperationException">Thrown in case of operation failure.</exception>
484         /// <since_tizen> 4 </since_tizen>
485         public void GetPitchRange(out int min, out int normal, out int max)
486         {
487             Error error = TTSEGetPitchRange(out min, out normal, out max);
488             if (error != Error.None)
489             {
490                 Log.Error(LogTag, "TTSEGetPitchRange Failed with error " + error);
491                 throw ExceptionFactory.CreateException((ErrorCode)error);
492             }
493
494         }
495
496         /// <summary>
497         /// Sends the synthesized result to the engine service user.
498         /// </summary>
499         /// <feature>
500         /// http://tizen.org/feature/speech.synthesis
501         /// </feature>
502         /// <remarks>
503         /// This API is used in StartSynthesis(), when the TTS engine sends the synthesized result to the engine service user.
504         /// The synthesized result must be transferred to the engine service user through this function.
505         /// </remarks>
506         /// <param name="resultEvent">The result event.</param>
507         /// <param name="data">The result data.</param>
508         /// <param name="dataSize">The result data size.</param>
509         /// <param name="audioType">The audio type.</param>
510         /// <param name="rate">The sample rate.</param>
511         /// <exception cref="ArgumentException">Thrown in case of an invalid parameter.</exception>
512         /// <exception cref="NotSupportedException">Thrown in case of not supported.</exception>
513         /// <exception cref="InvalidOperationException">Thrown in case of operation failure.</exception>
514         /// <precondition>
515         /// EngineMain function should be invoked before this function is called. StartSynthesis() will invoke this function.
516         /// </precondition>
517         /// <since_tizen> 4 </since_tizen>
518         public void SendResult(ResultEvent resultEvent, IntPtr data, int dataSize, AudioType audioType, int rate)
519         {
520             Error error = TTSESendResult(resultEvent, data, dataSize, audioType, rate, IntPtr.Zero);
521             if (error != Error.None)
522             {
523                 Log.Error(LogTag, "TTSESendResult Failed with error " + error);
524                 throw ExceptionFactory.CreateException((ErrorCode)error);
525             }
526         }
527
528         /// <summary>
529         /// Sends the error to the engine service user.
530         /// </summary>
531         /// <feature>
532         /// http://tizen.org/feature/speech.synthesis
533         /// </feature>
534         /// <param name="error">The error reason.</param>
535         /// <param name="msg">The error message.</param>
536         /// <exception cref="NotSupportedException">Thrown in case of not supported.</exception>
537         /// <exception cref="InvalidOperationException">Thrown in case of operation failure.</exception>
538         /// <precondition>
539         /// EngineMain function should be invoked before this function is called.
540         /// </precondition>
541         /// <since_tizen> 4 </since_tizen>
542         public void SendError(Error error, string msg)
543         {
544             Error err = TTSESendError(error, msg);
545             if (err != Error.None)
546             {
547                 Log.Error(LogTag, "SendError Failed with error " + err);
548                 throw ExceptionFactory.CreateException((ErrorCode)error);
549             }
550
551         }
552
553         /// <summary>
554         /// Sets a callback function for setting the private data.
555         /// </summary>
556         /// <feature>
557         /// http://tizen.org/feature/speech.synthesis
558         /// </feature>
559         /// <param name="callback">
560         /// Called when the engine service user gets the private data from the TTS engine.
561         /// In parameters:
562         /// a = Key -- The key field of private data.
563         /// b = data -- The data field of private data.
564         /// The following error codes can be returned:
565         /// 1. None
566         /// 2. InvalidParameter
567         /// 3. OperationFailed
568         /// 4. NotSupported
569         /// </param>
570         /// <exception cref="ArgumentException">Thrown in case of an invalid parameter.</exception>
571         /// <exception cref="NotSupportedException">Thrown in case of not supported.</exception>
572         /// <exception cref="InvalidOperationException">thrown in case of operation failure.</exception>
573         /// <precondition>
574         /// Main function should be invoked before this function is called.
575         /// </precondition>
576         /// <since_tizen> 4 </since_tizen>
577         public void SetPrivateDataSetDelegate(Action<string> callback)
578         {
579             if (null == callback)
580             {
581                 Log.Error(LogTag, "callback is null");
582                 throw ExceptionFactory.CreateException(ErrorCode.InvalidParameter);
583             }
584
585             _privateDatacallback = callback;
586             _privateDataSetCb = (string key, string data) =>
587             {
588                 return _privateDatacallback.Invoke(key, data);
589             };
590             Error error = TTSESetPrivateDataSetCb(_privateDataSetCb);
591             if (error != Error.None)
592             {
593                 Log.Error(LogTag, "SetPrivateDataSetDelegate Failed with error " + error);
594                 throw ExceptionFactory.CreateException((ErrorCode)error);
595             }
596
597         }
598
599         /// <summary>
600         /// Sets a callback function for setting the private data.
601         /// </summary>
602         /// <feature>
603         /// http://tizen.org/feature/speech.synthesis
604         /// </feature>
605         /// <param name="callback">The callback function.
606         /// Called when the TTS engine receives the private data from the engine service user.
607         /// This callback function is called when the engine service user sends the private data to the TTS engine.
608         /// Out parameters:
609         /// a = Key -- The key field of private data.
610         /// b = data -- The data field of private data.
611         /// The following error codes can be returned:
612         /// 1. None
613         /// 2. InvalidParameter
614         /// 3. OperationFailed
615         /// 4. NotSupported
616         /// </param>
617         /// <exception cref="ArgumentException">Thrown in case of an invalid parameter.</exception>
618         /// <exception cref="NotSupportedException">Thrown in case of not supported.</exception>
619         /// <exception cref="InvalidOperationException">thrown in case of operation failure.</exception>
620         /// <precondition>
621         /// Main function should be invoked before this function is called.
622         /// </precondition>
623         /// <since_tizen> 4 </since_tizen>
624         public void SetPrivateDataRequestedDelegate(OutAction<string> callback)
625         {
626             if (null == callback)
627             {
628                 Log.Error(LogTag, "callback is null");
629                 throw ExceptionFactory.CreateException(ErrorCode.InvalidParameter);
630             }
631
632             _privateDataRequestedCallback = callback;
633             _privateDataRequestedCb = (string key, out string data) =>
634             {
635                 return _privateDataRequestedCallback.Invoke(key, out data);
636             };
637             Error error = TTSESetPrivateDataRequestedCb(_privateDataRequestedCb);
638             if (error != Error.None)
639             {
640                 Log.Error(LogTag, "SetPrivateDataRequestedDelegate Failed with error " + error);
641                 throw ExceptionFactory.CreateException((ErrorCode)error);
642             }
643
644         }
645         private StartSynthesisCb _startSynthesisCb = (IntPtr language, int type, IntPtr text, int speed, IntPtr appid, IntPtr credential, IntPtr userData) =>
646         {
647             string lan = null;
648             string txt = null;
649             string apid = null;
650             string cre = null;
651             if (language != null)
652                 lan = Marshal.PtrToStringAnsi(language);
653             if (text != null)
654                 txt = Marshal.PtrToStringAnsi(text);
655             if (appid != null)
656                 apid = Marshal.PtrToStringAnsi(appid);
657             if (credential != null)
658                 cre = Marshal.PtrToStringAnsi(credential);
659             return _engine.StartSynthesis(lan, type, txt, speed, apid, cre, IntPtr.Zero);
660         };
661
662         private GetInfoCb _getInfoCb = (out IntPtr engineUuid, out IntPtr engineName, out IntPtr engineSetting, out int useNetwork) =>
663         {
664             string uuid;
665             string name;
666             string setting;
667             bool network;
668             Error err = _engine.GetInformation(out uuid, out name, out setting, out network);
669             if (network == true)
670             {
671                 useNetwork = 1;
672             }
673             else
674             {
675                 useNetwork = 0;
676             }
677             engineUuid = Marshal.StringToHGlobalAnsi(uuid);
678             engineName = Marshal.StringToHGlobalAnsi(name);
679             engineSetting = Marshal.StringToHGlobalAnsi(setting);
680             return err;
681         };
682
683         private DeinitializeCb _deinitializeCb = () =>
684         {
685             Marshal.FreeHGlobal(_engine._structIntPtrHandle);
686             return _engine.Deinitialize();
687         };
688     }
689 }