1 /* 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 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 // Stores any audio data in a way that allows the audio processing module to 31 // operate on it in a controlled manner. 32 class AudioBuffer { 33 public: 34 static const int kSplitBandSize = 160; 35 static const int 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 virtual ~AudioBuffer(); 44 45 AudioBuffer(const AudioBuffer&) = delete; 46 AudioBuffer& operator=(const AudioBuffer&) = delete; 47 48 // Specify that downmixing should be done by selecting a single channel. 49 void set_downmixing_to_specific_channel(size_t channel); 50 51 // Specify that downmixing should be done by averaging all channels,. 52 void set_downmixing_by_averaging(); 53 54 // Set the number of channels in the buffer. The specified number of channels 55 // cannot be larger than the specified buffer_num_channels. The number is also 56 // reset at each call to CopyFrom or InterleaveFrom. 57 void set_num_channels(size_t num_channels); 58 num_channels()59 size_t num_channels() const { return num_channels_; } num_frames()60 size_t num_frames() const { return buffer_num_frames_; } num_frames_per_band()61 size_t num_frames_per_band() const { return num_split_frames_; } num_bands()62 size_t num_bands() const { return num_bands_; } 63 64 // Returns pointer arrays to the full-band channels. 65 // Usage: 66 // channels()[channel][sample]. 67 // Where: 68 // 0 <= channel < `buffer_num_channels_` 69 // 0 <= sample < `buffer_num_frames_` channels()70 float* const* channels() { return data_->channels(); } channels_const()71 const float* const* channels_const() const { return data_->channels(); } 72 73 // Returns pointer arrays to the bands for a specific channel. 74 // Usage: 75 // split_bands(channel)[band][sample]. 76 // Where: 77 // 0 <= channel < `buffer_num_channels_` 78 // 0 <= band < `num_bands_` 79 // 0 <= sample < `num_split_frames_` split_bands_const(size_t channel)80 const float* const* split_bands_const(size_t channel) const { 81 return split_data_.get() ? split_data_->bands(channel) 82 : data_->bands(channel); 83 } split_bands(size_t channel)84 float* const* split_bands(size_t channel) { 85 return split_data_.get() ? split_data_->bands(channel) 86 : data_->bands(channel); 87 } 88 89 // Returns a pointer array to the channels for a specific band. 90 // Usage: 91 // split_channels(band)[channel][sample]. 92 // Where: 93 // 0 <= band < `num_bands_` 94 // 0 <= channel < `buffer_num_channels_` 95 // 0 <= sample < `num_split_frames_` split_channels_const(Band band)96 const float* const* split_channels_const(Band band) const { 97 if (split_data_.get()) { 98 return split_data_->channels(band); 99 } else { 100 return band == kBand0To8kHz ? data_->channels() : nullptr; 101 } 102 } 103 104 // Copies data into the buffer. 105 void CopyFrom(const int16_t* const interleaved_data, 106 const StreamConfig& stream_config); 107 void CopyFrom(const float* const* stacked_data, 108 const StreamConfig& stream_config); 109 110 // Copies data from the buffer. 111 void CopyTo(const StreamConfig& stream_config, 112 int16_t* const interleaved_data); 113 void CopyTo(const StreamConfig& stream_config, float* const* stacked_data); 114 void CopyTo(AudioBuffer* buffer) const; 115 116 // Splits the buffer data into frequency bands. 117 void SplitIntoFrequencyBands(); 118 119 // Recombines the frequency bands into a full-band signal. 120 void MergeFrequencyBands(); 121 122 // Copies the split bands data into the integer two-dimensional array. 123 void ExportSplitChannelData(size_t channel, 124 int16_t* const* split_band_data) const; 125 126 // Copies the data in the integer two-dimensional array into the split_bands 127 // data. 128 void ImportSplitChannelData(size_t channel, 129 const int16_t* const* split_band_data); 130 131 static const size_t kMaxSplitFrameLength = 160; 132 static const size_t kMaxNumBands = 3; 133 134 // Deprecated methods, will be removed soon. channels_f()135 float* const* channels_f() { return channels(); } channels_const_f()136 const float* const* channels_const_f() const { return channels_const(); } split_bands_const_f(size_t channel)137 const float* const* split_bands_const_f(size_t channel) const { 138 return split_bands_const(channel); 139 } split_bands_f(size_t channel)140 float* const* split_bands_f(size_t channel) { return split_bands(channel); } split_channels_const_f(Band band)141 const float* const* split_channels_const_f(Band band) const { 142 return split_channels_const(band); 143 } 144 145 private: 146 FRIEND_TEST_ALL_PREFIXES(AudioBufferTest, 147 SetNumChannelsSetsChannelBuffersNumChannels); 148 void RestoreNumChannels(); 149 150 const size_t input_num_frames_; 151 const size_t input_num_channels_; 152 const size_t buffer_num_frames_; 153 const size_t buffer_num_channels_; 154 const size_t output_num_frames_; 155 const size_t output_num_channels_; 156 157 size_t num_channels_; 158 size_t num_bands_; 159 size_t num_split_frames_; 160 161 std::unique_ptr<ChannelBuffer<float>> data_; 162 std::unique_ptr<ChannelBuffer<float>> split_data_; 163 std::unique_ptr<SplittingFilter> splitting_filter_; 164 std::vector<std::unique_ptr<PushSincResampler>> input_resamplers_; 165 std::vector<std::unique_ptr<PushSincResampler>> output_resamplers_; 166 bool downmix_by_averaging_ = true; 167 size_t channel_for_downmixing_ = 0; 168 }; 169 170 } // namespace webrtc 171 172 #endif // MODULES_AUDIO_PROCESSING_AUDIO_BUFFER_H_ 173