4 * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
29 #include <iniparser.h>
31 #include "tizen-audio-internal.h"
32 #include "tizen-audio-impl.h"
34 #define VOLUME_INI_DEFAULT_PATH SYSCONFDIR"/multimedia/mmfw_audio_volume.ini" /* SYSCONFDIR is defined at .spec */
35 #define VOLUME_INI_TEMP_PATH "/opt/system/mmfw_audio_volume.ini"
36 #define VOLUME_VALUE_MAX (1.0f)
37 #define GAIN_VALUE_MAX (1.0f)
38 #define RADIO_TUNING_DEFUALT_FILE SYSCONFDIR"/multimedia/mmfw_fmradio.ini"
39 #define RADIO_TUNING_TEMP_FILE "/opt/system/.mmfw_fmradio.ini"
40 #define RADIO_TUNING_ENABLE "tuning:enable"
41 #define RADIO_TUNING_VOLUME_LEVELS "fmradio:volume_levels"
42 #define RADIO_TUNING_VOLUME_TABLE "fmradio:volume_table"
43 #define RADIO_VOLUME_NUM_MAX 100
45 static const char *g_volume_vconf[AUDIO_VOLUME_TYPE_MAX] = {
46 "file/private/sound/volume/system", /* AUDIO_VOLUME_TYPE_SYSTEM */
47 "file/private/sound/volume/notification", /* AUDIO_VOLUME_TYPE_NOTIFICATION */
48 "file/private/sound/volume/alarm", /* AUDIO_VOLUME_TYPE_ALARM */
49 "file/private/sound/volume/ringtone", /* AUDIO_VOLUME_TYPE_RINGTONE */
50 "file/private/sound/volume/media", /* AUDIO_VOLUME_TYPE_MEDIA */
51 "file/private/sound/volume/call", /* AUDIO_VOLUME_TYPE_CALL */
52 "file/private/sound/volume/voip", /* AUDIO_VOLUME_TYPE_VOIP */
53 "file/private/sound/volume/voice", /* AUDIO_VOLUME_TYPE_VOICE */
56 static const char *__get_volume_type_string_by_idx(uint32_t vol_type_idx)
58 switch (vol_type_idx) {
59 case AUDIO_VOLUME_TYPE_SYSTEM: return "system";
60 case AUDIO_VOLUME_TYPE_NOTIFICATION: return "notification";
61 case AUDIO_VOLUME_TYPE_ALARM: return "alarm";
62 case AUDIO_VOLUME_TYPE_RINGTONE: return "ringtone";
63 case AUDIO_VOLUME_TYPE_MEDIA: return "media";
64 case AUDIO_VOLUME_TYPE_CALL: return "call";
65 case AUDIO_VOLUME_TYPE_VOIP: return "voip";
66 case AUDIO_VOLUME_TYPE_VOICE: return "voice";
67 default: return "invalid";
71 static uint32_t __get_volume_idx_by_string_type(const char *vol_type)
73 if (!strncmp(vol_type, "system", strlen(vol_type)) || !strncmp(vol_type, "0", strlen(vol_type)))
74 return AUDIO_VOLUME_TYPE_SYSTEM;
75 else if (!strncmp(vol_type, "notification", strlen(vol_type)) || !strncmp(vol_type, "1", strlen(vol_type)))
76 return AUDIO_VOLUME_TYPE_NOTIFICATION;
77 else if (!strncmp(vol_type, "alarm", strlen(vol_type)) || !strncmp(vol_type, "2", strlen(vol_type)))
78 return AUDIO_VOLUME_TYPE_ALARM;
79 else if (!strncmp(vol_type, "ringtone", strlen(vol_type)) || !strncmp(vol_type, "3", strlen(vol_type)))
80 return AUDIO_VOLUME_TYPE_RINGTONE;
81 else if (!strncmp(vol_type, "media", strlen(vol_type)) || !strncmp(vol_type, "4", strlen(vol_type)))
82 return AUDIO_VOLUME_TYPE_MEDIA;
83 else if (!strncmp(vol_type, "call", strlen(vol_type)) || !strncmp(vol_type, "5", strlen(vol_type)))
84 return AUDIO_VOLUME_TYPE_CALL;
85 else if (!strncmp(vol_type, "voip", strlen(vol_type)) || !strncmp(vol_type, "6", strlen(vol_type)))
86 return AUDIO_VOLUME_TYPE_VOIP;
87 else if (!strncmp(vol_type, "voice", strlen(vol_type)) || !strncmp(vol_type, "7", strlen(vol_type)))
88 return AUDIO_VOLUME_TYPE_VOICE;
90 return AUDIO_VOLUME_TYPE_MEDIA;
93 static const char *__get_gain_type_string_by_idx(uint32_t gain_type_idx)
95 switch (gain_type_idx) {
96 case AUDIO_GAIN_TYPE_DEFAULT: return "default";
97 case AUDIO_GAIN_TYPE_DIALER: return "dialer";
98 case AUDIO_GAIN_TYPE_TOUCH: return "touch";
99 case AUDIO_GAIN_TYPE_AF: return "af";
100 case AUDIO_GAIN_TYPE_SHUTTER1: return "shutter1";
101 case AUDIO_GAIN_TYPE_SHUTTER2: return "shutter2";
102 case AUDIO_GAIN_TYPE_CAMCODING: return "camcording";
103 case AUDIO_GAIN_TYPE_MIDI: return "midi";
104 case AUDIO_GAIN_TYPE_BOOTING: return "booting";
105 case AUDIO_GAIN_TYPE_VIDEO: return "video";
106 case AUDIO_GAIN_TYPE_TTS: return "tts";
107 default: return "invalid";
111 static void __dump_tb(audio_hal_t *ah)
113 audio_volume_value_table_t *volume_value_table = ah->volume.volume_value_table;
114 uint32_t vol_type_idx, vol_level_idx, gain_type_idx;
115 const char *gain_type_str[] = {
116 "def", /* AUDIO_GAIN_TYPE_DEFAULT */
117 "dial", /* AUDIO_GAIN_TYPE_DIALER */
118 "touch", /* AUDIO_GAIN_TYPE_TOUCH */
119 "af", /* AUDIO_GAIN_TYPE_AF */
120 "shut1", /* AUDIO_GAIN_TYPE_SHUTTER1 */
121 "shut2", /* AUDIO_GAIN_TYPE_SHUTTER2 */
122 "cam", /* AUDIO_GAIN_TYPE_CAMCODING */
123 "midi", /* AUDIO_GAIN_TYPE_MIDI */
124 "boot", /* AUDIO_GAIN_TYPE_BOOTING */
125 "video", /* AUDIO_GAIN_TYPE_VIDEO */
126 "tts", /* AUDIO_GAIN_TYPE_TTS */
128 char dump_str[AUDIO_DUMP_STR_LEN], *dump_str_ptr;
130 /* Dump volume table */
131 AUDIO_LOG_INFO("<<<<< volume table >>>>>");
133 const char *table_str = "volumes";
135 AUDIO_LOG_INFO("<< %s >>", table_str);
137 for (vol_type_idx = 0; vol_type_idx < AUDIO_VOLUME_TYPE_MAX; vol_type_idx++) {
138 const char *vol_type_str = __get_volume_type_string_by_idx(vol_type_idx);
140 dump_str_ptr = &dump_str[0];
141 memset(dump_str, 0x00, sizeof(char) * sizeof(dump_str));
142 snprintf(dump_str_ptr, 8, "%6s:", vol_type_str);
143 dump_str_ptr += strlen(dump_str_ptr);
145 for (vol_level_idx = 0; vol_level_idx < volume_value_table->volume_level_max[vol_type_idx]; vol_level_idx++) {
146 snprintf(dump_str_ptr, 6, "%01.2f ", volume_value_table->volume[vol_type_idx][vol_level_idx]);
147 dump_str_ptr += strlen(dump_str_ptr);
149 AUDIO_LOG_INFO("%s", dump_str);
152 volume_value_table = ah->volume.volume_value_table;
154 /* Dump gain table */
155 AUDIO_LOG_INFO("<<<<< gain table >>>>>");
157 dump_str_ptr = &dump_str[0];
158 memset(dump_str, 0x00, sizeof(char) * sizeof(dump_str));
160 snprintf(dump_str_ptr, 11, "%10s", " ");
161 dump_str_ptr += strlen(dump_str_ptr);
163 for (gain_type_idx = 0; gain_type_idx < AUDIO_GAIN_TYPE_MAX; gain_type_idx++) {
164 snprintf(dump_str_ptr, 7, "%5s ", gain_type_str[gain_type_idx]);
165 dump_str_ptr += strlen(dump_str_ptr);
167 AUDIO_LOG_INFO("%s", dump_str);
169 dump_str_ptr = &dump_str[0];
170 memset(dump_str, 0x00, sizeof(char) * sizeof(dump_str));
172 snprintf(dump_str_ptr, 11, "%9s:", table_str);
173 dump_str_ptr += strlen(dump_str_ptr);
175 for (gain_type_idx = 0; gain_type_idx < AUDIO_GAIN_TYPE_MAX; gain_type_idx++) {
176 snprintf(dump_str_ptr, 7, "%01.3f ", volume_value_table->gain[gain_type_idx]);
177 dump_str_ptr += strlen(dump_str_ptr);
179 AUDIO_LOG_INFO("%s", dump_str);
183 static audio_return_t __load_volume_value_table_from_ini(audio_hal_t *ah)
185 dictionary * dict = NULL;
186 uint32_t vol_type_idx, vol_level_idx, gain_type_idx;
187 audio_volume_value_table_t *volume_value_table = ah->volume.volume_value_table;
190 dict = iniparser_load(VOLUME_INI_TEMP_PATH);
192 AUDIO_LOG_DEBUG("Use default volume&gain ini file");
193 dict = iniparser_load(VOLUME_INI_DEFAULT_PATH);
195 AUDIO_LOG_WARN("Loading volume&gain table from ini file failed");
196 return AUDIO_ERR_UNDEFINED;
200 const char delimiter[] = ", ";
201 char *key, *list_str, *token, *ptr = NULL;
202 const char *table_str = "volumes";
204 /* Load volume table */
205 for (vol_type_idx = 0; vol_type_idx < AUDIO_VOLUME_TYPE_MAX; vol_type_idx++) {
206 const char *vol_type_str = __get_volume_type_string_by_idx(vol_type_idx);
208 volume_value_table->volume_level_max[vol_type_idx] = 0;
209 size = strlen(table_str) + strlen(vol_type_str) + 2;
212 snprintf(key, size, "%s:%s", table_str, vol_type_str);
213 list_str = iniparser_getstring(dict, key, NULL);
215 token = strtok_r(list_str, delimiter, &ptr);
217 /* convert dB volume to linear volume */
218 double vol_value = 0.0f;
219 if (strncmp(token, "0", strlen(token)))
220 vol_value = pow(10.0, (atof(token) - 100) / 20.0);
221 volume_value_table->volume[vol_type_idx][volume_value_table->volume_level_max[vol_type_idx]++] = vol_value;
222 token = strtok_r(NULL, delimiter, &ptr);
225 volume_value_table->volume_level_max[vol_type_idx] = 1;
226 for (vol_level_idx = 0; vol_level_idx < AUDIO_VOLUME_LEVEL_MAX; vol_level_idx++) {
227 volume_value_table->volume[vol_type_idx][vol_level_idx] = VOLUME_VALUE_MAX;
234 /* Load gain table */
235 volume_value_table->gain[AUDIO_GAIN_TYPE_DEFAULT] = GAIN_VALUE_MAX;
236 for (gain_type_idx = AUDIO_GAIN_TYPE_DEFAULT + 1; gain_type_idx < AUDIO_GAIN_TYPE_MAX; gain_type_idx++) {
237 const char *gain_type_str = __get_gain_type_string_by_idx(gain_type_idx);
239 size = strlen(table_str) + strlen("gain") + strlen(gain_type_str) + 3;
242 snprintf(key, size, "%s:gain_%s", table_str, gain_type_str);
243 token = iniparser_getstring(dict, key, NULL);
245 volume_value_table->gain[gain_type_idx] = atof(token);
247 volume_value_table->gain[gain_type_idx] = GAIN_VALUE_MAX;
251 volume_value_table->gain[gain_type_idx] = GAIN_VALUE_MAX;
255 iniparser_freedict(dict);
262 static audio_return_t __load_radio_volume_table(int** volume_table, int *number_of_elements)
264 dictionary * dict = NULL;
265 const char delimiter[] = ", ";
268 char* list_str = NULL;
269 int* temp_table = NULL;
273 bool tuning_enable = 0;
277 dict = iniparser_load(RADIO_TUNING_DEFUALT_FILE);
279 AUDIO_LOG_ERROR("%s load failed", RADIO_TUNING_DEFUALT_FILE);
280 return AUDIO_ERR_UNDEFINED;
283 value = iniparser_getboolean(dict, RADIO_TUNING_ENABLE, not_found);
284 if (value == not_found) {
285 AUDIO_LOG_ERROR("Can't get Tuning Enable value");
287 tuning_enable = value;
288 AUDIO_LOG_INFO("Tuning enabled.");
290 iniparser_freedict(dict); /*Cleanup*/
294 AUDIO_LOG_INFO("Tuning enabled. load temp tuning file.");
295 dict = iniparser_load(RADIO_TUNING_TEMP_FILE);
297 AUDIO_LOG_WARN("%s load failed. Tuning enabled but there is not tuning temp file. Use temporary file", RADIO_TUNING_TEMP_FILE);
298 dict = iniparser_load(RADIO_TUNING_DEFUALT_FILE);
300 AUDIO_LOG_ERROR("%s load failed", RADIO_TUNING_DEFUALT_FILE);
301 return AUDIO_ERR_UNDEFINED;
305 AUDIO_LOG_INFO("Tuning diabled. load default tuning file.");
306 dict = iniparser_load(RADIO_TUNING_DEFUALT_FILE);
308 AUDIO_LOG_ERROR("%s load failed", RADIO_TUNING_DEFUALT_FILE);
309 return AUDIO_ERR_UNDEFINED;
313 *number_of_elements = iniparser_getint(dict, RADIO_TUNING_VOLUME_LEVELS, -1);
314 if (*number_of_elements <= 0 || *number_of_elements > RADIO_VOLUME_NUM_MAX) {
315 AUDIO_LOG_ERROR("invalid number of elements, %d", *number_of_elements);
316 ret = AUDIO_ERR_INTERNAL;
319 temp_table = (int *)malloc((*number_of_elements) * sizeof(int));
323 *volume_table = temp_table;
325 list_str = iniparser_getstring(dict, RADIO_TUNING_VOLUME_TABLE, NULL);
327 token = strtok_r(list_str, delimiter, &ptr);
329 temp_table[index] = atoi(token);
330 AUDIO_LOG_INFO("fm volume index %d is %d", index, temp_table[index]);
332 token = strtok_r(NULL, delimiter, &ptr);
336 iniparser_freedict(dict);
340 audio_return_t _audio_volume_set_level_radio(audio_hal_t *ah, uint32_t level)
342 audio_return_t audio_ret = AUDIO_RET_OK;
347 /* Applying mute at volume zero */
349 if ((audio_ret = _mixer_control_set_value(ah, MIXER_FMRADIO_MUTE, 0)))
350 AUDIO_LOG_ERROR("set mixer(%s) failed", MIXER_FMRADIO_MUTE);
353 if ((audio_ret = _mixer_control_get_value(ah, MIXER_FMRADIO_MUTE, &mute))) {
354 AUDIO_LOG_ERROR("get mixer(%s) failed", MIXER_FMRADIO_MUTE);
358 if ((audio_ret = _mixer_control_set_value(ah, MIXER_FMRADIO_MUTE, 1))) {
359 AUDIO_LOG_ERROR("set mixer(%s) failed", MIXER_FMRADIO_MUTE);
365 if ((_mixer_control_get_value(ah, MIXER_FMRADIO_L_VOLUME, &volume)))
366 AUDIO_LOG_ERROR("get mixer(%s) failed", MIXER_FMRADIO_L_VOLUME);
368 if (volume != ah->volume.radio_volume_value_table[level]) {
369 if ((audio_ret = _mixer_control_set_value(ah, MIXER_FMRADIO_L_VOLUME, ah->volume.radio_volume_value_table[level]))) {
370 AUDIO_LOG_ERROR("set mixer(%s) failed", MIXER_FMRADIO_L_VOLUME);
376 if ((_mixer_control_get_value(ah, MIXER_FMRADIO_R_VOLUME, &volume)))
377 AUDIO_LOG_ERROR("get mixer(%s) failed", MIXER_FMRADIO_R_VOLUME);
379 if (volume != ah->volume.radio_volume_value_table[level]) {
380 if ((audio_ret = _mixer_control_set_value(ah, MIXER_FMRADIO_R_VOLUME, ah->volume.radio_volume_value_table[level])))
381 AUDIO_LOG_ERROR("set mixer(%s) failed", MIXER_FMRADIO_R_VOLUME);
388 audio_return_t _audio_volume_init(audio_hal_t *ah)
392 audio_return_t audio_ret = AUDIO_RET_OK;
393 int init_value[AUDIO_VOLUME_TYPE_MAX] = { 9, 11, 7, 11, 7, 4, 4, 7 };
394 int* fm_table = NULL;
395 int number_of_steps = 0;
397 AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
399 for (i = 0; i < AUDIO_VOLUME_TYPE_MAX; i++) {
400 ah->volume.volume_level[i] = init_value[i];
403 for (i = 0; i < AUDIO_VOLUME_TYPE_MAX; i++) {
404 /* Get volume value string from VCONF */
405 if (vconf_get_int(g_volume_vconf[i], &val) < 0) {
406 AUDIO_LOG_ERROR("vconf_get_int(%s) failed", g_volume_vconf[i]);
410 AUDIO_LOG_INFO("read vconf. %s = %d", g_volume_vconf[i], val);
411 ah->volume.volume_level[i] = val;
414 if (!(ah->volume.volume_value_table = malloc(AUDIO_VOLUME_DEVICE_MAX * sizeof(audio_volume_value_table_t)))) {
415 AUDIO_LOG_ERROR("volume_value_table malloc failed");
416 return AUDIO_ERR_RESOURCE;
419 audio_ret = __load_volume_value_table_from_ini(ah);
420 if (audio_ret != AUDIO_RET_OK) {
421 AUDIO_LOG_ERROR("gain table load error");
422 return AUDIO_ERR_UNDEFINED;
425 /* radio volume table */
426 __load_radio_volume_table(&fm_table, &number_of_steps);
428 AUDIO_LOG_DEBUG("number of steps -> %d", number_of_steps);
429 /*copy from temp structure to main strcture*/
430 for (i = 0; i < number_of_steps; i++) {
431 ah->volume.radio_volume_value_table[i] = fm_table[i];
440 audio_return_t _audio_volume_deinit(audio_hal_t *ah)
442 AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
444 if (ah->volume.volume_value_table) {
445 free(ah->volume.volume_value_table);
446 ah->volume.volume_value_table = NULL;
452 audio_return_t audio_get_volume_level_max(void *audio_handle, audio_volume_info_t *info, uint32_t *level)
454 audio_hal_t *ah = (audio_hal_t *)audio_handle;
455 audio_volume_value_table_t *volume_value_table;
457 AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
458 AUDIO_RETURN_VAL_IF_FAIL(ah->volume.volume_value_table, AUDIO_ERR_PARAMETER);
460 /* Get max volume level by device & type */
461 volume_value_table = ah->volume.volume_value_table;
462 *level = volume_value_table->volume_level_max[__get_volume_idx_by_string_type(info->type)];
464 AUDIO_LOG_DEBUG("get_[%s] volume_level_max: %d", info->type, *level);
469 audio_return_t audio_get_volume_level(void *audio_handle, audio_volume_info_t *info, uint32_t *level)
471 audio_hal_t *ah = (audio_hal_t *)audio_handle;
473 AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
475 *level = ah->volume.volume_level[__get_volume_idx_by_string_type(info->type)];
477 AUDIO_LOG_INFO("get [%s] volume_level: %d, direction(%d)", info->type, *level, info->direction);
482 audio_return_t audio_get_volume_value(void *audio_handle, audio_volume_info_t *info, uint32_t level, double *value)
484 audio_hal_t *ah = (audio_hal_t *)audio_handle;
485 audio_volume_value_table_t *volume_value_table;
486 char dump_str[AUDIO_DUMP_STR_LEN] = {0,};
488 AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
489 AUDIO_RETURN_VAL_IF_FAIL(ah->volume.volume_value_table, AUDIO_ERR_PARAMETER);
491 /* Get basic volume by device & type & level */
492 volume_value_table = ah->volume.volume_value_table;
493 if (volume_value_table->volume_level_max[__get_volume_idx_by_string_type(info->type)] < level)
494 *value = VOLUME_VALUE_MAX;
496 *value = volume_value_table->volume[__get_volume_idx_by_string_type(info->type)][level];
497 *value *= volume_value_table->gain[AUDIO_GAIN_TYPE_DEFAULT]; /* need to fix getting gain via audio_info_t */
499 AUDIO_LOG_DEBUG("get_volume_value:%d(%s)=>%f %s", level, info->type, *value, &dump_str[0]);
504 audio_return_t audio_set_volume_level(void *audio_handle, audio_volume_info_t *info, uint32_t level)
506 audio_return_t audio_ret = AUDIO_RET_OK;
507 audio_hal_t *ah = (audio_hal_t *)audio_handle;
509 AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
511 /* Update volume level */
512 ah->volume.volume_level[__get_volume_idx_by_string_type(info->type)] = level;
513 AUDIO_LOG_INFO("set [%s] volume_level: %d, direction(%d)", info->type, level, info->direction);
515 /* set mixer related to H/W volume if needed */
516 if ((__get_volume_idx_by_string_type(info->type) == AUDIO_VOLUME_TYPE_MEDIA) && ah->device.is_radio_on)
517 audio_ret = _audio_volume_set_level_radio(ah, level);
522 audio_return_t audio_get_volume_mute(void *audio_handle, audio_volume_info_t *info, uint32_t *mute)
524 audio_return_t audio_ret = AUDIO_RET_OK;
525 audio_hal_t *ah = (audio_hal_t *)audio_handle;
527 AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
529 /* TODO. Not implemented */
534 audio_return_t audio_set_volume_mute(void *audio_handle, audio_volume_info_t *info, uint32_t mute)
536 audio_return_t audio_ret = AUDIO_RET_OK;
537 audio_hal_t *ah = (audio_hal_t *)audio_handle;
539 AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
540 /* TODO. Not implemented */