| 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/mixer.h" |
| 18 | |
| 19 | #include "base/logging.h" |
| 20 | |
| 21 | namespace vraudio { |
| 22 | |
| 23 | Mixer::Mixer(size_t target_num_channels, size_t frames_per_buffer) |
| 24 | : output_(target_num_channels, frames_per_buffer), is_empty_(false) { |
| 25 | Reset(); |
| 26 | } |
| 27 | |
| 28 | void Mixer::AddInput(const AudioBuffer& input) { |
| 29 | DCHECK_EQ(input.num_frames(), output_.num_frames()); |
| 30 | |
| 31 | // Accumulate the input buffers into the output buffer. |
| 32 | const size_t num_channels = |
| 33 | std::min(a: input.num_channels(), b: output_.num_channels()); |
| 34 | for (size_t n = 0; n < num_channels; ++n) { |
| 35 | if (input[n].IsEnabled()) { |
| 36 | output_[n] += input[n]; |
| 37 | } |
| 38 | } |
| 39 | is_empty_ = false; |
| 40 | } |
| 41 | |
| 42 | const AudioBuffer* Mixer::GetOutput() const { |
| 43 | if (is_empty_) { |
| 44 | return nullptr; |
| 45 | } |
| 46 | return &output_; |
| 47 | } |
| 48 | |
| 49 | void Mixer::Reset() { |
| 50 | if (!is_empty_) { |
| 51 | output_.Clear(); |
| 52 | } |
| 53 | is_empty_ = true; |
| 54 | } |
| 55 | |
| 56 | } // namespace vraudio |
| 57 |
