summaryrefslogtreecommitdiff
path: root/tizen-audio-volume.c
blob: 96ea40a34119680fd97c78a23e1fd3da471e6aca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
 * audio-hal
 *
 * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#include <vconf.h>
#include <iniparser.h>

#include "tizen-audio-internal.h"

#define VOLUME_INI_DEFAULT_PATH     "/usr/etc/mmfw_audio_volume.ini"
#define VOLUME_INI_TEMP_PATH        "/opt/system/mmfw_audio_volume.ini"
#define VOLUME_VALUE_MAX            (1.0f)
#define GAIN_VALUE_MAX              (1.0f)

static const char *g_volume_vconf[AUDIO_VOLUME_TYPE_MAX] = {
    "file/private/sound/volume/system",         /* AUDIO_VOLUME_TYPE_SYSTEM */
    "file/private/sound/volume/notification",   /* AUDIO_VOLUME_TYPE_NOTIFICATION */
    "file/private/sound/volume/alarm",          /* AUDIO_VOLUME_TYPE_ALARM */
    "file/private/sound/volume/ringtone",       /* AUDIO_VOLUME_TYPE_RINGTONE */
    "file/private/sound/volume/media",          /* AUDIO_VOLUME_TYPE_MEDIA */
    "file/private/sound/volume/call",           /* AUDIO_VOLUME_TYPE_CALL */
    "file/private/sound/volume/voip",           /* AUDIO_VOLUME_TYPE_VOIP */
    "file/private/sound/volume/voice",          /* AUDIO_VOLUME_TYPE_VOICE */
    "file/private/sound/volume/fixed",          /* AUDIO_VOLUME_TYPE_FIXED */
};

static const char *__get_device_string_by_idx (uint32_t dev_idx)
{
    switch (dev_idx) {
    case AUDIO_VOLUME_DEVICE_DEFAULT:       return "speaker";
    default:                                return "invalid";
    }
}

static const char *__get_volume_type_string_by_idx (uint32_t vol_type_idx)
{
    switch (vol_type_idx) {
    case AUDIO_VOLUME_TYPE_SYSTEM:          return "system";
    case AUDIO_VOLUME_TYPE_NOTIFICATION:    return "notification";
    case AUDIO_VOLUME_TYPE_ALARM:           return "alarm";
    case AUDIO_VOLUME_TYPE_RINGTONE:        return "ringtone";
    case AUDIO_VOLUME_TYPE_MEDIA:           return "media";
    case AUDIO_VOLUME_TYPE_CALL:            return "call";
    case AUDIO_VOLUME_TYPE_VOIP:            return "voip";
    case AUDIO_VOLUME_TYPE_VOICE:           return "voice";
    case AUDIO_VOLUME_TYPE_FIXED:           return "fixed";
    default:                                return "invalid";
    }
}

static uint32_t __get_volume_idx_by_string_type (const char *vol_type)
{
    if (!strncmp(vol_type, "system", strlen(vol_type)) || !strncmp(vol_type, "0", strlen(vol_type)))
        return AUDIO_VOLUME_TYPE_SYSTEM;
    else if (!strncmp(vol_type, "notification", strlen(vol_type)) || !strncmp(vol_type, "1", strlen(vol_type)))
        return AUDIO_VOLUME_TYPE_NOTIFICATION;
    else if (!strncmp(vol_type, "alarm", strlen(vol_type)) || !strncmp(vol_type, "2", strlen(vol_type)))
        return AUDIO_VOLUME_TYPE_ALARM;
    else if (!strncmp(vol_type, "ringtone", strlen(vol_type)) || !strncmp(vol_type, "3", strlen(vol_type)))
        return AUDIO_VOLUME_TYPE_RINGTONE;
    else if (!strncmp(vol_type, "media", strlen(vol_type)) || !strncmp(vol_type, "4", strlen(vol_type)))
        return AUDIO_VOLUME_TYPE_MEDIA;
    else if (!strncmp(vol_type, "call", strlen(vol_type)) || !strncmp(vol_type, "5", strlen(vol_type)))
        return AUDIO_VOLUME_TYPE_CALL;
    else if (!strncmp(vol_type, "voip", strlen(vol_type)) || !strncmp(vol_type, "6", strlen(vol_type)))
        return AUDIO_VOLUME_TYPE_VOIP;
    else if (!strncmp(vol_type, "voice", strlen(vol_type)) || !strncmp(vol_type, "7", strlen(vol_type)))
        return AUDIO_VOLUME_TYPE_VOICE;
    else if (!strncmp(vol_type, "fixed", strlen(vol_type)) || !strncmp(vol_type, "8", strlen(vol_type)))
        return AUDIO_VOLUME_TYPE_FIXED;
    else
        return AUDIO_VOLUME_TYPE_MEDIA;
}

