tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

channel_mixer.h (3172B)


      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_MIXER_H_
     12 #define AUDIO_UTILITY_CHANNEL_MIXER_H_
     13 
     14 #include <cstddef>
     15 #include <cstdint>
     16 #include <memory>
     17 #include <vector>
     18 
     19 #include "api/audio/audio_frame.h"
     20 #include "api/audio/channel_layout.h"
     21 
     22 namespace webrtc {
     23 
     24 // ChannelMixer is for converting audio between channel layouts.  The conversion
     25 // matrix is built upon construction and used during each Transform() call.  The
     26 // algorithm works by generating a conversion matrix mapping each output channel
     27 // to list of input channels.  The transform renders all of the output channels,
     28 // with each output channel rendered according to a weighted sum of the relevant
     29 // input channels as defined in the matrix.
     30 // This file is derived from Chromium's media/base/channel_mixer.h.
     31 class ChannelMixer {
     32 public:
     33  // To mix two channels into one and preserve loudness, we must apply
     34  // (1 / sqrt(2)) gain to each.
     35  static constexpr float kHalfPower = 0.707106781186547524401f;
     36 
     37  ChannelMixer(ChannelLayout input_layout,
     38               size_t input_channels,
     39               ChannelLayout output_layout,
     40               size_t output_channels);
     41  ChannelMixer(ChannelLayout input_layout, ChannelLayout output_layout);
     42  ~ChannelMixer();
     43 
     44  // Transforms all input channels corresponding to the selected `input_layout`
     45  // to the number of channels in the selected `output_layout`.
     46  // Example usage (downmix from stereo to mono):
     47  //
     48  //   ChannelMixer mixer(CHANNEL_LAYOUT_STEREO, CHANNEL_LAYOUT_MONO);
     49  //   AudioFrame frame;
     50  //   frame.samples_per_channel_ = 160;
     51  //   frame.num_channels_ = 2;
     52  //   EXPECT_EQ(2u, frame.channels());
     53  //   mixer.Transform(&frame);
     54  //   EXPECT_EQ(1u, frame.channels());
     55  //
     56  void Transform(AudioFrame* frame);
     57 
     58 private:
     59  bool IsUpMixing() const { return output_channels_ > input_channels_; }
     60 
     61  // Selected channel layouts.
     62  const ChannelLayout input_layout_;
     63  const ChannelLayout output_layout_;
     64 
     65  // Channel counts for input and output.
     66  const size_t input_channels_;
     67  const size_t output_channels_;
     68 
     69  // 2D matrix of output channels to input channels.
     70  std::vector<std::vector<float> > matrix_;
     71 
     72  // 1D array used as temporary storage during the transformation.
     73  std::unique_ptr<int16_t[]> audio_vector_;
     74 
     75  // Number of elements allocated for `audio_vector_`.
     76  size_t audio_vector_size_ = 0;
     77 
     78  // Optimization case for when we can simply remap the input channels to output
     79  // channels, i.e., when all scaling factors in `matrix_` equals 1.0.
     80  bool remapping_;
     81 
     82  // Delete the copy constructor and assignment operator.
     83  ChannelMixer(const ChannelMixer& other) = delete;
     84  ChannelMixer& operator=(const ChannelMixer& other) = delete;
     85 };
     86 
     87 }  // namespace webrtc
     88 
     89 #endif  // AUDIO_UTILITY_CHANNEL_MIXER_H_