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