blob: da75dbf1b80de0217253fb8aca34d823490ea8c6 [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
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000011#ifndef WEBRTC_MODULES_AUDIO_PROCESSING_AUDIO_BUFFER_H_
12#define WEBRTC_MODULES_AUDIO_PROCESSING_AUDIO_BUFFER_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
kwiberg88788ad2016-02-19 07:04:49 -080014#include <memory>
kwiberg4a206a92016-03-31 10:24:26 -070015#include <vector>
kwiberg88788ad2016-02-19 07:04:49 -080016
kjellander@webrtc.org035e9122015-01-28 19:57:00 +000017#include "webrtc/common_audio/channel_buffer.h"
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000018#include "webrtc/modules/audio_processing/include/audio_processing.h"
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +000019#include "webrtc/modules/audio_processing/splitting_filter.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010020#include "webrtc/modules/include/module_common_types.h"
pbos@webrtc.org7fad4b82013-05-28 08:11:59 +000021#include "webrtc/typedefs.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000022
niklase@google.com470e71d2011-07-07 08:21:25 +000023namespace webrtc {
24
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000025class PushSincResampler;
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +000026class IFChannelBuffer;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000027
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +000028enum Band {
29 kBand0To8kHz = 0,
30 kBand8To16kHz = 1,
31 kBand16To24kHz = 2
32};
33
niklase@google.com470e71d2011-07-07 08:21:25 +000034class AudioBuffer {
35 public:
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000036 // TODO(ajm): Switch to take ChannelLayouts.
Peter Kastingdce40cf2015-08-24 14:52:23 -070037 AudioBuffer(size_t input_num_frames,
Peter Kasting69558702016-01-12 16:26:35 -080038 size_t num_input_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -070039 size_t process_num_frames,
Peter Kasting69558702016-01-12 16:26:35 -080040 size_t num_process_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -070041 size_t output_num_frames);
niklase@google.com470e71d2011-07-07 08:21:25 +000042 virtual ~AudioBuffer();
43
Peter Kasting69558702016-01-12 16:26:35 -080044 size_t num_channels() const;
45 void set_num_channels(size_t num_channels);
Peter Kastingdce40cf2015-08-24 14:52:23 -070046 size_t num_frames() const;
47 size_t num_frames_per_band() const;
48 size_t num_keyboard_frames() const;
49 size_t num_bands() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000050
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +000051 // Returns a pointer array to the full-band channels.
52 // Usage:
53 // channels()[channel][sample].
54 // Where:
55 // 0 <= channel < |num_proc_channels_|
56 // 0 <= sample < |proc_num_frames_|
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +000057 int16_t* const* channels();
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +000058 const int16_t* const* channels_const() const;
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +000059 float* const* channels_f();
60 const float* const* channels_const_f() const;
61
62 // Returns a pointer array to the bands for a specific channel.
63 // Usage:
64 // split_bands(channel)[band][sample].
65 // Where:
66 // 0 <= channel < |num_proc_channels_|
67 // 0 <= band < |num_bands_|
68 // 0 <= sample < |num_split_frames_|
Peter Kasting69558702016-01-12 16:26:35 -080069 int16_t* const* split_bands(size_t channel);
70 const int16_t* const* split_bands_const(size_t channel) const;
71 float* const* split_bands_f(size_t channel);
72 const float* const* split_bands_const_f(size_t channel) const;
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +000073
74 // Returns a pointer array to the channels for a specific band.
75 // Usage:
76 // split_channels(band)[channel][sample].
77 // Where:
78 // 0 <= band < |num_bands_|
79 // 0 <= channel < |num_proc_channels_|
80 // 0 <= sample < |num_split_frames_|
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +000081 int16_t* const* split_channels(Band band);
82 const int16_t* const* split_channels_const(Band band) const;
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +000083 float* const* split_channels_f(Band band);
84 const float* const* split_channels_const_f(Band band) const;
85
86 // Returns a pointer to the ChannelBuffer that encapsulates the full-band
87 // data.
88 ChannelBuffer<int16_t>* data();
89 const ChannelBuffer<int16_t>* data() const;
90 ChannelBuffer<float>* data_f();
91 const ChannelBuffer<float>* data_f() const;
92
93 // Returns a pointer to the ChannelBuffer that encapsulates the split data.
94 ChannelBuffer<int16_t>* split_data();
95 const ChannelBuffer<int16_t>* split_data() const;
96 ChannelBuffer<float>* split_data_f();
97 const ChannelBuffer<float>* split_data_f() const;
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +000098
aluebs@webrtc.org2561d522014-07-17 08:27:39 +000099 // Returns a pointer to the low-pass data downmixed to mono. If this data
100 // isn't already available it re-calculates it.
101 const int16_t* mixed_low_pass_data();
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000102 const int16_t* low_pass_reference(int channel) const;
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000103
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000104 const float* keyboard_data() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000105
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000106 void set_activity(AudioFrame::VADActivity activity);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000107 AudioFrame::VADActivity activity() const;
108
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000109 // Use for int16 interleaved data.
niklase@google.com470e71d2011-07-07 08:21:25 +0000110 void DeinterleaveFrom(AudioFrame* audioFrame);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000111 // If |data_changed| is false, only the non-audio data members will be copied
112 // to |frame|.
kthelgasonc7daea82017-03-14 03:10:07 -0700113 void InterleaveTo(AudioFrame* frame, bool data_changed) const;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000114
115 // Use for float deinterleaved data.
Michael Graczyk86c6d332015-07-23 11:41:39 -0700116 void CopyFrom(const float* const* data, const StreamConfig& stream_config);
117 void CopyTo(const StreamConfig& stream_config, float* const* data);
niklase@google.com470e71d2011-07-07 08:21:25 +0000118 void CopyLowPassToReference();
119
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000120 // Splits the signal into different bands.
121 void SplitIntoFrequencyBands();
122 // Recombine the different bands into one signal.
123 void MergeFrequencyBands();
124
niklase@google.com470e71d2011-07-07 08:21:25 +0000125 private:
Alejandro Luebsa181c9a2016-06-30 15:33:37 -0700126 FRIEND_TEST_ALL_PREFIXES(AudioBufferTest,
127 SetNumChannelsSetsChannelBuffersNumChannels);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000128 // Called from DeinterleaveFrom() and CopyFrom().
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000129 void InitForNewData();
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000130
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000131 // The audio is passed into DeinterleaveFrom() or CopyFrom() with input
132 // format (samples per channel and number of channels).
Peter Kastingdce40cf2015-08-24 14:52:23 -0700133 const size_t input_num_frames_;
Peter Kasting69558702016-01-12 16:26:35 -0800134 const size_t num_input_channels_;
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000135 // The audio is stored by DeinterleaveFrom() or CopyFrom() with processing
136 // format.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700137 const size_t proc_num_frames_;
Peter Kasting69558702016-01-12 16:26:35 -0800138 const size_t num_proc_channels_;
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000139 // The audio is returned by InterleaveTo() and CopyTo() with output samples
140 // per channels and the current number of channels. This last one can be
141 // changed at any time using set_num_channels().
Peter Kastingdce40cf2015-08-24 14:52:23 -0700142 const size_t output_num_frames_;
Peter Kasting69558702016-01-12 16:26:35 -0800143 size_t num_channels_;
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000144
Peter Kastingdce40cf2015-08-24 14:52:23 -0700145 size_t num_bands_;
146 size_t num_split_frames_;
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000147 bool mixed_low_pass_valid_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000148 bool reference_copied_;
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000149 AudioFrame::VADActivity activity_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000150
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000151 const float* keyboard_data_;
kwiberg88788ad2016-02-19 07:04:49 -0800152 std::unique_ptr<IFChannelBuffer> data_;
153 std::unique_ptr<IFChannelBuffer> split_data_;
154 std::unique_ptr<SplittingFilter> splitting_filter_;
155 std::unique_ptr<ChannelBuffer<int16_t> > mixed_low_pass_channels_;
156 std::unique_ptr<ChannelBuffer<int16_t> > low_pass_reference_channels_;
157 std::unique_ptr<IFChannelBuffer> input_buffer_;
158 std::unique_ptr<IFChannelBuffer> output_buffer_;
159 std::unique_ptr<ChannelBuffer<float> > process_buffer_;
kwiberg4a206a92016-03-31 10:24:26 -0700160 std::vector<std::unique_ptr<PushSincResampler>> input_resamplers_;
161 std::vector<std::unique_ptr<PushSincResampler>> output_resamplers_;
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
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000166#endif // WEBRTC_MODULES_AUDIO_PROCESSING_AUDIO_BUFFER_H_