Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 1 | /* |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2 | ** |
| 3 | ** Copyright 2007, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #define LOG_TAG "AudioMixer" |
Glenn Kasten | 7f5d335 | 2013-02-15 23:55:04 +0000 | [diff] [blame] | 19 | //#define LOG_NDEBUG 0 |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 20 | |
Mikhail Naganov | a4f00e2 | 2019-07-31 14:53:29 -0700 | [diff] [blame] | 21 | #include <sstream> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 22 | #include <stdint.h> |
| 23 | #include <string.h> |
| 24 | #include <stdlib.h> |
Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 25 | #include <math.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 26 | #include <sys/types.h> |
| 27 | |
| 28 | #include <utils/Errors.h> |
| 29 | #include <utils/Log.h> |
| 30 | |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 31 | #include <system/audio.h> |
| 32 | |
Glenn Kasten | 3b21c50 | 2011-12-15 09:52:39 -0800 | [diff] [blame] | 33 | #include <audio_utils/primitives.h> |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 34 | #include <audio_utils/format.h> |
Andy Hung | 068561c | 2017-01-03 17:09:32 -0800 | [diff] [blame] | 35 | #include <media/AudioMixer.h> |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 36 | |
Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 37 | #include "AudioMixerOps.h" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 38 | |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 39 | // The FCC_2 macro refers to the Fixed Channel Count of 2 for the legacy integer mixer. |
Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 40 | #ifndef FCC_2 |
| 41 | #define FCC_2 2 |
| 42 | #endif |
| 43 | |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 44 | // Look for MONO_HACK for any Mono hack involving legacy mono channel to |
| 45 | // stereo channel conversion. |
| 46 | |
Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 47 | /* VERY_VERY_VERBOSE_LOGGING will show exactly which process hook and track hook is |
| 48 | * being used. This is a considerable amount of log spam, so don't enable unless you |
| 49 | * are verifying the hook based code. |
| 50 | */ |
| 51 | //#define VERY_VERY_VERBOSE_LOGGING |
| 52 | #ifdef VERY_VERY_VERBOSE_LOGGING |
| 53 | #define ALOGVV ALOGV |
| 54 | //define ALOGVV printf // for test-mixer.cpp |
| 55 | #else |
| 56 | #define ALOGVV(a...) do { } while (0) |
| 57 | #endif |
| 58 | |
Andy Hung | 1b2fdcb | 2014-07-16 17:44:34 -0700 | [diff] [blame] | 59 | // Set to default copy buffer size in frames for input processing. |
Mikhail Naganov | a4f00e2 | 2019-07-31 14:53:29 -0700 | [diff] [blame] | 60 | static constexpr size_t kCopyBufferFrameCount = 256; |
Andy Hung | 1b2fdcb | 2014-07-16 17:44:34 -0700 | [diff] [blame] | 61 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 62 | namespace android { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 63 | |
| 64 | // ---------------------------------------------------------------------------- |
Andy Hung | 1b2fdcb | 2014-07-16 17:44:34 -0700 | [diff] [blame] | 65 | |
Mikhail Naganov | 32f0d16 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 66 | bool AudioMixer::isValidChannelMask(audio_channel_mask_t channelMask) const { |
| 67 | return audio_channel_mask_is_valid(channelMask); // the RemixBufferProvider is flexible. |
Glenn Kasten | c5ac4cb | 2011-12-12 09:05:55 -0800 | [diff] [blame] | 68 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 69 | |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 70 | // Called when channel masks have changed for a track name |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 71 | // TODO: Fix DownmixerBufferProvider not to (possibly) change mixer input format, |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 72 | // which will simplify this logic. |
| 73 | bool AudioMixer::setChannelMasks(int name, |
| 74 | audio_channel_mask_t trackChannelMask, audio_channel_mask_t mixerChannelMask) { |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 75 | LOG_ALWAYS_FATAL_IF(!exists(name), "invalid name: %d", name); |
Mikhail Naganov | 32f0d16 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 76 | const std::shared_ptr<Track> &track = getTrack(name); |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 77 | |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 78 | if (trackChannelMask == (track->channelMask | track->mHapticChannelMask) |
| 79 | && mixerChannelMask == (track->mMixerChannelMask | track->mMixerHapticChannelMask)) { |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 80 | return false; // no need to change |
Jean-Michel Trivi | 9bd2322 | 2012-04-16 13:43:48 -0700 | [diff] [blame] | 81 | } |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 82 | const audio_channel_mask_t hapticChannelMask = trackChannelMask & AUDIO_CHANNEL_HAPTIC_ALL; |
| 83 | trackChannelMask &= ~AUDIO_CHANNEL_HAPTIC_ALL; |
| 84 | const audio_channel_mask_t mixerHapticChannelMask = mixerChannelMask & AUDIO_CHANNEL_HAPTIC_ALL; |
| 85 | mixerChannelMask &= ~AUDIO_CHANNEL_HAPTIC_ALL; |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 86 | // always recompute for both channel masks even if only one has changed. |
| 87 | const uint32_t trackChannelCount = audio_channel_count_from_out_mask(trackChannelMask); |
| 88 | const uint32_t mixerChannelCount = audio_channel_count_from_out_mask(mixerChannelMask); |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 89 | const uint32_t hapticChannelCount = audio_channel_count_from_out_mask(hapticChannelMask); |
| 90 | const uint32_t mixerHapticChannelCount = |
| 91 | audio_channel_count_from_out_mask(mixerHapticChannelMask); |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 92 | |
| 93 | ALOG_ASSERT((trackChannelCount <= MAX_NUM_CHANNELS_TO_DOWNMIX) |
| 94 | && trackChannelCount |
| 95 | && mixerChannelCount); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 96 | track->channelMask = trackChannelMask; |
| 97 | track->channelCount = trackChannelCount; |
| 98 | track->mMixerChannelMask = mixerChannelMask; |
| 99 | track->mMixerChannelCount = mixerChannelCount; |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 100 | track->mHapticChannelMask = hapticChannelMask; |
| 101 | track->mHapticChannelCount = hapticChannelCount; |
| 102 | track->mMixerHapticChannelMask = mixerHapticChannelMask; |
| 103 | track->mMixerHapticChannelCount = mixerHapticChannelCount; |
| 104 | |
| 105 | if (track->mHapticChannelCount > 0) { |
| 106 | track->mAdjustInChannelCount = track->channelCount + track->mHapticChannelCount; |
| 107 | track->mAdjustOutChannelCount = track->channelCount + track->mMixerHapticChannelCount; |
| 108 | track->mAdjustNonDestructiveInChannelCount = track->mAdjustOutChannelCount; |
| 109 | track->mAdjustNonDestructiveOutChannelCount = track->channelCount; |
| 110 | track->mKeepContractedChannels = track->mHapticPlaybackEnabled; |
| 111 | } else { |
| 112 | track->mAdjustInChannelCount = 0; |
| 113 | track->mAdjustOutChannelCount = 0; |
| 114 | track->mAdjustNonDestructiveInChannelCount = 0; |
| 115 | track->mAdjustNonDestructiveOutChannelCount = 0; |
| 116 | track->mKeepContractedChannels = false; |
| 117 | } |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 118 | |
| 119 | // channel masks have changed, does this track need a downmixer? |
| 120 | // update to try using our desired format (if we aren't already using it) |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 121 | const status_t status = track->prepareForDownmix(); |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 122 | ALOGE_IF(status != OK, |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 123 | "prepareForDownmix error %d, track channel mask %#x, mixer channel mask %#x", |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 124 | status, track->channelMask, track->mMixerChannelMask); |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 125 | |
Yung Ti Su | 1a0ecc3 | 2018-05-07 11:09:15 +0800 | [diff] [blame] | 126 | // always do reformat since channel mask changed, |
| 127 | // do it after downmix since track format may change! |
| 128 | track->prepareForReformat(); |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 129 | |
jiabin | dce8f8c | 2018-12-10 17:49:31 -0800 | [diff] [blame] | 130 | track->prepareForAdjustChannelsNonDestructive(mFrameCount); |
| 131 | track->prepareForAdjustChannels(); |
| 132 | |
Mikhail Naganov | 32f0d16 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 133 | // Resampler channels may have changed. |
| 134 | track->recreateResampler(mSampleRate); |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 135 | return true; |
| 136 | } |
| 137 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 138 | void AudioMixer::Track::unprepareForDownmix() { |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 139 | ALOGV("AudioMixer::unprepareForDownmix(%p)", this); |
Jean-Michel Trivi | 9bd2322 | 2012-04-16 13:43:48 -0700 | [diff] [blame] | 140 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 141 | if (mPostDownmixReformatBufferProvider.get() != nullptr) { |
Andy Hung | 8539589 | 2017-04-25 16:47:52 -0700 | [diff] [blame] | 142 | // release any buffers held by the mPostDownmixReformatBufferProvider |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 143 | // before deallocating the mDownmixerBufferProvider. |
Andy Hung | 8539589 | 2017-04-25 16:47:52 -0700 | [diff] [blame] | 144 | mPostDownmixReformatBufferProvider->reset(); |
| 145 | } |
| 146 | |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 147 | mDownmixRequiresFormat = AUDIO_FORMAT_INVALID; |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 148 | if (mDownmixerBufferProvider.get() != nullptr) { |
Jean-Michel Trivi | 9bd2322 | 2012-04-16 13:43:48 -0700 | [diff] [blame] | 149 | // this track had previously been configured with a downmixer, delete it |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 150 | mDownmixerBufferProvider.reset(nullptr); |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 151 | reconfigureBufferProviders(); |
Jean-Michel Trivi | 9bd2322 | 2012-04-16 13:43:48 -0700 | [diff] [blame] | 152 | } else { |
| 153 | ALOGV(" nothing to do, no downmixer to delete"); |
| 154 | } |
| 155 | } |
| 156 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 157 | status_t AudioMixer::Track::prepareForDownmix() |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 158 | { |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 159 | ALOGV("AudioMixer::prepareForDownmix(%p) with mask 0x%x", |
| 160 | this, channelMask); |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 161 | |
Jean-Michel Trivi | 9bd2322 | 2012-04-16 13:43:48 -0700 | [diff] [blame] | 162 | // discard the previous downmixer if there was one |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 163 | unprepareForDownmix(); |
Andy Hung | 73e62e2 | 2015-04-20 12:06:38 -0700 | [diff] [blame] | 164 | // MONO_HACK Only remix (upmix or downmix) if the track and mixer/device channel masks |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 165 | // are not the same and not handled internally, as mono -> stereo currently is. |
| 166 | if (channelMask == mMixerChannelMask |
| 167 | || (channelMask == AUDIO_CHANNEL_OUT_MONO |
| 168 | && mMixerChannelMask == AUDIO_CHANNEL_OUT_STEREO)) { |
| 169 | return NO_ERROR; |
| 170 | } |
Andy Hung | 650ceb9 | 2015-01-29 13:31:12 -0800 | [diff] [blame] | 171 | // DownmixerBufferProvider is only used for position masks. |
| 172 | if (audio_channel_mask_get_representation(channelMask) |
| 173 | == AUDIO_CHANNEL_REPRESENTATION_POSITION |
| 174 | && DownmixerBufferProvider::isMultichannelCapable()) { |
Andy Hung | 6694255 | 2018-12-21 16:07:12 -0800 | [diff] [blame] | 175 | |
| 176 | // Check if we have a float or int16 downmixer, in that order. |
| 177 | for (const audio_format_t format : { AUDIO_FORMAT_PCM_FLOAT, AUDIO_FORMAT_PCM_16_BIT }) { |
| 178 | mDownmixerBufferProvider.reset(new DownmixerBufferProvider( |
| 179 | channelMask, mMixerChannelMask, |
| 180 | format, |
| 181 | sampleRate, sessionId, kCopyBufferFrameCount)); |
| 182 | if (static_cast<DownmixerBufferProvider *>(mDownmixerBufferProvider.get()) |
| 183 | ->isValid()) { |
| 184 | mDownmixRequiresFormat = format; |
| 185 | reconfigureBufferProviders(); |
| 186 | return NO_ERROR; |
| 187 | } |
Andy Hung | 34803d5 | 2014-07-16 21:41:35 -0700 | [diff] [blame] | 188 | } |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 189 | // mDownmixerBufferProvider reset below. |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 190 | } |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 191 | |
| 192 | // Effect downmixer does not accept the channel conversion. Let's use our remixer. |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 193 | mDownmixerBufferProvider.reset(new RemixBufferProvider(channelMask, |
| 194 | mMixerChannelMask, mMixerInFormat, kCopyBufferFrameCount)); |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 195 | // Remix always finds a conversion whereas Downmixer effect above may fail. |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 196 | reconfigureBufferProviders(); |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 197 | return NO_ERROR; |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 200 | void AudioMixer::Track::unprepareForReformat() { |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 201 | ALOGV("AudioMixer::unprepareForReformat(%p)", this); |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 202 | bool requiresReconfigure = false; |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 203 | if (mReformatBufferProvider.get() != nullptr) { |
| 204 | mReformatBufferProvider.reset(nullptr); |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 205 | requiresReconfigure = true; |
| 206 | } |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 207 | if (mPostDownmixReformatBufferProvider.get() != nullptr) { |
| 208 | mPostDownmixReformatBufferProvider.reset(nullptr); |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 209 | requiresReconfigure = true; |
| 210 | } |
| 211 | if (requiresReconfigure) { |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 212 | reconfigureBufferProviders(); |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 216 | status_t AudioMixer::Track::prepareForReformat() |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 217 | { |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 218 | ALOGV("AudioMixer::prepareForReformat(%p) with format %#x", this, mFormat); |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 219 | // discard previous reformatters |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 220 | unprepareForReformat(); |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 221 | // only configure reformatters as needed |
| 222 | const audio_format_t targetFormat = mDownmixRequiresFormat != AUDIO_FORMAT_INVALID |
| 223 | ? mDownmixRequiresFormat : mMixerInFormat; |
| 224 | bool requiresReconfigure = false; |
| 225 | if (mFormat != targetFormat) { |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 226 | mReformatBufferProvider.reset(new ReformatBufferProvider( |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 227 | audio_channel_count_from_out_mask(channelMask), |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 228 | mFormat, |
| 229 | targetFormat, |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 230 | kCopyBufferFrameCount)); |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 231 | requiresReconfigure = true; |
Kevin Rocard | e053bfa | 2017-11-09 22:07:34 -0800 | [diff] [blame] | 232 | } else if (mFormat == AUDIO_FORMAT_PCM_FLOAT) { |
| 233 | // Input and output are floats, make sure application did not provide > 3db samples |
| 234 | // that would break volume application (b/68099072) |
| 235 | // TODO: add a trusted source flag to avoid the overhead |
| 236 | mReformatBufferProvider.reset(new ClampFloatBufferProvider( |
| 237 | audio_channel_count_from_out_mask(channelMask), |
| 238 | kCopyBufferFrameCount)); |
| 239 | requiresReconfigure = true; |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 240 | } |
| 241 | if (targetFormat != mMixerInFormat) { |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 242 | mPostDownmixReformatBufferProvider.reset(new ReformatBufferProvider( |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 243 | audio_channel_count_from_out_mask(mMixerChannelMask), |
| 244 | targetFormat, |
| 245 | mMixerInFormat, |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 246 | kCopyBufferFrameCount)); |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 247 | requiresReconfigure = true; |
| 248 | } |
| 249 | if (requiresReconfigure) { |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 250 | reconfigureBufferProviders(); |
Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 251 | } |
| 252 | return NO_ERROR; |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 253 | } |
| 254 | |
jiabin | dce8f8c | 2018-12-10 17:49:31 -0800 | [diff] [blame] | 255 | void AudioMixer::Track::unprepareForAdjustChannels() |
| 256 | { |
| 257 | ALOGV("AUDIOMIXER::unprepareForAdjustChannels"); |
| 258 | if (mAdjustChannelsBufferProvider.get() != nullptr) { |
| 259 | mAdjustChannelsBufferProvider.reset(nullptr); |
| 260 | reconfigureBufferProviders(); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | status_t AudioMixer::Track::prepareForAdjustChannels() |
| 265 | { |
| 266 | ALOGV("AudioMixer::prepareForAdjustChannels(%p) with inChannelCount: %u, outChannelCount: %u", |
| 267 | this, mAdjustInChannelCount, mAdjustOutChannelCount); |
| 268 | unprepareForAdjustChannels(); |
| 269 | if (mAdjustInChannelCount != mAdjustOutChannelCount) { |
| 270 | mAdjustChannelsBufferProvider.reset(new AdjustChannelsBufferProvider( |
| 271 | mFormat, mAdjustInChannelCount, mAdjustOutChannelCount, kCopyBufferFrameCount)); |
| 272 | reconfigureBufferProviders(); |
| 273 | } |
| 274 | return NO_ERROR; |
| 275 | } |
| 276 | |
| 277 | void AudioMixer::Track::unprepareForAdjustChannelsNonDestructive() |
| 278 | { |
| 279 | ALOGV("AUDIOMIXER::unprepareForAdjustChannelsNonDestructive"); |
jiabin | ea8fa7a | 2019-02-22 14:41:50 -0800 | [diff] [blame] | 280 | if (mContractChannelsNonDestructiveBufferProvider.get() != nullptr) { |
| 281 | mContractChannelsNonDestructiveBufferProvider.reset(nullptr); |
jiabin | dce8f8c | 2018-12-10 17:49:31 -0800 | [diff] [blame] | 282 | reconfigureBufferProviders(); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | status_t AudioMixer::Track::prepareForAdjustChannelsNonDestructive(size_t frames) |
| 287 | { |
| 288 | ALOGV("AudioMixer::prepareForAdjustChannelsNonDestructive(%p) with inChannelCount: %u, " |
| 289 | "outChannelCount: %u, keepContractedChannels: %d", |
| 290 | this, mAdjustNonDestructiveInChannelCount, mAdjustNonDestructiveOutChannelCount, |
| 291 | mKeepContractedChannels); |
| 292 | unprepareForAdjustChannelsNonDestructive(); |
| 293 | if (mAdjustNonDestructiveInChannelCount != mAdjustNonDestructiveOutChannelCount) { |
| 294 | uint8_t* buffer = mKeepContractedChannels |
| 295 | ? (uint8_t*)mainBuffer + frames * audio_bytes_per_frame( |
| 296 | mMixerChannelCount, mMixerFormat) |
| 297 | : NULL; |
jiabin | ea8fa7a | 2019-02-22 14:41:50 -0800 | [diff] [blame] | 298 | mContractChannelsNonDestructiveBufferProvider.reset( |
| 299 | new AdjustChannelsBufferProvider( |
jiabin | dce8f8c | 2018-12-10 17:49:31 -0800 | [diff] [blame] | 300 | mFormat, |
| 301 | mAdjustNonDestructiveInChannelCount, |
| 302 | mAdjustNonDestructiveOutChannelCount, |
jiabin | dce8f8c | 2018-12-10 17:49:31 -0800 | [diff] [blame] | 303 | frames, |
jiabin | ea8fa7a | 2019-02-22 14:41:50 -0800 | [diff] [blame] | 304 | mKeepContractedChannels ? mMixerFormat : AUDIO_FORMAT_INVALID, |
jiabin | dce8f8c | 2018-12-10 17:49:31 -0800 | [diff] [blame] | 305 | buffer)); |
| 306 | reconfigureBufferProviders(); |
| 307 | } |
| 308 | return NO_ERROR; |
| 309 | } |
| 310 | |
| 311 | void AudioMixer::Track::clearContractedBuffer() |
| 312 | { |
jiabin | ea8fa7a | 2019-02-22 14:41:50 -0800 | [diff] [blame] | 313 | if (mContractChannelsNonDestructiveBufferProvider.get() != nullptr) { |
| 314 | static_cast<AdjustChannelsBufferProvider*>( |
| 315 | mContractChannelsNonDestructiveBufferProvider.get())->clearContractedFrames(); |
jiabin | dce8f8c | 2018-12-10 17:49:31 -0800 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 319 | void AudioMixer::Track::reconfigureBufferProviders() |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 320 | { |
Andy Hung | 3a34df9 | 2018-08-21 12:32:30 -0700 | [diff] [blame] | 321 | // configure from upstream to downstream buffer providers. |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 322 | bufferProvider = mInputBufferProvider; |
jiabin | dce8f8c | 2018-12-10 17:49:31 -0800 | [diff] [blame] | 323 | if (mAdjustChannelsBufferProvider.get() != nullptr) { |
| 324 | mAdjustChannelsBufferProvider->setBufferProvider(bufferProvider); |
| 325 | bufferProvider = mAdjustChannelsBufferProvider.get(); |
| 326 | } |
jiabin | ea8fa7a | 2019-02-22 14:41:50 -0800 | [diff] [blame] | 327 | if (mContractChannelsNonDestructiveBufferProvider.get() != nullptr) { |
| 328 | mContractChannelsNonDestructiveBufferProvider->setBufferProvider(bufferProvider); |
| 329 | bufferProvider = mContractChannelsNonDestructiveBufferProvider.get(); |
jiabin | dce8f8c | 2018-12-10 17:49:31 -0800 | [diff] [blame] | 330 | } |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 331 | if (mReformatBufferProvider.get() != nullptr) { |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 332 | mReformatBufferProvider->setBufferProvider(bufferProvider); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 333 | bufferProvider = mReformatBufferProvider.get(); |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 334 | } |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 335 | if (mDownmixerBufferProvider.get() != nullptr) { |
| 336 | mDownmixerBufferProvider->setBufferProvider(bufferProvider); |
| 337 | bufferProvider = mDownmixerBufferProvider.get(); |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 338 | } |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 339 | if (mPostDownmixReformatBufferProvider.get() != nullptr) { |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 340 | mPostDownmixReformatBufferProvider->setBufferProvider(bufferProvider); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 341 | bufferProvider = mPostDownmixReformatBufferProvider.get(); |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 342 | } |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 343 | if (mTimestretchBufferProvider.get() != nullptr) { |
Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 344 | mTimestretchBufferProvider->setBufferProvider(bufferProvider); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 345 | bufferProvider = mTimestretchBufferProvider.get(); |
Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 346 | } |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 349 | void AudioMixer::setParameter(int name, int target, int param, void *value) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 350 | { |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 351 | LOG_ALWAYS_FATAL_IF(!exists(name), "invalid name: %d", name); |
Mikhail Naganov | 32f0d16 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 352 | const std::shared_ptr<Track> &track = getTrack(name); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 353 | |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 354 | int valueInt = static_cast<int>(reinterpret_cast<uintptr_t>(value)); |
| 355 | int32_t *valueBuf = reinterpret_cast<int32_t*>(value); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 356 | |
| 357 | switch (target) { |
Glenn Kasten | 788040c | 2011-05-05 08:19:00 -0700 | [diff] [blame] | 358 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 359 | case TRACK: |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 360 | switch (param) { |
Glenn Kasten | 788040c | 2011-05-05 08:19:00 -0700 | [diff] [blame] | 361 | case CHANNEL_MASK: { |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 362 | const audio_channel_mask_t trackChannelMask = |
| 363 | static_cast<audio_channel_mask_t>(valueInt); |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 364 | if (setChannelMasks(name, trackChannelMask, |
| 365 | (track->mMixerChannelMask | track->mMixerHapticChannelMask))) { |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 366 | ALOGV("setParameter(TRACK, CHANNEL_MASK, %x)", trackChannelMask); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 367 | invalidate(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 368 | } |
Glenn Kasten | 788040c | 2011-05-05 08:19:00 -0700 | [diff] [blame] | 369 | } break; |
| 370 | case MAIN_BUFFER: |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 371 | if (track->mainBuffer != valueBuf) { |
| 372 | track->mainBuffer = valueBuf; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 373 | ALOGV("setParameter(TRACK, MAIN_BUFFER, %p)", valueBuf); |
jiabin | dce8f8c | 2018-12-10 17:49:31 -0800 | [diff] [blame] | 374 | if (track->mKeepContractedChannels) { |
| 375 | track->prepareForAdjustChannelsNonDestructive(mFrameCount); |
| 376 | } |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 377 | invalidate(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 378 | } |
Glenn Kasten | 788040c | 2011-05-05 08:19:00 -0700 | [diff] [blame] | 379 | break; |
| 380 | case AUX_BUFFER: |
Mikhail Naganov | 32f0d16 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 381 | AudioMixerBase::setParameter(name, target, param, value); |
Glenn Kasten | 788040c | 2011-05-05 08:19:00 -0700 | [diff] [blame] | 382 | break; |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 383 | case FORMAT: { |
| 384 | audio_format_t format = static_cast<audio_format_t>(valueInt); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 385 | if (track->mFormat != format) { |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 386 | ALOG_ASSERT(audio_is_linear_pcm(format), "Invalid format %#x", format); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 387 | track->mFormat = format; |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 388 | ALOGV("setParameter(TRACK, FORMAT, %#x)", format); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 389 | track->prepareForReformat(); |
| 390 | invalidate(); |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 391 | } |
| 392 | } break; |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 393 | // FIXME do we want to support setting the downmix type from AudioFlinger? |
| 394 | // for a specific track? or per mixer? |
| 395 | /* case DOWNMIX_TYPE: |
| 396 | break */ |
Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 397 | case MIXER_FORMAT: { |
Andy Hung | a1ab7cc | 2014-02-24 19:26:52 -0800 | [diff] [blame] | 398 | audio_format_t format = static_cast<audio_format_t>(valueInt); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 399 | if (track->mMixerFormat != format) { |
| 400 | track->mMixerFormat = format; |
Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 401 | ALOGV("setParameter(TRACK, MIXER_FORMAT, %#x)", format); |
jiabin | dce8f8c | 2018-12-10 17:49:31 -0800 | [diff] [blame] | 402 | if (track->mKeepContractedChannels) { |
| 403 | track->prepareForAdjustChannelsNonDestructive(mFrameCount); |
| 404 | } |
Andy Hung | a1ab7cc | 2014-02-24 19:26:52 -0800 | [diff] [blame] | 405 | } |
| 406 | } break; |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 407 | case MIXER_CHANNEL_MASK: { |
| 408 | const audio_channel_mask_t mixerChannelMask = |
| 409 | static_cast<audio_channel_mask_t>(valueInt); |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 410 | if (setChannelMasks(name, track->channelMask | track->mHapticChannelMask, |
| 411 | mixerChannelMask)) { |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 412 | ALOGV("setParameter(TRACK, MIXER_CHANNEL_MASK, %#x)", mixerChannelMask); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 413 | invalidate(); |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 414 | } |
| 415 | } break; |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 416 | case HAPTIC_ENABLED: { |
| 417 | const bool hapticPlaybackEnabled = static_cast<bool>(valueInt); |
| 418 | if (track->mHapticPlaybackEnabled != hapticPlaybackEnabled) { |
| 419 | track->mHapticPlaybackEnabled = hapticPlaybackEnabled; |
| 420 | track->mKeepContractedChannels = hapticPlaybackEnabled; |
| 421 | track->prepareForAdjustChannelsNonDestructive(mFrameCount); |
| 422 | track->prepareForAdjustChannels(); |
| 423 | } |
| 424 | } break; |
jiabin | 77270b8 | 2018-12-18 15:41:29 -0800 | [diff] [blame] | 425 | case HAPTIC_INTENSITY: { |
| 426 | const haptic_intensity_t hapticIntensity = static_cast<haptic_intensity_t>(valueInt); |
| 427 | if (track->mHapticIntensity != hapticIntensity) { |
| 428 | track->mHapticIntensity = hapticIntensity; |
| 429 | } |
| 430 | } break; |
Glenn Kasten | 788040c | 2011-05-05 08:19:00 -0700 | [diff] [blame] | 431 | default: |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 432 | LOG_ALWAYS_FATAL("setParameter track: bad param %d", param); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 433 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 434 | break; |
Glenn Kasten | 788040c | 2011-05-05 08:19:00 -0700 | [diff] [blame] | 435 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 436 | case RESAMPLE: |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 437 | case RAMP_VOLUME: |
| 438 | case VOLUME: |
Mikhail Naganov | 32f0d16 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 439 | AudioMixerBase::setParameter(name, target, param, value); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 440 | break; |
Mikhail Naganov | a4f00e2 | 2019-07-31 14:53:29 -0700 | [diff] [blame] | 441 | case TIMESTRETCH: |
| 442 | switch (param) { |
| 443 | case PLAYBACK_RATE: { |
| 444 | const AudioPlaybackRate *playbackRate = |
| 445 | reinterpret_cast<AudioPlaybackRate*>(value); |
| 446 | ALOGW_IF(!isAudioPlaybackRateValid(*playbackRate), |
| 447 | "bad parameters speed %f, pitch %f", |
| 448 | playbackRate->mSpeed, playbackRate->mPitch); |
| 449 | if (track->setPlaybackRate(*playbackRate)) { |
| 450 | ALOGV("setParameter(TIMESTRETCH, PLAYBACK_RATE, STRETCH_MODE, FALLBACK_MODE " |
| 451 | "%f %f %d %d", |
| 452 | playbackRate->mSpeed, |
| 453 | playbackRate->mPitch, |
| 454 | playbackRate->mStretchMode, |
| 455 | playbackRate->mFallbackMode); |
| 456 | // invalidate(); (should not require reconfigure) |
Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 457 | } |
Mikhail Naganov | a4f00e2 | 2019-07-31 14:53:29 -0700 | [diff] [blame] | 458 | } break; |
| 459 | default: |
| 460 | LOG_ALWAYS_FATAL("setParameter timestretch: bad param %d", param); |
| 461 | } |
| 462 | break; |
Glenn Kasten | 788040c | 2011-05-05 08:19:00 -0700 | [diff] [blame] | 463 | |
| 464 | default: |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 465 | LOG_ALWAYS_FATAL("setParameter: bad target %d", target); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 466 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 467 | } |
| 468 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 469 | bool AudioMixer::Track::setPlaybackRate(const AudioPlaybackRate &playbackRate) |
Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 470 | { |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 471 | if ((mTimestretchBufferProvider.get() == nullptr && |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 472 | fabs(playbackRate.mSpeed - mPlaybackRate.mSpeed) < AUDIO_TIMESTRETCH_SPEED_MIN_DELTA && |
| 473 | fabs(playbackRate.mPitch - mPlaybackRate.mPitch) < AUDIO_TIMESTRETCH_PITCH_MIN_DELTA) || |
| 474 | isAudioPlaybackRateEqual(playbackRate, mPlaybackRate)) { |
Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 475 | return false; |
| 476 | } |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 477 | mPlaybackRate = playbackRate; |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 478 | if (mTimestretchBufferProvider.get() == nullptr) { |
Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 479 | // TODO: Remove MONO_HACK. Resampler sees #channels after the downmixer |
| 480 | // but if none exists, it is the channel count (1 for mono). |
Mikhail Naganov | 32f0d16 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 481 | const int timestretchChannelCount = getOutputChannelCount(); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 482 | mTimestretchBufferProvider.reset(new TimestretchBufferProvider(timestretchChannelCount, |
| 483 | mMixerInFormat, sampleRate, playbackRate)); |
Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 484 | reconfigureBufferProviders(); |
| 485 | } else { |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 486 | static_cast<TimestretchBufferProvider*>(mTimestretchBufferProvider.get()) |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 487 | ->setPlaybackRate(playbackRate); |
Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 488 | } |
| 489 | return true; |
| 490 | } |
| 491 | |
Glenn Kasten | 01c4ebf | 2012-02-22 10:47:35 -0800 | [diff] [blame] | 492 | void AudioMixer::setBufferProvider(int name, AudioBufferProvider* bufferProvider) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 493 | { |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 494 | LOG_ALWAYS_FATAL_IF(!exists(name), "invalid name: %d", name); |
Mikhail Naganov | 32f0d16 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 495 | const std::shared_ptr<Track> &track = getTrack(name); |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 496 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 497 | if (track->mInputBufferProvider == bufferProvider) { |
Andy Hung | 1d26ddf | 2014-05-29 15:53:09 -0700 | [diff] [blame] | 498 | return; // don't reset any buffer providers if identical. |
| 499 | } |
Andy Hung | 3a34df9 | 2018-08-21 12:32:30 -0700 | [diff] [blame] | 500 | // reset order from downstream to upstream buffer providers. |
| 501 | if (track->mTimestretchBufferProvider.get() != nullptr) { |
| 502 | track->mTimestretchBufferProvider->reset(); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 503 | } else if (track->mPostDownmixReformatBufferProvider.get() != nullptr) { |
| 504 | track->mPostDownmixReformatBufferProvider->reset(); |
Andy Hung | 3a34df9 | 2018-08-21 12:32:30 -0700 | [diff] [blame] | 505 | } else if (track->mDownmixerBufferProvider != nullptr) { |
| 506 | track->mDownmixerBufferProvider->reset(); |
| 507 | } else if (track->mReformatBufferProvider.get() != nullptr) { |
| 508 | track->mReformatBufferProvider->reset(); |
jiabin | ea8fa7a | 2019-02-22 14:41:50 -0800 | [diff] [blame] | 509 | } else if (track->mContractChannelsNonDestructiveBufferProvider.get() != nullptr) { |
| 510 | track->mContractChannelsNonDestructiveBufferProvider->reset(); |
jiabin | dce8f8c | 2018-12-10 17:49:31 -0800 | [diff] [blame] | 511 | } else if (track->mAdjustChannelsBufferProvider.get() != nullptr) { |
| 512 | track->mAdjustChannelsBufferProvider->reset(); |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 513 | } |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 514 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 515 | track->mInputBufferProvider = bufferProvider; |
| 516 | track->reconfigureBufferProviders(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 517 | } |
| 518 | |
Glenn Kasten | 52008f8 | 2012-03-18 09:34:41 -0700 | [diff] [blame] | 519 | /*static*/ pthread_once_t AudioMixer::sOnceControl = PTHREAD_ONCE_INIT; |
| 520 | |
| 521 | /*static*/ void AudioMixer::sInitRoutine() |
| 522 | { |
Andy Hung | 34803d5 | 2014-07-16 21:41:35 -0700 | [diff] [blame] | 523 | DownmixerBufferProvider::init(); // for the downmixer |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 524 | } |
| 525 | |
Mikhail Naganov | 32f0d16 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 526 | std::shared_ptr<AudioMixerBase::TrackBase> AudioMixer::preCreateTrack() |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 527 | { |
Mikhail Naganov | 32f0d16 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 528 | return std::make_shared<Track>(); |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Mikhail Naganov | 32f0d16 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 531 | status_t AudioMixer::postCreateTrack(TrackBase *track) |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 532 | { |
Mikhail Naganov | 32f0d16 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 533 | Track* t = static_cast<Track*>(track); |
| 534 | |
| 535 | audio_channel_mask_t channelMask = t->channelMask; |
| 536 | t->mHapticChannelMask = channelMask & AUDIO_CHANNEL_HAPTIC_ALL; |
| 537 | t->mHapticChannelCount = audio_channel_count_from_out_mask(t->mHapticChannelMask); |
| 538 | channelMask &= ~AUDIO_CHANNEL_HAPTIC_ALL; |
| 539 | t->channelCount = audio_channel_count_from_out_mask(channelMask); |
| 540 | ALOGV_IF(audio_channel_mask_get_bits(channelMask) != AUDIO_CHANNEL_OUT_STEREO, |
| 541 | "Non-stereo channel mask: %d\n", channelMask); |
| 542 | t->channelMask = channelMask; |
| 543 | t->mInputBufferProvider = NULL; |
| 544 | t->mDownmixRequiresFormat = AUDIO_FORMAT_INVALID; // no format required |
| 545 | t->mPlaybackRate = AUDIO_PLAYBACK_RATE_DEFAULT; |
| 546 | // haptic |
| 547 | t->mHapticPlaybackEnabled = false; |
| 548 | t->mHapticIntensity = HAPTIC_SCALE_NONE; |
| 549 | t->mMixerHapticChannelMask = AUDIO_CHANNEL_NONE; |
| 550 | t->mMixerHapticChannelCount = 0; |
| 551 | t->mAdjustInChannelCount = t->channelCount + t->mHapticChannelCount; |
| 552 | t->mAdjustOutChannelCount = t->channelCount + t->mMixerHapticChannelCount; |
| 553 | t->mAdjustNonDestructiveInChannelCount = t->mAdjustOutChannelCount; |
| 554 | t->mAdjustNonDestructiveOutChannelCount = t->channelCount; |
| 555 | t->mKeepContractedChannels = false; |
| 556 | // Check the downmixing (or upmixing) requirements. |
| 557 | status_t status = t->prepareForDownmix(); |
| 558 | if (status != OK) { |
| 559 | ALOGE("AudioMixer::getTrackName invalid channelMask (%#x)", channelMask); |
| 560 | return BAD_VALUE; |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 561 | } |
Mikhail Naganov | 32f0d16 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 562 | // prepareForDownmix() may change mDownmixRequiresFormat |
| 563 | ALOGVV("mMixerFormat:%#x mMixerInFormat:%#x\n", t->mMixerFormat, t->mMixerInFormat); |
| 564 | t->prepareForReformat(); |
| 565 | t->prepareForAdjustChannelsNonDestructive(mFrameCount); |
| 566 | t->prepareForAdjustChannels(); |
| 567 | return OK; |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 568 | } |
| 569 | |
Mikhail Naganov | 32f0d16 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 570 | void AudioMixer::preProcess() |
Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 571 | { |
Mikhail Naganov | 32f0d16 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 572 | for (const auto &pair : mTracks) { |
| 573 | // Clear contracted buffer before processing if contracted channels are saved |
| 574 | const std::shared_ptr<TrackBase> &tb = pair.second; |
| 575 | Track *t = static_cast<Track*>(tb.get()); |
| 576 | if (t->mKeepContractedChannels) { |
| 577 | t->clearContractedBuffer(); |
Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 578 | } |
| 579 | } |
| 580 | } |
| 581 | |
Mikhail Naganov | 32f0d16 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 582 | void AudioMixer::postProcess() |
Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 583 | { |
Mikhail Naganov | 32f0d16 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 584 | // Process haptic data. |
jiabin | 77270b8 | 2018-12-18 15:41:29 -0800 | [diff] [blame] | 585 | // Need to keep consistent with VibrationEffect.scale(int, float, int) |
| 586 | for (const auto &pair : mGroups) { |
| 587 | // process by group of tracks with same output main buffer. |
| 588 | const auto &group = pair.second; |
| 589 | for (const int name : group) { |
Mikhail Naganov | 32f0d16 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 590 | const std::shared_ptr<Track> &t = getTrack(name); |
jiabin | 77270b8 | 2018-12-18 15:41:29 -0800 | [diff] [blame] | 591 | if (t->mHapticPlaybackEnabled) { |
| 592 | size_t sampleCount = mFrameCount * t->mMixerHapticChannelCount; |
| 593 | float gamma = t->getHapticScaleGamma(); |
| 594 | float maxAmplitudeRatio = t->getHapticMaxAmplitudeRatio(); |
| 595 | uint8_t* buffer = (uint8_t*)pair.first + mFrameCount * audio_bytes_per_frame( |
| 596 | t->mMixerChannelCount, t->mMixerFormat); |
| 597 | switch (t->mMixerFormat) { |
| 598 | // Mixer format should be AUDIO_FORMAT_PCM_FLOAT. |
| 599 | case AUDIO_FORMAT_PCM_FLOAT: { |
| 600 | float* fout = (float*) buffer; |
| 601 | for (size_t i = 0; i < sampleCount; i++) { |
| 602 | float mul = fout[i] >= 0 ? 1.0 : -1.0; |
| 603 | fout[i] = powf(fabsf(fout[i] / HAPTIC_MAX_AMPLITUDE_FLOAT), gamma) |
| 604 | * maxAmplitudeRatio * HAPTIC_MAX_AMPLITUDE_FLOAT * mul; |
| 605 | } |
| 606 | } break; |
| 607 | default: |
| 608 | LOG_ALWAYS_FATAL("bad mMixerFormat: %#x", t->mMixerFormat); |
| 609 | break; |
| 610 | } |
| 611 | break; |
| 612 | } |
| 613 | } |
| 614 | } |
| 615 | } |
| 616 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 617 | // ---------------------------------------------------------------------------- |
Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 618 | } // namespace android |