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