Add volume range translation
[profile/tv/apps/native/air_volume.git] / src / volume.c
1 /*
2  * Copyright (c) 2014 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 #include <app_debug.h>
18 #include <sound_manager.h>
19 #include <sound_manager_internal.h>
20
21 #include "define.h"
22 #include "volume.h"
23
24 static bool mute;
25 static int volume;
26
27 void volume_init(void)
28 {
29         volume = VOLUME_DEFAULT;
30         sound_manager_get_master_volume(&volume);
31
32         mute = false;
33 }
34
35 int volume_set_mute(bool is_mute)
36 {
37         mute = is_mute;
38
39         if (mute)
40                 sound_manager_set_master_volume(MUTE_VALUE);
41         else
42                 sound_manager_set_master_volume(volume);
43
44         return 0;
45 }
46
47 bool volume_is_mute(void)
48 {
49         return mute;
50 }
51
52 static int _volume_get_max_volume(void)
53 {
54         int vol;
55
56         vol = VOLUME_MAX;
57         sound_manager_get_max_master_volume(&vol);
58
59         return vol;
60 }
61
62 int volume_set_volume(int vol)
63 {
64         int r;
65
66         if (vol < VOLUME_MIN ||
67                         vol > VOLUME_MAX) {
68                 _ERR("volume value out of range");
69                 return -1;
70         }
71
72         vol = (vol * _volume_get_max_volume()) / VOLUME_MAX;
73
74         r = sound_manager_set_master_volume(vol);
75         if (r < 0) {
76                 _ERR("failed to set volume");
77                 return -1;
78         }
79
80         volume = vol;
81
82         return 0;
83 }
84
85 int volume_get_volume(void)
86 {
87         int vol, r;
88
89         if (volume_is_mute()) {
90                 vol = volume;
91         } else {
92                 r = sound_manager_get_master_volume(&vol);
93                 if (r < 0)
94                         return -1;
95         }
96
97         vol = (vol * VOLUME_MAX) / _volume_get_max_volume();
98
99         return vol;
100 }