1/*
2 * Copyright (C) 2015 The Android Open Source Project
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#define LOG_TAG "APM::AudioGain"
18//#define LOG_NDEBUG 0
19
20//#define VERY_VERBOSE_LOGGING
21#ifdef VERY_VERBOSE_LOGGING
22#define ALOGVV ALOGV
23#else
24#define ALOGVV(a...) do { } while(0)
25#endif
26
27#include "AudioGain.h"
28#include <utils/Log.h>
29#include <utils/String8.h>
30
31#include <math.h>
32
33namespace android {
34
35AudioGain::AudioGain(int index, bool useInChannelMask)
36{
37    mIndex = index;
38    mUseInChannelMask = useInChannelMask;
39    memset(&mGain, 0, sizeof(struct audio_gain));
40}
41
42void AudioGain::getDefaultConfig(struct audio_gain_config *config)
43{
44    config->index = mIndex;
45    config->mode = mGain.mode;
46    config->channel_mask = mGain.channel_mask;
47    if ((mGain.mode & AUDIO_GAIN_MODE_JOINT) == AUDIO_GAIN_MODE_JOINT) {
48        config->values[0] = mGain.default_value;
49    } else {
50        uint32_t numValues;
51        if (mUseInChannelMask) {
52            numValues = audio_channel_count_from_in_mask(mGain.channel_mask);
53        } else {
54            numValues = audio_channel_count_from_out_mask(mGain.channel_mask);
55        }
56        for (size_t i = 0; i < numValues; i++) {
57            config->values[i] = mGain.default_value;
58        }
59    }
60    if ((mGain.mode & AUDIO_GAIN_MODE_RAMP) == AUDIO_GAIN_MODE_RAMP) {
61        config->ramp_duration_ms = mGain.min_ramp_ms;
62    }
63}
64
65status_t AudioGain::checkConfig(const struct audio_gain_config *config)
66{
67    if ((config->mode & ~mGain.mode) != 0) {
68        return BAD_VALUE;
69    }
70    if ((config->mode & AUDIO_GAIN_MODE_JOINT) == AUDIO_GAIN_MODE_JOINT) {
71        if ((config->values[0] < mGain.min_value) ||
72                    (config->values[0] > mGain.max_value)) {
73            return BAD_VALUE;
74        }
75    } else {
76        if ((config->channel_mask & ~mGain.channel_mask) != 0) {
77            return BAD_VALUE;
78        }
79        uint32_t numValues;
80        if (mUseInChannelMask) {
81            numValues = audio_channel_count_from_in_mask(config->channel_mask);
82        } else {
83            numValues = audio_channel_count_from_out_mask(config->channel_mask);
84        }
85        for (size_t i = 0; i < numValues; i++) {
86            if ((config->values[i] < mGain.min_value) ||
87                    (config->values[i] > mGain.max_value)) {
88                return BAD_VALUE;
89            }
90        }
91    }
92    if ((config->mode & AUDIO_GAIN_MODE_RAMP) == AUDIO_GAIN_MODE_RAMP) {
93        if ((config->ramp_duration_ms < mGain.min_ramp_ms) ||
94                    (config->ramp_duration_ms > mGain.max_ramp_ms)) {
95            return BAD_VALUE;
96        }
97    }
98    return NO_ERROR;
99}
100
101void AudioGain::dump(int fd, int spaces, int index) const
102{
103    const size_t SIZE = 256;
104    char buffer[SIZE];
105    String8 result;
106
107    snprintf(buffer, SIZE, "%*sGain %d:\n", spaces, "", index+1);
108    result.append(buffer);
109    snprintf(buffer, SIZE, "%*s- mode: %08x\n", spaces, "", mGain.mode);
110    result.append(buffer);
111    snprintf(buffer, SIZE, "%*s- channel_mask: %08x\n", spaces, "", mGain.channel_mask);
112    result.append(buffer);
113    snprintf(buffer, SIZE, "%*s- min_value: %d mB\n", spaces, "", mGain.min_value);
114    result.append(buffer);
115    snprintf(buffer, SIZE, "%*s- max_value: %d mB\n", spaces, "", mGain.max_value);
116    result.append(buffer);
117    snprintf(buffer, SIZE, "%*s- default_value: %d mB\n", spaces, "", mGain.default_value);
118    result.append(buffer);
119    snprintf(buffer, SIZE, "%*s- step_value: %d mB\n", spaces, "", mGain.step_value);
120    result.append(buffer);
121    snprintf(buffer, SIZE, "%*s- min_ramp_ms: %d ms\n", spaces, "", mGain.min_ramp_ms);
122    result.append(buffer);
123    snprintf(buffer, SIZE, "%*s- max_ramp_ms: %d ms\n", spaces, "", mGain.max_ramp_ms);
124    result.append(buffer);
125
126    write(fd, result.string(), result.size());
127}
128
129}; // namespace android
130