Release 4.0.0-preview1-00201
[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     public class AudioEffect
26     {
27         private readonly EqualizerBand[] _bands;
28
29         internal AudioEffect(Player owner)
30         {
31             Player = owner;
32
33             bool available = false;
34
35             Native.EqualizerIsAvailable(Player.Handle, out available).
36                 ThrowIfFailed("Failed to initialize the AudioEffect");
37
38             IsAvailable = available;
39
40             if (IsAvailable == false)
41             {
42                 return;
43             }
44
45             int count = 0;
46             Native.GetEqualizerBandsCount(Player.Handle, out count).
47                 ThrowIfFailed("Failed to initialize the AudioEffect");
48
49             int min = 0;
50             int max = 0;
51             Native.GetEqualizerLevelRange(Player.Handle, out min, out max).
52                 ThrowIfFailed("Failed to initialize the AudioEffect");
53
54             Count = count;
55             BandLevelRange = new Range(min, max);
56
57             _bands = new EqualizerBand[count];
58         }
59
60         /// <summary>
61         /// Gets a <see cref="EqualizerBand"/> at the specified index.
62         /// </summary>
63         /// <param name="index">The index of the band to get.</param>
64         /// <exception cref="ObjectDisposedException">The <see cref="Player"/> has already been disposed of.</exception>
65         /// <exception cref="ArgumentOutOfRangeException">
66         ///     <pramref name="index"/> is less than zero.\n
67         ///     -or-\n
68         ///     <paramref name="index"/> is equal to or greater than <see cref="Count"/>.
69         /// </exception>
70         public EqualizerBand this[int index]
71         {
72             get
73             {
74                 Player.ValidateNotDisposed();
75
76                 if (index < 0 || Count <= index)
77                 {
78                     throw new ArgumentOutOfRangeException(nameof(index), index,
79                         $"Valid index is 0 <= x < { nameof(Count) } ");
80                 }
81
82                 if (_bands[index] == null)
83                 {
84                     _bands[index] = new EqualizerBand(this, index);
85                 }
86                 Log.Info(PlayerLog.Tag, "get equalizer band : " + _bands[index]);
87                 return _bands[index];
88             }
89         }
90
91         /// <summary>
92         /// Clears the equalizer effect.
93         /// </summary>
94         /// <exception cref="ObjectDisposedException">The <see cref="Player"/> has already been disposed of.</exception>
95         public void Clear()
96         {
97             Player.ValidateNotDisposed();
98
99             Native.EqualizerClear(Player.Handle).
100                 ThrowIfFailed("Failed to clear equalizer effect");
101         }
102
103         /// <summary>
104         /// Gets the number of items.
105         /// </summary>
106         public int Count { get; }
107
108         /// <summary>
109         /// Gets the band level range of the bands in the dB.
110         /// </summary>
111         public Range BandLevelRange { get; }
112
113         /// <summary>
114         /// Gets the value whether the AudioEffect is available or not.
115         /// </summary>
116         public bool IsAvailable { get; }
117
118         /// <summary>
119         /// Gets the player that this AudioEffect belongs to.
120         /// </summary>
121         public Player Player { get; }
122     }
123 }