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 <audio_effects/effect_dynamicsprocessing.h> 20 #include <system/audio_effects/effect_visualizer.h> 21 22 #include "effect-impl/EffectContext.h" 23 24 namespace aidl::android::hardware::audio::effect { 25 26 class VisualizerContext final : public EffectContext { 27 public: 28 // need align the min/max capture size to VISUALIZER_CAPTURE_SIZE_MIN and 29 // VISUALIZER_CAPTURE_SIZE_MAX because of limitation in audio_utils fixedfft. 30 static constexpr int32_t kMinCaptureBufSize = VISUALIZER_CAPTURE_SIZE_MIN; 31 static constexpr int32_t kMaxCaptureBufSize = VISUALIZER_CAPTURE_SIZE_MAX; 32 static constexpr uint32_t kMaxLatencyMs = 3000; // 3 seconds of latency for audio pipeline 33 34 VisualizerContext(int statusDepth, const Parameter::Common& common); 35 ~VisualizerContext(); 36 37 RetCode initParams(const Parameter::Common& common); 38 39 RetCode enable(); 40 RetCode disable(); 41 // keep all parameters and reset buffer. 42 void reset(); 43 44 RetCode setCaptureSamples(int32_t captureSize); 45 int32_t getCaptureSamples(); 46 RetCode setMeasurementMode(Visualizer::MeasurementMode mode); 47 Visualizer::MeasurementMode getMeasurementMode(); 48 RetCode setScalingMode(Visualizer::ScalingMode mode); 49 Visualizer::ScalingMode getScalingMode(); 50 RetCode setDownstreamLatency(int latency); 51 int getDownstreamLatency(); 52 53 IEffect::Status process(float* in, float* out, int samples); 54 // Gets the current measurements, measured by process() and consumed by getParameter() 55 Visualizer::Measurement getMeasure(); 56 // Gets the latest PCM capture, data captured by process() and consumed by getParameter() 57 std::vector<uint8_t> capture(); 58 59 struct BufferStats { 60 bool mIsValid; 61 uint16_t mPeakU16; // the positive peak of the absolute value of the samples in a buffer 62 float mRmsSquared; // the average square of the samples in a buffer 63 }; 64 65 enum State { 66 UNINITIALIZED, 67 INITIALIZED, 68 ACTIVE, 69 }; 70 71 private: 72 // maximum time since last capture buffer update before resetting capture buffer. This means 73 // that the framework has stopped playing audio and we must start returning silence 74 static const uint32_t kMaxStallTimeMs = 1000; 75 // discard measurements older than this number of ms 76 static const uint32_t kDiscardMeasurementsTimeMs = 2000; 77 // maximum number of buffers for which we keep track of the measurements 78 // note: buffer index is stored in uint8_t 79 static const uint32_t kMeasurementWindowMaxSizeInBuffers = 25; 80 81 Parameter::Common mCommon; 82 State mState = State::UNINITIALIZED; 83 uint32_t mCaptureIdx = 0; 84 uint32_t mLastCaptureIdx = 0; 85 Visualizer::ScalingMode mScalingMode = Visualizer::ScalingMode::NORMALIZED; 86 struct timespec mBufferUpdateTime; 87 // capture buf with 8 bits mono PCM samples 88 std::array<uint8_t, kMaxCaptureBufSize> mCaptureBuf; 89 uint32_t mDownstreamLatency = 0; 90 int32_t mCaptureSamples = kMaxCaptureBufSize; 91 92 // to avoid recomputing it every time a buffer is processed 93 uint8_t mChannelCount = 0; 94 Visualizer::MeasurementMode mMeasurementMode = 95 Visualizer::MeasurementMode::NONE; 96 uint8_t mMeasurementWindowSizeInBuffers = kMeasurementWindowMaxSizeInBuffers; 97 uint8_t mMeasurementBufferIdx = 0; 98 std::array<BufferStats, kMeasurementWindowMaxSizeInBuffers> mPastMeasurements; 99 void init_params(); 100 101 uint32_t getDeltaTimeMsFromUpdatedTime_l(); 102 }; 103 } // namespace aidl::android::hardware::audio::effect 104