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