blob: 3eecf0dbaa55a20a397c0c39a42952e6ea282265 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_PROCESSING_AUDIO_BUFFER_H_
12#define MODULES_AUDIO_PROCESSING_AUDIO_BUFFER_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
15#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020016
kwiberg88788ad2016-02-19 07:04:49 -080017#include <memory>
kwiberg4a206a92016-03-31 10:24:26 -070018#include <vector>
kwiberg88788ad2016-02-19 07:04:49 -080019
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "common_audio/channel_buffer.h"
21#include "modules/audio_processing/include/audio_processing.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000022
niklase@google.com470e71d2011-07-07 08:21:25 +000023namespace webrtc {
24
Yves Gerey988cc082018-10-23 12:03:01 +020025class PushSincResampler;
26class SplittingFilter;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000027
Yves Gerey665174f2018-06-19 15:03:05 +020028enum Band { kBand0To8kHz = 0, kBand8To16kHz = 1, kBand16To24kHz = 2 };
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +000029
Per Åhgrend47941e2019-08-22 11:51:13 +020030// Stores any audio data in a way that allows the audio processing module to
31// operate on it in a controlled manner.
niklase@google.com470e71d2011-07-07 08:21:25 +000032class AudioBuffer {
33 public:
Per Åhgren0aefbf02019-08-23 21:29:17 +020034 static const int kSplitBandSize = 160;
Per Åhgren3e8bf282019-08-29 23:38:40 +020035 static const size_t kMaxSampleRate = 384000;
Per Åhgrend47941e2019-08-22 11:51:13 +020036 AudioBuffer(size_t input_rate,
37 size_t input_num_channels,
38 size_t buffer_rate,
39 size_t buffer_num_channels,
40 size_t output_rate,
41 size_t output_num_channels);
42
43 // The constructor below will be deprecated.
Steve Antonf254e9e2019-08-21 17:52:28 +000044 AudioBuffer(size_t input_num_frames,
Per Åhgrend47941e2019-08-22 11:51:13 +020045 size_t input_num_channels,
46 size_t buffer_num_frames,
47 size_t buffer_num_channels,
Steve Antonf254e9e2019-08-21 17:52:28 +000048 size_t output_num_frames);
niklase@google.com470e71d2011-07-07 08:21:25 +000049 virtual ~AudioBuffer();
50
Per Åhgrend47941e2019-08-22 11:51:13 +020051 AudioBuffer(const AudioBuffer&) = delete;
52 AudioBuffer& operator=(const AudioBuffer&) = delete;
Per Åhgren81c0cf22019-08-21 15:02:37 +020053
Per Åhgrend47941e2019-08-22 11:51:13 +020054 // Specify that downmixing should be done by selecting a single channel.
55 void set_downmixing_to_specific_channel(size_t channel);
56
57 // Specify that downmixing should be done by averaging all channels,.
58 void set_downmixing_by_averaging();
59
60 // Set the number of channels in the buffer. The specified number of channels
61 // cannot be larger than the specified buffer_num_channels. The number is also
62 // reset at each call to CopyFrom or InterleaveFrom.
63 void set_num_channels(size_t num_channels);
64
65 size_t num_channels() const { return num_channels_; }
66 size_t num_frames() const { return buffer_num_frames_; }
67 size_t num_frames_per_band() const { return num_split_frames_; }
68 size_t num_bands() const { return num_bands_; }
69
70 // Returns pointer arrays to the full-band channels.
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +000071 // Usage:
72 // channels()[channel][sample].
73 // Where:
Per Åhgrend47941e2019-08-22 11:51:13 +020074 // 0 <= channel < |buffer_num_channels_|
75 // 0 <= sample < |buffer_num_frames_|
76 float* const* channels() { return data_->channels(); }
77 const float* const* channels_const() const { return data_->channels(); }
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +000078
Per Åhgrend47941e2019-08-22 11:51:13 +020079 // Returns pointer arrays to the bands for a specific channel.
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +000080 // Usage:
81 // split_bands(channel)[band][sample].
82 // Where:
Per Åhgrend47941e2019-08-22 11:51:13 +020083 // 0 <= channel < |buffer_num_channels_|
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +000084 // 0 <= band < |num_bands_|
85 // 0 <= sample < |num_split_frames_|
Per Åhgrend47941e2019-08-22 11:51:13 +020086 const float* const* split_bands_const(size_t channel) const {
87 return split_data_.get() ? split_data_->bands(channel)
88 : data_->bands(channel);
89 }
90 float* const* split_bands(size_t channel) {
91 return split_data_.get() ? split_data_->bands(channel)
92 : data_->bands(channel);
93 }
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +000094
95 // Returns a pointer array to the channels for a specific band.
96 // Usage:
97 // split_channels(band)[channel][sample].
98 // Where:
99 // 0 <= band < |num_bands_|
Per Åhgrend47941e2019-08-22 11:51:13 +0200100 // 0 <= channel < |buffer_num_channels_|
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +0000101 // 0 <= sample < |num_split_frames_|
Per Åhgrend47941e2019-08-22 11:51:13 +0200102 const float* const* split_channels_const(Band band) const {
103 if (split_data_.get()) {
104 return split_data_->channels(band);
105 } else {
106 return band == kBand0To8kHz ? data_->channels() : nullptr;
107 }
108 }
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000109
Per Åhgrend47941e2019-08-22 11:51:13 +0200110 // Copies data into the buffer.
Per Åhgren645f24c2020-03-16 12:06:02 +0100111 void CopyFrom(const int16_t* const interleaved_data,
112 const StreamConfig& stream_config);
113 void CopyFrom(const float* const* stacked_data,
114 const StreamConfig& stream_config);
Per Åhgrend47941e2019-08-22 11:51:13 +0200115
116 // Copies data from the buffer.
Per Åhgren645f24c2020-03-16 12:06:02 +0100117 void CopyTo(const StreamConfig& stream_config,
118 int16_t* const interleaved_data);
119 void CopyTo(const StreamConfig& stream_config, float* const* stacked_data);
Gustaf Ullberg422b9e02019-10-09 13:02:14 +0200120 void CopyTo(AudioBuffer* buffer) const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000121
Per Åhgrend47941e2019-08-22 11:51:13 +0200122 // Splits the buffer data into frequency bands.
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000123 void SplitIntoFrequencyBands();
Per Åhgrend47941e2019-08-22 11:51:13 +0200124
125 // Recombines the frequency bands into a full-band signal.
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000126 void MergeFrequencyBands();
127
Per Åhgren928146f2019-08-20 09:19:21 +0200128 // Copies the split bands data into the integer two-dimensional array.
Per Åhgrene35b32c2019-11-22 18:22:04 +0100129 void ExportSplitChannelData(size_t channel,
130 int16_t* const* split_band_data) const;
Per Åhgren928146f2019-08-20 09:19:21 +0200131
132 // Copies the data in the integer two-dimensional array into the split_bands
133 // data.
Per Åhgrend47941e2019-08-22 11:51:13 +0200134 void ImportSplitChannelData(size_t channel,
135 const int16_t* const* split_band_data);
Per Åhgren928146f2019-08-20 09:19:21 +0200136
137 static const size_t kMaxSplitFrameLength = 160;
138 static const size_t kMaxNumBands = 3;
139
Per Åhgrend47941e2019-08-22 11:51:13 +0200140 // Deprecated methods, will be removed soon.
141 float* const* channels_f() { return channels(); }
142 const float* const* channels_const_f() const { return channels_const(); }
143 const float* const* split_bands_const_f(size_t channel) const {
144 return split_bands_const(channel);
145 }
146 float* const* split_bands_f(size_t channel) { return split_bands(channel); }
147 const float* const* split_channels_const_f(Band band) const {
148 return split_channels_const(band);
149 }
Per Åhgrend47941e2019-08-22 11:51:13 +0200150
niklase@google.com470e71d2011-07-07 08:21:25 +0000151 private:
Alejandro Luebsa181c9a2016-06-30 15:33:37 -0700152 FRIEND_TEST_ALL_PREFIXES(AudioBufferTest,
153 SetNumChannelsSetsChannelBuffersNumChannels);
Per Åhgrend47941e2019-08-22 11:51:13 +0200154 void RestoreNumChannels();
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000155
Peter Kastingdce40cf2015-08-24 14:52:23 -0700156 const size_t input_num_frames_;
Per Åhgrend47941e2019-08-22 11:51:13 +0200157 const size_t input_num_channels_;
158 const size_t buffer_num_frames_;
159 const size_t buffer_num_channels_;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700160 const size_t output_num_frames_;
Per Åhgrend47941e2019-08-22 11:51:13 +0200161 const size_t output_num_channels_;
Steve Antonf254e9e2019-08-21 17:52:28 +0000162
Per Åhgrend47941e2019-08-22 11:51:13 +0200163 size_t num_channels_;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700164 size_t num_bands_;
165 size_t num_split_frames_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000166
Per Åhgrend47941e2019-08-22 11:51:13 +0200167 std::unique_ptr<ChannelBuffer<float>> data_;
168 std::unique_ptr<ChannelBuffer<float>> split_data_;
kwiberg88788ad2016-02-19 07:04:49 -0800169 std::unique_ptr<SplittingFilter> splitting_filter_;
kwiberg4a206a92016-03-31 10:24:26 -0700170 std::vector<std::unique_ptr<PushSincResampler>> input_resamplers_;
171 std::vector<std::unique_ptr<PushSincResampler>> output_resamplers_;
Per Åhgrend47941e2019-08-22 11:51:13 +0200172 bool downmix_by_averaging_ = true;
173 size_t channel_for_downmixing_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000174};
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000175
niklase@google.com470e71d2011-07-07 08:21:25 +0000176} // namespace webrtc
177
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200178#endif // MODULES_AUDIO_PROCESSING_AUDIO_BUFFER_H_