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