blob: cb90eced9381b4283f8b85368d18f9633ff69b0f [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>
Andy Hung857d5a22015-03-26 18:46:00 -070041#include <media/AudioResamplerPublic.h>
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -070042
Andy Hung296b7412014-06-17 15:25:47 -070043#include "AudioMixerOps.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070044#include "AudioMixer.h"
45
Andy Hunge93b6b72014-07-17 21:30:53 -070046// The FCC_2 macro refers to the Fixed Channel Count of 2 for the legacy integer mixer.
Andy Hung296b7412014-06-17 15:25:47 -070047#ifndef FCC_2
48#define FCC_2 2
49#endif
50
Andy Hunge93b6b72014-07-17 21:30:53 -070051// Look for MONO_HACK for any Mono hack involving legacy mono channel to
52// stereo channel conversion.
53
Andy Hung296b7412014-06-17 15:25:47 -070054/* VERY_VERY_VERBOSE_LOGGING will show exactly which process hook and track hook is
55 * being used. This is a considerable amount of log spam, so don't enable unless you
56 * are verifying the hook based code.
57 */
58//#define VERY_VERY_VERBOSE_LOGGING
59#ifdef VERY_VERY_VERBOSE_LOGGING
60#define ALOGVV ALOGV
61//define ALOGVV printf // for test-mixer.cpp
62#else
63#define ALOGVV(a...) do { } while (0)
64#endif
65
Andy Hunga08810b2014-07-16 21:53:43 -070066#ifndef ARRAY_SIZE
67#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
68#endif
69
Andy Hung5b8fde72014-09-02 21:14:34 -070070// Set kUseNewMixer to true to use the new mixer engine always. Otherwise the
71// original code will be used for stereo sinks, the new mixer for multichannel.
72static const bool kUseNewMixer = true;
Andy Hung296b7412014-06-17 15:25:47 -070073
74// Set kUseFloat to true to allow floating input into the mixer engine.
75// If kUseNewMixer is false, this is ignored or may be overridden internally
76// because of downmix/upmix support.
77static const bool kUseFloat = true;
78
Andy Hung1b2fdcb2014-07-16 17:44:34 -070079// Set to default copy buffer size in frames for input processing.
80static const size_t kCopyBufferFrameCount = 256;
81
Mathias Agopian65ab4712010-07-14 17:59:35 -070082namespace android {
Mathias Agopian65ab4712010-07-14 17:59:35 -070083
84// ----------------------------------------------------------------------------
Andy Hung1b2fdcb2014-07-16 17:44:34 -070085
86template <typename T>
87T min(const T& a, const T& b)
88{
89 return a < b ? a : b;
90}
91
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -070092// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -070093
Paul Lind3c0a0e82012-08-01 18:49:49 -070094// Ensure mConfiguredNames bitmask is initialized properly on all architectures.
95// The value of 1 << x is undefined in C when x >= 32.
96
Glenn Kasten5c94b6c2012-03-20 17:01:29 -070097AudioMixer::AudioMixer(size_t frameCount, uint32_t sampleRate, uint32_t maxNumTracks)
Paul Lind3c0a0e82012-08-01 18:49:49 -070098 : mTrackNames(0), mConfiguredNames((maxNumTracks >= 32 ? 0 : 1 << maxNumTracks) - 1),
Glenn Kasten7f5d3352013-02-15 23:55:04 +000099 mSampleRate(sampleRate)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700100{
Glenn Kasten5c94b6c2012-03-20 17:01:29 -0700101 ALOG_ASSERT(maxNumTracks <= MAX_NUM_TRACKS, "maxNumTracks %u > MAX_NUM_TRACKS %u",
102 maxNumTracks, MAX_NUM_TRACKS);
103
Glenn Kasten599fabc2012-03-08 12:33:37 -0800104 // AudioMixer is not yet capable of more than 32 active track inputs
105 ALOG_ASSERT(32 >= MAX_NUM_TRACKS, "bad MAX_NUM_TRACKS %d", MAX_NUM_TRACKS);
106
Glenn Kasten52008f82012-03-18 09:34:41 -0700107 pthread_once(&sOnceControl, &sInitRoutine);
108
Mathias Agopian65ab4712010-07-14 17:59:35 -0700109 mState.enabledTracks= 0;
110 mState.needsChanged = 0;
111 mState.frameCount = frameCount;
Glenn Kasten84afa3b2012-01-25 15:28:08 -0800112 mState.hook = process__nop;
Glenn Kastene0feee32011-12-13 11:53:26 -0800113 mState.outputTemp = NULL;
114 mState.resampleTemp = NULL;
Glenn Kastenab7d72f2013-02-27 09:05:28 -0800115 mState.mLog = &mDummyLog;
Glenn Kasten84afa3b2012-01-25 15:28:08 -0800116 // mState.reserved
Glenn Kasten17a736c2012-02-14 08:52:15 -0800117
118 // FIXME Most of the following initialization is probably redundant since
119 // tracks[i] should only be referenced if (mTrackNames & (1 << i)) != 0
120 // and mTrackNames is initially 0. However, leave it here until that's verified.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700121 track_t* t = mState.tracks;
Glenn Kastenbf71f1e2011-12-13 11:52:35 -0800122 for (unsigned i=0 ; i < MAX_NUM_TRACKS ; i++) {
Eric Laurenta5e82142012-04-16 13:47:17 -0700123 t->resampler = NULL;
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700124 t->downmixerBufferProvider = NULL;
Andy Hung1b2fdcb2014-07-16 17:44:34 -0700125 t->mReformatBufferProvider = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700126 t++;
127 }
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700128
Mathias Agopian65ab4712010-07-14 17:59:35 -0700129}
130
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800131AudioMixer::~AudioMixer()
132{
133 track_t* t = mState.tracks;
Glenn Kastenbf71f1e2011-12-13 11:52:35 -0800134 for (unsigned i=0 ; i < MAX_NUM_TRACKS ; i++) {
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800135 delete t->resampler;
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700136 delete t->downmixerBufferProvider;
Andy Hung1b2fdcb2014-07-16 17:44:34 -0700137 delete t->mReformatBufferProvider;
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800138 t++;
139 }
140 delete [] mState.outputTemp;
141 delete [] mState.resampleTemp;
142}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700143
Glenn Kastenab7d72f2013-02-27 09:05:28 -0800144void AudioMixer::setLog(NBLog::Writer *log)
145{
146 mState.mLog = log;
147}
148
Andy Hung7f475492014-08-25 16:36:37 -0700149static inline audio_format_t selectMixerInFormat(audio_format_t inputFormat __unused) {
150 return kUseFloat && kUseNewMixer ? AUDIO_FORMAT_PCM_FLOAT : AUDIO_FORMAT_PCM_16_BIT;
151}
152
Andy Hunge8a1ced2014-05-09 15:02:21 -0700153int AudioMixer::getTrackName(audio_channel_mask_t channelMask,
154 audio_format_t format, int sessionId)
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800155{
Andy Hunge8a1ced2014-05-09 15:02:21 -0700156 if (!isValidPcmTrackFormat(format)) {
157 ALOGE("AudioMixer::getTrackName invalid format (%#x)", format);
158 return -1;
159 }
Glenn Kasten5c94b6c2012-03-20 17:01:29 -0700160 uint32_t names = (~mTrackNames) & mConfiguredNames;
Glenn Kasten98dd5422011-12-15 14:38:29 -0800161 if (names != 0) {
162 int n = __builtin_ctz(names);
Steve Block3856b092011-10-20 11:56:00 +0100163 ALOGV("add track (%d)", n);
Glenn Kastendeeb1282012-03-25 11:59:31 -0700164 // assume default parameters for the track, except where noted below
165 track_t* t = &mState.tracks[n];
166 t->needs = 0;
Andy Hung5e58b0a2014-06-23 19:07:29 -0700167
168 // Integer volume.
169 // Currently integer volume is kept for the legacy integer mixer.
170 // Will be removed when the legacy mixer path is removed.
Andy Hung97ae8242014-05-30 10:35:47 -0700171 t->volume[0] = UNITY_GAIN_INT;
172 t->volume[1] = UNITY_GAIN_INT;
Andy Hung5e58b0a2014-06-23 19:07:29 -0700173 t->prevVolume[0] = UNITY_GAIN_INT << 16;
174 t->prevVolume[1] = UNITY_GAIN_INT << 16;
Glenn Kastendeeb1282012-03-25 11:59:31 -0700175 t->volumeInc[0] = 0;
176 t->volumeInc[1] = 0;
177 t->auxLevel = 0;
178 t->auxInc = 0;
Andy Hung5e58b0a2014-06-23 19:07:29 -0700179 t->prevAuxLevel = 0;
180
181 // Floating point volume.
182 t->mVolume[0] = UNITY_GAIN_FLOAT;
183 t->mVolume[1] = UNITY_GAIN_FLOAT;
184 t->mPrevVolume[0] = UNITY_GAIN_FLOAT;
185 t->mPrevVolume[1] = UNITY_GAIN_FLOAT;
186 t->mVolumeInc[0] = 0.;
187 t->mVolumeInc[1] = 0.;
188 t->mAuxLevel = 0.;
189 t->mAuxInc = 0.;
190 t->mPrevAuxLevel = 0.;
191
Glenn Kastendeeb1282012-03-25 11:59:31 -0700192 // no initialization needed
Glenn Kastendeeb1282012-03-25 11:59:31 -0700193 // t->frameCount
Andy Hung68112fc2014-05-14 14:13:23 -0700194 t->channelCount = audio_channel_count_from_out_mask(channelMask);
Glenn Kastendeeb1282012-03-25 11:59:31 -0700195 t->enabled = false;
Andy Hunge93b6b72014-07-17 21:30:53 -0700196 ALOGV_IF(audio_channel_mask_get_bits(channelMask) != AUDIO_CHANNEL_OUT_STEREO,
Andy Hungef7c7fb2014-05-12 16:51:41 -0700197 "Non-stereo channel mask: %d\n", channelMask);
Andy Hung68112fc2014-05-14 14:13:23 -0700198 t->channelMask = channelMask;
Jean-Michel Trivid06e1322012-09-12 15:47:07 -0700199 t->sessionId = sessionId;
Glenn Kastendeeb1282012-03-25 11:59:31 -0700200 // setBufferProvider(name, AudioBufferProvider *) is required before enable(name)
201 t->bufferProvider = NULL;
202 t->buffer.raw = NULL;
203 // no initialization needed
204 // t->buffer.frameCount
205 t->hook = NULL;
206 t->in = NULL;
207 t->resampler = NULL;
208 t->sampleRate = mSampleRate;
209 // setParameter(name, TRACK, MAIN_BUFFER, mixBuffer) is required before enable(name)
210 t->mainBuffer = NULL;
211 t->auxBuffer = NULL;
Andy Hungef7c7fb2014-05-12 16:51:41 -0700212 t->mInputBufferProvider = NULL;
213 t->mReformatBufferProvider = NULL;
Glenn Kasten52008f82012-03-18 09:34:41 -0700214 t->downmixerBufferProvider = NULL;
Andy Hung7f475492014-08-25 16:36:37 -0700215 t->mPostDownmixReformatBufferProvider = NULL;
Andy Hung78820702014-02-28 16:23:02 -0800216 t->mMixerFormat = AUDIO_FORMAT_PCM_16_BIT;
Andy Hunge8a1ced2014-05-09 15:02:21 -0700217 t->mFormat = format;
Andy Hung7f475492014-08-25 16:36:37 -0700218 t->mMixerInFormat = selectMixerInFormat(format);
219 t->mDownmixRequiresFormat = AUDIO_FORMAT_INVALID; // no format required
Andy Hunge93b6b72014-07-17 21:30:53 -0700220 t->mMixerChannelMask = audio_channel_mask_from_representation_and_bits(
221 AUDIO_CHANNEL_REPRESENTATION_POSITION, AUDIO_CHANNEL_OUT_STEREO);
222 t->mMixerChannelCount = audio_channel_count_from_out_mask(t->mMixerChannelMask);
Andy Hung296b7412014-06-17 15:25:47 -0700223 // Check the downmixing (or upmixing) requirements.
Andy Hung0f451e92014-08-04 21:28:47 -0700224 status_t status = t->prepareForDownmix();
Andy Hung68112fc2014-05-14 14:13:23 -0700225 if (status != OK) {
226 ALOGE("AudioMixer::getTrackName invalid channelMask (%#x)", channelMask);
227 return -1;
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700228 }
Andy Hung7f475492014-08-25 16:36:37 -0700229 // prepareForDownmix() may change mDownmixRequiresFormat
Andy Hung296b7412014-06-17 15:25:47 -0700230 ALOGVV("mMixerFormat:%#x mMixerInFormat:%#x\n", t->mMixerFormat, t->mMixerInFormat);
Andy Hung0f451e92014-08-04 21:28:47 -0700231 t->prepareForReformat();
Andy Hung68112fc2014-05-14 14:13:23 -0700232 mTrackNames |= 1 << n;
233 return TRACK0 + n;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700234 }
Andy Hung68112fc2014-05-14 14:13:23 -0700235 ALOGE("AudioMixer::getTrackName out of available tracks");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700236 return -1;
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800237}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700238
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800239void AudioMixer::invalidateState(uint32_t mask)
240{
Glenn Kasten34fca342013-08-13 09:48:14 -0700241 if (mask != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700242 mState.needsChanged |= mask;
243 mState.hook = process__validate;
244 }
245 }
246
Andy Hunge93b6b72014-07-17 21:30:53 -0700247// Called when channel masks have changed for a track name
Andy Hung7f475492014-08-25 16:36:37 -0700248// TODO: Fix DownmixerBufferProvider not to (possibly) change mixer input format,
Andy Hunge93b6b72014-07-17 21:30:53 -0700249// which will simplify this logic.
250bool AudioMixer::setChannelMasks(int name,
251 audio_channel_mask_t trackChannelMask, audio_channel_mask_t mixerChannelMask) {
252 track_t &track = mState.tracks[name];
253
254 if (trackChannelMask == track.channelMask
255 && mixerChannelMask == track.mMixerChannelMask) {
256 return false; // no need to change
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700257 }
Andy Hunge93b6b72014-07-17 21:30:53 -0700258 // always recompute for both channel masks even if only one has changed.
259 const uint32_t trackChannelCount = audio_channel_count_from_out_mask(trackChannelMask);
260 const uint32_t mixerChannelCount = audio_channel_count_from_out_mask(mixerChannelMask);
261 const bool mixerChannelCountChanged = track.mMixerChannelCount != mixerChannelCount;
262
263 ALOG_ASSERT((trackChannelCount <= MAX_NUM_CHANNELS_TO_DOWNMIX)
264 && trackChannelCount
265 && mixerChannelCount);
266 track.channelMask = trackChannelMask;
267 track.channelCount = trackChannelCount;
268 track.mMixerChannelMask = mixerChannelMask;
269 track.mMixerChannelCount = mixerChannelCount;
270
271 // channel masks have changed, does this track need a downmixer?
272 // update to try using our desired format (if we aren't already using it)
Andy Hung7f475492014-08-25 16:36:37 -0700273 const audio_format_t prevDownmixerFormat = track.mDownmixRequiresFormat;
Andy Hung0f451e92014-08-04 21:28:47 -0700274 const status_t status = mState.tracks[name].prepareForDownmix();
Andy Hunge93b6b72014-07-17 21:30:53 -0700275 ALOGE_IF(status != OK,
Andy Hung0f451e92014-08-04 21:28:47 -0700276 "prepareForDownmix error %d, track channel mask %#x, mixer channel mask %#x",
Andy Hunge93b6b72014-07-17 21:30:53 -0700277 status, track.channelMask, track.mMixerChannelMask);
278
Andy Hung7f475492014-08-25 16:36:37 -0700279 if (prevDownmixerFormat != track.mDownmixRequiresFormat) {
Andy Hung0f451e92014-08-04 21:28:47 -0700280 track.prepareForReformat(); // because of downmixer, track format may change!
Andy Hunge93b6b72014-07-17 21:30:53 -0700281 }
282
Andy Hung7f475492014-08-25 16:36:37 -0700283 if (track.resampler && mixerChannelCountChanged) {
284 // resampler channels may have changed.
Andy Hunge93b6b72014-07-17 21:30:53 -0700285 const uint32_t resetToSampleRate = track.sampleRate;
286 delete track.resampler;
287 track.resampler = NULL;
288 track.sampleRate = mSampleRate; // without resampler, track rate is device sample rate.
289 // recreate the resampler with updated format, channels, saved sampleRate.
290 track.setResampler(resetToSampleRate /*trackSampleRate*/, mSampleRate /*devSampleRate*/);
291 }
292 return true;
293}
294
Andy Hung0f451e92014-08-04 21:28:47 -0700295void AudioMixer::track_t::unprepareForDownmix() {
296 ALOGV("AudioMixer::unprepareForDownmix(%p)", this);
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700297
Andy Hung7f475492014-08-25 16:36:37 -0700298 mDownmixRequiresFormat = AUDIO_FORMAT_INVALID;
Andy Hung0f451e92014-08-04 21:28:47 -0700299 if (downmixerBufferProvider != NULL) {
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700300 // this track had previously been configured with a downmixer, delete it
301 ALOGV(" deleting old downmixer");
Andy Hung0f451e92014-08-04 21:28:47 -0700302 delete downmixerBufferProvider;
303 downmixerBufferProvider = NULL;
304 reconfigureBufferProviders();
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700305 } else {
306 ALOGV(" nothing to do, no downmixer to delete");
307 }
308}
309
Andy Hung0f451e92014-08-04 21:28:47 -0700310status_t AudioMixer::track_t::prepareForDownmix()
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700311{
Andy Hung0f451e92014-08-04 21:28:47 -0700312 ALOGV("AudioMixer::prepareForDownmix(%p) with mask 0x%x",
313 this, channelMask);
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700314
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700315 // discard the previous downmixer if there was one
Andy Hung0f451e92014-08-04 21:28:47 -0700316 unprepareForDownmix();
317 // Only remix (upmix or downmix) if the track and mixer/device channel masks
318 // are not the same and not handled internally, as mono -> stereo currently is.
319 if (channelMask == mMixerChannelMask
320 || (channelMask == AUDIO_CHANNEL_OUT_MONO
321 && mMixerChannelMask == AUDIO_CHANNEL_OUT_STEREO)) {
322 return NO_ERROR;
323 }
Andy Hung650ceb92015-01-29 13:31:12 -0800324 // DownmixerBufferProvider is only used for position masks.
325 if (audio_channel_mask_get_representation(channelMask)
326 == AUDIO_CHANNEL_REPRESENTATION_POSITION
327 && DownmixerBufferProvider::isMultichannelCapable()) {
Andy Hung0f451e92014-08-04 21:28:47 -0700328 DownmixerBufferProvider* pDbp = new DownmixerBufferProvider(channelMask,
329 mMixerChannelMask,
330 AUDIO_FORMAT_PCM_16_BIT /* TODO: use mMixerInFormat, now only PCM 16 */,
331 sampleRate, sessionId, kCopyBufferFrameCount);
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700332
Andy Hung34803d52014-07-16 21:41:35 -0700333 if (pDbp->isValid()) { // if constructor completed properly
Andy Hung7f475492014-08-25 16:36:37 -0700334 mDownmixRequiresFormat = AUDIO_FORMAT_PCM_16_BIT; // PCM 16 bit required for downmix
Andy Hung0f451e92014-08-04 21:28:47 -0700335 downmixerBufferProvider = pDbp;
336 reconfigureBufferProviders();
Andy Hung34803d52014-07-16 21:41:35 -0700337 return NO_ERROR;
338 }
339 delete pDbp;
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700340 }
Andy Hunge93b6b72014-07-17 21:30:53 -0700341
342 // Effect downmixer does not accept the channel conversion. Let's use our remixer.
Andy Hung0f451e92014-08-04 21:28:47 -0700343 RemixBufferProvider* pRbp = new RemixBufferProvider(channelMask,
344 mMixerChannelMask, mMixerInFormat, kCopyBufferFrameCount);
Andy Hunge93b6b72014-07-17 21:30:53 -0700345 // Remix always finds a conversion whereas Downmixer effect above may fail.
Andy Hung0f451e92014-08-04 21:28:47 -0700346 downmixerBufferProvider = pRbp;
347 reconfigureBufferProviders();
Andy Hunge93b6b72014-07-17 21:30:53 -0700348 return NO_ERROR;
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700349}
350
Andy Hung0f451e92014-08-04 21:28:47 -0700351void AudioMixer::track_t::unprepareForReformat() {
352 ALOGV("AudioMixer::unprepareForReformat(%p)", this);
Andy Hung7f475492014-08-25 16:36:37 -0700353 bool requiresReconfigure = false;
Andy Hung0f451e92014-08-04 21:28:47 -0700354 if (mReformatBufferProvider != NULL) {
355 delete mReformatBufferProvider;
356 mReformatBufferProvider = NULL;
Andy Hung7f475492014-08-25 16:36:37 -0700357 requiresReconfigure = true;
358 }
359 if (mPostDownmixReformatBufferProvider != NULL) {
360 delete mPostDownmixReformatBufferProvider;
361 mPostDownmixReformatBufferProvider = NULL;
362 requiresReconfigure = true;
363 }
364 if (requiresReconfigure) {
Andy Hung0f451e92014-08-04 21:28:47 -0700365 reconfigureBufferProviders();
Andy Hungef7c7fb2014-05-12 16:51:41 -0700366 }
367}
368
Andy Hung0f451e92014-08-04 21:28:47 -0700369status_t AudioMixer::track_t::prepareForReformat()
Andy Hungef7c7fb2014-05-12 16:51:41 -0700370{
Andy Hung0f451e92014-08-04 21:28:47 -0700371 ALOGV("AudioMixer::prepareForReformat(%p) with format %#x", this, mFormat);
Andy Hung7f475492014-08-25 16:36:37 -0700372 // discard previous reformatters
Andy Hung0f451e92014-08-04 21:28:47 -0700373 unprepareForReformat();
Andy Hung7f475492014-08-25 16:36:37 -0700374 // only configure reformatters as needed
375 const audio_format_t targetFormat = mDownmixRequiresFormat != AUDIO_FORMAT_INVALID
376 ? mDownmixRequiresFormat : mMixerInFormat;
377 bool requiresReconfigure = false;
378 if (mFormat != targetFormat) {
Andy Hung0f451e92014-08-04 21:28:47 -0700379 mReformatBufferProvider = new ReformatBufferProvider(
380 audio_channel_count_from_out_mask(channelMask),
Andy Hung7f475492014-08-25 16:36:37 -0700381 mFormat,
382 targetFormat,
Andy Hung1b2fdcb2014-07-16 17:44:34 -0700383 kCopyBufferFrameCount);
Andy Hung7f475492014-08-25 16:36:37 -0700384 requiresReconfigure = true;
385 }
386 if (targetFormat != mMixerInFormat) {
387 mPostDownmixReformatBufferProvider = new ReformatBufferProvider(
388 audio_channel_count_from_out_mask(mMixerChannelMask),
389 targetFormat,
390 mMixerInFormat,
391 kCopyBufferFrameCount);
392 requiresReconfigure = true;
393 }
394 if (requiresReconfigure) {
Andy Hung0f451e92014-08-04 21:28:47 -0700395 reconfigureBufferProviders();
Andy Hung296b7412014-06-17 15:25:47 -0700396 }
397 return NO_ERROR;
Andy Hungef7c7fb2014-05-12 16:51:41 -0700398}
399
Andy Hung0f451e92014-08-04 21:28:47 -0700400void AudioMixer::track_t::reconfigureBufferProviders()
Andy Hungef7c7fb2014-05-12 16:51:41 -0700401{
Andy Hung0f451e92014-08-04 21:28:47 -0700402 bufferProvider = mInputBufferProvider;
403 if (mReformatBufferProvider) {
404 mReformatBufferProvider->setBufferProvider(bufferProvider);
405 bufferProvider = mReformatBufferProvider;
Andy Hungef7c7fb2014-05-12 16:51:41 -0700406 }
Andy Hung0f451e92014-08-04 21:28:47 -0700407 if (downmixerBufferProvider) {
408 downmixerBufferProvider->setBufferProvider(bufferProvider);
409 bufferProvider = downmixerBufferProvider;
Andy Hungef7c7fb2014-05-12 16:51:41 -0700410 }
Andy Hung7f475492014-08-25 16:36:37 -0700411 if (mPostDownmixReformatBufferProvider) {
412 mPostDownmixReformatBufferProvider->setBufferProvider(bufferProvider);
413 bufferProvider = mPostDownmixReformatBufferProvider;
414 }
Andy Hungef7c7fb2014-05-12 16:51:41 -0700415}
416
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800417void AudioMixer::deleteTrackName(int name)
418{
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700419 ALOGV("AudioMixer::deleteTrackName(%d)", name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700420 name -= TRACK0;
Glenn Kasten5798d4e2012-03-08 12:18:35 -0800421 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
Glenn Kasten237a6242011-12-15 15:32:27 -0800422 ALOGV("deleteTrackName(%d)", name);
423 track_t& track(mState.tracks[ name ]);
Glenn Kasten4c340c62012-01-27 12:33:54 -0800424 if (track.enabled) {
425 track.enabled = false;
Glenn Kasten237a6242011-12-15 15:32:27 -0800426 invalidateState(1<<name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700427 }
Glenn Kasten4e2293f2012-04-12 09:39:07 -0700428 // delete the resampler
429 delete track.resampler;
430 track.resampler = NULL;
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700431 // delete the downmixer
Andy Hung0f451e92014-08-04 21:28:47 -0700432 mState.tracks[name].unprepareForDownmix();
Andy Hungef7c7fb2014-05-12 16:51:41 -0700433 // delete the reformatter
Andy Hung0f451e92014-08-04 21:28:47 -0700434 mState.tracks[name].unprepareForReformat();
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700435
Glenn Kasten237a6242011-12-15 15:32:27 -0800436 mTrackNames &= ~(1<<name);
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800437}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700438
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800439void AudioMixer::enable(int name)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700440{
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800441 name -= TRACK0;
Glenn Kasten5798d4e2012-03-08 12:18:35 -0800442 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800443 track_t& track = mState.tracks[name];
444
Glenn Kasten4c340c62012-01-27 12:33:54 -0800445 if (!track.enabled) {
446 track.enabled = true;
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800447 ALOGV("enable(%d)", name);
448 invalidateState(1 << name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700449 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700450}
451
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800452void AudioMixer::disable(int name)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700453{
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800454 name -= TRACK0;
Glenn Kasten5798d4e2012-03-08 12:18:35 -0800455 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800456 track_t& track = mState.tracks[name];
457
Glenn Kasten4c340c62012-01-27 12:33:54 -0800458 if (track.enabled) {
459 track.enabled = false;
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800460 ALOGV("disable(%d)", name);
461 invalidateState(1 << name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700462 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700463}
464
Andy Hung5866a3b2014-05-29 21:33:13 -0700465/* Sets the volume ramp variables for the AudioMixer.
466 *
Andy Hung5e58b0a2014-06-23 19:07:29 -0700467 * The volume ramp variables are used to transition from the previous
468 * volume to the set volume. ramp controls the duration of the transition.
469 * Its value is typically one state framecount period, but may also be 0,
470 * meaning "immediate."
Andy Hung5866a3b2014-05-29 21:33:13 -0700471 *
Andy Hung5e58b0a2014-06-23 19:07:29 -0700472 * FIXME: 1) Volume ramp is enabled only if there is a nonzero integer increment
473 * even if there is a nonzero floating point increment (in that case, the volume
474 * change is immediate). This restriction should be changed when the legacy mixer
475 * is removed (see #2).
476 * FIXME: 2) Integer volume variables are used for Legacy mixing and should be removed
477 * when no longer needed.
478 *
479 * @param newVolume set volume target in floating point [0.0, 1.0].
480 * @param ramp number of frames to increment over. if ramp is 0, the volume
481 * should be set immediately. Currently ramp should not exceed 65535 (frames).
482 * @param pIntSetVolume pointer to the U4.12 integer target volume, set on return.
483 * @param pIntPrevVolume pointer to the U4.28 integer previous volume, set on return.
484 * @param pIntVolumeInc pointer to the U4.28 increment per output audio frame, set on return.
485 * @param pSetVolume pointer to the float target volume, set on return.
486 * @param pPrevVolume pointer to the float previous volume, set on return.
487 * @param pVolumeInc pointer to the float increment per output audio frame, set on return.
Andy Hung5866a3b2014-05-29 21:33:13 -0700488 * @return true if the volume has changed, false if volume is same.
489 */
Andy Hung5e58b0a2014-06-23 19:07:29 -0700490static inline bool setVolumeRampVariables(float newVolume, int32_t ramp,
491 int16_t *pIntSetVolume, int32_t *pIntPrevVolume, int32_t *pIntVolumeInc,
492 float *pSetVolume, float *pPrevVolume, float *pVolumeInc) {
493 if (newVolume == *pSetVolume) {
Andy Hung5866a3b2014-05-29 21:33:13 -0700494 return false;
495 }
Andy Hung5e58b0a2014-06-23 19:07:29 -0700496 /* set the floating point volume variables */
Andy Hung5866a3b2014-05-29 21:33:13 -0700497 if (ramp != 0) {
Andy Hung5e58b0a2014-06-23 19:07:29 -0700498 *pVolumeInc = (newVolume - *pSetVolume) / ramp;
499 *pPrevVolume = *pSetVolume;
Andy Hung5866a3b2014-05-29 21:33:13 -0700500 } else {
Andy Hung5e58b0a2014-06-23 19:07:29 -0700501 *pVolumeInc = 0;
502 *pPrevVolume = newVolume;
Andy Hung5866a3b2014-05-29 21:33:13 -0700503 }
Andy Hung5e58b0a2014-06-23 19:07:29 -0700504 *pSetVolume = newVolume;
505
506 /* set the legacy integer volume variables */
507 int32_t intVolume = newVolume * AudioMixer::UNITY_GAIN_INT;
508 if (intVolume > AudioMixer::UNITY_GAIN_INT) {
509 intVolume = AudioMixer::UNITY_GAIN_INT;
510 } else if (intVolume < 0) {
511 ALOGE("negative volume %.7g", newVolume);
512 intVolume = 0; // should never happen, but for safety check.
513 }
514 if (intVolume == *pIntSetVolume) {
515 *pIntVolumeInc = 0;
516 /* TODO: integer/float workaround: ignore floating volume ramp */
517 *pVolumeInc = 0;
518 *pPrevVolume = newVolume;
519 return true;
520 }
521 if (ramp != 0) {
522 *pIntVolumeInc = ((intVolume - *pIntSetVolume) << 16) / ramp;
523 *pIntPrevVolume = (*pIntVolumeInc == 0 ? intVolume : *pIntSetVolume) << 16;
524 } else {
525 *pIntVolumeInc = 0;
526 *pIntPrevVolume = intVolume << 16;
527 }
528 *pIntSetVolume = intVolume;
Andy Hung5866a3b2014-05-29 21:33:13 -0700529 return true;
530}
531
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800532void AudioMixer::setParameter(int name, int target, int param, void *value)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700533{
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800534 name -= TRACK0;
Glenn Kasten5798d4e2012-03-08 12:18:35 -0800535 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800536 track_t& track = mState.tracks[name];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700537
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000538 int valueInt = static_cast<int>(reinterpret_cast<uintptr_t>(value));
539 int32_t *valueBuf = reinterpret_cast<int32_t*>(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700540
541 switch (target) {
Glenn Kasten788040c2011-05-05 08:19:00 -0700542
Mathias Agopian65ab4712010-07-14 17:59:35 -0700543 case TRACK:
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800544 switch (param) {
Glenn Kasten788040c2011-05-05 08:19:00 -0700545 case CHANNEL_MASK: {
Andy Hunge93b6b72014-07-17 21:30:53 -0700546 const audio_channel_mask_t trackChannelMask =
547 static_cast<audio_channel_mask_t>(valueInt);
548 if (setChannelMasks(name, trackChannelMask, track.mMixerChannelMask)) {
549 ALOGV("setParameter(TRACK, CHANNEL_MASK, %x)", trackChannelMask);
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800550 invalidateState(1 << name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700551 }
Glenn Kasten788040c2011-05-05 08:19:00 -0700552 } break;
553 case MAIN_BUFFER:
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800554 if (track.mainBuffer != valueBuf) {
555 track.mainBuffer = valueBuf;
Steve Block3856b092011-10-20 11:56:00 +0100556 ALOGV("setParameter(TRACK, MAIN_BUFFER, %p)", valueBuf);
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800557 invalidateState(1 << name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700558 }
Glenn Kasten788040c2011-05-05 08:19:00 -0700559 break;
560 case AUX_BUFFER:
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800561 if (track.auxBuffer != valueBuf) {
562 track.auxBuffer = valueBuf;
Steve Block3856b092011-10-20 11:56:00 +0100563 ALOGV("setParameter(TRACK, AUX_BUFFER, %p)", valueBuf);
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800564 invalidateState(1 << name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700565 }
Glenn Kasten788040c2011-05-05 08:19:00 -0700566 break;
Andy Hungef7c7fb2014-05-12 16:51:41 -0700567 case FORMAT: {
568 audio_format_t format = static_cast<audio_format_t>(valueInt);
569 if (track.mFormat != format) {
570 ALOG_ASSERT(audio_is_linear_pcm(format), "Invalid format %#x", format);
571 track.mFormat = format;
572 ALOGV("setParameter(TRACK, FORMAT, %#x)", format);
Andy Hung0f451e92014-08-04 21:28:47 -0700573 track.prepareForReformat();
Andy Hungef7c7fb2014-05-12 16:51:41 -0700574 invalidateState(1 << name);
575 }
576 } break;
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700577 // FIXME do we want to support setting the downmix type from AudioFlinger?
578 // for a specific track? or per mixer?
579 /* case DOWNMIX_TYPE:
580 break */
Andy Hung78820702014-02-28 16:23:02 -0800581 case MIXER_FORMAT: {
Andy Hunga1ab7cc2014-02-24 19:26:52 -0800582 audio_format_t format = static_cast<audio_format_t>(valueInt);
Andy Hung78820702014-02-28 16:23:02 -0800583 if (track.mMixerFormat != format) {
584 track.mMixerFormat = format;
585 ALOGV("setParameter(TRACK, MIXER_FORMAT, %#x)", format);
Andy Hunga1ab7cc2014-02-24 19:26:52 -0800586 }
587 } break;
Andy Hunge93b6b72014-07-17 21:30:53 -0700588 case MIXER_CHANNEL_MASK: {
589 const audio_channel_mask_t mixerChannelMask =
590 static_cast<audio_channel_mask_t>(valueInt);
591 if (setChannelMasks(name, track.channelMask, mixerChannelMask)) {
592 ALOGV("setParameter(TRACK, MIXER_CHANNEL_MASK, %#x)", mixerChannelMask);
593 invalidateState(1 << name);
594 }
595 } break;
Glenn Kasten788040c2011-05-05 08:19:00 -0700596 default:
Glenn Kastenadad3d72014-02-21 14:51:43 -0800597 LOG_ALWAYS_FATAL("setParameter track: bad param %d", param);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700598 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700599 break;
Glenn Kasten788040c2011-05-05 08:19:00 -0700600
Mathias Agopian65ab4712010-07-14 17:59:35 -0700601 case RESAMPLE:
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800602 switch (param) {
603 case SAMPLE_RATE:
Glenn Kasten5798d4e2012-03-08 12:18:35 -0800604 ALOG_ASSERT(valueInt > 0, "bad sample rate %d", valueInt);
Glenn Kasten788040c2011-05-05 08:19:00 -0700605 if (track.setResampler(uint32_t(valueInt), mSampleRate)) {
606 ALOGV("setParameter(RESAMPLE, SAMPLE_RATE, %u)",
607 uint32_t(valueInt));
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800608 invalidateState(1 << name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700609 }
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800610 break;
611 case RESET:
Eric Laurent243f5f92011-02-28 16:52:51 -0800612 track.resetResampler();
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800613 invalidateState(1 << name);
614 break;
Glenn Kasten4e2293f2012-04-12 09:39:07 -0700615 case REMOVE:
616 delete track.resampler;
617 track.resampler = NULL;
618 track.sampleRate = mSampleRate;
619 invalidateState(1 << name);
620 break;
Glenn Kasten788040c2011-05-05 08:19:00 -0700621 default:
Glenn Kastenadad3d72014-02-21 14:51:43 -0800622 LOG_ALWAYS_FATAL("setParameter resample: bad param %d", param);
Eric Laurent243f5f92011-02-28 16:52:51 -0800623 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700624 break;
Glenn Kasten788040c2011-05-05 08:19:00 -0700625
Mathias Agopian65ab4712010-07-14 17:59:35 -0700626 case RAMP_VOLUME:
627 case VOLUME:
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800628 switch (param) {
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800629 case AUXLEVEL:
Andy Hung6be49402014-05-30 10:42:03 -0700630 if (setVolumeRampVariables(*reinterpret_cast<float*>(value),
Andy Hung5866a3b2014-05-29 21:33:13 -0700631 target == RAMP_VOLUME ? mState.frameCount : 0,
Andy Hung5e58b0a2014-06-23 19:07:29 -0700632 &track.auxLevel, &track.prevAuxLevel, &track.auxInc,
633 &track.mAuxLevel, &track.mPrevAuxLevel, &track.mAuxInc)) {
Andy Hung5866a3b2014-05-29 21:33:13 -0700634 ALOGV("setParameter(%s, AUXLEVEL: %04x)",
Andy Hung6be49402014-05-30 10:42:03 -0700635 target == VOLUME ? "VOLUME" : "RAMP_VOLUME", track.auxLevel);
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800636 invalidateState(1 << name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700637 }
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800638 break;
Glenn Kasten788040c2011-05-05 08:19:00 -0700639 default:
Andy Hunge93b6b72014-07-17 21:30:53 -0700640 if ((unsigned)param >= VOLUME0 && (unsigned)param < VOLUME0 + MAX_NUM_VOLUMES) {
641 if (setVolumeRampVariables(*reinterpret_cast<float*>(value),
642 target == RAMP_VOLUME ? mState.frameCount : 0,
643 &track.volume[param - VOLUME0], &track.prevVolume[param - VOLUME0],
644 &track.volumeInc[param - VOLUME0],
645 &track.mVolume[param - VOLUME0], &track.mPrevVolume[param - VOLUME0],
646 &track.mVolumeInc[param - VOLUME0])) {
647 ALOGV("setParameter(%s, VOLUME%d: %04x)",
648 target == VOLUME ? "VOLUME" : "RAMP_VOLUME", param - VOLUME0,
649 track.volume[param - VOLUME0]);
650 invalidateState(1 << name);
651 }
652 } else {
653 LOG_ALWAYS_FATAL("setParameter volume: bad param %d", param);
654 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700655 }
656 break;
Glenn Kasten788040c2011-05-05 08:19:00 -0700657
658 default:
Glenn Kastenadad3d72014-02-21 14:51:43 -0800659 LOG_ALWAYS_FATAL("setParameter: bad target %d", target);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700660 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700661}
662
Andy Hunge93b6b72014-07-17 21:30:53 -0700663bool AudioMixer::track_t::setResampler(uint32_t trackSampleRate, uint32_t devSampleRate)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700664{
Andy Hunge93b6b72014-07-17 21:30:53 -0700665 if (trackSampleRate != devSampleRate || resampler != NULL) {
666 if (sampleRate != trackSampleRate) {
667 sampleRate = trackSampleRate;
Glenn Kastene0feee32011-12-13 11:53:26 -0800668 if (resampler == NULL) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700669 ALOGV("Creating resampler from track %d Hz to device %d Hz",
670 trackSampleRate, devSampleRate);
Glenn Kastenac602052012-10-01 14:04:31 -0700671 AudioResampler::src_quality quality;
672 // force lowest quality level resampler if use case isn't music or video
673 // FIXME this is flawed for dynamic sample rates, as we choose the resampler
674 // quality level based on the initial ratio, but that could change later.
675 // Should have a way to distinguish tracks with static ratios vs. dynamic ratios.
Andy Hunge93b6b72014-07-17 21:30:53 -0700676 if (!((trackSampleRate == 44100 && devSampleRate == 48000) ||
677 (trackSampleRate == 48000 && devSampleRate == 44100))) {
Andy Hung9e0308c2014-01-30 14:32:31 -0800678 quality = AudioResampler::DYN_LOW_QUALITY;
Glenn Kastenac602052012-10-01 14:04:31 -0700679 } else {
680 quality = AudioResampler::DEFAULT_QUALITY;
681 }
Andy Hung296b7412014-06-17 15:25:47 -0700682
Andy Hunge93b6b72014-07-17 21:30:53 -0700683 // TODO: Remove MONO_HACK. Resampler sees #channels after the downmixer
684 // but if none exists, it is the channel count (1 for mono).
685 const int resamplerChannelCount = downmixerBufferProvider != NULL
686 ? mMixerChannelCount : channelCount;
Andy Hung9a592762014-07-21 21:56:01 -0700687 ALOGVV("Creating resampler:"
688 " format(%#x) channels(%d) devSampleRate(%u) quality(%d)\n",
689 mMixerInFormat, resamplerChannelCount, devSampleRate, quality);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700690 resampler = AudioResampler::create(
Andy Hung3348e362014-07-07 10:21:44 -0700691 mMixerInFormat,
Andy Hunge93b6b72014-07-17 21:30:53 -0700692 resamplerChannelCount,
Glenn Kastenac602052012-10-01 14:04:31 -0700693 devSampleRate, quality);
Glenn Kasten52008f82012-03-18 09:34:41 -0700694 resampler->setLocalTimeFreq(sLocalTimeFreq);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700695 }
696 return true;
697 }
698 }
699 return false;
700}
701
Andy Hung5e58b0a2014-06-23 19:07:29 -0700702/* Checks to see if the volume ramp has completed and clears the increment
703 * variables appropriately.
704 *
705 * FIXME: There is code to handle int/float ramp variable switchover should it not
706 * complete within a mixer buffer processing call, but it is preferred to avoid switchover
707 * due to precision issues. The switchover code is included for legacy code purposes
708 * and can be removed once the integer volume is removed.
709 *
710 * It is not sufficient to clear only the volumeInc integer variable because
711 * if one channel requires ramping, all channels are ramped.
712 *
713 * There is a bit of duplicated code here, but it keeps backward compatibility.
714 */
715inline void AudioMixer::track_t::adjustVolumeRamp(bool aux, bool useFloat)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700716{
Andy Hung5e58b0a2014-06-23 19:07:29 -0700717 if (useFloat) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700718 for (uint32_t i = 0; i < MAX_NUM_VOLUMES; i++) {
Andy Hung5e58b0a2014-06-23 19:07:29 -0700719 if (mVolumeInc[i] != 0 && fabs(mVolume[i] - mPrevVolume[i]) <= fabs(mVolumeInc[i])) {
720 volumeInc[i] = 0;
721 prevVolume[i] = volume[i] << 16;
722 mVolumeInc[i] = 0.;
723 mPrevVolume[i] = mVolume[i];
Andy Hung5e58b0a2014-06-23 19:07:29 -0700724 } else {
725 //ALOGV("ramp: %f %f %f", mVolume[i], mPrevVolume[i], mVolumeInc[i]);
726 prevVolume[i] = u4_28_from_float(mPrevVolume[i]);
727 }
728 }
729 } else {
Andy Hunge93b6b72014-07-17 21:30:53 -0700730 for (uint32_t i = 0; i < MAX_NUM_VOLUMES; i++) {
Andy Hung5e58b0a2014-06-23 19:07:29 -0700731 if (((volumeInc[i]>0) && (((prevVolume[i]+volumeInc[i])>>16) >= volume[i])) ||
732 ((volumeInc[i]<0) && (((prevVolume[i]+volumeInc[i])>>16) <= volume[i]))) {
733 volumeInc[i] = 0;
734 prevVolume[i] = volume[i] << 16;
735 mVolumeInc[i] = 0.;
736 mPrevVolume[i] = mVolume[i];
737 } else {
738 //ALOGV("ramp: %d %d %d", volume[i] << 16, prevVolume[i], volumeInc[i]);
739 mPrevVolume[i] = float_from_u4_28(prevVolume[i]);
740 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700741 }
742 }
Andy Hung5e58b0a2014-06-23 19:07:29 -0700743 /* TODO: aux is always integer regardless of output buffer type */
Mathias Agopian65ab4712010-07-14 17:59:35 -0700744 if (aux) {
745 if (((auxInc>0) && (((prevAuxLevel+auxInc)>>16) >= auxLevel)) ||
Andy Hung5e58b0a2014-06-23 19:07:29 -0700746 ((auxInc<0) && (((prevAuxLevel+auxInc)>>16) <= auxLevel))) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700747 auxInc = 0;
Andy Hung5e58b0a2014-06-23 19:07:29 -0700748 prevAuxLevel = auxLevel << 16;
749 mAuxInc = 0.;
750 mPrevAuxLevel = mAuxLevel;
751 } else {
752 //ALOGV("aux ramp: %d %d %d", auxLevel << 16, prevAuxLevel, auxInc);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700753 }
754 }
755}
756
Glenn Kastenc59c0042012-02-02 14:06:11 -0800757size_t AudioMixer::getUnreleasedFrames(int name) const
Eric Laurent071ccd52011-12-22 16:08:41 -0800758{
759 name -= TRACK0;
760 if (uint32_t(name) < MAX_NUM_TRACKS) {
Glenn Kastenc59c0042012-02-02 14:06:11 -0800761 return mState.tracks[name].getUnreleasedFrames();
Eric Laurent071ccd52011-12-22 16:08:41 -0800762 }
763 return 0;
764}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700765
Glenn Kasten01c4ebf2012-02-22 10:47:35 -0800766void AudioMixer::setBufferProvider(int name, AudioBufferProvider* bufferProvider)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700767{
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800768 name -= TRACK0;
Glenn Kasten5798d4e2012-03-08 12:18:35 -0800769 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700770
Andy Hung1d26ddf2014-05-29 15:53:09 -0700771 if (mState.tracks[name].mInputBufferProvider == bufferProvider) {
772 return; // don't reset any buffer providers if identical.
773 }
Andy Hungef7c7fb2014-05-12 16:51:41 -0700774 if (mState.tracks[name].mReformatBufferProvider != NULL) {
775 mState.tracks[name].mReformatBufferProvider->reset();
776 } else if (mState.tracks[name].downmixerBufferProvider != NULL) {
Andy Hung7f475492014-08-25 16:36:37 -0700777 mState.tracks[name].downmixerBufferProvider->reset();
778 } else if (mState.tracks[name].mPostDownmixReformatBufferProvider != NULL) {
779 mState.tracks[name].mPostDownmixReformatBufferProvider->reset();
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700780 }
Andy Hungef7c7fb2014-05-12 16:51:41 -0700781
782 mState.tracks[name].mInputBufferProvider = bufferProvider;
Andy Hung0f451e92014-08-04 21:28:47 -0700783 mState.tracks[name].reconfigureBufferProviders();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700784}
785
786
John Grossman4ff14ba2012-02-08 16:37:41 -0800787void AudioMixer::process(int64_t pts)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700788{
John Grossman4ff14ba2012-02-08 16:37:41 -0800789 mState.hook(&mState, pts);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700790}
791
792
John Grossman4ff14ba2012-02-08 16:37:41 -0800793void AudioMixer::process__validate(state_t* state, int64_t pts)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700794{
Steve Block5ff1dd52012-01-05 23:22:43 +0000795 ALOGW_IF(!state->needsChanged,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700796 "in process__validate() but nothing's invalid");
797
798 uint32_t changed = state->needsChanged;
799 state->needsChanged = 0; // clear the validation flag
800
801 // recompute which tracks are enabled / disabled
802 uint32_t enabled = 0;
803 uint32_t disabled = 0;
804 while (changed) {
805 const int i = 31 - __builtin_clz(changed);
806 const uint32_t mask = 1<<i;
807 changed &= ~mask;
808 track_t& t = state->tracks[i];
809 (t.enabled ? enabled : disabled) |= mask;
810 }
811 state->enabledTracks &= ~disabled;
812 state->enabledTracks |= enabled;
813
814 // compute everything we need...
815 int countActiveTracks = 0;
Andy Hung395db4b2014-08-25 17:15:29 -0700816 // TODO: fix all16BitsStereNoResample logic to
817 // either properly handle muted tracks (it should ignore them)
818 // or remove altogether as an obsolete optimization.
Glenn Kasten4c340c62012-01-27 12:33:54 -0800819 bool all16BitsStereoNoResample = true;
820 bool resampling = false;
821 bool volumeRamp = false;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700822 uint32_t en = state->enabledTracks;
823 while (en) {
824 const int i = 31 - __builtin_clz(en);
825 en &= ~(1<<i);
826
827 countActiveTracks++;
828 track_t& t = state->tracks[i];
829 uint32_t n = 0;
Glenn Kastend6fadf02013-10-30 14:37:29 -0700830 // FIXME can overflow (mask is only 3 bits)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700831 n |= NEEDS_CHANNEL_1 + t.channelCount - 1;
Glenn Kastend6fadf02013-10-30 14:37:29 -0700832 if (t.doesResample()) {
833 n |= NEEDS_RESAMPLE;
834 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700835 if (t.auxLevel != 0 && t.auxBuffer != NULL) {
Glenn Kastend6fadf02013-10-30 14:37:29 -0700836 n |= NEEDS_AUX;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700837 }
838
839 if (t.volumeInc[0]|t.volumeInc[1]) {
Glenn Kasten4c340c62012-01-27 12:33:54 -0800840 volumeRamp = true;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700841 } else if (!t.doesResample() && t.volumeRL == 0) {
Glenn Kastend6fadf02013-10-30 14:37:29 -0700842 n |= NEEDS_MUTE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700843 }
844 t.needs = n;
845
Glenn Kastend6fadf02013-10-30 14:37:29 -0700846 if (n & NEEDS_MUTE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700847 t.hook = track__nop;
848 } else {
Glenn Kastend6fadf02013-10-30 14:37:29 -0700849 if (n & NEEDS_AUX) {
Glenn Kasten4c340c62012-01-27 12:33:54 -0800850 all16BitsStereoNoResample = false;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700851 }
Glenn Kastend6fadf02013-10-30 14:37:29 -0700852 if (n & NEEDS_RESAMPLE) {
Glenn Kasten4c340c62012-01-27 12:33:54 -0800853 all16BitsStereoNoResample = false;
854 resampling = true;
Andy Hunge93b6b72014-07-17 21:30:53 -0700855 t.hook = getTrackHook(TRACKTYPE_RESAMPLE, t.mMixerChannelCount,
Andy Hung296b7412014-06-17 15:25:47 -0700856 t.mMixerInFormat, t.mMixerFormat);
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700857 ALOGV_IF((n & NEEDS_CHANNEL_COUNT__MASK) > NEEDS_CHANNEL_2,
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700858 "Track %d needs downmix + resample", i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700859 } else {
860 if ((n & NEEDS_CHANNEL_COUNT__MASK) == NEEDS_CHANNEL_1){
Andy Hunge93b6b72014-07-17 21:30:53 -0700861 t.hook = getTrackHook(
862 t.mMixerChannelCount == 2 // TODO: MONO_HACK.
863 ? TRACKTYPE_NORESAMPLEMONO : TRACKTYPE_NORESAMPLE,
864 t.mMixerChannelCount,
Andy Hung296b7412014-06-17 15:25:47 -0700865 t.mMixerInFormat, t.mMixerFormat);
Glenn Kasten4c340c62012-01-27 12:33:54 -0800866 all16BitsStereoNoResample = false;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700867 }
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700868 if ((n & NEEDS_CHANNEL_COUNT__MASK) >= NEEDS_CHANNEL_2){
Andy Hunge93b6b72014-07-17 21:30:53 -0700869 t.hook = getTrackHook(TRACKTYPE_NORESAMPLE, t.mMixerChannelCount,
Andy Hung296b7412014-06-17 15:25:47 -0700870 t.mMixerInFormat, t.mMixerFormat);
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700871 ALOGV_IF((n & NEEDS_CHANNEL_COUNT__MASK) > NEEDS_CHANNEL_2,
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700872 "Track %d needs downmix", i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700873 }
874 }
875 }
876 }
877
878 // select the processing hooks
879 state->hook = process__nop;
Glenn Kasten34fca342013-08-13 09:48:14 -0700880 if (countActiveTracks > 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700881 if (resampling) {
882 if (!state->outputTemp) {
883 state->outputTemp = new int32_t[MAX_NUM_CHANNELS * state->frameCount];
884 }
885 if (!state->resampleTemp) {
886 state->resampleTemp = new int32_t[MAX_NUM_CHANNELS * state->frameCount];
887 }
888 state->hook = process__genericResampling;
889 } else {
890 if (state->outputTemp) {
891 delete [] state->outputTemp;
Glenn Kastene0feee32011-12-13 11:53:26 -0800892 state->outputTemp = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700893 }
894 if (state->resampleTemp) {
895 delete [] state->resampleTemp;
Glenn Kastene0feee32011-12-13 11:53:26 -0800896 state->resampleTemp = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700897 }
898 state->hook = process__genericNoResampling;
899 if (all16BitsStereoNoResample && !volumeRamp) {
900 if (countActiveTracks == 1) {
Andy Hung296b7412014-06-17 15:25:47 -0700901 const int i = 31 - __builtin_clz(state->enabledTracks);
902 track_t& t = state->tracks[i];
Andy Hung395db4b2014-08-25 17:15:29 -0700903 if ((t.needs & NEEDS_MUTE) == 0) {
904 // The check prevents a muted track from acquiring a process hook.
905 //
906 // This is dangerous if the track is MONO as that requires
907 // special case handling due to implicit channel duplication.
908 // Stereo or Multichannel should actually be fine here.
909 state->hook = getProcessHook(PROCESSTYPE_NORESAMPLEONETRACK,
910 t.mMixerChannelCount, t.mMixerInFormat, t.mMixerFormat);
911 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700912 }
913 }
914 }
915 }
916
Steve Block3856b092011-10-20 11:56:00 +0100917 ALOGV("mixer configuration change: %d activeTracks (%08x) "
Mathias Agopian65ab4712010-07-14 17:59:35 -0700918 "all16BitsStereoNoResample=%d, resampling=%d, volumeRamp=%d",
919 countActiveTracks, state->enabledTracks,
920 all16BitsStereoNoResample, resampling, volumeRamp);
921
John Grossman4ff14ba2012-02-08 16:37:41 -0800922 state->hook(state, pts);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700923
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800924 // Now that the volume ramp has been done, set optimal state and
925 // track hooks for subsequent mixer process
Glenn Kasten34fca342013-08-13 09:48:14 -0700926 if (countActiveTracks > 0) {
Glenn Kasten4c340c62012-01-27 12:33:54 -0800927 bool allMuted = true;
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800928 uint32_t en = state->enabledTracks;
929 while (en) {
930 const int i = 31 - __builtin_clz(en);
931 en &= ~(1<<i);
932 track_t& t = state->tracks[i];
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700933 if (!t.doesResample() && t.volumeRL == 0) {
Glenn Kastend6fadf02013-10-30 14:37:29 -0700934 t.needs |= NEEDS_MUTE;
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800935 t.hook = track__nop;
936 } else {
Glenn Kasten4c340c62012-01-27 12:33:54 -0800937 allMuted = false;
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800938 }
939 }
940 if (allMuted) {
941 state->hook = process__nop;
942 } else if (all16BitsStereoNoResample) {
943 if (countActiveTracks == 1) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700944 const int i = 31 - __builtin_clz(state->enabledTracks);
945 track_t& t = state->tracks[i];
Andy Hung395db4b2014-08-25 17:15:29 -0700946 // Muted single tracks handled by allMuted above.
Andy Hunge93b6b72014-07-17 21:30:53 -0700947 state->hook = getProcessHook(PROCESSTYPE_NORESAMPLEONETRACK,
948 t.mMixerChannelCount, t.mMixerInFormat, t.mMixerFormat);
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800949 }
950 }
951 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700952}
953
Mathias Agopian65ab4712010-07-14 17:59:35 -0700954
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700955void AudioMixer::track__genericResample(track_t* t, int32_t* out, size_t outFrameCount,
956 int32_t* temp, int32_t* aux)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700957{
Andy Hung296b7412014-06-17 15:25:47 -0700958 ALOGVV("track__genericResample\n");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700959 t->resampler->setSampleRate(t->sampleRate);
960
961 // ramp gain - resample to temp buffer and scale/mix in 2nd step
962 if (aux != NULL) {
963 // always resample with unity gain when sending to auxiliary buffer to be able
964 // to apply send level after resampling
Andy Hung5e58b0a2014-06-23 19:07:29 -0700965 t->resampler->setVolume(UNITY_GAIN_FLOAT, UNITY_GAIN_FLOAT);
Andy Hunge93b6b72014-07-17 21:30:53 -0700966 memset(temp, 0, outFrameCount * t->mMixerChannelCount * sizeof(int32_t));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700967 t->resampler->resample(temp, outFrameCount, t->bufferProvider);
Glenn Kastenf6b16782011-12-15 09:51:17 -0800968 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700969 volumeRampStereo(t, out, outFrameCount, temp, aux);
970 } else {
971 volumeStereo(t, out, outFrameCount, temp, aux);
972 }
973 } else {
Glenn Kastenf6b16782011-12-15 09:51:17 -0800974 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) {
Andy Hung5e58b0a2014-06-23 19:07:29 -0700975 t->resampler->setVolume(UNITY_GAIN_FLOAT, UNITY_GAIN_FLOAT);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700976 memset(temp, 0, outFrameCount * MAX_NUM_CHANNELS * sizeof(int32_t));
977 t->resampler->resample(temp, outFrameCount, t->bufferProvider);
978 volumeRampStereo(t, out, outFrameCount, temp, aux);
979 }
980
981 // constant gain
982 else {
Andy Hung5e58b0a2014-06-23 19:07:29 -0700983 t->resampler->setVolume(t->mVolume[0], t->mVolume[1]);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700984 t->resampler->resample(out, outFrameCount, t->bufferProvider);
985 }
986 }
987}
988
Andy Hungee931ff2014-01-28 13:44:14 -0800989void AudioMixer::track__nop(track_t* t __unused, int32_t* out __unused,
990 size_t outFrameCount __unused, int32_t* temp __unused, int32_t* aux __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700991{
992}
993
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700994void AudioMixer::volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp,
995 int32_t* aux)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700996{
997 int32_t vl = t->prevVolume[0];
998 int32_t vr = t->prevVolume[1];
999 const int32_t vlInc = t->volumeInc[0];
1000 const int32_t vrInc = t->volumeInc[1];
1001
Steve Blockb8a80522011-12-20 16:23:08 +00001002 //ALOGD("[0] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
Mathias Agopian65ab4712010-07-14 17:59:35 -07001003 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
1004 // (vl + vlInc*frameCount)/65536.0f, frameCount);
1005
1006 // ramp volume
Glenn Kastenf6b16782011-12-15 09:51:17 -08001007 if (CC_UNLIKELY(aux != NULL)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001008 int32_t va = t->prevAuxLevel;
1009 const int32_t vaInc = t->auxInc;
1010 int32_t l;
1011 int32_t r;
1012
1013 do {
1014 l = (*temp++ >> 12);
1015 r = (*temp++ >> 12);
1016 *out++ += (vl >> 16) * l;
1017 *out++ += (vr >> 16) * r;
1018 *aux++ += (va >> 17) * (l + r);
1019 vl += vlInc;
1020 vr += vrInc;
1021 va += vaInc;
1022 } while (--frameCount);
1023 t->prevAuxLevel = va;
1024 } else {
1025 do {
1026 *out++ += (vl >> 16) * (*temp++ >> 12);
1027 *out++ += (vr >> 16) * (*temp++ >> 12);
1028 vl += vlInc;
1029 vr += vrInc;
1030 } while (--frameCount);
1031 }
1032 t->prevVolume[0] = vl;
1033 t->prevVolume[1] = vr;
Glenn Kastena1117922012-01-26 10:53:32 -08001034 t->adjustVolumeRamp(aux != NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001035}
1036
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001037void AudioMixer::volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp,
1038 int32_t* aux)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001039{
1040 const int16_t vl = t->volume[0];
1041 const int16_t vr = t->volume[1];
1042
Glenn Kastenf6b16782011-12-15 09:51:17 -08001043 if (CC_UNLIKELY(aux != NULL)) {
Glenn Kasten3b81aca2012-01-27 15:26:23 -08001044 const int16_t va = t->auxLevel;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001045 do {
1046 int16_t l = (int16_t)(*temp++ >> 12);
1047 int16_t r = (int16_t)(*temp++ >> 12);
1048 out[0] = mulAdd(l, vl, out[0]);
1049 int16_t a = (int16_t)(((int32_t)l + r) >> 1);
1050 out[1] = mulAdd(r, vr, out[1]);
1051 out += 2;
1052 aux[0] = mulAdd(a, va, aux[0]);
1053 aux++;
1054 } while (--frameCount);
1055 } else {
1056 do {
1057 int16_t l = (int16_t)(*temp++ >> 12);
1058 int16_t r = (int16_t)(*temp++ >> 12);
1059 out[0] = mulAdd(l, vl, out[0]);
1060 out[1] = mulAdd(r, vr, out[1]);
1061 out += 2;
1062 } while (--frameCount);
1063 }
1064}
1065
Andy Hungee931ff2014-01-28 13:44:14 -08001066void AudioMixer::track__16BitsStereo(track_t* t, int32_t* out, size_t frameCount,
1067 int32_t* temp __unused, int32_t* aux)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001068{
Andy Hung296b7412014-06-17 15:25:47 -07001069 ALOGVV("track__16BitsStereo\n");
Glenn Kasten54c3b662012-01-06 07:46:30 -08001070 const int16_t *in = static_cast<const int16_t *>(t->in);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001071
Glenn Kastenf6b16782011-12-15 09:51:17 -08001072 if (CC_UNLIKELY(aux != NULL)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001073 int32_t l;
1074 int32_t r;
1075 // ramp gain
Glenn Kastenf6b16782011-12-15 09:51:17 -08001076 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001077 int32_t vl = t->prevVolume[0];
1078 int32_t vr = t->prevVolume[1];
1079 int32_t va = t->prevAuxLevel;
1080 const int32_t vlInc = t->volumeInc[0];
1081 const int32_t vrInc = t->volumeInc[1];
1082 const int32_t vaInc = t->auxInc;
Steve Blockb8a80522011-12-20 16:23:08 +00001083 // ALOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
Mathias Agopian65ab4712010-07-14 17:59:35 -07001084 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
1085 // (vl + vlInc*frameCount)/65536.0f, frameCount);
1086
1087 do {
1088 l = (int32_t)*in++;
1089 r = (int32_t)*in++;
1090 *out++ += (vl >> 16) * l;
1091 *out++ += (vr >> 16) * r;
1092 *aux++ += (va >> 17) * (l + r);
1093 vl += vlInc;
1094 vr += vrInc;
1095 va += vaInc;
1096 } while (--frameCount);
1097
1098 t->prevVolume[0] = vl;
1099 t->prevVolume[1] = vr;
1100 t->prevAuxLevel = va;
1101 t->adjustVolumeRamp(true);
1102 }
1103
1104 // constant gain
1105 else {
1106 const uint32_t vrl = t->volumeRL;
1107 const int16_t va = (int16_t)t->auxLevel;
1108 do {
Glenn Kasten54c3b662012-01-06 07:46:30 -08001109 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001110 int16_t a = (int16_t)(((int32_t)in[0] + in[1]) >> 1);
1111 in += 2;
1112 out[0] = mulAddRL(1, rl, vrl, out[0]);
1113 out[1] = mulAddRL(0, rl, vrl, out[1]);
1114 out += 2;
1115 aux[0] = mulAdd(a, va, aux[0]);
1116 aux++;
1117 } while (--frameCount);
1118 }
1119 } else {
1120 // ramp gain
Glenn Kastenf6b16782011-12-15 09:51:17 -08001121 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001122 int32_t vl = t->prevVolume[0];
1123 int32_t vr = t->prevVolume[1];
1124 const int32_t vlInc = t->volumeInc[0];
1125 const int32_t vrInc = t->volumeInc[1];
1126
Steve Blockb8a80522011-12-20 16:23:08 +00001127 // ALOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
Mathias Agopian65ab4712010-07-14 17:59:35 -07001128 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
1129 // (vl + vlInc*frameCount)/65536.0f, frameCount);
1130
1131 do {
1132 *out++ += (vl >> 16) * (int32_t) *in++;
1133 *out++ += (vr >> 16) * (int32_t) *in++;
1134 vl += vlInc;
1135 vr += vrInc;
1136 } while (--frameCount);
1137
1138 t->prevVolume[0] = vl;
1139 t->prevVolume[1] = vr;
1140 t->adjustVolumeRamp(false);
1141 }
1142
1143 // constant gain
1144 else {
1145 const uint32_t vrl = t->volumeRL;
1146 do {
Glenn Kasten54c3b662012-01-06 07:46:30 -08001147 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001148 in += 2;
1149 out[0] = mulAddRL(1, rl, vrl, out[0]);
1150 out[1] = mulAddRL(0, rl, vrl, out[1]);
1151 out += 2;
1152 } while (--frameCount);
1153 }
1154 }
1155 t->in = in;
1156}
1157
Andy Hungee931ff2014-01-28 13:44:14 -08001158void AudioMixer::track__16BitsMono(track_t* t, int32_t* out, size_t frameCount,
1159 int32_t* temp __unused, int32_t* aux)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001160{
Andy Hung296b7412014-06-17 15:25:47 -07001161 ALOGVV("track__16BitsMono\n");
Glenn Kasten54c3b662012-01-06 07:46:30 -08001162 const int16_t *in = static_cast<int16_t const *>(t->in);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001163
Glenn Kastenf6b16782011-12-15 09:51:17 -08001164 if (CC_UNLIKELY(aux != NULL)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001165 // ramp gain
Glenn Kastenf6b16782011-12-15 09:51:17 -08001166 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001167 int32_t vl = t->prevVolume[0];
1168 int32_t vr = t->prevVolume[1];
1169 int32_t va = t->prevAuxLevel;
1170 const int32_t vlInc = t->volumeInc[0];
1171 const int32_t vrInc = t->volumeInc[1];
1172 const int32_t vaInc = t->auxInc;
1173
Steve Blockb8a80522011-12-20 16:23:08 +00001174 // ALOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
Mathias Agopian65ab4712010-07-14 17:59:35 -07001175 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
1176 // (vl + vlInc*frameCount)/65536.0f, frameCount);
1177
1178 do {
1179 int32_t l = *in++;
1180 *out++ += (vl >> 16) * l;
1181 *out++ += (vr >> 16) * l;
1182 *aux++ += (va >> 16) * l;
1183 vl += vlInc;
1184 vr += vrInc;
1185 va += vaInc;
1186 } while (--frameCount);
1187
1188 t->prevVolume[0] = vl;
1189 t->prevVolume[1] = vr;
1190 t->prevAuxLevel = va;
1191 t->adjustVolumeRamp(true);
1192 }
1193 // constant gain
1194 else {
1195 const int16_t vl = t->volume[0];
1196 const int16_t vr = t->volume[1];
1197 const int16_t va = (int16_t)t->auxLevel;
1198 do {
1199 int16_t l = *in++;
1200 out[0] = mulAdd(l, vl, out[0]);
1201 out[1] = mulAdd(l, vr, out[1]);
1202 out += 2;
1203 aux[0] = mulAdd(l, va, aux[0]);
1204 aux++;
1205 } while (--frameCount);
1206 }
1207 } else {
1208 // ramp gain
Glenn Kastenf6b16782011-12-15 09:51:17 -08001209 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001210 int32_t vl = t->prevVolume[0];
1211 int32_t vr = t->prevVolume[1];
1212 const int32_t vlInc = t->volumeInc[0];
1213 const int32_t vrInc = t->volumeInc[1];
1214
Steve Blockb8a80522011-12-20 16:23:08 +00001215 // ALOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
Mathias Agopian65ab4712010-07-14 17:59:35 -07001216 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
1217 // (vl + vlInc*frameCount)/65536.0f, frameCount);
1218
1219 do {
1220 int32_t l = *in++;
1221 *out++ += (vl >> 16) * l;
1222 *out++ += (vr >> 16) * l;
1223 vl += vlInc;
1224 vr += vrInc;
1225 } while (--frameCount);
1226
1227 t->prevVolume[0] = vl;
1228 t->prevVolume[1] = vr;
1229 t->adjustVolumeRamp(false);
1230 }
1231 // constant gain
1232 else {
1233 const int16_t vl = t->volume[0];
1234 const int16_t vr = t->volume[1];
1235 do {
1236 int16_t l = *in++;
1237 out[0] = mulAdd(l, vl, out[0]);
1238 out[1] = mulAdd(l, vr, out[1]);
1239 out += 2;
1240 } while (--frameCount);
1241 }
1242 }
1243 t->in = in;
1244}
1245
Mathias Agopian65ab4712010-07-14 17:59:35 -07001246// no-op case
John Grossman4ff14ba2012-02-08 16:37:41 -08001247void AudioMixer::process__nop(state_t* state, int64_t pts)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001248{
Andy Hung296b7412014-06-17 15:25:47 -07001249 ALOGVV("process__nop\n");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001250 uint32_t e0 = state->enabledTracks;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001251 while (e0) {
1252 // process by group of tracks with same output buffer to
1253 // avoid multiple memset() on same buffer
1254 uint32_t e1 = e0, e2 = e0;
1255 int i = 31 - __builtin_clz(e1);
Glenn Kastenfc900c92013-02-18 12:47:49 -08001256 {
1257 track_t& t1 = state->tracks[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001258 e2 &= ~(1<<i);
Glenn Kastenfc900c92013-02-18 12:47:49 -08001259 while (e2) {
1260 i = 31 - __builtin_clz(e2);
1261 e2 &= ~(1<<i);
1262 track_t& t2 = state->tracks[i];
1263 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) {
1264 e1 &= ~(1<<i);
1265 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001266 }
Glenn Kastenfc900c92013-02-18 12:47:49 -08001267 e0 &= ~(e1);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001268
Andy Hunge93b6b72014-07-17 21:30:53 -07001269 memset(t1.mainBuffer, 0, state->frameCount * t1.mMixerChannelCount
Andy Hung78820702014-02-28 16:23:02 -08001270 * audio_bytes_per_sample(t1.mMixerFormat));
Glenn Kastenfc900c92013-02-18 12:47:49 -08001271 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001272
1273 while (e1) {
1274 i = 31 - __builtin_clz(e1);
1275 e1 &= ~(1<<i);
Glenn Kastenfc900c92013-02-18 12:47:49 -08001276 {
1277 track_t& t3 = state->tracks[i];
1278 size_t outFrames = state->frameCount;
1279 while (outFrames) {
1280 t3.buffer.frameCount = outFrames;
1281 int64_t outputPTS = calculateOutputPTS(
1282 t3, pts, state->frameCount - outFrames);
1283 t3.bufferProvider->getNextBuffer(&t3.buffer, outputPTS);
1284 if (t3.buffer.raw == NULL) break;
1285 outFrames -= t3.buffer.frameCount;
1286 t3.bufferProvider->releaseBuffer(&t3.buffer);
1287 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001288 }
1289 }
1290 }
1291}
1292
1293// generic code without resampling
John Grossman4ff14ba2012-02-08 16:37:41 -08001294void AudioMixer::process__genericNoResampling(state_t* state, int64_t pts)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001295{
Andy Hung296b7412014-06-17 15:25:47 -07001296 ALOGVV("process__genericNoResampling\n");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001297 int32_t outTemp[BLOCKSIZE * MAX_NUM_CHANNELS] __attribute__((aligned(32)));
1298
1299 // acquire each track's buffer
1300 uint32_t enabledTracks = state->enabledTracks;
1301 uint32_t e0 = enabledTracks;
1302 while (e0) {
1303 const int i = 31 - __builtin_clz(e0);
1304 e0 &= ~(1<<i);
1305 track_t& t = state->tracks[i];
1306 t.buffer.frameCount = state->frameCount;
John Grossman4ff14ba2012-02-08 16:37:41 -08001307 t.bufferProvider->getNextBuffer(&t.buffer, pts);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001308 t.frameCount = t.buffer.frameCount;
1309 t.in = t.buffer.raw;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001310 }
1311
1312 e0 = enabledTracks;
1313 while (e0) {
1314 // process by group of tracks with same output buffer to
1315 // optimize cache use
1316 uint32_t e1 = e0, e2 = e0;
1317 int j = 31 - __builtin_clz(e1);
1318 track_t& t1 = state->tracks[j];
1319 e2 &= ~(1<<j);
1320 while (e2) {
1321 j = 31 - __builtin_clz(e2);
1322 e2 &= ~(1<<j);
1323 track_t& t2 = state->tracks[j];
Glenn Kastenf6b16782011-12-15 09:51:17 -08001324 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001325 e1 &= ~(1<<j);
1326 }
1327 }
1328 e0 &= ~(e1);
1329 // this assumes output 16 bits stereo, no resampling
1330 int32_t *out = t1.mainBuffer;
1331 size_t numFrames = 0;
1332 do {
1333 memset(outTemp, 0, sizeof(outTemp));
1334 e2 = e1;
1335 while (e2) {
1336 const int i = 31 - __builtin_clz(e2);
1337 e2 &= ~(1<<i);
1338 track_t& t = state->tracks[i];
1339 size_t outFrames = BLOCKSIZE;
1340 int32_t *aux = NULL;
Glenn Kastend6fadf02013-10-30 14:37:29 -07001341 if (CC_UNLIKELY(t.needs & NEEDS_AUX)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001342 aux = t.auxBuffer + numFrames;
1343 }
1344 while (outFrames) {
Gaurav Kumar7e79cd22014-01-06 10:57:18 +05301345 // t.in == NULL can happen if the track was flushed just after having
1346 // been enabled for mixing.
1347 if (t.in == NULL) {
1348 enabledTracks &= ~(1<<i);
1349 e1 &= ~(1<<i);
1350 break;
1351 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001352 size_t inFrames = (t.frameCount > outFrames)?outFrames:t.frameCount;
Glenn Kasten34fca342013-08-13 09:48:14 -07001353 if (inFrames > 0) {
Andy Hunge93b6b72014-07-17 21:30:53 -07001354 t.hook(&t, outTemp + (BLOCKSIZE - outFrames) * t.mMixerChannelCount,
1355 inFrames, state->resampleTemp, aux);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001356 t.frameCount -= inFrames;
1357 outFrames -= inFrames;
Glenn Kastenf6b16782011-12-15 09:51:17 -08001358 if (CC_UNLIKELY(aux != NULL)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001359 aux += inFrames;
1360 }
1361 }
1362 if (t.frameCount == 0 && outFrames) {
1363 t.bufferProvider->releaseBuffer(&t.buffer);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001364 t.buffer.frameCount = (state->frameCount - numFrames) -
1365 (BLOCKSIZE - outFrames);
John Grossman4ff14ba2012-02-08 16:37:41 -08001366 int64_t outputPTS = calculateOutputPTS(
1367 t, pts, numFrames + (BLOCKSIZE - outFrames));
1368 t.bufferProvider->getNextBuffer(&t.buffer, outputPTS);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001369 t.in = t.buffer.raw;
1370 if (t.in == NULL) {
1371 enabledTracks &= ~(1<<i);
1372 e1 &= ~(1<<i);
1373 break;
1374 }
1375 t.frameCount = t.buffer.frameCount;
1376 }
1377 }
1378 }
Andy Hung296b7412014-06-17 15:25:47 -07001379
1380 convertMixerFormat(out, t1.mMixerFormat, outTemp, t1.mMixerInFormat,
Andy Hunge93b6b72014-07-17 21:30:53 -07001381 BLOCKSIZE * t1.mMixerChannelCount);
Andy Hung296b7412014-06-17 15:25:47 -07001382 // TODO: fix ugly casting due to choice of out pointer type
1383 out = reinterpret_cast<int32_t*>((uint8_t*)out
Andy Hunge93b6b72014-07-17 21:30:53 -07001384 + BLOCKSIZE * t1.mMixerChannelCount
1385 * audio_bytes_per_sample(t1.mMixerFormat));
Mathias Agopian65ab4712010-07-14 17:59:35 -07001386 numFrames += BLOCKSIZE;
1387 } while (numFrames < state->frameCount);
1388 }
1389
1390 // release each track's buffer
1391 e0 = enabledTracks;
1392 while (e0) {
1393 const int i = 31 - __builtin_clz(e0);
1394 e0 &= ~(1<<i);
1395 track_t& t = state->tracks[i];
1396 t.bufferProvider->releaseBuffer(&t.buffer);
1397 }
1398}
1399
1400
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -08001401// generic code with resampling
John Grossman4ff14ba2012-02-08 16:37:41 -08001402void AudioMixer::process__genericResampling(state_t* state, int64_t pts)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001403{
Andy Hung296b7412014-06-17 15:25:47 -07001404 ALOGVV("process__genericResampling\n");
Glenn Kasten54c3b662012-01-06 07:46:30 -08001405 // this const just means that local variable outTemp doesn't change
Mathias Agopian65ab4712010-07-14 17:59:35 -07001406 int32_t* const outTemp = state->outputTemp;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001407 size_t numFrames = state->frameCount;
1408
1409 uint32_t e0 = state->enabledTracks;
1410 while (e0) {
1411 // process by group of tracks with same output buffer
1412 // to optimize cache use
1413 uint32_t e1 = e0, e2 = e0;
1414 int j = 31 - __builtin_clz(e1);
1415 track_t& t1 = state->tracks[j];
1416 e2 &= ~(1<<j);
1417 while (e2) {
1418 j = 31 - __builtin_clz(e2);
1419 e2 &= ~(1<<j);
1420 track_t& t2 = state->tracks[j];
Glenn Kastenf6b16782011-12-15 09:51:17 -08001421 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001422 e1 &= ~(1<<j);
1423 }
1424 }
1425 e0 &= ~(e1);
1426 int32_t *out = t1.mainBuffer;
Andy Hunge93b6b72014-07-17 21:30:53 -07001427 memset(outTemp, 0, sizeof(*outTemp) * t1.mMixerChannelCount * state->frameCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001428 while (e1) {
1429 const int i = 31 - __builtin_clz(e1);
1430 e1 &= ~(1<<i);
1431 track_t& t = state->tracks[i];
1432 int32_t *aux = NULL;
Glenn Kastend6fadf02013-10-30 14:37:29 -07001433 if (CC_UNLIKELY(t.needs & NEEDS_AUX)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001434 aux = t.auxBuffer;
1435 }
1436
1437 // this is a little goofy, on the resampling case we don't
1438 // acquire/release the buffers because it's done by
1439 // the resampler.
Glenn Kastend6fadf02013-10-30 14:37:29 -07001440 if (t.needs & NEEDS_RESAMPLE) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001441 t.resampler->setPTS(pts);
Glenn Kastena1117922012-01-26 10:53:32 -08001442 t.hook(&t, outTemp, numFrames, state->resampleTemp, aux);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001443 } else {
1444
1445 size_t outFrames = 0;
1446
1447 while (outFrames < numFrames) {
1448 t.buffer.frameCount = numFrames - outFrames;
John Grossman4ff14ba2012-02-08 16:37:41 -08001449 int64_t outputPTS = calculateOutputPTS(t, pts, outFrames);
1450 t.bufferProvider->getNextBuffer(&t.buffer, outputPTS);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001451 t.in = t.buffer.raw;
1452 // t.in == NULL can happen if the track was flushed just after having
1453 // been enabled for mixing.
1454 if (t.in == NULL) break;
1455
Glenn Kastenf6b16782011-12-15 09:51:17 -08001456 if (CC_UNLIKELY(aux != NULL)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001457 aux += outFrames;
1458 }
Andy Hunge93b6b72014-07-17 21:30:53 -07001459 t.hook(&t, outTemp + outFrames * t.mMixerChannelCount, t.buffer.frameCount,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001460 state->resampleTemp, aux);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001461 outFrames += t.buffer.frameCount;
1462 t.bufferProvider->releaseBuffer(&t.buffer);
1463 }
1464 }
1465 }
Andy Hunge93b6b72014-07-17 21:30:53 -07001466 convertMixerFormat(out, t1.mMixerFormat,
1467 outTemp, t1.mMixerInFormat, numFrames * t1.mMixerChannelCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001468 }
1469}
1470
1471// one track, 16 bits stereo without resampling is the most common case
John Grossman4ff14ba2012-02-08 16:37:41 -08001472void AudioMixer::process__OneTrack16BitsStereoNoResampling(state_t* state,
1473 int64_t pts)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001474{
Andy Hung296b7412014-06-17 15:25:47 -07001475 ALOGVV("process__OneTrack16BitsStereoNoResampling\n");
Glenn Kasten99e53b82012-01-19 08:59:58 -08001476 // This method is only called when state->enabledTracks has exactly
1477 // one bit set. The asserts below would verify this, but are commented out
1478 // since the whole point of this method is to optimize performance.
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001479 //ALOG_ASSERT(0 != state->enabledTracks, "no tracks enabled");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001480 const int i = 31 - __builtin_clz(state->enabledTracks);
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001481 //ALOG_ASSERT((1 << i) == state->enabledTracks, "more than 1 track enabled");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001482 const track_t& t = state->tracks[i];
1483
1484 AudioBufferProvider::Buffer& b(t.buffer);
1485
1486 int32_t* out = t.mainBuffer;
Andy Hungf8a106a2014-05-29 18:52:38 -07001487 float *fout = reinterpret_cast<float*>(out);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001488 size_t numFrames = state->frameCount;
1489
1490 const int16_t vl = t.volume[0];
1491 const int16_t vr = t.volume[1];
1492 const uint32_t vrl = t.volumeRL;
1493 while (numFrames) {
1494 b.frameCount = numFrames;
John Grossman4ff14ba2012-02-08 16:37:41 -08001495 int64_t outputPTS = calculateOutputPTS(t, pts, out - t.mainBuffer);
1496 t.bufferProvider->getNextBuffer(&b, outputPTS);
Glenn Kasten54c3b662012-01-06 07:46:30 -08001497 const int16_t *in = b.i16;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001498
1499 // in == NULL can happen if the track was flushed just after having
1500 // been enabled for mixing.
Andy Hungf8a106a2014-05-29 18:52:38 -07001501 if (in == NULL || (((uintptr_t)in) & 3)) {
1502 memset(out, 0, numFrames
Andy Hunge93b6b72014-07-17 21:30:53 -07001503 * t.mMixerChannelCount * audio_bytes_per_sample(t.mMixerFormat));
Andy Hung395db4b2014-08-25 17:15:29 -07001504 ALOGE_IF((((uintptr_t)in) & 3),
1505 "process__OneTrack16BitsStereoNoResampling: misaligned buffer"
1506 " %p track %d, channels %d, needs %08x, volume %08x vfl %f vfr %f",
1507 in, i, t.channelCount, t.needs, vrl, t.mVolume[0], t.mVolume[1]);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001508 return;
1509 }
1510 size_t outFrames = b.frameCount;
1511
Andy Hung78820702014-02-28 16:23:02 -08001512 switch (t.mMixerFormat) {
Andy Hungf8a106a2014-05-29 18:52:38 -07001513 case AUDIO_FORMAT_PCM_FLOAT:
Mathias Agopian65ab4712010-07-14 17:59:35 -07001514 do {
Glenn Kasten54c3b662012-01-06 07:46:30 -08001515 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001516 in += 2;
Andy Hunga1ab7cc2014-02-24 19:26:52 -08001517 int32_t l = mulRL(1, rl, vrl);
1518 int32_t r = mulRL(0, rl, vrl);
Andy Hung84a0c6e2014-04-02 11:24:53 -07001519 *fout++ = float_from_q4_27(l);
1520 *fout++ = float_from_q4_27(r);
Andy Hung3375bde2014-02-28 15:51:47 -08001521 // Note: In case of later int16_t sink output,
1522 // conversion and clamping is done by memcpy_to_i16_from_float().
Mathias Agopian65ab4712010-07-14 17:59:35 -07001523 } while (--outFrames);
Andy Hungf8a106a2014-05-29 18:52:38 -07001524 break;
Andy Hunga1ab7cc2014-02-24 19:26:52 -08001525 case AUDIO_FORMAT_PCM_16_BIT:
Andy Hung97ae8242014-05-30 10:35:47 -07001526 if (CC_UNLIKELY(uint32_t(vl) > UNITY_GAIN_INT || uint32_t(vr) > UNITY_GAIN_INT)) {
Andy Hunga1ab7cc2014-02-24 19:26:52 -08001527 // volume is boosted, so we might need to clamp even though
1528 // we process only one track.
1529 do {
1530 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
1531 in += 2;
1532 int32_t l = mulRL(1, rl, vrl) >> 12;
1533 int32_t r = mulRL(0, rl, vrl) >> 12;
1534 // clamping...
1535 l = clamp16(l);
1536 r = clamp16(r);
1537 *out++ = (r<<16) | (l & 0xFFFF);
1538 } while (--outFrames);
1539 } else {
1540 do {
1541 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
1542 in += 2;
1543 int32_t l = mulRL(1, rl, vrl) >> 12;
1544 int32_t r = mulRL(0, rl, vrl) >> 12;
1545 *out++ = (r<<16) | (l & 0xFFFF);
1546 } while (--outFrames);
1547 }
1548 break;
1549 default:
Andy Hung78820702014-02-28 16:23:02 -08001550 LOG_ALWAYS_FATAL("bad mixer format: %d", t.mMixerFormat);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001551 }
1552 numFrames -= b.frameCount;
1553 t.bufferProvider->releaseBuffer(&b);
1554 }
1555}
1556
John Grossman4ff14ba2012-02-08 16:37:41 -08001557int64_t AudioMixer::calculateOutputPTS(const track_t& t, int64_t basePTS,
1558 int outputFrameIndex)
1559{
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001560 if (AudioBufferProvider::kInvalidPTS == basePTS) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001561 return AudioBufferProvider::kInvalidPTS;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001562 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001563
Glenn Kasten52008f82012-03-18 09:34:41 -07001564 return basePTS + ((outputFrameIndex * sLocalTimeFreq) / t.sampleRate);
1565}
1566
1567/*static*/ uint64_t AudioMixer::sLocalTimeFreq;
1568/*static*/ pthread_once_t AudioMixer::sOnceControl = PTHREAD_ONCE_INIT;
1569
1570/*static*/ void AudioMixer::sInitRoutine()
1571{
1572 LocalClock lc;
Andy Hung34803d52014-07-16 21:41:35 -07001573 sLocalTimeFreq = lc.getLocalFreq(); // for the resampler
Glenn Kasten49c34ac2013-10-30 14:37:01 -07001574
Andy Hung34803d52014-07-16 21:41:35 -07001575 DownmixerBufferProvider::init(); // for the downmixer
John Grossman4ff14ba2012-02-08 16:37:41 -08001576}
1577
Andy Hunge93b6b72014-07-17 21:30:53 -07001578/* TODO: consider whether this level of optimization is necessary.
1579 * Perhaps just stick with a single for loop.
1580 */
1581
1582// Needs to derive a compile time constant (constexpr). Could be targeted to go
1583// to a MONOVOL mixtype based on MAX_NUM_VOLUMES, but that's an unnecessary complication.
1584#define MIXTYPE_MONOVOL(mixtype) (mixtype == MIXTYPE_MULTI ? MIXTYPE_MULTI_MONOVOL : \
1585 mixtype == MIXTYPE_MULTI_SAVEONLY ? MIXTYPE_MULTI_SAVEONLY_MONOVOL : mixtype)
1586
1587/* MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
1588 * TO: int32_t (Q4.27) or float
1589 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
1590 * TA: int32_t (Q4.27)
1591 */
1592template <int MIXTYPE,
1593 typename TO, typename TI, typename TV, typename TA, typename TAV>
1594static void volumeRampMulti(uint32_t channels, TO* out, size_t frameCount,
1595 const TI* in, TA* aux, TV *vol, const TV *volinc, TAV *vola, TAV volainc)
1596{
1597 switch (channels) {
1598 case 1:
1599 volumeRampMulti<MIXTYPE, 1>(out, frameCount, in, aux, vol, volinc, vola, volainc);
1600 break;
1601 case 2:
1602 volumeRampMulti<MIXTYPE, 2>(out, frameCount, in, aux, vol, volinc, vola, volainc);
1603 break;
1604 case 3:
1605 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 3>(out,
1606 frameCount, in, aux, vol, volinc, vola, volainc);
1607 break;
1608 case 4:
1609 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 4>(out,
1610 frameCount, in, aux, vol, volinc, vola, volainc);
1611 break;
1612 case 5:
1613 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 5>(out,
1614 frameCount, in, aux, vol, volinc, vola, volainc);
1615 break;
1616 case 6:
1617 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 6>(out,
1618 frameCount, in, aux, vol, volinc, vola, volainc);
1619 break;
1620 case 7:
1621 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 7>(out,
1622 frameCount, in, aux, vol, volinc, vola, volainc);
1623 break;
1624 case 8:
1625 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 8>(out,
1626 frameCount, in, aux, vol, volinc, vola, volainc);
1627 break;
1628 }
1629}
1630
1631/* MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
1632 * TO: int32_t (Q4.27) or float
1633 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
1634 * TA: int32_t (Q4.27)
1635 */
1636template <int MIXTYPE,
1637 typename TO, typename TI, typename TV, typename TA, typename TAV>
1638static void volumeMulti(uint32_t channels, TO* out, size_t frameCount,
1639 const TI* in, TA* aux, const TV *vol, TAV vola)
1640{
1641 switch (channels) {
1642 case 1:
1643 volumeMulti<MIXTYPE, 1>(out, frameCount, in, aux, vol, vola);
1644 break;
1645 case 2:
1646 volumeMulti<MIXTYPE, 2>(out, frameCount, in, aux, vol, vola);
1647 break;
1648 case 3:
1649 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 3>(out, frameCount, in, aux, vol, vola);
1650 break;
1651 case 4:
1652 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 4>(out, frameCount, in, aux, vol, vola);
1653 break;
1654 case 5:
1655 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 5>(out, frameCount, in, aux, vol, vola);
1656 break;
1657 case 6:
1658 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 6>(out, frameCount, in, aux, vol, vola);
1659 break;
1660 case 7:
1661 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 7>(out, frameCount, in, aux, vol, vola);
1662 break;
1663 case 8:
1664 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 8>(out, frameCount, in, aux, vol, vola);
1665 break;
1666 }
1667}
1668
1669/* MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
1670 * USEFLOATVOL (set to true if float volume is used)
1671 * ADJUSTVOL (set to true if volume ramp parameters needs adjustment afterwards)
1672 * TO: int32_t (Q4.27) or float
1673 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
1674 * TA: int32_t (Q4.27)
1675 */
1676template <int MIXTYPE, bool USEFLOATVOL, bool ADJUSTVOL,
Andy Hung5e58b0a2014-06-23 19:07:29 -07001677 typename TO, typename TI, typename TA>
1678void AudioMixer::volumeMix(TO *out, size_t outFrames,
1679 const TI *in, TA *aux, bool ramp, AudioMixer::track_t *t)
1680{
1681 if (USEFLOATVOL) {
1682 if (ramp) {
Andy Hunge93b6b72014-07-17 21:30:53 -07001683 volumeRampMulti<MIXTYPE>(t->mMixerChannelCount, out, outFrames, in, aux,
Andy Hung5e58b0a2014-06-23 19:07:29 -07001684 t->mPrevVolume, t->mVolumeInc, &t->prevAuxLevel, t->auxInc);
1685 if (ADJUSTVOL) {
1686 t->adjustVolumeRamp(aux != NULL, true);
1687 }
1688 } else {
Andy Hunge93b6b72014-07-17 21:30:53 -07001689 volumeMulti<MIXTYPE>(t->mMixerChannelCount, out, outFrames, in, aux,
Andy Hung5e58b0a2014-06-23 19:07:29 -07001690 t->mVolume, t->auxLevel);
1691 }
1692 } else {
1693 if (ramp) {
Andy Hunge93b6b72014-07-17 21:30:53 -07001694 volumeRampMulti<MIXTYPE>(t->mMixerChannelCount, out, outFrames, in, aux,
Andy Hung5e58b0a2014-06-23 19:07:29 -07001695 t->prevVolume, t->volumeInc, &t->prevAuxLevel, t->auxInc);
1696 if (ADJUSTVOL) {
1697 t->adjustVolumeRamp(aux != NULL);
1698 }
1699 } else {
Andy Hunge93b6b72014-07-17 21:30:53 -07001700 volumeMulti<MIXTYPE>(t->mMixerChannelCount, out, outFrames, in, aux,
Andy Hung5e58b0a2014-06-23 19:07:29 -07001701 t->volume, t->auxLevel);
1702 }
1703 }
1704}
1705
Andy Hung296b7412014-06-17 15:25:47 -07001706/* This process hook is called when there is a single track without
1707 * aux buffer, volume ramp, or resampling.
1708 * TODO: Update the hook selection: this can properly handle aux and ramp.
Andy Hunge93b6b72014-07-17 21:30:53 -07001709 *
1710 * MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
1711 * TO: int32_t (Q4.27) or float
1712 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
1713 * TA: int32_t (Q4.27)
Andy Hung296b7412014-06-17 15:25:47 -07001714 */
Andy Hunge93b6b72014-07-17 21:30:53 -07001715template <int MIXTYPE, typename TO, typename TI, typename TA>
Andy Hung296b7412014-06-17 15:25:47 -07001716void AudioMixer::process_NoResampleOneTrack(state_t* state, int64_t pts)
1717{
1718 ALOGVV("process_NoResampleOneTrack\n");
1719 // CLZ is faster than CTZ on ARM, though really not sure if true after 31 - clz.
1720 const int i = 31 - __builtin_clz(state->enabledTracks);
1721 ALOG_ASSERT((1 << i) == state->enabledTracks, "more than 1 track enabled");
1722 track_t *t = &state->tracks[i];
Andy Hunge93b6b72014-07-17 21:30:53 -07001723 const uint32_t channels = t->mMixerChannelCount;
Andy Hung296b7412014-06-17 15:25:47 -07001724 TO* out = reinterpret_cast<TO*>(t->mainBuffer);
1725 TA* aux = reinterpret_cast<TA*>(t->auxBuffer);
1726 const bool ramp = t->needsRamp();
1727
1728 for (size_t numFrames = state->frameCount; numFrames; ) {
1729 AudioBufferProvider::Buffer& b(t->buffer);
1730 // get input buffer
1731 b.frameCount = numFrames;
1732 const int64_t outputPTS = calculateOutputPTS(*t, pts, state->frameCount - numFrames);
1733 t->bufferProvider->getNextBuffer(&b, outputPTS);
1734 const TI *in = reinterpret_cast<TI*>(b.raw);
1735
1736 // in == NULL can happen if the track was flushed just after having
1737 // been enabled for mixing.
1738 if (in == NULL || (((uintptr_t)in) & 3)) {
1739 memset(out, 0, numFrames
Andy Hunge93b6b72014-07-17 21:30:53 -07001740 * channels * audio_bytes_per_sample(t->mMixerFormat));
Andy Hung296b7412014-06-17 15:25:47 -07001741 ALOGE_IF((((uintptr_t)in) & 3), "process_NoResampleOneTrack: bus error: "
1742 "buffer %p track %p, channels %d, needs %#x",
1743 in, t, t->channelCount, t->needs);
1744 return;
1745 }
1746
1747 const size_t outFrames = b.frameCount;
Andy Hunge93b6b72014-07-17 21:30:53 -07001748 volumeMix<MIXTYPE, is_same<TI, float>::value, false> (
1749 out, outFrames, in, aux, ramp, t);
Andy Hung5e58b0a2014-06-23 19:07:29 -07001750
Andy Hunge93b6b72014-07-17 21:30:53 -07001751 out += outFrames * channels;
Andy Hung296b7412014-06-17 15:25:47 -07001752 if (aux != NULL) {
Andy Hunge93b6b72014-07-17 21:30:53 -07001753 aux += channels;
Andy Hung296b7412014-06-17 15:25:47 -07001754 }
1755 numFrames -= b.frameCount;
1756
1757 // release buffer
1758 t->bufferProvider->releaseBuffer(&b);
1759 }
1760 if (ramp) {
Andy Hung5e58b0a2014-06-23 19:07:29 -07001761 t->adjustVolumeRamp(aux != NULL, is_same<TI, float>::value);
Andy Hung296b7412014-06-17 15:25:47 -07001762 }
1763}
1764
1765/* This track hook is called to do resampling then mixing,
1766 * pulling from the track's upstream AudioBufferProvider.
Andy Hunge93b6b72014-07-17 21:30:53 -07001767 *
1768 * MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
1769 * TO: int32_t (Q4.27) or float
1770 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
1771 * TA: int32_t (Q4.27)
Andy Hung296b7412014-06-17 15:25:47 -07001772 */
Andy Hunge93b6b72014-07-17 21:30:53 -07001773template <int MIXTYPE, typename TO, typename TI, typename TA>
Andy Hung296b7412014-06-17 15:25:47 -07001774void AudioMixer::track__Resample(track_t* t, TO* out, size_t outFrameCount, TO* temp, TA* aux)
1775{
1776 ALOGVV("track__Resample\n");
1777 t->resampler->setSampleRate(t->sampleRate);
Andy Hung296b7412014-06-17 15:25:47 -07001778 const bool ramp = t->needsRamp();
1779 if (ramp || aux != NULL) {
1780 // if ramp: resample with unity gain to temp buffer and scale/mix in 2nd step.
1781 // if aux != NULL: resample with unity gain to temp buffer then apply send level.
1782
Andy Hung5e58b0a2014-06-23 19:07:29 -07001783 t->resampler->setVolume(UNITY_GAIN_FLOAT, UNITY_GAIN_FLOAT);
Andy Hunge93b6b72014-07-17 21:30:53 -07001784 memset(temp, 0, outFrameCount * t->mMixerChannelCount * sizeof(TO));
Andy Hung296b7412014-06-17 15:25:47 -07001785 t->resampler->resample((int32_t*)temp, outFrameCount, t->bufferProvider);
Andy Hung5e58b0a2014-06-23 19:07:29 -07001786
Andy Hunge93b6b72014-07-17 21:30:53 -07001787 volumeMix<MIXTYPE, is_same<TI, float>::value, true>(
1788 out, outFrameCount, temp, aux, ramp, t);
Andy Hung5e58b0a2014-06-23 19:07:29 -07001789
Andy Hung296b7412014-06-17 15:25:47 -07001790 } else { // constant volume gain
Andy Hung5e58b0a2014-06-23 19:07:29 -07001791 t->resampler->setVolume(t->mVolume[0], t->mVolume[1]);
Andy Hung296b7412014-06-17 15:25:47 -07001792 t->resampler->resample((int32_t*)out, outFrameCount, t->bufferProvider);
1793 }
1794}
1795
1796/* This track hook is called to mix a track, when no resampling is required.
1797 * The input buffer should be present in t->in.
Andy Hunge93b6b72014-07-17 21:30:53 -07001798 *
1799 * MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
1800 * TO: int32_t (Q4.27) or float
1801 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
1802 * TA: int32_t (Q4.27)
Andy Hung296b7412014-06-17 15:25:47 -07001803 */
Andy Hunge93b6b72014-07-17 21:30:53 -07001804template <int MIXTYPE, typename TO, typename TI, typename TA>
Andy Hung296b7412014-06-17 15:25:47 -07001805void AudioMixer::track__NoResample(track_t* t, TO* out, size_t frameCount,
1806 TO* temp __unused, TA* aux)
1807{
1808 ALOGVV("track__NoResample\n");
1809 const TI *in = static_cast<const TI *>(t->in);
1810
Andy Hunge93b6b72014-07-17 21:30:53 -07001811 volumeMix<MIXTYPE, is_same<TI, float>::value, true>(
1812 out, frameCount, in, aux, t->needsRamp(), t);
Andy Hung5e58b0a2014-06-23 19:07:29 -07001813
Andy Hung296b7412014-06-17 15:25:47 -07001814 // MIXTYPE_MONOEXPAND reads a single input channel and expands to NCHAN output channels.
1815 // MIXTYPE_MULTI reads NCHAN input channels and places to NCHAN output channels.
Andy Hunge93b6b72014-07-17 21:30:53 -07001816 in += (MIXTYPE == MIXTYPE_MONOEXPAND) ? frameCount : frameCount * t->mMixerChannelCount;
Andy Hung296b7412014-06-17 15:25:47 -07001817 t->in = in;
1818}
1819
1820/* The Mixer engine generates either int32_t (Q4_27) or float data.
1821 * We use this function to convert the engine buffers
1822 * to the desired mixer output format, either int16_t (Q.15) or float.
1823 */
1824void AudioMixer::convertMixerFormat(void *out, audio_format_t mixerOutFormat,
1825 void *in, audio_format_t mixerInFormat, size_t sampleCount)
1826{
1827 switch (mixerInFormat) {
1828 case AUDIO_FORMAT_PCM_FLOAT:
1829 switch (mixerOutFormat) {
1830 case AUDIO_FORMAT_PCM_FLOAT:
1831 memcpy(out, in, sampleCount * sizeof(float)); // MEMCPY. TODO optimize out
1832 break;
1833 case AUDIO_FORMAT_PCM_16_BIT:
1834 memcpy_to_i16_from_float((int16_t*)out, (float*)in, sampleCount);
1835 break;
1836 default:
1837 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat);
1838 break;
1839 }
1840 break;
1841 case AUDIO_FORMAT_PCM_16_BIT:
1842 switch (mixerOutFormat) {
1843 case AUDIO_FORMAT_PCM_FLOAT:
1844 memcpy_to_float_from_q4_27((float*)out, (int32_t*)in, sampleCount);
1845 break;
1846 case AUDIO_FORMAT_PCM_16_BIT:
1847 // two int16_t are produced per iteration
1848 ditherAndClamp((int32_t*)out, (int32_t*)in, sampleCount >> 1);
1849 break;
1850 default:
1851 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat);
1852 break;
1853 }
1854 break;
1855 default:
1856 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
1857 break;
1858 }
1859}
1860
1861/* Returns the proper track hook to use for mixing the track into the output buffer.
1862 */
Andy Hunge93b6b72014-07-17 21:30:53 -07001863AudioMixer::hook_t AudioMixer::getTrackHook(int trackType, uint32_t channelCount,
Andy Hung296b7412014-06-17 15:25:47 -07001864 audio_format_t mixerInFormat, audio_format_t mixerOutFormat __unused)
1865{
Andy Hunge93b6b72014-07-17 21:30:53 -07001866 if (!kUseNewMixer && channelCount == FCC_2 && mixerInFormat == AUDIO_FORMAT_PCM_16_BIT) {
Andy Hung296b7412014-06-17 15:25:47 -07001867 switch (trackType) {
1868 case TRACKTYPE_NOP:
1869 return track__nop;
1870 case TRACKTYPE_RESAMPLE:
1871 return track__genericResample;
1872 case TRACKTYPE_NORESAMPLEMONO:
1873 return track__16BitsMono;
1874 case TRACKTYPE_NORESAMPLE:
1875 return track__16BitsStereo;
1876 default:
1877 LOG_ALWAYS_FATAL("bad trackType: %d", trackType);
1878 break;
1879 }
1880 }
Andy Hunge93b6b72014-07-17 21:30:53 -07001881 LOG_ALWAYS_FATAL_IF(channelCount > MAX_NUM_CHANNELS);
Andy Hung296b7412014-06-17 15:25:47 -07001882 switch (trackType) {
1883 case TRACKTYPE_NOP:
1884 return track__nop;
1885 case TRACKTYPE_RESAMPLE:
1886 switch (mixerInFormat) {
1887 case AUDIO_FORMAT_PCM_FLOAT:
1888 return (AudioMixer::hook_t)
Andy Hunge93b6b72014-07-17 21:30:53 -07001889 track__Resample<MIXTYPE_MULTI, float /*TO*/, float /*TI*/, int32_t /*TA*/>;
Andy Hung296b7412014-06-17 15:25:47 -07001890 case AUDIO_FORMAT_PCM_16_BIT:
1891 return (AudioMixer::hook_t)\
Andy Hunge93b6b72014-07-17 21:30:53 -07001892 track__Resample<MIXTYPE_MULTI, int32_t, int16_t, int32_t>;
Andy Hung296b7412014-06-17 15:25:47 -07001893 default:
1894 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
1895 break;
1896 }
1897 break;
1898 case TRACKTYPE_NORESAMPLEMONO:
1899 switch (mixerInFormat) {
1900 case AUDIO_FORMAT_PCM_FLOAT:
1901 return (AudioMixer::hook_t)
Andy Hunge93b6b72014-07-17 21:30:53 -07001902 track__NoResample<MIXTYPE_MONOEXPAND, float, float, int32_t>;
Andy Hung296b7412014-06-17 15:25:47 -07001903 case AUDIO_FORMAT_PCM_16_BIT:
1904 return (AudioMixer::hook_t)
Andy Hunge93b6b72014-07-17 21:30:53 -07001905 track__NoResample<MIXTYPE_MONOEXPAND, int32_t, int16_t, int32_t>;
Andy Hung296b7412014-06-17 15:25:47 -07001906 default:
1907 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
1908 break;
1909 }
1910 break;
1911 case TRACKTYPE_NORESAMPLE:
1912 switch (mixerInFormat) {
1913 case AUDIO_FORMAT_PCM_FLOAT:
1914 return (AudioMixer::hook_t)
Andy Hunge93b6b72014-07-17 21:30:53 -07001915 track__NoResample<MIXTYPE_MULTI, float, float, int32_t>;
Andy Hung296b7412014-06-17 15:25:47 -07001916 case AUDIO_FORMAT_PCM_16_BIT:
1917 return (AudioMixer::hook_t)
Andy Hunge93b6b72014-07-17 21:30:53 -07001918 track__NoResample<MIXTYPE_MULTI, int32_t, int16_t, int32_t>;
Andy Hung296b7412014-06-17 15:25:47 -07001919 default:
1920 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
1921 break;
1922 }
1923 break;
1924 default:
1925 LOG_ALWAYS_FATAL("bad trackType: %d", trackType);
1926 break;
1927 }
1928 return NULL;
1929}
1930
1931/* Returns the proper process hook for mixing tracks. Currently works only for
1932 * PROCESSTYPE_NORESAMPLEONETRACK, a mix involving one track, no resampling.
Andy Hung395db4b2014-08-25 17:15:29 -07001933 *
1934 * TODO: Due to the special mixing considerations of duplicating to
1935 * a stereo output track, the input track cannot be MONO. This should be
1936 * prevented by the caller.
Andy Hung296b7412014-06-17 15:25:47 -07001937 */
Andy Hunge93b6b72014-07-17 21:30:53 -07001938AudioMixer::process_hook_t AudioMixer::getProcessHook(int processType, uint32_t channelCount,
Andy Hung296b7412014-06-17 15:25:47 -07001939 audio_format_t mixerInFormat, audio_format_t mixerOutFormat)
1940{
1941 if (processType != PROCESSTYPE_NORESAMPLEONETRACK) { // Only NORESAMPLEONETRACK
1942 LOG_ALWAYS_FATAL("bad processType: %d", processType);
1943 return NULL;
1944 }
Andy Hunge93b6b72014-07-17 21:30:53 -07001945 if (!kUseNewMixer && channelCount == FCC_2 && mixerInFormat == AUDIO_FORMAT_PCM_16_BIT) {
Andy Hung296b7412014-06-17 15:25:47 -07001946 return process__OneTrack16BitsStereoNoResampling;
1947 }
Andy Hunge93b6b72014-07-17 21:30:53 -07001948 LOG_ALWAYS_FATAL_IF(channelCount > MAX_NUM_CHANNELS);
Andy Hung296b7412014-06-17 15:25:47 -07001949 switch (mixerInFormat) {
1950 case AUDIO_FORMAT_PCM_FLOAT:
1951 switch (mixerOutFormat) {
1952 case AUDIO_FORMAT_PCM_FLOAT:
Andy Hunge93b6b72014-07-17 21:30:53 -07001953 return process_NoResampleOneTrack<MIXTYPE_MULTI_SAVEONLY,
1954 float /*TO*/, float /*TI*/, int32_t /*TA*/>;
Andy Hung296b7412014-06-17 15:25:47 -07001955 case AUDIO_FORMAT_PCM_16_BIT:
Andy Hunge93b6b72014-07-17 21:30:53 -07001956 return process_NoResampleOneTrack<MIXTYPE_MULTI_SAVEONLY,
Andy Hung296b7412014-06-17 15:25:47 -07001957 int16_t, float, int32_t>;
1958 default:
1959 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat);
1960 break;
1961 }
1962 break;
1963 case AUDIO_FORMAT_PCM_16_BIT:
1964 switch (mixerOutFormat) {
1965 case AUDIO_FORMAT_PCM_FLOAT:
Andy Hunge93b6b72014-07-17 21:30:53 -07001966 return process_NoResampleOneTrack<MIXTYPE_MULTI_SAVEONLY,
Andy Hung296b7412014-06-17 15:25:47 -07001967 float, int16_t, int32_t>;
1968 case AUDIO_FORMAT_PCM_16_BIT:
Andy Hunge93b6b72014-07-17 21:30:53 -07001969 return process_NoResampleOneTrack<MIXTYPE_MULTI_SAVEONLY,
Andy Hung296b7412014-06-17 15:25:47 -07001970 int16_t, int16_t, int32_t>;
1971 default:
1972 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat);
1973 break;
1974 }
1975 break;
1976 default:
1977 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
1978 break;
1979 }
1980 return NULL;
1981}
1982
Mathias Agopian65ab4712010-07-14 17:59:35 -07001983// ----------------------------------------------------------------------------
Glenn Kasten63238ef2015-03-02 15:50:29 -08001984} // namespace android