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