• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 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 #pragma once
18 
19 #include <array>
20 #include <audio_effects/effect_aec.h>
21 #include <audio_utils/channels.h>
22 #include <audio_utils/primitives.h>
23 #include <climits>
24 #include <cstdlib>
25 #include <gtest/gtest.h>
26 #include <hardware/audio_effect.h>
27 #include <log/log.h>
28 #include <random>
29 #include <stdint.h>
30 #include <system/audio.h>
31 #include <vector>
32 
33 template <typename T>
computeSnr(const T * ref,const T * tst,size_t count)34 static float computeSnr(const T* ref, const T* tst, size_t count) {
35     double signal{};
36     double noise{};
37 
38     for (size_t i = 0; i < count; ++i) {
39         const double value(ref[i]);
40         const double diff(tst[i] - value);
41         signal += value * value;
42         noise += diff * diff;
43     }
44     // Initialized to large value to handle
45     // cases where ref and tst match exactly
46     float snr = FLT_MAX;
47     if (signal > 0.0f && noise > 0.0f) {
48         snr = 10.f * log(signal / noise);
49     }
50     return snr;
51 }
52 
53 class EffectTestHelper {
54   public:
EffectTestHelper(const effect_uuid_t * uuid,size_t chMask,size_t sampleRate,size_t loopCount)55     EffectTestHelper(const effect_uuid_t* uuid, size_t chMask, size_t sampleRate, size_t loopCount)
56         : mUuid(uuid),
57           mChMask(chMask),
58           mChannelCount(audio_channel_count_from_in_mask(mChMask)),
59           mSampleRate(sampleRate),
60           mFrameCount(mSampleRate * kTenMilliSecVal),
61           mLoopCount(loopCount) {}
62     void createEffect();
63     void releaseEffect();
64     void setConfig(bool configReverse);
65     void setParam(uint32_t type, uint32_t val);
66     void process(int16_t* input, int16_t* output, bool setAecEchoDelay);
67     void process_reverse(int16_t* farInput, int16_t* output);
68 
69     // Corresponds to SNR for 1 bit difference between two int16_t signals
70     static constexpr float kSNRThreshold = 90.308998;
71 
72     static constexpr audio_channel_mask_t kChMasks[] = {
73             AUDIO_CHANNEL_IN_MONO,
74             AUDIO_CHANNEL_IN_STEREO,
75             AUDIO_CHANNEL_IN_FRONT_BACK,
76             AUDIO_CHANNEL_IN_6,
77             AUDIO_CHANNEL_IN_2POINT0POINT2,
78             AUDIO_CHANNEL_IN_2POINT1POINT2,
79             AUDIO_CHANNEL_IN_3POINT0POINT2,
80             AUDIO_CHANNEL_IN_3POINT1POINT2,
81             AUDIO_CHANNEL_IN_5POINT1,
82             AUDIO_CHANNEL_IN_VOICE_UPLINK_MONO,
83             AUDIO_CHANNEL_IN_VOICE_DNLINK_MONO,
84             AUDIO_CHANNEL_IN_VOICE_CALL_MONO,
85     };
86 
87     static constexpr float kTenMilliSecVal = 0.01;
88 
89     static constexpr size_t kNumChMasks = std::size(kChMasks);
90 
91     static constexpr size_t kSampleRates[] = {8000,  11025, 12000, 16000, 22050,
92                                               24000, 32000, 44100, 48000};
93 
94     static constexpr size_t kNumSampleRates = std::size(kSampleRates);
95 
96     static constexpr size_t kLoopCounts[] = {1, 4};
97 
98     static constexpr size_t kNumLoopCounts = std::size(kLoopCounts);
99 
100     static constexpr size_t kAECDelay = 0;
101 
102   private:
103     const effect_uuid_t* mUuid;
104     const size_t mChMask;
105     const size_t mChannelCount;
106     const size_t mSampleRate;
107     const size_t mFrameCount;
108     const size_t mLoopCount;
109     effect_handle_t mEffectHandle{};
110 };
111