blob: a169db9707547d9501e8710e9bbffc732332291d [file] [log] [blame]
Mikhail Naganov7ad7a252019-07-30 14:42:32 -07001/*
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.
52static constexpr int BLOCKSIZE = 16;
53
54namespace android {
55
56// ----------------------------------------------------------------------------
57
58bool 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
72bool AudioMixerBase::isValidChannelMask(audio_channel_mask_t channelMask) const
73{
74 return audio_channel_count_from_out_mask(channelMask) <= MAX_NUM_CHANNELS;
75}
76
77std::shared_ptr<AudioMixerBase::TrackBase> AudioMixerBase::preCreateTrack()
78{
79 return std::make_shared<TrackBase>();
80}
81
82status_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
160bool 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
184void 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
195void 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
207void 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 */
244static 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
345void 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
471bool 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 */
520void 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
572void 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
583size_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
592std::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
601void 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;
Judy Hsiao19e533c2019-08-14 16:52:51 +0800646 if ((n & NEEDS_CHANNEL_COUNT__MASK) >= NEEDS_CHANNEL_2
647 && t->useStereoVolume()) {
648 t->hook = TrackBase::getTrackHook(
649 TRACKTYPE_RESAMPLESTEREO, t->mMixerChannelCount,
650 t->mMixerInFormat, t->mMixerFormat);
651 } else {
652 t->hook = TrackBase::getTrackHook(
653 TRACKTYPE_RESAMPLE, t->mMixerChannelCount,
654 t->mMixerInFormat, t->mMixerFormat);
655 }
Mikhail Naganov7ad7a252019-07-30 14:42:32 -0700656 ALOGV_IF((n & NEEDS_CHANNEL_COUNT__MASK) > NEEDS_CHANNEL_2,
657 "Track %d needs downmix + resample", name);
658 } else {
659 if ((n & NEEDS_CHANNEL_COUNT__MASK) == NEEDS_CHANNEL_1){
660 t->hook = TrackBase::getTrackHook(
661 (t->mMixerChannelMask == AUDIO_CHANNEL_OUT_STEREO // TODO: MONO_HACK
662 && t->channelMask == AUDIO_CHANNEL_OUT_MONO)
663 ? TRACKTYPE_NORESAMPLEMONO : TRACKTYPE_NORESAMPLE,
664 t->mMixerChannelCount,
665 t->mMixerInFormat, t->mMixerFormat);
666 all16BitsStereoNoResample = false;
667 }
668 if ((n & NEEDS_CHANNEL_COUNT__MASK) >= NEEDS_CHANNEL_2){
Judy Hsiao19e533c2019-08-14 16:52:51 +0800669 t->hook = TrackBase::getTrackHook(
670 t->useStereoVolume() ? TRACKTYPE_NORESAMPLESTEREO
671 : TRACKTYPE_NORESAMPLE,
672 t->mMixerChannelCount, t->mMixerInFormat,
673 t->mMixerFormat);
Mikhail Naganov7ad7a252019-07-30 14:42:32 -0700674 ALOGV_IF((n & NEEDS_CHANNEL_COUNT__MASK) > NEEDS_CHANNEL_2,
675 "Track %d needs downmix", name);
676 }
677 }
678 }
679 }
680
681 // select the processing hooks
682 mHook = &AudioMixerBase::process__nop;
683 if (mEnabled.size() > 0) {
684 if (resampling) {
685 if (mOutputTemp.get() == nullptr) {
686 mOutputTemp.reset(new int32_t[MAX_NUM_CHANNELS * mFrameCount]);
687 }
688 if (mResampleTemp.get() == nullptr) {
689 mResampleTemp.reset(new int32_t[MAX_NUM_CHANNELS * mFrameCount]);
690 }
691 mHook = &AudioMixerBase::process__genericResampling;
692 } else {
693 // we keep temp arrays around.
694 mHook = &AudioMixerBase::process__genericNoResampling;
695 if (all16BitsStereoNoResample && !volumeRamp) {
696 if (mEnabled.size() == 1) {
697 const std::shared_ptr<TrackBase> &t = mTracks[mEnabled[0]];
698 if ((t->needs & NEEDS_MUTE) == 0) {
699 // The check prevents a muted track from acquiring a process hook.
700 //
701 // This is dangerous if the track is MONO as that requires
702 // special case handling due to implicit channel duplication.
703 // Stereo or Multichannel should actually be fine here.
704 mHook = getProcessHook(PROCESSTYPE_NORESAMPLEONETRACK,
Judy Hsiao19e533c2019-08-14 16:52:51 +0800705 t->mMixerChannelCount, t->mMixerInFormat, t->mMixerFormat,
706 t->useStereoVolume());
Mikhail Naganov7ad7a252019-07-30 14:42:32 -0700707 }
708 }
709 }
710 }
711 }
712
713 ALOGV("mixer configuration change: %zu "
714 "all16BitsStereoNoResample=%d, resampling=%d, volumeRamp=%d",
715 mEnabled.size(), all16BitsStereoNoResample, resampling, volumeRamp);
716
717 process();
718
719 // Now that the volume ramp has been done, set optimal state and
720 // track hooks for subsequent mixer process
721 if (mEnabled.size() > 0) {
722 bool allMuted = true;
723
724 for (const int name : mEnabled) {
725 const std::shared_ptr<TrackBase> &t = mTracks[name];
726 if (!t->doesResample() && t->volumeRL == 0) {
727 t->needs |= NEEDS_MUTE;
728 t->hook = &TrackBase::track__nop;
729 } else {
730 allMuted = false;
731 }
732 }
733 if (allMuted) {
734 mHook = &AudioMixerBase::process__nop;
735 } else if (all16BitsStereoNoResample) {
736 if (mEnabled.size() == 1) {
737 //const int i = 31 - __builtin_clz(enabledTracks);
738 const std::shared_ptr<TrackBase> &t = mTracks[mEnabled[0]];
739 // Muted single tracks handled by allMuted above.
740 mHook = getProcessHook(PROCESSTYPE_NORESAMPLEONETRACK,
Judy Hsiao19e533c2019-08-14 16:52:51 +0800741 t->mMixerChannelCount, t->mMixerInFormat, t->mMixerFormat,
742 t->useStereoVolume());
Mikhail Naganov7ad7a252019-07-30 14:42:32 -0700743 }
744 }
745 }
746}
747
748void AudioMixerBase::TrackBase::track__genericResample(
749 int32_t* out, size_t outFrameCount, int32_t* temp, int32_t* aux)
750{
751 ALOGVV("track__genericResample\n");
752 mResampler->setSampleRate(sampleRate);
753
754 // ramp gain - resample to temp buffer and scale/mix in 2nd step
755 if (aux != NULL) {
756 // always resample with unity gain when sending to auxiliary buffer to be able
757 // to apply send level after resampling
758 mResampler->setVolume(UNITY_GAIN_FLOAT, UNITY_GAIN_FLOAT);
759 memset(temp, 0, outFrameCount * mMixerChannelCount * sizeof(int32_t));
760 mResampler->resample(temp, outFrameCount, bufferProvider);
761 if (CC_UNLIKELY(volumeInc[0]|volumeInc[1]|auxInc)) {
762 volumeRampStereo(out, outFrameCount, temp, aux);
763 } else {
764 volumeStereo(out, outFrameCount, temp, aux);
765 }
766 } else {
767 if (CC_UNLIKELY(volumeInc[0]|volumeInc[1])) {
768 mResampler->setVolume(UNITY_GAIN_FLOAT, UNITY_GAIN_FLOAT);
769 memset(temp, 0, outFrameCount * MAX_NUM_CHANNELS * sizeof(int32_t));
770 mResampler->resample(temp, outFrameCount, bufferProvider);
771 volumeRampStereo(out, outFrameCount, temp, aux);
772 }
773
774 // constant gain
775 else {
776 mResampler->setVolume(mVolume[0], mVolume[1]);
777 mResampler->resample(out, outFrameCount, bufferProvider);
778 }
779 }
780}
781
782void AudioMixerBase::TrackBase::track__nop(int32_t* out __unused,
783 size_t outFrameCount __unused, int32_t* temp __unused, int32_t* aux __unused)
784{
785}
786
787void AudioMixerBase::TrackBase::volumeRampStereo(
788 int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux)
789{
790 int32_t vl = prevVolume[0];
791 int32_t vr = prevVolume[1];
792 const int32_t vlInc = volumeInc[0];
793 const int32_t vrInc = volumeInc[1];
794
795 //ALOGD("[0] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
796 // t, vlInc/65536.0f, vl/65536.0f, volume[0],
797 // (vl + vlInc*frameCount)/65536.0f, frameCount);
798
799 // ramp volume
800 if (CC_UNLIKELY(aux != NULL)) {
801 int32_t va = prevAuxLevel;
802 const int32_t vaInc = auxInc;
803 int32_t l;
804 int32_t r;
805
806 do {
807 l = (*temp++ >> 12);
808 r = (*temp++ >> 12);
809 *out++ += (vl >> 16) * l;
810 *out++ += (vr >> 16) * r;
811 *aux++ += (va >> 17) * (l + r);
812 vl += vlInc;
813 vr += vrInc;
814 va += vaInc;
815 } while (--frameCount);
816 prevAuxLevel = va;
817 } else {
818 do {
819 *out++ += (vl >> 16) * (*temp++ >> 12);
820 *out++ += (vr >> 16) * (*temp++ >> 12);
821 vl += vlInc;
822 vr += vrInc;
823 } while (--frameCount);
824 }
825 prevVolume[0] = vl;
826 prevVolume[1] = vr;
827 adjustVolumeRamp(aux != NULL);
828}
829
830void AudioMixerBase::TrackBase::volumeStereo(
831 int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux)
832{
833 const int16_t vl = volume[0];
834 const int16_t vr = volume[1];
835
836 if (CC_UNLIKELY(aux != NULL)) {
837 const int16_t va = auxLevel;
838 do {
839 int16_t l = (int16_t)(*temp++ >> 12);
840 int16_t r = (int16_t)(*temp++ >> 12);
841 out[0] = mulAdd(l, vl, out[0]);
842 int16_t a = (int16_t)(((int32_t)l + r) >> 1);
843 out[1] = mulAdd(r, vr, out[1]);
844 out += 2;
845 aux[0] = mulAdd(a, va, aux[0]);
846 aux++;
847 } while (--frameCount);
848 } else {
849 do {
850 int16_t l = (int16_t)(*temp++ >> 12);
851 int16_t r = (int16_t)(*temp++ >> 12);
852 out[0] = mulAdd(l, vl, out[0]);
853 out[1] = mulAdd(r, vr, out[1]);
854 out += 2;
855 } while (--frameCount);
856 }
857}
858
859void AudioMixerBase::TrackBase::track__16BitsStereo(
860 int32_t* out, size_t frameCount, int32_t* temp __unused, int32_t* aux)
861{
862 ALOGVV("track__16BitsStereo\n");
863 const int16_t *in = static_cast<const int16_t *>(mIn);
864
865 if (CC_UNLIKELY(aux != NULL)) {
866 int32_t l;
867 int32_t r;
868 // ramp gain
869 if (CC_UNLIKELY(volumeInc[0]|volumeInc[1]|auxInc)) {
870 int32_t vl = prevVolume[0];
871 int32_t vr = prevVolume[1];
872 int32_t va = prevAuxLevel;
873 const int32_t vlInc = volumeInc[0];
874 const int32_t vrInc = volumeInc[1];
875 const int32_t vaInc = auxInc;
876 // ALOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
877 // t, vlInc/65536.0f, vl/65536.0f, volume[0],
878 // (vl + vlInc*frameCount)/65536.0f, frameCount);
879
880 do {
881 l = (int32_t)*in++;
882 r = (int32_t)*in++;
883 *out++ += (vl >> 16) * l;
884 *out++ += (vr >> 16) * r;
885 *aux++ += (va >> 17) * (l + r);
886 vl += vlInc;
887 vr += vrInc;
888 va += vaInc;
889 } while (--frameCount);
890
891 prevVolume[0] = vl;
892 prevVolume[1] = vr;
893 prevAuxLevel = va;
894 adjustVolumeRamp(true);
895 }
896
897 // constant gain
898 else {
899 const uint32_t vrl = volumeRL;
900 const int16_t va = (int16_t)auxLevel;
901 do {
902 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
903 int16_t a = (int16_t)(((int32_t)in[0] + in[1]) >> 1);
904 in += 2;
905 out[0] = mulAddRL(1, rl, vrl, out[0]);
906 out[1] = mulAddRL(0, rl, vrl, out[1]);
907 out += 2;
908 aux[0] = mulAdd(a, va, aux[0]);
909 aux++;
910 } while (--frameCount);
911 }
912 } else {
913 // ramp gain
914 if (CC_UNLIKELY(volumeInc[0]|volumeInc[1])) {
915 int32_t vl = prevVolume[0];
916 int32_t vr = prevVolume[1];
917 const int32_t vlInc = volumeInc[0];
918 const int32_t vrInc = volumeInc[1];
919
920 // ALOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
921 // t, vlInc/65536.0f, vl/65536.0f, volume[0],
922 // (vl + vlInc*frameCount)/65536.0f, frameCount);
923
924 do {
925 *out++ += (vl >> 16) * (int32_t) *in++;
926 *out++ += (vr >> 16) * (int32_t) *in++;
927 vl += vlInc;
928 vr += vrInc;
929 } while (--frameCount);
930
931 prevVolume[0] = vl;
932 prevVolume[1] = vr;
933 adjustVolumeRamp(false);
934 }
935
936 // constant gain
937 else {
938 const uint32_t vrl = volumeRL;
939 do {
940 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
941 in += 2;
942 out[0] = mulAddRL(1, rl, vrl, out[0]);
943 out[1] = mulAddRL(0, rl, vrl, out[1]);
944 out += 2;
945 } while (--frameCount);
946 }
947 }
948 mIn = in;
949}
950
951void AudioMixerBase::TrackBase::track__16BitsMono(
952 int32_t* out, size_t frameCount, int32_t* temp __unused, int32_t* aux)
953{
954 ALOGVV("track__16BitsMono\n");
955 const int16_t *in = static_cast<int16_t const *>(mIn);
956
957 if (CC_UNLIKELY(aux != NULL)) {
958 // ramp gain
959 if (CC_UNLIKELY(volumeInc[0]|volumeInc[1]|auxInc)) {
960 int32_t vl = prevVolume[0];
961 int32_t vr = prevVolume[1];
962 int32_t va = prevAuxLevel;
963 const int32_t vlInc = volumeInc[0];
964 const int32_t vrInc = volumeInc[1];
965 const int32_t vaInc = auxInc;
966
967 // ALOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
968 // t, vlInc/65536.0f, vl/65536.0f, volume[0],
969 // (vl + vlInc*frameCount)/65536.0f, frameCount);
970
971 do {
972 int32_t l = *in++;
973 *out++ += (vl >> 16) * l;
974 *out++ += (vr >> 16) * l;
975 *aux++ += (va >> 16) * l;
976 vl += vlInc;
977 vr += vrInc;
978 va += vaInc;
979 } while (--frameCount);
980
981 prevVolume[0] = vl;
982 prevVolume[1] = vr;
983 prevAuxLevel = va;
984 adjustVolumeRamp(true);
985 }
986 // constant gain
987 else {
988 const int16_t vl = volume[0];
989 const int16_t vr = volume[1];
990 const int16_t va = (int16_t)auxLevel;
991 do {
992 int16_t l = *in++;
993 out[0] = mulAdd(l, vl, out[0]);
994 out[1] = mulAdd(l, vr, out[1]);
995 out += 2;
996 aux[0] = mulAdd(l, va, aux[0]);
997 aux++;
998 } while (--frameCount);
999 }
1000 } else {
1001 // ramp gain
1002 if (CC_UNLIKELY(volumeInc[0]|volumeInc[1])) {
1003 int32_t vl = prevVolume[0];
1004 int32_t vr = prevVolume[1];
1005 const int32_t vlInc = volumeInc[0];
1006 const int32_t vrInc = volumeInc[1];
1007
1008 // ALOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
1009 // t, vlInc/65536.0f, vl/65536.0f, volume[0],
1010 // (vl + vlInc*frameCount)/65536.0f, frameCount);
1011
1012 do {
1013 int32_t l = *in++;
1014 *out++ += (vl >> 16) * l;
1015 *out++ += (vr >> 16) * l;
1016 vl += vlInc;
1017 vr += vrInc;
1018 } while (--frameCount);
1019
1020 prevVolume[0] = vl;
1021 prevVolume[1] = vr;
1022 adjustVolumeRamp(false);
1023 }
1024 // constant gain
1025 else {
1026 const int16_t vl = volume[0];
1027 const int16_t vr = volume[1];
1028 do {
1029 int16_t l = *in++;
1030 out[0] = mulAdd(l, vl, out[0]);
1031 out[1] = mulAdd(l, vr, out[1]);
1032 out += 2;
1033 } while (--frameCount);
1034 }
1035 }
1036 mIn = in;
1037}
1038
1039// no-op case
1040void AudioMixerBase::process__nop()
1041{
1042 ALOGVV("process__nop\n");
1043
1044 for (const auto &pair : mGroups) {
1045 // process by group of tracks with same output buffer to
1046 // avoid multiple memset() on same buffer
1047 const auto &group = pair.second;
1048
1049 const std::shared_ptr<TrackBase> &t = mTracks[group[0]];
1050 memset(t->mainBuffer, 0,
1051 mFrameCount * audio_bytes_per_frame(t->getMixerChannelCount(), t->mMixerFormat));
1052
1053 // now consume data
1054 for (const int name : group) {
1055 const std::shared_ptr<TrackBase> &t = mTracks[name];
1056 size_t outFrames = mFrameCount;
1057 while (outFrames) {
1058 t->buffer.frameCount = outFrames;
1059 t->bufferProvider->getNextBuffer(&t->buffer);
1060 if (t->buffer.raw == NULL) break;
1061 outFrames -= t->buffer.frameCount;
1062 t->bufferProvider->releaseBuffer(&t->buffer);
1063 }
1064 }
1065 }
1066}
1067
1068// generic code without resampling
1069void AudioMixerBase::process__genericNoResampling()
1070{
1071 ALOGVV("process__genericNoResampling\n");
1072 int32_t outTemp[BLOCKSIZE * MAX_NUM_CHANNELS] __attribute__((aligned(32)));
1073
1074 for (const auto &pair : mGroups) {
1075 // process by group of tracks with same output main buffer to
1076 // avoid multiple memset() on same buffer
1077 const auto &group = pair.second;
1078
1079 // acquire buffer
1080 for (const int name : group) {
1081 const std::shared_ptr<TrackBase> &t = mTracks[name];
1082 t->buffer.frameCount = mFrameCount;
1083 t->bufferProvider->getNextBuffer(&t->buffer);
1084 t->frameCount = t->buffer.frameCount;
1085 t->mIn = t->buffer.raw;
1086 }
1087
1088 int32_t *out = (int *)pair.first;
1089 size_t numFrames = 0;
1090 do {
1091 const size_t frameCount = std::min((size_t)BLOCKSIZE, mFrameCount - numFrames);
1092 memset(outTemp, 0, sizeof(outTemp));
1093 for (const int name : group) {
1094 const std::shared_ptr<TrackBase> &t = mTracks[name];
1095 int32_t *aux = NULL;
1096 if (CC_UNLIKELY(t->needs & NEEDS_AUX)) {
1097 aux = t->auxBuffer + numFrames;
1098 }
1099 for (int outFrames = frameCount; outFrames > 0; ) {
1100 // t->in == nullptr can happen if the track was flushed just after having
1101 // been enabled for mixing.
1102 if (t->mIn == nullptr) {
1103 break;
1104 }
1105 size_t inFrames = (t->frameCount > outFrames)?outFrames:t->frameCount;
1106 if (inFrames > 0) {
1107 (t.get()->*t->hook)(
1108 outTemp + (frameCount - outFrames) * t->mMixerChannelCount,
1109 inFrames, mResampleTemp.get() /* naked ptr */, aux);
1110 t->frameCount -= inFrames;
1111 outFrames -= inFrames;
1112 if (CC_UNLIKELY(aux != NULL)) {
1113 aux += inFrames;
1114 }
1115 }
1116 if (t->frameCount == 0 && outFrames) {
1117 t->bufferProvider->releaseBuffer(&t->buffer);
1118 t->buffer.frameCount = (mFrameCount - numFrames) -
1119 (frameCount - outFrames);
1120 t->bufferProvider->getNextBuffer(&t->buffer);
1121 t->mIn = t->buffer.raw;
1122 if (t->mIn == nullptr) {
1123 break;
1124 }
1125 t->frameCount = t->buffer.frameCount;
1126 }
1127 }
1128 }
1129
1130 const std::shared_ptr<TrackBase> &t1 = mTracks[group[0]];
1131 convertMixerFormat(out, t1->mMixerFormat, outTemp, t1->mMixerInFormat,
1132 frameCount * t1->mMixerChannelCount);
1133 // TODO: fix ugly casting due to choice of out pointer type
1134 out = reinterpret_cast<int32_t*>((uint8_t*)out
1135 + frameCount * t1->mMixerChannelCount
1136 * audio_bytes_per_sample(t1->mMixerFormat));
1137 numFrames += frameCount;
1138 } while (numFrames < mFrameCount);
1139
1140 // release each track's buffer
1141 for (const int name : group) {
1142 const std::shared_ptr<TrackBase> &t = mTracks[name];
1143 t->bufferProvider->releaseBuffer(&t->buffer);
1144 }
1145 }
1146}
1147
1148// generic code with resampling
1149void AudioMixerBase::process__genericResampling()
1150{
1151 ALOGVV("process__genericResampling\n");
1152 int32_t * const outTemp = mOutputTemp.get(); // naked ptr
1153 size_t numFrames = mFrameCount;
1154
1155 for (const auto &pair : mGroups) {
1156 const auto &group = pair.second;
1157 const std::shared_ptr<TrackBase> &t1 = mTracks[group[0]];
1158
1159 // clear temp buffer
1160 memset(outTemp, 0, sizeof(*outTemp) * t1->mMixerChannelCount * mFrameCount);
1161 for (const int name : group) {
1162 const std::shared_ptr<TrackBase> &t = mTracks[name];
1163 int32_t *aux = NULL;
1164 if (CC_UNLIKELY(t->needs & NEEDS_AUX)) {
1165 aux = t->auxBuffer;
1166 }
1167
1168 // this is a little goofy, on the resampling case we don't
1169 // acquire/release the buffers because it's done by
1170 // the resampler.
1171 if (t->needs & NEEDS_RESAMPLE) {
1172 (t.get()->*t->hook)(outTemp, numFrames, mResampleTemp.get() /* naked ptr */, aux);
1173 } else {
1174
1175 size_t outFrames = 0;
1176
1177 while (outFrames < numFrames) {
1178 t->buffer.frameCount = numFrames - outFrames;
1179 t->bufferProvider->getNextBuffer(&t->buffer);
1180 t->mIn = t->buffer.raw;
1181 // t->mIn == nullptr can happen if the track was flushed just after having
1182 // been enabled for mixing.
1183 if (t->mIn == nullptr) break;
1184
1185 (t.get()->*t->hook)(
1186 outTemp + outFrames * t->mMixerChannelCount, t->buffer.frameCount,
1187 mResampleTemp.get() /* naked ptr */,
1188 aux != nullptr ? aux + outFrames : nullptr);
1189 outFrames += t->buffer.frameCount;
1190
1191 t->bufferProvider->releaseBuffer(&t->buffer);
1192 }
1193 }
1194 }
1195 convertMixerFormat(t1->mainBuffer, t1->mMixerFormat,
1196 outTemp, t1->mMixerInFormat, numFrames * t1->mMixerChannelCount);
1197 }
1198}
1199
1200// one track, 16 bits stereo without resampling is the most common case
1201void AudioMixerBase::process__oneTrack16BitsStereoNoResampling()
1202{
1203 ALOGVV("process__oneTrack16BitsStereoNoResampling\n");
1204 LOG_ALWAYS_FATAL_IF(mEnabled.size() != 0,
1205 "%zu != 1 tracks enabled", mEnabled.size());
1206 const int name = mEnabled[0];
1207 const std::shared_ptr<TrackBase> &t = mTracks[name];
1208
1209 AudioBufferProvider::Buffer& b(t->buffer);
1210
1211 int32_t* out = t->mainBuffer;
1212 float *fout = reinterpret_cast<float*>(out);
1213 size_t numFrames = mFrameCount;
1214
1215 const int16_t vl = t->volume[0];
1216 const int16_t vr = t->volume[1];
1217 const uint32_t vrl = t->volumeRL;
1218 while (numFrames) {
1219 b.frameCount = numFrames;
1220 t->bufferProvider->getNextBuffer(&b);
1221 const int16_t *in = b.i16;
1222
1223 // in == NULL can happen if the track was flushed just after having
1224 // been enabled for mixing.
1225 if (in == NULL || (((uintptr_t)in) & 3)) {
1226 if ( AUDIO_FORMAT_PCM_FLOAT == t->mMixerFormat ) {
1227 memset((char*)fout, 0, numFrames
1228 * t->mMixerChannelCount * audio_bytes_per_sample(t->mMixerFormat));
1229 } else {
1230 memset((char*)out, 0, numFrames
1231 * t->mMixerChannelCount * audio_bytes_per_sample(t->mMixerFormat));
1232 }
1233 ALOGE_IF((((uintptr_t)in) & 3),
1234 "process__oneTrack16BitsStereoNoResampling: misaligned buffer"
1235 " %p track %d, channels %d, needs %08x, volume %08x vfl %f vfr %f",
1236 in, name, t->channelCount, t->needs, vrl, t->mVolume[0], t->mVolume[1]);
1237 return;
1238 }
1239 size_t outFrames = b.frameCount;
1240
1241 switch (t->mMixerFormat) {
1242 case AUDIO_FORMAT_PCM_FLOAT:
1243 do {
1244 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
1245 in += 2;
1246 int32_t l = mulRL(1, rl, vrl);
1247 int32_t r = mulRL(0, rl, vrl);
1248 *fout++ = float_from_q4_27(l);
1249 *fout++ = float_from_q4_27(r);
1250 // Note: In case of later int16_t sink output,
1251 // conversion and clamping is done by memcpy_to_i16_from_float().
1252 } while (--outFrames);
1253 break;
1254 case AUDIO_FORMAT_PCM_16_BIT:
1255 if (CC_UNLIKELY(uint32_t(vl) > UNITY_GAIN_INT || uint32_t(vr) > UNITY_GAIN_INT)) {
1256 // volume is boosted, so we might need to clamp even though
1257 // we process only one track.
1258 do {
1259 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
1260 in += 2;
1261 int32_t l = mulRL(1, rl, vrl) >> 12;
1262 int32_t r = mulRL(0, rl, vrl) >> 12;
1263 // clamping...
1264 l = clamp16(l);
1265 r = clamp16(r);
1266 *out++ = (r<<16) | (l & 0xFFFF);
1267 } while (--outFrames);
1268 } else {
1269 do {
1270 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
1271 in += 2;
1272 int32_t l = mulRL(1, rl, vrl) >> 12;
1273 int32_t r = mulRL(0, rl, vrl) >> 12;
1274 *out++ = (r<<16) | (l & 0xFFFF);
1275 } while (--outFrames);
1276 }
1277 break;
1278 default:
1279 LOG_ALWAYS_FATAL("bad mixer format: %d", t->mMixerFormat);
1280 }
1281 numFrames -= b.frameCount;
1282 t->bufferProvider->releaseBuffer(&b);
1283 }
1284}
1285
1286/* TODO: consider whether this level of optimization is necessary.
1287 * Perhaps just stick with a single for loop.
1288 */
1289
1290// Needs to derive a compile time constant (constexpr). Could be targeted to go
1291// to a MONOVOL mixtype based on MAX_NUM_VOLUMES, but that's an unnecessary complication.
1292#define MIXTYPE_MONOVOL(mixtype) ((mixtype) == MIXTYPE_MULTI ? MIXTYPE_MULTI_MONOVOL : \
1293 (mixtype) == MIXTYPE_MULTI_SAVEONLY ? MIXTYPE_MULTI_SAVEONLY_MONOVOL : (mixtype))
1294
1295/* MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
1296 * TO: int32_t (Q4.27) or float
1297 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
1298 * TA: int32_t (Q4.27) or float
1299 */
1300template <int MIXTYPE,
1301 typename TO, typename TI, typename TV, typename TA, typename TAV>
1302static void volumeRampMulti(uint32_t channels, TO* out, size_t frameCount,
1303 const TI* in, TA* aux, TV *vol, const TV *volinc, TAV *vola, TAV volainc)
1304{
1305 switch (channels) {
1306 case 1:
1307 volumeRampMulti<MIXTYPE, 1>(out, frameCount, in, aux, vol, volinc, vola, volainc);
1308 break;
1309 case 2:
1310 volumeRampMulti<MIXTYPE, 2>(out, frameCount, in, aux, vol, volinc, vola, volainc);
1311 break;
1312 case 3:
1313 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 3>(out,
1314 frameCount, in, aux, vol, volinc, vola, volainc);
1315 break;
1316 case 4:
1317 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 4>(out,
1318 frameCount, in, aux, vol, volinc, vola, volainc);
1319 break;
1320 case 5:
1321 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 5>(out,
1322 frameCount, in, aux, vol, volinc, vola, volainc);
1323 break;
1324 case 6:
1325 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 6>(out,
1326 frameCount, in, aux, vol, volinc, vola, volainc);
1327 break;
1328 case 7:
1329 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 7>(out,
1330 frameCount, in, aux, vol, volinc, vola, volainc);
1331 break;
1332 case 8:
1333 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 8>(out,
1334 frameCount, in, aux, vol, volinc, vola, volainc);
1335 break;
1336 }
1337}
1338
1339/* MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
1340 * TO: int32_t (Q4.27) or float
1341 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
1342 * TA: int32_t (Q4.27) or float
1343 */
1344template <int MIXTYPE,
1345 typename TO, typename TI, typename TV, typename TA, typename TAV>
1346static void volumeMulti(uint32_t channels, TO* out, size_t frameCount,
1347 const TI* in, TA* aux, const TV *vol, TAV vola)
1348{
1349 switch (channels) {
1350 case 1:
1351 volumeMulti<MIXTYPE, 1>(out, frameCount, in, aux, vol, vola);
1352 break;
1353 case 2:
1354 volumeMulti<MIXTYPE, 2>(out, frameCount, in, aux, vol, vola);
1355 break;
1356 case 3:
1357 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 3>(out, frameCount, in, aux, vol, vola);
1358 break;
1359 case 4:
1360 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 4>(out, frameCount, in, aux, vol, vola);
1361 break;
1362 case 5:
1363 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 5>(out, frameCount, in, aux, vol, vola);
1364 break;
1365 case 6:
1366 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 6>(out, frameCount, in, aux, vol, vola);
1367 break;
1368 case 7:
1369 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 7>(out, frameCount, in, aux, vol, vola);
1370 break;
1371 case 8:
1372 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 8>(out, frameCount, in, aux, vol, vola);
1373 break;
1374 }
1375}
1376
1377/* MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
1378 * USEFLOATVOL (set to true if float volume is used)
1379 * ADJUSTVOL (set to true if volume ramp parameters needs adjustment afterwards)
1380 * TO: int32_t (Q4.27) or float
1381 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
1382 * TA: int32_t (Q4.27) or float
1383 */
1384template <int MIXTYPE, bool USEFLOATVOL, bool ADJUSTVOL,
1385 typename TO, typename TI, typename TA>
1386void AudioMixerBase::TrackBase::volumeMix(TO *out, size_t outFrames,
1387 const TI *in, TA *aux, bool ramp)
1388{
1389 if (USEFLOATVOL) {
1390 if (ramp) {
1391 volumeRampMulti<MIXTYPE>(mMixerChannelCount, out, outFrames, in, aux,
1392 mPrevVolume, mVolumeInc,
1393#ifdef FLOAT_AUX
1394 &mPrevAuxLevel, mAuxInc
1395#else
1396 &prevAuxLevel, auxInc
1397#endif
1398 );
1399 if (ADJUSTVOL) {
1400 adjustVolumeRamp(aux != NULL, true);
1401 }
1402 } else {
1403 volumeMulti<MIXTYPE>(mMixerChannelCount, out, outFrames, in, aux,
1404 mVolume,
1405#ifdef FLOAT_AUX
1406 mAuxLevel
1407#else
1408 auxLevel
1409#endif
1410 );
1411 }
1412 } else {
1413 if (ramp) {
1414 volumeRampMulti<MIXTYPE>(mMixerChannelCount, out, outFrames, in, aux,
1415 prevVolume, volumeInc, &prevAuxLevel, auxInc);
1416 if (ADJUSTVOL) {
1417 adjustVolumeRamp(aux != NULL);
1418 }
1419 } else {
1420 volumeMulti<MIXTYPE>(mMixerChannelCount, out, outFrames, in, aux,
1421 volume, auxLevel);
1422 }
1423 }
1424}
1425
1426/* This process hook is called when there is a single track without
1427 * aux buffer, volume ramp, or resampling.
1428 * TODO: Update the hook selection: this can properly handle aux and ramp.
1429 *
1430 * MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
1431 * TO: int32_t (Q4.27) or float
1432 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
1433 * TA: int32_t (Q4.27)
1434 */
1435template <int MIXTYPE, typename TO, typename TI, typename TA>
1436void AudioMixerBase::process__noResampleOneTrack()
1437{
1438 ALOGVV("process__noResampleOneTrack\n");
1439 LOG_ALWAYS_FATAL_IF(mEnabled.size() != 1,
1440 "%zu != 1 tracks enabled", mEnabled.size());
1441 const std::shared_ptr<TrackBase> &t = mTracks[mEnabled[0]];
1442 const uint32_t channels = t->mMixerChannelCount;
1443 TO* out = reinterpret_cast<TO*>(t->mainBuffer);
1444 TA* aux = reinterpret_cast<TA*>(t->auxBuffer);
1445 const bool ramp = t->needsRamp();
1446
1447 for (size_t numFrames = mFrameCount; numFrames > 0; ) {
1448 AudioBufferProvider::Buffer& b(t->buffer);
1449 // get input buffer
1450 b.frameCount = numFrames;
1451 t->bufferProvider->getNextBuffer(&b);
1452 const TI *in = reinterpret_cast<TI*>(b.raw);
1453
1454 // in == NULL can happen if the track was flushed just after having
1455 // been enabled for mixing.
1456 if (in == NULL || (((uintptr_t)in) & 3)) {
1457 memset(out, 0, numFrames
1458 * channels * audio_bytes_per_sample(t->mMixerFormat));
1459 ALOGE_IF((((uintptr_t)in) & 3), "process__noResampleOneTrack: bus error: "
1460 "buffer %p track %p, channels %d, needs %#x",
1461 in, &t, t->channelCount, t->needs);
1462 return;
1463 }
1464
1465 const size_t outFrames = b.frameCount;
Judy Hsiao19e533c2019-08-14 16:52:51 +08001466 t->volumeMix<MIXTYPE, std::is_same_v<TI, float> /* USEFLOATVOL */, false /* ADJUSTVOL */> (
Mikhail Naganov7ad7a252019-07-30 14:42:32 -07001467 out, outFrames, in, aux, ramp);
1468
1469 out += outFrames * channels;
1470 if (aux != NULL) {
1471 aux += outFrames;
1472 }
1473 numFrames -= b.frameCount;
1474
1475 // release buffer
1476 t->bufferProvider->releaseBuffer(&b);
1477 }
1478 if (ramp) {
Judy Hsiao19e533c2019-08-14 16:52:51 +08001479 t->adjustVolumeRamp(aux != NULL, std::is_same_v<TI, float>);
Mikhail Naganov7ad7a252019-07-30 14:42:32 -07001480 }
1481}
1482
1483/* This track hook is called to do resampling then mixing,
1484 * pulling from the track's upstream AudioBufferProvider.
1485 *
1486 * MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
1487 * TO: int32_t (Q4.27) or float
1488 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
1489 * TA: int32_t (Q4.27) or float
1490 */
1491template <int MIXTYPE, typename TO, typename TI, typename TA>
1492void AudioMixerBase::TrackBase::track__Resample(TO* out, size_t outFrameCount, TO* temp, TA* aux)
1493{
1494 ALOGVV("track__Resample\n");
1495 mResampler->setSampleRate(sampleRate);
1496 const bool ramp = needsRamp();
1497 if (ramp || aux != NULL) {
1498 // if ramp: resample with unity gain to temp buffer and scale/mix in 2nd step.
1499 // if aux != NULL: resample with unity gain to temp buffer then apply send level.
1500
1501 mResampler->setVolume(UNITY_GAIN_FLOAT, UNITY_GAIN_FLOAT);
1502 memset(temp, 0, outFrameCount * mMixerChannelCount * sizeof(TO));
1503 mResampler->resample((int32_t*)temp, outFrameCount, bufferProvider);
1504
Judy Hsiao19e533c2019-08-14 16:52:51 +08001505 volumeMix<MIXTYPE, std::is_same_v<TI, float> /* USEFLOATVOL */, true /* ADJUSTVOL */>(
Mikhail Naganov7ad7a252019-07-30 14:42:32 -07001506 out, outFrameCount, temp, aux, ramp);
1507
1508 } else { // constant volume gain
1509 mResampler->setVolume(mVolume[0], mVolume[1]);
1510 mResampler->resample((int32_t*)out, outFrameCount, bufferProvider);
1511 }
1512}
1513
1514/* This track hook is called to mix a track, when no resampling is required.
1515 * The input buffer should be present in in.
1516 *
1517 * MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
1518 * TO: int32_t (Q4.27) or float
1519 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
1520 * TA: int32_t (Q4.27) or float
1521 */
1522template <int MIXTYPE, typename TO, typename TI, typename TA>
1523void AudioMixerBase::TrackBase::track__NoResample(
1524 TO* out, size_t frameCount, TO* temp __unused, TA* aux)
1525{
1526 ALOGVV("track__NoResample\n");
1527 const TI *in = static_cast<const TI *>(mIn);
1528
Judy Hsiao19e533c2019-08-14 16:52:51 +08001529 volumeMix<MIXTYPE, std::is_same_v<TI, float> /* USEFLOATVOL */, true /* ADJUSTVOL */>(
Mikhail Naganov7ad7a252019-07-30 14:42:32 -07001530 out, frameCount, in, aux, needsRamp());
1531
1532 // MIXTYPE_MONOEXPAND reads a single input channel and expands to NCHAN output channels.
1533 // MIXTYPE_MULTI reads NCHAN input channels and places to NCHAN output channels.
1534 in += (MIXTYPE == MIXTYPE_MONOEXPAND) ? frameCount : frameCount * mMixerChannelCount;
1535 mIn = in;
1536}
1537
1538/* The Mixer engine generates either int32_t (Q4_27) or float data.
1539 * We use this function to convert the engine buffers
1540 * to the desired mixer output format, either int16_t (Q.15) or float.
1541 */
1542/* static */
1543void AudioMixerBase::convertMixerFormat(void *out, audio_format_t mixerOutFormat,
1544 void *in, audio_format_t mixerInFormat, size_t sampleCount)
1545{
1546 switch (mixerInFormat) {
1547 case AUDIO_FORMAT_PCM_FLOAT:
1548 switch (mixerOutFormat) {
1549 case AUDIO_FORMAT_PCM_FLOAT:
1550 memcpy(out, in, sampleCount * sizeof(float)); // MEMCPY. TODO optimize out
1551 break;
1552 case AUDIO_FORMAT_PCM_16_BIT:
1553 memcpy_to_i16_from_float((int16_t*)out, (float*)in, sampleCount);
1554 break;
1555 default:
1556 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat);
1557 break;
1558 }
1559 break;
1560 case AUDIO_FORMAT_PCM_16_BIT:
1561 switch (mixerOutFormat) {
1562 case AUDIO_FORMAT_PCM_FLOAT:
1563 memcpy_to_float_from_q4_27((float*)out, (const int32_t*)in, sampleCount);
1564 break;
1565 case AUDIO_FORMAT_PCM_16_BIT:
1566 memcpy_to_i16_from_q4_27((int16_t*)out, (const int32_t*)in, sampleCount);
1567 break;
1568 default:
1569 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat);
1570 break;
1571 }
1572 break;
1573 default:
1574 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
1575 break;
1576 }
1577}
1578
1579/* Returns the proper track hook to use for mixing the track into the output buffer.
1580 */
1581/* static */
1582AudioMixerBase::hook_t AudioMixerBase::TrackBase::getTrackHook(int trackType, uint32_t channelCount,
1583 audio_format_t mixerInFormat, audio_format_t mixerOutFormat __unused)
1584{
1585 if (!kUseNewMixer && channelCount == FCC_2 && mixerInFormat == AUDIO_FORMAT_PCM_16_BIT) {
1586 switch (trackType) {
1587 case TRACKTYPE_NOP:
1588 return &TrackBase::track__nop;
1589 case TRACKTYPE_RESAMPLE:
1590 return &TrackBase::track__genericResample;
1591 case TRACKTYPE_NORESAMPLEMONO:
1592 return &TrackBase::track__16BitsMono;
1593 case TRACKTYPE_NORESAMPLE:
1594 return &TrackBase::track__16BitsStereo;
1595 default:
1596 LOG_ALWAYS_FATAL("bad trackType: %d", trackType);
1597 break;
1598 }
1599 }
1600 LOG_ALWAYS_FATAL_IF(channelCount > MAX_NUM_CHANNELS);
1601 switch (trackType) {
1602 case TRACKTYPE_NOP:
1603 return &TrackBase::track__nop;
1604 case TRACKTYPE_RESAMPLE:
1605 switch (mixerInFormat) {
1606 case AUDIO_FORMAT_PCM_FLOAT:
1607 return (AudioMixerBase::hook_t) &TrackBase::track__Resample<
1608 MIXTYPE_MULTI, float /*TO*/, float /*TI*/, TYPE_AUX>;
1609 case AUDIO_FORMAT_PCM_16_BIT:
1610 return (AudioMixerBase::hook_t) &TrackBase::track__Resample<
1611 MIXTYPE_MULTI, int32_t /*TO*/, int16_t /*TI*/, TYPE_AUX>;
1612 default:
1613 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
1614 break;
1615 }
1616 break;
Judy Hsiao19e533c2019-08-14 16:52:51 +08001617 case TRACKTYPE_RESAMPLESTEREO:
1618 switch (mixerInFormat) {
1619 case AUDIO_FORMAT_PCM_FLOAT:
1620 return (AudioMixerBase::hook_t) &TrackBase::track__Resample<
1621 MIXTYPE_MULTI_STEREOVOL, float /*TO*/, float /*TI*/,
1622 TYPE_AUX>;
1623 case AUDIO_FORMAT_PCM_16_BIT:
1624 return (AudioMixerBase::hook_t) &TrackBase::track__Resample<
1625 MIXTYPE_MULTI_STEREOVOL, int32_t /*TO*/, int16_t /*TI*/,
1626 TYPE_AUX>;
1627 default:
1628 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
1629 break;
1630 }
1631 break;
Mikhail Naganov7ad7a252019-07-30 14:42:32 -07001632 case TRACKTYPE_NORESAMPLEMONO:
1633 switch (mixerInFormat) {
1634 case AUDIO_FORMAT_PCM_FLOAT:
1635 return (AudioMixerBase::hook_t) &TrackBase::track__NoResample<
1636 MIXTYPE_MONOEXPAND, float /*TO*/, float /*TI*/, TYPE_AUX>;
1637 case AUDIO_FORMAT_PCM_16_BIT:
1638 return (AudioMixerBase::hook_t) &TrackBase::track__NoResample<
1639 MIXTYPE_MONOEXPAND, int32_t /*TO*/, int16_t /*TI*/, TYPE_AUX>;
1640 default:
1641 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
1642 break;
1643 }
1644 break;
1645 case TRACKTYPE_NORESAMPLE:
1646 switch (mixerInFormat) {
1647 case AUDIO_FORMAT_PCM_FLOAT:
1648 return (AudioMixerBase::hook_t) &TrackBase::track__NoResample<
1649 MIXTYPE_MULTI, float /*TO*/, float /*TI*/, TYPE_AUX>;
1650 case AUDIO_FORMAT_PCM_16_BIT:
1651 return (AudioMixerBase::hook_t) &TrackBase::track__NoResample<
1652 MIXTYPE_MULTI, int32_t /*TO*/, int16_t /*TI*/, TYPE_AUX>;
1653 default:
1654 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
1655 break;
1656 }
1657 break;
Judy Hsiao19e533c2019-08-14 16:52:51 +08001658 case TRACKTYPE_NORESAMPLESTEREO:
1659 switch (mixerInFormat) {
1660 case AUDIO_FORMAT_PCM_FLOAT:
1661 return (AudioMixerBase::hook_t) &TrackBase::track__NoResample<
1662 MIXTYPE_MULTI_STEREOVOL, float /*TO*/, float /*TI*/,
1663 TYPE_AUX>;
1664 case AUDIO_FORMAT_PCM_16_BIT:
1665 return (AudioMixerBase::hook_t) &TrackBase::track__NoResample<
1666 MIXTYPE_MULTI_STEREOVOL, int32_t /*TO*/, int16_t /*TI*/,
1667 TYPE_AUX>;
1668 default:
1669 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
1670 break;
1671 }
1672 break;
Mikhail Naganov7ad7a252019-07-30 14:42:32 -07001673 default:
1674 LOG_ALWAYS_FATAL("bad trackType: %d", trackType);
1675 break;
1676 }
1677 return NULL;
1678}
1679
1680/* Returns the proper process hook for mixing tracks. Currently works only for
1681 * PROCESSTYPE_NORESAMPLEONETRACK, a mix involving one track, no resampling.
1682 *
1683 * TODO: Due to the special mixing considerations of duplicating to
1684 * a stereo output track, the input track cannot be MONO. This should be
1685 * prevented by the caller.
1686 */
1687/* static */
1688AudioMixerBase::process_hook_t AudioMixerBase::getProcessHook(
1689 int processType, uint32_t channelCount,
Judy Hsiao19e533c2019-08-14 16:52:51 +08001690 audio_format_t mixerInFormat, audio_format_t mixerOutFormat,
1691 bool stereoVolume)
Mikhail Naganov7ad7a252019-07-30 14:42:32 -07001692{
1693 if (processType != PROCESSTYPE_NORESAMPLEONETRACK) { // Only NORESAMPLEONETRACK
1694 LOG_ALWAYS_FATAL("bad processType: %d", processType);
1695 return NULL;
1696 }
1697 if (!kUseNewMixer && channelCount == FCC_2 && mixerInFormat == AUDIO_FORMAT_PCM_16_BIT) {
1698 return &AudioMixerBase::process__oneTrack16BitsStereoNoResampling;
1699 }
1700 LOG_ALWAYS_FATAL_IF(channelCount > MAX_NUM_CHANNELS);
Judy Hsiao19e533c2019-08-14 16:52:51 +08001701
1702 if (stereoVolume) { // templated arguments require explicit values.
1703 switch (mixerInFormat) {
Mikhail Naganov7ad7a252019-07-30 14:42:32 -07001704 case AUDIO_FORMAT_PCM_FLOAT:
Judy Hsiao19e533c2019-08-14 16:52:51 +08001705 switch (mixerOutFormat) {
1706 case AUDIO_FORMAT_PCM_FLOAT:
1707 return &AudioMixerBase::process__noResampleOneTrack<
1708 MIXTYPE_MULTI_SAVEONLY_STEREOVOL, float /*TO*/,
1709 float /*TI*/, TYPE_AUX>;
1710 case AUDIO_FORMAT_PCM_16_BIT:
1711 return &AudioMixerBase::process__noResampleOneTrack<
1712 MIXTYPE_MULTI_SAVEONLY_STEREOVOL, int16_t /*TO*/,
1713 float /*TI*/, TYPE_AUX>;
1714 default:
1715 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat);
1716 break;
1717 }
1718 break;
Mikhail Naganov7ad7a252019-07-30 14:42:32 -07001719 case AUDIO_FORMAT_PCM_16_BIT:
Judy Hsiao19e533c2019-08-14 16:52:51 +08001720 switch (mixerOutFormat) {
1721 case AUDIO_FORMAT_PCM_FLOAT:
1722 return &AudioMixerBase::process__noResampleOneTrack<
1723 MIXTYPE_MULTI_SAVEONLY_STEREOVOL, float /*TO*/,
1724 int16_t /*TI*/, TYPE_AUX>;
1725 case AUDIO_FORMAT_PCM_16_BIT:
1726 return &AudioMixerBase::process__noResampleOneTrack<
1727 MIXTYPE_MULTI_SAVEONLY_STEREOVOL, int16_t /*TO*/,
1728 int16_t /*TI*/, TYPE_AUX>;
1729 default:
1730 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat);
1731 break;
1732 }
1733 break;
Mikhail Naganov7ad7a252019-07-30 14:42:32 -07001734 default:
Judy Hsiao19e533c2019-08-14 16:52:51 +08001735 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
Mikhail Naganov7ad7a252019-07-30 14:42:32 -07001736 break;
1737 }
Judy Hsiao19e533c2019-08-14 16:52:51 +08001738 } else {
1739 switch (mixerInFormat) {
1740 case AUDIO_FORMAT_PCM_FLOAT:
1741 switch (mixerOutFormat) {
1742 case AUDIO_FORMAT_PCM_FLOAT:
1743 return &AudioMixerBase::process__noResampleOneTrack<
1744 MIXTYPE_MULTI_SAVEONLY, float /*TO*/,
1745 float /*TI*/, TYPE_AUX>;
1746 case AUDIO_FORMAT_PCM_16_BIT:
1747 return &AudioMixerBase::process__noResampleOneTrack<
1748 MIXTYPE_MULTI_SAVEONLY, int16_t /*TO*/,
1749 float /*TI*/, TYPE_AUX>;
1750 default:
1751 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat);
1752 break;
1753 }
1754 break;
1755 case AUDIO_FORMAT_PCM_16_BIT:
1756 switch (mixerOutFormat) {
1757 case AUDIO_FORMAT_PCM_FLOAT:
1758 return &AudioMixerBase::process__noResampleOneTrack<
1759 MIXTYPE_MULTI_SAVEONLY, float /*TO*/,
1760 int16_t /*TI*/, TYPE_AUX>;
1761 case AUDIO_FORMAT_PCM_16_BIT:
1762 return &AudioMixerBase::process__noResampleOneTrack<
1763 MIXTYPE_MULTI_SAVEONLY, int16_t /*TO*/,
1764 int16_t /*TI*/, TYPE_AUX>;
1765 default:
1766 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat);
1767 break;
1768 }
1769 break;
1770 default:
1771 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
1772 break;
1773 }
Mikhail Naganov7ad7a252019-07-30 14:42:32 -07001774 }
1775 return NULL;
1776}
1777
1778// ----------------------------------------------------------------------------
1779} // namespace android