blob: cd47dc68514c6f797801b76d592c6a88b7fdbc9a [file] [log] [blame]
Andy Hung296b7412014-06-17 15:25:47 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_AUDIO_MIXER_OPS_H
18#define ANDROID_AUDIO_MIXER_OPS_H
19
Andy Hung936845a2021-06-08 00:09:06 -070020#include <system/audio.h>
21
Andy Hung296b7412014-06-17 15:25:47 -070022namespace android {
23
Judy Hsiao19e533c2019-08-14 16:52:51 +080024// Hack to make static_assert work in a constexpr
25// https://en.cppreference.com/w/cpp/language/if
26template <int N>
27inline constexpr bool dependent_false = false;
Andy Hung296b7412014-06-17 15:25:47 -070028
29/* MixMul is a multiplication operator to scale an audio input signal
30 * by a volume gain, with the formula:
31 *
32 * O(utput) = I(nput) * V(olume)
33 *
34 * The output, input, and volume may have different types.
35 * There are 27 variants, of which 14 are actually defined in an
36 * explicitly templated class.
37 *
38 * The following type variables and the underlying meaning:
39 *
40 * Output type TO: int32_t (Q4.27) or int16_t (Q.15) or float [-1,1]
41 * Input signal type TI: int32_t (Q4.27) or int16_t (Q.15) or float [-1,1]
42 * Volume type TV: int32_t (U4.28) or int16_t (U4.12) or float [-1,1]
43 *
44 * For high precision audio, only the <TO, TI, TV> = <float, float, float>
45 * needs to be accelerated. This is perhaps the easiest form to do quickly as well.
Chih-Hung Hsieh65025402014-12-11 13:06:46 -080046 *
47 * A generic version is NOT defined to catch any mistake of using it.
Andy Hung296b7412014-06-17 15:25:47 -070048 */
49
50template <typename TO, typename TI, typename TV>
Chih-Hung Hsieh65025402014-12-11 13:06:46 -080051TO MixMul(TI value, TV volume);
Andy Hung296b7412014-06-17 15:25:47 -070052
53template <>
54inline int32_t MixMul<int32_t, int16_t, int16_t>(int16_t value, int16_t volume) {
55 return value * volume;
56}
57
58template <>
59inline int32_t MixMul<int32_t, int32_t, int16_t>(int32_t value, int16_t volume) {
60 return (value >> 12) * volume;
61}
62
63template <>
64inline int32_t MixMul<int32_t, int16_t, int32_t>(int16_t value, int32_t volume) {
65 return value * (volume >> 16);
66}
67
68template <>
69inline int32_t MixMul<int32_t, int32_t, int32_t>(int32_t value, int32_t volume) {
70 return (value >> 12) * (volume >> 16);
71}
72
73template <>
74inline float MixMul<float, float, int16_t>(float value, int16_t volume) {
75 static const float norm = 1. / (1 << 12);
76 return value * volume * norm;
77}
78
79template <>
80inline float MixMul<float, float, int32_t>(float value, int32_t volume) {
81 static const float norm = 1. / (1 << 28);
82 return value * volume * norm;
83}
84
85template <>
86inline int16_t MixMul<int16_t, float, int16_t>(float value, int16_t volume) {
87 return clamp16_from_float(MixMul<float, float, int16_t>(value, volume));
88}
89
90template <>
91inline int16_t MixMul<int16_t, float, int32_t>(float value, int32_t volume) {
92 return clamp16_from_float(MixMul<float, float, int32_t>(value, volume));
93}
94
95template <>
96inline float MixMul<float, int16_t, int16_t>(int16_t value, int16_t volume) {
97 static const float norm = 1. / (1 << (15 + 12));
98 return static_cast<float>(value) * static_cast<float>(volume) * norm;
99}
100
101template <>
102inline float MixMul<float, int16_t, int32_t>(int16_t value, int32_t volume) {
103 static const float norm = 1. / (1ULL << (15 + 28));
104 return static_cast<float>(value) * static_cast<float>(volume) * norm;
105}
106
107template <>
108inline int16_t MixMul<int16_t, int16_t, int16_t>(int16_t value, int16_t volume) {
109 return clamp16(MixMul<int32_t, int16_t, int16_t>(value, volume) >> 12);
110}
111
112template <>
113inline int16_t MixMul<int16_t, int32_t, int16_t>(int32_t value, int16_t volume) {
114 return clamp16(MixMul<int32_t, int32_t, int16_t>(value, volume) >> 12);
115}
116
117template <>
118inline int16_t MixMul<int16_t, int16_t, int32_t>(int16_t value, int32_t volume) {
119 return clamp16(MixMul<int32_t, int16_t, int32_t>(value, volume) >> 12);
120}
121
122template <>
123inline int16_t MixMul<int16_t, int32_t, int32_t>(int32_t value, int32_t volume) {
124 return clamp16(MixMul<int32_t, int32_t, int32_t>(value, volume) >> 12);
125}
126
Andy Hung5e58b0a2014-06-23 19:07:29 -0700127/* Required for floating point volume. Some are needed for compilation but
128 * are not needed in execution and should be removed from the final build by
129 * an optimizing compiler.
130 */
131template <>
132inline float MixMul<float, float, float>(float value, float volume) {
133 return value * volume;
134}
135
136template <>
137inline float MixMul<float, int16_t, float>(int16_t value, float volume) {
138 static const float float_from_q_15 = 1. / (1 << 15);
139 return value * volume * float_from_q_15;
140}
141
142template <>
143inline int32_t MixMul<int32_t, int32_t, float>(int32_t value, float volume) {
144 LOG_ALWAYS_FATAL("MixMul<int32_t, int32_t, float> Runtime Should not be here");
145 return value * volume;
146}
147
148template <>
149inline int32_t MixMul<int32_t, int16_t, float>(int16_t value, float volume) {
150 LOG_ALWAYS_FATAL("MixMul<int32_t, int16_t, float> Runtime Should not be here");
151 static const float u4_12_from_float = (1 << 12);
152 return value * volume * u4_12_from_float;
153}
154
155template <>
156inline int16_t MixMul<int16_t, int16_t, float>(int16_t value, float volume) {
157 LOG_ALWAYS_FATAL("MixMul<int16_t, int16_t, float> Runtime Should not be here");
Andy Hung83ffcfb2015-06-18 17:34:40 -0700158 return clamp16_from_float(MixMul<float, int16_t, float>(value, volume));
Andy Hung5e58b0a2014-06-23 19:07:29 -0700159}
160
161template <>
162inline int16_t MixMul<int16_t, float, float>(float value, float volume) {
Andy Hung83ffcfb2015-06-18 17:34:40 -0700163 return clamp16_from_float(value * volume);
Andy Hung5e58b0a2014-06-23 19:07:29 -0700164}
165
Andy Hung296b7412014-06-17 15:25:47 -0700166/*
167 * MixAccum is used to add into an accumulator register of a possibly different
168 * type. The TO and TI types are the same as MixMul.
169 */
170
171template <typename TO, typename TI>
172inline void MixAccum(TO *auxaccum, TI value) {
Judy Hsiao19e533c2019-08-14 16:52:51 +0800173 if (!std::is_same_v<TO, TI>) {
Glenn Kastena4daf0b2014-07-28 16:34:45 -0700174 LOG_ALWAYS_FATAL("MixAccum type not properly specialized: %zu %zu\n",
Andy Hung296b7412014-06-17 15:25:47 -0700175 sizeof(TO), sizeof(TI));
176 }
177 *auxaccum += value;
178}
179
180template<>
181inline void MixAccum<float, int16_t>(float *auxaccum, int16_t value) {
Andy Hung116a4982017-11-30 10:15:08 -0800182 static constexpr float norm = 1. / (1 << 15);
Andy Hung296b7412014-06-17 15:25:47 -0700183 *auxaccum += norm * value;
184}
185
186template<>
187inline void MixAccum<float, int32_t>(float *auxaccum, int32_t value) {
Andy Hung116a4982017-11-30 10:15:08 -0800188 static constexpr float norm = 1. / (1 << 27);
Andy Hung296b7412014-06-17 15:25:47 -0700189 *auxaccum += norm * value;
190}
191
192template<>
193inline void MixAccum<int32_t, int16_t>(int32_t *auxaccum, int16_t value) {
194 *auxaccum += value << 12;
195}
196
197template<>
198inline void MixAccum<int32_t, float>(int32_t *auxaccum, float value) {
199 *auxaccum += clampq4_27_from_float(value);
200}
201
202/* MixMulAux is just like MixMul except it combines with
203 * an accumulator operation MixAccum.
204 */
205
206template <typename TO, typename TI, typename TV, typename TA>
207inline TO MixMulAux(TI value, TV volume, TA *auxaccum) {
208 MixAccum<TA, TI>(auxaccum, value);
209 return MixMul<TO, TI, TV>(value, volume);
210}
211
212/* MIXTYPE is used to determine how the samples in the input frame
213 * are mixed with volume gain into the output frame.
214 * See the volumeRampMulti functions below for more details.
215 */
216enum {
217 MIXTYPE_MULTI,
218 MIXTYPE_MONOEXPAND,
219 MIXTYPE_MULTI_SAVEONLY,
Andy Hunge93b6b72014-07-17 21:30:53 -0700220 MIXTYPE_MULTI_MONOVOL,
221 MIXTYPE_MULTI_SAVEONLY_MONOVOL,
Judy Hsiao19e533c2019-08-14 16:52:51 +0800222 MIXTYPE_MULTI_STEREOVOL,
223 MIXTYPE_MULTI_SAVEONLY_STEREOVOL,
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800224 MIXTYPE_STEREOEXPAND,
Andy Hung296b7412014-06-17 15:25:47 -0700225};
226
227/*
Judy Hsiao19e533c2019-08-14 16:52:51 +0800228 * TODO: We should work on non-interleaved streams - the
229 * complexity of working on interleaved streams is now getting
230 * too high, and likely limits compiler optimization.
231 */
232template <int MIXTYPE, int NCHAN,
233 typename TO, typename TI, typename TV,
234 typename F>
235void stereoVolumeHelper(TO*& out, const TI*& in, const TV *vol, F f) {
Andy Hung936845a2021-06-08 00:09:06 -0700236 static_assert(NCHAN > 0 && NCHAN <= FCC_LIMIT);
Judy Hsiao19e533c2019-08-14 16:52:51 +0800237 static_assert(MIXTYPE == MIXTYPE_MULTI_STEREOVOL
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800238 || MIXTYPE == MIXTYPE_MULTI_SAVEONLY_STEREOVOL
Andy Hung2d8397a2020-07-14 00:56:03 -0700239 || MIXTYPE == MIXTYPE_STEREOEXPAND
240 || MIXTYPE == MIXTYPE_MONOEXPAND);
Judy Hsiao19e533c2019-08-14 16:52:51 +0800241 auto proc = [](auto& a, const auto& b) {
Andy Hunge0c3d792020-06-01 09:41:24 -0700242 if constexpr (MIXTYPE == MIXTYPE_MULTI_STEREOVOL
Andy Hung2d8397a2020-07-14 00:56:03 -0700243 || MIXTYPE == MIXTYPE_STEREOEXPAND
244 || MIXTYPE == MIXTYPE_MONOEXPAND) {
Judy Hsiao19e533c2019-08-14 16:52:51 +0800245 a += b;
246 } else {
247 a = b;
248 }
249 };
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800250 auto inp = [&in]() -> const TI& {
Andy Hung2d8397a2020-07-14 00:56:03 -0700251 if constexpr (MIXTYPE == MIXTYPE_STEREOEXPAND
252 || MIXTYPE == MIXTYPE_MONOEXPAND) {
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800253 return *in;
254 } else {
255 return *in++;
256 }
257 };
258
Judy Hsiao19e533c2019-08-14 16:52:51 +0800259 // HALs should only expose the canonical channel masks.
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800260 proc(*out++, f(inp(), vol[0])); // front left
Judy Hsiao19e533c2019-08-14 16:52:51 +0800261 if constexpr (NCHAN == 1) return;
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800262 proc(*out++, f(inp(), vol[1])); // front right
Judy Hsiao19e533c2019-08-14 16:52:51 +0800263 if constexpr (NCHAN == 2) return;
264 if constexpr (NCHAN == 4) {
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800265 proc(*out++, f(inp(), vol[0])); // back left
266 proc(*out++, f(inp(), vol[1])); // back right
Judy Hsiao19e533c2019-08-14 16:52:51 +0800267 return;
268 }
269
270 // TODO: Precompute center volume if not ramping.
271 std::decay_t<TV> center;
272 if constexpr (std::is_floating_point_v<TV>) {
273 center = (vol[0] + vol[1]) * 0.5; // do not use divide
274 } else {
275 center = (vol[0] >> 1) + (vol[1] >> 1); // rounds to 0.
276 }
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800277 proc(*out++, f(inp(), center)); // center (or 2.1 LFE)
Judy Hsiao19e533c2019-08-14 16:52:51 +0800278 if constexpr (NCHAN == 3) return;
279 if constexpr (NCHAN == 5) {
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800280 proc(*out++, f(inp(), vol[0])); // back left
281 proc(*out++, f(inp(), vol[1])); // back right
Judy Hsiao19e533c2019-08-14 16:52:51 +0800282 return;
283 }
284
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800285 proc(*out++, f(inp(), center)); // lfe
286 proc(*out++, f(inp(), vol[0])); // back left
287 proc(*out++, f(inp(), vol[1])); // back right
Judy Hsiao19e533c2019-08-14 16:52:51 +0800288 if constexpr (NCHAN == 6) return;
289 if constexpr (NCHAN == 7) {
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800290 proc(*out++, f(inp(), center)); // back center
Judy Hsiao19e533c2019-08-14 16:52:51 +0800291 return;
292 }
293 // NCHAN == 8
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800294 proc(*out++, f(inp(), vol[0])); // side left
295 proc(*out++, f(inp(), vol[1])); // side right
Andy Hung1b998522021-06-07 16:43:58 -0700296 if constexpr (NCHAN > FCC_8) {
297 // Mutes to zero extended surround channels.
298 // 7.1.4 has the correct behavior.
299 // 22.2 has the behavior that FLC and FRC will be mixed instead
300 // of SL and SR and LFE will be center, not left.
301 for (int i = 8; i < NCHAN; ++i) {
302 // TODO: Consider using android::audio_utils::channels::kSideFromChannelIdx
303 proc(*out++, f(inp(), 0.f));
304 }
305 }
Judy Hsiao19e533c2019-08-14 16:52:51 +0800306}
307
308/*
Andy Hung296b7412014-06-17 15:25:47 -0700309 * The volumeRampMulti and volumeRamp functions take a MIXTYPE
310 * which indicates the per-frame mixing and accumulation strategy.
311 *
312 * MIXTYPE_MULTI:
313 * NCHAN represents number of input and output channels.
314 * TO: int32_t (Q4.27) or float
315 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
Andy Hung116a4982017-11-30 10:15:08 -0800316 * TA: int32_t (Q4.27) or float
Andy Hung296b7412014-06-17 15:25:47 -0700317 * TV: int32_t (U4.28) or int16_t (U4.12) or float
318 * vol: represents a volume array.
319 *
320 * This accumulates into the out pointer.
321 *
322 * MIXTYPE_MONOEXPAND:
323 * Single input channel. NCHAN represents number of output channels.
324 * TO: int32_t (Q4.27) or float
325 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
Andy Hung116a4982017-11-30 10:15:08 -0800326 * TA: int32_t (Q4.27) or float
327 * TV/TAV: int32_t (U4.28) or int16_t (U4.12) or float
Andy Hung296b7412014-06-17 15:25:47 -0700328 * Input channel count is 1.
329 * vol: represents volume array.
Andy Hung2d8397a2020-07-14 00:56:03 -0700330 * This uses stereo balanced volume vol[0] and vol[1].
331 * Before R, this was a full volume array but was called only for channels <= 2.
Andy Hung296b7412014-06-17 15:25:47 -0700332 *
333 * This accumulates into the out pointer.
334 *
335 * MIXTYPE_MULTI_SAVEONLY:
336 * NCHAN represents number of input and output channels.
337 * TO: int16_t (Q.15) or float
338 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
Andy Hung116a4982017-11-30 10:15:08 -0800339 * TA: int32_t (Q4.27) or float
340 * TV/TAV: int32_t (U4.28) or int16_t (U4.12) or float
Andy Hung296b7412014-06-17 15:25:47 -0700341 * vol: represents a volume array.
342 *
343 * MIXTYPE_MULTI_SAVEONLY does not accumulate into the out pointer.
Andy Hunge93b6b72014-07-17 21:30:53 -0700344 *
345 * MIXTYPE_MULTI_MONOVOL:
346 * Same as MIXTYPE_MULTI, but uses only volume[0].
347 *
348 * MIXTYPE_MULTI_SAVEONLY_MONOVOL:
349 * Same as MIXTYPE_MULTI_SAVEONLY, but uses only volume[0].
350 *
Judy Hsiao19e533c2019-08-14 16:52:51 +0800351 * MIXTYPE_MULTI_STEREOVOL:
352 * Same as MIXTYPE_MULTI, but uses only volume[0] and volume[1].
353 *
354 * MIXTYPE_MULTI_SAVEONLY_STEREOVOL:
355 * Same as MIXTYPE_MULTI_SAVEONLY, but uses only volume[0] and volume[1].
356 *
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800357 * MIXTYPE_STEREOEXPAND:
358 * Stereo input channel. NCHAN represents number of output channels.
359 * Expand size 2 array "in" and "vol" to multi-channel output. Note
360 * that the 2 array is assumed to have replicated L+R.
361 *
Andy Hung296b7412014-06-17 15:25:47 -0700362 */
363
364template <int MIXTYPE, int NCHAN,
365 typename TO, typename TI, typename TV, typename TA, typename TAV>
366inline void volumeRampMulti(TO* out, size_t frameCount,
367 const TI* in, TA* aux, TV *vol, const TV *volinc, TAV *vola, TAV volainc)
368{
369#ifdef ALOGVV
370 ALOGVV("volumeRampMulti, MIXTYPE:%d\n", MIXTYPE);
371#endif
372 if (aux != NULL) {
373 do {
374 TA auxaccum = 0;
Judy Hsiao19e533c2019-08-14 16:52:51 +0800375 if constexpr (MIXTYPE == MIXTYPE_MULTI) {
Andy Hung2d8397a2020-07-14 00:56:03 -0700376 static_assert(NCHAN <= 2);
Andy Hung296b7412014-06-17 15:25:47 -0700377 for (int i = 0; i < NCHAN; ++i) {
378 *out++ += MixMulAux<TO, TI, TV, TA>(*in++, vol[i], &auxaccum);
379 vol[i] += volinc[i];
380 }
Judy Hsiao19e533c2019-08-14 16:52:51 +0800381 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_SAVEONLY) {
Andy Hung2d8397a2020-07-14 00:56:03 -0700382 static_assert(NCHAN <= 2);
Andy Hunge93b6b72014-07-17 21:30:53 -0700383 for (int i = 0; i < NCHAN; ++i) {
384 *out++ = MixMulAux<TO, TI, TV, TA>(*in++, vol[i], &auxaccum);
385 vol[i] += volinc[i];
386 }
Judy Hsiao19e533c2019-08-14 16:52:51 +0800387 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_MONOVOL) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700388 for (int i = 0; i < NCHAN; ++i) {
389 *out++ += MixMulAux<TO, TI, TV, TA>(*in++, vol[0], &auxaccum);
390 }
391 vol[0] += volinc[0];
Judy Hsiao19e533c2019-08-14 16:52:51 +0800392 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_SAVEONLY_MONOVOL) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700393 for (int i = 0; i < NCHAN; ++i) {
394 *out++ = MixMulAux<TO, TI, TV, TA>(*in++, vol[0], &auxaccum);
395 }
396 vol[0] += volinc[0];
Judy Hsiao19e533c2019-08-14 16:52:51 +0800397 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_STEREOVOL
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800398 || MIXTYPE == MIXTYPE_MULTI_SAVEONLY_STEREOVOL
Andy Hung2d8397a2020-07-14 00:56:03 -0700399 || MIXTYPE == MIXTYPE_MONOEXPAND
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800400 || MIXTYPE == MIXTYPE_STEREOEXPAND) {
Judy Hsiao19e533c2019-08-14 16:52:51 +0800401 stereoVolumeHelper<MIXTYPE, NCHAN>(
402 out, in, vol, [&auxaccum] (auto &a, const auto &b) {
403 return MixMulAux<TO, TI, TV, TA>(a, b, &auxaccum);
404 });
Andy Hung2d8397a2020-07-14 00:56:03 -0700405 if constexpr (MIXTYPE == MIXTYPE_MONOEXPAND) in += 1;
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800406 if constexpr (MIXTYPE == MIXTYPE_STEREOEXPAND) in += 2;
Judy Hsiao19e533c2019-08-14 16:52:51 +0800407 vol[0] += volinc[0];
408 vol[1] += volinc[1];
409 } else /* constexpr */ {
410 static_assert(dependent_false<MIXTYPE>, "invalid mixtype");
Andy Hung296b7412014-06-17 15:25:47 -0700411 }
412 auxaccum /= NCHAN;
413 *aux++ += MixMul<TA, TA, TAV>(auxaccum, *vola);
414 vola[0] += volainc;
415 } while (--frameCount);
416 } else {
417 do {
Judy Hsiao19e533c2019-08-14 16:52:51 +0800418 if constexpr (MIXTYPE == MIXTYPE_MULTI) {
Andy Hung2d8397a2020-07-14 00:56:03 -0700419 static_assert(NCHAN <= 2);
Andy Hung296b7412014-06-17 15:25:47 -0700420 for (int i = 0; i < NCHAN; ++i) {
421 *out++ += MixMul<TO, TI, TV>(*in++, vol[i]);
422 vol[i] += volinc[i];
423 }
Judy Hsiao19e533c2019-08-14 16:52:51 +0800424 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_SAVEONLY) {
Andy Hung2d8397a2020-07-14 00:56:03 -0700425 static_assert(NCHAN <= 2);
Andy Hunge93b6b72014-07-17 21:30:53 -0700426 for (int i = 0; i < NCHAN; ++i) {
427 *out++ = MixMul<TO, TI, TV>(*in++, vol[i]);
428 vol[i] += volinc[i];
429 }
Judy Hsiao19e533c2019-08-14 16:52:51 +0800430 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_MONOVOL) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700431 for (int i = 0; i < NCHAN; ++i) {
432 *out++ += MixMul<TO, TI, TV>(*in++, vol[0]);
433 }
434 vol[0] += volinc[0];
Judy Hsiao19e533c2019-08-14 16:52:51 +0800435 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_SAVEONLY_MONOVOL) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700436 for (int i = 0; i < NCHAN; ++i) {
437 *out++ = MixMul<TO, TI, TV>(*in++, vol[0]);
438 }
439 vol[0] += volinc[0];
Judy Hsiao19e533c2019-08-14 16:52:51 +0800440 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_STEREOVOL
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800441 || MIXTYPE == MIXTYPE_MULTI_SAVEONLY_STEREOVOL
Andy Hung2d8397a2020-07-14 00:56:03 -0700442 || MIXTYPE == MIXTYPE_MONOEXPAND
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800443 || MIXTYPE == MIXTYPE_STEREOEXPAND) {
Judy Hsiao19e533c2019-08-14 16:52:51 +0800444 stereoVolumeHelper<MIXTYPE, NCHAN>(out, in, vol, [] (auto &a, const auto &b) {
445 return MixMul<TO, TI, TV>(a, b);
446 });
Andy Hung2d8397a2020-07-14 00:56:03 -0700447 if constexpr (MIXTYPE == MIXTYPE_MONOEXPAND) in += 1;
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800448 if constexpr (MIXTYPE == MIXTYPE_STEREOEXPAND) in += 2;
Judy Hsiao19e533c2019-08-14 16:52:51 +0800449 vol[0] += volinc[0];
450 vol[1] += volinc[1];
451 } else /* constexpr */ {
452 static_assert(dependent_false<MIXTYPE>, "invalid mixtype");
Andy Hung296b7412014-06-17 15:25:47 -0700453 }
454 } while (--frameCount);
455 }
456}
457
458template <int MIXTYPE, int NCHAN,
459 typename TO, typename TI, typename TV, typename TA, typename TAV>
460inline void volumeMulti(TO* out, size_t frameCount,
461 const TI* in, TA* aux, const TV *vol, TAV vola)
462{
463#ifdef ALOGVV
464 ALOGVV("volumeMulti MIXTYPE:%d\n", MIXTYPE);
465#endif
466 if (aux != NULL) {
467 do {
468 TA auxaccum = 0;
Judy Hsiao19e533c2019-08-14 16:52:51 +0800469 if constexpr (MIXTYPE == MIXTYPE_MULTI) {
Andy Hung2d8397a2020-07-14 00:56:03 -0700470 static_assert(NCHAN <= 2);
Andy Hung296b7412014-06-17 15:25:47 -0700471 for (int i = 0; i < NCHAN; ++i) {
472 *out++ += MixMulAux<TO, TI, TV, TA>(*in++, vol[i], &auxaccum);
473 }
Judy Hsiao19e533c2019-08-14 16:52:51 +0800474 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_SAVEONLY) {
Andy Hung2d8397a2020-07-14 00:56:03 -0700475 static_assert(NCHAN <= 2);
Andy Hunge93b6b72014-07-17 21:30:53 -0700476 for (int i = 0; i < NCHAN; ++i) {
477 *out++ = MixMulAux<TO, TI, TV, TA>(*in++, vol[i], &auxaccum);
478 }
Judy Hsiao19e533c2019-08-14 16:52:51 +0800479 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_MONOVOL) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700480 for (int i = 0; i < NCHAN; ++i) {
481 *out++ += MixMulAux<TO, TI, TV, TA>(*in++, vol[0], &auxaccum);
482 }
Judy Hsiao19e533c2019-08-14 16:52:51 +0800483 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_SAVEONLY_MONOVOL) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700484 for (int i = 0; i < NCHAN; ++i) {
485 *out++ = MixMulAux<TO, TI, TV, TA>(*in++, vol[0], &auxaccum);
486 }
Judy Hsiao19e533c2019-08-14 16:52:51 +0800487 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_STEREOVOL
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800488 || MIXTYPE == MIXTYPE_MULTI_SAVEONLY_STEREOVOL
Andy Hung2d8397a2020-07-14 00:56:03 -0700489 || MIXTYPE == MIXTYPE_MONOEXPAND
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800490 || MIXTYPE == MIXTYPE_STEREOEXPAND) {
Judy Hsiao19e533c2019-08-14 16:52:51 +0800491 stereoVolumeHelper<MIXTYPE, NCHAN>(
492 out, in, vol, [&auxaccum] (auto &a, const auto &b) {
493 return MixMulAux<TO, TI, TV, TA>(a, b, &auxaccum);
494 });
Andy Hung2d8397a2020-07-14 00:56:03 -0700495 if constexpr (MIXTYPE == MIXTYPE_MONOEXPAND) in += 1;
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800496 if constexpr (MIXTYPE == MIXTYPE_STEREOEXPAND) in += 2;
Judy Hsiao19e533c2019-08-14 16:52:51 +0800497 } else /* constexpr */ {
498 static_assert(dependent_false<MIXTYPE>, "invalid mixtype");
Andy Hung296b7412014-06-17 15:25:47 -0700499 }
500 auxaccum /= NCHAN;
501 *aux++ += MixMul<TA, TA, TAV>(auxaccum, vola);
502 } while (--frameCount);
503 } else {
504 do {
Andy Hung2d8397a2020-07-14 00:56:03 -0700505 // ALOGD("Mixtype:%d NCHAN:%d", MIXTYPE, NCHAN);
Judy Hsiao19e533c2019-08-14 16:52:51 +0800506 if constexpr (MIXTYPE == MIXTYPE_MULTI) {
Andy Hung2d8397a2020-07-14 00:56:03 -0700507 static_assert(NCHAN <= 2);
Andy Hung296b7412014-06-17 15:25:47 -0700508 for (int i = 0; i < NCHAN; ++i) {
509 *out++ += MixMul<TO, TI, TV>(*in++, vol[i]);
510 }
Judy Hsiao19e533c2019-08-14 16:52:51 +0800511 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_SAVEONLY) {
Andy Hung2d8397a2020-07-14 00:56:03 -0700512 static_assert(NCHAN <= 2);
Andy Hunge93b6b72014-07-17 21:30:53 -0700513 for (int i = 0; i < NCHAN; ++i) {
514 *out++ = MixMul<TO, TI, TV>(*in++, vol[i]);
515 }
Judy Hsiao19e533c2019-08-14 16:52:51 +0800516 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_MONOVOL) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700517 for (int i = 0; i < NCHAN; ++i) {
518 *out++ += MixMul<TO, TI, TV>(*in++, vol[0]);
519 }
Judy Hsiao19e533c2019-08-14 16:52:51 +0800520 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_SAVEONLY_MONOVOL) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700521 for (int i = 0; i < NCHAN; ++i) {
522 *out++ = MixMul<TO, TI, TV>(*in++, vol[0]);
523 }
Judy Hsiao19e533c2019-08-14 16:52:51 +0800524 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_STEREOVOL
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800525 || MIXTYPE == MIXTYPE_MULTI_SAVEONLY_STEREOVOL
Andy Hung2d8397a2020-07-14 00:56:03 -0700526 || MIXTYPE == MIXTYPE_MONOEXPAND
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800527 || MIXTYPE == MIXTYPE_STEREOEXPAND) {
Judy Hsiao19e533c2019-08-14 16:52:51 +0800528 stereoVolumeHelper<MIXTYPE, NCHAN>(out, in, vol, [] (auto &a, const auto &b) {
529 return MixMul<TO, TI, TV>(a, b);
530 });
Andy Hung2d8397a2020-07-14 00:56:03 -0700531 if constexpr (MIXTYPE == MIXTYPE_MONOEXPAND) in += 1;
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800532 if constexpr (MIXTYPE == MIXTYPE_STEREOEXPAND) in += 2;
Judy Hsiao19e533c2019-08-14 16:52:51 +0800533 } else /* constexpr */ {
534 static_assert(dependent_false<MIXTYPE>, "invalid mixtype");
Andy Hung296b7412014-06-17 15:25:47 -0700535 }
536 } while (--frameCount);
537 }
538}
539
540};
541
542#endif /* ANDROID_AUDIO_MIXER_OPS_H */