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