static const char *__get_gain_type_string_by_idx (uint32_t gain_type_idx)
{
    switch (gain_type_idx) {
    case AUDIO_GAIN_TYPE_DEFAULT:           return "default";
    case AUDIO_GAIN_TYPE_DIALER:            return "dialer";
    case AUDIO_GAIN_TYPE_TOUCH:             return "touch";
    case AUDIO_GAIN_TYPE_AF:                return "af";
    case AUDIO_GAIN_TYPE_SHUTTER1:          return "shutter1";
    case AUDIO_GAIN_TYPE_SHUTTER2:          return "shutter2";
    case AUDIO_GAIN_TYPE_CAMCODING:         return "camcording";
    case AUDIO_GAIN_TYPE_MIDI:              return "midi";
    case AUDIO_GAIN_TYPE_BOOTING:           return "booting";
    case AUDIO_GAIN_TYPE_VIDEO:             return "video";
    case AUDIO_GAIN_TYPE_TTS:               return "tts";
    default:                                return "invalid";
    }
}

static void __dump_tb (audio_mgr_t *am)
{
    audio_volume_value_table_t *volume_value_table = am->volume.volume_value_table;
    uint32_t dev_idx, vol_type_idx, vol_level_idx, gain_type_idx;
    const char *gain_type_str[] = {
        "def",          /* AUDIO_GAIN_TYPE_DEFAULT */
        "dial",         /* AUDIO_GAIN_TYPE_DIALER */
        "touch",        /* AUDIO_GAIN_TYPE_TOUCH */
        "af",           /* AUDIO_GAIN_TYPE_AF */
        "shut1",        /* AUDIO_GAIN_TYPE_SHUTTER1 */
        "shut2",        /* AUDIO_GAIN_TYPE_SHUTTER2 */
        "cam",          /* AUDIO_GAIN_TYPE_CAMCODING */
        "midi",         /* AUDIO_GAIN_TYPE_MIDI */
        "boot",         /* AUDIO_GAIN_TYPE_BOOTING */
        "video",        /* AUDIO_GAIN_TYPE_VIDEO */
        "tts",          /* AUDIO_GAIN_TYPE_TTS */
    };
    char dump_str[AUDIO_DUMP_STR_LEN], *dump_str_ptr;

    /* Dump volume table */
    AUDIO_LOG_INFO("<<<<< volume table >>>>>");

    const char *table_str = "volumes";

    AUDIO_LOG_INFO("<< %s >>", table_str);

    for (vol_type_idx = 0; vol_type_idx < AUDIO_VOLUME_TYPE_MAX; vol_type_idx++) {
        const char *vol_type_str = __get_volume_type_string_by_idx(vol_type_idx);

        dump_str_ptr = &dump_str[0];
        memset(dump_str, 0x00, sizeof(char) * sizeof(dump_str));
        snprintf(dump_str_ptr, 8, "%6s:", vol_type_str);
        dump_str_ptr += strlen(dump_str_ptr);

        for (vol_level_idx = 0; vol_level_idx < volume_value_table->volume_level_max[vol_type_idx]; vol_level_idx++) {
            snprintf(dump_str_ptr, 6, "%01.2f ", volume_value_table->volume[vol_type_idx][vol_level_idx]);
            dump_str_ptr += strlen(dump_str_ptr);
        }
        AUDIO_LOG_INFO("%s", dump_str);
    }

    volume_value_table = am->volume.volume_value_table;

    /* Dump gain table */
    AUDIO_LOG_INFO("<<<<< gain table >>>>>");

    dump_str_ptr = &dump_str[0];
    memset(dump_str, 0x00, sizeof(char) * sizeof(dump_str));

    snprintf(dump_str_ptr, 11, "%10s", " ");
    dump_str_ptr += strlen(dump_str_ptr);

    for (gain_type_idx = 0; gain_type_idx < AUDIO_GAIN_TYPE_MAX; gain_type_idx++) {
        snprintf(dump_str_ptr, 7, "%5s ", gain_type_str[gain_type_idx]);
        dump_str_ptr += strlen(dump_str_ptr);
    }
    AUDIO_LOG_INFO("%s", dump_str);

    dump_str_ptr = &dump_str[0];
    memset(dump_str, 0x00, sizeof(char) * sizeof(dump_str));

    snprintf(dump_str_ptr, 11, "%9s:", table_str);
    dump_str_ptr += strlen(dump_str_ptr);

    for (gain_type_idx = 0; gain_type_idx < AUDIO_GAIN_TYPE_MAX; gain_type_idx++) {
        snprintf(dump_str_ptr, 7, "%01.3f ", volume_value_table->gain[gain_type_idx]);
        dump_str_ptr += strlen(dump_str_ptr);
    }
    AUDIO_LOG_INFO("%s", dump_str);

}

