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