| 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 | #include "dsp/gain.h" |
| 18 | |
| 19 | #include "base/constants_and_types.h" |
| 20 | #include "base/simd_macros.h" |
| 21 | #include "base/simd_utils.h" |
| 22 | |
| 23 | namespace vraudio { |
| 24 | |
| 25 | float LinearGainRamp(size_t ramp_length, float start_gain, float end_gain, |
| 26 | const AudioBuffer::Channel& input_samples, |
| 27 | AudioBuffer::Channel* output_samples, |
| 28 | bool accumulate_output) { |
| 29 | DCHECK(output_samples); |
| 30 | DCHECK_EQ(input_samples.size(), output_samples->size()); |
| 31 | DCHECK_GT(ramp_length, 0U); |
| 32 | |
| 33 | const size_t process_length = std::min(a: ramp_length, b: input_samples.size()); |
| 34 | const float gain_increment_per_sample = |
| 35 | (end_gain - start_gain) / static_cast<float>(ramp_length); |
| 36 | |
| 37 | float current_gain = start_gain; |
| 38 | if (accumulate_output) { |
| 39 | for (size_t frame = 0; frame < process_length; ++frame) { |
| 40 | (*output_samples)[frame] += current_gain * input_samples[frame]; |
| 41 | current_gain += gain_increment_per_sample; |
| 42 | } |
| 43 | } else { |
| 44 | for (size_t frame = 0; frame < process_length; ++frame) { |
| 45 | (*output_samples)[frame] = current_gain * input_samples[frame]; |
| 46 | current_gain += gain_increment_per_sample; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | return current_gain; |
| 51 | } |
| 52 | |
| 53 | void ConstantGain(size_t offset_index, float gain, |
| 54 | const AudioBuffer::Channel& input_samples, |
| 55 | AudioBuffer::Channel* output_samples, |
| 56 | bool accumulate_output) { |
| 57 | DCHECK(output_samples); |
| 58 | const size_t input_size = input_samples.size(); |
| 59 | DCHECK_EQ(input_size, output_samples->size()); |
| 60 | DCHECK_LT(offset_index, input_size); |
| 61 | |
| 62 | // Apply gain to samples at the beginning, prior to SIMD_LENGTH alignment. |
| 63 | const size_t unaligned_samples = SIMD_LENGTH - (offset_index % SIMD_LENGTH); |
| 64 | const size_t offset_index_simd = |
| 65 | std::min(a: input_size, b: offset_index + unaligned_samples); |
| 66 | if (accumulate_output) { |
| 67 | for (size_t i = offset_index; i < offset_index_simd; ++i) { |
| 68 | (*output_samples)[i] += input_samples[i] * gain; |
| 69 | } |
| 70 | } else { |
| 71 | for (size_t i = offset_index; i < offset_index_simd; ++i) { |
| 72 | (*output_samples)[i] = input_samples[i] * gain; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | if (offset_index_simd == input_size) { |
| 77 | // Return if there are no remaining operations to carry out. |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | const size_t aligned_length = input_size - offset_index_simd; |
| 82 | const float* aligned_input = &(input_samples[offset_index_simd]); |
| 83 | float* aligned_output = &(*output_samples)[offset_index_simd]; |
| 84 | |
| 85 | // Apply gain via SIMD operations. |
| 86 | if (accumulate_output) { |
| 87 | ScalarMultiplyAndAccumulate(length: aligned_length, gain, input: aligned_input, |
| 88 | accumulator: aligned_output); |
| 89 | } else { |
| 90 | ScalarMultiply(length: aligned_length, gain, input: aligned_input, output: aligned_output); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | bool IsGainNearZero(float gain) { |
| 95 | return std::abs(x: gain) < kNegative60dbInAmplitude; |
| 96 | } |
| 97 | |
| 98 | bool IsGainNearUnity(float gain) { |
| 99 | return std::abs(x: 1.0f - gain) < kNegative60dbInAmplitude; |
| 100 | } |
| 101 | |
| 102 | } // namespace vraudio |
| 103 | |