blob: c2c791f8e8155676f171b7da52f37c5d32770fb1 [file] [log] [blame]
Glenn Kasten99e53b82012-01-19 08:59:58 -08001/*
Mathias Agopian65ab4712010-07-14 17:59:35 -07002**
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"
Glenn Kasten7f5d3352013-02-15 23:55:04 +000019//#define LOG_NDEBUG 0
Mathias Agopian65ab4712010-07-14 17:59:35 -070020
Glenn Kasten153b9fe2013-07-15 11:23:36 -070021#include "Configuration.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070022#include <stdint.h>
23#include <string.h>
24#include <stdlib.h>
Andy Hung5e58b0a2014-06-23 19:07:29 -070025#include <math.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070026#include <sys/types.h>
27
28#include <utils/Errors.h>
29#include <utils/Log.h>
30
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -070031#include <cutils/bitops.h>
Glenn Kastenf6b16782011-12-15 09:51:17 -080032#include <cutils/compiler.h>
Glenn Kasten5798d4e2012-03-08 12:18:35 -080033#include <utils/Debug.h>
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -070034
35#include <system/audio.h>
36
Glenn Kasten3b21c502011-12-15 09:52:39 -080037#include <audio_utils/primitives.h>
Andy Hungef7c7fb2014-05-12 16:51:41 -070038#include <audio_utils/format.h>
John Grossman4ff14ba2012-02-08 16:37:41 -080039#include <common_time/local_clock.h>
40#include <common_time/cc_helper.h>
Andy Hung857d5a22015-03-26 18:46:00 -070041#include <media/AudioResamplerPublic.h>
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -070042
Andy Hung296b7412014-06-17 15:25:47 -070043#include "AudioMixerOps.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070044#include "AudioMixer.h"
45
Andy Hunge93b6b72014-07-17 21:30:53 -070046// The FCC_2 macro refers to the Fixed Channel Count of 2 for the legacy integer mixer.
Andy Hung296b7412014-06-17 15:25:47 -070047#ifndef FCC_2
48#define FCC_2 2
49#endif
50
Andy Hunge93b6b72014-07-17 21:30:53 -070051// Look for MONO_HACK for any Mono hack involving legacy mono channel to
52// stereo channel conversion.
53
Andy Hung296b7412014-06-17 15:25:47 -070054/* VERY_VERY_VERBOSE_LOGGING will show exactly which process hook and track hook is
55 * being used. This is a considerable amount of log spam, so don't enable unless you
56 * are verifying the hook based code.
57 */
58//#define VERY_VERY_VERBOSE_LOGGING
59#ifdef VERY_VERY_VERBOSE_LOGGING
60#define ALOGVV ALOGV
61//define ALOGVV printf // for test-mixer.cpp
62#else
63#define ALOGVV(a...) do { } while (0)
64#endif
65
Andy Hunga08810b2014-07-16 21:53:43 -070066#ifndef ARRAY_SIZE
67#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
68#endif
69
Andy Hung5b8fde72014-09-02 21:14:34 -070070// Set kUseNewMixer to true to use the new mixer engine always. Otherwise the
71// original code will be used for stereo sinks, the new mixer for multichannel.
72static const bool kUseNewMixer = true;
Andy Hung296b7412014-06-17 15:25:47 -070073
74// Set kUseFloat to true to allow floating input into the mixer engine.
75// If kUseNewMixer is false, this is ignored or may be overridden internally
76// because of downmix/upmix support.
77static const bool kUseFloat = true;
78
Andy Hung1b2fdcb2014-07-16 17:44:34 -070079// Set to default copy buffer size in frames for input processing.
80static const size_t kCopyBufferFrameCount = 256;
81
Mathias Agopian65ab4712010-07-14 17:59:35 -070082namespace android {
Mathias Agopian65ab4712010-07-14 17:59:35 -070083
84// ----------------------------------------------------------------------------
Andy Hung1b2fdcb2014-07-16 17:44:34 -070085
86template <typename T>
87T min(const T& a, const T& b)
88{
89 return a < b ? a : b;
90}
91
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -070092// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -070093
Paul Lind3c0a0e82012-08-01 18:49:49 -070094// Ensure mConfiguredNames bitmask is initialized properly on all architectures.
95// The value of 1 << x is undefined in C when x >= 32.
96
Glenn Kasten5c94b6c2012-03-20 17:01:29 -070097AudioMixer::AudioMixer(size_t frameCount, uint32_t sampleRate, uint32_t maxNumTracks)
Paul Lind3c0a0e82012-08-01 18:49:49 -070098 : mTrackNames(0), mConfiguredNames((maxNumTracks >= 32 ? 0 : 1 << maxNumTracks) - 1),
Glenn Kasten7f5d3352013-02-15 23:55:04 +000099 mSampleRate(sampleRate)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700100{
Glenn Kasten5c94b6c2012-03-20 17:01:29 -0700101 ALOG_ASSERT(maxNumTracks <= MAX_NUM_TRACKS, "maxNumTracks %u > MAX_NUM_TRACKS %u",
102 maxNumTracks, MAX_NUM_TRACKS);
103
Glenn Kasten599fabc2012-03-08 12:33:37 -0800104 // AudioMixer is not yet capable of more than 32 active track inputs
105 ALOG_ASSERT(32 >= MAX_NUM_TRACKS, "bad MAX_NUM_TRACKS %d", MAX_NUM_TRACKS);
106
Glenn Kasten52008f82012-03-18 09:34:41 -0700107 pthread_once(&sOnceControl, &sInitRoutine);
108
Mathias Agopian65ab4712010-07-14 17:59:35 -0700109 mState.enabledTracks= 0;
110 mState.needsChanged = 0;
111 mState.frameCount = frameCount;
Glenn Kasten84afa3b2012-01-25 15:28:08 -0800112 mState.hook = process__nop;
Glenn Kastene0feee32011-12-13 11:53:26 -0800113 mState.outputTemp = NULL;
114 mState.resampleTemp = NULL;
Glenn Kastenab7d72f2013-02-27 09:05:28 -0800115 mState.mLog = &mDummyLog;
Glenn Kasten84afa3b2012-01-25 15:28:08 -0800116 // mState.reserved
Glenn Kasten17a736c2012-02-14 08:52:15 -0800117
118 // FIXME Most of the following initialization is probably redundant since
119 // tracks[i] should only be referenced if (mTrackNames & (1 << i)) != 0
120 // and mTrackNames is initially 0. However, leave it here until that's verified.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700121 track_t* t = mState.tracks;
Glenn Kastenbf71f1e2011-12-13 11:52:35 -0800122 for (unsigned i=0 ; i < MAX_NUM_TRACKS ; i++) {
Eric Laurenta5e82142012-04-16 13:47:17 -0700123 t->resampler = NULL;
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700124 t->downmixerBufferProvider = NULL;
Andy Hung1b2fdcb2014-07-16 17:44:34 -0700125 t->mReformatBufferProvider = NULL;
Andy Hungc5656cc2015-03-26 19:04:33 -0700126 t->mTimestretchBufferProvider = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700127 t++;
128 }
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700129
Mathias Agopian65ab4712010-07-14 17:59:35 -0700130}
131
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800132AudioMixer::~AudioMixer()
133{
134 track_t* t = mState.tracks;
Glenn Kastenbf71f1e2011-12-13 11:52:35 -0800135 for (unsigned i=0 ; i < MAX_NUM_TRACKS ; i++) {
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800136 delete t->resampler;
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700137 delete t->downmixerBufferProvider;
Andy Hung1b2fdcb2014-07-16 17:44:34 -0700138 delete t->mReformatBufferProvider;
Andy Hungc5656cc2015-03-26 19:04:33 -0700139 delete t->mTimestretchBufferProvider;
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800140 t++;
141 }
142 delete [] mState.outputTemp;
143 delete [] mState.resampleTemp;
144}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700145
Glenn Kastenab7d72f2013-02-27 09:05:28 -0800146void AudioMixer::setLog(NBLog::Writer *log)
147{
148 mState.mLog = log;
149}
150
Andy Hung7f475492014-08-25 16:36:37 -0700151static inline audio_format_t selectMixerInFormat(audio_format_t inputFormat __unused) {
152 return kUseFloat && kUseNewMixer ? AUDIO_FORMAT_PCM_FLOAT : AUDIO_FORMAT_PCM_16_BIT;
153}
154
Andy Hunge8a1ced2014-05-09 15:02:21 -0700155int AudioMixer::getTrackName(audio_channel_mask_t channelMask,
156 audio_format_t format, int sessionId)
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800157{
Andy Hunge8a1ced2014-05-09 15:02:21 -0700158 if (!isValidPcmTrackFormat(format)) {
159 ALOGE("AudioMixer::getTrackName invalid format (%#x)", format);
160 return -1;
161 }
Glenn Kasten5c94b6c2012-03-20 17:01:29 -0700162 uint32_t names = (~mTrackNames) & mConfiguredNames;
Glenn Kasten98dd5422011-12-15 14:38:29 -0800163 if (names != 0) {
164 int n = __builtin_ctz(names);
Steve Block3856b092011-10-20 11:56:00 +0100165 ALOGV("add track (%d)", n);
Glenn Kastendeeb1282012-03-25 11:59:31 -0700166 // assume default parameters for the track, except where noted below
167 track_t* t = &mState.tracks[n];
168 t->needs = 0;
Andy Hung5e58b0a2014-06-23 19:07:29 -0700169
170 // Integer volume.
171 // Currently integer volume is kept for the legacy integer mixer.
172 // Will be removed when the legacy mixer path is removed.
Andy Hung97ae8242014-05-30 10:35:47 -0700173 t->volume[0] = UNITY_GAIN_INT;
174 t->volume[1] = UNITY_GAIN_INT;
Andy Hung5e58b0a2014-06-23 19:07:29 -0700175 t->prevVolume[0] = UNITY_GAIN_INT << 16;
176 t->prevVolume[1] = UNITY_GAIN_INT << 16;
Glenn Kastendeeb1282012-03-25 11:59:31 -0700177 t->volumeInc[0] = 0;
178 t->volumeInc[1] = 0;
179 t->auxLevel = 0;
180 t->auxInc = 0;
Andy Hung5e58b0a2014-06-23 19:07:29 -0700181 t->prevAuxLevel = 0;
182
183 // Floating point volume.
184 t->mVolume[0] = UNITY_GAIN_FLOAT;
185 t->mVolume[1] = UNITY_GAIN_FLOAT;
186 t->mPrevVolume[0] = UNITY_GAIN_FLOAT;
187 t->mPrevVolume[1] = UNITY_GAIN_FLOAT;
188 t->mVolumeInc[0] = 0.;
189 t->mVolumeInc[1] = 0.;
190 t->mAuxLevel = 0.;
191 t->mAuxInc = 0.;
192 t->mPrevAuxLevel = 0.;
193
Glenn Kastendeeb1282012-03-25 11:59:31 -0700194 // no initialization needed
Glenn Kastendeeb1282012-03-25 11:59:31 -0700195 // t->frameCount
Andy Hung68112fc2014-05-14 14:13:23 -0700196 t->channelCount = audio_channel_count_from_out_mask(channelMask);
Glenn Kastendeeb1282012-03-25 11:59:31 -0700197 t->enabled = false;
Andy Hunge93b6b72014-07-17 21:30:53 -0700198 ALOGV_IF(audio_channel_mask_get_bits(channelMask) != AUDIO_CHANNEL_OUT_STEREO,
Andy Hungef7c7fb2014-05-12 16:51:41 -0700199 "Non-stereo channel mask: %d\n", channelMask);
Andy Hung68112fc2014-05-14 14:13:23 -0700200 t->channelMask = channelMask;
Jean-Michel Trivid06e1322012-09-12 15:47:07 -0700201 t->sessionId = sessionId;
Glenn Kastendeeb1282012-03-25 11:59:31 -0700202 // setBufferProvider(name, AudioBufferProvider *) is required before enable(name)
203 t->bufferProvider = NULL;
204 t->buffer.raw = NULL;
205 // no initialization needed
206 // t->buffer.frameCount
207 t->hook = NULL;
208 t->in = NULL;
209 t->resampler = NULL;
210 t->sampleRate = mSampleRate;
211 // setParameter(name, TRACK, MAIN_BUFFER, mixBuffer) is required before enable(name)
212 t->mainBuffer = NULL;
213 t->auxBuffer = NULL;
Andy Hungef7c7fb2014-05-12 16:51:41 -0700214 t->mInputBufferProvider = NULL;
215 t->mReformatBufferProvider = NULL;
Glenn Kasten52008f82012-03-18 09:34:41 -0700216 t->downmixerBufferProvider = NULL;
Andy Hung7f475492014-08-25 16:36:37 -0700217 t->mPostDownmixReformatBufferProvider = NULL;
Andy Hungc5656cc2015-03-26 19:04:33 -0700218 t->mTimestretchBufferProvider = NULL;
Andy Hung78820702014-02-28 16:23:02 -0800219 t->mMixerFormat = AUDIO_FORMAT_PCM_16_BIT;
Andy Hunge8a1ced2014-05-09 15:02:21 -0700220 t->mFormat = format;
Andy Hung7f475492014-08-25 16:36:37 -0700221 t->mMixerInFormat = selectMixerInFormat(format);
222 t->mDownmixRequiresFormat = AUDIO_FORMAT_INVALID; // no format required
Andy Hunge93b6b72014-07-17 21:30:53 -0700223 t->mMixerChannelMask = audio_channel_mask_from_representation_and_bits(
224 AUDIO_CHANNEL_REPRESENTATION_POSITION, AUDIO_CHANNEL_OUT_STEREO);
225 t->mMixerChannelCount = audio_channel_count_from_out_mask(t->mMixerChannelMask);
Andy Hungc5656cc2015-03-26 19:04:33 -0700226 t->mSpeed = AUDIO_TIMESTRETCH_SPEED_NORMAL;
227 t->mPitch = AUDIO_TIMESTRETCH_PITCH_NORMAL;
Andy Hung296b7412014-06-17 15:25:47 -0700228 // Check the downmixing (or upmixing) requirements.
Andy Hung0f451e92014-08-04 21:28:47 -0700229 status_t status = t->prepareForDownmix();
Andy Hung68112fc2014-05-14 14:13:23 -0700230 if (status != OK) {
231 ALOGE("AudioMixer::getTrackName invalid channelMask (%#x)", channelMask);
232 return -1;
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700233 }
Andy Hung7f475492014-08-25 16:36:37 -0700234 // prepareForDownmix() may change mDownmixRequiresFormat
Andy Hung296b7412014-06-17 15:25:47 -0700235 ALOGVV("mMixerFormat:%#x mMixerInFormat:%#x\n", t->mMixerFormat, t->mMixerInFormat);
Andy Hung0f451e92014-08-04 21:28:47 -0700236 t->prepareForReformat();
Andy Hung68112fc2014-05-14 14:13:23 -0700237 mTrackNames |= 1 << n;
238 return TRACK0 + n;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700239 }
Andy Hung68112fc2014-05-14 14:13:23 -0700240 ALOGE("AudioMixer::getTrackName out of available tracks");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700241 return -1;
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800242}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700243
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800244void AudioMixer::invalidateState(uint32_t mask)
245{
Glenn Kasten34fca342013-08-13 09:48:14 -0700246 if (mask != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700247 mState.needsChanged |= mask;
248 mState.hook = process__validate;
249 }
250 }
251
Andy Hunge93b6b72014-07-17 21:30:53 -0700252// Called when channel masks have changed for a track name
Andy Hung7f475492014-08-25 16:36:37 -0700253// TODO: Fix DownmixerBufferProvider not to (possibly) change mixer input format,
Andy Hunge93b6b72014-07-17 21:30:53 -0700254// which will simplify this logic.
255bool AudioMixer::setChannelMasks(int name,
256 audio_channel_mask_t trackChannelMask, audio_channel_mask_t mixerChannelMask) {
257 track_t &track = mState.tracks[name];
258
259 if (trackChannelMask == track.channelMask
260 && mixerChannelMask == track.mMixerChannelMask) {
261 return false; // no need to change
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700262 }
Andy Hunge93b6b72014-07-17 21:30:53 -0700263 // always recompute for both channel masks even if only one has changed.
264 const uint32_t trackChannelCount = audio_channel_count_from_out_mask(trackChannelMask);
265 const uint32_t mixerChannelCount = audio_channel_count_from_out_mask(mixerChannelMask);
266 const bool mixerChannelCountChanged = track.mMixerChannelCount != mixerChannelCount;
267
268 ALOG_ASSERT((trackChannelCount <= MAX_NUM_CHANNELS_TO_DOWNMIX)
269 && trackChannelCount
270 && mixerChannelCount);
271 track.channelMask = trackChannelMask;
272 track.channelCount = trackChannelCount;
273 track.mMixerChannelMask = mixerChannelMask;
274 track.mMixerChannelCount = mixerChannelCount;
275
276 // channel masks have changed, does this track need a downmixer?
277 // update to try using our desired format (if we aren't already using it)
Andy Hung7f475492014-08-25 16:36:37 -0700278 const audio_format_t prevDownmixerFormat = track.mDownmixRequiresFormat;
Andy Hung0f451e92014-08-04 21:28:47 -0700279 const status_t status = mState.tracks[name].prepareForDownmix();
Andy Hunge93b6b72014-07-17 21:30:53 -0700280 ALOGE_IF(status != OK,
Andy Hung0f451e92014-08-04 21:28:47 -0700281 "prepareForDownmix error %d, track channel mask %#x, mixer channel mask %#x",
Andy Hunge93b6b72014-07-17 21:30:53 -0700282 status, track.channelMask, track.mMixerChannelMask);
283
Andy Hung7f475492014-08-25 16:36:37 -0700284 if (prevDownmixerFormat != track.mDownmixRequiresFormat) {
Andy Hung0f451e92014-08-04 21:28:47 -0700285 track.prepareForReformat(); // because of downmixer, track format may change!
Andy Hunge93b6b72014-07-17 21:30:53 -0700286 }
287
Andy Hung7f475492014-08-25 16:36:37 -0700288 if (track.resampler && mixerChannelCountChanged) {
289 // resampler channels may have changed.
Andy Hunge93b6b72014-07-17 21:30:53 -0700290 const uint32_t resetToSampleRate = track.sampleRate;
291 delete track.resampler;
292 track.resampler = NULL;
293 track.sampleRate = mSampleRate; // without resampler, track rate is device sample rate.
294 // recreate the resampler with updated format, channels, saved sampleRate.
295 track.setResampler(resetToSampleRate /*trackSampleRate*/, mSampleRate /*devSampleRate*/);
296 }
297 return true;
298}
299
Andy Hung0f451e92014-08-04 21:28:47 -0700300void AudioMixer::track_t::unprepareForDownmix() {
301 ALOGV("AudioMixer::unprepareForDownmix(%p)", this);
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700302
Andy Hung7f475492014-08-25 16:36:37 -0700303 mDownmixRequiresFormat = AUDIO_FORMAT_INVALID;
Andy Hung0f451e92014-08-04 21:28:47 -0700304 if (downmixerBufferProvider != NULL) {
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700305 // this track had previously been configured with a downmixer, delete it
306 ALOGV(" deleting old downmixer");
Andy Hung0f451e92014-08-04 21:28:47 -0700307 delete downmixerBufferProvider;
308 downmixerBufferProvider = NULL;
309 reconfigureBufferProviders();
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700310 } else {
311 ALOGV(" nothing to do, no downmixer to delete");
312 }
313}
314
Andy Hung0f451e92014-08-04 21:28:47 -0700315status_t AudioMixer::track_t::prepareForDownmix()
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700316{
Andy Hung0f451e92014-08-04 21:28:47 -0700317 ALOGV("AudioMixer::prepareForDownmix(%p) with mask 0x%x",
318 this, channelMask);
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700319
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700320 // discard the previous downmixer if there was one
Andy Hung0f451e92014-08-04 21:28:47 -0700321 unprepareForDownmix();
322 // Only remix (upmix or downmix) if the track and mixer/device channel masks
323 // are not the same and not handled internally, as mono -> stereo currently is.
324 if (channelMask == mMixerChannelMask
325 || (channelMask == AUDIO_CHANNEL_OUT_MONO
326 && mMixerChannelMask == AUDIO_CHANNEL_OUT_STEREO)) {
327 return NO_ERROR;
328 }
Andy Hung650ceb92015-01-29 13:31:12 -0800329 // DownmixerBufferProvider is only used for position masks.
330 if (audio_channel_mask_get_representation(channelMask)
331 == AUDIO_CHANNEL_REPRESENTATION_POSITION
332 && DownmixerBufferProvider::isMultichannelCapable()) {
Andy Hung0f451e92014-08-04 21:28:47 -0700333 DownmixerBufferProvider* pDbp = new DownmixerBufferProvider(channelMask,
334 mMixerChannelMask,
335 AUDIO_FORMAT_PCM_16_BIT /* TODO: use mMixerInFormat, now only PCM 16 */,
336 sampleRate, sessionId, kCopyBufferFrameCount);
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700337
Andy Hung34803d52014-07-16 21:41:35 -0700338 if (pDbp->isValid()) { // if constructor completed properly
Andy Hung7f475492014-08-25 16:36:37 -0700339 mDownmixRequiresFormat = AUDIO_FORMAT_PCM_16_BIT; // PCM 16 bit required for downmix
Andy Hung0f451e92014-08-04 21:28:47 -0700340 downmixerBufferProvider = pDbp;
341 reconfigureBufferProviders();
Andy Hung34803d52014-07-16 21:41:35 -0700342 return NO_ERROR;
343 }
344 delete pDbp;
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700345 }
Andy Hunge93b6b72014-07-17 21:30:53 -0700346
347 // Effect downmixer does not accept the channel conversion. Let's use our remixer.
Andy Hung0f451e92014-08-04 21:28:47 -0700348 RemixBufferProvider* pRbp = new RemixBufferProvider(channelMask,
349 mMixerChannelMask, mMixerInFormat, kCopyBufferFrameCount);
Andy Hunge93b6b72014-07-17 21:30:53 -0700350 // Remix always finds a conversion whereas Downmixer effect above may fail.
Andy Hung0f451e92014-08-04 21:28:47 -0700351 downmixerBufferProvider = pRbp;
352 reconfigureBufferProviders();
Andy Hunge93b6b72014-07-17 21:30:53 -0700353 return NO_ERROR;
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700354}
355
Andy Hung0f451e92014-08-04 21:28:47 -0700356void AudioMixer::track_t::unprepareForReformat() {
357 ALOGV("AudioMixer::unprepareForReformat(%p)", this);
Andy Hung7f475492014-08-25 16:36:37 -0700358 bool requiresReconfigure = false;
Andy Hung0f451e92014-08-04 21:28:47 -0700359 if (mReformatBufferProvider != NULL) {
360 delete mReformatBufferProvider;
361 mReformatBufferProvider = NULL;
Andy Hung7f475492014-08-25 16:36:37 -0700362 requiresReconfigure = true;
363 }
364 if (mPostDownmixReformatBufferProvider != NULL) {
365 delete mPostDownmixReformatBufferProvider;
366 mPostDownmixReformatBufferProvider = NULL;
367 requiresReconfigure = true;
368 }
369 if (requiresReconfigure) {
Andy Hung0f451e92014-08-04 21:28:47 -0700370 reconfigureBufferProviders();
Andy Hungef7c7fb2014-05-12 16:51:41 -0700371 }
372}
373
Andy Hung0f451e92014-08-04 21:28:47 -0700374status_t AudioMixer::track_t::prepareForReformat()
Andy Hungef7c7fb2014-05-12 16:51:41 -0700375{
Andy Hung0f451e92014-08-04 21:28:47 -0700376 ALOGV("AudioMixer::prepareForReformat(%p) with format %#x", this, mFormat);
Andy Hung7f475492014-08-25 16:36:37 -0700377 // discard previous reformatters
Andy Hung0f451e92014-08-04 21:28:47 -0700378 unprepareForReformat();
Andy Hung7f475492014-08-25 16:36:37 -0700379 // only configure reformatters as needed
380 const audio_format_t targetFormat = mDownmixRequiresFormat != AUDIO_FORMAT_INVALID
381 ? mDownmixRequiresFormat : mMixerInFormat;
382 bool requiresReconfigure = false;
383 if (mFormat != targetFormat) {
Andy Hung0f451e92014-08-04 21:28:47 -0700384 mReformatBufferProvider = new ReformatBufferProvider(
385 audio_channel_count_from_out_mask(channelMask),
Andy Hung7f475492014-08-25 16:36:37 -0700386 mFormat,
387 targetFormat,
Andy Hung1b2fdcb2014-07-16 17:44:34 -0700388 kCopyBufferFrameCount);
Andy Hung7f475492014-08-25 16:36:37 -0700389 requiresReconfigure = true;
390 }
391 if (targetFormat != mMixerInFormat) {
392 mPostDownmixReformatBufferProvider = new ReformatBufferProvider(
393 audio_channel_count_from_out_mask(mMixerChannelMask),
394 targetFormat,
395 mMixerInFormat,
396 kCopyBufferFrameCount);
397 requiresReconfigure = true;
398 }
399 if (requiresReconfigure) {
Andy Hung0f451e92014-08-04 21:28:47 -0700400 reconfigureBufferProviders();
Andy Hung296b7412014-06-17 15:25:47 -0700401 }
402 return NO_ERROR;
Andy Hungef7c7fb2014-05-12 16:51:41 -0700403}
404
Andy Hung0f451e92014-08-04 21:28:47 -0700405void AudioMixer::track_t::reconfigureBufferProviders()
Andy Hungef7c7fb2014-05-12 16:51:41 -0700406{
Andy Hung0f451e92014-08-04 21:28:47 -0700407 bufferProvider = mInputBufferProvider;
408 if (mReformatBufferProvider) {
409 mReformatBufferProvider->setBufferProvider(bufferProvider);
410 bufferProvider = mReformatBufferProvider;
Andy Hungef7c7fb2014-05-12 16:51:41 -0700411 }
Andy Hung0f451e92014-08-04 21:28:47 -0700412 if (downmixerBufferProvider) {
413 downmixerBufferProvider->setBufferProvider(bufferProvider);
414 bufferProvider = downmixerBufferProvider;
Andy Hungef7c7fb2014-05-12 16:51:41 -0700415 }
Andy Hung7f475492014-08-25 16:36:37 -0700416 if (mPostDownmixReformatBufferProvider) {
417 mPostDownmixReformatBufferProvider->setBufferProvider(bufferProvider);
418 bufferProvider = mPostDownmixReformatBufferProvider;
419 }
Andy Hungc5656cc2015-03-26 19:04:33 -0700420 if (mTimestretchBufferProvider) {
421 mTimestretchBufferProvider->setBufferProvider(bufferProvider);
422 bufferProvider = mTimestretchBufferProvider;
423 }
Andy Hungef7c7fb2014-05-12 16:51:41 -0700424}
425
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800426void AudioMixer::deleteTrackName(int name)
427{
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700428 ALOGV("AudioMixer::deleteTrackName(%d)", name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700429 name -= TRACK0;
Glenn Kasten5798d4e2012-03-08 12:18:35 -0800430 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
Glenn Kasten237a6242011-12-15 15:32:27 -0800431 ALOGV("deleteTrackName(%d)", name);
432 track_t& track(mState.tracks[ name ]);
Glenn Kasten4c340c62012-01-27 12:33:54 -0800433 if (track.enabled) {
434 track.enabled = false;
Glenn Kasten237a6242011-12-15 15:32:27 -0800435 invalidateState(1<<name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700436 }
Glenn Kasten4e2293f2012-04-12 09:39:07 -0700437 // delete the resampler
438 delete track.resampler;
439 track.resampler = NULL;
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700440 // delete the downmixer
Andy Hung0f451e92014-08-04 21:28:47 -0700441 mState.tracks[name].unprepareForDownmix();
Andy Hungef7c7fb2014-05-12 16:51:41 -0700442 // delete the reformatter
Andy Hung0f451e92014-08-04 21:28:47 -0700443 mState.tracks[name].unprepareForReformat();
Andy Hungc5656cc2015-03-26 19:04:33 -0700444 // delete the timestretch provider
445 delete track.mTimestretchBufferProvider;
446 track.mTimestretchBufferProvider = NULL;
Glenn Kasten237a6242011-12-15 15:32:27 -0800447 mTrackNames &= ~(1<<name);
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800448}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700449
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800450void AudioMixer::enable(int name)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700451{
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800452 name -= TRACK0;
Glenn Kasten5798d4e2012-03-08 12:18:35 -0800453 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800454 track_t& track = mState.tracks[name];
455
Glenn Kasten4c340c62012-01-27 12:33:54 -0800456 if (!track.enabled) {
457 track.enabled = true;
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800458 ALOGV("enable(%d)", name);
459 invalidateState(1 << name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700460 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700461}
462
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800463void AudioMixer::disable(int name)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700464{
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800465 name -= TRACK0;
Glenn Kasten5798d4e2012-03-08 12:18:35 -0800466 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800467 track_t& track = mState.tracks[name];
468
Glenn Kasten4c340c62012-01-27 12:33:54 -0800469 if (track.enabled) {
470 track.enabled = false;
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800471 ALOGV("disable(%d)", name);
472 invalidateState(1 << name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700473 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700474}
475
Andy Hung5866a3b2014-05-29 21:33:13 -0700476/* Sets the volume ramp variables for the AudioMixer.
477 *
Andy Hung5e58b0a2014-06-23 19:07:29 -0700478 * The volume ramp variables are used to transition from the previous
479 * volume to the set volume. ramp controls the duration of the transition.
480 * Its value is typically one state framecount period, but may also be 0,
481 * meaning "immediate."
Andy Hung5866a3b2014-05-29 21:33:13 -0700482 *
Andy Hung5e58b0a2014-06-23 19:07:29 -0700483 * FIXME: 1) Volume ramp is enabled only if there is a nonzero integer increment
484 * even if there is a nonzero floating point increment (in that case, the volume
485 * change is immediate). This restriction should be changed when the legacy mixer
486 * is removed (see #2).
487 * FIXME: 2) Integer volume variables are used for Legacy mixing and should be removed
488 * when no longer needed.
489 *
490 * @param newVolume set volume target in floating point [0.0, 1.0].
491 * @param ramp number of frames to increment over. if ramp is 0, the volume
492 * should be set immediately. Currently ramp should not exceed 65535 (frames).
493 * @param pIntSetVolume pointer to the U4.12 integer target volume, set on return.
494 * @param pIntPrevVolume pointer to the U4.28 integer previous volume, set on return.
495 * @param pIntVolumeInc pointer to the U4.28 increment per output audio frame, set on return.
496 * @param pSetVolume pointer to the float target volume, set on return.
497 * @param pPrevVolume pointer to the float previous volume, set on return.
498 * @param pVolumeInc pointer to the float increment per output audio frame, set on return.
Andy Hung5866a3b2014-05-29 21:33:13 -0700499 * @return true if the volume has changed, false if volume is same.
500 */
Andy Hung5e58b0a2014-06-23 19:07:29 -0700501static inline bool setVolumeRampVariables(float newVolume, int32_t ramp,
502 int16_t *pIntSetVolume, int32_t *pIntPrevVolume, int32_t *pIntVolumeInc,
503 float *pSetVolume, float *pPrevVolume, float *pVolumeInc) {
504 if (newVolume == *pSetVolume) {
Andy Hung5866a3b2014-05-29 21:33:13 -0700505 return false;
506 }
Andy Hung5e58b0a2014-06-23 19:07:29 -0700507 /* set the floating point volume variables */
Andy Hung5866a3b2014-05-29 21:33:13 -0700508 if (ramp != 0) {
Andy Hung5e58b0a2014-06-23 19:07:29 -0700509 *pVolumeInc = (newVolume - *pSetVolume) / ramp;
510 *pPrevVolume = *pSetVolume;
Andy Hung5866a3b2014-05-29 21:33:13 -0700511 } else {
Andy Hung5e58b0a2014-06-23 19:07:29 -0700512 *pVolumeInc = 0;
513 *pPrevVolume = newVolume;
Andy Hung5866a3b2014-05-29 21:33:13 -0700514 }
Andy Hung5e58b0a2014-06-23 19:07:29 -0700515 *pSetVolume = newVolume;
516
517 /* set the legacy integer volume variables */
518 int32_t intVolume = newVolume * AudioMixer::UNITY_GAIN_INT;
519 if (intVolume > AudioMixer::UNITY_GAIN_INT) {
520 intVolume = AudioMixer::UNITY_GAIN_INT;
521 } else if (intVolume < 0) {
522 ALOGE("negative volume %.7g", newVolume);
523 intVolume = 0; // should never happen, but for safety check.
524 }
525 if (intVolume == *pIntSetVolume) {
526 *pIntVolumeInc = 0;
527 /* TODO: integer/float workaround: ignore floating volume ramp */
528 *pVolumeInc = 0;
529 *pPrevVolume = newVolume;
530 return true;
531 }
532 if (ramp != 0) {
533 *pIntVolumeInc = ((intVolume - *pIntSetVolume) << 16) / ramp;
534 *pIntPrevVolume = (*pIntVolumeInc == 0 ? intVolume : *pIntSetVolume) << 16;
535 } else {
536 *pIntVolumeInc = 0;
537 *pIntPrevVolume = intVolume << 16;
538 }
539 *pIntSetVolume = intVolume;
Andy Hung5866a3b2014-05-29 21:33:13 -0700540 return true;
541}
542
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800543void AudioMixer::setParameter(int name, int target, int param, void *value)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700544{
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800545 name -= TRACK0;
Glenn Kasten5798d4e2012-03-08 12:18:35 -0800546 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800547 track_t& track = mState.tracks[name];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700548
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000549 int valueInt = static_cast<int>(reinterpret_cast<uintptr_t>(value));
550 int32_t *valueBuf = reinterpret_cast<int32_t*>(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700551
552 switch (target) {
Glenn Kasten788040c2011-05-05 08:19:00 -0700553
Mathias Agopian65ab4712010-07-14 17:59:35 -0700554 case TRACK:
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800555 switch (param) {
Glenn Kasten788040c2011-05-05 08:19:00 -0700556 case CHANNEL_MASK: {
Andy Hunge93b6b72014-07-17 21:30:53 -0700557 const audio_channel_mask_t trackChannelMask =
558 static_cast<audio_channel_mask_t>(valueInt);
559 if (setChannelMasks(name, trackChannelMask, track.mMixerChannelMask)) {
560 ALOGV("setParameter(TRACK, CHANNEL_MASK, %x)", trackChannelMask);
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800561 invalidateState(1 << name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700562 }
Glenn Kasten788040c2011-05-05 08:19:00 -0700563 } break;
564 case MAIN_BUFFER:
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800565 if (track.mainBuffer != valueBuf) {
566 track.mainBuffer = valueBuf;
Steve Block3856b092011-10-20 11:56:00 +0100567 ALOGV("setParameter(TRACK, MAIN_BUFFER, %p)", valueBuf);
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800568 invalidateState(1 << name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700569 }
Glenn Kasten788040c2011-05-05 08:19:00 -0700570 break;
571 case AUX_BUFFER:
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800572 if (track.auxBuffer != valueBuf) {
573 track.auxBuffer = valueBuf;
Steve Block3856b092011-10-20 11:56:00 +0100574 ALOGV("setParameter(TRACK, AUX_BUFFER, %p)", valueBuf);
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800575 invalidateState(1 << name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700576 }
Glenn Kasten788040c2011-05-05 08:19:00 -0700577 break;
Andy Hungef7c7fb2014-05-12 16:51:41 -0700578 case FORMAT: {
579 audio_format_t format = static_cast<audio_format_t>(valueInt);
580 if (track.mFormat != format) {
581 ALOG_ASSERT(audio_is_linear_pcm(format), "Invalid format %#x", format);
582 track.mFormat = format;
583 ALOGV("setParameter(TRACK, FORMAT, %#x)", format);
Andy Hung0f451e92014-08-04 21:28:47 -0700584 track.prepareForReformat();
Andy Hungef7c7fb2014-05-12 16:51:41 -0700585 invalidateState(1 << name);
586 }
587 } break;
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700588 // FIXME do we want to support setting the downmix type from AudioFlinger?
589 // for a specific track? or per mixer?
590 /* case DOWNMIX_TYPE:
591 break */
Andy Hung78820702014-02-28 16:23:02 -0800592 case MIXER_FORMAT: {
Andy Hunga1ab7cc2014-02-24 19:26:52 -0800593 audio_format_t format = static_cast<audio_format_t>(valueInt);
Andy Hung78820702014-02-28 16:23:02 -0800594 if (track.mMixerFormat != format) {
595 track.mMixerFormat = format;
596 ALOGV("setParameter(TRACK, MIXER_FORMAT, %#x)", format);
Andy Hunga1ab7cc2014-02-24 19:26:52 -0800597 }
598 } break;
Andy Hunge93b6b72014-07-17 21:30:53 -0700599 case MIXER_CHANNEL_MASK: {
600 const audio_channel_mask_t mixerChannelMask =
601 static_cast<audio_channel_mask_t>(valueInt);
602 if (setChannelMasks(name, track.channelMask, mixerChannelMask)) {
603 ALOGV("setParameter(TRACK, MIXER_CHANNEL_MASK, %#x)", mixerChannelMask);
604 invalidateState(1 << name);
605 }
606 } break;
Glenn Kasten788040c2011-05-05 08:19:00 -0700607 default:
Glenn Kastenadad3d72014-02-21 14:51:43 -0800608 LOG_ALWAYS_FATAL("setParameter track: bad param %d", param);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700609 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700610 break;
Glenn Kasten788040c2011-05-05 08:19:00 -0700611
Mathias Agopian65ab4712010-07-14 17:59:35 -0700612 case RESAMPLE:
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800613 switch (param) {
614 case SAMPLE_RATE:
Glenn Kasten5798d4e2012-03-08 12:18:35 -0800615 ALOG_ASSERT(valueInt > 0, "bad sample rate %d", valueInt);
Glenn Kasten788040c2011-05-05 08:19:00 -0700616 if (track.setResampler(uint32_t(valueInt), mSampleRate)) {
617 ALOGV("setParameter(RESAMPLE, SAMPLE_RATE, %u)",
618 uint32_t(valueInt));
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800619 invalidateState(1 << name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700620 }
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800621 break;
622 case RESET:
Eric Laurent243f5f92011-02-28 16:52:51 -0800623 track.resetResampler();
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800624 invalidateState(1 << name);
625 break;
Glenn Kasten4e2293f2012-04-12 09:39:07 -0700626 case REMOVE:
627 delete track.resampler;
628 track.resampler = NULL;
629 track.sampleRate = mSampleRate;
630 invalidateState(1 << name);
631 break;
Glenn Kasten788040c2011-05-05 08:19:00 -0700632 default:
Glenn Kastenadad3d72014-02-21 14:51:43 -0800633 LOG_ALWAYS_FATAL("setParameter resample: bad param %d", param);
Eric Laurent243f5f92011-02-28 16:52:51 -0800634 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700635 break;
Glenn Kasten788040c2011-05-05 08:19:00 -0700636
Mathias Agopian65ab4712010-07-14 17:59:35 -0700637 case RAMP_VOLUME:
638 case VOLUME:
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800639 switch (param) {
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800640 case AUXLEVEL:
Andy Hung6be49402014-05-30 10:42:03 -0700641 if (setVolumeRampVariables(*reinterpret_cast<float*>(value),
Andy Hung5866a3b2014-05-29 21:33:13 -0700642 target == RAMP_VOLUME ? mState.frameCount : 0,
Andy Hung5e58b0a2014-06-23 19:07:29 -0700643 &track.auxLevel, &track.prevAuxLevel, &track.auxInc,
644 &track.mAuxLevel, &track.mPrevAuxLevel, &track.mAuxInc)) {
Andy Hung5866a3b2014-05-29 21:33:13 -0700645 ALOGV("setParameter(%s, AUXLEVEL: %04x)",
Andy Hung6be49402014-05-30 10:42:03 -0700646 target == VOLUME ? "VOLUME" : "RAMP_VOLUME", track.auxLevel);
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800647 invalidateState(1 << name);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700648 }
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800649 break;
Glenn Kasten788040c2011-05-05 08:19:00 -0700650 default:
Andy Hunge93b6b72014-07-17 21:30:53 -0700651 if ((unsigned)param >= VOLUME0 && (unsigned)param < VOLUME0 + MAX_NUM_VOLUMES) {
652 if (setVolumeRampVariables(*reinterpret_cast<float*>(value),
653 target == RAMP_VOLUME ? mState.frameCount : 0,
654 &track.volume[param - VOLUME0], &track.prevVolume[param - VOLUME0],
655 &track.volumeInc[param - VOLUME0],
656 &track.mVolume[param - VOLUME0], &track.mPrevVolume[param - VOLUME0],
657 &track.mVolumeInc[param - VOLUME0])) {
658 ALOGV("setParameter(%s, VOLUME%d: %04x)",
659 target == VOLUME ? "VOLUME" : "RAMP_VOLUME", param - VOLUME0,
660 track.volume[param - VOLUME0]);
661 invalidateState(1 << name);
662 }
663 } else {
664 LOG_ALWAYS_FATAL("setParameter volume: bad param %d", param);
665 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700666 }
667 break;
Andy Hungc5656cc2015-03-26 19:04:33 -0700668 case TIMESTRETCH:
669 switch (param) {
670 case PLAYBACK_RATE: {
671 const float speed = reinterpret_cast<float*>(value)[0];
672 const float pitch = reinterpret_cast<float*>(value)[1];
673 ALOG_ASSERT(AUDIO_TIMESTRETCH_SPEED_MIN <= speed
674 && speed <= AUDIO_TIMESTRETCH_SPEED_MAX,
675 "bad speed %f", speed);
676 ALOG_ASSERT(AUDIO_TIMESTRETCH_PITCH_MIN <= pitch
677 && pitch <= AUDIO_TIMESTRETCH_PITCH_MAX,
678 "bad pitch %f", pitch);
679 if (track.setPlaybackRate(speed, pitch)) {
680 ALOGV("setParameter(TIMESTRETCH, PLAYBACK_RATE, %f %f", speed, pitch);
681 // invalidateState(1 << name);
682 }
683 } break;
684 default:
685 LOG_ALWAYS_FATAL("setParameter timestretch: bad param %d", param);
686 }
687 break;
Glenn Kasten788040c2011-05-05 08:19:00 -0700688
689 default:
Glenn Kastenadad3d72014-02-21 14:51:43 -0800690 LOG_ALWAYS_FATAL("setParameter: bad target %d", target);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700691 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700692}
693
Andy Hunge93b6b72014-07-17 21:30:53 -0700694bool AudioMixer::track_t::setResampler(uint32_t trackSampleRate, uint32_t devSampleRate)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700695{
Andy Hunge93b6b72014-07-17 21:30:53 -0700696 if (trackSampleRate != devSampleRate || resampler != NULL) {
697 if (sampleRate != trackSampleRate) {
698 sampleRate = trackSampleRate;
Glenn Kastene0feee32011-12-13 11:53:26 -0800699 if (resampler == NULL) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700700 ALOGV("Creating resampler from track %d Hz to device %d Hz",
701 trackSampleRate, devSampleRate);
Glenn Kastenac602052012-10-01 14:04:31 -0700702 AudioResampler::src_quality quality;
703 // force lowest quality level resampler if use case isn't music or video
704 // FIXME this is flawed for dynamic sample rates, as we choose the resampler
705 // quality level based on the initial ratio, but that could change later.
706 // Should have a way to distinguish tracks with static ratios vs. dynamic ratios.
Andy Hunge93b6b72014-07-17 21:30:53 -0700707 if (!((trackSampleRate == 44100 && devSampleRate == 48000) ||
708 (trackSampleRate == 48000 && devSampleRate == 44100))) {
Andy Hung9e0308c2014-01-30 14:32:31 -0800709 quality = AudioResampler::DYN_LOW_QUALITY;
Glenn Kastenac602052012-10-01 14:04:31 -0700710 } else {
711 quality = AudioResampler::DEFAULT_QUALITY;
712 }
Andy Hung296b7412014-06-17 15:25:47 -0700713
Andy Hunge93b6b72014-07-17 21:30:53 -0700714 // TODO: Remove MONO_HACK. Resampler sees #channels after the downmixer
715 // but if none exists, it is the channel count (1 for mono).
716 const int resamplerChannelCount = downmixerBufferProvider != NULL
717 ? mMixerChannelCount : channelCount;
Andy Hung9a592762014-07-21 21:56:01 -0700718 ALOGVV("Creating resampler:"
719 " format(%#x) channels(%d) devSampleRate(%u) quality(%d)\n",
720 mMixerInFormat, resamplerChannelCount, devSampleRate, quality);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700721 resampler = AudioResampler::create(
Andy Hung3348e362014-07-07 10:21:44 -0700722 mMixerInFormat,
Andy Hunge93b6b72014-07-17 21:30:53 -0700723 resamplerChannelCount,
Glenn Kastenac602052012-10-01 14:04:31 -0700724 devSampleRate, quality);
Glenn Kasten52008f82012-03-18 09:34:41 -0700725 resampler->setLocalTimeFreq(sLocalTimeFreq);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700726 }
727 return true;
728 }
729 }
730 return false;
731}
732
Andy Hungc5656cc2015-03-26 19:04:33 -0700733bool AudioMixer::track_t::setPlaybackRate(float speed, float pitch)
734{
735 if (speed == mSpeed && pitch == mPitch) {
736 return false;
737 }
738 mSpeed = speed;
739 mPitch = pitch;
740 if (mTimestretchBufferProvider == NULL) {
741 // TODO: Remove MONO_HACK. Resampler sees #channels after the downmixer
742 // but if none exists, it is the channel count (1 for mono).
743 const int timestretchChannelCount = downmixerBufferProvider != NULL
744 ? mMixerChannelCount : channelCount;
745 mTimestretchBufferProvider = new TimestretchBufferProvider(timestretchChannelCount,
746 mMixerInFormat, sampleRate, speed, pitch);
747 reconfigureBufferProviders();
748 } else {
749 reinterpret_cast<TimestretchBufferProvider*>(mTimestretchBufferProvider)
750 ->setPlaybackRate(speed, pitch);
751 }
752 return true;
753}
754
Andy Hung5e58b0a2014-06-23 19:07:29 -0700755/* Checks to see if the volume ramp has completed and clears the increment
756 * variables appropriately.
757 *
758 * FIXME: There is code to handle int/float ramp variable switchover should it not
759 * complete within a mixer buffer processing call, but it is preferred to avoid switchover
760 * due to precision issues. The switchover code is included for legacy code purposes
761 * and can be removed once the integer volume is removed.
762 *
763 * It is not sufficient to clear only the volumeInc integer variable because
764 * if one channel requires ramping, all channels are ramped.
765 *
766 * There is a bit of duplicated code here, but it keeps backward compatibility.
767 */
768inline void AudioMixer::track_t::adjustVolumeRamp(bool aux, bool useFloat)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700769{
Andy Hung5e58b0a2014-06-23 19:07:29 -0700770 if (useFloat) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700771 for (uint32_t i = 0; i < MAX_NUM_VOLUMES; i++) {
Andy Hung5e58b0a2014-06-23 19:07:29 -0700772 if (mVolumeInc[i] != 0 && fabs(mVolume[i] - mPrevVolume[i]) <= fabs(mVolumeInc[i])) {
773 volumeInc[i] = 0;
774 prevVolume[i] = volume[i] << 16;
775 mVolumeInc[i] = 0.;
776 mPrevVolume[i] = mVolume[i];
Andy Hung5e58b0a2014-06-23 19:07:29 -0700777 } else {
778 //ALOGV("ramp: %f %f %f", mVolume[i], mPrevVolume[i], mVolumeInc[i]);
779 prevVolume[i] = u4_28_from_float(mPrevVolume[i]);
780 }
781 }
782 } else {
Andy Hunge93b6b72014-07-17 21:30:53 -0700783 for (uint32_t i = 0; i < MAX_NUM_VOLUMES; i++) {
Andy Hung5e58b0a2014-06-23 19:07:29 -0700784 if (((volumeInc[i]>0) && (((prevVolume[i]+volumeInc[i])>>16) >= volume[i])) ||
785 ((volumeInc[i]<0) && (((prevVolume[i]+volumeInc[i])>>16) <= volume[i]))) {
786 volumeInc[i] = 0;
787 prevVolume[i] = volume[i] << 16;
788 mVolumeInc[i] = 0.;
789 mPrevVolume[i] = mVolume[i];
790 } else {
791 //ALOGV("ramp: %d %d %d", volume[i] << 16, prevVolume[i], volumeInc[i]);
792 mPrevVolume[i] = float_from_u4_28(prevVolume[i]);
793 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700794 }
795 }
Andy Hung5e58b0a2014-06-23 19:07:29 -0700796 /* TODO: aux is always integer regardless of output buffer type */
Mathias Agopian65ab4712010-07-14 17:59:35 -0700797 if (aux) {
798 if (((auxInc>0) && (((prevAuxLevel+auxInc)>>16) >= auxLevel)) ||
Andy Hung5e58b0a2014-06-23 19:07:29 -0700799 ((auxInc<0) && (((prevAuxLevel+auxInc)>>16) <= auxLevel))) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700800 auxInc = 0;
Andy Hung5e58b0a2014-06-23 19:07:29 -0700801 prevAuxLevel = auxLevel << 16;
802 mAuxInc = 0.;
803 mPrevAuxLevel = mAuxLevel;
804 } else {
805 //ALOGV("aux ramp: %d %d %d", auxLevel << 16, prevAuxLevel, auxInc);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700806 }
807 }
808}
809
Glenn Kastenc59c0042012-02-02 14:06:11 -0800810size_t AudioMixer::getUnreleasedFrames(int name) const
Eric Laurent071ccd52011-12-22 16:08:41 -0800811{
812 name -= TRACK0;
813 if (uint32_t(name) < MAX_NUM_TRACKS) {
Glenn Kastenc59c0042012-02-02 14:06:11 -0800814 return mState.tracks[name].getUnreleasedFrames();
Eric Laurent071ccd52011-12-22 16:08:41 -0800815 }
816 return 0;
817}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700818
Glenn Kasten01c4ebf2012-02-22 10:47:35 -0800819void AudioMixer::setBufferProvider(int name, AudioBufferProvider* bufferProvider)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700820{
Glenn Kasten9c56d4a2011-12-19 15:06:39 -0800821 name -= TRACK0;
Glenn Kasten5798d4e2012-03-08 12:18:35 -0800822 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700823
Andy Hung1d26ddf2014-05-29 15:53:09 -0700824 if (mState.tracks[name].mInputBufferProvider == bufferProvider) {
825 return; // don't reset any buffer providers if identical.
826 }
Andy Hungef7c7fb2014-05-12 16:51:41 -0700827 if (mState.tracks[name].mReformatBufferProvider != NULL) {
828 mState.tracks[name].mReformatBufferProvider->reset();
829 } else if (mState.tracks[name].downmixerBufferProvider != NULL) {
Andy Hung7f475492014-08-25 16:36:37 -0700830 mState.tracks[name].downmixerBufferProvider->reset();
831 } else if (mState.tracks[name].mPostDownmixReformatBufferProvider != NULL) {
832 mState.tracks[name].mPostDownmixReformatBufferProvider->reset();
Andy Hungc5656cc2015-03-26 19:04:33 -0700833 } else if (mState.tracks[name].mTimestretchBufferProvider != NULL) {
834 mState.tracks[name].mTimestretchBufferProvider->reset();
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700835 }
Andy Hungef7c7fb2014-05-12 16:51:41 -0700836
837 mState.tracks[name].mInputBufferProvider = bufferProvider;
Andy Hung0f451e92014-08-04 21:28:47 -0700838 mState.tracks[name].reconfigureBufferProviders();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700839}
840
841
John Grossman4ff14ba2012-02-08 16:37:41 -0800842void AudioMixer::process(int64_t pts)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700843{
John Grossman4ff14ba2012-02-08 16:37:41 -0800844 mState.hook(&mState, pts);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700845}
846
847
John Grossman4ff14ba2012-02-08 16:37:41 -0800848void AudioMixer::process__validate(state_t* state, int64_t pts)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700849{
Steve Block5ff1dd52012-01-05 23:22:43 +0000850 ALOGW_IF(!state->needsChanged,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700851 "in process__validate() but nothing's invalid");
852
853 uint32_t changed = state->needsChanged;
854 state->needsChanged = 0; // clear the validation flag
855
856 // recompute which tracks are enabled / disabled
857 uint32_t enabled = 0;
858 uint32_t disabled = 0;
859 while (changed) {
860 const int i = 31 - __builtin_clz(changed);
861 const uint32_t mask = 1<<i;
862 changed &= ~mask;
863 track_t& t = state->tracks[i];
864 (t.enabled ? enabled : disabled) |= mask;
865 }
866 state->enabledTracks &= ~disabled;
867 state->enabledTracks |= enabled;
868
869 // compute everything we need...
870 int countActiveTracks = 0;
Andy Hung395db4b2014-08-25 17:15:29 -0700871 // TODO: fix all16BitsStereNoResample logic to
872 // either properly handle muted tracks (it should ignore them)
873 // or remove altogether as an obsolete optimization.
Glenn Kasten4c340c62012-01-27 12:33:54 -0800874 bool all16BitsStereoNoResample = true;
875 bool resampling = false;
876 bool volumeRamp = false;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700877 uint32_t en = state->enabledTracks;
878 while (en) {
879 const int i = 31 - __builtin_clz(en);
880 en &= ~(1<<i);
881
882 countActiveTracks++;
883 track_t& t = state->tracks[i];
884 uint32_t n = 0;
Glenn Kastend6fadf02013-10-30 14:37:29 -0700885 // FIXME can overflow (mask is only 3 bits)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700886 n |= NEEDS_CHANNEL_1 + t.channelCount - 1;
Glenn Kastend6fadf02013-10-30 14:37:29 -0700887 if (t.doesResample()) {
888 n |= NEEDS_RESAMPLE;
889 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700890 if (t.auxLevel != 0 && t.auxBuffer != NULL) {
Glenn Kastend6fadf02013-10-30 14:37:29 -0700891 n |= NEEDS_AUX;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700892 }
893
894 if (t.volumeInc[0]|t.volumeInc[1]) {
Glenn Kasten4c340c62012-01-27 12:33:54 -0800895 volumeRamp = true;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700896 } else if (!t.doesResample() && t.volumeRL == 0) {
Glenn Kastend6fadf02013-10-30 14:37:29 -0700897 n |= NEEDS_MUTE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700898 }
899 t.needs = n;
900
Glenn Kastend6fadf02013-10-30 14:37:29 -0700901 if (n & NEEDS_MUTE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700902 t.hook = track__nop;
903 } else {
Glenn Kastend6fadf02013-10-30 14:37:29 -0700904 if (n & NEEDS_AUX) {
Glenn Kasten4c340c62012-01-27 12:33:54 -0800905 all16BitsStereoNoResample = false;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700906 }
Glenn Kastend6fadf02013-10-30 14:37:29 -0700907 if (n & NEEDS_RESAMPLE) {
Glenn Kasten4c340c62012-01-27 12:33:54 -0800908 all16BitsStereoNoResample = false;
909 resampling = true;
Andy Hunge93b6b72014-07-17 21:30:53 -0700910 t.hook = getTrackHook(TRACKTYPE_RESAMPLE, t.mMixerChannelCount,
Andy Hung296b7412014-06-17 15:25:47 -0700911 t.mMixerInFormat, t.mMixerFormat);
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700912 ALOGV_IF((n & NEEDS_CHANNEL_COUNT__MASK) > NEEDS_CHANNEL_2,
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700913 "Track %d needs downmix + resample", i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700914 } else {
915 if ((n & NEEDS_CHANNEL_COUNT__MASK) == NEEDS_CHANNEL_1){
Andy Hunge93b6b72014-07-17 21:30:53 -0700916 t.hook = getTrackHook(
917 t.mMixerChannelCount == 2 // TODO: MONO_HACK.
918 ? TRACKTYPE_NORESAMPLEMONO : TRACKTYPE_NORESAMPLE,
919 t.mMixerChannelCount,
Andy Hung296b7412014-06-17 15:25:47 -0700920 t.mMixerInFormat, t.mMixerFormat);
Glenn Kasten4c340c62012-01-27 12:33:54 -0800921 all16BitsStereoNoResample = false;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700922 }
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700923 if ((n & NEEDS_CHANNEL_COUNT__MASK) >= NEEDS_CHANNEL_2){
Andy Hunge93b6b72014-07-17 21:30:53 -0700924 t.hook = getTrackHook(TRACKTYPE_NORESAMPLE, t.mMixerChannelCount,
Andy Hung296b7412014-06-17 15:25:47 -0700925 t.mMixerInFormat, t.mMixerFormat);
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700926 ALOGV_IF((n & NEEDS_CHANNEL_COUNT__MASK) > NEEDS_CHANNEL_2,
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700927 "Track %d needs downmix", i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700928 }
929 }
930 }
931 }
932
933 // select the processing hooks
934 state->hook = process__nop;
Glenn Kasten34fca342013-08-13 09:48:14 -0700935 if (countActiveTracks > 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700936 if (resampling) {
937 if (!state->outputTemp) {
938 state->outputTemp = new int32_t[MAX_NUM_CHANNELS * state->frameCount];
939 }
940 if (!state->resampleTemp) {
941 state->resampleTemp = new int32_t[MAX_NUM_CHANNELS * state->frameCount];
942 }
943 state->hook = process__genericResampling;
944 } else {
945 if (state->outputTemp) {
946 delete [] state->outputTemp;
Glenn Kastene0feee32011-12-13 11:53:26 -0800947 state->outputTemp = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700948 }
949 if (state->resampleTemp) {
950 delete [] state->resampleTemp;
Glenn Kastene0feee32011-12-13 11:53:26 -0800951 state->resampleTemp = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700952 }
953 state->hook = process__genericNoResampling;
954 if (all16BitsStereoNoResample && !volumeRamp) {
955 if (countActiveTracks == 1) {
Andy Hung296b7412014-06-17 15:25:47 -0700956 const int i = 31 - __builtin_clz(state->enabledTracks);
957 track_t& t = state->tracks[i];
Andy Hung395db4b2014-08-25 17:15:29 -0700958 if ((t.needs & NEEDS_MUTE) == 0) {
959 // The check prevents a muted track from acquiring a process hook.
960 //
961 // This is dangerous if the track is MONO as that requires
962 // special case handling due to implicit channel duplication.
963 // Stereo or Multichannel should actually be fine here.
964 state->hook = getProcessHook(PROCESSTYPE_NORESAMPLEONETRACK,
965 t.mMixerChannelCount, t.mMixerInFormat, t.mMixerFormat);
966 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700967 }
968 }
969 }
970 }
971
Steve Block3856b092011-10-20 11:56:00 +0100972 ALOGV("mixer configuration change: %d activeTracks (%08x) "
Mathias Agopian65ab4712010-07-14 17:59:35 -0700973 "all16BitsStereoNoResample=%d, resampling=%d, volumeRamp=%d",
974 countActiveTracks, state->enabledTracks,
975 all16BitsStereoNoResample, resampling, volumeRamp);
976
John Grossman4ff14ba2012-02-08 16:37:41 -0800977 state->hook(state, pts);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700978
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800979 // Now that the volume ramp has been done, set optimal state and
980 // track hooks for subsequent mixer process
Glenn Kasten34fca342013-08-13 09:48:14 -0700981 if (countActiveTracks > 0) {
Glenn Kasten4c340c62012-01-27 12:33:54 -0800982 bool allMuted = true;
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800983 uint32_t en = state->enabledTracks;
984 while (en) {
985 const int i = 31 - __builtin_clz(en);
986 en &= ~(1<<i);
987 track_t& t = state->tracks[i];
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700988 if (!t.doesResample() && t.volumeRL == 0) {
Glenn Kastend6fadf02013-10-30 14:37:29 -0700989 t.needs |= NEEDS_MUTE;
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800990 t.hook = track__nop;
991 } else {
Glenn Kasten4c340c62012-01-27 12:33:54 -0800992 allMuted = false;
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -0800993 }
994 }
995 if (allMuted) {
996 state->hook = process__nop;
997 } else if (all16BitsStereoNoResample) {
998 if (countActiveTracks == 1) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700999 const int i = 31 - __builtin_clz(state->enabledTracks);
1000 track_t& t = state->tracks[i];
Andy Hung395db4b2014-08-25 17:15:29 -07001001 // Muted single tracks handled by allMuted above.
Andy Hunge93b6b72014-07-17 21:30:53 -07001002 state->hook = getProcessHook(PROCESSTYPE_NORESAMPLEONETRACK,
1003 t.mMixerChannelCount, t.mMixerInFormat, t.mMixerFormat);
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -08001004 }
1005 }
1006 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001007}
1008
Mathias Agopian65ab4712010-07-14 17:59:35 -07001009
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001010void AudioMixer::track__genericResample(track_t* t, int32_t* out, size_t outFrameCount,
1011 int32_t* temp, int32_t* aux)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001012{
Andy Hung296b7412014-06-17 15:25:47 -07001013 ALOGVV("track__genericResample\n");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001014 t->resampler->setSampleRate(t->sampleRate);
1015
1016 // ramp gain - resample to temp buffer and scale/mix in 2nd step
1017 if (aux != NULL) {
1018 // always resample with unity gain when sending to auxiliary buffer to be able
1019 // to apply send level after resampling
Andy Hung5e58b0a2014-06-23 19:07:29 -07001020 t->resampler->setVolume(UNITY_GAIN_FLOAT, UNITY_GAIN_FLOAT);
Andy Hunge93b6b72014-07-17 21:30:53 -07001021 memset(temp, 0, outFrameCount * t->mMixerChannelCount * sizeof(int32_t));
Mathias Agopian65ab4712010-07-14 17:59:35 -07001022 t->resampler->resample(temp, outFrameCount, t->bufferProvider);
Glenn Kastenf6b16782011-12-15 09:51:17 -08001023 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001024 volumeRampStereo(t, out, outFrameCount, temp, aux);
1025 } else {
1026 volumeStereo(t, out, outFrameCount, temp, aux);
1027 }
1028 } else {
Glenn Kastenf6b16782011-12-15 09:51:17 -08001029 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) {
Andy Hung5e58b0a2014-06-23 19:07:29 -07001030 t->resampler->setVolume(UNITY_GAIN_FLOAT, UNITY_GAIN_FLOAT);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001031 memset(temp, 0, outFrameCount * MAX_NUM_CHANNELS * sizeof(int32_t));
1032 t->resampler->resample(temp, outFrameCount, t->bufferProvider);
1033 volumeRampStereo(t, out, outFrameCount, temp, aux);
1034 }
1035
1036 // constant gain
1037 else {
Andy Hung5e58b0a2014-06-23 19:07:29 -07001038 t->resampler->setVolume(t->mVolume[0], t->mVolume[1]);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001039 t->resampler->resample(out, outFrameCount, t->bufferProvider);
1040 }
1041 }
1042}
1043
Andy Hungee931ff2014-01-28 13:44:14 -08001044void AudioMixer::track__nop(track_t* t __unused, int32_t* out __unused,
1045 size_t outFrameCount __unused, int32_t* temp __unused, int32_t* aux __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001046{
1047}
1048
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001049void AudioMixer::volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp,
1050 int32_t* aux)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001051{
1052 int32_t vl = t->prevVolume[0];
1053 int32_t vr = t->prevVolume[1];
1054 const int32_t vlInc = t->volumeInc[0];
1055 const int32_t vrInc = t->volumeInc[1];
1056
Steve Blockb8a80522011-12-20 16:23:08 +00001057 //ALOGD("[0] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
Mathias Agopian65ab4712010-07-14 17:59:35 -07001058 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
1059 // (vl + vlInc*frameCount)/65536.0f, frameCount);
1060
1061 // ramp volume
Glenn Kastenf6b16782011-12-15 09:51:17 -08001062 if (CC_UNLIKELY(aux != NULL)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001063 int32_t va = t->prevAuxLevel;
1064 const int32_t vaInc = t->auxInc;
1065 int32_t l;
1066 int32_t r;
1067
1068 do {
1069 l = (*temp++ >> 12);
1070 r = (*temp++ >> 12);
1071 *out++ += (vl >> 16) * l;
1072 *out++ += (vr >> 16) * r;
1073 *aux++ += (va >> 17) * (l + r);
1074 vl += vlInc;
1075 vr += vrInc;
1076 va += vaInc;
1077 } while (--frameCount);
1078 t->prevAuxLevel = va;
1079 } else {
1080 do {
1081 *out++ += (vl >> 16) * (*temp++ >> 12);
1082 *out++ += (vr >> 16) * (*temp++ >> 12);
1083 vl += vlInc;
1084 vr += vrInc;
1085 } while (--frameCount);
1086 }
1087 t->prevVolume[0] = vl;
1088 t->prevVolume[1] = vr;
Glenn Kastena1117922012-01-26 10:53:32 -08001089 t->adjustVolumeRamp(aux != NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001090}
1091
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001092void AudioMixer::volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp,
1093 int32_t* aux)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001094{
1095 const int16_t vl = t->volume[0];
1096 const int16_t vr = t->volume[1];
1097
Glenn Kastenf6b16782011-12-15 09:51:17 -08001098 if (CC_UNLIKELY(aux != NULL)) {
Glenn Kasten3b81aca2012-01-27 15:26:23 -08001099 const int16_t va = t->auxLevel;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001100 do {
1101 int16_t l = (int16_t)(*temp++ >> 12);
1102 int16_t r = (int16_t)(*temp++ >> 12);
1103 out[0] = mulAdd(l, vl, out[0]);
1104 int16_t a = (int16_t)(((int32_t)l + r) >> 1);
1105 out[1] = mulAdd(r, vr, out[1]);
1106 out += 2;
1107 aux[0] = mulAdd(a, va, aux[0]);
1108 aux++;
1109 } while (--frameCount);
1110 } else {
1111 do {
1112 int16_t l = (int16_t)(*temp++ >> 12);
1113 int16_t r = (int16_t)(*temp++ >> 12);
1114 out[0] = mulAdd(l, vl, out[0]);
1115 out[1] = mulAdd(r, vr, out[1]);
1116 out += 2;
1117 } while (--frameCount);
1118 }
1119}
1120
Andy Hungee931ff2014-01-28 13:44:14 -08001121void AudioMixer::track__16BitsStereo(track_t* t, int32_t* out, size_t frameCount,
1122 int32_t* temp __unused, int32_t* aux)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001123{
Andy Hung296b7412014-06-17 15:25:47 -07001124 ALOGVV("track__16BitsStereo\n");
Glenn Kasten54c3b662012-01-06 07:46:30 -08001125 const int16_t *in = static_cast<const int16_t *>(t->in);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001126
Glenn Kastenf6b16782011-12-15 09:51:17 -08001127 if (CC_UNLIKELY(aux != NULL)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001128 int32_t l;
1129 int32_t r;
1130 // ramp gain
Glenn Kastenf6b16782011-12-15 09:51:17 -08001131 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001132 int32_t vl = t->prevVolume[0];
1133 int32_t vr = t->prevVolume[1];
1134 int32_t va = t->prevAuxLevel;
1135 const int32_t vlInc = t->volumeInc[0];
1136 const int32_t vrInc = t->volumeInc[1];
1137 const int32_t vaInc = t->auxInc;
Steve Blockb8a80522011-12-20 16:23:08 +00001138 // ALOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
Mathias Agopian65ab4712010-07-14 17:59:35 -07001139 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
1140 // (vl + vlInc*frameCount)/65536.0f, frameCount);
1141
1142 do {
1143 l = (int32_t)*in++;
1144 r = (int32_t)*in++;
1145 *out++ += (vl >> 16) * l;
1146 *out++ += (vr >> 16) * r;
1147 *aux++ += (va >> 17) * (l + r);
1148 vl += vlInc;
1149 vr += vrInc;
1150 va += vaInc;
1151 } while (--frameCount);
1152
1153 t->prevVolume[0] = vl;
1154 t->prevVolume[1] = vr;
1155 t->prevAuxLevel = va;
1156 t->adjustVolumeRamp(true);
1157 }
1158
1159 // constant gain
1160 else {
1161 const uint32_t vrl = t->volumeRL;
1162 const int16_t va = (int16_t)t->auxLevel;
1163 do {
Glenn Kasten54c3b662012-01-06 07:46:30 -08001164 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001165 int16_t a = (int16_t)(((int32_t)in[0] + in[1]) >> 1);
1166 in += 2;
1167 out[0] = mulAddRL(1, rl, vrl, out[0]);
1168 out[1] = mulAddRL(0, rl, vrl, out[1]);
1169 out += 2;
1170 aux[0] = mulAdd(a, va, aux[0]);
1171 aux++;
1172 } while (--frameCount);
1173 }
1174 } else {
1175 // ramp gain
Glenn Kastenf6b16782011-12-15 09:51:17 -08001176 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001177 int32_t vl = t->prevVolume[0];
1178 int32_t vr = t->prevVolume[1];
1179 const int32_t vlInc = t->volumeInc[0];
1180 const int32_t vrInc = t->volumeInc[1];
1181
Steve Blockb8a80522011-12-20 16:23:08 +00001182 // ALOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
Mathias Agopian65ab4712010-07-14 17:59:35 -07001183 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
1184 // (vl + vlInc*frameCount)/65536.0f, frameCount);
1185
1186 do {
1187 *out++ += (vl >> 16) * (int32_t) *in++;
1188 *out++ += (vr >> 16) * (int32_t) *in++;
1189 vl += vlInc;
1190 vr += vrInc;
1191 } while (--frameCount);
1192
1193 t->prevVolume[0] = vl;
1194 t->prevVolume[1] = vr;
1195 t->adjustVolumeRamp(false);
1196 }
1197
1198 // constant gain
1199 else {
1200 const uint32_t vrl = t->volumeRL;
1201 do {
Glenn Kasten54c3b662012-01-06 07:46:30 -08001202 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001203 in += 2;
1204 out[0] = mulAddRL(1, rl, vrl, out[0]);
1205 out[1] = mulAddRL(0, rl, vrl, out[1]);
1206 out += 2;
1207 } while (--frameCount);
1208 }
1209 }
1210 t->in = in;
1211}
1212
Andy Hungee931ff2014-01-28 13:44:14 -08001213void AudioMixer::track__16BitsMono(track_t* t, int32_t* out, size_t frameCount,
1214 int32_t* temp __unused, int32_t* aux)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001215{
Andy Hung296b7412014-06-17 15:25:47 -07001216 ALOGVV("track__16BitsMono\n");
Glenn Kasten54c3b662012-01-06 07:46:30 -08001217 const int16_t *in = static_cast<int16_t const *>(t->in);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001218
Glenn Kastenf6b16782011-12-15 09:51:17 -08001219 if (CC_UNLIKELY(aux != NULL)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001220 // ramp gain
Glenn Kastenf6b16782011-12-15 09:51:17 -08001221 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001222 int32_t vl = t->prevVolume[0];
1223 int32_t vr = t->prevVolume[1];
1224 int32_t va = t->prevAuxLevel;
1225 const int32_t vlInc = t->volumeInc[0];
1226 const int32_t vrInc = t->volumeInc[1];
1227 const int32_t vaInc = t->auxInc;
1228
Steve Blockb8a80522011-12-20 16:23:08 +00001229 // ALOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
Mathias Agopian65ab4712010-07-14 17:59:35 -07001230 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
1231 // (vl + vlInc*frameCount)/65536.0f, frameCount);
1232
1233 do {
1234 int32_t l = *in++;
1235 *out++ += (vl >> 16) * l;
1236 *out++ += (vr >> 16) * l;
1237 *aux++ += (va >> 16) * l;
1238 vl += vlInc;
1239 vr += vrInc;
1240 va += vaInc;
1241 } while (--frameCount);
1242
1243 t->prevVolume[0] = vl;
1244 t->prevVolume[1] = vr;
1245 t->prevAuxLevel = va;
1246 t->adjustVolumeRamp(true);
1247 }
1248 // constant gain
1249 else {
1250 const int16_t vl = t->volume[0];
1251 const int16_t vr = t->volume[1];
1252 const int16_t va = (int16_t)t->auxLevel;
1253 do {
1254 int16_t l = *in++;
1255 out[0] = mulAdd(l, vl, out[0]);
1256 out[1] = mulAdd(l, vr, out[1]);
1257 out += 2;
1258 aux[0] = mulAdd(l, va, aux[0]);
1259 aux++;
1260 } while (--frameCount);
1261 }
1262 } else {
1263 // ramp gain
Glenn Kastenf6b16782011-12-15 09:51:17 -08001264 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001265 int32_t vl = t->prevVolume[0];
1266 int32_t vr = t->prevVolume[1];
1267 const int32_t vlInc = t->volumeInc[0];
1268 const int32_t vrInc = t->volumeInc[1];
1269
Steve Blockb8a80522011-12-20 16:23:08 +00001270 // ALOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
Mathias Agopian65ab4712010-07-14 17:59:35 -07001271 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
1272 // (vl + vlInc*frameCount)/65536.0f, frameCount);
1273
1274 do {
1275 int32_t l = *in++;
1276 *out++ += (vl >> 16) * l;
1277 *out++ += (vr >> 16) * l;
1278 vl += vlInc;
1279 vr += vrInc;
1280 } while (--frameCount);
1281
1282 t->prevVolume[0] = vl;
1283 t->prevVolume[1] = vr;
1284 t->adjustVolumeRamp(false);
1285 }
1286 // constant gain
1287 else {
1288 const int16_t vl = t->volume[0];
1289 const int16_t vr = t->volume[1];
1290 do {
1291 int16_t l = *in++;
1292 out[0] = mulAdd(l, vl, out[0]);
1293 out[1] = mulAdd(l, vr, out[1]);
1294 out += 2;
1295 } while (--frameCount);
1296 }
1297 }
1298 t->in = in;
1299}
1300
Mathias Agopian65ab4712010-07-14 17:59:35 -07001301// no-op case
John Grossman4ff14ba2012-02-08 16:37:41 -08001302void AudioMixer::process__nop(state_t* state, int64_t pts)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001303{
Andy Hung296b7412014-06-17 15:25:47 -07001304 ALOGVV("process__nop\n");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001305 uint32_t e0 = state->enabledTracks;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001306 while (e0) {
1307 // process by group of tracks with same output buffer to
1308 // avoid multiple memset() on same buffer
1309 uint32_t e1 = e0, e2 = e0;
1310 int i = 31 - __builtin_clz(e1);
Glenn Kastenfc900c92013-02-18 12:47:49 -08001311 {
1312 track_t& t1 = state->tracks[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001313 e2 &= ~(1<<i);
Glenn Kastenfc900c92013-02-18 12:47:49 -08001314 while (e2) {
1315 i = 31 - __builtin_clz(e2);
1316 e2 &= ~(1<<i);
1317 track_t& t2 = state->tracks[i];
1318 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) {
1319 e1 &= ~(1<<i);
1320 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001321 }
Glenn Kastenfc900c92013-02-18 12:47:49 -08001322 e0 &= ~(e1);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001323
Andy Hunge93b6b72014-07-17 21:30:53 -07001324 memset(t1.mainBuffer, 0, state->frameCount * t1.mMixerChannelCount
Andy Hung78820702014-02-28 16:23:02 -08001325 * audio_bytes_per_sample(t1.mMixerFormat));
Glenn Kastenfc900c92013-02-18 12:47:49 -08001326 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001327
1328 while (e1) {
1329 i = 31 - __builtin_clz(e1);
1330 e1 &= ~(1<<i);
Glenn Kastenfc900c92013-02-18 12:47:49 -08001331 {
1332 track_t& t3 = state->tracks[i];
1333 size_t outFrames = state->frameCount;
1334 while (outFrames) {
1335 t3.buffer.frameCount = outFrames;
1336 int64_t outputPTS = calculateOutputPTS(
1337 t3, pts, state->frameCount - outFrames);
1338 t3.bufferProvider->getNextBuffer(&t3.buffer, outputPTS);
1339 if (t3.buffer.raw == NULL) break;
1340 outFrames -= t3.buffer.frameCount;
1341 t3.bufferProvider->releaseBuffer(&t3.buffer);
1342 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001343 }
1344 }
1345 }
1346}
1347
1348// generic code without resampling
John Grossman4ff14ba2012-02-08 16:37:41 -08001349void AudioMixer::process__genericNoResampling(state_t* state, int64_t pts)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001350{
Andy Hung296b7412014-06-17 15:25:47 -07001351 ALOGVV("process__genericNoResampling\n");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001352 int32_t outTemp[BLOCKSIZE * MAX_NUM_CHANNELS] __attribute__((aligned(32)));
1353
1354 // acquire each track's buffer
1355 uint32_t enabledTracks = state->enabledTracks;
1356 uint32_t e0 = enabledTracks;
1357 while (e0) {
1358 const int i = 31 - __builtin_clz(e0);
1359 e0 &= ~(1<<i);
1360 track_t& t = state->tracks[i];
1361 t.buffer.frameCount = state->frameCount;
John Grossman4ff14ba2012-02-08 16:37:41 -08001362 t.bufferProvider->getNextBuffer(&t.buffer, pts);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001363 t.frameCount = t.buffer.frameCount;
1364 t.in = t.buffer.raw;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001365 }
1366
1367 e0 = enabledTracks;
1368 while (e0) {
1369 // process by group of tracks with same output buffer to
1370 // optimize cache use
1371 uint32_t e1 = e0, e2 = e0;
1372 int j = 31 - __builtin_clz(e1);
1373 track_t& t1 = state->tracks[j];
1374 e2 &= ~(1<<j);
1375 while (e2) {
1376 j = 31 - __builtin_clz(e2);
1377 e2 &= ~(1<<j);
1378 track_t& t2 = state->tracks[j];
Glenn Kastenf6b16782011-12-15 09:51:17 -08001379 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001380 e1 &= ~(1<<j);
1381 }
1382 }
1383 e0 &= ~(e1);
1384 // this assumes output 16 bits stereo, no resampling
1385 int32_t *out = t1.mainBuffer;
1386 size_t numFrames = 0;
1387 do {
1388 memset(outTemp, 0, sizeof(outTemp));
1389 e2 = e1;
1390 while (e2) {
1391 const int i = 31 - __builtin_clz(e2);
1392 e2 &= ~(1<<i);
1393 track_t& t = state->tracks[i];
1394 size_t outFrames = BLOCKSIZE;
1395 int32_t *aux = NULL;
Glenn Kastend6fadf02013-10-30 14:37:29 -07001396 if (CC_UNLIKELY(t.needs & NEEDS_AUX)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001397 aux = t.auxBuffer + numFrames;
1398 }
1399 while (outFrames) {
Gaurav Kumar7e79cd22014-01-06 10:57:18 +05301400 // t.in == NULL can happen if the track was flushed just after having
1401 // been enabled for mixing.
1402 if (t.in == NULL) {
1403 enabledTracks &= ~(1<<i);
1404 e1 &= ~(1<<i);
1405 break;
1406 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001407 size_t inFrames = (t.frameCount > outFrames)?outFrames:t.frameCount;
Glenn Kasten34fca342013-08-13 09:48:14 -07001408 if (inFrames > 0) {
Andy Hunge93b6b72014-07-17 21:30:53 -07001409 t.hook(&t, outTemp + (BLOCKSIZE - outFrames) * t.mMixerChannelCount,
1410 inFrames, state->resampleTemp, aux);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001411 t.frameCount -= inFrames;
1412 outFrames -= inFrames;
Glenn Kastenf6b16782011-12-15 09:51:17 -08001413 if (CC_UNLIKELY(aux != NULL)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001414 aux += inFrames;
1415 }
1416 }
1417 if (t.frameCount == 0 && outFrames) {
1418 t.bufferProvider->releaseBuffer(&t.buffer);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001419 t.buffer.frameCount = (state->frameCount - numFrames) -
1420 (BLOCKSIZE - outFrames);
John Grossman4ff14ba2012-02-08 16:37:41 -08001421 int64_t outputPTS = calculateOutputPTS(
1422 t, pts, numFrames + (BLOCKSIZE - outFrames));
1423 t.bufferProvider->getNextBuffer(&t.buffer, outputPTS);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001424 t.in = t.buffer.raw;
1425 if (t.in == NULL) {
1426 enabledTracks &= ~(1<<i);
1427 e1 &= ~(1<<i);
1428 break;
1429 }
1430 t.frameCount = t.buffer.frameCount;
1431 }
1432 }
1433 }
Andy Hung296b7412014-06-17 15:25:47 -07001434
1435 convertMixerFormat(out, t1.mMixerFormat, outTemp, t1.mMixerInFormat,
Andy Hunge93b6b72014-07-17 21:30:53 -07001436 BLOCKSIZE * t1.mMixerChannelCount);
Andy Hung296b7412014-06-17 15:25:47 -07001437 // TODO: fix ugly casting due to choice of out pointer type
1438 out = reinterpret_cast<int32_t*>((uint8_t*)out
Andy Hunge93b6b72014-07-17 21:30:53 -07001439 + BLOCKSIZE * t1.mMixerChannelCount
1440 * audio_bytes_per_sample(t1.mMixerFormat));
Mathias Agopian65ab4712010-07-14 17:59:35 -07001441 numFrames += BLOCKSIZE;
1442 } while (numFrames < state->frameCount);
1443 }
1444
1445 // release each track's buffer
1446 e0 = enabledTracks;
1447 while (e0) {
1448 const int i = 31 - __builtin_clz(e0);
1449 e0 &= ~(1<<i);
1450 track_t& t = state->tracks[i];
1451 t.bufferProvider->releaseBuffer(&t.buffer);
1452 }
1453}
1454
1455
Glenn Kastenc5ac4cb2011-12-12 09:05:55 -08001456// generic code with resampling
John Grossman4ff14ba2012-02-08 16:37:41 -08001457void AudioMixer::process__genericResampling(state_t* state, int64_t pts)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001458{
Andy Hung296b7412014-06-17 15:25:47 -07001459 ALOGVV("process__genericResampling\n");
Glenn Kasten54c3b662012-01-06 07:46:30 -08001460 // this const just means that local variable outTemp doesn't change
Mathias Agopian65ab4712010-07-14 17:59:35 -07001461 int32_t* const outTemp = state->outputTemp;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001462 size_t numFrames = state->frameCount;
1463
1464 uint32_t e0 = state->enabledTracks;
1465 while (e0) {
1466 // process by group of tracks with same output buffer
1467 // to optimize cache use
1468 uint32_t e1 = e0, e2 = e0;
1469 int j = 31 - __builtin_clz(e1);
1470 track_t& t1 = state->tracks[j];
1471 e2 &= ~(1<<j);
1472 while (e2) {
1473 j = 31 - __builtin_clz(e2);
1474 e2 &= ~(1<<j);
1475 track_t& t2 = state->tracks[j];
Glenn Kastenf6b16782011-12-15 09:51:17 -08001476 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001477 e1 &= ~(1<<j);
1478 }
1479 }
1480 e0 &= ~(e1);
1481 int32_t *out = t1.mainBuffer;
Andy Hunge93b6b72014-07-17 21:30:53 -07001482 memset(outTemp, 0, sizeof(*outTemp) * t1.mMixerChannelCount * state->frameCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001483 while (e1) {
1484 const int i = 31 - __builtin_clz(e1);
1485 e1 &= ~(1<<i);
1486 track_t& t = state->tracks[i];
1487 int32_t *aux = NULL;
Glenn Kastend6fadf02013-10-30 14:37:29 -07001488 if (CC_UNLIKELY(t.needs & NEEDS_AUX)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001489 aux = t.auxBuffer;
1490 }
1491
1492 // this is a little goofy, on the resampling case we don't
1493 // acquire/release the buffers because it's done by
1494 // the resampler.
Glenn Kastend6fadf02013-10-30 14:37:29 -07001495 if (t.needs & NEEDS_RESAMPLE) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001496 t.resampler->setPTS(pts);
Glenn Kastena1117922012-01-26 10:53:32 -08001497 t.hook(&t, outTemp, numFrames, state->resampleTemp, aux);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001498 } else {
1499
1500 size_t outFrames = 0;
1501
1502 while (outFrames < numFrames) {
1503 t.buffer.frameCount = numFrames - outFrames;
John Grossman4ff14ba2012-02-08 16:37:41 -08001504 int64_t outputPTS = calculateOutputPTS(t, pts, outFrames);
1505 t.bufferProvider->getNextBuffer(&t.buffer, outputPTS);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001506 t.in = t.buffer.raw;
1507 // t.in == NULL can happen if the track was flushed just after having
1508 // been enabled for mixing.
1509 if (t.in == NULL) break;
1510
Glenn Kastenf6b16782011-12-15 09:51:17 -08001511 if (CC_UNLIKELY(aux != NULL)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001512 aux += outFrames;
1513 }
Andy Hunge93b6b72014-07-17 21:30:53 -07001514 t.hook(&t, outTemp + outFrames * t.mMixerChannelCount, t.buffer.frameCount,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001515 state->resampleTemp, aux);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001516 outFrames += t.buffer.frameCount;
1517 t.bufferProvider->releaseBuffer(&t.buffer);
1518 }
1519 }
1520 }
Andy Hunge93b6b72014-07-17 21:30:53 -07001521 convertMixerFormat(out, t1.mMixerFormat,
1522 outTemp, t1.mMixerInFormat, numFrames * t1.mMixerChannelCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001523 }
1524}
1525
1526// one track, 16 bits stereo without resampling is the most common case
John Grossman4ff14ba2012-02-08 16:37:41 -08001527void AudioMixer::process__OneTrack16BitsStereoNoResampling(state_t* state,
1528 int64_t pts)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001529{
Andy Hung296b7412014-06-17 15:25:47 -07001530 ALOGVV("process__OneTrack16BitsStereoNoResampling\n");
Glenn Kasten99e53b82012-01-19 08:59:58 -08001531 // This method is only called when state->enabledTracks has exactly
1532 // one bit set. The asserts below would verify this, but are commented out
1533 // since the whole point of this method is to optimize performance.
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001534 //ALOG_ASSERT(0 != state->enabledTracks, "no tracks enabled");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001535 const int i = 31 - __builtin_clz(state->enabledTracks);
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001536 //ALOG_ASSERT((1 << i) == state->enabledTracks, "more than 1 track enabled");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001537 const track_t& t = state->tracks[i];
1538
1539 AudioBufferProvider::Buffer& b(t.buffer);
1540
1541 int32_t* out = t.mainBuffer;
Andy Hungf8a106a2014-05-29 18:52:38 -07001542 float *fout = reinterpret_cast<float*>(out);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001543 size_t numFrames = state->frameCount;
1544
1545 const int16_t vl = t.volume[0];
1546 const int16_t vr = t.volume[1];
1547 const uint32_t vrl = t.volumeRL;
1548 while (numFrames) {
1549 b.frameCount = numFrames;
John Grossman4ff14ba2012-02-08 16:37:41 -08001550 int64_t outputPTS = calculateOutputPTS(t, pts, out - t.mainBuffer);
1551 t.bufferProvider->getNextBuffer(&b, outputPTS);
Glenn Kasten54c3b662012-01-06 07:46:30 -08001552 const int16_t *in = b.i16;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001553
1554 // in == NULL can happen if the track was flushed just after having
1555 // been enabled for mixing.
Andy Hungf8a106a2014-05-29 18:52:38 -07001556 if (in == NULL || (((uintptr_t)in) & 3)) {
1557 memset(out, 0, numFrames
Andy Hunge93b6b72014-07-17 21:30:53 -07001558 * t.mMixerChannelCount * audio_bytes_per_sample(t.mMixerFormat));
Andy Hung395db4b2014-08-25 17:15:29 -07001559 ALOGE_IF((((uintptr_t)in) & 3),
1560 "process__OneTrack16BitsStereoNoResampling: misaligned buffer"
1561 " %p track %d, channels %d, needs %08x, volume %08x vfl %f vfr %f",
1562 in, i, t.channelCount, t.needs, vrl, t.mVolume[0], t.mVolume[1]);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001563 return;
1564 }
1565 size_t outFrames = b.frameCount;
1566
Andy Hung78820702014-02-28 16:23:02 -08001567 switch (t.mMixerFormat) {
Andy Hungf8a106a2014-05-29 18:52:38 -07001568 case AUDIO_FORMAT_PCM_FLOAT:
Mathias Agopian65ab4712010-07-14 17:59:35 -07001569 do {
Glenn Kasten54c3b662012-01-06 07:46:30 -08001570 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001571 in += 2;
Andy Hunga1ab7cc2014-02-24 19:26:52 -08001572 int32_t l = mulRL(1, rl, vrl);
1573 int32_t r = mulRL(0, rl, vrl);
Andy Hung84a0c6e2014-04-02 11:24:53 -07001574 *fout++ = float_from_q4_27(l);
1575 *fout++ = float_from_q4_27(r);
Andy Hung3375bde2014-02-28 15:51:47 -08001576 // Note: In case of later int16_t sink output,
1577 // conversion and clamping is done by memcpy_to_i16_from_float().
Mathias Agopian65ab4712010-07-14 17:59:35 -07001578 } while (--outFrames);
Andy Hungf8a106a2014-05-29 18:52:38 -07001579 break;
Andy Hunga1ab7cc2014-02-24 19:26:52 -08001580 case AUDIO_FORMAT_PCM_16_BIT:
Andy Hung97ae8242014-05-30 10:35:47 -07001581 if (CC_UNLIKELY(uint32_t(vl) > UNITY_GAIN_INT || uint32_t(vr) > UNITY_GAIN_INT)) {
Andy Hunga1ab7cc2014-02-24 19:26:52 -08001582 // volume is boosted, so we might need to clamp even though
1583 // we process only one track.
1584 do {
1585 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
1586 in += 2;
1587 int32_t l = mulRL(1, rl, vrl) >> 12;
1588 int32_t r = mulRL(0, rl, vrl) >> 12;
1589 // clamping...
1590 l = clamp16(l);
1591 r = clamp16(r);
1592 *out++ = (r<<16) | (l & 0xFFFF);
1593 } while (--outFrames);
1594 } else {
1595 do {
1596 uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
1597 in += 2;
1598 int32_t l = mulRL(1, rl, vrl) >> 12;
1599 int32_t r = mulRL(0, rl, vrl) >> 12;
1600 *out++ = (r<<16) | (l & 0xFFFF);
1601 } while (--outFrames);
1602 }
1603 break;
1604 default:
Andy Hung78820702014-02-28 16:23:02 -08001605 LOG_ALWAYS_FATAL("bad mixer format: %d", t.mMixerFormat);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001606 }
1607 numFrames -= b.frameCount;
1608 t.bufferProvider->releaseBuffer(&b);
1609 }
1610}
1611
John Grossman4ff14ba2012-02-08 16:37:41 -08001612int64_t AudioMixer::calculateOutputPTS(const track_t& t, int64_t basePTS,
1613 int outputFrameIndex)
1614{
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001615 if (AudioBufferProvider::kInvalidPTS == basePTS) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001616 return AudioBufferProvider::kInvalidPTS;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001617 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001618
Glenn Kasten52008f82012-03-18 09:34:41 -07001619 return basePTS + ((outputFrameIndex * sLocalTimeFreq) / t.sampleRate);
1620}
1621
1622/*static*/ uint64_t AudioMixer::sLocalTimeFreq;
1623/*static*/ pthread_once_t AudioMixer::sOnceControl = PTHREAD_ONCE_INIT;
1624
1625/*static*/ void AudioMixer::sInitRoutine()
1626{
1627 LocalClock lc;
Andy Hung34803d52014-07-16 21:41:35 -07001628 sLocalTimeFreq = lc.getLocalFreq(); // for the resampler
Glenn Kasten49c34ac2013-10-30 14:37:01 -07001629
Andy Hung34803d52014-07-16 21:41:35 -07001630 DownmixerBufferProvider::init(); // for the downmixer
John Grossman4ff14ba2012-02-08 16:37:41 -08001631}
1632
Andy Hunge93b6b72014-07-17 21:30:53 -07001633/* TODO: consider whether this level of optimization is necessary.
1634 * Perhaps just stick with a single for loop.
1635 */
1636
1637// Needs to derive a compile time constant (constexpr). Could be targeted to go
1638// to a MONOVOL mixtype based on MAX_NUM_VOLUMES, but that's an unnecessary complication.
1639#define MIXTYPE_MONOVOL(mixtype) (mixtype == MIXTYPE_MULTI ? MIXTYPE_MULTI_MONOVOL : \
1640 mixtype == MIXTYPE_MULTI_SAVEONLY ? MIXTYPE_MULTI_SAVEONLY_MONOVOL : mixtype)
1641
1642/* MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
1643 * TO: int32_t (Q4.27) or float
1644 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
1645 * TA: int32_t (Q4.27)
1646 */
1647template <int MIXTYPE,
1648 typename TO, typename TI, typename TV, typename TA, typename TAV>
1649static void volumeRampMulti(uint32_t channels, TO* out, size_t frameCount,
1650 const TI* in, TA* aux, TV *vol, const TV *volinc, TAV *vola, TAV volainc)
1651{
1652 switch (channels) {
1653 case 1:
1654 volumeRampMulti<MIXTYPE, 1>(out, frameCount, in, aux, vol, volinc, vola, volainc);
1655 break;
1656 case 2:
1657 volumeRampMulti<MIXTYPE, 2>(out, frameCount, in, aux, vol, volinc, vola, volainc);
1658 break;
1659 case 3:
1660 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 3>(out,
1661 frameCount, in, aux, vol, volinc, vola, volainc);
1662 break;
1663 case 4:
1664 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 4>(out,
1665 frameCount, in, aux, vol, volinc, vola, volainc);
1666 break;
1667 case 5:
1668 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 5>(out,
1669 frameCount, in, aux, vol, volinc, vola, volainc);
1670 break;
1671 case 6:
1672 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 6>(out,
1673 frameCount, in, aux, vol, volinc, vola, volainc);
1674 break;
1675 case 7:
1676 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 7>(out,
1677 frameCount, in, aux, vol, volinc, vola, volainc);
1678 break;
1679 case 8:
1680 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 8>(out,
1681 frameCount, in, aux, vol, volinc, vola, volainc);
1682 break;
1683 }
1684}
1685
1686/* MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
1687 * TO: int32_t (Q4.27) or float
1688 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
1689 * TA: int32_t (Q4.27)
1690 */
1691template <int MIXTYPE,
1692 typename TO, typename TI, typename TV, typename TA, typename TAV>
1693static void volumeMulti(uint32_t channels, TO* out, size_t frameCount,
1694 const TI* in, TA* aux, const TV *vol, TAV vola)
1695{
1696 switch (channels) {
1697 case 1:
1698 volumeMulti<MIXTYPE, 1>(out, frameCount, in, aux, vol, vola);
1699 break;
1700 case 2:
1701 volumeMulti<MIXTYPE, 2>(out, frameCount, in, aux, vol, vola);
1702 break;
1703 case 3:
1704 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 3>(out, frameCount, in, aux, vol, vola);
1705 break;
1706 case 4:
1707 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 4>(out, frameCount, in, aux, vol, vola);
1708 break;
1709 case 5:
1710 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 5>(out, frameCount, in, aux, vol, vola);
1711 break;
1712 case 6:
1713 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 6>(out, frameCount, in, aux, vol, vola);
1714 break;
1715 case 7:
1716 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 7>(out, frameCount, in, aux, vol, vola);
1717 break;
1718 case 8:
1719 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 8>(out, frameCount, in, aux, vol, vola);
1720 break;
1721 }
1722}
1723
1724/* MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
1725 * USEFLOATVOL (set to true if float volume is used)
1726 * ADJUSTVOL (set to true if volume ramp parameters needs adjustment afterwards)
1727 * TO: int32_t (Q4.27) or float
1728 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
1729 * TA: int32_t (Q4.27)
1730 */
1731template <int MIXTYPE, bool USEFLOATVOL, bool ADJUSTVOL,
Andy Hung5e58b0a2014-06-23 19:07:29 -07001732 typename TO, typename TI, typename TA>
1733void AudioMixer::volumeMix(TO *out, size_t outFrames,
1734 const TI *in, TA *aux, bool ramp, AudioMixer::track_t *t)
1735{
1736 if (USEFLOATVOL) {
1737 if (ramp) {
Andy Hunge93b6b72014-07-17 21:30:53 -07001738 volumeRampMulti<MIXTYPE>(t->mMixerChannelCount, out, outFrames, in, aux,
Andy Hung5e58b0a2014-06-23 19:07:29 -07001739 t->mPrevVolume, t->mVolumeInc, &t->prevAuxLevel, t->auxInc);
1740 if (ADJUSTVOL) {
1741 t->adjustVolumeRamp(aux != NULL, true);
1742 }
1743 } else {
Andy Hunge93b6b72014-07-17 21:30:53 -07001744 volumeMulti<MIXTYPE>(t->mMixerChannelCount, out, outFrames, in, aux,
Andy Hung5e58b0a2014-06-23 19:07:29 -07001745 t->mVolume, t->auxLevel);
1746 }
1747 } else {
1748 if (ramp) {
Andy Hunge93b6b72014-07-17 21:30:53 -07001749 volumeRampMulti<MIXTYPE>(t->mMixerChannelCount, out, outFrames, in, aux,
Andy Hung5e58b0a2014-06-23 19:07:29 -07001750 t->prevVolume, t->volumeInc, &t->prevAuxLevel, t->auxInc);
1751 if (ADJUSTVOL) {
1752 t->adjustVolumeRamp(aux != NULL);
1753 }
1754 } else {
Andy Hunge93b6b72014-07-17 21:30:53 -07001755 volumeMulti<MIXTYPE>(t->mMixerChannelCount, out, outFrames, in, aux,
Andy Hung5e58b0a2014-06-23 19:07:29 -07001756 t->volume, t->auxLevel);
1757 }
1758 }
1759}
1760
Andy Hung296b7412014-06-17 15:25:47 -07001761/* This process hook is called when there is a single track without
1762 * aux buffer, volume ramp, or resampling.
1763 * TODO: Update the hook selection: this can properly handle aux and ramp.
Andy Hunge93b6b72014-07-17 21:30:53 -07001764 *
1765 * MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
1766 * TO: int32_t (Q4.27) or float
1767 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
1768 * TA: int32_t (Q4.27)
Andy Hung296b7412014-06-17 15:25:47 -07001769 */
Andy Hunge93b6b72014-07-17 21:30:53 -07001770template <int MIXTYPE, typename TO, typename TI, typename TA>
Andy Hung296b7412014-06-17 15:25:47 -07001771void AudioMixer::process_NoResampleOneTrack(state_t* state, int64_t pts)
1772{
1773 ALOGVV("process_NoResampleOneTrack\n");
1774 // CLZ is faster than CTZ on ARM, though really not sure if true after 31 - clz.
1775 const int i = 31 - __builtin_clz(state->enabledTracks);
1776 ALOG_ASSERT((1 << i) == state->enabledTracks, "more than 1 track enabled");
1777 track_t *t = &state->tracks[i];
Andy Hunge93b6b72014-07-17 21:30:53 -07001778 const uint32_t channels = t->mMixerChannelCount;
Andy Hung296b7412014-06-17 15:25:47 -07001779 TO* out = reinterpret_cast<TO*>(t->mainBuffer);
1780 TA* aux = reinterpret_cast<TA*>(t->auxBuffer);
1781 const bool ramp = t->needsRamp();
1782
1783 for (size_t numFrames = state->frameCount; numFrames; ) {
1784 AudioBufferProvider::Buffer& b(t->buffer);
1785 // get input buffer
1786 b.frameCount = numFrames;
1787 const int64_t outputPTS = calculateOutputPTS(*t, pts, state->frameCount - numFrames);
1788 t->bufferProvider->getNextBuffer(&b, outputPTS);
1789 const TI *in = reinterpret_cast<TI*>(b.raw);
1790
1791 // in == NULL can happen if the track was flushed just after having
1792 // been enabled for mixing.
1793 if (in == NULL || (((uintptr_t)in) & 3)) {
1794 memset(out, 0, numFrames
Andy Hunge93b6b72014-07-17 21:30:53 -07001795 * channels * audio_bytes_per_sample(t->mMixerFormat));
Andy Hung296b7412014-06-17 15:25:47 -07001796 ALOGE_IF((((uintptr_t)in) & 3), "process_NoResampleOneTrack: bus error: "
1797 "buffer %p track %p, channels %d, needs %#x",
1798 in, t, t->channelCount, t->needs);
1799 return;
1800 }
1801
1802 const size_t outFrames = b.frameCount;
Andy Hunge93b6b72014-07-17 21:30:53 -07001803 volumeMix<MIXTYPE, is_same<TI, float>::value, false> (
1804 out, outFrames, in, aux, ramp, t);
Andy Hung5e58b0a2014-06-23 19:07:29 -07001805
Andy Hunge93b6b72014-07-17 21:30:53 -07001806 out += outFrames * channels;
Andy Hung296b7412014-06-17 15:25:47 -07001807 if (aux != NULL) {
Andy Hunge93b6b72014-07-17 21:30:53 -07001808 aux += channels;
Andy Hung296b7412014-06-17 15:25:47 -07001809 }
1810 numFrames -= b.frameCount;
1811
1812 // release buffer
1813 t->bufferProvider->releaseBuffer(&b);
1814 }
1815 if (ramp) {
Andy Hung5e58b0a2014-06-23 19:07:29 -07001816 t->adjustVolumeRamp(aux != NULL, is_same<TI, float>::value);
Andy Hung296b7412014-06-17 15:25:47 -07001817 }
1818}
1819
1820/* This track hook is called to do resampling then mixing,
1821 * pulling from the track's upstream AudioBufferProvider.
Andy Hunge93b6b72014-07-17 21:30:53 -07001822 *
1823 * MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
1824 * TO: int32_t (Q4.27) or float
1825 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
1826 * TA: int32_t (Q4.27)
Andy Hung296b7412014-06-17 15:25:47 -07001827 */
Andy Hunge93b6b72014-07-17 21:30:53 -07001828template <int MIXTYPE, typename TO, typename TI, typename TA>
Andy Hung296b7412014-06-17 15:25:47 -07001829void AudioMixer::track__Resample(track_t* t, TO* out, size_t outFrameCount, TO* temp, TA* aux)
1830{
1831 ALOGVV("track__Resample\n");
1832 t->resampler->setSampleRate(t->sampleRate);
Andy Hung296b7412014-06-17 15:25:47 -07001833 const bool ramp = t->needsRamp();
1834 if (ramp || aux != NULL) {
1835 // if ramp: resample with unity gain to temp buffer and scale/mix in 2nd step.
1836 // if aux != NULL: resample with unity gain to temp buffer then apply send level.
1837
Andy Hung5e58b0a2014-06-23 19:07:29 -07001838 t->resampler->setVolume(UNITY_GAIN_FLOAT, UNITY_GAIN_FLOAT);
Andy Hunge93b6b72014-07-17 21:30:53 -07001839 memset(temp, 0, outFrameCount * t->mMixerChannelCount * sizeof(TO));
Andy Hung296b7412014-06-17 15:25:47 -07001840 t->resampler->resample((int32_t*)temp, outFrameCount, t->bufferProvider);
Andy Hung5e58b0a2014-06-23 19:07:29 -07001841
Andy Hunge93b6b72014-07-17 21:30:53 -07001842 volumeMix<MIXTYPE, is_same<TI, float>::value, true>(
1843 out, outFrameCount, temp, aux, ramp, t);
Andy Hung5e58b0a2014-06-23 19:07:29 -07001844
Andy Hung296b7412014-06-17 15:25:47 -07001845 } else { // constant volume gain
Andy Hung5e58b0a2014-06-23 19:07:29 -07001846 t->resampler->setVolume(t->mVolume[0], t->mVolume[1]);
Andy Hung296b7412014-06-17 15:25:47 -07001847 t->resampler->resample((int32_t*)out, outFrameCount, t->bufferProvider);
1848 }
1849}
1850
1851/* This track hook is called to mix a track, when no resampling is required.
1852 * The input buffer should be present in t->in.
Andy Hunge93b6b72014-07-17 21:30:53 -07001853 *
1854 * MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
1855 * TO: int32_t (Q4.27) or float
1856 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
1857 * TA: int32_t (Q4.27)
Andy Hung296b7412014-06-17 15:25:47 -07001858 */
Andy Hunge93b6b72014-07-17 21:30:53 -07001859template <int MIXTYPE, typename TO, typename TI, typename TA>
Andy Hung296b7412014-06-17 15:25:47 -07001860void AudioMixer::track__NoResample(track_t* t, TO* out, size_t frameCount,
1861 TO* temp __unused, TA* aux)
1862{
1863 ALOGVV("track__NoResample\n");
1864 const TI *in = static_cast<const TI *>(t->in);
1865
Andy Hunge93b6b72014-07-17 21:30:53 -07001866 volumeMix<MIXTYPE, is_same<TI, float>::value, true>(
1867 out, frameCount, in, aux, t->needsRamp(), t);
Andy Hung5e58b0a2014-06-23 19:07:29 -07001868
Andy Hung296b7412014-06-17 15:25:47 -07001869 // MIXTYPE_MONOEXPAND reads a single input channel and expands to NCHAN output channels.
1870 // MIXTYPE_MULTI reads NCHAN input channels and places to NCHAN output channels.
Andy Hunge93b6b72014-07-17 21:30:53 -07001871 in += (MIXTYPE == MIXTYPE_MONOEXPAND) ? frameCount : frameCount * t->mMixerChannelCount;
Andy Hung296b7412014-06-17 15:25:47 -07001872 t->in = in;
1873}
1874
1875/* The Mixer engine generates either int32_t (Q4_27) or float data.
1876 * We use this function to convert the engine buffers
1877 * to the desired mixer output format, either int16_t (Q.15) or float.
1878 */
1879void AudioMixer::convertMixerFormat(void *out, audio_format_t mixerOutFormat,
1880 void *in, audio_format_t mixerInFormat, size_t sampleCount)
1881{
1882 switch (mixerInFormat) {
1883 case AUDIO_FORMAT_PCM_FLOAT:
1884 switch (mixerOutFormat) {
1885 case AUDIO_FORMAT_PCM_FLOAT:
1886 memcpy(out, in, sampleCount * sizeof(float)); // MEMCPY. TODO optimize out
1887 break;
1888 case AUDIO_FORMAT_PCM_16_BIT:
1889 memcpy_to_i16_from_float((int16_t*)out, (float*)in, sampleCount);
1890 break;
1891 default:
1892 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat);
1893 break;
1894 }
1895 break;
1896 case AUDIO_FORMAT_PCM_16_BIT:
1897 switch (mixerOutFormat) {
1898 case AUDIO_FORMAT_PCM_FLOAT:
1899 memcpy_to_float_from_q4_27((float*)out, (int32_t*)in, sampleCount);
1900 break;
1901 case AUDIO_FORMAT_PCM_16_BIT:
1902 // two int16_t are produced per iteration
1903 ditherAndClamp((int32_t*)out, (int32_t*)in, sampleCount >> 1);
1904 break;
1905 default:
1906 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat);
1907 break;
1908 }
1909 break;
1910 default:
1911 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
1912 break;
1913 }
1914}
1915
1916/* Returns the proper track hook to use for mixing the track into the output buffer.
1917 */
Andy Hunge93b6b72014-07-17 21:30:53 -07001918AudioMixer::hook_t AudioMixer::getTrackHook(int trackType, uint32_t channelCount,
Andy Hung296b7412014-06-17 15:25:47 -07001919 audio_format_t mixerInFormat, audio_format_t mixerOutFormat __unused)
1920{
Andy Hunge93b6b72014-07-17 21:30:53 -07001921 if (!kUseNewMixer && channelCount == FCC_2 && mixerInFormat == AUDIO_FORMAT_PCM_16_BIT) {
Andy Hung296b7412014-06-17 15:25:47 -07001922 switch (trackType) {
1923 case TRACKTYPE_NOP:
1924 return track__nop;
1925 case TRACKTYPE_RESAMPLE:
1926 return track__genericResample;
1927 case TRACKTYPE_NORESAMPLEMONO:
1928 return track__16BitsMono;
1929 case TRACKTYPE_NORESAMPLE:
1930 return track__16BitsStereo;
1931 default:
1932 LOG_ALWAYS_FATAL("bad trackType: %d", trackType);
1933 break;
1934 }
1935 }
Andy Hunge93b6b72014-07-17 21:30:53 -07001936 LOG_ALWAYS_FATAL_IF(channelCount > MAX_NUM_CHANNELS);
Andy Hung296b7412014-06-17 15:25:47 -07001937 switch (trackType) {
1938 case TRACKTYPE_NOP:
1939 return track__nop;
1940 case TRACKTYPE_RESAMPLE:
1941 switch (mixerInFormat) {
1942 case AUDIO_FORMAT_PCM_FLOAT:
1943 return (AudioMixer::hook_t)
Andy Hunge93b6b72014-07-17 21:30:53 -07001944 track__Resample<MIXTYPE_MULTI, float /*TO*/, float /*TI*/, int32_t /*TA*/>;
Andy Hung296b7412014-06-17 15:25:47 -07001945 case AUDIO_FORMAT_PCM_16_BIT:
1946 return (AudioMixer::hook_t)\
Andy Hunge93b6b72014-07-17 21:30:53 -07001947 track__Resample<MIXTYPE_MULTI, int32_t, int16_t, int32_t>;
Andy Hung296b7412014-06-17 15:25:47 -07001948 default:
1949 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
1950 break;
1951 }
1952 break;
1953 case TRACKTYPE_NORESAMPLEMONO:
1954 switch (mixerInFormat) {
1955 case AUDIO_FORMAT_PCM_FLOAT:
1956 return (AudioMixer::hook_t)
Andy Hunge93b6b72014-07-17 21:30:53 -07001957 track__NoResample<MIXTYPE_MONOEXPAND, float, float, int32_t>;
Andy Hung296b7412014-06-17 15:25:47 -07001958 case AUDIO_FORMAT_PCM_16_BIT:
1959 return (AudioMixer::hook_t)
Andy Hunge93b6b72014-07-17 21:30:53 -07001960 track__NoResample<MIXTYPE_MONOEXPAND, int32_t, int16_t, int32_t>;
Andy Hung296b7412014-06-17 15:25:47 -07001961 default:
1962 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
1963 break;
1964 }
1965 break;
1966 case TRACKTYPE_NORESAMPLE:
1967 switch (mixerInFormat) {
1968 case AUDIO_FORMAT_PCM_FLOAT:
1969 return (AudioMixer::hook_t)
Andy Hunge93b6b72014-07-17 21:30:53 -07001970 track__NoResample<MIXTYPE_MULTI, float, float, int32_t>;
Andy Hung296b7412014-06-17 15:25:47 -07001971 case AUDIO_FORMAT_PCM_16_BIT:
1972 return (AudioMixer::hook_t)
Andy Hunge93b6b72014-07-17 21:30:53 -07001973 track__NoResample<MIXTYPE_MULTI, int32_t, int16_t, int32_t>;
Andy Hung296b7412014-06-17 15:25:47 -07001974 default:
1975 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
1976 break;
1977 }
1978 break;
1979 default:
1980 LOG_ALWAYS_FATAL("bad trackType: %d", trackType);
1981 break;
1982 }
1983 return NULL;
1984}
1985
1986/* Returns the proper process hook for mixing tracks. Currently works only for
1987 * PROCESSTYPE_NORESAMPLEONETRACK, a mix involving one track, no resampling.
Andy Hung395db4b2014-08-25 17:15:29 -07001988 *
1989 * TODO: Due to the special mixing considerations of duplicating to
1990 * a stereo output track, the input track cannot be MONO. This should be
1991 * prevented by the caller.
Andy Hung296b7412014-06-17 15:25:47 -07001992 */
Andy Hunge93b6b72014-07-17 21:30:53 -07001993AudioMixer::process_hook_t AudioMixer::getProcessHook(int processType, uint32_t channelCount,
Andy Hung296b7412014-06-17 15:25:47 -07001994 audio_format_t mixerInFormat, audio_format_t mixerOutFormat)
1995{
1996 if (processType != PROCESSTYPE_NORESAMPLEONETRACK) { // Only NORESAMPLEONETRACK
1997 LOG_ALWAYS_FATAL("bad processType: %d", processType);
1998 return NULL;
1999 }
Andy Hunge93b6b72014-07-17 21:30:53 -07002000 if (!kUseNewMixer && channelCount == FCC_2 && mixerInFormat == AUDIO_FORMAT_PCM_16_BIT) {
Andy Hung296b7412014-06-17 15:25:47 -07002001 return process__OneTrack16BitsStereoNoResampling;
2002 }
Andy Hunge93b6b72014-07-17 21:30:53 -07002003 LOG_ALWAYS_FATAL_IF(channelCount > MAX_NUM_CHANNELS);
Andy Hung296b7412014-06-17 15:25:47 -07002004 switch (mixerInFormat) {
2005 case AUDIO_FORMAT_PCM_FLOAT:
2006 switch (mixerOutFormat) {
2007 case AUDIO_FORMAT_PCM_FLOAT:
Andy Hunge93b6b72014-07-17 21:30:53 -07002008 return process_NoResampleOneTrack<MIXTYPE_MULTI_SAVEONLY,
2009 float /*TO*/, float /*TI*/, int32_t /*TA*/>;
Andy Hung296b7412014-06-17 15:25:47 -07002010 case AUDIO_FORMAT_PCM_16_BIT:
Andy Hunge93b6b72014-07-17 21:30:53 -07002011 return process_NoResampleOneTrack<MIXTYPE_MULTI_SAVEONLY,
Andy Hung296b7412014-06-17 15:25:47 -07002012 int16_t, float, int32_t>;
2013 default:
2014 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat);
2015 break;
2016 }
2017 break;
2018 case AUDIO_FORMAT_PCM_16_BIT:
2019 switch (mixerOutFormat) {
2020 case AUDIO_FORMAT_PCM_FLOAT:
Andy Hunge93b6b72014-07-17 21:30:53 -07002021 return process_NoResampleOneTrack<MIXTYPE_MULTI_SAVEONLY,
Andy Hung296b7412014-06-17 15:25:47 -07002022 float, int16_t, int32_t>;
2023 case AUDIO_FORMAT_PCM_16_BIT:
Andy Hunge93b6b72014-07-17 21:30:53 -07002024 return process_NoResampleOneTrack<MIXTYPE_MULTI_SAVEONLY,
Andy Hung296b7412014-06-17 15:25:47 -07002025 int16_t, int16_t, int32_t>;
2026 default:
2027 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat);
2028 break;
2029 }
2030 break;
2031 default:
2032 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
2033 break;
2034 }
2035 return NULL;
2036}
2037
Mathias Agopian65ab4712010-07-14 17:59:35 -07002038// ----------------------------------------------------------------------------
Glenn Kasten63238ef2015-03-02 15:50:29 -08002039} // namespace android