blob: 27481820764078ef1e6848a521f01762de385325 [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) {
239 if constexpr (MIXTYPE == MIXTYPE_MULTI_STEREOVOL) {
240 a += b;
241 } else {
242 a = b;
243 }
244 };
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800245 auto inp = [&in]() -> const TI& {
246 if constexpr (MIXTYPE == MIXTYPE_STEREOEXPAND) {
247 return *in;
248 } else {
249 return *in++;
250 }
251 };
252
Judy Hsiao19e533c2019-08-14 16:52:51 +0800253 // HALs should only expose the canonical channel masks.
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800254 proc(*out++, f(inp(), vol[0])); // front left
Judy Hsiao19e533c2019-08-14 16:52:51 +0800255 if constexpr (NCHAN == 1) return;
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800256 proc(*out++, f(inp(), vol[1])); // front right
Judy Hsiao19e533c2019-08-14 16:52:51 +0800257 if constexpr (NCHAN == 2) return;
258 if constexpr (NCHAN == 4) {
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800259 proc(*out++, f(inp(), vol[0])); // back left
260 proc(*out++, f(inp(), vol[1])); // back right
Judy Hsiao19e533c2019-08-14 16:52:51 +0800261 return;
262 }
263
264 // TODO: Precompute center volume if not ramping.
265 std::decay_t<TV> center;
266 if constexpr (std::is_floating_point_v<TV>) {
267 center = (vol[0] + vol[1]) * 0.5; // do not use divide
268 } else {
269 center = (vol[0] >> 1) + (vol[1] >> 1); // rounds to 0.
270 }
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800271 proc(*out++, f(inp(), center)); // center (or 2.1 LFE)
Judy Hsiao19e533c2019-08-14 16:52:51 +0800272 if constexpr (NCHAN == 3) return;
273 if constexpr (NCHAN == 5) {
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800274 proc(*out++, f(inp(), vol[0])); // back left
275 proc(*out++, f(inp(), vol[1])); // back right
Judy Hsiao19e533c2019-08-14 16:52:51 +0800276 return;
277 }
278
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800279 proc(*out++, f(inp(), center)); // lfe
280 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 if constexpr (NCHAN == 6) return;
283 if constexpr (NCHAN == 7) {
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800284 proc(*out++, f(inp(), center)); // back center
Judy Hsiao19e533c2019-08-14 16:52:51 +0800285 return;
286 }
287 // NCHAN == 8
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800288 proc(*out++, f(inp(), vol[0])); // side left
289 proc(*out++, f(inp(), vol[1])); // side right
Judy Hsiao19e533c2019-08-14 16:52:51 +0800290}
291
292/*
Andy Hung296b7412014-06-17 15:25:47 -0700293 * The volumeRampMulti and volumeRamp functions take a MIXTYPE
294 * which indicates the per-frame mixing and accumulation strategy.
295 *
296 * MIXTYPE_MULTI:
297 * NCHAN represents number of input and output channels.
298 * TO: int32_t (Q4.27) or float
299 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
Andy Hung116a4982017-11-30 10:15:08 -0800300 * TA: int32_t (Q4.27) or float
Andy Hung296b7412014-06-17 15:25:47 -0700301 * TV: int32_t (U4.28) or int16_t (U4.12) or float
302 * vol: represents a volume array.
303 *
304 * This accumulates into the out pointer.
305 *
306 * MIXTYPE_MONOEXPAND:
307 * Single input channel. NCHAN represents number of output channels.
308 * TO: int32_t (Q4.27) or float
309 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
Andy Hung116a4982017-11-30 10:15:08 -0800310 * TA: int32_t (Q4.27) or float
311 * TV/TAV: int32_t (U4.28) or int16_t (U4.12) or float
Andy Hung296b7412014-06-17 15:25:47 -0700312 * Input channel count is 1.
313 * vol: represents volume array.
314 *
315 * This accumulates into the out pointer.
316 *
317 * MIXTYPE_MULTI_SAVEONLY:
318 * NCHAN represents number of input and output channels.
319 * TO: int16_t (Q.15) or float
320 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
Andy Hung116a4982017-11-30 10:15:08 -0800321 * TA: int32_t (Q4.27) or float
322 * TV/TAV: int32_t (U4.28) or int16_t (U4.12) or float
Andy Hung296b7412014-06-17 15:25:47 -0700323 * vol: represents a volume array.
324 *
325 * MIXTYPE_MULTI_SAVEONLY does not accumulate into the out pointer.
Andy Hunge93b6b72014-07-17 21:30:53 -0700326 *
327 * MIXTYPE_MULTI_MONOVOL:
328 * Same as MIXTYPE_MULTI, but uses only volume[0].
329 *
330 * MIXTYPE_MULTI_SAVEONLY_MONOVOL:
331 * Same as MIXTYPE_MULTI_SAVEONLY, but uses only volume[0].
332 *
Judy Hsiao19e533c2019-08-14 16:52:51 +0800333 * MIXTYPE_MULTI_STEREOVOL:
334 * Same as MIXTYPE_MULTI, but uses only volume[0] and volume[1].
335 *
336 * MIXTYPE_MULTI_SAVEONLY_STEREOVOL:
337 * Same as MIXTYPE_MULTI_SAVEONLY, but uses only volume[0] and volume[1].
338 *
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800339 * MIXTYPE_STEREOEXPAND:
340 * Stereo input channel. NCHAN represents number of output channels.
341 * Expand size 2 array "in" and "vol" to multi-channel output. Note
342 * that the 2 array is assumed to have replicated L+R.
343 *
Andy Hung296b7412014-06-17 15:25:47 -0700344 */
345
346template <int MIXTYPE, int NCHAN,
347 typename TO, typename TI, typename TV, typename TA, typename TAV>
348inline void volumeRampMulti(TO* out, size_t frameCount,
349 const TI* in, TA* aux, TV *vol, const TV *volinc, TAV *vola, TAV volainc)
350{
351#ifdef ALOGVV
352 ALOGVV("volumeRampMulti, MIXTYPE:%d\n", MIXTYPE);
353#endif
354 if (aux != NULL) {
355 do {
356 TA auxaccum = 0;
Judy Hsiao19e533c2019-08-14 16:52:51 +0800357 if constexpr (MIXTYPE == MIXTYPE_MULTI) {
Andy Hung296b7412014-06-17 15:25:47 -0700358 for (int i = 0; i < NCHAN; ++i) {
359 *out++ += MixMulAux<TO, TI, TV, TA>(*in++, vol[i], &auxaccum);
360 vol[i] += volinc[i];
361 }
Judy Hsiao19e533c2019-08-14 16:52:51 +0800362 } else if constexpr (MIXTYPE == MIXTYPE_MONOEXPAND) {
Andy Hung296b7412014-06-17 15:25:47 -0700363 for (int i = 0; i < NCHAN; ++i) {
364 *out++ += MixMulAux<TO, TI, TV, TA>(*in, vol[i], &auxaccum);
365 vol[i] += volinc[i];
366 }
367 in++;
Judy Hsiao19e533c2019-08-14 16:52:51 +0800368 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_SAVEONLY) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700369 for (int i = 0; i < NCHAN; ++i) {
370 *out++ = MixMulAux<TO, TI, TV, TA>(*in++, vol[i], &auxaccum);
371 vol[i] += volinc[i];
372 }
Judy Hsiao19e533c2019-08-14 16:52:51 +0800373 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_MONOVOL) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700374 for (int i = 0; i < NCHAN; ++i) {
375 *out++ += MixMulAux<TO, TI, TV, TA>(*in++, vol[0], &auxaccum);
376 }
377 vol[0] += volinc[0];
Judy Hsiao19e533c2019-08-14 16:52:51 +0800378 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_SAVEONLY_MONOVOL) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700379 for (int i = 0; i < NCHAN; ++i) {
380 *out++ = MixMulAux<TO, TI, TV, TA>(*in++, vol[0], &auxaccum);
381 }
382 vol[0] += volinc[0];
Judy Hsiao19e533c2019-08-14 16:52:51 +0800383 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_STEREOVOL
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800384 || MIXTYPE == MIXTYPE_MULTI_SAVEONLY_STEREOVOL
385 || MIXTYPE == MIXTYPE_STEREOEXPAND) {
Judy Hsiao19e533c2019-08-14 16:52:51 +0800386 stereoVolumeHelper<MIXTYPE, NCHAN>(
387 out, in, vol, [&auxaccum] (auto &a, const auto &b) {
388 return MixMulAux<TO, TI, TV, TA>(a, b, &auxaccum);
389 });
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800390 if constexpr (MIXTYPE == MIXTYPE_STEREOEXPAND) in += 2;
Judy Hsiao19e533c2019-08-14 16:52:51 +0800391 vol[0] += volinc[0];
392 vol[1] += volinc[1];
393 } else /* constexpr */ {
394 static_assert(dependent_false<MIXTYPE>, "invalid mixtype");
Andy Hung296b7412014-06-17 15:25:47 -0700395 }
396 auxaccum /= NCHAN;
397 *aux++ += MixMul<TA, TA, TAV>(auxaccum, *vola);
398 vola[0] += volainc;
399 } while (--frameCount);
400 } else {
401 do {
Judy Hsiao19e533c2019-08-14 16:52:51 +0800402 if constexpr (MIXTYPE == MIXTYPE_MULTI) {
Andy Hung296b7412014-06-17 15:25:47 -0700403 for (int i = 0; i < NCHAN; ++i) {
404 *out++ += MixMul<TO, TI, TV>(*in++, vol[i]);
405 vol[i] += volinc[i];
406 }
Judy Hsiao19e533c2019-08-14 16:52:51 +0800407 } else if constexpr (MIXTYPE == MIXTYPE_MONOEXPAND) {
Andy Hung296b7412014-06-17 15:25:47 -0700408 for (int i = 0; i < NCHAN; ++i) {
409 *out++ += MixMul<TO, TI, TV>(*in, vol[i]);
410 vol[i] += volinc[i];
411 }
412 in++;
Judy Hsiao19e533c2019-08-14 16:52:51 +0800413 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_SAVEONLY) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700414 for (int i = 0; i < NCHAN; ++i) {
415 *out++ = MixMul<TO, TI, TV>(*in++, vol[i]);
416 vol[i] += volinc[i];
417 }
Judy Hsiao19e533c2019-08-14 16:52:51 +0800418 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_MONOVOL) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700419 for (int i = 0; i < NCHAN; ++i) {
420 *out++ += MixMul<TO, TI, TV>(*in++, vol[0]);
421 }
422 vol[0] += volinc[0];
Judy Hsiao19e533c2019-08-14 16:52:51 +0800423 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_SAVEONLY_MONOVOL) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700424 for (int i = 0; i < NCHAN; ++i) {
425 *out++ = MixMul<TO, TI, TV>(*in++, vol[0]);
426 }
427 vol[0] += volinc[0];
Judy Hsiao19e533c2019-08-14 16:52:51 +0800428 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_STEREOVOL
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800429 || MIXTYPE == MIXTYPE_MULTI_SAVEONLY_STEREOVOL
430 || MIXTYPE == MIXTYPE_STEREOEXPAND) {
Judy Hsiao19e533c2019-08-14 16:52:51 +0800431 stereoVolumeHelper<MIXTYPE, NCHAN>(out, in, vol, [] (auto &a, const auto &b) {
432 return MixMul<TO, TI, TV>(a, b);
433 });
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800434 if constexpr (MIXTYPE == MIXTYPE_STEREOEXPAND) in += 2;
Judy Hsiao19e533c2019-08-14 16:52:51 +0800435 vol[0] += volinc[0];
436 vol[1] += volinc[1];
437 } else /* constexpr */ {
438 static_assert(dependent_false<MIXTYPE>, "invalid mixtype");
Andy Hung296b7412014-06-17 15:25:47 -0700439 }
440 } while (--frameCount);
441 }
442}
443
444template <int MIXTYPE, int NCHAN,
445 typename TO, typename TI, typename TV, typename TA, typename TAV>
446inline void volumeMulti(TO* out, size_t frameCount,
447 const TI* in, TA* aux, const TV *vol, TAV vola)
448{
449#ifdef ALOGVV
450 ALOGVV("volumeMulti MIXTYPE:%d\n", MIXTYPE);
451#endif
452 if (aux != NULL) {
453 do {
454 TA auxaccum = 0;
Judy Hsiao19e533c2019-08-14 16:52:51 +0800455 if constexpr (MIXTYPE == MIXTYPE_MULTI) {
Andy Hung296b7412014-06-17 15:25:47 -0700456 for (int i = 0; i < NCHAN; ++i) {
457 *out++ += MixMulAux<TO, TI, TV, TA>(*in++, vol[i], &auxaccum);
458 }
Judy Hsiao19e533c2019-08-14 16:52:51 +0800459 } else if constexpr (MIXTYPE == MIXTYPE_MONOEXPAND) {
Andy Hung296b7412014-06-17 15:25:47 -0700460 for (int i = 0; i < NCHAN; ++i) {
461 *out++ += MixMulAux<TO, TI, TV, TA>(*in, vol[i], &auxaccum);
462 }
463 in++;
Judy Hsiao19e533c2019-08-14 16:52:51 +0800464 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_SAVEONLY) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700465 for (int i = 0; i < NCHAN; ++i) {
466 *out++ = MixMulAux<TO, TI, TV, TA>(*in++, vol[i], &auxaccum);
467 }
Judy Hsiao19e533c2019-08-14 16:52:51 +0800468 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_MONOVOL) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700469 for (int i = 0; i < NCHAN; ++i) {
470 *out++ += MixMulAux<TO, TI, TV, TA>(*in++, vol[0], &auxaccum);
471 }
Judy Hsiao19e533c2019-08-14 16:52:51 +0800472 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_SAVEONLY_MONOVOL) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700473 for (int i = 0; i < NCHAN; ++i) {
474 *out++ = MixMulAux<TO, TI, TV, TA>(*in++, vol[0], &auxaccum);
475 }
Judy Hsiao19e533c2019-08-14 16:52:51 +0800476 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_STEREOVOL
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800477 || MIXTYPE == MIXTYPE_MULTI_SAVEONLY_STEREOVOL
478 || MIXTYPE == MIXTYPE_STEREOEXPAND) {
Judy Hsiao19e533c2019-08-14 16:52:51 +0800479 stereoVolumeHelper<MIXTYPE, NCHAN>(
480 out, in, vol, [&auxaccum] (auto &a, const auto &b) {
481 return MixMulAux<TO, TI, TV, TA>(a, b, &auxaccum);
482 });
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800483 if constexpr (MIXTYPE == MIXTYPE_STEREOEXPAND) in += 2;
Judy Hsiao19e533c2019-08-14 16:52:51 +0800484 } else /* constexpr */ {
485 static_assert(dependent_false<MIXTYPE>, "invalid mixtype");
Andy Hung296b7412014-06-17 15:25:47 -0700486 }
487 auxaccum /= NCHAN;
488 *aux++ += MixMul<TA, TA, TAV>(auxaccum, vola);
489 } while (--frameCount);
490 } else {
491 do {
Judy Hsiao19e533c2019-08-14 16:52:51 +0800492 if constexpr (MIXTYPE == MIXTYPE_MULTI) {
Andy Hung296b7412014-06-17 15:25:47 -0700493 for (int i = 0; i < NCHAN; ++i) {
494 *out++ += MixMul<TO, TI, TV>(*in++, vol[i]);
495 }
Judy Hsiao19e533c2019-08-14 16:52:51 +0800496 } else if constexpr (MIXTYPE == MIXTYPE_MONOEXPAND) {
Andy Hung296b7412014-06-17 15:25:47 -0700497 for (int i = 0; i < NCHAN; ++i) {
498 *out++ += MixMul<TO, TI, TV>(*in, vol[i]);
499 }
500 in++;
Judy Hsiao19e533c2019-08-14 16:52:51 +0800501 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_SAVEONLY) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700502 for (int i = 0; i < NCHAN; ++i) {
503 *out++ = MixMul<TO, TI, TV>(*in++, vol[i]);
504 }
Judy Hsiao19e533c2019-08-14 16:52:51 +0800505 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_MONOVOL) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700506 for (int i = 0; i < NCHAN; ++i) {
507 *out++ += MixMul<TO, TI, TV>(*in++, vol[0]);
508 }
Judy Hsiao19e533c2019-08-14 16:52:51 +0800509 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_SAVEONLY_MONOVOL) {
Andy Hunge93b6b72014-07-17 21:30:53 -0700510 for (int i = 0; i < NCHAN; ++i) {
511 *out++ = MixMul<TO, TI, TV>(*in++, vol[0]);
512 }
Judy Hsiao19e533c2019-08-14 16:52:51 +0800513 } else if constexpr (MIXTYPE == MIXTYPE_MULTI_STEREOVOL
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800514 || MIXTYPE == MIXTYPE_MULTI_SAVEONLY_STEREOVOL
515 || MIXTYPE == MIXTYPE_STEREOEXPAND) {
Judy Hsiao19e533c2019-08-14 16:52:51 +0800516 stereoVolumeHelper<MIXTYPE, NCHAN>(out, in, vol, [] (auto &a, const auto &b) {
517 return MixMul<TO, TI, TV>(a, b);
518 });
Judy Hsiaoc5cf9e22019-08-15 11:32:02 +0800519 if constexpr (MIXTYPE == MIXTYPE_STEREOEXPAND) in += 2;
Judy Hsiao19e533c2019-08-14 16:52:51 +0800520 } else /* constexpr */ {
521 static_assert(dependent_false<MIXTYPE>, "invalid mixtype");
Andy Hung296b7412014-06-17 15:25:47 -0700522 }
523 } while (--frameCount);
524 }
525}
526
527};
528
529#endif /* ANDROID_AUDIO_MIXER_OPS_H */