blob: 1ec238b28aeb451b9ea226b90b0f82b569c1a0d7 [file] [log] [blame]
Glenn Kastenb3db2132012-01-19 08:59:58 -08001/*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002**
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"
The Android Open Source Project10592532009-03-18 17:39:46 -070019//#define LOG_NDEBUG 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020
21#include <stdint.h>
22#include <string.h>
23#include <stdlib.h>
24#include <sys/types.h>
25
26#include <utils/Errors.h>
27#include <utils/Log.h>
28
Jean-Michel Trivi54392232011-05-24 15:53:33 -070029#include <cutils/bitops.h>
Glenn Kastene80a4cc2011-12-15 09:51:17 -080030#include <cutils/compiler.h>
Glenn Kasten81916df2012-03-08 12:18:35 -080031#include <utils/Debug.h>
Jean-Michel Trivi54392232011-05-24 15:53:33 -070032
33#include <system/audio.h>
34
Glenn Kasten490909d2011-12-15 09:52:39 -080035#include <audio_utils/primitives.h>
John Grossmand8cf2962012-02-08 16:37:41 -080036#include <common_time/local_clock.h>
37#include <common_time/cc_helper.h>
Glenn Kasten490909d2011-12-15 09:52:39 -080038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039#include "AudioMixer.h"
40
41namespace android {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
43// ----------------------------------------------------------------------------
44
45AudioMixer::AudioMixer(size_t frameCount, uint32_t sampleRate)
Glenn Kasten68deb152011-12-19 15:06:39 -080046 : mTrackNames(0), mSampleRate(sampleRate)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047{
Glenn Kastenbde164ab2011-05-05 08:19:00 -070048 // AudioMixer is not yet capable of multi-channel beyond stereo
Glenn Kasten81916df2012-03-08 12:18:35 -080049 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(2 == MAX_NUM_CHANNELS);
John Grossmand8cf2962012-02-08 16:37:41 -080050
51 LocalClock lc;
52
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 mState.enabledTracks= 0;
54 mState.needsChanged = 0;
55 mState.frameCount = frameCount;
Glenn Kastendc3ac852012-01-25 15:28:08 -080056 mState.hook = process__nop;
Glenn Kastenc434c902011-12-13 11:53:26 -080057 mState.outputTemp = NULL;
58 mState.resampleTemp = NULL;
Glenn Kastendc3ac852012-01-25 15:28:08 -080059 // mState.reserved
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 track_t* t = mState.tracks;
Glenn Kasten1f6f05d2011-12-13 11:52:35 -080061 for (unsigned i=0 ; i < MAX_NUM_TRACKS ; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 t->needs = 0;
63 t->volume[0] = UNITY_GAIN;
64 t->volume[1] = UNITY_GAIN;
Glenn Kastena763b442011-12-13 11:58:23 -080065 // no initialization needed
66 // t->prevVolume[0]
67 // t->prevVolume[1]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 t->volumeInc[0] = 0;
69 t->volumeInc[1] = 0;
Eric Laurent65b65452010-06-01 23:49:17 -070070 t->auxLevel = 0;
71 t->auxInc = 0;
Glenn Kastena763b442011-12-13 11:58:23 -080072 // no initialization needed
73 // t->prevAuxLevel
74 // t->frameCount
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 t->channelCount = 2;
Glenn Kasten450b9982012-01-27 12:33:54 -080076 t->enabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 t->format = 16;
Jean-Michel Trivi54392232011-05-24 15:53:33 -070078 t->channelMask = AUDIO_CHANNEL_OUT_STEREO;
Glenn Kastenc434c902011-12-13 11:53:26 -080079 t->bufferProvider = NULL;
Glenn Kastendc3ac852012-01-25 15:28:08 -080080 t->buffer.raw = NULL;
81 // t->buffer.frameCount
Glenn Kastenc434c902011-12-13 11:53:26 -080082 t->hook = NULL;
Glenn Kastendc3ac852012-01-25 15:28:08 -080083 t->in = NULL;
Glenn Kastenc434c902011-12-13 11:53:26 -080084 t->resampler = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 t->sampleRate = mSampleRate;
Eric Laurent65b65452010-06-01 23:49:17 -070086 t->mainBuffer = NULL;
87 t->auxBuffer = NULL;
John Grossmand8cf2962012-02-08 16:37:41 -080088 t->localTimeFreq = lc.getLocalFreq();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 t++;
90 }
91}
92
Glenn Kastenfb2ab9e2011-12-12 09:05:55 -080093AudioMixer::~AudioMixer()
94{
95 track_t* t = mState.tracks;
Glenn Kasten1f6f05d2011-12-13 11:52:35 -080096 for (unsigned i=0 ; i < MAX_NUM_TRACKS ; i++) {
Glenn Kastenfb2ab9e2011-12-12 09:05:55 -080097 delete t->resampler;
98 t++;
99 }
100 delete [] mState.outputTemp;
101 delete [] mState.resampleTemp;
102}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103
Glenn Kastenfb2ab9e2011-12-12 09:05:55 -0800104int AudioMixer::getTrackName()
105{
Glenn Kastenc1d810d2011-12-15 14:38:29 -0800106 uint32_t names = ~mTrackNames;
107 if (names != 0) {
108 int n = __builtin_ctz(names);
Steve Block71f2cf12011-10-20 11:56:00 +0100109 ALOGV("add track (%d)", n);
Glenn Kastenc1d810d2011-12-15 14:38:29 -0800110 mTrackNames |= 1 << n;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 return TRACK0 + n;
112 }
113 return -1;
Glenn Kastenfb2ab9e2011-12-12 09:05:55 -0800114}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115
Glenn Kastenfb2ab9e2011-12-12 09:05:55 -0800116void AudioMixer::invalidateState(uint32_t mask)
117{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 if (mask) {
119 mState.needsChanged |= mask;
120 mState.hook = process__validate;
121 }
122 }
123
Glenn Kastenfb2ab9e2011-12-12 09:05:55 -0800124void AudioMixer::deleteTrackName(int name)
125{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 name -= TRACK0;
Glenn Kasten81916df2012-03-08 12:18:35 -0800127 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
Glenn Kastenb3719252011-12-15 15:32:27 -0800128 ALOGV("deleteTrackName(%d)", name);
129 track_t& track(mState.tracks[ name ]);
Glenn Kasten450b9982012-01-27 12:33:54 -0800130 if (track.enabled) {
131 track.enabled = false;
Glenn Kastenb3719252011-12-15 15:32:27 -0800132 invalidateState(1<<name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 }
Glenn Kasten3694ec12012-01-27 16:47:15 -0800134 if (track.resampler != NULL) {
Glenn Kastenb3719252011-12-15 15:32:27 -0800135 // delete the resampler
136 delete track.resampler;
137 track.resampler = NULL;
138 track.sampleRate = mSampleRate;
139 invalidateState(1<<name);
140 }
141 track.volumeInc[0] = 0;
142 track.volumeInc[1] = 0;
143 mTrackNames &= ~(1<<name);
Glenn Kastenfb2ab9e2011-12-12 09:05:55 -0800144}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145
Glenn Kasten68deb152011-12-19 15:06:39 -0800146void AudioMixer::enable(int name)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147{
Glenn Kasten68deb152011-12-19 15:06:39 -0800148 name -= TRACK0;
Glenn Kasten81916df2012-03-08 12:18:35 -0800149 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
Glenn Kasten68deb152011-12-19 15:06:39 -0800150 track_t& track = mState.tracks[name];
151
Glenn Kasten450b9982012-01-27 12:33:54 -0800152 if (!track.enabled) {
153 track.enabled = true;
Glenn Kasten68deb152011-12-19 15:06:39 -0800154 ALOGV("enable(%d)", name);
155 invalidateState(1 << name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157}
158
Glenn Kasten68deb152011-12-19 15:06:39 -0800159void AudioMixer::disable(int name)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160{
Glenn Kasten68deb152011-12-19 15:06:39 -0800161 name -= TRACK0;
Glenn Kasten81916df2012-03-08 12:18:35 -0800162 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
Glenn Kasten68deb152011-12-19 15:06:39 -0800163 track_t& track = mState.tracks[name];
164
Glenn Kasten450b9982012-01-27 12:33:54 -0800165 if (track.enabled) {
166 track.enabled = false;
Glenn Kasten68deb152011-12-19 15:06:39 -0800167 ALOGV("disable(%d)", name);
168 invalidateState(1 << name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170}
171
Glenn Kasten68deb152011-12-19 15:06:39 -0800172void AudioMixer::setParameter(int name, int target, int param, void *value)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173{
Glenn Kasten68deb152011-12-19 15:06:39 -0800174 name -= TRACK0;
Glenn Kasten81916df2012-03-08 12:18:35 -0800175 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
Glenn Kasten68deb152011-12-19 15:06:39 -0800176 track_t& track = mState.tracks[name];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177
Eric Laurent65b65452010-06-01 23:49:17 -0700178 int valueInt = (int)value;
179 int32_t *valueBuf = (int32_t *)value;
180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 switch (target) {
Glenn Kastenbde164ab2011-05-05 08:19:00 -0700182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 case TRACK:
Glenn Kasten68deb152011-12-19 15:06:39 -0800184 switch (param) {
Glenn Kastenbde164ab2011-05-05 08:19:00 -0700185 case CHANNEL_MASK: {
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700186 uint32_t mask = (uint32_t)value;
Glenn Kasten68deb152011-12-19 15:06:39 -0800187 if (track.channelMask != mask) {
Glenn Kasten81916df2012-03-08 12:18:35 -0800188 uint32_t channelCount = popcount(mask);
189 ALOG_ASSERT((channelCount <= MAX_NUM_CHANNELS) && (channelCount),
190 "bad channel count %u", channelCount);
Glenn Kasten68deb152011-12-19 15:06:39 -0800191 track.channelMask = mask;
192 track.channelCount = channelCount;
Glenn Kastenbde164ab2011-05-05 08:19:00 -0700193 ALOGV("setParameter(TRACK, CHANNEL_MASK, %x)", mask);
Glenn Kasten68deb152011-12-19 15:06:39 -0800194 invalidateState(1 << name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 }
Glenn Kastenbde164ab2011-05-05 08:19:00 -0700196 } break;
197 case MAIN_BUFFER:
Glenn Kasten68deb152011-12-19 15:06:39 -0800198 if (track.mainBuffer != valueBuf) {
199 track.mainBuffer = valueBuf;
Steve Block71f2cf12011-10-20 11:56:00 +0100200 ALOGV("setParameter(TRACK, MAIN_BUFFER, %p)", valueBuf);
Glenn Kasten68deb152011-12-19 15:06:39 -0800201 invalidateState(1 << name);
Eric Laurent65b65452010-06-01 23:49:17 -0700202 }
Glenn Kastenbde164ab2011-05-05 08:19:00 -0700203 break;
204 case AUX_BUFFER:
Glenn Kasten68deb152011-12-19 15:06:39 -0800205 if (track.auxBuffer != valueBuf) {
206 track.auxBuffer = valueBuf;
Steve Block71f2cf12011-10-20 11:56:00 +0100207 ALOGV("setParameter(TRACK, AUX_BUFFER, %p)", valueBuf);
Glenn Kasten68deb152011-12-19 15:06:39 -0800208 invalidateState(1 << name);
Eric Laurent65b65452010-06-01 23:49:17 -0700209 }
Glenn Kastenbde164ab2011-05-05 08:19:00 -0700210 break;
211 default:
Glenn Kasten81916df2012-03-08 12:18:35 -0800212 LOG_FATAL("bad param");
Eric Laurent65b65452010-06-01 23:49:17 -0700213 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 break;
Glenn Kastenbde164ab2011-05-05 08:19:00 -0700215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 case RESAMPLE:
Glenn Kasten68deb152011-12-19 15:06:39 -0800217 switch (param) {
218 case SAMPLE_RATE:
Glenn Kasten81916df2012-03-08 12:18:35 -0800219 ALOG_ASSERT(valueInt > 0, "bad sample rate %d", valueInt);
Glenn Kastenbde164ab2011-05-05 08:19:00 -0700220 if (track.setResampler(uint32_t(valueInt), mSampleRate)) {
221 ALOGV("setParameter(RESAMPLE, SAMPLE_RATE, %u)",
222 uint32_t(valueInt));
Glenn Kasten68deb152011-12-19 15:06:39 -0800223 invalidateState(1 << name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 }
Glenn Kasten68deb152011-12-19 15:06:39 -0800225 break;
226 case RESET:
Eric Laurent4bb21c42011-02-28 16:52:51 -0800227 track.resetResampler();
Glenn Kasten68deb152011-12-19 15:06:39 -0800228 invalidateState(1 << name);
229 break;
Glenn Kastenbde164ab2011-05-05 08:19:00 -0700230 default:
Glenn Kasten81916df2012-03-08 12:18:35 -0800231 LOG_FATAL("bad param");
Eric Laurent4bb21c42011-02-28 16:52:51 -0800232 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 break;
Glenn Kastenbde164ab2011-05-05 08:19:00 -0700234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 case RAMP_VOLUME:
236 case VOLUME:
Glenn Kasten68deb152011-12-19 15:06:39 -0800237 switch (param) {
Glenn Kastenbde164ab2011-05-05 08:19:00 -0700238 case VOLUME0:
Glenn Kasten68deb152011-12-19 15:06:39 -0800239 case VOLUME1:
240 if (track.volume[param-VOLUME0] != valueInt) {
Steve Block71f2cf12011-10-20 11:56:00 +0100241 ALOGV("setParameter(VOLUME, VOLUME0/1: %04x)", valueInt);
Glenn Kasten68deb152011-12-19 15:06:39 -0800242 track.prevVolume[param-VOLUME0] = track.volume[param-VOLUME0] << 16;
243 track.volume[param-VOLUME0] = valueInt;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 if (target == VOLUME) {
Glenn Kasten68deb152011-12-19 15:06:39 -0800245 track.prevVolume[param-VOLUME0] = valueInt << 16;
246 track.volumeInc[param-VOLUME0] = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 } else {
Glenn Kasten68deb152011-12-19 15:06:39 -0800248 int32_t d = (valueInt<<16) - track.prevVolume[param-VOLUME0];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 int32_t volInc = d / int32_t(mState.frameCount);
Glenn Kasten68deb152011-12-19 15:06:39 -0800250 track.volumeInc[param-VOLUME0] = volInc;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 if (volInc == 0) {
Glenn Kasten68deb152011-12-19 15:06:39 -0800252 track.prevVolume[param-VOLUME0] = valueInt << 16;
Eric Laurent65b65452010-06-01 23:49:17 -0700253 }
254 }
Glenn Kasten68deb152011-12-19 15:06:39 -0800255 invalidateState(1 << name);
Eric Laurent65b65452010-06-01 23:49:17 -0700256 }
Glenn Kasten68deb152011-12-19 15:06:39 -0800257 break;
258 case AUXLEVEL:
Glenn Kasten81916df2012-03-08 12:18:35 -0800259 //ALOG_ASSERT(0 <= valueInt && valueInt <= MAX_GAIN_INT, "bad aux level %d", valueInt);
Eric Laurent65b65452010-06-01 23:49:17 -0700260 if (track.auxLevel != valueInt) {
Steve Block71f2cf12011-10-20 11:56:00 +0100261 ALOGV("setParameter(VOLUME, AUXLEVEL: %04x)", valueInt);
Eric Laurent65b65452010-06-01 23:49:17 -0700262 track.prevAuxLevel = track.auxLevel << 16;
263 track.auxLevel = valueInt;
264 if (target == VOLUME) {
265 track.prevAuxLevel = valueInt << 16;
266 track.auxInc = 0;
267 } else {
268 int32_t d = (valueInt<<16) - track.prevAuxLevel;
269 int32_t volInc = d / int32_t(mState.frameCount);
270 track.auxInc = volInc;
271 if (volInc == 0) {
272 track.prevAuxLevel = valueInt << 16;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 }
274 }
Glenn Kasten68deb152011-12-19 15:06:39 -0800275 invalidateState(1 << name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 }
Glenn Kasten68deb152011-12-19 15:06:39 -0800277 break;
Glenn Kastenbde164ab2011-05-05 08:19:00 -0700278 default:
Glenn Kasten81916df2012-03-08 12:18:35 -0800279 LOG_FATAL("bad param");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 }
281 break;
Glenn Kastenbde164ab2011-05-05 08:19:00 -0700282
283 default:
Glenn Kasten81916df2012-03-08 12:18:35 -0800284 LOG_FATAL("bad target");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286}
287
288bool AudioMixer::track_t::setResampler(uint32_t value, uint32_t devSampleRate)
289{
290 if (value!=devSampleRate || resampler) {
291 if (sampleRate != value) {
292 sampleRate = value;
Glenn Kastenc434c902011-12-13 11:53:26 -0800293 if (resampler == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 resampler = AudioResampler::create(
295 format, channelCount, devSampleRate);
John Grossmand8cf2962012-02-08 16:37:41 -0800296 resampler->setLocalTimeFreq(localTimeFreq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 }
298 return true;
299 }
300 }
301 return false;
302}
303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304inline
Eric Laurent65b65452010-06-01 23:49:17 -0700305void AudioMixer::track_t::adjustVolumeRamp(bool aux)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306{
Glenn Kastenadda27a2012-01-06 07:47:26 -0800307 for (uint32_t i=0 ; i<MAX_NUM_CHANNELS ; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 if (((volumeInc[i]>0) && (((prevVolume[i]+volumeInc[i])>>16) >= volume[i])) ||
309 ((volumeInc[i]<0) && (((prevVolume[i]+volumeInc[i])>>16) <= volume[i]))) {
310 volumeInc[i] = 0;
311 prevVolume[i] = volume[i]<<16;
312 }
313 }
Eric Laurent65b65452010-06-01 23:49:17 -0700314 if (aux) {
315 if (((auxInc>0) && (((prevAuxLevel+auxInc)>>16) >= auxLevel)) ||
316 ((auxInc<0) && (((prevAuxLevel+auxInc)>>16) <= auxLevel))) {
317 auxInc = 0;
318 prevAuxLevel = auxLevel<<16;
319 }
320 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321}
322
Glenn Kasteneabd94a2012-02-02 14:06:11 -0800323size_t AudioMixer::getUnreleasedFrames(int name) const
Eric Laurent72dafb22011-12-22 16:08:41 -0800324{
325 name -= TRACK0;
326 if (uint32_t(name) < MAX_NUM_TRACKS) {
Glenn Kasteneabd94a2012-02-02 14:06:11 -0800327 return mState.tracks[name].getUnreleasedFrames();
Eric Laurent72dafb22011-12-22 16:08:41 -0800328 }
329 return 0;
330}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331
Glenn Kastenc2db1192012-02-22 10:47:35 -0800332void AudioMixer::setBufferProvider(int name, AudioBufferProvider* bufferProvider)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333{
Glenn Kasten68deb152011-12-19 15:06:39 -0800334 name -= TRACK0;
Glenn Kasten81916df2012-03-08 12:18:35 -0800335 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
Glenn Kastenc2db1192012-02-22 10:47:35 -0800336 mState.tracks[name].bufferProvider = bufferProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337}
338
339
340
John Grossmand8cf2962012-02-08 16:37:41 -0800341void AudioMixer::process(int64_t pts)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342{
John Grossmand8cf2962012-02-08 16:37:41 -0800343 mState.hook(&mState, pts);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344}
345
346
John Grossmand8cf2962012-02-08 16:37:41 -0800347void AudioMixer::process__validate(state_t* state, int64_t pts)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348{
Steve Block8564c8d2012-01-05 23:22:43 +0000349 ALOGW_IF(!state->needsChanged,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 "in process__validate() but nothing's invalid");
351
352 uint32_t changed = state->needsChanged;
353 state->needsChanged = 0; // clear the validation flag
354
355 // recompute which tracks are enabled / disabled
356 uint32_t enabled = 0;
357 uint32_t disabled = 0;
358 while (changed) {
359 const int i = 31 - __builtin_clz(changed);
360 const uint32_t mask = 1<<i;
361 changed &= ~mask;
362 track_t& t = state->tracks[i];
363 (t.enabled ? enabled : disabled) |= mask;
364 }
365 state->enabledTracks &= ~disabled;
366 state->enabledTracks |= enabled;
367
368 // compute everything we need...
369 int countActiveTracks = 0;
Glenn Kasten450b9982012-01-27 12:33:54 -0800370 bool all16BitsStereoNoResample = true;
371 bool resampling = false;
372 bool volumeRamp = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 uint32_t en = state->enabledTracks;
374 while (en) {
375 const int i = 31 - __builtin_clz(en);
376 en &= ~(1<<i);
377
378 countActiveTracks++;
379 track_t& t = state->tracks[i];
380 uint32_t n = 0;
381 n |= NEEDS_CHANNEL_1 + t.channelCount - 1;
382 n |= NEEDS_FORMAT_16;
383 n |= t.doesResample() ? NEEDS_RESAMPLE_ENABLED : NEEDS_RESAMPLE_DISABLED;
Eric Laurent65b65452010-06-01 23:49:17 -0700384 if (t.auxLevel != 0 && t.auxBuffer != NULL) {
385 n |= NEEDS_AUX_ENABLED;
386 }
387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 if (t.volumeInc[0]|t.volumeInc[1]) {
Glenn Kasten450b9982012-01-27 12:33:54 -0800389 volumeRamp = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 } else if (!t.doesResample() && t.volumeRL == 0) {
391 n |= NEEDS_MUTE_ENABLED;
392 }
393 t.needs = n;
394
395 if ((n & NEEDS_MUTE__MASK) == NEEDS_MUTE_ENABLED) {
396 t.hook = track__nop;
397 } else {
Eric Laurent65b65452010-06-01 23:49:17 -0700398 if ((n & NEEDS_AUX__MASK) == NEEDS_AUX_ENABLED) {
Glenn Kasten450b9982012-01-27 12:33:54 -0800399 all16BitsStereoNoResample = false;
Eric Laurent65b65452010-06-01 23:49:17 -0700400 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 if ((n & NEEDS_RESAMPLE__MASK) == NEEDS_RESAMPLE_ENABLED) {
Glenn Kasten450b9982012-01-27 12:33:54 -0800402 all16BitsStereoNoResample = false;
403 resampling = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 t.hook = track__genericResample;
405 } else {
406 if ((n & NEEDS_CHANNEL_COUNT__MASK) == NEEDS_CHANNEL_1){
407 t.hook = track__16BitsMono;
Glenn Kasten450b9982012-01-27 12:33:54 -0800408 all16BitsStereoNoResample = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 }
410 if ((n & NEEDS_CHANNEL_COUNT__MASK) == NEEDS_CHANNEL_2){
411 t.hook = track__16BitsStereo;
412 }
413 }
414 }
415 }
416
417 // select the processing hooks
418 state->hook = process__nop;
419 if (countActiveTracks) {
420 if (resampling) {
421 if (!state->outputTemp) {
422 state->outputTemp = new int32_t[MAX_NUM_CHANNELS * state->frameCount];
423 }
424 if (!state->resampleTemp) {
425 state->resampleTemp = new int32_t[MAX_NUM_CHANNELS * state->frameCount];
426 }
427 state->hook = process__genericResampling;
428 } else {
429 if (state->outputTemp) {
430 delete [] state->outputTemp;
Glenn Kastenc434c902011-12-13 11:53:26 -0800431 state->outputTemp = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 }
433 if (state->resampleTemp) {
434 delete [] state->resampleTemp;
Glenn Kastenc434c902011-12-13 11:53:26 -0800435 state->resampleTemp = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 }
437 state->hook = process__genericNoResampling;
438 if (all16BitsStereoNoResample && !volumeRamp) {
439 if (countActiveTracks == 1) {
440 state->hook = process__OneTrack16BitsStereoNoResampling;
441 }
442 }
443 }
444 }
445
Steve Block71f2cf12011-10-20 11:56:00 +0100446 ALOGV("mixer configuration change: %d activeTracks (%08x) "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 "all16BitsStereoNoResample=%d, resampling=%d, volumeRamp=%d",
448 countActiveTracks, state->enabledTracks,
449 all16BitsStereoNoResample, resampling, volumeRamp);
450
John Grossmand8cf2962012-02-08 16:37:41 -0800451 state->hook(state, pts);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452
Glenn Kastenfb2ab9e2011-12-12 09:05:55 -0800453 // Now that the volume ramp has been done, set optimal state and
454 // track hooks for subsequent mixer process
455 if (countActiveTracks) {
Glenn Kasten450b9982012-01-27 12:33:54 -0800456 bool allMuted = true;
Glenn Kastenfb2ab9e2011-12-12 09:05:55 -0800457 uint32_t en = state->enabledTracks;
458 while (en) {
459 const int i = 31 - __builtin_clz(en);
460 en &= ~(1<<i);
461 track_t& t = state->tracks[i];
462 if (!t.doesResample() && t.volumeRL == 0)
463 {
464 t.needs |= NEEDS_MUTE_ENABLED;
465 t.hook = track__nop;
466 } else {
Glenn Kasten450b9982012-01-27 12:33:54 -0800467 allMuted = false;
Glenn Kastenfb2ab9e2011-12-12 09:05:55 -0800468 }
469 }
470 if (allMuted) {
471 state->hook = process__nop;
472 } else if (all16BitsStereoNoResample) {
473 if (countActiveTracks == 1) {
474 state->hook = process__OneTrack16BitsStereoNoResampling;
475 }
476 }
477 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478}
479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480
Eric Laurent65b65452010-06-01 23:49:17 -0700481void AudioMixer::track__genericResample(track_t* t, int32_t* out, size_t outFrameCount, int32_t* temp, int32_t* aux)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482{
483 t->resampler->setSampleRate(t->sampleRate);
484
485 // ramp gain - resample to temp buffer and scale/mix in 2nd step
Eric Laurent65b65452010-06-01 23:49:17 -0700486 if (aux != NULL) {
487 // always resample with unity gain when sending to auxiliary buffer to be able
488 // to apply send level after resampling
489 // TODO: modify each resampler to support aux channel?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 t->resampler->setVolume(UNITY_GAIN, UNITY_GAIN);
491 memset(temp, 0, outFrameCount * MAX_NUM_CHANNELS * sizeof(int32_t));
492 t->resampler->resample(temp, outFrameCount, t->bufferProvider);
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800493 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) {
Eric Laurent65b65452010-06-01 23:49:17 -0700494 volumeRampStereo(t, out, outFrameCount, temp, aux);
495 } else {
496 volumeStereo(t, out, outFrameCount, temp, aux);
497 }
498 } else {
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800499 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) {
Eric Laurent65b65452010-06-01 23:49:17 -0700500 t->resampler->setVolume(UNITY_GAIN, UNITY_GAIN);
501 memset(temp, 0, outFrameCount * MAX_NUM_CHANNELS * sizeof(int32_t));
502 t->resampler->resample(temp, outFrameCount, t->bufferProvider);
503 volumeRampStereo(t, out, outFrameCount, temp, aux);
504 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505
Eric Laurent65b65452010-06-01 23:49:17 -0700506 // constant gain
507 else {
508 t->resampler->setVolume(t->volume[0], t->volume[1]);
509 t->resampler->resample(out, outFrameCount, t->bufferProvider);
510 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 }
512}
513
Eric Laurent65b65452010-06-01 23:49:17 -0700514void AudioMixer::track__nop(track_t* t, int32_t* out, size_t outFrameCount, int32_t* temp, int32_t* aux)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515{
516}
517
Eric Laurent65b65452010-06-01 23:49:17 -0700518void AudioMixer::volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519{
520 int32_t vl = t->prevVolume[0];
521 int32_t vr = t->prevVolume[1];
522 const int32_t vlInc = t->volumeInc[0];
523 const int32_t vrInc = t->volumeInc[1];
524
Steve Block5baa3a62011-12-20 16:23:08 +0000525 //ALOGD("[0] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
527 // (vl + vlInc*frameCount)/65536.0f, frameCount);
Eric Laurent65b65452010-06-01 23:49:17 -0700528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 // ramp volume
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800530 if (CC_UNLIKELY(aux != NULL)) {
Eric Laurent65b65452010-06-01 23:49:17 -0700531 int32_t va = t->prevAuxLevel;
532 const int32_t vaInc = t->auxInc;
533 int32_t l;
534 int32_t r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535
536 do {
Eric Laurent65b65452010-06-01 23:49:17 -0700537 l = (*temp++ >> 12);
538 r = (*temp++ >> 12);
539 *out++ += (vl >> 16) * l;
540 *out++ += (vr >> 16) * r;
541 *aux++ += (va >> 17) * (l + r);
542 vl += vlInc;
543 vr += vrInc;
544 va += vaInc;
545 } while (--frameCount);
546 t->prevAuxLevel = va;
547 } else {
548 do {
549 *out++ += (vl >> 16) * (*temp++ >> 12);
550 *out++ += (vr >> 16) * (*temp++ >> 12);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 vl += vlInc;
552 vr += vrInc;
553 } while (--frameCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 }
Eric Laurent65b65452010-06-01 23:49:17 -0700555 t->prevVolume[0] = vl;
556 t->prevVolume[1] = vr;
Glenn Kasten7d3be3a2012-01-26 10:53:32 -0800557 t->adjustVolumeRamp(aux != NULL);
Eric Laurent65b65452010-06-01 23:49:17 -0700558}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559
Eric Laurent65b65452010-06-01 23:49:17 -0700560void AudioMixer::volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux)
561{
562 const int16_t vl = t->volume[0];
563 const int16_t vr = t->volume[1];
564
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800565 if (CC_UNLIKELY(aux != NULL)) {
Glenn Kasten4f22c052012-01-27 15:26:23 -0800566 const int16_t va = t->auxLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 do {
Eric Laurent65b65452010-06-01 23:49:17 -0700568 int16_t l = (int16_t)(*temp++ >> 12);
569 int16_t r = (int16_t)(*temp++ >> 12);
570 out[0] = mulAdd(l, vl, out[0]);
571 int16_t a = (int16_t)(((int32_t)l + r) >> 1);
572 out[1] = mulAdd(r, vr, out[1]);
573 out += 2;
574 aux[0] = mulAdd(a, va, aux[0]);
575 aux++;
576 } while (--frameCount);
577 } else {
578 do {
579 int16_t l = (int16_t)(*temp++ >> 12);
580 int16_t r = (int16_t)(*temp++ >> 12);
581 out[0] = mulAdd(l, vl, out[0]);
582 out[1] = mulAdd(r, vr, out[1]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 out += 2;
584 } while (--frameCount);
585 }
Eric Laurent65b65452010-06-01 23:49:17 -0700586}
587
588void AudioMixer::track__16BitsStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux)
589{
Glenn Kasten99c2fd32012-01-06 07:46:30 -0800590 const int16_t *in = static_cast<const int16_t *>(t->in);
Eric Laurent65b65452010-06-01 23:49:17 -0700591
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800592 if (CC_UNLIKELY(aux != NULL)) {
Eric Laurent65b65452010-06-01 23:49:17 -0700593 int32_t l;
594 int32_t r;
595 // ramp gain
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800596 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) {
Eric Laurent65b65452010-06-01 23:49:17 -0700597 int32_t vl = t->prevVolume[0];
598 int32_t vr = t->prevVolume[1];
599 int32_t va = t->prevAuxLevel;
600 const int32_t vlInc = t->volumeInc[0];
601 const int32_t vrInc = t->volumeInc[1];
602 const int32_t vaInc = t->auxInc;
Steve Block5baa3a62011-12-20 16:23:08 +0000603 // ALOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
Eric Laurent65b65452010-06-01 23:49:17 -0700604 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
605 // (vl + vlInc*frameCount)/65536.0f, frameCount);
606
607 do {
608 l = (int32_t)*in++;
609 r = (int32_t)*in++;
610 *out++ += (vl >> 16) * l;
611 *out++ += (vr >> 16) * r;
612 *aux++ += (va >> 17) * (l + r);
613 vl += vlInc;
614 vr += vrInc;
615 va += vaInc;
616 } while (--frameCount);
617
618 t->prevVolume[0] = vl;
619 t->prevVolume[1] = vr;
620 t->prevAuxLevel = va;
621 t->adjustVolumeRamp(true);
622 }
623
624 // constant gain
625 else {
626 const uint32_t vrl = t->volumeRL;
627 const int16_t va = (int16_t)t->auxLevel;
628 do {
Glenn Kasten99c2fd32012-01-06 07:46:30 -0800629 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
Eric Laurent65b65452010-06-01 23:49:17 -0700630 int16_t a = (int16_t)(((int32_t)in[0] + in[1]) >> 1);
631 in += 2;
632 out[0] = mulAddRL(1, rl, vrl, out[0]);
633 out[1] = mulAddRL(0, rl, vrl, out[1]);
634 out += 2;
635 aux[0] = mulAdd(a, va, aux[0]);
636 aux++;
637 } while (--frameCount);
638 }
639 } else {
640 // ramp gain
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800641 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) {
Eric Laurent65b65452010-06-01 23:49:17 -0700642 int32_t vl = t->prevVolume[0];
643 int32_t vr = t->prevVolume[1];
644 const int32_t vlInc = t->volumeInc[0];
645 const int32_t vrInc = t->volumeInc[1];
646
Steve Block5baa3a62011-12-20 16:23:08 +0000647 // ALOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
Eric Laurent65b65452010-06-01 23:49:17 -0700648 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
649 // (vl + vlInc*frameCount)/65536.0f, frameCount);
650
651 do {
652 *out++ += (vl >> 16) * (int32_t) *in++;
653 *out++ += (vr >> 16) * (int32_t) *in++;
654 vl += vlInc;
655 vr += vrInc;
656 } while (--frameCount);
657
658 t->prevVolume[0] = vl;
659 t->prevVolume[1] = vr;
660 t->adjustVolumeRamp(false);
661 }
662
663 // constant gain
664 else {
665 const uint32_t vrl = t->volumeRL;
666 do {
Glenn Kasten99c2fd32012-01-06 07:46:30 -0800667 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
Eric Laurent65b65452010-06-01 23:49:17 -0700668 in += 2;
669 out[0] = mulAddRL(1, rl, vrl, out[0]);
670 out[1] = mulAddRL(0, rl, vrl, out[1]);
671 out += 2;
672 } while (--frameCount);
673 }
674 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 t->in = in;
676}
677
Eric Laurent65b65452010-06-01 23:49:17 -0700678void AudioMixer::track__16BitsMono(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679{
Glenn Kasten99c2fd32012-01-06 07:46:30 -0800680 const int16_t *in = static_cast<int16_t const *>(t->in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800682 if (CC_UNLIKELY(aux != NULL)) {
Eric Laurent65b65452010-06-01 23:49:17 -0700683 // ramp gain
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800684 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) {
Eric Laurent65b65452010-06-01 23:49:17 -0700685 int32_t vl = t->prevVolume[0];
686 int32_t vr = t->prevVolume[1];
687 int32_t va = t->prevAuxLevel;
688 const int32_t vlInc = t->volumeInc[0];
689 const int32_t vrInc = t->volumeInc[1];
690 const int32_t vaInc = t->auxInc;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691
Steve Block5baa3a62011-12-20 16:23:08 +0000692 // ALOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
Eric Laurent65b65452010-06-01 23:49:17 -0700693 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
694 // (vl + vlInc*frameCount)/65536.0f, frameCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695
Eric Laurent65b65452010-06-01 23:49:17 -0700696 do {
697 int32_t l = *in++;
698 *out++ += (vl >> 16) * l;
699 *out++ += (vr >> 16) * l;
700 *aux++ += (va >> 16) * l;
701 vl += vlInc;
702 vr += vrInc;
703 va += vaInc;
704 } while (--frameCount);
705
706 t->prevVolume[0] = vl;
707 t->prevVolume[1] = vr;
708 t->prevAuxLevel = va;
709 t->adjustVolumeRamp(true);
710 }
711 // constant gain
712 else {
713 const int16_t vl = t->volume[0];
714 const int16_t vr = t->volume[1];
715 const int16_t va = (int16_t)t->auxLevel;
716 do {
717 int16_t l = *in++;
718 out[0] = mulAdd(l, vl, out[0]);
719 out[1] = mulAdd(l, vr, out[1]);
720 out += 2;
721 aux[0] = mulAdd(l, va, aux[0]);
722 aux++;
723 } while (--frameCount);
724 }
725 } else {
726 // ramp gain
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800727 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) {
Eric Laurent65b65452010-06-01 23:49:17 -0700728 int32_t vl = t->prevVolume[0];
729 int32_t vr = t->prevVolume[1];
730 const int32_t vlInc = t->volumeInc[0];
731 const int32_t vrInc = t->volumeInc[1];
732
Steve Block5baa3a62011-12-20 16:23:08 +0000733 // ALOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
Eric Laurent65b65452010-06-01 23:49:17 -0700734 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
735 // (vl + vlInc*frameCount)/65536.0f, frameCount);
736
737 do {
738 int32_t l = *in++;
739 *out++ += (vl >> 16) * l;
740 *out++ += (vr >> 16) * l;
741 vl += vlInc;
742 vr += vrInc;
743 } while (--frameCount);
744
745 t->prevVolume[0] = vl;
746 t->prevVolume[1] = vr;
747 t->adjustVolumeRamp(false);
748 }
749 // constant gain
750 else {
751 const int16_t vl = t->volume[0];
752 const int16_t vr = t->volume[1];
753 do {
754 int16_t l = *in++;
755 out[0] = mulAdd(l, vl, out[0]);
756 out[1] = mulAdd(l, vr, out[1]);
757 out += 2;
758 } while (--frameCount);
759 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 }
761 t->in = in;
762}
763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764// no-op case
John Grossmand8cf2962012-02-08 16:37:41 -0800765void AudioMixer::process__nop(state_t* state, int64_t pts)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766{
Eric Laurent65b65452010-06-01 23:49:17 -0700767 uint32_t e0 = state->enabledTracks;
768 size_t bufSize = state->frameCount * sizeof(int16_t) * MAX_NUM_CHANNELS;
769 while (e0) {
770 // process by group of tracks with same output buffer to
771 // avoid multiple memset() on same buffer
772 uint32_t e1 = e0, e2 = e0;
773 int i = 31 - __builtin_clz(e1);
774 track_t& t1 = state->tracks[i];
775 e2 &= ~(1<<i);
776 while (e2) {
777 i = 31 - __builtin_clz(e2);
778 e2 &= ~(1<<i);
779 track_t& t2 = state->tracks[i];
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800780 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) {
Eric Laurent65b65452010-06-01 23:49:17 -0700781 e1 &= ~(1<<i);
782 }
783 }
784 e0 &= ~(e1);
785
786 memset(t1.mainBuffer, 0, bufSize);
787
788 while (e1) {
789 i = 31 - __builtin_clz(e1);
790 e1 &= ~(1<<i);
791 t1 = state->tracks[i];
792 size_t outFrames = state->frameCount;
793 while (outFrames) {
794 t1.buffer.frameCount = outFrames;
John Grossmand8cf2962012-02-08 16:37:41 -0800795 int64_t outputPTS = calculateOutputPTS(
796 t1, pts, state->frameCount - outFrames);
797 t1.bufferProvider->getNextBuffer(&t1.buffer, outputPTS);
Glenn Kasten3694ec12012-01-27 16:47:15 -0800798 if (t1.buffer.raw == NULL) break;
Eric Laurent65b65452010-06-01 23:49:17 -0700799 outFrames -= t1.buffer.frameCount;
800 t1.bufferProvider->releaseBuffer(&t1.buffer);
801 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 }
803 }
804}
805
806// generic code without resampling
John Grossmand8cf2962012-02-08 16:37:41 -0800807void AudioMixer::process__genericNoResampling(state_t* state, int64_t pts)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808{
809 int32_t outTemp[BLOCKSIZE * MAX_NUM_CHANNELS] __attribute__((aligned(32)));
810
811 // acquire each track's buffer
812 uint32_t enabledTracks = state->enabledTracks;
Eric Laurent65b65452010-06-01 23:49:17 -0700813 uint32_t e0 = enabledTracks;
814 while (e0) {
815 const int i = 31 - __builtin_clz(e0);
816 e0 &= ~(1<<i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817 track_t& t = state->tracks[i];
818 t.buffer.frameCount = state->frameCount;
John Grossmand8cf2962012-02-08 16:37:41 -0800819 t.bufferProvider->getNextBuffer(&t.buffer, pts);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 t.frameCount = t.buffer.frameCount;
821 t.in = t.buffer.raw;
822 // t.in == NULL can happen if the track was flushed just after having
823 // been enabled for mixing.
824 if (t.in == NULL)
825 enabledTracks &= ~(1<<i);
826 }
827
Eric Laurent65b65452010-06-01 23:49:17 -0700828 e0 = enabledTracks;
829 while (e0) {
830 // process by group of tracks with same output buffer to
831 // optimize cache use
832 uint32_t e1 = e0, e2 = e0;
833 int j = 31 - __builtin_clz(e1);
834 track_t& t1 = state->tracks[j];
835 e2 &= ~(1<<j);
836 while (e2) {
837 j = 31 - __builtin_clz(e2);
838 e2 &= ~(1<<j);
839 track_t& t2 = state->tracks[j];
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800840 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) {
Eric Laurent65b65452010-06-01 23:49:17 -0700841 e1 &= ~(1<<j);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 }
843 }
Eric Laurent65b65452010-06-01 23:49:17 -0700844 e0 &= ~(e1);
845 // this assumes output 16 bits stereo, no resampling
846 int32_t *out = t1.mainBuffer;
847 size_t numFrames = 0;
848 do {
849 memset(outTemp, 0, sizeof(outTemp));
850 e2 = e1;
851 while (e2) {
852 const int i = 31 - __builtin_clz(e2);
853 e2 &= ~(1<<i);
854 track_t& t = state->tracks[i];
855 size_t outFrames = BLOCKSIZE;
856 int32_t *aux = NULL;
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800857 if (CC_UNLIKELY((t.needs & NEEDS_AUX__MASK) == NEEDS_AUX_ENABLED)) {
Eric Laurent65b65452010-06-01 23:49:17 -0700858 aux = t.auxBuffer + numFrames;
859 }
860 while (outFrames) {
861 size_t inFrames = (t.frameCount > outFrames)?outFrames:t.frameCount;
862 if (inFrames) {
Glenn Kasten7d3be3a2012-01-26 10:53:32 -0800863 t.hook(&t, outTemp + (BLOCKSIZE-outFrames)*MAX_NUM_CHANNELS, inFrames, state->resampleTemp, aux);
Eric Laurent65b65452010-06-01 23:49:17 -0700864 t.frameCount -= inFrames;
865 outFrames -= inFrames;
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800866 if (CC_UNLIKELY(aux != NULL)) {
Eric Laurent65b65452010-06-01 23:49:17 -0700867 aux += inFrames;
868 }
869 }
870 if (t.frameCount == 0 && outFrames) {
871 t.bufferProvider->releaseBuffer(&t.buffer);
872 t.buffer.frameCount = (state->frameCount - numFrames) - (BLOCKSIZE - outFrames);
John Grossmand8cf2962012-02-08 16:37:41 -0800873 int64_t outputPTS = calculateOutputPTS(
874 t, pts, numFrames + (BLOCKSIZE - outFrames));
875 t.bufferProvider->getNextBuffer(&t.buffer, outputPTS);
Eric Laurent65b65452010-06-01 23:49:17 -0700876 t.in = t.buffer.raw;
877 if (t.in == NULL) {
878 enabledTracks &= ~(1<<i);
879 e1 &= ~(1<<i);
880 break;
881 }
882 t.frameCount = t.buffer.frameCount;
883 }
884 }
885 }
886 ditherAndClamp(out, outTemp, BLOCKSIZE);
887 out += BLOCKSIZE;
888 numFrames += BLOCKSIZE;
889 } while (numFrames < state->frameCount);
890 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891
892 // release each track's buffer
Eric Laurent65b65452010-06-01 23:49:17 -0700893 e0 = enabledTracks;
894 while (e0) {
895 const int i = 31 - __builtin_clz(e0);
896 e0 &= ~(1<<i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 track_t& t = state->tracks[i];
898 t.bufferProvider->releaseBuffer(&t.buffer);
899 }
900}
901
Eric Laurent65b65452010-06-01 23:49:17 -0700902
Glenn Kastenfb2ab9e2011-12-12 09:05:55 -0800903// generic code with resampling
John Grossmand8cf2962012-02-08 16:37:41 -0800904void AudioMixer::process__genericResampling(state_t* state, int64_t pts)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905{
Glenn Kasten99c2fd32012-01-06 07:46:30 -0800906 // this const just means that local variable outTemp doesn't change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 int32_t* const outTemp = state->outputTemp;
908 const size_t size = sizeof(int32_t) * MAX_NUM_CHANNELS * state->frameCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 size_t numFrames = state->frameCount;
911
Eric Laurent65b65452010-06-01 23:49:17 -0700912 uint32_t e0 = state->enabledTracks;
913 while (e0) {
914 // process by group of tracks with same output buffer
915 // to optimize cache use
916 uint32_t e1 = e0, e2 = e0;
917 int j = 31 - __builtin_clz(e1);
918 track_t& t1 = state->tracks[j];
919 e2 &= ~(1<<j);
920 while (e2) {
921 j = 31 - __builtin_clz(e2);
922 e2 &= ~(1<<j);
923 track_t& t2 = state->tracks[j];
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800924 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) {
Eric Laurent65b65452010-06-01 23:49:17 -0700925 e1 &= ~(1<<j);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 }
927 }
Eric Laurent65b65452010-06-01 23:49:17 -0700928 e0 &= ~(e1);
929 int32_t *out = t1.mainBuffer;
Yuuhi Yamaguchi681d8182011-02-04 15:24:34 +0100930 memset(outTemp, 0, size);
Eric Laurent65b65452010-06-01 23:49:17 -0700931 while (e1) {
932 const int i = 31 - __builtin_clz(e1);
933 e1 &= ~(1<<i);
934 track_t& t = state->tracks[i];
935 int32_t *aux = NULL;
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800936 if (CC_UNLIKELY((t.needs & NEEDS_AUX__MASK) == NEEDS_AUX_ENABLED)) {
Eric Laurent65b65452010-06-01 23:49:17 -0700937 aux = t.auxBuffer;
938 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939
Eric Laurent65b65452010-06-01 23:49:17 -0700940 // this is a little goofy, on the resampling case we don't
941 // acquire/release the buffers because it's done by
942 // the resampler.
943 if ((t.needs & NEEDS_RESAMPLE__MASK) == NEEDS_RESAMPLE_ENABLED) {
John Grossmand8cf2962012-02-08 16:37:41 -0800944 t.resampler->setPTS(pts);
Glenn Kasten7d3be3a2012-01-26 10:53:32 -0800945 t.hook(&t, outTemp, numFrames, state->resampleTemp, aux);
Eric Laurent65b65452010-06-01 23:49:17 -0700946 } else {
947
948 size_t outFrames = 0;
949
950 while (outFrames < numFrames) {
951 t.buffer.frameCount = numFrames - outFrames;
John Grossmand8cf2962012-02-08 16:37:41 -0800952 int64_t outputPTS = calculateOutputPTS(t, pts, outFrames);
953 t.bufferProvider->getNextBuffer(&t.buffer, outputPTS);
Eric Laurent65b65452010-06-01 23:49:17 -0700954 t.in = t.buffer.raw;
955 // t.in == NULL can happen if the track was flushed just after having
956 // been enabled for mixing.
957 if (t.in == NULL) break;
958
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800959 if (CC_UNLIKELY(aux != NULL)) {
Eric Laurent65b65452010-06-01 23:49:17 -0700960 aux += outFrames;
961 }
Glenn Kasten7d3be3a2012-01-26 10:53:32 -0800962 t.hook(&t, outTemp + outFrames*MAX_NUM_CHANNELS, t.buffer.frameCount, state->resampleTemp, aux);
Eric Laurent65b65452010-06-01 23:49:17 -0700963 outFrames += t.buffer.frameCount;
964 t.bufferProvider->releaseBuffer(&t.buffer);
965 }
966 }
967 }
968 ditherAndClamp(out, outTemp, numFrames);
969 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970}
971
972// one track, 16 bits stereo without resampling is the most common case
John Grossmand8cf2962012-02-08 16:37:41 -0800973void AudioMixer::process__OneTrack16BitsStereoNoResampling(state_t* state,
974 int64_t pts)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975{
Glenn Kastenb3db2132012-01-19 08:59:58 -0800976 // This method is only called when state->enabledTracks has exactly
977 // one bit set. The asserts below would verify this, but are commented out
978 // since the whole point of this method is to optimize performance.
Glenn Kasten81916df2012-03-08 12:18:35 -0800979 //ALOG_ASSERT(0 != state->enabledTracks, "no tracks enabled");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 const int i = 31 - __builtin_clz(state->enabledTracks);
Glenn Kasten81916df2012-03-08 12:18:35 -0800981 //ALOG_ASSERT((1 << i) == state->enabledTracks, "more than 1 track enabled");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 const track_t& t = state->tracks[i];
983
984 AudioBufferProvider::Buffer& b(t.buffer);
Eric Laurent65b65452010-06-01 23:49:17 -0700985
986 int32_t* out = t.mainBuffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 size_t numFrames = state->frameCount;
Eric Laurent65b65452010-06-01 23:49:17 -0700988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 const int16_t vl = t.volume[0];
990 const int16_t vr = t.volume[1];
991 const uint32_t vrl = t.volumeRL;
992 while (numFrames) {
993 b.frameCount = numFrames;
John Grossmand8cf2962012-02-08 16:37:41 -0800994 int64_t outputPTS = calculateOutputPTS(t, pts, out - t.mainBuffer);
995 t.bufferProvider->getNextBuffer(&b, outputPTS);
Glenn Kasten99c2fd32012-01-06 07:46:30 -0800996 const int16_t *in = b.i16;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800997
998 // in == NULL can happen if the track was flushed just after having
999 // been enabled for mixing.
The Android Open Source Project10592532009-03-18 17:39:46 -07001000 if (in == NULL || ((unsigned long)in & 3)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 memset(out, 0, numFrames*MAX_NUM_CHANNELS*sizeof(int16_t));
Steve Block3762c312012-01-06 19:20:56 +00001002 ALOGE_IF(((unsigned long)in & 3), "process stereo track: input buffer alignment pb: buffer %p track %d, channels %d, needs %08x",
The Android Open Source Project10592532009-03-18 17:39:46 -07001003 in, i, t.channelCount, t.needs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 return;
1005 }
1006 size_t outFrames = b.frameCount;
Eric Laurent65b65452010-06-01 23:49:17 -07001007
Glenn Kastene80a4cc2011-12-15 09:51:17 -08001008 if (CC_UNLIKELY(uint32_t(vl) > UNITY_GAIN || uint32_t(vr) > UNITY_GAIN)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 // volume is boosted, so we might need to clamp even though
1010 // we process only one track.
1011 do {
Glenn Kasten99c2fd32012-01-06 07:46:30 -08001012 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 in += 2;
1014 int32_t l = mulRL(1, rl, vrl) >> 12;
1015 int32_t r = mulRL(0, rl, vrl) >> 12;
1016 // clamping...
1017 l = clamp16(l);
1018 r = clamp16(r);
1019 *out++ = (r<<16) | (l & 0xFFFF);
1020 } while (--outFrames);
1021 } else {
1022 do {
Glenn Kasten99c2fd32012-01-06 07:46:30 -08001023 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 in += 2;
1025 int32_t l = mulRL(1, rl, vrl) >> 12;
1026 int32_t r = mulRL(0, rl, vrl) >> 12;
1027 *out++ = (r<<16) | (l & 0xFFFF);
1028 } while (--outFrames);
1029 }
1030 numFrames -= b.frameCount;
1031 t.bufferProvider->releaseBuffer(&b);
1032 }
1033}
1034
Glenn Kasten31ba2e52011-12-15 09:53:12 -08001035#if 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036// 2 tracks is also a common case
Eric Laurent65b65452010-06-01 23:49:17 -07001037// NEVER used in current implementation of process__validate()
1038// only use if the 2 tracks have the same output buffer
John Grossmand8cf2962012-02-08 16:37:41 -08001039void AudioMixer::process__TwoTracks16BitsStereoNoResampling(state_t* state,
1040 int64_t pts)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041{
1042 int i;
1043 uint32_t en = state->enabledTracks;
1044
1045 i = 31 - __builtin_clz(en);
1046 const track_t& t0 = state->tracks[i];
1047 AudioBufferProvider::Buffer& b0(t0.buffer);
1048
1049 en &= ~(1<<i);
1050 i = 31 - __builtin_clz(en);
1051 const track_t& t1 = state->tracks[i];
1052 AudioBufferProvider::Buffer& b1(t1.buffer);
Eric Laurent65b65452010-06-01 23:49:17 -07001053
Glenn Kasten99c2fd32012-01-06 07:46:30 -08001054 const int16_t *in0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 const int16_t vl0 = t0.volume[0];
1056 const int16_t vr0 = t0.volume[1];
1057 size_t frameCount0 = 0;
Eric Laurent65b65452010-06-01 23:49:17 -07001058
Glenn Kasten99c2fd32012-01-06 07:46:30 -08001059 const int16_t *in1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 const int16_t vl1 = t1.volume[0];
1061 const int16_t vr1 = t1.volume[1];
1062 size_t frameCount1 = 0;
Eric Laurent65b65452010-06-01 23:49:17 -07001063
1064 //FIXME: only works if two tracks use same buffer
1065 int32_t* out = t0.mainBuffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 size_t numFrames = state->frameCount;
Glenn Kasten99c2fd32012-01-06 07:46:30 -08001067 const int16_t *buff = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068
Eric Laurent65b65452010-06-01 23:49:17 -07001069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 while (numFrames) {
Eric Laurent65b65452010-06-01 23:49:17 -07001071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 if (frameCount0 == 0) {
1073 b0.frameCount = numFrames;
John Grossmand8cf2962012-02-08 16:37:41 -08001074 int64_t outputPTS = calculateOutputPTS(t0, pts,
1075 out - t0.mainBuffer);
1076 t0.bufferProvider->getNextBuffer(&b0, outputPTS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 if (b0.i16 == NULL) {
1078 if (buff == NULL) {
1079 buff = new int16_t[MAX_NUM_CHANNELS * state->frameCount];
1080 }
1081 in0 = buff;
1082 b0.frameCount = numFrames;
1083 } else {
1084 in0 = b0.i16;
1085 }
1086 frameCount0 = b0.frameCount;
1087 }
1088 if (frameCount1 == 0) {
1089 b1.frameCount = numFrames;
John Grossmand8cf2962012-02-08 16:37:41 -08001090 int64_t outputPTS = calculateOutputPTS(t1, pts,
1091 out - t0.mainBuffer);
1092 t1.bufferProvider->getNextBuffer(&b1, outputPTS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 if (b1.i16 == NULL) {
1094 if (buff == NULL) {
1095 buff = new int16_t[MAX_NUM_CHANNELS * state->frameCount];
1096 }
1097 in1 = buff;
1098 b1.frameCount = numFrames;
Glenn Kastenfb2ab9e2011-12-12 09:05:55 -08001099 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 in1 = b1.i16;
1101 }
1102 frameCount1 = b1.frameCount;
1103 }
Eric Laurent65b65452010-06-01 23:49:17 -07001104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 size_t outFrames = frameCount0 < frameCount1?frameCount0:frameCount1;
1106
1107 numFrames -= outFrames;
1108 frameCount0 -= outFrames;
1109 frameCount1 -= outFrames;
Eric Laurent65b65452010-06-01 23:49:17 -07001110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 do {
1112 int32_t l0 = *in0++;
1113 int32_t r0 = *in0++;
1114 l0 = mul(l0, vl0);
1115 r0 = mul(r0, vr0);
1116 int32_t l = *in1++;
1117 int32_t r = *in1++;
1118 l = mulAdd(l, vl1, l0) >> 12;
1119 r = mulAdd(r, vr1, r0) >> 12;
1120 // clamping...
1121 l = clamp16(l);
1122 r = clamp16(r);
1123 *out++ = (r<<16) | (l & 0xFFFF);
1124 } while (--outFrames);
Eric Laurent65b65452010-06-01 23:49:17 -07001125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 if (frameCount0 == 0) {
1127 t0.bufferProvider->releaseBuffer(&b0);
1128 }
1129 if (frameCount1 == 0) {
1130 t1.bufferProvider->releaseBuffer(&b1);
1131 }
Eric Laurent65b65452010-06-01 23:49:17 -07001132 }
1133
Glenn Kasten2589cf92012-01-27 18:08:45 -08001134 delete [] buff;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135}
Glenn Kasten31ba2e52011-12-15 09:53:12 -08001136#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137
John Grossmand8cf2962012-02-08 16:37:41 -08001138int64_t AudioMixer::calculateOutputPTS(const track_t& t, int64_t basePTS,
1139 int outputFrameIndex)
1140{
1141 if (AudioBufferProvider::kInvalidPTS == basePTS)
1142 return AudioBufferProvider::kInvalidPTS;
1143
1144 return basePTS + ((outputFrameIndex * t.localTimeFreq) / t.sampleRate);
1145}
1146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147// ----------------------------------------------------------------------------
1148}; // namespace android