blob: a85144b39ea0362b7b2b9f65ccb372095d023ca5 [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>
kwiberg88788ad2016-02-19 07:04:49 -080016#include <memory>
kwiberg4a206a92016-03-31 10:24:26 -070017#include <vector>
kwiberg88788ad2016-02-19 07:04:49 -080018
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +020019#include "api/audio/audio_frame.h"
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
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +000025class IFChannelBuffer;
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
niklase@google.com470e71d2011-07-07 08:21:25 +000031class AudioBuffer {
32 public:
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000033 // TODO(ajm): Switch to take ChannelLayouts.
Peter Kastingdce40cf2015-08-24 14:52:23 -070034 AudioBuffer(size_t input_num_frames,
Peter Kasting69558702016-01-12 16:26:35 -080035 size_t num_input_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -070036 size_t process_num_frames,
Peter Kasting69558702016-01-12 16:26:35 -080037 size_t num_process_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -070038 size_t output_num_frames);
niklase@google.com470e71d2011-07-07 08:21:25 +000039 virtual ~AudioBuffer();
40
Peter Kasting69558702016-01-12 16:26:35 -080041 size_t num_channels() const;
42 void set_num_channels(size_t num_channels);
Peter Kastingdce40cf2015-08-24 14:52:23 -070043 size_t num_frames() const;
44 size_t num_frames_per_band() const;
45 size_t num_keyboard_frames() const;
46 size_t num_bands() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000047
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +000048 // Returns a pointer array to the full-band channels.
49 // Usage:
50 // channels()[channel][sample].
51 // Where:
52 // 0 <= channel < |num_proc_channels_|
53 // 0 <= sample < |proc_num_frames_|
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +000054 int16_t* const* channels();
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +000055 const int16_t* const* channels_const() const;
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +000056 float* const* channels_f();
57 const float* const* channels_const_f() const;
58
59 // Returns a pointer array to the bands for a specific channel.
60 // Usage:
61 // split_bands(channel)[band][sample].
62 // Where:
63 // 0 <= channel < |num_proc_channels_|
64 // 0 <= band < |num_bands_|
65 // 0 <= sample < |num_split_frames_|
Peter Kasting69558702016-01-12 16:26:35 -080066 int16_t* const* split_bands(size_t channel);
67 const int16_t* const* split_bands_const(size_t channel) const;
68 float* const* split_bands_f(size_t channel);
69 const float* const* split_bands_const_f(size_t channel) const;
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +000070
71 // Returns a pointer array to the channels for a specific band.
72 // Usage:
73 // split_channels(band)[channel][sample].
74 // Where:
75 // 0 <= band < |num_bands_|
76 // 0 <= channel < |num_proc_channels_|
77 // 0 <= sample < |num_split_frames_|
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +000078 int16_t* const* split_channels(Band band);
79 const int16_t* const* split_channels_const(Band band) const;
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +000080 float* const* split_channels_f(Band band);
81 const float* const* split_channels_const_f(Band band) const;
82
83 // Returns a pointer to the ChannelBuffer that encapsulates the full-band
84 // data.
85 ChannelBuffer<int16_t>* data();
86 const ChannelBuffer<int16_t>* data() const;
87 ChannelBuffer<float>* data_f();
88 const ChannelBuffer<float>* data_f() const;
89
90 // Returns a pointer to the ChannelBuffer that encapsulates the split data.
91 ChannelBuffer<int16_t>* split_data();
92 const ChannelBuffer<int16_t>* split_data() const;
93 ChannelBuffer<float>* split_data_f();
94 const ChannelBuffer<float>* split_data_f() const;
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +000095
aluebs@webrtc.org2561d522014-07-17 08:27:39 +000096 // Returns a pointer to the low-pass data downmixed to mono. If this data
97 // isn't already available it re-calculates it.
98 const int16_t* mixed_low_pass_data();
andrew@webrtc.org65f93382014-04-30 16:44:13 +000099 const int16_t* low_pass_reference(int channel) const;
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000100
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000101 const float* keyboard_data() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000102
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000103 void set_activity(AudioFrame::VADActivity activity);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000104 AudioFrame::VADActivity activity() const;
105
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000106 // Use for int16 interleaved data.
niklase@google.com470e71d2011-07-07 08:21:25 +0000107 void DeinterleaveFrom(AudioFrame* audioFrame);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000108 // If |data_changed| is false, only the non-audio data members will be copied
109 // to |frame|.
kthelgasonc7daea82017-03-14 03:10:07 -0700110 void InterleaveTo(AudioFrame* frame, bool data_changed) const;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000111
112 // Use for float deinterleaved data.
Michael Graczyk86c6d332015-07-23 11:41:39 -0700113 void CopyFrom(const float* const* data, const StreamConfig& stream_config);
114 void CopyTo(const StreamConfig& stream_config, float* const* data);
niklase@google.com470e71d2011-07-07 08:21:25 +0000115 void CopyLowPassToReference();
116
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000117 // Splits the signal into different bands.
118 void SplitIntoFrequencyBands();
119 // Recombine the different bands into one signal.
120 void MergeFrequencyBands();
121
niklase@google.com470e71d2011-07-07 08:21:25 +0000122 private:
Alejandro Luebsa181c9a2016-06-30 15:33:37 -0700123 FRIEND_TEST_ALL_PREFIXES(AudioBufferTest,
124 SetNumChannelsSetsChannelBuffersNumChannels);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000125 // Called from DeinterleaveFrom() and CopyFrom().
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000126 void InitForNewData();
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000127
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000128 // The audio is passed into DeinterleaveFrom() or CopyFrom() with input
129 // format (samples per channel and number of channels).
Peter Kastingdce40cf2015-08-24 14:52:23 -0700130 const size_t input_num_frames_;
Peter Kasting69558702016-01-12 16:26:35 -0800131 const size_t num_input_channels_;
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000132 // The audio is stored by DeinterleaveFrom() or CopyFrom() with processing
133 // format.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700134 const size_t proc_num_frames_;
Peter Kasting69558702016-01-12 16:26:35 -0800135 const size_t num_proc_channels_;
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000136 // The audio is returned by InterleaveTo() and CopyTo() with output samples
137 // per channels and the current number of channels. This last one can be
138 // changed at any time using set_num_channels().
Peter Kastingdce40cf2015-08-24 14:52:23 -0700139 const size_t output_num_frames_;
Peter Kasting69558702016-01-12 16:26:35 -0800140 size_t num_channels_;
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000141
Peter Kastingdce40cf2015-08-24 14:52:23 -0700142 size_t num_bands_;
143 size_t num_split_frames_;
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000144 bool mixed_low_pass_valid_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000145 bool reference_copied_;
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000146 AudioFrame::VADActivity activity_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000147
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000148 const float* keyboard_data_;
kwiberg88788ad2016-02-19 07:04:49 -0800149 std::unique_ptr<IFChannelBuffer> data_;
150 std::unique_ptr<IFChannelBuffer> split_data_;
151 std::unique_ptr<SplittingFilter> splitting_filter_;
Yves Gerey665174f2018-06-19 15:03:05 +0200152 std::unique_ptr<ChannelBuffer<int16_t>> mixed_low_pass_channels_;
153 std::unique_ptr<ChannelBuffer<int16_t>> low_pass_reference_channels_;
kwiberg88788ad2016-02-19 07:04:49 -0800154 std::unique_ptr<IFChannelBuffer> input_buffer_;
155 std::unique_ptr<IFChannelBuffer> output_buffer_;
Yves Gerey665174f2018-06-19 15:03:05 +0200156 std::unique_ptr<ChannelBuffer<float>> process_buffer_;
kwiberg4a206a92016-03-31 10:24:26 -0700157 std::vector<std::unique_ptr<PushSincResampler>> input_resamplers_;
158 std::vector<std::unique_ptr<PushSincResampler>> output_resamplers_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000159};
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000160
niklase@google.com470e71d2011-07-07 08:21:25 +0000161} // namespace webrtc
162
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200163#endif // MODULES_AUDIO_PROCESSING_AUDIO_BUFFER_H_