xref: /aosp_15_r20/external/webrtc/audio/utility/channel_mixing_matrix.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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   const bool use_voip_channel_mapping_adjustments_;
40 
41   // Result transformation of input channels to output channels
42   std::vector<std::vector<float>>* matrix_;
43 
44   // Input and output channel layout provided during construction.
45   ChannelLayout input_layout_;
46   int input_channels_;
47   ChannelLayout output_layout_;
48   int output_channels_;
49 
50   // Helper variable for tracking which inputs are currently unaccounted,
51   // should be empty after construction completes.
52   std::vector<Channels> unaccounted_inputs_;
53 
54   // Helper methods for managing unaccounted input channels.
55   void AccountFor(Channels ch);
56   bool IsUnaccounted(Channels ch) const;
57 
58   // Helper methods for checking if `ch` exists in either `input_layout_` or
59   // `output_layout_` respectively.
60   bool HasInputChannel(Channels ch) const;
61   bool HasOutputChannel(Channels ch) const;
62 
63   // Helper methods for updating `matrix_` with the proper value for
64   // mixing `input_ch` into `output_ch`.  MixWithoutAccounting() does not
65   // remove the channel from `unaccounted_inputs_`.
66   void Mix(Channels input_ch, Channels output_ch, float scale);
67   void MixWithoutAccounting(Channels input_ch, Channels output_ch, float scale);
68 
69   // Delete the copy constructor and assignment operator.
70   ChannelMixingMatrix(const ChannelMixingMatrix& other) = delete;
71   ChannelMixingMatrix& operator=(const ChannelMixingMatrix& other) = delete;
72 };
73 
74 }  // namespace webrtc
75 
76 #endif  // AUDIO_UTILITY_CHANNEL_MIXING_MATRIX_H_
77