blob: 5934d2ea45dea463cfc6c12b9eb494b29f4da903 [file] [log] [blame]
Vlad Popa2900c0a2022-10-24 13:38:00 +02001/*
2**
3** Copyright 2022, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#pragma once
19
20#include <audio_utils/MelProcessor.h>
21#include <audio_utils/MelAggregator.h>
22#include <mutex>
23#include <unordered_map>
Vlad Popaf09e93f2022-10-31 16:27:12 +010024#include <utils/Errors.h>
Vlad Popa2900c0a2022-10-24 13:38:00 +020025
26namespace android {
27
Vlad Popaf09e93f2022-10-31 16:27:12 +010028class SoundDoseManager : public audio_utils::MelProcessor::MelCallback {
Vlad Popa2900c0a2022-10-24 13:38:00 +020029public:
Vlad Popaf09e93f2022-10-31 16:27:12 +010030 /** CSD is computed with a rolling window of 7 days. */
31 static constexpr int64_t kCsdWindowSeconds = 604800; // 60s * 60m * 24h * 7d
32 /** Default RS2 value in dBA as defined in IEC 62368-1 3rd edition. */
33 static constexpr float kDefaultRs2Value = 100.f;
34
35 SoundDoseManager()
36 : mMelAggregator(sp<audio_utils::MelAggregator>::make(kCsdWindowSeconds)),
37 mRs2Value(kDefaultRs2Value) {};
Vlad Popa2900c0a2022-10-24 13:38:00 +020038
39 /**
Vlad Popaf09e93f2022-10-31 16:27:12 +010040 * \brief Creates or gets the MelProcessor assigned to the streamHandle
Vlad Popa2900c0a2022-10-24 13:38:00 +020041 *
42 * \param deviceId id for the devices where the stream is active.
43 * \param streanHandle handle to the stream
Vlad Popaf09e93f2022-10-31 16:27:12 +010044 * \param sampleRate sample rate for the processor
45 * \param channelCount number of channels to be processed.
46 * \param format format of the input samples.
47 *
48 * \return MelProcessor assigned to the stream and device id.
Vlad Popa2900c0a2022-10-24 13:38:00 +020049 */
Vlad Popaf09e93f2022-10-31 16:27:12 +010050 sp<audio_utils::MelProcessor> getOrCreateProcessorForDevice(
Vlad Popa2900c0a2022-10-24 13:38:00 +020051 audio_port_handle_t deviceId,
Vlad Popaf09e93f2022-10-31 16:27:12 +010052 audio_io_handle_t streamHandle,
53 uint32_t sampleRate,
54 size_t channelCount,
55 audio_format_t format);
Vlad Popa2900c0a2022-10-24 13:38:00 +020056
57 /**
Vlad Popaf09e93f2022-10-31 16:27:12 +010058 * \brief Removes stream processor when MEL computation is not needed anymore
Vlad Popa2900c0a2022-10-24 13:38:00 +020059 *
60 * \param streanHandle handle to the stream
61 */
Vlad Popaf09e93f2022-10-31 16:27:12 +010062 void removeStreamProcessor(audio_io_handle_t streamHandle);
63
64 /**
65 * Sets the output RS2 value for momentary exposure warnings. Must not be
66 * higher than 100dBA and not lower than 80dBA.
67 *
68 * \param rs2Value value to use for momentary exposure
69 */
70 void setOutputRs2(float rs2Value);
Vlad Popa2900c0a2022-10-24 13:38:00 +020071
72 std::string dump() const;
73
74 // used for testing
75 size_t getCachedMelRecordsSize() const;
Vlad Popaf09e93f2022-10-31 16:27:12 +010076
77 // ------ Override audio_utils::MelProcessor::MelCallback ------
78 void onNewMelValues(const std::vector<float>& mels,
79 size_t offset,
80 size_t length,
81 audio_port_handle_t deviceId) const override;
82
83 void onMomentaryExposure(float currentMel, audio_port_handle_t deviceId) const override;
Vlad Popa2900c0a2022-10-24 13:38:00 +020084private:
Vlad Popaf09e93f2022-10-31 16:27:12 +010085 mutable std::mutex mLock;
Vlad Popa2900c0a2022-10-24 13:38:00 +020086
87 // no need for lock since MelAggregator is thread-safe
Vlad Popaf09e93f2022-10-31 16:27:12 +010088 const sp<audio_utils::MelAggregator> mMelAggregator;
Vlad Popa2900c0a2022-10-24 13:38:00 +020089
Vlad Popaf09e93f2022-10-31 16:27:12 +010090 std::unordered_map<audio_io_handle_t,
91 wp<audio_utils::MelProcessor>> mActiveProcessors GUARDED_BY(mLock);
92
93 float mRs2Value GUARDED_BY(mLock);
Vlad Popa2900c0a2022-10-24 13:38:00 +020094};
95
96} // namespace android