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 | |
mflodman@webrtc.org | d5da250 | 2014-05-15 11:17:21 +0000 | [diff] [blame] | 26 | class IFChannelBuffer; |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 27 | class PushSincResampler; |
| 28 | class SplittingFilter; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 29 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 30 | enum Band { kBand0To8kHz = 0, kBand8To16kHz = 1, kBand16To24kHz = 2 }; |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 31 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 32 | class AudioBuffer { |
| 33 | public: |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 34 | // TODO(ajm): Switch to take ChannelLayouts. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 35 | AudioBuffer(size_t input_num_frames, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 36 | size_t num_input_channels, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 37 | size_t process_num_frames, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 38 | size_t num_process_channels, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 39 | size_t output_num_frames); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 40 | virtual ~AudioBuffer(); |
| 41 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 42 | size_t num_channels() const; |
Per Åhgren | a135127 | 2019-08-15 12:15:46 +0200 | [diff] [blame] | 43 | size_t num_proc_channels() const { return num_proc_channels_; } |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 44 | void set_num_channels(size_t num_channels); |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 45 | size_t num_frames() const; |
| 46 | size_t num_frames_per_band() const; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 47 | size_t num_bands() const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 48 | |
aluebs@webrtc.org | 3aca0b0 | 2015-02-26 21:52:20 +0000 | [diff] [blame] | 49 | // Returns a pointer array to the full-band channels. |
| 50 | // Usage: |
| 51 | // channels()[channel][sample]. |
| 52 | // Where: |
| 53 | // 0 <= channel < |num_proc_channels_| |
| 54 | // 0 <= sample < |proc_num_frames_| |
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 | float* const* split_bands_f(size_t channel); |
| 66 | const float* const* split_bands_const_f(size_t channel) const; |
aluebs@webrtc.org | 3aca0b0 | 2015-02-26 21:52:20 +0000 | [diff] [blame] | 67 | |
| 68 | // Returns a pointer array to the channels for a specific band. |
| 69 | // Usage: |
| 70 | // split_channels(band)[channel][sample]. |
| 71 | // Where: |
| 72 | // 0 <= band < |num_bands_| |
| 73 | // 0 <= channel < |num_proc_channels_| |
| 74 | // 0 <= sample < |num_split_frames_| |
Per Åhgren | 928146f | 2019-08-20 09:19:21 +0200 | [diff] [blame] | 75 | const float* const* split_channels_const_f(Band band) const; |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 76 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 77 | // Use for int16 interleaved data. |
Per Åhgren | a135127 | 2019-08-15 12:15:46 +0200 | [diff] [blame] | 78 | void DeinterleaveFrom(const AudioFrame* audioFrame); |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 79 | // If |data_changed| is false, only the non-audio data members will be copied |
| 80 | // to |frame|. |
Per Åhgren | a135127 | 2019-08-15 12:15:46 +0200 | [diff] [blame] | 81 | void InterleaveTo(AudioFrame* frame) const; |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 82 | |
| 83 | // Use for float deinterleaved data. |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 84 | void CopyFrom(const float* const* data, const StreamConfig& stream_config); |
| 85 | void CopyTo(const StreamConfig& stream_config, float* const* data); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 86 | |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 87 | // Splits the signal into different bands. |
| 88 | void SplitIntoFrequencyBands(); |
| 89 | // Recombine the different bands into one signal. |
| 90 | void MergeFrequencyBands(); |
| 91 | |
Per Åhgren | 928146f | 2019-08-20 09:19:21 +0200 | [diff] [blame] | 92 | // Copies the split bands data into the integer two-dimensional array. |
| 93 | void CopySplitChannelDataTo(size_t channel, int16_t* const* split_band_data); |
| 94 | |
| 95 | // Copies the data in the integer two-dimensional array into the split_bands |
| 96 | // data. |
| 97 | void CopySplitChannelDataFrom(size_t channel, |
| 98 | const int16_t* const* split_band_data); |
| 99 | |
| 100 | static const size_t kMaxSplitFrameLength = 160; |
| 101 | static const size_t kMaxNumBands = 3; |
| 102 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 103 | private: |
Alejandro Luebs | a181c9a | 2016-06-30 15:33:37 -0700 | [diff] [blame] | 104 | FRIEND_TEST_ALL_PREFIXES(AudioBufferTest, |
| 105 | SetNumChannelsSetsChannelBuffersNumChannels); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 106 | // Called from DeinterleaveFrom() and CopyFrom(). |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 107 | void InitForNewData(); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 108 | |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 109 | // The audio is passed into DeinterleaveFrom() or CopyFrom() with input |
| 110 | // format (samples per channel and number of channels). |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 111 | const size_t input_num_frames_; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 112 | const size_t num_input_channels_; |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 113 | // The audio is stored by DeinterleaveFrom() or CopyFrom() with processing |
| 114 | // format. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 115 | const size_t proc_num_frames_; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 116 | const size_t num_proc_channels_; |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 117 | // The audio is returned by InterleaveTo() and CopyTo() with output samples |
| 118 | // per channels and the current number of channels. This last one can be |
| 119 | // changed at any time using set_num_channels(). |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 120 | const size_t output_num_frames_; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 121 | size_t num_channels_; |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 122 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 123 | size_t num_bands_; |
| 124 | size_t num_split_frames_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 125 | |
kwiberg | 88788ad | 2016-02-19 07:04:49 -0800 | [diff] [blame] | 126 | std::unique_ptr<IFChannelBuffer> data_; |
| 127 | std::unique_ptr<IFChannelBuffer> split_data_; |
| 128 | std::unique_ptr<SplittingFilter> splitting_filter_; |
kwiberg | 88788ad | 2016-02-19 07:04:49 -0800 | [diff] [blame] | 129 | std::unique_ptr<IFChannelBuffer> input_buffer_; |
| 130 | std::unique_ptr<IFChannelBuffer> output_buffer_; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 131 | std::unique_ptr<ChannelBuffer<float>> process_buffer_; |
kwiberg | 4a206a9 | 2016-03-31 10:24:26 -0700 | [diff] [blame] | 132 | std::vector<std::unique_ptr<PushSincResampler>> input_resamplers_; |
| 133 | std::vector<std::unique_ptr<PushSincResampler>> output_resamplers_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 134 | }; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 135 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 136 | } // namespace webrtc |
| 137 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 138 | #endif // MODULES_AUDIO_PROCESSING_AUDIO_BUFFER_H_ |