blob: 3bd5220c528430cfc509da2f0ee34ca3edf92e7a [file] [log] [blame]
François Gaffie20f06f92015-03-24 09:01:14 +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#define LOG_TAG "APM::AudioPolicyEngine/Stream"
18
19#include "Stream.h"
20#include <system/audio.h>
21
22using std::string;
23
24namespace android
25{
26namespace audio_policy
27{
28
29status_t Element<audio_stream_type_t>::setIdentifier(audio_stream_type_t identifier)
30{
31 if (identifier > AUDIO_STREAM_CNT) {
32 return BAD_VALUE;
33 }
34 mIdentifier = identifier;
35 ALOGD("%s: Stream %s identifier 0x%X", __FUNCTION__, getName().c_str(), identifier);
36 return NO_ERROR;
37}
38
39/**
40* Set the strategy to follow for this stream.
41* It checks if the strategy is valid.
42*
43* @param[in] strategy to be followed.
44*
45* @return NO_ERROR if the strategy is set correctly, error code otherwise.
46*/
47template <>
48status_t Element<audio_stream_type_t>::set<routing_strategy>(routing_strategy strategy)
49{
50 if (strategy >= NUM_STRATEGIES) {
51 return BAD_VALUE;
52 }
53 mApplicableStrategy = strategy;
54 ALOGD("%s: 0x%X for Stream %s", __FUNCTION__, strategy, getName().c_str());
55 return NO_ERROR;
56}
57
58template <>
59routing_strategy Element<audio_stream_type_t>::get<routing_strategy>() const
60{
61 ALOGV("%s: 0x%X for Stream %s", __FUNCTION__, mApplicableStrategy, getName().c_str());
62 return mApplicableStrategy;
63}
64
François Gaffied0609ad2015-12-01 17:56:08 +010065status_t Element<audio_stream_type_t>::setVolumeProfile(device_category category,
François Gaffie20f06f92015-03-24 09:01:14 +010066 const VolumeCurvePoints &points)
67{
François Gaffie8649a342015-12-03 17:14:02 +010068 ALOGD("%s: adding volume profile for %s for device category %d, points nb =%zu", __FUNCTION__,
François Gaffie20f06f92015-03-24 09:01:14 +010069 getName().c_str(), category, points.size());
70 mVolumeProfiles[category] = points;
71
72 for (size_t i = 0; i < points.size(); i++) {
François Gaffie8649a342015-12-03 17:14:02 +010073 ALOGV("%s: %s cat=%d curve index =%zu Index=%d dBAttenuation=%f",
François Gaffie20f06f92015-03-24 09:01:14 +010074 __FUNCTION__, getName().c_str(), category, i, points[i].mIndex,
75 points[i].mDBAttenuation);
76 }
77 return NO_ERROR;
78}
79
80status_t Element<audio_stream_type_t>::initVolume(int indexMin, int indexMax)
81{
82 ALOGV("initStreamVolume() stream %s, min %d, max %d", getName().c_str(), indexMin, indexMax);
83 if (indexMin < 0 || indexMin >= indexMax) {
84 ALOGW("initStreamVolume() invalid index limits for stream %s, min %d, max %d",
85 getName().c_str(), indexMin, indexMax);
86 return BAD_VALUE;
87 }
88 mIndexMin = indexMin;
89 mIndexMax = indexMax;
90
91 return NO_ERROR;
92}
93
François Gaffied0609ad2015-12-01 17:56:08 +010094float Element<audio_stream_type_t>::volIndexToDb(device_category deviceCategory, int indexInUi)
François Gaffie20f06f92015-03-24 09:01:14 +010095{
96 VolumeProfileConstIterator it = mVolumeProfiles.find(deviceCategory);
97 if (it == mVolumeProfiles.end()) {
98 ALOGE("%s: device category %d not found for stream %s", __FUNCTION__, deviceCategory,
99 getName().c_str());
Safa Boumaiza8cf5cf72015-11-10 10:11:54 +0100100 return 0.0f;
François Gaffie20f06f92015-03-24 09:01:14 +0100101 }
102 const VolumeCurvePoints curve = mVolumeProfiles[deviceCategory];
103 if (curve.size() != Volume::VOLCNT) {
104 ALOGE("%s: invalid profile for category %d and for stream %s", __FUNCTION__, deviceCategory,
105 getName().c_str());
Safa Boumaiza8cf5cf72015-11-10 10:11:54 +0100106 return 0.0f;
François Gaffie20f06f92015-03-24 09:01:14 +0100107 }
108
109 // the volume index in the UI is relative to the min and max volume indices for this stream type
110 int nbSteps = 1 + curve[Volume::VOLMAX].mIndex -
111 curve[Volume::VOLMIN].mIndex;
112
113 if (mIndexMax - mIndexMin == 0) {
114 ALOGE("%s: Invalid volume indexes Min=Max=%d", __FUNCTION__, mIndexMin);
Safa Boumaiza8cf5cf72015-11-10 10:11:54 +0100115 return 0.0f;
François Gaffie20f06f92015-03-24 09:01:14 +0100116 }
117 int volIdx = (nbSteps * (indexInUi - mIndexMin)) /
118 (mIndexMax - mIndexMin);
119
120 // find what part of the curve this index volume belongs to, or if it's out of bounds
121 int segment = 0;
122 if (volIdx < curve[Volume::VOLMIN].mIndex) { // out of bounds
Safa Boumaiza8cf5cf72015-11-10 10:11:54 +0100123 return VOLUME_MIN_DB;
François Gaffie20f06f92015-03-24 09:01:14 +0100124 } else if (volIdx < curve[Volume::VOLKNEE1].mIndex) {
125 segment = 0;
126 } else if (volIdx < curve[Volume::VOLKNEE2].mIndex) {
127 segment = 1;
128 } else if (volIdx <= curve[Volume::VOLMAX].mIndex) {
129 segment = 2;
130 } else { // out of bounds
Safa Boumaiza8cf5cf72015-11-10 10:11:54 +0100131 return 0.0f;
François Gaffie20f06f92015-03-24 09:01:14 +0100132 }
133
134 // linear interpolation in the attenuation table in dB
135 float decibels = curve[segment].mDBAttenuation +
136 ((float)(volIdx - curve[segment].mIndex)) *
137 ( (curve[segment+1].mDBAttenuation -
138 curve[segment].mDBAttenuation) /
139 ((float)(curve[segment+1].mIndex -
140 curve[segment].mIndex)) );
141
142 ALOGV("VOLUME vol index=[%d %d %d], dB=[%.1f %.1f %.1f]",
143 curve[segment].mIndex, volIdx,
144 curve[segment+1].mIndex,
145 curve[segment].mDBAttenuation,
146 decibels,
147 curve[segment+1].mDBAttenuation);
148
149 return decibels;
150}
151
152} // namespace audio_policy
153} // namespace android
154