1 /*
2 * Copyright (C) 2020 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 #include <inttypes.h>
18 #include <type_traits>
19 #include "../../../../system/media/audio_utils/include/audio_utils/primitives.h"
20 #define LOG_ALWAYS_FATAL(...)
21
22 #include <../AudioMixerOps.h>
23
24 #include <benchmark/benchmark.h>
25
26 using namespace android;
27
28 template <int MIXTYPE, int NCHAN>
BM_VolumeRampMulti(benchmark::State & state)29 static void BM_VolumeRampMulti(benchmark::State& state) {
30 constexpr size_t FRAME_COUNT = 1000;
31 constexpr size_t SAMPLE_COUNT = FRAME_COUNT * NCHAN;
32
33 // data inialized to 0.
34 float out[SAMPLE_COUNT]{};
35 float in[SAMPLE_COUNT]{};
36 float aux[FRAME_COUNT]{};
37
38 // volume initialized to 0
39 float vola = 0.f;
40 float vol[2] = {0.f, 0.f};
41
42 // some volume increment
43 float volainc = 0.01f;
44 float volinc[2] = {0.01f, 0.01f};
45
46 while (state.KeepRunning()) {
47 benchmark::DoNotOptimize(out);
48 benchmark::DoNotOptimize(in);
49 volumeRampMulti<MIXTYPE, NCHAN>(out, FRAME_COUNT, in, aux, vol, volinc, &vola, volainc);
50 benchmark::ClobberMemory();
51 }
52 }
53
54 template <int MIXTYPE, int NCHAN>
BM_VolumeMulti(benchmark::State & state)55 static void BM_VolumeMulti(benchmark::State& state) {
56 constexpr size_t FRAME_COUNT = 1000;
57 constexpr size_t SAMPLE_COUNT = FRAME_COUNT * NCHAN;
58
59 // data inialized to 0.
60 float out[SAMPLE_COUNT]{};
61 float in[SAMPLE_COUNT]{};
62 float aux[FRAME_COUNT]{};
63
64 // volume initialized to 0
65 float vola = 0.f;
66 float vol[2] = {0.f, 0.f};
67
68
69 while (state.KeepRunning()) {
70 benchmark::DoNotOptimize(out);
71 benchmark::DoNotOptimize(in);
72 volumeMulti<MIXTYPE, NCHAN>(out, FRAME_COUNT, in, aux, vol, vola);
73 benchmark::ClobberMemory();
74 }
75 }
76
77 // MULTI mode and MULTI_SAVEONLY mode are not used by AudioMixer for channels > 2,
78 // which is ensured by a static_assert (won't compile for those configurations).
79 // So we benchmark MIXTYPE_MULTI_MONOVOL and MIXTYPE_MULTI_SAVEONLY_MONOVOL compared
80 // with MIXTYPE_MULTI_STEREOVOL and MIXTYPE_MULTI_SAVEONLY_STEREOVOL.
81 BENCHMARK_TEMPLATE(BM_VolumeRampMulti, MIXTYPE_MULTI_MONOVOL, 2);
82 BENCHMARK_TEMPLATE(BM_VolumeRampMulti, MIXTYPE_MULTI_SAVEONLY_MONOVOL, 2);
83 BENCHMARK_TEMPLATE(BM_VolumeRampMulti, MIXTYPE_MULTI_STEREOVOL, 2);
84 BENCHMARK_TEMPLATE(BM_VolumeRampMulti, MIXTYPE_MULTI_SAVEONLY_STEREOVOL, 2);
85
86 BENCHMARK_TEMPLATE(BM_VolumeRampMulti, MIXTYPE_MULTI_MONOVOL, 4);
87 BENCHMARK_TEMPLATE(BM_VolumeRampMulti, MIXTYPE_MULTI_SAVEONLY_MONOVOL, 4);
88 BENCHMARK_TEMPLATE(BM_VolumeRampMulti, MIXTYPE_MULTI_STEREOVOL, 4);
89 BENCHMARK_TEMPLATE(BM_VolumeRampMulti, MIXTYPE_MULTI_SAVEONLY_STEREOVOL, 4);
90
91 BENCHMARK_TEMPLATE(BM_VolumeRampMulti, MIXTYPE_MULTI_MONOVOL, 5);
92 BENCHMARK_TEMPLATE(BM_VolumeRampMulti, MIXTYPE_MULTI_SAVEONLY_MONOVOL, 5);
93 BENCHMARK_TEMPLATE(BM_VolumeRampMulti, MIXTYPE_MULTI_STEREOVOL, 5);
94 BENCHMARK_TEMPLATE(BM_VolumeRampMulti, MIXTYPE_MULTI_SAVEONLY_STEREOVOL, 5);
95
96 BENCHMARK_TEMPLATE(BM_VolumeRampMulti, MIXTYPE_MULTI_MONOVOL, 8);
97 BENCHMARK_TEMPLATE(BM_VolumeRampMulti, MIXTYPE_MULTI_SAVEONLY_MONOVOL, 8);
98 BENCHMARK_TEMPLATE(BM_VolumeRampMulti, MIXTYPE_MULTI_STEREOVOL, 8);
99 BENCHMARK_TEMPLATE(BM_VolumeRampMulti, MIXTYPE_MULTI_SAVEONLY_STEREOVOL, 8);
100
101 BENCHMARK_TEMPLATE(BM_VolumeMulti, MIXTYPE_MULTI_MONOVOL, 8);
102 BENCHMARK_TEMPLATE(BM_VolumeMulti, MIXTYPE_MULTI_SAVEONLY_MONOVOL, 8);
103 BENCHMARK_TEMPLATE(BM_VolumeMulti, MIXTYPE_MULTI_STEREOVOL, 8);
104 BENCHMARK_TEMPLATE(BM_VolumeMulti, MIXTYPE_MULTI_SAVEONLY_STEREOVOL, 8);
105
106 BENCHMARK_MAIN();
107