tor-browser

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

simulator_buffers.cc (3736B)


      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/simulator_buffers.h"
     12 
     13 #include <cstddef>
     14 #include <memory>
     15 #include <vector>
     16 
     17 #include "api/audio/audio_processing.h"
     18 #include "modules/audio_processing/audio_buffer.h"
     19 #include "modules/audio_processing/test/audio_buffer_tools.h"
     20 #include "rtc_base/checks.h"
     21 #include "rtc_base/random.h"
     22 
     23 namespace webrtc {
     24 namespace test {
     25 
     26 SimulatorBuffers::SimulatorBuffers(int render_input_sample_rate_hz,
     27                                   int capture_input_sample_rate_hz,
     28                                   int render_output_sample_rate_hz,
     29                                   int capture_output_sample_rate_hz,
     30                                   size_t num_render_input_channels,
     31                                   size_t num_capture_input_channels,
     32                                   size_t num_render_output_channels,
     33                                   size_t num_capture_output_channels) {
     34  Random rand_gen(42);
     35  CreateConfigAndBuffer(render_input_sample_rate_hz, num_render_input_channels,
     36                        &rand_gen, &render_input_buffer, &render_input_config,
     37                        &render_input, &render_input_samples);
     38 
     39  CreateConfigAndBuffer(render_output_sample_rate_hz,
     40                        num_render_output_channels, &rand_gen,
     41                        &render_output_buffer, &render_output_config,
     42                        &render_output, &render_output_samples);
     43 
     44  CreateConfigAndBuffer(capture_input_sample_rate_hz,
     45                        num_capture_input_channels, &rand_gen,
     46                        &capture_input_buffer, &capture_input_config,
     47                        &capture_input, &capture_input_samples);
     48 
     49  CreateConfigAndBuffer(capture_output_sample_rate_hz,
     50                        num_capture_output_channels, &rand_gen,
     51                        &capture_output_buffer, &capture_output_config,
     52                        &capture_output, &capture_output_samples);
     53 
     54  UpdateInputBuffers();
     55 }
     56 
     57 SimulatorBuffers::~SimulatorBuffers() = default;
     58 
     59 void SimulatorBuffers::CreateConfigAndBuffer(
     60    int sample_rate_hz,
     61    size_t num_channels,
     62    Random* rand_gen,
     63    std::unique_ptr<AudioBuffer>* buffer,
     64    StreamConfig* config,
     65    std::vector<float*>* buffer_data,
     66    std::vector<float>* buffer_data_samples) {
     67  int samples_per_channel = CheckedDivExact(sample_rate_hz, 100);
     68  *config = StreamConfig(sample_rate_hz, num_channels);
     69  buffer->reset(
     70      new AudioBuffer(config->sample_rate_hz(), config->num_channels(),
     71                      config->sample_rate_hz(), config->num_channels(),
     72                      config->sample_rate_hz(), config->num_channels()));
     73 
     74  buffer_data_samples->resize(samples_per_channel * num_channels);
     75  for (auto& v : *buffer_data_samples) {
     76    v = rand_gen->Rand<float>();
     77  }
     78 
     79  buffer_data->resize(num_channels);
     80  for (size_t ch = 0; ch < num_channels; ++ch) {
     81    (*buffer_data)[ch] = &(*buffer_data_samples)[ch * samples_per_channel];
     82  }
     83 }
     84 
     85 void SimulatorBuffers::UpdateInputBuffers() {
     86  test::CopyVectorToAudioBuffer(capture_input_config, capture_input_samples,
     87                                capture_input_buffer.get());
     88  test::CopyVectorToAudioBuffer(render_input_config, render_input_samples,
     89                                render_input_buffer.get());
     90 }
     91 
     92 }  // namespace test
     93 }  // namespace webrtc