static audio_return_t __load_volume_value_table_from_ini (audio_mgr_t *am)
{
    dictionary * dict = NULL;
    uint32_t vol_type_idx, vol_level_idx, gain_type_idx;
    audio_volume_value_table_t *volume_value_table = am->volume.volume_value_table;
    int size = 0;

    dict = iniparser_load(VOLUME_INI_TEMP_PATH);
    if (!dict) {
        AUDIO_LOG_DEBUG("Use default volume&gain ini file");
        dict = iniparser_load(VOLUME_INI_DEFAULT_PATH);
        if (!dict) {
            AUDIO_LOG_WARN("Loading volume&gain table from ini file failed");
            return AUDIO_ERR_UNDEFINED;
        }
    }

    const char delimiter[] = ", ";
    char *key, *list_str, *token, *ptr = NULL;
    const char *table_str = "volumes";

    /* Load volume table */
    for (vol_type_idx = 0; vol_type_idx < AUDIO_VOLUME_TYPE_MAX; vol_type_idx++) {
        const char *vol_type_str = __get_volume_type_string_by_idx(vol_type_idx);

        volume_value_table->volume_level_max[vol_type_idx] = 0;
        size = strlen(table_str) + strlen(vol_type_str) + 2;
        key = malloc(size);
        if (key) {
            snprintf(key, size, "%s:%s", table_str, vol_type_str);
            list_str = iniparser_getstring(dict, key, NULL);
            if (list_str) {
                token = strtok_r(list_str, delimiter, &ptr);
                while (token) {
                    /* convert dB volume to linear volume */
                    double vol_value = 0.0f;
                    if(strncmp(token, "0", strlen(token)))
                        vol_value = pow(10.0, (atof(token) - 100) / 20.0);
                    volume_value_table->volume[vol_type_idx][volume_value_table->volume_level_max[vol_type_idx]++] = vol_value;
                    token = strtok_r(NULL, delimiter, &ptr);
                }
            } else {
                volume_value_table->volume_level_max[vol_type_idx] = 1;
                for (vol_level_idx = 0; vol_level_idx < AUDIO_VOLUME_LEVEL_MAX; vol_level_idx++) {
                    volume_value_table->volume[vol_type_idx][vol_level_idx] = VOLUME_VALUE_MAX;
                }
            }
            free(key);
        }
    }

    /* Load gain table */
    volume_value_table->gain[AUDIO_GAIN_TYPE_DEFAULT] = GAIN_VALUE_MAX;
    for (gain_type_idx = AUDIO_GAIN_TYPE_DEFAULT + 1; gain_type_idx < AUDIO_GAIN_TYPE_MAX; gain_type_idx++) {
        const char *gain_type_str = __get_gain_type_string_by_idx(gain_type_idx);

        size = strlen(table_str) + strlen("gain") + strlen(gain_type_str) + 3;
        key = malloc(size);
        if (key) {
            snprintf(key, size, "%s:gain_%s", table_str, gain_type_str);
            token = iniparser_getstring(dict, key, NULL);
            if (token) {
                volume_value_table->gain[gain_type_idx] = atof(token);
            } else {
                volume_value_table->gain[gain_type_idx] = GAIN_VALUE_MAX;
            }
            free(key);
        } else {
            volume_value_table->gain[gain_type_idx] = GAIN_VALUE_MAX;
        }
    }

    iniparser_freedict(dict);

    __dump_tb(am);

    return AUDIO_RET_OK;
}

audio_return_t _audio_volume_init (audio_mgr_t *am)
{
    int i;
    int val = 0;
    audio_return_t audio_ret = AUDIO_RET_OK;
    int init_value[AUDIO_VOLUME_TYPE_MAX] = { 9, 11, 7, 11, 7, 4, 4, 7, 4, 0 };

    AUDIO_RETURN_VAL_IF_FAIL(am, AUDIO_ERR_PARAMETER);

    for (i = 0; i < AUDIO_VOLUME_TYPE_MAX; i++) {
        am->volume.volume_level[i] = init_value[i];
    }

    for (i = 0; i < AUDIO_VOLUME_TYPE_MAX; i++) {
        /* Get volume value string from VCONF */
        if(vconf_get_int(g_volume_vconf[i], &val) < 0) {
            AUDIO_LOG_ERROR("vconf_get_int(%s) failed", g_volume_vconf[i]);
            continue;
        }

        AUDIO_LOG_INFO("read vconf. %s = %d", g_volume_vconf[i], val);
        am->volume.volume_level[i] = val;
    }

    if (!(am->volume.volume_value_table = malloc(AUDIO_VOLUME_DEVICE_MAX * sizeof(audio_volume_value_table_t)))) {
        AUDIO_LOG_ERROR("volume_value_table malloc failed");
        return AUDIO_ERR_RESOURCE;
    }

    audio_ret = __load_volume_value_table_from_ini(am);
    if(audio_ret != AUDIO_RET_OK) {
        AUDIO_LOG_ERROR("gain table load error");
        return AUDIO_ERR_UNDEFINED;
    }

    return audio_ret;
}

