1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef VOLUME_RAMP_H 16 #define VOLUME_RAMP_H 17 18 #include <cstdint> 19 #include <string> 20 #include <map> 21 #include <mutex> 22 #include <cmath> 23 #include "audio_info.h" 24 25 namespace OHOS { 26 namespace AudioStandard { 27 28 enum RampDirection { 29 RAMP_UP = 0, 30 RAMP_DOWN = 1, 31 }; 32 33 class VolumeRamp { 34 public: 35 VolumeRamp(); 36 ~VolumeRamp() = default; 37 void SetVolumeRampConfig(float targetVolume, float currStreamVolume, int32_t duration); 38 float GetRampVolume(); 39 bool IsActive(); 40 void Terminate(); 41 42 private: 43 void SetVolumeCurve(std::vector<float> &volumes); 44 float GetScaledTime(int64_t currentTime); 45 float FindRampVolume(float time); 46 47 int32_t duration_ = 0; 48 RampDirection rampDirection_ = RAMP_UP; 49 std::mutex curveMapMutex_; 50 std::map<float, float> curvePoints_; 51 int64_t initTime_ = 0; 52 float scale_ = 0.0f; 53 bool isVolumeRampActive_ = false; 54 float rampVolume_ = 0.0f; 55 }; 56 } // namespace AudioStandard 57 } // namespace OHOS 58 #endif // VOLUME_RAMP_H 59