tor-browser

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

vad_circular_buffer_unittest.cc (4738B)


      1 /*
      2 *  Copyright (c) 2012 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 #include "modules/audio_processing/vad/vad_circular_buffer.h"
     12 
     13 #include <memory>
     14 
     15 #include "test/gtest.h"
     16 
     17 namespace webrtc {
     18 
     19 static const int kWidthThreshold = 7;
     20 static const double kValThreshold = 1.0;
     21 static const int kLongBuffSize = 100;
     22 static const int kShortBuffSize = 10;
     23 
     24 static void InsertSequentially(int k, VadCircularBuffer* circular_buffer) {
     25  double mean_val;
     26  for (int n = 1; n <= k; n++) {
     27    EXPECT_TRUE(!circular_buffer->is_full());
     28    circular_buffer->Insert(n);
     29    mean_val = circular_buffer->Mean();
     30    EXPECT_EQ((n + 1.0) / 2., mean_val);
     31  }
     32 }
     33 
     34 static void Insert(double value,
     35                   int num_insertion,
     36                   VadCircularBuffer* circular_buffer) {
     37  for (int n = 0; n < num_insertion; n++)
     38    circular_buffer->Insert(value);
     39 }
     40 
     41 static void InsertZeros(int num_zeros, VadCircularBuffer* circular_buffer) {
     42  Insert(0.0, num_zeros, circular_buffer);
     43 }
     44 
     45 TEST(VadCircularBufferTest, GeneralTest) {
     46  std::unique_ptr<VadCircularBuffer> circular_buffer(
     47      VadCircularBuffer::Create(kShortBuffSize));
     48  double mean_val;
     49 
     50  // Mean should return zero if nothing is inserted.
     51  mean_val = circular_buffer->Mean();
     52  EXPECT_DOUBLE_EQ(0.0, mean_val);
     53  InsertSequentially(kShortBuffSize, circular_buffer.get());
     54 
     55  // Should be full.
     56  EXPECT_TRUE(circular_buffer->is_full());
     57  // Correct update after being full.
     58  for (int n = 1; n < kShortBuffSize; n++) {
     59    circular_buffer->Insert(n);
     60    mean_val = circular_buffer->Mean();
     61    EXPECT_DOUBLE_EQ((kShortBuffSize + 1.) / 2., mean_val);
     62    EXPECT_TRUE(circular_buffer->is_full());
     63  }
     64 
     65  // Check reset. This should be like starting fresh.
     66  circular_buffer->Reset();
     67  mean_val = circular_buffer->Mean();
     68  EXPECT_DOUBLE_EQ(0, mean_val);
     69  InsertSequentially(kShortBuffSize, circular_buffer.get());
     70  EXPECT_TRUE(circular_buffer->is_full());
     71 }
     72 
     73 TEST(VadCircularBufferTest, TransientsRemoval) {
     74  std::unique_ptr<VadCircularBuffer> circular_buffer(
     75      VadCircularBuffer::Create(kLongBuffSize));
     76  // Let the first transient be in wrap-around.
     77  InsertZeros(kLongBuffSize - kWidthThreshold / 2, circular_buffer.get());
     78 
     79  double push_val = kValThreshold;
     80  double mean_val;
     81  for (int k = kWidthThreshold; k >= 1; k--) {
     82    Insert(push_val, k, circular_buffer.get());
     83    circular_buffer->Insert(0);
     84    mean_val = circular_buffer->Mean();
     85    EXPECT_DOUBLE_EQ(k * push_val / kLongBuffSize, mean_val);
     86    circular_buffer->RemoveTransient(kWidthThreshold, kValThreshold);
     87    mean_val = circular_buffer->Mean();
     88    EXPECT_DOUBLE_EQ(0, mean_val);
     89  }
     90 }
     91 
     92 TEST(VadCircularBufferTest, TransientDetection) {
     93  std::unique_ptr<VadCircularBuffer> circular_buffer(
     94      VadCircularBuffer::Create(kLongBuffSize));
     95  // Let the first transient be in wrap-around.
     96  int num_insertion = kLongBuffSize - kWidthThreshold / 2;
     97  InsertZeros(num_insertion, circular_buffer.get());
     98 
     99  double push_val = 2;
    100  // This is longer than a transient and shouldn't be removed.
    101  int num_non_zero_elements = kWidthThreshold + 1;
    102  Insert(push_val, num_non_zero_elements, circular_buffer.get());
    103 
    104  double mean_val = circular_buffer->Mean();
    105  EXPECT_DOUBLE_EQ(num_non_zero_elements * push_val / kLongBuffSize, mean_val);
    106  circular_buffer->Insert(0);
    107  EXPECT_EQ(0,
    108            circular_buffer->RemoveTransient(kWidthThreshold, kValThreshold));
    109  mean_val = circular_buffer->Mean();
    110  EXPECT_DOUBLE_EQ(num_non_zero_elements * push_val / kLongBuffSize, mean_val);
    111 
    112  // A transient right after a non-transient, should be removed and mean is
    113  // not changed.
    114  num_insertion = 3;
    115  Insert(push_val, num_insertion, circular_buffer.get());
    116  circular_buffer->Insert(0);
    117  EXPECT_EQ(0,
    118            circular_buffer->RemoveTransient(kWidthThreshold, kValThreshold));
    119  mean_val = circular_buffer->Mean();
    120  EXPECT_DOUBLE_EQ(num_non_zero_elements * push_val / kLongBuffSize, mean_val);
    121 
    122  // Last input is larger than threshold, although the sequence is short but
    123  // it shouldn't be considered transient.
    124  Insert(push_val, num_insertion, circular_buffer.get());
    125  num_non_zero_elements += num_insertion;
    126  EXPECT_EQ(0,
    127            circular_buffer->RemoveTransient(kWidthThreshold, kValThreshold));
    128  mean_val = circular_buffer->Mean();
    129  EXPECT_DOUBLE_EQ(num_non_zero_elements * push_val / kLongBuffSize, mean_val);
    130 }
    131 
    132 }  // namespace webrtc