| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#ifndef MODULES_AUDIO_PROCESSING_AUDIO_BUFFER_H_ |
| 12 |
#define MODULES_AUDIO_PROCESSING_AUDIO_BUFFER_H_ |
| 13 |
|
| 14 |
#include <stddef.h> |
| 15 |
#include <stdint.h> |
| 16 |
|
| 17 |
#include <memory> |
| 18 |
#include <vector> |
| 19 |
|
| 20 |
#include "common_audio/channel_buffer.h" |
| 21 |
#include "modules/audio_processing/include/audio_processing.h" |
| 22 |
|
| 23 |
namespace webrtc { |
| 24 |
|
| 25 |
class PushSincResampler; |
| 26 |
class SplittingFilter; |
| 27 |
|
| 28 |
enum Band { kBand0To8kHz = 0, kBand8To16kHz = 1, kBand16To24kHz = 2 }; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
class AudioBuffer { |
| 33 |
public: |
| 34 |
static const int kSplitBandSize = 160; |
| 35 |
static const size_t kMaxSampleRate = 384000; |
| 36 |
AudioBuffer(size_t input_rate, |
| 37 |
size_t input_num_channels, |
| 38 |
size_t buffer_rate, |
| 39 |
size_t buffer_num_channels, |
| 40 |
size_t output_rate, |
| 41 |
size_t output_num_channels); |
| 42 |
|
| 43 |
|
| 44 |
AudioBuffer(size_t input_num_frames, |
| 45 |
size_t input_num_channels, |
| 46 |
size_t buffer_num_frames, |
| 47 |
size_t buffer_num_channels, |
| 48 |
size_t output_num_frames); |
| 49 |
virtual ~AudioBuffer(); |
| 50 |
|
| 51 |
AudioBuffer(const AudioBuffer&) = delete; |
| 52 |
AudioBuffer& operator=(const AudioBuffer&) = delete; |
| 53 |
|
| 54 |
|
| 55 |
void set_downmixing_to_specific_channel(size_t channel); |
| 56 |
|
| 57 |
|
| 58 |
void set_downmixing_by_averaging(); |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
void set_num_channels(size_t num_channels); |
| 64 |
|
| 65 |
size_t num_channels() const { return num_channels_; } |
| 66 |
size_t num_frames() const { return buffer_num_frames_; } |
| 67 |
size_t num_frames_per_band() const { return num_split_frames_; } |
| 68 |
size_t num_bands() const { return num_bands_; } |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
float* const* channels() { return data_->channels(); } |
| 77 |
const float* const* channels_const() const { return data_->channels(); } |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
const float* const* split_bands_const(size_t channel) const { |
| 87 |
return split_data_.get() ? split_data_->bands(channel) |
| 88 |
: data_->bands(channel); |
| 89 |
} |
| 90 |
float* const* split_bands(size_t channel) { |
| 91 |
return split_data_.get() ? split_data_->bands(channel) |
| 92 |
: data_->bands(channel); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
const float* const* split_channels_const(Band band) const { |
| 103 |
if (split_data_.get()) { |
| 104 |
return split_data_->channels(band); |
| 105 |
} else { |
| 106 |
return band == kBand0To8kHz ? data_->channels() : nullptr; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
void CopyFrom(const int16_t* const interleaved_data, |
| 112 |
const StreamConfig& stream_config); |
| 113 |
void CopyFrom(const float* const* stacked_data, |
| 114 |
const StreamConfig& stream_config); |
| 115 |
|
| 116 |
|
| 117 |
void CopyTo(const StreamConfig& stream_config, |
| 118 |
int16_t* const interleaved_data); |
| 119 |
void CopyTo(const StreamConfig& stream_config, float* const* stacked_data); |
| 120 |
void CopyTo(AudioBuffer* buffer) const; |
| 121 |
|
| 122 |
|
| 123 |
void SplitIntoFrequencyBands(); |
| 124 |
|
| 125 |
|
| 126 |
void MergeFrequencyBands(); |
| 127 |
|
| 128 |
|
| 129 |
void ExportSplitChannelData(size_t channel, |
| 130 |
int16_t* const* split_band_data) const; |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
void ImportSplitChannelData(size_t channel, |
| 135 |
const int16_t* const* split_band_data); |
| 136 |
|
| 137 |
static const size_t kMaxSplitFrameLength = 160; |
| 138 |
static const size_t kMaxNumBands = 3; |
| 139 |
|
| 140 |
|
| 141 |
float* const* channels_f() { return channels(); } |
| 142 |
const float* const* channels_const_f() const { return channels_const(); } |
| 143 |
const float* const* split_bands_const_f(size_t channel) const { |
| 144 |
return split_bands_const(channel); |
| 145 |
} |
| 146 |
float* const* split_bands_f(size_t channel) { return split_bands(channel); } |
| 147 |
const float* const* split_channels_const_f(Band band) const { |
| 148 |
return split_channels_const(band); |
| 149 |
} |
| 150 |
|
| 151 |
private: |
| 152 |
FRIEND_TEST_ALL_PREFIXES(AudioBufferTest, |
| 153 |
SetNumChannelsSetsChannelBuffersNumChannels); |
| 154 |
void RestoreNumChannels(); |
| 155 |
|
| 156 |
const size_t input_num_frames_; |
| 157 |
const size_t input_num_channels_; |
| 158 |
const size_t buffer_num_frames_; |
| 159 |
const size_t buffer_num_channels_; |
| 160 |
const size_t output_num_frames_; |
| 161 |
const size_t output_num_channels_; |
| 162 |
|
| 163 |
size_t num_channels_; |
| 164 |
size_t num_bands_; |
| 165 |
size_t num_split_frames_; |
| 166 |
|
| 167 |
std::unique_ptr<ChannelBuffer<float>> data_; |
| 168 |
std::unique_ptr<ChannelBuffer<float>> split_data_; |
| 169 |
std::unique_ptr<SplittingFilter> splitting_filter_; |
| 170 |
std::vector<std::unique_ptr<PushSincResampler>> input_resamplers_; |
| 171 |
std::vector<std::unique_ptr<PushSincResampler>> output_resamplers_; |
| 172 |
bool downmix_by_averaging_ = true; |
| 173 |
size_t channel_for_downmixing_ = 0; |
| 174 |
}; |
| 175 |
|
| 176 |
} |
| 177 |
|
| 178 |
#endif |