blob: 86711de6476951f7d8b60dfeaeea80720689aac2 [file] [log] [blame]
Glenn Kasten99e53b82012-01-19 08:59:58 -08001/*
Mathias Agopian65ab4712010-07-14 17:59:35 -07002**
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 Kasten7f5d3352013-02-15 23:55:04 +000019//#define LOG_NDEBUG 0
Mathias Agopian65ab4712010-07-14 17:59:35 -070020
21#include <stdint.h>
22#include <string.h>
23#include <stdlib.h>
Andy Hung5e58b0a2014-06-23 19:07:29 -070024#include <math.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070025#include <sys/types.h>
26
27#include <utils/Errors.h>
28#include <utils/Log.h>
29
Glenn Kastenf6b16782011-12-15 09:51:17 -080030#include <cutils/compiler.h>
Glenn Kasten5798d4e2012-03-08 12:18:35 -080031#include <utils/Debug.h>
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -070032
33#include <system/audio.h>
34
Glenn Kasten3b21c502011-12-15 09:52:39 -080035#include <audio_utils/primitives.h>
Andy Hungef7c7fb2014-05-12 16:51:41 -070036#include <audio_utils/format.h>
Andy Hung068561c2017-01-03 17:09:32 -080037#include <media/AudioMixer.h>
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -070038
Andy Hung296b7412014-06-17 15:25:47 -070039#include "AudioMixerOps.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070040
Andy Hunge93b6b72014-07-17 21:30:53 -070041// The FCC_2 macro refers to the Fixed Channel Count of 2 for the legacy integer mixer.
Andy Hung296b7412014-06-17 15:25:47 -070042#ifndef FCC_2
43#define FCC_2 2
44#endif
45
Andy Hunge93b6b72014-07-17 21:30:53 -070046// Look for MONO_HACK for any Mono hack involving legacy mono channel to
47// stereo channel conversion.
48
Andy Hung296b7412014-06-17 15:25:47 -070049/* 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 Hunga08810b2014-07-16 21:53:43 -070061#ifndef ARRAY_SIZE
62#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
63#endif
64
Andy Hung5b8fde72014-09-02 21:14:34 -070065// 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 Hung116a4982017-11-30 10:15:08 -080067static constexpr bool kUseNewMixer = true;
Andy Hung296b7412014-06-17 15:25:47 -070068
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 Hung116a4982017-11-30 10:15:08 -080072static constexpr bool kUseFloat = true;
73
74#ifdef FLOAT_AUX
75using TYPE_AUX = float;
76static_assert(kUseNewMixer && kUseFloat,
77 "kUseNewMixer and kUseFloat must be true for FLOAT_AUX option");
78#else
79using TYPE_AUX = int32_t; // q4.27
80#endif
Andy Hung296b7412014-06-17 15:25:47 -070081
Andy Hung1b2fdcb2014-07-16 17:44:34 -070082// Set to default copy buffer size in frames for input processing.
83static const size_t kCopyBufferFrameCount = 256;
84
Mathias Agopian65ab4712010-07-14 17:59:35 -070085namespace android {
Mathias Agopian65ab4712010-07-14 17:59:35 -070086
87// ----------------------------------------------------------------------------
Andy Hung1b2fdcb2014-07-16 17:44:34 -070088
Andy Hung7f475492014-08-25 16:36:37 -070089static 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 Hung1bc088a2018-02-09 15:57:31 -080093status_t AudioMixer::create(
94 int name, audio_channel_mask_t channelMask, audio_format_t format, int sessionId)
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -080095{
Andy Hung1bc088a2018-02-09 15:57:31 -080096 LOG_ALWAYS_FATAL_IF(exists(name), "name %d already exists", name);
Andy Hung8ed196a2018-01-05 13:21:11 -080097
Andy Hung1bc088a2018-02-09 15:57:31 -080098 if (!isValidChannelMask(channelMask)) {
99 ALOGE("%s invalid channelMask: %#x", __func__, channelMask);
100 return BAD_VALUE;
Andy Hung8ed196a2018-01-05 13:21:11 -0800101 }
Andy Hung1bc088a2018-02-09 15:57:31 -0800102 if (!isValidFormat(format)) {
103 ALOGE("%s invalid format: %#x", __func__, format);
104 return BAD_VALUE;
105 }
Andy Hung8ed196a2018-01-05 13:21:11 -0800106
107 auto t = std::make_shared<Track>();
Andy Hung8ed196a2018-01-05 13:21:11 -0800108 {
109 // TODO: move initialization to the Track constructor.
Glenn Kastendeeb1282012-03-25 11:59:31 -0700110 // assume default parameters for the track, except where noted below
Glenn Kastendeeb1282012-03-25 11:59:31 -0700111 t->needs = 0;
Andy Hung5e58b0a2014-06-23 19:07:29 -0700112
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 Hung97ae8242014-05-30 10:35:47 -0700116 t->volume[0] = UNITY_GAIN_INT;
117 t->volume[1] = UNITY_GAIN_INT;
Andy Hung5e58b0a2014-06-23 19:07:29 -0700118 t->prevVolume[0] = UNITY_GAIN_INT << 16;
119 t->prevVolume[1] = UNITY_GAIN_INT << 16;
Glenn Kastendeeb1282012-03-25 11:59:31 -0700120 t->volumeInc[0] = 0;
121 t->volumeInc[1] = 0;
122 t->auxLevel = 0;
123 t->auxInc = 0;
Andy Hung5e58b0a2014-06-23 19:07:29 -0700124 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 Kastendeeb1282012-03-25 11:59:31 -0700137 // no initialization needed
Glenn Kastendeeb1282012-03-25 11:59:31 -0700138 // t->frameCount
jiabin245cdd92018-12-07 17:55:15 -0800139 t->mHapticChannelMask = channelMask & AUDIO_CHANNEL_HAPTIC_ALL;
140 t->mHapticChannelCount = audio_channel_count_from_out_mask(t->mHapticChannelMask);
141 channelMask &= ~AUDIO_CHANNEL_HAPTIC_ALL;
Andy Hung68112fc2014-05-14 14:13:23 -0700142 t->channelCount = audio_channel_count_from_out_mask(channelMask);
Glenn Kastendeeb1282012-03-25 11:59:31 -0700143 t->enabled = false;
Andy Hunge93b6b72014-07-17 21:30:53 -0700144 ALOGV_IF(audio_channel_mask_get_bits(channelMask) != AUDIO_CHANNEL_OUT_STEREO,
Andy Hungef7c7fb2014-05-12 16:51:41 -0700145 "Non-stereo channel mask: %d\n", channelMask);
Andy Hung68112fc2014-05-14 14:13:23 -0700146 t->channelMask = channelMask;
Jean-Michel Trivid06e1322012-09-12 15:47:07 -0700147 t->sessionId = sessionId;
Glenn Kastendeeb1282012-03-25 11:59:31 -0700148 // setBufferProvider(name, AudioBufferProvider *) is required before enable(name)
149 t->bufferProvider = NULL;
150 t->buffer.raw = NULL;
151 // no initialization needed
152 // t->buffer.frameCount
153 t->hook = NULL;
Andy Hung8ed196a2018-01-05 13:21:11 -0800154 t->mIn = NULL;
Glenn Kastendeeb1282012-03-25 11:59:31 -0700155 t->sampleRate = mSampleRate;
156 // setParameter(name, TRACK, MAIN_BUFFER, mixBuffer) is required before enable(name)
157 t->mainBuffer = NULL;
158 t->auxBuffer = NULL;
Andy Hungef7c7fb2014-05-12 16:51:41 -0700159 t->mInputBufferProvider = NULL;
Andy Hung78820702014-02-28 16:23:02 -0800160 t->mMixerFormat = AUDIO_FORMAT_PCM_16_BIT;
Andy Hunge8a1ced2014-05-09 15:02:21 -0700161 t->mFormat = format;
Andy Hung7f475492014-08-25 16:36:37 -0700162 t->mMixerInFormat = selectMixerInFormat(format);
163 t->mDownmixRequiresFormat = AUDIO_FORMAT_INVALID; // no format required
Andy Hunge93b6b72014-07-17 21:30:53 -0700164 t->mMixerChannelMask = audio_channel_mask_from_representation_and_bits(
165 AUDIO_CHANNEL_REPRESENTATION_POSITION, AUDIO_CHANNEL_OUT_STEREO);
166 t->mMixerChannelCount = audio_channel_count_from_out_mask(t->mMixerChannelMask);
Ricardo Garcia5a8a95d2015-04-18 14:47:04 -0700167 t->mPlaybackRate = AUDIO_PLAYBACK_RATE_DEFAULT;
jiabindce8f8c2018-12-10 17:49:31 -0800168 // haptic
jiabin245cdd92018-12-07 17:55:15 -0800169 t->mHapticPlaybackEnabled = false;
170 t->mMixerHapticChannelMask = AUDIO_CHANNEL_NONE;
171 t->mMixerHapticChannelCount = 0;
172 t->mAdjustInChannelCount = t->channelCount + t->mHapticChannelCount;
173 t->mAdjustOutChannelCount = t->channelCount + t->mMixerHapticChannelCount;
174 t->mAdjustNonDestructiveInChannelCount = t->mAdjustOutChannelCount;
175 t->mAdjustNonDestructiveOutChannelCount = t->channelCount;
jiabindce8f8c2018-12-10 17:49:31 -0800176 t->mKeepContractedChannels = false;
Andy Hung296b7412014-06-17 15:25:47 -0700177 // Check the downmixing (or upmixing) requirements.
Andy Hung0f451e92014-08-04 21:28:47 -0700178 status_t status = t->prepareForDownmix();
Andy Hung68112fc2014-05-14 14:13:23 -0700179 if (status != OK) {
180 ALOGE("AudioMixer::getTrackName invalid channelMask (%#x)", channelMask);
Andy Hung1bc088a2018-02-09 15:57:31 -0800181 return BAD_VALUE;
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700182 }
Andy Hung7f475492014-08-25 16:36:37 -0700183 // prepareForDownmix() may change mDownmixRequiresFormat
Andy Hung296b7412014-06-17 15:25:47 -0700184 ALOGVV("mMixerFormat:%#x mMixerInFormat:%#x\n", t->mMixerFormat, t->mMixerInFormat);
Andy Hung0f451e92014-08-04 21:28:47 -0700185 t->prepareForReformat();
jiabindce8f8c2018-12-10 17:49:31 -0800186 t->prepareForAdjustChannelsNonDestructive(mFrameCount);
187 t->prepareForAdjustChannels();
Andy Hung1bc088a2018-02-09 15:57:31 -0800188
189 mTracks[name] = t;
190 return OK;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700191 }
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800192}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700193
Andy Hunge93b6b72014-07-17 21:30:53 -0700194// Called when channel masks have changed for a track name
Andy Hung7f475492014-08-25 16:36:37 -0700195// TODO: Fix DownmixerBufferProvider not to (possibly) change mixer input format,
Andy Hunge93b6b72014-07-17 21:30:53 -0700196// which will simplify this logic.
197bool AudioMixer::setChannelMasks(int name,
198 audio_channel_mask_t trackChannelMask, audio_channel_mask_t mixerChannelMask) {
Andy Hung1bc088a2018-02-09 15:57:31 -0800199 LOG_ALWAYS_FATAL_IF(!exists(name), "invalid name: %d", name);
Andy Hung8ed196a2018-01-05 13:21:11 -0800200 const std::shared_ptr<Track> &track = mTracks[name];
Andy Hunge93b6b72014-07-17 21:30:53 -0700201
jiabin245cdd92018-12-07 17:55:15 -0800202 if (trackChannelMask == (track->channelMask | track->mHapticChannelMask)
203 && mixerChannelMask == (track->mMixerChannelMask | track->mMixerHapticChannelMask)) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700204 return false; // no need to change
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700205 }
jiabin245cdd92018-12-07 17:55:15 -0800206 const audio_channel_mask_t hapticChannelMask = trackChannelMask & AUDIO_CHANNEL_HAPTIC_ALL;
207 trackChannelMask &= ~AUDIO_CHANNEL_HAPTIC_ALL;
208 const audio_channel_mask_t mixerHapticChannelMask = mixerChannelMask & AUDIO_CHANNEL_HAPTIC_ALL;
209 mixerChannelMask &= ~AUDIO_CHANNEL_HAPTIC_ALL;
Andy Hunge93b6b72014-07-17 21:30:53 -0700210 // always recompute for both channel masks even if only one has changed.
211 const uint32_t trackChannelCount = audio_channel_count_from_out_mask(trackChannelMask);
212 const uint32_t mixerChannelCount = audio_channel_count_from_out_mask(mixerChannelMask);
jiabin245cdd92018-12-07 17:55:15 -0800213 const uint32_t hapticChannelCount = audio_channel_count_from_out_mask(hapticChannelMask);
214 const uint32_t mixerHapticChannelCount =
215 audio_channel_count_from_out_mask(mixerHapticChannelMask);
Andy Hunge93b6b72014-07-17 21:30:53 -0700216
217 ALOG_ASSERT((trackChannelCount <= MAX_NUM_CHANNELS_TO_DOWNMIX)
218 && trackChannelCount
219 && mixerChannelCount);
Andy Hung8ed196a2018-01-05 13:21:11 -0800220 track->channelMask = trackChannelMask;
221 track->channelCount = trackChannelCount;
222 track->mMixerChannelMask = mixerChannelMask;
223 track->mMixerChannelCount = mixerChannelCount;
jiabin245cdd92018-12-07 17:55:15 -0800224 track->mHapticChannelMask = hapticChannelMask;
225 track->mHapticChannelCount = hapticChannelCount;
226 track->mMixerHapticChannelMask = mixerHapticChannelMask;
227 track->mMixerHapticChannelCount = mixerHapticChannelCount;
228
229 if (track->mHapticChannelCount > 0) {
230 track->mAdjustInChannelCount = track->channelCount + track->mHapticChannelCount;
231 track->mAdjustOutChannelCount = track->channelCount + track->mMixerHapticChannelCount;
232 track->mAdjustNonDestructiveInChannelCount = track->mAdjustOutChannelCount;
233 track->mAdjustNonDestructiveOutChannelCount = track->channelCount;
234 track->mKeepContractedChannels = track->mHapticPlaybackEnabled;
235 } else {
236 track->mAdjustInChannelCount = 0;
237 track->mAdjustOutChannelCount = 0;
238 track->mAdjustNonDestructiveInChannelCount = 0;
239 track->mAdjustNonDestructiveOutChannelCount = 0;
240 track->mKeepContractedChannels = false;
241 }
Andy Hunge93b6b72014-07-17 21:30:53 -0700242
243 // channel masks have changed, does this track need a downmixer?
244 // update to try using our desired format (if we aren't already using it)
Andy Hung8ed196a2018-01-05 13:21:11 -0800245 const status_t status = track->prepareForDownmix();
Andy Hunge93b6b72014-07-17 21:30:53 -0700246 ALOGE_IF(status != OK,
Andy Hung0f451e92014-08-04 21:28:47 -0700247 "prepareForDownmix error %d, track channel mask %#x, mixer channel mask %#x",
Andy Hung8ed196a2018-01-05 13:21:11 -0800248 status, track->channelMask, track->mMixerChannelMask);
Andy Hunge93b6b72014-07-17 21:30:53 -0700249
Yung Ti Su1a0ecc32018-05-07 11:09:15 +0800250 // always do reformat since channel mask changed,
251 // do it after downmix since track format may change!
252 track->prepareForReformat();
Andy Hunge93b6b72014-07-17 21:30:53 -0700253
jiabindce8f8c2018-12-10 17:49:31 -0800254 track->prepareForAdjustChannelsNonDestructive(mFrameCount);
255 track->prepareForAdjustChannels();
256
Yung Ti Sub5d11952018-05-22 22:31:14 +0800257 if (track->mResampler.get() != nullptr) {
Andy Hung7f475492014-08-25 16:36:37 -0700258 // resampler channels may have changed.
Andy Hung8ed196a2018-01-05 13:21:11 -0800259 const uint32_t resetToSampleRate = track->sampleRate;
260 track->mResampler.reset(nullptr);
261 track->sampleRate = mSampleRate; // without resampler, track rate is device sample rate.
Andy Hunge93b6b72014-07-17 21:30:53 -0700262 // recreate the resampler with updated format, channels, saved sampleRate.
Andy Hung8ed196a2018-01-05 13:21:11 -0800263 track->setResampler(resetToSampleRate /*trackSampleRate*/, mSampleRate /*devSampleRate*/);
Andy Hunge93b6b72014-07-17 21:30:53 -0700264 }
265 return true;
266}
267
Andy Hung8ed196a2018-01-05 13:21:11 -0800268void AudioMixer::Track::unprepareForDownmix() {
Andy Hung0f451e92014-08-04 21:28:47 -0700269 ALOGV("AudioMixer::unprepareForDownmix(%p)", this);
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700270
Andy Hung8ed196a2018-01-05 13:21:11 -0800271 if (mPostDownmixReformatBufferProvider.get() != nullptr) {
Andy Hung85395892017-04-25 16:47:52 -0700272 // release any buffers held by the mPostDownmixReformatBufferProvider
Andy Hung8ed196a2018-01-05 13:21:11 -0800273 // before deallocating the mDownmixerBufferProvider.
Andy Hung85395892017-04-25 16:47:52 -0700274 mPostDownmixReformatBufferProvider->reset();
275 }
276
Andy Hung7f475492014-08-25 16:36:37 -0700277 mDownmixRequiresFormat = AUDIO_FORMAT_INVALID;
Andy Hung8ed196a2018-01-05 13:21:11 -0800278 if (mDownmixerBufferProvider.get() != nullptr) {
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700279 // this track had previously been configured with a downmixer, delete it
Andy Hung8ed196a2018-01-05 13:21:11 -0800280 mDownmixerBufferProvider.reset(nullptr);
Andy Hung0f451e92014-08-04 21:28:47 -0700281 reconfigureBufferProviders();
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700282 } else {
283 ALOGV(" nothing to do, no downmixer to delete");
284 }
285}
286
Andy Hung8ed196a2018-01-05 13:21:11 -0800287status_t AudioMixer::Track::prepareForDownmix()
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700288{
Andy Hung0f451e92014-08-04 21:28:47 -0700289 ALOGV("AudioMixer::prepareForDownmix(%p) with mask 0x%x",
290 this, channelMask);
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700291
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700292 // discard the previous downmixer if there was one
Andy Hung0f451e92014-08-04 21:28:47 -0700293 unprepareForDownmix();
Andy Hung73e62e22015-04-20 12:06:38 -0700294 // MONO_HACK Only remix (upmix or downmix) if the track and mixer/device channel masks
Andy Hung0f451e92014-08-04 21:28:47 -0700295 // are not the same and not handled internally, as mono -> stereo currently is.
296 if (channelMask == mMixerChannelMask
297 || (channelMask == AUDIO_CHANNEL_OUT_MONO
298 && mMixerChannelMask == AUDIO_CHANNEL_OUT_STEREO)) {
299 return NO_ERROR;
300 }
Andy Hung650ceb92015-01-29 13:31:12 -0800301 // DownmixerBufferProvider is only used for position masks.
302 if (audio_channel_mask_get_representation(channelMask)
303 == AUDIO_CHANNEL_REPRESENTATION_POSITION
304 && DownmixerBufferProvider::isMultichannelCapable()) {
Andy Hung66942552018-12-21 16:07:12 -0800305
306 // Check if we have a float or int16 downmixer, in that order.
307 for (const audio_format_t format : { AUDIO_FORMAT_PCM_FLOAT, AUDIO_FORMAT_PCM_16_BIT }) {
308 mDownmixerBufferProvider.reset(new DownmixerBufferProvider(
309 channelMask, mMixerChannelMask,
310 format,
311 sampleRate, sessionId, kCopyBufferFrameCount));
312 if (static_cast<DownmixerBufferProvider *>(mDownmixerBufferProvider.get())
313 ->isValid()) {
314 mDownmixRequiresFormat = format;
315 reconfigureBufferProviders();
316 return NO_ERROR;
317 }
Andy Hung34803d52014-07-16 21:41:35 -0700318 }
Andy Hung8ed196a2018-01-05 13:21:11 -0800319 // mDownmixerBufferProvider reset below.
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700320 }
Andy Hunge93b6b72014-07-17 21:30:53 -0700321
322 // Effect downmixer does not accept the channel conversion. Let's use our remixer.
Andy Hung8ed196a2018-01-05 13:21:11 -0800323 mDownmixerBufferProvider.reset(new RemixBufferProvider(channelMask,
324 mMixerChannelMask, mMixerInFormat, kCopyBufferFrameCount));
Andy Hunge93b6b72014-07-17 21:30:53 -0700325 // Remix always finds a conversion whereas Downmixer effect above may fail.
Andy Hung0f451e92014-08-04 21:28:47 -0700326 reconfigureBufferProviders();
Andy Hunge93b6b72014-07-17 21:30:53 -0700327 return NO_ERROR;
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700328}
329
Andy Hung8ed196a2018-01-05 13:21:11 -0800330void AudioMixer::Track::unprepareForReformat() {
Andy Hung0f451e92014-08-04 21:28:47 -0700331 ALOGV("AudioMixer::unprepareForReformat(%p)", this);
Andy Hung7f475492014-08-25 16:36:37 -0700332 bool requiresReconfigure = false;
Andy Hung8ed196a2018-01-05 13:21:11 -0800333 if (mReformatBufferProvider.get() != nullptr) {
334 mReformatBufferProvider.reset(nullptr);
Andy Hung7f475492014-08-25 16:36:37 -0700335 requiresReconfigure = true;
336 }
Andy Hung8ed196a2018-01-05 13:21:11 -0800337 if (mPostDownmixReformatBufferProvider.get() != nullptr) {
338 mPostDownmixReformatBufferProvider.reset(nullptr);
Andy Hung7f475492014-08-25 16:36:37 -0700339 requiresReconfigure = true;
340 }
341 if (requiresReconfigure) {
Andy Hung0f451e92014-08-04 21:28:47 -0700342 reconfigureBufferProviders();
Andy Hungef7c7fb2014-05-12 16:51:41 -0700343 }
344}
345
Andy Hung8ed196a2018-01-05 13:21:11 -0800346status_t AudioMixer::Track::prepareForReformat()
Andy Hungef7c7fb2014-05-12 16:51:41 -0700347{
Andy Hung0f451e92014-08-04 21:28:47 -0700348 ALOGV("AudioMixer::prepareForReformat(%p) with format %#x", this, mFormat);
Andy Hung7f475492014-08-25 16:36:37 -0700349 // discard previous reformatters
Andy Hung0f451e92014-08-04 21:28:47 -0700350 unprepareForReformat();
Andy Hung7f475492014-08-25 16:36:37 -0700351 // only configure reformatters as needed
352 const audio_format_t targetFormat = mDownmixRequiresFormat != AUDIO_FORMAT_INVALID
353 ? mDownmixRequiresFormat : mMixerInFormat;
354 bool requiresReconfigure = false;
355 if (mFormat != targetFormat) {
Andy Hung8ed196a2018-01-05 13:21:11 -0800356 mReformatBufferProvider.reset(new ReformatBufferProvider(
Andy Hung0f451e92014-08-04 21:28:47 -0700357 audio_channel_count_from_out_mask(channelMask),
Andy Hung7f475492014-08-25 16:36:37 -0700358 mFormat,
359 targetFormat,
Andy Hung8ed196a2018-01-05 13:21:11 -0800360 kCopyBufferFrameCount));
Andy Hung7f475492014-08-25 16:36:37 -0700361 requiresReconfigure = true;
Kevin Rocarde053bfa2017-11-09 22:07:34 -0800362 } else if (mFormat == AUDIO_FORMAT_PCM_FLOAT) {
363 // Input and output are floats, make sure application did not provide > 3db samples
364 // that would break volume application (b/68099072)
365 // TODO: add a trusted source flag to avoid the overhead
366 mReformatBufferProvider.reset(new ClampFloatBufferProvider(
367 audio_channel_count_from_out_mask(channelMask),
368 kCopyBufferFrameCount));
369 requiresReconfigure = true;
Andy Hung7f475492014-08-25 16:36:37 -0700370 }
371 if (targetFormat != mMixerInFormat) {
Andy Hung8ed196a2018-01-05 13:21:11 -0800372 mPostDownmixReformatBufferProvider.reset(new ReformatBufferProvider(
Andy Hung7f475492014-08-25 16:36:37 -0700373 audio_channel_count_from_out_mask(mMixerChannelMask),
374 targetFormat,
375 mMixerInFormat,
Andy Hung8ed196a2018-01-05 13:21:11 -0800376 kCopyBufferFrameCount));
Andy Hung7f475492014-08-25 16:36:37 -0700377 requiresReconfigure = true;
378 }
379 if (requiresReconfigure) {
Andy Hung0f451e92014-08-04 21:28:47 -0700380 reconfigureBufferProviders();
Andy Hung296b7412014-06-17 15:25:47 -0700381 }
382 return NO_ERROR;
Andy Hungef7c7fb2014-05-12 16:51:41 -0700383}
384
jiabindce8f8c2018-12-10 17:49:31 -0800385void AudioMixer::Track::unprepareForAdjustChannels()
386{
387 ALOGV("AUDIOMIXER::unprepareForAdjustChannels");
388 if (mAdjustChannelsBufferProvider.get() != nullptr) {
389 mAdjustChannelsBufferProvider.reset(nullptr);
390 reconfigureBufferProviders();
391 }
392}
393
394status_t AudioMixer::Track::prepareForAdjustChannels()
395{
396 ALOGV("AudioMixer::prepareForAdjustChannels(%p) with inChannelCount: %u, outChannelCount: %u",
397 this, mAdjustInChannelCount, mAdjustOutChannelCount);
398 unprepareForAdjustChannels();
399 if (mAdjustInChannelCount != mAdjustOutChannelCount) {
400 mAdjustChannelsBufferProvider.reset(new AdjustChannelsBufferProvider(
401 mFormat, mAdjustInChannelCount, mAdjustOutChannelCount, kCopyBufferFrameCount));
402 reconfigureBufferProviders();
403 }
404 return NO_ERROR;
405}
406
407void AudioMixer::Track::unprepareForAdjustChannelsNonDestructive()
408{
409 ALOGV("AUDIOMIXER::unprepareForAdjustChannelsNonDestructive");
410 if (mAdjustChannelsNonDestructiveBufferProvider.get() != nullptr) {
411 mAdjustChannelsNonDestructiveBufferProvider.reset(nullptr);
412 reconfigureBufferProviders();
413 }
414}
415
416status_t AudioMixer::Track::prepareForAdjustChannelsNonDestructive(size_t frames)
417{
418 ALOGV("AudioMixer::prepareForAdjustChannelsNonDestructive(%p) with inChannelCount: %u, "
419 "outChannelCount: %u, keepContractedChannels: %d",
420 this, mAdjustNonDestructiveInChannelCount, mAdjustNonDestructiveOutChannelCount,
421 mKeepContractedChannels);
422 unprepareForAdjustChannelsNonDestructive();
423 if (mAdjustNonDestructiveInChannelCount != mAdjustNonDestructiveOutChannelCount) {
424 uint8_t* buffer = mKeepContractedChannels
425 ? (uint8_t*)mainBuffer + frames * audio_bytes_per_frame(
426 mMixerChannelCount, mMixerFormat)
427 : NULL;
428 mAdjustChannelsNonDestructiveBufferProvider.reset(
429 new AdjustChannelsNonDestructiveBufferProvider(
430 mFormat,
431 mAdjustNonDestructiveInChannelCount,
432 mAdjustNonDestructiveOutChannelCount,
433 mKeepContractedChannels ? mMixerFormat : AUDIO_FORMAT_INVALID,
434 frames,
435 buffer));
436 reconfigureBufferProviders();
437 }
438 return NO_ERROR;
439}
440
441void AudioMixer::Track::clearContractedBuffer()
442{
443 if (mAdjustChannelsNonDestructiveBufferProvider.get() != nullptr) {
444 static_cast<AdjustChannelsNonDestructiveBufferProvider*>(
445 mAdjustChannelsNonDestructiveBufferProvider.get())->clearContractedFrames();
446 }
447}
448
Andy Hung8ed196a2018-01-05 13:21:11 -0800449void AudioMixer::Track::reconfigureBufferProviders()
Andy Hungef7c7fb2014-05-12 16:51:41 -0700450{
Andy Hung3a34df92018-08-21 12:32:30 -0700451 // configure from upstream to downstream buffer providers.
Andy Hung0f451e92014-08-04 21:28:47 -0700452 bufferProvider = mInputBufferProvider;
jiabindce8f8c2018-12-10 17:49:31 -0800453 if (mAdjustChannelsBufferProvider.get() != nullptr) {
454 mAdjustChannelsBufferProvider->setBufferProvider(bufferProvider);
455 bufferProvider = mAdjustChannelsBufferProvider.get();
456 }
457 if (mAdjustChannelsNonDestructiveBufferProvider.get() != nullptr) {
458 mAdjustChannelsNonDestructiveBufferProvider->setBufferProvider(bufferProvider);
459 bufferProvider = mAdjustChannelsNonDestructiveBufferProvider.get();
460 }
Andy Hung8ed196a2018-01-05 13:21:11 -0800461 if (mReformatBufferProvider.get() != nullptr) {
Andy Hung0f451e92014-08-04 21:28:47 -0700462 mReformatBufferProvider->setBufferProvider(bufferProvider);
Andy Hung8ed196a2018-01-05 13:21:11 -0800463 bufferProvider = mReformatBufferProvider.get();
Andy Hungef7c7fb2014-05-12 16:51:41 -0700464 }
Andy Hung8ed196a2018-01-05 13:21:11 -0800465 if (mDownmixerBufferProvider.get() != nullptr) {
466 mDownmixerBufferProvider->setBufferProvider(bufferProvider);
467 bufferProvider = mDownmixerBufferProvider.get();
Andy Hungef7c7fb2014-05-12 16:51:41 -0700468 }
Andy Hung8ed196a2018-01-05 13:21:11 -0800469 if (mPostDownmixReformatBufferProvider.get() != nullptr) {
Andy Hung7f475492014-08-25 16:36:37 -0700470 mPostDownmixReformatBufferProvider->setBufferProvider(bufferProvider);
Andy Hung8ed196a2018-01-05 13:21:11 -0800471 bufferProvider = mPostDownmixReformatBufferProvider.get();
Andy Hung7f475492014-08-25 16:36:37 -0700472 }
Andy Hung8ed196a2018-01-05 13:21:11 -0800473 if (mTimestretchBufferProvider.get() != nullptr) {
Andy Hungc5656cc2015-03-26 19:04:33 -0700474 mTimestretchBufferProvider->setBufferProvider(bufferProvider);
Andy Hung8ed196a2018-01-05 13:21:11 -0800475 bufferProvider = mTimestretchBufferProvider.get();
Andy Hungc5656cc2015-03-26 19:04:33 -0700476 }
Andy Hungef7c7fb2014-05-12 16:51:41 -0700477}
478
Andy Hung1bc088a2018-02-09 15:57:31 -0800479void AudioMixer::destroy(int name)
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800480{
Andy Hung1bc088a2018-02-09 15:57:31 -0800481 LOG_ALWAYS_FATAL_IF(!exists(name), "invalid name: %d", name);
Glenn Kasten237a6242011-12-15 15:32:27 -0800482 ALOGV("deleteTrackName(%d)", name);
Andy Hung8ed196a2018-01-05 13:21:11 -0800483
484 if (mTracks[name]->enabled) {
485 invalidate();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700486 }
Andy Hung8ed196a2018-01-05 13:21:11 -0800487 mTracks.erase(name); // deallocate track
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800488}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700489
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800490void AudioMixer::enable(int name)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700491{
Andy Hung1bc088a2018-02-09 15:57:31 -0800492 LOG_ALWAYS_FATAL_IF(!exists(name), "invalid name: %d", name);
Andy Hung8ed196a2018-01-05 13:21:11 -0800493 const std::shared_ptr<Track> &track = mTracks[name];
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800494
Andy Hung8ed196a2018-01-05 13:21:11 -0800495 if (!track->enabled) {
496 track->enabled = true;
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800497 ALOGV("enable(%d)", name);
Andy Hung8ed196a2018-01-05 13:21:11 -0800498 invalidate();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700499 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700500}
501
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800502void AudioMixer::disable(int name)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700503{
Andy Hung1bc088a2018-02-09 15:57:31 -0800504 LOG_ALWAYS_FATAL_IF(!exists(name), "invalid name: %d", name);
Andy Hung8ed196a2018-01-05 13:21:11 -0800505 const std::shared_ptr<Track> &track = mTracks[name];
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800506
Andy Hung8ed196a2018-01-05 13:21:11 -0800507 if (track->enabled) {
508 track->enabled = false;
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800509 ALOGV("disable(%d)", name);
Andy Hung8ed196a2018-01-05 13:21:11 -0800510 invalidate();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700511 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700512}
513
Andy Hung5866a3b2014-05-29 21:33:13 -0700514/* Sets the volume ramp variables for the AudioMixer.
515 *
Andy Hung5e58b0a2014-06-23 19:07:29 -0700516 * The volume ramp variables are used to transition from the previous
517 * volume to the set volume. ramp controls the duration of the transition.
518 * Its value is typically one state framecount period, but may also be 0,
519 * meaning "immediate."
Andy Hung5866a3b2014-05-29 21:33:13 -0700520 *
Andy Hung5e58b0a2014-06-23 19:07:29 -0700521 * FIXME: 1) Volume ramp is enabled only if there is a nonzero integer increment
522 * even if there is a nonzero floating point increment (in that case, the volume
523 * change is immediate). This restriction should be changed when the legacy mixer
524 * is removed (see #2).
525 * FIXME: 2) Integer volume variables are used for Legacy mixing and should be removed
526 * when no longer needed.
527 *
528 * @param newVolume set volume target in floating point [0.0, 1.0].
529 * @param ramp number of frames to increment over. if ramp is 0, the volume
530 * should be set immediately. Currently ramp should not exceed 65535 (frames).
531 * @param pIntSetVolume pointer to the U4.12 integer target volume, set on return.
532 * @param pIntPrevVolume pointer to the U4.28 integer previous volume, set on return.
533 * @param pIntVolumeInc pointer to the U4.28 increment per output audio frame, set on return.
534 * @param pSetVolume pointer to the float target volume, set on return.
535 * @param pPrevVolume pointer to the float previous volume, set on return.
536 * @param pVolumeInc pointer to the float increment per output audio frame, set on return.
Andy Hung5866a3b2014-05-29 21:33:13 -0700537 * @return true if the volume has changed, false if volume is same.
538 */
Andy Hung5e58b0a2014-06-23 19:07:29 -0700539static inline bool setVolumeRampVariables(float newVolume, int32_t ramp,
540 int16_t *pIntSetVolume, int32_t *pIntPrevVolume, int32_t *pIntVolumeInc,
541 float *pSetVolume, float *pPrevVolume, float *pVolumeInc) {
Andy Hunge09c9942015-05-08 16:58:13 -0700542 // check floating point volume to see if it is identical to the previously
543 // set volume.
544 // We do not use a tolerance here (and reject changes too small)
545 // as it may be confusing to use a different value than the one set.
546 // If the resulting volume is too small to ramp, it is a direct set of the volume.
Andy Hung5e58b0a2014-06-23 19:07:29 -0700547 if (newVolume == *pSetVolume) {
Andy Hung5866a3b2014-05-29 21:33:13 -0700548 return false;
549 }
Andy Hunge09c9942015-05-08 16:58:13 -0700550 if (newVolume < 0) {
551 newVolume = 0; // should not have negative volumes
Andy Hung5866a3b2014-05-29 21:33:13 -0700552 } else {
Andy Hunge09c9942015-05-08 16:58:13 -0700553 switch (fpclassify(newVolume)) {
554 case FP_SUBNORMAL:
555 case FP_NAN:
556 newVolume = 0;
557 break;
558 case FP_ZERO:
559 break; // zero volume is fine
560 case FP_INFINITE:
561 // Infinite volume could be handled consistently since
562 // floating point math saturates at infinities,
563 // but we limit volume to unity gain float.
564 // ramp = 0; break;
565 //
566 newVolume = AudioMixer::UNITY_GAIN_FLOAT;
567 break;
568 case FP_NORMAL:
569 default:
570 // Floating point does not have problems with overflow wrap
571 // that integer has. However, we limit the volume to
572 // unity gain here.
573 // TODO: Revisit the volume limitation and perhaps parameterize.
574 if (newVolume > AudioMixer::UNITY_GAIN_FLOAT) {
575 newVolume = AudioMixer::UNITY_GAIN_FLOAT;
576 }
577 break;
578 }
Andy Hung5866a3b2014-05-29 21:33:13 -0700579 }
Andy Hung5e58b0a2014-06-23 19:07:29 -0700580
Andy Hunge09c9942015-05-08 16:58:13 -0700581 // set floating point volume ramp
582 if (ramp != 0) {
583 // when the ramp completes, *pPrevVolume is set to *pSetVolume, so there
584 // is no computational mismatch; hence equality is checked here.
585 ALOGD_IF(*pPrevVolume != *pSetVolume, "previous float ramp hasn't finished,"
586 " prev:%f set_to:%f", *pPrevVolume, *pSetVolume);
587 const float inc = (newVolume - *pPrevVolume) / ramp; // could be inf, nan, subnormal
Andy Hung8ed196a2018-01-05 13:21:11 -0800588 // could be inf, cannot be nan, subnormal
589 const float maxv = std::max(newVolume, *pPrevVolume);
Andy Hunge09c9942015-05-08 16:58:13 -0700590
591 if (isnormal(inc) // inc must be a normal number (no subnormals, infinite, nan)
592 && maxv + inc != maxv) { // inc must make forward progress
593 *pVolumeInc = inc;
594 // ramp is set now.
595 // Note: if newVolume is 0, then near the end of the ramp,
596 // it may be possible that the ramped volume may be subnormal or
597 // temporarily negative by a small amount or subnormal due to floating
598 // point inaccuracies.
599 } else {
600 ramp = 0; // ramp not allowed
601 }
Andy Hung5e58b0a2014-06-23 19:07:29 -0700602 }
Andy Hunge09c9942015-05-08 16:58:13 -0700603
604 // compute and check integer volume, no need to check negative values
605 // The integer volume is limited to "unity_gain" to avoid wrapping and other
606 // audio artifacts, so it never reaches the range limit of U4.28.
607 // We safely use signed 16 and 32 bit integers here.
608 const float scaledVolume = newVolume * AudioMixer::UNITY_GAIN_INT; // not neg, subnormal, nan
609 const int32_t intVolume = (scaledVolume >= (float)AudioMixer::UNITY_GAIN_INT) ?
610 AudioMixer::UNITY_GAIN_INT : (int32_t)scaledVolume;
611
612 // set integer volume ramp
613 if (ramp != 0) {
614 // integer volume is U4.12 (to use 16 bit multiplies), but ramping uses U4.28.
615 // when the ramp completes, *pIntPrevVolume is set to *pIntSetVolume << 16, so there
616 // is no computational mismatch; hence equality is checked here.
617 ALOGD_IF(*pIntPrevVolume != *pIntSetVolume << 16, "previous int ramp hasn't finished,"
618 " prev:%d set_to:%d", *pIntPrevVolume, *pIntSetVolume << 16);
619 const int32_t inc = ((intVolume << 16) - *pIntPrevVolume) / ramp;
620
621 if (inc != 0) { // inc must make forward progress
622 *pIntVolumeInc = inc;
623 } else {
624 ramp = 0; // ramp not allowed
625 }
626 }
627
628 // if no ramp, or ramp not allowed, then clear float and integer increments
629 if (ramp == 0) {
Andy Hung5e58b0a2014-06-23 19:07:29 -0700630 *pVolumeInc = 0;
631 *pPrevVolume = newVolume;
Andy Hung5e58b0a2014-06-23 19:07:29 -0700632 *pIntVolumeInc = 0;
633 *pIntPrevVolume = intVolume << 16;
634 }
Andy Hunge09c9942015-05-08 16:58:13 -0700635 *pSetVolume = newVolume;
Andy Hung5e58b0a2014-06-23 19:07:29 -0700636 *pIntSetVolume = intVolume;
Andy Hung5866a3b2014-05-29 21:33:13 -0700637 return true;
638}
639
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800640void AudioMixer::setParameter(int name, int target, int param, void *value)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700641{
Andy Hung1bc088a2018-02-09 15:57:31 -0800642 LOG_ALWAYS_FATAL_IF(!exists(name), "invalid name: %d", name);
Andy Hung8ed196a2018-01-05 13:21:11 -0800643 const std::shared_ptr<Track> &track = mTracks[name];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700644
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000645 int valueInt = static_cast<int>(reinterpret_cast<uintptr_t>(value));
646 int32_t *valueBuf = reinterpret_cast<int32_t*>(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700647
648 switch (target) {
Glenn Kasten788040c2011-05-05 08:19:00 -0700649
Mathias Agopian65ab4712010-07-14 17:59:35 -0700650 case TRACK:
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800651 switch (param) {
Glenn Kasten788040c2011-05-05 08:19:00 -0700652 case CHANNEL_MASK: {
Andy Hunge93b6b72014-07-17 21:30:53 -0700653 const audio_channel_mask_t trackChannelMask =
654 static_cast<audio_channel_mask_t>(valueInt);
jiabin245cdd92018-12-07 17:55:15 -0800655 if (setChannelMasks(name, trackChannelMask,
656 (track->mMixerChannelMask | track->mMixerHapticChannelMask))) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700657 ALOGV("setParameter(TRACK, CHANNEL_MASK, %x)", trackChannelMask);
Andy Hung8ed196a2018-01-05 13:21:11 -0800658 invalidate();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700659 }
Glenn Kasten788040c2011-05-05 08:19:00 -0700660 } break;
661 case MAIN_BUFFER:
Andy Hung8ed196a2018-01-05 13:21:11 -0800662 if (track->mainBuffer != valueBuf) {
663 track->mainBuffer = valueBuf;
Steve Block3856b092011-10-20 11:56:00 +0100664 ALOGV("setParameter(TRACK, MAIN_BUFFER, %p)", valueBuf);
jiabindce8f8c2018-12-10 17:49:31 -0800665 if (track->mKeepContractedChannels) {
666 track->prepareForAdjustChannelsNonDestructive(mFrameCount);
667 }
Andy Hung8ed196a2018-01-05 13:21:11 -0800668 invalidate();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700669 }
Glenn Kasten788040c2011-05-05 08:19:00 -0700670 break;
671 case AUX_BUFFER:
Andy Hung8ed196a2018-01-05 13:21:11 -0800672 if (track->auxBuffer != valueBuf) {
673 track->auxBuffer = valueBuf;
Steve Block3856b092011-10-20 11:56:00 +0100674 ALOGV("setParameter(TRACK, AUX_BUFFER, %p)", valueBuf);
Andy Hung8ed196a2018-01-05 13:21:11 -0800675 invalidate();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700676 }
Glenn Kasten788040c2011-05-05 08:19:00 -0700677 break;
Andy Hungef7c7fb2014-05-12 16:51:41 -0700678 case FORMAT: {
679 audio_format_t format = static_cast<audio_format_t>(valueInt);
Andy Hung8ed196a2018-01-05 13:21:11 -0800680 if (track->mFormat != format) {
Andy Hungef7c7fb2014-05-12 16:51:41 -0700681 ALOG_ASSERT(audio_is_linear_pcm(format), "Invalid format %#x", format);
Andy Hung8ed196a2018-01-05 13:21:11 -0800682 track->mFormat = format;
Andy Hungef7c7fb2014-05-12 16:51:41 -0700683 ALOGV("setParameter(TRACK, FORMAT, %#x)", format);
Andy Hung8ed196a2018-01-05 13:21:11 -0800684 track->prepareForReformat();
685 invalidate();
Andy Hungef7c7fb2014-05-12 16:51:41 -0700686 }
687 } break;
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700688 // FIXME do we want to support setting the downmix type from AudioFlinger?
689 // for a specific track? or per mixer?
690 /* case DOWNMIX_TYPE:
691 break */
Andy Hung78820702014-02-28 16:23:02 -0800692 case MIXER_FORMAT: {
Andy Hunga1ab7cc2014-02-24 19:26:52 -0800693 audio_format_t format = static_cast<audio_format_t>(valueInt);
Andy Hung8ed196a2018-01-05 13:21:11 -0800694 if (track->mMixerFormat != format) {
695 track->mMixerFormat = format;
Andy Hung78820702014-02-28 16:23:02 -0800696 ALOGV("setParameter(TRACK, MIXER_FORMAT, %#x)", format);
jiabindce8f8c2018-12-10 17:49:31 -0800697 if (track->mKeepContractedChannels) {
698 track->prepareForAdjustChannelsNonDestructive(mFrameCount);
699 }
Andy Hunga1ab7cc2014-02-24 19:26:52 -0800700 }
701 } break;
Andy Hunge93b6b72014-07-17 21:30:53 -0700702 case MIXER_CHANNEL_MASK: {
703 const audio_channel_mask_t mixerChannelMask =
704 static_cast<audio_channel_mask_t>(valueInt);
jiabin245cdd92018-12-07 17:55:15 -0800705 if (setChannelMasks(name, track->channelMask | track->mHapticChannelMask,
706 mixerChannelMask)) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700707 ALOGV("setParameter(TRACK, MIXER_CHANNEL_MASK, %#x)", mixerChannelMask);
Andy Hung8ed196a2018-01-05 13:21:11 -0800708 invalidate();
Andy Hunge93b6b72014-07-17 21:30:53 -0700709 }
710 } break;
jiabin245cdd92018-12-07 17:55:15 -0800711 case HAPTIC_ENABLED: {
712 const bool hapticPlaybackEnabled = static_cast<bool>(valueInt);
713 if (track->mHapticPlaybackEnabled != hapticPlaybackEnabled) {
714 track->mHapticPlaybackEnabled = hapticPlaybackEnabled;
715 track->mKeepContractedChannels = hapticPlaybackEnabled;
716 track->prepareForAdjustChannelsNonDestructive(mFrameCount);
717 track->prepareForAdjustChannels();
718 }
719 } break;
Glenn Kasten788040c2011-05-05 08:19:00 -0700720 default:
Glenn Kastenadad3d72014-02-21 14:51:43 -0800721 LOG_ALWAYS_FATAL("setParameter track: bad param %d", param);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700722 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700723 break;
Glenn Kasten788040c2011-05-05 08:19:00 -0700724
Mathias Agopian65ab4712010-07-14 17:59:35 -0700725 case RESAMPLE:
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800726 switch (param) {
727 case SAMPLE_RATE:
Glenn Kasten5798d4e2012-03-08 12:18:35 -0800728 ALOG_ASSERT(valueInt > 0, "bad sample rate %d", valueInt);
Andy Hung8ed196a2018-01-05 13:21:11 -0800729 if (track->setResampler(uint32_t(valueInt), mSampleRate)) {
Glenn Kasten788040c2011-05-05 08:19:00 -0700730 ALOGV("setParameter(RESAMPLE, SAMPLE_RATE, %u)",
731 uint32_t(valueInt));
Andy Hung8ed196a2018-01-05 13:21:11 -0800732 invalidate();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700733 }
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800734 break;
735 case RESET:
Andy Hung8ed196a2018-01-05 13:21:11 -0800736 track->resetResampler();
737 invalidate();
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800738 break;
Glenn Kasten4e2293f2012-04-12 09:39:07 -0700739 case REMOVE:
Andy Hung8ed196a2018-01-05 13:21:11 -0800740 track->mResampler.reset(nullptr);
741 track->sampleRate = mSampleRate;
742 invalidate();
Glenn Kasten4e2293f2012-04-12 09:39:07 -0700743 break;
Glenn Kasten788040c2011-05-05 08:19:00 -0700744 default:
Glenn Kastenadad3d72014-02-21 14:51:43 -0800745 LOG_ALWAYS_FATAL("setParameter resample: bad param %d", param);
Eric Laurent243f5f92011-02-28 16:52:51 -0800746 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700747 break;
Glenn Kasten788040c2011-05-05 08:19:00 -0700748
Mathias Agopian65ab4712010-07-14 17:59:35 -0700749 case RAMP_VOLUME:
750 case VOLUME:
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800751 switch (param) {
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800752 case AUXLEVEL:
Andy Hung6be49402014-05-30 10:42:03 -0700753 if (setVolumeRampVariables(*reinterpret_cast<float*>(value),
Andy Hung8ed196a2018-01-05 13:21:11 -0800754 target == RAMP_VOLUME ? mFrameCount : 0,
755 &track->auxLevel, &track->prevAuxLevel, &track->auxInc,
756 &track->mAuxLevel, &track->mPrevAuxLevel, &track->mAuxInc)) {
Andy Hung5866a3b2014-05-29 21:33:13 -0700757 ALOGV("setParameter(%s, AUXLEVEL: %04x)",
Andy Hung8ed196a2018-01-05 13:21:11 -0800758 target == VOLUME ? "VOLUME" : "RAMP_VOLUME", track->auxLevel);
759 invalidate();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700760 }
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800761 break;
Glenn Kasten788040c2011-05-05 08:19:00 -0700762 default:
Andy Hunge93b6b72014-07-17 21:30:53 -0700763 if ((unsigned)param >= VOLUME0 && (unsigned)param < VOLUME0 + MAX_NUM_VOLUMES) {
764 if (setVolumeRampVariables(*reinterpret_cast<float*>(value),
Andy Hung8ed196a2018-01-05 13:21:11 -0800765 target == RAMP_VOLUME ? mFrameCount : 0,
766 &track->volume[param - VOLUME0],
767 &track->prevVolume[param - VOLUME0],
768 &track->volumeInc[param - VOLUME0],
769 &track->mVolume[param - VOLUME0],
770 &track->mPrevVolume[param - VOLUME0],
771 &track->mVolumeInc[param - VOLUME0])) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700772 ALOGV("setParameter(%s, VOLUME%d: %04x)",
773 target == VOLUME ? "VOLUME" : "RAMP_VOLUME", param - VOLUME0,
Andy Hung8ed196a2018-01-05 13:21:11 -0800774 track->volume[param - VOLUME0]);
775 invalidate();
Andy Hunge93b6b72014-07-17 21:30:53 -0700776 }
777 } else {
778 LOG_ALWAYS_FATAL("setParameter volume: bad param %d", param);
779 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700780 }
781 break;
Andy Hungc5656cc2015-03-26 19:04:33 -0700782 case TIMESTRETCH:
783 switch (param) {
784 case PLAYBACK_RATE: {
Ricardo Garcia5a8a95d2015-04-18 14:47:04 -0700785 const AudioPlaybackRate *playbackRate =
786 reinterpret_cast<AudioPlaybackRate*>(value);
Ricardo Garcia6c7f0622015-04-30 18:39:16 -0700787 ALOGW_IF(!isAudioPlaybackRateValid(*playbackRate),
Andy Hung8ed196a2018-01-05 13:21:11 -0800788 "bad parameters speed %f, pitch %f",
789 playbackRate->mSpeed, playbackRate->mPitch);
790 if (track->setPlaybackRate(*playbackRate)) {
Ricardo Garcia5a8a95d2015-04-18 14:47:04 -0700791 ALOGV("setParameter(TIMESTRETCH, PLAYBACK_RATE, STRETCH_MODE, FALLBACK_MODE "
792 "%f %f %d %d",
793 playbackRate->mSpeed,
794 playbackRate->mPitch,
795 playbackRate->mStretchMode,
796 playbackRate->mFallbackMode);
Andy Hung8ed196a2018-01-05 13:21:11 -0800797 // invalidate(); (should not require reconfigure)
Andy Hungc5656cc2015-03-26 19:04:33 -0700798 }
Ricardo Garcia5a8a95d2015-04-18 14:47:04 -0700799 } break;
Andy Hungc5656cc2015-03-26 19:04:33 -0700800 default:
801 LOG_ALWAYS_FATAL("setParameter timestretch: bad param %d", param);
802 }
803 break;
Glenn Kasten788040c2011-05-05 08:19:00 -0700804
805 default:
Glenn Kastenadad3d72014-02-21 14:51:43 -0800806 LOG_ALWAYS_FATAL("setParameter: bad target %d", target);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700807 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700808}
809
Andy Hung8ed196a2018-01-05 13:21:11 -0800810bool AudioMixer::Track::setResampler(uint32_t trackSampleRate, uint32_t devSampleRate)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700811{
Andy Hung8ed196a2018-01-05 13:21:11 -0800812 if (trackSampleRate != devSampleRate || mResampler.get() != nullptr) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700813 if (sampleRate != trackSampleRate) {
814 sampleRate = trackSampleRate;
Andy Hung8ed196a2018-01-05 13:21:11 -0800815 if (mResampler.get() == nullptr) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700816 ALOGV("Creating resampler from track %d Hz to device %d Hz",
817 trackSampleRate, devSampleRate);
Glenn Kastenac602052012-10-01 14:04:31 -0700818 AudioResampler::src_quality quality;
819 // force lowest quality level resampler if use case isn't music or video
820 // FIXME this is flawed for dynamic sample rates, as we choose the resampler
821 // quality level based on the initial ratio, but that could change later.
822 // Should have a way to distinguish tracks with static ratios vs. dynamic ratios.
Andy Hungdb4c0312015-05-06 08:46:52 -0700823 if (isMusicRate(trackSampleRate)) {
Glenn Kastenac602052012-10-01 14:04:31 -0700824 quality = AudioResampler::DEFAULT_QUALITY;
Andy Hungdb4c0312015-05-06 08:46:52 -0700825 } else {
826 quality = AudioResampler::DYN_LOW_QUALITY;
Glenn Kastenac602052012-10-01 14:04:31 -0700827 }
Andy Hung296b7412014-06-17 15:25:47 -0700828
Andy Hunge93b6b72014-07-17 21:30:53 -0700829 // TODO: Remove MONO_HACK. Resampler sees #channels after the downmixer
830 // but if none exists, it is the channel count (1 for mono).
Andy Hung8ed196a2018-01-05 13:21:11 -0800831 const int resamplerChannelCount = mDownmixerBufferProvider.get() != nullptr
Andy Hunge93b6b72014-07-17 21:30:53 -0700832 ? mMixerChannelCount : channelCount;
Andy Hung9a592762014-07-21 21:56:01 -0700833 ALOGVV("Creating resampler:"
834 " format(%#x) channels(%d) devSampleRate(%u) quality(%d)\n",
835 mMixerInFormat, resamplerChannelCount, devSampleRate, quality);
Andy Hung8ed196a2018-01-05 13:21:11 -0800836 mResampler.reset(AudioResampler::create(
Andy Hung3348e362014-07-07 10:21:44 -0700837 mMixerInFormat,
Andy Hunge93b6b72014-07-17 21:30:53 -0700838 resamplerChannelCount,
Andy Hung8ed196a2018-01-05 13:21:11 -0800839 devSampleRate, quality));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700840 }
841 return true;
842 }
843 }
844 return false;
845}
846
Andy Hung8ed196a2018-01-05 13:21:11 -0800847bool AudioMixer::Track::setPlaybackRate(const AudioPlaybackRate &playbackRate)
Andy Hungc5656cc2015-03-26 19:04:33 -0700848{
Andy Hung8ed196a2018-01-05 13:21:11 -0800849 if ((mTimestretchBufferProvider.get() == nullptr &&
Ricardo Garcia5a8a95d2015-04-18 14:47:04 -0700850 fabs(playbackRate.mSpeed - mPlaybackRate.mSpeed) < AUDIO_TIMESTRETCH_SPEED_MIN_DELTA &&
851 fabs(playbackRate.mPitch - mPlaybackRate.mPitch) < AUDIO_TIMESTRETCH_PITCH_MIN_DELTA) ||
852 isAudioPlaybackRateEqual(playbackRate, mPlaybackRate)) {
Andy Hungc5656cc2015-03-26 19:04:33 -0700853 return false;
854 }
Ricardo Garcia5a8a95d2015-04-18 14:47:04 -0700855 mPlaybackRate = playbackRate;
Andy Hung8ed196a2018-01-05 13:21:11 -0800856 if (mTimestretchBufferProvider.get() == nullptr) {
Andy Hungc5656cc2015-03-26 19:04:33 -0700857 // TODO: Remove MONO_HACK. Resampler sees #channels after the downmixer
858 // but if none exists, it is the channel count (1 for mono).
Andy Hung8ed196a2018-01-05 13:21:11 -0800859 const int timestretchChannelCount = mDownmixerBufferProvider.get() != nullptr
Andy Hungc5656cc2015-03-26 19:04:33 -0700860 ? mMixerChannelCount : channelCount;
Andy Hung8ed196a2018-01-05 13:21:11 -0800861 mTimestretchBufferProvider.reset(new TimestretchBufferProvider(timestretchChannelCount,
862 mMixerInFormat, sampleRate, playbackRate));
Andy Hungc5656cc2015-03-26 19:04:33 -0700863 reconfigureBufferProviders();
864 } else {
Andy Hung8ed196a2018-01-05 13:21:11 -0800865 static_cast<TimestretchBufferProvider*>(mTimestretchBufferProvider.get())
Ricardo Garcia5a8a95d2015-04-18 14:47:04 -0700866 ->setPlaybackRate(playbackRate);
Andy Hungc5656cc2015-03-26 19:04:33 -0700867 }
868 return true;
869}
870
Andy Hung5e58b0a2014-06-23 19:07:29 -0700871/* Checks to see if the volume ramp has completed and clears the increment
872 * variables appropriately.
873 *
874 * FIXME: There is code to handle int/float ramp variable switchover should it not
875 * complete within a mixer buffer processing call, but it is preferred to avoid switchover
876 * due to precision issues. The switchover code is included for legacy code purposes
877 * and can be removed once the integer volume is removed.
878 *
879 * It is not sufficient to clear only the volumeInc integer variable because
880 * if one channel requires ramping, all channels are ramped.
881 *
882 * There is a bit of duplicated code here, but it keeps backward compatibility.
883 */
Andy Hung8ed196a2018-01-05 13:21:11 -0800884inline void AudioMixer::Track::adjustVolumeRamp(bool aux, bool useFloat)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700885{
Andy Hung5e58b0a2014-06-23 19:07:29 -0700886 if (useFloat) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700887 for (uint32_t i = 0; i < MAX_NUM_VOLUMES; i++) {
Eric Laurent43412fc2015-05-08 16:14:36 -0700888 if ((mVolumeInc[i] > 0 && mPrevVolume[i] + mVolumeInc[i] >= mVolume[i]) ||
889 (mVolumeInc[i] < 0 && mPrevVolume[i] + mVolumeInc[i] <= mVolume[i])) {
Andy Hung5e58b0a2014-06-23 19:07:29 -0700890 volumeInc[i] = 0;
891 prevVolume[i] = volume[i] << 16;
892 mVolumeInc[i] = 0.;
893 mPrevVolume[i] = mVolume[i];
Andy Hung5e58b0a2014-06-23 19:07:29 -0700894 } else {
895 //ALOGV("ramp: %f %f %f", mVolume[i], mPrevVolume[i], mVolumeInc[i]);
896 prevVolume[i] = u4_28_from_float(mPrevVolume[i]);
897 }
898 }
899 } else {
Andy Hunge93b6b72014-07-17 21:30:53 -0700900 for (uint32_t i = 0; i < MAX_NUM_VOLUMES; i++) {
Andy Hung5e58b0a2014-06-23 19:07:29 -0700901 if (((volumeInc[i]>0) && (((prevVolume[i]+volumeInc[i])>>16) >= volume[i])) ||
902 ((volumeInc[i]<0) && (((prevVolume[i]+volumeInc[i])>>16) <= volume[i]))) {
903 volumeInc[i] = 0;
904 prevVolume[i] = volume[i] << 16;
905 mVolumeInc[i] = 0.;
906 mPrevVolume[i] = mVolume[i];
907 } else {
908 //ALOGV("ramp: %d %d %d", volume[i] << 16, prevVolume[i], volumeInc[i]);
909 mPrevVolume[i] = float_from_u4_28(prevVolume[i]);
910 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700911 }
912 }
Andy Hung116a4982017-11-30 10:15:08 -0800913
Mathias Agopian65ab4712010-07-14 17:59:35 -0700914 if (aux) {
Andy Hung116a4982017-11-30 10:15:08 -0800915#ifdef FLOAT_AUX
916 if (useFloat) {
917 if ((mAuxInc > 0.f && mPrevAuxLevel + mAuxInc >= mAuxLevel) ||
918 (mAuxInc < 0.f && mPrevAuxLevel + mAuxInc <= mAuxLevel)) {
919 auxInc = 0;
920 prevAuxLevel = auxLevel << 16;
921 mAuxInc = 0.f;
922 mPrevAuxLevel = mAuxLevel;
923 }
924 } else
925#endif
926 if ((auxInc > 0 && ((prevAuxLevel + auxInc) >> 16) >= auxLevel) ||
927 (auxInc < 0 && ((prevAuxLevel + auxInc) >> 16) <= auxLevel)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700928 auxInc = 0;
Andy Hung5e58b0a2014-06-23 19:07:29 -0700929 prevAuxLevel = auxLevel << 16;
Andy Hung116a4982017-11-30 10:15:08 -0800930 mAuxInc = 0.f;
Andy Hung5e58b0a2014-06-23 19:07:29 -0700931 mPrevAuxLevel = mAuxLevel;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700932 }
933 }
934}
935
Glenn Kastenc59c0042012-02-02 14:06:11 -0800936size_t AudioMixer::getUnreleasedFrames(int name) const
Eric Laurent071ccd52011-12-22 16:08:41 -0800937{
Andy Hung8ed196a2018-01-05 13:21:11 -0800938 const auto it = mTracks.find(name);
939 if (it != mTracks.end()) {
940 return it->second->getUnreleasedFrames();
Eric Laurent071ccd52011-12-22 16:08:41 -0800941 }
942 return 0;
943}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700944
Glenn Kasten01c4ebf2012-02-22 10:47:35 -0800945void AudioMixer::setBufferProvider(int name, AudioBufferProvider* bufferProvider)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700946{
Andy Hung1bc088a2018-02-09 15:57:31 -0800947 LOG_ALWAYS_FATAL_IF(!exists(name), "invalid name: %d", name);
Andy Hung8ed196a2018-01-05 13:21:11 -0800948 const std::shared_ptr<Track> &track = mTracks[name];
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700949
Andy Hung8ed196a2018-01-05 13:21:11 -0800950 if (track->mInputBufferProvider == bufferProvider) {
Andy Hung1d26ddf2014-05-29 15:53:09 -0700951 return; // don't reset any buffer providers if identical.
952 }
Andy Hung3a34df92018-08-21 12:32:30 -0700953 // reset order from downstream to upstream buffer providers.
954 if (track->mTimestretchBufferProvider.get() != nullptr) {
955 track->mTimestretchBufferProvider->reset();
Andy Hung8ed196a2018-01-05 13:21:11 -0800956 } else if (track->mPostDownmixReformatBufferProvider.get() != nullptr) {
957 track->mPostDownmixReformatBufferProvider->reset();
Andy Hung3a34df92018-08-21 12:32:30 -0700958 } else if (track->mDownmixerBufferProvider != nullptr) {
959 track->mDownmixerBufferProvider->reset();
960 } else if (track->mReformatBufferProvider.get() != nullptr) {
961 track->mReformatBufferProvider->reset();
jiabindce8f8c2018-12-10 17:49:31 -0800962 } else if (track->mAdjustChannelsNonDestructiveBufferProvider.get() != nullptr) {
963 track->mAdjustChannelsNonDestructiveBufferProvider->reset();
964 } else if (track->mAdjustChannelsBufferProvider.get() != nullptr) {
965 track->mAdjustChannelsBufferProvider->reset();
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700966 }
Andy Hungef7c7fb2014-05-12 16:51:41 -0700967
Andy Hung8ed196a2018-01-05 13:21:11 -0800968 track->mInputBufferProvider = bufferProvider;
969 track->reconfigureBufferProviders();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700970}
971
Andy Hung8ed196a2018-01-05 13:21:11 -0800972void AudioMixer::process__validate()
Mathias Agopian65ab4712010-07-14 17:59:35 -0700973{
Andy Hung395db4b2014-08-25 17:15:29 -0700974 // TODO: fix all16BitsStereNoResample logic to
975 // either properly handle muted tracks (it should ignore them)
976 // or remove altogether as an obsolete optimization.
Glenn Kasten4c340c62012-01-27 12:33:54 -0800977 bool all16BitsStereoNoResample = true;
978 bool resampling = false;
979 bool volumeRamp = false;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700980
Andy Hung8ed196a2018-01-05 13:21:11 -0800981 mEnabled.clear();
982 mGroups.clear();
983 for (const auto &pair : mTracks) {
984 const int name = pair.first;
985 const std::shared_ptr<Track> &t = pair.second;
986 if (!t->enabled) continue;
987
988 mEnabled.emplace_back(name); // we add to mEnabled in order of name.
989 mGroups[t->mainBuffer].emplace_back(name); // mGroups also in order of name.
990
Mathias Agopian65ab4712010-07-14 17:59:35 -0700991 uint32_t n = 0;
Glenn Kastend6fadf02013-10-30 14:37:29 -0700992 // FIXME can overflow (mask is only 3 bits)
Andy Hung8ed196a2018-01-05 13:21:11 -0800993 n |= NEEDS_CHANNEL_1 + t->channelCount - 1;
994 if (t->doesResample()) {
Glenn Kastend6fadf02013-10-30 14:37:29 -0700995 n |= NEEDS_RESAMPLE;
996 }
Andy Hung8ed196a2018-01-05 13:21:11 -0800997 if (t->auxLevel != 0 && t->auxBuffer != NULL) {
Glenn Kastend6fadf02013-10-30 14:37:29 -0700998 n |= NEEDS_AUX;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700999 }
1000
Andy Hung8ed196a2018-01-05 13:21:11 -08001001 if (t->volumeInc[0]|t->volumeInc[1]) {
Glenn Kasten4c340c62012-01-27 12:33:54 -08001002 volumeRamp = true;
Andy Hung8ed196a2018-01-05 13:21:11 -08001003 } else if (!t->doesResample() && t->volumeRL == 0) {
Glenn Kastend6fadf02013-10-30 14:37:29 -07001004 n |= NEEDS_MUTE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001005 }
Andy Hung8ed196a2018-01-05 13:21:11 -08001006 t->needs = n;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001007
Glenn Kastend6fadf02013-10-30 14:37:29 -07001008 if (n & NEEDS_MUTE) {
Andy Hung8ed196a2018-01-05 13:21:11 -08001009 t->hook = &Track::track__nop;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001010 } else {
Glenn Kastend6fadf02013-10-30 14:37:29 -07001011 if (n & NEEDS_AUX) {
Glenn Kasten4c340c62012-01-27 12:33:54 -08001012 all16BitsStereoNoResample = false;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001013 }
Glenn Kastend6fadf02013-10-30 14:37:29 -07001014 if (n & NEEDS_RESAMPLE) {
Glenn Kasten4c340c62012-01-27 12:33:54 -08001015 all16BitsStereoNoResample = false;
1016 resampling = true;
Andy Hung8ed196a2018-01-05 13:21:11 -08001017 t->hook = Track::getTrackHook(TRACKTYPE_RESAMPLE, t->mMixerChannelCount,
1018 t->mMixerInFormat, t->mMixerFormat);
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -07001019 ALOGV_IF((n & NEEDS_CHANNEL_COUNT__MASK) > NEEDS_CHANNEL_2,
Chih-Hung Hsieh09f9c022018-07-27 10:22:35 -07001020 "Track %d needs downmix + resample", name);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001021 } else {
1022 if ((n & NEEDS_CHANNEL_COUNT__MASK) == NEEDS_CHANNEL_1){
Andy Hung8ed196a2018-01-05 13:21:11 -08001023 t->hook = Track::getTrackHook(
1024 (t->mMixerChannelMask == AUDIO_CHANNEL_OUT_STEREO // TODO: MONO_HACK
1025 && t->channelMask == AUDIO_CHANNEL_OUT_MONO)
Andy Hunge93b6b72014-07-17 21:30:53 -07001026 ? TRACKTYPE_NORESAMPLEMONO : TRACKTYPE_NORESAMPLE,
Andy Hung8ed196a2018-01-05 13:21:11 -08001027 t->mMixerChannelCount,
1028 t->mMixerInFormat, t->mMixerFormat);
Glenn Kasten4c340c62012-01-27 12:33:54 -08001029 all16BitsStereoNoResample = false;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001030 }
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -07001031 if ((n & NEEDS_CHANNEL_COUNT__MASK) >= NEEDS_CHANNEL_2){
Andy Hung8ed196a2018-01-05 13:21:11 -08001032 t->hook = Track::getTrackHook(TRACKTYPE_NORESAMPLE, t->mMixerChannelCount,
1033 t->mMixerInFormat, t->mMixerFormat);
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -07001034 ALOGV_IF((n & NEEDS_CHANNEL_COUNT__MASK) > NEEDS_CHANNEL_2,
Chih-Hung Hsieh09f9c022018-07-27 10:22:35 -07001035 "Track %d needs downmix", name);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001036 }
1037 }
1038 }
1039 }
1040
1041 // select the processing hooks
Andy Hung8ed196a2018-01-05 13:21:11 -08001042 mHook = &AudioMixer::process__nop;
1043 if (mEnabled.size() > 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001044 if (resampling) {
Andy Hung8ed196a2018-01-05 13:21:11 -08001045 if (mOutputTemp.get() == nullptr) {
1046 mOutputTemp.reset(new int32_t[MAX_NUM_CHANNELS * mFrameCount]);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001047 }
Andy Hung8ed196a2018-01-05 13:21:11 -08001048 if (mResampleTemp.get() == nullptr) {
1049 mResampleTemp.reset(new int32_t[MAX_NUM_CHANNELS * mFrameCount]);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001050 }
Andy Hung8ed196a2018-01-05 13:21:11 -08001051 mHook = &AudioMixer::process__genericResampling;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001052 } else {
Andy Hung8ed196a2018-01-05 13:21:11 -08001053 // we keep temp arrays around.
1054 mHook = &AudioMixer::process__genericNoResampling;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001055 if (all16BitsStereoNoResample && !volumeRamp) {
Andy Hung8ed196a2018-01-05 13:21:11 -08001056 if (mEnabled.size() == 1) {
1057 const std::shared_ptr<Track> &t = mTracks[mEnabled[0]];
1058 if ((t->needs & NEEDS_MUTE) == 0) {
Andy Hung395db4b2014-08-25 17:15:29 -07001059 // The check prevents a muted track from acquiring a process hook.
1060 //
1061 // This is dangerous if the track is MONO as that requires
1062 // special case handling due to implicit channel duplication.
1063 // Stereo or Multichannel should actually be fine here.
Andy Hung8ed196a2018-01-05 13:21:11 -08001064 mHook = getProcessHook(PROCESSTYPE_NORESAMPLEONETRACK,
1065 t->mMixerChannelCount, t->mMixerInFormat, t->mMixerFormat);
Andy Hung395db4b2014-08-25 17:15:29 -07001066 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001067 }
1068 }
1069 }
1070 }
1071
Andy Hung8ed196a2018-01-05 13:21:11 -08001072 ALOGV("mixer configuration change: %zu "
Mathias Agopian65ab4712010-07-14 17:59:35 -07001073 "all16BitsStereoNoResample=%d, resampling=%d, volumeRamp=%d",
Andy Hung8ed196a2018-01-05 13:21:11 -08001074 mEnabled.size(), all16BitsStereoNoResample, resampling, volumeRamp);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001075
Andy Hung8ed196a2018-01-05 13:21:11 -08001076 process();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001077
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -08001078 // Now that the volume ramp has been done, set optimal state and
1079 // track hooks for subsequent mixer process
Andy Hung8ed196a2018-01-05 13:21:11 -08001080 if (mEnabled.size() > 0) {
Glenn Kasten4c340c62012-01-27 12:33:54 -08001081 bool allMuted = true;
Andy Hung8ed196a2018-01-05 13:21:11 -08001082
1083 for (const int name : mEnabled) {
1084 const std::shared_ptr<Track> &t = mTracks[name];
1085 if (!t->doesResample() && t->volumeRL == 0) {
1086 t->needs |= NEEDS_MUTE;
1087 t->hook = &Track::track__nop;
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -08001088 } else {
Glenn Kasten4c340c62012-01-27 12:33:54 -08001089 allMuted = false;
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -08001090 }
1091 }
1092 if (allMuted) {
Andy Hung8ed196a2018-01-05 13:21:11 -08001093 mHook = &AudioMixer::process__nop;
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -08001094 } else if (all16BitsStereoNoResample) {
Andy Hung8ed196a2018-01-05 13:21:11 -08001095 if (mEnabled.size() == 1) {
1096 //const int i = 31 - __builtin_clz(enabledTracks);
1097 const std::shared_ptr<Track> &t = mTracks[mEnabled[0]];
Andy Hung395db4b2014-08-25 17:15:29 -07001098 // Muted single tracks handled by allMuted above.
Andy Hung8ed196a2018-01-05 13:21:11 -08001099 mHook = getProcessHook(PROCESSTYPE_NORESAMPLEONETRACK,
1100 t->mMixerChannelCount, t->mMixerInFormat, t->mMixerFormat);
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -08001101 }
1102 }
1103 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001104}
1105
Andy Hung8ed196a2018-01-05 13:21:11 -08001106void AudioMixer::Track::track__genericResample(
1107 int32_t* out, size_t outFrameCount, int32_t* temp, int32_t* aux)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001108{
Andy Hung296b7412014-06-17 15:25:47 -07001109 ALOGVV("track__genericResample\n");
Andy Hung8ed196a2018-01-05 13:21:11 -08001110 mResampler->setSampleRate(sampleRate);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001111
1112 // ramp gain - resample to temp buffer and scale/mix in 2nd step
1113 if (aux != NULL) {
1114 // always resample with unity gain when sending to auxiliary buffer to be able
1115 // to apply send level after resampling
Andy Hung8ed196a2018-01-05 13:21:11 -08001116 mResampler->setVolume(UNITY_GAIN_FLOAT, UNITY_GAIN_FLOAT);
1117 memset(temp, 0, outFrameCount * mMixerChannelCount * sizeof(int32_t));
1118 mResampler->resample(temp, outFrameCount, bufferProvider);
1119 if (CC_UNLIKELY(volumeInc[0]|volumeInc[1]|auxInc)) {
1120 volumeRampStereo(out, outFrameCount, temp, aux);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001121 } else {
Andy Hung8ed196a2018-01-05 13:21:11 -08001122 volumeStereo(out, outFrameCount, temp, aux);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001123 }
1124 } else {
Andy Hung8ed196a2018-01-05 13:21:11 -08001125 if (CC_UNLIKELY(volumeInc[0]|volumeInc[1])) {
1126 mResampler->setVolume(UNITY_GAIN_FLOAT, UNITY_GAIN_FLOAT);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001127 memset(temp, 0, outFrameCount * MAX_NUM_CHANNELS * sizeof(int32_t));
Andy Hung8ed196a2018-01-05 13:21:11 -08001128 mResampler->resample(temp, outFrameCount, bufferProvider);
1129 volumeRampStereo(out, outFrameCount, temp, aux);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001130 }
1131
1132 // constant gain
1133 else {
Andy Hung8ed196a2018-01-05 13:21:11 -08001134 mResampler->setVolume(mVolume[0], mVolume[1]);
1135 mResampler->resample(out, outFrameCount, bufferProvider);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001136 }
1137 }
1138}
1139
Andy Hung8ed196a2018-01-05 13:21:11 -08001140void AudioMixer::Track::track__nop(int32_t* out __unused,
Andy Hungee931ff2014-01-28 13:44:14 -08001141 size_t outFrameCount __unused, int32_t* temp __unused, int32_t* aux __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001142{
1143}
1144
Andy Hung8ed196a2018-01-05 13:21:11 -08001145void AudioMixer::Track::volumeRampStereo(
1146 int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001147{
Andy Hung8ed196a2018-01-05 13:21:11 -08001148 int32_t vl = prevVolume[0];
1149 int32_t vr = prevVolume[1];
1150 const int32_t vlInc = volumeInc[0];
1151 const int32_t vrInc = volumeInc[1];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001152
Steve Blockb8a80522011-12-20 16:23:08 +00001153 //ALOGD("[0] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
Andy Hung8ed196a2018-01-05 13:21:11 -08001154 // t, vlInc/65536.0f, vl/65536.0f, volume[0],
Mathias Agopian65ab4712010-07-14 17:59:35 -07001155 // (vl + vlInc*frameCount)/65536.0f, frameCount);
1156
1157 // ramp volume
Glenn Kastenf6b16782011-12-15 09:51:17 -08001158 if (CC_UNLIKELY(aux != NULL)) {
Andy Hung8ed196a2018-01-05 13:21:11 -08001159 int32_t va = prevAuxLevel;
1160 const int32_t vaInc = auxInc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001161 int32_t l;
1162 int32_t r;
1163
1164 do {
1165 l = (*temp++ >> 12);
1166 r = (*temp++ >> 12);
1167 *out++ += (vl >> 16) * l;
1168 *out++ += (vr >> 16) * r;
1169 *aux++ += (va >> 17) * (l + r);
1170 vl += vlInc;
1171 vr += vrInc;
1172 va += vaInc;
1173 } while (--frameCount);
Andy Hung8ed196a2018-01-05 13:21:11 -08001174 prevAuxLevel = va;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001175 } else {
1176 do {
1177 *out++ += (vl >> 16) * (*temp++ >> 12);
1178 *out++ += (vr >> 16) * (*temp++ >> 12);
1179 vl += vlInc;
1180 vr += vrInc;
1181 } while (--frameCount);
1182 }
Andy Hung8ed196a2018-01-05 13:21:11 -08001183 prevVolume[0] = vl;
1184 prevVolume[1] = vr;
1185 adjustVolumeRamp(aux != NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001186}
1187
Andy Hung8ed196a2018-01-05 13:21:11 -08001188void AudioMixer::Track::volumeStereo(
1189 int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001190{
Andy Hung8ed196a2018-01-05 13:21:11 -08001191 const int16_t vl = volume[0];
1192 const int16_t vr = volume[1];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001193
Glenn Kastenf6b16782011-12-15 09:51:17 -08001194 if (CC_UNLIKELY(aux != NULL)) {
Andy Hung8ed196a2018-01-05 13:21:11 -08001195 const int16_t va = auxLevel;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001196 do {
1197 int16_t l = (int16_t)(*temp++ >> 12);
1198 int16_t r = (int16_t)(*temp++ >> 12);
1199 out[0] = mulAdd(l, vl, out[0]);
1200 int16_t a = (int16_t)(((int32_t)l + r) >> 1);
1201 out[1] = mulAdd(r, vr, out[1]);
1202 out += 2;
1203 aux[0] = mulAdd(a, va, aux[0]);
1204 aux++;
1205 } while (--frameCount);
1206 } else {
1207 do {
1208 int16_t l = (int16_t)(*temp++ >> 12);
1209 int16_t r = (int16_t)(*temp++ >> 12);
1210 out[0] = mulAdd(l, vl, out[0]);
1211 out[1] = mulAdd(r, vr, out[1]);
1212 out += 2;
1213 } while (--frameCount);
1214 }
1215}
1216
Andy Hung8ed196a2018-01-05 13:21:11 -08001217void AudioMixer::Track::track__16BitsStereo(
1218 int32_t* out, size_t frameCount, int32_t* temp __unused, int32_t* aux)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001219{
Andy Hung296b7412014-06-17 15:25:47 -07001220 ALOGVV("track__16BitsStereo\n");
Andy Hung8ed196a2018-01-05 13:21:11 -08001221 const int16_t *in = static_cast<const int16_t *>(mIn);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001222
Glenn Kastenf6b16782011-12-15 09:51:17 -08001223 if (CC_UNLIKELY(aux != NULL)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001224 int32_t l;
1225 int32_t r;
1226 // ramp gain
Andy Hung8ed196a2018-01-05 13:21:11 -08001227 if (CC_UNLIKELY(volumeInc[0]|volumeInc[1]|auxInc)) {
1228 int32_t vl = prevVolume[0];
1229 int32_t vr = prevVolume[1];
1230 int32_t va = prevAuxLevel;
1231 const int32_t vlInc = volumeInc[0];
1232 const int32_t vrInc = volumeInc[1];
1233 const int32_t vaInc = auxInc;
Steve Blockb8a80522011-12-20 16:23:08 +00001234 // ALOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
Andy Hung8ed196a2018-01-05 13:21:11 -08001235 // t, vlInc/65536.0f, vl/65536.0f, volume[0],
Mathias Agopian65ab4712010-07-14 17:59:35 -07001236 // (vl + vlInc*frameCount)/65536.0f, frameCount);
1237
1238 do {
1239 l = (int32_t)*in++;
1240 r = (int32_t)*in++;
1241 *out++ += (vl >> 16) * l;
1242 *out++ += (vr >> 16) * r;
1243 *aux++ += (va >> 17) * (l + r);
1244 vl += vlInc;
1245 vr += vrInc;
1246 va += vaInc;
1247 } while (--frameCount);
1248
Andy Hung8ed196a2018-01-05 13:21:11 -08001249 prevVolume[0] = vl;
1250 prevVolume[1] = vr;
1251 prevAuxLevel = va;
1252 adjustVolumeRamp(true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001253 }
1254
1255 // constant gain
1256 else {
Andy Hung8ed196a2018-01-05 13:21:11 -08001257 const uint32_t vrl = volumeRL;
1258 const int16_t va = (int16_t)auxLevel;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001259 do {
Glenn Kasten54c3b662012-01-06 07:46:30 -08001260 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001261 int16_t a = (int16_t)(((int32_t)in[0] + in[1]) >> 1);
1262 in += 2;
1263 out[0] = mulAddRL(1, rl, vrl, out[0]);
1264 out[1] = mulAddRL(0, rl, vrl, out[1]);
1265 out += 2;
1266 aux[0] = mulAdd(a, va, aux[0]);
1267 aux++;
1268 } while (--frameCount);
1269 }
1270 } else {
1271 // ramp gain
Andy Hung8ed196a2018-01-05 13:21:11 -08001272 if (CC_UNLIKELY(volumeInc[0]|volumeInc[1])) {
1273 int32_t vl = prevVolume[0];
1274 int32_t vr = prevVolume[1];
1275 const int32_t vlInc = volumeInc[0];
1276 const int32_t vrInc = volumeInc[1];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001277
Steve Blockb8a80522011-12-20 16:23:08 +00001278 // ALOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
Andy Hung8ed196a2018-01-05 13:21:11 -08001279 // t, vlInc/65536.0f, vl/65536.0f, volume[0],
Mathias Agopian65ab4712010-07-14 17:59:35 -07001280 // (vl + vlInc*frameCount)/65536.0f, frameCount);
1281
1282 do {
1283 *out++ += (vl >> 16) * (int32_t) *in++;
1284 *out++ += (vr >> 16) * (int32_t) *in++;
1285 vl += vlInc;
1286 vr += vrInc;
1287 } while (--frameCount);
1288
Andy Hung8ed196a2018-01-05 13:21:11 -08001289 prevVolume[0] = vl;
1290 prevVolume[1] = vr;
1291 adjustVolumeRamp(false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001292 }
1293
1294 // constant gain
1295 else {
Andy Hung8ed196a2018-01-05 13:21:11 -08001296 const uint32_t vrl = volumeRL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001297 do {
Glenn Kasten54c3b662012-01-06 07:46:30 -08001298 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001299 in += 2;
1300 out[0] = mulAddRL(1, rl, vrl, out[0]);
1301 out[1] = mulAddRL(0, rl, vrl, out[1]);
1302 out += 2;
1303 } while (--frameCount);
1304 }
1305 }
Andy Hung8ed196a2018-01-05 13:21:11 -08001306 mIn = in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001307}
1308
Andy Hung8ed196a2018-01-05 13:21:11 -08001309void AudioMixer::Track::track__16BitsMono(
1310 int32_t* out, size_t frameCount, int32_t* temp __unused, int32_t* aux)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001311{
Andy Hung296b7412014-06-17 15:25:47 -07001312 ALOGVV("track__16BitsMono\n");
Andy Hung8ed196a2018-01-05 13:21:11 -08001313 const int16_t *in = static_cast<int16_t const *>(mIn);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001314
Glenn Kastenf6b16782011-12-15 09:51:17 -08001315 if (CC_UNLIKELY(aux != NULL)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001316 // ramp gain
Andy Hung8ed196a2018-01-05 13:21:11 -08001317 if (CC_UNLIKELY(volumeInc[0]|volumeInc[1]|auxInc)) {
1318 int32_t vl = prevVolume[0];
1319 int32_t vr = prevVolume[1];
1320 int32_t va = prevAuxLevel;
1321 const int32_t vlInc = volumeInc[0];
1322 const int32_t vrInc = volumeInc[1];
1323 const int32_t vaInc = auxInc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001324
Steve Blockb8a80522011-12-20 16:23:08 +00001325 // ALOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
Andy Hung8ed196a2018-01-05 13:21:11 -08001326 // t, vlInc/65536.0f, vl/65536.0f, volume[0],
Mathias Agopian65ab4712010-07-14 17:59:35 -07001327 // (vl + vlInc*frameCount)/65536.0f, frameCount);
1328
1329 do {
1330 int32_t l = *in++;
1331 *out++ += (vl >> 16) * l;
1332 *out++ += (vr >> 16) * l;
1333 *aux++ += (va >> 16) * l;
1334 vl += vlInc;
1335 vr += vrInc;
1336 va += vaInc;
1337 } while (--frameCount);
1338
Andy Hung8ed196a2018-01-05 13:21:11 -08001339 prevVolume[0] = vl;
1340 prevVolume[1] = vr;
1341 prevAuxLevel = va;
1342 adjustVolumeRamp(true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001343 }
1344 // constant gain
1345 else {
Andy Hung8ed196a2018-01-05 13:21:11 -08001346 const int16_t vl = volume[0];
1347 const int16_t vr = volume[1];
1348 const int16_t va = (int16_t)auxLevel;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001349 do {
1350 int16_t l = *in++;
1351 out[0] = mulAdd(l, vl, out[0]);
1352 out[1] = mulAdd(l, vr, out[1]);
1353 out += 2;
1354 aux[0] = mulAdd(l, va, aux[0]);
1355 aux++;
1356 } while (--frameCount);
1357 }
1358 } else {
1359 // ramp gain
Andy Hung8ed196a2018-01-05 13:21:11 -08001360 if (CC_UNLIKELY(volumeInc[0]|volumeInc[1])) {
1361 int32_t vl = prevVolume[0];
1362 int32_t vr = prevVolume[1];
1363 const int32_t vlInc = volumeInc[0];
1364 const int32_t vrInc = volumeInc[1];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001365
Steve Blockb8a80522011-12-20 16:23:08 +00001366 // ALOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
Andy Hung8ed196a2018-01-05 13:21:11 -08001367 // t, vlInc/65536.0f, vl/65536.0f, volume[0],
Mathias Agopian65ab4712010-07-14 17:59:35 -07001368 // (vl + vlInc*frameCount)/65536.0f, frameCount);
1369
1370 do {
1371 int32_t l = *in++;
1372 *out++ += (vl >> 16) * l;
1373 *out++ += (vr >> 16) * l;
1374 vl += vlInc;
1375 vr += vrInc;
1376 } while (--frameCount);
1377
Andy Hung8ed196a2018-01-05 13:21:11 -08001378 prevVolume[0] = vl;
1379 prevVolume[1] = vr;
1380 adjustVolumeRamp(false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001381 }
1382 // constant gain
1383 else {
Andy Hung8ed196a2018-01-05 13:21:11 -08001384 const int16_t vl = volume[0];
1385 const int16_t vr = volume[1];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001386 do {
1387 int16_t l = *in++;
1388 out[0] = mulAdd(l, vl, out[0]);
1389 out[1] = mulAdd(l, vr, out[1]);
1390 out += 2;
1391 } while (--frameCount);
1392 }
1393 }
Andy Hung8ed196a2018-01-05 13:21:11 -08001394 mIn = in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001395}
1396
Mathias Agopian65ab4712010-07-14 17:59:35 -07001397// no-op case
Andy Hung8ed196a2018-01-05 13:21:11 -08001398void AudioMixer::process__nop()
Mathias Agopian65ab4712010-07-14 17:59:35 -07001399{
Andy Hung296b7412014-06-17 15:25:47 -07001400 ALOGVV("process__nop\n");
Andy Hung8ed196a2018-01-05 13:21:11 -08001401
1402 for (const auto &pair : mGroups) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001403 // process by group of tracks with same output buffer to
1404 // avoid multiple memset() on same buffer
Andy Hung8ed196a2018-01-05 13:21:11 -08001405 const auto &group = pair.second;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001406
Andy Hung8ed196a2018-01-05 13:21:11 -08001407 const std::shared_ptr<Track> &t = mTracks[group[0]];
1408 memset(t->mainBuffer, 0,
jiabin245cdd92018-12-07 17:55:15 -08001409 mFrameCount * audio_bytes_per_frame(
1410 t->mMixerChannelCount + t->mMixerHapticChannelCount, t->mMixerFormat));
Mathias Agopian65ab4712010-07-14 17:59:35 -07001411
Andy Hung8ed196a2018-01-05 13:21:11 -08001412 // now consume data
1413 for (const int name : group) {
1414 const std::shared_ptr<Track> &t = mTracks[name];
1415 size_t outFrames = mFrameCount;
1416 while (outFrames) {
1417 t->buffer.frameCount = outFrames;
1418 t->bufferProvider->getNextBuffer(&t->buffer);
1419 if (t->buffer.raw == NULL) break;
1420 outFrames -= t->buffer.frameCount;
1421 t->bufferProvider->releaseBuffer(&t->buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001422 }
1423 }
1424 }
1425}
1426
1427// generic code without resampling
Andy Hung8ed196a2018-01-05 13:21:11 -08001428void AudioMixer::process__genericNoResampling()
Mathias Agopian65ab4712010-07-14 17:59:35 -07001429{
Andy Hung296b7412014-06-17 15:25:47 -07001430 ALOGVV("process__genericNoResampling\n");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001431 int32_t outTemp[BLOCKSIZE * MAX_NUM_CHANNELS] __attribute__((aligned(32)));
1432
Andy Hung8ed196a2018-01-05 13:21:11 -08001433 for (const auto &pair : mGroups) {
1434 // process by group of tracks with same output main buffer to
1435 // avoid multiple memset() on same buffer
1436 const auto &group = pair.second;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001437
Andy Hung8ed196a2018-01-05 13:21:11 -08001438 // acquire buffer
1439 for (const int name : group) {
1440 const std::shared_ptr<Track> &t = mTracks[name];
1441 t->buffer.frameCount = mFrameCount;
1442 t->bufferProvider->getNextBuffer(&t->buffer);
1443 t->frameCount = t->buffer.frameCount;
1444 t->mIn = t->buffer.raw;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001445 }
Andy Hung8ed196a2018-01-05 13:21:11 -08001446
1447 int32_t *out = (int *)pair.first;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001448 size_t numFrames = 0;
1449 do {
Andy Hung8ed196a2018-01-05 13:21:11 -08001450 const size_t frameCount = std::min((size_t)BLOCKSIZE, mFrameCount - numFrames);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001451 memset(outTemp, 0, sizeof(outTemp));
Andy Hung8ed196a2018-01-05 13:21:11 -08001452 for (const int name : group) {
1453 const std::shared_ptr<Track> &t = mTracks[name];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001454 int32_t *aux = NULL;
Andy Hung8ed196a2018-01-05 13:21:11 -08001455 if (CC_UNLIKELY(t->needs & NEEDS_AUX)) {
1456 aux = t->auxBuffer + numFrames;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001457 }
Andy Hung8ed196a2018-01-05 13:21:11 -08001458 for (int outFrames = frameCount; outFrames > 0; ) {
1459 // t->in == nullptr can happen if the track was flushed just after having
Gaurav Kumar7e79cd22014-01-06 10:57:18 +05301460 // been enabled for mixing.
Andy Hung8ed196a2018-01-05 13:21:11 -08001461 if (t->mIn == nullptr) {
Gaurav Kumar7e79cd22014-01-06 10:57:18 +05301462 break;
1463 }
Andy Hung8ed196a2018-01-05 13:21:11 -08001464 size_t inFrames = (t->frameCount > outFrames)?outFrames:t->frameCount;
Glenn Kasten34fca342013-08-13 09:48:14 -07001465 if (inFrames > 0) {
Andy Hung8ed196a2018-01-05 13:21:11 -08001466 (t.get()->*t->hook)(
1467 outTemp + (frameCount - outFrames) * t->mMixerChannelCount,
1468 inFrames, mResampleTemp.get() /* naked ptr */, aux);
1469 t->frameCount -= inFrames;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001470 outFrames -= inFrames;
Glenn Kastenf6b16782011-12-15 09:51:17 -08001471 if (CC_UNLIKELY(aux != NULL)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001472 aux += inFrames;
1473 }
1474 }
Andy Hung8ed196a2018-01-05 13:21:11 -08001475 if (t->frameCount == 0 && outFrames) {
1476 t->bufferProvider->releaseBuffer(&t->buffer);
1477 t->buffer.frameCount = (mFrameCount - numFrames) -
Yahan Zhouc1c11b42018-01-16 12:44:04 -08001478 (frameCount - outFrames);
Andy Hung8ed196a2018-01-05 13:21:11 -08001479 t->bufferProvider->getNextBuffer(&t->buffer);
1480 t->mIn = t->buffer.raw;
1481 if (t->mIn == nullptr) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001482 break;
1483 }
Andy Hung8ed196a2018-01-05 13:21:11 -08001484 t->frameCount = t->buffer.frameCount;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001485 }
1486 }
1487 }
Andy Hung296b7412014-06-17 15:25:47 -07001488
Andy Hung8ed196a2018-01-05 13:21:11 -08001489 const std::shared_ptr<Track> &t1 = mTracks[group[0]];
1490 convertMixerFormat(out, t1->mMixerFormat, outTemp, t1->mMixerInFormat,
1491 frameCount * t1->mMixerChannelCount);
Andy Hung296b7412014-06-17 15:25:47 -07001492 // TODO: fix ugly casting due to choice of out pointer type
1493 out = reinterpret_cast<int32_t*>((uint8_t*)out
Andy Hung8ed196a2018-01-05 13:21:11 -08001494 + frameCount * t1->mMixerChannelCount
1495 * audio_bytes_per_sample(t1->mMixerFormat));
Yahan Zhouc1c11b42018-01-16 12:44:04 -08001496 numFrames += frameCount;
Andy Hung8ed196a2018-01-05 13:21:11 -08001497 } while (numFrames < mFrameCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001498
Andy Hung8ed196a2018-01-05 13:21:11 -08001499 // release each track's buffer
1500 for (const int name : group) {
1501 const std::shared_ptr<Track> &t = mTracks[name];
1502 t->bufferProvider->releaseBuffer(&t->buffer);
1503 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001504 }
1505}
1506
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -08001507// generic code with resampling
Andy Hung8ed196a2018-01-05 13:21:11 -08001508void AudioMixer::process__genericResampling()
Mathias Agopian65ab4712010-07-14 17:59:35 -07001509{
Andy Hung296b7412014-06-17 15:25:47 -07001510 ALOGVV("process__genericResampling\n");
Andy Hung8ed196a2018-01-05 13:21:11 -08001511 int32_t * const outTemp = mOutputTemp.get(); // naked ptr
1512 size_t numFrames = mFrameCount;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001513
Andy Hung8ed196a2018-01-05 13:21:11 -08001514 for (const auto &pair : mGroups) {
1515 const auto &group = pair.second;
1516 const std::shared_ptr<Track> &t1 = mTracks[group[0]];
1517
1518 // clear temp buffer
1519 memset(outTemp, 0, sizeof(*outTemp) * t1->mMixerChannelCount * mFrameCount);
1520 for (const int name : group) {
1521 const std::shared_ptr<Track> &t = mTracks[name];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001522 int32_t *aux = NULL;
Andy Hung8ed196a2018-01-05 13:21:11 -08001523 if (CC_UNLIKELY(t->needs & NEEDS_AUX)) {
1524 aux = t->auxBuffer;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001525 }
1526
1527 // this is a little goofy, on the resampling case we don't
1528 // acquire/release the buffers because it's done by
1529 // the resampler.
Andy Hung8ed196a2018-01-05 13:21:11 -08001530 if (t->needs & NEEDS_RESAMPLE) {
1531 (t.get()->*t->hook)(outTemp, numFrames, mResampleTemp.get() /* naked ptr */, aux);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001532 } else {
1533
1534 size_t outFrames = 0;
1535
1536 while (outFrames < numFrames) {
Andy Hung8ed196a2018-01-05 13:21:11 -08001537 t->buffer.frameCount = numFrames - outFrames;
1538 t->bufferProvider->getNextBuffer(&t->buffer);
1539 t->mIn = t->buffer.raw;
1540 // t->mIn == nullptr can happen if the track was flushed just after having
Mathias Agopian65ab4712010-07-14 17:59:35 -07001541 // been enabled for mixing.
Andy Hung8ed196a2018-01-05 13:21:11 -08001542 if (t->mIn == nullptr) break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001543
Andy Hung8ed196a2018-01-05 13:21:11 -08001544 (t.get()->*t->hook)(
1545 outTemp + outFrames * t->mMixerChannelCount, t->buffer.frameCount,
Andy Hunga6018892018-02-21 14:32:16 -08001546 mResampleTemp.get() /* naked ptr */,
1547 aux != nullptr ? aux + outFrames : nullptr);
Andy Hung8ed196a2018-01-05 13:21:11 -08001548 outFrames += t->buffer.frameCount;
Andy Hunga6018892018-02-21 14:32:16 -08001549
Andy Hung8ed196a2018-01-05 13:21:11 -08001550 t->bufferProvider->releaseBuffer(&t->buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001551 }
1552 }
1553 }
Andy Hung8ed196a2018-01-05 13:21:11 -08001554 convertMixerFormat(t1->mainBuffer, t1->mMixerFormat,
1555 outTemp, t1->mMixerInFormat, numFrames * t1->mMixerChannelCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001556 }
1557}
1558
1559// one track, 16 bits stereo without resampling is the most common case
Andy Hung8ed196a2018-01-05 13:21:11 -08001560void AudioMixer::process__oneTrack16BitsStereoNoResampling()
Mathias Agopian65ab4712010-07-14 17:59:35 -07001561{
Andy Hung8ed196a2018-01-05 13:21:11 -08001562 ALOGVV("process__oneTrack16BitsStereoNoResampling\n");
1563 LOG_ALWAYS_FATAL_IF(mEnabled.size() != 0,
1564 "%zu != 1 tracks enabled", mEnabled.size());
1565 const int name = mEnabled[0];
1566 const std::shared_ptr<Track> &t = mTracks[name];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001567
Andy Hung8ed196a2018-01-05 13:21:11 -08001568 AudioBufferProvider::Buffer& b(t->buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001569
Andy Hung8ed196a2018-01-05 13:21:11 -08001570 int32_t* out = t->mainBuffer;
Andy Hungf8a106a2014-05-29 18:52:38 -07001571 float *fout = reinterpret_cast<float*>(out);
Andy Hung8ed196a2018-01-05 13:21:11 -08001572 size_t numFrames = mFrameCount;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001573
Andy Hung8ed196a2018-01-05 13:21:11 -08001574 const int16_t vl = t->volume[0];
1575 const int16_t vr = t->volume[1];
1576 const uint32_t vrl = t->volumeRL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001577 while (numFrames) {
1578 b.frameCount = numFrames;
Andy Hung8ed196a2018-01-05 13:21:11 -08001579 t->bufferProvider->getNextBuffer(&b);
Glenn Kasten54c3b662012-01-06 07:46:30 -08001580 const int16_t *in = b.i16;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001581
1582 // in == NULL can happen if the track was flushed just after having
1583 // been enabled for mixing.
Andy Hungf8a106a2014-05-29 18:52:38 -07001584 if (in == NULL || (((uintptr_t)in) & 3)) {
Andy Hung8ed196a2018-01-05 13:21:11 -08001585 if ( AUDIO_FORMAT_PCM_FLOAT == t->mMixerFormat ) {
Jinguang Dong7c5ec032016-11-14 19:57:14 +08001586 memset((char*)fout, 0, numFrames
Andy Hung8ed196a2018-01-05 13:21:11 -08001587 * t->mMixerChannelCount * audio_bytes_per_sample(t->mMixerFormat));
Jinguang Dong7c5ec032016-11-14 19:57:14 +08001588 } else {
1589 memset((char*)out, 0, numFrames
Andy Hung8ed196a2018-01-05 13:21:11 -08001590 * t->mMixerChannelCount * audio_bytes_per_sample(t->mMixerFormat));
Jinguang Dong7c5ec032016-11-14 19:57:14 +08001591 }
Andy Hung395db4b2014-08-25 17:15:29 -07001592 ALOGE_IF((((uintptr_t)in) & 3),
Andy Hung8ed196a2018-01-05 13:21:11 -08001593 "process__oneTrack16BitsStereoNoResampling: misaligned buffer"
Andy Hung395db4b2014-08-25 17:15:29 -07001594 " %p track %d, channels %d, needs %08x, volume %08x vfl %f vfr %f",
Andy Hung8ed196a2018-01-05 13:21:11 -08001595 in, name, t->channelCount, t->needs, vrl, t->mVolume[0], t->mVolume[1]);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001596 return;
1597 }
1598 size_t outFrames = b.frameCount;
1599
Andy Hung8ed196a2018-01-05 13:21:11 -08001600 switch (t->mMixerFormat) {
Andy Hungf8a106a2014-05-29 18:52:38 -07001601 case AUDIO_FORMAT_PCM_FLOAT:
Mathias Agopian65ab4712010-07-14 17:59:35 -07001602 do {
Glenn Kasten54c3b662012-01-06 07:46:30 -08001603 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001604 in += 2;
Andy Hunga1ab7cc2014-02-24 19:26:52 -08001605 int32_t l = mulRL(1, rl, vrl);
1606 int32_t r = mulRL(0, rl, vrl);
Andy Hung84a0c6e2014-04-02 11:24:53 -07001607 *fout++ = float_from_q4_27(l);
1608 *fout++ = float_from_q4_27(r);
Andy Hung3375bde2014-02-28 15:51:47 -08001609 // Note: In case of later int16_t sink output,
1610 // conversion and clamping is done by memcpy_to_i16_from_float().
Mathias Agopian65ab4712010-07-14 17:59:35 -07001611 } while (--outFrames);
Andy Hungf8a106a2014-05-29 18:52:38 -07001612 break;
Andy Hunga1ab7cc2014-02-24 19:26:52 -08001613 case AUDIO_FORMAT_PCM_16_BIT:
Andy Hung97ae8242014-05-30 10:35:47 -07001614 if (CC_UNLIKELY(uint32_t(vl) > UNITY_GAIN_INT || uint32_t(vr) > UNITY_GAIN_INT)) {
Andy Hunga1ab7cc2014-02-24 19:26:52 -08001615 // volume is boosted, so we might need to clamp even though
1616 // we process only one track.
1617 do {
1618 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
1619 in += 2;
1620 int32_t l = mulRL(1, rl, vrl) >> 12;
1621 int32_t r = mulRL(0, rl, vrl) >> 12;
1622 // clamping...
1623 l = clamp16(l);
1624 r = clamp16(r);
1625 *out++ = (r<<16) | (l & 0xFFFF);
1626 } while (--outFrames);
1627 } else {
1628 do {
1629 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
1630 in += 2;
1631 int32_t l = mulRL(1, rl, vrl) >> 12;
1632 int32_t r = mulRL(0, rl, vrl) >> 12;
1633 *out++ = (r<<16) | (l & 0xFFFF);
1634 } while (--outFrames);
1635 }
1636 break;
1637 default:
Andy Hung8ed196a2018-01-05 13:21:11 -08001638 LOG_ALWAYS_FATAL("bad mixer format: %d", t->mMixerFormat);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001639 }
1640 numFrames -= b.frameCount;
Andy Hung8ed196a2018-01-05 13:21:11 -08001641 t->bufferProvider->releaseBuffer(&b);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001642 }
1643}
1644
Glenn Kasten52008f82012-03-18 09:34:41 -07001645/*static*/ pthread_once_t AudioMixer::sOnceControl = PTHREAD_ONCE_INIT;
1646
1647/*static*/ void AudioMixer::sInitRoutine()
1648{
Andy Hung34803d52014-07-16 21:41:35 -07001649 DownmixerBufferProvider::init(); // for the downmixer
John Grossman4ff14ba2012-02-08 16:37:41 -08001650}
1651
Andy Hunge93b6b72014-07-17 21:30:53 -07001652/* TODO: consider whether this level of optimization is necessary.
1653 * Perhaps just stick with a single for loop.
1654 */
1655
1656// Needs to derive a compile time constant (constexpr). Could be targeted to go
1657// to a MONOVOL mixtype based on MAX_NUM_VOLUMES, but that's an unnecessary complication.
Chih-Hung Hsiehbf291732016-05-17 15:16:07 -07001658#define MIXTYPE_MONOVOL(mixtype) ((mixtype) == MIXTYPE_MULTI ? MIXTYPE_MULTI_MONOVOL : \
1659 (mixtype) == MIXTYPE_MULTI_SAVEONLY ? MIXTYPE_MULTI_SAVEONLY_MONOVOL : (mixtype))
Andy Hunge93b6b72014-07-17 21:30:53 -07001660
1661/* MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
1662 * TO: int32_t (Q4.27) or float
1663 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
Andy Hung116a4982017-11-30 10:15:08 -08001664 * TA: int32_t (Q4.27) or float
Andy Hunge93b6b72014-07-17 21:30:53 -07001665 */
1666template <int MIXTYPE,
1667 typename TO, typename TI, typename TV, typename TA, typename TAV>
1668static void volumeRampMulti(uint32_t channels, TO* out, size_t frameCount,
1669 const TI* in, TA* aux, TV *vol, const TV *volinc, TAV *vola, TAV volainc)
1670{
1671 switch (channels) {
1672 case 1:
1673 volumeRampMulti<MIXTYPE, 1>(out, frameCount, in, aux, vol, volinc, vola, volainc);
1674 break;
1675 case 2:
1676 volumeRampMulti<MIXTYPE, 2>(out, frameCount, in, aux, vol, volinc, vola, volainc);
1677 break;
1678 case 3:
1679 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 3>(out,
1680 frameCount, in, aux, vol, volinc, vola, volainc);
1681 break;
1682 case 4:
1683 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 4>(out,
1684 frameCount, in, aux, vol, volinc, vola, volainc);
1685 break;
1686 case 5:
1687 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 5>(out,
1688 frameCount, in, aux, vol, volinc, vola, volainc);
1689 break;
1690 case 6:
1691 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 6>(out,
1692 frameCount, in, aux, vol, volinc, vola, volainc);
1693 break;
1694 case 7:
1695 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 7>(out,
1696 frameCount, in, aux, vol, volinc, vola, volainc);
1697 break;
1698 case 8:
1699 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 8>(out,
1700 frameCount, in, aux, vol, volinc, vola, volainc);
1701 break;
1702 }
1703}
1704
1705/* MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
1706 * TO: int32_t (Q4.27) or float
1707 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
Andy Hung116a4982017-11-30 10:15:08 -08001708 * TA: int32_t (Q4.27) or float
Andy Hunge93b6b72014-07-17 21:30:53 -07001709 */
1710template <int MIXTYPE,
1711 typename TO, typename TI, typename TV, typename TA, typename TAV>
1712static void volumeMulti(uint32_t channels, TO* out, size_t frameCount,
1713 const TI* in, TA* aux, const TV *vol, TAV vola)
1714{
1715 switch (channels) {
1716 case 1:
1717 volumeMulti<MIXTYPE, 1>(out, frameCount, in, aux, vol, vola);
1718 break;
1719 case 2:
1720 volumeMulti<MIXTYPE, 2>(out, frameCount, in, aux, vol, vola);
1721 break;
1722 case 3:
1723 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 3>(out, frameCount, in, aux, vol, vola);
1724 break;
1725 case 4:
1726 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 4>(out, frameCount, in, aux, vol, vola);
1727 break;
1728 case 5:
1729 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 5>(out, frameCount, in, aux, vol, vola);
1730 break;
1731 case 6:
1732 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 6>(out, frameCount, in, aux, vol, vola);
1733 break;
1734 case 7:
1735 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 7>(out, frameCount, in, aux, vol, vola);
1736 break;
1737 case 8:
1738 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 8>(out, frameCount, in, aux, vol, vola);
1739 break;
1740 }
1741}
1742
1743/* MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
1744 * USEFLOATVOL (set to true if float volume is used)
1745 * ADJUSTVOL (set to true if volume ramp parameters needs adjustment afterwards)
1746 * TO: int32_t (Q4.27) or float
1747 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
Andy Hung116a4982017-11-30 10:15:08 -08001748 * TA: int32_t (Q4.27) or float
Andy Hunge93b6b72014-07-17 21:30:53 -07001749 */
1750template <int MIXTYPE, bool USEFLOATVOL, bool ADJUSTVOL,
Andy Hung5e58b0a2014-06-23 19:07:29 -07001751 typename TO, typename TI, typename TA>
Andy Hung8ed196a2018-01-05 13:21:11 -08001752void AudioMixer::Track::volumeMix(TO *out, size_t outFrames,
1753 const TI *in, TA *aux, bool ramp)
Andy Hung5e58b0a2014-06-23 19:07:29 -07001754{
1755 if (USEFLOATVOL) {
1756 if (ramp) {
Andy Hung8ed196a2018-01-05 13:21:11 -08001757 volumeRampMulti<MIXTYPE>(mMixerChannelCount, out, outFrames, in, aux,
1758 mPrevVolume, mVolumeInc,
Andy Hung116a4982017-11-30 10:15:08 -08001759#ifdef FLOAT_AUX
Andy Hung8ed196a2018-01-05 13:21:11 -08001760 &mPrevAuxLevel, mAuxInc
Andy Hung116a4982017-11-30 10:15:08 -08001761#else
Andy Hung8ed196a2018-01-05 13:21:11 -08001762 &prevAuxLevel, auxInc
Andy Hung116a4982017-11-30 10:15:08 -08001763#endif
1764 );
Andy Hung5e58b0a2014-06-23 19:07:29 -07001765 if (ADJUSTVOL) {
Andy Hung8ed196a2018-01-05 13:21:11 -08001766 adjustVolumeRamp(aux != NULL, true);
Andy Hung5e58b0a2014-06-23 19:07:29 -07001767 }
1768 } else {
Andy Hung8ed196a2018-01-05 13:21:11 -08001769 volumeMulti<MIXTYPE>(mMixerChannelCount, out, outFrames, in, aux,
1770 mVolume,
Andy Hung116a4982017-11-30 10:15:08 -08001771#ifdef FLOAT_AUX
Andy Hung8ed196a2018-01-05 13:21:11 -08001772 mAuxLevel
Andy Hung116a4982017-11-30 10:15:08 -08001773#else
Andy Hung8ed196a2018-01-05 13:21:11 -08001774 auxLevel
Andy Hung116a4982017-11-30 10:15:08 -08001775#endif
1776 );
Andy Hung5e58b0a2014-06-23 19:07:29 -07001777 }
1778 } else {
1779 if (ramp) {
Andy Hung8ed196a2018-01-05 13:21:11 -08001780 volumeRampMulti<MIXTYPE>(mMixerChannelCount, out, outFrames, in, aux,
1781 prevVolume, volumeInc, &prevAuxLevel, auxInc);
Andy Hung5e58b0a2014-06-23 19:07:29 -07001782 if (ADJUSTVOL) {
Andy Hung8ed196a2018-01-05 13:21:11 -08001783 adjustVolumeRamp(aux != NULL);
Andy Hung5e58b0a2014-06-23 19:07:29 -07001784 }
1785 } else {
Andy Hung8ed196a2018-01-05 13:21:11 -08001786 volumeMulti<MIXTYPE>(mMixerChannelCount, out, outFrames, in, aux,
1787 volume, auxLevel);
Andy Hung5e58b0a2014-06-23 19:07:29 -07001788 }
1789 }
1790}
1791
Andy Hung296b7412014-06-17 15:25:47 -07001792/* This process hook is called when there is a single track without
1793 * aux buffer, volume ramp, or resampling.
1794 * TODO: Update the hook selection: this can properly handle aux and ramp.
Andy Hunge93b6b72014-07-17 21:30:53 -07001795 *
1796 * MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
1797 * TO: int32_t (Q4.27) or float
1798 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
1799 * TA: int32_t (Q4.27)
Andy Hung296b7412014-06-17 15:25:47 -07001800 */
Andy Hunge93b6b72014-07-17 21:30:53 -07001801template <int MIXTYPE, typename TO, typename TI, typename TA>
Andy Hung8ed196a2018-01-05 13:21:11 -08001802void AudioMixer::process__noResampleOneTrack()
Andy Hung296b7412014-06-17 15:25:47 -07001803{
Andy Hung8ed196a2018-01-05 13:21:11 -08001804 ALOGVV("process__noResampleOneTrack\n");
1805 LOG_ALWAYS_FATAL_IF(mEnabled.size() != 1,
1806 "%zu != 1 tracks enabled", mEnabled.size());
1807 const std::shared_ptr<Track> &t = mTracks[mEnabled[0]];
Andy Hunge93b6b72014-07-17 21:30:53 -07001808 const uint32_t channels = t->mMixerChannelCount;
Andy Hung296b7412014-06-17 15:25:47 -07001809 TO* out = reinterpret_cast<TO*>(t->mainBuffer);
1810 TA* aux = reinterpret_cast<TA*>(t->auxBuffer);
1811 const bool ramp = t->needsRamp();
1812
Andy Hung8ed196a2018-01-05 13:21:11 -08001813 for (size_t numFrames = mFrameCount; numFrames > 0; ) {
Andy Hung296b7412014-06-17 15:25:47 -07001814 AudioBufferProvider::Buffer& b(t->buffer);
1815 // get input buffer
1816 b.frameCount = numFrames;
Glenn Kastend79072e2016-01-06 08:41:20 -08001817 t->bufferProvider->getNextBuffer(&b);
Andy Hung296b7412014-06-17 15:25:47 -07001818 const TI *in = reinterpret_cast<TI*>(b.raw);
1819
1820 // in == NULL can happen if the track was flushed just after having
1821 // been enabled for mixing.
1822 if (in == NULL || (((uintptr_t)in) & 3)) {
1823 memset(out, 0, numFrames
Andy Hunge93b6b72014-07-17 21:30:53 -07001824 * channels * audio_bytes_per_sample(t->mMixerFormat));
Andy Hung8ed196a2018-01-05 13:21:11 -08001825 ALOGE_IF((((uintptr_t)in) & 3), "process__noResampleOneTrack: bus error: "
Andy Hung296b7412014-06-17 15:25:47 -07001826 "buffer %p track %p, channels %d, needs %#x",
Andy Hung8ed196a2018-01-05 13:21:11 -08001827 in, &t, t->channelCount, t->needs);
Andy Hung296b7412014-06-17 15:25:47 -07001828 return;
1829 }
1830
1831 const size_t outFrames = b.frameCount;
Andy Hung8ed196a2018-01-05 13:21:11 -08001832 t->volumeMix<MIXTYPE, is_same<TI, float>::value /* USEFLOATVOL */, false /* ADJUSTVOL */> (
1833 out, outFrames, in, aux, ramp);
Andy Hung5e58b0a2014-06-23 19:07:29 -07001834
Andy Hunge93b6b72014-07-17 21:30:53 -07001835 out += outFrames * channels;
Andy Hung296b7412014-06-17 15:25:47 -07001836 if (aux != NULL) {
Andy Hunga6018892018-02-21 14:32:16 -08001837 aux += outFrames;
Andy Hung296b7412014-06-17 15:25:47 -07001838 }
1839 numFrames -= b.frameCount;
1840
1841 // release buffer
1842 t->bufferProvider->releaseBuffer(&b);
1843 }
1844 if (ramp) {
Andy Hung5e58b0a2014-06-23 19:07:29 -07001845 t->adjustVolumeRamp(aux != NULL, is_same<TI, float>::value);
Andy Hung296b7412014-06-17 15:25:47 -07001846 }
1847}
1848
1849/* This track hook is called to do resampling then mixing,
1850 * pulling from the track's upstream AudioBufferProvider.
Andy Hunge93b6b72014-07-17 21:30:53 -07001851 *
1852 * MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
1853 * TO: int32_t (Q4.27) or float
1854 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
Andy Hung116a4982017-11-30 10:15:08 -08001855 * TA: int32_t (Q4.27) or float
Andy Hung296b7412014-06-17 15:25:47 -07001856 */
Andy Hunge93b6b72014-07-17 21:30:53 -07001857template <int MIXTYPE, typename TO, typename TI, typename TA>
Andy Hung8ed196a2018-01-05 13:21:11 -08001858void AudioMixer::Track::track__Resample(TO* out, size_t outFrameCount, TO* temp, TA* aux)
Andy Hung296b7412014-06-17 15:25:47 -07001859{
1860 ALOGVV("track__Resample\n");
Andy Hung8ed196a2018-01-05 13:21:11 -08001861 mResampler->setSampleRate(sampleRate);
1862 const bool ramp = needsRamp();
Andy Hung296b7412014-06-17 15:25:47 -07001863 if (ramp || aux != NULL) {
1864 // if ramp: resample with unity gain to temp buffer and scale/mix in 2nd step.
1865 // if aux != NULL: resample with unity gain to temp buffer then apply send level.
1866
Andy Hung8ed196a2018-01-05 13:21:11 -08001867 mResampler->setVolume(UNITY_GAIN_FLOAT, UNITY_GAIN_FLOAT);
1868 memset(temp, 0, outFrameCount * mMixerChannelCount * sizeof(TO));
1869 mResampler->resample((int32_t*)temp, outFrameCount, bufferProvider);
Andy Hung5e58b0a2014-06-23 19:07:29 -07001870
Andy Hung116a4982017-11-30 10:15:08 -08001871 volumeMix<MIXTYPE, is_same<TI, float>::value /* USEFLOATVOL */, true /* ADJUSTVOL */>(
Andy Hung8ed196a2018-01-05 13:21:11 -08001872 out, outFrameCount, temp, aux, ramp);
Andy Hung5e58b0a2014-06-23 19:07:29 -07001873
Andy Hung296b7412014-06-17 15:25:47 -07001874 } else { // constant volume gain
Andy Hung8ed196a2018-01-05 13:21:11 -08001875 mResampler->setVolume(mVolume[0], mVolume[1]);
1876 mResampler->resample((int32_t*)out, outFrameCount, bufferProvider);
Andy Hung296b7412014-06-17 15:25:47 -07001877 }
1878}
1879
1880/* This track hook is called to mix a track, when no resampling is required.
Andy Hung8ed196a2018-01-05 13:21:11 -08001881 * The input buffer should be present in in.
Andy Hunge93b6b72014-07-17 21:30:53 -07001882 *
1883 * MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
1884 * TO: int32_t (Q4.27) or float
1885 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
Andy Hung116a4982017-11-30 10:15:08 -08001886 * TA: int32_t (Q4.27) or float
Andy Hung296b7412014-06-17 15:25:47 -07001887 */
Andy Hunge93b6b72014-07-17 21:30:53 -07001888template <int MIXTYPE, typename TO, typename TI, typename TA>
Andy Hung8ed196a2018-01-05 13:21:11 -08001889void AudioMixer::Track::track__NoResample(TO* out, size_t frameCount, TO* temp __unused, TA* aux)
Andy Hung296b7412014-06-17 15:25:47 -07001890{
1891 ALOGVV("track__NoResample\n");
Andy Hung8ed196a2018-01-05 13:21:11 -08001892 const TI *in = static_cast<const TI *>(mIn);
Andy Hung296b7412014-06-17 15:25:47 -07001893
Andy Hung116a4982017-11-30 10:15:08 -08001894 volumeMix<MIXTYPE, is_same<TI, float>::value /* USEFLOATVOL */, true /* ADJUSTVOL */>(
Andy Hung8ed196a2018-01-05 13:21:11 -08001895 out, frameCount, in, aux, needsRamp());
Andy Hung5e58b0a2014-06-23 19:07:29 -07001896
Andy Hung296b7412014-06-17 15:25:47 -07001897 // MIXTYPE_MONOEXPAND reads a single input channel and expands to NCHAN output channels.
1898 // MIXTYPE_MULTI reads NCHAN input channels and places to NCHAN output channels.
Andy Hung8ed196a2018-01-05 13:21:11 -08001899 in += (MIXTYPE == MIXTYPE_MONOEXPAND) ? frameCount : frameCount * mMixerChannelCount;
1900 mIn = in;
Andy Hung296b7412014-06-17 15:25:47 -07001901}
1902
1903/* The Mixer engine generates either int32_t (Q4_27) or float data.
1904 * We use this function to convert the engine buffers
1905 * to the desired mixer output format, either int16_t (Q.15) or float.
1906 */
Andy Hung8ed196a2018-01-05 13:21:11 -08001907/* static */
Andy Hung296b7412014-06-17 15:25:47 -07001908void AudioMixer::convertMixerFormat(void *out, audio_format_t mixerOutFormat,
1909 void *in, audio_format_t mixerInFormat, size_t sampleCount)
1910{
1911 switch (mixerInFormat) {
1912 case AUDIO_FORMAT_PCM_FLOAT:
1913 switch (mixerOutFormat) {
1914 case AUDIO_FORMAT_PCM_FLOAT:
1915 memcpy(out, in, sampleCount * sizeof(float)); // MEMCPY. TODO optimize out
1916 break;
1917 case AUDIO_FORMAT_PCM_16_BIT:
1918 memcpy_to_i16_from_float((int16_t*)out, (float*)in, sampleCount);
1919 break;
1920 default:
1921 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat);
1922 break;
1923 }
1924 break;
1925 case AUDIO_FORMAT_PCM_16_BIT:
1926 switch (mixerOutFormat) {
1927 case AUDIO_FORMAT_PCM_FLOAT:
Andy Hung5effdf62017-11-27 13:51:40 -08001928 memcpy_to_float_from_q4_27((float*)out, (const int32_t*)in, sampleCount);
Andy Hung296b7412014-06-17 15:25:47 -07001929 break;
1930 case AUDIO_FORMAT_PCM_16_BIT:
Andy Hung5effdf62017-11-27 13:51:40 -08001931 memcpy_to_i16_from_q4_27((int16_t*)out, (const int32_t*)in, sampleCount);
Andy Hung296b7412014-06-17 15:25:47 -07001932 break;
1933 default:
1934 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat);
1935 break;
1936 }
1937 break;
1938 default:
1939 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
1940 break;
1941 }
1942}
1943
1944/* Returns the proper track hook to use for mixing the track into the output buffer.
1945 */
Andy Hung8ed196a2018-01-05 13:21:11 -08001946/* static */
1947AudioMixer::hook_t AudioMixer::Track::getTrackHook(int trackType, uint32_t channelCount,
Andy Hung296b7412014-06-17 15:25:47 -07001948 audio_format_t mixerInFormat, audio_format_t mixerOutFormat __unused)
1949{
Andy Hunge93b6b72014-07-17 21:30:53 -07001950 if (!kUseNewMixer && channelCount == FCC_2 && mixerInFormat == AUDIO_FORMAT_PCM_16_BIT) {
Andy Hung296b7412014-06-17 15:25:47 -07001951 switch (trackType) {
1952 case TRACKTYPE_NOP:
Andy Hung8ed196a2018-01-05 13:21:11 -08001953 return &Track::track__nop;
Andy Hung296b7412014-06-17 15:25:47 -07001954 case TRACKTYPE_RESAMPLE:
Andy Hung8ed196a2018-01-05 13:21:11 -08001955 return &Track::track__genericResample;
Andy Hung296b7412014-06-17 15:25:47 -07001956 case TRACKTYPE_NORESAMPLEMONO:
Andy Hung8ed196a2018-01-05 13:21:11 -08001957 return &Track::track__16BitsMono;
Andy Hung296b7412014-06-17 15:25:47 -07001958 case TRACKTYPE_NORESAMPLE:
Andy Hung8ed196a2018-01-05 13:21:11 -08001959 return &Track::track__16BitsStereo;
Andy Hung296b7412014-06-17 15:25:47 -07001960 default:
1961 LOG_ALWAYS_FATAL("bad trackType: %d", trackType);
1962 break;
1963 }
1964 }
Andy Hunge93b6b72014-07-17 21:30:53 -07001965 LOG_ALWAYS_FATAL_IF(channelCount > MAX_NUM_CHANNELS);
Andy Hung296b7412014-06-17 15:25:47 -07001966 switch (trackType) {
1967 case TRACKTYPE_NOP:
Andy Hung8ed196a2018-01-05 13:21:11 -08001968 return &Track::track__nop;
Andy Hung296b7412014-06-17 15:25:47 -07001969 case TRACKTYPE_RESAMPLE:
1970 switch (mixerInFormat) {
1971 case AUDIO_FORMAT_PCM_FLOAT:
Andy Hung8ed196a2018-01-05 13:21:11 -08001972 return (AudioMixer::hook_t) &Track::track__Resample<
Andy Hung116a4982017-11-30 10:15:08 -08001973 MIXTYPE_MULTI, float /*TO*/, float /*TI*/, TYPE_AUX>;
Andy Hung296b7412014-06-17 15:25:47 -07001974 case AUDIO_FORMAT_PCM_16_BIT:
Andy Hung8ed196a2018-01-05 13:21:11 -08001975 return (AudioMixer::hook_t) &Track::track__Resample<
Andy Hung116a4982017-11-30 10:15:08 -08001976 MIXTYPE_MULTI, int32_t /*TO*/, int16_t /*TI*/, TYPE_AUX>;
Andy Hung296b7412014-06-17 15:25:47 -07001977 default:
1978 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
1979 break;
1980 }
1981 break;
1982 case TRACKTYPE_NORESAMPLEMONO:
1983 switch (mixerInFormat) {
1984 case AUDIO_FORMAT_PCM_FLOAT:
Andy Hung8ed196a2018-01-05 13:21:11 -08001985 return (AudioMixer::hook_t) &Track::track__NoResample<
Andy Hung116a4982017-11-30 10:15:08 -08001986 MIXTYPE_MONOEXPAND, float /*TO*/, float /*TI*/, TYPE_AUX>;
Andy Hung296b7412014-06-17 15:25:47 -07001987 case AUDIO_FORMAT_PCM_16_BIT:
Andy Hung8ed196a2018-01-05 13:21:11 -08001988 return (AudioMixer::hook_t) &Track::track__NoResample<
Andy Hung116a4982017-11-30 10:15:08 -08001989 MIXTYPE_MONOEXPAND, int32_t /*TO*/, int16_t /*TI*/, TYPE_AUX>;
Andy Hung296b7412014-06-17 15:25:47 -07001990 default:
1991 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
1992 break;
1993 }
1994 break;
1995 case TRACKTYPE_NORESAMPLE:
1996 switch (mixerInFormat) {
1997 case AUDIO_FORMAT_PCM_FLOAT:
Andy Hung8ed196a2018-01-05 13:21:11 -08001998 return (AudioMixer::hook_t) &Track::track__NoResample<
Andy Hung116a4982017-11-30 10:15:08 -08001999 MIXTYPE_MULTI, float /*TO*/, float /*TI*/, TYPE_AUX>;
Andy Hung296b7412014-06-17 15:25:47 -07002000 case AUDIO_FORMAT_PCM_16_BIT:
Andy Hung8ed196a2018-01-05 13:21:11 -08002001 return (AudioMixer::hook_t) &Track::track__NoResample<
Andy Hung116a4982017-11-30 10:15:08 -08002002 MIXTYPE_MULTI, int32_t /*TO*/, int16_t /*TI*/, TYPE_AUX>;
Andy Hung296b7412014-06-17 15:25:47 -07002003 default:
2004 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
2005 break;
2006 }
2007 break;
2008 default:
2009 LOG_ALWAYS_FATAL("bad trackType: %d", trackType);
2010 break;
2011 }
2012 return NULL;
2013}
2014
2015/* Returns the proper process hook for mixing tracks. Currently works only for
2016 * PROCESSTYPE_NORESAMPLEONETRACK, a mix involving one track, no resampling.
Andy Hung395db4b2014-08-25 17:15:29 -07002017 *
2018 * TODO: Due to the special mixing considerations of duplicating to
2019 * a stereo output track, the input track cannot be MONO. This should be
2020 * prevented by the caller.
Andy Hung296b7412014-06-17 15:25:47 -07002021 */
Andy Hung8ed196a2018-01-05 13:21:11 -08002022/* static */
2023AudioMixer::process_hook_t AudioMixer::getProcessHook(
2024 int processType, uint32_t channelCount,
Andy Hung296b7412014-06-17 15:25:47 -07002025 audio_format_t mixerInFormat, audio_format_t mixerOutFormat)
2026{
2027 if (processType != PROCESSTYPE_NORESAMPLEONETRACK) { // Only NORESAMPLEONETRACK
2028 LOG_ALWAYS_FATAL("bad processType: %d", processType);
2029 return NULL;
2030 }
Andy Hunge93b6b72014-07-17 21:30:53 -07002031 if (!kUseNewMixer && channelCount == FCC_2 && mixerInFormat == AUDIO_FORMAT_PCM_16_BIT) {
Andy Hung8ed196a2018-01-05 13:21:11 -08002032 return &AudioMixer::process__oneTrack16BitsStereoNoResampling;
Andy Hung296b7412014-06-17 15:25:47 -07002033 }
Andy Hunge93b6b72014-07-17 21:30:53 -07002034 LOG_ALWAYS_FATAL_IF(channelCount > MAX_NUM_CHANNELS);
Andy Hung296b7412014-06-17 15:25:47 -07002035 switch (mixerInFormat) {
2036 case AUDIO_FORMAT_PCM_FLOAT:
2037 switch (mixerOutFormat) {
2038 case AUDIO_FORMAT_PCM_FLOAT:
Andy Hung8ed196a2018-01-05 13:21:11 -08002039 return &AudioMixer::process__noResampleOneTrack<
Andy Hung116a4982017-11-30 10:15:08 -08002040 MIXTYPE_MULTI_SAVEONLY, float /*TO*/, float /*TI*/, TYPE_AUX>;
Andy Hung296b7412014-06-17 15:25:47 -07002041 case AUDIO_FORMAT_PCM_16_BIT:
Andy Hung8ed196a2018-01-05 13:21:11 -08002042 return &AudioMixer::process__noResampleOneTrack<
Andy Hung116a4982017-11-30 10:15:08 -08002043 MIXTYPE_MULTI_SAVEONLY, int16_t /*TO*/, float /*TI*/, TYPE_AUX>;
Andy Hung296b7412014-06-17 15:25:47 -07002044 default:
2045 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat);
2046 break;
2047 }
2048 break;
2049 case AUDIO_FORMAT_PCM_16_BIT:
2050 switch (mixerOutFormat) {
2051 case AUDIO_FORMAT_PCM_FLOAT:
Andy Hung8ed196a2018-01-05 13:21:11 -08002052 return &AudioMixer::process__noResampleOneTrack<
Andy Hung116a4982017-11-30 10:15:08 -08002053 MIXTYPE_MULTI_SAVEONLY, float /*TO*/, int16_t /*TI*/, TYPE_AUX>;
Andy Hung296b7412014-06-17 15:25:47 -07002054 case AUDIO_FORMAT_PCM_16_BIT:
Andy Hung8ed196a2018-01-05 13:21:11 -08002055 return &AudioMixer::process__noResampleOneTrack<
Andy Hung116a4982017-11-30 10:15:08 -08002056 MIXTYPE_MULTI_SAVEONLY, int16_t /*TO*/, int16_t /*TI*/, TYPE_AUX>;
Andy Hung296b7412014-06-17 15:25:47 -07002057 default:
2058 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat);
2059 break;
2060 }
2061 break;
2062 default:
2063 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
2064 break;
2065 }
2066 return NULL;
2067}
2068
Mathias Agopian65ab4712010-07-14 17:59:35 -07002069// ----------------------------------------------------------------------------
Glenn Kasten63238ef2015-03-02 15:50:29 -08002070} // namespace android