| 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_MIXER_H_ |
| 18 | #define RESONANCE_AUDIO_DSP_GAIN_MIXER_H_ |
| 19 | |
| 20 | #include <memory> |
| 21 | #include <unordered_map> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include "base/audio_buffer.h" |
| 25 | #include "base/constants_and_types.h" |
| 26 | #include "dsp/gain_processor.h" |
| 27 | |
| 28 | namespace vraudio { |
| 29 | |
| 30 | class GainMixer { |
| 31 | public: |
| 32 | GainMixer(size_t num_channels, size_t frames_per_buffer); |
| 33 | |
| 34 | // Adds a separately scaled version of each channel of the input buffer to the |
| 35 | // output buffer's corresponding channels. |
| 36 | // |
| 37 | // @param input Input buffer to be added. |
| 38 | // @param gains Gains to be applied to the buffers channels. Must be equal in |
| 39 | // length to the number of channels in |input|. |
| 40 | void AddInput(const AudioBuffer& input, const std::vector<float>& gains); |
| 41 | |
| 42 | // Adds a single input channel to each of the output buffer's channels, with |
| 43 | // a separate gain applied to the input channel per output channel. |
| 44 | // |
| 45 | // @param input Input channel to be added. |
| 46 | // @param source_id Identifier corresponding to the input. |
| 47 | // @param gains Gains to be applied to the buffers channels. Must be equal in |
| 48 | // length to the number of channels in the output buffer. |
| 49 | void AddInputChannel(const AudioBuffer::Channel& input, SourceId source_id, |
| 50 | const std::vector<float>& gains); |
| 51 | |
| 52 | // Returns a pointer to the accumulator. |
| 53 | // |
| 54 | // @return Pointer to the processed (mixed) output buffer, or nullptr if no |
| 55 | // input has been added to the accumulator. |
| 56 | const AudioBuffer* GetOutput() const; |
| 57 | |
| 58 | // Resets the state of the accumulator. |
| 59 | void Reset(); |
| 60 | |
| 61 | private: |
| 62 | // Comprises one |GainProcessor| per channel of a source and a boolean to |
| 63 | // denote whether that source is active. |
| 64 | struct GainProcessors { |
| 65 | explicit GainProcessors(size_t num_channels); |
| 66 | |
| 67 | // Bool to signify if a given source is still passing data to a processor. |
| 68 | bool processors_active; |
| 69 | |
| 70 | // Scale and accumulation processors, one per channel for each source. |
| 71 | std::vector<GainProcessor> processors; |
| 72 | }; |
| 73 | |
| 74 | // Returns the |GainProcessor|s associated with a |source_id| (or creates |
| 75 | // one if needed) and sets the corresponding |processors_active| flag to true. |
| 76 | // |
| 77 | // @param source_id Identifier for a given input. |
| 78 | // @return The corresponding |ScalingAccumulators|. |
| 79 | std::vector<GainProcessor>* GetOrCreateProcessors(SourceId source_id); |
| 80 | |
| 81 | // Number of channels. |
| 82 | const size_t num_channels_; |
| 83 | |
| 84 | // Output buffer (accumulator). |
| 85 | AudioBuffer output_; |
| 86 | |
| 87 | // Denotes whether the accumulator has processed any inputs or not. |
| 88 | bool is_empty_; |
| 89 | |
| 90 | // Scale and accumulation processors, one per channel for each source. |
| 91 | std::unordered_map<SourceId, GainProcessors> source_gain_processors_; |
| 92 | }; |
| 93 | |
| 94 | } // namespace vraudio |
| 95 | |
| 96 | #endif // RESONANCE_AUDIO_DSP_GAIN_MIXER_H_ |
| 97 | |