block_buffer.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright (c) 2017 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. #ifndef MODULES_AUDIO_PROCESSING_AEC3_BLOCK_BUFFER_H_
  11. #define MODULES_AUDIO_PROCESSING_AEC3_BLOCK_BUFFER_H_
  12. #include <stddef.h>
  13. #include <vector>
  14. #include "rtc_base/checks.h"
  15. namespace webrtc {
  16. // Struct for bundling a circular buffer of two dimensional vector objects
  17. // together with the read and write indices.
  18. struct BlockBuffer {
  19. BlockBuffer(size_t size,
  20. size_t num_bands,
  21. size_t num_channels,
  22. size_t frame_length);
  23. ~BlockBuffer();
  24. int IncIndex(int index) const {
  25. RTC_DCHECK_EQ(buffer.size(), static_cast<size_t>(size));
  26. return index < size - 1 ? index + 1 : 0;
  27. }
  28. int DecIndex(int index) const {
  29. RTC_DCHECK_EQ(buffer.size(), static_cast<size_t>(size));
  30. return index > 0 ? index - 1 : size - 1;
  31. }
  32. int OffsetIndex(int index, int offset) const {
  33. RTC_DCHECK_EQ(buffer.size(), static_cast<size_t>(size));
  34. RTC_DCHECK_GE(size, offset);
  35. return (size + index + offset) % size;
  36. }
  37. void UpdateWriteIndex(int offset) { write = OffsetIndex(write, offset); }
  38. void IncWriteIndex() { write = IncIndex(write); }
  39. void DecWriteIndex() { write = DecIndex(write); }
  40. void UpdateReadIndex(int offset) { read = OffsetIndex(read, offset); }
  41. void IncReadIndex() { read = IncIndex(read); }
  42. void DecReadIndex() { read = DecIndex(read); }
  43. const int size;
  44. std::vector<std::vector<std::vector<std::vector<float>>>> buffer;
  45. int write = 0;
  46. int read = 0;
  47. };
  48. } // namespace webrtc
  49. #endif // MODULES_AUDIO_PROCESSING_AEC3_BLOCK_BUFFER_H_