blob: a526ca07c86baa354f251e2c0bb60a89986bba91 [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
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +000016#include "webrtc/common_audio/include/audio_util.h"
aluebs@webrtc.org87893762014-11-27 23:40:25 +000017#include "webrtc/modules/audio_processing/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"
pbos@webrtc.org7fad4b82013-05-28 08:11:59 +000020#include "webrtc/modules/interface/module_common_types.h"
21#include "webrtc/system_wrappers/interface/scoped_ptr.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.orgc5ebbd92014-12-10 19:30:57 +000030static const int kMaxNumBands = 3;
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +000031enum Band {
32 kBand0To8kHz = 0,
33 kBand8To16kHz = 1,
34 kBand16To24kHz = 2
35};
36
niklase@google.com470e71d2011-07-07 08:21:25 +000037class AudioBuffer {
38 public:
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000039 // TODO(ajm): Switch to take ChannelLayouts.
40 AudioBuffer(int input_samples_per_channel,
41 int num_input_channels,
42 int process_samples_per_channel,
43 int num_process_channels,
44 int output_samples_per_channel);
niklase@google.com470e71d2011-07-07 08:21:25 +000045 virtual ~AudioBuffer();
46
andrew@webrtc.orged083d42011-09-19 15:28:51 +000047 int num_channels() const;
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +000048 void set_num_channels(int num_channels);
andrew@webrtc.orged083d42011-09-19 15:28:51 +000049 int samples_per_channel() const;
50 int samples_per_split_channel() const;
andrew@webrtc.org103657b2014-04-24 18:28:56 +000051 int samples_per_keyboard_channel() const;
aluebs@webrtc.orgc5ebbd92014-12-10 19:30:57 +000052 int num_bands() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000053
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +000054 // Sample array accessors. Channels are guaranteed to be stored contiguously
55 // in memory. Prefer to use the const variants of each accessor when
56 // possible, since they incur less float<->int16 conversion overhead.
andrew@webrtc.org65f93382014-04-30 16:44:13 +000057 int16_t* data(int channel);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +000058 const int16_t* data_const(int channel) const;
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.orgc5ebbd92014-12-10 19:30:57 +000061 int16_t* const* split_bands(int channel);
62 const int16_t* const* split_bands_const(int channel) const;
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +000063 int16_t* const* split_channels(Band band);
64 const int16_t* const* split_channels_const(Band band) const;
65
aluebs@webrtc.org2561d522014-07-17 08:27:39 +000066 // Returns a pointer to the low-pass data downmixed to mono. If this data
67 // isn't already available it re-calculates it.
68 const int16_t* mixed_low_pass_data();
andrew@webrtc.org65f93382014-04-30 16:44:13 +000069 const int16_t* low_pass_reference(int channel) const;
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +000070
71 // Float versions of the accessors, with automatic conversion back and forth
72 // as necessary. The range of the numbers are the same as for int16_t.
73 float* data_f(int channel);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +000074 const float* data_const_f(int channel) const;
claguna@google.combfacaab2014-09-25 20:52:08 +000075 float* const* channels_f();
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +000076 const float* const* channels_const_f() const;
aluebs@webrtc.orgc5ebbd92014-12-10 19:30:57 +000077 float* const* split_bands_f(int channel);
78 const float* const* split_bands_const_f(int channel) const;
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +000079 float* const* split_channels_f(Band band);
80 const float* const* split_channels_const_f(Band band) const;
claguna@google.combfacaab2014-09-25 20:52:08 +000081
andrew@webrtc.org103657b2014-04-24 18:28:56 +000082 const float* keyboard_data() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000083
andrew@webrtc.orged083d42011-09-19 15:28:51 +000084 void set_activity(AudioFrame::VADActivity activity);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +000085 AudioFrame::VADActivity activity() const;
86
andrew@webrtc.org17e40642014-03-04 20:58:13 +000087 // Use for int16 interleaved data.
niklase@google.com470e71d2011-07-07 08:21:25 +000088 void DeinterleaveFrom(AudioFrame* audioFrame);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +000089 // If |data_changed| is false, only the non-audio data members will be copied
90 // to |frame|.
91 void InterleaveTo(AudioFrame* frame, bool data_changed) const;
andrew@webrtc.org17e40642014-03-04 20:58:13 +000092
93 // Use for float deinterleaved data.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000094 void CopyFrom(const float* const* data,
95 int samples_per_channel,
96 AudioProcessing::ChannelLayout layout);
97 void CopyTo(int samples_per_channel,
98 AudioProcessing::ChannelLayout layout,
99 float* const* data);
niklase@google.com470e71d2011-07-07 08:21:25 +0000100 void CopyLowPassToReference();
101
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000102 // Splits the signal into different bands.
103 void SplitIntoFrequencyBands();
104 // Recombine the different bands into one signal.
105 void MergeFrequencyBands();
106
niklase@google.com470e71d2011-07-07 08:21:25 +0000107 private:
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000108 // Called from DeinterleaveFrom() and CopyFrom().
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000109 void InitForNewData();
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000110
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000111 // The audio is passed into DeinterleaveFrom() or CopyFrom() with input
112 // format (samples per channel and number of channels).
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000113 const int input_samples_per_channel_;
114 const int num_input_channels_;
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000115 // The audio is stored by DeinterleaveFrom() or CopyFrom() with processing
116 // format.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000117 const int proc_samples_per_channel_;
118 const int num_proc_channels_;
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000119 // The audio is returned by InterleaveTo() and CopyTo() with output samples
120 // per channels and the current number of channels. This last one can be
121 // changed at any time using set_num_channels().
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000122 const int output_samples_per_channel_;
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000123 int num_channels_;
124
aluebs@webrtc.orgc5ebbd92014-12-10 19:30:57 +0000125 int num_bands_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000126 int samples_per_split_channel_;
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000127 bool mixed_low_pass_valid_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000128 bool reference_copied_;
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000129 AudioFrame::VADActivity activity_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000130
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000131 const float* keyboard_data_;
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000132 scoped_ptr<IFChannelBuffer> channels_;
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +0000133 ScopedVector<IFChannelBuffer> split_channels_;
aluebs@webrtc.orgc5ebbd92014-12-10 19:30:57 +0000134 scoped_ptr<int16_t*[]> bands_;
135 scoped_ptr<float*[]> bands_f_;
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000136 scoped_ptr<SplittingFilter> splitting_filter_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000137 scoped_ptr<ChannelBuffer<int16_t> > mixed_low_pass_channels_;
138 scoped_ptr<ChannelBuffer<int16_t> > low_pass_reference_channels_;
139 scoped_ptr<ChannelBuffer<float> > input_buffer_;
140 scoped_ptr<ChannelBuffer<float> > process_buffer_;
141 ScopedVector<PushSincResampler> input_resamplers_;
142 ScopedVector<PushSincResampler> output_resamplers_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000143};
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000144
niklase@google.com470e71d2011-07-07 08:21:25 +0000145} // namespace webrtc
146
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000147#endif // WEBRTC_MODULES_AUDIO_PROCESSING_AUDIO_BUFFER_H_