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 | |
kwiberg | 88788ad | 2016-02-19 07:04:49 -0800 | [diff] [blame] | 14 | #include <memory> |
kwiberg | 4a206a9 | 2016-03-31 10:24:26 -0700 | [diff] [blame] | 15 | #include <vector> |
kwiberg | 88788ad | 2016-02-19 07:04:49 -0800 | [diff] [blame] | 16 | |
Fredrik Solenberg | bbf21a3 | 2018-04-12 22:44:09 +0200 | [diff] [blame] | 17 | #include "api/audio/audio_frame.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "common_audio/channel_buffer.h" |
| 19 | #include "modules/audio_processing/include/audio_processing.h" |
| 20 | #include "modules/audio_processing/splitting_filter.h" |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 21 | #include "typedefs.h" // NOLINT(build/include) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 22 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 23 | namespace webrtc { |
| 24 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 25 | class PushSincResampler; |
mflodman@webrtc.org | d5da250 | 2014-05-15 11:17:21 +0000 | [diff] [blame] | 26 | class IFChannelBuffer; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 27 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 28 | enum Band { kBand0To8kHz = 0, kBand8To16kHz = 1, kBand16To24kHz = 2 }; |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 29 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 30 | class AudioBuffer { |
| 31 | public: |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 32 | // TODO(ajm): Switch to take ChannelLayouts. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 33 | AudioBuffer(size_t input_num_frames, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 34 | size_t num_input_channels, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 35 | size_t process_num_frames, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 36 | size_t num_process_channels, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 37 | size_t output_num_frames); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 38 | virtual ~AudioBuffer(); |
| 39 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 40 | size_t num_channels() const; |
| 41 | void set_num_channels(size_t num_channels); |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 42 | size_t num_frames() const; |
| 43 | size_t num_frames_per_band() const; |
| 44 | size_t num_keyboard_frames() const; |
| 45 | size_t num_bands() const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 46 | |
aluebs@webrtc.org | 3aca0b0 | 2015-02-26 21:52:20 +0000 | [diff] [blame] | 47 | // Returns a pointer array to the full-band channels. |
| 48 | // Usage: |
| 49 | // channels()[channel][sample]. |
| 50 | // Where: |
| 51 | // 0 <= channel < |num_proc_channels_| |
| 52 | // 0 <= sample < |proc_num_frames_| |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 53 | int16_t* const* channels(); |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 54 | const int16_t* const* channels_const() const; |
aluebs@webrtc.org | 3aca0b0 | 2015-02-26 21:52:20 +0000 | [diff] [blame] | 55 | float* const* channels_f(); |
| 56 | const float* const* channels_const_f() const; |
| 57 | |
| 58 | // Returns a pointer array to the bands for a specific channel. |
| 59 | // Usage: |
| 60 | // split_bands(channel)[band][sample]. |
| 61 | // Where: |
| 62 | // 0 <= channel < |num_proc_channels_| |
| 63 | // 0 <= band < |num_bands_| |
| 64 | // 0 <= sample < |num_split_frames_| |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 65 | int16_t* const* split_bands(size_t channel); |
| 66 | const int16_t* const* split_bands_const(size_t channel) const; |
| 67 | float* const* split_bands_f(size_t channel); |
| 68 | const float* const* split_bands_const_f(size_t channel) const; |
aluebs@webrtc.org | 3aca0b0 | 2015-02-26 21:52:20 +0000 | [diff] [blame] | 69 | |
| 70 | // Returns a pointer array to the channels for a specific band. |
| 71 | // Usage: |
| 72 | // split_channels(band)[channel][sample]. |
| 73 | // Where: |
| 74 | // 0 <= band < |num_bands_| |
| 75 | // 0 <= channel < |num_proc_channels_| |
| 76 | // 0 <= sample < |num_split_frames_| |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 77 | int16_t* const* split_channels(Band band); |
| 78 | const int16_t* const* split_channels_const(Band band) const; |
aluebs@webrtc.org | 3aca0b0 | 2015-02-26 21:52:20 +0000 | [diff] [blame] | 79 | float* const* split_channels_f(Band band); |
| 80 | const float* const* split_channels_const_f(Band band) const; |
| 81 | |
| 82 | // Returns a pointer to the ChannelBuffer that encapsulates the full-band |
| 83 | // data. |
| 84 | ChannelBuffer<int16_t>* data(); |
| 85 | const ChannelBuffer<int16_t>* data() const; |
| 86 | ChannelBuffer<float>* data_f(); |
| 87 | const ChannelBuffer<float>* data_f() const; |
| 88 | |
| 89 | // Returns a pointer to the ChannelBuffer that encapsulates the split data. |
| 90 | ChannelBuffer<int16_t>* split_data(); |
| 91 | const ChannelBuffer<int16_t>* split_data() const; |
| 92 | ChannelBuffer<float>* split_data_f(); |
| 93 | const ChannelBuffer<float>* split_data_f() const; |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 94 | |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame] | 95 | // Returns a pointer to the low-pass data downmixed to mono. If this data |
| 96 | // isn't already available it re-calculates it. |
| 97 | const int16_t* mixed_low_pass_data(); |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame] | 98 | const int16_t* low_pass_reference(int channel) const; |
mflodman@webrtc.org | d5da250 | 2014-05-15 11:17:21 +0000 | [diff] [blame] | 99 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 100 | const float* keyboard_data() const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 101 | |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 102 | void set_activity(AudioFrame::VADActivity activity); |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 103 | AudioFrame::VADActivity activity() const; |
| 104 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 105 | // Use for int16 interleaved data. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 106 | void DeinterleaveFrom(AudioFrame* audioFrame); |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 107 | // If |data_changed| is false, only the non-audio data members will be copied |
| 108 | // to |frame|. |
kthelgason | c7daea8 | 2017-03-14 03:10:07 -0700 | [diff] [blame] | 109 | void InterleaveTo(AudioFrame* frame, bool data_changed) const; |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 110 | |
| 111 | // Use for float deinterleaved data. |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 112 | void CopyFrom(const float* const* data, const StreamConfig& stream_config); |
| 113 | void CopyTo(const StreamConfig& stream_config, float* const* data); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 114 | void CopyLowPassToReference(); |
| 115 | |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 116 | // Splits the signal into different bands. |
| 117 | void SplitIntoFrequencyBands(); |
| 118 | // Recombine the different bands into one signal. |
| 119 | void MergeFrequencyBands(); |
| 120 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 121 | private: |
Alejandro Luebs | a181c9a | 2016-06-30 15:33:37 -0700 | [diff] [blame] | 122 | FRIEND_TEST_ALL_PREFIXES(AudioBufferTest, |
| 123 | SetNumChannelsSetsChannelBuffersNumChannels); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 124 | // Called from DeinterleaveFrom() and CopyFrom(). |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 125 | void InitForNewData(); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 126 | |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 127 | // The audio is passed into DeinterleaveFrom() or CopyFrom() with input |
| 128 | // format (samples per channel and number of channels). |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 129 | const size_t input_num_frames_; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 130 | const size_t num_input_channels_; |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 131 | // The audio is stored by DeinterleaveFrom() or CopyFrom() with processing |
| 132 | // format. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 133 | const size_t proc_num_frames_; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 134 | const size_t num_proc_channels_; |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 135 | // The audio is returned by InterleaveTo() and CopyTo() with output samples |
| 136 | // per channels and the current number of channels. This last one can be |
| 137 | // changed at any time using set_num_channels(). |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 138 | const size_t output_num_frames_; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 139 | size_t num_channels_; |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 140 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 141 | size_t num_bands_; |
| 142 | size_t num_split_frames_; |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame] | 143 | bool mixed_low_pass_valid_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 144 | bool reference_copied_; |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 145 | AudioFrame::VADActivity activity_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 146 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 147 | const float* keyboard_data_; |
kwiberg | 88788ad | 2016-02-19 07:04:49 -0800 | [diff] [blame] | 148 | std::unique_ptr<IFChannelBuffer> data_; |
| 149 | std::unique_ptr<IFChannelBuffer> split_data_; |
| 150 | std::unique_ptr<SplittingFilter> splitting_filter_; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 151 | std::unique_ptr<ChannelBuffer<int16_t>> mixed_low_pass_channels_; |
| 152 | std::unique_ptr<ChannelBuffer<int16_t>> low_pass_reference_channels_; |
kwiberg | 88788ad | 2016-02-19 07:04:49 -0800 | [diff] [blame] | 153 | std::unique_ptr<IFChannelBuffer> input_buffer_; |
| 154 | std::unique_ptr<IFChannelBuffer> output_buffer_; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 155 | std::unique_ptr<ChannelBuffer<float>> process_buffer_; |
kwiberg | 4a206a9 | 2016-03-31 10:24:26 -0700 | [diff] [blame] | 156 | std::vector<std::unique_ptr<PushSincResampler>> input_resamplers_; |
| 157 | std::vector<std::unique_ptr<PushSincResampler>> output_resamplers_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 158 | }; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 159 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 160 | } // namespace webrtc |
| 161 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 162 | #endif // MODULES_AUDIO_PROCESSING_AUDIO_BUFFER_H_ |