| 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 "utils/buffer_crossfader.h" |
| 18 | |
| 19 | #include "base/constants_and_types.h" |
| 20 | #include "base/simd_utils.h" |
| 21 | |
| 22 | namespace vraudio { |
| 23 | |
| 24 | BufferCrossfader::BufferCrossfader(size_t num_frames) |
| 25 | : crossfade_buffer_(kNumStereoChannels, num_frames) { |
| 26 | DCHECK_NE(num_frames, 0); |
| 27 | // Initialize the |crossfade_buffer_|. |
| 28 | auto* fade_in_channel = &crossfade_buffer_[0]; |
| 29 | auto* fade_out_channel = &crossfade_buffer_[1]; |
| 30 | for (size_t frame = 0; frame < num_frames; ++frame) { |
| 31 | const float crossfade_factor = |
| 32 | static_cast<float>(frame) / static_cast<float>(num_frames); |
| 33 | (*fade_in_channel)[frame] = crossfade_factor; |
| 34 | (*fade_out_channel)[frame] = 1.0f - crossfade_factor; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | void BufferCrossfader::ApplyLinearCrossfade(const AudioBuffer& input_fade_in, |
| 39 | const AudioBuffer& input_fade_out, |
| 40 | AudioBuffer* output) const { |
| 41 | DCHECK(output); |
| 42 | DCHECK_NE(output, &input_fade_in); |
| 43 | DCHECK_NE(output, &input_fade_out); |
| 44 | |
| 45 | const size_t num_channels = input_fade_in.num_channels(); |
| 46 | const size_t num_frames = input_fade_in.num_frames(); |
| 47 | DCHECK_EQ(num_channels, input_fade_out.num_channels()); |
| 48 | DCHECK_EQ(num_channels, output->num_channels()); |
| 49 | DCHECK_EQ(num_frames, input_fade_out.num_frames()); |
| 50 | DCHECK_EQ(num_frames, output->num_frames()); |
| 51 | DCHECK_EQ(num_frames, crossfade_buffer_.num_frames()); |
| 52 | |
| 53 | const auto* gain_fade_in_channel = crossfade_buffer_[0].begin(); |
| 54 | const auto* gain_fade_out_channel = crossfade_buffer_[1].begin(); |
| 55 | for (size_t channel = 0; channel < num_channels; ++channel) { |
| 56 | const auto* input_fade_in_channel = input_fade_in[channel].begin(); |
| 57 | const auto* input_fade_out_channel = input_fade_out[channel].begin(); |
| 58 | auto* output_channel = ((*output)[channel]).begin(); |
| 59 | MultiplyPointwise(length: num_frames, input_a: gain_fade_in_channel, input_b: input_fade_in_channel, |
| 60 | output: output_channel); |
| 61 | MultiplyAndAccumulatePointwise(length: num_frames, input_a: gain_fade_out_channel, |
| 62 | input_b: input_fade_out_channel, accumulator: output_channel); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | } // namespace vraudio |
| 67 | |