1 /*
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"
19 //#define LOG_NDEBUG 0
20
21 #include <stdint.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <math.h>
25 #include <sys/types.h>
26
27 #include <utils/Errors.h>
28 #include <utils/Log.h>
29
30 #include <cutils/bitops.h>
31 #include <cutils/compiler.h>
32 #include <utils/Debug.h>
33
34 #include <system/audio.h>
35
36 #include <audio_utils/primitives.h>
37 #include <audio_utils/format.h>
38 #include <media/AudioMixer.h>
39
40 #include "AudioMixerOps.h"
41
42 // The FCC_2 macro refers to the Fixed Channel Count of 2 for the legacy integer mixer.
43 #ifndef FCC_2
44 #define FCC_2 2
45 #endif
46
47 // Look for MONO_HACK for any Mono hack involving legacy mono channel to
48 // stereo channel conversion.
49
50 /* VERY_VERY_VERBOSE_LOGGING will show exactly which process hook and track hook is
51 * being used. This is a considerable amount of log spam, so don't enable unless you
52 * are verifying the hook based code.
53 */
54 //#define VERY_VERY_VERBOSE_LOGGING
55 #ifdef VERY_VERY_VERBOSE_LOGGING
56 #define ALOGVV ALOGV
57 //define ALOGVV printf // for test-mixer.cpp
58 #else
59 #define ALOGVV(a...) do { } while (0)
60 #endif
61
62 #ifndef ARRAY_SIZE
63 #define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
64 #endif
65
66 // TODO: Move these macro/inlines to a header file.
67 template <typename T>
68 static inline
max(const T & x,const T & y)69 T max(const T& x, const T& y) {
70 return x > y ? x : y;
71 }
72
73 // Set kUseNewMixer to true to use the new mixer engine always. Otherwise the
74 // original code will be used for stereo sinks, the new mixer for multichannel.
75 static const bool kUseNewMixer = true;
76
77 // Set kUseFloat to true to allow floating input into the mixer engine.
78 // If kUseNewMixer is false, this is ignored or may be overridden internally
79 // because of downmix/upmix support.
80 static const bool kUseFloat = true;
81
82 // Set to default copy buffer size in frames for input processing.
83 static const size_t kCopyBufferFrameCount = 256;
84
85 namespace android {
86
87 // ----------------------------------------------------------------------------
88
89 template <typename T>
min(const T & a,const T & b)90 T min(const T& a, const T& b)
91 {
92 return a < b ? a : b;
93 }
94
95 // ----------------------------------------------------------------------------
96
97 // Ensure mConfiguredNames bitmask is initialized properly on all architectures.
98 // The value of 1 << x is undefined in C when x >= 32.
99
AudioMixer(size_t frameCount,uint32_t sampleRate,uint32_t maxNumTracks)100 AudioMixer::AudioMixer(size_t frameCount, uint32_t sampleRate, uint32_t maxNumTracks)
101 : mTrackNames(0), mConfiguredNames((maxNumTracks >= 32 ? 0 : 1 << maxNumTracks) - 1),
102 mSampleRate(sampleRate)
103 {
104 ALOG_ASSERT(maxNumTracks <= MAX_NUM_TRACKS, "maxNumTracks %u > MAX_NUM_TRACKS %u",
105 maxNumTracks, MAX_NUM_TRACKS);
106
107 // AudioMixer is not yet capable of more than 32 active track inputs
108 ALOG_ASSERT(32 >= MAX_NUM_TRACKS, "bad MAX_NUM_TRACKS %d", MAX_NUM_TRACKS);
109
110 pthread_once(&sOnceControl, &sInitRoutine);
111
112 mState.enabledTracks= 0;
113 mState.needsChanged = 0;
114 mState.frameCount = frameCount;
115 mState.hook = process__nop;
116 mState.outputTemp = NULL;
117 mState.resampleTemp = NULL;
118 mState.mLog = &mDummyLog;
119 // mState.reserved
120
121 // FIXME Most of the following initialization is probably redundant since
122 // tracks[i] should only be referenced if (mTrackNames & (1 << i)) != 0
123 // and mTrackNames is initially 0. However, leave it here until that's verified.
124 track_t* t = mState.tracks;
125 for (unsigned i=0 ; i < MAX_NUM_TRACKS ; i++) {
126 t->resampler = NULL;
127 t->downmixerBufferProvider = NULL;
128 t->mReformatBufferProvider = NULL;
129 t->mTimestretchBufferProvider = NULL;
130 t++;
131 }
132
133 }
134
~AudioMixer()135 AudioMixer::~AudioMixer()
136 {
137 track_t* t = mState.tracks;
138 for (unsigned i=0 ; i < MAX_NUM_TRACKS ; i++) {
139 delete t->resampler;
140 delete t->downmixerBufferProvider;
141 delete t->mReformatBufferProvider;
142 delete t->mTimestretchBufferProvider;
143 t++;
144 }
145 delete [] mState.outputTemp;
146 delete [] mState.resampleTemp;
147 }
148
setLog(NBLog::Writer * log)149 void AudioMixer::setLog(NBLog::Writer *log)
150 {
151 mState.mLog = log;
152 }
153
selectMixerInFormat(audio_format_t inputFormat __unused)154 static inline audio_format_t selectMixerInFormat(audio_format_t inputFormat __unused) {
155 return kUseFloat && kUseNewMixer ? AUDIO_FORMAT_PCM_FLOAT : AUDIO_FORMAT_PCM_16_BIT;
156 }
157
getTrackName(audio_channel_mask_t channelMask,audio_format_t format,int sessionId)158 int AudioMixer::getTrackName(audio_channel_mask_t channelMask,
159 audio_format_t format, int sessionId)
160 {
161 if (!isValidPcmTrackFormat(format)) {
162 ALOGE("AudioMixer::getTrackName invalid format (%#x)", format);
163 return -1;
164 }
165 uint32_t names = (~mTrackNames) & mConfiguredNames;
166 if (names != 0) {
167 int n = __builtin_ctz(names);
168 ALOGV("add track (%d)", n);
169 // assume default parameters for the track, except where noted below
170 track_t* t = &mState.tracks[n];
171 t->needs = 0;
172
173 // Integer volume.
174 // Currently integer volume is kept for the legacy integer mixer.
175 // Will be removed when the legacy mixer path is removed.
176 t->volume[0] = UNITY_GAIN_INT;
177 t->volume[1] = UNITY_GAIN_INT;
178 t->prevVolume[0] = UNITY_GAIN_INT << 16;
179 t->prevVolume[1] = UNITY_GAIN_INT << 16;
180 t->volumeInc[0] = 0;
181 t->volumeInc[1] = 0;
182 t->auxLevel = 0;
183 t->auxInc = 0;
184 t->prevAuxLevel = 0;
185
186 // Floating point volume.
187 t->mVolume[0] = UNITY_GAIN_FLOAT;
188 t->mVolume[1] = UNITY_GAIN_FLOAT;
189 t->mPrevVolume[0] = UNITY_GAIN_FLOAT;
190 t->mPrevVolume[1] = UNITY_GAIN_FLOAT;
191 t->mVolumeInc[0] = 0.;
192 t->mVolumeInc[1] = 0.;
193 t->mAuxLevel = 0.;
194 t->mAuxInc = 0.;
195 t->mPrevAuxLevel = 0.;
196
197 // no initialization needed
198 // t->frameCount
199 t->channelCount = audio_channel_count_from_out_mask(channelMask);
200 t->enabled = false;
201 ALOGV_IF(audio_channel_mask_get_bits(channelMask) != AUDIO_CHANNEL_OUT_STEREO,
202 "Non-stereo channel mask: %d\n", channelMask);
203 t->channelMask = channelMask;
204 t->sessionId = sessionId;
205 // setBufferProvider(name, AudioBufferProvider *) is required before enable(name)
206 t->bufferProvider = NULL;
207 t->buffer.raw = NULL;
208 // no initialization needed
209 // t->buffer.frameCount
210 t->hook = NULL;
211 t->in = NULL;
212 t->resampler = NULL;
213 t->sampleRate = mSampleRate;
214 // setParameter(name, TRACK, MAIN_BUFFER, mixBuffer) is required before enable(name)
215 t->mainBuffer = NULL;
216 t->auxBuffer = NULL;
217 t->mInputBufferProvider = NULL;
218 t->mReformatBufferProvider = NULL;
219 t->downmixerBufferProvider = NULL;
220 t->mPostDownmixReformatBufferProvider = NULL;
221 t->mTimestretchBufferProvider = NULL;
222 t->mMixerFormat = AUDIO_FORMAT_PCM_16_BIT;
223 t->mFormat = format;
224 t->mMixerInFormat = selectMixerInFormat(format);
225 t->mDownmixRequiresFormat = AUDIO_FORMAT_INVALID; // no format required
226 t->mMixerChannelMask = audio_channel_mask_from_representation_and_bits(
227 AUDIO_CHANNEL_REPRESENTATION_POSITION, AUDIO_CHANNEL_OUT_STEREO);
228 t->mMixerChannelCount = audio_channel_count_from_out_mask(t->mMixerChannelMask);
229 t->mPlaybackRate = AUDIO_PLAYBACK_RATE_DEFAULT;
230 // Check the downmixing (or upmixing) requirements.
231 status_t status = t->prepareForDownmix();
232 if (status != OK) {
233 ALOGE("AudioMixer::getTrackName invalid channelMask (%#x)", channelMask);
234 return -1;
235 }
236 // prepareForDownmix() may change mDownmixRequiresFormat
237 ALOGVV("mMixerFormat:%#x mMixerInFormat:%#x\n", t->mMixerFormat, t->mMixerInFormat);
238 t->prepareForReformat();
239 mTrackNames |= 1 << n;
240 return TRACK0 + n;
241 }
242 ALOGE("AudioMixer::getTrackName out of available tracks");
243 return -1;
244 }
245
invalidateState(uint32_t mask)246 void AudioMixer::invalidateState(uint32_t mask)
247 {
248 if (mask != 0) {
249 mState.needsChanged |= mask;
250 mState.hook = process__validate;
251 }
252 }
253
254 // Called when channel masks have changed for a track name
255 // TODO: Fix DownmixerBufferProvider not to (possibly) change mixer input format,
256 // which will simplify this logic.
setChannelMasks(int name,audio_channel_mask_t trackChannelMask,audio_channel_mask_t mixerChannelMask)257 bool AudioMixer::setChannelMasks(int name,
258 audio_channel_mask_t trackChannelMask, audio_channel_mask_t mixerChannelMask) {
259 track_t &track = mState.tracks[name];
260
261 if (trackChannelMask == track.channelMask
262 && mixerChannelMask == track.mMixerChannelMask) {
263 return false; // no need to change
264 }
265 // always recompute for both channel masks even if only one has changed.
266 const uint32_t trackChannelCount = audio_channel_count_from_out_mask(trackChannelMask);
267 const uint32_t mixerChannelCount = audio_channel_count_from_out_mask(mixerChannelMask);
268 const bool mixerChannelCountChanged = track.mMixerChannelCount != mixerChannelCount;
269
270 ALOG_ASSERT((trackChannelCount <= MAX_NUM_CHANNELS_TO_DOWNMIX)
271 && trackChannelCount
272 && mixerChannelCount);
273 track.channelMask = trackChannelMask;
274 track.channelCount = trackChannelCount;
275 track.mMixerChannelMask = mixerChannelMask;
276 track.mMixerChannelCount = mixerChannelCount;
277
278 // channel masks have changed, does this track need a downmixer?
279 // update to try using our desired format (if we aren't already using it)
280 const audio_format_t prevDownmixerFormat = track.mDownmixRequiresFormat;
281 const status_t status = mState.tracks[name].prepareForDownmix();
282 ALOGE_IF(status != OK,
283 "prepareForDownmix error %d, track channel mask %#x, mixer channel mask %#x",
284 status, track.channelMask, track.mMixerChannelMask);
285
286 if (prevDownmixerFormat != track.mDownmixRequiresFormat) {
287 track.prepareForReformat(); // because of downmixer, track format may change!
288 }
289
290 if (track.resampler && mixerChannelCountChanged) {
291 // resampler channels may have changed.
292 const uint32_t resetToSampleRate = track.sampleRate;
293 delete track.resampler;
294 track.resampler = NULL;
295 track.sampleRate = mSampleRate; // without resampler, track rate is device sample rate.
296 // recreate the resampler with updated format, channels, saved sampleRate.
297 track.setResampler(resetToSampleRate /*trackSampleRate*/, mSampleRate /*devSampleRate*/);
298 }
299 return true;
300 }
301
unprepareForDownmix()302 void AudioMixer::track_t::unprepareForDownmix() {
303 ALOGV("AudioMixer::unprepareForDownmix(%p)", this);
304
305 if (mPostDownmixReformatBufferProvider != nullptr) {
306 // release any buffers held by the mPostDownmixReformatBufferProvider
307 // before deallocating the downmixerBufferProvider.
308 mPostDownmixReformatBufferProvider->reset();
309 }
310
311 mDownmixRequiresFormat = AUDIO_FORMAT_INVALID;
312 if (downmixerBufferProvider != NULL) {
313 // this track had previously been configured with a downmixer, delete it
314 ALOGV(" deleting old downmixer");
315 delete downmixerBufferProvider;
316 downmixerBufferProvider = NULL;
317 reconfigureBufferProviders();
318 } else {
319 ALOGV(" nothing to do, no downmixer to delete");
320 }
321 }
322
prepareForDownmix()323 status_t AudioMixer::track_t::prepareForDownmix()
324 {
325 ALOGV("AudioMixer::prepareForDownmix(%p) with mask 0x%x",
326 this, channelMask);
327
328 // discard the previous downmixer if there was one
329 unprepareForDownmix();
330 // MONO_HACK Only remix (upmix or downmix) if the track and mixer/device channel masks
331 // are not the same and not handled internally, as mono -> stereo currently is.
332 if (channelMask == mMixerChannelMask
333 || (channelMask == AUDIO_CHANNEL_OUT_MONO
334 && mMixerChannelMask == AUDIO_CHANNEL_OUT_STEREO)) {
335 return NO_ERROR;
336 }
337 // DownmixerBufferProvider is only used for position masks.
338 if (audio_channel_mask_get_representation(channelMask)
339 == AUDIO_CHANNEL_REPRESENTATION_POSITION
340 && DownmixerBufferProvider::isMultichannelCapable()) {
341 DownmixerBufferProvider* pDbp = new DownmixerBufferProvider(channelMask,
342 mMixerChannelMask,
343 AUDIO_FORMAT_PCM_16_BIT /* TODO: use mMixerInFormat, now only PCM 16 */,
344 sampleRate, sessionId, kCopyBufferFrameCount);
345
346 if (pDbp->isValid()) { // if constructor completed properly
347 mDownmixRequiresFormat = AUDIO_FORMAT_PCM_16_BIT; // PCM 16 bit required for downmix
348 downmixerBufferProvider = pDbp;
349 reconfigureBufferProviders();
350 return NO_ERROR;
351 }
352 delete pDbp;
353 }
354
355 // Effect downmixer does not accept the channel conversion. Let's use our remixer.
356 RemixBufferProvider* pRbp = new RemixBufferProvider(channelMask,
357 mMixerChannelMask, mMixerInFormat, kCopyBufferFrameCount);
358 // Remix always finds a conversion whereas Downmixer effect above may fail.
359 downmixerBufferProvider = pRbp;
360 reconfigureBufferProviders();
361 return NO_ERROR;
362 }
363
unprepareForReformat()364 void AudioMixer::track_t::unprepareForReformat() {
365 ALOGV("AudioMixer::unprepareForReformat(%p)", this);
366 bool requiresReconfigure = false;
367 if (mReformatBufferProvider != NULL) {
368 delete mReformatBufferProvider;
369 mReformatBufferProvider = NULL;
370 requiresReconfigure = true;
371 }
372 if (mPostDownmixReformatBufferProvider != NULL) {
373 delete mPostDownmixReformatBufferProvider;
374 mPostDownmixReformatBufferProvider = NULL;
375 requiresReconfigure = true;
376 }
377 if (requiresReconfigure) {
378 reconfigureBufferProviders();
379 }
380 }
381
prepareForReformat()382 status_t AudioMixer::track_t::prepareForReformat()
383 {
384 ALOGV("AudioMixer::prepareForReformat(%p) with format %#x", this, mFormat);
385 // discard previous reformatters
386 unprepareForReformat();
387 // only configure reformatters as needed
388 const audio_format_t targetFormat = mDownmixRequiresFormat != AUDIO_FORMAT_INVALID
389 ? mDownmixRequiresFormat : mMixerInFormat;
390 bool requiresReconfigure = false;
391 if (mFormat != targetFormat) {
392 mReformatBufferProvider = new ReformatBufferProvider(
393 audio_channel_count_from_out_mask(channelMask),
394 mFormat,
395 targetFormat,
396 kCopyBufferFrameCount);
397 requiresReconfigure = true;
398 }
399 if (targetFormat != mMixerInFormat) {
400 mPostDownmixReformatBufferProvider = new ReformatBufferProvider(
401 audio_channel_count_from_out_mask(mMixerChannelMask),
402 targetFormat,
403 mMixerInFormat,
404 kCopyBufferFrameCount);
405 requiresReconfigure = true;
406 }
407 if (requiresReconfigure) {
408 reconfigureBufferProviders();
409 }
410 return NO_ERROR;
411 }
412
reconfigureBufferProviders()413 void AudioMixer::track_t::reconfigureBufferProviders()
414 {
415 bufferProvider = mInputBufferProvider;
416 if (mReformatBufferProvider) {
417 mReformatBufferProvider->setBufferProvider(bufferProvider);
418 bufferProvider = mReformatBufferProvider;
419 }
420 if (downmixerBufferProvider) {
421 downmixerBufferProvider->setBufferProvider(bufferProvider);
422 bufferProvider = downmixerBufferProvider;
423 }
424 if (mPostDownmixReformatBufferProvider) {
425 mPostDownmixReformatBufferProvider->setBufferProvider(bufferProvider);
426 bufferProvider = mPostDownmixReformatBufferProvider;
427 }
428 if (mTimestretchBufferProvider) {
429 mTimestretchBufferProvider->setBufferProvider(bufferProvider);
430 bufferProvider = mTimestretchBufferProvider;
431 }
432 }
433
deleteTrackName(int name)434 void AudioMixer::deleteTrackName(int name)
435 {
436 ALOGV("AudioMixer::deleteTrackName(%d)", name);
437 name -= TRACK0;
438 LOG_ALWAYS_FATAL_IF(name < 0 || name >= (int)MAX_NUM_TRACKS, "bad track name %d", name);
439 ALOGV("deleteTrackName(%d)", name);
440 track_t& track(mState.tracks[ name ]);
441 if (track.enabled) {
442 track.enabled = false;
443 invalidateState(1<<name);
444 }
445 // delete the resampler
446 delete track.resampler;
447 track.resampler = NULL;
448 // delete the downmixer
449 mState.tracks[name].unprepareForDownmix();
450 // delete the reformatter
451 mState.tracks[name].unprepareForReformat();
452 // delete the timestretch provider
453 delete track.mTimestretchBufferProvider;
454 track.mTimestretchBufferProvider = NULL;
455 mTrackNames &= ~(1<<name);
456 }
457
enable(int name)458 void AudioMixer::enable(int name)
459 {
460 name -= TRACK0;
461 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
462 track_t& track = mState.tracks[name];
463
464 if (!track.enabled) {
465 track.enabled = true;
466 ALOGV("enable(%d)", name);
467 invalidateState(1 << name);
468 }
469 }
470
disable(int name)471 void AudioMixer::disable(int name)
472 {
473 name -= TRACK0;
474 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
475 track_t& track = mState.tracks[name];
476
477 if (track.enabled) {
478 track.enabled = false;
479 ALOGV("disable(%d)", name);
480 invalidateState(1 << name);
481 }
482 }
483
484 /* Sets the volume ramp variables for the AudioMixer.
485 *
486 * The volume ramp variables are used to transition from the previous
487 * volume to the set volume. ramp controls the duration of the transition.
488 * Its value is typically one state framecount period, but may also be 0,
489 * meaning "immediate."
490 *
491 * FIXME: 1) Volume ramp is enabled only if there is a nonzero integer increment
492 * even if there is a nonzero floating point increment (in that case, the volume
493 * change is immediate). This restriction should be changed when the legacy mixer
494 * is removed (see #2).
495 * FIXME: 2) Integer volume variables are used for Legacy mixing and should be removed
496 * when no longer needed.
497 *
498 * @param newVolume set volume target in floating point [0.0, 1.0].
499 * @param ramp number of frames to increment over. if ramp is 0, the volume
500 * should be set immediately. Currently ramp should not exceed 65535 (frames).
501 * @param pIntSetVolume pointer to the U4.12 integer target volume, set on return.
502 * @param pIntPrevVolume pointer to the U4.28 integer previous volume, set on return.
503 * @param pIntVolumeInc pointer to the U4.28 increment per output audio frame, set on return.
504 * @param pSetVolume pointer to the float target volume, set on return.
505 * @param pPrevVolume pointer to the float previous volume, set on return.
506 * @param pVolumeInc pointer to the float increment per output audio frame, set on return.
507 * @return true if the volume has changed, false if volume is same.
508 */
setVolumeRampVariables(float newVolume,int32_t ramp,int16_t * pIntSetVolume,int32_t * pIntPrevVolume,int32_t * pIntVolumeInc,float * pSetVolume,float * pPrevVolume,float * pVolumeInc)509 static inline bool setVolumeRampVariables(float newVolume, int32_t ramp,
510 int16_t *pIntSetVolume, int32_t *pIntPrevVolume, int32_t *pIntVolumeInc,
511 float *pSetVolume, float *pPrevVolume, float *pVolumeInc) {
512 // check floating point volume to see if it is identical to the previously
513 // set volume.
514 // We do not use a tolerance here (and reject changes too small)
515 // as it may be confusing to use a different value than the one set.
516 // If the resulting volume is too small to ramp, it is a direct set of the volume.
517 if (newVolume == *pSetVolume) {
518 return false;
519 }
520 if (newVolume < 0) {
521 newVolume = 0; // should not have negative volumes
522 } else {
523 switch (fpclassify(newVolume)) {
524 case FP_SUBNORMAL:
525 case FP_NAN:
526 newVolume = 0;
527 break;
528 case FP_ZERO:
529 break; // zero volume is fine
530 case FP_INFINITE:
531 // Infinite volume could be handled consistently since
532 // floating point math saturates at infinities,
533 // but we limit volume to unity gain float.
534 // ramp = 0; break;
535 //
536 newVolume = AudioMixer::UNITY_GAIN_FLOAT;
537 break;
538 case FP_NORMAL:
539 default:
540 // Floating point does not have problems with overflow wrap
541 // that integer has. However, we limit the volume to
542 // unity gain here.
543 // TODO: Revisit the volume limitation and perhaps parameterize.
544 if (newVolume > AudioMixer::UNITY_GAIN_FLOAT) {
545 newVolume = AudioMixer::UNITY_GAIN_FLOAT;
546 }
547 break;
548 }
549 }
550
551 // set floating point volume ramp
552 if (ramp != 0) {
553 // when the ramp completes, *pPrevVolume is set to *pSetVolume, so there
554 // is no computational mismatch; hence equality is checked here.
555 ALOGD_IF(*pPrevVolume != *pSetVolume, "previous float ramp hasn't finished,"
556 " prev:%f set_to:%f", *pPrevVolume, *pSetVolume);
557 const float inc = (newVolume - *pPrevVolume) / ramp; // could be inf, nan, subnormal
558 const float maxv = max(newVolume, *pPrevVolume); // could be inf, cannot be nan, subnormal
559
560 if (isnormal(inc) // inc must be a normal number (no subnormals, infinite, nan)
561 && maxv + inc != maxv) { // inc must make forward progress
562 *pVolumeInc = inc;
563 // ramp is set now.
564 // Note: if newVolume is 0, then near the end of the ramp,
565 // it may be possible that the ramped volume may be subnormal or
566 // temporarily negative by a small amount or subnormal due to floating
567 // point inaccuracies.
568 } else {
569 ramp = 0; // ramp not allowed
570 }
571 }
572
573 // compute and check integer volume, no need to check negative values
574 // The integer volume is limited to "unity_gain" to avoid wrapping and other
575 // audio artifacts, so it never reaches the range limit of U4.28.
576 // We safely use signed 16 and 32 bit integers here.
577 const float scaledVolume = newVolume * AudioMixer::UNITY_GAIN_INT; // not neg, subnormal, nan
578 const int32_t intVolume = (scaledVolume >= (float)AudioMixer::UNITY_GAIN_INT) ?
579 AudioMixer::UNITY_GAIN_INT : (int32_t)scaledVolume;
580
581 // set integer volume ramp
582 if (ramp != 0) {
583 // integer volume is U4.12 (to use 16 bit multiplies), but ramping uses U4.28.
584 // when the ramp completes, *pIntPrevVolume is set to *pIntSetVolume << 16, so there
585 // is no computational mismatch; hence equality is checked here.
586 ALOGD_IF(*pIntPrevVolume != *pIntSetVolume << 16, "previous int ramp hasn't finished,"
587 " prev:%d set_to:%d", *pIntPrevVolume, *pIntSetVolume << 16);
588 const int32_t inc = ((intVolume << 16) - *pIntPrevVolume) / ramp;
589
590 if (inc != 0) { // inc must make forward progress
591 *pIntVolumeInc = inc;
592 } else {
593 ramp = 0; // ramp not allowed
594 }
595 }
596
597 // if no ramp, or ramp not allowed, then clear float and integer increments
598 if (ramp == 0) {
599 *pVolumeInc = 0;
600 *pPrevVolume = newVolume;
601 *pIntVolumeInc = 0;
602 *pIntPrevVolume = intVolume << 16;
603 }
604 *pSetVolume = newVolume;
605 *pIntSetVolume = intVolume;
606 return true;
607 }
608
setParameter(int name,int target,int param,void * value)609 void AudioMixer::setParameter(int name, int target, int param, void *value)
610 {
611 name -= TRACK0;
612 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
613 track_t& track = mState.tracks[name];
614
615 int valueInt = static_cast<int>(reinterpret_cast<uintptr_t>(value));
616 int32_t *valueBuf = reinterpret_cast<int32_t*>(value);
617
618 switch (target) {
619
620 case TRACK:
621 switch (param) {
622 case CHANNEL_MASK: {
623 const audio_channel_mask_t trackChannelMask =
624 static_cast<audio_channel_mask_t>(valueInt);
625 if (setChannelMasks(name, trackChannelMask, track.mMixerChannelMask)) {
626 ALOGV("setParameter(TRACK, CHANNEL_MASK, %x)", trackChannelMask);
627 invalidateState(1 << name);
628 }
629 } break;
630 case MAIN_BUFFER:
631 if (track.mainBuffer != valueBuf) {
632 track.mainBuffer = valueBuf;
633 ALOGV("setParameter(TRACK, MAIN_BUFFER, %p)", valueBuf);
634 invalidateState(1 << name);
635 }
636 break;
637 case AUX_BUFFER:
638 if (track.auxBuffer != valueBuf) {
639 track.auxBuffer = valueBuf;
640 ALOGV("setParameter(TRACK, AUX_BUFFER, %p)", valueBuf);
641 invalidateState(1 << name);
642 }
643 break;
644 case FORMAT: {
645 audio_format_t format = static_cast<audio_format_t>(valueInt);
646 if (track.mFormat != format) {
647 ALOG_ASSERT(audio_is_linear_pcm(format), "Invalid format %#x", format);
648 track.mFormat = format;
649 ALOGV("setParameter(TRACK, FORMAT, %#x)", format);
650 track.prepareForReformat();
651 invalidateState(1 << name);
652 }
653 } break;
654 // FIXME do we want to support setting the downmix type from AudioFlinger?
655 // for a specific track? or per mixer?
656 /* case DOWNMIX_TYPE:
657 break */
658 case MIXER_FORMAT: {
659 audio_format_t format = static_cast<audio_format_t>(valueInt);
660 if (track.mMixerFormat != format) {
661 track.mMixerFormat = format;
662 ALOGV("setParameter(TRACK, MIXER_FORMAT, %#x)", format);
663 }
664 } break;
665 case MIXER_CHANNEL_MASK: {
666 const audio_channel_mask_t mixerChannelMask =
667 static_cast<audio_channel_mask_t>(valueInt);
668 if (setChannelMasks(name, track.channelMask, mixerChannelMask)) {
669 ALOGV("setParameter(TRACK, MIXER_CHANNEL_MASK, %#x)", mixerChannelMask);
670 invalidateState(1 << name);
671 }
672 } break;
673 default:
674 LOG_ALWAYS_FATAL("setParameter track: bad param %d", param);
675 }
676 break;
677
678 case RESAMPLE:
679 switch (param) {
680 case SAMPLE_RATE:
681 ALOG_ASSERT(valueInt > 0, "bad sample rate %d", valueInt);
682 if (track.setResampler(uint32_t(valueInt), mSampleRate)) {
683 ALOGV("setParameter(RESAMPLE, SAMPLE_RATE, %u)",
684 uint32_t(valueInt));
685 invalidateState(1 << name);
686 }
687 break;
688 case RESET:
689 track.resetResampler();
690 invalidateState(1 << name);
691 break;
692 case REMOVE:
693 delete track.resampler;
694 track.resampler = NULL;
695 track.sampleRate = mSampleRate;
696 invalidateState(1 << name);
697 break;
698 default:
699 LOG_ALWAYS_FATAL("setParameter resample: bad param %d", param);
700 }
701 break;
702
703 case RAMP_VOLUME:
704 case VOLUME:
705 switch (param) {
706 case AUXLEVEL:
707 if (setVolumeRampVariables(*reinterpret_cast<float*>(value),
708 target == RAMP_VOLUME ? mState.frameCount : 0,
709 &track.auxLevel, &track.prevAuxLevel, &track.auxInc,
710 &track.mAuxLevel, &track.mPrevAuxLevel, &track.mAuxInc)) {
711 ALOGV("setParameter(%s, AUXLEVEL: %04x)",
712 target == VOLUME ? "VOLUME" : "RAMP_VOLUME", track.auxLevel);
713 invalidateState(1 << name);
714 }
715 break;
716 default:
717 if ((unsigned)param >= VOLUME0 && (unsigned)param < VOLUME0 + MAX_NUM_VOLUMES) {
718 if (setVolumeRampVariables(*reinterpret_cast<float*>(value),
719 target == RAMP_VOLUME ? mState.frameCount : 0,
720 &track.volume[param - VOLUME0], &track.prevVolume[param - VOLUME0],
721 &track.volumeInc[param - VOLUME0],
722 &track.mVolume[param - VOLUME0], &track.mPrevVolume[param - VOLUME0],
723 &track.mVolumeInc[param - VOLUME0])) {
724 ALOGV("setParameter(%s, VOLUME%d: %04x)",
725 target == VOLUME ? "VOLUME" : "RAMP_VOLUME", param - VOLUME0,
726 track.volume[param - VOLUME0]);
727 invalidateState(1 << name);
728 }
729 } else {
730 LOG_ALWAYS_FATAL("setParameter volume: bad param %d", param);
731 }
732 }
733 break;
734 case TIMESTRETCH:
735 switch (param) {
736 case PLAYBACK_RATE: {
737 const AudioPlaybackRate *playbackRate =
738 reinterpret_cast<AudioPlaybackRate*>(value);
739 ALOGW_IF(!isAudioPlaybackRateValid(*playbackRate),
740 "bad parameters speed %f, pitch %f",playbackRate->mSpeed,
741 playbackRate->mPitch);
742 if (track.setPlaybackRate(*playbackRate)) {
743 ALOGV("setParameter(TIMESTRETCH, PLAYBACK_RATE, STRETCH_MODE, FALLBACK_MODE "
744 "%f %f %d %d",
745 playbackRate->mSpeed,
746 playbackRate->mPitch,
747 playbackRate->mStretchMode,
748 playbackRate->mFallbackMode);
749 // invalidateState(1 << name);
750 }
751 } break;
752 default:
753 LOG_ALWAYS_FATAL("setParameter timestretch: bad param %d", param);
754 }
755 break;
756
757 default:
758 LOG_ALWAYS_FATAL("setParameter: bad target %d", target);
759 }
760 }
761
setResampler(uint32_t trackSampleRate,uint32_t devSampleRate)762 bool AudioMixer::track_t::setResampler(uint32_t trackSampleRate, uint32_t devSampleRate)
763 {
764 if (trackSampleRate != devSampleRate || resampler != NULL) {
765 if (sampleRate != trackSampleRate) {
766 sampleRate = trackSampleRate;
767 if (resampler == NULL) {
768 ALOGV("Creating resampler from track %d Hz to device %d Hz",
769 trackSampleRate, devSampleRate);
770 AudioResampler::src_quality quality;
771 // force lowest quality level resampler if use case isn't music or video
772 // FIXME this is flawed for dynamic sample rates, as we choose the resampler
773 // quality level based on the initial ratio, but that could change later.
774 // Should have a way to distinguish tracks with static ratios vs. dynamic ratios.
775 if (isMusicRate(trackSampleRate)) {
776 quality = AudioResampler::DEFAULT_QUALITY;
777 } else {
778 quality = AudioResampler::DYN_LOW_QUALITY;
779 }
780
781 // TODO: Remove MONO_HACK. Resampler sees #channels after the downmixer
782 // but if none exists, it is the channel count (1 for mono).
783 const int resamplerChannelCount = downmixerBufferProvider != NULL
784 ? mMixerChannelCount : channelCount;
785 ALOGVV("Creating resampler:"
786 " format(%#x) channels(%d) devSampleRate(%u) quality(%d)\n",
787 mMixerInFormat, resamplerChannelCount, devSampleRate, quality);
788 resampler = AudioResampler::create(
789 mMixerInFormat,
790 resamplerChannelCount,
791 devSampleRate, quality);
792 }
793 return true;
794 }
795 }
796 return false;
797 }
798
setPlaybackRate(const AudioPlaybackRate & playbackRate)799 bool AudioMixer::track_t::setPlaybackRate(const AudioPlaybackRate &playbackRate)
800 {
801 if ((mTimestretchBufferProvider == NULL &&
802 fabs(playbackRate.mSpeed - mPlaybackRate.mSpeed) < AUDIO_TIMESTRETCH_SPEED_MIN_DELTA &&
803 fabs(playbackRate.mPitch - mPlaybackRate.mPitch) < AUDIO_TIMESTRETCH_PITCH_MIN_DELTA) ||
804 isAudioPlaybackRateEqual(playbackRate, mPlaybackRate)) {
805 return false;
806 }
807 mPlaybackRate = playbackRate;
808 if (mTimestretchBufferProvider == NULL) {
809 // TODO: Remove MONO_HACK. Resampler sees #channels after the downmixer
810 // but if none exists, it is the channel count (1 for mono).
811 const int timestretchChannelCount = downmixerBufferProvider != NULL
812 ? mMixerChannelCount : channelCount;
813 mTimestretchBufferProvider = new TimestretchBufferProvider(timestretchChannelCount,
814 mMixerInFormat, sampleRate, playbackRate);
815 reconfigureBufferProviders();
816 } else {
817 reinterpret_cast<TimestretchBufferProvider*>(mTimestretchBufferProvider)
818 ->setPlaybackRate(playbackRate);
819 }
820 return true;
821 }
822
823 /* Checks to see if the volume ramp has completed and clears the increment
824 * variables appropriately.
825 *
826 * FIXME: There is code to handle int/float ramp variable switchover should it not
827 * complete within a mixer buffer processing call, but it is preferred to avoid switchover
828 * due to precision issues. The switchover code is included for legacy code purposes
829 * and can be removed once the integer volume is removed.
830 *
831 * It is not sufficient to clear only the volumeInc integer variable because
832 * if one channel requires ramping, all channels are ramped.
833 *
834 * There is a bit of duplicated code here, but it keeps backward compatibility.
835 */
adjustVolumeRamp(bool aux,bool useFloat)836 inline void AudioMixer::track_t::adjustVolumeRamp(bool aux, bool useFloat)
837 {
838 if (useFloat) {
839 for (uint32_t i = 0; i < MAX_NUM_VOLUMES; i++) {
840 if ((mVolumeInc[i] > 0 && mPrevVolume[i] + mVolumeInc[i] >= mVolume[i]) ||
841 (mVolumeInc[i] < 0 && mPrevVolume[i] + mVolumeInc[i] <= mVolume[i])) {
842 volumeInc[i] = 0;
843 prevVolume[i] = volume[i] << 16;
844 mVolumeInc[i] = 0.;
845 mPrevVolume[i] = mVolume[i];
846 } else {
847 //ALOGV("ramp: %f %f %f", mVolume[i], mPrevVolume[i], mVolumeInc[i]);
848 prevVolume[i] = u4_28_from_float(mPrevVolume[i]);
849 }
850 }
851 } else {
852 for (uint32_t i = 0; i < MAX_NUM_VOLUMES; i++) {
853 if (((volumeInc[i]>0) && (((prevVolume[i]+volumeInc[i])>>16) >= volume[i])) ||
854 ((volumeInc[i]<0) && (((prevVolume[i]+volumeInc[i])>>16) <= volume[i]))) {
855 volumeInc[i] = 0;
856 prevVolume[i] = volume[i] << 16;
857 mVolumeInc[i] = 0.;
858 mPrevVolume[i] = mVolume[i];
859 } else {
860 //ALOGV("ramp: %d %d %d", volume[i] << 16, prevVolume[i], volumeInc[i]);
861 mPrevVolume[i] = float_from_u4_28(prevVolume[i]);
862 }
863 }
864 }
865 /* TODO: aux is always integer regardless of output buffer type */
866 if (aux) {
867 if (((auxInc>0) && (((prevAuxLevel+auxInc)>>16) >= auxLevel)) ||
868 ((auxInc<0) && (((prevAuxLevel+auxInc)>>16) <= auxLevel))) {
869 auxInc = 0;
870 prevAuxLevel = auxLevel << 16;
871 mAuxInc = 0.;
872 mPrevAuxLevel = mAuxLevel;
873 } else {
874 //ALOGV("aux ramp: %d %d %d", auxLevel << 16, prevAuxLevel, auxInc);
875 }
876 }
877 }
878
getUnreleasedFrames(int name) const879 size_t AudioMixer::getUnreleasedFrames(int name) const
880 {
881 name -= TRACK0;
882 if (uint32_t(name) < MAX_NUM_TRACKS) {
883 return mState.tracks[name].getUnreleasedFrames();
884 }
885 return 0;
886 }
887
setBufferProvider(int name,AudioBufferProvider * bufferProvider)888 void AudioMixer::setBufferProvider(int name, AudioBufferProvider* bufferProvider)
889 {
890 name -= TRACK0;
891 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
892
893 if (mState.tracks[name].mInputBufferProvider == bufferProvider) {
894 return; // don't reset any buffer providers if identical.
895 }
896 if (mState.tracks[name].mReformatBufferProvider != NULL) {
897 mState.tracks[name].mReformatBufferProvider->reset();
898 } else if (mState.tracks[name].downmixerBufferProvider != NULL) {
899 mState.tracks[name].downmixerBufferProvider->reset();
900 } else if (mState.tracks[name].mPostDownmixReformatBufferProvider != NULL) {
901 mState.tracks[name].mPostDownmixReformatBufferProvider->reset();
902 } else if (mState.tracks[name].mTimestretchBufferProvider != NULL) {
903 mState.tracks[name].mTimestretchBufferProvider->reset();
904 }
905
906 mState.tracks[name].mInputBufferProvider = bufferProvider;
907 mState.tracks[name].reconfigureBufferProviders();
908 }
909
910
process()911 void AudioMixer::process()
912 {
913 mState.hook(&mState);
914 }
915
916
process__validate(state_t * state)917 void AudioMixer::process__validate(state_t* state)
918 {
919 ALOGW_IF(!state->needsChanged,
920 "in process__validate() but nothing's invalid");
921
922 uint32_t changed = state->needsChanged;
923 state->needsChanged = 0; // clear the validation flag
924
925 // recompute which tracks are enabled / disabled
926 uint32_t enabled = 0;
927 uint32_t disabled = 0;
928 while (changed) {
929 const int i = 31 - __builtin_clz(changed);
930 const uint32_t mask = 1<<i;
931 changed &= ~mask;
932 track_t& t = state->tracks[i];
933 (t.enabled ? enabled : disabled) |= mask;
934 }
935 state->enabledTracks &= ~disabled;
936 state->enabledTracks |= enabled;
937
938 // compute everything we need...
939 int countActiveTracks = 0;
940 // TODO: fix all16BitsStereNoResample logic to
941 // either properly handle muted tracks (it should ignore them)
942 // or remove altogether as an obsolete optimization.
943 bool all16BitsStereoNoResample = true;
944 bool resampling = false;
945 bool volumeRamp = false;
946 uint32_t en = state->enabledTracks;
947 while (en) {
948 const int i = 31 - __builtin_clz(en);
949 en &= ~(1<<i);
950
951 countActiveTracks++;
952 track_t& t = state->tracks[i];
953 uint32_t n = 0;
954 // FIXME can overflow (mask is only 3 bits)
955 n |= NEEDS_CHANNEL_1 + t.channelCount - 1;
956 if (t.doesResample()) {
957 n |= NEEDS_RESAMPLE;
958 }
959 if (t.auxLevel != 0 && t.auxBuffer != NULL) {
960 n |= NEEDS_AUX;
961 }
962
963 if (t.volumeInc[0]|t.volumeInc[1]) {
964 volumeRamp = true;
965 } else if (!t.doesResample() && t.volumeRL == 0) {
966 n |= NEEDS_MUTE;
967 }
968 t.needs = n;
969
970 if (n & NEEDS_MUTE) {
971 t.hook = track__nop;
972 } else {
973 if (n & NEEDS_AUX) {
974 all16BitsStereoNoResample = false;
975 }
976 if (n & NEEDS_RESAMPLE) {
977 all16BitsStereoNoResample = false;
978 resampling = true;
979 t.hook = getTrackHook(TRACKTYPE_RESAMPLE, t.mMixerChannelCount,
980 t.mMixerInFormat, t.mMixerFormat);
981 ALOGV_IF((n & NEEDS_CHANNEL_COUNT__MASK) > NEEDS_CHANNEL_2,
982 "Track %d needs downmix + resample", i);
983 } else {
984 if ((n & NEEDS_CHANNEL_COUNT__MASK) == NEEDS_CHANNEL_1){
985 t.hook = getTrackHook(
986 (t.mMixerChannelMask == AUDIO_CHANNEL_OUT_STEREO // TODO: MONO_HACK
987 && t.channelMask == AUDIO_CHANNEL_OUT_MONO)
988 ? TRACKTYPE_NORESAMPLEMONO : TRACKTYPE_NORESAMPLE,
989 t.mMixerChannelCount,
990 t.mMixerInFormat, t.mMixerFormat);
991 all16BitsStereoNoResample = false;
992 }
993 if ((n & NEEDS_CHANNEL_COUNT__MASK) >= NEEDS_CHANNEL_2){
994 t.hook = getTrackHook(TRACKTYPE_NORESAMPLE, t.mMixerChannelCount,
995 t.mMixerInFormat, t.mMixerFormat);
996 ALOGV_IF((n & NEEDS_CHANNEL_COUNT__MASK) > NEEDS_CHANNEL_2,
997 "Track %d needs downmix", i);
998 }
999 }
1000 }
1001 }
1002
1003 // select the processing hooks
1004 state->hook = process__nop;
1005 if (countActiveTracks > 0) {
1006 if (resampling) {
1007 if (!state->outputTemp) {
1008 state->outputTemp = new int32_t[MAX_NUM_CHANNELS * state->frameCount];
1009 }
1010 if (!state->resampleTemp) {
1011 state->resampleTemp = new int32_t[MAX_NUM_CHANNELS * state->frameCount];
1012 }
1013 state->hook = process__genericResampling;
1014 } else {
1015 if (state->outputTemp) {
1016 delete [] state->outputTemp;
1017 state->outputTemp = NULL;
1018 }
1019 if (state->resampleTemp) {
1020 delete [] state->resampleTemp;
1021 state->resampleTemp = NULL;
1022 }
1023 state->hook = process__genericNoResampling;
1024 if (all16BitsStereoNoResample && !volumeRamp) {
1025 if (countActiveTracks == 1) {
1026 const int i = 31 - __builtin_clz(state->enabledTracks);
1027 track_t& t = state->tracks[i];
1028 if ((t.needs & NEEDS_MUTE) == 0) {
1029 // The check prevents a muted track from acquiring a process hook.
1030 //
1031 // This is dangerous if the track is MONO as that requires
1032 // special case handling due to implicit channel duplication.
1033 // Stereo or Multichannel should actually be fine here.
1034 state->hook = getProcessHook(PROCESSTYPE_NORESAMPLEONETRACK,
1035 t.mMixerChannelCount, t.mMixerInFormat, t.mMixerFormat);
1036 }
1037 }
1038 }
1039 }
1040 }
1041
1042 ALOGV("mixer configuration change: %d activeTracks (%08x) "
1043 "all16BitsStereoNoResample=%d, resampling=%d, volumeRamp=%d",
1044 countActiveTracks, state->enabledTracks,
1045 all16BitsStereoNoResample, resampling, volumeRamp);
1046
1047 state->hook(state);
1048
1049 // Now that the volume ramp has been done, set optimal state and
1050 // track hooks for subsequent mixer process
1051 if (countActiveTracks > 0) {
1052 bool allMuted = true;
1053 uint32_t en = state->enabledTracks;
1054 while (en) {
1055 const int i = 31 - __builtin_clz(en);
1056 en &= ~(1<<i);
1057 track_t& t = state->tracks[i];
1058 if (!t.doesResample() && t.volumeRL == 0) {
1059 t.needs |= NEEDS_MUTE;
1060 t.hook = track__nop;
1061 } else {
1062 allMuted = false;
1063 }
1064 }
1065 if (allMuted) {
1066 state->hook = process__nop;
1067 } else if (all16BitsStereoNoResample) {
1068 if (countActiveTracks == 1) {
1069 const int i = 31 - __builtin_clz(state->enabledTracks);
1070 track_t& t = state->tracks[i];
1071 // Muted single tracks handled by allMuted above.
1072 state->hook = getProcessHook(PROCESSTYPE_NORESAMPLEONETRACK,
1073 t.mMixerChannelCount, t.mMixerInFormat, t.mMixerFormat);
1074 }
1075 }
1076 }
1077 }
1078
1079
track__genericResample(track_t * t,int32_t * out,size_t outFrameCount,int32_t * temp,int32_t * aux)1080 void AudioMixer::track__genericResample(track_t* t, int32_t* out, size_t outFrameCount,
1081 int32_t* temp, int32_t* aux)
1082 {
1083 ALOGVV("track__genericResample\n");
1084 t->resampler->setSampleRate(t->sampleRate);
1085
1086 // ramp gain - resample to temp buffer and scale/mix in 2nd step
1087 if (aux != NULL) {
1088 // always resample with unity gain when sending to auxiliary buffer to be able
1089 // to apply send level after resampling
1090 t->resampler->setVolume(UNITY_GAIN_FLOAT, UNITY_GAIN_FLOAT);
1091 memset(temp, 0, outFrameCount * t->mMixerChannelCount * sizeof(int32_t));
1092 t->resampler->resample(temp, outFrameCount, t->bufferProvider);
1093 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) {
1094 volumeRampStereo(t, out, outFrameCount, temp, aux);
1095 } else {
1096 volumeStereo(t, out, outFrameCount, temp, aux);
1097 }
1098 } else {
1099 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) {
1100 t->resampler->setVolume(UNITY_GAIN_FLOAT, UNITY_GAIN_FLOAT);
1101 memset(temp, 0, outFrameCount * MAX_NUM_CHANNELS * sizeof(int32_t));
1102 t->resampler->resample(temp, outFrameCount, t->bufferProvider);
1103 volumeRampStereo(t, out, outFrameCount, temp, aux);
1104 }
1105
1106 // constant gain
1107 else {
1108 t->resampler->setVolume(t->mVolume[0], t->mVolume[1]);
1109 t->resampler->resample(out, outFrameCount, t->bufferProvider);
1110 }
1111 }
1112 }
1113
track__nop(track_t * t __unused,int32_t * out __unused,size_t outFrameCount __unused,int32_t * temp __unused,int32_t * aux __unused)1114 void AudioMixer::track__nop(track_t* t __unused, int32_t* out __unused,
1115 size_t outFrameCount __unused, int32_t* temp __unused, int32_t* aux __unused)
1116 {
1117 }
1118
volumeRampStereo(track_t * t,int32_t * out,size_t frameCount,int32_t * temp,int32_t * aux)1119 void AudioMixer::volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp,
1120 int32_t* aux)
1121 {
1122 int32_t vl = t->prevVolume[0];
1123 int32_t vr = t->prevVolume[1];
1124 const int32_t vlInc = t->volumeInc[0];
1125 const int32_t vrInc = t->volumeInc[1];
1126
1127 //ALOGD("[0] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
1128 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
1129 // (vl + vlInc*frameCount)/65536.0f, frameCount);
1130
1131 // ramp volume
1132 if (CC_UNLIKELY(aux != NULL)) {
1133 int32_t va = t->prevAuxLevel;
1134 const int32_t vaInc = t->auxInc;
1135 int32_t l;
1136 int32_t r;
1137
1138 do {
1139 l = (*temp++ >> 12);
1140 r = (*temp++ >> 12);
1141 *out++ += (vl >> 16) * l;
1142 *out++ += (vr >> 16) * r;
1143 *aux++ += (va >> 17) * (l + r);
1144 vl += vlInc;
1145 vr += vrInc;
1146 va += vaInc;
1147 } while (--frameCount);
1148 t->prevAuxLevel = va;
1149 } else {
1150 do {
1151 *out++ += (vl >> 16) * (*temp++ >> 12);
1152 *out++ += (vr >> 16) * (*temp++ >> 12);
1153 vl += vlInc;
1154 vr += vrInc;
1155 } while (--frameCount);
1156 }
1157 t->prevVolume[0] = vl;
1158 t->prevVolume[1] = vr;
1159 t->adjustVolumeRamp(aux != NULL);
1160 }
1161
volumeStereo(track_t * t,int32_t * out,size_t frameCount,int32_t * temp,int32_t * aux)1162 void AudioMixer::volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp,
1163 int32_t* aux)
1164 {
1165 const int16_t vl = t->volume[0];
1166 const int16_t vr = t->volume[1];
1167
1168 if (CC_UNLIKELY(aux != NULL)) {
1169 const int16_t va = t->auxLevel;
1170 do {
1171 int16_t l = (int16_t)(*temp++ >> 12);
1172 int16_t r = (int16_t)(*temp++ >> 12);
1173 out[0] = mulAdd(l, vl, out[0]);
1174 int16_t a = (int16_t)(((int32_t)l + r) >> 1);
1175 out[1] = mulAdd(r, vr, out[1]);
1176 out += 2;
1177 aux[0] = mulAdd(a, va, aux[0]);
1178 aux++;
1179 } while (--frameCount);
1180 } else {
1181 do {
1182 int16_t l = (int16_t)(*temp++ >> 12);
1183 int16_t r = (int16_t)(*temp++ >> 12);
1184 out[0] = mulAdd(l, vl, out[0]);
1185 out[1] = mulAdd(r, vr, out[1]);
1186 out += 2;
1187 } while (--frameCount);
1188 }
1189 }
1190
track__16BitsStereo(track_t * t,int32_t * out,size_t frameCount,int32_t * temp __unused,int32_t * aux)1191 void AudioMixer::track__16BitsStereo(track_t* t, int32_t* out, size_t frameCount,
1192 int32_t* temp __unused, int32_t* aux)
1193 {
1194 ALOGVV("track__16BitsStereo\n");
1195 const int16_t *in = static_cast<const int16_t *>(t->in);
1196
1197 if (CC_UNLIKELY(aux != NULL)) {
1198 int32_t l;
1199 int32_t r;
1200 // ramp gain
1201 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) {
1202 int32_t vl = t->prevVolume[0];
1203 int32_t vr = t->prevVolume[1];
1204 int32_t va = t->prevAuxLevel;
1205 const int32_t vlInc = t->volumeInc[0];
1206 const int32_t vrInc = t->volumeInc[1];
1207 const int32_t vaInc = t->auxInc;
1208 // ALOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
1209 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
1210 // (vl + vlInc*frameCount)/65536.0f, frameCount);
1211
1212 do {
1213 l = (int32_t)*in++;
1214 r = (int32_t)*in++;
1215 *out++ += (vl >> 16) * l;
1216 *out++ += (vr >> 16) * r;
1217 *aux++ += (va >> 17) * (l + r);
1218 vl += vlInc;
1219 vr += vrInc;
1220 va += vaInc;
1221 } while (--frameCount);
1222
1223 t->prevVolume[0] = vl;
1224 t->prevVolume[1] = vr;
1225 t->prevAuxLevel = va;
1226 t->adjustVolumeRamp(true);
1227 }
1228
1229 // constant gain
1230 else {
1231 const uint32_t vrl = t->volumeRL;
1232 const int16_t va = (int16_t)t->auxLevel;
1233 do {
1234 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
1235 int16_t a = (int16_t)(((int32_t)in[0] + in[1]) >> 1);
1236 in += 2;
1237 out[0] = mulAddRL(1, rl, vrl, out[0]);
1238 out[1] = mulAddRL(0, rl, vrl, out[1]);
1239 out += 2;
1240 aux[0] = mulAdd(a, va, aux[0]);
1241 aux++;
1242 } while (--frameCount);
1243 }
1244 } else {
1245 // ramp gain
1246 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) {
1247 int32_t vl = t->prevVolume[0];
1248 int32_t vr = t->prevVolume[1];
1249 const int32_t vlInc = t->volumeInc[0];
1250 const int32_t vrInc = t->volumeInc[1];
1251
1252 // ALOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
1253 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
1254 // (vl + vlInc*frameCount)/65536.0f, frameCount);
1255
1256 do {
1257 *out++ += (vl >> 16) * (int32_t) *in++;
1258 *out++ += (vr >> 16) * (int32_t) *in++;
1259 vl += vlInc;
1260 vr += vrInc;
1261 } while (--frameCount);
1262
1263 t->prevVolume[0] = vl;
1264 t->prevVolume[1] = vr;
1265 t->adjustVolumeRamp(false);
1266 }
1267
1268 // constant gain
1269 else {
1270 const uint32_t vrl = t->volumeRL;
1271 do {
1272 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
1273 in += 2;
1274 out[0] = mulAddRL(1, rl, vrl, out[0]);
1275 out[1] = mulAddRL(0, rl, vrl, out[1]);
1276 out += 2;
1277 } while (--frameCount);
1278 }
1279 }
1280 t->in = in;
1281 }
1282
track__16BitsMono(track_t * t,int32_t * out,size_t frameCount,int32_t * temp __unused,int32_t * aux)1283 void AudioMixer::track__16BitsMono(track_t* t, int32_t* out, size_t frameCount,
1284 int32_t* temp __unused, int32_t* aux)
1285 {
1286 ALOGVV("track__16BitsMono\n");
1287 const int16_t *in = static_cast<int16_t const *>(t->in);
1288
1289 if (CC_UNLIKELY(aux != NULL)) {
1290 // ramp gain
1291 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) {
1292 int32_t vl = t->prevVolume[0];
1293 int32_t vr = t->prevVolume[1];
1294 int32_t va = t->prevAuxLevel;
1295 const int32_t vlInc = t->volumeInc[0];
1296 const int32_t vrInc = t->volumeInc[1];
1297 const int32_t vaInc = t->auxInc;
1298
1299 // ALOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
1300 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
1301 // (vl + vlInc*frameCount)/65536.0f, frameCount);
1302
1303 do {
1304 int32_t l = *in++;
1305 *out++ += (vl >> 16) * l;
1306 *out++ += (vr >> 16) * l;
1307 *aux++ += (va >> 16) * l;
1308 vl += vlInc;
1309 vr += vrInc;
1310 va += vaInc;
1311 } while (--frameCount);
1312
1313 t->prevVolume[0] = vl;
1314 t->prevVolume[1] = vr;
1315 t->prevAuxLevel = va;
1316 t->adjustVolumeRamp(true);
1317 }
1318 // constant gain
1319 else {
1320 const int16_t vl = t->volume[0];
1321 const int16_t vr = t->volume[1];
1322 const int16_t va = (int16_t)t->auxLevel;
1323 do {
1324 int16_t l = *in++;
1325 out[0] = mulAdd(l, vl, out[0]);
1326 out[1] = mulAdd(l, vr, out[1]);
1327 out += 2;
1328 aux[0] = mulAdd(l, va, aux[0]);
1329 aux++;
1330 } while (--frameCount);
1331 }
1332 } else {
1333 // ramp gain
1334 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) {
1335 int32_t vl = t->prevVolume[0];
1336 int32_t vr = t->prevVolume[1];
1337 const int32_t vlInc = t->volumeInc[0];
1338 const int32_t vrInc = t->volumeInc[1];
1339
1340 // ALOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
1341 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
1342 // (vl + vlInc*frameCount)/65536.0f, frameCount);
1343
1344 do {
1345 int32_t l = *in++;
1346 *out++ += (vl >> 16) * l;
1347 *out++ += (vr >> 16) * l;
1348 vl += vlInc;
1349 vr += vrInc;
1350 } while (--frameCount);
1351
1352 t->prevVolume[0] = vl;
1353 t->prevVolume[1] = vr;
1354 t->adjustVolumeRamp(false);
1355 }
1356 // constant gain
1357 else {
1358 const int16_t vl = t->volume[0];
1359 const int16_t vr = t->volume[1];
1360 do {
1361 int16_t l = *in++;
1362 out[0] = mulAdd(l, vl, out[0]);
1363 out[1] = mulAdd(l, vr, out[1]);
1364 out += 2;
1365 } while (--frameCount);
1366 }
1367 }
1368 t->in = in;
1369 }
1370
1371 // no-op case
process__nop(state_t * state)1372 void AudioMixer::process__nop(state_t* state)
1373 {
1374 ALOGVV("process__nop\n");
1375 uint32_t e0 = state->enabledTracks;
1376 while (e0) {
1377 // process by group of tracks with same output buffer to
1378 // avoid multiple memset() on same buffer
1379 uint32_t e1 = e0, e2 = e0;
1380 int i = 31 - __builtin_clz(e1);
1381 {
1382 track_t& t1 = state->tracks[i];
1383 e2 &= ~(1<<i);
1384 while (e2) {
1385 i = 31 - __builtin_clz(e2);
1386 e2 &= ~(1<<i);
1387 track_t& t2 = state->tracks[i];
1388 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) {
1389 e1 &= ~(1<<i);
1390 }
1391 }
1392 e0 &= ~(e1);
1393
1394 memset(t1.mainBuffer, 0, state->frameCount * t1.mMixerChannelCount
1395 * audio_bytes_per_sample(t1.mMixerFormat));
1396 }
1397
1398 while (e1) {
1399 i = 31 - __builtin_clz(e1);
1400 e1 &= ~(1<<i);
1401 {
1402 track_t& t3 = state->tracks[i];
1403 size_t outFrames = state->frameCount;
1404 while (outFrames) {
1405 t3.buffer.frameCount = outFrames;
1406 t3.bufferProvider->getNextBuffer(&t3.buffer);
1407 if (t3.buffer.raw == NULL) break;
1408 outFrames -= t3.buffer.frameCount;
1409 t3.bufferProvider->releaseBuffer(&t3.buffer);
1410 }
1411 }
1412 }
1413 }
1414 }
1415
1416 // generic code without resampling
process__genericNoResampling(state_t * state)1417 void AudioMixer::process__genericNoResampling(state_t* state)
1418 {
1419 ALOGVV("process__genericNoResampling\n");
1420 int32_t outTemp[BLOCKSIZE * MAX_NUM_CHANNELS] __attribute__((aligned(32)));
1421
1422 // acquire each track's buffer
1423 uint32_t enabledTracks = state->enabledTracks;
1424 uint32_t e0 = enabledTracks;
1425 while (e0) {
1426 const int i = 31 - __builtin_clz(e0);
1427 e0 &= ~(1<<i);
1428 track_t& t = state->tracks[i];
1429 t.buffer.frameCount = state->frameCount;
1430 t.bufferProvider->getNextBuffer(&t.buffer);
1431 t.frameCount = t.buffer.frameCount;
1432 t.in = t.buffer.raw;
1433 }
1434
1435 e0 = enabledTracks;
1436 while (e0) {
1437 // process by group of tracks with same output buffer to
1438 // optimize cache use
1439 uint32_t e1 = e0, e2 = e0;
1440 int j = 31 - __builtin_clz(e1);
1441 track_t& t1 = state->tracks[j];
1442 e2 &= ~(1<<j);
1443 while (e2) {
1444 j = 31 - __builtin_clz(e2);
1445 e2 &= ~(1<<j);
1446 track_t& t2 = state->tracks[j];
1447 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) {
1448 e1 &= ~(1<<j);
1449 }
1450 }
1451 e0 &= ~(e1);
1452 // this assumes output 16 bits stereo, no resampling
1453 int32_t *out = t1.mainBuffer;
1454 size_t numFrames = 0;
1455 do {
1456 memset(outTemp, 0, sizeof(outTemp));
1457 e2 = e1;
1458 while (e2) {
1459 const int i = 31 - __builtin_clz(e2);
1460 e2 &= ~(1<<i);
1461 track_t& t = state->tracks[i];
1462 size_t outFrames = BLOCKSIZE;
1463 int32_t *aux = NULL;
1464 if (CC_UNLIKELY(t.needs & NEEDS_AUX)) {
1465 aux = t.auxBuffer + numFrames;
1466 }
1467 while (outFrames) {
1468 // t.in == NULL can happen if the track was flushed just after having
1469 // been enabled for mixing.
1470 if (t.in == NULL) {
1471 enabledTracks &= ~(1<<i);
1472 e1 &= ~(1<<i);
1473 break;
1474 }
1475 size_t inFrames = (t.frameCount > outFrames)?outFrames:t.frameCount;
1476 if (inFrames > 0) {
1477 t.hook(&t, outTemp + (BLOCKSIZE - outFrames) * t.mMixerChannelCount,
1478 inFrames, state->resampleTemp, aux);
1479 t.frameCount -= inFrames;
1480 outFrames -= inFrames;
1481 if (CC_UNLIKELY(aux != NULL)) {
1482 aux += inFrames;
1483 }
1484 }
1485 if (t.frameCount == 0 && outFrames) {
1486 t.bufferProvider->releaseBuffer(&t.buffer);
1487 t.buffer.frameCount = (state->frameCount - numFrames) -
1488 (BLOCKSIZE - outFrames);
1489 t.bufferProvider->getNextBuffer(&t.buffer);
1490 t.in = t.buffer.raw;
1491 if (t.in == NULL) {
1492 enabledTracks &= ~(1<<i);
1493 e1 &= ~(1<<i);
1494 break;
1495 }
1496 t.frameCount = t.buffer.frameCount;
1497 }
1498 }
1499 }
1500
1501 convertMixerFormat(out, t1.mMixerFormat, outTemp, t1.mMixerInFormat,
1502 BLOCKSIZE * t1.mMixerChannelCount);
1503 // TODO: fix ugly casting due to choice of out pointer type
1504 out = reinterpret_cast<int32_t*>((uint8_t*)out
1505 + BLOCKSIZE * t1.mMixerChannelCount
1506 * audio_bytes_per_sample(t1.mMixerFormat));
1507 numFrames += BLOCKSIZE;
1508 } while (numFrames < state->frameCount);
1509 }
1510
1511 // release each track's buffer
1512 e0 = enabledTracks;
1513 while (e0) {
1514 const int i = 31 - __builtin_clz(e0);
1515 e0 &= ~(1<<i);
1516 track_t& t = state->tracks[i];
1517 t.bufferProvider->releaseBuffer(&t.buffer);
1518 }
1519 }
1520
1521
1522 // generic code with resampling
process__genericResampling(state_t * state)1523 void AudioMixer::process__genericResampling(state_t* state)
1524 {
1525 ALOGVV("process__genericResampling\n");
1526 // this const just means that local variable outTemp doesn't change
1527 int32_t* const outTemp = state->outputTemp;
1528 size_t numFrames = state->frameCount;
1529
1530 uint32_t e0 = state->enabledTracks;
1531 while (e0) {
1532 // process by group of tracks with same output buffer
1533 // to optimize cache use
1534 uint32_t e1 = e0, e2 = e0;
1535 int j = 31 - __builtin_clz(e1);
1536 track_t& t1 = state->tracks[j];
1537 e2 &= ~(1<<j);
1538 while (e2) {
1539 j = 31 - __builtin_clz(e2);
1540 e2 &= ~(1<<j);
1541 track_t& t2 = state->tracks[j];
1542 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) {
1543 e1 &= ~(1<<j);
1544 }
1545 }
1546 e0 &= ~(e1);
1547 int32_t *out = t1.mainBuffer;
1548 memset(outTemp, 0, sizeof(*outTemp) * t1.mMixerChannelCount * state->frameCount);
1549 while (e1) {
1550 const int i = 31 - __builtin_clz(e1);
1551 e1 &= ~(1<<i);
1552 track_t& t = state->tracks[i];
1553 int32_t *aux = NULL;
1554 if (CC_UNLIKELY(t.needs & NEEDS_AUX)) {
1555 aux = t.auxBuffer;
1556 }
1557
1558 // this is a little goofy, on the resampling case we don't
1559 // acquire/release the buffers because it's done by
1560 // the resampler.
1561 if (t.needs & NEEDS_RESAMPLE) {
1562 t.hook(&t, outTemp, numFrames, state->resampleTemp, aux);
1563 } else {
1564
1565 size_t outFrames = 0;
1566
1567 while (outFrames < numFrames) {
1568 t.buffer.frameCount = numFrames - outFrames;
1569 t.bufferProvider->getNextBuffer(&t.buffer);
1570 t.in = t.buffer.raw;
1571 // t.in == NULL can happen if the track was flushed just after having
1572 // been enabled for mixing.
1573 if (t.in == NULL) break;
1574
1575 if (CC_UNLIKELY(aux != NULL)) {
1576 aux += outFrames;
1577 }
1578 t.hook(&t, outTemp + outFrames * t.mMixerChannelCount, t.buffer.frameCount,
1579 state->resampleTemp, aux);
1580 outFrames += t.buffer.frameCount;
1581 t.bufferProvider->releaseBuffer(&t.buffer);
1582 }
1583 }
1584 }
1585 convertMixerFormat(out, t1.mMixerFormat,
1586 outTemp, t1.mMixerInFormat, numFrames * t1.mMixerChannelCount);
1587 }
1588 }
1589
1590 // one track, 16 bits stereo without resampling is the most common case
process__OneTrack16BitsStereoNoResampling(state_t * state)1591 void AudioMixer::process__OneTrack16BitsStereoNoResampling(state_t* state)
1592 {
1593 ALOGVV("process__OneTrack16BitsStereoNoResampling\n");
1594 // This method is only called when state->enabledTracks has exactly
1595 // one bit set. The asserts below would verify this, but are commented out
1596 // since the whole point of this method is to optimize performance.
1597 //ALOG_ASSERT(0 != state->enabledTracks, "no tracks enabled");
1598 const int i = 31 - __builtin_clz(state->enabledTracks);
1599 //ALOG_ASSERT((1 << i) == state->enabledTracks, "more than 1 track enabled");
1600 const track_t& t = state->tracks[i];
1601
1602 AudioBufferProvider::Buffer& b(t.buffer);
1603
1604 int32_t* out = t.mainBuffer;
1605 float *fout = reinterpret_cast<float*>(out);
1606 size_t numFrames = state->frameCount;
1607
1608 const int16_t vl = t.volume[0];
1609 const int16_t vr = t.volume[1];
1610 const uint32_t vrl = t.volumeRL;
1611 while (numFrames) {
1612 b.frameCount = numFrames;
1613 t.bufferProvider->getNextBuffer(&b);
1614 const int16_t *in = b.i16;
1615
1616 // in == NULL can happen if the track was flushed just after having
1617 // been enabled for mixing.
1618 if (in == NULL || (((uintptr_t)in) & 3)) {
1619 if ( AUDIO_FORMAT_PCM_FLOAT == t.mMixerFormat ) {
1620 memset((char*)fout, 0, numFrames
1621 * t.mMixerChannelCount * audio_bytes_per_sample(t.mMixerFormat));
1622 } else {
1623 memset((char*)out, 0, numFrames
1624 * t.mMixerChannelCount * audio_bytes_per_sample(t.mMixerFormat));
1625 }
1626 ALOGE_IF((((uintptr_t)in) & 3),
1627 "process__OneTrack16BitsStereoNoResampling: misaligned buffer"
1628 " %p track %d, channels %d, needs %08x, volume %08x vfl %f vfr %f",
1629 in, i, t.channelCount, t.needs, vrl, t.mVolume[0], t.mVolume[1]);
1630 return;
1631 }
1632 size_t outFrames = b.frameCount;
1633
1634 switch (t.mMixerFormat) {
1635 case AUDIO_FORMAT_PCM_FLOAT:
1636 do {
1637 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
1638 in += 2;
1639 int32_t l = mulRL(1, rl, vrl);
1640 int32_t r = mulRL(0, rl, vrl);
1641 *fout++ = float_from_q4_27(l);
1642 *fout++ = float_from_q4_27(r);
1643 // Note: In case of later int16_t sink output,
1644 // conversion and clamping is done by memcpy_to_i16_from_float().
1645 } while (--outFrames);
1646 break;
1647 case AUDIO_FORMAT_PCM_16_BIT:
1648 if (CC_UNLIKELY(uint32_t(vl) > UNITY_GAIN_INT || uint32_t(vr) > UNITY_GAIN_INT)) {
1649 // volume is boosted, so we might need to clamp even though
1650 // we process only one track.
1651 do {
1652 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
1653 in += 2;
1654 int32_t l = mulRL(1, rl, vrl) >> 12;
1655 int32_t r = mulRL(0, rl, vrl) >> 12;
1656 // clamping...
1657 l = clamp16(l);
1658 r = clamp16(r);
1659 *out++ = (r<<16) | (l & 0xFFFF);
1660 } while (--outFrames);
1661 } else {
1662 do {
1663 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
1664 in += 2;
1665 int32_t l = mulRL(1, rl, vrl) >> 12;
1666 int32_t r = mulRL(0, rl, vrl) >> 12;
1667 *out++ = (r<<16) | (l & 0xFFFF);
1668 } while (--outFrames);
1669 }
1670 break;
1671 default:
1672 LOG_ALWAYS_FATAL("bad mixer format: %d", t.mMixerFormat);
1673 }
1674 numFrames -= b.frameCount;
1675 t.bufferProvider->releaseBuffer(&b);
1676 }
1677 }
1678
1679 /*static*/ pthread_once_t AudioMixer::sOnceControl = PTHREAD_ONCE_INIT;
1680
sInitRoutine()1681 /*static*/ void AudioMixer::sInitRoutine()
1682 {
1683 DownmixerBufferProvider::init(); // for the downmixer
1684 }
1685
1686 /* TODO: consider whether this level of optimization is necessary.
1687 * Perhaps just stick with a single for loop.
1688 */
1689
1690 // Needs to derive a compile time constant (constexpr). Could be targeted to go
1691 // to a MONOVOL mixtype based on MAX_NUM_VOLUMES, but that's an unnecessary complication.
1692 #define MIXTYPE_MONOVOL(mixtype) ((mixtype) == MIXTYPE_MULTI ? MIXTYPE_MULTI_MONOVOL : \
1693 (mixtype) == MIXTYPE_MULTI_SAVEONLY ? MIXTYPE_MULTI_SAVEONLY_MONOVOL : (mixtype))
1694
1695 /* MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
1696 * TO: int32_t (Q4.27) or float
1697 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
1698 * TA: int32_t (Q4.27)
1699 */
1700 template <int MIXTYPE,
1701 typename TO, typename TI, typename TV, typename TA, typename TAV>
volumeRampMulti(uint32_t channels,TO * out,size_t frameCount,const TI * in,TA * aux,TV * vol,const TV * volinc,TAV * vola,TAV volainc)1702 static void volumeRampMulti(uint32_t channels, TO* out, size_t frameCount,
1703 const TI* in, TA* aux, TV *vol, const TV *volinc, TAV *vola, TAV volainc)
1704 {
1705 switch (channels) {
1706 case 1:
1707 volumeRampMulti<MIXTYPE, 1>(out, frameCount, in, aux, vol, volinc, vola, volainc);
1708 break;
1709 case 2:
1710 volumeRampMulti<MIXTYPE, 2>(out, frameCount, in, aux, vol, volinc, vola, volainc);
1711 break;
1712 case 3:
1713 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 3>(out,
1714 frameCount, in, aux, vol, volinc, vola, volainc);
1715 break;
1716 case 4:
1717 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 4>(out,
1718 frameCount, in, aux, vol, volinc, vola, volainc);
1719 break;
1720 case 5:
1721 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 5>(out,
1722 frameCount, in, aux, vol, volinc, vola, volainc);
1723 break;
1724 case 6:
1725 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 6>(out,
1726 frameCount, in, aux, vol, volinc, vola, volainc);
1727 break;
1728 case 7:
1729 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 7>(out,
1730 frameCount, in, aux, vol, volinc, vola, volainc);
1731 break;
1732 case 8:
1733 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 8>(out,
1734 frameCount, in, aux, vol, volinc, vola, volainc);
1735 break;
1736 }
1737 }
1738
1739 /* MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
1740 * TO: int32_t (Q4.27) or float
1741 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
1742 * TA: int32_t (Q4.27)
1743 */
1744 template <int MIXTYPE,
1745 typename TO, typename TI, typename TV, typename TA, typename TAV>
volumeMulti(uint32_t channels,TO * out,size_t frameCount,const TI * in,TA * aux,const TV * vol,TAV vola)1746 static void volumeMulti(uint32_t channels, TO* out, size_t frameCount,
1747 const TI* in, TA* aux, const TV *vol, TAV vola)
1748 {
1749 switch (channels) {
1750 case 1:
1751 volumeMulti<MIXTYPE, 1>(out, frameCount, in, aux, vol, vola);
1752 break;
1753 case 2:
1754 volumeMulti<MIXTYPE, 2>(out, frameCount, in, aux, vol, vola);
1755 break;
1756 case 3:
1757 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 3>(out, frameCount, in, aux, vol, vola);
1758 break;
1759 case 4:
1760 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 4>(out, frameCount, in, aux, vol, vola);
1761 break;
1762 case 5:
1763 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 5>(out, frameCount, in, aux, vol, vola);
1764 break;
1765 case 6:
1766 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 6>(out, frameCount, in, aux, vol, vola);
1767 break;
1768 case 7:
1769 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 7>(out, frameCount, in, aux, vol, vola);
1770 break;
1771 case 8:
1772 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 8>(out, frameCount, in, aux, vol, vola);
1773 break;
1774 }
1775 }
1776
1777 /* MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
1778 * USEFLOATVOL (set to true if float volume is used)
1779 * ADJUSTVOL (set to true if volume ramp parameters needs adjustment afterwards)
1780 * TO: int32_t (Q4.27) or float
1781 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
1782 * TA: int32_t (Q4.27)
1783 */
1784 template <int MIXTYPE, bool USEFLOATVOL, bool ADJUSTVOL,
1785 typename TO, typename TI, typename TA>
volumeMix(TO * out,size_t outFrames,const TI * in,TA * aux,bool ramp,AudioMixer::track_t * t)1786 void AudioMixer::volumeMix(TO *out, size_t outFrames,
1787 const TI *in, TA *aux, bool ramp, AudioMixer::track_t *t)
1788 {
1789 if (USEFLOATVOL) {
1790 if (ramp) {
1791 volumeRampMulti<MIXTYPE>(t->mMixerChannelCount, out, outFrames, in, aux,
1792 t->mPrevVolume, t->mVolumeInc, &t->prevAuxLevel, t->auxInc);
1793 if (ADJUSTVOL) {
1794 t->adjustVolumeRamp(aux != NULL, true);
1795 }
1796 } else {
1797 volumeMulti<MIXTYPE>(t->mMixerChannelCount, out, outFrames, in, aux,
1798 t->mVolume, t->auxLevel);
1799 }
1800 } else {
1801 if (ramp) {
1802 volumeRampMulti<MIXTYPE>(t->mMixerChannelCount, out, outFrames, in, aux,
1803 t->prevVolume, t->volumeInc, &t->prevAuxLevel, t->auxInc);
1804 if (ADJUSTVOL) {
1805 t->adjustVolumeRamp(aux != NULL);
1806 }
1807 } else {
1808 volumeMulti<MIXTYPE>(t->mMixerChannelCount, out, outFrames, in, aux,
1809 t->volume, t->auxLevel);
1810 }
1811 }
1812 }
1813
1814 /* This process hook is called when there is a single track without
1815 * aux buffer, volume ramp, or resampling.
1816 * TODO: Update the hook selection: this can properly handle aux and ramp.
1817 *
1818 * MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
1819 * TO: int32_t (Q4.27) or float
1820 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
1821 * TA: int32_t (Q4.27)
1822 */
1823 template <int MIXTYPE, typename TO, typename TI, typename TA>
process_NoResampleOneTrack(state_t * state)1824 void AudioMixer::process_NoResampleOneTrack(state_t* state)
1825 {
1826 ALOGVV("process_NoResampleOneTrack\n");
1827 // CLZ is faster than CTZ on ARM, though really not sure if true after 31 - clz.
1828 const int i = 31 - __builtin_clz(state->enabledTracks);
1829 ALOG_ASSERT((1 << i) == state->enabledTracks, "more than 1 track enabled");
1830 track_t *t = &state->tracks[i];
1831 const uint32_t channels = t->mMixerChannelCount;
1832 TO* out = reinterpret_cast<TO*>(t->mainBuffer);
1833 TA* aux = reinterpret_cast<TA*>(t->auxBuffer);
1834 const bool ramp = t->needsRamp();
1835
1836 for (size_t numFrames = state->frameCount; numFrames; ) {
1837 AudioBufferProvider::Buffer& b(t->buffer);
1838 // get input buffer
1839 b.frameCount = numFrames;
1840 t->bufferProvider->getNextBuffer(&b);
1841 const TI *in = reinterpret_cast<TI*>(b.raw);
1842
1843 // in == NULL can happen if the track was flushed just after having
1844 // been enabled for mixing.
1845 if (in == NULL || (((uintptr_t)in) & 3)) {
1846 memset(out, 0, numFrames
1847 * channels * audio_bytes_per_sample(t->mMixerFormat));
1848 ALOGE_IF((((uintptr_t)in) & 3), "process_NoResampleOneTrack: bus error: "
1849 "buffer %p track %p, channels %d, needs %#x",
1850 in, t, t->channelCount, t->needs);
1851 return;
1852 }
1853
1854 const size_t outFrames = b.frameCount;
1855 volumeMix<MIXTYPE, is_same<TI, float>::value, false> (
1856 out, outFrames, in, aux, ramp, t);
1857
1858 out += outFrames * channels;
1859 if (aux != NULL) {
1860 aux += channels;
1861 }
1862 numFrames -= b.frameCount;
1863
1864 // release buffer
1865 t->bufferProvider->releaseBuffer(&b);
1866 }
1867 if (ramp) {
1868 t->adjustVolumeRamp(aux != NULL, is_same<TI, float>::value);
1869 }
1870 }
1871
1872 /* This track hook is called to do resampling then mixing,
1873 * pulling from the track's upstream AudioBufferProvider.
1874 *
1875 * MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
1876 * TO: int32_t (Q4.27) or float
1877 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
1878 * TA: int32_t (Q4.27)
1879 */
1880 template <int MIXTYPE, typename TO, typename TI, typename TA>
track__Resample(track_t * t,TO * out,size_t outFrameCount,TO * temp,TA * aux)1881 void AudioMixer::track__Resample(track_t* t, TO* out, size_t outFrameCount, TO* temp, TA* aux)
1882 {
1883 ALOGVV("track__Resample\n");
1884 t->resampler->setSampleRate(t->sampleRate);
1885 const bool ramp = t->needsRamp();
1886 if (ramp || aux != NULL) {
1887 // if ramp: resample with unity gain to temp buffer and scale/mix in 2nd step.
1888 // if aux != NULL: resample with unity gain to temp buffer then apply send level.
1889
1890 t->resampler->setVolume(UNITY_GAIN_FLOAT, UNITY_GAIN_FLOAT);
1891 memset(temp, 0, outFrameCount * t->mMixerChannelCount * sizeof(TO));
1892 t->resampler->resample((int32_t*)temp, outFrameCount, t->bufferProvider);
1893
1894 volumeMix<MIXTYPE, is_same<TI, float>::value, true>(
1895 out, outFrameCount, temp, aux, ramp, t);
1896
1897 } else { // constant volume gain
1898 t->resampler->setVolume(t->mVolume[0], t->mVolume[1]);
1899 t->resampler->resample((int32_t*)out, outFrameCount, t->bufferProvider);
1900 }
1901 }
1902
1903 /* This track hook is called to mix a track, when no resampling is required.
1904 * The input buffer should be present in t->in.
1905 *
1906 * MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
1907 * TO: int32_t (Q4.27) or float
1908 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
1909 * TA: int32_t (Q4.27)
1910 */
1911 template <int MIXTYPE, typename TO, typename TI, typename TA>
track__NoResample(track_t * t,TO * out,size_t frameCount,TO * temp __unused,TA * aux)1912 void AudioMixer::track__NoResample(track_t* t, TO* out, size_t frameCount,
1913 TO* temp __unused, TA* aux)
1914 {
1915 ALOGVV("track__NoResample\n");
1916 const TI *in = static_cast<const TI *>(t->in);
1917
1918 volumeMix<MIXTYPE, is_same<TI, float>::value, true>(
1919 out, frameCount, in, aux, t->needsRamp(), t);
1920
1921 // MIXTYPE_MONOEXPAND reads a single input channel and expands to NCHAN output channels.
1922 // MIXTYPE_MULTI reads NCHAN input channels and places to NCHAN output channels.
1923 in += (MIXTYPE == MIXTYPE_MONOEXPAND) ? frameCount : frameCount * t->mMixerChannelCount;
1924 t->in = in;
1925 }
1926
1927 /* The Mixer engine generates either int32_t (Q4_27) or float data.
1928 * We use this function to convert the engine buffers
1929 * to the desired mixer output format, either int16_t (Q.15) or float.
1930 */
convertMixerFormat(void * out,audio_format_t mixerOutFormat,void * in,audio_format_t mixerInFormat,size_t sampleCount)1931 void AudioMixer::convertMixerFormat(void *out, audio_format_t mixerOutFormat,
1932 void *in, audio_format_t mixerInFormat, size_t sampleCount)
1933 {
1934 switch (mixerInFormat) {
1935 case AUDIO_FORMAT_PCM_FLOAT:
1936 switch (mixerOutFormat) {
1937 case AUDIO_FORMAT_PCM_FLOAT:
1938 memcpy(out, in, sampleCount * sizeof(float)); // MEMCPY. TODO optimize out
1939 break;
1940 case AUDIO_FORMAT_PCM_16_BIT:
1941 memcpy_to_i16_from_float((int16_t*)out, (float*)in, sampleCount);
1942 break;
1943 default:
1944 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat);
1945 break;
1946 }
1947 break;
1948 case AUDIO_FORMAT_PCM_16_BIT:
1949 switch (mixerOutFormat) {
1950 case AUDIO_FORMAT_PCM_FLOAT:
1951 memcpy_to_float_from_q4_27((float*)out, (int32_t*)in, sampleCount);
1952 break;
1953 case AUDIO_FORMAT_PCM_16_BIT:
1954 // two int16_t are produced per iteration
1955 ditherAndClamp((int32_t*)out, (int32_t*)in, sampleCount >> 1);
1956 break;
1957 default:
1958 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat);
1959 break;
1960 }
1961 break;
1962 default:
1963 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
1964 break;
1965 }
1966 }
1967
1968 /* Returns the proper track hook to use for mixing the track into the output buffer.
1969 */
getTrackHook(int trackType,uint32_t channelCount,audio_format_t mixerInFormat,audio_format_t mixerOutFormat __unused)1970 AudioMixer::hook_t AudioMixer::getTrackHook(int trackType, uint32_t channelCount,
1971 audio_format_t mixerInFormat, audio_format_t mixerOutFormat __unused)
1972 {
1973 if (!kUseNewMixer && channelCount == FCC_2 && mixerInFormat == AUDIO_FORMAT_PCM_16_BIT) {
1974 switch (trackType) {
1975 case TRACKTYPE_NOP:
1976 return track__nop;
1977 case TRACKTYPE_RESAMPLE:
1978 return track__genericResample;
1979 case TRACKTYPE_NORESAMPLEMONO:
1980 return track__16BitsMono;
1981 case TRACKTYPE_NORESAMPLE:
1982 return track__16BitsStereo;
1983 default:
1984 LOG_ALWAYS_FATAL("bad trackType: %d", trackType);
1985 break;
1986 }
1987 }
1988 LOG_ALWAYS_FATAL_IF(channelCount > MAX_NUM_CHANNELS);
1989 switch (trackType) {
1990 case TRACKTYPE_NOP:
1991 return track__nop;
1992 case TRACKTYPE_RESAMPLE:
1993 switch (mixerInFormat) {
1994 case AUDIO_FORMAT_PCM_FLOAT:
1995 return (AudioMixer::hook_t)
1996 track__Resample<MIXTYPE_MULTI, float /*TO*/, float /*TI*/, int32_t /*TA*/>;
1997 case AUDIO_FORMAT_PCM_16_BIT:
1998 return (AudioMixer::hook_t)\
1999 track__Resample<MIXTYPE_MULTI, int32_t, int16_t, int32_t>;
2000 default:
2001 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
2002 break;
2003 }
2004 break;
2005 case TRACKTYPE_NORESAMPLEMONO:
2006 switch (mixerInFormat) {
2007 case AUDIO_FORMAT_PCM_FLOAT:
2008 return (AudioMixer::hook_t)
2009 track__NoResample<MIXTYPE_MONOEXPAND, float, float, int32_t>;
2010 case AUDIO_FORMAT_PCM_16_BIT:
2011 return (AudioMixer::hook_t)
2012 track__NoResample<MIXTYPE_MONOEXPAND, int32_t, int16_t, int32_t>;
2013 default:
2014 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
2015 break;
2016 }
2017 break;
2018 case TRACKTYPE_NORESAMPLE:
2019 switch (mixerInFormat) {
2020 case AUDIO_FORMAT_PCM_FLOAT:
2021 return (AudioMixer::hook_t)
2022 track__NoResample<MIXTYPE_MULTI, float, float, int32_t>;
2023 case AUDIO_FORMAT_PCM_16_BIT:
2024 return (AudioMixer::hook_t)
2025 track__NoResample<MIXTYPE_MULTI, int32_t, int16_t, int32_t>;
2026 default:
2027 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
2028 break;
2029 }
2030 break;
2031 default:
2032 LOG_ALWAYS_FATAL("bad trackType: %d", trackType);
2033 break;
2034 }
2035 return NULL;
2036 }
2037
2038 /* Returns the proper process hook for mixing tracks. Currently works only for
2039 * PROCESSTYPE_NORESAMPLEONETRACK, a mix involving one track, no resampling.
2040 *
2041 * TODO: Due to the special mixing considerations of duplicating to
2042 * a stereo output track, the input track cannot be MONO. This should be
2043 * prevented by the caller.
2044 */
getProcessHook(int processType,uint32_t channelCount,audio_format_t mixerInFormat,audio_format_t mixerOutFormat)2045 AudioMixer::process_hook_t AudioMixer::getProcessHook(int processType, uint32_t channelCount,
2046 audio_format_t mixerInFormat, audio_format_t mixerOutFormat)
2047 {
2048 if (processType != PROCESSTYPE_NORESAMPLEONETRACK) { // Only NORESAMPLEONETRACK
2049 LOG_ALWAYS_FATAL("bad processType: %d", processType);
2050 return NULL;
2051 }
2052 if (!kUseNewMixer && channelCount == FCC_2 && mixerInFormat == AUDIO_FORMAT_PCM_16_BIT) {
2053 return process__OneTrack16BitsStereoNoResampling;
2054 }
2055 LOG_ALWAYS_FATAL_IF(channelCount > MAX_NUM_CHANNELS);
2056 switch (mixerInFormat) {
2057 case AUDIO_FORMAT_PCM_FLOAT:
2058 switch (mixerOutFormat) {
2059 case AUDIO_FORMAT_PCM_FLOAT:
2060 return process_NoResampleOneTrack<MIXTYPE_MULTI_SAVEONLY,
2061 float /*TO*/, float /*TI*/, int32_t /*TA*/>;
2062 case AUDIO_FORMAT_PCM_16_BIT:
2063 return process_NoResampleOneTrack<MIXTYPE_MULTI_SAVEONLY,
2064 int16_t, float, int32_t>;
2065 default:
2066 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat);
2067 break;
2068 }
2069 break;
2070 case AUDIO_FORMAT_PCM_16_BIT:
2071 switch (mixerOutFormat) {
2072 case AUDIO_FORMAT_PCM_FLOAT:
2073 return process_NoResampleOneTrack<MIXTYPE_MULTI_SAVEONLY,
2074 float, int16_t, int32_t>;
2075 case AUDIO_FORMAT_PCM_16_BIT:
2076 return process_NoResampleOneTrack<MIXTYPE_MULTI_SAVEONLY,
2077 int16_t, int16_t, int32_t>;
2078 default:
2079 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat);
2080 break;
2081 }
2082 break;
2083 default:
2084 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
2085 break;
2086 }
2087 return NULL;
2088 }
2089
2090 // ----------------------------------------------------------------------------
2091 } // namespace android
2092