blob: 4aca26d01f93b572b0089ccceea8930013aac680 [file] [log] [blame]
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08001/*
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 "AudioPolicyManager.h"
28
29#include <math.h>
30
31namespace android {
32
33const VolumeCurvePoint
34ApmGains::sDefaultVolumeCurve[ApmGains::VOLCNT] = {
35 {1, -49.5f}, {33, -33.5f}, {66, -17.0f}, {100, 0.0f}
36};
37
38
39const VolumeCurvePoint
40ApmGains::sDefaultMediaVolumeCurve[ApmGains::VOLCNT] = {
41 {1, -58.0f}, {20, -40.0f}, {60, -17.0f}, {100, 0.0f}
42};
43
44const VolumeCurvePoint
45ApmGains::sExtMediaSystemVolumeCurve[ApmGains::VOLCNT] = {
46 {1, -58.0f}, {20, -40.0f}, {60, -21.0f}, {100, -10.0f}
47};
48
49const VolumeCurvePoint
50ApmGains::sSpeakerMediaVolumeCurve[ApmGains::VOLCNT] = {
51 {1, -56.0f}, {20, -34.0f}, {60, -11.0f}, {100, 0.0f}
52};
53
54const VolumeCurvePoint
55ApmGains::sSpeakerMediaVolumeCurveDrc[ApmGains::VOLCNT] = {
56 {1, -55.0f}, {20, -43.0f}, {86, -12.0f}, {100, 0.0f}
57};
58
59const VolumeCurvePoint
60ApmGains::sSpeakerSonificationVolumeCurve[ApmGains::VOLCNT] = {
61 {1, -29.7f}, {33, -20.1f}, {66, -10.2f}, {100, 0.0f}
62};
63
64const VolumeCurvePoint
65ApmGains::sSpeakerSonificationVolumeCurveDrc[ApmGains::VOLCNT] = {
66 {1, -35.7f}, {33, -26.1f}, {66, -13.2f}, {100, 0.0f}
67};
68
69// AUDIO_STREAM_SYSTEM, AUDIO_STREAM_ENFORCED_AUDIBLE and AUDIO_STREAM_DTMF volume tracks
70// AUDIO_STREAM_RING on phones and AUDIO_STREAM_MUSIC on tablets.
71// AUDIO_STREAM_DTMF tracks AUDIO_STREAM_VOICE_CALL while in call (See AudioService.java).
72// The range is constrained between -24dB and -6dB over speaker and -30dB and -18dB over headset.
73
74const VolumeCurvePoint
75ApmGains::sDefaultSystemVolumeCurve[ApmGains::VOLCNT] = {
76 {1, -24.0f}, {33, -18.0f}, {66, -12.0f}, {100, -6.0f}
77};
78
79const VolumeCurvePoint
80ApmGains::sDefaultSystemVolumeCurveDrc[ApmGains::VOLCNT] = {
81 {1, -34.0f}, {33, -24.0f}, {66, -15.0f}, {100, -6.0f}
82};
83
84const VolumeCurvePoint
85ApmGains::sHeadsetSystemVolumeCurve[ApmGains::VOLCNT] = {
86 {1, -30.0f}, {33, -26.0f}, {66, -22.0f}, {100, -18.0f}
87};
88
89const VolumeCurvePoint
90ApmGains::sDefaultVoiceVolumeCurve[ApmGains::VOLCNT] = {
91 {0, -42.0f}, {33, -28.0f}, {66, -14.0f}, {100, 0.0f}
92};
93
94const VolumeCurvePoint
95ApmGains::sSpeakerVoiceVolumeCurve[ApmGains::VOLCNT] = {
96 {0, -24.0f}, {33, -16.0f}, {66, -8.0f}, {100, 0.0f}
97};
98
99const VolumeCurvePoint
100ApmGains::sLinearVolumeCurve[ApmGains::VOLCNT] = {
101 {0, -96.0f}, {33, -68.0f}, {66, -34.0f}, {100, 0.0f}
102};
103
104const VolumeCurvePoint
105ApmGains::sSilentVolumeCurve[ApmGains::VOLCNT] = {
106 {0, -96.0f}, {1, -96.0f}, {2, -96.0f}, {100, -96.0f}
107};
108
109const VolumeCurvePoint
110ApmGains::sFullScaleVolumeCurve[ApmGains::VOLCNT] = {
111 {0, 0.0f}, {1, 0.0f}, {2, 0.0f}, {100, 0.0f}
112};
113
114const VolumeCurvePoint *ApmGains::sVolumeProfiles[AUDIO_STREAM_CNT]
115 [ApmGains::DEVICE_CATEGORY_CNT] = {
116 { // AUDIO_STREAM_VOICE_CALL
117 ApmGains::sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_HEADSET
118 ApmGains::sSpeakerVoiceVolumeCurve, // DEVICE_CATEGORY_SPEAKER
119 ApmGains::sSpeakerVoiceVolumeCurve, // DEVICE_CATEGORY_EARPIECE
120 ApmGains::sDefaultMediaVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA
121 },
122 { // AUDIO_STREAM_SYSTEM
123 ApmGains::sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
124 ApmGains::sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
125 ApmGains::sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_EARPIECE
126 ApmGains::sExtMediaSystemVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA
127 },
128 { // AUDIO_STREAM_RING
129 ApmGains::sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
130 ApmGains::sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
131 ApmGains::sDefaultVolumeCurve, // DEVICE_CATEGORY_EARPIECE
132 ApmGains::sExtMediaSystemVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA
133 },
134 { // AUDIO_STREAM_MUSIC
135 ApmGains::sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET
136 ApmGains::sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER
137 ApmGains::sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_EARPIECE
138 ApmGains::sDefaultMediaVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA
139 },
140 { // AUDIO_STREAM_ALARM
141 ApmGains::sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
142 ApmGains::sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
143 ApmGains::sDefaultVolumeCurve, // DEVICE_CATEGORY_EARPIECE
144 ApmGains::sExtMediaSystemVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA
145 },
146 { // AUDIO_STREAM_NOTIFICATION
147 ApmGains::sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
148 ApmGains::sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
149 ApmGains::sDefaultVolumeCurve, // DEVICE_CATEGORY_EARPIECE
150 ApmGains::sExtMediaSystemVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA
151 },
152 { // AUDIO_STREAM_BLUETOOTH_SCO
153 ApmGains::sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_HEADSET
154 ApmGains::sSpeakerVoiceVolumeCurve, // DEVICE_CATEGORY_SPEAKER
155 ApmGains::sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_EARPIECE
156 ApmGains::sDefaultMediaVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA
157 },
158 { // AUDIO_STREAM_ENFORCED_AUDIBLE
159 ApmGains::sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
160 ApmGains::sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
161 ApmGains::sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_EARPIECE
162 ApmGains::sExtMediaSystemVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA
163 },
164 { // AUDIO_STREAM_DTMF
165 ApmGains::sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
166 ApmGains::sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
167 ApmGains::sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_EARPIECE
168 ApmGains::sExtMediaSystemVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA
169 },
170 { // AUDIO_STREAM_TTS
171 // "Transmitted Through Speaker": always silent except on DEVICE_CATEGORY_SPEAKER
172 ApmGains::sSilentVolumeCurve, // DEVICE_CATEGORY_HEADSET
173 ApmGains::sLinearVolumeCurve, // DEVICE_CATEGORY_SPEAKER
174 ApmGains::sSilentVolumeCurve, // DEVICE_CATEGORY_EARPIECE
175 ApmGains::sSilentVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA
176 },
177 { // AUDIO_STREAM_ACCESSIBILITY
178 ApmGains::sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET
179 ApmGains::sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER
180 ApmGains::sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_EARPIECE
181 ApmGains::sDefaultMediaVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA
182 },
183 { // AUDIO_STREAM_REROUTING
184 ApmGains::sFullScaleVolumeCurve, // DEVICE_CATEGORY_HEADSET
185 ApmGains::sFullScaleVolumeCurve, // DEVICE_CATEGORY_SPEAKER
186 ApmGains::sFullScaleVolumeCurve, // DEVICE_CATEGORY_EARPIECE
187 ApmGains::sFullScaleVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA
188 },
189 { // AUDIO_STREAM_PATCH
190 ApmGains::sFullScaleVolumeCurve, // DEVICE_CATEGORY_HEADSET
191 ApmGains::sFullScaleVolumeCurve, // DEVICE_CATEGORY_SPEAKER
192 ApmGains::sFullScaleVolumeCurve, // DEVICE_CATEGORY_EARPIECE
193 ApmGains::sFullScaleVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA
194 },
195};
196
197//static
198audio_devices_t ApmGains::getDeviceForVolume(audio_devices_t device)
199{
200 if (device == AUDIO_DEVICE_NONE) {
201 // this happens when forcing a route update and no track is active on an output.
202 // In this case the returned category is not important.
203 device = AUDIO_DEVICE_OUT_SPEAKER;
204 } else if (popcount(device) > 1) {
205 // Multiple device selection is either:
206 // - speaker + one other device: give priority to speaker in this case.
207 // - one A2DP device + another device: happens with duplicated output. In this case
208 // retain the device on the A2DP output as the other must not correspond to an active
209 // selection if not the speaker.
210 // - HDMI-CEC system audio mode only output: give priority to available item in order.
211 if (device & AUDIO_DEVICE_OUT_SPEAKER) {
212 device = AUDIO_DEVICE_OUT_SPEAKER;
213 } else if (device & AUDIO_DEVICE_OUT_HDMI_ARC) {
214 device = AUDIO_DEVICE_OUT_HDMI_ARC;
215 } else if (device & AUDIO_DEVICE_OUT_AUX_LINE) {
216 device = AUDIO_DEVICE_OUT_AUX_LINE;
217 } else if (device & AUDIO_DEVICE_OUT_SPDIF) {
218 device = AUDIO_DEVICE_OUT_SPDIF;
219 } else {
220 device = (audio_devices_t)(device & AUDIO_DEVICE_OUT_ALL_A2DP);
221 }
222 }
223
224 /*SPEAKER_SAFE is an alias of SPEAKER for purposes of volume control*/
225 if (device == AUDIO_DEVICE_OUT_SPEAKER_SAFE)
226 device = AUDIO_DEVICE_OUT_SPEAKER;
227
228 ALOGW_IF(popcount(device) != 1,
229 "getDeviceForVolume() invalid device combination: %08x",
230 device);
231
232 return device;
233}
234
235//static
236ApmGains::device_category ApmGains::getDeviceCategory(audio_devices_t device)
237{
238 switch(getDeviceForVolume(device)) {
239 case AUDIO_DEVICE_OUT_EARPIECE:
240 return ApmGains::DEVICE_CATEGORY_EARPIECE;
241 case AUDIO_DEVICE_OUT_WIRED_HEADSET:
242 case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
243 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
244 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
245 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
246 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
247 return ApmGains::DEVICE_CATEGORY_HEADSET;
248 case AUDIO_DEVICE_OUT_LINE:
249 case AUDIO_DEVICE_OUT_AUX_DIGITAL:
250 /*USB? Remote submix?*/
251 return ApmGains::DEVICE_CATEGORY_EXT_MEDIA;
252 case AUDIO_DEVICE_OUT_SPEAKER:
253 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
254 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
255 case AUDIO_DEVICE_OUT_USB_ACCESSORY:
256 case AUDIO_DEVICE_OUT_USB_DEVICE:
257 case AUDIO_DEVICE_OUT_REMOTE_SUBMIX:
258 default:
259 return ApmGains::DEVICE_CATEGORY_SPEAKER;
260 }
261}
262
263//static
264float ApmGains::volIndexToAmpl(audio_devices_t device, const StreamDescriptor& streamDesc,
265 int indexInUi)
266{
267 ApmGains::device_category deviceCategory = ApmGains::getDeviceCategory(device);
268 const VolumeCurvePoint *curve = streamDesc.mVolumeCurve[deviceCategory];
269
270 // the volume index in the UI is relative to the min and max volume indices for this stream type
271 int nbSteps = 1 + curve[ApmGains::VOLMAX].mIndex -
272 curve[ApmGains::VOLMIN].mIndex;
273 int volIdx = (nbSteps * (indexInUi - streamDesc.mIndexMin)) /
274 (streamDesc.mIndexMax - streamDesc.mIndexMin);
275
276 // find what part of the curve this index volume belongs to, or if it's out of bounds
277 int segment = 0;
278 if (volIdx < curve[ApmGains::VOLMIN].mIndex) { // out of bounds
279 return 0.0f;
280 } else if (volIdx < curve[ApmGains::VOLKNEE1].mIndex) {
281 segment = 0;
282 } else if (volIdx < curve[ApmGains::VOLKNEE2].mIndex) {
283 segment = 1;
284 } else if (volIdx <= curve[ApmGains::VOLMAX].mIndex) {
285 segment = 2;
286 } else { // out of bounds
287 return 1.0f;
288 }
289
290 // linear interpolation in the attenuation table in dB
291 float decibels = curve[segment].mDBAttenuation +
292 ((float)(volIdx - curve[segment].mIndex)) *
293 ( (curve[segment+1].mDBAttenuation -
294 curve[segment].mDBAttenuation) /
295 ((float)(curve[segment+1].mIndex -
296 curve[segment].mIndex)) );
297
298 float amplification = exp( decibels * 0.115129f); // exp( dB * ln(10) / 20 )
299
300 ALOGVV("VOLUME vol index=[%d %d %d], dB=[%.1f %.1f %.1f] ampl=%.5f",
301 curve[segment].mIndex, volIdx,
302 curve[segment+1].mIndex,
303 curve[segment].mDBAttenuation,
304 decibels,
305 curve[segment+1].mDBAttenuation,
306 amplification);
307
308 return amplification;
309}
310
311
312
313AudioGain::AudioGain(int index, bool useInChannelMask)
314{
315 mIndex = index;
316 mUseInChannelMask = useInChannelMask;
317 memset(&mGain, 0, sizeof(struct audio_gain));
318}
319
320void AudioGain::getDefaultConfig(struct audio_gain_config *config)
321{
322 config->index = mIndex;
323 config->mode = mGain.mode;
324 config->channel_mask = mGain.channel_mask;
325 if ((mGain.mode & AUDIO_GAIN_MODE_JOINT) == AUDIO_GAIN_MODE_JOINT) {
326 config->values[0] = mGain.default_value;
327 } else {
328 uint32_t numValues;
329 if (mUseInChannelMask) {
330 numValues = audio_channel_count_from_in_mask(mGain.channel_mask);
331 } else {
332 numValues = audio_channel_count_from_out_mask(mGain.channel_mask);
333 }
334 for (size_t i = 0; i < numValues; i++) {
335 config->values[i] = mGain.default_value;
336 }
337 }
338 if ((mGain.mode & AUDIO_GAIN_MODE_RAMP) == AUDIO_GAIN_MODE_RAMP) {
339 config->ramp_duration_ms = mGain.min_ramp_ms;
340 }
341}
342
343status_t AudioGain::checkConfig(const struct audio_gain_config *config)
344{
345 if ((config->mode & ~mGain.mode) != 0) {
346 return BAD_VALUE;
347 }
348 if ((config->mode & AUDIO_GAIN_MODE_JOINT) == AUDIO_GAIN_MODE_JOINT) {
349 if ((config->values[0] < mGain.min_value) ||
350 (config->values[0] > mGain.max_value)) {
351 return BAD_VALUE;
352 }
353 } else {
354 if ((config->channel_mask & ~mGain.channel_mask) != 0) {
355 return BAD_VALUE;
356 }
357 uint32_t numValues;
358 if (mUseInChannelMask) {
359 numValues = audio_channel_count_from_in_mask(config->channel_mask);
360 } else {
361 numValues = audio_channel_count_from_out_mask(config->channel_mask);
362 }
363 for (size_t i = 0; i < numValues; i++) {
364 if ((config->values[i] < mGain.min_value) ||
365 (config->values[i] > mGain.max_value)) {
366 return BAD_VALUE;
367 }
368 }
369 }
370 if ((config->mode & AUDIO_GAIN_MODE_RAMP) == AUDIO_GAIN_MODE_RAMP) {
371 if ((config->ramp_duration_ms < mGain.min_ramp_ms) ||
372 (config->ramp_duration_ms > mGain.max_ramp_ms)) {
373 return BAD_VALUE;
374 }
375 }
376 return NO_ERROR;
377}
378
379void AudioGain::dump(int fd, int spaces, int index) const
380{
381 const size_t SIZE = 256;
382 char buffer[SIZE];
383 String8 result;
384
385 snprintf(buffer, SIZE, "%*sGain %d:\n", spaces, "", index+1);
386 result.append(buffer);
387 snprintf(buffer, SIZE, "%*s- mode: %08x\n", spaces, "", mGain.mode);
388 result.append(buffer);
389 snprintf(buffer, SIZE, "%*s- channel_mask: %08x\n", spaces, "", mGain.channel_mask);
390 result.append(buffer);
391 snprintf(buffer, SIZE, "%*s- min_value: %d mB\n", spaces, "", mGain.min_value);
392 result.append(buffer);
393 snprintf(buffer, SIZE, "%*s- max_value: %d mB\n", spaces, "", mGain.max_value);
394 result.append(buffer);
395 snprintf(buffer, SIZE, "%*s- default_value: %d mB\n", spaces, "", mGain.default_value);
396 result.append(buffer);
397 snprintf(buffer, SIZE, "%*s- step_value: %d mB\n", spaces, "", mGain.step_value);
398 result.append(buffer);
399 snprintf(buffer, SIZE, "%*s- min_ramp_ms: %d ms\n", spaces, "", mGain.min_ramp_ms);
400 result.append(buffer);
401 snprintf(buffer, SIZE, "%*s- max_ramp_ms: %d ms\n", spaces, "", mGain.max_ramp_ms);
402 result.append(buffer);
403
404 write(fd, result.string(), result.size());
405}
406
407
408// --- StreamDescriptor class implementation
409
410StreamDescriptor::StreamDescriptor()
411 : mIndexMin(0), mIndexMax(1), mCanBeMuted(true)
412{
413 mIndexCur.add(AUDIO_DEVICE_OUT_DEFAULT, 0);
414}
415
416int StreamDescriptor::getVolumeIndex(audio_devices_t device)
417{
418 device = ApmGains::getDeviceForVolume(device);
419 // there is always a valid entry for AUDIO_DEVICE_OUT_DEFAULT
420 if (mIndexCur.indexOfKey(device) < 0) {
421 device = AUDIO_DEVICE_OUT_DEFAULT;
422 }
423 return mIndexCur.valueFor(device);
424}
425
426void StreamDescriptor::dump(int fd)
427{
428 const size_t SIZE = 256;
429 char buffer[SIZE];
430 String8 result;
431
432 snprintf(buffer, SIZE, "%s %02d %02d ",
433 mCanBeMuted ? "true " : "false", mIndexMin, mIndexMax);
434 result.append(buffer);
435 for (size_t i = 0; i < mIndexCur.size(); i++) {
436 snprintf(buffer, SIZE, "%04x : %02d, ",
437 mIndexCur.keyAt(i),
438 mIndexCur.valueAt(i));
439 result.append(buffer);
440 }
441 result.append("\n");
442
443 write(fd, result.string(), result.size());
444}
445
446}; // namespace android