blob: 54c5c76bf1cbf5f5c27d15aaa5b0c95dfd8ee3e2 [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
François Gaffie1c878552018-11-22 16:53:21 +010036} // namespace android
37
Eric Laurentffbc80f2015-03-18 18:30:19 -070038// Absolute min volume in dB (can be represented in single precision normal float value)
39#define VOLUME_MIN_DB (-758)
François Gaffiedfd74092015-03-19 12:10:59 +010040
41class VolumeCurvePoint
42{
43public:
44 int mIndex;
45 float mDBAttenuation;
46};
47
François Gaffied0609ad2015-12-01 17:56:08 +010048/**
49 * device categories used for volume curve management.
50 */
51enum device_category {
52 DEVICE_CATEGORY_HEADSET,
53 DEVICE_CATEGORY_SPEAKER,
54 DEVICE_CATEGORY_EARPIECE,
55 DEVICE_CATEGORY_EXT_MEDIA,
Jakub Pawlowski24b0a892018-04-02 19:17:11 -070056 DEVICE_CATEGORY_HEARING_AID,
François Gaffied0609ad2015-12-01 17:56:08 +010057 DEVICE_CATEGORY_CNT
58};
59
François Gaffiedfd74092015-03-19 12:10:59 +010060class Volume
61{
62public:
63 /**
64 * 4 points to define the volume attenuation curve, each characterized by the volume
65 * index (from 0 to 100) at which they apply, and the attenuation in dB at that index.
Eric Laurentffbc80f2015-03-18 18:30:19 -070066 * we use 100 steps to avoid rounding errors when computing the volume in volIndexToDb()
François Gaffiedfd74092015-03-19 12:10:59 +010067 *
68 * @todo shall become configurable
69 */
70 enum {
71 VOLMIN = 0,
72 VOLKNEE1 = 1,
73 VOLKNEE2 = 2,
74 VOLMAX = 3,
75
76 VOLCNT = 4
77 };
78
79 /**
François Gaffiedfd74092015-03-19 12:10:59 +010080 * extract one device relevant for volume control from multiple device selection
81 *
82 * @param[in] device for which the volume category is associated
83 *
84 * @return subset of device required to limit the number of volume category per device
85 */
86 static audio_devices_t getDeviceForVolume(audio_devices_t device)
87 {
88 if (device == AUDIO_DEVICE_NONE) {
89 // this happens when forcing a route update and no track is active on an output.
90 // In this case the returned category is not important.
91 device = AUDIO_DEVICE_OUT_SPEAKER;
92 } else if (popcount(device) > 1) {
93 // Multiple device selection is either:
94 // - speaker + one other device: give priority to speaker in this case.
95 // - one A2DP device + another device: happens with duplicated output. In this case
96 // retain the device on the A2DP output as the other must not correspond to an active
97 // selection if not the speaker.
98 // - HDMI-CEC system audio mode only output: give priority to available item in order.
99 if (device & AUDIO_DEVICE_OUT_SPEAKER) {
100 device = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurent9a7d9222015-07-02 15:30:23 -0700101 } else if (device & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
102 device = AUDIO_DEVICE_OUT_SPEAKER_SAFE;
François Gaffiedfd74092015-03-19 12:10:59 +0100103 } else if (device & AUDIO_DEVICE_OUT_HDMI_ARC) {
104 device = AUDIO_DEVICE_OUT_HDMI_ARC;
105 } else if (device & AUDIO_DEVICE_OUT_AUX_LINE) {
106 device = AUDIO_DEVICE_OUT_AUX_LINE;
107 } else if (device & AUDIO_DEVICE_OUT_SPDIF) {
108 device = AUDIO_DEVICE_OUT_SPDIF;
109 } else {
110 device = (audio_devices_t)(device & AUDIO_DEVICE_OUT_ALL_A2DP);
111 }
112 }
113
114 /*SPEAKER_SAFE is an alias of SPEAKER for purposes of volume control*/
115 if (device == AUDIO_DEVICE_OUT_SPEAKER_SAFE)
116 device = AUDIO_DEVICE_OUT_SPEAKER;
117
118 ALOGW_IF(popcount(device) != 1,
119 "getDeviceForVolume() invalid device combination: %08x",
120 device);
121
122 return device;
123 }
124
125 /**
126 * returns the category the device belongs to with regard to volume curve management
127 *
128 * @param[in] device to check upon the category to whom it belongs to.
129 *
130 * @return device category.
131 */
132 static device_category getDeviceCategory(audio_devices_t device)
133 {
134 switch(getDeviceForVolume(device)) {
135 case AUDIO_DEVICE_OUT_EARPIECE:
136 return DEVICE_CATEGORY_EARPIECE;
137 case AUDIO_DEVICE_OUT_WIRED_HEADSET:
138 case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
139 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
140 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
141 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
142 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
Eric Laurent904d6322017-03-17 17:20:47 -0700143 case AUDIO_DEVICE_OUT_USB_HEADSET:
François Gaffiedfd74092015-03-19 12:10:59 +0100144 return DEVICE_CATEGORY_HEADSET;
Jakub Pawlowski24b0a892018-04-02 19:17:11 -0700145 case AUDIO_DEVICE_OUT_HEARING_AID:
146 return DEVICE_CATEGORY_HEARING_AID;
François Gaffiedfd74092015-03-19 12:10:59 +0100147 case AUDIO_DEVICE_OUT_LINE:
148 case AUDIO_DEVICE_OUT_AUX_DIGITAL:
Eric Laurent904d6322017-03-17 17:20:47 -0700149 case AUDIO_DEVICE_OUT_USB_DEVICE:
François Gaffiedfd74092015-03-19 12:10:59 +0100150 return DEVICE_CATEGORY_EXT_MEDIA;
151 case AUDIO_DEVICE_OUT_SPEAKER:
Jean-Michel Trivi227342a2018-09-14 11:42:37 -0700152 case AUDIO_DEVICE_OUT_SPEAKER_SAFE:
François Gaffiedfd74092015-03-19 12:10:59 +0100153 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
154 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
155 case AUDIO_DEVICE_OUT_USB_ACCESSORY:
François Gaffiedfd74092015-03-19 12:10:59 +0100156 case AUDIO_DEVICE_OUT_REMOTE_SUBMIX:
157 default:
158 return DEVICE_CATEGORY_SPEAKER;
159 }
160 }
161
Eric Laurentffbc80f2015-03-18 18:30:19 -0700162 static inline float DbToAmpl(float decibels)
163 {
164 if (decibels <= VOLUME_MIN_DB) {
165 return 0.0f;
166 }
167 return exp( decibels * 0.115129f); // exp( dB * ln(10) / 20 )
168 }
169
170 static inline float AmplToDb(float amplification)
171 {
172 if (amplification == 0) {
173 return VOLUME_MIN_DB;
174 }
175 return 20 * log10(amplification);
176 }
177
François Gaffiedfd74092015-03-19 12:10:59 +0100178};