blob: 3cb711e0e0a9bbfdfec1259a7a6795ba2c98563e [file] [log] [blame]
Shunkai Yao05b190a2022-12-22 00:21:31 +00001/*
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
24namespace aidl::android::hardware::audio::effect {
25
26class 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 Yao6b857c92023-02-13 17:44:52 +000048 int getDownstreamLatency();
Shunkai Yao05b190a2022-12-22 00:21:31 +000049
50 IEffect::Status process(float* in, float* out, int samples);
51 // Gets the current measurements, measured by process() and consumed by getParameter()
Shunkai Yao6b857c92023-02-13 17:44:52 +000052 Visualizer::Measurement getMeasure();
Shunkai Yao05b190a2022-12-22 00:21:31 +000053 // 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 Yaoc045a032022-12-27 21:33:17 +000082 uint32_t mCaptureIdx GUARDED_BY(mMutex) = 0;
83 uint32_t mLastCaptureIdx GUARDED_BY(mMutex) = 0;
Shunkai Yao05b190a2022-12-22 00:21:31 +000084 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 Yaoc045a032022-12-27 21:33:17 +000088 uint32_t mDownstreamLatency GUARDED_BY(mMutex) = 0;
89 uint32_t mCaptureSamples GUARDED_BY(mMutex) = kMaxCaptureBufSize;
Shunkai Yao05b190a2022-12-22 00:21:31 +000090
91 // to avoid recomputing it every time a buffer is processed
Shunkai Yaoc045a032022-12-27 21:33:17 +000092 uint8_t mChannelCount GUARDED_BY(mMutex) = 0;
Shunkai Yao05b190a2022-12-22 00:21:31 +000093 Visualizer::MeasurementMode mMeasurementMode GUARDED_BY(mMutex) =
94 Visualizer::MeasurementMode::NONE;
95 uint8_t mMeasurementWindowSizeInBuffers = kMeasurementWindowMaxSizeInBuffers;
Shunkai Yaoc045a032022-12-27 21:33:17 +000096 uint8_t mMeasurementBufferIdx GUARDED_BY(mMutex) = 0;
Shunkai Yao05b190a2022-12-22 00:21:31 +000097 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