1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef MEDIA_BASE_CHANNEL_MIXER_H_
6 #define MEDIA_BASE_CHANNEL_MIXER_H_
10 #include "base/basictypes.h"
11 #include "media/base/channel_layout.h"
12 #include "media/base/media_export.h"
18 // ChannelMixer is for converting audio between channel layouts. The conversion
19 // matrix is built upon construction and used during each Transform() call. The
20 // algorithm works by generating a conversion matrix mapping each output channel
21 // to list of input channels. The transform renders all of the output channels,
22 // with each output channel rendered according to a weighted sum of the relevant
23 // input channels as defined in the matrix.
24 class MEDIA_EXPORT ChannelMixer
{
26 ChannelMixer(ChannelLayout input
, ChannelLayout output
);
29 // Transforms all channels from |input| into |output| channels.
30 void Transform(const AudioBus
* input
, AudioBus
* output
);
33 // Constructor helper methods for managing unaccounted input channels.
34 void AccountFor(Channels ch
);
35 bool IsUnaccounted(Channels ch
);
37 // Helper methods for checking if |ch| exists in either |input_layout_| or
38 // |output_layout_| respectively.
39 bool HasInputChannel(Channels ch
);
40 bool HasOutputChannel(Channels ch
);
42 // Constructor helper methods for updating |matrix_| with the proper value for
43 // mixing |input_ch| into |output_ch|. MixWithoutAccounting() does not remove
44 // the channel from |unaccounted_inputs_|.
45 void Mix(Channels input_ch
, Channels output_ch
, float scale
);
46 void MixWithoutAccounting(Channels input_ch
, Channels output_ch
, float scale
);
48 // Input and output channel layout provided during construction.
49 ChannelLayout input_layout_
;
50 ChannelLayout output_layout_
;
52 // Helper variable for tracking which inputs are currently unaccounted, should
53 // be empty after construction completes.
54 std::vector
<Channels
> unaccounted_inputs_
;
56 // 2D matrix of output channels to input channels.
57 std::vector
< std::vector
<float> > matrix_
;
59 // Optimization case for when we can simply remap the input channels to output
60 // channels and don't need to do a multiply-accumulate loop over |matrix_|.
63 DISALLOW_COPY_AND_ASSIGN(ChannelMixer
);
68 #endif // MEDIA_BASE_CHANNEL_MIXER_H_