| 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 "graph/gain_mixer_node.h" | 
| 18 |  | 
| 19 | #include <vector> | 
| 20 |  | 
| 21 | #include "base/constants_and_types.h" | 
| 22 |  | 
| 23 |  | 
| 24 | namespace vraudio { | 
| 25 |  | 
| 26 | GainMixerNode::GainMixerNode(const AttenuationType& attenuation_type, | 
| 27 |                              const SystemSettings& system_settings, | 
| 28 |                              size_t num_channels) | 
| 29 |     : mute_enabled_(false), | 
| 30 |       attenuation_type_(attenuation_type), | 
| 31 |       gain_mixer_(num_channels, system_settings.GetFramesPerBuffer()), | 
| 32 |       system_settings_(system_settings) {} | 
| 33 |  | 
| 34 | void GainMixerNode::SetMute(bool mute_enabled) { mute_enabled_ = mute_enabled; } | 
| 35 |  | 
| 36 | bool GainMixerNode::CleanUp() { | 
| 37 |   CallCleanUpOnInputNodes(); | 
| 38 |   // Prevent node from being disconnected when all sources are removed. | 
| 39 |   return false; | 
| 40 | } | 
| 41 |  | 
| 42 | const AudioBuffer* GainMixerNode::AudioProcess(const NodeInput& input) { | 
| 43 |  | 
| 44 |  | 
| 45 |   if (mute_enabled_) { | 
| 46 |     // Skip processing and output nullptr audio buffer. | 
| 47 |     return nullptr; | 
| 48 |   } | 
| 49 |  | 
| 50 |   // Apply the gain to each input buffer channel. | 
| 51 |   gain_mixer_.Reset(); | 
| 52 |   for (auto input_buffer : input.GetInputBuffers()) { | 
| 53 |     const auto source_parameters = | 
| 54 |         system_settings_.GetSourceParameters(source_id: input_buffer->source_id()); | 
| 55 |     if (source_parameters != nullptr) { | 
| 56 |       const float target_gain = | 
| 57 |           source_parameters->attenuations[attenuation_type_]; | 
| 58 |       const size_t num_channels = input_buffer->num_channels(); | 
| 59 |       gain_mixer_.AddInput(input: *input_buffer, | 
| 60 |                            gains: std::vector<float>(num_channels, target_gain)); | 
| 61 |     } | 
| 62 |   } | 
| 63 |   return gain_mixer_.GetOutput(); | 
| 64 | } | 
| 65 |  | 
| 66 | }  // namespace vraudio | 
| 67 |  |