tor-browser

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

decimator.cc (3555B)


      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 #include "modules/audio_processing/aec3/decimator.h"
     11 
     12 #include <array>
     13 #include <cstddef>
     14 
     15 #include "api/array_view.h"
     16 #include "modules/audio_processing/aec3/aec3_common.h"
     17 #include "modules/audio_processing/utility/cascaded_biquad_filter.h"
     18 #include "rtc_base/checks.h"
     19 
     20 namespace webrtc {
     21 namespace {
     22 // signal.ellip(6, 1, 40, 1800/8000, 'lowpass', output='sos')
     23 constexpr std::array<CascadedBiQuadFilter::BiQuadCoefficients, 3>
     24    kLowPassFilterDs4 = {{
     25        {.b = {0.0180919877f, 0.00320961363f, 0.0180919877f},
     26         .a = {-1.5183195f, 0.633165865f}},
     27        {.b = {1.0f, -1.24550459f, 1.0f}, .a = {-1.49784254f, 0.853586692f}},
     28        {.b = {1.0f, -1.4221681f, 1.0f}, .a = {-1.49791282f, 0.969572384f}},
     29    }};
     30 
     31 // signal.cheby1(1, 6, [1000/8000, 2000/8000], 'bandpass', output='sos')
     32 // repeated 5 times.
     33 constexpr std::array<CascadedBiQuadFilter::BiQuadCoefficients, 5>
     34    kBandPassFilterDs8 = {{
     35        {.b = {0.103304783f, 0.0f, -0.103304783f},
     36         .a = {-1.520363f, 0.793390435f}},
     37        {.b = {0.103304783f, 0.0f, -0.103304783f},
     38         .a = {-1.520363f, 0.793390435f}},
     39        {.b = {0.103304783f, 0.0f, -0.103304783f},
     40         .a = {-1.520363f, 0.793390435f}},
     41        {.b = {0.103304783f, 0.0f, -0.103304783f},
     42         .a = {-1.520363f, 0.793390435f}},
     43        {.b = {0.103304783f, 0.0f, -0.103304783f},
     44         .a = {-1.520363f, 0.793390435f}},
     45    }};
     46 
     47 // signal.butter(2, 1000/8000.0, 'highpass', output='sos')
     48 constexpr std::array<CascadedBiQuadFilter::BiQuadCoefficients, 1>
     49    kHighPassFilter = {{
     50        {.b = {0.757076375f, -1.51415275f, 0.757076375f},
     51         .a = {-1.45424359f, 0.574061915f}},
     52    }};
     53 
     54 constexpr std::array<CascadedBiQuadFilter::BiQuadCoefficients, 0>
     55    kPassThroughFilter = {{}};
     56 }  // namespace
     57 
     58 Decimator::Decimator(size_t down_sampling_factor)
     59    : down_sampling_factor_(down_sampling_factor),
     60      anti_aliasing_filter_(
     61          down_sampling_factor_ == 4
     62              ? ArrayView<const CascadedBiQuadFilter::BiQuadCoefficients>(
     63                    kLowPassFilterDs4)
     64              : ArrayView<const CascadedBiQuadFilter::BiQuadCoefficients>(
     65                    kBandPassFilterDs8)),
     66      noise_reduction_filter_(
     67          down_sampling_factor_ == 8
     68              ? (ArrayView<const CascadedBiQuadFilter::BiQuadCoefficients>(
     69                    kPassThroughFilter))
     70              : (ArrayView<const CascadedBiQuadFilter::BiQuadCoefficients>(
     71                    kHighPassFilter))) {
     72  RTC_DCHECK(down_sampling_factor_ == 4 || down_sampling_factor_ == 8);
     73 }
     74 
     75 void Decimator::Decimate(ArrayView<const float> in, ArrayView<float> out) {
     76  RTC_DCHECK_EQ(kBlockSize, in.size());
     77  RTC_DCHECK_EQ(kBlockSize / down_sampling_factor_, out.size());
     78  std::array<float, kBlockSize> x;
     79 
     80  // Limit the frequency content of the signal to avoid aliasing.
     81  anti_aliasing_filter_.Process(in, x);
     82 
     83  // Reduce the impact of near-end noise.
     84  noise_reduction_filter_.Process(x);
     85 
     86  // Downsample the signal.
     87  for (size_t j = 0, k = 0; j < out.size(); ++j, k += down_sampling_factor_) {
     88    RTC_DCHECK_GT(kBlockSize, k);
     89    out[j] = x[k];
     90  }
     91 }
     92 
     93 }  // namespace webrtc