blob: 5ccc8fdf2e1fc356c0381b512b5be4d061cefc98 [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
19#include <system/audio.h>
20#include <utils/Log.h>
Eric Laurentffbc80f2015-03-18 18:30:19 -070021#include <math.h>
22
23// Absolute min volume in dB (can be represented in single precision normal float value)
24#define VOLUME_MIN_DB (-758)
François Gaffiedfd74092015-03-19 12:10:59 +010025
26class VolumeCurvePoint
27{
28public:
29 int mIndex;
30 float mDBAttenuation;
31};
32
François Gaffied0609ad2015-12-01 17:56:08 +010033/**
34 * device categories used for volume curve management.
35 */
36enum device_category {
37 DEVICE_CATEGORY_HEADSET,
38 DEVICE_CATEGORY_SPEAKER,
39 DEVICE_CATEGORY_EARPIECE,
40 DEVICE_CATEGORY_EXT_MEDIA,
Jakub Pawlowski24b0a892018-04-02 19:17:11 -070041 DEVICE_CATEGORY_HEARING_AID,
François Gaffied0609ad2015-12-01 17:56:08 +010042 DEVICE_CATEGORY_CNT
43};
44
François Gaffiedfd74092015-03-19 12:10:59 +010045class Volume
46{
47public:
48 /**
49 * 4 points to define the volume attenuation curve, each characterized by the volume
50 * index (from 0 to 100) at which they apply, and the attenuation in dB at that index.
Eric Laurentffbc80f2015-03-18 18:30:19 -070051 * we use 100 steps to avoid rounding errors when computing the volume in volIndexToDb()
François Gaffiedfd74092015-03-19 12:10:59 +010052 *
53 * @todo shall become configurable
54 */
55 enum {
56 VOLMIN = 0,
57 VOLKNEE1 = 1,
58 VOLKNEE2 = 2,
59 VOLMAX = 3,
60
61 VOLCNT = 4
62 };
63
64 /**
François Gaffiedfd74092015-03-19 12:10:59 +010065 * extract one device relevant for volume control from multiple device selection
66 *
67 * @param[in] device for which the volume category is associated
68 *
69 * @return subset of device required to limit the number of volume category per device
70 */
71 static audio_devices_t getDeviceForVolume(audio_devices_t device)
72 {
73 if (device == AUDIO_DEVICE_NONE) {
74 // this happens when forcing a route update and no track is active on an output.
75 // In this case the returned category is not important.
76 device = AUDIO_DEVICE_OUT_SPEAKER;
77 } else if (popcount(device) > 1) {
78 // Multiple device selection is either:
79 // - speaker + one other device: give priority to speaker in this case.
80 // - one A2DP device + another device: happens with duplicated output. In this case
81 // retain the device on the A2DP output as the other must not correspond to an active
82 // selection if not the speaker.
83 // - HDMI-CEC system audio mode only output: give priority to available item in order.
84 if (device & AUDIO_DEVICE_OUT_SPEAKER) {
85 device = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurent9a7d9222015-07-02 15:30:23 -070086 } else if (device & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
87 device = AUDIO_DEVICE_OUT_SPEAKER_SAFE;
François Gaffiedfd74092015-03-19 12:10:59 +010088 } else if (device & AUDIO_DEVICE_OUT_HDMI_ARC) {
89 device = AUDIO_DEVICE_OUT_HDMI_ARC;
90 } else if (device & AUDIO_DEVICE_OUT_AUX_LINE) {
91 device = AUDIO_DEVICE_OUT_AUX_LINE;
92 } else if (device & AUDIO_DEVICE_OUT_SPDIF) {
93 device = AUDIO_DEVICE_OUT_SPDIF;
94 } else {
95 device = (audio_devices_t)(device & AUDIO_DEVICE_OUT_ALL_A2DP);
96 }
97 }
98
99 /*SPEAKER_SAFE is an alias of SPEAKER for purposes of volume control*/
100 if (device == AUDIO_DEVICE_OUT_SPEAKER_SAFE)
101 device = AUDIO_DEVICE_OUT_SPEAKER;
102
103 ALOGW_IF(popcount(device) != 1,
104 "getDeviceForVolume() invalid device combination: %08x",
105 device);
106
107 return device;
108 }
109
110 /**
111 * returns the category the device belongs to with regard to volume curve management
112 *
113 * @param[in] device to check upon the category to whom it belongs to.
114 *
115 * @return device category.
116 */
117 static device_category getDeviceCategory(audio_devices_t device)
118 {
119 switch(getDeviceForVolume(device)) {
120 case AUDIO_DEVICE_OUT_EARPIECE:
121 return DEVICE_CATEGORY_EARPIECE;
122 case AUDIO_DEVICE_OUT_WIRED_HEADSET:
123 case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
124 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
125 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
126 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
127 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
Eric Laurent904d6322017-03-17 17:20:47 -0700128 case AUDIO_DEVICE_OUT_USB_HEADSET:
François Gaffiedfd74092015-03-19 12:10:59 +0100129 return DEVICE_CATEGORY_HEADSET;
Jakub Pawlowski24b0a892018-04-02 19:17:11 -0700130 case AUDIO_DEVICE_OUT_HEARING_AID:
131 return DEVICE_CATEGORY_HEARING_AID;
François Gaffiedfd74092015-03-19 12:10:59 +0100132 case AUDIO_DEVICE_OUT_LINE:
133 case AUDIO_DEVICE_OUT_AUX_DIGITAL:
Eric Laurent904d6322017-03-17 17:20:47 -0700134 case AUDIO_DEVICE_OUT_USB_DEVICE:
François Gaffiedfd74092015-03-19 12:10:59 +0100135 return DEVICE_CATEGORY_EXT_MEDIA;
136 case AUDIO_DEVICE_OUT_SPEAKER:
Jean-Michel Trivi227342a2018-09-14 11:42:37 -0700137 case AUDIO_DEVICE_OUT_SPEAKER_SAFE:
François Gaffiedfd74092015-03-19 12:10:59 +0100138 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
139 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
140 case AUDIO_DEVICE_OUT_USB_ACCESSORY:
François Gaffiedfd74092015-03-19 12:10:59 +0100141 case AUDIO_DEVICE_OUT_REMOTE_SUBMIX:
142 default:
143 return DEVICE_CATEGORY_SPEAKER;
144 }
145 }
146
Eric Laurentffbc80f2015-03-18 18:30:19 -0700147 static inline float DbToAmpl(float decibels)
148 {
149 if (decibels <= VOLUME_MIN_DB) {
150 return 0.0f;
151 }
152 return exp( decibels * 0.115129f); // exp( dB * ln(10) / 20 )
153 }
154
155 static inline float AmplToDb(float amplification)
156 {
157 if (amplification == 0) {
158 return VOLUME_MIN_DB;
159 }
160 return 20 * log10(amplification);
161 }
162
François Gaffiedfd74092015-03-19 12:10:59 +0100163};