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 #define LOG_TAG "APM::Gains"
18 //#define LOG_NDEBUG 0
19
20 //#define VERY_VERBOSE_LOGGING
21 #ifdef VERY_VERBOSE_LOGGING
22 #define ALOGVV ALOGV
23 #else
24 #define ALOGVV(a...) do { } while(0)
25 #endif
26
27 #include "Gains.h"
28 #include <Volume.h>
29 #include <math.h>
30 #include <utils/String8.h>
31
32 namespace android {
33
34 // Enginedefault
35 const VolumeCurvePoint
36 Gains::sDefaultVolumeCurve[Volume::VOLCNT] = {
37 {1, -49.5f}, {33, -33.5f}, {66, -17.0f}, {100, 0.0f}
38 };
39
40
41 const VolumeCurvePoint
42 Gains::sDefaultMediaVolumeCurve[Volume::VOLCNT] = {
43 {1, -58.0f}, {20, -40.0f}, {60, -17.0f}, {100, 0.0f}
44 };
45
46 const VolumeCurvePoint
47 Gains::sExtMediaSystemVolumeCurve[Volume::VOLCNT] = {
48 {1, -58.0f}, {20, -40.0f}, {60, -21.0f}, {100, -10.0f}
49 };
50
51 const VolumeCurvePoint
52 Gains::sSpeakerMediaVolumeCurve[Volume::VOLCNT] = {
53 {1, -56.0f}, {20, -34.0f}, {60, -11.0f}, {100, 0.0f}
54 };
55
56 const VolumeCurvePoint
57 Gains::sSpeakerMediaVolumeCurveDrc[Volume::VOLCNT] = {
58 {1, -55.0f}, {20, -43.0f}, {86, -12.0f}, {100, 0.0f}
59 };
60
61 const VolumeCurvePoint
62 Gains::sSpeakerSonificationVolumeCurve[Volume::VOLCNT] = {
63 {1, -29.7f}, {33, -20.1f}, {66, -10.2f}, {100, 0.0f}
64 };
65
66 const VolumeCurvePoint
67 Gains::sSpeakerSonificationVolumeCurveDrc[Volume::VOLCNT] = {
68 {1, -35.7f}, {33, -26.1f}, {66, -13.2f}, {100, 0.0f}
69 };
70
71 // AUDIO_STREAM_SYSTEM, AUDIO_STREAM_ENFORCED_AUDIBLE and AUDIO_STREAM_DTMF volume tracks
72 // AUDIO_STREAM_RING on phones and AUDIO_STREAM_MUSIC on tablets.
73 // AUDIO_STREAM_DTMF tracks AUDIO_STREAM_VOICE_CALL while in call (See AudioService.java).
74 // The range is constrained between -24dB and -6dB over speaker and -30dB and -18dB over headset.
75
76 const VolumeCurvePoint
77 Gains::sDefaultSystemVolumeCurve[Volume::VOLCNT] = {
78 {1, -24.0f}, {33, -18.0f}, {66, -12.0f}, {100, -6.0f}
79 };
80
81 const VolumeCurvePoint
82 Gains::sDefaultSystemVolumeCurveDrc[Volume::VOLCNT] = {
83 {1, -34.0f}, {33, -24.0f}, {66, -15.0f}, {100, -6.0f}
84 };
85
86 const VolumeCurvePoint
87 Gains::sHeadsetSystemVolumeCurve[Volume::VOLCNT] = {
88 {1, -30.0f}, {33, -26.0f}, {66, -22.0f}, {100, -18.0f}
89 };
90
91 const VolumeCurvePoint
92 Gains::sDefaultVoiceVolumeCurve[Volume::VOLCNT] = {
93 {0, -42.0f}, {33, -28.0f}, {66, -14.0f}, {100, 0.0f}
94 };
95
96 const VolumeCurvePoint
97 Gains::sSpeakerVoiceVolumeCurve[Volume::VOLCNT] = {
98 {0, -24.0f}, {33, -16.0f}, {66, -8.0f}, {100, 0.0f}
99 };
100
101 const VolumeCurvePoint
102 Gains::sLinearVolumeCurve[Volume::VOLCNT] = {
103 {0, -96.0f}, {33, -68.0f}, {66, -34.0f}, {100, 0.0f}
104 };
105
106 const VolumeCurvePoint
107 Gains::sSilentVolumeCurve[Volume::VOLCNT] = {
108 {0, -96.0f}, {1, -96.0f}, {2, -96.0f}, {100, -96.0f}
109 };
110
111 const VolumeCurvePoint
112 Gains::sFullScaleVolumeCurve[Volume::VOLCNT] = {
113 {0, 0.0f}, {1, 0.0f}, {2, 0.0f}, {100, 0.0f}
114 };
115
116 const VolumeCurvePoint
117 Gains::sHearingAidVolumeCurve[Volume::VOLCNT] = {
118 {1, -128.0f}, {20, -80.0f}, {60, -40.0f}, {100, 0.0f}
119 };
120
121 const VolumeCurvePoint *Gains::sVolumeProfiles[AUDIO_STREAM_CNT]
122 [DEVICE_CATEGORY_CNT] = {
123 { // AUDIO_STREAM_VOICE_CALL
124 Gains::sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_HEADSET
125 Gains::sSpeakerVoiceVolumeCurve, // DEVICE_CATEGORY_SPEAKER
126 Gains::sSpeakerVoiceVolumeCurve, // DEVICE_CATEGORY_EARPIECE
127 Gains::sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_EXT_MEDIA
128 Gains::sHearingAidVolumeCurve // DEVICE_CATEGORY_HEARING_AID
129 },
130 { // AUDIO_STREAM_SYSTEM
131 Gains::sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
132 Gains::sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
133 Gains::sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_EARPIECE
134 Gains::sExtMediaSystemVolumeCurve, // DEVICE_CATEGORY_EXT_MEDIA
135 Gains::sHearingAidVolumeCurve // DEVICE_CATEGORY_HEARING_AID
136 },
137 { // AUDIO_STREAM_RING
138 Gains::sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
139 Gains::sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
140 Gains::sDefaultVolumeCurve, // DEVICE_CATEGORY_EARPIECE
141 Gains::sExtMediaSystemVolumeCurve, // DEVICE_CATEGORY_EXT_MEDIA
142 Gains::sHearingAidVolumeCurve // DEVICE_CATEGORY_HEARING_AID
143 },
144 { // AUDIO_STREAM_MUSIC
145 Gains::sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET
146 Gains::sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER
147 Gains::sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_EARPIECE
148 Gains::sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_EXT_MEDIA
149 Gains::sHearingAidVolumeCurve // DEVICE_CATEGORY_HEARING_AID
150 },
151 { // AUDIO_STREAM_ALARM
152 Gains::sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
153 Gains::sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
154 Gains::sDefaultVolumeCurve, // DEVICE_CATEGORY_EARPIECE
155 Gains::sExtMediaSystemVolumeCurve, // DEVICE_CATEGORY_EXT_MEDIA
156 Gains::sHearingAidVolumeCurve // DEVICE_CATEGORY_HEARING_AID
157 },
158 { // AUDIO_STREAM_NOTIFICATION
159 Gains::sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
160 Gains::sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
161 Gains::sDefaultVolumeCurve, // DEVICE_CATEGORY_EARPIECE
162 Gains::sExtMediaSystemVolumeCurve, // DEVICE_CATEGORY_EXT_MEDIA
163 Gains::sHearingAidVolumeCurve // DEVICE_CATEGORY_HEARING_AID
164 },
165 { // AUDIO_STREAM_BLUETOOTH_SCO
166 Gains::sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_HEADSET
167 Gains::sSpeakerVoiceVolumeCurve, // DEVICE_CATEGORY_SPEAKER
168 Gains::sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_EARPIECE
169 Gains::sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_EXT_MEDIA
170 Gains::sHearingAidVolumeCurve // DEVICE_CATEGORY_HEARING_AID
171 },
172 { // AUDIO_STREAM_ENFORCED_AUDIBLE
173 Gains::sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
174 Gains::sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
175 Gains::sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_EARPIECE
176 Gains::sExtMediaSystemVolumeCurve, // DEVICE_CATEGORY_EXT_MEDIA
177 Gains::sHearingAidVolumeCurve // DEVICE_CATEGORY_HEARING_AID
178 },
179 { // AUDIO_STREAM_DTMF
180 Gains::sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
181 Gains::sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
182 Gains::sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_EARPIECE
183 Gains::sExtMediaSystemVolumeCurve, // DEVICE_CATEGORY_EXT_MEDIA
184 Gains::sHearingAidVolumeCurve // DEVICE_CATEGORY_HEARING_AID
185 },
186 { // AUDIO_STREAM_TTS
187 // "Transmitted Through Speaker": always silent except on DEVICE_CATEGORY_SPEAKER
188 Gains::sSilentVolumeCurve, // DEVICE_CATEGORY_HEADSET
189 Gains::sFullScaleVolumeCurve, // DEVICE_CATEGORY_SPEAKER
190 Gains::sSilentVolumeCurve, // DEVICE_CATEGORY_EARPIECE
191 Gains::sSilentVolumeCurve, // DEVICE_CATEGORY_EXT_MEDIA
192 Gains::sHearingAidVolumeCurve // DEVICE_CATEGORY_HEARING_AID
193 },
194 { // AUDIO_STREAM_ACCESSIBILITY
195 Gains::sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET
196 Gains::sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER
197 Gains::sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_EARPIECE
198 Gains::sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_EXT_MEDIA
199 Gains::sHearingAidVolumeCurve // DEVICE_CATEGORY_HEARING_AID
200 },
201 { // AUDIO_STREAM_REROUTING
202 Gains::sFullScaleVolumeCurve, // DEVICE_CATEGORY_HEADSET
203 Gains::sFullScaleVolumeCurve, // DEVICE_CATEGORY_SPEAKER
204 Gains::sFullScaleVolumeCurve, // DEVICE_CATEGORY_EARPIECE
205 Gains::sFullScaleVolumeCurve, // DEVICE_CATEGORY_EXT_MEDIA
206 Gains::sFullScaleVolumeCurve // DEVICE_CATEGORY_HEARING_AID
207 },
208 { // AUDIO_STREAM_PATCH
209 Gains::sFullScaleVolumeCurve, // DEVICE_CATEGORY_HEADSET
210 Gains::sFullScaleVolumeCurve, // DEVICE_CATEGORY_SPEAKER
211 Gains::sFullScaleVolumeCurve, // DEVICE_CATEGORY_EARPIECE
212 Gains::sFullScaleVolumeCurve, // DEVICE_CATEGORY_EXT_MEDIA
213 Gains::sFullScaleVolumeCurve // DEVICE_CATEGORY_HEARING_AID
214 },
215 };
216
217 //static
volIndexToDb(const VolumeCurvePoint * curve,int indexMin,int indexMax,int indexInUi)218 float Gains::volIndexToDb(const VolumeCurvePoint *curve, int indexMin, int indexMax, int indexInUi)
219 {
220 // the volume index in the UI is relative to the min and max volume indices for this stream type
221 int nbSteps = 1 + curve[Volume::VOLMAX].mIndex - curve[Volume::VOLMIN].mIndex;
222 int volIdx = (nbSteps * (indexInUi - indexMin)) / (indexMax - indexMin);
223
224 // find what part of the curve this index volume belongs to, or if it's out of bounds
225 int segment = 0;
226 if (volIdx < curve[Volume::VOLMIN].mIndex) { // out of bounds
227 return VOLUME_MIN_DB;
228 } else if (volIdx < curve[Volume::VOLKNEE1].mIndex) {
229 segment = 0;
230 } else if (volIdx < curve[Volume::VOLKNEE2].mIndex) {
231 segment = 1;
232 } else if (volIdx <= curve[Volume::VOLMAX].mIndex) {
233 segment = 2;
234 } else { // out of bounds
235 return 0.0f;
236 }
237
238 // linear interpolation in the attenuation table in dB
239 float decibels = curve[segment].mDBAttenuation +
240 ((float)(volIdx - curve[segment].mIndex)) *
241 ( (curve[segment+1].mDBAttenuation -
242 curve[segment].mDBAttenuation) /
243 ((float)(curve[segment+1].mIndex -
244 curve[segment].mIndex)) );
245
246 ALOGVV("VOLUME vol index=[%d %d %d], dB=[%.1f %.1f %.1f]",
247 curve[segment].mIndex, volIdx,
248 curve[segment+1].mIndex,
249 curve[segment].mDBAttenuation,
250 decibels,
251 curve[segment+1].mDBAttenuation);
252
253 return decibels;
254 }
255
256 } // namespace android
257