File: channel_mixer.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (90 lines) | stat: -rw-r--r-- 3,175 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 *  Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#ifndef AUDIO_UTILITY_CHANNEL_MIXER_H_
#define AUDIO_UTILITY_CHANNEL_MIXER_H_

#include <stddef.h>
#include <stdint.h>

#include <memory>
#include <vector>

#include "api/audio/audio_frame.h"
#include "api/audio/channel_layout.h"

namespace webrtc {

// ChannelMixer is for converting audio between channel layouts.  The conversion
// matrix is built upon construction and used during each Transform() call.  The
// algorithm works by generating a conversion matrix mapping each output channel
// to list of input channels.  The transform renders all of the output channels,
// with each output channel rendered according to a weighted sum of the relevant
// input channels as defined in the matrix.
// This file is derived from Chromium's media/base/channel_mixer.h.
class ChannelMixer {
 public:
  // To mix two channels into one and preserve loudness, we must apply
  // (1 / sqrt(2)) gain to each.
  static constexpr float kHalfPower = 0.707106781186547524401f;

  ChannelMixer(ChannelLayout input_layout,
               size_t input_channels,
               ChannelLayout output_layout,
               size_t output_channels);
  ChannelMixer(ChannelLayout input_layout, ChannelLayout output_layout);
  ~ChannelMixer();

  // Transforms all input channels corresponding to the selected `input_layout`
  // to the number of channels in the selected `output_layout`.
  // Example usage (downmix from stereo to mono):
  //
  //   ChannelMixer mixer(CHANNEL_LAYOUT_STEREO, CHANNEL_LAYOUT_MONO);
  //   AudioFrame frame;
  //   frame.samples_per_channel_ = 160;
  //   frame.num_channels_ = 2;
  //   EXPECT_EQ(2u, frame.channels());
  //   mixer.Transform(&frame);
  //   EXPECT_EQ(1u, frame.channels());
  //
  void Transform(AudioFrame* frame);

 private:
  bool IsUpMixing() const { return output_channels_ > input_channels_; }

  // Selected channel layouts.
  const ChannelLayout input_layout_;
  const ChannelLayout output_layout_;

  // Channel counts for input and output.
  const size_t input_channels_;
  const size_t output_channels_;

  // 2D matrix of output channels to input channels.
  std::vector<std::vector<float> > matrix_;

  // 1D array used as temporary storage during the transformation.
  std::unique_ptr<int16_t[]> audio_vector_;

  // Number of elements allocated for `audio_vector_`.
  size_t audio_vector_size_ = 0;

  // Optimization case for when we can simply remap the input channels to output
  // channels, i.e., when all scaling factors in `matrix_` equals 1.0.
  bool remapping_;

  // Delete the copy constructor and assignment operator.
  ChannelMixer(const ChannelMixer& other) = delete;
  ChannelMixer& operator=(const ChannelMixer& other) = delete;
};

}  // namespace webrtc

#endif  // AUDIO_UTILITY_CHANNEL_MIXER_H_