OLD | NEW |
(Empty) | |
| 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 "webrtc/modules/audio_processing/test/wav_based_simulator.h" |
| 12 |
| 13 #include "webrtc/base/checks.h" |
| 14 #include "webrtc/test/testsupport/trace_to_stderr.h" |
| 15 |
| 16 namespace webrtc { |
| 17 namespace test { |
| 18 |
| 19 std::vector<WavBasedSimulator::SimulationEventType> |
| 20 WavBasedSimulator::GetDefaultEventChain() const { |
| 21 std::vector<WavBasedSimulator::SimulationEventType> call_chain(2); |
| 22 call_chain[0] = SimulationEventType::kProcessStream; |
| 23 call_chain[1] = SimulationEventType::kProcessReverseStream; |
| 24 return call_chain; |
| 25 } |
| 26 |
| 27 void WavBasedSimulator::PrepareProcessStreamCall() { |
| 28 if (settings_.fixed_interface) { |
| 29 CopyToAudioFrame(*in_buf_, &fwd_frame_); |
| 30 } |
| 31 ap_->set_stream_key_pressed(settings_.use_ts && (*settings_.use_ts)); |
| 32 |
| 33 RTC_CHECK_EQ(AudioProcessing::kNoError, |
| 34 ap_->set_stream_delay_ms( |
| 35 settings_.stream_delay ? *settings_.stream_delay : 0)); |
| 36 |
| 37 ap_->echo_cancellation()->set_stream_drift_samples( |
| 38 settings_.stream_drift_samples ? *settings_.stream_drift_samples : 0); |
| 39 |
| 40 RTC_CHECK_EQ(AudioProcessing::kNoError, |
| 41 ap_->gain_control()->set_stream_analog_level( |
| 42 last_specified_microphone_level_)); |
| 43 } |
| 44 |
| 45 void WavBasedSimulator::PrepareReverseProcessStreamCall() { |
| 46 if (settings_.fixed_interface) { |
| 47 CopyToAudioFrame(*reverse_in_buf_, &rev_frame_); |
| 48 } |
| 49 } |
| 50 |
| 51 void WavBasedSimulator::Process() { |
| 52 std::unique_ptr<test::TraceToStderr> trace_to_stderr; |
| 53 if (settings_.use_verbose_logging) { |
| 54 trace_to_stderr.reset(new test::TraceToStderr(true)); |
| 55 } |
| 56 |
| 57 call_chain_ = GetDefaultEventChain(); |
| 58 CreateAudioProcessor(); |
| 59 |
| 60 Initialize(); |
| 61 |
| 62 bool samples_left_to_process = true; |
| 63 int call_chain_index = 0; |
| 64 int num_forward_chunks_processed = 0; |
| 65 const int kOneBykChunksPerSecond = |
| 66 1.f / AudioProcessingSimulator::kChunksPerSecond; |
| 67 while (samples_left_to_process) { |
| 68 switch (call_chain_[call_chain_index]) { |
| 69 case SimulationEventType::kProcessStream: |
| 70 samples_left_to_process = HandleProcessStreamCall(); |
| 71 ++num_forward_chunks_processed; |
| 72 break; |
| 73 case SimulationEventType::kProcessReverseStream: |
| 74 if (settings_.reverse_input_filename) { |
| 75 samples_left_to_process = HandleProcessReverseStreamCall(); |
| 76 } |
| 77 break; |
| 78 default: |
| 79 RTC_CHECK(false); |
| 80 } |
| 81 |
| 82 call_chain_index = (call_chain_index + 1) % call_chain_.size(); |
| 83 |
| 84 if (trace_to_stderr) { |
| 85 trace_to_stderr->SetTimeSeconds(num_forward_chunks_processed * |
| 86 kOneBykChunksPerSecond); |
| 87 } |
| 88 } |
| 89 |
| 90 DestroyAudioProcessor(); |
| 91 } |
| 92 |
| 93 bool WavBasedSimulator::HandleProcessStreamCall() { |
| 94 bool samples_left_to_process = buffer_reader_->Read(in_buf_.get()); |
| 95 if (samples_left_to_process) { |
| 96 PrepareProcessStreamCall(); |
| 97 ProcessStream(settings_.fixed_interface); |
| 98 last_specified_microphone_level_ = |
| 99 ap_->gain_control()->stream_analog_level(); |
| 100 } |
| 101 return samples_left_to_process; |
| 102 } |
| 103 |
| 104 bool WavBasedSimulator::HandleProcessReverseStreamCall() { |
| 105 bool samples_left_to_process = |
| 106 reverse_buffer_reader_->Read(reverse_in_buf_.get()); |
| 107 if (samples_left_to_process) { |
| 108 PrepareReverseProcessStreamCall(); |
| 109 ProcessReverseStream(settings_.fixed_interface); |
| 110 } |
| 111 return samples_left_to_process; |
| 112 } |
| 113 |
| 114 void WavBasedSimulator::Initialize() { |
| 115 std::unique_ptr<WavReader> in_file( |
| 116 new WavReader(settings_.input_filename->c_str())); |
| 117 int input_sample_rate_hz = in_file->sample_rate(); |
| 118 int input_num_channels = in_file->num_channels(); |
| 119 buffer_reader_.reset(new ChannelBufferWavReader(std::move(in_file))); |
| 120 |
| 121 int output_sample_rate_hz = settings_.output_sample_rate_hz |
| 122 ? *settings_.output_sample_rate_hz |
| 123 : input_sample_rate_hz; |
| 124 int output_num_channels = settings_.output_num_channels |
| 125 ? *settings_.output_num_channels |
| 126 : input_num_channels; |
| 127 |
| 128 int reverse_sample_rate_hz = 48000; |
| 129 int reverse_num_channels = 1; |
| 130 int reverse_output_sample_rate_hz = 48000; |
| 131 int reverse_output_num_channels = 1; |
| 132 if (settings_.reverse_input_filename) { |
| 133 std::unique_ptr<WavReader> reverse_in_file( |
| 134 new WavReader(settings_.reverse_input_filename->c_str())); |
| 135 reverse_sample_rate_hz = reverse_in_file->sample_rate(); |
| 136 reverse_num_channels = reverse_in_file->num_channels(); |
| 137 reverse_buffer_reader_.reset( |
| 138 new ChannelBufferWavReader(std::move(reverse_in_file))); |
| 139 |
| 140 reverse_output_sample_rate_hz = |
| 141 settings_.reverse_output_sample_rate_hz |
| 142 ? *settings_.reverse_output_sample_rate_hz |
| 143 : reverse_sample_rate_hz; |
| 144 reverse_output_num_channels = settings_.reverse_output_num_channels |
| 145 ? *settings_.reverse_output_num_channels |
| 146 : reverse_num_channels; |
| 147 } |
| 148 |
| 149 SetupBuffersConfigsOutputs( |
| 150 input_sample_rate_hz, output_sample_rate_hz, reverse_sample_rate_hz, |
| 151 reverse_output_sample_rate_hz, input_num_channels, output_num_channels, |
| 152 reverse_num_channels, reverse_output_num_channels); |
| 153 } |
| 154 |
| 155 } // namespace test |
| 156 } // namespace webrtc |
OLD | NEW |