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