• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 <algorithm>
20 #include <memory>
21 #include <optional>
22 #include <string>
23 #include <type_traits>
24 #include <unordered_map>
25 #include <utility>
26 #include <vector>
27 
28 #include <Utils.h>
29 #include <aidl/android/hardware/audio/effect/IEffect.h>
30 #include <aidl/android/hardware/audio/effect/IFactory.h>
31 #include <aidl/android/media/audio/common/AudioChannelLayout.h>
32 #include <android/binder_auto_utils.h>
33 #include <fmq/AidlMessageQueue.h>
34 #include <gtest/gtest.h>
35 #include <system/audio_aidl_utils.h>
36 #include <system/audio_effects/aidl_effects_utils.h>
37 #include <system/audio_effects/effect_uuid.h>
38 
39 #include "EffectFactoryHelper.h"
40 #include "TestUtils.h"
41 #include "pffft.hpp"
42 
43 using namespace android;
44 using aidl::android::hardware::audio::effect::CommandId;
45 using aidl::android::hardware::audio::effect::Descriptor;
46 using aidl::android::hardware::audio::effect::getEffectTypeUuidSpatializer;
47 using aidl::android::hardware::audio::effect::getRange;
48 using aidl::android::hardware::audio::effect::IEffect;
49 using aidl::android::hardware::audio::effect::isRangeValid;
50 using aidl::android::hardware::audio::effect::kDrainSupportedVersion;
51 using aidl::android::hardware::audio::effect::kEffectTypeUuidSpatializer;
52 using aidl::android::hardware::audio::effect::kEventFlagDataMqNotEmpty;
53 using aidl::android::hardware::audio::effect::kEventFlagDataMqUpdate;
54 using aidl::android::hardware::audio::effect::kEventFlagNotEmpty;
55 using aidl::android::hardware::audio::effect::kReopenSupportedVersion;
56 using aidl::android::hardware::audio::effect::Parameter;
57 using aidl::android::hardware::audio::effect::Range;
58 using aidl::android::hardware::audio::effect::Spatializer;
59 using aidl::android::hardware::audio::effect::State;
60 using aidl::android::hardware::common::fmq::SynchronizedReadWrite;
61 using aidl::android::media::audio::common::AudioChannelLayout;
62 using aidl::android::media::audio::common::AudioFormatDescription;
63 using aidl::android::media::audio::common::AudioFormatType;
64 using aidl::android::media::audio::common::AudioUuid;
65 using aidl::android::media::audio::common::PcmType;
66 using ::android::audio::utils::toString;
67 using ::android::hardware::EventFlag;
68 
69 const AudioFormatDescription kDefaultFormatDescription = {
70         .type = AudioFormatType::PCM, .pcm = PcmType::FLOAT_32_BIT, .encoding = ""};
71 
72 typedef ::android::AidlMessageQueue<IEffect::Status,
73                                     ::aidl::android::hardware::common::fmq::SynchronizedReadWrite>
74         StatusMQ;
75 typedef ::android::AidlMessageQueue<float,
76                                     ::aidl::android::hardware::common::fmq::SynchronizedReadWrite>
77         DataMQ;
78 
getPrefix(Descriptor & descriptor)79 static inline std::string getPrefix(Descriptor& descriptor) {
80     std::string prefix = "Implementor_" + descriptor.common.implementor + "_name_" +
81                          descriptor.common.name + "_UUID_" + toString(descriptor.common.id.uuid);
82     std::replace_if(
83             prefix.begin(), prefix.end(), [](const char c) { return !std::isalnum(c); }, '_');
84     return prefix;
85 }
86 
87 static constexpr float kMaxAudioSampleValue = 1;
88 static constexpr int kNPointFFT = 16384;
89 static constexpr int kSamplingFrequency = 44100;
90 static constexpr int kDefaultChannelLayout = AudioChannelLayout::LAYOUT_STEREO;
91 static constexpr float kLn10Div20 = -0.11512925f;  // -ln(10)/20
92 
93 class EffectHelper {
94   public:
95     void create(std::shared_ptr<IFactory> factory, std::shared_ptr<IEffect>& effect,
96                 Descriptor& desc, binder_status_t status = EX_NONE) {
97         ASSERT_NE(factory, nullptr);
98         auto& id = desc.common.id;
99         ASSERT_STATUS(status, factory->createEffect(id.uuid, &effect));
100         if (status == EX_NONE) {
101             ASSERT_NE(effect, nullptr) << toString(id.uuid);
102             ASSERT_NO_FATAL_FAILURE(expectState(effect, State::INIT));
103         }
104         mIsSpatializer = id.type == getEffectTypeUuidSpatializer();
105         mDescriptor = desc;
106     }
107 
destroyIgnoreRet(std::shared_ptr<IFactory> factory,std::shared_ptr<IEffect> effect)108     static void destroyIgnoreRet(std::shared_ptr<IFactory> factory,
109                                  std::shared_ptr<IEffect> effect) {
110         if (factory && effect) {
111             factory->destroyEffect(effect);
112         }
113     }
114 
115     static void destroy(std::shared_ptr<IFactory> factory, std::shared_ptr<IEffect> effect,
116                         binder_status_t status = EX_NONE) {
117         ASSERT_NE(factory, nullptr);
118         ASSERT_NE(effect, nullptr);
119         ASSERT_STATUS(status, factory->destroyEffect(effect));
120     }
121 
122     void open(std::shared_ptr<IEffect> effect, const Parameter::Common& common,
123               const std::optional<Parameter::Specific>& specific, IEffect::OpenEffectReturn* ret,
124               binder_status_t status = EX_NONE) {
125         ASSERT_NE(effect, nullptr);
126         ASSERT_STATUS(status, effect->open(common, specific, ret));
127         if (status != EX_NONE) {
128             return;
129         }
130 
131         ASSERT_TRUE(expectState(effect, State::IDLE));
132         updateFrameSize(common);
133     }
134 
135     void open(std::shared_ptr<IEffect> effect, int session = 0, binder_status_t status = EX_NONE) {
136         ASSERT_NE(effect, nullptr);
137         Parameter::Common common = createParamCommon(session);
138         IEffect::OpenEffectReturn ret;
139         ASSERT_NO_FATAL_FAILURE(open(effect, common, std::nullopt /* specific */, &ret, status));
140     }
141 
142     void reopen(std::shared_ptr<IEffect> effect, const Parameter::Common& common,
143                 IEffect::OpenEffectReturn* ret, binder_status_t status = EX_NONE) {
144         ASSERT_NE(effect, nullptr);
145         ASSERT_STATUS(status, effect->reopen(ret));
146         if (status != EX_NONE) {
147             return;
148         }
149         updateFrameSize(common);
150     }
151 
closeIgnoreRet(std::shared_ptr<IEffect> effect)152     static void closeIgnoreRet(std::shared_ptr<IEffect> effect) {
153         if (effect) {
154             effect->close();
155         }
156     }
157 
158     static void close(std::shared_ptr<IEffect> effect, binder_status_t status = EX_NONE) {
159         if (effect) {
160             ASSERT_STATUS(status, effect->close());
161             if (status == EX_NONE) {
162                 ASSERT_TRUE(expectState(effect, State::INIT));
163             }
164         }
165     }
166 
167     static void getDescriptor(std::shared_ptr<IEffect> effect, Descriptor& desc,
168                               binder_status_t status = EX_NONE) {
169         ASSERT_NE(effect, nullptr);
170         ASSERT_STATUS(status, effect->getDescriptor(&desc));
171     }
172 
expectState(std::shared_ptr<IEffect> effect,State expectState)173     static bool expectState(std::shared_ptr<IEffect> effect, State expectState) {
174         if (effect == nullptr) return false;
175 
176         if (State state; EX_NONE != effect->getState(&state).getStatus() || expectState != state) {
177             return false;
178         }
179 
180         return true;
181     }
182 
commandIgnoreRet(std::shared_ptr<IEffect> effect,CommandId command)183     static void commandIgnoreRet(std::shared_ptr<IEffect> effect, CommandId command) {
184         if (effect) {
185             effect->command(command);
186         }
187     }
188 
189     static void command(std::shared_ptr<IEffect> effect, CommandId command,
190                         binder_status_t status = EX_NONE) {
191         ASSERT_NE(effect, nullptr);
192         ASSERT_STATUS(status, effect->command(command));
193         if (status != EX_NONE) {
194             return;
195         }
196 
197         switch (command) {
198             case CommandId::START:
199                 ASSERT_TRUE(expectState(effect, State::PROCESSING));
200                 break;
201             case CommandId::STOP:
202                 // Enforce the state checking after kDrainSupportedVersion
203                 if (getHalVersion(effect) >= kDrainSupportedVersion) {
204                     ASSERT_TRUE(expectState(effect, State::IDLE) ||
205                                 expectState(effect, State::DRAINING));
206                 }
207                 break;
208             case CommandId::RESET:
209                 ASSERT_TRUE(expectState(effect, State::IDLE));
210                 break;
211             default:
212                 return;
213         }
214     }
215 
writeToFmq(std::unique_ptr<StatusMQ> & statusMq,std::unique_ptr<DataMQ> & dataMq,const std::vector<float> & buffer,int version)216     static void writeToFmq(std::unique_ptr<StatusMQ>& statusMq, std::unique_ptr<DataMQ>& dataMq,
217                            const std::vector<float>& buffer, int version) {
218         const size_t available = dataMq->availableToWrite();
219         ASSERT_NE(0Ul, available);
220         auto bufferFloats = buffer.size();
221         auto floatsToWrite = std::min(available, bufferFloats);
222         ASSERT_TRUE(dataMq->write(buffer.data(), floatsToWrite));
223 
224         EventFlag* efGroup;
225         ASSERT_EQ(::android::OK,
226                   EventFlag::createEventFlag(statusMq->getEventFlagWord(), &efGroup));
227         ASSERT_NE(nullptr, efGroup);
228         efGroup->wake(version >= kReopenSupportedVersion ? kEventFlagDataMqNotEmpty
229                                                          : kEventFlagNotEmpty);
230         ASSERT_EQ(::android::OK, EventFlag::deleteEventFlag(&efGroup));
231     }
232 
233     static void readFromFmq(std::unique_ptr<StatusMQ>& statusMq, size_t statusNum,
234                             std::unique_ptr<DataMQ>& dataMq, size_t expectFloats,
235                             std::vector<float>& buffer,
236                             std::optional<int> expectStatus = STATUS_OK) {
237         if (0 == statusNum) {
238             ASSERT_EQ(0ul, statusMq->availableToRead());
239             return;
240         }
241         IEffect::Status status{};
242         ASSERT_TRUE(statusMq->readBlocking(&status, statusNum));
243         if (expectStatus.has_value()) {
244             ASSERT_EQ(expectStatus.value(), status.status);
245         }
246 
247         ASSERT_EQ(expectFloats, (unsigned)status.fmqProduced);
248         ASSERT_EQ(expectFloats, dataMq->availableToRead());
249         if (expectFloats != 0) {
250             ASSERT_TRUE(dataMq->read(buffer.data(), expectFloats));
251         }
252     }
253 
expectDataMqUpdateEventFlag(std::unique_ptr<StatusMQ> & statusMq)254     static void expectDataMqUpdateEventFlag(std::unique_ptr<StatusMQ>& statusMq) {
255         EventFlag* efGroup;
256         ASSERT_EQ(::android::OK,
257                   EventFlag::createEventFlag(statusMq->getEventFlagWord(), &efGroup));
258         ASSERT_NE(nullptr, efGroup);
259         uint32_t efState = 0;
260         EXPECT_EQ(::android::OK, efGroup->wait(kEventFlagDataMqUpdate, &efState, 1'000'000 /*1ms*/,
261                                                true /* retry */));
262         EXPECT_TRUE(efState & kEventFlagDataMqUpdate);
263     }
264 
265     Parameter::Common createParamCommon(
266             int session = 0, int ioHandle = -1, int iSampleRate = 48000, int oSampleRate = 48000,
267             long iFrameCount = 0x100, long oFrameCount = 0x100,
268             AudioChannelLayout inputChannelLayout =
269                     AudioChannelLayout::make<AudioChannelLayout::layoutMask>(kDefaultChannelLayout),
270             AudioChannelLayout outputChannelLayout =
271                     AudioChannelLayout::make<AudioChannelLayout::layoutMask>(
272                             kDefaultChannelLayout)) {
273         // query supported input layout and use it as the default parameter in common
274         if (mIsSpatializer && isRangeValid<Range::spatializer>(Spatializer::supportedChannelLayout,
275                                                                mDescriptor.capability)) {
276             const auto layoutRange = getRange<Range::spatializer, Range::SpatializerRange>(
277                     mDescriptor.capability, Spatializer::supportedChannelLayout);
278             if (std::vector<AudioChannelLayout> layouts;
279                 layoutRange &&
280                 0 != (layouts = layoutRange->min.get<Spatializer::supportedChannelLayout>())
281                                 .size()) {
282                 inputChannelLayout = layouts[0];
283             }
284         }
285 
286         Parameter::Common common;
287         common.session = session;
288         common.ioHandle = ioHandle;
289 
290         auto& input = common.input;
291         auto& output = common.output;
292         input.base.sampleRate = iSampleRate;
293         input.base.channelMask = inputChannelLayout;
294         input.base.format = kDefaultFormatDescription;
295         input.frameCount = iFrameCount;
296         output.base.sampleRate = oSampleRate;
297         output.base.channelMask = outputChannelLayout;
298         output.base.format = kDefaultFormatDescription;
299         output.frameCount = oFrameCount;
300         return common;
301     }
302 
303     typedef ::android::AidlMessageQueue<
304             IEffect::Status, ::aidl::android::hardware::common::fmq::SynchronizedReadWrite>
305             StatusMQ;
306     typedef ::android::AidlMessageQueue<
307             float, ::aidl::android::hardware::common::fmq::SynchronizedReadWrite>
308             DataMQ;
309 
310     class EffectParam {
311       public:
312         std::unique_ptr<StatusMQ> statusMQ;
313         std::unique_ptr<DataMQ> inputMQ;
314         std::unique_ptr<DataMQ> outputMQ;
315     };
316 
317     template <typename T, Range::Tag tag>
isParameterValid(const T & target,const Descriptor & desc)318     static bool isParameterValid(const T& target, const Descriptor& desc) {
319         if (desc.capability.range.getTag() != tag) {
320             return true;
321         }
322         const auto& ranges = desc.capability.range.get<tag>();
323         return inRange(target, ranges);
324     }
325 
326     /**
327      * Add to test value set: (min+max)/2, minimum/maximum numeric limits, and min-1/max+1 if
328      * result still in numeric limits after -1/+1.
329      * Only use this when the type of test value is basic type (std::is_arithmetic return true).
330      */
331     template <typename S, typename = std::enable_if_t<std::is_arithmetic_v<S>>>
expandTestValueBasic(std::set<S> & s)332     static std::set<S> expandTestValueBasic(std::set<S>& s) {
333         const auto minLimit = std::numeric_limits<S>::min(),
334                    maxLimit = std::numeric_limits<S>::max();
335         if (s.size()) {
336             const auto min = *s.begin(), max = *s.rbegin();
337             s.insert((min & max) + ((min ^ max) >> 1));
338             if (min > minLimit + 1) {
339                 s.insert(min - 1);
340             }
341             if (max < maxLimit - 1) {
342                 s.insert(max + 1);
343             }
344         }
345         s.insert(minLimit);
346         s.insert(maxLimit);
347         return s;
348     }
349 
350     template <typename T, typename S, Range::Tag R, typename T::Tag tag>
getTestValueSet(std::vector<std::pair<std::shared_ptr<IFactory>,Descriptor>> descList)351     static std::set<S> getTestValueSet(
352             std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> descList) {
353         std::set<S> result;
354         for (const auto& [_, desc] : descList) {
355             if (desc.capability.range.getTag() == R) {
356                 const auto& ranges = desc.capability.range.get<R>();
357                 for (const auto& range : ranges) {
358                     if (range.min.getTag() == tag) {
359                         result.insert(range.min.template get<tag>());
360                     }
361                     if (range.max.getTag() == tag) {
362                         result.insert(range.max.template get<tag>());
363                     }
364                 }
365             }
366         }
367         return result;
368     }
369 
370     template <typename T, typename S, Range::Tag R, typename T::Tag tag, typename Functor>
getTestValueSet(std::vector<std::pair<std::shared_ptr<IFactory>,Descriptor>> descList,Functor functor)371     static std::set<S> getTestValueSet(
372             std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> descList,
373             Functor functor) {
374         auto result = getTestValueSet<T, S, R, tag>(descList);
375         return functor(result);
376     }
377 
378     // keep writing data to the FMQ until effect transit from DRAINING to IDLE
waitForDrain(std::vector<float> & inputBuffer,std::vector<float> & outputBuffer,const std::shared_ptr<IEffect> & effect,std::unique_ptr<EffectHelper::StatusMQ> & statusMQ,std::unique_ptr<EffectHelper::DataMQ> & inputMQ,std::unique_ptr<EffectHelper::DataMQ> & outputMQ,int version)379     static void waitForDrain(std::vector<float>& inputBuffer, std::vector<float>& outputBuffer,
380                              const std::shared_ptr<IEffect>& effect,
381                              std::unique_ptr<EffectHelper::StatusMQ>& statusMQ,
382                              std::unique_ptr<EffectHelper::DataMQ>& inputMQ,
383                              std::unique_ptr<EffectHelper::DataMQ>& outputMQ, int version) {
384         State state;
385         while (effect->getState(&state).getStatus() == EX_NONE && state == State::DRAINING) {
386             EXPECT_NO_FATAL_FAILURE(
387                     EffectHelper::writeToFmq(statusMQ, inputMQ, inputBuffer, version));
388             EXPECT_NO_FATAL_FAILURE(EffectHelper::readFromFmq(
389                     statusMQ, 1, outputMQ, outputBuffer.size(), outputBuffer, std::nullopt));
390         }
391         ASSERT_TRUE(State::IDLE == state);
392         EXPECT_NO_FATAL_FAILURE(EffectHelper::readFromFmq(statusMQ, 0, outputMQ, 0, outputBuffer));
393         return;
394     }
395 
396     static void processAndWriteToOutput(std::vector<float>& inputBuffer,
397                                         std::vector<float>& outputBuffer,
398                                         const std::shared_ptr<IEffect>& effect,
399                                         IEffect::OpenEffectReturn* openEffectReturn,
400                                         int version = -1, int times = 1,
401                                         bool callStopReset = true) {
402         // Initialize AidlMessagequeues
403         auto statusMQ = std::make_unique<EffectHelper::StatusMQ>(openEffectReturn->statusMQ);
404         ASSERT_TRUE(statusMQ->isValid());
405         auto inputMQ = std::make_unique<EffectHelper::DataMQ>(openEffectReturn->inputDataMQ);
406         ASSERT_TRUE(inputMQ->isValid());
407         auto outputMQ = std::make_unique<EffectHelper::DataMQ>(openEffectReturn->outputDataMQ);
408         ASSERT_TRUE(outputMQ->isValid());
409 
410         // Enabling the process
411         ASSERT_NO_FATAL_FAILURE(command(effect, CommandId::START));
412 
413         // Write from buffer to message queues and calling process
414         if (version == -1) {
415             ASSERT_IS_OK(effect->getInterfaceVersion(&version));
416         }
417 
418         for (int i = 0; i < times; i++) {
419             EXPECT_NO_FATAL_FAILURE(
420                     EffectHelper::writeToFmq(statusMQ, inputMQ, inputBuffer, version));
421             // Read the updated message queues into buffer
422             EXPECT_NO_FATAL_FAILURE(EffectHelper::readFromFmq(statusMQ, 1, outputMQ,
423                                                               outputBuffer.size(), outputBuffer));
424         }
425 
426         // Disable the process
427         if (callStopReset) {
428             ASSERT_NO_FATAL_FAILURE(command(effect, CommandId::STOP));
429             EXPECT_NO_FATAL_FAILURE(waitForDrain(inputBuffer, outputBuffer, effect, statusMQ,
430                                                  inputMQ, outputMQ, version));
431         }
432 
433         if (callStopReset) {
434             ASSERT_NO_FATAL_FAILURE(command(effect, CommandId::RESET));
435         }
436     }
437 
438     // Find FFT bin indices for testFrequencies and get bin center frequencies
roundToFreqCenteredToFftBin(std::vector<int> & testFrequencies,std::vector<int> & binOffsets,const float kBinWidth)439     void roundToFreqCenteredToFftBin(std::vector<int>& testFrequencies,
440                                      std::vector<int>& binOffsets, const float kBinWidth) {
441         for (size_t i = 0; i < testFrequencies.size(); i++) {
442             binOffsets[i] = std::round(testFrequencies[i] / kBinWidth);
443             testFrequencies[i] = std::round(binOffsets[i] * kBinWidth);
444         }
445     }
446 
447     // Fill inputBuffer with random values between -maxAudioSampleValue to maxAudioSampleValue
448     void generateInputBuffer(std::vector<float>& inputBuffer, size_t startPosition, bool isStrip,
449                              size_t channelCount,
450                              float maxAudioSampleValue = kMaxAudioSampleValue) {
451         size_t increment = isStrip ? 1 /*Fill input at all the channels*/
452                                    : channelCount /*Fill input at only one channel*/;
453 
454         for (size_t i = startPosition; i < inputBuffer.size(); i += increment) {
455             inputBuffer[i] =
456                     ((static_cast<float>(std::rand()) / RAND_MAX) * 2 - 1) * maxAudioSampleValue;
457         }
458     }
459 
460     // Generate multitone input between -amplitude to +amplitude using testFrequencies
461     // All test frequencies are considered having the same amplitude
462     // The function supports only mono and stereo channel layout
463     void generateSineWave(const std::vector<int>& testFrequencies, std::vector<float>& input,
464                           const float amplitude = 1.0,
465                           const int samplingFrequency = kSamplingFrequency,
466                           int channelLayout = AudioChannelLayout::LAYOUT_STEREO) {
467         bool isStereo = (channelLayout == AudioChannelLayout::LAYOUT_STEREO);
468         if (isStereo) {
469             ASSERT_EQ(input.size() % 2, 0u)
470                     << "In case of stereo input, the input size value must be even";
471         }
472         for (size_t i = 0; i < input.size(); i += (isStereo ? 2 : 1)) {
473             input[i] = 0;
474 
475             for (size_t j = 0; j < testFrequencies.size(); j++) {
476                 input[i] += sin(2 * M_PI * testFrequencies[j] * (i / (isStereo ? 2 : 1)) /
477                                 samplingFrequency);
478             }
479             input[i] *= amplitude / testFrequencies.size();
480 
481             if (isStereo) {
482                 input[i + 1] = input[i];
483             }
484         }
485     }
486 
487     // Generate single tone input between -amplitude to +amplitude using testFrequency
488     // The function supports only mono and stereo channel layout
489     void generateSineWave(const int testFrequency, std::vector<float>& input,
490                           const float amplitude = 1.0,
491                           const int samplingFrequency = kSamplingFrequency,
492                           int channelLayout = AudioChannelLayout::LAYOUT_STEREO) {
493         ASSERT_NO_FATAL_FAILURE(generateSineWave(std::vector<int>{testFrequency}, input, amplitude,
494                                                  samplingFrequency, channelLayout));
495     }
496 
497     // PFFFT only supports transforms for inputs of length N of the form N = (2^a)*(3^b)*(5^c) where
498     // a >= 5, b >=0, c >= 0.
isFftInputSizeValid(size_t n)499     constexpr bool isFftInputSizeValid(size_t n) {
500         if (n == 0 || n & 0b11111) {
501             return false;
502         }
503         for (const int factor : {2, 3, 5}) {
504             while (n % factor == 0) {
505                 n /= factor;
506             }
507         }
508         return n == 1;
509     }
510 
511     // Use FFT transform to convert the buffer to frequency domain
512     // Compute its magnitude at binOffsets
513     void calculateMagnitudeMono(std::vector<float>& bufferMag,       // Output parameter
514                                 const std::vector<float>& buffer,    // Input parameter
515                                 const std::vector<int>& binOffsets,  // Input parameter
516                                 const int nPointFFT = kNPointFFT) {  // Input parameter
517         ASSERT_TRUE(isFftInputSizeValid(nPointFFT))
518                 << "PFFFT only supports transforms for inputs of length N of the form N = (2 ^ a) "
519                    "* (3 ^ b) * (5 ^ c) where a >= 5, b >= 0, c >= 0. ";
520         ASSERT_GE((int)buffer.size(), nPointFFT)
521                 << "The input(buffer) size must be greater than or equal to nPointFFT";
522         bufferMag.resize(binOffsets.size());
523         std::vector<float> fftInput(nPointFFT);
524         pffft::detail::PFFFT_Setup* inputHandle =
525                 pffft_new_setup(nPointFFT, pffft::detail::PFFFT_REAL);
526         pffft_transform_ordered(inputHandle, buffer.data(), fftInput.data(), nullptr,
527                                 pffft::detail::PFFFT_FORWARD);
528         pffft_destroy_setup(inputHandle);
529         for (size_t i = 0; i < binOffsets.size(); i++) {
530             size_t k = binOffsets[i];
531             bufferMag[i] = sqrt((fftInput[k * 2] * fftInput[k * 2]) +
532                                 (fftInput[k * 2 + 1] * fftInput[k * 2 + 1]));
533         }
534     }
535 
536     // Use FFT transform to convert the buffer to frequency domain
537     // Compute its magnitude at binOffsets
538     void calculateMagnitudeStereo(
539             std::pair<std::vector<float>, std::vector<float>>& bufferMag,  // Output parameter
540             const std::vector<float>& buffer,                              // Input parameter
541             const std::vector<int>& binOffsets,                            // Input parameter
542             const int nPointFFT = kNPointFFT) {                            // Input parameter
543         std::vector<float> leftChannelBuffer(buffer.size() / 2),
544                 rightChannelBuffer(buffer.size() / 2);
545         for (size_t i = 0; i < buffer.size(); i += 2) {
546             leftChannelBuffer[i / 2] = buffer[i];
547             rightChannelBuffer[i / 2] = buffer[i + 1];
548         }
549         std::vector<float> leftMagnitude(binOffsets.size());
550         std::vector<float> rightMagnitude(binOffsets.size());
551 
552         ASSERT_NO_FATAL_FAILURE(
553                 calculateMagnitudeMono(leftMagnitude, leftChannelBuffer, binOffsets, nPointFFT));
554         ASSERT_NO_FATAL_FAILURE(
555                 calculateMagnitudeMono(rightMagnitude, rightChannelBuffer, binOffsets, nPointFFT));
556 
557         bufferMag = {leftMagnitude, rightMagnitude};
558     }
559 
560     // Computes magnitude for mono and stereo inputs and verifies equal magnitude for left and right
561     // channel in case of stereo inputs
562     void calculateAndVerifyMagnitude(std::vector<float>& mag,             // Output parameter
563                                      const int channelLayout,             // Input parameter
564                                      const std::vector<float>& buffer,    // Input parameter
565                                      const std::vector<int>& binOffsets,  // Input parameter
566                                      const int nPointFFT = kNPointFFT) {  // Input parameter
567         if (channelLayout == AudioChannelLayout::LAYOUT_STEREO) {
568             std::pair<std::vector<float>, std::vector<float>> magStereo;
569             ASSERT_NO_FATAL_FAILURE(
570                     calculateMagnitudeStereo(magStereo, buffer, binOffsets, nPointFFT));
571             ASSERT_EQ(magStereo.first, magStereo.second);
572 
573             mag = magStereo.first;
574         } else {
575             ASSERT_NO_FATAL_FAILURE(calculateMagnitudeMono(mag, buffer, binOffsets, nPointFFT));
576         }
577     }
578 
updateFrameSize(const Parameter::Common & common)579     void updateFrameSize(const Parameter::Common& common) {
580         mInputFrameSize = ::aidl::android::hardware::audio::common::getFrameSizeInBytes(
581                 common.input.base.format, common.input.base.channelMask);
582         mInputSamples = common.input.frameCount * mInputFrameSize / sizeof(float);
583         mOutputFrameSize = ::aidl::android::hardware::audio::common::getFrameSizeInBytes(
584                 common.output.base.format, common.output.base.channelMask);
585         mOutputSamples = common.output.frameCount * mOutputFrameSize / sizeof(float);
586     }
587 
588     void generateInput(std::vector<float>& input, float inputFrequency, float samplingFrequency,
589                        size_t inputSize = 0) {
590         if (inputSize == 0 || inputSize > input.size()) {
591             inputSize = input.size();
592         }
593 
594         for (size_t i = 0; i < inputSize; i++) {
595             input[i] = sin(2 * M_PI * inputFrequency * i / samplingFrequency);
596         }
597     }
598 
dBToAmplitude(float dB)599     constexpr float dBToAmplitude(float dB) { return std::exp(dB * kLn10Div20); }
600 
getHalVersion(const std::shared_ptr<IEffect> & effect)601     static int getHalVersion(const std::shared_ptr<IEffect>& effect) {
602         int version = 0;
603         return (effect && effect->getInterfaceVersion(&version).isOk()) ? version : 0;
604     }
605 
606     bool mIsSpatializer;
607     Descriptor mDescriptor;
608     size_t mInputFrameSize, mOutputFrameSize;
609     size_t mInputSamples, mOutputSamples;
610 };
611