audio_return_t _audio_volume_deinit (audio_mgr_t *am)
{
    AUDIO_RETURN_VAL_IF_FAIL(am, AUDIO_ERR_PARAMETER);

    if (am->volume.volume_value_table) {
        free(am->volume.volume_value_table);
        am->volume.volume_value_table = NULL;
    }

    return AUDIO_RET_OK;
}

audio_return_t audio_get_volume_level_max (void *userdata, audio_volume_info_t *info, uint32_t *level)
{
    audio_mgr_t *am = (audio_mgr_t *)userdata;
    audio_volume_value_table_t *volume_value_table;

    AUDIO_RETURN_VAL_IF_FAIL(am, AUDIO_ERR_PARAMETER);
    AUDIO_RETURN_VAL_IF_FAIL(am->volume.volume_value_table, AUDIO_ERR_PARAMETER);

    /* Get max volume level by device & type */
    volume_value_table = am->volume.volume_value_table;
    *level = volume_value_table->volume_level_max[__get_volume_idx_by_string_type(info->type)];

    AUDIO_LOG_DEBUG("get_[%s] volume_level_max: %d", info->type, *level);

    return AUDIO_RET_OK;
}

audio_return_t audio_get_volume_level (void *userdata, audio_volume_info_t *info, uint32_t *level)
{
    audio_mgr_t *am = (audio_mgr_t *)userdata;

    AUDIO_RETURN_VAL_IF_FAIL(am, AUDIO_ERR_PARAMETER);

    *level = am->volume.volume_level[__get_volume_idx_by_string_type(info->type)];

    AUDIO_LOG_INFO("get [%s] volume_level: %d, direction(%d)", info->type, *level, info->direction);

    return AUDIO_RET_OK;
}

audio_return_t audio_get_volume_value (void *userdata, audio_volume_info_t *info, uint32_t level, double *value)
{
    audio_mgr_t *am = (audio_mgr_t *)userdata;
    audio_volume_value_table_t *volume_value_table;
    char dump_str[AUDIO_DUMP_STR_LEN] = {0,};

    AUDIO_RETURN_VAL_IF_FAIL(am, AUDIO_ERR_PARAMETER);
    AUDIO_RETURN_VAL_IF_FAIL(am->volume.volume_value_table, AUDIO_ERR_PARAMETER);

    /* Get basic volume by device & type & level */
    volume_value_table = am->volume.volume_value_table;
    if (volume_value_table->volume_level_max[__get_volume_idx_by_string_type(info->type)] < level)
        *value = VOLUME_VALUE_MAX;
    else
        *value = volume_value_table->volume[__get_volume_idx_by_string_type(info->type)][level];
    *value *= volume_value_table->gain[AUDIO_GAIN_TYPE_DEFAULT]; /* need to fix getting gain via audio_info_t */

    AUDIO_LOG_DEBUG("get_volume_value:%d(%s)=>%f %s", level, info->type, *value, &dump_str[0]);

    return AUDIO_RET_OK;
}

audio_return_t audio_set_volume_level (void *userdata, audio_volume_info_t *info, uint32_t level)
{
    audio_return_t audio_ret = AUDIO_RET_OK;
    audio_mgr_t *am = (audio_mgr_t *)userdata;

    AUDIO_RETURN_VAL_IF_FAIL(am, AUDIO_ERR_PARAMETER);

    /* Update volume level */
    am->volume.volume_level[__get_volume_idx_by_string_type(info->type)] = level;
    AUDIO_LOG_INFO("set [%s] volume_level: %d, direction(%d)", info->type, level, info->direction);

    /* set mixer related to H/W volume if needed */

    return audio_ret;
}

audio_return_t audio_get_volume_mute (void *userdata, audio_volume_info_t *info, uint32_t *mute)
{
    audio_return_t audio_ret = AUDIO_RET_OK;
    audio_mgr_t *am = (audio_mgr_t *)userdata;

    AUDIO_RETURN_VAL_IF_FAIL(am, AUDIO_ERR_PARAMETER);

    /* TODO. Not implemented */

    return audio_ret;
}

audio_return_t audio_set_volume_mute (void *userdata, audio_volume_info_t *info, uint32_t mute)
{
    audio_return_t audio_ret = AUDIO_RET_OK;
    audio_mgr_t *am = (audio_mgr_t *)userdata;

    AUDIO_RETURN_VAL_IF_FAIL(am, AUDIO_ERR_PARAMETER);
    /* TODO. Not implemented */

    return audio_ret;
}