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