channel_mixer.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef MEDIA_BASE_CHANNEL_MIXER_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define MEDIA_BASE_CHANNEL_MIXER_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "media/base/channel_layout.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "media/base/media_export.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace media {
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class AudioBus;
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ChannelMixer is for converting audio between channel layouts.  The conversion
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// matrix is built upon construction and used during each Transform() call.  The
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// algorithm works by generating a conversion matrix mapping each output channel
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// to list of input channels.  The transform renders all of the output channels,
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// with each output channel rendered according to a weighted sum of the relevant
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// input channels as defined in the matrix.
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MEDIA_EXPORT ChannelMixer {
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ChannelMixer(ChannelLayout input, ChannelLayout output);
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~ChannelMixer();
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Transforms all channels from |input| into |output| channels.
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Transform(const AudioBus* input, AudioBus* output);
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Constructor helper methods for managing unaccounted input channels.
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AccountFor(Channels ch);
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsUnaccounted(Channels ch);
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Helper methods for checking if |ch| exists in either |input_layout_| or
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |output_layout_| respectively.
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool HasInputChannel(Channels ch);
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool HasOutputChannel(Channels ch);
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Constructor helper methods for updating |matrix_| with the proper value for
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // mixing |input_ch| into |output_ch|.  MixWithoutAccounting() does not remove
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the channel from |unaccounted_inputs_|.
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Mix(Channels input_ch, Channels output_ch, float scale);
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void MixWithoutAccounting(Channels input_ch, Channels output_ch, float scale);
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Input and output channel layout provided during construction.
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ChannelLayout input_layout_;
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ChannelLayout output_layout_;
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Helper variable for tracking which inputs are currently unaccounted, should
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // be empty after construction completes.
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<Channels> unaccounted_inputs_;
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // 2D matrix of output channels to input channels.
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector< std::vector<float> > matrix_;
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Optimization case for when we can simply remap the input channels to output
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // channels and don't need to do a multiply-accumulate loop over |matrix_|.
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool remapping_;
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(ChannelMixer);
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace media
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // MEDIA_BASE_CHANNEL_MIXER_H_
69