tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

render_buffer.h (4042B)


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