niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_AUDIO_PROCESSING_AUDIO_BUFFER_H_ |
| 12 | #define MODULES_AUDIO_PROCESSING_AUDIO_BUFFER_H_ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 16 | |
kwiberg | 88788ad | 2016-02-19 07:04:49 -0800 | [diff] [blame] | 17 | #include <memory> |
kwiberg | 4a206a9 | 2016-03-31 10:24:26 -0700 | [diff] [blame] | 18 | #include <vector> |
kwiberg | 88788ad | 2016-02-19 07:04:49 -0800 | [diff] [blame] | 19 | |
Fredrik Solenberg | bbf21a3 | 2018-04-12 22:44:09 +0200 | [diff] [blame] | 20 | #include "api/audio/audio_frame.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "common_audio/channel_buffer.h" |
| 22 | #include "modules/audio_processing/include/audio_processing.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 23 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 24 | namespace webrtc { |
| 25 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 26 | class PushSincResampler; |
| 27 | class SplittingFilter; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 28 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 29 | enum Band { kBand0To8kHz = 0, kBand8To16kHz = 1, kBand16To24kHz = 2 }; |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 30 | |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 31 | // Stores any audio data in a way that allows the audio processing module to |
| 32 | // operate on it in a controlled manner. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 33 | class AudioBuffer { |
| 34 | public: |
Per Åhgren | 0aefbf0 | 2019-08-23 21:29:17 +0200 | [diff] [blame] | 35 | static const int kSplitBandSize = 160; |
Per Åhgren | 3e8bf28 | 2019-08-29 23:38:40 +0200 | [diff] [blame] | 36 | static const size_t kMaxSampleRate = 384000; |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 37 | AudioBuffer(size_t input_rate, |
| 38 | size_t input_num_channels, |
| 39 | size_t buffer_rate, |
| 40 | size_t buffer_num_channels, |
| 41 | size_t output_rate, |
| 42 | size_t output_num_channels); |
| 43 | |
| 44 | // The constructor below will be deprecated. |
Steve Anton | f254e9e | 2019-08-21 17:52:28 +0000 | [diff] [blame] | 45 | AudioBuffer(size_t input_num_frames, |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 46 | size_t input_num_channels, |
| 47 | size_t buffer_num_frames, |
| 48 | size_t buffer_num_channels, |
Steve Anton | f254e9e | 2019-08-21 17:52:28 +0000 | [diff] [blame] | 49 | size_t output_num_frames); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 50 | virtual ~AudioBuffer(); |
| 51 | |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 52 | AudioBuffer(const AudioBuffer&) = delete; |
| 53 | AudioBuffer& operator=(const AudioBuffer&) = delete; |
Per Åhgren | 81c0cf2 | 2019-08-21 15:02:37 +0200 | [diff] [blame] | 54 | |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 55 | // Specify that downmixing should be done by selecting a single channel. |
| 56 | void set_downmixing_to_specific_channel(size_t channel); |
| 57 | |
| 58 | // Specify that downmixing should be done by averaging all channels,. |
| 59 | void set_downmixing_by_averaging(); |
| 60 | |
| 61 | // Set the number of channels in the buffer. The specified number of channels |
| 62 | // cannot be larger than the specified buffer_num_channels. The number is also |
| 63 | // reset at each call to CopyFrom or InterleaveFrom. |
| 64 | void set_num_channels(size_t num_channels); |
| 65 | |
| 66 | size_t num_channels() const { return num_channels_; } |
| 67 | size_t num_frames() const { return buffer_num_frames_; } |
| 68 | size_t num_frames_per_band() const { return num_split_frames_; } |
| 69 | size_t num_bands() const { return num_bands_; } |
| 70 | |
| 71 | // Returns pointer arrays to the full-band channels. |
aluebs@webrtc.org | 3aca0b0 | 2015-02-26 21:52:20 +0000 | [diff] [blame] | 72 | // Usage: |
| 73 | // channels()[channel][sample]. |
| 74 | // Where: |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 75 | // 0 <= channel < |buffer_num_channels_| |
| 76 | // 0 <= sample < |buffer_num_frames_| |
| 77 | float* const* channels() { return data_->channels(); } |
| 78 | const float* const* channels_const() const { return data_->channels(); } |
aluebs@webrtc.org | 3aca0b0 | 2015-02-26 21:52:20 +0000 | [diff] [blame] | 79 | |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 80 | // Returns pointer arrays to the bands for a specific channel. |
aluebs@webrtc.org | 3aca0b0 | 2015-02-26 21:52:20 +0000 | [diff] [blame] | 81 | // Usage: |
| 82 | // split_bands(channel)[band][sample]. |
| 83 | // Where: |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 84 | // 0 <= channel < |buffer_num_channels_| |
aluebs@webrtc.org | 3aca0b0 | 2015-02-26 21:52:20 +0000 | [diff] [blame] | 85 | // 0 <= band < |num_bands_| |
| 86 | // 0 <= sample < |num_split_frames_| |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 87 | const float* const* split_bands_const(size_t channel) const { |
| 88 | return split_data_.get() ? split_data_->bands(channel) |
| 89 | : data_->bands(channel); |
| 90 | } |
| 91 | float* const* split_bands(size_t channel) { |
| 92 | return split_data_.get() ? split_data_->bands(channel) |
| 93 | : data_->bands(channel); |
| 94 | } |
aluebs@webrtc.org | 3aca0b0 | 2015-02-26 21:52:20 +0000 | [diff] [blame] | 95 | |
| 96 | // Returns a pointer array to the channels for a specific band. |
| 97 | // Usage: |
| 98 | // split_channels(band)[channel][sample]. |
| 99 | // Where: |
| 100 | // 0 <= band < |num_bands_| |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 101 | // 0 <= channel < |buffer_num_channels_| |
aluebs@webrtc.org | 3aca0b0 | 2015-02-26 21:52:20 +0000 | [diff] [blame] | 102 | // 0 <= sample < |num_split_frames_| |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 103 | const float* const* split_channels_const(Band band) const { |
| 104 | if (split_data_.get()) { |
| 105 | return split_data_->channels(band); |
| 106 | } else { |
| 107 | return band == kBand0To8kHz ? data_->channels() : nullptr; |
| 108 | } |
| 109 | } |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 110 | |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 111 | // Copies data into the buffer. |
| 112 | void CopyFrom(const AudioFrame* frame); |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 113 | void CopyFrom(const float* const* data, const StreamConfig& stream_config); |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 114 | |
| 115 | // Copies data from the buffer. |
| 116 | void CopyTo(AudioFrame* frame) const; |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 117 | void CopyTo(const StreamConfig& stream_config, float* const* data); |
Gustaf Ullberg | 422b9e0 | 2019-10-09 13:02:14 +0200 | [diff] [blame] | 118 | void CopyTo(AudioBuffer* buffer) const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 119 | |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 120 | // Splits the buffer data into frequency bands. |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 121 | void SplitIntoFrequencyBands(); |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 122 | |
| 123 | // Recombines the frequency bands into a full-band signal. |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 124 | void MergeFrequencyBands(); |
| 125 | |
Per Åhgren | 928146f | 2019-08-20 09:19:21 +0200 | [diff] [blame] | 126 | // Copies the split bands data into the integer two-dimensional array. |
Per Åhgren | e35b32c | 2019-11-22 18:22:04 +0100 | [diff] [blame^] | 127 | void ExportSplitChannelData(size_t channel, |
| 128 | int16_t* const* split_band_data) const; |
Per Åhgren | 928146f | 2019-08-20 09:19:21 +0200 | [diff] [blame] | 129 | |
| 130 | // Copies the data in the integer two-dimensional array into the split_bands |
| 131 | // data. |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 132 | void ImportSplitChannelData(size_t channel, |
| 133 | const int16_t* const* split_band_data); |
Per Åhgren | 928146f | 2019-08-20 09:19:21 +0200 | [diff] [blame] | 134 | |
| 135 | static const size_t kMaxSplitFrameLength = 160; |
| 136 | static const size_t kMaxNumBands = 3; |
| 137 | |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 138 | // Deprecated methods, will be removed soon. |
| 139 | float* const* channels_f() { return channels(); } |
| 140 | const float* const* channels_const_f() const { return channels_const(); } |
| 141 | const float* const* split_bands_const_f(size_t channel) const { |
| 142 | return split_bands_const(channel); |
| 143 | } |
| 144 | float* const* split_bands_f(size_t channel) { return split_bands(channel); } |
| 145 | const float* const* split_channels_const_f(Band band) const { |
| 146 | return split_channels_const(band); |
| 147 | } |
| 148 | void DeinterleaveFrom(const AudioFrame* frame) { CopyFrom(frame); } |
| 149 | void InterleaveTo(AudioFrame* frame) const { CopyTo(frame); } |
| 150 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 151 | private: |
Alejandro Luebs | a181c9a | 2016-06-30 15:33:37 -0700 | [diff] [blame] | 152 | FRIEND_TEST_ALL_PREFIXES(AudioBufferTest, |
| 153 | SetNumChannelsSetsChannelBuffersNumChannels); |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 154 | void RestoreNumChannels(); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 155 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 156 | const size_t input_num_frames_; |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 157 | const size_t input_num_channels_; |
| 158 | const size_t buffer_num_frames_; |
| 159 | const size_t buffer_num_channels_; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 160 | const size_t output_num_frames_; |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 161 | const size_t output_num_channels_; |
Steve Anton | f254e9e | 2019-08-21 17:52:28 +0000 | [diff] [blame] | 162 | |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 163 | size_t num_channels_; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 164 | size_t num_bands_; |
| 165 | size_t num_split_frames_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 166 | |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 167 | std::unique_ptr<ChannelBuffer<float>> data_; |
| 168 | std::unique_ptr<ChannelBuffer<float>> split_data_; |
kwiberg | 88788ad | 2016-02-19 07:04:49 -0800 | [diff] [blame] | 169 | std::unique_ptr<SplittingFilter> splitting_filter_; |
kwiberg | 4a206a9 | 2016-03-31 10:24:26 -0700 | [diff] [blame] | 170 | std::vector<std::unique_ptr<PushSincResampler>> input_resamplers_; |
| 171 | std::vector<std::unique_ptr<PushSincResampler>> output_resamplers_; |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 172 | bool downmix_by_averaging_ = true; |
| 173 | size_t channel_for_downmixing_ = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 174 | }; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 175 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 176 | } // namespace webrtc |
| 177 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 178 | #endif // MODULES_AUDIO_PROCESSING_AUDIO_BUFFER_H_ |