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.
18 using System.Collections.Generic;
20 using static Tizen.AIAvatar.AIAvatar;
22 namespace Tizen.AIAvatar
24 internal class VowelConverter
26 private TFVowel6 vowelClassfierModel = null;
27 private VowelClassifier vowelClassifier = null;
28 private Dictionary<int, VowelClassifier> vowelClassifiers;
29 private AnimationConverter animationConverter = new AnimationConverter();
31 internal VowelConverter()
33 vowelClassifiers = new Dictionary<int, VowelClassifier>();
34 vowelClassifier = GetVowelClassifier(CurrentAudioOptions.SampleRate);
35 vowelClassfierModel = new TFVowel6(new int[4] { 12, 1, 1, 1 }, new int[4] { 7, 1, 1, 1 });
36 animationConverter.InitializeVisemeInfo(VisemeInfo);
39 internal AnimationKeyFrame CreateKeyFrames(string[] vowels, int sampleRate, bool isMic = false)
41 vowelClassifier = GetVowelClassifier(sampleRate);
45 return animationConverter.ConvertVowelsToAnimationMic(vowels, vowelClassifier.GetStepTime());
49 return animationConverter.ConvertVowelsToAnimation(vowels, vowelClassifier.GetStepTime());
53 internal AnimationKeyFrame CreateKeyFrames(byte[] audioData, int sampleRate, bool isMic = false)
55 vowelClassifier = GetVowelClassifier(sampleRate);
57 if (vowelClassifier == null)
59 Log.Error(LogTag, "Failed to play Buffer");
62 var vowels = PredictVowels(audioData);
63 Log.Info(LogTag, $"vowelRecognition: {string.Join(", ", vowels)}");
65 if (isMic) return animationConverter.ConvertVowelsToAnimationMic(vowels, vowelClassifier.GetStepTime());
66 else return animationConverter.ConvertVowelsToAnimation(vowels, vowelClassifier.GetStepTime());
69 internal string[] PredictVowels(byte[] audioData)
71 var vowels = vowelClassifier.Inference(audioData, vowelClassfierModel);
75 internal VowelClassifier GetVowelClassifier(int sampleRate)
77 if (vowelClassifiers.ContainsKey(sampleRate))
79 return vowelClassifiers[sampleRate];
83 vowelClassifiers[sampleRate] = new VowelClassifier(sampleRate);
84 return vowelClassifiers[sampleRate];