tor-browser

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

test_utils.h (4606B)


      1 /*
      2 *  Copyright (c) 2018 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_AGC2_RNN_VAD_TEST_UTILS_H_
     12 #define MODULES_AUDIO_PROCESSING_AGC2_RNN_VAD_TEST_UTILS_H_
     13 
     14 #include <array>
     15 #include <fstream>
     16 #include <ios>
     17 #include <limits>
     18 #include <memory>
     19 #include <string>
     20 
     21 #include "absl/strings/string_view.h"
     22 #include "api/array_view.h"
     23 #include "modules/audio_processing/agc2/rnn_vad/common.h"
     24 
     25 namespace webrtc {
     26 namespace rnn_vad {
     27 
     28 constexpr float kFloatMin = std::numeric_limits<float>::min();
     29 
     30 // Fails for every pair from two equally sized webrtc::ArrayView<float> views
     31 // such that the values in the pair do not match.
     32 void ExpectEqualFloatArray(ArrayView<const float> expected,
     33                           ArrayView<const float> computed);
     34 
     35 // Fails for every pair from two equally sized webrtc::ArrayView<float> views
     36 // such that their absolute error is above a given threshold.
     37 void ExpectNearAbsolute(ArrayView<const float> expected,
     38                        ArrayView<const float> computed,
     39                        float tolerance);
     40 
     41 // File reader interface.
     42 class FileReader {
     43 public:
     44  virtual ~FileReader() = default;
     45  // Number of values in the file.
     46  virtual int size() const = 0;
     47  // Reads `dst.size()` float values into `dst`, advances the internal file
     48  // position according to the number of read bytes and returns true if the
     49  // values are correctly read. If the number of remaining bytes in the file is
     50  // not sufficient to read `dst.size()` float values, `dst` is partially
     51  // modified and false is returned.
     52  virtual bool ReadChunk(ArrayView<float> dst) = 0;
     53  // Reads a single float value, advances the internal file position according
     54  // to the number of read bytes and returns true if the value is correctly
     55  // read. If the number of remaining bytes in the file is not sufficient to
     56  // read one float, `dst` is not modified and false is returned.
     57  virtual bool ReadValue(float& dst) = 0;
     58  // Advances the internal file position by `hop` float values.
     59  virtual void SeekForward(int hop) = 0;
     60  // Resets the internal file position to BOF.
     61  virtual void SeekBeginning() = 0;
     62 };
     63 
     64 // File reader for files that contain `num_chunks` chunks with size equal to
     65 // `chunk_size`.
     66 struct ChunksFileReader {
     67  const int chunk_size;
     68  const int num_chunks;
     69  std::unique_ptr<FileReader> reader;
     70 };
     71 
     72 // Creates a reader for the PCM S16 samples file.
     73 std::unique_ptr<FileReader> CreatePcmSamplesReader();
     74 
     75 // Creates a reader for the 24 kHz pitch buffer test data.
     76 ChunksFileReader CreatePitchBuffer24kHzReader();
     77 
     78 // Creates a reader for the LP residual and pitch information test data.
     79 ChunksFileReader CreateLpResidualAndPitchInfoReader();
     80 
     81 // Creates a reader for the sequence of GRU input vectors.
     82 std::unique_ptr<FileReader> CreateGruInputReader();
     83 
     84 // Creates a reader for the VAD probabilities test data.
     85 std::unique_ptr<FileReader> CreateVadProbsReader();
     86 
     87 // Class to retrieve a test pitch buffer content and the expected output for the
     88 // analysis steps.
     89 class PitchTestData {
     90 public:
     91  PitchTestData();
     92  ~PitchTestData();
     93  ArrayView<const float, kBufSize24kHz> PitchBuffer24kHzView() const {
     94    return pitch_buffer_24k_;
     95  }
     96  ArrayView<const float, kRefineNumLags24kHz> SquareEnergies24kHzView() const {
     97    return square_energies_24k_;
     98  }
     99  ArrayView<const float, kNumLags12kHz> AutoCorrelation12kHzView() const {
    100    return auto_correlation_12k_;
    101  }
    102 
    103 private:
    104  std::array<float, kBufSize24kHz> pitch_buffer_24k_;
    105  std::array<float, kRefineNumLags24kHz> square_energies_24k_;
    106  std::array<float, kNumLags12kHz> auto_correlation_12k_;
    107 };
    108 
    109 // Writer for binary files.
    110 class FileWriter {
    111 public:
    112  explicit FileWriter(absl::string_view file_path)
    113      : os_(std::string(file_path), std::ios::binary) {}
    114  FileWriter(const FileWriter&) = delete;
    115  FileWriter& operator=(const FileWriter&) = delete;
    116  ~FileWriter() = default;
    117  void WriteChunk(ArrayView<const float> value) {
    118    const std::streamsize bytes_to_write = value.size() * sizeof(float);
    119    os_.write(reinterpret_cast<const char*>(value.data()), bytes_to_write);
    120  }
    121 
    122 private:
    123  std::ofstream os_;
    124 };
    125 
    126 }  // namespace rnn_vad
    127 }  // namespace webrtc
    128 
    129 #endif  // MODULES_AUDIO_PROCESSING_AGC2_RNN_VAD_TEST_UTILS_H_