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 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 // The constructor below will be deprecated. 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 // Specify that downmixing should be done by selecting a single channel. 55 void set_downmixing_to_specific_channel(size_t channel); 56 57 // Specify that downmixing should be done by averaging all channels,. 58 void set_downmixing_by_averaging(); 59 60 // Set the number of channels in the buffer. The specified number of channels 61 // cannot be larger than the specified buffer_num_channels. The number is also 62 // reset at each call to CopyFrom or InterleaveFrom. 63 void set_num_channels(size_t num_channels); 64 num_channels()65 size_t num_channels() const { return num_channels_; } num_frames()66 size_t num_frames() const { return buffer_num_frames_; } num_frames_per_band()67 size_t num_frames_per_band() const { return num_split_frames_; } num_bands()68 size_t num_bands() const { return num_bands_; } 69 70 // Returns pointer arrays to the full-band channels. 71 // Usage: 72 // channels()[channel][sample]. 73 // Where: 74 // 0 <= channel < |buffer_num_channels_| 75 // 0 <= sample < |buffer_num_frames_| channels()76 float* const* channels() { return data_->channels(); } channels_const()77 const float* const* channels_const() const { return data_->channels(); } 78 79 // Returns pointer arrays to the bands for a specific channel. 80 // Usage: 81 // split_bands(channel)[band][sample]. 82 // Where: 83 // 0 <= channel < |buffer_num_channels_| 84 // 0 <= band < |num_bands_| 85 // 0 <= sample < |num_split_frames_| split_bands_const(size_t channel)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 } split_bands(size_t channel)90 float* const* split_bands(size_t channel) { 91 return split_data_.get() ? split_data_->bands(channel) 92 : data_->bands(channel); 93 } 94 95 // Returns a pointer array to the channels for a specific band. 96 // Usage: 97 // split_channels(band)[channel][sample]. 98 // Where: 99 // 0 <= band < |num_bands_| 100 // 0 <= channel < |buffer_num_channels_| 101 // 0 <= sample < |num_split_frames_| split_channels_const(Band band)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 // Copies data into the buffer. 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 // Copies data from the buffer. 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 // Splits the buffer data into frequency bands. 123 void SplitIntoFrequencyBands(); 124 125 // Recombines the frequency bands into a full-band signal. 126 void MergeFrequencyBands(); 127 128 // Copies the split bands data into the integer two-dimensional array. 129 void ExportSplitChannelData(size_t channel, 130 int16_t* const* split_band_data) const; 131 132 // Copies the data in the integer two-dimensional array into the split_bands 133 // data. 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 // Deprecated methods, will be removed soon. channels_f()141 float* const* channels_f() { return channels(); } channels_const_f()142 const float* const* channels_const_f() const { return channels_const(); } split_bands_const_f(size_t channel)143 const float* const* split_bands_const_f(size_t channel) const { 144 return split_bands_const(channel); 145 } split_bands_f(size_t channel)146 float* const* split_bands_f(size_t channel) { return split_bands(channel); } split_channels_const_f(Band band)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 } // namespace webrtc 177 178 #endif // MODULES_AUDIO_PROCESSING_AUDIO_BUFFER_H_ 179