blob: 247f72854ea850c5e9bca1cede521063a64a390f [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
Glenn Kasten153b9fe2013-07-15 11:23:36 -070021#include "Configuration.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070022#include <stdint.h>
23#include <string.h>
24#include <stdlib.h>
Andy Hung5e58b0a2014-06-23 19:07:29 -070025#include <math.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070026#include <sys/types.h>
27
28#include <utils/Errors.h>
29#include <utils/Log.h>
30
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -070031#include <cutils/bitops.h>
Glenn Kastenf6b16782011-12-15 09:51:17 -080032#include <cutils/compiler.h>
Glenn Kasten5798d4e2012-03-08 12:18:35 -080033#include <utils/Debug.h>
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -070034
35#include <system/audio.h>
36
Glenn Kasten3b21c502011-12-15 09:52:39 -080037#include <audio_utils/primitives.h>
Andy Hungef7c7fb2014-05-12 16:51:41 -070038#include <audio_utils/format.h>
John Grossman4ff14ba2012-02-08 16:37:41 -080039#include <common_time/local_clock.h>
40#include <common_time/cc_helper.h>
Glenn Kasten3b21c502011-12-15 09:52:39 -080041
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -070042#include <media/EffectsFactoryApi.h>
Andy Hung9a592762014-07-21 21:56:01 -070043#include <audio_effects/effect_downmix.h>
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -070044
Andy Hung296b7412014-06-17 15:25:47 -070045#include "AudioMixerOps.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070046#include "AudioMixer.h"
47
Andy Hunge93b6b72014-07-17 21:30:53 -070048// The FCC_2 macro refers to the Fixed Channel Count of 2 for the legacy integer mixer.
Andy Hung296b7412014-06-17 15:25:47 -070049#ifndef FCC_2
50#define FCC_2 2
51#endif
52
Andy Hunge93b6b72014-07-17 21:30:53 -070053// Look for MONO_HACK for any Mono hack involving legacy mono channel to
54// stereo channel conversion.
55
Andy Hung296b7412014-06-17 15:25:47 -070056/* VERY_VERY_VERBOSE_LOGGING will show exactly which process hook and track hook is
57 * being used. This is a considerable amount of log spam, so don't enable unless you
58 * are verifying the hook based code.
59 */
60//#define VERY_VERY_VERBOSE_LOGGING
61#ifdef VERY_VERY_VERBOSE_LOGGING
62#define ALOGVV ALOGV
63//define ALOGVV printf // for test-mixer.cpp
64#else
65#define ALOGVV(a...) do { } while (0)
66#endif
67
Andy Hunga08810b2014-07-16 21:53:43 -070068#ifndef ARRAY_SIZE
69#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
70#endif
71
Andy Hung5b8fde72014-09-02 21:14:34 -070072// Set kUseNewMixer to true to use the new mixer engine always. Otherwise the
73// original code will be used for stereo sinks, the new mixer for multichannel.
74static const bool kUseNewMixer = true;
Andy Hung296b7412014-06-17 15:25:47 -070075
76// Set kUseFloat to true to allow floating input into the mixer engine.
77// If kUseNewMixer is false, this is ignored or may be overridden internally
78// because of downmix/upmix support.
79static const bool kUseFloat = true;
80
Andy Hung1b2fdcb2014-07-16 17:44:34 -070081// Set to default copy buffer size in frames for input processing.
82static const size_t kCopyBufferFrameCount = 256;
83
Mathias Agopian65ab4712010-07-14 17:59:35 -070084namespace android {
Mathias Agopian65ab4712010-07-14 17:59:35 -070085
86// ----------------------------------------------------------------------------
Andy Hung1b2fdcb2014-07-16 17:44:34 -070087
88template <typename T>
89T min(const T& a, const T& b)
90{
91 return a < b ? a : b;
92}
93
94AudioMixer::CopyBufferProvider::CopyBufferProvider(size_t inputFrameSize,
95 size_t outputFrameSize, size_t bufferFrameCount) :
96 mInputFrameSize(inputFrameSize),
97 mOutputFrameSize(outputFrameSize),
98 mLocalBufferFrameCount(bufferFrameCount),
99 mLocalBufferData(NULL),
100 mConsumed(0)
101{
102 ALOGV("CopyBufferProvider(%p)(%zu, %zu, %zu)", this,
103 inputFrameSize, outputFrameSize, bufferFrameCount);
104 LOG_ALWAYS_FATAL_IF(inputFrameSize < outputFrameSize && bufferFrameCount == 0,
Andy Hunge93b6b72014-07-17 21:30:53 -0700105 "Requires local buffer if inputFrameSize(%zu) < outputFrameSize(%zu)",
Andy Hung1b2fdcb2014-07-16 17:44:34 -0700106 inputFrameSize, outputFrameSize);
107 if (mLocalBufferFrameCount) {
108 (void)posix_memalign(&mLocalBufferData, 32, mLocalBufferFrameCount * mOutputFrameSize);
109 }
110 mBuffer.frameCount = 0;
111}
112
113AudioMixer::CopyBufferProvider::~CopyBufferProvider()
114{
115 ALOGV("~CopyBufferProvider(%p)", this);
116 if (mBuffer.frameCount != 0) {
117 mTrackBufferProvider->releaseBuffer(&mBuffer);
118 }
119 free(mLocalBufferData);
120}
121
122status_t AudioMixer::CopyBufferProvider::getNextBuffer(AudioBufferProvider::Buffer *pBuffer,
123 int64_t pts)
124{
125 //ALOGV("CopyBufferProvider(%p)::getNextBuffer(%p (%zu), %lld)",
126 // this, pBuffer, pBuffer->frameCount, pts);
127 if (mLocalBufferFrameCount == 0) {
128 status_t res = mTrackBufferProvider->getNextBuffer(pBuffer, pts);
129 if (res == OK) {
130 copyFrames(pBuffer->raw, pBuffer->raw, pBuffer->frameCount);
131 }
132 return res;
133 }
134 if (mBuffer.frameCount == 0) {
135 mBuffer.frameCount = pBuffer->frameCount;
136 status_t res = mTrackBufferProvider->getNextBuffer(&mBuffer, pts);
137 // At one time an upstream buffer provider had
138 // res == OK and mBuffer.frameCount == 0, doesn't seem to happen now 7/18/2014.
139 //
140 // By API spec, if res != OK, then mBuffer.frameCount == 0.
141 // but there may be improper implementations.
142 ALOG_ASSERT(res == OK || mBuffer.frameCount == 0);
143 if (res != OK || mBuffer.frameCount == 0) { // not needed by API spec, but to be safe.
144 pBuffer->raw = NULL;
145 pBuffer->frameCount = 0;
146 return res;
147 }
148 mConsumed = 0;
149 }
150 ALOG_ASSERT(mConsumed < mBuffer.frameCount);
151 size_t count = min(mLocalBufferFrameCount, mBuffer.frameCount - mConsumed);
152 count = min(count, pBuffer->frameCount);
153 pBuffer->raw = mLocalBufferData;
154 pBuffer->frameCount = count;
155 copyFrames(pBuffer->raw, (uint8_t*)mBuffer.raw + mConsumed * mInputFrameSize,
156 pBuffer->frameCount);
157 return OK;
158}
159
160void AudioMixer::CopyBufferProvider::releaseBuffer(AudioBufferProvider::Buffer *pBuffer)
161{
162 //ALOGV("CopyBufferProvider(%p)::releaseBuffer(%p(%zu))",
163 // this, pBuffer, pBuffer->frameCount);
164 if (mLocalBufferFrameCount == 0) {
165 mTrackBufferProvider->releaseBuffer(pBuffer);
166 return;
167 }
168 // LOG_ALWAYS_FATAL_IF(pBuffer->frameCount == 0, "Invalid framecount");
169 mConsumed += pBuffer->frameCount; // TODO: update for efficiency to reuse existing content
170 if (mConsumed != 0 && mConsumed >= mBuffer.frameCount) {
171 mTrackBufferProvider->releaseBuffer(&mBuffer);
172 ALOG_ASSERT(mBuffer.frameCount == 0);
173 }
174 pBuffer->raw = NULL;
175 pBuffer->frameCount = 0;
176}
177
178void AudioMixer::CopyBufferProvider::reset()
179{
180 if (mBuffer.frameCount != 0) {
181 mTrackBufferProvider->releaseBuffer(&mBuffer);
182 }
183 mConsumed = 0;
184}
185
Andy Hung34803d52014-07-16 21:41:35 -0700186AudioMixer::DownmixerBufferProvider::DownmixerBufferProvider(
187 audio_channel_mask_t inputChannelMask,
188 audio_channel_mask_t outputChannelMask, audio_format_t format,
189 uint32_t sampleRate, int32_t sessionId, size_t bufferFrameCount) :
190 CopyBufferProvider(
191 audio_bytes_per_sample(format) * audio_channel_count_from_out_mask(inputChannelMask),
192 audio_bytes_per_sample(format) * audio_channel_count_from_out_mask(outputChannelMask),
193 bufferFrameCount) // set bufferFrameCount to 0 to do in-place
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700194{
Andy Hung34803d52014-07-16 21:41:35 -0700195 ALOGV("DownmixerBufferProvider(%p)(%#x, %#x, %#x %u %d)",
196 this, inputChannelMask, outputChannelMask, format,
197 sampleRate, sessionId);
198 if (!sIsMultichannelCapable
199 || EffectCreate(&sDwnmFxDesc.uuid,
200 sessionId,
201 SESSION_ID_INVALID_AND_IGNORED,
202 &mDownmixHandle) != 0) {
203 ALOGE("DownmixerBufferProvider() error creating downmixer effect");
204 mDownmixHandle = NULL;
205 return;
206 }
207 // channel input configuration will be overridden per-track
208 mDownmixConfig.inputCfg.channels = inputChannelMask; // FIXME: Should be bits
209 mDownmixConfig.outputCfg.channels = outputChannelMask; // FIXME: should be bits
210 mDownmixConfig.inputCfg.format = format;
211 mDownmixConfig.outputCfg.format = format;
212 mDownmixConfig.inputCfg.samplingRate = sampleRate;
213 mDownmixConfig.outputCfg.samplingRate = sampleRate;
214 mDownmixConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
215 mDownmixConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
216 // input and output buffer provider, and frame count will not be used as the downmix effect
217 // process() function is called directly (see DownmixerBufferProvider::getNextBuffer())
218 mDownmixConfig.inputCfg.mask = EFFECT_CONFIG_SMP_RATE | EFFECT_CONFIG_CHANNELS |
219 EFFECT_CONFIG_FORMAT | EFFECT_CONFIG_ACC_MODE;
220 mDownmixConfig.outputCfg.mask = mDownmixConfig.inputCfg.mask;
221
222 int cmdStatus;
223 uint32_t replySize = sizeof(int);
224
225 // Configure downmixer
226 status_t status = (*mDownmixHandle)->command(mDownmixHandle,
227 EFFECT_CMD_SET_CONFIG /*cmdCode*/, sizeof(effect_config_t) /*cmdSize*/,
228 &mDownmixConfig /*pCmdData*/,
229 &replySize, &cmdStatus /*pReplyData*/);
230 if (status != 0 || cmdStatus != 0) {
231 ALOGE("DownmixerBufferProvider() error %d cmdStatus %d while configuring downmixer",
232 status, cmdStatus);
233 EffectRelease(mDownmixHandle);
234 mDownmixHandle = NULL;
235 return;
236 }
237
238 // Enable downmixer
239 replySize = sizeof(int);
240 status = (*mDownmixHandle)->command(mDownmixHandle,
241 EFFECT_CMD_ENABLE /*cmdCode*/, 0 /*cmdSize*/, NULL /*pCmdData*/,
242 &replySize, &cmdStatus /*pReplyData*/);
243 if (status != 0 || cmdStatus != 0) {
244 ALOGE("DownmixerBufferProvider() error %d cmdStatus %d while enabling downmixer",
245 status, cmdStatus);
246 EffectRelease(mDownmixHandle);
247 mDownmixHandle = NULL;
248 return;
249 }
250
251 // Set downmix type
252 // parameter size rounded for padding on 32bit boundary
253 const int psizePadded = ((sizeof(downmix_params_t) - 1)/sizeof(int) + 1) * sizeof(int);
254 const int downmixParamSize =
255 sizeof(effect_param_t) + psizePadded + sizeof(downmix_type_t);
256 effect_param_t * const param = (effect_param_t *) malloc(downmixParamSize);
257 param->psize = sizeof(downmix_params_t);
258 const downmix_params_t downmixParam = DOWNMIX_PARAM_TYPE;
259 memcpy(param->data, &downmixParam, param->psize);
260 const downmix_type_t downmixType = DOWNMIX_TYPE_FOLD;
261 param->vsize = sizeof(downmix_type_t);
262 memcpy(param->data + psizePadded, &downmixType, param->vsize);
263 replySize = sizeof(int);
264 status = (*mDownmixHandle)->command(mDownmixHandle,
265 EFFECT_CMD_SET_PARAM /* cmdCode */, downmixParamSize /* cmdSize */,
266 param /*pCmdData*/, &replySize, &cmdStatus /*pReplyData*/);
267 free(param);
268 if (status != 0 || cmdStatus != 0) {
269 ALOGE("DownmixerBufferProvider() error %d cmdStatus %d while setting downmix type",
270 status, cmdStatus);
271 EffectRelease(mDownmixHandle);
272 mDownmixHandle = NULL;
273 return;
274 }
275 ALOGV("DownmixerBufferProvider() downmix type set to %d", (int) downmixType);
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700276}
277
278AudioMixer::DownmixerBufferProvider::~DownmixerBufferProvider()
279{
Andy Hung34803d52014-07-16 21:41:35 -0700280 ALOGV("~DownmixerBufferProvider (%p)", this);
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700281 EffectRelease(mDownmixHandle);
Andy Hung34803d52014-07-16 21:41:35 -0700282 mDownmixHandle = NULL;
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700283}
284
Andy Hung34803d52014-07-16 21:41:35 -0700285void AudioMixer::DownmixerBufferProvider::copyFrames(void *dst, const void *src, size_t frames)
286{
287 mDownmixConfig.inputCfg.buffer.frameCount = frames;
288 mDownmixConfig.inputCfg.buffer.raw = const_cast<void *>(src);
289 mDownmixConfig.outputCfg.buffer.frameCount = frames;
290 mDownmixConfig.outputCfg.buffer.raw = dst;
291 // may be in-place if src == dst.
292 status_t res = (*mDownmixHandle)->process(mDownmixHandle,
293 &mDownmixConfig.inputCfg.buffer, &mDownmixConfig.outputCfg.buffer);
294 ALOGE_IF(res != OK, "DownmixBufferProvider error %d", res);
295}
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700296
Andy Hung34803d52014-07-16 21:41:35 -0700297/* call once in a pthread_once handler. */
298/*static*/ status_t AudioMixer::DownmixerBufferProvider::init()
299{
300 // find multichannel downmix effect if we have to play multichannel content
301 uint32_t numEffects = 0;
302 int ret = EffectQueryNumberEffects(&numEffects);
303 if (ret != 0) {
304 ALOGE("AudioMixer() error %d querying number of effects", ret);
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700305 return NO_INIT;
306 }
Andy Hung34803d52014-07-16 21:41:35 -0700307 ALOGV("EffectQueryNumberEffects() numEffects=%d", numEffects);
308
309 for (uint32_t i = 0 ; i < numEffects ; i++) {
310 if (EffectQueryEffect(i, &sDwnmFxDesc) == 0) {
311 ALOGV("effect %d is called %s", i, sDwnmFxDesc.name);
312 if (memcmp(&sDwnmFxDesc.type, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0) {
313 ALOGI("found effect \"%s\" from %s",
314 sDwnmFxDesc.name, sDwnmFxDesc.implementor);
315 sIsMultichannelCapable = true;
316 break;
317 }
318 }
319 }
320 ALOGW_IF(!sIsMultichannelCapable, "unable to find downmix effect");
321 return NO_INIT;
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700322}
323
Andy Hung34803d52014-07-16 21:41:35 -0700324/*static*/ bool AudioMixer::DownmixerBufferProvider::sIsMultichannelCapable = false;
325/*static*/ effect_descriptor_t AudioMixer::DownmixerBufferProvider::sDwnmFxDesc;
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700326
Andy Hunga08810b2014-07-16 21:53:43 -0700327AudioMixer::RemixBufferProvider::RemixBufferProvider(audio_channel_mask_t inputChannelMask,
328 audio_channel_mask_t outputChannelMask, audio_format_t format,
329 size_t bufferFrameCount) :
330 CopyBufferProvider(
331 audio_bytes_per_sample(format)
332 * audio_channel_count_from_out_mask(inputChannelMask),
333 audio_bytes_per_sample(format)
334 * audio_channel_count_from_out_mask(outputChannelMask),
335 bufferFrameCount),
336 mFormat(format),
337 mSampleSize(audio_bytes_per_sample(format)),
338 mInputChannels(audio_channel_count_from_out_mask(inputChannelMask)),
339 mOutputChannels(audio_channel_count_from_out_mask(outputChannelMask))
340{
Andy Hunge93b6b72014-07-17 21:30:53 -0700341 ALOGV("RemixBufferProvider(%p)(%#x, %#x, %#x) %zu %zu",
Andy Hunga08810b2014-07-16 21:53:43 -0700342 this, format, inputChannelMask, outputChannelMask,
343 mInputChannels, mOutputChannels);
344 // TODO: consider channel representation in index array formulation
345 // We ignore channel representation, and just use the bits.
346 memcpy_by_index_array_initialization(mIdxAry, ARRAY_SIZE(mIdxAry),
347 audio_channel_mask_get_bits(outputChannelMask),
348 audio_channel_mask_get_bits(inputChannelMask));
349}
350
351void AudioMixer::RemixBufferProvider::copyFrames(void *dst, const void *src, size_t frames)
352{
353 memcpy_by_index_array(dst, mOutputChannels,
354 src, mInputChannels, mIdxAry, mSampleSize, frames);
355}
356
Andy Hungef7c7fb2014-05-12 16:51:41 -0700357AudioMixer::ReformatBufferProvider::ReformatBufferProvider(int32_t channels,
Andy Hung1b2fdcb2014-07-16 17:44:34 -0700358 audio_format_t inputFormat, audio_format_t outputFormat,
359 size_t bufferFrameCount) :
360 CopyBufferProvider(
361 channels * audio_bytes_per_sample(inputFormat),
362 channels * audio_bytes_per_sample(outputFormat),
363 bufferFrameCount),
Andy Hungef7c7fb2014-05-12 16:51:41 -0700364 mChannels(channels),
365 mInputFormat(inputFormat),
Andy Hung1b2fdcb2014-07-16 17:44:34 -0700366 mOutputFormat(outputFormat)
Andy Hungef7c7fb2014-05-12 16:51:41 -0700367{
368 ALOGV("ReformatBufferProvider(%p)(%d, %#x, %#x)", this, channels, inputFormat, outputFormat);
Andy Hungef7c7fb2014-05-12 16:51:41 -0700369}
370
Andy Hung1b2fdcb2014-07-16 17:44:34 -0700371void AudioMixer::ReformatBufferProvider::copyFrames(void *dst, const void *src, size_t frames)
Andy Hungef7c7fb2014-05-12 16:51:41 -0700372{
Andy Hung1b2fdcb2014-07-16 17:44:34 -0700373 memcpy_by_audio_format(dst, mOutputFormat, src, mInputFormat, frames * mChannels);
Andy Hungef7c7fb2014-05-12 16:51:41 -0700374}
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700375
376// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -0700377
Paul Lind3c0a0e82012-08-01 18:49:49 -0700378// Ensure mConfiguredNames bitmask is initialized properly on all architectures.
379// The value of 1 << x is undefined in C when x >= 32.
380
Glenn Kasten5c94b6c2012-03-20 17:01:29 -0700381AudioMixer::AudioMixer(size_t frameCount, uint32_t sampleRate, uint32_t maxNumTracks)
Paul Lind3c0a0e82012-08-01 18:49:49 -0700382 : mTrackNames(0), mConfiguredNames((maxNumTracks >= 32 ? 0 : 1 << maxNumTracks) - 1),
Glenn Kasten7f5d3352013-02-15 23:55:04 +0000383 mSampleRate(sampleRate)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700384{
Glenn Kasten5c94b6c2012-03-20 17:01:29 -0700385 ALOG_ASSERT(maxNumTracks <= MAX_NUM_TRACKS, "maxNumTracks %u > MAX_NUM_TRACKS %u",
386 maxNumTracks, MAX_NUM_TRACKS);
387
Glenn Kasten599fabc2012-03-08 12:33:37 -0800388 // AudioMixer is not yet capable of more than 32 active track inputs
389 ALOG_ASSERT(32 >= MAX_NUM_TRACKS, "bad MAX_NUM_TRACKS %d", MAX_NUM_TRACKS);
390
Glenn Kasten52008f82012-03-18 09:34:41 -0700391 pthread_once(&sOnceControl, &sInitRoutine);
392
Mathias Agopian65ab4712010-07-14 17:59:35 -0700393 mState.enabledTracks= 0;
394 mState.needsChanged = 0;
395 mState.frameCount = frameCount;
Glenn Kasten84afa3b2012-01-25 15:28:08 -0800396 mState.hook = process__nop;
Glenn Kastene0feee32011-12-13 11:53:26 -0800397 mState.outputTemp = NULL;
398 mState.resampleTemp = NULL;
Glenn Kastenab7d72f2013-02-27 09:05:28 -0800399 mState.mLog = &mDummyLog;
Glenn Kasten84afa3b2012-01-25 15:28:08 -0800400 // mState.reserved
Glenn Kasten17a736c2012-02-14 08:52:15 -0800401
402 // FIXME Most of the following initialization is probably redundant since
403 // tracks[i] should only be referenced if (mTrackNames & (1 << i)) != 0
404 // and mTrackNames is initially 0. However, leave it here until that's verified.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700405 track_t* t = mState.tracks;
Glenn Kastenbf71f1e2011-12-13 11:52:35 -0800406 for (unsigned i=0 ; i < MAX_NUM_TRACKS ; i++) {
Eric Laurenta5e82142012-04-16 13:47:17 -0700407 t->resampler = NULL;
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700408 t->downmixerBufferProvider = NULL;
Andy Hung1b2fdcb2014-07-16 17:44:34 -0700409 t->mReformatBufferProvider = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700410 t++;
411 }
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700412
Mathias Agopian65ab4712010-07-14 17:59:35 -0700413}
414
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800415AudioMixer::~AudioMixer()
416{
417 track_t* t = mState.tracks;
Glenn Kastenbf71f1e2011-12-13 11:52:35 -0800418 for (unsigned i=0 ; i < MAX_NUM_TRACKS ; i++) {
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800419 delete t->resampler;
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700420 delete t->downmixerBufferProvider;
Andy Hung1b2fdcb2014-07-16 17:44:34 -0700421 delete t->mReformatBufferProvider;
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800422 t++;
423 }
424 delete [] mState.outputTemp;
425 delete [] mState.resampleTemp;
426}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700427
Glenn Kastenab7d72f2013-02-27 09:05:28 -0800428void AudioMixer::setLog(NBLog::Writer *log)
429{
430 mState.mLog = log;
431}
432
Andy Hung7f475492014-08-25 16:36:37 -0700433static inline audio_format_t selectMixerInFormat(audio_format_t inputFormat __unused) {
434 return kUseFloat && kUseNewMixer ? AUDIO_FORMAT_PCM_FLOAT : AUDIO_FORMAT_PCM_16_BIT;
435}
436
Andy Hunge8a1ced2014-05-09 15:02:21 -0700437int AudioMixer::getTrackName(audio_channel_mask_t channelMask,
438 audio_format_t format, int sessionId)
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800439{
Andy Hunge8a1ced2014-05-09 15:02:21 -0700440 if (!isValidPcmTrackFormat(format)) {
441 ALOGE("AudioMixer::getTrackName invalid format (%#x)", format);
442 return -1;
443 }
Glenn Kasten5c94b6c2012-03-20 17:01:29 -0700444 uint32_t names = (~mTrackNames) & mConfiguredNames;
Glenn Kasten98dd5422011-12-15 14:38:29 -0800445 if (names != 0) {
446 int n = __builtin_ctz(names);
Steve Block3856b092011-10-20 11:56:00 +0100447 ALOGV("add track (%d)", n);
Glenn Kastendeeb1282012-03-25 11:59:31 -0700448 // assume default parameters for the track, except where noted below
449 track_t* t = &mState.tracks[n];
450 t->needs = 0;
Andy Hung5e58b0a2014-06-23 19:07:29 -0700451
452 // Integer volume.
453 // Currently integer volume is kept for the legacy integer mixer.
454 // Will be removed when the legacy mixer path is removed.
Andy Hung97ae8242014-05-30 10:35:47 -0700455 t->volume[0] = UNITY_GAIN_INT;
456 t->volume[1] = UNITY_GAIN_INT;
Andy Hung5e58b0a2014-06-23 19:07:29 -0700457 t->prevVolume[0] = UNITY_GAIN_INT << 16;
458 t->prevVolume[1] = UNITY_GAIN_INT << 16;
Glenn Kastendeeb1282012-03-25 11:59:31 -0700459 t->volumeInc[0] = 0;
460 t->volumeInc[1] = 0;
461 t->auxLevel = 0;
462 t->auxInc = 0;
Andy Hung5e58b0a2014-06-23 19:07:29 -0700463 t->prevAuxLevel = 0;
464
465 // Floating point volume.
466 t->mVolume[0] = UNITY_GAIN_FLOAT;
467 t->mVolume[1] = UNITY_GAIN_FLOAT;
468 t->mPrevVolume[0] = UNITY_GAIN_FLOAT;
469 t->mPrevVolume[1] = UNITY_GAIN_FLOAT;
470 t->mVolumeInc[0] = 0.;
471 t->mVolumeInc[1] = 0.;
472 t->mAuxLevel = 0.;
473 t->mAuxInc = 0.;
474 t->mPrevAuxLevel = 0.;
475
Glenn Kastendeeb1282012-03-25 11:59:31 -0700476 // no initialization needed
Glenn Kastendeeb1282012-03-25 11:59:31 -0700477 // t->frameCount
Andy Hung68112fc2014-05-14 14:13:23 -0700478 t->channelCount = audio_channel_count_from_out_mask(channelMask);
Glenn Kastendeeb1282012-03-25 11:59:31 -0700479 t->enabled = false;
Andy Hunge93b6b72014-07-17 21:30:53 -0700480 ALOGV_IF(audio_channel_mask_get_bits(channelMask) != AUDIO_CHANNEL_OUT_STEREO,
Andy Hungef7c7fb2014-05-12 16:51:41 -0700481 "Non-stereo channel mask: %d\n", channelMask);
Andy Hung68112fc2014-05-14 14:13:23 -0700482 t->channelMask = channelMask;
Jean-Michel Trivid06e1322012-09-12 15:47:07 -0700483 t->sessionId = sessionId;
Glenn Kastendeeb1282012-03-25 11:59:31 -0700484 // setBufferProvider(name, AudioBufferProvider *) is required before enable(name)
485 t->bufferProvider = NULL;
486 t->buffer.raw = NULL;
487 // no initialization needed
488 // t->buffer.frameCount
489 t->hook = NULL;
490 t->in = NULL;
491 t->resampler = NULL;
492 t->sampleRate = mSampleRate;
493 // setParameter(name, TRACK, MAIN_BUFFER, mixBuffer) is required before enable(name)
494 t->mainBuffer = NULL;
495 t->auxBuffer = NULL;
Andy Hungef7c7fb2014-05-12 16:51:41 -0700496 t->mInputBufferProvider = NULL;
497 t->mReformatBufferProvider = NULL;
Glenn Kasten52008f82012-03-18 09:34:41 -0700498 t->downmixerBufferProvider = NULL;
Andy Hung7f475492014-08-25 16:36:37 -0700499 t->mPostDownmixReformatBufferProvider = NULL;
Andy Hung78820702014-02-28 16:23:02 -0800500 t->mMixerFormat = AUDIO_FORMAT_PCM_16_BIT;
Andy Hunge8a1ced2014-05-09 15:02:21 -0700501 t->mFormat = format;
Andy Hung7f475492014-08-25 16:36:37 -0700502 t->mMixerInFormat = selectMixerInFormat(format);
503 t->mDownmixRequiresFormat = AUDIO_FORMAT_INVALID; // no format required
Andy Hunge93b6b72014-07-17 21:30:53 -0700504 t->mMixerChannelMask = audio_channel_mask_from_representation_and_bits(
505 AUDIO_CHANNEL_REPRESENTATION_POSITION, AUDIO_CHANNEL_OUT_STEREO);
506 t->mMixerChannelCount = audio_channel_count_from_out_mask(t->mMixerChannelMask);
Andy Hung296b7412014-06-17 15:25:47 -0700507 // Check the downmixing (or upmixing) requirements.
Andy Hung0f451e92014-08-04 21:28:47 -0700508 status_t status = t->prepareForDownmix();
Andy Hung68112fc2014-05-14 14:13:23 -0700509 if (status != OK) {
510 ALOGE("AudioMixer::getTrackName invalid channelMask (%#x)", channelMask);
511 return -1;
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700512 }
Andy Hung7f475492014-08-25 16:36:37 -0700513 // prepareForDownmix() may change mDownmixRequiresFormat
Andy Hung296b7412014-06-17 15:25:47 -0700514 ALOGVV("mMixerFormat:%#x mMixerInFormat:%#x\n", t->mMixerFormat, t->mMixerInFormat);
Andy Hung0f451e92014-08-04 21:28:47 -0700515 t->prepareForReformat();
Andy Hung68112fc2014-05-14 14:13:23 -0700516 mTrackNames |= 1 << n;
517 return TRACK0 + n;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700518 }
Andy Hung68112fc2014-05-14 14:13:23 -0700519 ALOGE("AudioMixer::getTrackName out of available tracks");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700520 return -1;
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800521}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700522
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800523void AudioMixer::invalidateState(uint32_t mask)
524{
Glenn Kasten34fca342013-08-13 09:48:14 -0700525 if (mask != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700526 mState.needsChanged |= mask;
527 mState.hook = process__validate;
528 }
529 }
530
Andy Hunge93b6b72014-07-17 21:30:53 -0700531// Called when channel masks have changed for a track name
Andy Hung7f475492014-08-25 16:36:37 -0700532// TODO: Fix DownmixerBufferProvider not to (possibly) change mixer input format,
Andy Hunge93b6b72014-07-17 21:30:53 -0700533// which will simplify this logic.
534bool AudioMixer::setChannelMasks(int name,
535 audio_channel_mask_t trackChannelMask, audio_channel_mask_t mixerChannelMask) {
536 track_t &track = mState.tracks[name];
537
538 if (trackChannelMask == track.channelMask
539 && mixerChannelMask == track.mMixerChannelMask) {
540 return false; // no need to change
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700541 }
Andy Hunge93b6b72014-07-17 21:30:53 -0700542 // always recompute for both channel masks even if only one has changed.
543 const uint32_t trackChannelCount = audio_channel_count_from_out_mask(trackChannelMask);
544 const uint32_t mixerChannelCount = audio_channel_count_from_out_mask(mixerChannelMask);
545 const bool mixerChannelCountChanged = track.mMixerChannelCount != mixerChannelCount;
546
547 ALOG_ASSERT((trackChannelCount <= MAX_NUM_CHANNELS_TO_DOWNMIX)
548 && trackChannelCount
549 && mixerChannelCount);
550 track.channelMask = trackChannelMask;
551 track.channelCount = trackChannelCount;
552 track.mMixerChannelMask = mixerChannelMask;
553 track.mMixerChannelCount = mixerChannelCount;
554
555 // channel masks have changed, does this track need a downmixer?
556 // update to try using our desired format (if we aren't already using it)
Andy Hung7f475492014-08-25 16:36:37 -0700557 const audio_format_t prevDownmixerFormat = track.mDownmixRequiresFormat;
Andy Hung0f451e92014-08-04 21:28:47 -0700558 const status_t status = mState.tracks[name].prepareForDownmix();
Andy Hunge93b6b72014-07-17 21:30:53 -0700559 ALOGE_IF(status != OK,
Andy Hung0f451e92014-08-04 21:28:47 -0700560 "prepareForDownmix error %d, track channel mask %#x, mixer channel mask %#x",
Andy Hunge93b6b72014-07-17 21:30:53 -0700561 status, track.channelMask, track.mMixerChannelMask);
562
Andy Hung7f475492014-08-25 16:36:37 -0700563 if (prevDownmixerFormat != track.mDownmixRequiresFormat) {
Andy Hung0f451e92014-08-04 21:28:47 -0700564 track.prepareForReformat(); // because of downmixer, track format may change!
Andy Hunge93b6b72014-07-17 21:30:53 -0700565 }
566
Andy Hung7f475492014-08-25 16:36:37 -0700567 if (track.resampler && mixerChannelCountChanged) {
568 // resampler channels may have changed.
Andy Hunge93b6b72014-07-17 21:30:53 -0700569 const uint32_t resetToSampleRate = track.sampleRate;
570 delete track.resampler;
571 track.resampler = NULL;
572 track.sampleRate = mSampleRate; // without resampler, track rate is device sample rate.
573 // recreate the resampler with updated format, channels, saved sampleRate.
574 track.setResampler(resetToSampleRate /*trackSampleRate*/, mSampleRate /*devSampleRate*/);
575 }
576 return true;
577}
578
Andy Hung0f451e92014-08-04 21:28:47 -0700579void AudioMixer::track_t::unprepareForDownmix() {
580 ALOGV("AudioMixer::unprepareForDownmix(%p)", this);
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700581
Andy Hung7f475492014-08-25 16:36:37 -0700582 mDownmixRequiresFormat = AUDIO_FORMAT_INVALID;
Andy Hung0f451e92014-08-04 21:28:47 -0700583 if (downmixerBufferProvider != NULL) {
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700584 // this track had previously been configured with a downmixer, delete it
585 ALOGV(" deleting old downmixer");
Andy Hung0f451e92014-08-04 21:28:47 -0700586 delete downmixerBufferProvider;
587 downmixerBufferProvider = NULL;
588 reconfigureBufferProviders();
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700589 } else {
590 ALOGV(" nothing to do, no downmixer to delete");
591 }
592}
593
Andy Hung0f451e92014-08-04 21:28:47 -0700594status_t AudioMixer::track_t::prepareForDownmix()
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700595{
Andy Hung0f451e92014-08-04 21:28:47 -0700596 ALOGV("AudioMixer::prepareForDownmix(%p) with mask 0x%x",
597 this, channelMask);
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700598
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700599 // discard the previous downmixer if there was one
Andy Hung0f451e92014-08-04 21:28:47 -0700600 unprepareForDownmix();
601 // Only remix (upmix or downmix) if the track and mixer/device channel masks
602 // are not the same and not handled internally, as mono -> stereo currently is.
603 if (channelMask == mMixerChannelMask
604 || (channelMask == AUDIO_CHANNEL_OUT_MONO
605 && mMixerChannelMask == AUDIO_CHANNEL_OUT_STEREO)) {
606 return NO_ERROR;
607 }
Andy Hung34803d52014-07-16 21:41:35 -0700608 if (DownmixerBufferProvider::isMultichannelCapable()) {
Andy Hung0f451e92014-08-04 21:28:47 -0700609 DownmixerBufferProvider* pDbp = new DownmixerBufferProvider(channelMask,
610 mMixerChannelMask,
611 AUDIO_FORMAT_PCM_16_BIT /* TODO: use mMixerInFormat, now only PCM 16 */,
612 sampleRate, sessionId, kCopyBufferFrameCount);
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700613
Andy Hung34803d52014-07-16 21:41:35 -0700614 if (pDbp->isValid()) { // if constructor completed properly
Andy Hung7f475492014-08-25 16:36:37 -0700615 mDownmixRequiresFormat = AUDIO_FORMAT_PCM_16_BIT; // PCM 16 bit required for downmix
Andy Hung0f451e92014-08-04 21:28:47 -0700616 downmixerBufferProvider = pDbp;
617 reconfigureBufferProviders();
Andy Hung34803d52014-07-16 21:41:35 -0700618 return NO_ERROR;
619 }
620 delete pDbp;
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700621 }
Andy Hunge93b6b72014-07-17 21:30:53 -0700622
623 // Effect downmixer does not accept the channel conversion. Let's use our remixer.
Andy Hung0f451e92014-08-04 21:28:47 -0700624 RemixBufferProvider* pRbp = new RemixBufferProvider(channelMask,
625 mMixerChannelMask, mMixerInFormat, kCopyBufferFrameCount);
Andy Hunge93b6b72014-07-17 21:30:53 -0700626 // Remix always finds a conversion whereas Downmixer effect above may fail.
Andy Hung0f451e92014-08-04 21:28:47 -0700627 downmixerBufferProvider = pRbp;
628 reconfigureBufferProviders();
Andy Hunge93b6b72014-07-17 21:30:53 -0700629 return NO_ERROR;
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700630}
631
Andy Hung0f451e92014-08-04 21:28:47 -0700632void AudioMixer::track_t::unprepareForReformat() {
633 ALOGV("AudioMixer::unprepareForReformat(%p)", this);
Andy Hung7f475492014-08-25 16:36:37 -0700634 bool requiresReconfigure = false;
Andy Hung0f451e92014-08-04 21:28:47 -0700635 if (mReformatBufferProvider != NULL) {
636 delete mReformatBufferProvider;
637 mReformatBufferProvider = NULL;
Andy Hung7f475492014-08-25 16:36:37 -0700638 requiresReconfigure = true;
639 }
640 if (mPostDownmixReformatBufferProvider != NULL) {
641 delete mPostDownmixReformatBufferProvider;
642 mPostDownmixReformatBufferProvider = NULL;
643 requiresReconfigure = true;
644 }
645 if (requiresReconfigure) {
Andy Hung0f451e92014-08-04 21:28:47 -0700646 reconfigureBufferProviders();
Andy Hungef7c7fb2014-05-12 16:51:41 -0700647 }
648}
649
Andy Hung0f451e92014-08-04 21:28:47 -0700650status_t AudioMixer::track_t::prepareForReformat()
Andy Hungef7c7fb2014-05-12 16:51:41 -0700651{
Andy Hung0f451e92014-08-04 21:28:47 -0700652 ALOGV("AudioMixer::prepareForReformat(%p) with format %#x", this, mFormat);
Andy Hung7f475492014-08-25 16:36:37 -0700653 // discard previous reformatters
Andy Hung0f451e92014-08-04 21:28:47 -0700654 unprepareForReformat();
Andy Hung7f475492014-08-25 16:36:37 -0700655 // only configure reformatters as needed
656 const audio_format_t targetFormat = mDownmixRequiresFormat != AUDIO_FORMAT_INVALID
657 ? mDownmixRequiresFormat : mMixerInFormat;
658 bool requiresReconfigure = false;
659 if (mFormat != targetFormat) {
Andy Hung0f451e92014-08-04 21:28:47 -0700660 mReformatBufferProvider = new ReformatBufferProvider(
661 audio_channel_count_from_out_mask(channelMask),
Andy Hung7f475492014-08-25 16:36:37 -0700662 mFormat,
663 targetFormat,
Andy Hung1b2fdcb2014-07-16 17:44:34 -0700664 kCopyBufferFrameCount);
Andy Hung7f475492014-08-25 16:36:37 -0700665 requiresReconfigure = true;
666 }
667 if (targetFormat != mMixerInFormat) {
668 mPostDownmixReformatBufferProvider = new ReformatBufferProvider(
669 audio_channel_count_from_out_mask(mMixerChannelMask),
670 targetFormat,
671 mMixerInFormat,
672 kCopyBufferFrameCount);
673 requiresReconfigure = true;
674 }
675 if (requiresReconfigure) {
Andy Hung0f451e92014-08-04 21:28:47 -0700676 reconfigureBufferProviders();
Andy Hung296b7412014-06-17 15:25:47 -0700677 }
678 return NO_ERROR;
Andy Hungef7c7fb2014-05-12 16:51:41 -0700679}
680
Andy Hung0f451e92014-08-04 21:28:47 -0700681void AudioMixer::track_t::reconfigureBufferProviders()
Andy Hungef7c7fb2014-05-12 16:51:41 -0700682{
Andy Hung0f451e92014-08-04 21:28:47 -0700683 bufferProvider = mInputBufferProvider;
684 if (mReformatBufferProvider) {
685 mReformatBufferProvider->setBufferProvider(bufferProvider);
686 bufferProvider = mReformatBufferProvider;
Andy Hungef7c7fb2014-05-12 16:51:41 -0700687 }
Andy Hung0f451e92014-08-04 21:28:47 -0700688 if (downmixerBufferProvider) {
689 downmixerBufferProvider->setBufferProvider(bufferProvider);
690 bufferProvider = downmixerBufferProvider;
Andy Hungef7c7fb2014-05-12 16:51:41 -0700691 }
Andy Hung7f475492014-08-25 16:36:37 -0700692 if (mPostDownmixReformatBufferProvider) {
693 mPostDownmixReformatBufferProvider->setBufferProvider(bufferProvider);
694 bufferProvider = mPostDownmixReformatBufferProvider;
695 }
Andy Hungef7c7fb2014-05-12 16:51:41 -0700696}
697
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800698void AudioMixer::deleteTrackName(int name)
699{
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700700 ALOGV("AudioMixer::deleteTrackName(%d)", name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700701 name -= TRACK0;
Glenn Kasten5798d4e2012-03-08 12:18:35 -0800702 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
Glenn Kasten237a6242011-12-15 15:32:27 -0800703 ALOGV("deleteTrackName(%d)", name);
704 track_t& track(mState.tracks[ name ]);
Glenn Kasten4c340c62012-01-27 12:33:54 -0800705 if (track.enabled) {
706 track.enabled = false;
Glenn Kasten237a6242011-12-15 15:32:27 -0800707 invalidateState(1<<name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700708 }
Glenn Kasten4e2293f2012-04-12 09:39:07 -0700709 // delete the resampler
710 delete track.resampler;
711 track.resampler = NULL;
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700712 // delete the downmixer
Andy Hung0f451e92014-08-04 21:28:47 -0700713 mState.tracks[name].unprepareForDownmix();
Andy Hungef7c7fb2014-05-12 16:51:41 -0700714 // delete the reformatter
Andy Hung0f451e92014-08-04 21:28:47 -0700715 mState.tracks[name].unprepareForReformat();
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700716
Glenn Kasten237a6242011-12-15 15:32:27 -0800717 mTrackNames &= ~(1<<name);
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800718}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700719
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800720void AudioMixer::enable(int name)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700721{
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800722 name -= TRACK0;
Glenn Kasten5798d4e2012-03-08 12:18:35 -0800723 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800724 track_t& track = mState.tracks[name];
725
Glenn Kasten4c340c62012-01-27 12:33:54 -0800726 if (!track.enabled) {
727 track.enabled = true;
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800728 ALOGV("enable(%d)", name);
729 invalidateState(1 << name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700730 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700731}
732
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800733void AudioMixer::disable(int name)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700734{
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800735 name -= TRACK0;
Glenn Kasten5798d4e2012-03-08 12:18:35 -0800736 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800737 track_t& track = mState.tracks[name];
738
Glenn Kasten4c340c62012-01-27 12:33:54 -0800739 if (track.enabled) {
740 track.enabled = false;
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800741 ALOGV("disable(%d)", name);
742 invalidateState(1 << name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700743 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700744}
745
Andy Hung5866a3b2014-05-29 21:33:13 -0700746/* Sets the volume ramp variables for the AudioMixer.
747 *
Andy Hung5e58b0a2014-06-23 19:07:29 -0700748 * The volume ramp variables are used to transition from the previous
749 * volume to the set volume. ramp controls the duration of the transition.
750 * Its value is typically one state framecount period, but may also be 0,
751 * meaning "immediate."
Andy Hung5866a3b2014-05-29 21:33:13 -0700752 *
Andy Hung5e58b0a2014-06-23 19:07:29 -0700753 * FIXME: 1) Volume ramp is enabled only if there is a nonzero integer increment
754 * even if there is a nonzero floating point increment (in that case, the volume
755 * change is immediate). This restriction should be changed when the legacy mixer
756 * is removed (see #2).
757 * FIXME: 2) Integer volume variables are used for Legacy mixing and should be removed
758 * when no longer needed.
759 *
760 * @param newVolume set volume target in floating point [0.0, 1.0].
761 * @param ramp number of frames to increment over. if ramp is 0, the volume
762 * should be set immediately. Currently ramp should not exceed 65535 (frames).
763 * @param pIntSetVolume pointer to the U4.12 integer target volume, set on return.
764 * @param pIntPrevVolume pointer to the U4.28 integer previous volume, set on return.
765 * @param pIntVolumeInc pointer to the U4.28 increment per output audio frame, set on return.
766 * @param pSetVolume pointer to the float target volume, set on return.
767 * @param pPrevVolume pointer to the float previous volume, set on return.
768 * @param pVolumeInc pointer to the float increment per output audio frame, set on return.
Andy Hung5866a3b2014-05-29 21:33:13 -0700769 * @return true if the volume has changed, false if volume is same.
770 */
Andy Hung5e58b0a2014-06-23 19:07:29 -0700771static inline bool setVolumeRampVariables(float newVolume, int32_t ramp,
772 int16_t *pIntSetVolume, int32_t *pIntPrevVolume, int32_t *pIntVolumeInc,
773 float *pSetVolume, float *pPrevVolume, float *pVolumeInc) {
774 if (newVolume == *pSetVolume) {
Andy Hung5866a3b2014-05-29 21:33:13 -0700775 return false;
776 }
Andy Hung5e58b0a2014-06-23 19:07:29 -0700777 /* set the floating point volume variables */
Andy Hung5866a3b2014-05-29 21:33:13 -0700778 if (ramp != 0) {
Andy Hung5e58b0a2014-06-23 19:07:29 -0700779 *pVolumeInc = (newVolume - *pSetVolume) / ramp;
780 *pPrevVolume = *pSetVolume;
Andy Hung5866a3b2014-05-29 21:33:13 -0700781 } else {
Andy Hung5e58b0a2014-06-23 19:07:29 -0700782 *pVolumeInc = 0;
783 *pPrevVolume = newVolume;
Andy Hung5866a3b2014-05-29 21:33:13 -0700784 }
Andy Hung5e58b0a2014-06-23 19:07:29 -0700785 *pSetVolume = newVolume;
786
787 /* set the legacy integer volume variables */
788 int32_t intVolume = newVolume * AudioMixer::UNITY_GAIN_INT;
789 if (intVolume > AudioMixer::UNITY_GAIN_INT) {
790 intVolume = AudioMixer::UNITY_GAIN_INT;
791 } else if (intVolume < 0) {
792 ALOGE("negative volume %.7g", newVolume);
793 intVolume = 0; // should never happen, but for safety check.
794 }
795 if (intVolume == *pIntSetVolume) {
796 *pIntVolumeInc = 0;
797 /* TODO: integer/float workaround: ignore floating volume ramp */
798 *pVolumeInc = 0;
799 *pPrevVolume = newVolume;
800 return true;
801 }
802 if (ramp != 0) {
803 *pIntVolumeInc = ((intVolume - *pIntSetVolume) << 16) / ramp;
804 *pIntPrevVolume = (*pIntVolumeInc == 0 ? intVolume : *pIntSetVolume) << 16;
805 } else {
806 *pIntVolumeInc = 0;
807 *pIntPrevVolume = intVolume << 16;
808 }
809 *pIntSetVolume = intVolume;
Andy Hung5866a3b2014-05-29 21:33:13 -0700810 return true;
811}
812
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800813void AudioMixer::setParameter(int name, int target, int param, void *value)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700814{
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800815 name -= TRACK0;
Glenn Kasten5798d4e2012-03-08 12:18:35 -0800816 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800817 track_t& track = mState.tracks[name];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700818
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000819 int valueInt = static_cast<int>(reinterpret_cast<uintptr_t>(value));
820 int32_t *valueBuf = reinterpret_cast<int32_t*>(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700821
822 switch (target) {
Glenn Kasten788040c2011-05-05 08:19:00 -0700823
Mathias Agopian65ab4712010-07-14 17:59:35 -0700824 case TRACK:
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800825 switch (param) {
Glenn Kasten788040c2011-05-05 08:19:00 -0700826 case CHANNEL_MASK: {
Andy Hunge93b6b72014-07-17 21:30:53 -0700827 const audio_channel_mask_t trackChannelMask =
828 static_cast<audio_channel_mask_t>(valueInt);
829 if (setChannelMasks(name, trackChannelMask, track.mMixerChannelMask)) {
830 ALOGV("setParameter(TRACK, CHANNEL_MASK, %x)", trackChannelMask);
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800831 invalidateState(1 << name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700832 }
Glenn Kasten788040c2011-05-05 08:19:00 -0700833 } break;
834 case MAIN_BUFFER:
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800835 if (track.mainBuffer != valueBuf) {
836 track.mainBuffer = valueBuf;
Steve Block3856b092011-10-20 11:56:00 +0100837 ALOGV("setParameter(TRACK, MAIN_BUFFER, %p)", valueBuf);
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800838 invalidateState(1 << name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700839 }
Glenn Kasten788040c2011-05-05 08:19:00 -0700840 break;
841 case AUX_BUFFER:
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800842 if (track.auxBuffer != valueBuf) {
843 track.auxBuffer = valueBuf;
Steve Block3856b092011-10-20 11:56:00 +0100844 ALOGV("setParameter(TRACK, AUX_BUFFER, %p)", valueBuf);
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800845 invalidateState(1 << name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700846 }
Glenn Kasten788040c2011-05-05 08:19:00 -0700847 break;
Andy Hungef7c7fb2014-05-12 16:51:41 -0700848 case FORMAT: {
849 audio_format_t format = static_cast<audio_format_t>(valueInt);
850 if (track.mFormat != format) {
851 ALOG_ASSERT(audio_is_linear_pcm(format), "Invalid format %#x", format);
852 track.mFormat = format;
853 ALOGV("setParameter(TRACK, FORMAT, %#x)", format);
Andy Hung0f451e92014-08-04 21:28:47 -0700854 track.prepareForReformat();
Andy Hungef7c7fb2014-05-12 16:51:41 -0700855 invalidateState(1 << name);
856 }
857 } break;
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700858 // FIXME do we want to support setting the downmix type from AudioFlinger?
859 // for a specific track? or per mixer?
860 /* case DOWNMIX_TYPE:
861 break */
Andy Hung78820702014-02-28 16:23:02 -0800862 case MIXER_FORMAT: {
Andy Hunga1ab7cc2014-02-24 19:26:52 -0800863 audio_format_t format = static_cast<audio_format_t>(valueInt);
Andy Hung78820702014-02-28 16:23:02 -0800864 if (track.mMixerFormat != format) {
865 track.mMixerFormat = format;
866 ALOGV("setParameter(TRACK, MIXER_FORMAT, %#x)", format);
Andy Hunga1ab7cc2014-02-24 19:26:52 -0800867 }
868 } break;
Andy Hunge93b6b72014-07-17 21:30:53 -0700869 case MIXER_CHANNEL_MASK: {
870 const audio_channel_mask_t mixerChannelMask =
871 static_cast<audio_channel_mask_t>(valueInt);
872 if (setChannelMasks(name, track.channelMask, mixerChannelMask)) {
873 ALOGV("setParameter(TRACK, MIXER_CHANNEL_MASK, %#x)", mixerChannelMask);
874 invalidateState(1 << name);
875 }
876 } break;
Glenn Kasten788040c2011-05-05 08:19:00 -0700877 default:
Glenn Kastenadad3d72014-02-21 14:51:43 -0800878 LOG_ALWAYS_FATAL("setParameter track: bad param %d", param);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700879 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700880 break;
Glenn Kasten788040c2011-05-05 08:19:00 -0700881
Mathias Agopian65ab4712010-07-14 17:59:35 -0700882 case RESAMPLE:
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800883 switch (param) {
884 case SAMPLE_RATE:
Glenn Kasten5798d4e2012-03-08 12:18:35 -0800885 ALOG_ASSERT(valueInt > 0, "bad sample rate %d", valueInt);
Glenn Kasten788040c2011-05-05 08:19:00 -0700886 if (track.setResampler(uint32_t(valueInt), mSampleRate)) {
887 ALOGV("setParameter(RESAMPLE, SAMPLE_RATE, %u)",
888 uint32_t(valueInt));
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800889 invalidateState(1 << name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700890 }
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800891 break;
892 case RESET:
Eric Laurent243f5f92011-02-28 16:52:51 -0800893 track.resetResampler();
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800894 invalidateState(1 << name);
895 break;
Glenn Kasten4e2293f2012-04-12 09:39:07 -0700896 case REMOVE:
897 delete track.resampler;
898 track.resampler = NULL;
899 track.sampleRate = mSampleRate;
900 invalidateState(1 << name);
901 break;
Glenn Kasten788040c2011-05-05 08:19:00 -0700902 default:
Glenn Kastenadad3d72014-02-21 14:51:43 -0800903 LOG_ALWAYS_FATAL("setParameter resample: bad param %d", param);
Eric Laurent243f5f92011-02-28 16:52:51 -0800904 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700905 break;
Glenn Kasten788040c2011-05-05 08:19:00 -0700906
Mathias Agopian65ab4712010-07-14 17:59:35 -0700907 case RAMP_VOLUME:
908 case VOLUME:
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800909 switch (param) {
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800910 case AUXLEVEL:
Andy Hung6be49402014-05-30 10:42:03 -0700911 if (setVolumeRampVariables(*reinterpret_cast<float*>(value),
Andy Hung5866a3b2014-05-29 21:33:13 -0700912 target == RAMP_VOLUME ? mState.frameCount : 0,
Andy Hung5e58b0a2014-06-23 19:07:29 -0700913 &track.auxLevel, &track.prevAuxLevel, &track.auxInc,
914 &track.mAuxLevel, &track.mPrevAuxLevel, &track.mAuxInc)) {
Andy Hung5866a3b2014-05-29 21:33:13 -0700915 ALOGV("setParameter(%s, AUXLEVEL: %04x)",
Andy Hung6be49402014-05-30 10:42:03 -0700916 target == VOLUME ? "VOLUME" : "RAMP_VOLUME", track.auxLevel);
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800917 invalidateState(1 << name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700918 }
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800919 break;
Glenn Kasten788040c2011-05-05 08:19:00 -0700920 default:
Andy Hunge93b6b72014-07-17 21:30:53 -0700921 if ((unsigned)param >= VOLUME0 && (unsigned)param < VOLUME0 + MAX_NUM_VOLUMES) {
922 if (setVolumeRampVariables(*reinterpret_cast<float*>(value),
923 target == RAMP_VOLUME ? mState.frameCount : 0,
924 &track.volume[param - VOLUME0], &track.prevVolume[param - VOLUME0],
925 &track.volumeInc[param - VOLUME0],
926 &track.mVolume[param - VOLUME0], &track.mPrevVolume[param - VOLUME0],
927 &track.mVolumeInc[param - VOLUME0])) {
928 ALOGV("setParameter(%s, VOLUME%d: %04x)",
929 target == VOLUME ? "VOLUME" : "RAMP_VOLUME", param - VOLUME0,
930 track.volume[param - VOLUME0]);
931 invalidateState(1 << name);
932 }
933 } else {
934 LOG_ALWAYS_FATAL("setParameter volume: bad param %d", param);
935 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700936 }
937 break;
Glenn Kasten788040c2011-05-05 08:19:00 -0700938
939 default:
Glenn Kastenadad3d72014-02-21 14:51:43 -0800940 LOG_ALWAYS_FATAL("setParameter: bad target %d", target);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700941 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700942}
943
Andy Hunge93b6b72014-07-17 21:30:53 -0700944bool AudioMixer::track_t::setResampler(uint32_t trackSampleRate, uint32_t devSampleRate)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700945{
Andy Hunge93b6b72014-07-17 21:30:53 -0700946 if (trackSampleRate != devSampleRate || resampler != NULL) {
947 if (sampleRate != trackSampleRate) {
948 sampleRate = trackSampleRate;
Glenn Kastene0feee32011-12-13 11:53:26 -0800949 if (resampler == NULL) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700950 ALOGV("Creating resampler from track %d Hz to device %d Hz",
951 trackSampleRate, devSampleRate);
Glenn Kastenac602052012-10-01 14:04:31 -0700952 AudioResampler::src_quality quality;
953 // force lowest quality level resampler if use case isn't music or video
954 // FIXME this is flawed for dynamic sample rates, as we choose the resampler
955 // quality level based on the initial ratio, but that could change later.
956 // Should have a way to distinguish tracks with static ratios vs. dynamic ratios.
Andy Hunge93b6b72014-07-17 21:30:53 -0700957 if (!((trackSampleRate == 44100 && devSampleRate == 48000) ||
958 (trackSampleRate == 48000 && devSampleRate == 44100))) {
Andy Hung9e0308c2014-01-30 14:32:31 -0800959 quality = AudioResampler::DYN_LOW_QUALITY;
Glenn Kastenac602052012-10-01 14:04:31 -0700960 } else {
961 quality = AudioResampler::DEFAULT_QUALITY;
962 }
Andy Hung296b7412014-06-17 15:25:47 -0700963
Andy Hunge93b6b72014-07-17 21:30:53 -0700964 // TODO: Remove MONO_HACK. Resampler sees #channels after the downmixer
965 // but if none exists, it is the channel count (1 for mono).
966 const int resamplerChannelCount = downmixerBufferProvider != NULL
967 ? mMixerChannelCount : channelCount;
Andy Hung9a592762014-07-21 21:56:01 -0700968 ALOGVV("Creating resampler:"
969 " format(%#x) channels(%d) devSampleRate(%u) quality(%d)\n",
970 mMixerInFormat, resamplerChannelCount, devSampleRate, quality);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700971 resampler = AudioResampler::create(
Andy Hung3348e362014-07-07 10:21:44 -0700972 mMixerInFormat,
Andy Hunge93b6b72014-07-17 21:30:53 -0700973 resamplerChannelCount,
Glenn Kastenac602052012-10-01 14:04:31 -0700974 devSampleRate, quality);
Glenn Kasten52008f82012-03-18 09:34:41 -0700975 resampler->setLocalTimeFreq(sLocalTimeFreq);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700976 }
977 return true;
978 }
979 }
980 return false;
981}
982
Andy Hung5e58b0a2014-06-23 19:07:29 -0700983/* Checks to see if the volume ramp has completed and clears the increment
984 * variables appropriately.
985 *
986 * FIXME: There is code to handle int/float ramp variable switchover should it not
987 * complete within a mixer buffer processing call, but it is preferred to avoid switchover
988 * due to precision issues. The switchover code is included for legacy code purposes
989 * and can be removed once the integer volume is removed.
990 *
991 * It is not sufficient to clear only the volumeInc integer variable because
992 * if one channel requires ramping, all channels are ramped.
993 *
994 * There is a bit of duplicated code here, but it keeps backward compatibility.
995 */
996inline void AudioMixer::track_t::adjustVolumeRamp(bool aux, bool useFloat)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700997{
Andy Hung5e58b0a2014-06-23 19:07:29 -0700998 if (useFloat) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700999 for (uint32_t i = 0; i < MAX_NUM_VOLUMES; i++) {
Andy Hung5e58b0a2014-06-23 19:07:29 -07001000 if (mVolumeInc[i] != 0 && fabs(mVolume[i] - mPrevVolume[i]) <= fabs(mVolumeInc[i])) {
1001 volumeInc[i] = 0;
1002 prevVolume[i] = volume[i] << 16;
1003 mVolumeInc[i] = 0.;
1004 mPrevVolume[i] = mVolume[i];
Andy Hung5e58b0a2014-06-23 19:07:29 -07001005 } else {
1006 //ALOGV("ramp: %f %f %f", mVolume[i], mPrevVolume[i], mVolumeInc[i]);
1007 prevVolume[i] = u4_28_from_float(mPrevVolume[i]);
1008 }
1009 }
1010 } else {
Andy Hunge93b6b72014-07-17 21:30:53 -07001011 for (uint32_t i = 0; i < MAX_NUM_VOLUMES; i++) {
Andy Hung5e58b0a2014-06-23 19:07:29 -07001012 if (((volumeInc[i]>0) && (((prevVolume[i]+volumeInc[i])>>16) >= volume[i])) ||
1013 ((volumeInc[i]<0) && (((prevVolume[i]+volumeInc[i])>>16) <= volume[i]))) {
1014 volumeInc[i] = 0;
1015 prevVolume[i] = volume[i] << 16;
1016 mVolumeInc[i] = 0.;
1017 mPrevVolume[i] = mVolume[i];
1018 } else {
1019 //ALOGV("ramp: %d %d %d", volume[i] << 16, prevVolume[i], volumeInc[i]);
1020 mPrevVolume[i] = float_from_u4_28(prevVolume[i]);
1021 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001022 }
1023 }
Andy Hung5e58b0a2014-06-23 19:07:29 -07001024 /* TODO: aux is always integer regardless of output buffer type */
Mathias Agopian65ab4712010-07-14 17:59:35 -07001025 if (aux) {
1026 if (((auxInc>0) && (((prevAuxLevel+auxInc)>>16) >= auxLevel)) ||
Andy Hung5e58b0a2014-06-23 19:07:29 -07001027 ((auxInc<0) && (((prevAuxLevel+auxInc)>>16) <= auxLevel))) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001028 auxInc = 0;
Andy Hung5e58b0a2014-06-23 19:07:29 -07001029 prevAuxLevel = auxLevel << 16;
1030 mAuxInc = 0.;
1031 mPrevAuxLevel = mAuxLevel;
1032 } else {
1033 //ALOGV("aux ramp: %d %d %d", auxLevel << 16, prevAuxLevel, auxInc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001034 }
1035 }
1036}
1037
Glenn Kastenc59c0042012-02-02 14:06:11 -08001038size_t AudioMixer::getUnreleasedFrames(int name) const
Eric Laurent071ccd52011-12-22 16:08:41 -08001039{
1040 name -= TRACK0;
1041 if (uint32_t(name) < MAX_NUM_TRACKS) {
Glenn Kastenc59c0042012-02-02 14:06:11 -08001042 return mState.tracks[name].getUnreleasedFrames();
Eric Laurent071ccd52011-12-22 16:08:41 -08001043 }
1044 return 0;
1045}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001046
Glenn Kasten01c4ebf2012-02-22 10:47:35 -08001047void AudioMixer::setBufferProvider(int name, AudioBufferProvider* bufferProvider)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001048{
Glenn Kasten9c56d4a2011-12-19 15:06:39 -08001049 name -= TRACK0;
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001050 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -07001051
Andy Hung1d26ddf2014-05-29 15:53:09 -07001052 if (mState.tracks[name].mInputBufferProvider == bufferProvider) {
1053 return; // don't reset any buffer providers if identical.
1054 }
Andy Hungef7c7fb2014-05-12 16:51:41 -07001055 if (mState.tracks[name].mReformatBufferProvider != NULL) {
1056 mState.tracks[name].mReformatBufferProvider->reset();
1057 } else if (mState.tracks[name].downmixerBufferProvider != NULL) {
Andy Hung7f475492014-08-25 16:36:37 -07001058 mState.tracks[name].downmixerBufferProvider->reset();
1059 } else if (mState.tracks[name].mPostDownmixReformatBufferProvider != NULL) {
1060 mState.tracks[name].mPostDownmixReformatBufferProvider->reset();
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -07001061 }
Andy Hungef7c7fb2014-05-12 16:51:41 -07001062
1063 mState.tracks[name].mInputBufferProvider = bufferProvider;
Andy Hung0f451e92014-08-04 21:28:47 -07001064 mState.tracks[name].reconfigureBufferProviders();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001065}
1066
1067
John Grossman4ff14ba2012-02-08 16:37:41 -08001068void AudioMixer::process(int64_t pts)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001069{
John Grossman4ff14ba2012-02-08 16:37:41 -08001070 mState.hook(&mState, pts);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001071}
1072
1073
John Grossman4ff14ba2012-02-08 16:37:41 -08001074void AudioMixer::process__validate(state_t* state, int64_t pts)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001075{
Steve Block5ff1dd52012-01-05 23:22:43 +00001076 ALOGW_IF(!state->needsChanged,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001077 "in process__validate() but nothing's invalid");
1078
1079 uint32_t changed = state->needsChanged;
1080 state->needsChanged = 0; // clear the validation flag
1081
1082 // recompute which tracks are enabled / disabled
1083 uint32_t enabled = 0;
1084 uint32_t disabled = 0;
1085 while (changed) {
1086 const int i = 31 - __builtin_clz(changed);
1087 const uint32_t mask = 1<<i;
1088 changed &= ~mask;
1089 track_t& t = state->tracks[i];
1090 (t.enabled ? enabled : disabled) |= mask;
1091 }
1092 state->enabledTracks &= ~disabled;
1093 state->enabledTracks |= enabled;
1094
1095 // compute everything we need...
1096 int countActiveTracks = 0;
Andy Hung395db4b2014-08-25 17:15:29 -07001097 // TODO: fix all16BitsStereNoResample logic to
1098 // either properly handle muted tracks (it should ignore them)
1099 // or remove altogether as an obsolete optimization.
Glenn Kasten4c340c62012-01-27 12:33:54 -08001100 bool all16BitsStereoNoResample = true;
1101 bool resampling = false;
1102 bool volumeRamp = false;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001103 uint32_t en = state->enabledTracks;
1104 while (en) {
1105 const int i = 31 - __builtin_clz(en);
1106 en &= ~(1<<i);
1107
1108 countActiveTracks++;
1109 track_t& t = state->tracks[i];
1110 uint32_t n = 0;
Glenn Kastend6fadf02013-10-30 14:37:29 -07001111 // FIXME can overflow (mask is only 3 bits)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001112 n |= NEEDS_CHANNEL_1 + t.channelCount - 1;
Glenn Kastend6fadf02013-10-30 14:37:29 -07001113 if (t.doesResample()) {
1114 n |= NEEDS_RESAMPLE;
1115 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001116 if (t.auxLevel != 0 && t.auxBuffer != NULL) {
Glenn Kastend6fadf02013-10-30 14:37:29 -07001117 n |= NEEDS_AUX;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001118 }
1119
1120 if (t.volumeInc[0]|t.volumeInc[1]) {
Glenn Kasten4c340c62012-01-27 12:33:54 -08001121 volumeRamp = true;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001122 } else if (!t.doesResample() && t.volumeRL == 0) {
Glenn Kastend6fadf02013-10-30 14:37:29 -07001123 n |= NEEDS_MUTE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001124 }
1125 t.needs = n;
1126
Glenn Kastend6fadf02013-10-30 14:37:29 -07001127 if (n & NEEDS_MUTE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001128 t.hook = track__nop;
1129 } else {
Glenn Kastend6fadf02013-10-30 14:37:29 -07001130 if (n & NEEDS_AUX) {
Glenn Kasten4c340c62012-01-27 12:33:54 -08001131 all16BitsStereoNoResample = false;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001132 }
Glenn Kastend6fadf02013-10-30 14:37:29 -07001133 if (n & NEEDS_RESAMPLE) {
Glenn Kasten4c340c62012-01-27 12:33:54 -08001134 all16BitsStereoNoResample = false;
1135 resampling = true;
Andy Hunge93b6b72014-07-17 21:30:53 -07001136 t.hook = getTrackHook(TRACKTYPE_RESAMPLE, t.mMixerChannelCount,
Andy Hung296b7412014-06-17 15:25:47 -07001137 t.mMixerInFormat, t.mMixerFormat);
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -07001138 ALOGV_IF((n & NEEDS_CHANNEL_COUNT__MASK) > NEEDS_CHANNEL_2,
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -07001139 "Track %d needs downmix + resample", i);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001140 } else {
1141 if ((n & NEEDS_CHANNEL_COUNT__MASK) == NEEDS_CHANNEL_1){
Andy Hunge93b6b72014-07-17 21:30:53 -07001142 t.hook = getTrackHook(
1143 t.mMixerChannelCount == 2 // TODO: MONO_HACK.
1144 ? TRACKTYPE_NORESAMPLEMONO : TRACKTYPE_NORESAMPLE,
1145 t.mMixerChannelCount,
Andy Hung296b7412014-06-17 15:25:47 -07001146 t.mMixerInFormat, t.mMixerFormat);
Glenn Kasten4c340c62012-01-27 12:33:54 -08001147 all16BitsStereoNoResample = false;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001148 }
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -07001149 if ((n & NEEDS_CHANNEL_COUNT__MASK) >= NEEDS_CHANNEL_2){
Andy Hunge93b6b72014-07-17 21:30:53 -07001150 t.hook = getTrackHook(TRACKTYPE_NORESAMPLE, t.mMixerChannelCount,
Andy Hung296b7412014-06-17 15:25:47 -07001151 t.mMixerInFormat, t.mMixerFormat);
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -07001152 ALOGV_IF((n & NEEDS_CHANNEL_COUNT__MASK) > NEEDS_CHANNEL_2,
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -07001153 "Track %d needs downmix", i);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001154 }
1155 }
1156 }
1157 }
1158
1159 // select the processing hooks
1160 state->hook = process__nop;
Glenn Kasten34fca342013-08-13 09:48:14 -07001161 if (countActiveTracks > 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001162 if (resampling) {
1163 if (!state->outputTemp) {
1164 state->outputTemp = new int32_t[MAX_NUM_CHANNELS * state->frameCount];
1165 }
1166 if (!state->resampleTemp) {
1167 state->resampleTemp = new int32_t[MAX_NUM_CHANNELS * state->frameCount];
1168 }
1169 state->hook = process__genericResampling;
1170 } else {
1171 if (state->outputTemp) {
1172 delete [] state->outputTemp;
Glenn Kastene0feee32011-12-13 11:53:26 -08001173 state->outputTemp = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001174 }
1175 if (state->resampleTemp) {
1176 delete [] state->resampleTemp;
Glenn Kastene0feee32011-12-13 11:53:26 -08001177 state->resampleTemp = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001178 }
1179 state->hook = process__genericNoResampling;
1180 if (all16BitsStereoNoResample && !volumeRamp) {
1181 if (countActiveTracks == 1) {
Andy Hung296b7412014-06-17 15:25:47 -07001182 const int i = 31 - __builtin_clz(state->enabledTracks);
1183 track_t& t = state->tracks[i];
Andy Hung395db4b2014-08-25 17:15:29 -07001184 if ((t.needs & NEEDS_MUTE) == 0) {
1185 // The check prevents a muted track from acquiring a process hook.
1186 //
1187 // This is dangerous if the track is MONO as that requires
1188 // special case handling due to implicit channel duplication.
1189 // Stereo or Multichannel should actually be fine here.
1190 state->hook = getProcessHook(PROCESSTYPE_NORESAMPLEONETRACK,
1191 t.mMixerChannelCount, t.mMixerInFormat, t.mMixerFormat);
1192 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001193 }
1194 }
1195 }
1196 }
1197
Steve Block3856b092011-10-20 11:56:00 +01001198 ALOGV("mixer configuration change: %d activeTracks (%08x) "
Mathias Agopian65ab4712010-07-14 17:59:35 -07001199 "all16BitsStereoNoResample=%d, resampling=%d, volumeRamp=%d",
1200 countActiveTracks, state->enabledTracks,
1201 all16BitsStereoNoResample, resampling, volumeRamp);
1202
John Grossman4ff14ba2012-02-08 16:37:41 -08001203 state->hook(state, pts);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001204
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -08001205 // Now that the volume ramp has been done, set optimal state and
1206 // track hooks for subsequent mixer process
Glenn Kasten34fca342013-08-13 09:48:14 -07001207 if (countActiveTracks > 0) {
Glenn Kasten4c340c62012-01-27 12:33:54 -08001208 bool allMuted = true;
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -08001209 uint32_t en = state->enabledTracks;
1210 while (en) {
1211 const int i = 31 - __builtin_clz(en);
1212 en &= ~(1<<i);
1213 track_t& t = state->tracks[i];
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001214 if (!t.doesResample() && t.volumeRL == 0) {
Glenn Kastend6fadf02013-10-30 14:37:29 -07001215 t.needs |= NEEDS_MUTE;
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -08001216 t.hook = track__nop;
1217 } else {
Glenn Kasten4c340c62012-01-27 12:33:54 -08001218 allMuted = false;
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -08001219 }
1220 }
1221 if (allMuted) {
1222 state->hook = process__nop;
1223 } else if (all16BitsStereoNoResample) {
1224 if (countActiveTracks == 1) {
Andy Hunge93b6b72014-07-17 21:30:53 -07001225 const int i = 31 - __builtin_clz(state->enabledTracks);
1226 track_t& t = state->tracks[i];
Andy Hung395db4b2014-08-25 17:15:29 -07001227 // Muted single tracks handled by allMuted above.
Andy Hunge93b6b72014-07-17 21:30:53 -07001228 state->hook = getProcessHook(PROCESSTYPE_NORESAMPLEONETRACK,
1229 t.mMixerChannelCount, t.mMixerInFormat, t.mMixerFormat);
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -08001230 }
1231 }
1232 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001233}
1234
Mathias Agopian65ab4712010-07-14 17:59:35 -07001235
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001236void AudioMixer::track__genericResample(track_t* t, int32_t* out, size_t outFrameCount,
1237 int32_t* temp, int32_t* aux)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001238{
Andy Hung296b7412014-06-17 15:25:47 -07001239 ALOGVV("track__genericResample\n");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001240 t->resampler->setSampleRate(t->sampleRate);
1241
1242 // ramp gain - resample to temp buffer and scale/mix in 2nd step
1243 if (aux != NULL) {
1244 // always resample with unity gain when sending to auxiliary buffer to be able
1245 // to apply send level after resampling
Andy Hung5e58b0a2014-06-23 19:07:29 -07001246 t->resampler->setVolume(UNITY_GAIN_FLOAT, UNITY_GAIN_FLOAT);
Andy Hunge93b6b72014-07-17 21:30:53 -07001247 memset(temp, 0, outFrameCount * t->mMixerChannelCount * sizeof(int32_t));
Mathias Agopian65ab4712010-07-14 17:59:35 -07001248 t->resampler->resample(temp, outFrameCount, t->bufferProvider);
Glenn Kastenf6b16782011-12-15 09:51:17 -08001249 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001250 volumeRampStereo(t, out, outFrameCount, temp, aux);
1251 } else {
1252 volumeStereo(t, out, outFrameCount, temp, aux);
1253 }
1254 } else {
Glenn Kastenf6b16782011-12-15 09:51:17 -08001255 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) {
Andy Hung5e58b0a2014-06-23 19:07:29 -07001256 t->resampler->setVolume(UNITY_GAIN_FLOAT, UNITY_GAIN_FLOAT);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001257 memset(temp, 0, outFrameCount * MAX_NUM_CHANNELS * sizeof(int32_t));
1258 t->resampler->resample(temp, outFrameCount, t->bufferProvider);
1259 volumeRampStereo(t, out, outFrameCount, temp, aux);
1260 }
1261
1262 // constant gain
1263 else {
Andy Hung5e58b0a2014-06-23 19:07:29 -07001264 t->resampler->setVolume(t->mVolume[0], t->mVolume[1]);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001265 t->resampler->resample(out, outFrameCount, t->bufferProvider);
1266 }
1267 }
1268}
1269
Andy Hungee931ff2014-01-28 13:44:14 -08001270void AudioMixer::track__nop(track_t* t __unused, int32_t* out __unused,
1271 size_t outFrameCount __unused, int32_t* temp __unused, int32_t* aux __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001272{
1273}
1274
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001275void AudioMixer::volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp,
1276 int32_t* aux)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001277{
1278 int32_t vl = t->prevVolume[0];
1279 int32_t vr = t->prevVolume[1];
1280 const int32_t vlInc = t->volumeInc[0];
1281 const int32_t vrInc = t->volumeInc[1];
1282
Steve Blockb8a80522011-12-20 16:23:08 +00001283 //ALOGD("[0] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
Mathias Agopian65ab4712010-07-14 17:59:35 -07001284 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
1285 // (vl + vlInc*frameCount)/65536.0f, frameCount);
1286
1287 // ramp volume
Glenn Kastenf6b16782011-12-15 09:51:17 -08001288 if (CC_UNLIKELY(aux != NULL)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001289 int32_t va = t->prevAuxLevel;
1290 const int32_t vaInc = t->auxInc;
1291 int32_t l;
1292 int32_t r;
1293
1294 do {
1295 l = (*temp++ >> 12);
1296 r = (*temp++ >> 12);
1297 *out++ += (vl >> 16) * l;
1298 *out++ += (vr >> 16) * r;
1299 *aux++ += (va >> 17) * (l + r);
1300 vl += vlInc;
1301 vr += vrInc;
1302 va += vaInc;
1303 } while (--frameCount);
1304 t->prevAuxLevel = va;
1305 } else {
1306 do {
1307 *out++ += (vl >> 16) * (*temp++ >> 12);
1308 *out++ += (vr >> 16) * (*temp++ >> 12);
1309 vl += vlInc;
1310 vr += vrInc;
1311 } while (--frameCount);
1312 }
1313 t->prevVolume[0] = vl;
1314 t->prevVolume[1] = vr;
Glenn Kastena1117922012-01-26 10:53:32 -08001315 t->adjustVolumeRamp(aux != NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001316}
1317
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001318void AudioMixer::volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp,
1319 int32_t* aux)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001320{
1321 const int16_t vl = t->volume[0];
1322 const int16_t vr = t->volume[1];
1323
Glenn Kastenf6b16782011-12-15 09:51:17 -08001324 if (CC_UNLIKELY(aux != NULL)) {
Glenn Kasten3b81aca2012-01-27 15:26:23 -08001325 const int16_t va = t->auxLevel;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001326 do {
1327 int16_t l = (int16_t)(*temp++ >> 12);
1328 int16_t r = (int16_t)(*temp++ >> 12);
1329 out[0] = mulAdd(l, vl, out[0]);
1330 int16_t a = (int16_t)(((int32_t)l + r) >> 1);
1331 out[1] = mulAdd(r, vr, out[1]);
1332 out += 2;
1333 aux[0] = mulAdd(a, va, aux[0]);
1334 aux++;
1335 } while (--frameCount);
1336 } else {
1337 do {
1338 int16_t l = (int16_t)(*temp++ >> 12);
1339 int16_t r = (int16_t)(*temp++ >> 12);
1340 out[0] = mulAdd(l, vl, out[0]);
1341 out[1] = mulAdd(r, vr, out[1]);
1342 out += 2;
1343 } while (--frameCount);
1344 }
1345}
1346
Andy Hungee931ff2014-01-28 13:44:14 -08001347void AudioMixer::track__16BitsStereo(track_t* t, int32_t* out, size_t frameCount,
1348 int32_t* temp __unused, int32_t* aux)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001349{
Andy Hung296b7412014-06-17 15:25:47 -07001350 ALOGVV("track__16BitsStereo\n");
Glenn Kasten54c3b662012-01-06 07:46:30 -08001351 const int16_t *in = static_cast<const int16_t *>(t->in);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001352
Glenn Kastenf6b16782011-12-15 09:51:17 -08001353 if (CC_UNLIKELY(aux != NULL)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001354 int32_t l;
1355 int32_t r;
1356 // ramp gain
Glenn Kastenf6b16782011-12-15 09:51:17 -08001357 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001358 int32_t vl = t->prevVolume[0];
1359 int32_t vr = t->prevVolume[1];
1360 int32_t va = t->prevAuxLevel;
1361 const int32_t vlInc = t->volumeInc[0];
1362 const int32_t vrInc = t->volumeInc[1];
1363 const int32_t vaInc = t->auxInc;
Steve Blockb8a80522011-12-20 16:23:08 +00001364 // ALOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
Mathias Agopian65ab4712010-07-14 17:59:35 -07001365 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
1366 // (vl + vlInc*frameCount)/65536.0f, frameCount);
1367
1368 do {
1369 l = (int32_t)*in++;
1370 r = (int32_t)*in++;
1371 *out++ += (vl >> 16) * l;
1372 *out++ += (vr >> 16) * r;
1373 *aux++ += (va >> 17) * (l + r);
1374 vl += vlInc;
1375 vr += vrInc;
1376 va += vaInc;
1377 } while (--frameCount);
1378
1379 t->prevVolume[0] = vl;
1380 t->prevVolume[1] = vr;
1381 t->prevAuxLevel = va;
1382 t->adjustVolumeRamp(true);
1383 }
1384
1385 // constant gain
1386 else {
1387 const uint32_t vrl = t->volumeRL;
1388 const int16_t va = (int16_t)t->auxLevel;
1389 do {
Glenn Kasten54c3b662012-01-06 07:46:30 -08001390 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001391 int16_t a = (int16_t)(((int32_t)in[0] + in[1]) >> 1);
1392 in += 2;
1393 out[0] = mulAddRL(1, rl, vrl, out[0]);
1394 out[1] = mulAddRL(0, rl, vrl, out[1]);
1395 out += 2;
1396 aux[0] = mulAdd(a, va, aux[0]);
1397 aux++;
1398 } while (--frameCount);
1399 }
1400 } else {
1401 // ramp gain
Glenn Kastenf6b16782011-12-15 09:51:17 -08001402 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001403 int32_t vl = t->prevVolume[0];
1404 int32_t vr = t->prevVolume[1];
1405 const int32_t vlInc = t->volumeInc[0];
1406 const int32_t vrInc = t->volumeInc[1];
1407
Steve Blockb8a80522011-12-20 16:23:08 +00001408 // ALOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
Mathias Agopian65ab4712010-07-14 17:59:35 -07001409 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
1410 // (vl + vlInc*frameCount)/65536.0f, frameCount);
1411
1412 do {
1413 *out++ += (vl >> 16) * (int32_t) *in++;
1414 *out++ += (vr >> 16) * (int32_t) *in++;
1415 vl += vlInc;
1416 vr += vrInc;
1417 } while (--frameCount);
1418
1419 t->prevVolume[0] = vl;
1420 t->prevVolume[1] = vr;
1421 t->adjustVolumeRamp(false);
1422 }
1423
1424 // constant gain
1425 else {
1426 const uint32_t vrl = t->volumeRL;
1427 do {
Glenn Kasten54c3b662012-01-06 07:46:30 -08001428 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001429 in += 2;
1430 out[0] = mulAddRL(1, rl, vrl, out[0]);
1431 out[1] = mulAddRL(0, rl, vrl, out[1]);
1432 out += 2;
1433 } while (--frameCount);
1434 }
1435 }
1436 t->in = in;
1437}
1438
Andy Hungee931ff2014-01-28 13:44:14 -08001439void AudioMixer::track__16BitsMono(track_t* t, int32_t* out, size_t frameCount,
1440 int32_t* temp __unused, int32_t* aux)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001441{
Andy Hung296b7412014-06-17 15:25:47 -07001442 ALOGVV("track__16BitsMono\n");
Glenn Kasten54c3b662012-01-06 07:46:30 -08001443 const int16_t *in = static_cast<int16_t const *>(t->in);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001444
Glenn Kastenf6b16782011-12-15 09:51:17 -08001445 if (CC_UNLIKELY(aux != NULL)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001446 // ramp gain
Glenn Kastenf6b16782011-12-15 09:51:17 -08001447 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001448 int32_t vl = t->prevVolume[0];
1449 int32_t vr = t->prevVolume[1];
1450 int32_t va = t->prevAuxLevel;
1451 const int32_t vlInc = t->volumeInc[0];
1452 const int32_t vrInc = t->volumeInc[1];
1453 const int32_t vaInc = t->auxInc;
1454
Steve Blockb8a80522011-12-20 16:23:08 +00001455 // ALOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
Mathias Agopian65ab4712010-07-14 17:59:35 -07001456 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
1457 // (vl + vlInc*frameCount)/65536.0f, frameCount);
1458
1459 do {
1460 int32_t l = *in++;
1461 *out++ += (vl >> 16) * l;
1462 *out++ += (vr >> 16) * l;
1463 *aux++ += (va >> 16) * l;
1464 vl += vlInc;
1465 vr += vrInc;
1466 va += vaInc;
1467 } while (--frameCount);
1468
1469 t->prevVolume[0] = vl;
1470 t->prevVolume[1] = vr;
1471 t->prevAuxLevel = va;
1472 t->adjustVolumeRamp(true);
1473 }
1474 // constant gain
1475 else {
1476 const int16_t vl = t->volume[0];
1477 const int16_t vr = t->volume[1];
1478 const int16_t va = (int16_t)t->auxLevel;
1479 do {
1480 int16_t l = *in++;
1481 out[0] = mulAdd(l, vl, out[0]);
1482 out[1] = mulAdd(l, vr, out[1]);
1483 out += 2;
1484 aux[0] = mulAdd(l, va, aux[0]);
1485 aux++;
1486 } while (--frameCount);
1487 }
1488 } else {
1489 // ramp gain
Glenn Kastenf6b16782011-12-15 09:51:17 -08001490 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001491 int32_t vl = t->prevVolume[0];
1492 int32_t vr = t->prevVolume[1];
1493 const int32_t vlInc = t->volumeInc[0];
1494 const int32_t vrInc = t->volumeInc[1];
1495
Steve Blockb8a80522011-12-20 16:23:08 +00001496 // ALOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
Mathias Agopian65ab4712010-07-14 17:59:35 -07001497 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
1498 // (vl + vlInc*frameCount)/65536.0f, frameCount);
1499
1500 do {
1501 int32_t l = *in++;
1502 *out++ += (vl >> 16) * l;
1503 *out++ += (vr >> 16) * l;
1504 vl += vlInc;
1505 vr += vrInc;
1506 } while (--frameCount);
1507
1508 t->prevVolume[0] = vl;
1509 t->prevVolume[1] = vr;
1510 t->adjustVolumeRamp(false);
1511 }
1512 // constant gain
1513 else {
1514 const int16_t vl = t->volume[0];
1515 const int16_t vr = t->volume[1];
1516 do {
1517 int16_t l = *in++;
1518 out[0] = mulAdd(l, vl, out[0]);
1519 out[1] = mulAdd(l, vr, out[1]);
1520 out += 2;
1521 } while (--frameCount);
1522 }
1523 }
1524 t->in = in;
1525}
1526
Mathias Agopian65ab4712010-07-14 17:59:35 -07001527// no-op case
John Grossman4ff14ba2012-02-08 16:37:41 -08001528void AudioMixer::process__nop(state_t* state, int64_t pts)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001529{
Andy Hung296b7412014-06-17 15:25:47 -07001530 ALOGVV("process__nop\n");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001531 uint32_t e0 = state->enabledTracks;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001532 while (e0) {
1533 // process by group of tracks with same output buffer to
1534 // avoid multiple memset() on same buffer
1535 uint32_t e1 = e0, e2 = e0;
1536 int i = 31 - __builtin_clz(e1);
Glenn Kastenfc900c92013-02-18 12:47:49 -08001537 {
1538 track_t& t1 = state->tracks[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001539 e2 &= ~(1<<i);
Glenn Kastenfc900c92013-02-18 12:47:49 -08001540 while (e2) {
1541 i = 31 - __builtin_clz(e2);
1542 e2 &= ~(1<<i);
1543 track_t& t2 = state->tracks[i];
1544 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) {
1545 e1 &= ~(1<<i);
1546 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001547 }
Glenn Kastenfc900c92013-02-18 12:47:49 -08001548 e0 &= ~(e1);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001549
Andy Hunge93b6b72014-07-17 21:30:53 -07001550 memset(t1.mainBuffer, 0, state->frameCount * t1.mMixerChannelCount
Andy Hung78820702014-02-28 16:23:02 -08001551 * audio_bytes_per_sample(t1.mMixerFormat));
Glenn Kastenfc900c92013-02-18 12:47:49 -08001552 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001553
1554 while (e1) {
1555 i = 31 - __builtin_clz(e1);
1556 e1 &= ~(1<<i);
Glenn Kastenfc900c92013-02-18 12:47:49 -08001557 {
1558 track_t& t3 = state->tracks[i];
1559 size_t outFrames = state->frameCount;
1560 while (outFrames) {
1561 t3.buffer.frameCount = outFrames;
1562 int64_t outputPTS = calculateOutputPTS(
1563 t3, pts, state->frameCount - outFrames);
1564 t3.bufferProvider->getNextBuffer(&t3.buffer, outputPTS);
1565 if (t3.buffer.raw == NULL) break;
1566 outFrames -= t3.buffer.frameCount;
1567 t3.bufferProvider->releaseBuffer(&t3.buffer);
1568 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001569 }
1570 }
1571 }
1572}
1573
1574// generic code without resampling
John Grossman4ff14ba2012-02-08 16:37:41 -08001575void AudioMixer::process__genericNoResampling(state_t* state, int64_t pts)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001576{
Andy Hung296b7412014-06-17 15:25:47 -07001577 ALOGVV("process__genericNoResampling\n");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001578 int32_t outTemp[BLOCKSIZE * MAX_NUM_CHANNELS] __attribute__((aligned(32)));
1579
1580 // acquire each track's buffer
1581 uint32_t enabledTracks = state->enabledTracks;
1582 uint32_t e0 = enabledTracks;
1583 while (e0) {
1584 const int i = 31 - __builtin_clz(e0);
1585 e0 &= ~(1<<i);
1586 track_t& t = state->tracks[i];
1587 t.buffer.frameCount = state->frameCount;
John Grossman4ff14ba2012-02-08 16:37:41 -08001588 t.bufferProvider->getNextBuffer(&t.buffer, pts);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001589 t.frameCount = t.buffer.frameCount;
1590 t.in = t.buffer.raw;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001591 }
1592
1593 e0 = enabledTracks;
1594 while (e0) {
1595 // process by group of tracks with same output buffer to
1596 // optimize cache use
1597 uint32_t e1 = e0, e2 = e0;
1598 int j = 31 - __builtin_clz(e1);
1599 track_t& t1 = state->tracks[j];
1600 e2 &= ~(1<<j);
1601 while (e2) {
1602 j = 31 - __builtin_clz(e2);
1603 e2 &= ~(1<<j);
1604 track_t& t2 = state->tracks[j];
Glenn Kastenf6b16782011-12-15 09:51:17 -08001605 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001606 e1 &= ~(1<<j);
1607 }
1608 }
1609 e0 &= ~(e1);
1610 // this assumes output 16 bits stereo, no resampling
1611 int32_t *out = t1.mainBuffer;
1612 size_t numFrames = 0;
1613 do {
1614 memset(outTemp, 0, sizeof(outTemp));
1615 e2 = e1;
1616 while (e2) {
1617 const int i = 31 - __builtin_clz(e2);
1618 e2 &= ~(1<<i);
1619 track_t& t = state->tracks[i];
1620 size_t outFrames = BLOCKSIZE;
1621 int32_t *aux = NULL;
Glenn Kastend6fadf02013-10-30 14:37:29 -07001622 if (CC_UNLIKELY(t.needs & NEEDS_AUX)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001623 aux = t.auxBuffer + numFrames;
1624 }
1625 while (outFrames) {
Gaurav Kumar7e79cd22014-01-06 10:57:18 +05301626 // t.in == NULL can happen if the track was flushed just after having
1627 // been enabled for mixing.
1628 if (t.in == NULL) {
1629 enabledTracks &= ~(1<<i);
1630 e1 &= ~(1<<i);
1631 break;
1632 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001633 size_t inFrames = (t.frameCount > outFrames)?outFrames:t.frameCount;
Glenn Kasten34fca342013-08-13 09:48:14 -07001634 if (inFrames > 0) {
Andy Hunge93b6b72014-07-17 21:30:53 -07001635 t.hook(&t, outTemp + (BLOCKSIZE - outFrames) * t.mMixerChannelCount,
1636 inFrames, state->resampleTemp, aux);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001637 t.frameCount -= inFrames;
1638 outFrames -= inFrames;
Glenn Kastenf6b16782011-12-15 09:51:17 -08001639 if (CC_UNLIKELY(aux != NULL)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001640 aux += inFrames;
1641 }
1642 }
1643 if (t.frameCount == 0 && outFrames) {
1644 t.bufferProvider->releaseBuffer(&t.buffer);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001645 t.buffer.frameCount = (state->frameCount - numFrames) -
1646 (BLOCKSIZE - outFrames);
John Grossman4ff14ba2012-02-08 16:37:41 -08001647 int64_t outputPTS = calculateOutputPTS(
1648 t, pts, numFrames + (BLOCKSIZE - outFrames));
1649 t.bufferProvider->getNextBuffer(&t.buffer, outputPTS);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001650 t.in = t.buffer.raw;
1651 if (t.in == NULL) {
1652 enabledTracks &= ~(1<<i);
1653 e1 &= ~(1<<i);
1654 break;
1655 }
1656 t.frameCount = t.buffer.frameCount;
1657 }
1658 }
1659 }
Andy Hung296b7412014-06-17 15:25:47 -07001660
1661 convertMixerFormat(out, t1.mMixerFormat, outTemp, t1.mMixerInFormat,
Andy Hunge93b6b72014-07-17 21:30:53 -07001662 BLOCKSIZE * t1.mMixerChannelCount);
Andy Hung296b7412014-06-17 15:25:47 -07001663 // TODO: fix ugly casting due to choice of out pointer type
1664 out = reinterpret_cast<int32_t*>((uint8_t*)out
Andy Hunge93b6b72014-07-17 21:30:53 -07001665 + BLOCKSIZE * t1.mMixerChannelCount
1666 * audio_bytes_per_sample(t1.mMixerFormat));
Mathias Agopian65ab4712010-07-14 17:59:35 -07001667 numFrames += BLOCKSIZE;
1668 } while (numFrames < state->frameCount);
1669 }
1670
1671 // release each track's buffer
1672 e0 = enabledTracks;
1673 while (e0) {
1674 const int i = 31 - __builtin_clz(e0);
1675 e0 &= ~(1<<i);
1676 track_t& t = state->tracks[i];
1677 t.bufferProvider->releaseBuffer(&t.buffer);
1678 }
1679}
1680
1681
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -08001682// generic code with resampling
John Grossman4ff14ba2012-02-08 16:37:41 -08001683void AudioMixer::process__genericResampling(state_t* state, int64_t pts)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001684{
Andy Hung296b7412014-06-17 15:25:47 -07001685 ALOGVV("process__genericResampling\n");
Glenn Kasten54c3b662012-01-06 07:46:30 -08001686 // this const just means that local variable outTemp doesn't change
Mathias Agopian65ab4712010-07-14 17:59:35 -07001687 int32_t* const outTemp = state->outputTemp;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001688 size_t numFrames = state->frameCount;
1689
1690 uint32_t e0 = state->enabledTracks;
1691 while (e0) {
1692 // process by group of tracks with same output buffer
1693 // to optimize cache use
1694 uint32_t e1 = e0, e2 = e0;
1695 int j = 31 - __builtin_clz(e1);
1696 track_t& t1 = state->tracks[j];
1697 e2 &= ~(1<<j);
1698 while (e2) {
1699 j = 31 - __builtin_clz(e2);
1700 e2 &= ~(1<<j);
1701 track_t& t2 = state->tracks[j];
Glenn Kastenf6b16782011-12-15 09:51:17 -08001702 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001703 e1 &= ~(1<<j);
1704 }
1705 }
1706 e0 &= ~(e1);
1707 int32_t *out = t1.mainBuffer;
Andy Hunge93b6b72014-07-17 21:30:53 -07001708 memset(outTemp, 0, sizeof(*outTemp) * t1.mMixerChannelCount * state->frameCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001709 while (e1) {
1710 const int i = 31 - __builtin_clz(e1);
1711 e1 &= ~(1<<i);
1712 track_t& t = state->tracks[i];
1713 int32_t *aux = NULL;
Glenn Kastend6fadf02013-10-30 14:37:29 -07001714 if (CC_UNLIKELY(t.needs & NEEDS_AUX)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001715 aux = t.auxBuffer;
1716 }
1717
1718 // this is a little goofy, on the resampling case we don't
1719 // acquire/release the buffers because it's done by
1720 // the resampler.
Glenn Kastend6fadf02013-10-30 14:37:29 -07001721 if (t.needs & NEEDS_RESAMPLE) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001722 t.resampler->setPTS(pts);
Glenn Kastena1117922012-01-26 10:53:32 -08001723 t.hook(&t, outTemp, numFrames, state->resampleTemp, aux);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001724 } else {
1725
1726 size_t outFrames = 0;
1727
1728 while (outFrames < numFrames) {
1729 t.buffer.frameCount = numFrames - outFrames;
John Grossman4ff14ba2012-02-08 16:37:41 -08001730 int64_t outputPTS = calculateOutputPTS(t, pts, outFrames);
1731 t.bufferProvider->getNextBuffer(&t.buffer, outputPTS);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001732 t.in = t.buffer.raw;
1733 // t.in == NULL can happen if the track was flushed just after having
1734 // been enabled for mixing.
1735 if (t.in == NULL) break;
1736
Glenn Kastenf6b16782011-12-15 09:51:17 -08001737 if (CC_UNLIKELY(aux != NULL)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001738 aux += outFrames;
1739 }
Andy Hunge93b6b72014-07-17 21:30:53 -07001740 t.hook(&t, outTemp + outFrames * t.mMixerChannelCount, t.buffer.frameCount,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001741 state->resampleTemp, aux);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001742 outFrames += t.buffer.frameCount;
1743 t.bufferProvider->releaseBuffer(&t.buffer);
1744 }
1745 }
1746 }
Andy Hunge93b6b72014-07-17 21:30:53 -07001747 convertMixerFormat(out, t1.mMixerFormat,
1748 outTemp, t1.mMixerInFormat, numFrames * t1.mMixerChannelCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001749 }
1750}
1751
1752// one track, 16 bits stereo without resampling is the most common case
John Grossman4ff14ba2012-02-08 16:37:41 -08001753void AudioMixer::process__OneTrack16BitsStereoNoResampling(state_t* state,
1754 int64_t pts)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001755{
Andy Hung296b7412014-06-17 15:25:47 -07001756 ALOGVV("process__OneTrack16BitsStereoNoResampling\n");
Glenn Kasten99e53b82012-01-19 08:59:58 -08001757 // This method is only called when state->enabledTracks has exactly
1758 // one bit set. The asserts below would verify this, but are commented out
1759 // since the whole point of this method is to optimize performance.
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001760 //ALOG_ASSERT(0 != state->enabledTracks, "no tracks enabled");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001761 const int i = 31 - __builtin_clz(state->enabledTracks);
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001762 //ALOG_ASSERT((1 << i) == state->enabledTracks, "more than 1 track enabled");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001763 const track_t& t = state->tracks[i];
1764
1765 AudioBufferProvider::Buffer& b(t.buffer);
1766
1767 int32_t* out = t.mainBuffer;
Andy Hungf8a106a2014-05-29 18:52:38 -07001768 float *fout = reinterpret_cast<float*>(out);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001769 size_t numFrames = state->frameCount;
1770
1771 const int16_t vl = t.volume[0];
1772 const int16_t vr = t.volume[1];
1773 const uint32_t vrl = t.volumeRL;
1774 while (numFrames) {
1775 b.frameCount = numFrames;
John Grossman4ff14ba2012-02-08 16:37:41 -08001776 int64_t outputPTS = calculateOutputPTS(t, pts, out - t.mainBuffer);
1777 t.bufferProvider->getNextBuffer(&b, outputPTS);
Glenn Kasten54c3b662012-01-06 07:46:30 -08001778 const int16_t *in = b.i16;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001779
1780 // in == NULL can happen if the track was flushed just after having
1781 // been enabled for mixing.
Andy Hungf8a106a2014-05-29 18:52:38 -07001782 if (in == NULL || (((uintptr_t)in) & 3)) {
1783 memset(out, 0, numFrames
Andy Hunge93b6b72014-07-17 21:30:53 -07001784 * t.mMixerChannelCount * audio_bytes_per_sample(t.mMixerFormat));
Andy Hung395db4b2014-08-25 17:15:29 -07001785 ALOGE_IF((((uintptr_t)in) & 3),
1786 "process__OneTrack16BitsStereoNoResampling: misaligned buffer"
1787 " %p track %d, channels %d, needs %08x, volume %08x vfl %f vfr %f",
1788 in, i, t.channelCount, t.needs, vrl, t.mVolume[0], t.mVolume[1]);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001789 return;
1790 }
1791 size_t outFrames = b.frameCount;
1792
Andy Hung78820702014-02-28 16:23:02 -08001793 switch (t.mMixerFormat) {
Andy Hungf8a106a2014-05-29 18:52:38 -07001794 case AUDIO_FORMAT_PCM_FLOAT:
Mathias Agopian65ab4712010-07-14 17:59:35 -07001795 do {
Glenn Kasten54c3b662012-01-06 07:46:30 -08001796 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001797 in += 2;
Andy Hunga1ab7cc2014-02-24 19:26:52 -08001798 int32_t l = mulRL(1, rl, vrl);
1799 int32_t r = mulRL(0, rl, vrl);
Andy Hung84a0c6e2014-04-02 11:24:53 -07001800 *fout++ = float_from_q4_27(l);
1801 *fout++ = float_from_q4_27(r);
Andy Hung3375bde2014-02-28 15:51:47 -08001802 // Note: In case of later int16_t sink output,
1803 // conversion and clamping is done by memcpy_to_i16_from_float().
Mathias Agopian65ab4712010-07-14 17:59:35 -07001804 } while (--outFrames);
Andy Hungf8a106a2014-05-29 18:52:38 -07001805 break;
Andy Hunga1ab7cc2014-02-24 19:26:52 -08001806 case AUDIO_FORMAT_PCM_16_BIT:
Andy Hung97ae8242014-05-30 10:35:47 -07001807 if (CC_UNLIKELY(uint32_t(vl) > UNITY_GAIN_INT || uint32_t(vr) > UNITY_GAIN_INT)) {
Andy Hunga1ab7cc2014-02-24 19:26:52 -08001808 // volume is boosted, so we might need to clamp even though
1809 // we process only one track.
1810 do {
1811 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
1812 in += 2;
1813 int32_t l = mulRL(1, rl, vrl) >> 12;
1814 int32_t r = mulRL(0, rl, vrl) >> 12;
1815 // clamping...
1816 l = clamp16(l);
1817 r = clamp16(r);
1818 *out++ = (r<<16) | (l & 0xFFFF);
1819 } while (--outFrames);
1820 } else {
1821 do {
1822 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
1823 in += 2;
1824 int32_t l = mulRL(1, rl, vrl) >> 12;
1825 int32_t r = mulRL(0, rl, vrl) >> 12;
1826 *out++ = (r<<16) | (l & 0xFFFF);
1827 } while (--outFrames);
1828 }
1829 break;
1830 default:
Andy Hung78820702014-02-28 16:23:02 -08001831 LOG_ALWAYS_FATAL("bad mixer format: %d", t.mMixerFormat);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001832 }
1833 numFrames -= b.frameCount;
1834 t.bufferProvider->releaseBuffer(&b);
1835 }
1836}
1837
John Grossman4ff14ba2012-02-08 16:37:41 -08001838int64_t AudioMixer::calculateOutputPTS(const track_t& t, int64_t basePTS,
1839 int outputFrameIndex)
1840{
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001841 if (AudioBufferProvider::kInvalidPTS == basePTS) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001842 return AudioBufferProvider::kInvalidPTS;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001843 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001844
Glenn Kasten52008f82012-03-18 09:34:41 -07001845 return basePTS + ((outputFrameIndex * sLocalTimeFreq) / t.sampleRate);
1846}
1847
1848/*static*/ uint64_t AudioMixer::sLocalTimeFreq;
1849/*static*/ pthread_once_t AudioMixer::sOnceControl = PTHREAD_ONCE_INIT;
1850
1851/*static*/ void AudioMixer::sInitRoutine()
1852{
1853 LocalClock lc;
Andy Hung34803d52014-07-16 21:41:35 -07001854 sLocalTimeFreq = lc.getLocalFreq(); // for the resampler
Glenn Kasten49c34ac2013-10-30 14:37:01 -07001855
Andy Hung34803d52014-07-16 21:41:35 -07001856 DownmixerBufferProvider::init(); // for the downmixer
John Grossman4ff14ba2012-02-08 16:37:41 -08001857}
1858
Andy Hunge93b6b72014-07-17 21:30:53 -07001859/* TODO: consider whether this level of optimization is necessary.
1860 * Perhaps just stick with a single for loop.
1861 */
1862
1863// Needs to derive a compile time constant (constexpr). Could be targeted to go
1864// to a MONOVOL mixtype based on MAX_NUM_VOLUMES, but that's an unnecessary complication.
1865#define MIXTYPE_MONOVOL(mixtype) (mixtype == MIXTYPE_MULTI ? MIXTYPE_MULTI_MONOVOL : \
1866 mixtype == MIXTYPE_MULTI_SAVEONLY ? MIXTYPE_MULTI_SAVEONLY_MONOVOL : mixtype)
1867
1868/* MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
1869 * TO: int32_t (Q4.27) or float
1870 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
1871 * TA: int32_t (Q4.27)
1872 */
1873template <int MIXTYPE,
1874 typename TO, typename TI, typename TV, typename TA, typename TAV>
1875static void volumeRampMulti(uint32_t channels, TO* out, size_t frameCount,
1876 const TI* in, TA* aux, TV *vol, const TV *volinc, TAV *vola, TAV volainc)
1877{
1878 switch (channels) {
1879 case 1:
1880 volumeRampMulti<MIXTYPE, 1>(out, frameCount, in, aux, vol, volinc, vola, volainc);
1881 break;
1882 case 2:
1883 volumeRampMulti<MIXTYPE, 2>(out, frameCount, in, aux, vol, volinc, vola, volainc);
1884 break;
1885 case 3:
1886 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 3>(out,
1887 frameCount, in, aux, vol, volinc, vola, volainc);
1888 break;
1889 case 4:
1890 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 4>(out,
1891 frameCount, in, aux, vol, volinc, vola, volainc);
1892 break;
1893 case 5:
1894 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 5>(out,
1895 frameCount, in, aux, vol, volinc, vola, volainc);
1896 break;
1897 case 6:
1898 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 6>(out,
1899 frameCount, in, aux, vol, volinc, vola, volainc);
1900 break;
1901 case 7:
1902 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 7>(out,
1903 frameCount, in, aux, vol, volinc, vola, volainc);
1904 break;
1905 case 8:
1906 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 8>(out,
1907 frameCount, in, aux, vol, volinc, vola, volainc);
1908 break;
1909 }
1910}
1911
1912/* MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
1913 * TO: int32_t (Q4.27) or float
1914 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
1915 * TA: int32_t (Q4.27)
1916 */
1917template <int MIXTYPE,
1918 typename TO, typename TI, typename TV, typename TA, typename TAV>
1919static void volumeMulti(uint32_t channels, TO* out, size_t frameCount,
1920 const TI* in, TA* aux, const TV *vol, TAV vola)
1921{
1922 switch (channels) {
1923 case 1:
1924 volumeMulti<MIXTYPE, 1>(out, frameCount, in, aux, vol, vola);
1925 break;
1926 case 2:
1927 volumeMulti<MIXTYPE, 2>(out, frameCount, in, aux, vol, vola);
1928 break;
1929 case 3:
1930 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 3>(out, frameCount, in, aux, vol, vola);
1931 break;
1932 case 4:
1933 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 4>(out, frameCount, in, aux, vol, vola);
1934 break;
1935 case 5:
1936 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 5>(out, frameCount, in, aux, vol, vola);
1937 break;
1938 case 6:
1939 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 6>(out, frameCount, in, aux, vol, vola);
1940 break;
1941 case 7:
1942 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 7>(out, frameCount, in, aux, vol, vola);
1943 break;
1944 case 8:
1945 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 8>(out, frameCount, in, aux, vol, vola);
1946 break;
1947 }
1948}
1949
1950/* MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
1951 * USEFLOATVOL (set to true if float volume is used)
1952 * ADJUSTVOL (set to true if volume ramp parameters needs adjustment afterwards)
1953 * TO: int32_t (Q4.27) or float
1954 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
1955 * TA: int32_t (Q4.27)
1956 */
1957template <int MIXTYPE, bool USEFLOATVOL, bool ADJUSTVOL,
Andy Hung5e58b0a2014-06-23 19:07:29 -07001958 typename TO, typename TI, typename TA>
1959void AudioMixer::volumeMix(TO *out, size_t outFrames,
1960 const TI *in, TA *aux, bool ramp, AudioMixer::track_t *t)
1961{
1962 if (USEFLOATVOL) {
1963 if (ramp) {
Andy Hunge93b6b72014-07-17 21:30:53 -07001964 volumeRampMulti<MIXTYPE>(t->mMixerChannelCount, out, outFrames, in, aux,
Andy Hung5e58b0a2014-06-23 19:07:29 -07001965 t->mPrevVolume, t->mVolumeInc, &t->prevAuxLevel, t->auxInc);
1966 if (ADJUSTVOL) {
1967 t->adjustVolumeRamp(aux != NULL, true);
1968 }
1969 } else {
Andy Hunge93b6b72014-07-17 21:30:53 -07001970 volumeMulti<MIXTYPE>(t->mMixerChannelCount, out, outFrames, in, aux,
Andy Hung5e58b0a2014-06-23 19:07:29 -07001971 t->mVolume, t->auxLevel);
1972 }
1973 } else {
1974 if (ramp) {
Andy Hunge93b6b72014-07-17 21:30:53 -07001975 volumeRampMulti<MIXTYPE>(t->mMixerChannelCount, out, outFrames, in, aux,
Andy Hung5e58b0a2014-06-23 19:07:29 -07001976 t->prevVolume, t->volumeInc, &t->prevAuxLevel, t->auxInc);
1977 if (ADJUSTVOL) {
1978 t->adjustVolumeRamp(aux != NULL);
1979 }
1980 } else {
Andy Hunge93b6b72014-07-17 21:30:53 -07001981 volumeMulti<MIXTYPE>(t->mMixerChannelCount, out, outFrames, in, aux,
Andy Hung5e58b0a2014-06-23 19:07:29 -07001982 t->volume, t->auxLevel);
1983 }
1984 }
1985}
1986
Andy Hung296b7412014-06-17 15:25:47 -07001987/* This process hook is called when there is a single track without
1988 * aux buffer, volume ramp, or resampling.
1989 * TODO: Update the hook selection: this can properly handle aux and ramp.
Andy Hunge93b6b72014-07-17 21:30:53 -07001990 *
1991 * MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
1992 * TO: int32_t (Q4.27) or float
1993 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
1994 * TA: int32_t (Q4.27)
Andy Hung296b7412014-06-17 15:25:47 -07001995 */
Andy Hunge93b6b72014-07-17 21:30:53 -07001996template <int MIXTYPE, typename TO, typename TI, typename TA>
Andy Hung296b7412014-06-17 15:25:47 -07001997void AudioMixer::process_NoResampleOneTrack(state_t* state, int64_t pts)
1998{
1999 ALOGVV("process_NoResampleOneTrack\n");
2000 // CLZ is faster than CTZ on ARM, though really not sure if true after 31 - clz.
2001 const int i = 31 - __builtin_clz(state->enabledTracks);
2002 ALOG_ASSERT((1 << i) == state->enabledTracks, "more than 1 track enabled");
2003 track_t *t = &state->tracks[i];
Andy Hunge93b6b72014-07-17 21:30:53 -07002004 const uint32_t channels = t->mMixerChannelCount;
Andy Hung296b7412014-06-17 15:25:47 -07002005 TO* out = reinterpret_cast<TO*>(t->mainBuffer);
2006 TA* aux = reinterpret_cast<TA*>(t->auxBuffer);
2007 const bool ramp = t->needsRamp();
2008
2009 for (size_t numFrames = state->frameCount; numFrames; ) {
2010 AudioBufferProvider::Buffer& b(t->buffer);
2011 // get input buffer
2012 b.frameCount = numFrames;
2013 const int64_t outputPTS = calculateOutputPTS(*t, pts, state->frameCount - numFrames);
2014 t->bufferProvider->getNextBuffer(&b, outputPTS);
2015 const TI *in = reinterpret_cast<TI*>(b.raw);
2016
2017 // in == NULL can happen if the track was flushed just after having
2018 // been enabled for mixing.
2019 if (in == NULL || (((uintptr_t)in) & 3)) {
2020 memset(out, 0, numFrames
Andy Hunge93b6b72014-07-17 21:30:53 -07002021 * channels * audio_bytes_per_sample(t->mMixerFormat));
Andy Hung296b7412014-06-17 15:25:47 -07002022 ALOGE_IF((((uintptr_t)in) & 3), "process_NoResampleOneTrack: bus error: "
2023 "buffer %p track %p, channels %d, needs %#x",
2024 in, t, t->channelCount, t->needs);
2025 return;
2026 }
2027
2028 const size_t outFrames = b.frameCount;
Andy Hunge93b6b72014-07-17 21:30:53 -07002029 volumeMix<MIXTYPE, is_same<TI, float>::value, false> (
2030 out, outFrames, in, aux, ramp, t);
Andy Hung5e58b0a2014-06-23 19:07:29 -07002031
Andy Hunge93b6b72014-07-17 21:30:53 -07002032 out += outFrames * channels;
Andy Hung296b7412014-06-17 15:25:47 -07002033 if (aux != NULL) {
Andy Hunge93b6b72014-07-17 21:30:53 -07002034 aux += channels;
Andy Hung296b7412014-06-17 15:25:47 -07002035 }
2036 numFrames -= b.frameCount;
2037
2038 // release buffer
2039 t->bufferProvider->releaseBuffer(&b);
2040 }
2041 if (ramp) {
Andy Hung5e58b0a2014-06-23 19:07:29 -07002042 t->adjustVolumeRamp(aux != NULL, is_same<TI, float>::value);
Andy Hung296b7412014-06-17 15:25:47 -07002043 }
2044}
2045
2046/* This track hook is called to do resampling then mixing,
2047 * pulling from the track's upstream AudioBufferProvider.
Andy Hunge93b6b72014-07-17 21:30:53 -07002048 *
2049 * MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
2050 * TO: int32_t (Q4.27) or float
2051 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
2052 * TA: int32_t (Q4.27)
Andy Hung296b7412014-06-17 15:25:47 -07002053 */
Andy Hunge93b6b72014-07-17 21:30:53 -07002054template <int MIXTYPE, typename TO, typename TI, typename TA>
Andy Hung296b7412014-06-17 15:25:47 -07002055void AudioMixer::track__Resample(track_t* t, TO* out, size_t outFrameCount, TO* temp, TA* aux)
2056{
2057 ALOGVV("track__Resample\n");
2058 t->resampler->setSampleRate(t->sampleRate);
Andy Hung296b7412014-06-17 15:25:47 -07002059 const bool ramp = t->needsRamp();
2060 if (ramp || aux != NULL) {
2061 // if ramp: resample with unity gain to temp buffer and scale/mix in 2nd step.
2062 // if aux != NULL: resample with unity gain to temp buffer then apply send level.
2063
Andy Hung5e58b0a2014-06-23 19:07:29 -07002064 t->resampler->setVolume(UNITY_GAIN_FLOAT, UNITY_GAIN_FLOAT);
Andy Hunge93b6b72014-07-17 21:30:53 -07002065 memset(temp, 0, outFrameCount * t->mMixerChannelCount * sizeof(TO));
Andy Hung296b7412014-06-17 15:25:47 -07002066 t->resampler->resample((int32_t*)temp, outFrameCount, t->bufferProvider);
Andy Hung5e58b0a2014-06-23 19:07:29 -07002067
Andy Hunge93b6b72014-07-17 21:30:53 -07002068 volumeMix<MIXTYPE, is_same<TI, float>::value, true>(
2069 out, outFrameCount, temp, aux, ramp, t);
Andy Hung5e58b0a2014-06-23 19:07:29 -07002070
Andy Hung296b7412014-06-17 15:25:47 -07002071 } else { // constant volume gain
Andy Hung5e58b0a2014-06-23 19:07:29 -07002072 t->resampler->setVolume(t->mVolume[0], t->mVolume[1]);
Andy Hung296b7412014-06-17 15:25:47 -07002073 t->resampler->resample((int32_t*)out, outFrameCount, t->bufferProvider);
2074 }
2075}
2076
2077/* This track hook is called to mix a track, when no resampling is required.
2078 * The input buffer should be present in t->in.
Andy Hunge93b6b72014-07-17 21:30:53 -07002079 *
2080 * MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
2081 * TO: int32_t (Q4.27) or float
2082 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
2083 * TA: int32_t (Q4.27)
Andy Hung296b7412014-06-17 15:25:47 -07002084 */
Andy Hunge93b6b72014-07-17 21:30:53 -07002085template <int MIXTYPE, typename TO, typename TI, typename TA>
Andy Hung296b7412014-06-17 15:25:47 -07002086void AudioMixer::track__NoResample(track_t* t, TO* out, size_t frameCount,
2087 TO* temp __unused, TA* aux)
2088{
2089 ALOGVV("track__NoResample\n");
2090 const TI *in = static_cast<const TI *>(t->in);
2091
Andy Hunge93b6b72014-07-17 21:30:53 -07002092 volumeMix<MIXTYPE, is_same<TI, float>::value, true>(
2093 out, frameCount, in, aux, t->needsRamp(), t);
Andy Hung5e58b0a2014-06-23 19:07:29 -07002094
Andy Hung296b7412014-06-17 15:25:47 -07002095 // MIXTYPE_MONOEXPAND reads a single input channel and expands to NCHAN output channels.
2096 // MIXTYPE_MULTI reads NCHAN input channels and places to NCHAN output channels.
Andy Hunge93b6b72014-07-17 21:30:53 -07002097 in += (MIXTYPE == MIXTYPE_MONOEXPAND) ? frameCount : frameCount * t->mMixerChannelCount;
Andy Hung296b7412014-06-17 15:25:47 -07002098 t->in = in;
2099}
2100
2101/* The Mixer engine generates either int32_t (Q4_27) or float data.
2102 * We use this function to convert the engine buffers
2103 * to the desired mixer output format, either int16_t (Q.15) or float.
2104 */
2105void AudioMixer::convertMixerFormat(void *out, audio_format_t mixerOutFormat,
2106 void *in, audio_format_t mixerInFormat, size_t sampleCount)
2107{
2108 switch (mixerInFormat) {
2109 case AUDIO_FORMAT_PCM_FLOAT:
2110 switch (mixerOutFormat) {
2111 case AUDIO_FORMAT_PCM_FLOAT:
2112 memcpy(out, in, sampleCount * sizeof(float)); // MEMCPY. TODO optimize out
2113 break;
2114 case AUDIO_FORMAT_PCM_16_BIT:
2115 memcpy_to_i16_from_float((int16_t*)out, (float*)in, sampleCount);
2116 break;
2117 default:
2118 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat);
2119 break;
2120 }
2121 break;
2122 case AUDIO_FORMAT_PCM_16_BIT:
2123 switch (mixerOutFormat) {
2124 case AUDIO_FORMAT_PCM_FLOAT:
2125 memcpy_to_float_from_q4_27((float*)out, (int32_t*)in, sampleCount);
2126 break;
2127 case AUDIO_FORMAT_PCM_16_BIT:
2128 // two int16_t are produced per iteration
2129 ditherAndClamp((int32_t*)out, (int32_t*)in, sampleCount >> 1);
2130 break;
2131 default:
2132 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat);
2133 break;
2134 }
2135 break;
2136 default:
2137 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
2138 break;
2139 }
2140}
2141
2142/* Returns the proper track hook to use for mixing the track into the output buffer.
2143 */
Andy Hunge93b6b72014-07-17 21:30:53 -07002144AudioMixer::hook_t AudioMixer::getTrackHook(int trackType, uint32_t channelCount,
Andy Hung296b7412014-06-17 15:25:47 -07002145 audio_format_t mixerInFormat, audio_format_t mixerOutFormat __unused)
2146{
Andy Hunge93b6b72014-07-17 21:30:53 -07002147 if (!kUseNewMixer && channelCount == FCC_2 && mixerInFormat == AUDIO_FORMAT_PCM_16_BIT) {
Andy Hung296b7412014-06-17 15:25:47 -07002148 switch (trackType) {
2149 case TRACKTYPE_NOP:
2150 return track__nop;
2151 case TRACKTYPE_RESAMPLE:
2152 return track__genericResample;
2153 case TRACKTYPE_NORESAMPLEMONO:
2154 return track__16BitsMono;
2155 case TRACKTYPE_NORESAMPLE:
2156 return track__16BitsStereo;
2157 default:
2158 LOG_ALWAYS_FATAL("bad trackType: %d", trackType);
2159 break;
2160 }
2161 }
Andy Hunge93b6b72014-07-17 21:30:53 -07002162 LOG_ALWAYS_FATAL_IF(channelCount > MAX_NUM_CHANNELS);
Andy Hung296b7412014-06-17 15:25:47 -07002163 switch (trackType) {
2164 case TRACKTYPE_NOP:
2165 return track__nop;
2166 case TRACKTYPE_RESAMPLE:
2167 switch (mixerInFormat) {
2168 case AUDIO_FORMAT_PCM_FLOAT:
2169 return (AudioMixer::hook_t)
Andy Hunge93b6b72014-07-17 21:30:53 -07002170 track__Resample<MIXTYPE_MULTI, float /*TO*/, float /*TI*/, int32_t /*TA*/>;
Andy Hung296b7412014-06-17 15:25:47 -07002171 case AUDIO_FORMAT_PCM_16_BIT:
2172 return (AudioMixer::hook_t)\
Andy Hunge93b6b72014-07-17 21:30:53 -07002173 track__Resample<MIXTYPE_MULTI, int32_t, int16_t, int32_t>;
Andy Hung296b7412014-06-17 15:25:47 -07002174 default:
2175 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
2176 break;
2177 }
2178 break;
2179 case TRACKTYPE_NORESAMPLEMONO:
2180 switch (mixerInFormat) {
2181 case AUDIO_FORMAT_PCM_FLOAT:
2182 return (AudioMixer::hook_t)
Andy Hunge93b6b72014-07-17 21:30:53 -07002183 track__NoResample<MIXTYPE_MONOEXPAND, float, float, int32_t>;
Andy Hung296b7412014-06-17 15:25:47 -07002184 case AUDIO_FORMAT_PCM_16_BIT:
2185 return (AudioMixer::hook_t)
Andy Hunge93b6b72014-07-17 21:30:53 -07002186 track__NoResample<MIXTYPE_MONOEXPAND, int32_t, int16_t, int32_t>;
Andy Hung296b7412014-06-17 15:25:47 -07002187 default:
2188 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
2189 break;
2190 }
2191 break;
2192 case TRACKTYPE_NORESAMPLE:
2193 switch (mixerInFormat) {
2194 case AUDIO_FORMAT_PCM_FLOAT:
2195 return (AudioMixer::hook_t)
Andy Hunge93b6b72014-07-17 21:30:53 -07002196 track__NoResample<MIXTYPE_MULTI, float, float, int32_t>;
Andy Hung296b7412014-06-17 15:25:47 -07002197 case AUDIO_FORMAT_PCM_16_BIT:
2198 return (AudioMixer::hook_t)
Andy Hunge93b6b72014-07-17 21:30:53 -07002199 track__NoResample<MIXTYPE_MULTI, int32_t, int16_t, int32_t>;
Andy Hung296b7412014-06-17 15:25:47 -07002200 default:
2201 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
2202 break;
2203 }
2204 break;
2205 default:
2206 LOG_ALWAYS_FATAL("bad trackType: %d", trackType);
2207 break;
2208 }
2209 return NULL;
2210}
2211
2212/* Returns the proper process hook for mixing tracks. Currently works only for
2213 * PROCESSTYPE_NORESAMPLEONETRACK, a mix involving one track, no resampling.
Andy Hung395db4b2014-08-25 17:15:29 -07002214 *
2215 * TODO: Due to the special mixing considerations of duplicating to
2216 * a stereo output track, the input track cannot be MONO. This should be
2217 * prevented by the caller.
Andy Hung296b7412014-06-17 15:25:47 -07002218 */
Andy Hunge93b6b72014-07-17 21:30:53 -07002219AudioMixer::process_hook_t AudioMixer::getProcessHook(int processType, uint32_t channelCount,
Andy Hung296b7412014-06-17 15:25:47 -07002220 audio_format_t mixerInFormat, audio_format_t mixerOutFormat)
2221{
2222 if (processType != PROCESSTYPE_NORESAMPLEONETRACK) { // Only NORESAMPLEONETRACK
2223 LOG_ALWAYS_FATAL("bad processType: %d", processType);
2224 return NULL;
2225 }
Andy Hunge93b6b72014-07-17 21:30:53 -07002226 if (!kUseNewMixer && channelCount == FCC_2 && mixerInFormat == AUDIO_FORMAT_PCM_16_BIT) {
Andy Hung296b7412014-06-17 15:25:47 -07002227 return process__OneTrack16BitsStereoNoResampling;
2228 }
Andy Hunge93b6b72014-07-17 21:30:53 -07002229 LOG_ALWAYS_FATAL_IF(channelCount > MAX_NUM_CHANNELS);
Andy Hung296b7412014-06-17 15:25:47 -07002230 switch (mixerInFormat) {
2231 case AUDIO_FORMAT_PCM_FLOAT:
2232 switch (mixerOutFormat) {
2233 case AUDIO_FORMAT_PCM_FLOAT:
Andy Hunge93b6b72014-07-17 21:30:53 -07002234 return process_NoResampleOneTrack<MIXTYPE_MULTI_SAVEONLY,
2235 float /*TO*/, float /*TI*/, int32_t /*TA*/>;
Andy Hung296b7412014-06-17 15:25:47 -07002236 case AUDIO_FORMAT_PCM_16_BIT:
Andy Hunge93b6b72014-07-17 21:30:53 -07002237 return process_NoResampleOneTrack<MIXTYPE_MULTI_SAVEONLY,
Andy Hung296b7412014-06-17 15:25:47 -07002238 int16_t, float, int32_t>;
2239 default:
2240 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat);
2241 break;
2242 }
2243 break;
2244 case AUDIO_FORMAT_PCM_16_BIT:
2245 switch (mixerOutFormat) {
2246 case AUDIO_FORMAT_PCM_FLOAT:
Andy Hunge93b6b72014-07-17 21:30:53 -07002247 return process_NoResampleOneTrack<MIXTYPE_MULTI_SAVEONLY,
Andy Hung296b7412014-06-17 15:25:47 -07002248 float, int16_t, int32_t>;
2249 case AUDIO_FORMAT_PCM_16_BIT:
Andy Hunge93b6b72014-07-17 21:30:53 -07002250 return process_NoResampleOneTrack<MIXTYPE_MULTI_SAVEONLY,
Andy Hung296b7412014-06-17 15:25:47 -07002251 int16_t, int16_t, int32_t>;
2252 default:
2253 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat);
2254 break;
2255 }
2256 break;
2257 default:
2258 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
2259 break;
2260 }
2261 return NULL;
2262}
2263
Mathias Agopian65ab4712010-07-14 17:59:35 -07002264// ----------------------------------------------------------------------------
2265}; // namespace android