Add Tizen.AIAvatar project (#6014)
[platform/core/csapi/tizenfx.git] / src / Tizen.AIAvatar / src / internal / Multimedia / AudioPlayer.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 Tizen.Multimedia;
19 using System.IO;
20 using System;
21
22 using static Tizen.AIAvatar.AIAvatar;
23
24 namespace Tizen.AIAvatar
25 {
26     internal class AudioPlayer
27     {
28         private AudioPlayback audioPlayback;
29         private MemoryStream audioStream;
30
31         internal AudioPlayer()
32         {
33         }
34
35         internal void PlayAsync(byte[] buffer, int sampleRate = 0)
36         {
37             if (audioPlayback == null)
38             {
39                 Play(buffer, sampleRate);
40             }
41             else
42             {
43                 audioPlayback.Write(buffer);
44             }
45         }
46
47         internal void Play(byte[] audioBytes, int sampleRate = 0)
48         {
49             if (audioBytes == null)
50             {
51                 return;
52             }
53
54             try
55             {
56                 if (audioPlayback != null)
57                 {
58                     DestroyAudioPlayback();
59                 }
60                 if (sampleRate == 0)
61                 {
62                     sampleRate = CurrentAudioOptions.SampleRate;
63                 }
64                 audioPlayback = new AudioPlayback(sampleRate, CurrentAudioOptions.Channel, CurrentAudioOptions.SampleType);
65             }
66             catch (Exception e)
67             {
68                 Log.Error(LogTag, $"Failed to create AudioPlayback. {e.Message}");
69                 return;
70             }
71
72             if (audioPlayback != null)
73             {
74                 audioPlayback.Prepare();
75                 audioPlayback.BufferAvailable += (sender, args) =>
76                 {
77                     if (audioStream.Position == audioStream.Length)
78                     {
79                         return;
80                     }
81
82                     try
83                     {
84                         var buffer = new byte[args.Length];
85                         audioStream.Read(buffer, 0, args.Length);
86                         audioPlayback.Write(buffer);
87                     }
88                     catch (Exception e)
89                     {
90                         Log.Error(LogTag, $"Failed to write. {e.Message}");
91                     }
92                 };
93
94                 audioStream = new MemoryStream(audioBytes);
95             }
96         }
97
98         internal void Pause()
99         {
100             if (audioPlayback != null)
101             {
102                 audioPlayback.Pause();
103             }
104             else
105             {
106                 Log.Error(LogTag, $"audioPlayBack is null");
107             }
108         }
109
110         internal void Stop()
111         {
112             if (audioPlayback != null)
113             {
114                 audioPlayback.Pause();
115                 DestroyAudioPlayback();
116             }
117             else
118             {
119                 Log.Error(LogTag, $"audioPlayBack is null");
120             }
121         }
122
123         internal void Destroy()
124         {
125             DestroyAudioPlayback();
126         }
127
128         private void DestroyAudioPlayback()
129         {
130             audioPlayback?.Unprepare();
131             audioPlayback?.Dispose();
132             audioPlayback = null;
133         }
134     }
135 }