tor-browser

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

wav_based_simulator.cc (6922B)


      1 /*
      2 *  Copyright (c) 2016 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/test/wav_based_simulator.h"
     12 
     13 #include <cstdio>
     14 #include <iostream>
     15 #include <memory>
     16 #include <utility>
     17 #include <vector>
     18 
     19 #include "absl/base/nullability.h"
     20 #include "absl/strings/string_view.h"
     21 #include "api/audio/audio_processing.h"
     22 #include "api/scoped_refptr.h"
     23 #include "common_audio/wav_file.h"
     24 #include "modules/audio_processing/test/audio_processing_simulator.h"
     25 #include "modules/audio_processing/test/test_utils.h"
     26 #include "rtc_base/checks.h"
     27 #include "rtc_base/system/file_wrapper.h"
     28 
     29 namespace webrtc {
     30 namespace test {
     31 
     32 std::vector<WavBasedSimulator::SimulationEventType>
     33 WavBasedSimulator::GetCustomEventChain(absl::string_view filename) {
     34  std::vector<WavBasedSimulator::SimulationEventType> call_chain;
     35  FileWrapper file_wrapper = FileWrapper::OpenReadOnly(filename);
     36 
     37  RTC_CHECK(file_wrapper.is_open())
     38      << "Could not open the custom call order file, reverting "
     39         "to using the default call order";
     40 
     41  char c;
     42  size_t num_read = file_wrapper.Read(&c, sizeof(char));
     43  while (num_read > 0) {
     44    switch (c) {
     45      case 'r':
     46        call_chain.push_back(SimulationEventType::kProcessReverseStream);
     47        break;
     48      case 'c':
     49        call_chain.push_back(SimulationEventType::kProcessStream);
     50        break;
     51      case '\n':
     52        break;
     53      default:
     54        RTC_FATAL() << "Incorrect custom call order file";
     55    }
     56 
     57    num_read = file_wrapper.Read(&c, sizeof(char));
     58  }
     59 
     60  return call_chain;
     61 }
     62 
     63 WavBasedSimulator::WavBasedSimulator(
     64    const SimulationSettings& settings,
     65    absl_nonnull scoped_refptr<AudioProcessing> audio_processing)
     66    : AudioProcessingSimulator(settings, std::move(audio_processing)) {
     67  if (settings_.call_order_input_filename) {
     68    call_chain_ = WavBasedSimulator::GetCustomEventChain(
     69        *settings_.call_order_input_filename);
     70  } else {
     71    call_chain_ = WavBasedSimulator::GetDefaultEventChain();
     72  }
     73 }
     74 
     75 WavBasedSimulator::~WavBasedSimulator() = default;
     76 
     77 std::vector<WavBasedSimulator::SimulationEventType>
     78 WavBasedSimulator::GetDefaultEventChain() {
     79  std::vector<WavBasedSimulator::SimulationEventType> call_chain(2);
     80  call_chain[0] = SimulationEventType::kProcessStream;
     81  call_chain[1] = SimulationEventType::kProcessReverseStream;
     82  return call_chain;
     83 }
     84 
     85 void WavBasedSimulator::PrepareProcessStreamCall() {
     86  if (settings_.fixed_interface) {
     87    fwd_frame_.CopyFrom(*in_buf_);
     88  }
     89  ap_->set_stream_key_pressed(settings_.override_key_pressed.value_or(false));
     90 
     91  if (!settings_.use_stream_delay || *settings_.use_stream_delay) {
     92    RTC_CHECK_EQ(AudioProcessing::kNoError,
     93                 ap_->set_stream_delay_ms(
     94                     settings_.stream_delay ? *settings_.stream_delay : 0));
     95  }
     96 }
     97 
     98 void WavBasedSimulator::PrepareReverseProcessStreamCall() {
     99  if (settings_.fixed_interface) {
    100    rev_frame_.CopyFrom(*reverse_in_buf_);
    101  }
    102 }
    103 
    104 void WavBasedSimulator::Process() {
    105  ConfigureAudioProcessor();
    106 
    107  Initialize();
    108 
    109  bool samples_left_to_process = true;
    110  int call_chain_index = 0;
    111  int capture_frames_since_init = 0;
    112  constexpr int kInitIndex = 1;
    113  while (samples_left_to_process) {
    114    switch (call_chain_[call_chain_index]) {
    115      case SimulationEventType::kProcessStream:
    116        SelectivelyToggleDataDumping(kInitIndex, capture_frames_since_init);
    117 
    118        samples_left_to_process = HandleProcessStreamCall();
    119        ++capture_frames_since_init;
    120        break;
    121      case SimulationEventType::kProcessReverseStream:
    122        if (settings_.reverse_input_filename) {
    123          samples_left_to_process = HandleProcessReverseStreamCall();
    124        }
    125        break;
    126      default:
    127        RTC_CHECK_NOTREACHED();
    128    }
    129 
    130    call_chain_index = (call_chain_index + 1) % call_chain_.size();
    131  }
    132 
    133  DetachAecDump();
    134 }
    135 
    136 void WavBasedSimulator::Analyze() {
    137  std::cout << "Inits:" << std::endl;
    138  std::cout << "1: -->" << std::endl;
    139  std::cout << " Time:" << std::endl;
    140  std::cout << "  Capture: 0 s (0 frames) " << std::endl;
    141  std::cout << "  Render: 0 s (0 frames)" << std::endl;
    142 }
    143 
    144 bool WavBasedSimulator::HandleProcessStreamCall() {
    145  bool samples_left_to_process = buffer_reader_->Read(in_buf_.get());
    146  if (samples_left_to_process) {
    147    PrepareProcessStreamCall();
    148    ProcessStream(settings_.fixed_interface);
    149  }
    150  return samples_left_to_process;
    151 }
    152 
    153 bool WavBasedSimulator::HandleProcessReverseStreamCall() {
    154  bool samples_left_to_process =
    155      reverse_buffer_reader_->Read(reverse_in_buf_.get());
    156  if (samples_left_to_process) {
    157    PrepareReverseProcessStreamCall();
    158    ProcessReverseStream(settings_.fixed_interface);
    159  }
    160  return samples_left_to_process;
    161 }
    162 
    163 void WavBasedSimulator::Initialize() {
    164  std::unique_ptr<WavReader> in_file(
    165      new WavReader(settings_.input_filename->c_str()));
    166  int input_sample_rate_hz = in_file->sample_rate();
    167  int input_num_channels = in_file->num_channels();
    168  buffer_reader_.reset(new ChannelBufferWavReader(std::move(in_file)));
    169 
    170  int output_sample_rate_hz = settings_.output_sample_rate_hz
    171                                  ? *settings_.output_sample_rate_hz
    172                                  : input_sample_rate_hz;
    173  int output_num_channels = settings_.output_num_channels
    174                                ? *settings_.output_num_channels
    175                                : input_num_channels;
    176 
    177  int reverse_sample_rate_hz = 48000;
    178  int reverse_num_channels = 1;
    179  int reverse_output_sample_rate_hz = 48000;
    180  int reverse_output_num_channels = 1;
    181  if (settings_.reverse_input_filename) {
    182    std::unique_ptr<WavReader> reverse_in_file(
    183        new WavReader(settings_.reverse_input_filename->c_str()));
    184    reverse_sample_rate_hz = reverse_in_file->sample_rate();
    185    reverse_num_channels = reverse_in_file->num_channels();
    186    reverse_buffer_reader_.reset(
    187        new ChannelBufferWavReader(std::move(reverse_in_file)));
    188 
    189    reverse_output_sample_rate_hz =
    190        settings_.reverse_output_sample_rate_hz
    191            ? *settings_.reverse_output_sample_rate_hz
    192            : reverse_sample_rate_hz;
    193    reverse_output_num_channels = settings_.reverse_output_num_channels
    194                                      ? *settings_.reverse_output_num_channels
    195                                      : reverse_num_channels;
    196  }
    197 
    198  SetupBuffersConfigsOutputs(
    199      input_sample_rate_hz, output_sample_rate_hz, reverse_sample_rate_hz,
    200      reverse_output_sample_rate_hz, input_num_channels, output_num_channels,
    201      reverse_num_channels, reverse_output_num_channels);
    202 }
    203 
    204 }  // namespace test
    205 }  // namespace webrtc