tor-browser

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

push_sinc_resampler.h (3361B)


      1 /*
      2 *  Copyright (c) 2013 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 COMMON_AUDIO_RESAMPLER_PUSH_SINC_RESAMPLER_H_
     12 #define COMMON_AUDIO_RESAMPLER_PUSH_SINC_RESAMPLER_H_
     13 
     14 #include <stddef.h>
     15 #include <stdint.h>
     16 
     17 #include <memory>
     18 
     19 #include "api/audio/audio_view.h"
     20 #include "common_audio/resampler/sinc_resampler.h"
     21 
     22 namespace webrtc {
     23 
     24 // A thin wrapper over SincResampler to provide a push-based interface as
     25 // required by WebRTC. SincResampler uses a pull-based interface, and will
     26 // use SincResamplerCallback::Run() to request data upon a call to Resample().
     27 // These Run() calls will happen on the same thread Resample() is called on.
     28 class PushSincResampler : public SincResamplerCallback {
     29 public:
     30  // Provide the size of the source and destination blocks in samples. These
     31  // must correspond to the same time duration (typically 10 ms) as the sample
     32  // ratio is inferred from them.
     33  PushSincResampler(size_t source_frames, size_t destination_frames);
     34  ~PushSincResampler() override;
     35 
     36  PushSincResampler(const PushSincResampler&) = delete;
     37  PushSincResampler& operator=(const PushSincResampler&) = delete;
     38 
     39  // Perform the resampling. `source_frames` must always equal the
     40  // `source_frames` provided at construction. `destination_capacity` must be
     41  // at least as large as `destination_frames`. Returns the number of samples
     42  // provided in destination (for convenience, since this will always be equal
     43  // to `destination_frames`).
     44  template <typename S, typename D>
     45  size_t Resample(const MonoView<S>& source, const MonoView<D>& destination) {
     46    return Resample(&source[0], SamplesPerChannel(source), &destination[0],
     47                    SamplesPerChannel(destination));
     48  }
     49 
     50  size_t Resample(const int16_t* source,
     51                  size_t source_frames,
     52                  int16_t* destination,
     53                  size_t destination_capacity);
     54  size_t Resample(const float* source,
     55                  size_t source_frames,
     56                  float* destination,
     57                  size_t destination_capacity);
     58 
     59  // Delay due to the filter kernel. Essentially, the time after which an input
     60  // sample will appear in the resampled output.
     61  static float AlgorithmicDelaySeconds(int source_rate_hz) {
     62    return 1.f / source_rate_hz * SincResampler::kKernelSize / 2;
     63  }
     64 
     65 protected:
     66  // Implements SincResamplerCallback.
     67  void Run(size_t frames, float* destination) override;
     68 
     69 private:
     70  friend class PushSincResamplerTest;
     71  SincResampler* get_resampler_for_testing() { return resampler_.get(); }
     72 
     73  std::unique_ptr<SincResampler> resampler_;
     74  std::unique_ptr<float[]> float_buffer_;
     75  const float* source_ptr_;
     76  const int16_t* source_ptr_int_;
     77  const size_t destination_frames_;
     78 
     79  // True on the first call to Resample(), to prime the SincResampler buffer.
     80  bool first_pass_;
     81 
     82  // Used to assert we are only requested for as much data as is available.
     83  size_t source_available_;
     84 };
     85 
     86 }  // namespace webrtc
     87 
     88 #endif  // COMMON_AUDIO_RESAMPLER_PUSH_SINC_RESAMPLER_H_