2 * Copyright(c) 2023 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Collections.Generic;
19 using System.ComponentModel;
23 using static Tizen.AIAvatar.AIAvatar;
25 namespace Tizen.AIAvatar
27 [EditorBrowsable(EditorBrowsableState.Never)]
28 internal class AvatarTTS
30 private TTSLipSyncer ttsLipSyncer;
32 private event EventHandler ttsReadyFinished;
33 private readonly Object ttsReadyFinishedLock = new Object();
35 [EditorBrowsable(EditorBrowsableState.Never)]
42 [EditorBrowsable(EditorBrowsableState.Never)]
43 internal event EventHandler TTSReadyFinished
47 lock (ttsReadyFinishedLock)
49 ttsReadyFinished += value;
56 lock (ttsReadyFinishedLock)
58 if (ttsReadyFinished == null)
60 Log.Error(LogTag, "ttsReadyFinished is null");
63 ttsReadyFinished -= value;
69 /// <param name=""></param>
71 [EditorBrowsable(EditorBrowsableState.Never)]
72 internal TtsClient CurrentTTSClient => ttsLipSyncer?.TtsHandle;
75 /// <param name=""></param>
77 [EditorBrowsable(EditorBrowsableState.Never)]
78 internal void InitTTS(Avatar avatar)
80 if (ttsLipSyncer == null)
84 ttsLipSyncer = new TTSLipSyncer(avatar?.AvatarAnimator?.LipSyncer);
88 Log.Error(LogTag, $"error :{e.Message}");
89 Log.Error(LogTag, $"{e.StackTrace}");
95 /// <param name=""></param>
97 [EditorBrowsable(EditorBrowsableState.Never)]
98 internal void DeinitTTS()
100 if (ttsLipSyncer != null)
104 ttsLipSyncer.DeinitTts();
109 Log.Error(LogTag, $"error :{e.Message}");
110 Log.Error(LogTag, $"{e.StackTrace}");
116 /// <param name=""></param>
118 [EditorBrowsable(EditorBrowsableState.Never)]
119 internal List<VoiceInfo> GetSupportedVoices()
121 return ttsLipSyncer.GetSupportedVoices();
125 /// <param name=""></param>
127 [EditorBrowsable(EditorBrowsableState.Never)]
128 internal bool PrepareTTS(string text, VoiceInfo voiceInfo, EventHandler ttsReadyFinishedCallback = null)
130 if (ttsLipSyncer == null || ttsLipSyncer.TtsHandle == null)
132 Log.Error(LogTag, "tts is null");
137 if (!ttsLipSyncer.IsSupportedVoice(voiceInfo))
139 Log.Info(LogTag, $"{voiceInfo.Lang} & {voiceInfo.Type} is not supported");
143 Log.Info(LogTag, "Current TTS State :" + ttsLipSyncer.TtsHandle.CurrentState);
144 if (ttsLipSyncer.TtsHandle.CurrentState != Tizen.Uix.Tts.State.Ready)
146 Log.Info(LogTag, "TTS is not ready");
152 ttsLipSyncer.AddText(text, voiceInfo);
153 ttsLipSyncer.Prepare(ttsReadyFinishedCallback);
157 Log.Error(LogTag, $"error :{e.Message}");
158 Log.Error(LogTag, $"{e.StackTrace}");
165 /// <param name=""></param>
167 [EditorBrowsable(EditorBrowsableState.Never)]
168 internal bool PrepareTTS(string text, string lang = "ko_KR", EventHandler ttsReadyFinishedCallback = null)
170 if (ttsLipSyncer == null || ttsLipSyncer.TtsHandle == null)
172 Log.Error(LogTag, "tts is null");
176 if (!ttsLipSyncer.IsSupportedVoice(lang))
178 Log.Error(LogTag, $"{lang} is not supported");
182 Log.Info(LogTag, "Current TTS State :" + ttsLipSyncer.TtsHandle.CurrentState);
183 if (ttsLipSyncer.TtsHandle.CurrentState != Tizen.Uix.Tts.State.Ready)
185 Log.Error(LogTag, "TTS is not ready");
190 ttsLipSyncer.AddText(text, lang);
191 ttsLipSyncer.Prepare(ttsReadyFinishedCallback);
195 Log.Error(LogTag, $"error :{e.Message}");
196 Log.Error(LogTag, $"{e.StackTrace}");
203 /// <param name=""></param>
205 [EditorBrowsable(EditorBrowsableState.Never)]
206 internal bool PlayPreparedTTS()
208 if (ttsLipSyncer == null || ttsLipSyncer.TtsHandle == null)
210 Log.Error(LogTag, "tts is null");
214 return ttsLipSyncer.PlayPreparedText();
218 /// <param name=""></param>
220 [EditorBrowsable(EditorBrowsableState.Never)]
221 internal bool PlayTTS(string text, VoiceInfo voiceInfo)
223 if (ttsLipSyncer == null || ttsLipSyncer.TtsHandle == null)
225 Log.Error(LogTag, "tts is null");
230 if (!ttsLipSyncer.IsSupportedVoice(voiceInfo))
232 Log.Info(LogTag, $"{voiceInfo.Lang} & {voiceInfo.Type} is not supported");
236 Log.Info(LogTag, "Current TTS State :" + ttsLipSyncer.TtsHandle.CurrentState);
237 if (ttsLipSyncer.TtsHandle.CurrentState != Tizen.Uix.Tts.State.Ready)
239 Log.Info(LogTag, "TTS is not ready");
245 ttsLipSyncer.AddText(text, voiceInfo);
250 Log.Error(LogTag, $"error :{e.Message}");
251 Log.Error(LogTag, $"{e.StackTrace}");
258 /// <param name=""></param>
260 [EditorBrowsable(EditorBrowsableState.Never)]
261 internal bool PlayTTS(string text, string lang = "ko_KR")
263 if (ttsLipSyncer == null || ttsLipSyncer.TtsHandle == null)
265 Log.Error(LogTag, "tts is null");
269 if (!ttsLipSyncer.IsSupportedVoice(lang))
271 Log.Error(LogTag, $"{lang} is not supported");
275 Log.Info(LogTag, "Current TTS State :" + ttsLipSyncer.TtsHandle.CurrentState);
276 if (ttsLipSyncer.TtsHandle.CurrentState != Tizen.Uix.Tts.State.Ready)
278 Log.Error(LogTag, "TTS is not ready");
283 ttsLipSyncer.AddText(text, lang);
288 Log.Error(LogTag, $"error :{e.Message}");
289 Log.Error(LogTag, $"{e.StackTrace}");
296 /// <param name=""></param>
298 [EditorBrowsable(EditorBrowsableState.Never)]
299 internal bool PlayTTSAsync(string text, VoiceInfo voiceInfo, EventHandler ttsReadyFinishedCallback = null)
301 if (ttsLipSyncer == null || ttsLipSyncer.TtsHandle == null)
303 Log.Error(LogTag, "tts is null");
308 if (!ttsLipSyncer.IsSupportedVoice(voiceInfo))
310 Log.Info(LogTag, $"{voiceInfo.Lang} & {voiceInfo.Type} is not supported");
314 Log.Info(LogTag, "Current TTS State :" + ttsLipSyncer.TtsHandle.CurrentState);
315 if (ttsLipSyncer.TtsHandle.CurrentState != Tizen.Uix.Tts.State.Ready)
317 Log.Info(LogTag, "TTS is not ready");
323 ttsLipSyncer.AddText(text, voiceInfo);
324 ttsLipSyncer.PlayAsync(ttsReadyFinishedCallback);
328 Log.Error(LogTag, $"error :{e.Message}");
329 Log.Error(LogTag, $"{e.StackTrace}");
336 /// <param name=""></param>
338 [EditorBrowsable(EditorBrowsableState.Never)]
339 internal bool PlayTTS(string text, string lang = "ko_KR", EventHandler ttsReadyFinishedCallback = null)
341 if (ttsLipSyncer == null || ttsLipSyncer.TtsHandle == null)
343 Log.Error(LogTag, "tts is null");
347 if (!ttsLipSyncer.IsSupportedVoice(lang))
349 Log.Error(LogTag, $"{lang} is not supported");
353 Log.Info(LogTag, "Current TTS State :" + ttsLipSyncer.TtsHandle.CurrentState);
354 if (ttsLipSyncer.TtsHandle.CurrentState != Tizen.Uix.Tts.State.Ready)
356 Log.Error(LogTag, "TTS is not ready");
361 ttsLipSyncer.AddText(text, lang);
362 ttsLipSyncer.PlayAsync(ttsReadyFinishedCallback);
366 Log.Error(LogTag, $"error :{e.Message}");
367 Log.Error(LogTag, $"{e.StackTrace}");
374 /// <param name=""></param>
376 [EditorBrowsable(EditorBrowsableState.Never)]
377 internal void PauseTTS()
379 if (ttsLipSyncer == null || ttsLipSyncer.TtsHandle == null)
381 Log.Error(LogTag, "tts is null");
387 Log.Info(LogTag, "Current TTS State :" + ttsLipSyncer.TtsHandle.CurrentState);
388 ttsLipSyncer?.Pause();
392 Log.Error(LogTag, $"error :{e.Message}");
393 Log.Error(LogTag, $"{e.StackTrace}");
398 /// <param name=""></param>
400 [EditorBrowsable(EditorBrowsableState.Never)]
401 internal void StopTTS()
403 if (ttsLipSyncer == null || ttsLipSyncer.TtsHandle == null)
405 Log.Error(LogTag, "tts is null");
411 Log.Info(LogTag, "Current TTS State :" + ttsLipSyncer.TtsHandle.CurrentState);
412 ttsLipSyncer?.Stop();
416 Log.Error(LogTag, $"error :{e.Message}");
417 Log.Error(LogTag, $"{e.StackTrace}");