blob: dd9b768437c8cc828ec46c732eabf3fa83d78105 [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
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +020020#include "api/audio/audio_frame.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "common_audio/channel_buffer.h"
22#include "modules/audio_processing/include/audio_processing.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000023
niklase@google.com470e71d2011-07-07 08:21:25 +000024namespace webrtc {
25
Yves Gerey988cc082018-10-23 12:03:01 +020026class PushSincResampler;
27class SplittingFilter;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000028
Yves Gerey665174f2018-06-19 15:03:05 +020029enum Band { kBand0To8kHz = 0, kBand8To16kHz = 1, kBand16To24kHz = 2 };
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +000030
Per Åhgren81c0cf22019-08-21 15:02:37 +020031// Stores any audio data in a way that allows the audio processing module to
32// operate on it in a controlled manner.
niklase@google.com470e71d2011-07-07 08:21:25 +000033class AudioBuffer {
34 public:
Per Åhgren81c0cf22019-08-21 15:02:37 +020035 AudioBuffer(size_t input_rate,
36 size_t input_num_channels,
37 size_t buffer_rate,
38 size_t buffer_num_channels,
39 size_t output_rate);
niklase@google.com470e71d2011-07-07 08:21:25 +000040 virtual ~AudioBuffer();
41
Per Åhgren81c0cf22019-08-21 15:02:37 +020042 AudioBuffer(const AudioBuffer&) = delete;
43 AudioBuffer& operator=(const AudioBuffer&) = delete;
niklase@google.com470e71d2011-07-07 08:21:25 +000044
Per Åhgren81c0cf22019-08-21 15:02:37 +020045 // Specify that downmixing should be done by selecting a single channel.
46 void set_downmixing_to_specific_channel(size_t channel);
47
48 // Specify that downmixing should be done by averaging all channels,.
49 void set_downmixing_by_averaging();
50
51 // Set the number of channels in the buffer. The specified number of channels
52 // cannot be larger than the specified buffer_num_channels. The number is also
53 // reset at each call to CopyFrom or InterleaveFrom.
54 void set_num_channels(size_t num_channels);
55
56 size_t num_channels() const { return num_channels_; }
57 size_t num_frames() const { return buffer_num_frames_; }
58 size_t num_frames_per_band() const { return num_split_frames_; }
59 size_t num_bands() const { return num_bands_; }
60
61 // Returns pointer arrays to the full-band channels.
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +000062 // Usage:
63 // channels()[channel][sample].
64 // Where:
Per Åhgren81c0cf22019-08-21 15:02:37 +020065 // 0 <= channel < |buffer_num_channels_|
66 // 0 <= sample < |buffer_num_frames_|
67 float* const* channels() { return data_->channels(); }
68 const float* const* channels_const() const { return data_->channels(); }
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +000069
Per Åhgren81c0cf22019-08-21 15:02:37 +020070 // Returns pointer arrays to the bands for a specific channel.
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +000071 // Usage:
72 // split_bands(channel)[band][sample].
73 // Where:
Per Åhgren81c0cf22019-08-21 15:02:37 +020074 // 0 <= channel < |buffer_num_channels_|
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +000075 // 0 <= band < |num_bands_|
76 // 0 <= sample < |num_split_frames_|
Per Åhgren81c0cf22019-08-21 15:02:37 +020077 const float* const* split_bands_const(size_t channel) const {
78 return split_data_.get() ? split_data_->bands(channel)
79 : data_->bands(channel);
80 }
81 float* const* split_bands(size_t channel) {
82 return split_data_.get() ? split_data_->bands(channel)
83 : data_->bands(channel);
84 }
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +000085
86 // Returns a pointer array to the channels for a specific band.
87 // Usage:
88 // split_channels(band)[channel][sample].
89 // Where:
90 // 0 <= band < |num_bands_|
Per Åhgren81c0cf22019-08-21 15:02:37 +020091 // 0 <= channel < |buffer_num_channels_|
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +000092 // 0 <= sample < |num_split_frames_|
Per Åhgren81c0cf22019-08-21 15:02:37 +020093 const float* const* split_channels_const(Band band) const {
94 if (split_data_.get()) {
95 return split_data_->channels(band);
96 } else {
97 return band == kBand0To8kHz ? data_->channels() : nullptr;
98 }
99 }
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000100
Per Åhgren81c0cf22019-08-21 15:02:37 +0200101 // Copies data into the buffer.
102 void CopyFrom(const AudioFrame* frame);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700103 void CopyFrom(const float* const* data, const StreamConfig& stream_config);
Per Åhgren81c0cf22019-08-21 15:02:37 +0200104
105 // Copies data from the buffer.
106 void CopyTo(AudioFrame* frame) const;
Michael Graczyk86c6d332015-07-23 11:41:39 -0700107 void CopyTo(const StreamConfig& stream_config, float* const* data);
niklase@google.com470e71d2011-07-07 08:21:25 +0000108
Per Åhgren81c0cf22019-08-21 15:02:37 +0200109 // Splits the buffer data into frequency bands.
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000110 void SplitIntoFrequencyBands();
Per Åhgren81c0cf22019-08-21 15:02:37 +0200111
112 // Recombines the frequency bands into a full-band signal.
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000113 void MergeFrequencyBands();
114
Per Åhgren928146f2019-08-20 09:19:21 +0200115 // Copies the split bands data into the integer two-dimensional array.
Per Åhgren81c0cf22019-08-21 15:02:37 +0200116 void ExportSplitChannelData(size_t channel, int16_t* const* split_band_data);
Per Åhgren928146f2019-08-20 09:19:21 +0200117
118 // Copies the data in the integer two-dimensional array into the split_bands
119 // data.
Per Åhgren81c0cf22019-08-21 15:02:37 +0200120 void ImportSplitChannelData(size_t channel,
121 const int16_t* const* split_band_data);
Per Åhgren928146f2019-08-20 09:19:21 +0200122
123 static const size_t kMaxSplitFrameLength = 160;
124 static const size_t kMaxNumBands = 3;
125
Per Åhgren81c0cf22019-08-21 15:02:37 +0200126 // Deprecated methods, will be removed soon.
127 float* const* channels_f() { return channels(); }
128 const float* const* channels_const_f() const { return channels_const(); }
129 const float* const* split_bands_const_f(size_t channel) const {
130 return split_bands_const(channel);
131 }
132 float* const* split_bands_f(size_t channel) { return split_bands(channel); }
133 const float* const* split_channels_const_f(Band band) const {
134 return split_channels_const(band);
135 }
136 void DeinterleaveFrom(const AudioFrame* frame) { CopyFrom(frame); }
137 void InterleaveTo(AudioFrame* frame) const { CopyTo(frame); }
138
niklase@google.com470e71d2011-07-07 08:21:25 +0000139 private:
Alejandro Luebsa181c9a2016-06-30 15:33:37 -0700140 FRIEND_TEST_ALL_PREFIXES(AudioBufferTest,
141 SetNumChannelsSetsChannelBuffersNumChannels);
Per Åhgren81c0cf22019-08-21 15:02:37 +0200142 void RestoreNumChannels();
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000143
Peter Kastingdce40cf2015-08-24 14:52:23 -0700144 const size_t input_num_frames_;
Per Åhgren81c0cf22019-08-21 15:02:37 +0200145 const size_t input_num_channels_;
146 const size_t buffer_num_frames_;
147 const size_t buffer_num_channels_;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700148 const size_t output_num_frames_;
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000149
Per Åhgren81c0cf22019-08-21 15:02:37 +0200150 size_t num_channels_;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700151 size_t num_bands_;
152 size_t num_split_frames_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000153
Per Åhgren81c0cf22019-08-21 15:02:37 +0200154 std::unique_ptr<ChannelBuffer<float>> data_;
155 std::unique_ptr<ChannelBuffer<float>> split_data_;
kwiberg88788ad2016-02-19 07:04:49 -0800156 std::unique_ptr<SplittingFilter> splitting_filter_;
Per Åhgren81c0cf22019-08-21 15:02:37 +0200157 std::unique_ptr<ChannelBuffer<float>> output_buffer_;
kwiberg4a206a92016-03-31 10:24:26 -0700158 std::vector<std::unique_ptr<PushSincResampler>> input_resamplers_;
159 std::vector<std::unique_ptr<PushSincResampler>> output_resamplers_;
Per Åhgren81c0cf22019-08-21 15:02:37 +0200160 bool downmix_by_averaging_ = true;
161 size_t channel_for_downmixing_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000162};
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000163
niklase@google.com470e71d2011-07-07 08:21:25 +0000164} // namespace webrtc
165
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200166#endif // MODULES_AUDIO_PROCESSING_AUDIO_BUFFER_H_