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 #pragma once 18 19 #include <system/audio.h> 20 #include <utils/Log.h> 21 #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) 25 26 class VolumeCurvePoint 27 { 28 public: 29 int mIndex; 30 float mDBAttenuation; 31 }; 32 33 /** 34 * device categories used for volume curve management. 35 */ 36 enum device_category { 37 DEVICE_CATEGORY_HEADSET, 38 DEVICE_CATEGORY_SPEAKER, 39 DEVICE_CATEGORY_EARPIECE, 40 DEVICE_CATEGORY_EXT_MEDIA, 41 DEVICE_CATEGORY_HEARING_AID, 42 DEVICE_CATEGORY_CNT 43 }; 44 45 class Volume 46 { 47 public: 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. 51 * we use 100 steps to avoid rounding errors when computing the volume in volIndexToDb() 52 * 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 /** 65 * 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 */ getDeviceForVolume(audio_devices_t device)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; 86 } else if (device & AUDIO_DEVICE_OUT_SPEAKER_SAFE) { 87 device = AUDIO_DEVICE_OUT_SPEAKER_SAFE; 88 } 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 */ getDeviceCategory(audio_devices_t device)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: 128 case AUDIO_DEVICE_OUT_USB_HEADSET: 129 return DEVICE_CATEGORY_HEADSET; 130 case AUDIO_DEVICE_OUT_HEARING_AID: 131 return DEVICE_CATEGORY_HEARING_AID; 132 case AUDIO_DEVICE_OUT_LINE: 133 case AUDIO_DEVICE_OUT_AUX_DIGITAL: 134 case AUDIO_DEVICE_OUT_USB_DEVICE: 135 return DEVICE_CATEGORY_EXT_MEDIA; 136 case AUDIO_DEVICE_OUT_SPEAKER: 137 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT: 138 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER: 139 case AUDIO_DEVICE_OUT_USB_ACCESSORY: 140 case AUDIO_DEVICE_OUT_REMOTE_SUBMIX: 141 default: 142 return DEVICE_CATEGORY_SPEAKER; 143 } 144 } 145 DbToAmpl(float decibels)146 static inline float DbToAmpl(float decibels) 147 { 148 if (decibels <= VOLUME_MIN_DB) { 149 return 0.0f; 150 } 151 return exp( decibels * 0.115129f); // exp( dB * ln(10) / 20 ) 152 } 153 AmplToDb(float amplification)154 static inline float AmplToDb(float amplification) 155 { 156 if (amplification == 0) { 157 return VOLUME_MIN_DB; 158 } 159 return 20 * log10(amplification); 160 } 161 162 }; 163