blob: 80bd093efd626174a5b0b77c88da3663fdcdd5f8 [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
20namespace android {
21
Judy Hsiao19e533c2019-08-14 16:52:51 +080022// Hack to make static_assert work in a constexpr
23// https://en.cppreference.com/w/cpp/language/if
24template <int N>
25inline constexpr bool dependent_false = false;
Andy Hung296b7412014-06-17 15:25:47 -070026
27/* MixMul is a multiplication operator to scale an audio input signal
28 * by a volume gain, with the formula:
29 *
30 * O(utput) = I(nput) * V(olume)
31 *
32 * The output, input, and volume may have different types.
33 * There are 27 variants, of which 14 are actually defined in an
34 * explicitly templated class.
35 *
36 * The following type variables and the underlying meaning:
37 *
38 * Output type TO: int32_t (Q4.27) or int16_t (Q.15) or float [-1,1]
39 * Input signal type TI: int32_t (Q4.27) or int16_t (Q.15) or float [-1,1]
40 * Volume type TV: int32_t (U4.28) or int16_t (U4.12) or float [-1,1]
41 *
42 * For high precision audio, only the <TO, TI, TV> = <float, float, float>
43 * needs to be accelerated. This is perhaps the easiest form to do quickly as well.
Chih-Hung Hsieh65025402014-12-11 13:06:46 -080044 *
45 * A generic version is NOT defined to catch any mistake of using it.
Andy Hung296b7412014-06-17 15:25:47 -070046 */
47
48template <typename TO, typename TI, typename TV>
Chih-Hung Hsieh65025402014-12-11 13:06:46 -080049TO MixMul(TI value, TV volume);
Andy Hung296b7412014-06-17 15:25:47 -070050
51template <>
52inline int32_t MixMul<int32_t, int16_t, int16_t>(int16_t value, int16_t volume) {
53 return value * volume;
54}
55
56template <>
57inline int32_t MixMul<int32_t, int32_t, int16_t>(int32_t value, int16_t volume) {
58 return (value >> 12) * volume;
59}
60
61template <>
62inline int32_t MixMul<int32_t, int16_t, int32_t>(int16_t value, int32_t volume) {
63 return value * (volume >> 16);
64}
65
66template <>
67inline int32_t MixMul<int32_t, int32_t, int32_t>(int32_t value, int32_t volume) {
68 return (value >> 12) * (volume >> 16);
69}
70
71template <>
72inline float MixMul<float, float, int16_t>(float value, int16_t volume) {
73 static const float norm = 1. / (1 << 12);
74 return value * volume * norm;
75}
76
77template <>
78inline float MixMul<float, float, int32_t>(float value, int32_t volume) {
79 static const float norm = 1. / (1 << 28);
80 return value * volume * norm;
81}
82
83template <>
84inline int16_t MixMul<int16_t, float, int16_t>(float value, int16_t volume) {
85 return clamp16_from_float(MixMul<float, float, int16_t>(value, volume));
86}
87
88template <>
89inline int16_t MixMul<int16_t, float, int32_t>(float value, int32_t volume) {
90 return clamp16_from_float(MixMul<float, float, int32_t>(value, volume));
91}
92
93template <>
94inline float MixMul<float, int16_t, int16_t>(int16_t value, int16_t volume) {
95 static const float norm = 1. / (1 << (15 + 12));
96 return static_cast<float>(value) * static_cast<float>(volume) * norm;
97}
98
99template <>
100inline float MixMul<float, int16_t, int32_t>(int16_t value, int32_t volume) {
101 static const float norm = 1. / (1ULL << (15 + 28));
102 return static_cast<float>(value) * static_cast<float>(volume) * norm;
103}
104
105template <>
106inline int16_t MixMul<int16_t, int16_t, int16_t>(int16_t value, int16_t volume) {
107 return clamp16(MixMul<int32_t, int16_t, int16_t>(value, volume) >> 12);
108}
109
110template <>
111inline int16_t MixMul<int16_t, int32_t, int16_t>(int32_t value, int16_t volume) {
112 return clamp16(MixMul<int32_t, int32_t, int16_t>(value, volume) >> 12);
113}
114
115template <>
116inline int16_t MixMul<int16_t, int16_t, int32_t>(int16_t value, int32_t volume) {
117 return clamp16(MixMul<int32_t, int16_t, int32_t>(value, volume) >> 12);
118}
119
120template <>
121inline int16_t MixMul<int16_t, int32_t, int32_t>(int32_t value, int32_t volume) {
122 return clamp16(MixMul<int32_t, int32_t, int32_t>(value, volume) >> 12);
123}
124
Andy Hung5e58b0a2014-06-23 19:07:29 -0700125/* Required for floating point volume. Some are needed for compilation but
126 * are not needed in execution and should be removed from the final build by
127 * an optimizing compiler.
128 */
129template <>
130inline float MixMul<float, float, float>(float value, float volume) {
131 return value * volume;
132}
133
134template <>
135inline float MixMul<float, int16_t, float>(int16_t value, float volume) {
136 static const float float_from_q_15 = 1. / (1 << 15);
137 return value * volume * float_from_q_15;
138}
139
140template <>
141inline int32_t MixMul<int32_t, int32_t, float>(int32_t value, float volume) {
142 LOG_ALWAYS_FATAL("MixMul<int32_t, int32_t, float> Runtime Should not be here");
143 return value * volume;
144}
145
146template <>
147inline int32_t MixMul<int32_t, int16_t, float>(int16_t value, float volume) {
148 LOG_ALWAYS_FATAL("MixMul<int32_t, int16_t, float> Runtime Should not be here");
149 static const float u4_12_from_float = (1 << 12);
150 return value * volume * u4_12_from_float;
151}
152
153template <>
154inline int16_t MixMul<int16_t, int16_t, float>(int16_t value, float volume) {
155 LOG_ALWAYS_FATAL("MixMul<int16_t, int16_t, float> Runtime Should not be here");
Andy Hung83ffcfb2015-06-18 17:34:40 -0700156 return clamp16_from_float(MixMul<float, int16_t, float>(value, volume));
Andy Hung5e58b0a2014-06-23 19:07:29 -0700157}
158
159template <>
160inline int16_t MixMul<int16_t, float, float>(float value, float volume) {
Andy Hung83ffcfb2015-06-18 17:34:40 -0700161 return clamp16_from_float(value * volume);
Andy Hung5e58b0a2014-06-23 19:07:29 -0700162}
163
Andy Hung296b7412014-06-17 15:25:47 -0700164/*
165 * MixAccum is used to add into an accumulator register of a possibly different
166 * type. The TO and TI types are the same as MixMul.
167 */
168
169template <typename TO, typename TI>
170inline void MixAccum(TO *auxaccum, TI value) {
Judy Hsiao19e533c2019-08-14 16:52:51 +0800171 if (!std::is_same_v<TO, TI>) {
Glenn Kastena4daf0b2014-07-28 16:34:45 -0700172 LOG_ALWAYS_FATAL("MixAccum type not properly specialized: %zu %zu\n",
Andy Hung296b7412014-06-17 15:25:47 -0700173 sizeof(TO), sizeof(TI));
174 }
175 *auxaccum += value;
176}
177
178template<>
179inline void MixAccum<float, int16_t>(float *auxaccum, int16_t value) {
Andy Hung116a4982017-11-30 10:15:08 -0800180 static constexpr float norm = 1. / (1 << 15);
Andy Hung296b7412014-06-17 15:25:47 -0700181 *auxaccum += norm * value;
182}
183
184template<>
185inline void MixAccum<float, int32_t>(float *auxaccum, int32_t value) {
Andy Hung116a4982017-11-30 10:15:08 -0800186 static constexpr float norm = 1. / (1 << 27);
Andy Hung296b7412014-06-17 15:25:47 -0700187 *auxaccum += norm * value;
188}
189
190template<>
191inline void MixAccum<int32_t, int16_t>(int32_t *auxaccum, int16_t value) {
192 *auxaccum += value << 12;
193}
194
195template<>
196inline void MixAccum<int32_t, float>(int32_t *auxaccum, float value) {
197 *auxaccum += clampq4_27_from_float(value);
198}
199
200/* MixMulAux is just like MixMul except it combines with
201 * an accumulator operation MixAccum.
202 */
203
204template <typename TO, typename TI, typename TV, typename TA>
205inline TO MixMulAux(TI value, TV volume, TA *auxaccum) {
206 MixAccum<TA, TI>(auxaccum, value);
207 return MixMul<TO, TI, TV>(value, volume);
208}
209
210/* MIXTYPE is used to determine how the samples in the input frame
211 * are mixed with volume gain into the output frame.
212 * See the volumeRampMulti functions below for more details.
213 */
214enum {
215 MIXTYPE_MULTI,
216 MIXTYPE_MONOEXPAND,
217 MIXTYPE_MULTI_SAVEONLY,
Andy Hunge93b6b72014-07-17 21:30:53 -0700218 MIXTYPE_MULTI_MONOVOL,
219 MIXTYPE_MULTI_SAVEONLY_MONOVOL,
Judy Hsiao19e533c2019-08-14 16:52:51 +0800220 MIXTYPE_MULTI_STEREOVOL,
221 MIXTYPE_MULTI_SAVEONLY_STEREOVOL,
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800222 MIXTYPE_STEREOEXPAND,
Andy Hung296b7412014-06-17 15:25:47 -0700223};
224
225/*
Judy Hsiao19e533c2019-08-14 16:52:51 +0800226 * TODO: We should work on non-interleaved streams - the
227 * complexity of working on interleaved streams is now getting
228 * too high, and likely limits compiler optimization.
229 */
230template <int MIXTYPE, int NCHAN,
231 typename TO, typename TI, typename TV,
232 typename F>
233void stereoVolumeHelper(TO*& out, const TI*& in, const TV *vol, F f) {
234 static_assert(NCHAN > 0 && NCHAN <= 8);
235 static_assert(MIXTYPE == MIXTYPE_MULTI_STEREOVOL
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800236 || MIXTYPE == MIXTYPE_MULTI_SAVEONLY_STEREOVOL
237 || MIXTYPE == MIXTYPE_STEREOEXPAND);
Judy Hsiao19e533c2019-08-14 16:52:51 +0800238 auto proc = [](auto& a, const auto& b) {
Andy Hunge0c3d792020-06-01 09:41:24 -0700239 if constexpr (MIXTYPE == MIXTYPE_MULTI_STEREOVOL
240 || MIXTYPE == MIXTYPE_STEREOEXPAND) {
Judy Hsiao19e533c2019-08-14 16:52:51 +0800241 a += b;
242 } else {
243 a = b;
244 }
245 };
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800246 auto inp = [&in]() -> const TI& {
247 if constexpr (MIXTYPE == MIXTYPE_STEREOEXPAND) {
248 return *in;
249 } else {
250 return *in++;
251 }
252 };
253
Judy Hsiao19e533c2019-08-14 16:52:51 +0800254 // HALs should only expose the canonical channel masks.
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800255 proc(*out++, f(inp(), vol[0])); // front left
Judy Hsiao19e533c2019-08-14 16:52:51 +0800256 if constexpr (NCHAN == 1) return;
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800257 proc(*out++, f(inp(), vol[1])); // front right
Judy Hsiao19e533c2019-08-14 16:52:51 +0800258 if constexpr (NCHAN == 2) return;
259 if constexpr (NCHAN == 4) {
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800260 proc(*out++, f(inp(), vol[0])); // back left
261 proc(*out++, f(inp(), vol[1])); // back right
Judy Hsiao19e533c2019-08-14 16:52:51 +0800262 return;
263 }
264
265 // TODO: Precompute center volume if not ramping.
266 std::decay_t<TV> center;
267 if constexpr (std::is_floating_point_v<TV>) {
268 center = (vol[0] + vol[1]) * 0.5; // do not use divide
269 } else {
270 center = (vol[0] >> 1) + (vol[1] >> 1); // rounds to 0.
271 }
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800272 proc(*out++, f(inp(), center)); // center (or 2.1 LFE)
Judy Hsiao19e533c2019-08-14 16:52:51 +0800273 if constexpr (NCHAN == 3) return;
274 if constexpr (NCHAN == 5) {
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800275 proc(*out++, f(inp(), vol[0])); // back left
276 proc(*out++, f(inp(), vol[1])); // back right
Judy Hsiao19e533c2019-08-14 16:52:51 +0800277 return;
278 }
279
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800280 proc(*out++, f(inp(), center)); // lfe
281 proc(*out++, f(inp(), vol[0])); // back left
282 proc(*out++, f(inp(), vol[1])); // back right
Judy Hsiao19e533c2019-08-14 16:52:51 +0800283 if constexpr (NCHAN == 6) return;
284 if constexpr (NCHAN == 7) {
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800285 proc(*out++, f(inp(), center)); // back center
Judy Hsiao19e533c2019-08-14 16:52:51 +0800286 return;
287 }
288 // NCHAN == 8
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800289 proc(*out++, f(inp(), vol[0])); // side left
290 proc(*out++, f(inp(), vol[1])); // side right
Judy Hsiao19e533c2019-08-14 16:52:51 +0800291}
292
293/*
Andy Hung296b7412014-06-17 15:25:47 -0700294 * The volumeRampMulti and volumeRamp functions take a MIXTYPE
295 * which indicates the per-frame mixing and accumulation strategy.
296 *
297 * MIXTYPE_MULTI:
298 * NCHAN represents number of input and output channels.
299 * TO: int32_t (Q4.27) or float
300 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
Andy Hung116a4982017-11-30 10:15:08 -0800301 * TA: int32_t (Q4.27) or float
Andy Hung296b7412014-06-17 15:25:47 -0700302 * TV: int32_t (U4.28) or int16_t (U4.12) or float
303 * vol: represents a volume array.
304 *
305 * This accumulates into the out pointer.
306 *
307 * MIXTYPE_MONOEXPAND:
308 * Single input channel. NCHAN represents number of output channels.
309 * TO: int32_t (Q4.27) or float
310 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
Andy Hung116a4982017-11-30 10:15:08 -0800311 * TA: int32_t (Q4.27) or float
312 * TV/TAV: int32_t (U4.28) or int16_t (U4.12) or float
Andy Hung296b7412014-06-17 15:25:47 -0700313 * Input channel count is 1.
314 * vol: represents volume array.
315 *
316 * This accumulates into the out pointer.
317 *
318 * MIXTYPE_MULTI_SAVEONLY:
319 * NCHAN represents number of input and output channels.
320 * TO: int16_t (Q.15) or float
321 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
Andy Hung116a4982017-11-30 10:15:08 -0800322 * TA: int32_t (Q4.27) or float
323 * TV/TAV: int32_t (U4.28) or int16_t (U4.12) or float
Andy Hung296b7412014-06-17 15:25:47 -0700324 * vol: represents a volume array.
325 *
326 * MIXTYPE_MULTI_SAVEONLY does not accumulate into the out pointer.
Andy Hunge93b6b72014-07-17 21:30:53 -0700327 *
328 * MIXTYPE_MULTI_MONOVOL:
329 * Same as MIXTYPE_MULTI, but uses only volume[0].
330 *
331 * MIXTYPE_MULTI_SAVEONLY_MONOVOL:
332 * Same as MIXTYPE_MULTI_SAVEONLY, but uses only volume[0].
333 *
Judy Hsiao19e533c2019-08-14 16:52:51 +0800334 * MIXTYPE_MULTI_STEREOVOL:
335 * Same as MIXTYPE_MULTI, but uses only volume[0] and volume[1].
336 *
337 * MIXTYPE_MULTI_SAVEONLY_STEREOVOL:
338 * Same as MIXTYPE_MULTI_SAVEONLY, but uses only volume[0] and volume[1].
339 *
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800340 * MIXTYPE_STEREOEXPAND:
341 * Stereo input channel. NCHAN represents number of output channels.
342 * Expand size 2 array "in" and "vol" to multi-channel output. Note
343 * that the 2 array is assumed to have replicated L+R.
344 *
Andy Hung296b7412014-06-17 15:25:47 -0700345 */
346
347template <int MIXTYPE, int NCHAN,
348 typename TO, typename TI, typename TV, typename TA, typename TAV>
349inline void volumeRampMulti(TO* out, size_t frameCount,
350 const TI* in, TA* aux, TV *vol, const TV *volinc, TAV *vola, TAV volainc)
351{
352#ifdef ALOGVV
353 ALOGVV("volumeRampMulti, MIXTYPE:%d\n", MIXTYPE);
354#endif
355 if (aux != NULL) {
356 do {
357 TA auxaccum = 0;
Judy Hsiao19e533c2019-08-14 16:52:51 +0800358 if constexpr (MIXTYPE == MIXTYPE_MULTI) {
Andy Hung296b7412014-06-17 15:25:47 -0700359 for (int i = 0; i < NCHAN; ++i) {
360 *out++ += MixMulAux<TO, TI, TV, TA>(*in++, vol[i], &auxaccum);
361 vol[i] += volinc[i];
362 }
Judy Hsiao19e533c2019-08-14 16:52:51 +0800363 } else if constexpr (MIXTYPE == MIXTYPE_MONOEXPAND) {
Andy Hung296b7412014-06-17 15:25:47 -0700364 for (int i = 0; i < NCHAN; ++i) {
365 *out++ += MixMulAux<TO, TI, TV, TA>(*in, vol[i], &auxaccum);
366 vol[i] += volinc[i];
367 }
368 in++;
Judy Hsiao19e533c2019-08-14 16:52:51 +0800369 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_SAVEONLY) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700370 for (int i = 0; i < NCHAN; ++i) {
371 *out++ = MixMulAux<TO, TI, TV, TA>(*in++, vol[i], &auxaccum);
372 vol[i] += volinc[i];
373 }
Judy Hsiao19e533c2019-08-14 16:52:51 +0800374 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_MONOVOL) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700375 for (int i = 0; i < NCHAN; ++i) {
376 *out++ += MixMulAux<TO, TI, TV, TA>(*in++, vol[0], &auxaccum);
377 }
378 vol[0] += volinc[0];
Judy Hsiao19e533c2019-08-14 16:52:51 +0800379 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_SAVEONLY_MONOVOL) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700380 for (int i = 0; i < NCHAN; ++i) {
381 *out++ = MixMulAux<TO, TI, TV, TA>(*in++, vol[0], &auxaccum);
382 }
383 vol[0] += volinc[0];
Judy Hsiao19e533c2019-08-14 16:52:51 +0800384 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_STEREOVOL
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800385 || MIXTYPE == MIXTYPE_MULTI_SAVEONLY_STEREOVOL
386 || MIXTYPE == MIXTYPE_STEREOEXPAND) {
Judy Hsiao19e533c2019-08-14 16:52:51 +0800387 stereoVolumeHelper<MIXTYPE, NCHAN>(
388 out, in, vol, [&auxaccum] (auto &a, const auto &b) {
389 return MixMulAux<TO, TI, TV, TA>(a, b, &auxaccum);
390 });
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800391 if constexpr (MIXTYPE == MIXTYPE_STEREOEXPAND) in += 2;
Judy Hsiao19e533c2019-08-14 16:52:51 +0800392 vol[0] += volinc[0];
393 vol[1] += volinc[1];
394 } else /* constexpr */ {
395 static_assert(dependent_false<MIXTYPE>, "invalid mixtype");
Andy Hung296b7412014-06-17 15:25:47 -0700396 }
397 auxaccum /= NCHAN;
398 *aux++ += MixMul<TA, TA, TAV>(auxaccum, *vola);
399 vola[0] += volainc;
400 } while (--frameCount);
401 } else {
402 do {
Judy Hsiao19e533c2019-08-14 16:52:51 +0800403 if constexpr (MIXTYPE == MIXTYPE_MULTI) {
Andy Hung296b7412014-06-17 15:25:47 -0700404 for (int i = 0; i < NCHAN; ++i) {
405 *out++ += MixMul<TO, TI, TV>(*in++, vol[i]);
406 vol[i] += volinc[i];
407 }
Judy Hsiao19e533c2019-08-14 16:52:51 +0800408 } else if constexpr (MIXTYPE == MIXTYPE_MONOEXPAND) {
Andy Hung296b7412014-06-17 15:25:47 -0700409 for (int i = 0; i < NCHAN; ++i) {
410 *out++ += MixMul<TO, TI, TV>(*in, vol[i]);
411 vol[i] += volinc[i];
412 }
413 in++;
Judy Hsiao19e533c2019-08-14 16:52:51 +0800414 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_SAVEONLY) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700415 for (int i = 0; i < NCHAN; ++i) {
416 *out++ = MixMul<TO, TI, TV>(*in++, vol[i]);
417 vol[i] += volinc[i];
418 }
Judy Hsiao19e533c2019-08-14 16:52:51 +0800419 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_MONOVOL) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700420 for (int i = 0; i < NCHAN; ++i) {
421 *out++ += MixMul<TO, TI, TV>(*in++, vol[0]);
422 }
423 vol[0] += volinc[0];
Judy Hsiao19e533c2019-08-14 16:52:51 +0800424 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_SAVEONLY_MONOVOL) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700425 for (int i = 0; i < NCHAN; ++i) {
426 *out++ = MixMul<TO, TI, TV>(*in++, vol[0]);
427 }
428 vol[0] += volinc[0];
Judy Hsiao19e533c2019-08-14 16:52:51 +0800429 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_STEREOVOL
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800430 || MIXTYPE == MIXTYPE_MULTI_SAVEONLY_STEREOVOL
431 || MIXTYPE == MIXTYPE_STEREOEXPAND) {
Judy Hsiao19e533c2019-08-14 16:52:51 +0800432 stereoVolumeHelper<MIXTYPE, NCHAN>(out, in, vol, [] (auto &a, const auto &b) {
433 return MixMul<TO, TI, TV>(a, b);
434 });
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800435 if constexpr (MIXTYPE == MIXTYPE_STEREOEXPAND) in += 2;
Judy Hsiao19e533c2019-08-14 16:52:51 +0800436 vol[0] += volinc[0];
437 vol[1] += volinc[1];
438 } else /* constexpr */ {
439 static_assert(dependent_false<MIXTYPE>, "invalid mixtype");
Andy Hung296b7412014-06-17 15:25:47 -0700440 }
441 } while (--frameCount);
442 }
443}
444
445template <int MIXTYPE, int NCHAN,
446 typename TO, typename TI, typename TV, typename TA, typename TAV>
447inline void volumeMulti(TO* out, size_t frameCount,
448 const TI* in, TA* aux, const TV *vol, TAV vola)
449{
450#ifdef ALOGVV
451 ALOGVV("volumeMulti MIXTYPE:%d\n", MIXTYPE);
452#endif
453 if (aux != NULL) {
454 do {
455 TA auxaccum = 0;
Judy Hsiao19e533c2019-08-14 16:52:51 +0800456 if constexpr (MIXTYPE == MIXTYPE_MULTI) {
Andy Hung296b7412014-06-17 15:25:47 -0700457 for (int i = 0; i < NCHAN; ++i) {
458 *out++ += MixMulAux<TO, TI, TV, TA>(*in++, vol[i], &auxaccum);
459 }
Judy Hsiao19e533c2019-08-14 16:52:51 +0800460 } else if constexpr (MIXTYPE == MIXTYPE_MONOEXPAND) {
Andy Hung296b7412014-06-17 15:25:47 -0700461 for (int i = 0; i < NCHAN; ++i) {
462 *out++ += MixMulAux<TO, TI, TV, TA>(*in, vol[i], &auxaccum);
463 }
464 in++;
Judy Hsiao19e533c2019-08-14 16:52:51 +0800465 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_SAVEONLY) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700466 for (int i = 0; i < NCHAN; ++i) {
467 *out++ = MixMulAux<TO, TI, TV, TA>(*in++, vol[i], &auxaccum);
468 }
Judy Hsiao19e533c2019-08-14 16:52:51 +0800469 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_MONOVOL) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700470 for (int i = 0; i < NCHAN; ++i) {
471 *out++ += MixMulAux<TO, TI, TV, TA>(*in++, vol[0], &auxaccum);
472 }
Judy Hsiao19e533c2019-08-14 16:52:51 +0800473 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_SAVEONLY_MONOVOL) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700474 for (int i = 0; i < NCHAN; ++i) {
475 *out++ = MixMulAux<TO, TI, TV, TA>(*in++, vol[0], &auxaccum);
476 }
Judy Hsiao19e533c2019-08-14 16:52:51 +0800477 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_STEREOVOL
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800478 || MIXTYPE == MIXTYPE_MULTI_SAVEONLY_STEREOVOL
479 || MIXTYPE == MIXTYPE_STEREOEXPAND) {
Judy Hsiao19e533c2019-08-14 16:52:51 +0800480 stereoVolumeHelper<MIXTYPE, NCHAN>(
481 out, in, vol, [&auxaccum] (auto &a, const auto &b) {
482 return MixMulAux<TO, TI, TV, TA>(a, b, &auxaccum);
483 });
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800484 if constexpr (MIXTYPE == MIXTYPE_STEREOEXPAND) in += 2;
Judy Hsiao19e533c2019-08-14 16:52:51 +0800485 } else /* constexpr */ {
486 static_assert(dependent_false<MIXTYPE>, "invalid mixtype");
Andy Hung296b7412014-06-17 15:25:47 -0700487 }
488 auxaccum /= NCHAN;
489 *aux++ += MixMul<TA, TA, TAV>(auxaccum, vola);
490 } while (--frameCount);
491 } else {
492 do {
Judy Hsiao19e533c2019-08-14 16:52:51 +0800493 if constexpr (MIXTYPE == MIXTYPE_MULTI) {
Andy Hung296b7412014-06-17 15:25:47 -0700494 for (int i = 0; i < NCHAN; ++i) {
495 *out++ += MixMul<TO, TI, TV>(*in++, vol[i]);
496 }
Judy Hsiao19e533c2019-08-14 16:52:51 +0800497 } else if constexpr (MIXTYPE == MIXTYPE_MONOEXPAND) {
Andy Hung296b7412014-06-17 15:25:47 -0700498 for (int i = 0; i < NCHAN; ++i) {
499 *out++ += MixMul<TO, TI, TV>(*in, vol[i]);
500 }
501 in++;
Judy Hsiao19e533c2019-08-14 16:52:51 +0800502 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_SAVEONLY) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700503 for (int i = 0; i < NCHAN; ++i) {
504 *out++ = MixMul<TO, TI, TV>(*in++, vol[i]);
505 }
Judy Hsiao19e533c2019-08-14 16:52:51 +0800506 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_MONOVOL) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700507 for (int i = 0; i < NCHAN; ++i) {
508 *out++ += MixMul<TO, TI, TV>(*in++, vol[0]);
509 }
Judy Hsiao19e533c2019-08-14 16:52:51 +0800510 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_SAVEONLY_MONOVOL) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700511 for (int i = 0; i < NCHAN; ++i) {
512 *out++ = MixMul<TO, TI, TV>(*in++, vol[0]);
513 }
Judy Hsiao19e533c2019-08-14 16:52:51 +0800514 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_STEREOVOL
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800515 || MIXTYPE == MIXTYPE_MULTI_SAVEONLY_STEREOVOL
516 || MIXTYPE == MIXTYPE_STEREOEXPAND) {
Judy Hsiao19e533c2019-08-14 16:52:51 +0800517 stereoVolumeHelper<MIXTYPE, NCHAN>(out, in, vol, [] (auto &a, const auto &b) {
518 return MixMul<TO, TI, TV>(a, b);
519 });
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800520 if constexpr (MIXTYPE == MIXTYPE_STEREOEXPAND) in += 2;
Judy Hsiao19e533c2019-08-14 16:52:51 +0800521 } else /* constexpr */ {
522 static_assert(dependent_false<MIXTYPE>, "invalid mixtype");
Andy Hung296b7412014-06-17 15:25:47 -0700523 }
524 } while (--frameCount);
525 }
526}
527
528};
529
530#endif /* ANDROID_AUDIO_MIXER_OPS_H */