blob: 2678857e32335858ae4fe513a37d82f358245658 [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
22/* Behavior of is_same<>::value is true if the types are identical,
23 * false otherwise. Identical to the STL std::is_same.
24 */
25template<typename T, typename U>
26struct is_same
27{
28 static const bool value = false;
29};
30
31template<typename T>
32struct is_same<T, T> // partial specialization
33{
34 static const bool value = true;
35};
36
37
38/* MixMul is a multiplication operator to scale an audio input signal
39 * by a volume gain, with the formula:
40 *
41 * O(utput) = I(nput) * V(olume)
42 *
43 * The output, input, and volume may have different types.
44 * There are 27 variants, of which 14 are actually defined in an
45 * explicitly templated class.
46 *
47 * The following type variables and the underlying meaning:
48 *
49 * Output type TO: int32_t (Q4.27) or int16_t (Q.15) or float [-1,1]
50 * Input signal type TI: int32_t (Q4.27) or int16_t (Q.15) or float [-1,1]
51 * Volume type TV: int32_t (U4.28) or int16_t (U4.12) or float [-1,1]
52 *
53 * For high precision audio, only the <TO, TI, TV> = <float, float, float>
54 * needs to be accelerated. This is perhaps the easiest form to do quickly as well.
Chih-Hung Hsieh65025402014-12-11 13:06:46 -080055 *
56 * A generic version is NOT defined to catch any mistake of using it.
Andy Hung296b7412014-06-17 15:25:47 -070057 */
58
59template <typename TO, typename TI, typename TV>
Chih-Hung Hsieh65025402014-12-11 13:06:46 -080060TO MixMul(TI value, TV volume);
Andy Hung296b7412014-06-17 15:25:47 -070061
62template <>
63inline int32_t MixMul<int32_t, int16_t, int16_t>(int16_t value, int16_t volume) {
64 return value * volume;
65}
66
67template <>
68inline int32_t MixMul<int32_t, int32_t, int16_t>(int32_t value, int16_t volume) {
69 return (value >> 12) * volume;
70}
71
72template <>
73inline int32_t MixMul<int32_t, int16_t, int32_t>(int16_t value, int32_t volume) {
74 return value * (volume >> 16);
75}
76
77template <>
78inline int32_t MixMul<int32_t, int32_t, int32_t>(int32_t value, int32_t volume) {
79 return (value >> 12) * (volume >> 16);
80}
81
82template <>
83inline float MixMul<float, float, int16_t>(float value, int16_t volume) {
84 static const float norm = 1. / (1 << 12);
85 return value * volume * norm;
86}
87
88template <>
89inline float MixMul<float, float, int32_t>(float value, int32_t volume) {
90 static const float norm = 1. / (1 << 28);
91 return value * volume * norm;
92}
93
94template <>
95inline int16_t MixMul<int16_t, float, int16_t>(float value, int16_t volume) {
96 return clamp16_from_float(MixMul<float, float, int16_t>(value, volume));
97}
98
99template <>
100inline int16_t MixMul<int16_t, float, int32_t>(float value, int32_t volume) {
101 return clamp16_from_float(MixMul<float, float, int32_t>(value, volume));
102}
103
104template <>
105inline float MixMul<float, int16_t, int16_t>(int16_t value, int16_t volume) {
106 static const float norm = 1. / (1 << (15 + 12));
107 return static_cast<float>(value) * static_cast<float>(volume) * norm;
108}
109
110template <>
111inline float MixMul<float, int16_t, int32_t>(int16_t value, int32_t volume) {
112 static const float norm = 1. / (1ULL << (15 + 28));
113 return static_cast<float>(value) * static_cast<float>(volume) * norm;
114}
115
116template <>
117inline int16_t MixMul<int16_t, int16_t, int16_t>(int16_t value, int16_t volume) {
118 return clamp16(MixMul<int32_t, int16_t, int16_t>(value, volume) >> 12);
119}
120
121template <>
122inline int16_t MixMul<int16_t, int32_t, int16_t>(int32_t value, int16_t volume) {
123 return clamp16(MixMul<int32_t, int32_t, int16_t>(value, volume) >> 12);
124}
125
126template <>
127inline int16_t MixMul<int16_t, int16_t, int32_t>(int16_t value, int32_t volume) {
128 return clamp16(MixMul<int32_t, int16_t, int32_t>(value, volume) >> 12);
129}
130
131template <>
132inline int16_t MixMul<int16_t, int32_t, int32_t>(int32_t value, int32_t volume) {
133 return clamp16(MixMul<int32_t, int32_t, int32_t>(value, volume) >> 12);
134}
135
Andy Hung5e58b0a2014-06-23 19:07:29 -0700136/* Required for floating point volume. Some are needed for compilation but
137 * are not needed in execution and should be removed from the final build by
138 * an optimizing compiler.
139 */
140template <>
141inline float MixMul<float, float, float>(float value, float volume) {
142 return value * volume;
143}
144
145template <>
146inline float MixMul<float, int16_t, float>(int16_t value, float volume) {
147 static const float float_from_q_15 = 1. / (1 << 15);
148 return value * volume * float_from_q_15;
149}
150
151template <>
152inline int32_t MixMul<int32_t, int32_t, float>(int32_t value, float volume) {
153 LOG_ALWAYS_FATAL("MixMul<int32_t, int32_t, float> Runtime Should not be here");
154 return value * volume;
155}
156
157template <>
158inline int32_t MixMul<int32_t, int16_t, float>(int16_t value, float volume) {
159 LOG_ALWAYS_FATAL("MixMul<int32_t, int16_t, float> Runtime Should not be here");
160 static const float u4_12_from_float = (1 << 12);
161 return value * volume * u4_12_from_float;
162}
163
164template <>
165inline int16_t MixMul<int16_t, int16_t, float>(int16_t value, float volume) {
166 LOG_ALWAYS_FATAL("MixMul<int16_t, int16_t, float> Runtime Should not be here");
167 return value * volume;
168}
169
170template <>
171inline int16_t MixMul<int16_t, float, float>(float value, float volume) {
172 static const float q_15_from_float = (1 << 15);
173 return value * volume * q_15_from_float;
174}
175
Andy Hung296b7412014-06-17 15:25:47 -0700176/*
177 * MixAccum is used to add into an accumulator register of a possibly different
178 * type. The TO and TI types are the same as MixMul.
179 */
180
181template <typename TO, typename TI>
182inline void MixAccum(TO *auxaccum, TI value) {
183 if (!is_same<TO, TI>::value) {
Glenn Kastena4daf0b2014-07-28 16:34:45 -0700184 LOG_ALWAYS_FATAL("MixAccum type not properly specialized: %zu %zu\n",
Andy Hung296b7412014-06-17 15:25:47 -0700185 sizeof(TO), sizeof(TI));
186 }
187 *auxaccum += value;
188}
189
190template<>
191inline void MixAccum<float, int16_t>(float *auxaccum, int16_t value) {
192 static const float norm = 1. / (1 << 15);
193 *auxaccum += norm * value;
194}
195
196template<>
197inline void MixAccum<float, int32_t>(float *auxaccum, int32_t value) {
198 static const float norm = 1. / (1 << 27);
199 *auxaccum += norm * value;
200}
201
202template<>
203inline void MixAccum<int32_t, int16_t>(int32_t *auxaccum, int16_t value) {
204 *auxaccum += value << 12;
205}
206
207template<>
208inline void MixAccum<int32_t, float>(int32_t *auxaccum, float value) {
209 *auxaccum += clampq4_27_from_float(value);
210}
211
212/* MixMulAux is just like MixMul except it combines with
213 * an accumulator operation MixAccum.
214 */
215
216template <typename TO, typename TI, typename TV, typename TA>
217inline TO MixMulAux(TI value, TV volume, TA *auxaccum) {
218 MixAccum<TA, TI>(auxaccum, value);
219 return MixMul<TO, TI, TV>(value, volume);
220}
221
222/* MIXTYPE is used to determine how the samples in the input frame
223 * are mixed with volume gain into the output frame.
224 * See the volumeRampMulti functions below for more details.
225 */
226enum {
227 MIXTYPE_MULTI,
228 MIXTYPE_MONOEXPAND,
229 MIXTYPE_MULTI_SAVEONLY,
Andy Hunge93b6b72014-07-17 21:30:53 -0700230 MIXTYPE_MULTI_MONOVOL,
231 MIXTYPE_MULTI_SAVEONLY_MONOVOL,
Andy Hung296b7412014-06-17 15:25:47 -0700232};
233
234/*
235 * The volumeRampMulti and volumeRamp functions take a MIXTYPE
236 * which indicates the per-frame mixing and accumulation strategy.
237 *
238 * MIXTYPE_MULTI:
239 * NCHAN represents number of input and output channels.
240 * TO: int32_t (Q4.27) or float
241 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
242 * TV: int32_t (U4.28) or int16_t (U4.12) or float
243 * vol: represents a volume array.
244 *
245 * This accumulates into the out pointer.
246 *
247 * MIXTYPE_MONOEXPAND:
248 * Single input channel. NCHAN represents number of output channels.
249 * TO: int32_t (Q4.27) or float
250 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
251 * TV: int32_t (U4.28) or int16_t (U4.12) or float
252 * Input channel count is 1.
253 * vol: represents volume array.
254 *
255 * This accumulates into the out pointer.
256 *
257 * MIXTYPE_MULTI_SAVEONLY:
258 * NCHAN represents number of input and output channels.
259 * TO: int16_t (Q.15) or float
260 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
261 * TV: int32_t (U4.28) or int16_t (U4.12) or float
262 * vol: represents a volume array.
263 *
264 * MIXTYPE_MULTI_SAVEONLY does not accumulate into the out pointer.
Andy Hunge93b6b72014-07-17 21:30:53 -0700265 *
266 * MIXTYPE_MULTI_MONOVOL:
267 * Same as MIXTYPE_MULTI, but uses only volume[0].
268 *
269 * MIXTYPE_MULTI_SAVEONLY_MONOVOL:
270 * Same as MIXTYPE_MULTI_SAVEONLY, but uses only volume[0].
271 *
Andy Hung296b7412014-06-17 15:25:47 -0700272 */
273
274template <int MIXTYPE, int NCHAN,
275 typename TO, typename TI, typename TV, typename TA, typename TAV>
276inline void volumeRampMulti(TO* out, size_t frameCount,
277 const TI* in, TA* aux, TV *vol, const TV *volinc, TAV *vola, TAV volainc)
278{
279#ifdef ALOGVV
280 ALOGVV("volumeRampMulti, MIXTYPE:%d\n", MIXTYPE);
281#endif
282 if (aux != NULL) {
283 do {
284 TA auxaccum = 0;
285 switch (MIXTYPE) {
286 case MIXTYPE_MULTI:
287 for (int i = 0; i < NCHAN; ++i) {
288 *out++ += MixMulAux<TO, TI, TV, TA>(*in++, vol[i], &auxaccum);
289 vol[i] += volinc[i];
290 }
291 break;
Andy Hung296b7412014-06-17 15:25:47 -0700292 case MIXTYPE_MONOEXPAND:
293 for (int i = 0; i < NCHAN; ++i) {
294 *out++ += MixMulAux<TO, TI, TV, TA>(*in, vol[i], &auxaccum);
295 vol[i] += volinc[i];
296 }
297 in++;
298 break;
Andy Hunge93b6b72014-07-17 21:30:53 -0700299 case MIXTYPE_MULTI_SAVEONLY:
300 for (int i = 0; i < NCHAN; ++i) {
301 *out++ = MixMulAux<TO, TI, TV, TA>(*in++, vol[i], &auxaccum);
302 vol[i] += volinc[i];
303 }
304 break;
305 case MIXTYPE_MULTI_MONOVOL:
306 for (int i = 0; i < NCHAN; ++i) {
307 *out++ += MixMulAux<TO, TI, TV, TA>(*in++, vol[0], &auxaccum);
308 }
309 vol[0] += volinc[0];
310 break;
311 case MIXTYPE_MULTI_SAVEONLY_MONOVOL:
312 for (int i = 0; i < NCHAN; ++i) {
313 *out++ = MixMulAux<TO, TI, TV, TA>(*in++, vol[0], &auxaccum);
314 }
315 vol[0] += volinc[0];
316 break;
Andy Hung296b7412014-06-17 15:25:47 -0700317 default:
318 LOG_ALWAYS_FATAL("invalid mixtype %d", MIXTYPE);
319 break;
320 }
321 auxaccum /= NCHAN;
322 *aux++ += MixMul<TA, TA, TAV>(auxaccum, *vola);
323 vola[0] += volainc;
324 } while (--frameCount);
325 } else {
326 do {
327 switch (MIXTYPE) {
328 case MIXTYPE_MULTI:
329 for (int i = 0; i < NCHAN; ++i) {
330 *out++ += MixMul<TO, TI, TV>(*in++, vol[i]);
331 vol[i] += volinc[i];
332 }
333 break;
Andy Hung296b7412014-06-17 15:25:47 -0700334 case MIXTYPE_MONOEXPAND:
335 for (int i = 0; i < NCHAN; ++i) {
336 *out++ += MixMul<TO, TI, TV>(*in, vol[i]);
337 vol[i] += volinc[i];
338 }
339 in++;
340 break;
Andy Hunge93b6b72014-07-17 21:30:53 -0700341 case MIXTYPE_MULTI_SAVEONLY:
342 for (int i = 0; i < NCHAN; ++i) {
343 *out++ = MixMul<TO, TI, TV>(*in++, vol[i]);
344 vol[i] += volinc[i];
345 }
346 break;
347 case MIXTYPE_MULTI_MONOVOL:
348 for (int i = 0; i < NCHAN; ++i) {
349 *out++ += MixMul<TO, TI, TV>(*in++, vol[0]);
350 }
351 vol[0] += volinc[0];
352 break;
353 case MIXTYPE_MULTI_SAVEONLY_MONOVOL:
354 for (int i = 0; i < NCHAN; ++i) {
355 *out++ = MixMul<TO, TI, TV>(*in++, vol[0]);
356 }
357 vol[0] += volinc[0];
358 break;
Andy Hung296b7412014-06-17 15:25:47 -0700359 default:
360 LOG_ALWAYS_FATAL("invalid mixtype %d", MIXTYPE);
361 break;
362 }
363 } while (--frameCount);
364 }
365}
366
367template <int MIXTYPE, int NCHAN,
368 typename TO, typename TI, typename TV, typename TA, typename TAV>
369inline void volumeMulti(TO* out, size_t frameCount,
370 const TI* in, TA* aux, const TV *vol, TAV vola)
371{
372#ifdef ALOGVV
373 ALOGVV("volumeMulti MIXTYPE:%d\n", MIXTYPE);
374#endif
375 if (aux != NULL) {
376 do {
377 TA auxaccum = 0;
378 switch (MIXTYPE) {
379 case MIXTYPE_MULTI:
380 for (int i = 0; i < NCHAN; ++i) {
381 *out++ += MixMulAux<TO, TI, TV, TA>(*in++, vol[i], &auxaccum);
382 }
383 break;
Andy Hung296b7412014-06-17 15:25:47 -0700384 case MIXTYPE_MONOEXPAND:
385 for (int i = 0; i < NCHAN; ++i) {
386 *out++ += MixMulAux<TO, TI, TV, TA>(*in, vol[i], &auxaccum);
387 }
388 in++;
389 break;
Andy Hunge93b6b72014-07-17 21:30:53 -0700390 case MIXTYPE_MULTI_SAVEONLY:
391 for (int i = 0; i < NCHAN; ++i) {
392 *out++ = MixMulAux<TO, TI, TV, TA>(*in++, vol[i], &auxaccum);
393 }
394 break;
395 case MIXTYPE_MULTI_MONOVOL:
396 for (int i = 0; i < NCHAN; ++i) {
397 *out++ += MixMulAux<TO, TI, TV, TA>(*in++, vol[0], &auxaccum);
398 }
399 break;
400 case MIXTYPE_MULTI_SAVEONLY_MONOVOL:
401 for (int i = 0; i < NCHAN; ++i) {
402 *out++ = MixMulAux<TO, TI, TV, TA>(*in++, vol[0], &auxaccum);
403 }
404 break;
Andy Hung296b7412014-06-17 15:25:47 -0700405 default:
406 LOG_ALWAYS_FATAL("invalid mixtype %d", MIXTYPE);
407 break;
408 }
409 auxaccum /= NCHAN;
410 *aux++ += MixMul<TA, TA, TAV>(auxaccum, vola);
411 } while (--frameCount);
412 } else {
413 do {
414 switch (MIXTYPE) {
415 case MIXTYPE_MULTI:
416 for (int i = 0; i < NCHAN; ++i) {
417 *out++ += MixMul<TO, TI, TV>(*in++, vol[i]);
418 }
419 break;
Andy Hung296b7412014-06-17 15:25:47 -0700420 case MIXTYPE_MONOEXPAND:
421 for (int i = 0; i < NCHAN; ++i) {
422 *out++ += MixMul<TO, TI, TV>(*in, vol[i]);
423 }
424 in++;
425 break;
Andy Hunge93b6b72014-07-17 21:30:53 -0700426 case MIXTYPE_MULTI_SAVEONLY:
427 for (int i = 0; i < NCHAN; ++i) {
428 *out++ = MixMul<TO, TI, TV>(*in++, vol[i]);
429 }
430 break;
431 case MIXTYPE_MULTI_MONOVOL:
432 for (int i = 0; i < NCHAN; ++i) {
433 *out++ += MixMul<TO, TI, TV>(*in++, vol[0]);
434 }
435 break;
436 case MIXTYPE_MULTI_SAVEONLY_MONOVOL:
437 for (int i = 0; i < NCHAN; ++i) {
438 *out++ = MixMul<TO, TI, TV>(*in++, vol[0]);
439 }
440 break;
Andy Hung296b7412014-06-17 15:25:47 -0700441 default:
442 LOG_ALWAYS_FATAL("invalid mixtype %d", MIXTYPE);
443 break;
444 }
445 } while (--frameCount);
446 }
447}
448
449};
450
451#endif /* ANDROID_AUDIO_MIXER_OPS_H */