blob: 561f1000925c1e7aeea1844b8af98a144a13068a [file] [log] [blame]
François Gaffiedfd74092015-03-19 12:10:59 +01001/*
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#pragma once
18
François Gaffieaaac0fd2018-11-22 17:56:39 +010019#include <media/AudioCommonTypes.h>
François Gaffiedfd74092015-03-19 12:10:59 +010020#include <system/audio.h>
21#include <utils/Log.h>
Eric Laurentffbc80f2015-03-18 18:30:19 -070022#include <math.h>
23
François Gaffie1c878552018-11-22 16:53:21 +010024namespace android {
François Gaffie251c7f02018-11-07 10:41:08 +010025
François Gaffieaaac0fd2018-11-22 17:56:39 +010026
François Gaffie1c878552018-11-22 16:53:21 +010027/**
28 * VolumeSource is the discriminent for volume management on an output.
29 * It used to be the stream type by legacy, it may be host volume group or a volume curves if
François Gaffieaaac0fd2018-11-22 17:56:39 +010030 * we allow to have more than one curve per volume group (mandatory to get rid of AudioServer
31 * stream aliases.
François Gaffie1c878552018-11-22 16:53:21 +010032 */
François Gaffieaaac0fd2018-11-22 17:56:39 +010033enum VolumeSource : std::underlying_type<volume_group_t>::type;
34static const VolumeSource VOLUME_SOURCE_NONE = static_cast<VolumeSource>(VOLUME_GROUP_NONE);
François Gaffie1c878552018-11-22 16:53:21 +010035
36static inline VolumeSource streamToVolumeSource(audio_stream_type_t stream) {
37 return static_cast<VolumeSource>(stream);
38}
39
40
41} // namespace android
42
Eric Laurentffbc80f2015-03-18 18:30:19 -070043// Absolute min volume in dB (can be represented in single precision normal float value)
44#define VOLUME_MIN_DB (-758)
François Gaffiedfd74092015-03-19 12:10:59 +010045
46class VolumeCurvePoint
47{
48public:
49 int mIndex;
50 float mDBAttenuation;
51};
52
François Gaffied0609ad2015-12-01 17:56:08 +010053/**
54 * device categories used for volume curve management.
55 */
56enum device_category {
57 DEVICE_CATEGORY_HEADSET,
58 DEVICE_CATEGORY_SPEAKER,
59 DEVICE_CATEGORY_EARPIECE,
60 DEVICE_CATEGORY_EXT_MEDIA,
Jakub Pawlowski24b0a892018-04-02 19:17:11 -070061 DEVICE_CATEGORY_HEARING_AID,
François Gaffied0609ad2015-12-01 17:56:08 +010062 DEVICE_CATEGORY_CNT
63};
64
François Gaffiedfd74092015-03-19 12:10:59 +010065class Volume
66{
67public:
68 /**
69 * 4 points to define the volume attenuation curve, each characterized by the volume
70 * index (from 0 to 100) at which they apply, and the attenuation in dB at that index.
Eric Laurentffbc80f2015-03-18 18:30:19 -070071 * we use 100 steps to avoid rounding errors when computing the volume in volIndexToDb()
François Gaffiedfd74092015-03-19 12:10:59 +010072 *
73 * @todo shall become configurable
74 */
75 enum {
76 VOLMIN = 0,
77 VOLKNEE1 = 1,
78 VOLKNEE2 = 2,
79 VOLMAX = 3,
80
81 VOLCNT = 4
82 };
83
84 /**
François Gaffiedfd74092015-03-19 12:10:59 +010085 * extract one device relevant for volume control from multiple device selection
86 *
87 * @param[in] device for which the volume category is associated
88 *
89 * @return subset of device required to limit the number of volume category per device
90 */
91 static audio_devices_t getDeviceForVolume(audio_devices_t device)
92 {
93 if (device == AUDIO_DEVICE_NONE) {
94 // this happens when forcing a route update and no track is active on an output.
95 // In this case the returned category is not important.
96 device = AUDIO_DEVICE_OUT_SPEAKER;
97 } else if (popcount(device) > 1) {
98 // Multiple device selection is either:
99 // - speaker + one other device: give priority to speaker in this case.
100 // - one A2DP device + another device: happens with duplicated output. In this case
101 // retain the device on the A2DP output as the other must not correspond to an active
102 // selection if not the speaker.
103 // - HDMI-CEC system audio mode only output: give priority to available item in order.
104 if (device & AUDIO_DEVICE_OUT_SPEAKER) {
105 device = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurent9a7d9222015-07-02 15:30:23 -0700106 } else if (device & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
107 device = AUDIO_DEVICE_OUT_SPEAKER_SAFE;
François Gaffiedfd74092015-03-19 12:10:59 +0100108 } else if (device & AUDIO_DEVICE_OUT_HDMI_ARC) {
109 device = AUDIO_DEVICE_OUT_HDMI_ARC;
110 } else if (device & AUDIO_DEVICE_OUT_AUX_LINE) {
111 device = AUDIO_DEVICE_OUT_AUX_LINE;
112 } else if (device & AUDIO_DEVICE_OUT_SPDIF) {
113 device = AUDIO_DEVICE_OUT_SPDIF;
114 } else {
115 device = (audio_devices_t)(device & AUDIO_DEVICE_OUT_ALL_A2DP);
116 }
117 }
118
119 /*SPEAKER_SAFE is an alias of SPEAKER for purposes of volume control*/
120 if (device == AUDIO_DEVICE_OUT_SPEAKER_SAFE)
121 device = AUDIO_DEVICE_OUT_SPEAKER;
122
123 ALOGW_IF(popcount(device) != 1,
124 "getDeviceForVolume() invalid device combination: %08x",
125 device);
126
127 return device;
128 }
129
130 /**
131 * returns the category the device belongs to with regard to volume curve management
132 *
133 * @param[in] device to check upon the category to whom it belongs to.
134 *
135 * @return device category.
136 */
137 static device_category getDeviceCategory(audio_devices_t device)
138 {
139 switch(getDeviceForVolume(device)) {
140 case AUDIO_DEVICE_OUT_EARPIECE:
141 return DEVICE_CATEGORY_EARPIECE;
142 case AUDIO_DEVICE_OUT_WIRED_HEADSET:
143 case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
144 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
145 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
146 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
147 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
Eric Laurent904d6322017-03-17 17:20:47 -0700148 case AUDIO_DEVICE_OUT_USB_HEADSET:
François Gaffiedfd74092015-03-19 12:10:59 +0100149 return DEVICE_CATEGORY_HEADSET;
Jakub Pawlowski24b0a892018-04-02 19:17:11 -0700150 case AUDIO_DEVICE_OUT_HEARING_AID:
151 return DEVICE_CATEGORY_HEARING_AID;
François Gaffiedfd74092015-03-19 12:10:59 +0100152 case AUDIO_DEVICE_OUT_LINE:
153 case AUDIO_DEVICE_OUT_AUX_DIGITAL:
Eric Laurent904d6322017-03-17 17:20:47 -0700154 case AUDIO_DEVICE_OUT_USB_DEVICE:
François Gaffiedfd74092015-03-19 12:10:59 +0100155 return DEVICE_CATEGORY_EXT_MEDIA;
156 case AUDIO_DEVICE_OUT_SPEAKER:
Jean-Michel Trivi227342a2018-09-14 11:42:37 -0700157 case AUDIO_DEVICE_OUT_SPEAKER_SAFE:
François Gaffiedfd74092015-03-19 12:10:59 +0100158 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
159 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
160 case AUDIO_DEVICE_OUT_USB_ACCESSORY:
François Gaffiedfd74092015-03-19 12:10:59 +0100161 case AUDIO_DEVICE_OUT_REMOTE_SUBMIX:
162 default:
163 return DEVICE_CATEGORY_SPEAKER;
164 }
165 }
166
Eric Laurentffbc80f2015-03-18 18:30:19 -0700167 static inline float DbToAmpl(float decibels)
168 {
169 if (decibels <= VOLUME_MIN_DB) {
170 return 0.0f;
171 }
172 return exp( decibels * 0.115129f); // exp( dB * ln(10) / 20 )
173 }
174
175 static inline float AmplToDb(float amplification)
176 {
177 if (amplification == 0) {
178 return VOLUME_MIN_DB;
179 }
180 return 20 * log10(amplification);
181 }
182
François Gaffiedfd74092015-03-19 12:10:59 +0100183};