blob: eb45fb2a7a33b50d1511f259d28883ebbfd1f587 [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
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000014#include <vector>
15
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000016#include "webrtc/base/scoped_ptr.h"
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +000017#include "webrtc/common_audio/include/audio_util.h"
kjellander@webrtc.org035e9122015-01-28 19:57:00 +000018#include "webrtc/common_audio/channel_buffer.h"
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000019#include "webrtc/modules/audio_processing/include/audio_processing.h"
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +000020#include "webrtc/modules/audio_processing/splitting_filter.h"
pbos@webrtc.org7fad4b82013-05-28 08:11:59 +000021#include "webrtc/modules/interface/module_common_types.h"
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000022#include "webrtc/system_wrappers/interface/scoped_vector.h"
pbos@webrtc.org7fad4b82013-05-28 08:11:59 +000023#include "webrtc/typedefs.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000024
niklase@google.com470e71d2011-07-07 08:21:25 +000025namespace webrtc {
26
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000027class PushSincResampler;
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +000028class IFChannelBuffer;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000029
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +000030enum Band {
31 kBand0To8kHz = 0,
32 kBand8To16kHz = 1,
33 kBand16To24kHz = 2
34};
35
niklase@google.com470e71d2011-07-07 08:21:25 +000036class AudioBuffer {
37 public:
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000038 // TODO(ajm): Switch to take ChannelLayouts.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000039 AudioBuffer(int input_num_frames,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000040 int num_input_channels,
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000041 int process_num_frames,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000042 int num_process_channels,
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000043 int output_num_frames);
niklase@google.com470e71d2011-07-07 08:21:25 +000044 virtual ~AudioBuffer();
45
andrew@webrtc.orged083d42011-09-19 15:28:51 +000046 int num_channels() const;
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +000047 void set_num_channels(int num_channels);
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000048 int num_frames() const;
49 int num_frames_per_band() const;
50 int num_keyboard_frames() const;
aluebs@webrtc.orgc5ebbd92014-12-10 19:30:57 +000051 int num_bands() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000052
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +000053 // Returns a pointer array to the full-band channels.
54 // Usage:
55 // channels()[channel][sample].
56 // Where:
57 // 0 <= channel < |num_proc_channels_|
58 // 0 <= sample < |proc_num_frames_|
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +000059 int16_t* const* channels();
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +000060 const int16_t* const* channels_const() const;
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +000061 float* const* channels_f();
62 const float* const* channels_const_f() const;
63
64 // Returns a pointer array to the bands for a specific channel.
65 // Usage:
66 // split_bands(channel)[band][sample].
67 // Where:
68 // 0 <= channel < |num_proc_channels_|
69 // 0 <= band < |num_bands_|
70 // 0 <= sample < |num_split_frames_|
aluebs@webrtc.orgc5ebbd92014-12-10 19:30:57 +000071 int16_t* const* split_bands(int channel);
72 const int16_t* const* split_bands_const(int channel) const;
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +000073 float* const* split_bands_f(int channel);
74 const float* const* split_bands_const_f(int channel) const;
75
76 // Returns a pointer array to the channels for a specific band.
77 // Usage:
78 // split_channels(band)[channel][sample].
79 // Where:
80 // 0 <= band < |num_bands_|
81 // 0 <= channel < |num_proc_channels_|
82 // 0 <= sample < |num_split_frames_|
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +000083 int16_t* const* split_channels(Band band);
84 const int16_t* const* split_channels_const(Band band) const;
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +000085 float* const* split_channels_f(Band band);
86 const float* const* split_channels_const_f(Band band) const;
87
88 // Returns a pointer to the ChannelBuffer that encapsulates the full-band
89 // data.
90 ChannelBuffer<int16_t>* data();
91 const ChannelBuffer<int16_t>* data() const;
92 ChannelBuffer<float>* data_f();
93 const ChannelBuffer<float>* data_f() const;
94
95 // Returns a pointer to the ChannelBuffer that encapsulates the split data.
96 ChannelBuffer<int16_t>* split_data();
97 const ChannelBuffer<int16_t>* split_data() const;
98 ChannelBuffer<float>* split_data_f();
99 const ChannelBuffer<float>* split_data_f() const;
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000100
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000101 // Returns a pointer to the low-pass data downmixed to mono. If this data
102 // isn't already available it re-calculates it.
103 const int16_t* mixed_low_pass_data();
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000104 const int16_t* low_pass_reference(int channel) const;
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000105
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000106 const float* keyboard_data() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000107
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000108 void set_activity(AudioFrame::VADActivity activity);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000109 AudioFrame::VADActivity activity() const;
110
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000111 // Use for int16 interleaved data.
niklase@google.com470e71d2011-07-07 08:21:25 +0000112 void DeinterleaveFrom(AudioFrame* audioFrame);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000113 // If |data_changed| is false, only the non-audio data members will be copied
114 // to |frame|.
115 void InterleaveTo(AudioFrame* frame, bool data_changed) const;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000116
117 // Use for float deinterleaved data.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000118 void CopyFrom(const float* const* data,
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000119 int num_frames,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000120 AudioProcessing::ChannelLayout layout);
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000121 void CopyTo(int num_frames,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000122 AudioProcessing::ChannelLayout layout,
123 float* const* data);
niklase@google.com470e71d2011-07-07 08:21:25 +0000124 void CopyLowPassToReference();
125
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000126 // Splits the signal into different bands.
127 void SplitIntoFrequencyBands();
128 // Recombine the different bands into one signal.
129 void MergeFrequencyBands();
130
niklase@google.com470e71d2011-07-07 08:21:25 +0000131 private:
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000132 // Called from DeinterleaveFrom() and CopyFrom().
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000133 void InitForNewData();
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000134
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000135 // The audio is passed into DeinterleaveFrom() or CopyFrom() with input
136 // format (samples per channel and number of channels).
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000137 const int input_num_frames_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000138 const int num_input_channels_;
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000139 // The audio is stored by DeinterleaveFrom() or CopyFrom() with processing
140 // format.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000141 const int proc_num_frames_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000142 const int num_proc_channels_;
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000143 // The audio is returned by InterleaveTo() and CopyTo() with output samples
144 // per channels and the current number of channels. This last one can be
145 // changed at any time using set_num_channels().
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000146 const int output_num_frames_;
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000147 int num_channels_;
148
aluebs@webrtc.orgc5ebbd92014-12-10 19:30:57 +0000149 int num_bands_;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000150 int num_split_frames_;
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000151 bool mixed_low_pass_valid_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000152 bool reference_copied_;
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000153 AudioFrame::VADActivity activity_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000154
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000155 const float* keyboard_data_;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000156 rtc::scoped_ptr<IFChannelBuffer> data_;
157 rtc::scoped_ptr<IFChannelBuffer> split_data_;
158 rtc::scoped_ptr<SplittingFilter> splitting_filter_;
159 rtc::scoped_ptr<ChannelBuffer<int16_t> > mixed_low_pass_channels_;
160 rtc::scoped_ptr<ChannelBuffer<int16_t> > low_pass_reference_channels_;
161 rtc::scoped_ptr<ChannelBuffer<float> > input_buffer_;
162 rtc::scoped_ptr<ChannelBuffer<float> > process_buffer_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000163 ScopedVector<PushSincResampler> input_resamplers_;
164 ScopedVector<PushSincResampler> output_resamplers_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000165};
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000166
niklase@google.com470e71d2011-07-07 08:21:25 +0000167} // namespace webrtc
168
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000169#endif // WEBRTC_MODULES_AUDIO_PROCESSING_AUDIO_BUFFER_H_