channel_mixing_matrix.h (2872B)
1 /* 2 * Copyright (c) 2019 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #ifndef AUDIO_UTILITY_CHANNEL_MIXING_MATRIX_H_ 12 #define AUDIO_UTILITY_CHANNEL_MIXING_MATRIX_H_ 13 14 #include <vector> 15 16 #include "api/audio/channel_layout.h" 17 18 namespace webrtc { 19 20 class ChannelMixingMatrix { 21 public: 22 ChannelMixingMatrix(ChannelLayout input_layout, 23 int input_channels, 24 ChannelLayout output_layout, 25 int output_channels); 26 27 ~ChannelMixingMatrix(); 28 29 // Create the transformation matrix of input channels to output channels. 30 // Updates the empty matrix with the transformation, and returns true 31 // if the transformation is just a remapping of channels (no mixing). 32 // The size of `matrix` is `output_channels` x `input_channels`, i.e., the 33 // number of rows equals the number of output channels and the number of 34 // columns corresponds to the number of input channels. 35 // This file is derived from Chromium's media/base/channel_mixing_matrix.h. 36 bool CreateTransformationMatrix(std::vector<std::vector<float>>* matrix); 37 38 private: 39 // Result transformation of input channels to output channels 40 std::vector<std::vector<float>>* matrix_; 41 42 // Input and output channel layout provided during construction. 43 const ChannelLayout input_layout_; 44 const int input_channels_; 45 const ChannelLayout output_layout_; 46 const int output_channels_; 47 48 // Helper variable for tracking which inputs are currently unaccounted, 49 // should be empty after construction completes. 50 std::vector<Channels> unaccounted_inputs_; 51 52 // Helper methods for managing unaccounted input channels. 53 void AccountFor(Channels ch); 54 bool IsUnaccounted(Channels ch) const; 55 56 // Helper methods for checking if `ch` exists in either `input_layout_` or 57 // `output_layout_` respectively. 58 bool HasInputChannel(Channels ch) const; 59 bool HasOutputChannel(Channels ch) const; 60 61 // Helper methods for updating `matrix_` with the proper value for 62 // mixing `input_ch` into `output_ch`. MixWithoutAccounting() does not 63 // remove the channel from `unaccounted_inputs_`. 64 void Mix(Channels input_ch, Channels output_ch, float scale); 65 void MixWithoutAccounting(Channels input_ch, Channels output_ch, float scale); 66 67 // Delete the copy constructor and assignment operator. 68 ChannelMixingMatrix(const ChannelMixingMatrix& other) = delete; 69 ChannelMixingMatrix& operator=(const ChannelMixingMatrix& other) = delete; 70 }; 71 72 } // namespace webrtc 73 74 #endif // AUDIO_UTILITY_CHANNEL_MIXING_MATRIX_H_