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