blob: c9c61a5814a7b559b9934eaf2f428a489e4b2728 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/* //device/include/server/AudioFlinger/AudioMixer.cpp
2**
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
Glenn Kastenafb40b52011-12-15 15:46:46 -080021#include <assert.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022#include <stdint.h>
23#include <string.h>
24#include <stdlib.h>
25#include <sys/types.h>
26
27#include <utils/Errors.h>
28#include <utils/Log.h>
29
Jean-Michel Trivi54392232011-05-24 15:53:33 -070030#include <cutils/bitops.h>
Glenn Kastene80a4cc2011-12-15 09:51:17 -080031#include <cutils/compiler.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>
36
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037#include "AudioMixer.h"
38
39namespace android {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
41// ----------------------------------------------------------------------------
42
43AudioMixer::AudioMixer(size_t frameCount, uint32_t sampleRate)
Glenn Kasten68deb152011-12-19 15:06:39 -080044 : mTrackNames(0), mSampleRate(sampleRate)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045{
Glenn Kastenbde164a2011-05-05 08:19:00 -070046 // AudioMixer is not yet capable of multi-channel beyond stereo
47 assert(2 == MAX_NUM_CHANNELS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048 mState.enabledTracks= 0;
49 mState.needsChanged = 0;
50 mState.frameCount = frameCount;
Glenn Kastenc434c902011-12-13 11:53:26 -080051 mState.outputTemp = NULL;
52 mState.resampleTemp = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 mState.hook = process__nop;
54 track_t* t = mState.tracks;
Glenn Kasten1f6f05d2011-12-13 11:52:35 -080055 for (unsigned i=0 ; i < MAX_NUM_TRACKS ; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 t->needs = 0;
57 t->volume[0] = UNITY_GAIN;
58 t->volume[1] = UNITY_GAIN;
Glenn Kastena763b442011-12-13 11:58:23 -080059 // no initialization needed
60 // t->prevVolume[0]
61 // t->prevVolume[1]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 t->volumeInc[0] = 0;
63 t->volumeInc[1] = 0;
Eric Laurent65b65452010-06-01 23:49:17 -070064 t->auxLevel = 0;
65 t->auxInc = 0;
Glenn Kastena763b442011-12-13 11:58:23 -080066 // no initialization needed
67 // t->prevAuxLevel
68 // t->frameCount
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 t->channelCount = 2;
70 t->enabled = 0;
71 t->format = 16;
Jean-Michel Trivi54392232011-05-24 15:53:33 -070072 t->channelMask = AUDIO_CHANNEL_OUT_STEREO;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 t->buffer.raw = 0;
Glenn Kastenc434c902011-12-13 11:53:26 -080074 t->bufferProvider = NULL;
75 t->hook = NULL;
76 t->resampler = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 t->sampleRate = mSampleRate;
Glenn Kastenc434c902011-12-13 11:53:26 -080078 t->in = NULL;
Eric Laurent65b65452010-06-01 23:49:17 -070079 t->mainBuffer = NULL;
80 t->auxBuffer = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 t++;
82 }
83}
84
Glenn Kastenfb2ab9e2011-12-12 09:05:55 -080085AudioMixer::~AudioMixer()
86{
87 track_t* t = mState.tracks;
Glenn Kasten1f6f05d2011-12-13 11:52:35 -080088 for (unsigned i=0 ; i < MAX_NUM_TRACKS ; i++) {
Glenn Kastenfb2ab9e2011-12-12 09:05:55 -080089 delete t->resampler;
90 t++;
91 }
92 delete [] mState.outputTemp;
93 delete [] mState.resampleTemp;
94}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095
Glenn Kastenfb2ab9e2011-12-12 09:05:55 -080096int AudioMixer::getTrackName()
97{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 uint32_t names = mTrackNames;
99 uint32_t mask = 1;
100 int n = 0;
101 while (names & mask) {
102 mask <<= 1;
103 n++;
104 }
105 if (mask) {
Steve Block71f2cf12011-10-20 11:56:00 +0100106 ALOGV("add track (%d)", n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 mTrackNames |= mask;
108 return TRACK0 + n;
109 }
110 return -1;
Glenn Kastenfb2ab9e2011-12-12 09:05:55 -0800111}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112
Glenn Kastenfb2ab9e2011-12-12 09:05:55 -0800113void AudioMixer::invalidateState(uint32_t mask)
114{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 if (mask) {
116 mState.needsChanged |= mask;
117 mState.hook = process__validate;
118 }
119 }
120
Glenn Kastenfb2ab9e2011-12-12 09:05:55 -0800121void AudioMixer::deleteTrackName(int name)
122{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 name -= TRACK0;
Glenn Kastenb3719252011-12-15 15:32:27 -0800124 assert(uint32_t(name) < MAX_NUM_TRACKS);
125 ALOGV("deleteTrackName(%d)", name);
126 track_t& track(mState.tracks[ name ]);
127 if (track.enabled != 0) {
128 track.enabled = 0;
129 invalidateState(1<<name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 }
Glenn Kastenb3719252011-12-15 15:32:27 -0800131 if (track.resampler) {
132 // delete the resampler
133 delete track.resampler;
134 track.resampler = NULL;
135 track.sampleRate = mSampleRate;
136 invalidateState(1<<name);
137 }
138 track.volumeInc[0] = 0;
139 track.volumeInc[1] = 0;
140 mTrackNames &= ~(1<<name);
Glenn Kastenfb2ab9e2011-12-12 09:05:55 -0800141}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142
Glenn Kasten68deb152011-12-19 15:06:39 -0800143void AudioMixer::enable(int name)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144{
Glenn Kasten68deb152011-12-19 15:06:39 -0800145 name -= TRACK0;
146 assert(uint32_t(name) < MAX_NUM_TRACKS);
147 track_t& track = mState.tracks[name];
148
149 if (track.enabled != 1) {
150 track.enabled = 1;
151 ALOGV("enable(%d)", name);
152 invalidateState(1 << name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154}
155
Glenn Kasten68deb152011-12-19 15:06:39 -0800156void AudioMixer::disable(int name)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157{
Glenn Kasten68deb152011-12-19 15:06:39 -0800158 name -= TRACK0;
159 assert(uint32_t(name) < MAX_NUM_TRACKS);
160 track_t& track = mState.tracks[name];
161
162 if (track.enabled != 0) {
163 track.enabled = 0;
164 ALOGV("disable(%d)", name);
165 invalidateState(1 << name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167}
168
Glenn Kasten68deb152011-12-19 15:06:39 -0800169void AudioMixer::setParameter(int name, int target, int param, void *value)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170{
Glenn Kasten68deb152011-12-19 15:06:39 -0800171 name -= TRACK0;
172 assert(uint32_t(name) < MAX_NUM_TRACKS);
173 track_t& track = mState.tracks[name];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174
Eric Laurent65b65452010-06-01 23:49:17 -0700175 int valueInt = (int)value;
176 int32_t *valueBuf = (int32_t *)value;
177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 switch (target) {
Glenn Kastenbde164a2011-05-05 08:19:00 -0700179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 case TRACK:
Glenn Kasten68deb152011-12-19 15:06:39 -0800181 switch (param) {
Glenn Kastenbde164a2011-05-05 08:19:00 -0700182 case CHANNEL_MASK: {
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700183 uint32_t mask = (uint32_t)value;
Glenn Kasten68deb152011-12-19 15:06:39 -0800184 if (track.channelMask != mask) {
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700185 uint8_t channelCount = popcount(mask);
Glenn Kastenbde164a2011-05-05 08:19:00 -0700186 assert((channelCount <= MAX_NUM_CHANNELS) && (channelCount));
Glenn Kasten68deb152011-12-19 15:06:39 -0800187 track.channelMask = mask;
188 track.channelCount = channelCount;
Glenn Kastenbde164a2011-05-05 08:19:00 -0700189 ALOGV("setParameter(TRACK, CHANNEL_MASK, %x)", mask);
Glenn Kasten68deb152011-12-19 15:06:39 -0800190 invalidateState(1 << name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 }
Glenn Kastenbde164a2011-05-05 08:19:00 -0700192 } break;
193 case MAIN_BUFFER:
Glenn Kasten68deb152011-12-19 15:06:39 -0800194 if (track.mainBuffer != valueBuf) {
195 track.mainBuffer = valueBuf;
Steve Block71f2cf12011-10-20 11:56:00 +0100196 ALOGV("setParameter(TRACK, MAIN_BUFFER, %p)", valueBuf);
Glenn Kasten68deb152011-12-19 15:06:39 -0800197 invalidateState(1 << name);
Eric Laurent65b65452010-06-01 23:49:17 -0700198 }
Glenn Kastenbde164a2011-05-05 08:19:00 -0700199 break;
200 case AUX_BUFFER:
Glenn Kasten68deb152011-12-19 15:06:39 -0800201 if (track.auxBuffer != valueBuf) {
202 track.auxBuffer = valueBuf;
Steve Block71f2cf12011-10-20 11:56:00 +0100203 ALOGV("setParameter(TRACK, AUX_BUFFER, %p)", valueBuf);
Glenn Kasten68deb152011-12-19 15:06:39 -0800204 invalidateState(1 << name);
Eric Laurent65b65452010-06-01 23:49:17 -0700205 }
Glenn Kastenbde164a2011-05-05 08:19:00 -0700206 break;
207 default:
Glenn Kasten68deb152011-12-19 15:06:39 -0800208 // bad param
Glenn Kastenbde164a2011-05-05 08:19:00 -0700209 assert(false);
Eric Laurent65b65452010-06-01 23:49:17 -0700210 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 break;
Glenn Kastenbde164a2011-05-05 08:19:00 -0700212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 case RESAMPLE:
Glenn Kasten68deb152011-12-19 15:06:39 -0800214 switch (param) {
215 case SAMPLE_RATE:
Glenn Kastenbde164a2011-05-05 08:19:00 -0700216 assert(valueInt > 0);
Glenn Kastenbde164a2011-05-05 08:19:00 -0700217 if (track.setResampler(uint32_t(valueInt), mSampleRate)) {
218 ALOGV("setParameter(RESAMPLE, SAMPLE_RATE, %u)",
219 uint32_t(valueInt));
Glenn Kasten68deb152011-12-19 15:06:39 -0800220 invalidateState(1 << name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 }
Glenn Kasten68deb152011-12-19 15:06:39 -0800222 break;
223 case RESET:
Eric Laurent4bb21c42011-02-28 16:52:51 -0800224 track.resetResampler();
Glenn Kasten68deb152011-12-19 15:06:39 -0800225 invalidateState(1 << name);
226 break;
Glenn Kastenbde164a2011-05-05 08:19:00 -0700227 default:
Glenn Kasten68deb152011-12-19 15:06:39 -0800228 // bad param
Glenn Kastenbde164a2011-05-05 08:19:00 -0700229 assert(false);
Eric Laurent4bb21c42011-02-28 16:52:51 -0800230 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 break;
Glenn Kastenbde164a2011-05-05 08:19:00 -0700232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 case RAMP_VOLUME:
234 case VOLUME:
Glenn Kasten68deb152011-12-19 15:06:39 -0800235 switch (param) {
Glenn Kastenbde164a2011-05-05 08:19:00 -0700236 case VOLUME0:
Glenn Kasten68deb152011-12-19 15:06:39 -0800237 case VOLUME1:
238 if (track.volume[param-VOLUME0] != valueInt) {
Steve Block71f2cf12011-10-20 11:56:00 +0100239 ALOGV("setParameter(VOLUME, VOLUME0/1: %04x)", valueInt);
Glenn Kasten68deb152011-12-19 15:06:39 -0800240 track.prevVolume[param-VOLUME0] = track.volume[param-VOLUME0] << 16;
241 track.volume[param-VOLUME0] = valueInt;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 if (target == VOLUME) {
Glenn Kasten68deb152011-12-19 15:06:39 -0800243 track.prevVolume[param-VOLUME0] = valueInt << 16;
244 track.volumeInc[param-VOLUME0] = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 } else {
Glenn Kasten68deb152011-12-19 15:06:39 -0800246 int32_t d = (valueInt<<16) - track.prevVolume[param-VOLUME0];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 int32_t volInc = d / int32_t(mState.frameCount);
Glenn Kasten68deb152011-12-19 15:06:39 -0800248 track.volumeInc[param-VOLUME0] = volInc;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 if (volInc == 0) {
Glenn Kasten68deb152011-12-19 15:06:39 -0800250 track.prevVolume[param-VOLUME0] = valueInt << 16;
Eric Laurent65b65452010-06-01 23:49:17 -0700251 }
252 }
Glenn Kasten68deb152011-12-19 15:06:39 -0800253 invalidateState(1 << name);
Eric Laurent65b65452010-06-01 23:49:17 -0700254 }
Glenn Kasten68deb152011-12-19 15:06:39 -0800255 break;
256 case AUXLEVEL:
Eric Laurent65b65452010-06-01 23:49:17 -0700257 if (track.auxLevel != valueInt) {
Steve Block71f2cf12011-10-20 11:56:00 +0100258 ALOGV("setParameter(VOLUME, AUXLEVEL: %04x)", valueInt);
Eric Laurent65b65452010-06-01 23:49:17 -0700259 track.prevAuxLevel = track.auxLevel << 16;
260 track.auxLevel = valueInt;
261 if (target == VOLUME) {
262 track.prevAuxLevel = valueInt << 16;
263 track.auxInc = 0;
264 } else {
265 int32_t d = (valueInt<<16) - track.prevAuxLevel;
266 int32_t volInc = d / int32_t(mState.frameCount);
267 track.auxInc = volInc;
268 if (volInc == 0) {
269 track.prevAuxLevel = valueInt << 16;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 }
271 }
Glenn Kasten68deb152011-12-19 15:06:39 -0800272 invalidateState(1 << name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 }
Glenn Kasten68deb152011-12-19 15:06:39 -0800274 break;
Glenn Kastenbde164a2011-05-05 08:19:00 -0700275 default:
Glenn Kasten68deb152011-12-19 15:06:39 -0800276 // bad param
Glenn Kastenbde164a2011-05-05 08:19:00 -0700277 assert(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 }
279 break;
Glenn Kastenbde164a2011-05-05 08:19:00 -0700280
281 default:
282 // bad target
283 assert(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285}
286
287bool AudioMixer::track_t::setResampler(uint32_t value, uint32_t devSampleRate)
288{
289 if (value!=devSampleRate || resampler) {
290 if (sampleRate != value) {
291 sampleRate = value;
Glenn Kastenc434c902011-12-13 11:53:26 -0800292 if (resampler == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 resampler = AudioResampler::create(
294 format, channelCount, devSampleRate);
295 }
296 return true;
297 }
298 }
299 return false;
300}
301
302bool AudioMixer::track_t::doesResample() const
303{
Glenn Kastenc434c902011-12-13 11:53:26 -0800304 return resampler != NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305}
306
Eric Laurent4bb21c42011-02-28 16:52:51 -0800307void AudioMixer::track_t::resetResampler()
308{
Glenn Kastenc434c902011-12-13 11:53:26 -0800309 if (resampler != NULL) {
Eric Laurent4bb21c42011-02-28 16:52:51 -0800310 resampler->reset();
311 }
312}
313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314inline
Eric Laurent65b65452010-06-01 23:49:17 -0700315void AudioMixer::track_t::adjustVolumeRamp(bool aux)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316{
Glenn Kasten1f6f05d2011-12-13 11:52:35 -0800317 for (int i=0 ; i<MAX_NUM_CHANNELS ; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 if (((volumeInc[i]>0) && (((prevVolume[i]+volumeInc[i])>>16) >= volume[i])) ||
319 ((volumeInc[i]<0) && (((prevVolume[i]+volumeInc[i])>>16) <= volume[i]))) {
320 volumeInc[i] = 0;
321 prevVolume[i] = volume[i]<<16;
322 }
323 }
Eric Laurent65b65452010-06-01 23:49:17 -0700324 if (aux) {
325 if (((auxInc>0) && (((prevAuxLevel+auxInc)>>16) >= auxLevel)) ||
326 ((auxInc<0) && (((prevAuxLevel+auxInc)>>16) <= auxLevel))) {
327 auxInc = 0;
328 prevAuxLevel = auxLevel<<16;
329 }
330 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331}
332
Eric Laurent72dafb22011-12-22 16:08:41 -0800333size_t AudioMixer::track_t::getUnreleasedFrames()
334{
335 if (resampler != NULL) {
336 return resampler->getUnreleasedFrames();
337 }
338 return 0;
339}
340
341size_t AudioMixer::getUnreleasedFrames(int name)
342{
343 name -= TRACK0;
344 if (uint32_t(name) < MAX_NUM_TRACKS) {
345 track_t& track(mState.tracks[name]);
346 return track.getUnreleasedFrames();
347 }
348 return 0;
349}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350
Glenn Kasten68deb152011-12-19 15:06:39 -0800351void AudioMixer::setBufferProvider(int name, AudioBufferProvider* buffer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352{
Glenn Kasten68deb152011-12-19 15:06:39 -0800353 name -= TRACK0;
354 assert(uint32_t(name) < MAX_NUM_TRACKS);
355 mState.tracks[name].bufferProvider = buffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356}
357
358
359
Eric Laurent65b65452010-06-01 23:49:17 -0700360void AudioMixer::process()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361{
Eric Laurent65b65452010-06-01 23:49:17 -0700362 mState.hook(&mState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363}
364
365
Eric Laurent65b65452010-06-01 23:49:17 -0700366void AudioMixer::process__validate(state_t* state)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367{
368 LOGW_IF(!state->needsChanged,
369 "in process__validate() but nothing's invalid");
370
371 uint32_t changed = state->needsChanged;
372 state->needsChanged = 0; // clear the validation flag
373
374 // recompute which tracks are enabled / disabled
375 uint32_t enabled = 0;
376 uint32_t disabled = 0;
377 while (changed) {
378 const int i = 31 - __builtin_clz(changed);
379 const uint32_t mask = 1<<i;
380 changed &= ~mask;
381 track_t& t = state->tracks[i];
382 (t.enabled ? enabled : disabled) |= mask;
383 }
384 state->enabledTracks &= ~disabled;
385 state->enabledTracks |= enabled;
386
387 // compute everything we need...
388 int countActiveTracks = 0;
389 int all16BitsStereoNoResample = 1;
390 int resampling = 0;
391 int volumeRamp = 0;
392 uint32_t en = state->enabledTracks;
393 while (en) {
394 const int i = 31 - __builtin_clz(en);
395 en &= ~(1<<i);
396
397 countActiveTracks++;
398 track_t& t = state->tracks[i];
399 uint32_t n = 0;
400 n |= NEEDS_CHANNEL_1 + t.channelCount - 1;
401 n |= NEEDS_FORMAT_16;
402 n |= t.doesResample() ? NEEDS_RESAMPLE_ENABLED : NEEDS_RESAMPLE_DISABLED;
Eric Laurent65b65452010-06-01 23:49:17 -0700403 if (t.auxLevel != 0 && t.auxBuffer != NULL) {
404 n |= NEEDS_AUX_ENABLED;
405 }
406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 if (t.volumeInc[0]|t.volumeInc[1]) {
408 volumeRamp = 1;
409 } else if (!t.doesResample() && t.volumeRL == 0) {
410 n |= NEEDS_MUTE_ENABLED;
411 }
412 t.needs = n;
413
414 if ((n & NEEDS_MUTE__MASK) == NEEDS_MUTE_ENABLED) {
415 t.hook = track__nop;
416 } else {
Eric Laurent65b65452010-06-01 23:49:17 -0700417 if ((n & NEEDS_AUX__MASK) == NEEDS_AUX_ENABLED) {
418 all16BitsStereoNoResample = 0;
419 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 if ((n & NEEDS_RESAMPLE__MASK) == NEEDS_RESAMPLE_ENABLED) {
421 all16BitsStereoNoResample = 0;
422 resampling = 1;
423 t.hook = track__genericResample;
424 } else {
425 if ((n & NEEDS_CHANNEL_COUNT__MASK) == NEEDS_CHANNEL_1){
426 t.hook = track__16BitsMono;
427 all16BitsStereoNoResample = 0;
428 }
429 if ((n & NEEDS_CHANNEL_COUNT__MASK) == NEEDS_CHANNEL_2){
430 t.hook = track__16BitsStereo;
431 }
432 }
433 }
434 }
435
436 // select the processing hooks
437 state->hook = process__nop;
438 if (countActiveTracks) {
439 if (resampling) {
440 if (!state->outputTemp) {
441 state->outputTemp = new int32_t[MAX_NUM_CHANNELS * state->frameCount];
442 }
443 if (!state->resampleTemp) {
444 state->resampleTemp = new int32_t[MAX_NUM_CHANNELS * state->frameCount];
445 }
446 state->hook = process__genericResampling;
447 } else {
448 if (state->outputTemp) {
449 delete [] state->outputTemp;
Glenn Kastenc434c902011-12-13 11:53:26 -0800450 state->outputTemp = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 }
452 if (state->resampleTemp) {
453 delete [] state->resampleTemp;
Glenn Kastenc434c902011-12-13 11:53:26 -0800454 state->resampleTemp = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 }
456 state->hook = process__genericNoResampling;
457 if (all16BitsStereoNoResample && !volumeRamp) {
458 if (countActiveTracks == 1) {
459 state->hook = process__OneTrack16BitsStereoNoResampling;
460 }
461 }
462 }
463 }
464
Steve Block71f2cf12011-10-20 11:56:00 +0100465 ALOGV("mixer configuration change: %d activeTracks (%08x) "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 "all16BitsStereoNoResample=%d, resampling=%d, volumeRamp=%d",
467 countActiveTracks, state->enabledTracks,
468 all16BitsStereoNoResample, resampling, volumeRamp);
469
Glenn Kastenfb2ab9e2011-12-12 09:05:55 -0800470 state->hook(state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471
Glenn Kastenfb2ab9e2011-12-12 09:05:55 -0800472 // Now that the volume ramp has been done, set optimal state and
473 // track hooks for subsequent mixer process
474 if (countActiveTracks) {
475 int allMuted = 1;
476 uint32_t en = state->enabledTracks;
477 while (en) {
478 const int i = 31 - __builtin_clz(en);
479 en &= ~(1<<i);
480 track_t& t = state->tracks[i];
481 if (!t.doesResample() && t.volumeRL == 0)
482 {
483 t.needs |= NEEDS_MUTE_ENABLED;
484 t.hook = track__nop;
485 } else {
486 allMuted = 0;
487 }
488 }
489 if (allMuted) {
490 state->hook = process__nop;
491 } else if (all16BitsStereoNoResample) {
492 if (countActiveTracks == 1) {
493 state->hook = process__OneTrack16BitsStereoNoResampling;
494 }
495 }
496 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497}
498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499
Eric Laurent65b65452010-06-01 23:49:17 -0700500void 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 -0800501{
502 t->resampler->setSampleRate(t->sampleRate);
503
504 // ramp gain - resample to temp buffer and scale/mix in 2nd step
Eric Laurent65b65452010-06-01 23:49:17 -0700505 if (aux != NULL) {
506 // always resample with unity gain when sending to auxiliary buffer to be able
507 // to apply send level after resampling
508 // TODO: modify each resampler to support aux channel?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 t->resampler->setVolume(UNITY_GAIN, UNITY_GAIN);
510 memset(temp, 0, outFrameCount * MAX_NUM_CHANNELS * sizeof(int32_t));
511 t->resampler->resample(temp, outFrameCount, t->bufferProvider);
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800512 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) {
Eric Laurent65b65452010-06-01 23:49:17 -0700513 volumeRampStereo(t, out, outFrameCount, temp, aux);
514 } else {
515 volumeStereo(t, out, outFrameCount, temp, aux);
516 }
517 } else {
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800518 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) {
Eric Laurent65b65452010-06-01 23:49:17 -0700519 t->resampler->setVolume(UNITY_GAIN, UNITY_GAIN);
520 memset(temp, 0, outFrameCount * MAX_NUM_CHANNELS * sizeof(int32_t));
521 t->resampler->resample(temp, outFrameCount, t->bufferProvider);
522 volumeRampStereo(t, out, outFrameCount, temp, aux);
523 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524
Eric Laurent65b65452010-06-01 23:49:17 -0700525 // constant gain
526 else {
527 t->resampler->setVolume(t->volume[0], t->volume[1]);
528 t->resampler->resample(out, outFrameCount, t->bufferProvider);
529 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 }
531}
532
Eric Laurent65b65452010-06-01 23:49:17 -0700533void 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 -0800534{
535}
536
Eric Laurent65b65452010-06-01 23:49:17 -0700537void 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 -0800538{
539 int32_t vl = t->prevVolume[0];
540 int32_t vr = t->prevVolume[1];
541 const int32_t vlInc = t->volumeInc[0];
542 const int32_t vrInc = t->volumeInc[1];
543
Steve Block5baa3a62011-12-20 16:23:08 +0000544 //ALOGD("[0] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
546 // (vl + vlInc*frameCount)/65536.0f, frameCount);
Eric Laurent65b65452010-06-01 23:49:17 -0700547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 // ramp volume
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800549 if (CC_UNLIKELY(aux != NULL)) {
Eric Laurent65b65452010-06-01 23:49:17 -0700550 int32_t va = t->prevAuxLevel;
551 const int32_t vaInc = t->auxInc;
552 int32_t l;
553 int32_t r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554
555 do {
Eric Laurent65b65452010-06-01 23:49:17 -0700556 l = (*temp++ >> 12);
557 r = (*temp++ >> 12);
558 *out++ += (vl >> 16) * l;
559 *out++ += (vr >> 16) * r;
560 *aux++ += (va >> 17) * (l + r);
561 vl += vlInc;
562 vr += vrInc;
563 va += vaInc;
564 } while (--frameCount);
565 t->prevAuxLevel = va;
566 } else {
567 do {
568 *out++ += (vl >> 16) * (*temp++ >> 12);
569 *out++ += (vr >> 16) * (*temp++ >> 12);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 vl += vlInc;
571 vr += vrInc;
572 } while (--frameCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 }
Eric Laurent65b65452010-06-01 23:49:17 -0700574 t->prevVolume[0] = vl;
575 t->prevVolume[1] = vr;
576 t->adjustVolumeRamp((aux != NULL));
577}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578
Eric Laurent65b65452010-06-01 23:49:17 -0700579void AudioMixer::volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux)
580{
581 const int16_t vl = t->volume[0];
582 const int16_t vr = t->volume[1];
583
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800584 if (CC_UNLIKELY(aux != NULL)) {
Eric Laurent65b65452010-06-01 23:49:17 -0700585 const int16_t va = (int16_t)t->auxLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 do {
Eric Laurent65b65452010-06-01 23:49:17 -0700587 int16_t l = (int16_t)(*temp++ >> 12);
588 int16_t r = (int16_t)(*temp++ >> 12);
589 out[0] = mulAdd(l, vl, out[0]);
590 int16_t a = (int16_t)(((int32_t)l + r) >> 1);
591 out[1] = mulAdd(r, vr, out[1]);
592 out += 2;
593 aux[0] = mulAdd(a, va, aux[0]);
594 aux++;
595 } while (--frameCount);
596 } else {
597 do {
598 int16_t l = (int16_t)(*temp++ >> 12);
599 int16_t r = (int16_t)(*temp++ >> 12);
600 out[0] = mulAdd(l, vl, out[0]);
601 out[1] = mulAdd(r, vr, out[1]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 out += 2;
603 } while (--frameCount);
604 }
Eric Laurent65b65452010-06-01 23:49:17 -0700605}
606
607void AudioMixer::track__16BitsStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux)
608{
609 int16_t const *in = static_cast<int16_t const *>(t->in);
610
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800611 if (CC_UNLIKELY(aux != NULL)) {
Eric Laurent65b65452010-06-01 23:49:17 -0700612 int32_t l;
613 int32_t r;
614 // ramp gain
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800615 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) {
Eric Laurent65b65452010-06-01 23:49:17 -0700616 int32_t vl = t->prevVolume[0];
617 int32_t vr = t->prevVolume[1];
618 int32_t va = t->prevAuxLevel;
619 const int32_t vlInc = t->volumeInc[0];
620 const int32_t vrInc = t->volumeInc[1];
621 const int32_t vaInc = t->auxInc;
Steve Block5baa3a62011-12-20 16:23:08 +0000622 // ALOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
Eric Laurent65b65452010-06-01 23:49:17 -0700623 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
624 // (vl + vlInc*frameCount)/65536.0f, frameCount);
625
626 do {
627 l = (int32_t)*in++;
628 r = (int32_t)*in++;
629 *out++ += (vl >> 16) * l;
630 *out++ += (vr >> 16) * r;
631 *aux++ += (va >> 17) * (l + r);
632 vl += vlInc;
633 vr += vrInc;
634 va += vaInc;
635 } while (--frameCount);
636
637 t->prevVolume[0] = vl;
638 t->prevVolume[1] = vr;
639 t->prevAuxLevel = va;
640 t->adjustVolumeRamp(true);
641 }
642
643 // constant gain
644 else {
645 const uint32_t vrl = t->volumeRL;
646 const int16_t va = (int16_t)t->auxLevel;
647 do {
648 uint32_t rl = *reinterpret_cast<uint32_t const *>(in);
649 int16_t a = (int16_t)(((int32_t)in[0] + in[1]) >> 1);
650 in += 2;
651 out[0] = mulAddRL(1, rl, vrl, out[0]);
652 out[1] = mulAddRL(0, rl, vrl, out[1]);
653 out += 2;
654 aux[0] = mulAdd(a, va, aux[0]);
655 aux++;
656 } while (--frameCount);
657 }
658 } else {
659 // ramp gain
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800660 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) {
Eric Laurent65b65452010-06-01 23:49:17 -0700661 int32_t vl = t->prevVolume[0];
662 int32_t vr = t->prevVolume[1];
663 const int32_t vlInc = t->volumeInc[0];
664 const int32_t vrInc = t->volumeInc[1];
665
Steve Block5baa3a62011-12-20 16:23:08 +0000666 // ALOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
Eric Laurent65b65452010-06-01 23:49:17 -0700667 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
668 // (vl + vlInc*frameCount)/65536.0f, frameCount);
669
670 do {
671 *out++ += (vl >> 16) * (int32_t) *in++;
672 *out++ += (vr >> 16) * (int32_t) *in++;
673 vl += vlInc;
674 vr += vrInc;
675 } while (--frameCount);
676
677 t->prevVolume[0] = vl;
678 t->prevVolume[1] = vr;
679 t->adjustVolumeRamp(false);
680 }
681
682 // constant gain
683 else {
684 const uint32_t vrl = t->volumeRL;
685 do {
686 uint32_t rl = *reinterpret_cast<uint32_t const *>(in);
687 in += 2;
688 out[0] = mulAddRL(1, rl, vrl, out[0]);
689 out[1] = mulAddRL(0, rl, vrl, out[1]);
690 out += 2;
691 } while (--frameCount);
692 }
693 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 t->in = in;
695}
696
Eric Laurent65b65452010-06-01 23:49:17 -0700697void 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 -0800698{
699 int16_t const *in = static_cast<int16_t const *>(t->in);
700
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800701 if (CC_UNLIKELY(aux != NULL)) {
Eric Laurent65b65452010-06-01 23:49:17 -0700702 // ramp gain
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800703 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) {
Eric Laurent65b65452010-06-01 23:49:17 -0700704 int32_t vl = t->prevVolume[0];
705 int32_t vr = t->prevVolume[1];
706 int32_t va = t->prevAuxLevel;
707 const int32_t vlInc = t->volumeInc[0];
708 const int32_t vrInc = t->volumeInc[1];
709 const int32_t vaInc = t->auxInc;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710
Steve Block5baa3a62011-12-20 16:23:08 +0000711 // ALOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
Eric Laurent65b65452010-06-01 23:49:17 -0700712 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
713 // (vl + vlInc*frameCount)/65536.0f, frameCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714
Eric Laurent65b65452010-06-01 23:49:17 -0700715 do {
716 int32_t l = *in++;
717 *out++ += (vl >> 16) * l;
718 *out++ += (vr >> 16) * l;
719 *aux++ += (va >> 16) * l;
720 vl += vlInc;
721 vr += vrInc;
722 va += vaInc;
723 } while (--frameCount);
724
725 t->prevVolume[0] = vl;
726 t->prevVolume[1] = vr;
727 t->prevAuxLevel = va;
728 t->adjustVolumeRamp(true);
729 }
730 // constant gain
731 else {
732 const int16_t vl = t->volume[0];
733 const int16_t vr = t->volume[1];
734 const int16_t va = (int16_t)t->auxLevel;
735 do {
736 int16_t l = *in++;
737 out[0] = mulAdd(l, vl, out[0]);
738 out[1] = mulAdd(l, vr, out[1]);
739 out += 2;
740 aux[0] = mulAdd(l, va, aux[0]);
741 aux++;
742 } while (--frameCount);
743 }
744 } else {
745 // ramp gain
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800746 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) {
Eric Laurent65b65452010-06-01 23:49:17 -0700747 int32_t vl = t->prevVolume[0];
748 int32_t vr = t->prevVolume[1];
749 const int32_t vlInc = t->volumeInc[0];
750 const int32_t vrInc = t->volumeInc[1];
751
Steve Block5baa3a62011-12-20 16:23:08 +0000752 // ALOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
Eric Laurent65b65452010-06-01 23:49:17 -0700753 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
754 // (vl + vlInc*frameCount)/65536.0f, frameCount);
755
756 do {
757 int32_t l = *in++;
758 *out++ += (vl >> 16) * l;
759 *out++ += (vr >> 16) * l;
760 vl += vlInc;
761 vr += vrInc;
762 } while (--frameCount);
763
764 t->prevVolume[0] = vl;
765 t->prevVolume[1] = vr;
766 t->adjustVolumeRamp(false);
767 }
768 // constant gain
769 else {
770 const int16_t vl = t->volume[0];
771 const int16_t vr = t->volume[1];
772 do {
773 int16_t l = *in++;
774 out[0] = mulAdd(l, vl, out[0]);
775 out[1] = mulAdd(l, vr, out[1]);
776 out += 2;
777 } while (--frameCount);
778 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779 }
780 t->in = in;
781}
782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783// no-op case
Eric Laurent65b65452010-06-01 23:49:17 -0700784void AudioMixer::process__nop(state_t* state)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785{
Eric Laurent65b65452010-06-01 23:49:17 -0700786 uint32_t e0 = state->enabledTracks;
787 size_t bufSize = state->frameCount * sizeof(int16_t) * MAX_NUM_CHANNELS;
788 while (e0) {
789 // process by group of tracks with same output buffer to
790 // avoid multiple memset() on same buffer
791 uint32_t e1 = e0, e2 = e0;
792 int i = 31 - __builtin_clz(e1);
793 track_t& t1 = state->tracks[i];
794 e2 &= ~(1<<i);
795 while (e2) {
796 i = 31 - __builtin_clz(e2);
797 e2 &= ~(1<<i);
798 track_t& t2 = state->tracks[i];
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800799 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) {
Eric Laurent65b65452010-06-01 23:49:17 -0700800 e1 &= ~(1<<i);
801 }
802 }
803 e0 &= ~(e1);
804
805 memset(t1.mainBuffer, 0, bufSize);
806
807 while (e1) {
808 i = 31 - __builtin_clz(e1);
809 e1 &= ~(1<<i);
810 t1 = state->tracks[i];
811 size_t outFrames = state->frameCount;
812 while (outFrames) {
813 t1.buffer.frameCount = outFrames;
814 t1.bufferProvider->getNextBuffer(&t1.buffer);
815 if (!t1.buffer.raw) break;
816 outFrames -= t1.buffer.frameCount;
817 t1.bufferProvider->releaseBuffer(&t1.buffer);
818 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 }
820 }
821}
822
823// generic code without resampling
Eric Laurent65b65452010-06-01 23:49:17 -0700824void AudioMixer::process__genericNoResampling(state_t* state)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800825{
826 int32_t outTemp[BLOCKSIZE * MAX_NUM_CHANNELS] __attribute__((aligned(32)));
827
828 // acquire each track's buffer
829 uint32_t enabledTracks = state->enabledTracks;
Eric Laurent65b65452010-06-01 23:49:17 -0700830 uint32_t e0 = enabledTracks;
831 while (e0) {
832 const int i = 31 - __builtin_clz(e0);
833 e0 &= ~(1<<i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800834 track_t& t = state->tracks[i];
835 t.buffer.frameCount = state->frameCount;
836 t.bufferProvider->getNextBuffer(&t.buffer);
837 t.frameCount = t.buffer.frameCount;
838 t.in = t.buffer.raw;
839 // t.in == NULL can happen if the track was flushed just after having
840 // been enabled for mixing.
841 if (t.in == NULL)
842 enabledTracks &= ~(1<<i);
843 }
844
Eric Laurent65b65452010-06-01 23:49:17 -0700845 e0 = enabledTracks;
846 while (e0) {
847 // process by group of tracks with same output buffer to
848 // optimize cache use
849 uint32_t e1 = e0, e2 = e0;
850 int j = 31 - __builtin_clz(e1);
851 track_t& t1 = state->tracks[j];
852 e2 &= ~(1<<j);
853 while (e2) {
854 j = 31 - __builtin_clz(e2);
855 e2 &= ~(1<<j);
856 track_t& t2 = state->tracks[j];
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800857 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) {
Eric Laurent65b65452010-06-01 23:49:17 -0700858 e1 &= ~(1<<j);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 }
860 }
Eric Laurent65b65452010-06-01 23:49:17 -0700861 e0 &= ~(e1);
862 // this assumes output 16 bits stereo, no resampling
863 int32_t *out = t1.mainBuffer;
864 size_t numFrames = 0;
865 do {
866 memset(outTemp, 0, sizeof(outTemp));
867 e2 = e1;
868 while (e2) {
869 const int i = 31 - __builtin_clz(e2);
870 e2 &= ~(1<<i);
871 track_t& t = state->tracks[i];
872 size_t outFrames = BLOCKSIZE;
873 int32_t *aux = NULL;
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800874 if (CC_UNLIKELY((t.needs & NEEDS_AUX__MASK) == NEEDS_AUX_ENABLED)) {
Eric Laurent65b65452010-06-01 23:49:17 -0700875 aux = t.auxBuffer + numFrames;
876 }
877 while (outFrames) {
878 size_t inFrames = (t.frameCount > outFrames)?outFrames:t.frameCount;
879 if (inFrames) {
880 (t.hook)(&t, outTemp + (BLOCKSIZE-outFrames)*MAX_NUM_CHANNELS, inFrames, state->resampleTemp, aux);
881 t.frameCount -= inFrames;
882 outFrames -= inFrames;
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800883 if (CC_UNLIKELY(aux != NULL)) {
Eric Laurent65b65452010-06-01 23:49:17 -0700884 aux += inFrames;
885 }
886 }
887 if (t.frameCount == 0 && outFrames) {
888 t.bufferProvider->releaseBuffer(&t.buffer);
889 t.buffer.frameCount = (state->frameCount - numFrames) - (BLOCKSIZE - outFrames);
890 t.bufferProvider->getNextBuffer(&t.buffer);
891 t.in = t.buffer.raw;
892 if (t.in == NULL) {
893 enabledTracks &= ~(1<<i);
894 e1 &= ~(1<<i);
895 break;
896 }
897 t.frameCount = t.buffer.frameCount;
898 }
899 }
900 }
901 ditherAndClamp(out, outTemp, BLOCKSIZE);
902 out += BLOCKSIZE;
903 numFrames += BLOCKSIZE;
904 } while (numFrames < state->frameCount);
905 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906
907 // release each track's buffer
Eric Laurent65b65452010-06-01 23:49:17 -0700908 e0 = enabledTracks;
909 while (e0) {
910 const int i = 31 - __builtin_clz(e0);
911 e0 &= ~(1<<i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 track_t& t = state->tracks[i];
913 t.bufferProvider->releaseBuffer(&t.buffer);
914 }
915}
916
Eric Laurent65b65452010-06-01 23:49:17 -0700917
Glenn Kastenfb2ab9e2011-12-12 09:05:55 -0800918// generic code with resampling
Eric Laurent65b65452010-06-01 23:49:17 -0700919void AudioMixer::process__genericResampling(state_t* state)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920{
921 int32_t* const outTemp = state->outputTemp;
922 const size_t size = sizeof(int32_t) * MAX_NUM_CHANNELS * state->frameCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 size_t numFrames = state->frameCount;
925
Eric Laurent65b65452010-06-01 23:49:17 -0700926 uint32_t e0 = state->enabledTracks;
927 while (e0) {
928 // process by group of tracks with same output buffer
929 // to optimize cache use
930 uint32_t e1 = e0, e2 = e0;
931 int j = 31 - __builtin_clz(e1);
932 track_t& t1 = state->tracks[j];
933 e2 &= ~(1<<j);
934 while (e2) {
935 j = 31 - __builtin_clz(e2);
936 e2 &= ~(1<<j);
937 track_t& t2 = state->tracks[j];
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800938 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) {
Eric Laurent65b65452010-06-01 23:49:17 -0700939 e1 &= ~(1<<j);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 }
941 }
Eric Laurent65b65452010-06-01 23:49:17 -0700942 e0 &= ~(e1);
943 int32_t *out = t1.mainBuffer;
Yuuhi Yamaguchi681d8182011-02-04 15:24:34 +0100944 memset(outTemp, 0, size);
Eric Laurent65b65452010-06-01 23:49:17 -0700945 while (e1) {
946 const int i = 31 - __builtin_clz(e1);
947 e1 &= ~(1<<i);
948 track_t& t = state->tracks[i];
949 int32_t *aux = NULL;
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800950 if (CC_UNLIKELY((t.needs & NEEDS_AUX__MASK) == NEEDS_AUX_ENABLED)) {
Eric Laurent65b65452010-06-01 23:49:17 -0700951 aux = t.auxBuffer;
952 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953
Eric Laurent65b65452010-06-01 23:49:17 -0700954 // this is a little goofy, on the resampling case we don't
955 // acquire/release the buffers because it's done by
956 // the resampler.
957 if ((t.needs & NEEDS_RESAMPLE__MASK) == NEEDS_RESAMPLE_ENABLED) {
958 (t.hook)(&t, outTemp, numFrames, state->resampleTemp, aux);
959 } else {
960
961 size_t outFrames = 0;
962
963 while (outFrames < numFrames) {
964 t.buffer.frameCount = numFrames - outFrames;
965 t.bufferProvider->getNextBuffer(&t.buffer);
966 t.in = t.buffer.raw;
967 // t.in == NULL can happen if the track was flushed just after having
968 // been enabled for mixing.
969 if (t.in == NULL) break;
970
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800971 if (CC_UNLIKELY(aux != NULL)) {
Eric Laurent65b65452010-06-01 23:49:17 -0700972 aux += outFrames;
973 }
974 (t.hook)(&t, outTemp + outFrames*MAX_NUM_CHANNELS, t.buffer.frameCount, state->resampleTemp, aux);
975 outFrames += t.buffer.frameCount;
976 t.bufferProvider->releaseBuffer(&t.buffer);
977 }
978 }
979 }
980 ditherAndClamp(out, outTemp, numFrames);
981 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982}
983
984// one track, 16 bits stereo without resampling is the most common case
Eric Laurent65b65452010-06-01 23:49:17 -0700985void AudioMixer::process__OneTrack16BitsStereoNoResampling(state_t* state)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986{
987 const int i = 31 - __builtin_clz(state->enabledTracks);
988 const track_t& t = state->tracks[i];
989
990 AudioBufferProvider::Buffer& b(t.buffer);
Eric Laurent65b65452010-06-01 23:49:17 -0700991
992 int32_t* out = t.mainBuffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 size_t numFrames = state->frameCount;
Eric Laurent65b65452010-06-01 23:49:17 -0700994
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 const int16_t vl = t.volume[0];
996 const int16_t vr = t.volume[1];
997 const uint32_t vrl = t.volumeRL;
998 while (numFrames) {
999 b.frameCount = numFrames;
1000 t.bufferProvider->getNextBuffer(&b);
1001 int16_t const *in = b.i16;
1002
1003 // in == NULL can happen if the track was flushed just after having
1004 // been enabled for mixing.
The Android Open Source Project10592532009-03-18 17:39:46 -07001005 if (in == NULL || ((unsigned long)in & 3)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 memset(out, 0, numFrames*MAX_NUM_CHANNELS*sizeof(int16_t));
The Android Open Source Project10592532009-03-18 17:39:46 -07001007 LOGE_IF(((unsigned long)in & 3), "process stereo track: input buffer alignment pb: buffer %p track %d, channels %d, needs %08x",
1008 in, i, t.channelCount, t.needs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 return;
1010 }
1011 size_t outFrames = b.frameCount;
Eric Laurent65b65452010-06-01 23:49:17 -07001012
Glenn Kastene80a4cc2011-12-15 09:51:17 -08001013 if (CC_UNLIKELY(uint32_t(vl) > UNITY_GAIN || uint32_t(vr) > UNITY_GAIN)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 // volume is boosted, so we might need to clamp even though
1015 // we process only one track.
1016 do {
1017 uint32_t rl = *reinterpret_cast<uint32_t const *>(in);
1018 in += 2;
1019 int32_t l = mulRL(1, rl, vrl) >> 12;
1020 int32_t r = mulRL(0, rl, vrl) >> 12;
1021 // clamping...
1022 l = clamp16(l);
1023 r = clamp16(r);
1024 *out++ = (r<<16) | (l & 0xFFFF);
1025 } while (--outFrames);
1026 } else {
1027 do {
1028 uint32_t rl = *reinterpret_cast<uint32_t const *>(in);
1029 in += 2;
1030 int32_t l = mulRL(1, rl, vrl) >> 12;
1031 int32_t r = mulRL(0, rl, vrl) >> 12;
1032 *out++ = (r<<16) | (l & 0xFFFF);
1033 } while (--outFrames);
1034 }
1035 numFrames -= b.frameCount;
1036 t.bufferProvider->releaseBuffer(&b);
1037 }
1038}
1039
Glenn Kasten31ba2e52011-12-15 09:53:12 -08001040#if 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041// 2 tracks is also a common case
Eric Laurent65b65452010-06-01 23:49:17 -07001042// NEVER used in current implementation of process__validate()
1043// only use if the 2 tracks have the same output buffer
1044void AudioMixer::process__TwoTracks16BitsStereoNoResampling(state_t* state)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045{
1046 int i;
1047 uint32_t en = state->enabledTracks;
1048
1049 i = 31 - __builtin_clz(en);
1050 const track_t& t0 = state->tracks[i];
1051 AudioBufferProvider::Buffer& b0(t0.buffer);
1052
1053 en &= ~(1<<i);
1054 i = 31 - __builtin_clz(en);
1055 const track_t& t1 = state->tracks[i];
1056 AudioBufferProvider::Buffer& b1(t1.buffer);
Eric Laurent65b65452010-06-01 23:49:17 -07001057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 int16_t const *in0;
1059 const int16_t vl0 = t0.volume[0];
1060 const int16_t vr0 = t0.volume[1];
1061 size_t frameCount0 = 0;
Eric Laurent65b65452010-06-01 23:49:17 -07001062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 int16_t const *in1;
1064 const int16_t vl1 = t1.volume[0];
1065 const int16_t vr1 = t1.volume[1];
1066 size_t frameCount1 = 0;
Eric Laurent65b65452010-06-01 23:49:17 -07001067
1068 //FIXME: only works if two tracks use same buffer
1069 int32_t* out = t0.mainBuffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 size_t numFrames = state->frameCount;
1071 int16_t const *buff = NULL;
1072
Eric Laurent65b65452010-06-01 23:49:17 -07001073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 while (numFrames) {
Eric Laurent65b65452010-06-01 23:49:17 -07001075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 if (frameCount0 == 0) {
1077 b0.frameCount = numFrames;
1078 t0.bufferProvider->getNextBuffer(&b0);
1079 if (b0.i16 == NULL) {
1080 if (buff == NULL) {
1081 buff = new int16_t[MAX_NUM_CHANNELS * state->frameCount];
1082 }
1083 in0 = buff;
1084 b0.frameCount = numFrames;
1085 } else {
1086 in0 = b0.i16;
1087 }
1088 frameCount0 = b0.frameCount;
1089 }
1090 if (frameCount1 == 0) {
1091 b1.frameCount = numFrames;
1092 t1.bufferProvider->getNextBuffer(&b1);
1093 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 if (buff != NULL) {
Eric Laurent65b65452010-06-01 23:49:17 -07001135 delete [] buff;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 }
1137}
Glenn Kasten31ba2e52011-12-15 09:53:12 -08001138#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139
1140// ----------------------------------------------------------------------------
1141}; // namespace android