blob: ee00860846c58c9c8cd43b0fed11d2d78c78f54b [file] [log] [blame]
henrika2250b052019-07-04 11:27:52 +02001/*
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
18namespace webrtc {
19
20class 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).
Artem Titovb0ea6372021-07-26 11:47:07 +020032 // The size of `matrix` is `output_channels` x `input_channels`, i.e., the
henrika2250b052019-07-04 11:27:52 +020033 // 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:
Per Ã…hgren5b82ba32019-11-22 15:35:57 +010039 const bool use_voip_channel_mapping_adjustments_;
40
henrika2250b052019-07-04 11:27:52 +020041 // 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
Artem Titovb0ea6372021-07-26 11:47:07 +020058 // Helper methods for checking if `ch` exists in either `input_layout_` or
59 // `output_layout_` respectively.
henrika2250b052019-07-04 11:27:52 +020060 bool HasInputChannel(Channels ch) const;
61 bool HasOutputChannel(Channels ch) const;
62
Artem Titovb0ea6372021-07-26 11:47:07 +020063 // 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_`.
henrika2250b052019-07-04 11:27:52 +020066 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_