Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 1 | /* |
| 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 Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame^] | 24 | #include <utils/Errors.h> |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame^] | 28 | class SoundDoseManager : public audio_utils::MelProcessor::MelCallback { |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 29 | public: |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame^] | 30 | /** 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 Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 38 | |
| 39 | /** |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame^] | 40 | * \brief Creates or gets the MelProcessor assigned to the streamHandle |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 41 | * |
| 42 | * \param deviceId id for the devices where the stream is active. |
| 43 | * \param streanHandle handle to the stream |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame^] | 44 | * \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 Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 49 | */ |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame^] | 50 | sp<audio_utils::MelProcessor> getOrCreateProcessorForDevice( |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 51 | audio_port_handle_t deviceId, |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame^] | 52 | audio_io_handle_t streamHandle, |
| 53 | uint32_t sampleRate, |
| 54 | size_t channelCount, |
| 55 | audio_format_t format); |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 56 | |
| 57 | /** |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame^] | 58 | * \brief Removes stream processor when MEL computation is not needed anymore |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 59 | * |
| 60 | * \param streanHandle handle to the stream |
| 61 | */ |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame^] | 62 | 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 Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 71 | |
| 72 | std::string dump() const; |
| 73 | |
| 74 | // used for testing |
| 75 | size_t getCachedMelRecordsSize() const; |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame^] | 76 | |
| 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 Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 84 | private: |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame^] | 85 | mutable std::mutex mLock; |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 86 | |
| 87 | // no need for lock since MelAggregator is thread-safe |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame^] | 88 | const sp<audio_utils::MelAggregator> mMelAggregator; |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 89 | |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame^] | 90 | std::unordered_map<audio_io_handle_t, |
| 91 | wp<audio_utils::MelProcessor>> mActiveProcessors GUARDED_BY(mLock); |
| 92 | |
| 93 | float mRs2Value GUARDED_BY(mLock); |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | } // namespace android |