[MediaPlayer] add APIs to use AudioOffload (#974)
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.MediaPlayer / Player / AudioEffect.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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 using System;
18 using Native = Interop.AudioEffect;
19
20 namespace Tizen.Multimedia
21 {
22     /// <summary>
23     /// Provides the ability to control the audio effects for <see cref="Multimedia.Player"/>.
24     /// </summary>
25     /// <since_tizen> 3 </since_tizen>
26     public class AudioEffect
27     {
28         private readonly EqualizerBand[] _bands;
29
30         internal AudioEffect(Player owner)
31         {
32             Player = owner;
33
34             if (IsAvailable== false)
35             {
36                 return;
37             }
38
39             _bands = new EqualizerBand[Count];
40         }
41
42         /// <summary>
43         /// Gets a <see cref="EqualizerBand"/> at the specified index.
44         /// </summary>
45         /// <param name="index">The index of the band to get.</param>
46         /// <exception cref="ObjectDisposedException">The <see cref="Player"/> has already been disposed of.</exception>
47         /// <exception cref="ArgumentOutOfRangeException">
48         ///     <pramref name="index"/> is less than zero.<br/>
49         ///     -or-<br/>
50         ///     <paramref name="index"/> is equal to or greater than <see cref="Count"/>.
51         /// </exception>
52         /// <exception cref="InvalidOperationException">
53         ///     If audio offload is enabled by calling <see cref="AudioOffload.IsEnabled"/>. (Since tizen 6.0)
54         /// </exception>
55         /// <since_tizen> 3 </since_tizen>
56         public EqualizerBand this[int index]
57         {
58             get
59             {
60                 Player.ValidateNotDisposed();
61                 Player.AudioOffload.CheckDisabled();
62
63                 if (index < 0 || Count <= index)
64                 {
65                     throw new ArgumentOutOfRangeException(nameof(index), index,
66                         $"Valid index is 0 <= x < { nameof(Count) } ");
67                 }
68
69                 if (_bands[index] == null)
70                 {
71                     _bands[index] = new EqualizerBand(this, index);
72                 }
73                 Log.Info(PlayerLog.Tag, "get equalizer band : " + _bands[index]);
74                 return _bands[index];
75             }
76         }
77
78         /// <summary>
79         /// Clears the equalizer effect.
80         /// </summary>
81         /// <exception cref="ObjectDisposedException">The <see cref="Player"/> has already been disposed of.</exception>
82         /// <exception cref="InvalidOperationException">
83         ///     If audio offload is enabled by calling <see cref="AudioOffload.IsEnabled"/>. (Since tizen 6.0)
84         /// </exception>
85         /// <since_tizen> 3 </since_tizen>
86         public void Clear()
87         {
88             Player.ValidateNotDisposed();
89             Player.AudioOffload.CheckDisabled();
90
91             Native.EqualizerClear(Player.Handle).
92                 ThrowIfFailed(Player, "Failed to clear equalizer effect");
93         }
94
95         /// <summary>
96         /// Gets the number of items.
97         /// </summary>
98         /// <exception cref="InvalidOperationException">
99         ///     If audio offload is enabled by calling <see cref="AudioOffload.IsEnabled"/>. (Since tizen 6.0)
100         /// </exception>
101         /// <since_tizen> 3 </since_tizen>
102         public int Count
103         {
104             get
105             {
106                 Player.AudioOffload.CheckDisabled();
107
108                 Native.GetEqualizerBandsCount(Player.Handle, out var count).
109                     ThrowIfFailed(Player, "Failed to initialize the AudioEffect");
110
111                 return count;
112             }
113         }
114
115         /// <summary>
116         /// Gets the band level range of the bands in the dB.
117         /// </summary>
118         /// <exception cref="InvalidOperationException">
119         ///     If audio offload is enabled by calling <see cref="AudioOffload.IsEnabled"/>. (Since tizen 6.0)
120         /// </exception>
121         /// <since_tizen> 3 </since_tizen>
122         public Range BandLevelRange
123         {
124             get
125             {
126                 Player.AudioOffload.CheckDisabled();
127
128                 Native.GetEqualizerLevelRange(Player.Handle, out var min, out var max).
129                     ThrowIfFailed(Player, "Failed to initialize the AudioEffect");
130
131                 return new Range(min, max);
132             }
133         }
134
135         /// <summary>
136         /// Gets the value whether the AudioEffect is available or not.
137         /// </summary>
138         /// <exception cref="InvalidOperationException">
139         ///     If audio offload is enabled by calling <see cref="AudioOffload.IsEnabled"/>. (Since tizen 6.0)
140         /// </exception>
141         /// <since_tizen> 3 </since_tizen>
142         public bool IsAvailable
143         {
144             get
145             {
146                 Player.AudioOffload.CheckDisabled();
147
148                 Native.EqualizerIsAvailable(Player.Handle, out var available).
149                     ThrowIfFailed(Player, "Failed to initialize the AudioEffect");
150                 return available;
151             }
152         }
153
154         /// <summary>
155         /// Gets the player that this AudioEffect belongs to.
156         /// </summary>
157         /// <since_tizen> 3 </since_tizen>
158         public Player Player { get; }
159     }
160 }