| 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 | |
| 21 | #include <stdint.h> |
| 22 | #include <string.h> |
| 23 | #include <stdlib.h> |
| Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 24 | #include <math.h> |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 25 | #include <sys/types.h> |
| 26 | |
| 27 | #include <utils/Errors.h> |
| 28 | #include <utils/Log.h> |
| 29 | |
| Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 30 | #include <cutils/compiler.h> |
| Glenn Kasten | 5798d4e | 2012-03-08 12:18:35 -0800 | [diff] [blame] | 31 | #include <utils/Debug.h> |
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 32 | |
| 33 | #include <system/audio.h> |
| 34 | |
| Glenn Kasten | 3b21c50 | 2011-12-15 09:52:39 -0800 | [diff] [blame] | 35 | #include <audio_utils/primitives.h> |
| Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 36 | #include <audio_utils/format.h> |
| Andy Hung | 068561c | 2017-01-03 17:09:32 -0800 | [diff] [blame] | 37 | #include <media/AudioMixer.h> |
| Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 38 | |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 39 | #include "AudioMixerOps.h" |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 40 | |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 41 | // 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] | 42 | #ifndef FCC_2 |
| 43 | #define FCC_2 2 |
| 44 | #endif |
| 45 | |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 46 | // Look for MONO_HACK for any Mono hack involving legacy mono channel to |
| 47 | // stereo channel conversion. |
| 48 | |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 49 | /* VERY_VERY_VERBOSE_LOGGING will show exactly which process hook and track hook is |
| 50 | * being used. This is a considerable amount of log spam, so don't enable unless you |
| 51 | * are verifying the hook based code. |
| 52 | */ |
| 53 | //#define VERY_VERY_VERBOSE_LOGGING |
| 54 | #ifdef VERY_VERY_VERBOSE_LOGGING |
| 55 | #define ALOGVV ALOGV |
| 56 | //define ALOGVV printf // for test-mixer.cpp |
| 57 | #else |
| 58 | #define ALOGVV(a...) do { } while (0) |
| 59 | #endif |
| 60 | |
| Andy Hung | a08810b | 2014-07-16 21:53:43 -0700 | [diff] [blame] | 61 | #ifndef ARRAY_SIZE |
| 62 | #define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0])) |
| 63 | #endif |
| 64 | |
| Andy Hung | 5b8fde7 | 2014-09-02 21:14:34 -0700 | [diff] [blame] | 65 | // Set kUseNewMixer to true to use the new mixer engine always. Otherwise the |
| 66 | // original code will be used for stereo sinks, the new mixer for multichannel. |
| Andy Hung | 116a498 | 2017-11-30 10:15:08 -0800 | [diff] [blame] | 67 | static constexpr bool kUseNewMixer = true; |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 68 | |
| 69 | // Set kUseFloat to true to allow floating input into the mixer engine. |
| 70 | // If kUseNewMixer is false, this is ignored or may be overridden internally |
| 71 | // because of downmix/upmix support. |
| Andy Hung | 116a498 | 2017-11-30 10:15:08 -0800 | [diff] [blame] | 72 | static constexpr bool kUseFloat = true; |
| 73 | |
| 74 | #ifdef FLOAT_AUX |
| 75 | using TYPE_AUX = float; |
| 76 | static_assert(kUseNewMixer && kUseFloat, |
| 77 | "kUseNewMixer and kUseFloat must be true for FLOAT_AUX option"); |
| 78 | #else |
| 79 | using TYPE_AUX = int32_t; // q4.27 |
| 80 | #endif |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 81 | |
| Andy Hung | 1b2fdcb | 2014-07-16 17:44:34 -0700 | [diff] [blame] | 82 | // Set to default copy buffer size in frames for input processing. |
| 83 | static const size_t kCopyBufferFrameCount = 256; |
| 84 | |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 85 | namespace android { |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 86 | |
| 87 | // ---------------------------------------------------------------------------- |
| Andy Hung | 1b2fdcb | 2014-07-16 17:44:34 -0700 | [diff] [blame] | 88 | |
| Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 89 | static inline audio_format_t selectMixerInFormat(audio_format_t inputFormat __unused) { |
| 90 | return kUseFloat && kUseNewMixer ? AUDIO_FORMAT_PCM_FLOAT : AUDIO_FORMAT_PCM_16_BIT; |
| 91 | } |
| 92 | |
| Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 93 | status_t AudioMixer::create( |
| 94 | int name, audio_channel_mask_t channelMask, audio_format_t format, int sessionId) |
| Glenn Kasten | c5ac4cb | 2011-12-12 09:05:55 -0800 | [diff] [blame] | 95 | { |
| Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 96 | LOG_ALWAYS_FATAL_IF(exists(name), "name %d already exists", name); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 97 | |
| Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 98 | if (!isValidChannelMask(channelMask)) { |
| 99 | ALOGE("%s invalid channelMask: %#x", __func__, channelMask); |
| 100 | return BAD_VALUE; |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 101 | } |
| Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 102 | if (!isValidFormat(format)) { |
| 103 | ALOGE("%s invalid format: %#x", __func__, format); |
| 104 | return BAD_VALUE; |
| 105 | } |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 106 | |
| 107 | auto t = std::make_shared<Track>(); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 108 | { |
| 109 | // TODO: move initialization to the Track constructor. |
| Glenn Kasten | deeb128 | 2012-03-25 11:59:31 -0700 | [diff] [blame] | 110 | // assume default parameters for the track, except where noted below |
| Glenn Kasten | deeb128 | 2012-03-25 11:59:31 -0700 | [diff] [blame] | 111 | t->needs = 0; |
| Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 112 | |
| 113 | // Integer volume. |
| 114 | // Currently integer volume is kept for the legacy integer mixer. |
| 115 | // Will be removed when the legacy mixer path is removed. |
| Andy Hung | 97ae824 | 2014-05-30 10:35:47 -0700 | [diff] [blame] | 116 | t->volume[0] = UNITY_GAIN_INT; |
| 117 | t->volume[1] = UNITY_GAIN_INT; |
| Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 118 | t->prevVolume[0] = UNITY_GAIN_INT << 16; |
| 119 | t->prevVolume[1] = UNITY_GAIN_INT << 16; |
| Glenn Kasten | deeb128 | 2012-03-25 11:59:31 -0700 | [diff] [blame] | 120 | t->volumeInc[0] = 0; |
| 121 | t->volumeInc[1] = 0; |
| 122 | t->auxLevel = 0; |
| 123 | t->auxInc = 0; |
| Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 124 | t->prevAuxLevel = 0; |
| 125 | |
| 126 | // Floating point volume. |
| 127 | t->mVolume[0] = UNITY_GAIN_FLOAT; |
| 128 | t->mVolume[1] = UNITY_GAIN_FLOAT; |
| 129 | t->mPrevVolume[0] = UNITY_GAIN_FLOAT; |
| 130 | t->mPrevVolume[1] = UNITY_GAIN_FLOAT; |
| 131 | t->mVolumeInc[0] = 0.; |
| 132 | t->mVolumeInc[1] = 0.; |
| 133 | t->mAuxLevel = 0.; |
| 134 | t->mAuxInc = 0.; |
| 135 | t->mPrevAuxLevel = 0.; |
| 136 | |
| Glenn Kasten | deeb128 | 2012-03-25 11:59:31 -0700 | [diff] [blame] | 137 | // no initialization needed |
| Glenn Kasten | deeb128 | 2012-03-25 11:59:31 -0700 | [diff] [blame] | 138 | // t->frameCount |
| Andy Hung | 68112fc | 2014-05-14 14:13:23 -0700 | [diff] [blame] | 139 | t->channelCount = audio_channel_count_from_out_mask(channelMask); |
| Glenn Kasten | deeb128 | 2012-03-25 11:59:31 -0700 | [diff] [blame] | 140 | t->enabled = false; |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 141 | ALOGV_IF(audio_channel_mask_get_bits(channelMask) != AUDIO_CHANNEL_OUT_STEREO, |
| Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 142 | "Non-stereo channel mask: %d\n", channelMask); |
| Andy Hung | 68112fc | 2014-05-14 14:13:23 -0700 | [diff] [blame] | 143 | t->channelMask = channelMask; |
| Jean-Michel Trivi | d06e132 | 2012-09-12 15:47:07 -0700 | [diff] [blame] | 144 | t->sessionId = sessionId; |
| Glenn Kasten | deeb128 | 2012-03-25 11:59:31 -0700 | [diff] [blame] | 145 | // setBufferProvider(name, AudioBufferProvider *) is required before enable(name) |
| 146 | t->bufferProvider = NULL; |
| 147 | t->buffer.raw = NULL; |
| 148 | // no initialization needed |
| 149 | // t->buffer.frameCount |
| 150 | t->hook = NULL; |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 151 | t->mIn = NULL; |
| Glenn Kasten | deeb128 | 2012-03-25 11:59:31 -0700 | [diff] [blame] | 152 | t->sampleRate = mSampleRate; |
| 153 | // setParameter(name, TRACK, MAIN_BUFFER, mixBuffer) is required before enable(name) |
| 154 | t->mainBuffer = NULL; |
| 155 | t->auxBuffer = NULL; |
| Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 156 | t->mInputBufferProvider = NULL; |
| Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 157 | t->mMixerFormat = AUDIO_FORMAT_PCM_16_BIT; |
| Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 158 | t->mFormat = format; |
| Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 159 | t->mMixerInFormat = selectMixerInFormat(format); |
| 160 | t->mDownmixRequiresFormat = AUDIO_FORMAT_INVALID; // no format required |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 161 | t->mMixerChannelMask = audio_channel_mask_from_representation_and_bits( |
| 162 | AUDIO_CHANNEL_REPRESENTATION_POSITION, AUDIO_CHANNEL_OUT_STEREO); |
| 163 | t->mMixerChannelCount = audio_channel_count_from_out_mask(t->mMixerChannelMask); |
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 164 | t->mPlaybackRate = AUDIO_PLAYBACK_RATE_DEFAULT; |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 165 | // Check the downmixing (or upmixing) requirements. |
| Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 166 | status_t status = t->prepareForDownmix(); |
| Andy Hung | 68112fc | 2014-05-14 14:13:23 -0700 | [diff] [blame] | 167 | if (status != OK) { |
| 168 | ALOGE("AudioMixer::getTrackName invalid channelMask (%#x)", channelMask); |
| Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 169 | return BAD_VALUE; |
| Jean-Michel Trivi | 9bd2322 | 2012-04-16 13:43:48 -0700 | [diff] [blame] | 170 | } |
| Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 171 | // prepareForDownmix() may change mDownmixRequiresFormat |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 172 | ALOGVV("mMixerFormat:%#x mMixerInFormat:%#x\n", t->mMixerFormat, t->mMixerInFormat); |
| Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 173 | t->prepareForReformat(); |
| Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 174 | |
| 175 | mTracks[name] = t; |
| 176 | return OK; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 177 | } |
| Glenn Kasten | c5ac4cb | 2011-12-12 09:05:55 -0800 | [diff] [blame] | 178 | } |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 179 | |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 180 | // Called when channel masks have changed for a track name |
| Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 181 | // TODO: Fix DownmixerBufferProvider not to (possibly) change mixer input format, |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 182 | // which will simplify this logic. |
| 183 | bool AudioMixer::setChannelMasks(int name, |
| 184 | audio_channel_mask_t trackChannelMask, audio_channel_mask_t mixerChannelMask) { |
| Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 185 | LOG_ALWAYS_FATAL_IF(!exists(name), "invalid name: %d", name); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 186 | const std::shared_ptr<Track> &track = mTracks[name]; |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 187 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 188 | if (trackChannelMask == track->channelMask |
| 189 | && mixerChannelMask == track->mMixerChannelMask) { |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 190 | return false; // no need to change |
| Jean-Michel Trivi | 9bd2322 | 2012-04-16 13:43:48 -0700 | [diff] [blame] | 191 | } |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 192 | // always recompute for both channel masks even if only one has changed. |
| 193 | const uint32_t trackChannelCount = audio_channel_count_from_out_mask(trackChannelMask); |
| 194 | const uint32_t mixerChannelCount = audio_channel_count_from_out_mask(mixerChannelMask); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 195 | const bool mixerChannelCountChanged = track->mMixerChannelCount != mixerChannelCount; |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 196 | |
| 197 | ALOG_ASSERT((trackChannelCount <= MAX_NUM_CHANNELS_TO_DOWNMIX) |
| 198 | && trackChannelCount |
| 199 | && mixerChannelCount); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 200 | track->channelMask = trackChannelMask; |
| 201 | track->channelCount = trackChannelCount; |
| 202 | track->mMixerChannelMask = mixerChannelMask; |
| 203 | track->mMixerChannelCount = mixerChannelCount; |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 204 | |
| 205 | // channel masks have changed, does this track need a downmixer? |
| 206 | // 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] | 207 | const status_t status = track->prepareForDownmix(); |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 208 | ALOGE_IF(status != OK, |
| Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 209 | "prepareForDownmix error %d, track channel mask %#x, mixer channel mask %#x", |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 210 | status, track->channelMask, track->mMixerChannelMask); |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 211 | |
| Yung Ti Su | 1a0ecc3 | 2018-05-07 11:09:15 +0800 | [diff] [blame] | 212 | // always do reformat since channel mask changed, |
| 213 | // do it after downmix since track format may change! |
| 214 | track->prepareForReformat(); |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 215 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 216 | if (track->mResampler.get() != nullptr && mixerChannelCountChanged) { |
| Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 217 | // resampler channels may have changed. |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 218 | const uint32_t resetToSampleRate = track->sampleRate; |
| 219 | track->mResampler.reset(nullptr); |
| 220 | track->sampleRate = mSampleRate; // without resampler, track rate is device sample rate. |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 221 | // recreate the resampler with updated format, channels, saved sampleRate. |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 222 | track->setResampler(resetToSampleRate /*trackSampleRate*/, mSampleRate /*devSampleRate*/); |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 223 | } |
| 224 | return true; |
| 225 | } |
| 226 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 227 | void AudioMixer::Track::unprepareForDownmix() { |
| Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 228 | ALOGV("AudioMixer::unprepareForDownmix(%p)", this); |
| Jean-Michel Trivi | 9bd2322 | 2012-04-16 13:43:48 -0700 | [diff] [blame] | 229 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 230 | if (mPostDownmixReformatBufferProvider.get() != nullptr) { |
| Andy Hung | 8539589 | 2017-04-25 16:47:52 -0700 | [diff] [blame] | 231 | // release any buffers held by the mPostDownmixReformatBufferProvider |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 232 | // before deallocating the mDownmixerBufferProvider. |
| Andy Hung | 8539589 | 2017-04-25 16:47:52 -0700 | [diff] [blame] | 233 | mPostDownmixReformatBufferProvider->reset(); |
| 234 | } |
| 235 | |
| Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 236 | mDownmixRequiresFormat = AUDIO_FORMAT_INVALID; |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 237 | if (mDownmixerBufferProvider.get() != nullptr) { |
| Jean-Michel Trivi | 9bd2322 | 2012-04-16 13:43:48 -0700 | [diff] [blame] | 238 | // this track had previously been configured with a downmixer, delete it |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 239 | mDownmixerBufferProvider.reset(nullptr); |
| Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 240 | reconfigureBufferProviders(); |
| Jean-Michel Trivi | 9bd2322 | 2012-04-16 13:43:48 -0700 | [diff] [blame] | 241 | } else { |
| 242 | ALOGV(" nothing to do, no downmixer to delete"); |
| 243 | } |
| 244 | } |
| 245 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 246 | status_t AudioMixer::Track::prepareForDownmix() |
| Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 247 | { |
| Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 248 | ALOGV("AudioMixer::prepareForDownmix(%p) with mask 0x%x", |
| 249 | this, channelMask); |
| Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 250 | |
| Jean-Michel Trivi | 9bd2322 | 2012-04-16 13:43:48 -0700 | [diff] [blame] | 251 | // discard the previous downmixer if there was one |
| Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 252 | unprepareForDownmix(); |
| Andy Hung | 73e62e2 | 2015-04-20 12:06:38 -0700 | [diff] [blame] | 253 | // 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] | 254 | // are not the same and not handled internally, as mono -> stereo currently is. |
| 255 | if (channelMask == mMixerChannelMask |
| 256 | || (channelMask == AUDIO_CHANNEL_OUT_MONO |
| 257 | && mMixerChannelMask == AUDIO_CHANNEL_OUT_STEREO)) { |
| 258 | return NO_ERROR; |
| 259 | } |
| Andy Hung | 650ceb9 | 2015-01-29 13:31:12 -0800 | [diff] [blame] | 260 | // DownmixerBufferProvider is only used for position masks. |
| 261 | if (audio_channel_mask_get_representation(channelMask) |
| 262 | == AUDIO_CHANNEL_REPRESENTATION_POSITION |
| 263 | && DownmixerBufferProvider::isMultichannelCapable()) { |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 264 | mDownmixerBufferProvider.reset(new DownmixerBufferProvider(channelMask, |
| Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 265 | mMixerChannelMask, |
| 266 | AUDIO_FORMAT_PCM_16_BIT /* TODO: use mMixerInFormat, now only PCM 16 */, |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 267 | sampleRate, sessionId, kCopyBufferFrameCount)); |
| 268 | if (static_cast<DownmixerBufferProvider *>(mDownmixerBufferProvider.get())->isValid()) { |
| Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 269 | mDownmixRequiresFormat = AUDIO_FORMAT_PCM_16_BIT; // PCM 16 bit required for downmix |
| Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 270 | reconfigureBufferProviders(); |
| Andy Hung | 34803d5 | 2014-07-16 21:41:35 -0700 | [diff] [blame] | 271 | return NO_ERROR; |
| 272 | } |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 273 | // mDownmixerBufferProvider reset below. |
| Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 274 | } |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 275 | |
| 276 | // 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] | 277 | mDownmixerBufferProvider.reset(new RemixBufferProvider(channelMask, |
| 278 | mMixerChannelMask, mMixerInFormat, kCopyBufferFrameCount)); |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 279 | // Remix always finds a conversion whereas Downmixer effect above may fail. |
| Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 280 | reconfigureBufferProviders(); |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 281 | return NO_ERROR; |
| Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 284 | void AudioMixer::Track::unprepareForReformat() { |
| Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 285 | ALOGV("AudioMixer::unprepareForReformat(%p)", this); |
| Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 286 | bool requiresReconfigure = false; |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 287 | if (mReformatBufferProvider.get() != nullptr) { |
| 288 | mReformatBufferProvider.reset(nullptr); |
| Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 289 | requiresReconfigure = true; |
| 290 | } |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 291 | if (mPostDownmixReformatBufferProvider.get() != nullptr) { |
| 292 | mPostDownmixReformatBufferProvider.reset(nullptr); |
| Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 293 | requiresReconfigure = true; |
| 294 | } |
| 295 | if (requiresReconfigure) { |
| Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 296 | reconfigureBufferProviders(); |
| Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 300 | status_t AudioMixer::Track::prepareForReformat() |
| Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 301 | { |
| Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 302 | ALOGV("AudioMixer::prepareForReformat(%p) with format %#x", this, mFormat); |
| Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 303 | // discard previous reformatters |
| Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 304 | unprepareForReformat(); |
| Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 305 | // only configure reformatters as needed |
| 306 | const audio_format_t targetFormat = mDownmixRequiresFormat != AUDIO_FORMAT_INVALID |
| 307 | ? mDownmixRequiresFormat : mMixerInFormat; |
| 308 | bool requiresReconfigure = false; |
| 309 | if (mFormat != targetFormat) { |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 310 | mReformatBufferProvider.reset(new ReformatBufferProvider( |
| Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 311 | audio_channel_count_from_out_mask(channelMask), |
| Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 312 | mFormat, |
| 313 | targetFormat, |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 314 | kCopyBufferFrameCount)); |
| Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 315 | requiresReconfigure = true; |
| Kevin Rocard | e053bfa | 2017-11-09 22:07:34 -0800 | [diff] [blame] | 316 | } else if (mFormat == AUDIO_FORMAT_PCM_FLOAT) { |
| 317 | // Input and output are floats, make sure application did not provide > 3db samples |
| 318 | // that would break volume application (b/68099072) |
| 319 | // TODO: add a trusted source flag to avoid the overhead |
| 320 | mReformatBufferProvider.reset(new ClampFloatBufferProvider( |
| 321 | audio_channel_count_from_out_mask(channelMask), |
| 322 | kCopyBufferFrameCount)); |
| 323 | requiresReconfigure = true; |
| Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 324 | } |
| 325 | if (targetFormat != mMixerInFormat) { |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 326 | mPostDownmixReformatBufferProvider.reset(new ReformatBufferProvider( |
| Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 327 | audio_channel_count_from_out_mask(mMixerChannelMask), |
| 328 | targetFormat, |
| 329 | mMixerInFormat, |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 330 | kCopyBufferFrameCount)); |
| Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 331 | requiresReconfigure = true; |
| 332 | } |
| 333 | if (requiresReconfigure) { |
| Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 334 | reconfigureBufferProviders(); |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 335 | } |
| 336 | return NO_ERROR; |
| Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 339 | void AudioMixer::Track::reconfigureBufferProviders() |
| Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 340 | { |
| Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 341 | bufferProvider = mInputBufferProvider; |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 342 | if (mReformatBufferProvider.get() != nullptr) { |
| Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 343 | mReformatBufferProvider->setBufferProvider(bufferProvider); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 344 | bufferProvider = mReformatBufferProvider.get(); |
| Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 345 | } |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 346 | if (mDownmixerBufferProvider.get() != nullptr) { |
| 347 | mDownmixerBufferProvider->setBufferProvider(bufferProvider); |
| 348 | bufferProvider = mDownmixerBufferProvider.get(); |
| Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 349 | } |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 350 | if (mPostDownmixReformatBufferProvider.get() != nullptr) { |
| Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 351 | mPostDownmixReformatBufferProvider->setBufferProvider(bufferProvider); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 352 | bufferProvider = mPostDownmixReformatBufferProvider.get(); |
| Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 353 | } |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 354 | if (mTimestretchBufferProvider.get() != nullptr) { |
| Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 355 | mTimestretchBufferProvider->setBufferProvider(bufferProvider); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 356 | bufferProvider = mTimestretchBufferProvider.get(); |
| Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 357 | } |
| Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 360 | void AudioMixer::destroy(int name) |
| Glenn Kasten | c5ac4cb | 2011-12-12 09:05:55 -0800 | [diff] [blame] | 361 | { |
| Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 362 | LOG_ALWAYS_FATAL_IF(!exists(name), "invalid name: %d", name); |
| Glenn Kasten | 237a624 | 2011-12-15 15:32:27 -0800 | [diff] [blame] | 363 | ALOGV("deleteTrackName(%d)", name); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 364 | |
| 365 | if (mTracks[name]->enabled) { |
| 366 | invalidate(); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 367 | } |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 368 | mTracks.erase(name); // deallocate track |
| Glenn Kasten | c5ac4cb | 2011-12-12 09:05:55 -0800 | [diff] [blame] | 369 | } |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 370 | |
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 371 | void AudioMixer::enable(int name) |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 372 | { |
| Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 373 | LOG_ALWAYS_FATAL_IF(!exists(name), "invalid name: %d", name); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 374 | const std::shared_ptr<Track> &track = mTracks[name]; |
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 375 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 376 | if (!track->enabled) { |
| 377 | track->enabled = true; |
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 378 | ALOGV("enable(%d)", name); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 379 | invalidate(); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 380 | } |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 383 | void AudioMixer::disable(int name) |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 384 | { |
| Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 385 | LOG_ALWAYS_FATAL_IF(!exists(name), "invalid name: %d", name); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 386 | const std::shared_ptr<Track> &track = mTracks[name]; |
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 387 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 388 | if (track->enabled) { |
| 389 | track->enabled = false; |
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 390 | ALOGV("disable(%d)", name); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 391 | invalidate(); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 392 | } |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| Andy Hung | 5866a3b | 2014-05-29 21:33:13 -0700 | [diff] [blame] | 395 | /* Sets the volume ramp variables for the AudioMixer. |
| 396 | * |
| Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 397 | * The volume ramp variables are used to transition from the previous |
| 398 | * volume to the set volume. ramp controls the duration of the transition. |
| 399 | * Its value is typically one state framecount period, but may also be 0, |
| 400 | * meaning "immediate." |
| Andy Hung | 5866a3b | 2014-05-29 21:33:13 -0700 | [diff] [blame] | 401 | * |
| Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 402 | * FIXME: 1) Volume ramp is enabled only if there is a nonzero integer increment |
| 403 | * even if there is a nonzero floating point increment (in that case, the volume |
| 404 | * change is immediate). This restriction should be changed when the legacy mixer |
| 405 | * is removed (see #2). |
| 406 | * FIXME: 2) Integer volume variables are used for Legacy mixing and should be removed |
| 407 | * when no longer needed. |
| 408 | * |
| 409 | * @param newVolume set volume target in floating point [0.0, 1.0]. |
| 410 | * @param ramp number of frames to increment over. if ramp is 0, the volume |
| 411 | * should be set immediately. Currently ramp should not exceed 65535 (frames). |
| 412 | * @param pIntSetVolume pointer to the U4.12 integer target volume, set on return. |
| 413 | * @param pIntPrevVolume pointer to the U4.28 integer previous volume, set on return. |
| 414 | * @param pIntVolumeInc pointer to the U4.28 increment per output audio frame, set on return. |
| 415 | * @param pSetVolume pointer to the float target volume, set on return. |
| 416 | * @param pPrevVolume pointer to the float previous volume, set on return. |
| 417 | * @param pVolumeInc pointer to the float increment per output audio frame, set on return. |
| Andy Hung | 5866a3b | 2014-05-29 21:33:13 -0700 | [diff] [blame] | 418 | * @return true if the volume has changed, false if volume is same. |
| 419 | */ |
| Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 420 | static inline bool setVolumeRampVariables(float newVolume, int32_t ramp, |
| 421 | int16_t *pIntSetVolume, int32_t *pIntPrevVolume, int32_t *pIntVolumeInc, |
| 422 | float *pSetVolume, float *pPrevVolume, float *pVolumeInc) { |
| Andy Hung | e09c994 | 2015-05-08 16:58:13 -0700 | [diff] [blame] | 423 | // check floating point volume to see if it is identical to the previously |
| 424 | // set volume. |
| 425 | // We do not use a tolerance here (and reject changes too small) |
| 426 | // as it may be confusing to use a different value than the one set. |
| 427 | // If the resulting volume is too small to ramp, it is a direct set of the volume. |
| Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 428 | if (newVolume == *pSetVolume) { |
| Andy Hung | 5866a3b | 2014-05-29 21:33:13 -0700 | [diff] [blame] | 429 | return false; |
| 430 | } |
| Andy Hung | e09c994 | 2015-05-08 16:58:13 -0700 | [diff] [blame] | 431 | if (newVolume < 0) { |
| 432 | newVolume = 0; // should not have negative volumes |
| Andy Hung | 5866a3b | 2014-05-29 21:33:13 -0700 | [diff] [blame] | 433 | } else { |
| Andy Hung | e09c994 | 2015-05-08 16:58:13 -0700 | [diff] [blame] | 434 | switch (fpclassify(newVolume)) { |
| 435 | case FP_SUBNORMAL: |
| 436 | case FP_NAN: |
| 437 | newVolume = 0; |
| 438 | break; |
| 439 | case FP_ZERO: |
| 440 | break; // zero volume is fine |
| 441 | case FP_INFINITE: |
| 442 | // Infinite volume could be handled consistently since |
| 443 | // floating point math saturates at infinities, |
| 444 | // but we limit volume to unity gain float. |
| 445 | // ramp = 0; break; |
| 446 | // |
| 447 | newVolume = AudioMixer::UNITY_GAIN_FLOAT; |
| 448 | break; |
| 449 | case FP_NORMAL: |
| 450 | default: |
| 451 | // Floating point does not have problems with overflow wrap |
| 452 | // that integer has. However, we limit the volume to |
| 453 | // unity gain here. |
| 454 | // TODO: Revisit the volume limitation and perhaps parameterize. |
| 455 | if (newVolume > AudioMixer::UNITY_GAIN_FLOAT) { |
| 456 | newVolume = AudioMixer::UNITY_GAIN_FLOAT; |
| 457 | } |
| 458 | break; |
| 459 | } |
| Andy Hung | 5866a3b | 2014-05-29 21:33:13 -0700 | [diff] [blame] | 460 | } |
| Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 461 | |
| Andy Hung | e09c994 | 2015-05-08 16:58:13 -0700 | [diff] [blame] | 462 | // set floating point volume ramp |
| 463 | if (ramp != 0) { |
| 464 | // when the ramp completes, *pPrevVolume is set to *pSetVolume, so there |
| 465 | // is no computational mismatch; hence equality is checked here. |
| 466 | ALOGD_IF(*pPrevVolume != *pSetVolume, "previous float ramp hasn't finished," |
| 467 | " prev:%f set_to:%f", *pPrevVolume, *pSetVolume); |
| 468 | const float inc = (newVolume - *pPrevVolume) / ramp; // could be inf, nan, subnormal |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 469 | // could be inf, cannot be nan, subnormal |
| 470 | const float maxv = std::max(newVolume, *pPrevVolume); |
| Andy Hung | e09c994 | 2015-05-08 16:58:13 -0700 | [diff] [blame] | 471 | |
| 472 | if (isnormal(inc) // inc must be a normal number (no subnormals, infinite, nan) |
| 473 | && maxv + inc != maxv) { // inc must make forward progress |
| 474 | *pVolumeInc = inc; |
| 475 | // ramp is set now. |
| 476 | // Note: if newVolume is 0, then near the end of the ramp, |
| 477 | // it may be possible that the ramped volume may be subnormal or |
| 478 | // temporarily negative by a small amount or subnormal due to floating |
| 479 | // point inaccuracies. |
| 480 | } else { |
| 481 | ramp = 0; // ramp not allowed |
| 482 | } |
| Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 483 | } |
| Andy Hung | e09c994 | 2015-05-08 16:58:13 -0700 | [diff] [blame] | 484 | |
| 485 | // compute and check integer volume, no need to check negative values |
| 486 | // The integer volume is limited to "unity_gain" to avoid wrapping and other |
| 487 | // audio artifacts, so it never reaches the range limit of U4.28. |
| 488 | // We safely use signed 16 and 32 bit integers here. |
| 489 | const float scaledVolume = newVolume * AudioMixer::UNITY_GAIN_INT; // not neg, subnormal, nan |
| 490 | const int32_t intVolume = (scaledVolume >= (float)AudioMixer::UNITY_GAIN_INT) ? |
| 491 | AudioMixer::UNITY_GAIN_INT : (int32_t)scaledVolume; |
| 492 | |
| 493 | // set integer volume ramp |
| 494 | if (ramp != 0) { |
| 495 | // integer volume is U4.12 (to use 16 bit multiplies), but ramping uses U4.28. |
| 496 | // when the ramp completes, *pIntPrevVolume is set to *pIntSetVolume << 16, so there |
| 497 | // is no computational mismatch; hence equality is checked here. |
| 498 | ALOGD_IF(*pIntPrevVolume != *pIntSetVolume << 16, "previous int ramp hasn't finished," |
| 499 | " prev:%d set_to:%d", *pIntPrevVolume, *pIntSetVolume << 16); |
| 500 | const int32_t inc = ((intVolume << 16) - *pIntPrevVolume) / ramp; |
| 501 | |
| 502 | if (inc != 0) { // inc must make forward progress |
| 503 | *pIntVolumeInc = inc; |
| 504 | } else { |
| 505 | ramp = 0; // ramp not allowed |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | // if no ramp, or ramp not allowed, then clear float and integer increments |
| 510 | if (ramp == 0) { |
| Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 511 | *pVolumeInc = 0; |
| 512 | *pPrevVolume = newVolume; |
| Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 513 | *pIntVolumeInc = 0; |
| 514 | *pIntPrevVolume = intVolume << 16; |
| 515 | } |
| Andy Hung | e09c994 | 2015-05-08 16:58:13 -0700 | [diff] [blame] | 516 | *pSetVolume = newVolume; |
| Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 517 | *pIntSetVolume = intVolume; |
| Andy Hung | 5866a3b | 2014-05-29 21:33:13 -0700 | [diff] [blame] | 518 | return true; |
| 519 | } |
| 520 | |
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 521 | void AudioMixer::setParameter(int name, int target, int param, void *value) |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 522 | { |
| Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 523 | LOG_ALWAYS_FATAL_IF(!exists(name), "invalid name: %d", name); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 524 | const std::shared_ptr<Track> &track = mTracks[name]; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 525 | |
| Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 526 | int valueInt = static_cast<int>(reinterpret_cast<uintptr_t>(value)); |
| 527 | int32_t *valueBuf = reinterpret_cast<int32_t*>(value); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 528 | |
| 529 | switch (target) { |
| Glenn Kasten | 788040c | 2011-05-05 08:19:00 -0700 | [diff] [blame] | 530 | |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 531 | case TRACK: |
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 532 | switch (param) { |
| Glenn Kasten | 788040c | 2011-05-05 08:19:00 -0700 | [diff] [blame] | 533 | case CHANNEL_MASK: { |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 534 | const audio_channel_mask_t trackChannelMask = |
| 535 | static_cast<audio_channel_mask_t>(valueInt); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 536 | if (setChannelMasks(name, trackChannelMask, track->mMixerChannelMask)) { |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 537 | ALOGV("setParameter(TRACK, CHANNEL_MASK, %x)", trackChannelMask); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 538 | invalidate(); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 539 | } |
| Glenn Kasten | 788040c | 2011-05-05 08:19:00 -0700 | [diff] [blame] | 540 | } break; |
| 541 | case MAIN_BUFFER: |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 542 | if (track->mainBuffer != valueBuf) { |
| 543 | track->mainBuffer = valueBuf; |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 544 | ALOGV("setParameter(TRACK, MAIN_BUFFER, %p)", valueBuf); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 545 | invalidate(); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 546 | } |
| Glenn Kasten | 788040c | 2011-05-05 08:19:00 -0700 | [diff] [blame] | 547 | break; |
| 548 | case AUX_BUFFER: |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 549 | if (track->auxBuffer != valueBuf) { |
| 550 | track->auxBuffer = valueBuf; |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 551 | ALOGV("setParameter(TRACK, AUX_BUFFER, %p)", valueBuf); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 552 | invalidate(); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 553 | } |
| Glenn Kasten | 788040c | 2011-05-05 08:19:00 -0700 | [diff] [blame] | 554 | break; |
| Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 555 | case FORMAT: { |
| 556 | audio_format_t format = static_cast<audio_format_t>(valueInt); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 557 | if (track->mFormat != format) { |
| Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 558 | ALOG_ASSERT(audio_is_linear_pcm(format), "Invalid format %#x", format); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 559 | track->mFormat = format; |
| Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 560 | ALOGV("setParameter(TRACK, FORMAT, %#x)", format); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 561 | track->prepareForReformat(); |
| 562 | invalidate(); |
| Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 563 | } |
| 564 | } break; |
| Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 565 | // FIXME do we want to support setting the downmix type from AudioFlinger? |
| 566 | // for a specific track? or per mixer? |
| 567 | /* case DOWNMIX_TYPE: |
| 568 | break */ |
| Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 569 | case MIXER_FORMAT: { |
| Andy Hung | a1ab7cc | 2014-02-24 19:26:52 -0800 | [diff] [blame] | 570 | audio_format_t format = static_cast<audio_format_t>(valueInt); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 571 | if (track->mMixerFormat != format) { |
| 572 | track->mMixerFormat = format; |
| Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 573 | ALOGV("setParameter(TRACK, MIXER_FORMAT, %#x)", format); |
| Andy Hung | a1ab7cc | 2014-02-24 19:26:52 -0800 | [diff] [blame] | 574 | } |
| 575 | } break; |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 576 | case MIXER_CHANNEL_MASK: { |
| 577 | const audio_channel_mask_t mixerChannelMask = |
| 578 | static_cast<audio_channel_mask_t>(valueInt); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 579 | if (setChannelMasks(name, track->channelMask, mixerChannelMask)) { |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 580 | ALOGV("setParameter(TRACK, MIXER_CHANNEL_MASK, %#x)", mixerChannelMask); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 581 | invalidate(); |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 582 | } |
| 583 | } break; |
| Glenn Kasten | 788040c | 2011-05-05 08:19:00 -0700 | [diff] [blame] | 584 | default: |
| Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 585 | LOG_ALWAYS_FATAL("setParameter track: bad param %d", param); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 586 | } |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 587 | break; |
| Glenn Kasten | 788040c | 2011-05-05 08:19:00 -0700 | [diff] [blame] | 588 | |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 589 | case RESAMPLE: |
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 590 | switch (param) { |
| 591 | case SAMPLE_RATE: |
| Glenn Kasten | 5798d4e | 2012-03-08 12:18:35 -0800 | [diff] [blame] | 592 | ALOG_ASSERT(valueInt > 0, "bad sample rate %d", valueInt); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 593 | if (track->setResampler(uint32_t(valueInt), mSampleRate)) { |
| Glenn Kasten | 788040c | 2011-05-05 08:19:00 -0700 | [diff] [blame] | 594 | ALOGV("setParameter(RESAMPLE, SAMPLE_RATE, %u)", |
| 595 | uint32_t(valueInt)); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 596 | invalidate(); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 597 | } |
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 598 | break; |
| 599 | case RESET: |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 600 | track->resetResampler(); |
| 601 | invalidate(); |
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 602 | break; |
| Glenn Kasten | 4e2293f | 2012-04-12 09:39:07 -0700 | [diff] [blame] | 603 | case REMOVE: |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 604 | track->mResampler.reset(nullptr); |
| 605 | track->sampleRate = mSampleRate; |
| 606 | invalidate(); |
| Glenn Kasten | 4e2293f | 2012-04-12 09:39:07 -0700 | [diff] [blame] | 607 | break; |
| Glenn Kasten | 788040c | 2011-05-05 08:19:00 -0700 | [diff] [blame] | 608 | default: |
| Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 609 | LOG_ALWAYS_FATAL("setParameter resample: bad param %d", param); |
| Eric Laurent | 243f5f9 | 2011-02-28 16:52:51 -0800 | [diff] [blame] | 610 | } |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 611 | break; |
| Glenn Kasten | 788040c | 2011-05-05 08:19:00 -0700 | [diff] [blame] | 612 | |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 613 | case RAMP_VOLUME: |
| 614 | case VOLUME: |
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 615 | switch (param) { |
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 616 | case AUXLEVEL: |
| Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 617 | if (setVolumeRampVariables(*reinterpret_cast<float*>(value), |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 618 | target == RAMP_VOLUME ? mFrameCount : 0, |
| 619 | &track->auxLevel, &track->prevAuxLevel, &track->auxInc, |
| 620 | &track->mAuxLevel, &track->mPrevAuxLevel, &track->mAuxInc)) { |
| Andy Hung | 5866a3b | 2014-05-29 21:33:13 -0700 | [diff] [blame] | 621 | ALOGV("setParameter(%s, AUXLEVEL: %04x)", |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 622 | target == VOLUME ? "VOLUME" : "RAMP_VOLUME", track->auxLevel); |
| 623 | invalidate(); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 624 | } |
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 625 | break; |
| Glenn Kasten | 788040c | 2011-05-05 08:19:00 -0700 | [diff] [blame] | 626 | default: |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 627 | if ((unsigned)param >= VOLUME0 && (unsigned)param < VOLUME0 + MAX_NUM_VOLUMES) { |
| 628 | if (setVolumeRampVariables(*reinterpret_cast<float*>(value), |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 629 | target == RAMP_VOLUME ? mFrameCount : 0, |
| 630 | &track->volume[param - VOLUME0], |
| 631 | &track->prevVolume[param - VOLUME0], |
| 632 | &track->volumeInc[param - VOLUME0], |
| 633 | &track->mVolume[param - VOLUME0], |
| 634 | &track->mPrevVolume[param - VOLUME0], |
| 635 | &track->mVolumeInc[param - VOLUME0])) { |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 636 | ALOGV("setParameter(%s, VOLUME%d: %04x)", |
| 637 | target == VOLUME ? "VOLUME" : "RAMP_VOLUME", param - VOLUME0, |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 638 | track->volume[param - VOLUME0]); |
| 639 | invalidate(); |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 640 | } |
| 641 | } else { |
| 642 | LOG_ALWAYS_FATAL("setParameter volume: bad param %d", param); |
| 643 | } |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 644 | } |
| 645 | break; |
| Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 646 | case TIMESTRETCH: |
| 647 | switch (param) { |
| 648 | case PLAYBACK_RATE: { |
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 649 | const AudioPlaybackRate *playbackRate = |
| 650 | reinterpret_cast<AudioPlaybackRate*>(value); |
| Ricardo Garcia | 6c7f062 | 2015-04-30 18:39:16 -0700 | [diff] [blame] | 651 | ALOGW_IF(!isAudioPlaybackRateValid(*playbackRate), |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 652 | "bad parameters speed %f, pitch %f", |
| 653 | playbackRate->mSpeed, playbackRate->mPitch); |
| 654 | if (track->setPlaybackRate(*playbackRate)) { |
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 655 | ALOGV("setParameter(TIMESTRETCH, PLAYBACK_RATE, STRETCH_MODE, FALLBACK_MODE " |
| 656 | "%f %f %d %d", |
| 657 | playbackRate->mSpeed, |
| 658 | playbackRate->mPitch, |
| 659 | playbackRate->mStretchMode, |
| 660 | playbackRate->mFallbackMode); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 661 | // invalidate(); (should not require reconfigure) |
| Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 662 | } |
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 663 | } break; |
| Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 664 | default: |
| 665 | LOG_ALWAYS_FATAL("setParameter timestretch: bad param %d", param); |
| 666 | } |
| 667 | break; |
| Glenn Kasten | 788040c | 2011-05-05 08:19:00 -0700 | [diff] [blame] | 668 | |
| 669 | default: |
| Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 670 | LOG_ALWAYS_FATAL("setParameter: bad target %d", target); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 671 | } |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 672 | } |
| 673 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 674 | bool AudioMixer::Track::setResampler(uint32_t trackSampleRate, uint32_t devSampleRate) |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 675 | { |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 676 | if (trackSampleRate != devSampleRate || mResampler.get() != nullptr) { |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 677 | if (sampleRate != trackSampleRate) { |
| 678 | sampleRate = trackSampleRate; |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 679 | if (mResampler.get() == nullptr) { |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 680 | ALOGV("Creating resampler from track %d Hz to device %d Hz", |
| 681 | trackSampleRate, devSampleRate); |
| Glenn Kasten | ac60205 | 2012-10-01 14:04:31 -0700 | [diff] [blame] | 682 | AudioResampler::src_quality quality; |
| 683 | // force lowest quality level resampler if use case isn't music or video |
| 684 | // FIXME this is flawed for dynamic sample rates, as we choose the resampler |
| 685 | // quality level based on the initial ratio, but that could change later. |
| 686 | // Should have a way to distinguish tracks with static ratios vs. dynamic ratios. |
| Andy Hung | db4c031 | 2015-05-06 08:46:52 -0700 | [diff] [blame] | 687 | if (isMusicRate(trackSampleRate)) { |
| Glenn Kasten | ac60205 | 2012-10-01 14:04:31 -0700 | [diff] [blame] | 688 | quality = AudioResampler::DEFAULT_QUALITY; |
| Andy Hung | db4c031 | 2015-05-06 08:46:52 -0700 | [diff] [blame] | 689 | } else { |
| 690 | quality = AudioResampler::DYN_LOW_QUALITY; |
| Glenn Kasten | ac60205 | 2012-10-01 14:04:31 -0700 | [diff] [blame] | 691 | } |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 692 | |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 693 | // TODO: Remove MONO_HACK. Resampler sees #channels after the downmixer |
| 694 | // but if none exists, it is the channel count (1 for mono). |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 695 | const int resamplerChannelCount = mDownmixerBufferProvider.get() != nullptr |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 696 | ? mMixerChannelCount : channelCount; |
| Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 697 | ALOGVV("Creating resampler:" |
| 698 | " format(%#x) channels(%d) devSampleRate(%u) quality(%d)\n", |
| 699 | mMixerInFormat, resamplerChannelCount, devSampleRate, quality); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 700 | mResampler.reset(AudioResampler::create( |
| Andy Hung | 3348e36 | 2014-07-07 10:21:44 -0700 | [diff] [blame] | 701 | mMixerInFormat, |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 702 | resamplerChannelCount, |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 703 | devSampleRate, quality)); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 704 | } |
| 705 | return true; |
| 706 | } |
| 707 | } |
| 708 | return false; |
| 709 | } |
| 710 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 711 | bool AudioMixer::Track::setPlaybackRate(const AudioPlaybackRate &playbackRate) |
| Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 712 | { |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 713 | if ((mTimestretchBufferProvider.get() == nullptr && |
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 714 | fabs(playbackRate.mSpeed - mPlaybackRate.mSpeed) < AUDIO_TIMESTRETCH_SPEED_MIN_DELTA && |
| 715 | fabs(playbackRate.mPitch - mPlaybackRate.mPitch) < AUDIO_TIMESTRETCH_PITCH_MIN_DELTA) || |
| 716 | isAudioPlaybackRateEqual(playbackRate, mPlaybackRate)) { |
| Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 717 | return false; |
| 718 | } |
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 719 | mPlaybackRate = playbackRate; |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 720 | if (mTimestretchBufferProvider.get() == nullptr) { |
| Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 721 | // TODO: Remove MONO_HACK. Resampler sees #channels after the downmixer |
| 722 | // but if none exists, it is the channel count (1 for mono). |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 723 | const int timestretchChannelCount = mDownmixerBufferProvider.get() != nullptr |
| Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 724 | ? mMixerChannelCount : channelCount; |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 725 | mTimestretchBufferProvider.reset(new TimestretchBufferProvider(timestretchChannelCount, |
| 726 | mMixerInFormat, sampleRate, playbackRate)); |
| Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 727 | reconfigureBufferProviders(); |
| 728 | } else { |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 729 | static_cast<TimestretchBufferProvider*>(mTimestretchBufferProvider.get()) |
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 730 | ->setPlaybackRate(playbackRate); |
| Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 731 | } |
| 732 | return true; |
| 733 | } |
| 734 | |
| Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 735 | /* Checks to see if the volume ramp has completed and clears the increment |
| 736 | * variables appropriately. |
| 737 | * |
| 738 | * FIXME: There is code to handle int/float ramp variable switchover should it not |
| 739 | * complete within a mixer buffer processing call, but it is preferred to avoid switchover |
| 740 | * due to precision issues. The switchover code is included for legacy code purposes |
| 741 | * and can be removed once the integer volume is removed. |
| 742 | * |
| 743 | * It is not sufficient to clear only the volumeInc integer variable because |
| 744 | * if one channel requires ramping, all channels are ramped. |
| 745 | * |
| 746 | * There is a bit of duplicated code here, but it keeps backward compatibility. |
| 747 | */ |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 748 | inline void AudioMixer::Track::adjustVolumeRamp(bool aux, bool useFloat) |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 749 | { |
| Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 750 | if (useFloat) { |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 751 | for (uint32_t i = 0; i < MAX_NUM_VOLUMES; i++) { |
| Eric Laurent | 43412fc | 2015-05-08 16:14:36 -0700 | [diff] [blame] | 752 | if ((mVolumeInc[i] > 0 && mPrevVolume[i] + mVolumeInc[i] >= mVolume[i]) || |
| 753 | (mVolumeInc[i] < 0 && mPrevVolume[i] + mVolumeInc[i] <= mVolume[i])) { |
| Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 754 | volumeInc[i] = 0; |
| 755 | prevVolume[i] = volume[i] << 16; |
| 756 | mVolumeInc[i] = 0.; |
| 757 | mPrevVolume[i] = mVolume[i]; |
| Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 758 | } else { |
| 759 | //ALOGV("ramp: %f %f %f", mVolume[i], mPrevVolume[i], mVolumeInc[i]); |
| 760 | prevVolume[i] = u4_28_from_float(mPrevVolume[i]); |
| 761 | } |
| 762 | } |
| 763 | } else { |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 764 | for (uint32_t i = 0; i < MAX_NUM_VOLUMES; i++) { |
| Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 765 | if (((volumeInc[i]>0) && (((prevVolume[i]+volumeInc[i])>>16) >= volume[i])) || |
| 766 | ((volumeInc[i]<0) && (((prevVolume[i]+volumeInc[i])>>16) <= volume[i]))) { |
| 767 | volumeInc[i] = 0; |
| 768 | prevVolume[i] = volume[i] << 16; |
| 769 | mVolumeInc[i] = 0.; |
| 770 | mPrevVolume[i] = mVolume[i]; |
| 771 | } else { |
| 772 | //ALOGV("ramp: %d %d %d", volume[i] << 16, prevVolume[i], volumeInc[i]); |
| 773 | mPrevVolume[i] = float_from_u4_28(prevVolume[i]); |
| 774 | } |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 775 | } |
| 776 | } |
| Andy Hung | 116a498 | 2017-11-30 10:15:08 -0800 | [diff] [blame] | 777 | |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 778 | if (aux) { |
| Andy Hung | 116a498 | 2017-11-30 10:15:08 -0800 | [diff] [blame] | 779 | #ifdef FLOAT_AUX |
| 780 | if (useFloat) { |
| 781 | if ((mAuxInc > 0.f && mPrevAuxLevel + mAuxInc >= mAuxLevel) || |
| 782 | (mAuxInc < 0.f && mPrevAuxLevel + mAuxInc <= mAuxLevel)) { |
| 783 | auxInc = 0; |
| 784 | prevAuxLevel = auxLevel << 16; |
| 785 | mAuxInc = 0.f; |
| 786 | mPrevAuxLevel = mAuxLevel; |
| 787 | } |
| 788 | } else |
| 789 | #endif |
| 790 | if ((auxInc > 0 && ((prevAuxLevel + auxInc) >> 16) >= auxLevel) || |
| 791 | (auxInc < 0 && ((prevAuxLevel + auxInc) >> 16) <= auxLevel)) { |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 792 | auxInc = 0; |
| Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 793 | prevAuxLevel = auxLevel << 16; |
| Andy Hung | 116a498 | 2017-11-30 10:15:08 -0800 | [diff] [blame] | 794 | mAuxInc = 0.f; |
| Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 795 | mPrevAuxLevel = mAuxLevel; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 796 | } |
| 797 | } |
| 798 | } |
| 799 | |
| Glenn Kasten | c59c004 | 2012-02-02 14:06:11 -0800 | [diff] [blame] | 800 | size_t AudioMixer::getUnreleasedFrames(int name) const |
| Eric Laurent | 071ccd5 | 2011-12-22 16:08:41 -0800 | [diff] [blame] | 801 | { |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 802 | const auto it = mTracks.find(name); |
| 803 | if (it != mTracks.end()) { |
| 804 | return it->second->getUnreleasedFrames(); |
| Eric Laurent | 071ccd5 | 2011-12-22 16:08:41 -0800 | [diff] [blame] | 805 | } |
| 806 | return 0; |
| 807 | } |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 808 | |
| Glenn Kasten | 01c4ebf | 2012-02-22 10:47:35 -0800 | [diff] [blame] | 809 | void AudioMixer::setBufferProvider(int name, AudioBufferProvider* bufferProvider) |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 810 | { |
| Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 811 | LOG_ALWAYS_FATAL_IF(!exists(name), "invalid name: %d", name); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 812 | const std::shared_ptr<Track> &track = mTracks[name]; |
| Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 813 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 814 | if (track->mInputBufferProvider == bufferProvider) { |
| Andy Hung | 1d26ddf | 2014-05-29 15:53:09 -0700 | [diff] [blame] | 815 | return; // don't reset any buffer providers if identical. |
| 816 | } |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 817 | if (track->mReformatBufferProvider.get() != nullptr) { |
| 818 | track->mReformatBufferProvider->reset(); |
| 819 | } else if (track->mDownmixerBufferProvider != nullptr) { |
| 820 | track->mDownmixerBufferProvider->reset(); |
| 821 | } else if (track->mPostDownmixReformatBufferProvider.get() != nullptr) { |
| 822 | track->mPostDownmixReformatBufferProvider->reset(); |
| 823 | } else if (track->mTimestretchBufferProvider.get() != nullptr) { |
| 824 | track->mTimestretchBufferProvider->reset(); |
| Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 825 | } |
| Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 826 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 827 | track->mInputBufferProvider = bufferProvider; |
| 828 | track->reconfigureBufferProviders(); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 829 | } |
| 830 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 831 | void AudioMixer::process__validate() |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 832 | { |
| Andy Hung | 395db4b | 2014-08-25 17:15:29 -0700 | [diff] [blame] | 833 | // TODO: fix all16BitsStereNoResample logic to |
| 834 | // either properly handle muted tracks (it should ignore them) |
| 835 | // or remove altogether as an obsolete optimization. |
| Glenn Kasten | 4c340c6 | 2012-01-27 12:33:54 -0800 | [diff] [blame] | 836 | bool all16BitsStereoNoResample = true; |
| 837 | bool resampling = false; |
| 838 | bool volumeRamp = false; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 839 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 840 | mEnabled.clear(); |
| 841 | mGroups.clear(); |
| 842 | for (const auto &pair : mTracks) { |
| 843 | const int name = pair.first; |
| 844 | const std::shared_ptr<Track> &t = pair.second; |
| 845 | if (!t->enabled) continue; |
| 846 | |
| 847 | mEnabled.emplace_back(name); // we add to mEnabled in order of name. |
| 848 | mGroups[t->mainBuffer].emplace_back(name); // mGroups also in order of name. |
| 849 | |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 850 | uint32_t n = 0; |
| Glenn Kasten | d6fadf0 | 2013-10-30 14:37:29 -0700 | [diff] [blame] | 851 | // FIXME can overflow (mask is only 3 bits) |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 852 | n |= NEEDS_CHANNEL_1 + t->channelCount - 1; |
| 853 | if (t->doesResample()) { |
| Glenn Kasten | d6fadf0 | 2013-10-30 14:37:29 -0700 | [diff] [blame] | 854 | n |= NEEDS_RESAMPLE; |
| 855 | } |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 856 | if (t->auxLevel != 0 && t->auxBuffer != NULL) { |
| Glenn Kasten | d6fadf0 | 2013-10-30 14:37:29 -0700 | [diff] [blame] | 857 | n |= NEEDS_AUX; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 858 | } |
| 859 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 860 | if (t->volumeInc[0]|t->volumeInc[1]) { |
| Glenn Kasten | 4c340c6 | 2012-01-27 12:33:54 -0800 | [diff] [blame] | 861 | volumeRamp = true; |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 862 | } else if (!t->doesResample() && t->volumeRL == 0) { |
| Glenn Kasten | d6fadf0 | 2013-10-30 14:37:29 -0700 | [diff] [blame] | 863 | n |= NEEDS_MUTE; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 864 | } |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 865 | t->needs = n; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 866 | |
| Glenn Kasten | d6fadf0 | 2013-10-30 14:37:29 -0700 | [diff] [blame] | 867 | if (n & NEEDS_MUTE) { |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 868 | t->hook = &Track::track__nop; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 869 | } else { |
| Glenn Kasten | d6fadf0 | 2013-10-30 14:37:29 -0700 | [diff] [blame] | 870 | if (n & NEEDS_AUX) { |
| Glenn Kasten | 4c340c6 | 2012-01-27 12:33:54 -0800 | [diff] [blame] | 871 | all16BitsStereoNoResample = false; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 872 | } |
| Glenn Kasten | d6fadf0 | 2013-10-30 14:37:29 -0700 | [diff] [blame] | 873 | if (n & NEEDS_RESAMPLE) { |
| Glenn Kasten | 4c340c6 | 2012-01-27 12:33:54 -0800 | [diff] [blame] | 874 | all16BitsStereoNoResample = false; |
| 875 | resampling = true; |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 876 | t->hook = Track::getTrackHook(TRACKTYPE_RESAMPLE, t->mMixerChannelCount, |
| 877 | t->mMixerInFormat, t->mMixerFormat); |
| Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 878 | ALOGV_IF((n & NEEDS_CHANNEL_COUNT__MASK) > NEEDS_CHANNEL_2, |
| Jean-Michel Trivi | 9bd2322 | 2012-04-16 13:43:48 -0700 | [diff] [blame] | 879 | "Track %d needs downmix + resample", i); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 880 | } else { |
| 881 | if ((n & NEEDS_CHANNEL_COUNT__MASK) == NEEDS_CHANNEL_1){ |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 882 | t->hook = Track::getTrackHook( |
| 883 | (t->mMixerChannelMask == AUDIO_CHANNEL_OUT_STEREO // TODO: MONO_HACK |
| 884 | && t->channelMask == AUDIO_CHANNEL_OUT_MONO) |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 885 | ? TRACKTYPE_NORESAMPLEMONO : TRACKTYPE_NORESAMPLE, |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 886 | t->mMixerChannelCount, |
| 887 | t->mMixerInFormat, t->mMixerFormat); |
| Glenn Kasten | 4c340c6 | 2012-01-27 12:33:54 -0800 | [diff] [blame] | 888 | all16BitsStereoNoResample = false; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 889 | } |
| Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 890 | if ((n & NEEDS_CHANNEL_COUNT__MASK) >= NEEDS_CHANNEL_2){ |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 891 | t->hook = Track::getTrackHook(TRACKTYPE_NORESAMPLE, t->mMixerChannelCount, |
| 892 | t->mMixerInFormat, t->mMixerFormat); |
| Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 893 | ALOGV_IF((n & NEEDS_CHANNEL_COUNT__MASK) > NEEDS_CHANNEL_2, |
| Jean-Michel Trivi | 9bd2322 | 2012-04-16 13:43:48 -0700 | [diff] [blame] | 894 | "Track %d needs downmix", i); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 895 | } |
| 896 | } |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | // select the processing hooks |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 901 | mHook = &AudioMixer::process__nop; |
| 902 | if (mEnabled.size() > 0) { |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 903 | if (resampling) { |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 904 | if (mOutputTemp.get() == nullptr) { |
| 905 | mOutputTemp.reset(new int32_t[MAX_NUM_CHANNELS * mFrameCount]); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 906 | } |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 907 | if (mResampleTemp.get() == nullptr) { |
| 908 | mResampleTemp.reset(new int32_t[MAX_NUM_CHANNELS * mFrameCount]); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 909 | } |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 910 | mHook = &AudioMixer::process__genericResampling; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 911 | } else { |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 912 | // we keep temp arrays around. |
| 913 | mHook = &AudioMixer::process__genericNoResampling; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 914 | if (all16BitsStereoNoResample && !volumeRamp) { |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 915 | if (mEnabled.size() == 1) { |
| 916 | const std::shared_ptr<Track> &t = mTracks[mEnabled[0]]; |
| 917 | if ((t->needs & NEEDS_MUTE) == 0) { |
| Andy Hung | 395db4b | 2014-08-25 17:15:29 -0700 | [diff] [blame] | 918 | // The check prevents a muted track from acquiring a process hook. |
| 919 | // |
| 920 | // This is dangerous if the track is MONO as that requires |
| 921 | // special case handling due to implicit channel duplication. |
| 922 | // Stereo or Multichannel should actually be fine here. |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 923 | mHook = getProcessHook(PROCESSTYPE_NORESAMPLEONETRACK, |
| 924 | t->mMixerChannelCount, t->mMixerInFormat, t->mMixerFormat); |
| Andy Hung | 395db4b | 2014-08-25 17:15:29 -0700 | [diff] [blame] | 925 | } |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 926 | } |
| 927 | } |
| 928 | } |
| 929 | } |
| 930 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 931 | ALOGV("mixer configuration change: %zu " |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 932 | "all16BitsStereoNoResample=%d, resampling=%d, volumeRamp=%d", |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 933 | mEnabled.size(), all16BitsStereoNoResample, resampling, volumeRamp); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 934 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 935 | process(); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 936 | |
| Glenn Kasten | c5ac4cb | 2011-12-12 09:05:55 -0800 | [diff] [blame] | 937 | // Now that the volume ramp has been done, set optimal state and |
| 938 | // track hooks for subsequent mixer process |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 939 | if (mEnabled.size() > 0) { |
| Glenn Kasten | 4c340c6 | 2012-01-27 12:33:54 -0800 | [diff] [blame] | 940 | bool allMuted = true; |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 941 | |
| 942 | for (const int name : mEnabled) { |
| 943 | const std::shared_ptr<Track> &t = mTracks[name]; |
| 944 | if (!t->doesResample() && t->volumeRL == 0) { |
| 945 | t->needs |= NEEDS_MUTE; |
| 946 | t->hook = &Track::track__nop; |
| Glenn Kasten | c5ac4cb | 2011-12-12 09:05:55 -0800 | [diff] [blame] | 947 | } else { |
| Glenn Kasten | 4c340c6 | 2012-01-27 12:33:54 -0800 | [diff] [blame] | 948 | allMuted = false; |
| Glenn Kasten | c5ac4cb | 2011-12-12 09:05:55 -0800 | [diff] [blame] | 949 | } |
| 950 | } |
| 951 | if (allMuted) { |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 952 | mHook = &AudioMixer::process__nop; |
| Glenn Kasten | c5ac4cb | 2011-12-12 09:05:55 -0800 | [diff] [blame] | 953 | } else if (all16BitsStereoNoResample) { |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 954 | if (mEnabled.size() == 1) { |
| 955 | //const int i = 31 - __builtin_clz(enabledTracks); |
| 956 | const std::shared_ptr<Track> &t = mTracks[mEnabled[0]]; |
| Andy Hung | 395db4b | 2014-08-25 17:15:29 -0700 | [diff] [blame] | 957 | // Muted single tracks handled by allMuted above. |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 958 | mHook = getProcessHook(PROCESSTYPE_NORESAMPLEONETRACK, |
| 959 | t->mMixerChannelCount, t->mMixerInFormat, t->mMixerFormat); |
| Glenn Kasten | c5ac4cb | 2011-12-12 09:05:55 -0800 | [diff] [blame] | 960 | } |
| 961 | } |
| 962 | } |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 963 | } |
| 964 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 965 | void AudioMixer::Track::track__genericResample( |
| 966 | int32_t* out, size_t outFrameCount, int32_t* temp, int32_t* aux) |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 967 | { |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 968 | ALOGVV("track__genericResample\n"); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 969 | mResampler->setSampleRate(sampleRate); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 970 | |
| 971 | // ramp gain - resample to temp buffer and scale/mix in 2nd step |
| 972 | if (aux != NULL) { |
| 973 | // always resample with unity gain when sending to auxiliary buffer to be able |
| 974 | // to apply send level after resampling |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 975 | mResampler->setVolume(UNITY_GAIN_FLOAT, UNITY_GAIN_FLOAT); |
| 976 | memset(temp, 0, outFrameCount * mMixerChannelCount * sizeof(int32_t)); |
| 977 | mResampler->resample(temp, outFrameCount, bufferProvider); |
| 978 | if (CC_UNLIKELY(volumeInc[0]|volumeInc[1]|auxInc)) { |
| 979 | volumeRampStereo(out, outFrameCount, temp, aux); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 980 | } else { |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 981 | volumeStereo(out, outFrameCount, temp, aux); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 982 | } |
| 983 | } else { |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 984 | if (CC_UNLIKELY(volumeInc[0]|volumeInc[1])) { |
| 985 | mResampler->setVolume(UNITY_GAIN_FLOAT, UNITY_GAIN_FLOAT); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 986 | memset(temp, 0, outFrameCount * MAX_NUM_CHANNELS * sizeof(int32_t)); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 987 | mResampler->resample(temp, outFrameCount, bufferProvider); |
| 988 | volumeRampStereo(out, outFrameCount, temp, aux); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | // constant gain |
| 992 | else { |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 993 | mResampler->setVolume(mVolume[0], mVolume[1]); |
| 994 | mResampler->resample(out, outFrameCount, bufferProvider); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 995 | } |
| 996 | } |
| 997 | } |
| 998 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 999 | void AudioMixer::Track::track__nop(int32_t* out __unused, |
| Andy Hung | ee931ff | 2014-01-28 13:44:14 -0800 | [diff] [blame] | 1000 | size_t outFrameCount __unused, int32_t* temp __unused, int32_t* aux __unused) |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1001 | { |
| 1002 | } |
| 1003 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1004 | void AudioMixer::Track::volumeRampStereo( |
| 1005 | int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux) |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1006 | { |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1007 | int32_t vl = prevVolume[0]; |
| 1008 | int32_t vr = prevVolume[1]; |
| 1009 | const int32_t vlInc = volumeInc[0]; |
| 1010 | const int32_t vrInc = volumeInc[1]; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1011 | |
| Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1012 | //ALOGD("[0] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d", |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1013 | // t, vlInc/65536.0f, vl/65536.0f, volume[0], |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1014 | // (vl + vlInc*frameCount)/65536.0f, frameCount); |
| 1015 | |
| 1016 | // ramp volume |
| Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 1017 | if (CC_UNLIKELY(aux != NULL)) { |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1018 | int32_t va = prevAuxLevel; |
| 1019 | const int32_t vaInc = auxInc; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1020 | int32_t l; |
| 1021 | int32_t r; |
| 1022 | |
| 1023 | do { |
| 1024 | l = (*temp++ >> 12); |
| 1025 | r = (*temp++ >> 12); |
| 1026 | *out++ += (vl >> 16) * l; |
| 1027 | *out++ += (vr >> 16) * r; |
| 1028 | *aux++ += (va >> 17) * (l + r); |
| 1029 | vl += vlInc; |
| 1030 | vr += vrInc; |
| 1031 | va += vaInc; |
| 1032 | } while (--frameCount); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1033 | prevAuxLevel = va; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1034 | } else { |
| 1035 | do { |
| 1036 | *out++ += (vl >> 16) * (*temp++ >> 12); |
| 1037 | *out++ += (vr >> 16) * (*temp++ >> 12); |
| 1038 | vl += vlInc; |
| 1039 | vr += vrInc; |
| 1040 | } while (--frameCount); |
| 1041 | } |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1042 | prevVolume[0] = vl; |
| 1043 | prevVolume[1] = vr; |
| 1044 | adjustVolumeRamp(aux != NULL); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1045 | } |
| 1046 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1047 | void AudioMixer::Track::volumeStereo( |
| 1048 | int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux) |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1049 | { |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1050 | const int16_t vl = volume[0]; |
| 1051 | const int16_t vr = volume[1]; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1052 | |
| Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 1053 | if (CC_UNLIKELY(aux != NULL)) { |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1054 | const int16_t va = auxLevel; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1055 | do { |
| 1056 | int16_t l = (int16_t)(*temp++ >> 12); |
| 1057 | int16_t r = (int16_t)(*temp++ >> 12); |
| 1058 | out[0] = mulAdd(l, vl, out[0]); |
| 1059 | int16_t a = (int16_t)(((int32_t)l + r) >> 1); |
| 1060 | out[1] = mulAdd(r, vr, out[1]); |
| 1061 | out += 2; |
| 1062 | aux[0] = mulAdd(a, va, aux[0]); |
| 1063 | aux++; |
| 1064 | } while (--frameCount); |
| 1065 | } else { |
| 1066 | do { |
| 1067 | int16_t l = (int16_t)(*temp++ >> 12); |
| 1068 | int16_t r = (int16_t)(*temp++ >> 12); |
| 1069 | out[0] = mulAdd(l, vl, out[0]); |
| 1070 | out[1] = mulAdd(r, vr, out[1]); |
| 1071 | out += 2; |
| 1072 | } while (--frameCount); |
| 1073 | } |
| 1074 | } |
| 1075 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1076 | void AudioMixer::Track::track__16BitsStereo( |
| 1077 | int32_t* out, size_t frameCount, int32_t* temp __unused, int32_t* aux) |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1078 | { |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1079 | ALOGVV("track__16BitsStereo\n"); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1080 | const int16_t *in = static_cast<const int16_t *>(mIn); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1081 | |
| Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 1082 | if (CC_UNLIKELY(aux != NULL)) { |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1083 | int32_t l; |
| 1084 | int32_t r; |
| 1085 | // ramp gain |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1086 | if (CC_UNLIKELY(volumeInc[0]|volumeInc[1]|auxInc)) { |
| 1087 | int32_t vl = prevVolume[0]; |
| 1088 | int32_t vr = prevVolume[1]; |
| 1089 | int32_t va = prevAuxLevel; |
| 1090 | const int32_t vlInc = volumeInc[0]; |
| 1091 | const int32_t vrInc = volumeInc[1]; |
| 1092 | const int32_t vaInc = auxInc; |
| Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1093 | // ALOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d", |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1094 | // t, vlInc/65536.0f, vl/65536.0f, volume[0], |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1095 | // (vl + vlInc*frameCount)/65536.0f, frameCount); |
| 1096 | |
| 1097 | do { |
| 1098 | l = (int32_t)*in++; |
| 1099 | r = (int32_t)*in++; |
| 1100 | *out++ += (vl >> 16) * l; |
| 1101 | *out++ += (vr >> 16) * r; |
| 1102 | *aux++ += (va >> 17) * (l + r); |
| 1103 | vl += vlInc; |
| 1104 | vr += vrInc; |
| 1105 | va += vaInc; |
| 1106 | } while (--frameCount); |
| 1107 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1108 | prevVolume[0] = vl; |
| 1109 | prevVolume[1] = vr; |
| 1110 | prevAuxLevel = va; |
| 1111 | adjustVolumeRamp(true); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | // constant gain |
| 1115 | else { |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1116 | const uint32_t vrl = volumeRL; |
| 1117 | const int16_t va = (int16_t)auxLevel; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1118 | do { |
| Glenn Kasten | 54c3b66 | 2012-01-06 07:46:30 -0800 | [diff] [blame] | 1119 | uint32_t rl = *reinterpret_cast<const uint32_t *>(in); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1120 | int16_t a = (int16_t)(((int32_t)in[0] + in[1]) >> 1); |
| 1121 | in += 2; |
| 1122 | out[0] = mulAddRL(1, rl, vrl, out[0]); |
| 1123 | out[1] = mulAddRL(0, rl, vrl, out[1]); |
| 1124 | out += 2; |
| 1125 | aux[0] = mulAdd(a, va, aux[0]); |
| 1126 | aux++; |
| 1127 | } while (--frameCount); |
| 1128 | } |
| 1129 | } else { |
| 1130 | // ramp gain |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1131 | if (CC_UNLIKELY(volumeInc[0]|volumeInc[1])) { |
| 1132 | int32_t vl = prevVolume[0]; |
| 1133 | int32_t vr = prevVolume[1]; |
| 1134 | const int32_t vlInc = volumeInc[0]; |
| 1135 | const int32_t vrInc = volumeInc[1]; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1136 | |
| Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1137 | // ALOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d", |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1138 | // t, vlInc/65536.0f, vl/65536.0f, volume[0], |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1139 | // (vl + vlInc*frameCount)/65536.0f, frameCount); |
| 1140 | |
| 1141 | do { |
| 1142 | *out++ += (vl >> 16) * (int32_t) *in++; |
| 1143 | *out++ += (vr >> 16) * (int32_t) *in++; |
| 1144 | vl += vlInc; |
| 1145 | vr += vrInc; |
| 1146 | } while (--frameCount); |
| 1147 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1148 | prevVolume[0] = vl; |
| 1149 | prevVolume[1] = vr; |
| 1150 | adjustVolumeRamp(false); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | // constant gain |
| 1154 | else { |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1155 | const uint32_t vrl = volumeRL; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1156 | do { |
| Glenn Kasten | 54c3b66 | 2012-01-06 07:46:30 -0800 | [diff] [blame] | 1157 | uint32_t rl = *reinterpret_cast<const uint32_t *>(in); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1158 | in += 2; |
| 1159 | out[0] = mulAddRL(1, rl, vrl, out[0]); |
| 1160 | out[1] = mulAddRL(0, rl, vrl, out[1]); |
| 1161 | out += 2; |
| 1162 | } while (--frameCount); |
| 1163 | } |
| 1164 | } |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1165 | mIn = in; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1166 | } |
| 1167 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1168 | void AudioMixer::Track::track__16BitsMono( |
| 1169 | int32_t* out, size_t frameCount, int32_t* temp __unused, int32_t* aux) |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1170 | { |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1171 | ALOGVV("track__16BitsMono\n"); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1172 | const int16_t *in = static_cast<int16_t const *>(mIn); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1173 | |
| Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 1174 | if (CC_UNLIKELY(aux != NULL)) { |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1175 | // ramp gain |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1176 | if (CC_UNLIKELY(volumeInc[0]|volumeInc[1]|auxInc)) { |
| 1177 | int32_t vl = prevVolume[0]; |
| 1178 | int32_t vr = prevVolume[1]; |
| 1179 | int32_t va = prevAuxLevel; |
| 1180 | const int32_t vlInc = volumeInc[0]; |
| 1181 | const int32_t vrInc = volumeInc[1]; |
| 1182 | const int32_t vaInc = auxInc; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1183 | |
| Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1184 | // ALOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d", |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1185 | // t, vlInc/65536.0f, vl/65536.0f, volume[0], |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1186 | // (vl + vlInc*frameCount)/65536.0f, frameCount); |
| 1187 | |
| 1188 | do { |
| 1189 | int32_t l = *in++; |
| 1190 | *out++ += (vl >> 16) * l; |
| 1191 | *out++ += (vr >> 16) * l; |
| 1192 | *aux++ += (va >> 16) * l; |
| 1193 | vl += vlInc; |
| 1194 | vr += vrInc; |
| 1195 | va += vaInc; |
| 1196 | } while (--frameCount); |
| 1197 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1198 | prevVolume[0] = vl; |
| 1199 | prevVolume[1] = vr; |
| 1200 | prevAuxLevel = va; |
| 1201 | adjustVolumeRamp(true); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1202 | } |
| 1203 | // constant gain |
| 1204 | else { |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1205 | const int16_t vl = volume[0]; |
| 1206 | const int16_t vr = volume[1]; |
| 1207 | const int16_t va = (int16_t)auxLevel; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1208 | do { |
| 1209 | int16_t l = *in++; |
| 1210 | out[0] = mulAdd(l, vl, out[0]); |
| 1211 | out[1] = mulAdd(l, vr, out[1]); |
| 1212 | out += 2; |
| 1213 | aux[0] = mulAdd(l, va, aux[0]); |
| 1214 | aux++; |
| 1215 | } while (--frameCount); |
| 1216 | } |
| 1217 | } else { |
| 1218 | // ramp gain |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1219 | if (CC_UNLIKELY(volumeInc[0]|volumeInc[1])) { |
| 1220 | int32_t vl = prevVolume[0]; |
| 1221 | int32_t vr = prevVolume[1]; |
| 1222 | const int32_t vlInc = volumeInc[0]; |
| 1223 | const int32_t vrInc = volumeInc[1]; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1224 | |
| Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1225 | // ALOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d", |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1226 | // t, vlInc/65536.0f, vl/65536.0f, volume[0], |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1227 | // (vl + vlInc*frameCount)/65536.0f, frameCount); |
| 1228 | |
| 1229 | do { |
| 1230 | int32_t l = *in++; |
| 1231 | *out++ += (vl >> 16) * l; |
| 1232 | *out++ += (vr >> 16) * l; |
| 1233 | vl += vlInc; |
| 1234 | vr += vrInc; |
| 1235 | } while (--frameCount); |
| 1236 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1237 | prevVolume[0] = vl; |
| 1238 | prevVolume[1] = vr; |
| 1239 | adjustVolumeRamp(false); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1240 | } |
| 1241 | // constant gain |
| 1242 | else { |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1243 | const int16_t vl = volume[0]; |
| 1244 | const int16_t vr = volume[1]; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1245 | do { |
| 1246 | int16_t l = *in++; |
| 1247 | out[0] = mulAdd(l, vl, out[0]); |
| 1248 | out[1] = mulAdd(l, vr, out[1]); |
| 1249 | out += 2; |
| 1250 | } while (--frameCount); |
| 1251 | } |
| 1252 | } |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1253 | mIn = in; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1254 | } |
| 1255 | |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1256 | // no-op case |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1257 | void AudioMixer::process__nop() |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1258 | { |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1259 | ALOGVV("process__nop\n"); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1260 | |
| 1261 | for (const auto &pair : mGroups) { |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1262 | // process by group of tracks with same output buffer to |
| 1263 | // avoid multiple memset() on same buffer |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1264 | const auto &group = pair.second; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1265 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1266 | const std::shared_ptr<Track> &t = mTracks[group[0]]; |
| 1267 | memset(t->mainBuffer, 0, |
| 1268 | mFrameCount * t->mMixerChannelCount |
| 1269 | * audio_bytes_per_sample(t->mMixerFormat)); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1270 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1271 | // now consume data |
| 1272 | for (const int name : group) { |
| 1273 | const std::shared_ptr<Track> &t = mTracks[name]; |
| 1274 | size_t outFrames = mFrameCount; |
| 1275 | while (outFrames) { |
| 1276 | t->buffer.frameCount = outFrames; |
| 1277 | t->bufferProvider->getNextBuffer(&t->buffer); |
| 1278 | if (t->buffer.raw == NULL) break; |
| 1279 | outFrames -= t->buffer.frameCount; |
| 1280 | t->bufferProvider->releaseBuffer(&t->buffer); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1281 | } |
| 1282 | } |
| 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | // generic code without resampling |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1287 | void AudioMixer::process__genericNoResampling() |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1288 | { |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1289 | ALOGVV("process__genericNoResampling\n"); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1290 | int32_t outTemp[BLOCKSIZE * MAX_NUM_CHANNELS] __attribute__((aligned(32))); |
| 1291 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1292 | for (const auto &pair : mGroups) { |
| 1293 | // process by group of tracks with same output main buffer to |
| 1294 | // avoid multiple memset() on same buffer |
| 1295 | const auto &group = pair.second; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1296 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1297 | // acquire buffer |
| 1298 | for (const int name : group) { |
| 1299 | const std::shared_ptr<Track> &t = mTracks[name]; |
| 1300 | t->buffer.frameCount = mFrameCount; |
| 1301 | t->bufferProvider->getNextBuffer(&t->buffer); |
| 1302 | t->frameCount = t->buffer.frameCount; |
| 1303 | t->mIn = t->buffer.raw; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1304 | } |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1305 | |
| 1306 | int32_t *out = (int *)pair.first; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1307 | size_t numFrames = 0; |
| 1308 | do { |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1309 | const size_t frameCount = std::min((size_t)BLOCKSIZE, mFrameCount - numFrames); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1310 | memset(outTemp, 0, sizeof(outTemp)); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1311 | for (const int name : group) { |
| 1312 | const std::shared_ptr<Track> &t = mTracks[name]; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1313 | int32_t *aux = NULL; |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1314 | if (CC_UNLIKELY(t->needs & NEEDS_AUX)) { |
| 1315 | aux = t->auxBuffer + numFrames; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1316 | } |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1317 | for (int outFrames = frameCount; outFrames > 0; ) { |
| 1318 | // t->in == nullptr can happen if the track was flushed just after having |
| Gaurav Kumar | 7e79cd2 | 2014-01-06 10:57:18 +0530 | [diff] [blame] | 1319 | // been enabled for mixing. |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1320 | if (t->mIn == nullptr) { |
| Gaurav Kumar | 7e79cd2 | 2014-01-06 10:57:18 +0530 | [diff] [blame] | 1321 | break; |
| 1322 | } |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1323 | size_t inFrames = (t->frameCount > outFrames)?outFrames:t->frameCount; |
| Glenn Kasten | 34fca34 | 2013-08-13 09:48:14 -0700 | [diff] [blame] | 1324 | if (inFrames > 0) { |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1325 | (t.get()->*t->hook)( |
| 1326 | outTemp + (frameCount - outFrames) * t->mMixerChannelCount, |
| 1327 | inFrames, mResampleTemp.get() /* naked ptr */, aux); |
| 1328 | t->frameCount -= inFrames; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1329 | outFrames -= inFrames; |
| Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 1330 | if (CC_UNLIKELY(aux != NULL)) { |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1331 | aux += inFrames; |
| 1332 | } |
| 1333 | } |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1334 | if (t->frameCount == 0 && outFrames) { |
| 1335 | t->bufferProvider->releaseBuffer(&t->buffer); |
| 1336 | t->buffer.frameCount = (mFrameCount - numFrames) - |
| Yahan Zhou | c1c11b4 | 2018-01-16 12:44:04 -0800 | [diff] [blame] | 1337 | (frameCount - outFrames); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1338 | t->bufferProvider->getNextBuffer(&t->buffer); |
| 1339 | t->mIn = t->buffer.raw; |
| 1340 | if (t->mIn == nullptr) { |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1341 | break; |
| 1342 | } |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1343 | t->frameCount = t->buffer.frameCount; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1344 | } |
| 1345 | } |
| 1346 | } |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1347 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1348 | const std::shared_ptr<Track> &t1 = mTracks[group[0]]; |
| 1349 | convertMixerFormat(out, t1->mMixerFormat, outTemp, t1->mMixerInFormat, |
| 1350 | frameCount * t1->mMixerChannelCount); |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1351 | // TODO: fix ugly casting due to choice of out pointer type |
| 1352 | out = reinterpret_cast<int32_t*>((uint8_t*)out |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1353 | + frameCount * t1->mMixerChannelCount |
| 1354 | * audio_bytes_per_sample(t1->mMixerFormat)); |
| Yahan Zhou | c1c11b4 | 2018-01-16 12:44:04 -0800 | [diff] [blame] | 1355 | numFrames += frameCount; |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1356 | } while (numFrames < mFrameCount); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1357 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1358 | // release each track's buffer |
| 1359 | for (const int name : group) { |
| 1360 | const std::shared_ptr<Track> &t = mTracks[name]; |
| 1361 | t->bufferProvider->releaseBuffer(&t->buffer); |
| 1362 | } |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1363 | } |
| 1364 | } |
| 1365 | |
| Glenn Kasten | c5ac4cb | 2011-12-12 09:05:55 -0800 | [diff] [blame] | 1366 | // generic code with resampling |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1367 | void AudioMixer::process__genericResampling() |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1368 | { |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1369 | ALOGVV("process__genericResampling\n"); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1370 | int32_t * const outTemp = mOutputTemp.get(); // naked ptr |
| 1371 | size_t numFrames = mFrameCount; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1372 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1373 | for (const auto &pair : mGroups) { |
| 1374 | const auto &group = pair.second; |
| 1375 | const std::shared_ptr<Track> &t1 = mTracks[group[0]]; |
| 1376 | |
| 1377 | // clear temp buffer |
| 1378 | memset(outTemp, 0, sizeof(*outTemp) * t1->mMixerChannelCount * mFrameCount); |
| 1379 | for (const int name : group) { |
| 1380 | const std::shared_ptr<Track> &t = mTracks[name]; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1381 | int32_t *aux = NULL; |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1382 | if (CC_UNLIKELY(t->needs & NEEDS_AUX)) { |
| 1383 | aux = t->auxBuffer; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | // this is a little goofy, on the resampling case we don't |
| 1387 | // acquire/release the buffers because it's done by |
| 1388 | // the resampler. |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1389 | if (t->needs & NEEDS_RESAMPLE) { |
| 1390 | (t.get()->*t->hook)(outTemp, numFrames, mResampleTemp.get() /* naked ptr */, aux); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1391 | } else { |
| 1392 | |
| 1393 | size_t outFrames = 0; |
| 1394 | |
| 1395 | while (outFrames < numFrames) { |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1396 | t->buffer.frameCount = numFrames - outFrames; |
| 1397 | t->bufferProvider->getNextBuffer(&t->buffer); |
| 1398 | t->mIn = t->buffer.raw; |
| 1399 | // t->mIn == nullptr can happen if the track was flushed just after having |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1400 | // been enabled for mixing. |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1401 | if (t->mIn == nullptr) break; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1402 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1403 | (t.get()->*t->hook)( |
| 1404 | outTemp + outFrames * t->mMixerChannelCount, t->buffer.frameCount, |
| Andy Hung | a601889 | 2018-02-21 14:32:16 -0800 | [diff] [blame] | 1405 | mResampleTemp.get() /* naked ptr */, |
| 1406 | aux != nullptr ? aux + outFrames : nullptr); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1407 | outFrames += t->buffer.frameCount; |
| Andy Hung | a601889 | 2018-02-21 14:32:16 -0800 | [diff] [blame] | 1408 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1409 | t->bufferProvider->releaseBuffer(&t->buffer); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1410 | } |
| 1411 | } |
| 1412 | } |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1413 | convertMixerFormat(t1->mainBuffer, t1->mMixerFormat, |
| 1414 | outTemp, t1->mMixerInFormat, numFrames * t1->mMixerChannelCount); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | // one track, 16 bits stereo without resampling is the most common case |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1419 | void AudioMixer::process__oneTrack16BitsStereoNoResampling() |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1420 | { |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1421 | ALOGVV("process__oneTrack16BitsStereoNoResampling\n"); |
| 1422 | LOG_ALWAYS_FATAL_IF(mEnabled.size() != 0, |
| 1423 | "%zu != 1 tracks enabled", mEnabled.size()); |
| 1424 | const int name = mEnabled[0]; |
| 1425 | const std::shared_ptr<Track> &t = mTracks[name]; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1426 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1427 | AudioBufferProvider::Buffer& b(t->buffer); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1428 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1429 | int32_t* out = t->mainBuffer; |
| Andy Hung | f8a106a | 2014-05-29 18:52:38 -0700 | [diff] [blame] | 1430 | float *fout = reinterpret_cast<float*>(out); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1431 | size_t numFrames = mFrameCount; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1432 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1433 | const int16_t vl = t->volume[0]; |
| 1434 | const int16_t vr = t->volume[1]; |
| 1435 | const uint32_t vrl = t->volumeRL; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1436 | while (numFrames) { |
| 1437 | b.frameCount = numFrames; |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1438 | t->bufferProvider->getNextBuffer(&b); |
| Glenn Kasten | 54c3b66 | 2012-01-06 07:46:30 -0800 | [diff] [blame] | 1439 | const int16_t *in = b.i16; |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1440 | |
| 1441 | // in == NULL can happen if the track was flushed just after having |
| 1442 | // been enabled for mixing. |
| Andy Hung | f8a106a | 2014-05-29 18:52:38 -0700 | [diff] [blame] | 1443 | if (in == NULL || (((uintptr_t)in) & 3)) { |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1444 | if ( AUDIO_FORMAT_PCM_FLOAT == t->mMixerFormat ) { |
| Jinguang Dong | 7c5ec03 | 2016-11-14 19:57:14 +0800 | [diff] [blame] | 1445 | memset((char*)fout, 0, numFrames |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1446 | * t->mMixerChannelCount * audio_bytes_per_sample(t->mMixerFormat)); |
| Jinguang Dong | 7c5ec03 | 2016-11-14 19:57:14 +0800 | [diff] [blame] | 1447 | } else { |
| 1448 | memset((char*)out, 0, numFrames |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1449 | * t->mMixerChannelCount * audio_bytes_per_sample(t->mMixerFormat)); |
| Jinguang Dong | 7c5ec03 | 2016-11-14 19:57:14 +0800 | [diff] [blame] | 1450 | } |
| Andy Hung | 395db4b | 2014-08-25 17:15:29 -0700 | [diff] [blame] | 1451 | ALOGE_IF((((uintptr_t)in) & 3), |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1452 | "process__oneTrack16BitsStereoNoResampling: misaligned buffer" |
| Andy Hung | 395db4b | 2014-08-25 17:15:29 -0700 | [diff] [blame] | 1453 | " %p track %d, channels %d, needs %08x, volume %08x vfl %f vfr %f", |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1454 | in, name, t->channelCount, t->needs, vrl, t->mVolume[0], t->mVolume[1]); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1455 | return; |
| 1456 | } |
| 1457 | size_t outFrames = b.frameCount; |
| 1458 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1459 | switch (t->mMixerFormat) { |
| Andy Hung | f8a106a | 2014-05-29 18:52:38 -0700 | [diff] [blame] | 1460 | case AUDIO_FORMAT_PCM_FLOAT: |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1461 | do { |
| Glenn Kasten | 54c3b66 | 2012-01-06 07:46:30 -0800 | [diff] [blame] | 1462 | uint32_t rl = *reinterpret_cast<const uint32_t *>(in); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1463 | in += 2; |
| Andy Hung | a1ab7cc | 2014-02-24 19:26:52 -0800 | [diff] [blame] | 1464 | int32_t l = mulRL(1, rl, vrl); |
| 1465 | int32_t r = mulRL(0, rl, vrl); |
| Andy Hung | 84a0c6e | 2014-04-02 11:24:53 -0700 | [diff] [blame] | 1466 | *fout++ = float_from_q4_27(l); |
| 1467 | *fout++ = float_from_q4_27(r); |
| Andy Hung | 3375bde | 2014-02-28 15:51:47 -0800 | [diff] [blame] | 1468 | // Note: In case of later int16_t sink output, |
| 1469 | // conversion and clamping is done by memcpy_to_i16_from_float(). |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1470 | } while (--outFrames); |
| Andy Hung | f8a106a | 2014-05-29 18:52:38 -0700 | [diff] [blame] | 1471 | break; |
| Andy Hung | a1ab7cc | 2014-02-24 19:26:52 -0800 | [diff] [blame] | 1472 | case AUDIO_FORMAT_PCM_16_BIT: |
| Andy Hung | 97ae824 | 2014-05-30 10:35:47 -0700 | [diff] [blame] | 1473 | if (CC_UNLIKELY(uint32_t(vl) > UNITY_GAIN_INT || uint32_t(vr) > UNITY_GAIN_INT)) { |
| Andy Hung | a1ab7cc | 2014-02-24 19:26:52 -0800 | [diff] [blame] | 1474 | // volume is boosted, so we might need to clamp even though |
| 1475 | // we process only one track. |
| 1476 | do { |
| 1477 | uint32_t rl = *reinterpret_cast<const uint32_t *>(in); |
| 1478 | in += 2; |
| 1479 | int32_t l = mulRL(1, rl, vrl) >> 12; |
| 1480 | int32_t r = mulRL(0, rl, vrl) >> 12; |
| 1481 | // clamping... |
| 1482 | l = clamp16(l); |
| 1483 | r = clamp16(r); |
| 1484 | *out++ = (r<<16) | (l & 0xFFFF); |
| 1485 | } while (--outFrames); |
| 1486 | } else { |
| 1487 | do { |
| 1488 | uint32_t rl = *reinterpret_cast<const uint32_t *>(in); |
| 1489 | in += 2; |
| 1490 | int32_t l = mulRL(1, rl, vrl) >> 12; |
| 1491 | int32_t r = mulRL(0, rl, vrl) >> 12; |
| 1492 | *out++ = (r<<16) | (l & 0xFFFF); |
| 1493 | } while (--outFrames); |
| 1494 | } |
| 1495 | break; |
| 1496 | default: |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1497 | LOG_ALWAYS_FATAL("bad mixer format: %d", t->mMixerFormat); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1498 | } |
| 1499 | numFrames -= b.frameCount; |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1500 | t->bufferProvider->releaseBuffer(&b); |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1501 | } |
| 1502 | } |
| 1503 | |
| Glenn Kasten | 52008f8 | 2012-03-18 09:34:41 -0700 | [diff] [blame] | 1504 | /*static*/ pthread_once_t AudioMixer::sOnceControl = PTHREAD_ONCE_INIT; |
| 1505 | |
| 1506 | /*static*/ void AudioMixer::sInitRoutine() |
| 1507 | { |
| Andy Hung | 34803d5 | 2014-07-16 21:41:35 -0700 | [diff] [blame] | 1508 | DownmixerBufferProvider::init(); // for the downmixer |
| John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1509 | } |
| 1510 | |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 1511 | /* TODO: consider whether this level of optimization is necessary. |
| 1512 | * Perhaps just stick with a single for loop. |
| 1513 | */ |
| 1514 | |
| 1515 | // Needs to derive a compile time constant (constexpr). Could be targeted to go |
| 1516 | // to a MONOVOL mixtype based on MAX_NUM_VOLUMES, but that's an unnecessary complication. |
| Chih-Hung Hsieh | bf29173 | 2016-05-17 15:16:07 -0700 | [diff] [blame] | 1517 | #define MIXTYPE_MONOVOL(mixtype) ((mixtype) == MIXTYPE_MULTI ? MIXTYPE_MULTI_MONOVOL : \ |
| 1518 | (mixtype) == MIXTYPE_MULTI_SAVEONLY ? MIXTYPE_MULTI_SAVEONLY_MONOVOL : (mixtype)) |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 1519 | |
| 1520 | /* MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration) |
| 1521 | * TO: int32_t (Q4.27) or float |
| 1522 | * TI: int32_t (Q4.27) or int16_t (Q0.15) or float |
| Andy Hung | 116a498 | 2017-11-30 10:15:08 -0800 | [diff] [blame] | 1523 | * TA: int32_t (Q4.27) or float |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 1524 | */ |
| 1525 | template <int MIXTYPE, |
| 1526 | typename TO, typename TI, typename TV, typename TA, typename TAV> |
| 1527 | static void volumeRampMulti(uint32_t channels, TO* out, size_t frameCount, |
| 1528 | const TI* in, TA* aux, TV *vol, const TV *volinc, TAV *vola, TAV volainc) |
| 1529 | { |
| 1530 | switch (channels) { |
| 1531 | case 1: |
| 1532 | volumeRampMulti<MIXTYPE, 1>(out, frameCount, in, aux, vol, volinc, vola, volainc); |
| 1533 | break; |
| 1534 | case 2: |
| 1535 | volumeRampMulti<MIXTYPE, 2>(out, frameCount, in, aux, vol, volinc, vola, volainc); |
| 1536 | break; |
| 1537 | case 3: |
| 1538 | volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 3>(out, |
| 1539 | frameCount, in, aux, vol, volinc, vola, volainc); |
| 1540 | break; |
| 1541 | case 4: |
| 1542 | volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 4>(out, |
| 1543 | frameCount, in, aux, vol, volinc, vola, volainc); |
| 1544 | break; |
| 1545 | case 5: |
| 1546 | volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 5>(out, |
| 1547 | frameCount, in, aux, vol, volinc, vola, volainc); |
| 1548 | break; |
| 1549 | case 6: |
| 1550 | volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 6>(out, |
| 1551 | frameCount, in, aux, vol, volinc, vola, volainc); |
| 1552 | break; |
| 1553 | case 7: |
| 1554 | volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 7>(out, |
| 1555 | frameCount, in, aux, vol, volinc, vola, volainc); |
| 1556 | break; |
| 1557 | case 8: |
| 1558 | volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 8>(out, |
| 1559 | frameCount, in, aux, vol, volinc, vola, volainc); |
| 1560 | break; |
| 1561 | } |
| 1562 | } |
| 1563 | |
| 1564 | /* MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration) |
| 1565 | * TO: int32_t (Q4.27) or float |
| 1566 | * TI: int32_t (Q4.27) or int16_t (Q0.15) or float |
| Andy Hung | 116a498 | 2017-11-30 10:15:08 -0800 | [diff] [blame] | 1567 | * TA: int32_t (Q4.27) or float |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 1568 | */ |
| 1569 | template <int MIXTYPE, |
| 1570 | typename TO, typename TI, typename TV, typename TA, typename TAV> |
| 1571 | static void volumeMulti(uint32_t channels, TO* out, size_t frameCount, |
| 1572 | const TI* in, TA* aux, const TV *vol, TAV vola) |
| 1573 | { |
| 1574 | switch (channels) { |
| 1575 | case 1: |
| 1576 | volumeMulti<MIXTYPE, 1>(out, frameCount, in, aux, vol, vola); |
| 1577 | break; |
| 1578 | case 2: |
| 1579 | volumeMulti<MIXTYPE, 2>(out, frameCount, in, aux, vol, vola); |
| 1580 | break; |
| 1581 | case 3: |
| 1582 | volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 3>(out, frameCount, in, aux, vol, vola); |
| 1583 | break; |
| 1584 | case 4: |
| 1585 | volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 4>(out, frameCount, in, aux, vol, vola); |
| 1586 | break; |
| 1587 | case 5: |
| 1588 | volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 5>(out, frameCount, in, aux, vol, vola); |
| 1589 | break; |
| 1590 | case 6: |
| 1591 | volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 6>(out, frameCount, in, aux, vol, vola); |
| 1592 | break; |
| 1593 | case 7: |
| 1594 | volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 7>(out, frameCount, in, aux, vol, vola); |
| 1595 | break; |
| 1596 | case 8: |
| 1597 | volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 8>(out, frameCount, in, aux, vol, vola); |
| 1598 | break; |
| 1599 | } |
| 1600 | } |
| 1601 | |
| 1602 | /* MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration) |
| 1603 | * USEFLOATVOL (set to true if float volume is used) |
| 1604 | * ADJUSTVOL (set to true if volume ramp parameters needs adjustment afterwards) |
| 1605 | * TO: int32_t (Q4.27) or float |
| 1606 | * TI: int32_t (Q4.27) or int16_t (Q0.15) or float |
| Andy Hung | 116a498 | 2017-11-30 10:15:08 -0800 | [diff] [blame] | 1607 | * TA: int32_t (Q4.27) or float |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 1608 | */ |
| 1609 | template <int MIXTYPE, bool USEFLOATVOL, bool ADJUSTVOL, |
| Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 1610 | typename TO, typename TI, typename TA> |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1611 | void AudioMixer::Track::volumeMix(TO *out, size_t outFrames, |
| 1612 | const TI *in, TA *aux, bool ramp) |
| Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 1613 | { |
| 1614 | if (USEFLOATVOL) { |
| 1615 | if (ramp) { |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1616 | volumeRampMulti<MIXTYPE>(mMixerChannelCount, out, outFrames, in, aux, |
| 1617 | mPrevVolume, mVolumeInc, |
| Andy Hung | 116a498 | 2017-11-30 10:15:08 -0800 | [diff] [blame] | 1618 | #ifdef FLOAT_AUX |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1619 | &mPrevAuxLevel, mAuxInc |
| Andy Hung | 116a498 | 2017-11-30 10:15:08 -0800 | [diff] [blame] | 1620 | #else |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1621 | &prevAuxLevel, auxInc |
| Andy Hung | 116a498 | 2017-11-30 10:15:08 -0800 | [diff] [blame] | 1622 | #endif |
| 1623 | ); |
| Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 1624 | if (ADJUSTVOL) { |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1625 | adjustVolumeRamp(aux != NULL, true); |
| Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 1626 | } |
| 1627 | } else { |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1628 | volumeMulti<MIXTYPE>(mMixerChannelCount, out, outFrames, in, aux, |
| 1629 | mVolume, |
| Andy Hung | 116a498 | 2017-11-30 10:15:08 -0800 | [diff] [blame] | 1630 | #ifdef FLOAT_AUX |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1631 | mAuxLevel |
| Andy Hung | 116a498 | 2017-11-30 10:15:08 -0800 | [diff] [blame] | 1632 | #else |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1633 | auxLevel |
| Andy Hung | 116a498 | 2017-11-30 10:15:08 -0800 | [diff] [blame] | 1634 | #endif |
| 1635 | ); |
| Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 1636 | } |
| 1637 | } else { |
| 1638 | if (ramp) { |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1639 | volumeRampMulti<MIXTYPE>(mMixerChannelCount, out, outFrames, in, aux, |
| 1640 | prevVolume, volumeInc, &prevAuxLevel, auxInc); |
| Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 1641 | if (ADJUSTVOL) { |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1642 | adjustVolumeRamp(aux != NULL); |
| Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 1643 | } |
| 1644 | } else { |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1645 | volumeMulti<MIXTYPE>(mMixerChannelCount, out, outFrames, in, aux, |
| 1646 | volume, auxLevel); |
| Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 1647 | } |
| 1648 | } |
| 1649 | } |
| 1650 | |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1651 | /* This process hook is called when there is a single track without |
| 1652 | * aux buffer, volume ramp, or resampling. |
| 1653 | * TODO: Update the hook selection: this can properly handle aux and ramp. |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 1654 | * |
| 1655 | * MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration) |
| 1656 | * TO: int32_t (Q4.27) or float |
| 1657 | * TI: int32_t (Q4.27) or int16_t (Q0.15) or float |
| 1658 | * TA: int32_t (Q4.27) |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1659 | */ |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 1660 | template <int MIXTYPE, typename TO, typename TI, typename TA> |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1661 | void AudioMixer::process__noResampleOneTrack() |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1662 | { |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1663 | ALOGVV("process__noResampleOneTrack\n"); |
| 1664 | LOG_ALWAYS_FATAL_IF(mEnabled.size() != 1, |
| 1665 | "%zu != 1 tracks enabled", mEnabled.size()); |
| 1666 | const std::shared_ptr<Track> &t = mTracks[mEnabled[0]]; |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 1667 | const uint32_t channels = t->mMixerChannelCount; |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1668 | TO* out = reinterpret_cast<TO*>(t->mainBuffer); |
| 1669 | TA* aux = reinterpret_cast<TA*>(t->auxBuffer); |
| 1670 | const bool ramp = t->needsRamp(); |
| 1671 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1672 | for (size_t numFrames = mFrameCount; numFrames > 0; ) { |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1673 | AudioBufferProvider::Buffer& b(t->buffer); |
| 1674 | // get input buffer |
| 1675 | b.frameCount = numFrames; |
| Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 1676 | t->bufferProvider->getNextBuffer(&b); |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1677 | const TI *in = reinterpret_cast<TI*>(b.raw); |
| 1678 | |
| 1679 | // in == NULL can happen if the track was flushed just after having |
| 1680 | // been enabled for mixing. |
| 1681 | if (in == NULL || (((uintptr_t)in) & 3)) { |
| 1682 | memset(out, 0, numFrames |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 1683 | * channels * audio_bytes_per_sample(t->mMixerFormat)); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1684 | ALOGE_IF((((uintptr_t)in) & 3), "process__noResampleOneTrack: bus error: " |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1685 | "buffer %p track %p, channels %d, needs %#x", |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1686 | in, &t, t->channelCount, t->needs); |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1687 | return; |
| 1688 | } |
| 1689 | |
| 1690 | const size_t outFrames = b.frameCount; |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1691 | t->volumeMix<MIXTYPE, is_same<TI, float>::value /* USEFLOATVOL */, false /* ADJUSTVOL */> ( |
| 1692 | out, outFrames, in, aux, ramp); |
| Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 1693 | |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 1694 | out += outFrames * channels; |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1695 | if (aux != NULL) { |
| Andy Hung | a601889 | 2018-02-21 14:32:16 -0800 | [diff] [blame] | 1696 | aux += outFrames; |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1697 | } |
| 1698 | numFrames -= b.frameCount; |
| 1699 | |
| 1700 | // release buffer |
| 1701 | t->bufferProvider->releaseBuffer(&b); |
| 1702 | } |
| 1703 | if (ramp) { |
| Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 1704 | t->adjustVolumeRamp(aux != NULL, is_same<TI, float>::value); |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1705 | } |
| 1706 | } |
| 1707 | |
| 1708 | /* This track hook is called to do resampling then mixing, |
| 1709 | * pulling from the track's upstream AudioBufferProvider. |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 1710 | * |
| 1711 | * MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration) |
| 1712 | * TO: int32_t (Q4.27) or float |
| 1713 | * TI: int32_t (Q4.27) or int16_t (Q0.15) or float |
| Andy Hung | 116a498 | 2017-11-30 10:15:08 -0800 | [diff] [blame] | 1714 | * TA: int32_t (Q4.27) or float |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1715 | */ |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 1716 | template <int MIXTYPE, typename TO, typename TI, typename TA> |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1717 | void AudioMixer::Track::track__Resample(TO* out, size_t outFrameCount, TO* temp, TA* aux) |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1718 | { |
| 1719 | ALOGVV("track__Resample\n"); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1720 | mResampler->setSampleRate(sampleRate); |
| 1721 | const bool ramp = needsRamp(); |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1722 | if (ramp || aux != NULL) { |
| 1723 | // if ramp: resample with unity gain to temp buffer and scale/mix in 2nd step. |
| 1724 | // if aux != NULL: resample with unity gain to temp buffer then apply send level. |
| 1725 | |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1726 | mResampler->setVolume(UNITY_GAIN_FLOAT, UNITY_GAIN_FLOAT); |
| 1727 | memset(temp, 0, outFrameCount * mMixerChannelCount * sizeof(TO)); |
| 1728 | mResampler->resample((int32_t*)temp, outFrameCount, bufferProvider); |
| Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 1729 | |
| Andy Hung | 116a498 | 2017-11-30 10:15:08 -0800 | [diff] [blame] | 1730 | volumeMix<MIXTYPE, is_same<TI, float>::value /* USEFLOATVOL */, true /* ADJUSTVOL */>( |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1731 | out, outFrameCount, temp, aux, ramp); |
| Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 1732 | |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1733 | } else { // constant volume gain |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1734 | mResampler->setVolume(mVolume[0], mVolume[1]); |
| 1735 | mResampler->resample((int32_t*)out, outFrameCount, bufferProvider); |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1736 | } |
| 1737 | } |
| 1738 | |
| 1739 | /* This track hook is called to mix a track, when no resampling is required. |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1740 | * The input buffer should be present in in. |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 1741 | * |
| 1742 | * MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration) |
| 1743 | * TO: int32_t (Q4.27) or float |
| 1744 | * TI: int32_t (Q4.27) or int16_t (Q0.15) or float |
| Andy Hung | 116a498 | 2017-11-30 10:15:08 -0800 | [diff] [blame] | 1745 | * TA: int32_t (Q4.27) or float |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1746 | */ |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 1747 | template <int MIXTYPE, typename TO, typename TI, typename TA> |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1748 | void AudioMixer::Track::track__NoResample(TO* out, size_t frameCount, TO* temp __unused, TA* aux) |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1749 | { |
| 1750 | ALOGVV("track__NoResample\n"); |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1751 | const TI *in = static_cast<const TI *>(mIn); |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1752 | |
| Andy Hung | 116a498 | 2017-11-30 10:15:08 -0800 | [diff] [blame] | 1753 | volumeMix<MIXTYPE, is_same<TI, float>::value /* USEFLOATVOL */, true /* ADJUSTVOL */>( |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1754 | out, frameCount, in, aux, needsRamp()); |
| Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 1755 | |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1756 | // MIXTYPE_MONOEXPAND reads a single input channel and expands to NCHAN output channels. |
| 1757 | // MIXTYPE_MULTI reads NCHAN input channels and places to NCHAN output channels. |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1758 | in += (MIXTYPE == MIXTYPE_MONOEXPAND) ? frameCount : frameCount * mMixerChannelCount; |
| 1759 | mIn = in; |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1760 | } |
| 1761 | |
| 1762 | /* The Mixer engine generates either int32_t (Q4_27) or float data. |
| 1763 | * We use this function to convert the engine buffers |
| 1764 | * to the desired mixer output format, either int16_t (Q.15) or float. |
| 1765 | */ |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1766 | /* static */ |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1767 | void AudioMixer::convertMixerFormat(void *out, audio_format_t mixerOutFormat, |
| 1768 | void *in, audio_format_t mixerInFormat, size_t sampleCount) |
| 1769 | { |
| 1770 | switch (mixerInFormat) { |
| 1771 | case AUDIO_FORMAT_PCM_FLOAT: |
| 1772 | switch (mixerOutFormat) { |
| 1773 | case AUDIO_FORMAT_PCM_FLOAT: |
| 1774 | memcpy(out, in, sampleCount * sizeof(float)); // MEMCPY. TODO optimize out |
| 1775 | break; |
| 1776 | case AUDIO_FORMAT_PCM_16_BIT: |
| 1777 | memcpy_to_i16_from_float((int16_t*)out, (float*)in, sampleCount); |
| 1778 | break; |
| 1779 | default: |
| 1780 | LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat); |
| 1781 | break; |
| 1782 | } |
| 1783 | break; |
| 1784 | case AUDIO_FORMAT_PCM_16_BIT: |
| 1785 | switch (mixerOutFormat) { |
| 1786 | case AUDIO_FORMAT_PCM_FLOAT: |
| Andy Hung | 5effdf6 | 2017-11-27 13:51:40 -0800 | [diff] [blame] | 1787 | memcpy_to_float_from_q4_27((float*)out, (const int32_t*)in, sampleCount); |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1788 | break; |
| 1789 | case AUDIO_FORMAT_PCM_16_BIT: |
| Andy Hung | 5effdf6 | 2017-11-27 13:51:40 -0800 | [diff] [blame] | 1790 | memcpy_to_i16_from_q4_27((int16_t*)out, (const int32_t*)in, sampleCount); |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1791 | break; |
| 1792 | default: |
| 1793 | LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat); |
| 1794 | break; |
| 1795 | } |
| 1796 | break; |
| 1797 | default: |
| 1798 | LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat); |
| 1799 | break; |
| 1800 | } |
| 1801 | } |
| 1802 | |
| 1803 | /* Returns the proper track hook to use for mixing the track into the output buffer. |
| 1804 | */ |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1805 | /* static */ |
| 1806 | AudioMixer::hook_t AudioMixer::Track::getTrackHook(int trackType, uint32_t channelCount, |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1807 | audio_format_t mixerInFormat, audio_format_t mixerOutFormat __unused) |
| 1808 | { |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 1809 | if (!kUseNewMixer && channelCount == FCC_2 && mixerInFormat == AUDIO_FORMAT_PCM_16_BIT) { |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1810 | switch (trackType) { |
| 1811 | case TRACKTYPE_NOP: |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1812 | return &Track::track__nop; |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1813 | case TRACKTYPE_RESAMPLE: |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1814 | return &Track::track__genericResample; |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1815 | case TRACKTYPE_NORESAMPLEMONO: |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1816 | return &Track::track__16BitsMono; |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1817 | case TRACKTYPE_NORESAMPLE: |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1818 | return &Track::track__16BitsStereo; |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1819 | default: |
| 1820 | LOG_ALWAYS_FATAL("bad trackType: %d", trackType); |
| 1821 | break; |
| 1822 | } |
| 1823 | } |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 1824 | LOG_ALWAYS_FATAL_IF(channelCount > MAX_NUM_CHANNELS); |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1825 | switch (trackType) { |
| 1826 | case TRACKTYPE_NOP: |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1827 | return &Track::track__nop; |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1828 | case TRACKTYPE_RESAMPLE: |
| 1829 | switch (mixerInFormat) { |
| 1830 | case AUDIO_FORMAT_PCM_FLOAT: |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1831 | return (AudioMixer::hook_t) &Track::track__Resample< |
| Andy Hung | 116a498 | 2017-11-30 10:15:08 -0800 | [diff] [blame] | 1832 | MIXTYPE_MULTI, float /*TO*/, float /*TI*/, TYPE_AUX>; |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1833 | case AUDIO_FORMAT_PCM_16_BIT: |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1834 | return (AudioMixer::hook_t) &Track::track__Resample< |
| Andy Hung | 116a498 | 2017-11-30 10:15:08 -0800 | [diff] [blame] | 1835 | MIXTYPE_MULTI, int32_t /*TO*/, int16_t /*TI*/, TYPE_AUX>; |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1836 | default: |
| 1837 | LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat); |
| 1838 | break; |
| 1839 | } |
| 1840 | break; |
| 1841 | case TRACKTYPE_NORESAMPLEMONO: |
| 1842 | switch (mixerInFormat) { |
| 1843 | case AUDIO_FORMAT_PCM_FLOAT: |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1844 | return (AudioMixer::hook_t) &Track::track__NoResample< |
| Andy Hung | 116a498 | 2017-11-30 10:15:08 -0800 | [diff] [blame] | 1845 | MIXTYPE_MONOEXPAND, float /*TO*/, float /*TI*/, TYPE_AUX>; |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1846 | case AUDIO_FORMAT_PCM_16_BIT: |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1847 | return (AudioMixer::hook_t) &Track::track__NoResample< |
| Andy Hung | 116a498 | 2017-11-30 10:15:08 -0800 | [diff] [blame] | 1848 | MIXTYPE_MONOEXPAND, int32_t /*TO*/, int16_t /*TI*/, TYPE_AUX>; |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1849 | default: |
| 1850 | LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat); |
| 1851 | break; |
| 1852 | } |
| 1853 | break; |
| 1854 | case TRACKTYPE_NORESAMPLE: |
| 1855 | switch (mixerInFormat) { |
| 1856 | case AUDIO_FORMAT_PCM_FLOAT: |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1857 | return (AudioMixer::hook_t) &Track::track__NoResample< |
| Andy Hung | 116a498 | 2017-11-30 10:15:08 -0800 | [diff] [blame] | 1858 | MIXTYPE_MULTI, float /*TO*/, float /*TI*/, TYPE_AUX>; |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1859 | case AUDIO_FORMAT_PCM_16_BIT: |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1860 | return (AudioMixer::hook_t) &Track::track__NoResample< |
| Andy Hung | 116a498 | 2017-11-30 10:15:08 -0800 | [diff] [blame] | 1861 | MIXTYPE_MULTI, int32_t /*TO*/, int16_t /*TI*/, TYPE_AUX>; |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1862 | default: |
| 1863 | LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat); |
| 1864 | break; |
| 1865 | } |
| 1866 | break; |
| 1867 | default: |
| 1868 | LOG_ALWAYS_FATAL("bad trackType: %d", trackType); |
| 1869 | break; |
| 1870 | } |
| 1871 | return NULL; |
| 1872 | } |
| 1873 | |
| 1874 | /* Returns the proper process hook for mixing tracks. Currently works only for |
| 1875 | * PROCESSTYPE_NORESAMPLEONETRACK, a mix involving one track, no resampling. |
| Andy Hung | 395db4b | 2014-08-25 17:15:29 -0700 | [diff] [blame] | 1876 | * |
| 1877 | * TODO: Due to the special mixing considerations of duplicating to |
| 1878 | * a stereo output track, the input track cannot be MONO. This should be |
| 1879 | * prevented by the caller. |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1880 | */ |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1881 | /* static */ |
| 1882 | AudioMixer::process_hook_t AudioMixer::getProcessHook( |
| 1883 | int processType, uint32_t channelCount, |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1884 | audio_format_t mixerInFormat, audio_format_t mixerOutFormat) |
| 1885 | { |
| 1886 | if (processType != PROCESSTYPE_NORESAMPLEONETRACK) { // Only NORESAMPLEONETRACK |
| 1887 | LOG_ALWAYS_FATAL("bad processType: %d", processType); |
| 1888 | return NULL; |
| 1889 | } |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 1890 | if (!kUseNewMixer && channelCount == FCC_2 && mixerInFormat == AUDIO_FORMAT_PCM_16_BIT) { |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1891 | return &AudioMixer::process__oneTrack16BitsStereoNoResampling; |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1892 | } |
| Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 1893 | LOG_ALWAYS_FATAL_IF(channelCount > MAX_NUM_CHANNELS); |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1894 | switch (mixerInFormat) { |
| 1895 | case AUDIO_FORMAT_PCM_FLOAT: |
| 1896 | switch (mixerOutFormat) { |
| 1897 | case AUDIO_FORMAT_PCM_FLOAT: |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1898 | return &AudioMixer::process__noResampleOneTrack< |
| Andy Hung | 116a498 | 2017-11-30 10:15:08 -0800 | [diff] [blame] | 1899 | MIXTYPE_MULTI_SAVEONLY, float /*TO*/, float /*TI*/, TYPE_AUX>; |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1900 | case AUDIO_FORMAT_PCM_16_BIT: |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1901 | return &AudioMixer::process__noResampleOneTrack< |
| Andy Hung | 116a498 | 2017-11-30 10:15:08 -0800 | [diff] [blame] | 1902 | MIXTYPE_MULTI_SAVEONLY, int16_t /*TO*/, float /*TI*/, TYPE_AUX>; |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1903 | default: |
| 1904 | LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat); |
| 1905 | break; |
| 1906 | } |
| 1907 | break; |
| 1908 | case AUDIO_FORMAT_PCM_16_BIT: |
| 1909 | switch (mixerOutFormat) { |
| 1910 | case AUDIO_FORMAT_PCM_FLOAT: |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1911 | return &AudioMixer::process__noResampleOneTrack< |
| Andy Hung | 116a498 | 2017-11-30 10:15:08 -0800 | [diff] [blame] | 1912 | MIXTYPE_MULTI_SAVEONLY, float /*TO*/, int16_t /*TI*/, TYPE_AUX>; |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1913 | case AUDIO_FORMAT_PCM_16_BIT: |
| Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 1914 | return &AudioMixer::process__noResampleOneTrack< |
| Andy Hung | 116a498 | 2017-11-30 10:15:08 -0800 | [diff] [blame] | 1915 | MIXTYPE_MULTI_SAVEONLY, int16_t /*TO*/, int16_t /*TI*/, TYPE_AUX>; |
| Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 1916 | default: |
| 1917 | LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat); |
| 1918 | break; |
| 1919 | } |
| 1920 | break; |
| 1921 | default: |
| 1922 | LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat); |
| 1923 | break; |
| 1924 | } |
| 1925 | return NULL; |
| 1926 | } |
| 1927 | |
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1928 | // ---------------------------------------------------------------------------- |
| Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 1929 | } // namespace android |