tor-browser

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

block_delay_buffer_unittest.cc (3591B)


      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 #include "modules/audio_processing/aec3/block_delay_buffer.h"
     12 
     13 #include <cstddef>
     14 #include <string>
     15 #include <tuple>
     16 
     17 #include "modules/audio_processing/aec3/aec3_common.h"
     18 #include "modules/audio_processing/audio_buffer.h"
     19 #include "rtc_base/strings/string_builder.h"
     20 #include "test/gtest.h"
     21 
     22 namespace webrtc {
     23 
     24 namespace {
     25 
     26 float SampleValue(size_t sample_index) {
     27  return sample_index % 32768;
     28 }
     29 
     30 // Populates the frame with linearly increasing sample values for each band.
     31 void PopulateInputFrame(size_t frame_length,
     32                        size_t num_bands,
     33                        size_t first_sample_index,
     34                        float* const* frame) {
     35  for (size_t k = 0; k < num_bands; ++k) {
     36    for (size_t i = 0; i < frame_length; ++i) {
     37      frame[k][i] = SampleValue(first_sample_index + i);
     38    }
     39  }
     40 }
     41 
     42 std::string ProduceDebugText(int sample_rate_hz, size_t delay) {
     43  char log_stream_buffer[8 * 1024];
     44  SimpleStringBuilder ss(log_stream_buffer);
     45  ss << "Sample rate: " << sample_rate_hz;
     46  ss << ", Delay: " << delay;
     47  return ss.str();
     48 }
     49 
     50 }  // namespace
     51 
     52 class BlockDelayBufferTest
     53    : public ::testing::Test,
     54      public ::testing::WithParamInterface<std::tuple<size_t, int, size_t>> {};
     55 
     56 INSTANTIATE_TEST_SUITE_P(
     57    ParameterCombinations,
     58    BlockDelayBufferTest,
     59    ::testing::Combine(::testing::Values(0, 1, 27, 160, 4321, 7021),
     60                       ::testing::Values(16000, 32000, 48000),
     61                       ::testing::Values(1, 2, 4)));
     62 
     63 // Verifies that the correct signal delay is achived.
     64 TEST_P(BlockDelayBufferTest, CorrectDelayApplied) {
     65  const size_t delay = std::get<0>(GetParam());
     66  const int rate = std::get<1>(GetParam());
     67  const size_t num_channels = std::get<2>(GetParam());
     68 
     69  SCOPED_TRACE(ProduceDebugText(rate, delay));
     70  size_t num_bands = NumBandsForRate(rate);
     71  size_t subband_frame_length = 160;
     72 
     73  BlockDelayBuffer delay_buffer(num_channels, num_bands, subband_frame_length,
     74                                delay);
     75 
     76  static constexpr size_t kNumFramesToProcess = 20;
     77  for (size_t frame_index = 0; frame_index < kNumFramesToProcess;
     78       ++frame_index) {
     79    AudioBuffer audio_buffer(rate, num_channels, rate, num_channels, rate,
     80                             num_channels);
     81    if (rate > 16000) {
     82      audio_buffer.SplitIntoFrequencyBands();
     83    }
     84    size_t first_sample_index = frame_index * subband_frame_length;
     85    for (size_t ch = 0; ch < num_channels; ++ch) {
     86      PopulateInputFrame(subband_frame_length, num_bands, first_sample_index,
     87                         &audio_buffer.split_bands(ch)[0]);
     88    }
     89    delay_buffer.DelaySignal(&audio_buffer);
     90 
     91    for (size_t ch = 0; ch < num_channels; ++ch) {
     92      for (size_t band = 0; band < num_bands; ++band) {
     93        size_t sample_index = first_sample_index;
     94        for (size_t i = 0; i < subband_frame_length; ++i, ++sample_index) {
     95          if (sample_index < delay) {
     96            EXPECT_EQ(0.f, audio_buffer.split_bands(ch)[band][i]);
     97          } else {
     98            EXPECT_EQ(SampleValue(sample_index - delay),
     99                      audio_buffer.split_bands(ch)[band][i]);
    100          }
    101        }
    102      }
    103    }
    104  }
    105 }
    106 
    107 }  // namespace webrtc