François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 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 | |
| 22 | class VolumeCurvePoint |
| 23 | { |
| 24 | public: |
| 25 | int mIndex; |
| 26 | float mDBAttenuation; |
| 27 | }; |
| 28 | |
| 29 | class Volume |
| 30 | { |
| 31 | public: |
| 32 | /** |
| 33 | * 4 points to define the volume attenuation curve, each characterized by the volume |
| 34 | * index (from 0 to 100) at which they apply, and the attenuation in dB at that index. |
| 35 | * we use 100 steps to avoid rounding errors when computing the volume in volIndexToAmpl() |
| 36 | * |
| 37 | * @todo shall become configurable |
| 38 | */ |
| 39 | enum { |
| 40 | VOLMIN = 0, |
| 41 | VOLKNEE1 = 1, |
| 42 | VOLKNEE2 = 2, |
| 43 | VOLMAX = 3, |
| 44 | |
| 45 | VOLCNT = 4 |
| 46 | }; |
| 47 | |
| 48 | /** |
| 49 | * device categories used for volume curve management. |
| 50 | */ |
| 51 | enum device_category { |
| 52 | DEVICE_CATEGORY_HEADSET, |
| 53 | DEVICE_CATEGORY_SPEAKER, |
| 54 | DEVICE_CATEGORY_EARPIECE, |
| 55 | DEVICE_CATEGORY_EXT_MEDIA, |
| 56 | DEVICE_CATEGORY_CNT |
| 57 | }; |
| 58 | |
| 59 | /** |
| 60 | * extract one device relevant for volume control from multiple device selection |
| 61 | * |
| 62 | * @param[in] device for which the volume category is associated |
| 63 | * |
| 64 | * @return subset of device required to limit the number of volume category per device |
| 65 | */ |
| 66 | static audio_devices_t getDeviceForVolume(audio_devices_t device) |
| 67 | { |
| 68 | if (device == AUDIO_DEVICE_NONE) { |
| 69 | // this happens when forcing a route update and no track is active on an output. |
| 70 | // In this case the returned category is not important. |
| 71 | device = AUDIO_DEVICE_OUT_SPEAKER; |
| 72 | } else if (popcount(device) > 1) { |
| 73 | // Multiple device selection is either: |
| 74 | // - speaker + one other device: give priority to speaker in this case. |
| 75 | // - one A2DP device + another device: happens with duplicated output. In this case |
| 76 | // retain the device on the A2DP output as the other must not correspond to an active |
| 77 | // selection if not the speaker. |
| 78 | // - HDMI-CEC system audio mode only output: give priority to available item in order. |
| 79 | if (device & AUDIO_DEVICE_OUT_SPEAKER) { |
| 80 | device = AUDIO_DEVICE_OUT_SPEAKER; |
| 81 | } else if (device & AUDIO_DEVICE_OUT_HDMI_ARC) { |
| 82 | device = AUDIO_DEVICE_OUT_HDMI_ARC; |
| 83 | } else if (device & AUDIO_DEVICE_OUT_AUX_LINE) { |
| 84 | device = AUDIO_DEVICE_OUT_AUX_LINE; |
| 85 | } else if (device & AUDIO_DEVICE_OUT_SPDIF) { |
| 86 | device = AUDIO_DEVICE_OUT_SPDIF; |
| 87 | } else { |
| 88 | device = (audio_devices_t)(device & AUDIO_DEVICE_OUT_ALL_A2DP); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /*SPEAKER_SAFE is an alias of SPEAKER for purposes of volume control*/ |
| 93 | if (device == AUDIO_DEVICE_OUT_SPEAKER_SAFE) |
| 94 | device = AUDIO_DEVICE_OUT_SPEAKER; |
| 95 | |
| 96 | ALOGW_IF(popcount(device) != 1, |
| 97 | "getDeviceForVolume() invalid device combination: %08x", |
| 98 | device); |
| 99 | |
| 100 | return device; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * returns the category the device belongs to with regard to volume curve management |
| 105 | * |
| 106 | * @param[in] device to check upon the category to whom it belongs to. |
| 107 | * |
| 108 | * @return device category. |
| 109 | */ |
| 110 | static device_category getDeviceCategory(audio_devices_t device) |
| 111 | { |
| 112 | switch(getDeviceForVolume(device)) { |
| 113 | case AUDIO_DEVICE_OUT_EARPIECE: |
| 114 | return DEVICE_CATEGORY_EARPIECE; |
| 115 | case AUDIO_DEVICE_OUT_WIRED_HEADSET: |
| 116 | case AUDIO_DEVICE_OUT_WIRED_HEADPHONE: |
| 117 | case AUDIO_DEVICE_OUT_BLUETOOTH_SCO: |
| 118 | case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET: |
| 119 | case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP: |
| 120 | case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES: |
| 121 | return DEVICE_CATEGORY_HEADSET; |
| 122 | case AUDIO_DEVICE_OUT_LINE: |
| 123 | case AUDIO_DEVICE_OUT_AUX_DIGITAL: |
| 124 | /*USB? Remote submix?*/ |
| 125 | return DEVICE_CATEGORY_EXT_MEDIA; |
| 126 | case AUDIO_DEVICE_OUT_SPEAKER: |
| 127 | case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT: |
| 128 | case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER: |
| 129 | case AUDIO_DEVICE_OUT_USB_ACCESSORY: |
| 130 | case AUDIO_DEVICE_OUT_USB_DEVICE: |
| 131 | case AUDIO_DEVICE_OUT_REMOTE_SUBMIX: |
| 132 | default: |
| 133 | return DEVICE_CATEGORY_SPEAKER; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | }; |