Release 12.0.0.18314
[platform/core/csapi/tizenfx.git] / src / Tizen.AIAvatar / src / Multimedia / Audio / AudioPlayer.cs
1 /*
2  * Copyright(c) 2024 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 : IDisposable
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                     Destroy();
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             }
70
71             if (audioPlayback != null)
72             {
73                 audioPlayback.Prepare();
74                 audioPlayback.BufferAvailable += (sender, args) =>
75                 {
76                     if (audioStream.Position == audioStream.Length)
77                     {
78                         return;
79                     }
80
81                     try
82                     {
83                         var buffer = new byte[args.Length];
84                         audioStream.Read(buffer, 0, args.Length);
85                         audioPlayback.Write(buffer);
86                     }
87                     catch (Exception e)
88                     {
89                         Log.Error(LogTag, $"Failed to write. {e.Message}");
90                     }
91                 };
92
93                 audioStream = new MemoryStream(audioBytes);
94             }
95         }
96
97         internal void Pause()
98         {
99             if (audioPlayback != null)
100             {
101                 audioPlayback.Pause();
102             }
103             else
104             {
105                 Log.Error(LogTag, $"audioPlayBack is null");
106             }
107         }
108
109         internal void Stop()
110         {
111             if (audioPlayback != null)
112             {
113                 audioPlayback.Pause();
114                 Destroy();
115             }
116             else
117             {
118                 Log.Error(LogTag, $"audioPlayBack is null");
119             }
120         }
121
122         public void Dispose()
123         {
124             Destroy();
125
126             audioStream?.Flush();
127             audioStream?.Dispose();
128             audioStream = null;
129         }
130
131         private void Destroy()
132         {
133             audioPlayback?.Unprepare();
134             audioPlayback?.Dispose();
135             audioPlayback = null;
136         }
137     }
138 }