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 BENCHMARK_TEMPLATE(BM_VolumeRampMulti, MIXTYPE_MULTI, 2);
78 BENCHMARK_TEMPLATE(BM_VolumeRampMulti, MIXTYPE_MULTI_SAVEONLY, 2);
79 BENCHMARK_TEMPLATE(BM_VolumeRampMulti, MIXTYPE_MULTI_STEREOVOL, 2);
80 BENCHMARK_TEMPLATE(BM_VolumeRampMulti, MIXTYPE_MULTI_SAVEONLY_STEREOVOL, 2);
81 
82 BENCHMARK_TEMPLATE(BM_VolumeRampMulti, MIXTYPE_MULTI, 4);
83 BENCHMARK_TEMPLATE(BM_VolumeRampMulti, MIXTYPE_MULTI_SAVEONLY, 4);
84 BENCHMARK_TEMPLATE(BM_VolumeRampMulti, MIXTYPE_MULTI_STEREOVOL, 4);
85 BENCHMARK_TEMPLATE(BM_VolumeRampMulti, MIXTYPE_MULTI_SAVEONLY_STEREOVOL, 4);
86 
87 BENCHMARK_TEMPLATE(BM_VolumeRampMulti, MIXTYPE_MULTI, 5);
88 BENCHMARK_TEMPLATE(BM_VolumeRampMulti, MIXTYPE_MULTI_SAVEONLY, 5);
89 BENCHMARK_TEMPLATE(BM_VolumeRampMulti, MIXTYPE_MULTI_STEREOVOL, 5);
90 BENCHMARK_TEMPLATE(BM_VolumeRampMulti, MIXTYPE_MULTI_SAVEONLY_STEREOVOL, 5);
91 
92 BENCHMARK_TEMPLATE(BM_VolumeRampMulti, MIXTYPE_MULTI, 8);
93 BENCHMARK_TEMPLATE(BM_VolumeRampMulti, MIXTYPE_MULTI_SAVEONLY, 8);
94 BENCHMARK_TEMPLATE(BM_VolumeRampMulti, MIXTYPE_MULTI_STEREOVOL, 8);
95 BENCHMARK_TEMPLATE(BM_VolumeRampMulti, MIXTYPE_MULTI_SAVEONLY_STEREOVOL, 8);
96 
97 BENCHMARK_TEMPLATE(BM_VolumeMulti, MIXTYPE_MULTI, 8);
98 BENCHMARK_TEMPLATE(BM_VolumeMulti, MIXTYPE_MULTI_SAVEONLY, 8);
99 BENCHMARK_TEMPLATE(BM_VolumeMulti, MIXTYPE_MULTI_STEREOVOL, 8);
100 BENCHMARK_TEMPLATE(BM_VolumeMulti, MIXTYPE_MULTI_SAVEONLY_STEREOVOL, 8);
101 
102 BENCHMARK_MAIN();
103