block.h (2870B)
1 /* 2 * Copyright (c) 2022 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_AEC3_BLOCK_H_ 12 #define MODULES_AUDIO_PROCESSING_AEC3_BLOCK_H_ 13 14 #include <algorithm> 15 #include <utility> 16 #include <vector> 17 18 #include "api/array_view.h" 19 #include "modules/audio_processing/aec3/aec3_common.h" 20 21 namespace webrtc { 22 23 // Contains one or more channels of 4 milliseconds of audio data. 24 // The audio is split in one or more frequency bands, each with a sampling 25 // rate of 16 kHz. 26 class Block { 27 public: 28 Block(int num_bands, int num_channels, float default_value = 0.0f) 29 : num_bands_(num_bands), 30 num_channels_(num_channels), 31 data_(num_bands * num_channels * kBlockSize, default_value) {} 32 33 // Returns the number of bands. 34 int NumBands() const { return num_bands_; } 35 36 // Returns the number of channels. 37 int NumChannels() const { return num_channels_; } 38 39 // Modifies the number of channels and sets all samples to zero. 40 void SetNumChannels(int num_channels) { 41 num_channels_ = num_channels; 42 data_.resize(num_bands_ * num_channels_ * kBlockSize); 43 std::fill(data_.begin(), data_.end(), 0.0f); 44 } 45 46 // Iterators for accessing the data. 47 auto begin(int band, int channel) { 48 return data_.begin() + GetIndex(band, channel); 49 } 50 51 auto begin(int band, int channel) const { 52 return data_.begin() + GetIndex(band, channel); 53 } 54 55 auto end(int band, int channel) { return begin(band, channel) + kBlockSize; } 56 57 auto end(int band, int channel) const { 58 return begin(band, channel) + kBlockSize; 59 } 60 61 // Access data via ArrayView. 62 ArrayView<float, kBlockSize> View(int band, int channel) { 63 return ArrayView<float, kBlockSize>(&data_[GetIndex(band, channel)], 64 kBlockSize); 65 } 66 67 ArrayView<const float, kBlockSize> View(int band, int channel) const { 68 return ArrayView<const float, kBlockSize>(&data_[GetIndex(band, channel)], 69 kBlockSize); 70 } 71 72 // Lets two Blocks swap audio data. 73 void Swap(Block& b) { 74 std::swap(num_bands_, b.num_bands_); 75 std::swap(num_channels_, b.num_channels_); 76 data_.swap(b.data_); 77 } 78 79 private: 80 // Returns the index of the first sample of the requested |band| and 81 // |channel|. 82 int GetIndex(int band, int channel) const { 83 return (band * num_channels_ + channel) * kBlockSize; 84 } 85 86 int num_bands_; 87 int num_channels_; 88 std::vector<float> data_; 89 }; 90 91 } // namespace webrtc 92 #endif // MODULES_AUDIO_PROCESSING_AEC3_BLOCK_H_