Release 12.0.0.18314
[platform/core/csapi/tizenfx.git] / src / Tizen.AIAvatar / src / public / Avatar / Controller / AvatarTTS.cs
1 /*
2  * Copyright(c) 2023 Samsung Electronics Co., Ltd.
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
18 using System.Collections.Generic;
19 using System.ComponentModel;
20 using System;
21 using Tizen.Uix.Tts;
22
23 using static Tizen.AIAvatar.AIAvatar;
24
25 namespace Tizen.AIAvatar
26 {
27     [EditorBrowsable(EditorBrowsableState.Never)]
28     internal class AvatarTTS
29     {
30         private TTSLipSyncer ttsLipSyncer;
31
32         private event EventHandler ttsReadyFinished;
33         private readonly Object ttsReadyFinishedLock = new Object();
34
35         [EditorBrowsable(EditorBrowsableState.Never)]
36         internal AvatarTTS()
37         {
38         }
39
40         /// <summary>
41         /// </summary>
42         [EditorBrowsable(EditorBrowsableState.Never)]
43         internal event EventHandler TTSReadyFinished
44         {
45             add
46             {
47                 lock (ttsReadyFinishedLock)
48                 {
49                     ttsReadyFinished += value;
50                 }
51
52             }
53
54             remove
55             {
56                 lock (ttsReadyFinishedLock)
57                 {
58                     if (ttsReadyFinished == null)
59                     {
60                         Log.Error(LogTag, "ttsReadyFinished is null");
61                         return;
62                     }
63                     ttsReadyFinished -= value;
64                 }
65             }
66         }
67
68         /// <summary>
69         /// <param name=""></param>
70         /// </summary>
71         [EditorBrowsable(EditorBrowsableState.Never)]
72         internal TtsClient CurrentTTSClient => ttsLipSyncer?.TtsHandle;
73
74         /// <summary>
75         /// <param name=""></param>
76         /// </summary>
77         [EditorBrowsable(EditorBrowsableState.Never)]
78         internal void InitTTS(Avatar avatar)
79         {
80             if (ttsLipSyncer == null)
81             {
82                 try
83                 {
84                     ttsLipSyncer = new TTSLipSyncer(avatar?.AvatarAnimator?.LipSyncer);
85                 }
86                 catch (Exception e)
87                 {
88                     Log.Error(LogTag, $"error :{e.Message}");
89                     Log.Error(LogTag, $"{e.StackTrace}");
90                 }
91             }
92         }
93
94         /// <summary>
95         /// <param name=""></param>
96         /// </summary>
97         [EditorBrowsable(EditorBrowsableState.Never)]
98         internal void DeinitTTS()
99         {
100             if (ttsLipSyncer != null)
101             {
102                 try
103                 {
104                     ttsLipSyncer.DeinitTts();
105                     ttsLipSyncer = null;
106                 }
107                 catch (Exception e)
108                 {
109                     Log.Error(LogTag, $"error :{e.Message}");
110                     Log.Error(LogTag, $"{e.StackTrace}");
111                 }
112             }
113         }
114
115         /// <summary>
116         /// <param name=""></param>
117         /// </summary>
118         [EditorBrowsable(EditorBrowsableState.Never)]
119         internal List<VoiceInfo> GetSupportedVoices()
120         {
121             return ttsLipSyncer.GetSupportedVoices();
122         }
123
124         /// <summary>
125         /// <param name=""></param>
126         /// </summary>
127         [EditorBrowsable(EditorBrowsableState.Never)]
128         internal bool PrepareTTS(string text, VoiceInfo voiceInfo, EventHandler ttsReadyFinishedCallback = null)
129         {
130             if (ttsLipSyncer == null || ttsLipSyncer.TtsHandle == null)
131             {
132                 Log.Error(LogTag, "tts is null");
133                 return false;
134             }
135
136
137             if (!ttsLipSyncer.IsSupportedVoice(voiceInfo))
138             {
139                 Log.Info(LogTag, $"{voiceInfo.Lang} & {voiceInfo.Type} is not supported");
140                 return false;
141             }
142
143             Log.Info(LogTag, "Current TTS State :" + ttsLipSyncer.TtsHandle.CurrentState);
144             if (ttsLipSyncer.TtsHandle.CurrentState != Tizen.Uix.Tts.State.Ready)
145             {
146                 Log.Info(LogTag, "TTS is not ready");
147                 return false;
148             }
149
150             try
151             {
152                 ttsLipSyncer.AddText(text, voiceInfo);
153                 ttsLipSyncer.Prepare(ttsReadyFinishedCallback);
154             }
155             catch (Exception e)
156             {
157                 Log.Error(LogTag, $"error :{e.Message}");
158                 Log.Error(LogTag, $"{e.StackTrace}");
159                 return false;
160             }
161             return true;
162         }
163
164         /// <summary>
165         /// <param name=""></param>
166         /// </summary>
167         [EditorBrowsable(EditorBrowsableState.Never)]
168         internal bool PrepareTTS(string text, string lang = "ko_KR", EventHandler ttsReadyFinishedCallback = null)
169         {
170             if (ttsLipSyncer == null || ttsLipSyncer.TtsHandle == null)
171             {
172                 Log.Error(LogTag, "tts is null");
173                 return false;
174             }
175
176             if (!ttsLipSyncer.IsSupportedVoice(lang))
177             {
178                 Log.Error(LogTag, $"{lang} is not supported");
179                 return false;
180             }
181
182             Log.Info(LogTag, "Current TTS State :" + ttsLipSyncer.TtsHandle.CurrentState);
183             if (ttsLipSyncer.TtsHandle.CurrentState != Tizen.Uix.Tts.State.Ready)
184             {
185                 Log.Error(LogTag, "TTS is not ready");
186                 return false;
187             }
188             try
189             {
190                 ttsLipSyncer.AddText(text, lang);
191                 ttsLipSyncer.Prepare(ttsReadyFinishedCallback);
192             }
193             catch (Exception e)
194             {
195                 Log.Error(LogTag, $"error :{e.Message}");
196                 Log.Error(LogTag, $"{e.StackTrace}");
197                 return false;
198             }
199             return true;
200         }
201
202         /// <summary>
203         /// <param name=""></param>
204         /// </summary>
205         [EditorBrowsable(EditorBrowsableState.Never)]
206         internal bool PlayPreparedTTS()
207         {
208             if (ttsLipSyncer == null || ttsLipSyncer.TtsHandle == null)
209             {
210                 Log.Error(LogTag, "tts is null");
211                 return false;
212             }
213
214             return ttsLipSyncer.PlayPreparedText();
215         }
216
217         /// <summary>
218         /// <param name=""></param>
219         /// </summary>
220         [EditorBrowsable(EditorBrowsableState.Never)]
221         internal bool PlayTTS(string text, VoiceInfo voiceInfo)
222         {
223             if (ttsLipSyncer == null || ttsLipSyncer.TtsHandle == null)
224             {
225                 Log.Error(LogTag, "tts is null");
226                 return false;
227             }
228
229
230             if (!ttsLipSyncer.IsSupportedVoice(voiceInfo))
231             {
232                 Log.Info(LogTag, $"{voiceInfo.Lang} & {voiceInfo.Type} is not supported");
233                 return false;
234             }
235
236             Log.Info(LogTag, "Current TTS State :" + ttsLipSyncer.TtsHandle.CurrentState);
237             if (ttsLipSyncer.TtsHandle.CurrentState != Tizen.Uix.Tts.State.Ready)
238             {
239                 Log.Info(LogTag, "TTS is not ready");
240                 return false;
241             }
242
243             try
244             {
245                 ttsLipSyncer.AddText(text, voiceInfo);
246                 ttsLipSyncer.Play();
247             }
248             catch (Exception e)
249             {
250                 Log.Error(LogTag, $"error :{e.Message}");
251                 Log.Error(LogTag, $"{e.StackTrace}");
252                 return false;
253             }
254             return true;
255         }
256
257         /// <summary>
258         /// <param name=""></param>
259         /// </summary>
260         [EditorBrowsable(EditorBrowsableState.Never)]
261         internal bool PlayTTS(string text, string lang = "ko_KR")
262         {
263             if (ttsLipSyncer == null || ttsLipSyncer.TtsHandle == null)
264             {
265                 Log.Error(LogTag, "tts is null");
266                 return false;
267             }
268
269             if (!ttsLipSyncer.IsSupportedVoice(lang))
270             {
271                 Log.Error(LogTag, $"{lang} is not supported");
272                 return false;
273             }
274
275             Log.Info(LogTag, "Current TTS State :" + ttsLipSyncer.TtsHandle.CurrentState);
276             if (ttsLipSyncer.TtsHandle.CurrentState != Tizen.Uix.Tts.State.Ready)
277             {
278                 Log.Error(LogTag, "TTS is not ready");
279                 return false;
280             }
281             try
282             {
283                 ttsLipSyncer.AddText(text, lang);
284                 ttsLipSyncer.Play();
285             }
286             catch (Exception e)
287             {
288                 Log.Error(LogTag, $"error :{e.Message}");
289                 Log.Error(LogTag, $"{e.StackTrace}");
290                 return false;
291             }
292             return true;
293         }
294         
295         /// <summary>
296         /// <param name=""></param>
297         /// </summary>
298         [EditorBrowsable(EditorBrowsableState.Never)]
299         internal bool PlayTTSAsync(string text, VoiceInfo voiceInfo, EventHandler ttsReadyFinishedCallback = null)
300         {
301             if (ttsLipSyncer == null || ttsLipSyncer.TtsHandle == null)
302             {
303                 Log.Error(LogTag, "tts is null");
304                 return false;
305             }
306
307
308             if (!ttsLipSyncer.IsSupportedVoice(voiceInfo))
309             {
310                 Log.Info(LogTag, $"{voiceInfo.Lang} & {voiceInfo.Type} is not supported");
311                 return false;
312             }
313
314             Log.Info(LogTag, "Current TTS State :" + ttsLipSyncer.TtsHandle.CurrentState);
315             if (ttsLipSyncer.TtsHandle.CurrentState != Tizen.Uix.Tts.State.Ready)
316             {
317                 Log.Info(LogTag, "TTS is not ready");
318                 return false;
319             }
320
321             try
322             {
323                 ttsLipSyncer.AddText(text, voiceInfo);
324                 ttsLipSyncer.PlayAsync(ttsReadyFinishedCallback);
325             }
326             catch (Exception e)
327             {
328                 Log.Error(LogTag, $"error :{e.Message}");
329                 Log.Error(LogTag, $"{e.StackTrace}");
330                 return false;
331             }
332             return true;
333         }
334
335         /// <summary>
336         /// <param name=""></param>
337         /// </summary>
338         [EditorBrowsable(EditorBrowsableState.Never)]
339         internal bool PlayTTS(string text, string lang = "ko_KR", EventHandler ttsReadyFinishedCallback = null)
340         {
341             if (ttsLipSyncer == null || ttsLipSyncer.TtsHandle == null)
342             {
343                 Log.Error(LogTag, "tts is null");
344                 return false;
345             }
346
347             if (!ttsLipSyncer.IsSupportedVoice(lang))
348             {
349                 Log.Error(LogTag, $"{lang} is not supported");
350                 return false;
351             }
352
353             Log.Info(LogTag, "Current TTS State :" + ttsLipSyncer.TtsHandle.CurrentState);
354             if (ttsLipSyncer.TtsHandle.CurrentState != Tizen.Uix.Tts.State.Ready)
355             {
356                 Log.Error(LogTag, "TTS is not ready");
357                 return false;
358             }
359             try
360             {
361                 ttsLipSyncer.AddText(text, lang);
362                 ttsLipSyncer.PlayAsync(ttsReadyFinishedCallback);
363             }
364             catch (Exception e)
365             {
366                 Log.Error(LogTag, $"error :{e.Message}");
367                 Log.Error(LogTag, $"{e.StackTrace}");
368                 return false;
369             }
370             return true;
371         }
372
373         /// <summary>
374         /// <param name=""></param>
375         /// </summary>
376         [EditorBrowsable(EditorBrowsableState.Never)]
377         internal void PauseTTS()
378         {
379             if (ttsLipSyncer == null || ttsLipSyncer.TtsHandle == null)
380             {
381                 Log.Error(LogTag, "tts is null");
382                 return;
383             }
384
385             try
386             {
387                 Log.Info(LogTag, "Current TTS State :" + ttsLipSyncer.TtsHandle.CurrentState);
388                 ttsLipSyncer?.Pause();
389             }
390             catch (Exception e)
391             {
392                 Log.Error(LogTag, $"error :{e.Message}");
393                 Log.Error(LogTag, $"{e.StackTrace}");
394             }
395         }
396
397         /// <summary>
398         /// <param name=""></param>
399         /// </summary>
400         [EditorBrowsable(EditorBrowsableState.Never)]
401         internal void StopTTS()
402         {
403             if (ttsLipSyncer == null || ttsLipSyncer.TtsHandle == null)
404             {
405                 Log.Error(LogTag, "tts is null");
406                 return;
407             }
408
409             try
410             {
411                 Log.Info(LogTag, "Current TTS State :" + ttsLipSyncer.TtsHandle.CurrentState);
412                 ttsLipSyncer?.Stop();
413             }
414             catch (Exception e)
415             {
416                 Log.Error(LogTag, $"error :{e.Message}");
417                 Log.Error(LogTag, $"{e.StackTrace}");
418             }
419         }
420     }
421 }