blob: 529f2af149a6feb53b481cd4301882b305e39810 [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>
43
Andy Hung296b7412014-06-17 15:25:47 -070044#include "AudioMixerOps.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070045#include "AudioMixer.h"
46
Andy Hung296b7412014-06-17 15:25:47 -070047// Use the FCC_2 macro for code assuming Fixed Channel Count of 2 and
48// whose stereo assumption may need to be revisited later.
49#ifndef FCC_2
50#define FCC_2 2
51#endif
52
53/* VERY_VERY_VERBOSE_LOGGING will show exactly which process hook and track hook is
54 * being used. This is a considerable amount of log spam, so don't enable unless you
55 * are verifying the hook based code.
56 */
57//#define VERY_VERY_VERBOSE_LOGGING
58#ifdef VERY_VERY_VERBOSE_LOGGING
59#define ALOGVV ALOGV
60//define ALOGVV printf // for test-mixer.cpp
61#else
62#define ALOGVV(a...) do { } while (0)
63#endif
64
Andy Hunga08810b2014-07-16 21:53:43 -070065#ifndef ARRAY_SIZE
66#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
67#endif
68
Andy Hung296b7412014-06-17 15:25:47 -070069// Set kUseNewMixer to true to use the new mixer engine. Otherwise the
70// original code will be used. This is false for now.
71static const bool kUseNewMixer = false;
72
73// Set kUseFloat to true to allow floating input into the mixer engine.
74// If kUseNewMixer is false, this is ignored or may be overridden internally
75// because of downmix/upmix support.
76static const bool kUseFloat = true;
77
Andy Hung1b2fdcb2014-07-16 17:44:34 -070078// Set to default copy buffer size in frames for input processing.
79static const size_t kCopyBufferFrameCount = 256;
80
Mathias Agopian65ab4712010-07-14 17:59:35 -070081namespace android {
Mathias Agopian65ab4712010-07-14 17:59:35 -070082
83// ----------------------------------------------------------------------------
Andy Hung1b2fdcb2014-07-16 17:44:34 -070084
85template <typename T>
86T min(const T& a, const T& b)
87{
88 return a < b ? a : b;
89}
90
91AudioMixer::CopyBufferProvider::CopyBufferProvider(size_t inputFrameSize,
92 size_t outputFrameSize, size_t bufferFrameCount) :
93 mInputFrameSize(inputFrameSize),
94 mOutputFrameSize(outputFrameSize),
95 mLocalBufferFrameCount(bufferFrameCount),
96 mLocalBufferData(NULL),
97 mConsumed(0)
98{
99 ALOGV("CopyBufferProvider(%p)(%zu, %zu, %zu)", this,
100 inputFrameSize, outputFrameSize, bufferFrameCount);
101 LOG_ALWAYS_FATAL_IF(inputFrameSize < outputFrameSize && bufferFrameCount == 0,
102 "Requires local buffer if inputFrameSize(%d) < outputFrameSize(%d)",
103 inputFrameSize, outputFrameSize);
104 if (mLocalBufferFrameCount) {
105 (void)posix_memalign(&mLocalBufferData, 32, mLocalBufferFrameCount * mOutputFrameSize);
106 }
107 mBuffer.frameCount = 0;
108}
109
110AudioMixer::CopyBufferProvider::~CopyBufferProvider()
111{
112 ALOGV("~CopyBufferProvider(%p)", this);
113 if (mBuffer.frameCount != 0) {
114 mTrackBufferProvider->releaseBuffer(&mBuffer);
115 }
116 free(mLocalBufferData);
117}
118
119status_t AudioMixer::CopyBufferProvider::getNextBuffer(AudioBufferProvider::Buffer *pBuffer,
120 int64_t pts)
121{
122 //ALOGV("CopyBufferProvider(%p)::getNextBuffer(%p (%zu), %lld)",
123 // this, pBuffer, pBuffer->frameCount, pts);
124 if (mLocalBufferFrameCount == 0) {
125 status_t res = mTrackBufferProvider->getNextBuffer(pBuffer, pts);
126 if (res == OK) {
127 copyFrames(pBuffer->raw, pBuffer->raw, pBuffer->frameCount);
128 }
129 return res;
130 }
131 if (mBuffer.frameCount == 0) {
132 mBuffer.frameCount = pBuffer->frameCount;
133 status_t res = mTrackBufferProvider->getNextBuffer(&mBuffer, pts);
134 // At one time an upstream buffer provider had
135 // res == OK and mBuffer.frameCount == 0, doesn't seem to happen now 7/18/2014.
136 //
137 // By API spec, if res != OK, then mBuffer.frameCount == 0.
138 // but there may be improper implementations.
139 ALOG_ASSERT(res == OK || mBuffer.frameCount == 0);
140 if (res != OK || mBuffer.frameCount == 0) { // not needed by API spec, but to be safe.
141 pBuffer->raw = NULL;
142 pBuffer->frameCount = 0;
143 return res;
144 }
145 mConsumed = 0;
146 }
147 ALOG_ASSERT(mConsumed < mBuffer.frameCount);
148 size_t count = min(mLocalBufferFrameCount, mBuffer.frameCount - mConsumed);
149 count = min(count, pBuffer->frameCount);
150 pBuffer->raw = mLocalBufferData;
151 pBuffer->frameCount = count;
152 copyFrames(pBuffer->raw, (uint8_t*)mBuffer.raw + mConsumed * mInputFrameSize,
153 pBuffer->frameCount);
154 return OK;
155}
156
157void AudioMixer::CopyBufferProvider::releaseBuffer(AudioBufferProvider::Buffer *pBuffer)
158{
159 //ALOGV("CopyBufferProvider(%p)::releaseBuffer(%p(%zu))",
160 // this, pBuffer, pBuffer->frameCount);
161 if (mLocalBufferFrameCount == 0) {
162 mTrackBufferProvider->releaseBuffer(pBuffer);
163 return;
164 }
165 // LOG_ALWAYS_FATAL_IF(pBuffer->frameCount == 0, "Invalid framecount");
166 mConsumed += pBuffer->frameCount; // TODO: update for efficiency to reuse existing content
167 if (mConsumed != 0 && mConsumed >= mBuffer.frameCount) {
168 mTrackBufferProvider->releaseBuffer(&mBuffer);
169 ALOG_ASSERT(mBuffer.frameCount == 0);
170 }
171 pBuffer->raw = NULL;
172 pBuffer->frameCount = 0;
173}
174
175void AudioMixer::CopyBufferProvider::reset()
176{
177 if (mBuffer.frameCount != 0) {
178 mTrackBufferProvider->releaseBuffer(&mBuffer);
179 }
180 mConsumed = 0;
181}
182
Andy Hung34803d52014-07-16 21:41:35 -0700183AudioMixer::DownmixerBufferProvider::DownmixerBufferProvider(
184 audio_channel_mask_t inputChannelMask,
185 audio_channel_mask_t outputChannelMask, audio_format_t format,
186 uint32_t sampleRate, int32_t sessionId, size_t bufferFrameCount) :
187 CopyBufferProvider(
188 audio_bytes_per_sample(format) * audio_channel_count_from_out_mask(inputChannelMask),
189 audio_bytes_per_sample(format) * audio_channel_count_from_out_mask(outputChannelMask),
190 bufferFrameCount) // set bufferFrameCount to 0 to do in-place
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700191{
Andy Hung34803d52014-07-16 21:41:35 -0700192 ALOGV("DownmixerBufferProvider(%p)(%#x, %#x, %#x %u %d)",
193 this, inputChannelMask, outputChannelMask, format,
194 sampleRate, sessionId);
195 if (!sIsMultichannelCapable
196 || EffectCreate(&sDwnmFxDesc.uuid,
197 sessionId,
198 SESSION_ID_INVALID_AND_IGNORED,
199 &mDownmixHandle) != 0) {
200 ALOGE("DownmixerBufferProvider() error creating downmixer effect");
201 mDownmixHandle = NULL;
202 return;
203 }
204 // channel input configuration will be overridden per-track
205 mDownmixConfig.inputCfg.channels = inputChannelMask; // FIXME: Should be bits
206 mDownmixConfig.outputCfg.channels = outputChannelMask; // FIXME: should be bits
207 mDownmixConfig.inputCfg.format = format;
208 mDownmixConfig.outputCfg.format = format;
209 mDownmixConfig.inputCfg.samplingRate = sampleRate;
210 mDownmixConfig.outputCfg.samplingRate = sampleRate;
211 mDownmixConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
212 mDownmixConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
213 // input and output buffer provider, and frame count will not be used as the downmix effect
214 // process() function is called directly (see DownmixerBufferProvider::getNextBuffer())
215 mDownmixConfig.inputCfg.mask = EFFECT_CONFIG_SMP_RATE | EFFECT_CONFIG_CHANNELS |
216 EFFECT_CONFIG_FORMAT | EFFECT_CONFIG_ACC_MODE;
217 mDownmixConfig.outputCfg.mask = mDownmixConfig.inputCfg.mask;
218
219 int cmdStatus;
220 uint32_t replySize = sizeof(int);
221
222 // Configure downmixer
223 status_t status = (*mDownmixHandle)->command(mDownmixHandle,
224 EFFECT_CMD_SET_CONFIG /*cmdCode*/, sizeof(effect_config_t) /*cmdSize*/,
225 &mDownmixConfig /*pCmdData*/,
226 &replySize, &cmdStatus /*pReplyData*/);
227 if (status != 0 || cmdStatus != 0) {
228 ALOGE("DownmixerBufferProvider() error %d cmdStatus %d while configuring downmixer",
229 status, cmdStatus);
230 EffectRelease(mDownmixHandle);
231 mDownmixHandle = NULL;
232 return;
233 }
234
235 // Enable downmixer
236 replySize = sizeof(int);
237 status = (*mDownmixHandle)->command(mDownmixHandle,
238 EFFECT_CMD_ENABLE /*cmdCode*/, 0 /*cmdSize*/, NULL /*pCmdData*/,
239 &replySize, &cmdStatus /*pReplyData*/);
240 if (status != 0 || cmdStatus != 0) {
241 ALOGE("DownmixerBufferProvider() error %d cmdStatus %d while enabling downmixer",
242 status, cmdStatus);
243 EffectRelease(mDownmixHandle);
244 mDownmixHandle = NULL;
245 return;
246 }
247
248 // Set downmix type
249 // parameter size rounded for padding on 32bit boundary
250 const int psizePadded = ((sizeof(downmix_params_t) - 1)/sizeof(int) + 1) * sizeof(int);
251 const int downmixParamSize =
252 sizeof(effect_param_t) + psizePadded + sizeof(downmix_type_t);
253 effect_param_t * const param = (effect_param_t *) malloc(downmixParamSize);
254 param->psize = sizeof(downmix_params_t);
255 const downmix_params_t downmixParam = DOWNMIX_PARAM_TYPE;
256 memcpy(param->data, &downmixParam, param->psize);
257 const downmix_type_t downmixType = DOWNMIX_TYPE_FOLD;
258 param->vsize = sizeof(downmix_type_t);
259 memcpy(param->data + psizePadded, &downmixType, param->vsize);
260 replySize = sizeof(int);
261 status = (*mDownmixHandle)->command(mDownmixHandle,
262 EFFECT_CMD_SET_PARAM /* cmdCode */, downmixParamSize /* cmdSize */,
263 param /*pCmdData*/, &replySize, &cmdStatus /*pReplyData*/);
264 free(param);
265 if (status != 0 || cmdStatus != 0) {
266 ALOGE("DownmixerBufferProvider() error %d cmdStatus %d while setting downmix type",
267 status, cmdStatus);
268 EffectRelease(mDownmixHandle);
269 mDownmixHandle = NULL;
270 return;
271 }
272 ALOGV("DownmixerBufferProvider() downmix type set to %d", (int) downmixType);
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700273}
274
275AudioMixer::DownmixerBufferProvider::~DownmixerBufferProvider()
276{
Andy Hung34803d52014-07-16 21:41:35 -0700277 ALOGV("~DownmixerBufferProvider (%p)", this);
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700278 EffectRelease(mDownmixHandle);
Andy Hung34803d52014-07-16 21:41:35 -0700279 mDownmixHandle = NULL;
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700280}
281
Andy Hung34803d52014-07-16 21:41:35 -0700282void AudioMixer::DownmixerBufferProvider::copyFrames(void *dst, const void *src, size_t frames)
283{
284 mDownmixConfig.inputCfg.buffer.frameCount = frames;
285 mDownmixConfig.inputCfg.buffer.raw = const_cast<void *>(src);
286 mDownmixConfig.outputCfg.buffer.frameCount = frames;
287 mDownmixConfig.outputCfg.buffer.raw = dst;
288 // may be in-place if src == dst.
289 status_t res = (*mDownmixHandle)->process(mDownmixHandle,
290 &mDownmixConfig.inputCfg.buffer, &mDownmixConfig.outputCfg.buffer);
291 ALOGE_IF(res != OK, "DownmixBufferProvider error %d", res);
292}
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700293
Andy Hung34803d52014-07-16 21:41:35 -0700294/* call once in a pthread_once handler. */
295/*static*/ status_t AudioMixer::DownmixerBufferProvider::init()
296{
297 // find multichannel downmix effect if we have to play multichannel content
298 uint32_t numEffects = 0;
299 int ret = EffectQueryNumberEffects(&numEffects);
300 if (ret != 0) {
301 ALOGE("AudioMixer() error %d querying number of effects", ret);
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700302 return NO_INIT;
303 }
Andy Hung34803d52014-07-16 21:41:35 -0700304 ALOGV("EffectQueryNumberEffects() numEffects=%d", numEffects);
305
306 for (uint32_t i = 0 ; i < numEffects ; i++) {
307 if (EffectQueryEffect(i, &sDwnmFxDesc) == 0) {
308 ALOGV("effect %d is called %s", i, sDwnmFxDesc.name);
309 if (memcmp(&sDwnmFxDesc.type, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0) {
310 ALOGI("found effect \"%s\" from %s",
311 sDwnmFxDesc.name, sDwnmFxDesc.implementor);
312 sIsMultichannelCapable = true;
313 break;
314 }
315 }
316 }
317 ALOGW_IF(!sIsMultichannelCapable, "unable to find downmix effect");
318 return NO_INIT;
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700319}
320
Andy Hung34803d52014-07-16 21:41:35 -0700321/*static*/ bool AudioMixer::DownmixerBufferProvider::sIsMultichannelCapable = false;
322/*static*/ effect_descriptor_t AudioMixer::DownmixerBufferProvider::sDwnmFxDesc;
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700323
Andy Hunga08810b2014-07-16 21:53:43 -0700324AudioMixer::RemixBufferProvider::RemixBufferProvider(audio_channel_mask_t inputChannelMask,
325 audio_channel_mask_t outputChannelMask, audio_format_t format,
326 size_t bufferFrameCount) :
327 CopyBufferProvider(
328 audio_bytes_per_sample(format)
329 * audio_channel_count_from_out_mask(inputChannelMask),
330 audio_bytes_per_sample(format)
331 * audio_channel_count_from_out_mask(outputChannelMask),
332 bufferFrameCount),
333 mFormat(format),
334 mSampleSize(audio_bytes_per_sample(format)),
335 mInputChannels(audio_channel_count_from_out_mask(inputChannelMask)),
336 mOutputChannels(audio_channel_count_from_out_mask(outputChannelMask))
337{
338 ALOGV("RemixBufferProvider(%p)(%#x, %#x, %#x) %d %d",
339 this, format, inputChannelMask, outputChannelMask,
340 mInputChannels, mOutputChannels);
341 // TODO: consider channel representation in index array formulation
342 // We ignore channel representation, and just use the bits.
343 memcpy_by_index_array_initialization(mIdxAry, ARRAY_SIZE(mIdxAry),
344 audio_channel_mask_get_bits(outputChannelMask),
345 audio_channel_mask_get_bits(inputChannelMask));
346}
347
348void AudioMixer::RemixBufferProvider::copyFrames(void *dst, const void *src, size_t frames)
349{
350 memcpy_by_index_array(dst, mOutputChannels,
351 src, mInputChannels, mIdxAry, mSampleSize, frames);
352}
353
Andy Hungef7c7fb2014-05-12 16:51:41 -0700354AudioMixer::ReformatBufferProvider::ReformatBufferProvider(int32_t channels,
Andy Hung1b2fdcb2014-07-16 17:44:34 -0700355 audio_format_t inputFormat, audio_format_t outputFormat,
356 size_t bufferFrameCount) :
357 CopyBufferProvider(
358 channels * audio_bytes_per_sample(inputFormat),
359 channels * audio_bytes_per_sample(outputFormat),
360 bufferFrameCount),
Andy Hungef7c7fb2014-05-12 16:51:41 -0700361 mChannels(channels),
362 mInputFormat(inputFormat),
Andy Hung1b2fdcb2014-07-16 17:44:34 -0700363 mOutputFormat(outputFormat)
Andy Hungef7c7fb2014-05-12 16:51:41 -0700364{
365 ALOGV("ReformatBufferProvider(%p)(%d, %#x, %#x)", this, channels, inputFormat, outputFormat);
Andy Hungef7c7fb2014-05-12 16:51:41 -0700366}
367
Andy Hung1b2fdcb2014-07-16 17:44:34 -0700368void AudioMixer::ReformatBufferProvider::copyFrames(void *dst, const void *src, size_t frames)
Andy Hungef7c7fb2014-05-12 16:51:41 -0700369{
Andy Hung1b2fdcb2014-07-16 17:44:34 -0700370 memcpy_by_audio_format(dst, mOutputFormat, src, mInputFormat, frames * mChannels);
Andy Hungef7c7fb2014-05-12 16:51:41 -0700371}
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700372
373// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -0700374
Paul Lind3c0a0e82012-08-01 18:49:49 -0700375// Ensure mConfiguredNames bitmask is initialized properly on all architectures.
376// The value of 1 << x is undefined in C when x >= 32.
377
Glenn Kasten5c94b6c2012-03-20 17:01:29 -0700378AudioMixer::AudioMixer(size_t frameCount, uint32_t sampleRate, uint32_t maxNumTracks)
Paul Lind3c0a0e82012-08-01 18:49:49 -0700379 : mTrackNames(0), mConfiguredNames((maxNumTracks >= 32 ? 0 : 1 << maxNumTracks) - 1),
Glenn Kasten7f5d3352013-02-15 23:55:04 +0000380 mSampleRate(sampleRate)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700381{
Glenn Kasten788040c2011-05-05 08:19:00 -0700382 // AudioMixer is not yet capable of multi-channel beyond stereo
Glenn Kasten5798d4e2012-03-08 12:18:35 -0800383 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(2 == MAX_NUM_CHANNELS);
Jean-Michel Triviacb86cc2012-04-16 12:43:57 -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
391 // AudioMixer is not yet capable of multi-channel output beyond stereo
392 ALOG_ASSERT(2 == MAX_NUM_CHANNELS, "bad MAX_NUM_CHANNELS %d", MAX_NUM_CHANNELS);
393
Glenn Kasten52008f82012-03-18 09:34:41 -0700394 pthread_once(&sOnceControl, &sInitRoutine);
395
Mathias Agopian65ab4712010-07-14 17:59:35 -0700396 mState.enabledTracks= 0;
397 mState.needsChanged = 0;
398 mState.frameCount = frameCount;
Glenn Kasten84afa3b2012-01-25 15:28:08 -0800399 mState.hook = process__nop;
Glenn Kastene0feee32011-12-13 11:53:26 -0800400 mState.outputTemp = NULL;
401 mState.resampleTemp = NULL;
Glenn Kastenab7d72f2013-02-27 09:05:28 -0800402 mState.mLog = &mDummyLog;
Glenn Kasten84afa3b2012-01-25 15:28:08 -0800403 // mState.reserved
Glenn Kasten17a736c2012-02-14 08:52:15 -0800404
405 // FIXME Most of the following initialization is probably redundant since
406 // tracks[i] should only be referenced if (mTrackNames & (1 << i)) != 0
407 // and mTrackNames is initially 0. However, leave it here until that's verified.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700408 track_t* t = mState.tracks;
Glenn Kastenbf71f1e2011-12-13 11:52:35 -0800409 for (unsigned i=0 ; i < MAX_NUM_TRACKS ; i++) {
Eric Laurenta5e82142012-04-16 13:47:17 -0700410 t->resampler = NULL;
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700411 t->downmixerBufferProvider = NULL;
Andy Hung1b2fdcb2014-07-16 17:44:34 -0700412 t->mReformatBufferProvider = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700413 t++;
414 }
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700415
Mathias Agopian65ab4712010-07-14 17:59:35 -0700416}
417
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800418AudioMixer::~AudioMixer()
419{
420 track_t* t = mState.tracks;
Glenn Kastenbf71f1e2011-12-13 11:52:35 -0800421 for (unsigned i=0 ; i < MAX_NUM_TRACKS ; i++) {
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800422 delete t->resampler;
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700423 delete t->downmixerBufferProvider;
Andy Hung1b2fdcb2014-07-16 17:44:34 -0700424 delete t->mReformatBufferProvider;
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800425 t++;
426 }
427 delete [] mState.outputTemp;
428 delete [] mState.resampleTemp;
429}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700430
Glenn Kastenab7d72f2013-02-27 09:05:28 -0800431void AudioMixer::setLog(NBLog::Writer *log)
432{
433 mState.mLog = log;
434}
435
Andy Hunge8a1ced2014-05-09 15:02:21 -0700436int AudioMixer::getTrackName(audio_channel_mask_t channelMask,
437 audio_format_t format, int sessionId)
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800438{
Andy Hunge8a1ced2014-05-09 15:02:21 -0700439 if (!isValidPcmTrackFormat(format)) {
440 ALOGE("AudioMixer::getTrackName invalid format (%#x)", format);
441 return -1;
442 }
Glenn Kasten5c94b6c2012-03-20 17:01:29 -0700443 uint32_t names = (~mTrackNames) & mConfiguredNames;
Glenn Kasten98dd5422011-12-15 14:38:29 -0800444 if (names != 0) {
445 int n = __builtin_ctz(names);
Steve Block3856b092011-10-20 11:56:00 +0100446 ALOGV("add track (%d)", n);
Glenn Kastendeeb1282012-03-25 11:59:31 -0700447 // assume default parameters for the track, except where noted below
448 track_t* t = &mState.tracks[n];
449 t->needs = 0;
Andy Hung5e58b0a2014-06-23 19:07:29 -0700450
451 // Integer volume.
452 // Currently integer volume is kept for the legacy integer mixer.
453 // Will be removed when the legacy mixer path is removed.
Andy Hung97ae8242014-05-30 10:35:47 -0700454 t->volume[0] = UNITY_GAIN_INT;
455 t->volume[1] = UNITY_GAIN_INT;
Andy Hung5e58b0a2014-06-23 19:07:29 -0700456 t->prevVolume[0] = UNITY_GAIN_INT << 16;
457 t->prevVolume[1] = UNITY_GAIN_INT << 16;
Glenn Kastendeeb1282012-03-25 11:59:31 -0700458 t->volumeInc[0] = 0;
459 t->volumeInc[1] = 0;
460 t->auxLevel = 0;
461 t->auxInc = 0;
Andy Hung5e58b0a2014-06-23 19:07:29 -0700462 t->prevAuxLevel = 0;
463
464 // Floating point volume.
465 t->mVolume[0] = UNITY_GAIN_FLOAT;
466 t->mVolume[1] = UNITY_GAIN_FLOAT;
467 t->mPrevVolume[0] = UNITY_GAIN_FLOAT;
468 t->mPrevVolume[1] = UNITY_GAIN_FLOAT;
469 t->mVolumeInc[0] = 0.;
470 t->mVolumeInc[1] = 0.;
471 t->mAuxLevel = 0.;
472 t->mAuxInc = 0.;
473 t->mPrevAuxLevel = 0.;
474
Glenn Kastendeeb1282012-03-25 11:59:31 -0700475 // no initialization needed
Glenn Kastendeeb1282012-03-25 11:59:31 -0700476 // t->frameCount
Andy Hung68112fc2014-05-14 14:13:23 -0700477 t->channelCount = audio_channel_count_from_out_mask(channelMask);
Glenn Kastendeeb1282012-03-25 11:59:31 -0700478 t->enabled = false;
Andy Hungef7c7fb2014-05-12 16:51:41 -0700479 ALOGV_IF(channelMask != AUDIO_CHANNEL_OUT_STEREO,
480 "Non-stereo channel mask: %d\n", channelMask);
Andy Hung68112fc2014-05-14 14:13:23 -0700481 t->channelMask = channelMask;
Jean-Michel Trivid06e1322012-09-12 15:47:07 -0700482 t->sessionId = sessionId;
Glenn Kastendeeb1282012-03-25 11:59:31 -0700483 // setBufferProvider(name, AudioBufferProvider *) is required before enable(name)
484 t->bufferProvider = NULL;
485 t->buffer.raw = NULL;
486 // no initialization needed
487 // t->buffer.frameCount
488 t->hook = NULL;
489 t->in = NULL;
490 t->resampler = NULL;
491 t->sampleRate = mSampleRate;
492 // setParameter(name, TRACK, MAIN_BUFFER, mixBuffer) is required before enable(name)
493 t->mainBuffer = NULL;
494 t->auxBuffer = NULL;
Andy Hungef7c7fb2014-05-12 16:51:41 -0700495 t->mInputBufferProvider = NULL;
496 t->mReformatBufferProvider = NULL;
Glenn Kasten52008f82012-03-18 09:34:41 -0700497 t->downmixerBufferProvider = NULL;
Andy Hung78820702014-02-28 16:23:02 -0800498 t->mMixerFormat = AUDIO_FORMAT_PCM_16_BIT;
Andy Hunge8a1ced2014-05-09 15:02:21 -0700499 t->mFormat = format;
Andy Hung296b7412014-06-17 15:25:47 -0700500 t->mMixerInFormat = kUseFloat && kUseNewMixer
501 ? AUDIO_FORMAT_PCM_FLOAT : AUDIO_FORMAT_PCM_16_BIT;
502 // Check the downmixing (or upmixing) requirements.
503 status_t status = initTrackDownmix(t, n, channelMask);
Andy Hung68112fc2014-05-14 14:13:23 -0700504 if (status != OK) {
505 ALOGE("AudioMixer::getTrackName invalid channelMask (%#x)", channelMask);
506 return -1;
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700507 }
Andy Hung296b7412014-06-17 15:25:47 -0700508 // initTrackDownmix() may change the input format requirement.
509 // If you desire floating point input to the mixer, it may change
510 // to integer because the downmixer requires integer to process.
511 ALOGVV("mMixerFormat:%#x mMixerInFormat:%#x\n", t->mMixerFormat, t->mMixerInFormat);
512 prepareTrackForReformat(t, n);
Andy Hung68112fc2014-05-14 14:13:23 -0700513 mTrackNames |= 1 << n;
514 return TRACK0 + n;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700515 }
Andy Hung68112fc2014-05-14 14:13:23 -0700516 ALOGE("AudioMixer::getTrackName out of available tracks");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700517 return -1;
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800518}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700519
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800520void AudioMixer::invalidateState(uint32_t mask)
521{
Glenn Kasten34fca342013-08-13 09:48:14 -0700522 if (mask != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700523 mState.needsChanged |= mask;
524 mState.hook = process__validate;
525 }
526 }
527
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700528status_t AudioMixer::initTrackDownmix(track_t* pTrack, int trackNum, audio_channel_mask_t mask)
529{
Andy Hunge5412692014-05-16 11:25:07 -0700530 uint32_t channelCount = audio_channel_count_from_out_mask(mask);
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700531 ALOG_ASSERT((channelCount <= MAX_NUM_CHANNELS_TO_DOWNMIX) && channelCount);
532 status_t status = OK;
533 if (channelCount > MAX_NUM_CHANNELS) {
534 pTrack->channelMask = mask;
535 pTrack->channelCount = channelCount;
536 ALOGV("initTrackDownmix(track=%d, mask=0x%x) calls prepareTrackForDownmix()",
537 trackNum, mask);
538 status = prepareTrackForDownmix(pTrack, trackNum);
539 } else {
540 unprepareTrackForDownmix(pTrack, trackNum);
541 }
542 return status;
543}
544
Andy Hungee931ff2014-01-28 13:44:14 -0800545void AudioMixer::unprepareTrackForDownmix(track_t* pTrack, int trackName __unused) {
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700546 ALOGV("AudioMixer::unprepareTrackForDownmix(%d)", trackName);
547
548 if (pTrack->downmixerBufferProvider != NULL) {
549 // this track had previously been configured with a downmixer, delete it
550 ALOGV(" deleting old downmixer");
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700551 delete pTrack->downmixerBufferProvider;
552 pTrack->downmixerBufferProvider = NULL;
Andy Hungef7c7fb2014-05-12 16:51:41 -0700553 reconfigureBufferProviders(pTrack);
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700554 } else {
555 ALOGV(" nothing to do, no downmixer to delete");
556 }
557}
558
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700559status_t AudioMixer::prepareTrackForDownmix(track_t* pTrack, int trackName)
560{
561 ALOGV("AudioMixer::prepareTrackForDownmix(%d) with mask 0x%x", trackName, pTrack->channelMask);
562
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700563 // discard the previous downmixer if there was one
564 unprepareTrackForDownmix(pTrack, trackName);
Andy Hung34803d52014-07-16 21:41:35 -0700565 if (DownmixerBufferProvider::isMultichannelCapable()) {
566 DownmixerBufferProvider* pDbp = new DownmixerBufferProvider(pTrack->channelMask,
567 /* pTrack->mMixerChannelMask */ audio_channel_out_mask_from_count(2),
568 /* pTrack->mMixerInFormat */ AUDIO_FORMAT_PCM_16_BIT,
569 pTrack->sampleRate, pTrack->sessionId, kCopyBufferFrameCount);
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700570
Andy Hung34803d52014-07-16 21:41:35 -0700571 if (pDbp->isValid()) { // if constructor completed properly
572 pTrack->mMixerInFormat = AUDIO_FORMAT_PCM_16_BIT; // PCM 16 bit required for downmix
573 pTrack->downmixerBufferProvider = pDbp;
574 reconfigureBufferProviders(pTrack);
575 return NO_ERROR;
576 }
577 delete pDbp;
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700578 }
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700579 pTrack->downmixerBufferProvider = NULL;
Andy Hungef7c7fb2014-05-12 16:51:41 -0700580 reconfigureBufferProviders(pTrack);
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700581 return NO_INIT;
582}
583
Andy Hungef7c7fb2014-05-12 16:51:41 -0700584void AudioMixer::unprepareTrackForReformat(track_t* pTrack, int trackName __unused) {
585 ALOGV("AudioMixer::unprepareTrackForReformat(%d)", trackName);
586 if (pTrack->mReformatBufferProvider != NULL) {
587 delete pTrack->mReformatBufferProvider;
588 pTrack->mReformatBufferProvider = NULL;
589 reconfigureBufferProviders(pTrack);
590 }
591}
592
593status_t AudioMixer::prepareTrackForReformat(track_t* pTrack, int trackName)
594{
595 ALOGV("AudioMixer::prepareTrackForReformat(%d) with format %#x", trackName, pTrack->mFormat);
596 // discard the previous reformatter if there was one
Andy Hung296b7412014-06-17 15:25:47 -0700597 unprepareTrackForReformat(pTrack, trackName);
598 // only configure reformatter if needed
599 if (pTrack->mFormat != pTrack->mMixerInFormat) {
600 pTrack->mReformatBufferProvider = new ReformatBufferProvider(
601 audio_channel_count_from_out_mask(pTrack->channelMask),
Andy Hung1b2fdcb2014-07-16 17:44:34 -0700602 pTrack->mFormat, pTrack->mMixerInFormat,
603 kCopyBufferFrameCount);
Andy Hung296b7412014-06-17 15:25:47 -0700604 reconfigureBufferProviders(pTrack);
605 }
606 return NO_ERROR;
Andy Hungef7c7fb2014-05-12 16:51:41 -0700607}
608
609void AudioMixer::reconfigureBufferProviders(track_t* pTrack)
610{
611 pTrack->bufferProvider = pTrack->mInputBufferProvider;
612 if (pTrack->mReformatBufferProvider) {
Andy Hung1b2fdcb2014-07-16 17:44:34 -0700613 pTrack->mReformatBufferProvider->setBufferProvider(pTrack->bufferProvider);
Andy Hungef7c7fb2014-05-12 16:51:41 -0700614 pTrack->bufferProvider = pTrack->mReformatBufferProvider;
615 }
616 if (pTrack->downmixerBufferProvider) {
Andy Hung34803d52014-07-16 21:41:35 -0700617 pTrack->downmixerBufferProvider->setBufferProvider(pTrack->bufferProvider);
Andy Hungef7c7fb2014-05-12 16:51:41 -0700618 pTrack->bufferProvider = pTrack->downmixerBufferProvider;
619 }
620}
621
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800622void AudioMixer::deleteTrackName(int name)
623{
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700624 ALOGV("AudioMixer::deleteTrackName(%d)", name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700625 name -= TRACK0;
Glenn Kasten5798d4e2012-03-08 12:18:35 -0800626 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
Glenn Kasten237a6242011-12-15 15:32:27 -0800627 ALOGV("deleteTrackName(%d)", name);
628 track_t& track(mState.tracks[ name ]);
Glenn Kasten4c340c62012-01-27 12:33:54 -0800629 if (track.enabled) {
630 track.enabled = false;
Glenn Kasten237a6242011-12-15 15:32:27 -0800631 invalidateState(1<<name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700632 }
Glenn Kasten4e2293f2012-04-12 09:39:07 -0700633 // delete the resampler
634 delete track.resampler;
635 track.resampler = NULL;
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700636 // delete the downmixer
637 unprepareTrackForDownmix(&mState.tracks[name], name);
Andy Hungef7c7fb2014-05-12 16:51:41 -0700638 // delete the reformatter
639 unprepareTrackForReformat(&mState.tracks[name], name);
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700640
Glenn Kasten237a6242011-12-15 15:32:27 -0800641 mTrackNames &= ~(1<<name);
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800642}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700643
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800644void AudioMixer::enable(int name)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700645{
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800646 name -= TRACK0;
Glenn Kasten5798d4e2012-03-08 12:18:35 -0800647 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800648 track_t& track = mState.tracks[name];
649
Glenn Kasten4c340c62012-01-27 12:33:54 -0800650 if (!track.enabled) {
651 track.enabled = true;
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800652 ALOGV("enable(%d)", name);
653 invalidateState(1 << name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700654 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700655}
656
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800657void AudioMixer::disable(int name)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700658{
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800659 name -= TRACK0;
Glenn Kasten5798d4e2012-03-08 12:18:35 -0800660 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800661 track_t& track = mState.tracks[name];
662
Glenn Kasten4c340c62012-01-27 12:33:54 -0800663 if (track.enabled) {
664 track.enabled = false;
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800665 ALOGV("disable(%d)", name);
666 invalidateState(1 << name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700667 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700668}
669
Andy Hung5866a3b2014-05-29 21:33:13 -0700670/* Sets the volume ramp variables for the AudioMixer.
671 *
Andy Hung5e58b0a2014-06-23 19:07:29 -0700672 * The volume ramp variables are used to transition from the previous
673 * volume to the set volume. ramp controls the duration of the transition.
674 * Its value is typically one state framecount period, but may also be 0,
675 * meaning "immediate."
Andy Hung5866a3b2014-05-29 21:33:13 -0700676 *
Andy Hung5e58b0a2014-06-23 19:07:29 -0700677 * FIXME: 1) Volume ramp is enabled only if there is a nonzero integer increment
678 * even if there is a nonzero floating point increment (in that case, the volume
679 * change is immediate). This restriction should be changed when the legacy mixer
680 * is removed (see #2).
681 * FIXME: 2) Integer volume variables are used for Legacy mixing and should be removed
682 * when no longer needed.
683 *
684 * @param newVolume set volume target in floating point [0.0, 1.0].
685 * @param ramp number of frames to increment over. if ramp is 0, the volume
686 * should be set immediately. Currently ramp should not exceed 65535 (frames).
687 * @param pIntSetVolume pointer to the U4.12 integer target volume, set on return.
688 * @param pIntPrevVolume pointer to the U4.28 integer previous volume, set on return.
689 * @param pIntVolumeInc pointer to the U4.28 increment per output audio frame, set on return.
690 * @param pSetVolume pointer to the float target volume, set on return.
691 * @param pPrevVolume pointer to the float previous volume, set on return.
692 * @param pVolumeInc pointer to the float increment per output audio frame, set on return.
Andy Hung5866a3b2014-05-29 21:33:13 -0700693 * @return true if the volume has changed, false if volume is same.
694 */
Andy Hung5e58b0a2014-06-23 19:07:29 -0700695static inline bool setVolumeRampVariables(float newVolume, int32_t ramp,
696 int16_t *pIntSetVolume, int32_t *pIntPrevVolume, int32_t *pIntVolumeInc,
697 float *pSetVolume, float *pPrevVolume, float *pVolumeInc) {
698 if (newVolume == *pSetVolume) {
Andy Hung5866a3b2014-05-29 21:33:13 -0700699 return false;
700 }
Andy Hung5e58b0a2014-06-23 19:07:29 -0700701 /* set the floating point volume variables */
Andy Hung5866a3b2014-05-29 21:33:13 -0700702 if (ramp != 0) {
Andy Hung5e58b0a2014-06-23 19:07:29 -0700703 *pVolumeInc = (newVolume - *pSetVolume) / ramp;
704 *pPrevVolume = *pSetVolume;
Andy Hung5866a3b2014-05-29 21:33:13 -0700705 } else {
Andy Hung5e58b0a2014-06-23 19:07:29 -0700706 *pVolumeInc = 0;
707 *pPrevVolume = newVolume;
Andy Hung5866a3b2014-05-29 21:33:13 -0700708 }
Andy Hung5e58b0a2014-06-23 19:07:29 -0700709 *pSetVolume = newVolume;
710
711 /* set the legacy integer volume variables */
712 int32_t intVolume = newVolume * AudioMixer::UNITY_GAIN_INT;
713 if (intVolume > AudioMixer::UNITY_GAIN_INT) {
714 intVolume = AudioMixer::UNITY_GAIN_INT;
715 } else if (intVolume < 0) {
716 ALOGE("negative volume %.7g", newVolume);
717 intVolume = 0; // should never happen, but for safety check.
718 }
719 if (intVolume == *pIntSetVolume) {
720 *pIntVolumeInc = 0;
721 /* TODO: integer/float workaround: ignore floating volume ramp */
722 *pVolumeInc = 0;
723 *pPrevVolume = newVolume;
724 return true;
725 }
726 if (ramp != 0) {
727 *pIntVolumeInc = ((intVolume - *pIntSetVolume) << 16) / ramp;
728 *pIntPrevVolume = (*pIntVolumeInc == 0 ? intVolume : *pIntSetVolume) << 16;
729 } else {
730 *pIntVolumeInc = 0;
731 *pIntPrevVolume = intVolume << 16;
732 }
733 *pIntSetVolume = intVolume;
Andy Hung5866a3b2014-05-29 21:33:13 -0700734 return true;
735}
736
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800737void AudioMixer::setParameter(int name, int target, int param, void *value)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700738{
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800739 name -= TRACK0;
Glenn Kasten5798d4e2012-03-08 12:18:35 -0800740 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800741 track_t& track = mState.tracks[name];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700742
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000743 int valueInt = static_cast<int>(reinterpret_cast<uintptr_t>(value));
744 int32_t *valueBuf = reinterpret_cast<int32_t*>(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700745
746 switch (target) {
Glenn Kasten788040c2011-05-05 08:19:00 -0700747
Mathias Agopian65ab4712010-07-14 17:59:35 -0700748 case TRACK:
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800749 switch (param) {
Glenn Kasten788040c2011-05-05 08:19:00 -0700750 case CHANNEL_MASK: {
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000751 audio_channel_mask_t mask =
752 static_cast<audio_channel_mask_t>(reinterpret_cast<uintptr_t>(value));
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800753 if (track.channelMask != mask) {
Andy Hunge5412692014-05-16 11:25:07 -0700754 uint32_t channelCount = audio_channel_count_from_out_mask(mask);
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700755 ALOG_ASSERT((channelCount <= MAX_NUM_CHANNELS_TO_DOWNMIX) && channelCount);
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800756 track.channelMask = mask;
757 track.channelCount = channelCount;
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700758 // the mask has changed, does this track need a downmixer?
Andy Hung296b7412014-06-17 15:25:47 -0700759 // update to try using our desired format (if we aren't already using it)
760 track.mMixerInFormat = kUseFloat && kUseNewMixer
761 ? AUDIO_FORMAT_PCM_FLOAT : AUDIO_FORMAT_PCM_16_BIT;
762 status_t status = initTrackDownmix(&mState.tracks[name], name, mask);
763 ALOGE_IF(status != OK,
764 "Invalid channel mask %#x, initTrackDownmix returned %d",
765 mask, status);
Glenn Kasten788040c2011-05-05 08:19:00 -0700766 ALOGV("setParameter(TRACK, CHANNEL_MASK, %x)", mask);
Andy Hung296b7412014-06-17 15:25:47 -0700767 prepareTrackForReformat(&track, name); // format may have changed
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800768 invalidateState(1 << name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700769 }
Glenn Kasten788040c2011-05-05 08:19:00 -0700770 } break;
771 case MAIN_BUFFER:
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800772 if (track.mainBuffer != valueBuf) {
773 track.mainBuffer = valueBuf;
Steve Block3856b092011-10-20 11:56:00 +0100774 ALOGV("setParameter(TRACK, MAIN_BUFFER, %p)", valueBuf);
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800775 invalidateState(1 << name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700776 }
Glenn Kasten788040c2011-05-05 08:19:00 -0700777 break;
778 case AUX_BUFFER:
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800779 if (track.auxBuffer != valueBuf) {
780 track.auxBuffer = valueBuf;
Steve Block3856b092011-10-20 11:56:00 +0100781 ALOGV("setParameter(TRACK, AUX_BUFFER, %p)", valueBuf);
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800782 invalidateState(1 << name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700783 }
Glenn Kasten788040c2011-05-05 08:19:00 -0700784 break;
Andy Hungef7c7fb2014-05-12 16:51:41 -0700785 case FORMAT: {
786 audio_format_t format = static_cast<audio_format_t>(valueInt);
787 if (track.mFormat != format) {
788 ALOG_ASSERT(audio_is_linear_pcm(format), "Invalid format %#x", format);
789 track.mFormat = format;
790 ALOGV("setParameter(TRACK, FORMAT, %#x)", format);
Andy Hung296b7412014-06-17 15:25:47 -0700791 prepareTrackForReformat(&track, name);
Andy Hungef7c7fb2014-05-12 16:51:41 -0700792 invalidateState(1 << name);
793 }
794 } break;
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700795 // FIXME do we want to support setting the downmix type from AudioFlinger?
796 // for a specific track? or per mixer?
797 /* case DOWNMIX_TYPE:
798 break */
Andy Hung78820702014-02-28 16:23:02 -0800799 case MIXER_FORMAT: {
Andy Hunga1ab7cc2014-02-24 19:26:52 -0800800 audio_format_t format = static_cast<audio_format_t>(valueInt);
Andy Hung78820702014-02-28 16:23:02 -0800801 if (track.mMixerFormat != format) {
802 track.mMixerFormat = format;
803 ALOGV("setParameter(TRACK, MIXER_FORMAT, %#x)", format);
Andy Hunga1ab7cc2014-02-24 19:26:52 -0800804 }
805 } break;
Glenn Kasten788040c2011-05-05 08:19:00 -0700806 default:
Glenn Kastenadad3d72014-02-21 14:51:43 -0800807 LOG_ALWAYS_FATAL("setParameter track: bad param %d", param);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700808 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700809 break;
Glenn Kasten788040c2011-05-05 08:19:00 -0700810
Mathias Agopian65ab4712010-07-14 17:59:35 -0700811 case RESAMPLE:
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800812 switch (param) {
813 case SAMPLE_RATE:
Glenn Kasten5798d4e2012-03-08 12:18:35 -0800814 ALOG_ASSERT(valueInt > 0, "bad sample rate %d", valueInt);
Glenn Kasten788040c2011-05-05 08:19:00 -0700815 if (track.setResampler(uint32_t(valueInt), mSampleRate)) {
816 ALOGV("setParameter(RESAMPLE, SAMPLE_RATE, %u)",
817 uint32_t(valueInt));
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800818 invalidateState(1 << name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700819 }
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800820 break;
821 case RESET:
Eric Laurent243f5f92011-02-28 16:52:51 -0800822 track.resetResampler();
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800823 invalidateState(1 << name);
824 break;
Glenn Kasten4e2293f2012-04-12 09:39:07 -0700825 case REMOVE:
826 delete track.resampler;
827 track.resampler = NULL;
828 track.sampleRate = mSampleRate;
829 invalidateState(1 << name);
830 break;
Glenn Kasten788040c2011-05-05 08:19:00 -0700831 default:
Glenn Kastenadad3d72014-02-21 14:51:43 -0800832 LOG_ALWAYS_FATAL("setParameter resample: bad param %d", param);
Eric Laurent243f5f92011-02-28 16:52:51 -0800833 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700834 break;
Glenn Kasten788040c2011-05-05 08:19:00 -0700835
Mathias Agopian65ab4712010-07-14 17:59:35 -0700836 case RAMP_VOLUME:
837 case VOLUME:
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800838 switch (param) {
Glenn Kasten788040c2011-05-05 08:19:00 -0700839 case VOLUME0:
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800840 case VOLUME1:
Andy Hung6be49402014-05-30 10:42:03 -0700841 if (setVolumeRampVariables(*reinterpret_cast<float*>(value),
Andy Hung5866a3b2014-05-29 21:33:13 -0700842 target == RAMP_VOLUME ? mState.frameCount : 0,
Andy Hung5e58b0a2014-06-23 19:07:29 -0700843 &track.volume[param - VOLUME0], &track.prevVolume[param - VOLUME0],
844 &track.volumeInc[param - VOLUME0],
845 &track.mVolume[param - VOLUME0], &track.mPrevVolume[param - VOLUME0],
846 &track.mVolumeInc[param - VOLUME0])) {
Andy Hung5866a3b2014-05-29 21:33:13 -0700847 ALOGV("setParameter(%s, VOLUME%d: %04x)",
Andy Hung6be49402014-05-30 10:42:03 -0700848 target == VOLUME ? "VOLUME" : "RAMP_VOLUME", param - VOLUME0,
849 track.volume[param - VOLUME0]);
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800850 invalidateState(1 << name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700851 }
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800852 break;
853 case AUXLEVEL:
Andy Hung6be49402014-05-30 10:42:03 -0700854 if (setVolumeRampVariables(*reinterpret_cast<float*>(value),
Andy Hung5866a3b2014-05-29 21:33:13 -0700855 target == RAMP_VOLUME ? mState.frameCount : 0,
Andy Hung5e58b0a2014-06-23 19:07:29 -0700856 &track.auxLevel, &track.prevAuxLevel, &track.auxInc,
857 &track.mAuxLevel, &track.mPrevAuxLevel, &track.mAuxInc)) {
Andy Hung5866a3b2014-05-29 21:33:13 -0700858 ALOGV("setParameter(%s, AUXLEVEL: %04x)",
Andy Hung6be49402014-05-30 10:42:03 -0700859 target == VOLUME ? "VOLUME" : "RAMP_VOLUME", track.auxLevel);
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800860 invalidateState(1 << name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700861 }
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800862 break;
Glenn Kasten788040c2011-05-05 08:19:00 -0700863 default:
Glenn Kastenadad3d72014-02-21 14:51:43 -0800864 LOG_ALWAYS_FATAL("setParameter volume: bad param %d", param);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700865 }
866 break;
Glenn Kasten788040c2011-05-05 08:19:00 -0700867
868 default:
Glenn Kastenadad3d72014-02-21 14:51:43 -0800869 LOG_ALWAYS_FATAL("setParameter: bad target %d", target);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700870 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700871}
872
873bool AudioMixer::track_t::setResampler(uint32_t value, uint32_t devSampleRate)
874{
Glenn Kasten4e2293f2012-04-12 09:39:07 -0700875 if (value != devSampleRate || resampler != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700876 if (sampleRate != value) {
877 sampleRate = value;
Glenn Kastene0feee32011-12-13 11:53:26 -0800878 if (resampler == NULL) {
Glenn Kastenac602052012-10-01 14:04:31 -0700879 ALOGV("creating resampler from track %d Hz to device %d Hz", value, devSampleRate);
880 AudioResampler::src_quality quality;
881 // force lowest quality level resampler if use case isn't music or video
882 // FIXME this is flawed for dynamic sample rates, as we choose the resampler
883 // quality level based on the initial ratio, but that could change later.
884 // Should have a way to distinguish tracks with static ratios vs. dynamic ratios.
885 if (!((value == 44100 && devSampleRate == 48000) ||
886 (value == 48000 && devSampleRate == 44100))) {
Andy Hung9e0308c2014-01-30 14:32:31 -0800887 quality = AudioResampler::DYN_LOW_QUALITY;
Glenn Kastenac602052012-10-01 14:04:31 -0700888 } else {
889 quality = AudioResampler::DEFAULT_QUALITY;
890 }
Andy Hung296b7412014-06-17 15:25:47 -0700891
Andy Hung296b7412014-06-17 15:25:47 -0700892 ALOGVV("Creating resampler with %d bits\n", bits);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700893 resampler = AudioResampler::create(
Andy Hung3348e362014-07-07 10:21:44 -0700894 mMixerInFormat,
Jean-Michel Triviacb86cc2012-04-16 12:43:57 -0700895 // the resampler sees the number of channels after the downmixer, if any
Glenn Kastenf551e992013-08-19 18:45:42 -0700896 (int) (downmixerBufferProvider != NULL ? MAX_NUM_CHANNELS : channelCount),
Glenn Kastenac602052012-10-01 14:04:31 -0700897 devSampleRate, quality);
Glenn Kasten52008f82012-03-18 09:34:41 -0700898 resampler->setLocalTimeFreq(sLocalTimeFreq);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700899 }
900 return true;
901 }
902 }
903 return false;
904}
905
Andy Hung5e58b0a2014-06-23 19:07:29 -0700906/* Checks to see if the volume ramp has completed and clears the increment
907 * variables appropriately.
908 *
909 * FIXME: There is code to handle int/float ramp variable switchover should it not
910 * complete within a mixer buffer processing call, but it is preferred to avoid switchover
911 * due to precision issues. The switchover code is included for legacy code purposes
912 * and can be removed once the integer volume is removed.
913 *
914 * It is not sufficient to clear only the volumeInc integer variable because
915 * if one channel requires ramping, all channels are ramped.
916 *
917 * There is a bit of duplicated code here, but it keeps backward compatibility.
918 */
919inline void AudioMixer::track_t::adjustVolumeRamp(bool aux, bool useFloat)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700920{
Andy Hung5e58b0a2014-06-23 19:07:29 -0700921 if (useFloat) {
922 for (uint32_t i=0 ; i<MAX_NUM_CHANNELS ; i++) {
923 if (mVolumeInc[i] != 0 && fabs(mVolume[i] - mPrevVolume[i]) <= fabs(mVolumeInc[i])) {
924 volumeInc[i] = 0;
925 prevVolume[i] = volume[i] << 16;
926 mVolumeInc[i] = 0.;
927 mPrevVolume[i] = mVolume[i];
928
929 } else {
930 //ALOGV("ramp: %f %f %f", mVolume[i], mPrevVolume[i], mVolumeInc[i]);
931 prevVolume[i] = u4_28_from_float(mPrevVolume[i]);
932 }
933 }
934 } else {
935 for (uint32_t i=0 ; i<MAX_NUM_CHANNELS ; i++) {
936 if (((volumeInc[i]>0) && (((prevVolume[i]+volumeInc[i])>>16) >= volume[i])) ||
937 ((volumeInc[i]<0) && (((prevVolume[i]+volumeInc[i])>>16) <= volume[i]))) {
938 volumeInc[i] = 0;
939 prevVolume[i] = volume[i] << 16;
940 mVolumeInc[i] = 0.;
941 mPrevVolume[i] = mVolume[i];
942 } else {
943 //ALOGV("ramp: %d %d %d", volume[i] << 16, prevVolume[i], volumeInc[i]);
944 mPrevVolume[i] = float_from_u4_28(prevVolume[i]);
945 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700946 }
947 }
Andy Hung5e58b0a2014-06-23 19:07:29 -0700948 /* TODO: aux is always integer regardless of output buffer type */
Mathias Agopian65ab4712010-07-14 17:59:35 -0700949 if (aux) {
950 if (((auxInc>0) && (((prevAuxLevel+auxInc)>>16) >= auxLevel)) ||
Andy Hung5e58b0a2014-06-23 19:07:29 -0700951 ((auxInc<0) && (((prevAuxLevel+auxInc)>>16) <= auxLevel))) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700952 auxInc = 0;
Andy Hung5e58b0a2014-06-23 19:07:29 -0700953 prevAuxLevel = auxLevel << 16;
954 mAuxInc = 0.;
955 mPrevAuxLevel = mAuxLevel;
956 } else {
957 //ALOGV("aux ramp: %d %d %d", auxLevel << 16, prevAuxLevel, auxInc);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700958 }
959 }
960}
961
Glenn Kastenc59c0042012-02-02 14:06:11 -0800962size_t AudioMixer::getUnreleasedFrames(int name) const
Eric Laurent071ccd52011-12-22 16:08:41 -0800963{
964 name -= TRACK0;
965 if (uint32_t(name) < MAX_NUM_TRACKS) {
Glenn Kastenc59c0042012-02-02 14:06:11 -0800966 return mState.tracks[name].getUnreleasedFrames();
Eric Laurent071ccd52011-12-22 16:08:41 -0800967 }
968 return 0;
969}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700970
Glenn Kasten01c4ebf2012-02-22 10:47:35 -0800971void AudioMixer::setBufferProvider(int name, AudioBufferProvider* bufferProvider)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700972{
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800973 name -= TRACK0;
Glenn Kasten5798d4e2012-03-08 12:18:35 -0800974 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700975
Andy Hung1d26ddf2014-05-29 15:53:09 -0700976 if (mState.tracks[name].mInputBufferProvider == bufferProvider) {
977 return; // don't reset any buffer providers if identical.
978 }
Andy Hungef7c7fb2014-05-12 16:51:41 -0700979 if (mState.tracks[name].mReformatBufferProvider != NULL) {
980 mState.tracks[name].mReformatBufferProvider->reset();
981 } else if (mState.tracks[name].downmixerBufferProvider != NULL) {
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700982 }
Andy Hungef7c7fb2014-05-12 16:51:41 -0700983
984 mState.tracks[name].mInputBufferProvider = bufferProvider;
985 reconfigureBufferProviders(&mState.tracks[name]);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700986}
987
988
John Grossman4ff14ba2012-02-08 16:37:41 -0800989void AudioMixer::process(int64_t pts)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700990{
John Grossman4ff14ba2012-02-08 16:37:41 -0800991 mState.hook(&mState, pts);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700992}
993
994
John Grossman4ff14ba2012-02-08 16:37:41 -0800995void AudioMixer::process__validate(state_t* state, int64_t pts)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700996{
Steve Block5ff1dd52012-01-05 23:22:43 +0000997 ALOGW_IF(!state->needsChanged,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700998 "in process__validate() but nothing's invalid");
999
1000 uint32_t changed = state->needsChanged;
1001 state->needsChanged = 0; // clear the validation flag
1002
1003 // recompute which tracks are enabled / disabled
1004 uint32_t enabled = 0;
1005 uint32_t disabled = 0;
1006 while (changed) {
1007 const int i = 31 - __builtin_clz(changed);
1008 const uint32_t mask = 1<<i;
1009 changed &= ~mask;
1010 track_t& t = state->tracks[i];
1011 (t.enabled ? enabled : disabled) |= mask;
1012 }
1013 state->enabledTracks &= ~disabled;
1014 state->enabledTracks |= enabled;
1015
1016 // compute everything we need...
1017 int countActiveTracks = 0;
Glenn Kasten4c340c62012-01-27 12:33:54 -08001018 bool all16BitsStereoNoResample = true;
1019 bool resampling = false;
1020 bool volumeRamp = false;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001021 uint32_t en = state->enabledTracks;
1022 while (en) {
1023 const int i = 31 - __builtin_clz(en);
1024 en &= ~(1<<i);
1025
1026 countActiveTracks++;
1027 track_t& t = state->tracks[i];
1028 uint32_t n = 0;
Glenn Kastend6fadf02013-10-30 14:37:29 -07001029 // FIXME can overflow (mask is only 3 bits)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001030 n |= NEEDS_CHANNEL_1 + t.channelCount - 1;
Glenn Kastend6fadf02013-10-30 14:37:29 -07001031 if (t.doesResample()) {
1032 n |= NEEDS_RESAMPLE;
1033 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001034 if (t.auxLevel != 0 && t.auxBuffer != NULL) {
Glenn Kastend6fadf02013-10-30 14:37:29 -07001035 n |= NEEDS_AUX;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001036 }
1037
1038 if (t.volumeInc[0]|t.volumeInc[1]) {
Glenn Kasten4c340c62012-01-27 12:33:54 -08001039 volumeRamp = true;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001040 } else if (!t.doesResample() && t.volumeRL == 0) {
Glenn Kastend6fadf02013-10-30 14:37:29 -07001041 n |= NEEDS_MUTE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001042 }
1043 t.needs = n;
1044
Glenn Kastend6fadf02013-10-30 14:37:29 -07001045 if (n & NEEDS_MUTE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001046 t.hook = track__nop;
1047 } else {
Glenn Kastend6fadf02013-10-30 14:37:29 -07001048 if (n & NEEDS_AUX) {
Glenn Kasten4c340c62012-01-27 12:33:54 -08001049 all16BitsStereoNoResample = false;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001050 }
Glenn Kastend6fadf02013-10-30 14:37:29 -07001051 if (n & NEEDS_RESAMPLE) {
Glenn Kasten4c340c62012-01-27 12:33:54 -08001052 all16BitsStereoNoResample = false;
1053 resampling = true;
Andy Hung296b7412014-06-17 15:25:47 -07001054 t.hook = getTrackHook(TRACKTYPE_RESAMPLE, FCC_2,
1055 t.mMixerInFormat, t.mMixerFormat);
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -07001056 ALOGV_IF((n & NEEDS_CHANNEL_COUNT__MASK) > NEEDS_CHANNEL_2,
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -07001057 "Track %d needs downmix + resample", i);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001058 } else {
1059 if ((n & NEEDS_CHANNEL_COUNT__MASK) == NEEDS_CHANNEL_1){
Andy Hung296b7412014-06-17 15:25:47 -07001060 t.hook = getTrackHook(TRACKTYPE_NORESAMPLEMONO, FCC_2,
1061 t.mMixerInFormat, t.mMixerFormat);
Glenn Kasten4c340c62012-01-27 12:33:54 -08001062 all16BitsStereoNoResample = false;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001063 }
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -07001064 if ((n & NEEDS_CHANNEL_COUNT__MASK) >= NEEDS_CHANNEL_2){
Andy Hung296b7412014-06-17 15:25:47 -07001065 t.hook = getTrackHook(TRACKTYPE_NORESAMPLE, FCC_2,
1066 t.mMixerInFormat, t.mMixerFormat);
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -07001067 ALOGV_IF((n & NEEDS_CHANNEL_COUNT__MASK) > NEEDS_CHANNEL_2,
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -07001068 "Track %d needs downmix", i);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001069 }
1070 }
1071 }
1072 }
1073
1074 // select the processing hooks
1075 state->hook = process__nop;
Glenn Kasten34fca342013-08-13 09:48:14 -07001076 if (countActiveTracks > 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001077 if (resampling) {
1078 if (!state->outputTemp) {
1079 state->outputTemp = new int32_t[MAX_NUM_CHANNELS * state->frameCount];
1080 }
1081 if (!state->resampleTemp) {
1082 state->resampleTemp = new int32_t[MAX_NUM_CHANNELS * state->frameCount];
1083 }
1084 state->hook = process__genericResampling;
1085 } else {
1086 if (state->outputTemp) {
1087 delete [] state->outputTemp;
Glenn Kastene0feee32011-12-13 11:53:26 -08001088 state->outputTemp = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001089 }
1090 if (state->resampleTemp) {
1091 delete [] state->resampleTemp;
Glenn Kastene0feee32011-12-13 11:53:26 -08001092 state->resampleTemp = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001093 }
1094 state->hook = process__genericNoResampling;
1095 if (all16BitsStereoNoResample && !volumeRamp) {
1096 if (countActiveTracks == 1) {
Andy Hung296b7412014-06-17 15:25:47 -07001097 const int i = 31 - __builtin_clz(state->enabledTracks);
1098 track_t& t = state->tracks[i];
1099 state->hook = getProcessHook(PROCESSTYPE_NORESAMPLEONETRACK, FCC_2,
1100 t.mMixerInFormat, t.mMixerFormat);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001101 }
1102 }
1103 }
1104 }
1105
Steve Block3856b092011-10-20 11:56:00 +01001106 ALOGV("mixer configuration change: %d activeTracks (%08x) "
Mathias Agopian65ab4712010-07-14 17:59:35 -07001107 "all16BitsStereoNoResample=%d, resampling=%d, volumeRamp=%d",
1108 countActiveTracks, state->enabledTracks,
1109 all16BitsStereoNoResample, resampling, volumeRamp);
1110
John Grossman4ff14ba2012-02-08 16:37:41 -08001111 state->hook(state, pts);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001112
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -08001113 // Now that the volume ramp has been done, set optimal state and
1114 // track hooks for subsequent mixer process
Glenn Kasten34fca342013-08-13 09:48:14 -07001115 if (countActiveTracks > 0) {
Glenn Kasten4c340c62012-01-27 12:33:54 -08001116 bool allMuted = true;
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -08001117 uint32_t en = state->enabledTracks;
1118 while (en) {
1119 const int i = 31 - __builtin_clz(en);
1120 en &= ~(1<<i);
1121 track_t& t = state->tracks[i];
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001122 if (!t.doesResample() && t.volumeRL == 0) {
Glenn Kastend6fadf02013-10-30 14:37:29 -07001123 t.needs |= NEEDS_MUTE;
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -08001124 t.hook = track__nop;
1125 } else {
Glenn Kasten4c340c62012-01-27 12:33:54 -08001126 allMuted = false;
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -08001127 }
1128 }
1129 if (allMuted) {
1130 state->hook = process__nop;
1131 } else if (all16BitsStereoNoResample) {
1132 if (countActiveTracks == 1) {
1133 state->hook = process__OneTrack16BitsStereoNoResampling;
1134 }
1135 }
1136 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001137}
1138
Mathias Agopian65ab4712010-07-14 17:59:35 -07001139
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001140void AudioMixer::track__genericResample(track_t* t, int32_t* out, size_t outFrameCount,
1141 int32_t* temp, int32_t* aux)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001142{
Andy Hung296b7412014-06-17 15:25:47 -07001143 ALOGVV("track__genericResample\n");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001144 t->resampler->setSampleRate(t->sampleRate);
1145
1146 // ramp gain - resample to temp buffer and scale/mix in 2nd step
1147 if (aux != NULL) {
1148 // always resample with unity gain when sending to auxiliary buffer to be able
1149 // to apply send level after resampling
1150 // TODO: modify each resampler to support aux channel?
Andy Hung5e58b0a2014-06-23 19:07:29 -07001151 t->resampler->setVolume(UNITY_GAIN_FLOAT, UNITY_GAIN_FLOAT);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001152 memset(temp, 0, outFrameCount * MAX_NUM_CHANNELS * sizeof(int32_t));
1153 t->resampler->resample(temp, outFrameCount, t->bufferProvider);
Glenn Kastenf6b16782011-12-15 09:51:17 -08001154 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001155 volumeRampStereo(t, out, outFrameCount, temp, aux);
1156 } else {
1157 volumeStereo(t, out, outFrameCount, temp, aux);
1158 }
1159 } else {
Glenn Kastenf6b16782011-12-15 09:51:17 -08001160 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) {
Andy Hung5e58b0a2014-06-23 19:07:29 -07001161 t->resampler->setVolume(UNITY_GAIN_FLOAT, UNITY_GAIN_FLOAT);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001162 memset(temp, 0, outFrameCount * MAX_NUM_CHANNELS * sizeof(int32_t));
1163 t->resampler->resample(temp, outFrameCount, t->bufferProvider);
1164 volumeRampStereo(t, out, outFrameCount, temp, aux);
1165 }
1166
1167 // constant gain
1168 else {
Andy Hung5e58b0a2014-06-23 19:07:29 -07001169 t->resampler->setVolume(t->mVolume[0], t->mVolume[1]);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001170 t->resampler->resample(out, outFrameCount, t->bufferProvider);
1171 }
1172 }
1173}
1174
Andy Hungee931ff2014-01-28 13:44:14 -08001175void AudioMixer::track__nop(track_t* t __unused, int32_t* out __unused,
1176 size_t outFrameCount __unused, int32_t* temp __unused, int32_t* aux __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001177{
1178}
1179
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001180void AudioMixer::volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp,
1181 int32_t* aux)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001182{
1183 int32_t vl = t->prevVolume[0];
1184 int32_t vr = t->prevVolume[1];
1185 const int32_t vlInc = t->volumeInc[0];
1186 const int32_t vrInc = t->volumeInc[1];
1187
Steve Blockb8a80522011-12-20 16:23:08 +00001188 //ALOGD("[0] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
Mathias Agopian65ab4712010-07-14 17:59:35 -07001189 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
1190 // (vl + vlInc*frameCount)/65536.0f, frameCount);
1191
1192 // ramp volume
Glenn Kastenf6b16782011-12-15 09:51:17 -08001193 if (CC_UNLIKELY(aux != NULL)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001194 int32_t va = t->prevAuxLevel;
1195 const int32_t vaInc = t->auxInc;
1196 int32_t l;
1197 int32_t r;
1198
1199 do {
1200 l = (*temp++ >> 12);
1201 r = (*temp++ >> 12);
1202 *out++ += (vl >> 16) * l;
1203 *out++ += (vr >> 16) * r;
1204 *aux++ += (va >> 17) * (l + r);
1205 vl += vlInc;
1206 vr += vrInc;
1207 va += vaInc;
1208 } while (--frameCount);
1209 t->prevAuxLevel = va;
1210 } else {
1211 do {
1212 *out++ += (vl >> 16) * (*temp++ >> 12);
1213 *out++ += (vr >> 16) * (*temp++ >> 12);
1214 vl += vlInc;
1215 vr += vrInc;
1216 } while (--frameCount);
1217 }
1218 t->prevVolume[0] = vl;
1219 t->prevVolume[1] = vr;
Glenn Kastena1117922012-01-26 10:53:32 -08001220 t->adjustVolumeRamp(aux != NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001221}
1222
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001223void AudioMixer::volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp,
1224 int32_t* aux)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001225{
1226 const int16_t vl = t->volume[0];
1227 const int16_t vr = t->volume[1];
1228
Glenn Kastenf6b16782011-12-15 09:51:17 -08001229 if (CC_UNLIKELY(aux != NULL)) {
Glenn Kasten3b81aca2012-01-27 15:26:23 -08001230 const int16_t va = t->auxLevel;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001231 do {
1232 int16_t l = (int16_t)(*temp++ >> 12);
1233 int16_t r = (int16_t)(*temp++ >> 12);
1234 out[0] = mulAdd(l, vl, out[0]);
1235 int16_t a = (int16_t)(((int32_t)l + r) >> 1);
1236 out[1] = mulAdd(r, vr, out[1]);
1237 out += 2;
1238 aux[0] = mulAdd(a, va, aux[0]);
1239 aux++;
1240 } while (--frameCount);
1241 } else {
1242 do {
1243 int16_t l = (int16_t)(*temp++ >> 12);
1244 int16_t r = (int16_t)(*temp++ >> 12);
1245 out[0] = mulAdd(l, vl, out[0]);
1246 out[1] = mulAdd(r, vr, out[1]);
1247 out += 2;
1248 } while (--frameCount);
1249 }
1250}
1251
Andy Hungee931ff2014-01-28 13:44:14 -08001252void AudioMixer::track__16BitsStereo(track_t* t, int32_t* out, size_t frameCount,
1253 int32_t* temp __unused, int32_t* aux)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001254{
Andy Hung296b7412014-06-17 15:25:47 -07001255 ALOGVV("track__16BitsStereo\n");
Glenn Kasten54c3b662012-01-06 07:46:30 -08001256 const int16_t *in = static_cast<const int16_t *>(t->in);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001257
Glenn Kastenf6b16782011-12-15 09:51:17 -08001258 if (CC_UNLIKELY(aux != NULL)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001259 int32_t l;
1260 int32_t r;
1261 // ramp gain
Glenn Kastenf6b16782011-12-15 09:51:17 -08001262 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001263 int32_t vl = t->prevVolume[0];
1264 int32_t vr = t->prevVolume[1];
1265 int32_t va = t->prevAuxLevel;
1266 const int32_t vlInc = t->volumeInc[0];
1267 const int32_t vrInc = t->volumeInc[1];
1268 const int32_t vaInc = t->auxInc;
Steve Blockb8a80522011-12-20 16:23:08 +00001269 // ALOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
Mathias Agopian65ab4712010-07-14 17:59:35 -07001270 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
1271 // (vl + vlInc*frameCount)/65536.0f, frameCount);
1272
1273 do {
1274 l = (int32_t)*in++;
1275 r = (int32_t)*in++;
1276 *out++ += (vl >> 16) * l;
1277 *out++ += (vr >> 16) * r;
1278 *aux++ += (va >> 17) * (l + r);
1279 vl += vlInc;
1280 vr += vrInc;
1281 va += vaInc;
1282 } while (--frameCount);
1283
1284 t->prevVolume[0] = vl;
1285 t->prevVolume[1] = vr;
1286 t->prevAuxLevel = va;
1287 t->adjustVolumeRamp(true);
1288 }
1289
1290 // constant gain
1291 else {
1292 const uint32_t vrl = t->volumeRL;
1293 const int16_t va = (int16_t)t->auxLevel;
1294 do {
Glenn Kasten54c3b662012-01-06 07:46:30 -08001295 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001296 int16_t a = (int16_t)(((int32_t)in[0] + in[1]) >> 1);
1297 in += 2;
1298 out[0] = mulAddRL(1, rl, vrl, out[0]);
1299 out[1] = mulAddRL(0, rl, vrl, out[1]);
1300 out += 2;
1301 aux[0] = mulAdd(a, va, aux[0]);
1302 aux++;
1303 } while (--frameCount);
1304 }
1305 } else {
1306 // ramp gain
Glenn Kastenf6b16782011-12-15 09:51:17 -08001307 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001308 int32_t vl = t->prevVolume[0];
1309 int32_t vr = t->prevVolume[1];
1310 const int32_t vlInc = t->volumeInc[0];
1311 const int32_t vrInc = t->volumeInc[1];
1312
Steve Blockb8a80522011-12-20 16:23:08 +00001313 // ALOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
Mathias Agopian65ab4712010-07-14 17:59:35 -07001314 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
1315 // (vl + vlInc*frameCount)/65536.0f, frameCount);
1316
1317 do {
1318 *out++ += (vl >> 16) * (int32_t) *in++;
1319 *out++ += (vr >> 16) * (int32_t) *in++;
1320 vl += vlInc;
1321 vr += vrInc;
1322 } while (--frameCount);
1323
1324 t->prevVolume[0] = vl;
1325 t->prevVolume[1] = vr;
1326 t->adjustVolumeRamp(false);
1327 }
1328
1329 // constant gain
1330 else {
1331 const uint32_t vrl = t->volumeRL;
1332 do {
Glenn Kasten54c3b662012-01-06 07:46:30 -08001333 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001334 in += 2;
1335 out[0] = mulAddRL(1, rl, vrl, out[0]);
1336 out[1] = mulAddRL(0, rl, vrl, out[1]);
1337 out += 2;
1338 } while (--frameCount);
1339 }
1340 }
1341 t->in = in;
1342}
1343
Andy Hungee931ff2014-01-28 13:44:14 -08001344void AudioMixer::track__16BitsMono(track_t* t, int32_t* out, size_t frameCount,
1345 int32_t* temp __unused, int32_t* aux)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001346{
Andy Hung296b7412014-06-17 15:25:47 -07001347 ALOGVV("track__16BitsMono\n");
Glenn Kasten54c3b662012-01-06 07:46:30 -08001348 const int16_t *in = static_cast<int16_t const *>(t->in);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001349
Glenn Kastenf6b16782011-12-15 09:51:17 -08001350 if (CC_UNLIKELY(aux != NULL)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001351 // ramp gain
Glenn Kastenf6b16782011-12-15 09:51:17 -08001352 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001353 int32_t vl = t->prevVolume[0];
1354 int32_t vr = t->prevVolume[1];
1355 int32_t va = t->prevAuxLevel;
1356 const int32_t vlInc = t->volumeInc[0];
1357 const int32_t vrInc = t->volumeInc[1];
1358 const int32_t vaInc = t->auxInc;
1359
Steve Blockb8a80522011-12-20 16:23:08 +00001360 // ALOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
Mathias Agopian65ab4712010-07-14 17:59:35 -07001361 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
1362 // (vl + vlInc*frameCount)/65536.0f, frameCount);
1363
1364 do {
1365 int32_t l = *in++;
1366 *out++ += (vl >> 16) * l;
1367 *out++ += (vr >> 16) * l;
1368 *aux++ += (va >> 16) * l;
1369 vl += vlInc;
1370 vr += vrInc;
1371 va += vaInc;
1372 } while (--frameCount);
1373
1374 t->prevVolume[0] = vl;
1375 t->prevVolume[1] = vr;
1376 t->prevAuxLevel = va;
1377 t->adjustVolumeRamp(true);
1378 }
1379 // constant gain
1380 else {
1381 const int16_t vl = t->volume[0];
1382 const int16_t vr = t->volume[1];
1383 const int16_t va = (int16_t)t->auxLevel;
1384 do {
1385 int16_t l = *in++;
1386 out[0] = mulAdd(l, vl, out[0]);
1387 out[1] = mulAdd(l, vr, out[1]);
1388 out += 2;
1389 aux[0] = mulAdd(l, va, aux[0]);
1390 aux++;
1391 } while (--frameCount);
1392 }
1393 } else {
1394 // ramp gain
Glenn Kastenf6b16782011-12-15 09:51:17 -08001395 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001396 int32_t vl = t->prevVolume[0];
1397 int32_t vr = t->prevVolume[1];
1398 const int32_t vlInc = t->volumeInc[0];
1399 const int32_t vrInc = t->volumeInc[1];
1400
Steve Blockb8a80522011-12-20 16:23:08 +00001401 // ALOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
Mathias Agopian65ab4712010-07-14 17:59:35 -07001402 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
1403 // (vl + vlInc*frameCount)/65536.0f, frameCount);
1404
1405 do {
1406 int32_t l = *in++;
1407 *out++ += (vl >> 16) * l;
1408 *out++ += (vr >> 16) * l;
1409 vl += vlInc;
1410 vr += vrInc;
1411 } while (--frameCount);
1412
1413 t->prevVolume[0] = vl;
1414 t->prevVolume[1] = vr;
1415 t->adjustVolumeRamp(false);
1416 }
1417 // constant gain
1418 else {
1419 const int16_t vl = t->volume[0];
1420 const int16_t vr = t->volume[1];
1421 do {
1422 int16_t l = *in++;
1423 out[0] = mulAdd(l, vl, out[0]);
1424 out[1] = mulAdd(l, vr, out[1]);
1425 out += 2;
1426 } while (--frameCount);
1427 }
1428 }
1429 t->in = in;
1430}
1431
Mathias Agopian65ab4712010-07-14 17:59:35 -07001432// no-op case
John Grossman4ff14ba2012-02-08 16:37:41 -08001433void AudioMixer::process__nop(state_t* state, int64_t pts)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001434{
Andy Hung296b7412014-06-17 15:25:47 -07001435 ALOGVV("process__nop\n");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001436 uint32_t e0 = state->enabledTracks;
Andy Hunga1ab7cc2014-02-24 19:26:52 -08001437 size_t sampleCount = state->frameCount * MAX_NUM_CHANNELS;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001438 while (e0) {
1439 // process by group of tracks with same output buffer to
1440 // avoid multiple memset() on same buffer
1441 uint32_t e1 = e0, e2 = e0;
1442 int i = 31 - __builtin_clz(e1);
Glenn Kastenfc900c92013-02-18 12:47:49 -08001443 {
1444 track_t& t1 = state->tracks[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001445 e2 &= ~(1<<i);
Glenn Kastenfc900c92013-02-18 12:47:49 -08001446 while (e2) {
1447 i = 31 - __builtin_clz(e2);
1448 e2 &= ~(1<<i);
1449 track_t& t2 = state->tracks[i];
1450 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) {
1451 e1 &= ~(1<<i);
1452 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001453 }
Glenn Kastenfc900c92013-02-18 12:47:49 -08001454 e0 &= ~(e1);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001455
Andy Hunga1ab7cc2014-02-24 19:26:52 -08001456 memset(t1.mainBuffer, 0, sampleCount
Andy Hung78820702014-02-28 16:23:02 -08001457 * audio_bytes_per_sample(t1.mMixerFormat));
Glenn Kastenfc900c92013-02-18 12:47:49 -08001458 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001459
1460 while (e1) {
1461 i = 31 - __builtin_clz(e1);
1462 e1 &= ~(1<<i);
Glenn Kastenfc900c92013-02-18 12:47:49 -08001463 {
1464 track_t& t3 = state->tracks[i];
1465 size_t outFrames = state->frameCount;
1466 while (outFrames) {
1467 t3.buffer.frameCount = outFrames;
1468 int64_t outputPTS = calculateOutputPTS(
1469 t3, pts, state->frameCount - outFrames);
1470 t3.bufferProvider->getNextBuffer(&t3.buffer, outputPTS);
1471 if (t3.buffer.raw == NULL) break;
1472 outFrames -= t3.buffer.frameCount;
1473 t3.bufferProvider->releaseBuffer(&t3.buffer);
1474 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001475 }
1476 }
1477 }
1478}
1479
1480// generic code without resampling
John Grossman4ff14ba2012-02-08 16:37:41 -08001481void AudioMixer::process__genericNoResampling(state_t* state, int64_t pts)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001482{
Andy Hung296b7412014-06-17 15:25:47 -07001483 ALOGVV("process__genericNoResampling\n");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001484 int32_t outTemp[BLOCKSIZE * MAX_NUM_CHANNELS] __attribute__((aligned(32)));
1485
1486 // acquire each track's buffer
1487 uint32_t enabledTracks = state->enabledTracks;
1488 uint32_t e0 = enabledTracks;
1489 while (e0) {
1490 const int i = 31 - __builtin_clz(e0);
1491 e0 &= ~(1<<i);
1492 track_t& t = state->tracks[i];
1493 t.buffer.frameCount = state->frameCount;
John Grossman4ff14ba2012-02-08 16:37:41 -08001494 t.bufferProvider->getNextBuffer(&t.buffer, pts);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001495 t.frameCount = t.buffer.frameCount;
1496 t.in = t.buffer.raw;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001497 }
1498
1499 e0 = enabledTracks;
1500 while (e0) {
1501 // process by group of tracks with same output buffer to
1502 // optimize cache use
1503 uint32_t e1 = e0, e2 = e0;
1504 int j = 31 - __builtin_clz(e1);
1505 track_t& t1 = state->tracks[j];
1506 e2 &= ~(1<<j);
1507 while (e2) {
1508 j = 31 - __builtin_clz(e2);
1509 e2 &= ~(1<<j);
1510 track_t& t2 = state->tracks[j];
Glenn Kastenf6b16782011-12-15 09:51:17 -08001511 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001512 e1 &= ~(1<<j);
1513 }
1514 }
1515 e0 &= ~(e1);
1516 // this assumes output 16 bits stereo, no resampling
1517 int32_t *out = t1.mainBuffer;
1518 size_t numFrames = 0;
1519 do {
1520 memset(outTemp, 0, sizeof(outTemp));
1521 e2 = e1;
1522 while (e2) {
1523 const int i = 31 - __builtin_clz(e2);
1524 e2 &= ~(1<<i);
1525 track_t& t = state->tracks[i];
1526 size_t outFrames = BLOCKSIZE;
1527 int32_t *aux = NULL;
Glenn Kastend6fadf02013-10-30 14:37:29 -07001528 if (CC_UNLIKELY(t.needs & NEEDS_AUX)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001529 aux = t.auxBuffer + numFrames;
1530 }
1531 while (outFrames) {
Gaurav Kumar7e79cd22014-01-06 10:57:18 +05301532 // t.in == NULL can happen if the track was flushed just after having
1533 // been enabled for mixing.
1534 if (t.in == NULL) {
1535 enabledTracks &= ~(1<<i);
1536 e1 &= ~(1<<i);
1537 break;
1538 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001539 size_t inFrames = (t.frameCount > outFrames)?outFrames:t.frameCount;
Glenn Kasten34fca342013-08-13 09:48:14 -07001540 if (inFrames > 0) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001541 t.hook(&t, outTemp + (BLOCKSIZE-outFrames)*MAX_NUM_CHANNELS, inFrames,
1542 state->resampleTemp, aux);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001543 t.frameCount -= inFrames;
1544 outFrames -= inFrames;
Glenn Kastenf6b16782011-12-15 09:51:17 -08001545 if (CC_UNLIKELY(aux != NULL)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001546 aux += inFrames;
1547 }
1548 }
1549 if (t.frameCount == 0 && outFrames) {
1550 t.bufferProvider->releaseBuffer(&t.buffer);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001551 t.buffer.frameCount = (state->frameCount - numFrames) -
1552 (BLOCKSIZE - outFrames);
John Grossman4ff14ba2012-02-08 16:37:41 -08001553 int64_t outputPTS = calculateOutputPTS(
1554 t, pts, numFrames + (BLOCKSIZE - outFrames));
1555 t.bufferProvider->getNextBuffer(&t.buffer, outputPTS);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001556 t.in = t.buffer.raw;
1557 if (t.in == NULL) {
1558 enabledTracks &= ~(1<<i);
1559 e1 &= ~(1<<i);
1560 break;
1561 }
1562 t.frameCount = t.buffer.frameCount;
1563 }
1564 }
1565 }
Andy Hung296b7412014-06-17 15:25:47 -07001566
1567 convertMixerFormat(out, t1.mMixerFormat, outTemp, t1.mMixerInFormat,
1568 BLOCKSIZE * FCC_2);
1569 // TODO: fix ugly casting due to choice of out pointer type
1570 out = reinterpret_cast<int32_t*>((uint8_t*)out
1571 + BLOCKSIZE * FCC_2 * audio_bytes_per_sample(t1.mMixerFormat));
Mathias Agopian65ab4712010-07-14 17:59:35 -07001572 numFrames += BLOCKSIZE;
1573 } while (numFrames < state->frameCount);
1574 }
1575
1576 // release each track's buffer
1577 e0 = enabledTracks;
1578 while (e0) {
1579 const int i = 31 - __builtin_clz(e0);
1580 e0 &= ~(1<<i);
1581 track_t& t = state->tracks[i];
1582 t.bufferProvider->releaseBuffer(&t.buffer);
1583 }
1584}
1585
1586
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -08001587// generic code with resampling
John Grossman4ff14ba2012-02-08 16:37:41 -08001588void AudioMixer::process__genericResampling(state_t* state, int64_t pts)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001589{
Andy Hung296b7412014-06-17 15:25:47 -07001590 ALOGVV("process__genericResampling\n");
Glenn Kasten54c3b662012-01-06 07:46:30 -08001591 // this const just means that local variable outTemp doesn't change
Mathias Agopian65ab4712010-07-14 17:59:35 -07001592 int32_t* const outTemp = state->outputTemp;
1593 const size_t size = sizeof(int32_t) * MAX_NUM_CHANNELS * state->frameCount;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001594
1595 size_t numFrames = state->frameCount;
1596
1597 uint32_t e0 = state->enabledTracks;
1598 while (e0) {
1599 // process by group of tracks with same output buffer
1600 // to optimize cache use
1601 uint32_t e1 = e0, e2 = e0;
1602 int j = 31 - __builtin_clz(e1);
1603 track_t& t1 = state->tracks[j];
1604 e2 &= ~(1<<j);
1605 while (e2) {
1606 j = 31 - __builtin_clz(e2);
1607 e2 &= ~(1<<j);
1608 track_t& t2 = state->tracks[j];
Glenn Kastenf6b16782011-12-15 09:51:17 -08001609 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001610 e1 &= ~(1<<j);
1611 }
1612 }
1613 e0 &= ~(e1);
1614 int32_t *out = t1.mainBuffer;
Yuuhi Yamaguchi2151d7b2011-02-04 15:24:34 +01001615 memset(outTemp, 0, size);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001616 while (e1) {
1617 const int i = 31 - __builtin_clz(e1);
1618 e1 &= ~(1<<i);
1619 track_t& t = state->tracks[i];
1620 int32_t *aux = NULL;
Glenn Kastend6fadf02013-10-30 14:37:29 -07001621 if (CC_UNLIKELY(t.needs & NEEDS_AUX)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001622 aux = t.auxBuffer;
1623 }
1624
1625 // this is a little goofy, on the resampling case we don't
1626 // acquire/release the buffers because it's done by
1627 // the resampler.
Glenn Kastend6fadf02013-10-30 14:37:29 -07001628 if (t.needs & NEEDS_RESAMPLE) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001629 t.resampler->setPTS(pts);
Glenn Kastena1117922012-01-26 10:53:32 -08001630 t.hook(&t, outTemp, numFrames, state->resampleTemp, aux);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001631 } else {
1632
1633 size_t outFrames = 0;
1634
1635 while (outFrames < numFrames) {
1636 t.buffer.frameCount = numFrames - outFrames;
John Grossman4ff14ba2012-02-08 16:37:41 -08001637 int64_t outputPTS = calculateOutputPTS(t, pts, outFrames);
1638 t.bufferProvider->getNextBuffer(&t.buffer, outputPTS);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001639 t.in = t.buffer.raw;
1640 // t.in == NULL can happen if the track was flushed just after having
1641 // been enabled for mixing.
1642 if (t.in == NULL) break;
1643
Glenn Kastenf6b16782011-12-15 09:51:17 -08001644 if (CC_UNLIKELY(aux != NULL)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001645 aux += outFrames;
1646 }
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001647 t.hook(&t, outTemp + outFrames*MAX_NUM_CHANNELS, t.buffer.frameCount,
1648 state->resampleTemp, aux);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001649 outFrames += t.buffer.frameCount;
1650 t.bufferProvider->releaseBuffer(&t.buffer);
1651 }
1652 }
1653 }
Andy Hung296b7412014-06-17 15:25:47 -07001654 convertMixerFormat(out, t1.mMixerFormat, outTemp, t1.mMixerInFormat, numFrames * FCC_2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001655 }
1656}
1657
1658// one track, 16 bits stereo without resampling is the most common case
John Grossman4ff14ba2012-02-08 16:37:41 -08001659void AudioMixer::process__OneTrack16BitsStereoNoResampling(state_t* state,
1660 int64_t pts)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001661{
Andy Hung296b7412014-06-17 15:25:47 -07001662 ALOGVV("process__OneTrack16BitsStereoNoResampling\n");
Glenn Kasten99e53b82012-01-19 08:59:58 -08001663 // This method is only called when state->enabledTracks has exactly
1664 // one bit set. The asserts below would verify this, but are commented out
1665 // since the whole point of this method is to optimize performance.
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001666 //ALOG_ASSERT(0 != state->enabledTracks, "no tracks enabled");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001667 const int i = 31 - __builtin_clz(state->enabledTracks);
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001668 //ALOG_ASSERT((1 << i) == state->enabledTracks, "more than 1 track enabled");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001669 const track_t& t = state->tracks[i];
1670
1671 AudioBufferProvider::Buffer& b(t.buffer);
1672
1673 int32_t* out = t.mainBuffer;
Andy Hungf8a106a2014-05-29 18:52:38 -07001674 float *fout = reinterpret_cast<float*>(out);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001675 size_t numFrames = state->frameCount;
1676
1677 const int16_t vl = t.volume[0];
1678 const int16_t vr = t.volume[1];
1679 const uint32_t vrl = t.volumeRL;
1680 while (numFrames) {
1681 b.frameCount = numFrames;
John Grossman4ff14ba2012-02-08 16:37:41 -08001682 int64_t outputPTS = calculateOutputPTS(t, pts, out - t.mainBuffer);
1683 t.bufferProvider->getNextBuffer(&b, outputPTS);
Glenn Kasten54c3b662012-01-06 07:46:30 -08001684 const int16_t *in = b.i16;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001685
1686 // in == NULL can happen if the track was flushed just after having
1687 // been enabled for mixing.
Andy Hungf8a106a2014-05-29 18:52:38 -07001688 if (in == NULL || (((uintptr_t)in) & 3)) {
1689 memset(out, 0, numFrames
1690 * MAX_NUM_CHANNELS * audio_bytes_per_sample(t.mMixerFormat));
1691 ALOGE_IF((((uintptr_t)in) & 3), "process stereo track: input buffer alignment pb: "
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001692 "buffer %p track %d, channels %d, needs %08x",
Mathias Agopian65ab4712010-07-14 17:59:35 -07001693 in, i, t.channelCount, t.needs);
1694 return;
1695 }
1696 size_t outFrames = b.frameCount;
1697
Andy Hung78820702014-02-28 16:23:02 -08001698 switch (t.mMixerFormat) {
Andy Hungf8a106a2014-05-29 18:52:38 -07001699 case AUDIO_FORMAT_PCM_FLOAT:
Mathias Agopian65ab4712010-07-14 17:59:35 -07001700 do {
Glenn Kasten54c3b662012-01-06 07:46:30 -08001701 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001702 in += 2;
Andy Hunga1ab7cc2014-02-24 19:26:52 -08001703 int32_t l = mulRL(1, rl, vrl);
1704 int32_t r = mulRL(0, rl, vrl);
Andy Hung84a0c6e2014-04-02 11:24:53 -07001705 *fout++ = float_from_q4_27(l);
1706 *fout++ = float_from_q4_27(r);
Andy Hung3375bde2014-02-28 15:51:47 -08001707 // Note: In case of later int16_t sink output,
1708 // conversion and clamping is done by memcpy_to_i16_from_float().
Mathias Agopian65ab4712010-07-14 17:59:35 -07001709 } while (--outFrames);
Andy Hungf8a106a2014-05-29 18:52:38 -07001710 break;
Andy Hunga1ab7cc2014-02-24 19:26:52 -08001711 case AUDIO_FORMAT_PCM_16_BIT:
Andy Hung97ae8242014-05-30 10:35:47 -07001712 if (CC_UNLIKELY(uint32_t(vl) > UNITY_GAIN_INT || uint32_t(vr) > UNITY_GAIN_INT)) {
Andy Hunga1ab7cc2014-02-24 19:26:52 -08001713 // volume is boosted, so we might need to clamp even though
1714 // we process only one track.
1715 do {
1716 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
1717 in += 2;
1718 int32_t l = mulRL(1, rl, vrl) >> 12;
1719 int32_t r = mulRL(0, rl, vrl) >> 12;
1720 // clamping...
1721 l = clamp16(l);
1722 r = clamp16(r);
1723 *out++ = (r<<16) | (l & 0xFFFF);
1724 } while (--outFrames);
1725 } else {
1726 do {
1727 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
1728 in += 2;
1729 int32_t l = mulRL(1, rl, vrl) >> 12;
1730 int32_t r = mulRL(0, rl, vrl) >> 12;
1731 *out++ = (r<<16) | (l & 0xFFFF);
1732 } while (--outFrames);
1733 }
1734 break;
1735 default:
Andy Hung78820702014-02-28 16:23:02 -08001736 LOG_ALWAYS_FATAL("bad mixer format: %d", t.mMixerFormat);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001737 }
1738 numFrames -= b.frameCount;
1739 t.bufferProvider->releaseBuffer(&b);
1740 }
1741}
1742
Glenn Kasten81a028f2011-12-15 09:53:12 -08001743#if 0
Mathias Agopian65ab4712010-07-14 17:59:35 -07001744// 2 tracks is also a common case
1745// NEVER used in current implementation of process__validate()
1746// only use if the 2 tracks have the same output buffer
John Grossman4ff14ba2012-02-08 16:37:41 -08001747void AudioMixer::process__TwoTracks16BitsStereoNoResampling(state_t* state,
1748 int64_t pts)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001749{
1750 int i;
1751 uint32_t en = state->enabledTracks;
1752
1753 i = 31 - __builtin_clz(en);
1754 const track_t& t0 = state->tracks[i];
1755 AudioBufferProvider::Buffer& b0(t0.buffer);
1756
1757 en &= ~(1<<i);
1758 i = 31 - __builtin_clz(en);
1759 const track_t& t1 = state->tracks[i];
1760 AudioBufferProvider::Buffer& b1(t1.buffer);
1761
Glenn Kasten54c3b662012-01-06 07:46:30 -08001762 const int16_t *in0;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001763 const int16_t vl0 = t0.volume[0];
1764 const int16_t vr0 = t0.volume[1];
1765 size_t frameCount0 = 0;
1766
Glenn Kasten54c3b662012-01-06 07:46:30 -08001767 const int16_t *in1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001768 const int16_t vl1 = t1.volume[0];
1769 const int16_t vr1 = t1.volume[1];
1770 size_t frameCount1 = 0;
1771
1772 //FIXME: only works if two tracks use same buffer
1773 int32_t* out = t0.mainBuffer;
1774 size_t numFrames = state->frameCount;
Glenn Kasten54c3b662012-01-06 07:46:30 -08001775 const int16_t *buff = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001776
1777
1778 while (numFrames) {
1779
1780 if (frameCount0 == 0) {
1781 b0.frameCount = numFrames;
John Grossman4ff14ba2012-02-08 16:37:41 -08001782 int64_t outputPTS = calculateOutputPTS(t0, pts,
1783 out - t0.mainBuffer);
1784 t0.bufferProvider->getNextBuffer(&b0, outputPTS);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001785 if (b0.i16 == NULL) {
1786 if (buff == NULL) {
1787 buff = new int16_t[MAX_NUM_CHANNELS * state->frameCount];
1788 }
1789 in0 = buff;
1790 b0.frameCount = numFrames;
1791 } else {
1792 in0 = b0.i16;
1793 }
1794 frameCount0 = b0.frameCount;
1795 }
1796 if (frameCount1 == 0) {
1797 b1.frameCount = numFrames;
John Grossman4ff14ba2012-02-08 16:37:41 -08001798 int64_t outputPTS = calculateOutputPTS(t1, pts,
1799 out - t0.mainBuffer);
1800 t1.bufferProvider->getNextBuffer(&b1, outputPTS);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001801 if (b1.i16 == NULL) {
1802 if (buff == NULL) {
1803 buff = new int16_t[MAX_NUM_CHANNELS * state->frameCount];
1804 }
1805 in1 = buff;
1806 b1.frameCount = numFrames;
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -08001807 } else {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001808 in1 = b1.i16;
1809 }
1810 frameCount1 = b1.frameCount;
1811 }
1812
1813 size_t outFrames = frameCount0 < frameCount1?frameCount0:frameCount1;
1814
1815 numFrames -= outFrames;
1816 frameCount0 -= outFrames;
1817 frameCount1 -= outFrames;
1818
1819 do {
1820 int32_t l0 = *in0++;
1821 int32_t r0 = *in0++;
1822 l0 = mul(l0, vl0);
1823 r0 = mul(r0, vr0);
1824 int32_t l = *in1++;
1825 int32_t r = *in1++;
1826 l = mulAdd(l, vl1, l0) >> 12;
1827 r = mulAdd(r, vr1, r0) >> 12;
1828 // clamping...
1829 l = clamp16(l);
1830 r = clamp16(r);
1831 *out++ = (r<<16) | (l & 0xFFFF);
1832 } while (--outFrames);
1833
1834 if (frameCount0 == 0) {
1835 t0.bufferProvider->releaseBuffer(&b0);
1836 }
1837 if (frameCount1 == 0) {
1838 t1.bufferProvider->releaseBuffer(&b1);
1839 }
1840 }
1841
Glenn Kastene9dd0172012-01-27 18:08:45 -08001842 delete [] buff;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001843}
Glenn Kasten81a028f2011-12-15 09:53:12 -08001844#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -07001845
John Grossman4ff14ba2012-02-08 16:37:41 -08001846int64_t AudioMixer::calculateOutputPTS(const track_t& t, int64_t basePTS,
1847 int outputFrameIndex)
1848{
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001849 if (AudioBufferProvider::kInvalidPTS == basePTS) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001850 return AudioBufferProvider::kInvalidPTS;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001851 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001852
Glenn Kasten52008f82012-03-18 09:34:41 -07001853 return basePTS + ((outputFrameIndex * sLocalTimeFreq) / t.sampleRate);
1854}
1855
1856/*static*/ uint64_t AudioMixer::sLocalTimeFreq;
1857/*static*/ pthread_once_t AudioMixer::sOnceControl = PTHREAD_ONCE_INIT;
1858
1859/*static*/ void AudioMixer::sInitRoutine()
1860{
1861 LocalClock lc;
Andy Hung34803d52014-07-16 21:41:35 -07001862 sLocalTimeFreq = lc.getLocalFreq(); // for the resampler
Glenn Kasten49c34ac2013-10-30 14:37:01 -07001863
Andy Hung34803d52014-07-16 21:41:35 -07001864 DownmixerBufferProvider::init(); // for the downmixer
John Grossman4ff14ba2012-02-08 16:37:41 -08001865}
1866
Andy Hung5e58b0a2014-06-23 19:07:29 -07001867template <int MIXTYPE, int NCHAN, bool USEFLOATVOL, bool ADJUSTVOL,
1868 typename TO, typename TI, typename TA>
1869void AudioMixer::volumeMix(TO *out, size_t outFrames,
1870 const TI *in, TA *aux, bool ramp, AudioMixer::track_t *t)
1871{
1872 if (USEFLOATVOL) {
1873 if (ramp) {
1874 volumeRampMulti<MIXTYPE, NCHAN>(out, outFrames, in, aux,
1875 t->mPrevVolume, t->mVolumeInc, &t->prevAuxLevel, t->auxInc);
1876 if (ADJUSTVOL) {
1877 t->adjustVolumeRamp(aux != NULL, true);
1878 }
1879 } else {
1880 volumeMulti<MIXTYPE, NCHAN>(out, outFrames, in, aux,
1881 t->mVolume, t->auxLevel);
1882 }
1883 } else {
1884 if (ramp) {
1885 volumeRampMulti<MIXTYPE, NCHAN>(out, outFrames, in, aux,
1886 t->prevVolume, t->volumeInc, &t->prevAuxLevel, t->auxInc);
1887 if (ADJUSTVOL) {
1888 t->adjustVolumeRamp(aux != NULL);
1889 }
1890 } else {
1891 volumeMulti<MIXTYPE, NCHAN>(out, outFrames, in, aux,
1892 t->volume, t->auxLevel);
1893 }
1894 }
1895}
1896
Andy Hung296b7412014-06-17 15:25:47 -07001897/* This process hook is called when there is a single track without
1898 * aux buffer, volume ramp, or resampling.
1899 * TODO: Update the hook selection: this can properly handle aux and ramp.
1900 */
1901template <int MIXTYPE, int NCHAN, typename TO, typename TI, typename TA>
1902void AudioMixer::process_NoResampleOneTrack(state_t* state, int64_t pts)
1903{
1904 ALOGVV("process_NoResampleOneTrack\n");
1905 // CLZ is faster than CTZ on ARM, though really not sure if true after 31 - clz.
1906 const int i = 31 - __builtin_clz(state->enabledTracks);
1907 ALOG_ASSERT((1 << i) == state->enabledTracks, "more than 1 track enabled");
1908 track_t *t = &state->tracks[i];
1909 TO* out = reinterpret_cast<TO*>(t->mainBuffer);
1910 TA* aux = reinterpret_cast<TA*>(t->auxBuffer);
1911 const bool ramp = t->needsRamp();
1912
1913 for (size_t numFrames = state->frameCount; numFrames; ) {
1914 AudioBufferProvider::Buffer& b(t->buffer);
1915 // get input buffer
1916 b.frameCount = numFrames;
1917 const int64_t outputPTS = calculateOutputPTS(*t, pts, state->frameCount - numFrames);
1918 t->bufferProvider->getNextBuffer(&b, outputPTS);
1919 const TI *in = reinterpret_cast<TI*>(b.raw);
1920
1921 // in == NULL can happen if the track was flushed just after having
1922 // been enabled for mixing.
1923 if (in == NULL || (((uintptr_t)in) & 3)) {
1924 memset(out, 0, numFrames
1925 * NCHAN * audio_bytes_per_sample(t->mMixerFormat));
1926 ALOGE_IF((((uintptr_t)in) & 3), "process_NoResampleOneTrack: bus error: "
1927 "buffer %p track %p, channels %d, needs %#x",
1928 in, t, t->channelCount, t->needs);
1929 return;
1930 }
1931
1932 const size_t outFrames = b.frameCount;
Andy Hung5e58b0a2014-06-23 19:07:29 -07001933 volumeMix<MIXTYPE, NCHAN, is_same<TI, float>::value, false> (out,
1934 outFrames, in, aux, ramp, t);
1935
Andy Hung296b7412014-06-17 15:25:47 -07001936 out += outFrames * NCHAN;
1937 if (aux != NULL) {
1938 aux += NCHAN;
1939 }
1940 numFrames -= b.frameCount;
1941
1942 // release buffer
1943 t->bufferProvider->releaseBuffer(&b);
1944 }
1945 if (ramp) {
Andy Hung5e58b0a2014-06-23 19:07:29 -07001946 t->adjustVolumeRamp(aux != NULL, is_same<TI, float>::value);
Andy Hung296b7412014-06-17 15:25:47 -07001947 }
1948}
1949
1950/* This track hook is called to do resampling then mixing,
1951 * pulling from the track's upstream AudioBufferProvider.
1952 */
1953template <int MIXTYPE, int NCHAN, typename TO, typename TI, typename TA>
1954void AudioMixer::track__Resample(track_t* t, TO* out, size_t outFrameCount, TO* temp, TA* aux)
1955{
1956 ALOGVV("track__Resample\n");
1957 t->resampler->setSampleRate(t->sampleRate);
1958
1959 const bool ramp = t->needsRamp();
1960 if (ramp || aux != NULL) {
1961 // if ramp: resample with unity gain to temp buffer and scale/mix in 2nd step.
1962 // if aux != NULL: resample with unity gain to temp buffer then apply send level.
1963
Andy Hung5e58b0a2014-06-23 19:07:29 -07001964 t->resampler->setVolume(UNITY_GAIN_FLOAT, UNITY_GAIN_FLOAT);
Andy Hung296b7412014-06-17 15:25:47 -07001965 memset(temp, 0, outFrameCount * NCHAN * sizeof(TO));
1966 t->resampler->resample((int32_t*)temp, outFrameCount, t->bufferProvider);
Andy Hung5e58b0a2014-06-23 19:07:29 -07001967
1968 volumeMix<MIXTYPE, NCHAN, is_same<TI, float>::value, true>(out, outFrameCount,
1969 temp, aux, ramp, t);
1970
Andy Hung296b7412014-06-17 15:25:47 -07001971 } else { // constant volume gain
Andy Hung5e58b0a2014-06-23 19:07:29 -07001972 t->resampler->setVolume(t->mVolume[0], t->mVolume[1]);
Andy Hung296b7412014-06-17 15:25:47 -07001973 t->resampler->resample((int32_t*)out, outFrameCount, t->bufferProvider);
1974 }
1975}
1976
1977/* This track hook is called to mix a track, when no resampling is required.
1978 * The input buffer should be present in t->in.
1979 */
1980template <int MIXTYPE, int NCHAN, typename TO, typename TI, typename TA>
1981void AudioMixer::track__NoResample(track_t* t, TO* out, size_t frameCount,
1982 TO* temp __unused, TA* aux)
1983{
1984 ALOGVV("track__NoResample\n");
1985 const TI *in = static_cast<const TI *>(t->in);
1986
Andy Hung5e58b0a2014-06-23 19:07:29 -07001987 volumeMix<MIXTYPE, NCHAN, is_same<TI, float>::value, true>(out, frameCount,
1988 in, aux, t->needsRamp(), t);
1989
Andy Hung296b7412014-06-17 15:25:47 -07001990 // MIXTYPE_MONOEXPAND reads a single input channel and expands to NCHAN output channels.
1991 // MIXTYPE_MULTI reads NCHAN input channels and places to NCHAN output channels.
1992 in += (MIXTYPE == MIXTYPE_MONOEXPAND) ? frameCount : frameCount * NCHAN;
1993 t->in = in;
1994}
1995
1996/* The Mixer engine generates either int32_t (Q4_27) or float data.
1997 * We use this function to convert the engine buffers
1998 * to the desired mixer output format, either int16_t (Q.15) or float.
1999 */
2000void AudioMixer::convertMixerFormat(void *out, audio_format_t mixerOutFormat,
2001 void *in, audio_format_t mixerInFormat, size_t sampleCount)
2002{
2003 switch (mixerInFormat) {
2004 case AUDIO_FORMAT_PCM_FLOAT:
2005 switch (mixerOutFormat) {
2006 case AUDIO_FORMAT_PCM_FLOAT:
2007 memcpy(out, in, sampleCount * sizeof(float)); // MEMCPY. TODO optimize out
2008 break;
2009 case AUDIO_FORMAT_PCM_16_BIT:
2010 memcpy_to_i16_from_float((int16_t*)out, (float*)in, sampleCount);
2011 break;
2012 default:
2013 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat);
2014 break;
2015 }
2016 break;
2017 case AUDIO_FORMAT_PCM_16_BIT:
2018 switch (mixerOutFormat) {
2019 case AUDIO_FORMAT_PCM_FLOAT:
2020 memcpy_to_float_from_q4_27((float*)out, (int32_t*)in, sampleCount);
2021 break;
2022 case AUDIO_FORMAT_PCM_16_BIT:
2023 // two int16_t are produced per iteration
2024 ditherAndClamp((int32_t*)out, (int32_t*)in, sampleCount >> 1);
2025 break;
2026 default:
2027 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat);
2028 break;
2029 }
2030 break;
2031 default:
2032 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
2033 break;
2034 }
2035}
2036
2037/* Returns the proper track hook to use for mixing the track into the output buffer.
2038 */
2039AudioMixer::hook_t AudioMixer::getTrackHook(int trackType, int channels,
2040 audio_format_t mixerInFormat, audio_format_t mixerOutFormat __unused)
2041{
2042 if (!kUseNewMixer && channels == FCC_2 && mixerInFormat == AUDIO_FORMAT_PCM_16_BIT) {
2043 switch (trackType) {
2044 case TRACKTYPE_NOP:
2045 return track__nop;
2046 case TRACKTYPE_RESAMPLE:
2047 return track__genericResample;
2048 case TRACKTYPE_NORESAMPLEMONO:
2049 return track__16BitsMono;
2050 case TRACKTYPE_NORESAMPLE:
2051 return track__16BitsStereo;
2052 default:
2053 LOG_ALWAYS_FATAL("bad trackType: %d", trackType);
2054 break;
2055 }
2056 }
2057 LOG_ALWAYS_FATAL_IF(channels != FCC_2); // TODO: must be stereo right now
2058 switch (trackType) {
2059 case TRACKTYPE_NOP:
2060 return track__nop;
2061 case TRACKTYPE_RESAMPLE:
2062 switch (mixerInFormat) {
2063 case AUDIO_FORMAT_PCM_FLOAT:
2064 return (AudioMixer::hook_t)
2065 track__Resample<MIXTYPE_MULTI, 2, float, float, int32_t>;
2066 case AUDIO_FORMAT_PCM_16_BIT:
2067 return (AudioMixer::hook_t)\
2068 track__Resample<MIXTYPE_MULTI, 2, int32_t, int16_t, int32_t>;
2069 default:
2070 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
2071 break;
2072 }
2073 break;
2074 case TRACKTYPE_NORESAMPLEMONO:
2075 switch (mixerInFormat) {
2076 case AUDIO_FORMAT_PCM_FLOAT:
2077 return (AudioMixer::hook_t)
2078 track__NoResample<MIXTYPE_MONOEXPAND, 2, float, float, int32_t>;
2079 case AUDIO_FORMAT_PCM_16_BIT:
2080 return (AudioMixer::hook_t)
2081 track__NoResample<MIXTYPE_MONOEXPAND, 2, int32_t, int16_t, int32_t>;
2082 default:
2083 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
2084 break;
2085 }
2086 break;
2087 case TRACKTYPE_NORESAMPLE:
2088 switch (mixerInFormat) {
2089 case AUDIO_FORMAT_PCM_FLOAT:
2090 return (AudioMixer::hook_t)
2091 track__NoResample<MIXTYPE_MULTI, 2, float, float, int32_t>;
2092 case AUDIO_FORMAT_PCM_16_BIT:
2093 return (AudioMixer::hook_t)
2094 track__NoResample<MIXTYPE_MULTI, 2, int32_t, int16_t, int32_t>;
2095 default:
2096 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
2097 break;
2098 }
2099 break;
2100 default:
2101 LOG_ALWAYS_FATAL("bad trackType: %d", trackType);
2102 break;
2103 }
2104 return NULL;
2105}
2106
2107/* Returns the proper process hook for mixing tracks. Currently works only for
2108 * PROCESSTYPE_NORESAMPLEONETRACK, a mix involving one track, no resampling.
2109 */
2110AudioMixer::process_hook_t AudioMixer::getProcessHook(int processType, int channels,
2111 audio_format_t mixerInFormat, audio_format_t mixerOutFormat)
2112{
2113 if (processType != PROCESSTYPE_NORESAMPLEONETRACK) { // Only NORESAMPLEONETRACK
2114 LOG_ALWAYS_FATAL("bad processType: %d", processType);
2115 return NULL;
2116 }
2117 if (!kUseNewMixer && channels == FCC_2 && mixerInFormat == AUDIO_FORMAT_PCM_16_BIT) {
2118 return process__OneTrack16BitsStereoNoResampling;
2119 }
2120 LOG_ALWAYS_FATAL_IF(channels != FCC_2); // TODO: must be stereo right now
2121 switch (mixerInFormat) {
2122 case AUDIO_FORMAT_PCM_FLOAT:
2123 switch (mixerOutFormat) {
2124 case AUDIO_FORMAT_PCM_FLOAT:
2125 return process_NoResampleOneTrack<MIXTYPE_MULTI_SAVEONLY, 2,
2126 float, float, int32_t>;
2127 case AUDIO_FORMAT_PCM_16_BIT:
2128 return process_NoResampleOneTrack<MIXTYPE_MULTI_SAVEONLY, 2,
2129 int16_t, float, int32_t>;
2130 default:
2131 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat);
2132 break;
2133 }
2134 break;
2135 case AUDIO_FORMAT_PCM_16_BIT:
2136 switch (mixerOutFormat) {
2137 case AUDIO_FORMAT_PCM_FLOAT:
2138 return process_NoResampleOneTrack<MIXTYPE_MULTI_SAVEONLY, 2,
2139 float, int16_t, int32_t>;
2140 case AUDIO_FORMAT_PCM_16_BIT:
2141 return process_NoResampleOneTrack<MIXTYPE_MULTI_SAVEONLY, 2,
2142 int16_t, int16_t, int32_t>;
2143 default:
2144 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat);
2145 break;
2146 }
2147 break;
2148 default:
2149 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
2150 break;
2151 }
2152 return NULL;
2153}
2154
Mathias Agopian65ab4712010-07-14 17:59:35 -07002155// ----------------------------------------------------------------------------
2156}; // namespace android