blob: bfda0b9fecfd52ac8e0f9b26f6379b9170c605c9 [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);
48
49 IEffect::Status process(float* in, float* out, int samples);
50 // Gets the current measurements, measured by process() and consumed by getParameter()
51 Visualizer::GetOnlyParameters::Measurement getMeasure();
52 // Gets the latest PCM capture, data captured by process() and consumed by getParameter()
53 std::vector<uint8_t> capture();
54
55 struct BufferStats {
56 bool mIsValid;
57 uint16_t mPeakU16; // the positive peak of the absolute value of the samples in a buffer
58 float mRmsSquared; // the average square of the samples in a buffer
59 };
60
61 enum State {
62 UNINITIALIZED,
63 INITIALIZED,
64 ACTIVE,
65 };
66
67 private:
68 // maximum time since last capture buffer update before resetting capture buffer. This means
69 // that the framework has stopped playing audio and we must start returning silence
70 static const uint32_t kMaxStallTimeMs = 1000;
71 // discard measurements older than this number of ms
72 static const uint32_t kDiscardMeasurementsTimeMs = 2000;
73 // maximum number of buffers for which we keep track of the measurements
74 // note: buffer index is stored in uint8_t
75 static const uint32_t kMeasurementWindowMaxSizeInBuffers = 25;
76
77 // serialize process() and parameter setting
78 std::mutex mMutex;
79 Parameter::Common mCommon GUARDED_BY(mMutex);
80 State mState GUARDED_BY(mMutex) = State::UNINITIALIZED;
Shunkai Yaoc045a032022-12-27 21:33:17 +000081 uint32_t mCaptureIdx GUARDED_BY(mMutex) = 0;
82 uint32_t mLastCaptureIdx GUARDED_BY(mMutex) = 0;
Shunkai Yao05b190a2022-12-22 00:21:31 +000083 Visualizer::ScalingMode mScalingMode GUARDED_BY(mMutex) = Visualizer::ScalingMode::NORMALIZED;
84 struct timespec mBufferUpdateTime GUARDED_BY(mMutex);
85 // capture buf with 8 bits PCM
86 std::array<uint8_t, kMaxCaptureBufSize> mCaptureBuf GUARDED_BY(mMutex);
Shunkai Yaoc045a032022-12-27 21:33:17 +000087 uint32_t mDownstreamLatency GUARDED_BY(mMutex) = 0;
88 uint32_t mCaptureSamples GUARDED_BY(mMutex) = kMaxCaptureBufSize;
Shunkai Yao05b190a2022-12-22 00:21:31 +000089
90 // to avoid recomputing it every time a buffer is processed
Shunkai Yaoc045a032022-12-27 21:33:17 +000091 uint8_t mChannelCount GUARDED_BY(mMutex) = 0;
Shunkai Yao05b190a2022-12-22 00:21:31 +000092 Visualizer::MeasurementMode mMeasurementMode GUARDED_BY(mMutex) =
93 Visualizer::MeasurementMode::NONE;
94 uint8_t mMeasurementWindowSizeInBuffers = kMeasurementWindowMaxSizeInBuffers;
Shunkai Yaoc045a032022-12-27 21:33:17 +000095 uint8_t mMeasurementBufferIdx GUARDED_BY(mMutex) = 0;
Shunkai Yao05b190a2022-12-22 00:21:31 +000096 std::array<BufferStats, kMeasurementWindowMaxSizeInBuffers> mPastMeasurements;
97 void init_params();
98
99 uint32_t getDeltaTimeMsFromUpdatedTime_l() REQUIRES(mMutex);
100};
101} // namespace aidl::android::hardware::audio::effect