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 <vector>
20 
21 #include <aidl/android/hardware/audio/effect/BnEffect.h>
22 #include <system/audio_effects/effect_visualizer.h>
23 #include "effect-impl/EffectImpl.h"
24 
25 namespace aidl::android::hardware::audio::effect {
26 
27 class VisualizerSwContext final : public EffectContext {
28   public:
29     // need align the min/max capture size to VISUALIZER_CAPTURE_SIZE_MIN and
30     // VISUALIZER_CAPTURE_SIZE_MAX because of limitation in audio_utils fixedfft.
31     static constexpr int32_t kMinCaptureSize = VISUALIZER_CAPTURE_SIZE_MIN;
32     static constexpr int32_t kMaxCaptureSize = VISUALIZER_CAPTURE_SIZE_MAX;
33     static constexpr int32_t kMaxLatencyMs = 3000;
VisualizerSwContext(int statusDepth,const Parameter::Common & common)34     VisualizerSwContext(int statusDepth, const Parameter::Common& common)
35         : EffectContext(statusDepth, common) {
36         LOG(DEBUG) << __func__;
37         mCaptureSampleBuffer.resize(kMaxCaptureSize);
38         fill(mCaptureSampleBuffer.begin(), mCaptureSampleBuffer.end(), 0x80);
39     }
40 
41     RetCode setVsCaptureSize(int captureSize);
getVsCaptureSize()42     int getVsCaptureSize() const { return mCaptureSize; }
43 
44     RetCode setVsScalingMode(Visualizer::ScalingMode scalingMode);
getVsScalingMode()45     Visualizer::ScalingMode getVsScalingMode() const { return mScalingMode; }
46 
47     RetCode setVsMeasurementMode(Visualizer::MeasurementMode measurementMode);
getVsMeasurementMode()48     Visualizer::MeasurementMode getVsMeasurementMode() const { return mMeasurementMode; }
49 
50     RetCode setVsLatency(int latency);
getVsLatency()51     int getVsLatency() const { return mLatency; }
52 
getVsMeasurement()53     Visualizer::Measurement getVsMeasurement() const { return mMeasurement; }
getVsCaptureSampleBuffer()54     std::vector<uint8_t> getVsCaptureSampleBuffer() const { return mCaptureSampleBuffer; }
55 
56   private:
57     int mCaptureSize = kMaxCaptureSize;
58     Visualizer::ScalingMode mScalingMode = Visualizer::ScalingMode::NORMALIZED;
59     Visualizer::MeasurementMode mMeasurementMode = Visualizer::MeasurementMode::NONE;
60     int mLatency = 0;
61     const Visualizer::Measurement mMeasurement = {0, 0};
62     std::vector<uint8_t> mCaptureSampleBuffer;
63 };
64 
65 class VisualizerSw final : public EffectImpl {
66   public:
67     static const std::string kEffectName;
68     static const Capability kCapability;
69     static const Descriptor kDescriptor;
VisualizerSw()70     VisualizerSw() { LOG(DEBUG) << __func__; }
~VisualizerSw()71     ~VisualizerSw() {
72         cleanUp();
73         LOG(DEBUG) << __func__;
74     }
75 
76     ndk::ScopedAStatus getDescriptor(Descriptor* _aidl_return) override;
77     ndk::ScopedAStatus setParameterSpecific(const Parameter::Specific& specific)
78             REQUIRES(mImplMutex) override;
79     ndk::ScopedAStatus getParameterSpecific(const Parameter::Id& id, Parameter::Specific* specific)
80             REQUIRES(mImplMutex) override;
81 
82     std::shared_ptr<EffectContext> createContext(const Parameter::Common& common)
83             REQUIRES(mImplMutex) override;
84     RetCode releaseContext() REQUIRES(mImplMutex) override;
85 
86     IEffect::Status effectProcessImpl(float* in, float* out, int samples)
87             REQUIRES(mImplMutex) override;
getEffectName()88     std::string getEffectName() override { return kEffectName; }
89 
90   private:
91     static const std::vector<Range::VisualizerRange> kRanges;
92     std::shared_ptr<VisualizerSwContext> mContext GUARDED_BY(mImplMutex);
93     ndk::ScopedAStatus getParameterVisualizer(const Visualizer::Tag& tag,
94                                               Parameter::Specific* specific) REQUIRES(mImplMutex);
95 };
96 }  // namespace aidl::android::hardware::audio::effect
97