render_buffer.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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_RENDER_BUFFER_H_
  11. #define MODULES_AUDIO_PROCESSING_AEC3_RENDER_BUFFER_H_
  12. #include <stddef.h>
  13. #include <array>
  14. #include <vector>
  15. #include "api/array_view.h"
  16. #include "modules/audio_processing/aec3/aec3_common.h"
  17. #include "modules/audio_processing/aec3/block_buffer.h"
  18. #include "modules/audio_processing/aec3/fft_buffer.h"
  19. #include "modules/audio_processing/aec3/fft_data.h"
  20. #include "modules/audio_processing/aec3/spectrum_buffer.h"
  21. #include "rtc_base/checks.h"
  22. namespace webrtc {
  23. // Provides a buffer of the render data for the echo remover.
  24. class RenderBuffer {
  25. public:
  26. RenderBuffer(BlockBuffer* block_buffer,
  27. SpectrumBuffer* spectrum_buffer,
  28. FftBuffer* fft_buffer);
  29. RenderBuffer() = delete;
  30. RenderBuffer(const RenderBuffer&) = delete;
  31. RenderBuffer& operator=(const RenderBuffer&) = delete;
  32. ~RenderBuffer();
  33. // Get a block.
  34. const std::vector<std::vector<std::vector<float>>>& Block(
  35. int buffer_offset_blocks) const {
  36. int position =
  37. block_buffer_->OffsetIndex(block_buffer_->read, buffer_offset_blocks);
  38. return block_buffer_->buffer[position];
  39. }
  40. // Get the spectrum from one of the FFTs in the buffer.
  41. rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> Spectrum(
  42. int buffer_offset_ffts) const {
  43. int position = spectrum_buffer_->OffsetIndex(spectrum_buffer_->read,
  44. buffer_offset_ffts);
  45. return spectrum_buffer_->buffer[position];
  46. }
  47. // Returns the circular fft buffer.
  48. rtc::ArrayView<const std::vector<FftData>> GetFftBuffer() const {
  49. return fft_buffer_->buffer;
  50. }
  51. // Returns the current position in the circular buffer.
  52. size_t Position() const {
  53. RTC_DCHECK_EQ(spectrum_buffer_->read, fft_buffer_->read);
  54. RTC_DCHECK_EQ(spectrum_buffer_->write, fft_buffer_->write);
  55. return fft_buffer_->read;
  56. }
  57. // Returns the sum of the spectrums for a certain number of FFTs.
  58. void SpectralSum(size_t num_spectra,
  59. std::array<float, kFftLengthBy2Plus1>* X2) const;
  60. // Returns the sums of the spectrums for two numbers of FFTs.
  61. void SpectralSums(size_t num_spectra_shorter,
  62. size_t num_spectra_longer,
  63. std::array<float, kFftLengthBy2Plus1>* X2_shorter,
  64. std::array<float, kFftLengthBy2Plus1>* X2_longer) const;
  65. // Gets the recent activity seen in the render signal.
  66. bool GetRenderActivity() const { return render_activity_; }
  67. // Specifies the recent activity seen in the render signal.
  68. void SetRenderActivity(bool activity) { render_activity_ = activity; }
  69. // Returns the headroom between the write and the read positions in the
  70. // buffer.
  71. int Headroom() const {
  72. // The write and read indices are decreased over time.
  73. int headroom =
  74. fft_buffer_->write < fft_buffer_->read
  75. ? fft_buffer_->read - fft_buffer_->write
  76. : fft_buffer_->size - fft_buffer_->write + fft_buffer_->read;
  77. RTC_DCHECK_LE(0, headroom);
  78. RTC_DCHECK_GE(fft_buffer_->size, headroom);
  79. return headroom;
  80. }
  81. // Returns a reference to the spectrum buffer.
  82. const SpectrumBuffer& GetSpectrumBuffer() const { return *spectrum_buffer_; }
  83. // Returns a reference to the block buffer.
  84. const BlockBuffer& GetBlockBuffer() const { return *block_buffer_; }
  85. private:
  86. const BlockBuffer* const block_buffer_;
  87. const SpectrumBuffer* const spectrum_buffer_;
  88. const FftBuffer* const fft_buffer_;
  89. bool render_activity_ = false;
  90. };
  91. } // namespace webrtc
  92. #endif // MODULES_AUDIO_PROCESSING_AEC3_RENDER_BUFFER_H_