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