spectrum_buffer.h 2.0 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_SPECTRUM_BUFFER_H_
  11. #define MODULES_AUDIO_PROCESSING_AEC3_SPECTRUM_BUFFER_H_
  12. #include <stddef.h>
  13. #include <array>
  14. #include <vector>
  15. #include "modules/audio_processing/aec3/aec3_common.h"
  16. #include "rtc_base/checks.h"
  17. namespace webrtc {
  18. // Struct for bundling a circular buffer of one dimensional vector objects
  19. // together with the read and write indices.
  20. struct SpectrumBuffer {
  21. SpectrumBuffer(size_t size, size_t num_channels);
  22. ~SpectrumBuffer();
  23. int IncIndex(int index) const {
  24. RTC_DCHECK_EQ(buffer.size(), static_cast<size_t>(size));
  25. return index < size - 1 ? index + 1 : 0;
  26. }
  27. int DecIndex(int index) const {
  28. RTC_DCHECK_EQ(buffer.size(), static_cast<size_t>(size));
  29. return index > 0 ? index - 1 : size - 1;
  30. }
  31. int OffsetIndex(int index, int offset) const {
  32. RTC_DCHECK_GE(size, offset);
  33. RTC_DCHECK_EQ(buffer.size(), static_cast<size_t>(size));
  34. RTC_DCHECK_GE(size + index + offset, 0);
  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::array<float, kFftLengthBy2Plus1>>> buffer;
  45. int write = 0;
  46. int read = 0;
  47. };
  48. } // namespace webrtc
  49. #endif // MODULES_AUDIO_PROCESSING_AEC3_SPECTRUM_BUFFER_H_