| 1 | /* |
| 2 | Copyright 2018 Google Inc. All Rights Reserved. |
| 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 | #ifndef RESONANCE_AUDIO_DSP_GAIN_PROCESSOR_H_ |
| 18 | #define RESONANCE_AUDIO_DSP_GAIN_PROCESSOR_H_ |
| 19 | |
| 20 | #include <cmath> |
| 21 | #include <cstddef> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include "base/audio_buffer.h" |
| 25 | |
| 26 | namespace vraudio { |
| 27 | |
| 28 | // Processor class which applies a gain to a vector of samples from an audio |
| 29 | // buffer. A short linear ramp is applied to the gain to reduce audible |
| 30 | // artifacts in the output. |
| 31 | class GainProcessor { |
| 32 | public: |
| 33 | // Default constructor keeps the gain state uninitialized. The first call to |
| 34 | // |ApplyGain| sets the internal gain state. |
| 35 | GainProcessor(); |
| 36 | |
| 37 | // Constructs |GainProcessor| with some initial gain value. |
| 38 | // |
| 39 | // @param initial_gain Gain value used as starting point for first processing |
| 40 | // period's gain ramping. |
| 41 | explicit GainProcessor(float initial_gain); |
| 42 | |
| 43 | // Applies gain supplied to the input samples. |
| 44 | // |
| 45 | // @param target_gain Target gain value. |
| 46 | // @param input Samples to which gain will be applied. |
| 47 | // @param output Samples to which gain has been applied. |
| 48 | // @param accumulate_output True if the processed input should be mixed into |
| 49 | // the output. Otherwise, the output will be replaced by the processed |
| 50 | // input. |
| 51 | void ApplyGain(float target_gain, const AudioBuffer::Channel& input, |
| 52 | AudioBuffer::Channel* output, bool accumulate_output); |
| 53 | |
| 54 | // Returns the |current_gain_| value. |
| 55 | // |
| 56 | // @return Current gain applied by the |GainProcessor|. |
| 57 | float GetGain() const; |
| 58 | |
| 59 | // Resets the gain processor to a new gain factor. |
| 60 | // |
| 61 | // @param gain Gain value. |
| 62 | void Reset(float gain); |
| 63 | |
| 64 | private: |
| 65 | // Latest gain value to be applied to buffer values. |
| 66 | float current_gain_; |
| 67 | |
| 68 | // Flag to indiciate if an initial gain has been assigned. |
| 69 | bool is_initialized_; |
| 70 | }; |
| 71 | |
| 72 | } // namespace vraudio |
| 73 | |
| 74 | #endif // RESONANCE_AUDIO_DSP_GAIN_PROCESSOR_H_ |
| 75 | |