2 * Copyright(c) 2024 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.
19 using System.Collections.Generic;
20 using System.ComponentModel;
23 using static Tizen.AIAvatar.AIAvatar;
25 namespace Tizen.AIAvatar
27 [EditorBrowsable(EditorBrowsableState.Never)]
28 internal class AsyncLipSyncer : LipSyncer
30 private readonly uint AsyncPlayTime = 160;
31 private Queue<Animation> lipAnimations;
32 private Queue<byte[]> lipAudios;
33 private Timer asyncVowelTimer;
35 private bool isAsyncInit = false;
36 private bool isFinishAsyncLip = false;
38 [EditorBrowsable(EditorBrowsableState.Never)]
39 internal AsyncLipSyncer()
44 internal int SampleRate { get; set; }
46 [EditorBrowsable(EditorBrowsableState.Never)]
47 internal bool IsAsyncInit { get=>isAsyncInit; set=>isAsyncInit = value; }
49 [EditorBrowsable(EditorBrowsableState.Never)]
50 internal void SetFinishAsyncLip(bool finished)
52 isFinishAsyncLip = finished;
55 [EditorBrowsable(EditorBrowsableState.Never)]
56 protected new void OnLipAnimationFinished(object sender, EventArgs e)
60 CurrentMotionState = AvatarMotionState.Stopped;
64 Tizen.Log.Error(LogTag, "OnLipAnimationFinished---------------c");
66 if (isFinishAsyncLip && lipAnimations.Count == 0)
68 Tizen.Log.Error(LogTag, "Finish vowel lip sync");
71 CurrentMotionState = AvatarMotionState.Stopped;
78 internal void InitAsyncLipsync()
80 if (lipAnimations == null)
82 lipAnimations = new Queue<Animation>();
86 lipAnimations.Clear();
89 if (lipAudios == null)
91 lipAudios = new Queue<byte[]>();
99 internal void EnqueueAnimation(byte[] recordBuffer, int sampleRate, int audioLength)
101 var createdAni = CreateAsyncLipAnimation(recordBuffer, sampleRate);
102 if (createdAni != null)
104 lipAnimations.Enqueue(createdAni);
107 //Use Audio Full File
108 ///var createdAni = avatarLipSyncer.CreateLipAnimation(recordBuffer, sampleRate);
109 //lipAnimations.Enqueue(createdAni);
111 var currentAudioBuffer = new byte[audioLength];
112 Buffer.BlockCopy(recordBuffer, 0, currentAudioBuffer, 0, audioLength);
114 lipAudios.Enqueue(currentAudioBuffer);
117 internal bool PlayAsyncLip(int sampleRate, bool isFinishAsyncLip)
121 if (lipAudios.Count <= 0 && lipAnimations.Count <= 0)
123 Tizen.Log.Info(LogTag, "Return lipaudio 0");
124 if (isFinishAsyncLip)
126 Tizen.Log.Info(LogTag, "Finish Async lipsync");
131 Tizen.Log.Info(LogTag, "Async timer tick lipAudios : " + lipAudios.Count);
132 Tizen.Log.Info(LogTag, "Async timer tick lipAnimations : " + lipAnimations.Count);
134 var lipAnimation = lipAnimations.Dequeue();
135 lipAnimation.Finished += OnLipAnimationFinished;
137 ResetLipAnimation(lipAnimation);
139 var audioBuffer = lipAudios.Dequeue();
140 AudioPlayer.PlayAsync(audioBuffer, sampleRate);
146 Log.Error(LogTag, $"---Log Tick : {ex.StackTrace}");
152 internal void StartAsyncLipPlayTimer()
154 if (asyncVowelTimer == null)
156 Tizen.Log.Info(LogTag, "Start Async");
157 asyncVowelTimer = new Timer(AsyncPlayTime);
158 asyncVowelTimer.Tick += OnAsyncVowelTick;
159 asyncVowelTimer.Start();
164 private void DestroyVowelTimer()
166 if (asyncVowelTimer != null)
169 asyncVowelTimer.Tick -= OnAsyncVowelTick;
170 asyncVowelTimer.Stop();
171 asyncVowelTimer.Dispose();
172 asyncVowelTimer = null;
176 private bool OnAsyncVowelTick(object source, Tizen.NUI.Timer.TickEventArgs e)
178 return PlayAsyncLip(SampleRate, isFinishAsyncLip);