blob: dfca9b129a71458550003977c148c8765b0ddab4 [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>
31
32#include <system/audio.h>
33
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034#include "AudioMixer.h"
35
36namespace android {
37// ----------------------------------------------------------------------------
38
39static inline int16_t clamp16(int32_t sample)
40{
41 if ((sample>>15) ^ (sample>>31))
42 sample = 0x7FFF ^ (sample>>31);
43 return sample;
44}
45
46// ----------------------------------------------------------------------------
47
48AudioMixer::AudioMixer(size_t frameCount, uint32_t sampleRate)
49 : mActiveTrack(0), mTrackNames(0), mSampleRate(sampleRate)
50{
Glenn Kastenbde164ab2011-05-05 08:19:00 -070051 // AudioMixer is not yet capable of multi-channel beyond stereo
52 assert(2 == MAX_NUM_CHANNELS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 mState.enabledTracks= 0;
54 mState.needsChanged = 0;
55 mState.frameCount = frameCount;
Glenn Kastenc434c902011-12-13 11:53:26 -080056 mState.outputTemp = NULL;
57 mState.resampleTemp = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 mState.hook = process__nop;
59 track_t* t = mState.tracks;
60 for (int i=0 ; i<32 ; i++) {
61 t->needs = 0;
62 t->volume[0] = UNITY_GAIN;
63 t->volume[1] = UNITY_GAIN;
Glenn Kastena763b442011-12-13 11:58:23 -080064 // no initialization needed
65 // t->prevVolume[0]
66 // t->prevVolume[1]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 t->volumeInc[0] = 0;
68 t->volumeInc[1] = 0;
Eric Laurent65b65452010-06-01 23:49:17 -070069 t->auxLevel = 0;
70 t->auxInc = 0;
Glenn Kastena763b442011-12-13 11:58:23 -080071 // no initialization needed
72 // t->prevAuxLevel
73 // t->frameCount
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 t->channelCount = 2;
75 t->enabled = 0;
76 t->format = 16;
Jean-Michel Trivi54392232011-05-24 15:53:33 -070077 t->channelMask = AUDIO_CHANNEL_OUT_STEREO;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 t->buffer.raw = 0;
Glenn Kastenc434c902011-12-13 11:53:26 -080079 t->bufferProvider = NULL;
80 t->hook = NULL;
81 t->resampler = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 t->sampleRate = mSampleRate;
Glenn Kastenc434c902011-12-13 11:53:26 -080083 t->in = NULL;
Eric Laurent65b65452010-06-01 23:49:17 -070084 t->mainBuffer = NULL;
85 t->auxBuffer = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 t++;
87 }
88}
89
Glenn Kastenfb2ab9e2011-12-12 09:05:55 -080090AudioMixer::~AudioMixer()
91{
92 track_t* t = mState.tracks;
93 for (int i=0 ; i<32 ; i++) {
94 delete t->resampler;
95 t++;
96 }
97 delete [] mState.outputTemp;
98 delete [] mState.resampleTemp;
99}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100
Glenn Kastenfb2ab9e2011-12-12 09:05:55 -0800101int AudioMixer::getTrackName()
102{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 uint32_t names = mTrackNames;
104 uint32_t mask = 1;
105 int n = 0;
106 while (names & mask) {
107 mask <<= 1;
108 n++;
109 }
110 if (mask) {
Steve Block71f2cf12011-10-20 11:56:00 +0100111 ALOGV("add track (%d)", n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 mTrackNames |= mask;
113 return TRACK0 + n;
114 }
115 return -1;
Glenn Kastenfb2ab9e2011-12-12 09:05:55 -0800116}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117
Glenn Kastenfb2ab9e2011-12-12 09:05:55 -0800118void AudioMixer::invalidateState(uint32_t mask)
119{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 if (mask) {
121 mState.needsChanged |= mask;
122 mState.hook = process__validate;
123 }
124 }
125
Glenn Kastenfb2ab9e2011-12-12 09:05:55 -0800126void AudioMixer::deleteTrackName(int name)
127{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 name -= TRACK0;
129 if (uint32_t(name) < MAX_NUM_TRACKS) {
Steve Block71f2cf12011-10-20 11:56:00 +0100130 ALOGV("deleteTrackName(%d)", name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 track_t& track(mState.tracks[ name ]);
132 if (track.enabled != 0) {
133 track.enabled = 0;
134 invalidateState(1<<name);
135 }
136 if (track.resampler) {
137 // delete the resampler
138 delete track.resampler;
Glenn Kastenc434c902011-12-13 11:53:26 -0800139 track.resampler = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 track.sampleRate = mSampleRate;
141 invalidateState(1<<name);
142 }
143 track.volumeInc[0] = 0;
144 track.volumeInc[1] = 0;
145 mTrackNames &= ~(1<<name);
146 }
Glenn Kastenfb2ab9e2011-12-12 09:05:55 -0800147}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148
Glenn Kastenaf62dbc2011-12-15 14:54:01 -0800149void AudioMixer::enable()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150{
Glenn Kastenaf62dbc2011-12-15 14:54:01 -0800151 if (mState.tracks[ mActiveTrack ].enabled != 1) {
152 mState.tracks[ mActiveTrack ].enabled = 1;
153 ALOGV("enable(%d)", mActiveTrack);
154 invalidateState(1<<mActiveTrack);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156}
157
Glenn Kastenaf62dbc2011-12-15 14:54:01 -0800158void AudioMixer::disable()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159{
Glenn Kastenaf62dbc2011-12-15 14:54:01 -0800160 if (mState.tracks[ mActiveTrack ].enabled != 0) {
161 mState.tracks[ mActiveTrack ].enabled = 0;
162 ALOGV("disable(%d)", mActiveTrack);
163 invalidateState(1<<mActiveTrack);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165}
166
Glenn Kastenafb40b52011-12-15 15:46:46 -0800167void AudioMixer::setActiveTrack(int track)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168{
Glenn Kastenafb40b52011-12-15 15:46:46 -0800169 // this also catches track < TRACK0
170 track -= TRACK0;
171 assert(uint32_t(track) < MAX_NUM_TRACKS);
172 mActiveTrack = track;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173}
174
Glenn Kastenbde164ab2011-05-05 08:19:00 -0700175void AudioMixer::setParameter(int target, int name, void *value)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176{
Eric Laurent65b65452010-06-01 23:49:17 -0700177 int valueInt = (int)value;
178 int32_t *valueBuf = (int32_t *)value;
179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 switch (target) {
Glenn Kastenbde164ab2011-05-05 08:19:00 -0700181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 case TRACK:
Glenn Kastenbde164ab2011-05-05 08:19:00 -0700183 switch (name) {
184 case CHANNEL_MASK: {
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700185 uint32_t mask = (uint32_t)value;
186 if (mState.tracks[ mActiveTrack ].channelMask != mask) {
187 uint8_t channelCount = popcount(mask);
Glenn Kastenbde164ab2011-05-05 08:19:00 -0700188 assert((channelCount <= MAX_NUM_CHANNELS) && (channelCount));
189 mState.tracks[ mActiveTrack ].channelMask = mask;
190 mState.tracks[ mActiveTrack ].channelCount = channelCount;
191 ALOGV("setParameter(TRACK, CHANNEL_MASK, %x)", mask);
192 invalidateState(1<<mActiveTrack);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 }
Glenn Kastenbde164ab2011-05-05 08:19:00 -0700194 } break;
195 case MAIN_BUFFER:
Eric Laurent65b65452010-06-01 23:49:17 -0700196 if (mState.tracks[ mActiveTrack ].mainBuffer != valueBuf) {
197 mState.tracks[ mActiveTrack ].mainBuffer = valueBuf;
Steve Block71f2cf12011-10-20 11:56:00 +0100198 ALOGV("setParameter(TRACK, MAIN_BUFFER, %p)", valueBuf);
Eric Laurent65b65452010-06-01 23:49:17 -0700199 invalidateState(1<<mActiveTrack);
200 }
Glenn Kastenbde164ab2011-05-05 08:19:00 -0700201 break;
202 case AUX_BUFFER:
Eric Laurent65b65452010-06-01 23:49:17 -0700203 if (mState.tracks[ mActiveTrack ].auxBuffer != valueBuf) {
204 mState.tracks[ mActiveTrack ].auxBuffer = valueBuf;
Steve Block71f2cf12011-10-20 11:56:00 +0100205 ALOGV("setParameter(TRACK, AUX_BUFFER, %p)", valueBuf);
Eric Laurent65b65452010-06-01 23:49:17 -0700206 invalidateState(1<<mActiveTrack);
207 }
Glenn Kastenbde164ab2011-05-05 08:19:00 -0700208 break;
209 default:
210 // bad name
211 assert(false);
Eric Laurent65b65452010-06-01 23:49:17 -0700212 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 break;
Glenn Kastenbde164ab2011-05-05 08:19:00 -0700214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 case RESAMPLE:
Glenn Kastenbde164ab2011-05-05 08:19:00 -0700216 switch (name) {
217 case SAMPLE_RATE: {
218 assert(valueInt > 0);
219 track_t& track = mState.tracks[ mActiveTrack ];
220 if (track.setResampler(uint32_t(valueInt), mSampleRate)) {
221 ALOGV("setParameter(RESAMPLE, SAMPLE_RATE, %u)",
222 uint32_t(valueInt));
223 invalidateState(1<<mActiveTrack);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 }
Glenn Kastenbde164ab2011-05-05 08:19:00 -0700225 } break;
226 case RESET: {
Eric Laurent4bb21c42011-02-28 16:52:51 -0800227 track_t& track = mState.tracks[ mActiveTrack ];
228 track.resetResampler();
229 invalidateState(1<<mActiveTrack);
Glenn Kastenbde164ab2011-05-05 08:19:00 -0700230 } break;
231 default:
232 // bad name
233 assert(false);
Eric Laurent4bb21c42011-02-28 16:52:51 -0800234 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 break;
Glenn Kastenbde164ab2011-05-05 08:19:00 -0700236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 case RAMP_VOLUME:
238 case VOLUME:
Glenn Kastenbde164ab2011-05-05 08:19:00 -0700239 switch (name) {
240 case VOLUME0:
241 case VOLUME1: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 track_t& track = mState.tracks[ mActiveTrack ];
Eric Laurent65b65452010-06-01 23:49:17 -0700243 if (track.volume[name-VOLUME0] != valueInt) {
Steve Block71f2cf12011-10-20 11:56:00 +0100244 ALOGV("setParameter(VOLUME, VOLUME0/1: %04x)", valueInt);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 track.prevVolume[name-VOLUME0] = track.volume[name-VOLUME0] << 16;
Eric Laurent65b65452010-06-01 23:49:17 -0700246 track.volume[name-VOLUME0] = valueInt;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 if (target == VOLUME) {
Eric Laurent65b65452010-06-01 23:49:17 -0700248 track.prevVolume[name-VOLUME0] = valueInt << 16;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 track.volumeInc[name-VOLUME0] = 0;
250 } else {
Eric Laurent65b65452010-06-01 23:49:17 -0700251 int32_t d = (valueInt<<16) - track.prevVolume[name-VOLUME0];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 int32_t volInc = d / int32_t(mState.frameCount);
253 track.volumeInc[name-VOLUME0] = volInc;
254 if (volInc == 0) {
Eric Laurent65b65452010-06-01 23:49:17 -0700255 track.prevVolume[name-VOLUME0] = valueInt << 16;
256 }
257 }
258 invalidateState(1<<mActiveTrack);
259 }
Glenn Kastenbde164ab2011-05-05 08:19:00 -0700260 } break;
261 case AUXLEVEL: {
Eric Laurent65b65452010-06-01 23:49:17 -0700262 track_t& track = mState.tracks[ mActiveTrack ];
263 if (track.auxLevel != valueInt) {
Steve Block71f2cf12011-10-20 11:56:00 +0100264 ALOGV("setParameter(VOLUME, AUXLEVEL: %04x)", valueInt);
Eric Laurent65b65452010-06-01 23:49:17 -0700265 track.prevAuxLevel = track.auxLevel << 16;
266 track.auxLevel = valueInt;
267 if (target == VOLUME) {
268 track.prevAuxLevel = valueInt << 16;
269 track.auxInc = 0;
270 } else {
271 int32_t d = (valueInt<<16) - track.prevAuxLevel;
272 int32_t volInc = d / int32_t(mState.frameCount);
273 track.auxInc = volInc;
274 if (volInc == 0) {
275 track.prevAuxLevel = valueInt << 16;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 }
277 }
278 invalidateState(1<<mActiveTrack);
279 }
Glenn Kastenbde164ab2011-05-05 08:19:00 -0700280 } break;
281 default:
282 // bad name
283 assert(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 }
285 break;
Glenn Kastenbde164ab2011-05-05 08:19:00 -0700286
287 default:
288 // bad target
289 assert(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291}
292
293bool AudioMixer::track_t::setResampler(uint32_t value, uint32_t devSampleRate)
294{
295 if (value!=devSampleRate || resampler) {
296 if (sampleRate != value) {
297 sampleRate = value;
Glenn Kastenc434c902011-12-13 11:53:26 -0800298 if (resampler == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 resampler = AudioResampler::create(
300 format, channelCount, devSampleRate);
301 }
302 return true;
303 }
304 }
305 return false;
306}
307
308bool AudioMixer::track_t::doesResample() const
309{
Glenn Kastenc434c902011-12-13 11:53:26 -0800310 return resampler != NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311}
312
Eric Laurent4bb21c42011-02-28 16:52:51 -0800313void AudioMixer::track_t::resetResampler()
314{
Glenn Kastenc434c902011-12-13 11:53:26 -0800315 if (resampler != NULL) {
Eric Laurent4bb21c42011-02-28 16:52:51 -0800316 resampler->reset();
317 }
318}
319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320inline
Eric Laurent65b65452010-06-01 23:49:17 -0700321void AudioMixer::track_t::adjustVolumeRamp(bool aux)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322{
323 for (int i=0 ; i<2 ; i++) {
324 if (((volumeInc[i]>0) && (((prevVolume[i]+volumeInc[i])>>16) >= volume[i])) ||
325 ((volumeInc[i]<0) && (((prevVolume[i]+volumeInc[i])>>16) <= volume[i]))) {
326 volumeInc[i] = 0;
327 prevVolume[i] = volume[i]<<16;
328 }
329 }
Eric Laurent65b65452010-06-01 23:49:17 -0700330 if (aux) {
331 if (((auxInc>0) && (((prevAuxLevel+auxInc)>>16) >= auxLevel)) ||
332 ((auxInc<0) && (((prevAuxLevel+auxInc)>>16) <= auxLevel))) {
333 auxInc = 0;
334 prevAuxLevel = auxLevel<<16;
335 }
336 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337}
338
339
Glenn Kastenafb40b52011-12-15 15:46:46 -0800340void AudioMixer::setBufferProvider(AudioBufferProvider* buffer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341{
342 mState.tracks[ mActiveTrack ].bufferProvider = buffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343}
344
345
346
Eric Laurent65b65452010-06-01 23:49:17 -0700347void AudioMixer::process()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348{
Eric Laurent65b65452010-06-01 23:49:17 -0700349 mState.hook(&mState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350}
351
352
Eric Laurent65b65452010-06-01 23:49:17 -0700353void AudioMixer::process__validate(state_t* state)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354{
355 LOGW_IF(!state->needsChanged,
356 "in process__validate() but nothing's invalid");
357
358 uint32_t changed = state->needsChanged;
359 state->needsChanged = 0; // clear the validation flag
360
361 // recompute which tracks are enabled / disabled
362 uint32_t enabled = 0;
363 uint32_t disabled = 0;
364 while (changed) {
365 const int i = 31 - __builtin_clz(changed);
366 const uint32_t mask = 1<<i;
367 changed &= ~mask;
368 track_t& t = state->tracks[i];
369 (t.enabled ? enabled : disabled) |= mask;
370 }
371 state->enabledTracks &= ~disabled;
372 state->enabledTracks |= enabled;
373
374 // compute everything we need...
375 int countActiveTracks = 0;
376 int all16BitsStereoNoResample = 1;
377 int resampling = 0;
378 int volumeRamp = 0;
379 uint32_t en = state->enabledTracks;
380 while (en) {
381 const int i = 31 - __builtin_clz(en);
382 en &= ~(1<<i);
383
384 countActiveTracks++;
385 track_t& t = state->tracks[i];
386 uint32_t n = 0;
387 n |= NEEDS_CHANNEL_1 + t.channelCount - 1;
388 n |= NEEDS_FORMAT_16;
389 n |= t.doesResample() ? NEEDS_RESAMPLE_ENABLED : NEEDS_RESAMPLE_DISABLED;
Eric Laurent65b65452010-06-01 23:49:17 -0700390 if (t.auxLevel != 0 && t.auxBuffer != NULL) {
391 n |= NEEDS_AUX_ENABLED;
392 }
393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 if (t.volumeInc[0]|t.volumeInc[1]) {
395 volumeRamp = 1;
396 } else if (!t.doesResample() && t.volumeRL == 0) {
397 n |= NEEDS_MUTE_ENABLED;
398 }
399 t.needs = n;
400
401 if ((n & NEEDS_MUTE__MASK) == NEEDS_MUTE_ENABLED) {
402 t.hook = track__nop;
403 } else {
Eric Laurent65b65452010-06-01 23:49:17 -0700404 if ((n & NEEDS_AUX__MASK) == NEEDS_AUX_ENABLED) {
405 all16BitsStereoNoResample = 0;
406 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 if ((n & NEEDS_RESAMPLE__MASK) == NEEDS_RESAMPLE_ENABLED) {
408 all16BitsStereoNoResample = 0;
409 resampling = 1;
410 t.hook = track__genericResample;
411 } else {
412 if ((n & NEEDS_CHANNEL_COUNT__MASK) == NEEDS_CHANNEL_1){
413 t.hook = track__16BitsMono;
414 all16BitsStereoNoResample = 0;
415 }
416 if ((n & NEEDS_CHANNEL_COUNT__MASK) == NEEDS_CHANNEL_2){
417 t.hook = track__16BitsStereo;
418 }
419 }
420 }
421 }
422
423 // select the processing hooks
424 state->hook = process__nop;
425 if (countActiveTracks) {
426 if (resampling) {
427 if (!state->outputTemp) {
428 state->outputTemp = new int32_t[MAX_NUM_CHANNELS * state->frameCount];
429 }
430 if (!state->resampleTemp) {
431 state->resampleTemp = new int32_t[MAX_NUM_CHANNELS * state->frameCount];
432 }
433 state->hook = process__genericResampling;
434 } else {
435 if (state->outputTemp) {
436 delete [] state->outputTemp;
Glenn Kastenc434c902011-12-13 11:53:26 -0800437 state->outputTemp = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 }
439 if (state->resampleTemp) {
440 delete [] state->resampleTemp;
Glenn Kastenc434c902011-12-13 11:53:26 -0800441 state->resampleTemp = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 }
443 state->hook = process__genericNoResampling;
444 if (all16BitsStereoNoResample && !volumeRamp) {
445 if (countActiveTracks == 1) {
446 state->hook = process__OneTrack16BitsStereoNoResampling;
447 }
448 }
449 }
450 }
451
Steve Block71f2cf12011-10-20 11:56:00 +0100452 ALOGV("mixer configuration change: %d activeTracks (%08x) "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 "all16BitsStereoNoResample=%d, resampling=%d, volumeRamp=%d",
454 countActiveTracks, state->enabledTracks,
455 all16BitsStereoNoResample, resampling, volumeRamp);
456
Glenn Kastenfb2ab9e2011-12-12 09:05:55 -0800457 state->hook(state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458
Glenn Kastenfb2ab9e2011-12-12 09:05:55 -0800459 // Now that the volume ramp has been done, set optimal state and
460 // track hooks for subsequent mixer process
461 if (countActiveTracks) {
462 int allMuted = 1;
463 uint32_t en = state->enabledTracks;
464 while (en) {
465 const int i = 31 - __builtin_clz(en);
466 en &= ~(1<<i);
467 track_t& t = state->tracks[i];
468 if (!t.doesResample() && t.volumeRL == 0)
469 {
470 t.needs |= NEEDS_MUTE_ENABLED;
471 t.hook = track__nop;
472 } else {
473 allMuted = 0;
474 }
475 }
476 if (allMuted) {
477 state->hook = process__nop;
478 } else if (all16BitsStereoNoResample) {
479 if (countActiveTracks == 1) {
480 state->hook = process__OneTrack16BitsStereoNoResampling;
481 }
482 }
483 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484}
485
486static inline
487int32_t mulAdd(int16_t in, int16_t v, int32_t a)
488{
489#if defined(__arm__) && !defined(__thumb__)
490 int32_t out;
491 asm( "smlabb %[out], %[in], %[v], %[a] \n"
492 : [out]"=r"(out)
493 : [in]"%r"(in), [v]"r"(v), [a]"r"(a)
494 : );
495 return out;
496#else
497 return a + in * int32_t(v);
498#endif
499}
500
501static inline
502int32_t mul(int16_t in, int16_t v)
503{
504#if defined(__arm__) && !defined(__thumb__)
505 int32_t out;
506 asm( "smulbb %[out], %[in], %[v] \n"
507 : [out]"=r"(out)
508 : [in]"%r"(in), [v]"r"(v)
509 : );
510 return out;
511#else
512 return in * int32_t(v);
513#endif
514}
515
516static inline
517int32_t mulAddRL(int left, uint32_t inRL, uint32_t vRL, int32_t a)
518{
519#if defined(__arm__) && !defined(__thumb__)
520 int32_t out;
521 if (left) {
522 asm( "smlabb %[out], %[inRL], %[vRL], %[a] \n"
523 : [out]"=r"(out)
524 : [inRL]"%r"(inRL), [vRL]"r"(vRL), [a]"r"(a)
525 : );
526 } else {
527 asm( "smlatt %[out], %[inRL], %[vRL], %[a] \n"
528 : [out]"=r"(out)
529 : [inRL]"%r"(inRL), [vRL]"r"(vRL), [a]"r"(a)
530 : );
531 }
532 return out;
533#else
534 if (left) {
535 return a + int16_t(inRL&0xFFFF) * int16_t(vRL&0xFFFF);
536 } else {
537 return a + int16_t(inRL>>16) * int16_t(vRL>>16);
538 }
539#endif
540}
541
542static inline
543int32_t mulRL(int left, uint32_t inRL, uint32_t vRL)
544{
545#if defined(__arm__) && !defined(__thumb__)
546 int32_t out;
547 if (left) {
548 asm( "smulbb %[out], %[inRL], %[vRL] \n"
549 : [out]"=r"(out)
550 : [inRL]"%r"(inRL), [vRL]"r"(vRL)
551 : );
552 } else {
553 asm( "smultt %[out], %[inRL], %[vRL] \n"
554 : [out]"=r"(out)
555 : [inRL]"%r"(inRL), [vRL]"r"(vRL)
556 : );
557 }
558 return out;
559#else
560 if (left) {
561 return int16_t(inRL&0xFFFF) * int16_t(vRL&0xFFFF);
562 } else {
563 return int16_t(inRL>>16) * int16_t(vRL>>16);
564 }
565#endif
566}
567
568
Eric Laurent65b65452010-06-01 23:49:17 -0700569void 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 -0800570{
571 t->resampler->setSampleRate(t->sampleRate);
572
573 // ramp gain - resample to temp buffer and scale/mix in 2nd step
Eric Laurent65b65452010-06-01 23:49:17 -0700574 if (aux != NULL) {
575 // always resample with unity gain when sending to auxiliary buffer to be able
576 // to apply send level after resampling
577 // TODO: modify each resampler to support aux channel?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 t->resampler->setVolume(UNITY_GAIN, UNITY_GAIN);
579 memset(temp, 0, outFrameCount * MAX_NUM_CHANNELS * sizeof(int32_t));
580 t->resampler->resample(temp, outFrameCount, t->bufferProvider);
Eric Laurent65b65452010-06-01 23:49:17 -0700581 if UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc) {
582 volumeRampStereo(t, out, outFrameCount, temp, aux);
583 } else {
584 volumeStereo(t, out, outFrameCount, temp, aux);
585 }
586 } else {
587 if UNLIKELY(t->volumeInc[0]|t->volumeInc[1]) {
588 t->resampler->setVolume(UNITY_GAIN, UNITY_GAIN);
589 memset(temp, 0, outFrameCount * MAX_NUM_CHANNELS * sizeof(int32_t));
590 t->resampler->resample(temp, outFrameCount, t->bufferProvider);
591 volumeRampStereo(t, out, outFrameCount, temp, aux);
592 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593
Eric Laurent65b65452010-06-01 23:49:17 -0700594 // constant gain
595 else {
596 t->resampler->setVolume(t->volume[0], t->volume[1]);
597 t->resampler->resample(out, outFrameCount, t->bufferProvider);
598 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 }
600}
601
Eric Laurent65b65452010-06-01 23:49:17 -0700602void 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 -0800603{
604}
605
Eric Laurent65b65452010-06-01 23:49:17 -0700606void 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 -0800607{
608 int32_t vl = t->prevVolume[0];
609 int32_t vr = t->prevVolume[1];
610 const int32_t vlInc = t->volumeInc[0];
611 const int32_t vrInc = t->volumeInc[1];
612
613 //LOGD("[0] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
614 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
615 // (vl + vlInc*frameCount)/65536.0f, frameCount);
Eric Laurent65b65452010-06-01 23:49:17 -0700616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 // ramp volume
Eric Laurent65b65452010-06-01 23:49:17 -0700618 if UNLIKELY(aux != NULL) {
619 int32_t va = t->prevAuxLevel;
620 const int32_t vaInc = t->auxInc;
621 int32_t l;
622 int32_t r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623
624 do {
Eric Laurent65b65452010-06-01 23:49:17 -0700625 l = (*temp++ >> 12);
626 r = (*temp++ >> 12);
627 *out++ += (vl >> 16) * l;
628 *out++ += (vr >> 16) * r;
629 *aux++ += (va >> 17) * (l + r);
630 vl += vlInc;
631 vr += vrInc;
632 va += vaInc;
633 } while (--frameCount);
634 t->prevAuxLevel = va;
635 } else {
636 do {
637 *out++ += (vl >> 16) * (*temp++ >> 12);
638 *out++ += (vr >> 16) * (*temp++ >> 12);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 vl += vlInc;
640 vr += vrInc;
641 } while (--frameCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 }
Eric Laurent65b65452010-06-01 23:49:17 -0700643 t->prevVolume[0] = vl;
644 t->prevVolume[1] = vr;
645 t->adjustVolumeRamp((aux != NULL));
646}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647
Eric Laurent65b65452010-06-01 23:49:17 -0700648void AudioMixer::volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux)
649{
650 const int16_t vl = t->volume[0];
651 const int16_t vr = t->volume[1];
652
653 if UNLIKELY(aux != NULL) {
654 const int16_t va = (int16_t)t->auxLevel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 do {
Eric Laurent65b65452010-06-01 23:49:17 -0700656 int16_t l = (int16_t)(*temp++ >> 12);
657 int16_t r = (int16_t)(*temp++ >> 12);
658 out[0] = mulAdd(l, vl, out[0]);
659 int16_t a = (int16_t)(((int32_t)l + r) >> 1);
660 out[1] = mulAdd(r, vr, out[1]);
661 out += 2;
662 aux[0] = mulAdd(a, va, aux[0]);
663 aux++;
664 } while (--frameCount);
665 } else {
666 do {
667 int16_t l = (int16_t)(*temp++ >> 12);
668 int16_t r = (int16_t)(*temp++ >> 12);
669 out[0] = mulAdd(l, vl, out[0]);
670 out[1] = mulAdd(r, vr, out[1]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 out += 2;
672 } while (--frameCount);
673 }
Eric Laurent65b65452010-06-01 23:49:17 -0700674}
675
676void AudioMixer::track__16BitsStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux)
677{
678 int16_t const *in = static_cast<int16_t const *>(t->in);
679
680 if UNLIKELY(aux != NULL) {
681 int32_t l;
682 int32_t r;
683 // ramp gain
684 if UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc) {
685 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;
691 // LOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
692 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
693 // (vl + vlInc*frameCount)/65536.0f, frameCount);
694
695 do {
696 l = (int32_t)*in++;
697 r = (int32_t)*in++;
698 *out++ += (vl >> 16) * l;
699 *out++ += (vr >> 16) * r;
700 *aux++ += (va >> 17) * (l + r);
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
712 // constant gain
713 else {
714 const uint32_t vrl = t->volumeRL;
715 const int16_t va = (int16_t)t->auxLevel;
716 do {
717 uint32_t rl = *reinterpret_cast<uint32_t const *>(in);
718 int16_t a = (int16_t)(((int32_t)in[0] + in[1]) >> 1);
719 in += 2;
720 out[0] = mulAddRL(1, rl, vrl, out[0]);
721 out[1] = mulAddRL(0, rl, vrl, out[1]);
722 out += 2;
723 aux[0] = mulAdd(a, va, aux[0]);
724 aux++;
725 } while (--frameCount);
726 }
727 } else {
728 // ramp gain
729 if UNLIKELY(t->volumeInc[0]|t->volumeInc[1]) {
730 int32_t vl = t->prevVolume[0];
731 int32_t vr = t->prevVolume[1];
732 const int32_t vlInc = t->volumeInc[0];
733 const int32_t vrInc = t->volumeInc[1];
734
735 // LOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
736 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
737 // (vl + vlInc*frameCount)/65536.0f, frameCount);
738
739 do {
740 *out++ += (vl >> 16) * (int32_t) *in++;
741 *out++ += (vr >> 16) * (int32_t) *in++;
742 vl += vlInc;
743 vr += vrInc;
744 } while (--frameCount);
745
746 t->prevVolume[0] = vl;
747 t->prevVolume[1] = vr;
748 t->adjustVolumeRamp(false);
749 }
750
751 // constant gain
752 else {
753 const uint32_t vrl = t->volumeRL;
754 do {
755 uint32_t rl = *reinterpret_cast<uint32_t const *>(in);
756 in += 2;
757 out[0] = mulAddRL(1, rl, vrl, out[0]);
758 out[1] = mulAddRL(0, rl, vrl, out[1]);
759 out += 2;
760 } while (--frameCount);
761 }
762 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 t->in = in;
764}
765
Eric Laurent65b65452010-06-01 23:49:17 -0700766void 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 -0800767{
768 int16_t const *in = static_cast<int16_t const *>(t->in);
769
Eric Laurent65b65452010-06-01 23:49:17 -0700770 if UNLIKELY(aux != NULL) {
771 // ramp gain
772 if UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc) {
773 int32_t vl = t->prevVolume[0];
774 int32_t vr = t->prevVolume[1];
775 int32_t va = t->prevAuxLevel;
776 const int32_t vlInc = t->volumeInc[0];
777 const int32_t vrInc = t->volumeInc[1];
778 const int32_t vaInc = t->auxInc;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779
Eric Laurent65b65452010-06-01 23:49:17 -0700780 // LOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
781 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
782 // (vl + vlInc*frameCount)/65536.0f, frameCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783
Eric Laurent65b65452010-06-01 23:49:17 -0700784 do {
785 int32_t l = *in++;
786 *out++ += (vl >> 16) * l;
787 *out++ += (vr >> 16) * l;
788 *aux++ += (va >> 16) * l;
789 vl += vlInc;
790 vr += vrInc;
791 va += vaInc;
792 } while (--frameCount);
793
794 t->prevVolume[0] = vl;
795 t->prevVolume[1] = vr;
796 t->prevAuxLevel = va;
797 t->adjustVolumeRamp(true);
798 }
799 // constant gain
800 else {
801 const int16_t vl = t->volume[0];
802 const int16_t vr = t->volume[1];
803 const int16_t va = (int16_t)t->auxLevel;
804 do {
805 int16_t l = *in++;
806 out[0] = mulAdd(l, vl, out[0]);
807 out[1] = mulAdd(l, vr, out[1]);
808 out += 2;
809 aux[0] = mulAdd(l, va, aux[0]);
810 aux++;
811 } while (--frameCount);
812 }
813 } else {
814 // ramp gain
815 if UNLIKELY(t->volumeInc[0]|t->volumeInc[1]) {
816 int32_t vl = t->prevVolume[0];
817 int32_t vr = t->prevVolume[1];
818 const int32_t vlInc = t->volumeInc[0];
819 const int32_t vrInc = t->volumeInc[1];
820
821 // LOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
822 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
823 // (vl + vlInc*frameCount)/65536.0f, frameCount);
824
825 do {
826 int32_t l = *in++;
827 *out++ += (vl >> 16) * l;
828 *out++ += (vr >> 16) * l;
829 vl += vlInc;
830 vr += vrInc;
831 } while (--frameCount);
832
833 t->prevVolume[0] = vl;
834 t->prevVolume[1] = vr;
835 t->adjustVolumeRamp(false);
836 }
837 // constant gain
838 else {
839 const int16_t vl = t->volume[0];
840 const int16_t vr = t->volume[1];
841 do {
842 int16_t l = *in++;
843 out[0] = mulAdd(l, vl, out[0]);
844 out[1] = mulAdd(l, vr, out[1]);
845 out += 2;
846 } while (--frameCount);
847 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 }
849 t->in = in;
850}
851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852void AudioMixer::ditherAndClamp(int32_t* out, int32_t const *sums, size_t c)
853{
854 for (size_t i=0 ; i<c ; i++) {
855 int32_t l = *sums++;
856 int32_t r = *sums++;
857 int32_t nl = l >> 12;
858 int32_t nr = r >> 12;
859 l = clamp16(nl);
860 r = clamp16(nr);
861 *out++ = (r<<16) | (l & 0xFFFF);
862 }
863}
864
865// no-op case
Eric Laurent65b65452010-06-01 23:49:17 -0700866void AudioMixer::process__nop(state_t* state)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867{
Eric Laurent65b65452010-06-01 23:49:17 -0700868 uint32_t e0 = state->enabledTracks;
869 size_t bufSize = state->frameCount * sizeof(int16_t) * MAX_NUM_CHANNELS;
870 while (e0) {
871 // process by group of tracks with same output buffer to
872 // avoid multiple memset() on same buffer
873 uint32_t e1 = e0, e2 = e0;
874 int i = 31 - __builtin_clz(e1);
875 track_t& t1 = state->tracks[i];
876 e2 &= ~(1<<i);
877 while (e2) {
878 i = 31 - __builtin_clz(e2);
879 e2 &= ~(1<<i);
880 track_t& t2 = state->tracks[i];
881 if UNLIKELY(t2.mainBuffer != t1.mainBuffer) {
882 e1 &= ~(1<<i);
883 }
884 }
885 e0 &= ~(e1);
886
887 memset(t1.mainBuffer, 0, bufSize);
888
889 while (e1) {
890 i = 31 - __builtin_clz(e1);
891 e1 &= ~(1<<i);
892 t1 = state->tracks[i];
893 size_t outFrames = state->frameCount;
894 while (outFrames) {
895 t1.buffer.frameCount = outFrames;
896 t1.bufferProvider->getNextBuffer(&t1.buffer);
897 if (!t1.buffer.raw) break;
898 outFrames -= t1.buffer.frameCount;
899 t1.bufferProvider->releaseBuffer(&t1.buffer);
900 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 }
902 }
903}
904
905// generic code without resampling
Eric Laurent65b65452010-06-01 23:49:17 -0700906void AudioMixer::process__genericNoResampling(state_t* state)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907{
908 int32_t outTemp[BLOCKSIZE * MAX_NUM_CHANNELS] __attribute__((aligned(32)));
909
910 // acquire each track's buffer
911 uint32_t enabledTracks = state->enabledTracks;
Eric Laurent65b65452010-06-01 23:49:17 -0700912 uint32_t e0 = enabledTracks;
913 while (e0) {
914 const int i = 31 - __builtin_clz(e0);
915 e0 &= ~(1<<i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 track_t& t = state->tracks[i];
917 t.buffer.frameCount = state->frameCount;
918 t.bufferProvider->getNextBuffer(&t.buffer);
919 t.frameCount = t.buffer.frameCount;
920 t.in = t.buffer.raw;
921 // t.in == NULL can happen if the track was flushed just after having
922 // been enabled for mixing.
923 if (t.in == NULL)
924 enabledTracks &= ~(1<<i);
925 }
926
Eric Laurent65b65452010-06-01 23:49:17 -0700927 e0 = enabledTracks;
928 while (e0) {
929 // process by group of tracks with same output buffer to
930 // optimize cache use
931 uint32_t e1 = e0, e2 = e0;
932 int j = 31 - __builtin_clz(e1);
933 track_t& t1 = state->tracks[j];
934 e2 &= ~(1<<j);
935 while (e2) {
936 j = 31 - __builtin_clz(e2);
937 e2 &= ~(1<<j);
938 track_t& t2 = state->tracks[j];
939 if UNLIKELY(t2.mainBuffer != t1.mainBuffer) {
940 e1 &= ~(1<<j);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 }
942 }
Eric Laurent65b65452010-06-01 23:49:17 -0700943 e0 &= ~(e1);
944 // this assumes output 16 bits stereo, no resampling
945 int32_t *out = t1.mainBuffer;
946 size_t numFrames = 0;
947 do {
948 memset(outTemp, 0, sizeof(outTemp));
949 e2 = e1;
950 while (e2) {
951 const int i = 31 - __builtin_clz(e2);
952 e2 &= ~(1<<i);
953 track_t& t = state->tracks[i];
954 size_t outFrames = BLOCKSIZE;
955 int32_t *aux = NULL;
956 if UNLIKELY((t.needs & NEEDS_AUX__MASK) == NEEDS_AUX_ENABLED) {
957 aux = t.auxBuffer + numFrames;
958 }
959 while (outFrames) {
960 size_t inFrames = (t.frameCount > outFrames)?outFrames:t.frameCount;
961 if (inFrames) {
962 (t.hook)(&t, outTemp + (BLOCKSIZE-outFrames)*MAX_NUM_CHANNELS, inFrames, state->resampleTemp, aux);
963 t.frameCount -= inFrames;
964 outFrames -= inFrames;
965 if UNLIKELY(aux != NULL) {
966 aux += inFrames;
967 }
968 }
969 if (t.frameCount == 0 && outFrames) {
970 t.bufferProvider->releaseBuffer(&t.buffer);
971 t.buffer.frameCount = (state->frameCount - numFrames) - (BLOCKSIZE - outFrames);
972 t.bufferProvider->getNextBuffer(&t.buffer);
973 t.in = t.buffer.raw;
974 if (t.in == NULL) {
975 enabledTracks &= ~(1<<i);
976 e1 &= ~(1<<i);
977 break;
978 }
979 t.frameCount = t.buffer.frameCount;
980 }
981 }
982 }
983 ditherAndClamp(out, outTemp, BLOCKSIZE);
984 out += BLOCKSIZE;
985 numFrames += BLOCKSIZE;
986 } while (numFrames < state->frameCount);
987 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988
989 // release each track's buffer
Eric Laurent65b65452010-06-01 23:49:17 -0700990 e0 = enabledTracks;
991 while (e0) {
992 const int i = 31 - __builtin_clz(e0);
993 e0 &= ~(1<<i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 track_t& t = state->tracks[i];
995 t.bufferProvider->releaseBuffer(&t.buffer);
996 }
997}
998
Eric Laurent65b65452010-06-01 23:49:17 -0700999
Glenn Kastenfb2ab9e2011-12-12 09:05:55 -08001000// generic code with resampling
Eric Laurent65b65452010-06-01 23:49:17 -07001001void AudioMixer::process__genericResampling(state_t* state)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002{
1003 int32_t* const outTemp = state->outputTemp;
1004 const size_t size = sizeof(int32_t) * MAX_NUM_CHANNELS * state->frameCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 size_t numFrames = state->frameCount;
1007
Eric Laurent65b65452010-06-01 23:49:17 -07001008 uint32_t e0 = state->enabledTracks;
1009 while (e0) {
1010 // process by group of tracks with same output buffer
1011 // to optimize cache use
1012 uint32_t e1 = e0, e2 = e0;
1013 int j = 31 - __builtin_clz(e1);
1014 track_t& t1 = state->tracks[j];
1015 e2 &= ~(1<<j);
1016 while (e2) {
1017 j = 31 - __builtin_clz(e2);
1018 e2 &= ~(1<<j);
1019 track_t& t2 = state->tracks[j];
1020 if UNLIKELY(t2.mainBuffer != t1.mainBuffer) {
1021 e1 &= ~(1<<j);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 }
1023 }
Eric Laurent65b65452010-06-01 23:49:17 -07001024 e0 &= ~(e1);
1025 int32_t *out = t1.mainBuffer;
Yuuhi Yamaguchi681d8182011-02-04 15:24:34 +01001026 memset(outTemp, 0, size);
Eric Laurent65b65452010-06-01 23:49:17 -07001027 while (e1) {
1028 const int i = 31 - __builtin_clz(e1);
1029 e1 &= ~(1<<i);
1030 track_t& t = state->tracks[i];
1031 int32_t *aux = NULL;
1032 if UNLIKELY((t.needs & NEEDS_AUX__MASK) == NEEDS_AUX_ENABLED) {
1033 aux = t.auxBuffer;
1034 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035
Eric Laurent65b65452010-06-01 23:49:17 -07001036 // this is a little goofy, on the resampling case we don't
1037 // acquire/release the buffers because it's done by
1038 // the resampler.
1039 if ((t.needs & NEEDS_RESAMPLE__MASK) == NEEDS_RESAMPLE_ENABLED) {
1040 (t.hook)(&t, outTemp, numFrames, state->resampleTemp, aux);
1041 } else {
1042
1043 size_t outFrames = 0;
1044
1045 while (outFrames < numFrames) {
1046 t.buffer.frameCount = numFrames - outFrames;
1047 t.bufferProvider->getNextBuffer(&t.buffer);
1048 t.in = t.buffer.raw;
1049 // t.in == NULL can happen if the track was flushed just after having
1050 // been enabled for mixing.
1051 if (t.in == NULL) break;
1052
1053 if UNLIKELY(aux != NULL) {
1054 aux += outFrames;
1055 }
1056 (t.hook)(&t, outTemp + outFrames*MAX_NUM_CHANNELS, t.buffer.frameCount, state->resampleTemp, aux);
1057 outFrames += t.buffer.frameCount;
1058 t.bufferProvider->releaseBuffer(&t.buffer);
1059 }
1060 }
1061 }
1062 ditherAndClamp(out, outTemp, numFrames);
1063 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064}
1065
1066// one track, 16 bits stereo without resampling is the most common case
Eric Laurent65b65452010-06-01 23:49:17 -07001067void AudioMixer::process__OneTrack16BitsStereoNoResampling(state_t* state)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068{
1069 const int i = 31 - __builtin_clz(state->enabledTracks);
1070 const track_t& t = state->tracks[i];
1071
1072 AudioBufferProvider::Buffer& b(t.buffer);
Eric Laurent65b65452010-06-01 23:49:17 -07001073
1074 int32_t* out = t.mainBuffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 size_t numFrames = state->frameCount;
Eric Laurent65b65452010-06-01 23:49:17 -07001076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 const int16_t vl = t.volume[0];
1078 const int16_t vr = t.volume[1];
1079 const uint32_t vrl = t.volumeRL;
1080 while (numFrames) {
1081 b.frameCount = numFrames;
1082 t.bufferProvider->getNextBuffer(&b);
1083 int16_t const *in = b.i16;
1084
1085 // in == NULL can happen if the track was flushed just after having
1086 // been enabled for mixing.
The Android Open Source Project10592532009-03-18 17:39:46 -07001087 if (in == NULL || ((unsigned long)in & 3)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 memset(out, 0, numFrames*MAX_NUM_CHANNELS*sizeof(int16_t));
The Android Open Source Project10592532009-03-18 17:39:46 -07001089 LOGE_IF(((unsigned long)in & 3), "process stereo track: input buffer alignment pb: buffer %p track %d, channels %d, needs %08x",
1090 in, i, t.channelCount, t.needs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 return;
1092 }
1093 size_t outFrames = b.frameCount;
Eric Laurent65b65452010-06-01 23:49:17 -07001094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 if (UNLIKELY(uint32_t(vl) > UNITY_GAIN || uint32_t(vr) > UNITY_GAIN)) {
1096 // volume is boosted, so we might need to clamp even though
1097 // we process only one track.
1098 do {
1099 uint32_t rl = *reinterpret_cast<uint32_t const *>(in);
1100 in += 2;
1101 int32_t l = mulRL(1, rl, vrl) >> 12;
1102 int32_t r = mulRL(0, rl, vrl) >> 12;
1103 // clamping...
1104 l = clamp16(l);
1105 r = clamp16(r);
1106 *out++ = (r<<16) | (l & 0xFFFF);
1107 } while (--outFrames);
1108 } else {
1109 do {
1110 uint32_t rl = *reinterpret_cast<uint32_t const *>(in);
1111 in += 2;
1112 int32_t l = mulRL(1, rl, vrl) >> 12;
1113 int32_t r = mulRL(0, rl, vrl) >> 12;
1114 *out++ = (r<<16) | (l & 0xFFFF);
1115 } while (--outFrames);
1116 }
1117 numFrames -= b.frameCount;
1118 t.bufferProvider->releaseBuffer(&b);
1119 }
1120}
1121
1122// 2 tracks is also a common case
Eric Laurent65b65452010-06-01 23:49:17 -07001123// NEVER used in current implementation of process__validate()
1124// only use if the 2 tracks have the same output buffer
1125void AudioMixer::process__TwoTracks16BitsStereoNoResampling(state_t* state)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126{
1127 int i;
1128 uint32_t en = state->enabledTracks;
1129
1130 i = 31 - __builtin_clz(en);
1131 const track_t& t0 = state->tracks[i];
1132 AudioBufferProvider::Buffer& b0(t0.buffer);
1133
1134 en &= ~(1<<i);
1135 i = 31 - __builtin_clz(en);
1136 const track_t& t1 = state->tracks[i];
1137 AudioBufferProvider::Buffer& b1(t1.buffer);
Eric Laurent65b65452010-06-01 23:49:17 -07001138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 int16_t const *in0;
1140 const int16_t vl0 = t0.volume[0];
1141 const int16_t vr0 = t0.volume[1];
1142 size_t frameCount0 = 0;
Eric Laurent65b65452010-06-01 23:49:17 -07001143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 int16_t const *in1;
1145 const int16_t vl1 = t1.volume[0];
1146 const int16_t vr1 = t1.volume[1];
1147 size_t frameCount1 = 0;
Eric Laurent65b65452010-06-01 23:49:17 -07001148
1149 //FIXME: only works if two tracks use same buffer
1150 int32_t* out = t0.mainBuffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 size_t numFrames = state->frameCount;
1152 int16_t const *buff = NULL;
1153
Eric Laurent65b65452010-06-01 23:49:17 -07001154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 while (numFrames) {
Eric Laurent65b65452010-06-01 23:49:17 -07001156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 if (frameCount0 == 0) {
1158 b0.frameCount = numFrames;
1159 t0.bufferProvider->getNextBuffer(&b0);
1160 if (b0.i16 == NULL) {
1161 if (buff == NULL) {
1162 buff = new int16_t[MAX_NUM_CHANNELS * state->frameCount];
1163 }
1164 in0 = buff;
1165 b0.frameCount = numFrames;
1166 } else {
1167 in0 = b0.i16;
1168 }
1169 frameCount0 = b0.frameCount;
1170 }
1171 if (frameCount1 == 0) {
1172 b1.frameCount = numFrames;
1173 t1.bufferProvider->getNextBuffer(&b1);
1174 if (b1.i16 == NULL) {
1175 if (buff == NULL) {
1176 buff = new int16_t[MAX_NUM_CHANNELS * state->frameCount];
1177 }
1178 in1 = buff;
1179 b1.frameCount = numFrames;
Glenn Kastenfb2ab9e2011-12-12 09:05:55 -08001180 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 in1 = b1.i16;
1182 }
1183 frameCount1 = b1.frameCount;
1184 }
Eric Laurent65b65452010-06-01 23:49:17 -07001185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 size_t outFrames = frameCount0 < frameCount1?frameCount0:frameCount1;
1187
1188 numFrames -= outFrames;
1189 frameCount0 -= outFrames;
1190 frameCount1 -= outFrames;
Eric Laurent65b65452010-06-01 23:49:17 -07001191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 do {
1193 int32_t l0 = *in0++;
1194 int32_t r0 = *in0++;
1195 l0 = mul(l0, vl0);
1196 r0 = mul(r0, vr0);
1197 int32_t l = *in1++;
1198 int32_t r = *in1++;
1199 l = mulAdd(l, vl1, l0) >> 12;
1200 r = mulAdd(r, vr1, r0) >> 12;
1201 // clamping...
1202 l = clamp16(l);
1203 r = clamp16(r);
1204 *out++ = (r<<16) | (l & 0xFFFF);
1205 } while (--outFrames);
Eric Laurent65b65452010-06-01 23:49:17 -07001206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 if (frameCount0 == 0) {
1208 t0.bufferProvider->releaseBuffer(&b0);
1209 }
1210 if (frameCount1 == 0) {
1211 t1.bufferProvider->releaseBuffer(&b1);
1212 }
Eric Laurent65b65452010-06-01 23:49:17 -07001213 }
1214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 if (buff != NULL) {
Eric Laurent65b65452010-06-01 23:49:17 -07001216 delete [] buff;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217 }
1218}
1219
1220// ----------------------------------------------------------------------------
1221}; // namespace android