Andy Hung | 65e7a1b | 2019-01-18 15:49:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | #include <audio_utils/Balance.h> |
| 18 | |
| 19 | namespace android::audio_utils { |
| 20 | |
Andy Hung | 65e7a1b | 2019-01-18 15:49:12 -0800 | [diff] [blame] | 21 | void Balance::setChannelMask(audio_channel_mask_t channelMask) |
| 22 | { |
Andy Hung | ad861e6 | 2021-05-18 11:51:54 -0700 | [diff] [blame] | 23 | using namespace ::android::audio_utils::channels; |
Mikhail Naganov | 90453e4 | 2020-10-01 15:08:31 -0700 | [diff] [blame] | 24 | channelMask = static_cast<audio_channel_mask_t>(channelMask & ~AUDIO_CHANNEL_HAPTIC_ALL); |
Andy Hung | 65e7a1b | 2019-01-18 15:49:12 -0800 | [diff] [blame] | 25 | if (!audio_is_output_channel(channelMask) // invalid mask |
| 26 | || mChannelMask == channelMask) { // no need to do anything |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | mChannelMask = channelMask; |
| 31 | mChannelCount = audio_channel_count_from_out_mask(channelMask); |
| 32 | |
Andy Hung | 78a8b57 | 2019-01-28 15:41:17 -0800 | [diff] [blame] | 33 | // save mBalance into balance for later restoring, then reset |
| 34 | const float balance = mBalance; |
| 35 | mBalance = 0.f; |
| 36 | |
| 37 | // reset mVolumes |
Andy Hung | 65e7a1b | 2019-01-18 15:49:12 -0800 | [diff] [blame] | 38 | mVolumes.resize(mChannelCount); |
| 39 | std::fill(mVolumes.begin(), mVolumes.end(), 1.f); |
Andy Hung | 78a8b57 | 2019-01-28 15:41:17 -0800 | [diff] [blame] | 40 | |
| 41 | // reset ramping variables |
| 42 | mRampBalance = 0.f; |
| 43 | mRampVolumes.clear(); |
| 44 | |
| 45 | if (audio_channel_mask_get_representation(mChannelMask) |
| 46 | == AUDIO_CHANNEL_REPRESENTATION_INDEX) { |
| 47 | mSides.clear(); // mSides unused for channel index masks. |
| 48 | setBalance(balance); // recompute balance |
| 49 | return; |
| 50 | } |
Andy Hung | 65e7a1b | 2019-01-18 15:49:12 -0800 | [diff] [blame] | 51 | |
Andy Hung | 65e7a1b | 2019-01-18 15:49:12 -0800 | [diff] [blame] | 52 | mSides.resize(mChannelCount); |
Andy Hung | 9ae48e6 | 2021-05-06 21:15:36 -0700 | [diff] [blame] | 53 | // If LFE and LFE2 both exist, it should be L and R in 22.2 |
| 54 | int lfe = -1; |
| 55 | int lfe2 = -1; |
| 56 | constexpr unsigned LFE_CHANNEL_INDEX = 3; |
| 57 | constexpr unsigned LFE2_CHANNEL_INDEX = 23; |
Andy Hung | 65e7a1b | 2019-01-18 15:49:12 -0800 | [diff] [blame] | 58 | for (unsigned i = 0, channel = channelMask; channel != 0; ++i) { |
| 59 | const int index = __builtin_ctz(channel); |
Andy Hung | ad861e6 | 2021-05-18 11:51:54 -0700 | [diff] [blame] | 60 | mSides[i] = sideFromChannelIdx(index); |
| 61 | // Keep track of LFE indices |
| 62 | if (index == LFE_CHANNEL_INDEX) { |
| 63 | lfe = i; |
| 64 | } else if (index == LFE2_CHANNEL_INDEX) { |
| 65 | lfe2 = i; |
Andy Hung | 65e7a1b | 2019-01-18 15:49:12 -0800 | [diff] [blame] | 66 | } |
| 67 | channel &= ~(1 << index); |
| 68 | } |
Andy Hung | 9ae48e6 | 2021-05-06 21:15:36 -0700 | [diff] [blame] | 69 | if (lfe >= 0 && lfe2 >= 0) { // if both LFEs exist assign to L and R. |
Andy Hung | ad861e6 | 2021-05-18 11:51:54 -0700 | [diff] [blame] | 70 | mSides[lfe] = AUDIO_GEOMETRY_SIDE_LEFT; |
| 71 | mSides[lfe2] = AUDIO_GEOMETRY_SIDE_RIGHT; |
Andy Hung | 9ae48e6 | 2021-05-06 21:15:36 -0700 | [diff] [blame] | 72 | } |
Andy Hung | 78a8b57 | 2019-01-28 15:41:17 -0800 | [diff] [blame] | 73 | setBalance(balance); // recompute balance |
Andy Hung | 65e7a1b | 2019-01-18 15:49:12 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Andy Hung | 78a8b57 | 2019-01-28 15:41:17 -0800 | [diff] [blame] | 76 | void Balance::process(float *buffer, size_t frames) |
Andy Hung | 65e7a1b | 2019-01-18 15:49:12 -0800 | [diff] [blame] | 77 | { |
Andy Hung | 78a8b57 | 2019-01-28 15:41:17 -0800 | [diff] [blame] | 78 | if (mBalance == 0.f || mChannelCount < 2) { |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | if (mRamp) { |
| 83 | if (mRampVolumes.size() != mVolumes.size()) { |
| 84 | // If mRampVolumes is empty, we do not ramp in this process() but directly |
| 85 | // apply the existing mVolumes. We save the balance and volume state here |
| 86 | // and fall through to non-ramping code below. The next process() will ramp if needed. |
| 87 | mRampBalance = mBalance; |
| 88 | mRampVolumes = mVolumes; |
| 89 | } else if (mRampBalance != mBalance) { |
| 90 | if (frames > 0) { |
| 91 | std::vector<float> mDeltas(mVolumes.size()); |
| 92 | const float r = 1.f / frames; |
| 93 | for (size_t j = 0; j < mChannelCount; ++j) { |
| 94 | mDeltas[j] = (mVolumes[j] - mRampVolumes[j]) * r; |
| 95 | } |
| 96 | |
| 97 | // ramped balance |
| 98 | for (size_t i = 0; i < frames; ++i) { |
| 99 | const float findex = i; |
| 100 | for (size_t j = 0; j < mChannelCount; ++j) { // better precision: delta * i |
| 101 | *buffer++ *= mRampVolumes[j] + mDeltas[j] * findex; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | mRampBalance = mBalance; |
| 106 | mRampVolumes = mVolumes; |
| 107 | return; |
| 108 | } |
| 109 | // fall through |
| 110 | } |
| 111 | |
| 112 | // non-ramped balance |
Andy Hung | 65e7a1b | 2019-01-18 15:49:12 -0800 | [diff] [blame] | 113 | for (size_t i = 0; i < frames; ++i) { |
| 114 | for (size_t j = 0; j < mChannelCount; ++j) { |
| 115 | *buffer++ *= mVolumes[j]; |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
Andy Hung | 65e7a1b | 2019-01-18 15:49:12 -0800 | [diff] [blame] | 120 | void Balance::computeStereoBalance(float balance, float *left, float *right) const |
| 121 | { |
| 122 | if (balance > 0.f) { |
Andy Hung | 78a8b57 | 2019-01-28 15:41:17 -0800 | [diff] [blame] | 123 | *left = mCurve(1.f - balance); |
Andy Hung | 65e7a1b | 2019-01-18 15:49:12 -0800 | [diff] [blame] | 124 | *right = 1.f; |
| 125 | } else if (balance < 0.f) { |
| 126 | *left = 1.f; |
Andy Hung | 78a8b57 | 2019-01-28 15:41:17 -0800 | [diff] [blame] | 127 | *right = mCurve(1.f + balance); |
Andy Hung | 65e7a1b | 2019-01-18 15:49:12 -0800 | [diff] [blame] | 128 | } else { |
| 129 | *left = 1.f; |
| 130 | *right = 1.f; |
| 131 | } |
| 132 | |
| 133 | // Functionally: |
Andy Hung | 78a8b57 | 2019-01-28 15:41:17 -0800 | [diff] [blame] | 134 | // *left = balance > 0.f ? mCurve(1.f - balance) : 1.f; |
| 135 | // *right = balance < 0.f ? mCurve(1.f + balance) : 1.f; |
Andy Hung | 65e7a1b | 2019-01-18 15:49:12 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | std::string Balance::toString() const |
| 139 | { |
| 140 | std::stringstream ss; |
| 141 | ss << "balance " << mBalance << " channelCount " << mChannelCount << " volumes:"; |
| 142 | for (float volume : mVolumes) { |
| 143 | ss << " " << volume; |
| 144 | } |
Andy Hung | 78a8b57 | 2019-01-28 15:41:17 -0800 | [diff] [blame] | 145 | // we do not show mSides, which is only valid for channel position masks. |
Andy Hung | 65e7a1b | 2019-01-18 15:49:12 -0800 | [diff] [blame] | 146 | return ss.str(); |
| 147 | } |
| 148 | |
| 149 | void Balance::setBalance(float balance) |
| 150 | { |
Andy Hung | ad861e6 | 2021-05-18 11:51:54 -0700 | [diff] [blame] | 151 | using namespace ::android::audio_utils::channels; |
Andy Hung | 78a8b57 | 2019-01-28 15:41:17 -0800 | [diff] [blame] | 152 | if (mBalance == balance // no change |
| 153 | || isnan(balance) || fabs(balance) > 1.f) { // balance out of range |
Andy Hung | 65e7a1b | 2019-01-18 15:49:12 -0800 | [diff] [blame] | 154 | return; |
| 155 | } |
| 156 | |
| 157 | mBalance = balance; |
| 158 | |
Andy Hung | 78a8b57 | 2019-01-28 15:41:17 -0800 | [diff] [blame] | 159 | if (mChannelCount < 2) { // if channel count is 1, mVolumes[0] is already set to 1.f |
| 160 | return; // and if channel count < 2, we don't do anything in process(). |
| 161 | } |
| 162 | |
| 163 | // Handle the common cases: |
| 164 | // stereo and channel index masks only affect the first two channels as left and right. |
Andy Hung | 65e7a1b | 2019-01-18 15:49:12 -0800 | [diff] [blame] | 165 | if (mChannelMask == AUDIO_CHANNEL_OUT_STEREO |
| 166 | || audio_channel_mask_get_representation(mChannelMask) |
| 167 | == AUDIO_CHANNEL_REPRESENTATION_INDEX) { |
| 168 | computeStereoBalance(balance, &mVolumes[0], &mVolumes[1]); |
| 169 | return; |
| 170 | } |
| 171 | |
Andy Hung | 78a8b57 | 2019-01-28 15:41:17 -0800 | [diff] [blame] | 172 | // For position masks with more than 2 channels, we consider which side the |
| 173 | // speaker position is on to figure the volume used. |
Andy Hung | ad861e6 | 2021-05-18 11:51:54 -0700 | [diff] [blame] | 174 | float balanceVolumes[3]; // left, right, center (we don't care the order) |
| 175 | static_assert(AUDIO_GEOMETRY_SIDE_LEFT >= 0 |
| 176 | && AUDIO_GEOMETRY_SIDE_LEFT <= std::size(balanceVolumes)); |
| 177 | static_assert(AUDIO_GEOMETRY_SIDE_RIGHT >= 0 |
| 178 | && AUDIO_GEOMETRY_SIDE_RIGHT <= std::size(balanceVolumes)); |
| 179 | static_assert(AUDIO_GEOMETRY_SIDE_CENTER >= 0 |
| 180 | && AUDIO_GEOMETRY_SIDE_CENTER <= std::size(balanceVolumes)); |
| 181 | computeStereoBalance(balance, &balanceVolumes[AUDIO_GEOMETRY_SIDE_LEFT], |
| 182 | &balanceVolumes[AUDIO_GEOMETRY_SIDE_RIGHT]); |
| 183 | balanceVolumes[AUDIO_GEOMETRY_SIDE_CENTER] = 1.f; // center TODO: consider center scaling. |
Andy Hung | 65e7a1b | 2019-01-18 15:49:12 -0800 | [diff] [blame] | 184 | |
| 185 | for (size_t i = 0; i < mVolumes.size(); ++i) { |
| 186 | mVolumes[i] = balanceVolumes[mSides[i]]; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | } // namespace android::audio_utils |