Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(102)

Side by Side Diff: webrtc/modules/audio_processing/test/audio_processing_simulator.cc

Issue 2834643002: audioproc_f with simulated mic analog gain (Closed)
Patch Set: Merge + comments addressed Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/modules/audio_processing/test/audio_processing_simulator.h" 11 #include "webrtc/modules/audio_processing/test/audio_processing_simulator.h"
12 12
13 #include <algorithm> 13 #include <algorithm>
14 #include <iostream> 14 #include <iostream>
15 #include <sstream> 15 #include <sstream>
16 #include <string> 16 #include <string>
17 #include <utility>
17 #include <vector> 18 #include <vector>
18 19
19 #include "webrtc/base/checks.h" 20 #include "webrtc/base/checks.h"
21 #include "webrtc/base/logging.h"
20 #include "webrtc/base/stringutils.h" 22 #include "webrtc/base/stringutils.h"
21 #include "webrtc/common_audio/include/audio_util.h" 23 #include "webrtc/common_audio/include/audio_util.h"
22 #include "webrtc/modules/audio_processing/aec_dump/aec_dump_factory.h" 24 #include "webrtc/modules/audio_processing/aec_dump/aec_dump_factory.h"
AleBzk 2017/06/29 11:43:36 Unrelated (merge)
23 #include "webrtc/modules/audio_processing/include/audio_processing.h" 25 #include "webrtc/modules/audio_processing/include/audio_processing.h"
26 #include "webrtc/modules/audio_processing/test/fake_recording_device.h"
24 27
25 namespace webrtc { 28 namespace webrtc {
26 namespace test { 29 namespace test {
27 namespace { 30 namespace {
28 31
32 constexpr FakeRecordingDevice::DeviceKind kDefaultFakeRecDeviceKind =
33 FakeRecordingDevice::DeviceKind::IDENTITY;
34
29 void CopyFromAudioFrame(const AudioFrame& src, ChannelBuffer<float>* dest) { 35 void CopyFromAudioFrame(const AudioFrame& src, ChannelBuffer<float>* dest) {
30 RTC_CHECK_EQ(src.num_channels_, dest->num_channels()); 36 RTC_CHECK_EQ(src.num_channels_, dest->num_channels());
31 RTC_CHECK_EQ(src.samples_per_channel_, dest->num_frames()); 37 RTC_CHECK_EQ(src.samples_per_channel_, dest->num_frames());
32 // Copy the data from the input buffer. 38 // Copy the data from the input buffer.
33 std::vector<float> tmp(src.samples_per_channel_ * src.num_channels_); 39 std::vector<float> tmp(src.samples_per_channel_ * src.num_channels_);
34 S16ToFloat(src.data(), tmp.size(), tmp.data()); 40 S16ToFloat(src.data(), tmp.size(), tmp.data());
35 Deinterleave(tmp.data(), src.samples_per_channel_, src.num_channels_, 41 Deinterleave(tmp.data(), src.samples_per_channel_, src.num_channels_,
36 dest->channels()); 42 dest->channels());
37 } 43 }
38 44
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 for (size_t ch = 0; ch < dest->num_channels_; ++ch) { 79 for (size_t ch = 0; ch < dest->num_channels_; ++ch) {
74 for (size_t sample = 0; sample < dest->samples_per_channel_; ++sample) { 80 for (size_t sample = 0; sample < dest->samples_per_channel_; ++sample) {
75 dest_data[sample * dest->num_channels_ + ch] = 81 dest_data[sample * dest->num_channels_ + ch] =
76 src.channels()[ch][sample] * 32767; 82 src.channels()[ch][sample] * 32767;
77 } 83 }
78 } 84 }
79 } 85 }
80 86
81 AudioProcessingSimulator::AudioProcessingSimulator( 87 AudioProcessingSimulator::AudioProcessingSimulator(
82 const SimulationSettings& settings) 88 const SimulationSettings& settings)
83 : settings_(settings), worker_queue_("file_writer_task_queue") { 89 : settings_(settings),
90 fake_recording_device_(settings.initial_mic_level,
91 settings_.simulate_mic_gain
92 ? static_cast<FakeRecordingDevice::DeviceKind>(
peah-webrtc 2017/06/29 22:04:00 I think you should move the selection of DeviceKin
AleBzk 2017/07/26 13:42:30 Done.
93 *settings.simulated_mic_kind)
94 : kDefaultFakeRecDeviceKind),
95 worker_queue_("file_writer_task_queue") {
AleBzk 2017/06/29 11:43:36 last line is unrelated (merge)
84 if (settings_.ed_graph_output_filename && 96 if (settings_.ed_graph_output_filename &&
85 settings_.ed_graph_output_filename->size() > 0) { 97 settings_.ed_graph_output_filename->size() > 0) {
86 residual_echo_likelihood_graph_writer_.open( 98 residual_echo_likelihood_graph_writer_.open(
87 *settings_.ed_graph_output_filename); 99 *settings_.ed_graph_output_filename);
88 RTC_CHECK(residual_echo_likelihood_graph_writer_.is_open()); 100 RTC_CHECK(residual_echo_likelihood_graph_writer_.is_open());
89 WriteEchoLikelihoodGraphFileHeader(&residual_echo_likelihood_graph_writer_); 101 WriteEchoLikelihoodGraphFileHeader(&residual_echo_likelihood_graph_writer_);
90 } 102 }
91 } 103 }
92 104
93 AudioProcessingSimulator::~AudioProcessingSimulator() { 105 AudioProcessingSimulator::~AudioProcessingSimulator() {
94 if (residual_echo_likelihood_graph_writer_.is_open()) { 106 if (residual_echo_likelihood_graph_writer_.is_open()) {
95 WriteEchoLikelihoodGraphFileFooter(&residual_echo_likelihood_graph_writer_); 107 WriteEchoLikelihoodGraphFileFooter(&residual_echo_likelihood_graph_writer_);
96 residual_echo_likelihood_graph_writer_.close(); 108 residual_echo_likelihood_graph_writer_.close();
97 } 109 }
98 } 110 }
99 111
100 AudioProcessingSimulator::ScopedTimer::~ScopedTimer() { 112 AudioProcessingSimulator::ScopedTimer::~ScopedTimer() {
101 int64_t interval = rtc::TimeNanos() - start_time_; 113 int64_t interval = rtc::TimeNanos() - start_time_;
102 proc_time_->sum += interval; 114 proc_time_->sum += interval;
103 proc_time_->max = std::max(proc_time_->max, interval); 115 proc_time_->max = std::max(proc_time_->max, interval);
104 proc_time_->min = std::min(proc_time_->min, interval); 116 proc_time_->min = std::min(proc_time_->min, interval);
105 } 117 }
106 118
107 void AudioProcessingSimulator::ProcessStream(bool fixed_interface) { 119 void AudioProcessingSimulator::ProcessStream(bool fixed_interface) {
120 if (settings_.aec_dump_input_filename && settings_.simulate_mic_gain) {
peah-webrtc 2017/06/29 22:04:00 This is better, but I'd rephrase it as if (sett
AleBzk 2017/07/26 13:42:30 Done.
121 // When the analog gain is sumulated and an AEC dump is used as input, set
122 // the undo level to |aec_dump_mic_level_| to virtually restore the
123 // unmodified microphone signal level.
124 RTC_DCHECK(aec_dump_mic_level_);
125 fake_recording_device_.set_undo_mic_level(aec_dump_mic_level_);
126 }
127
128 // Optionally use the fake recording device to simulate analog gain.
129 if (settings_.simulate_mic_gain) {
130 if (fixed_interface) {
131 fake_recording_device_.SimulateAnalogGain(&fwd_frame_);
132 } else {
133 fake_recording_device_.SimulateAnalogGain(in_buf_.get());
134 }
135 }
136
137 // Notify the current mic level to AGC.
138 if (settings_.simulate_mic_gain) {
139 RTC_CHECK_EQ(AudioProcessing::kNoError,
140 ap_->gain_control()->set_stream_analog_level(
peah-webrtc 2017/06/29 22:04:00 No, I don't think this is ok. The fake recording d
AleBzk 2017/07/26 13:42:30 The code below is executed only when the mic gain
141 fake_recording_device_.mic_level()));
142 }
143
144 // Process the current audio frame.
108 if (fixed_interface) { 145 if (fixed_interface) {
109 { 146 {
110 const auto st = ScopedTimer(mutable_proc_time()); 147 const auto st = ScopedTimer(mutable_proc_time());
111 RTC_CHECK_EQ(AudioProcessing::kNoError, ap_->ProcessStream(&fwd_frame_)); 148 RTC_CHECK_EQ(AudioProcessing::kNoError, ap_->ProcessStream(&fwd_frame_));
112 } 149 }
113 CopyFromAudioFrame(fwd_frame_, out_buf_.get()); 150 CopyFromAudioFrame(fwd_frame_, out_buf_.get());
114 } else { 151 } else {
115 const auto st = ScopedTimer(mutable_proc_time()); 152 const auto st = ScopedTimer(mutable_proc_time());
116 RTC_CHECK_EQ(AudioProcessing::kNoError, 153 RTC_CHECK_EQ(AudioProcessing::kNoError,
117 ap_->ProcessStream(in_buf_->channels(), in_config_, 154 ap_->ProcessStream(in_buf_->channels(), in_config_,
118 out_config_, out_buf_->channels())); 155 out_config_, out_buf_->channels()));
119 } 156 }
120 157
158 // Store the mic level suggested by AGC.
159 fake_recording_device_.set_mic_level(
160 ap_->gain_control()->stream_analog_level());
161
121 if (buffer_writer_) { 162 if (buffer_writer_) {
122 buffer_writer_->Write(*out_buf_); 163 buffer_writer_->Write(*out_buf_);
123 } 164 }
124 165
125 if (residual_echo_likelihood_graph_writer_.is_open()) { 166 if (residual_echo_likelihood_graph_writer_.is_open()) {
126 auto stats = ap_->GetStatistics(); 167 auto stats = ap_->GetStatistics();
127 residual_echo_likelihood_graph_writer_ << stats.residual_echo_likelihood 168 residual_echo_likelihood_graph_writer_ << stats.residual_echo_likelihood
128 << ", "; 169 << ", ";
129 } 170 }
130 171
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 fwd_frame_.samples_per_channel_ = 229 fwd_frame_.samples_per_channel_ =
189 rtc::CheckedDivExact(fwd_frame_.sample_rate_hz_, kChunksPerSecond); 230 rtc::CheckedDivExact(fwd_frame_.sample_rate_hz_, kChunksPerSecond);
190 fwd_frame_.num_channels_ = input_num_channels; 231 fwd_frame_.num_channels_ = input_num_channels;
191 232
192 rev_frame_.sample_rate_hz_ = reverse_input_sample_rate_hz; 233 rev_frame_.sample_rate_hz_ = reverse_input_sample_rate_hz;
193 rev_frame_.samples_per_channel_ = 234 rev_frame_.samples_per_channel_ =
194 rtc::CheckedDivExact(rev_frame_.sample_rate_hz_, kChunksPerSecond); 235 rtc::CheckedDivExact(rev_frame_.sample_rate_hz_, kChunksPerSecond);
195 rev_frame_.num_channels_ = reverse_input_num_channels; 236 rev_frame_.num_channels_ = reverse_input_num_channels;
196 237
197 if (settings_.use_verbose_logging) { 238 if (settings_.use_verbose_logging) {
239 rtc::LogMessage::LogToDebug(rtc::LS_VERBOSE);
240
198 std::cout << "Sample rates:" << std::endl; 241 std::cout << "Sample rates:" << std::endl;
199 std::cout << " Forward input: " << input_sample_rate_hz << std::endl; 242 std::cout << " Forward input: " << input_sample_rate_hz << std::endl;
200 std::cout << " Forward output: " << output_sample_rate_hz << std::endl; 243 std::cout << " Forward output: " << output_sample_rate_hz << std::endl;
201 std::cout << " Reverse input: " << reverse_input_sample_rate_hz 244 std::cout << " Reverse input: " << reverse_input_sample_rate_hz
202 << std::endl; 245 << std::endl;
203 std::cout << " Reverse output: " << reverse_output_sample_rate_hz 246 std::cout << " Reverse output: " << reverse_output_sample_rate_hz
204 << std::endl; 247 << std::endl;
205 std::cout << "Number of channels: " << std::endl; 248 std::cout << "Number of channels: " << std::endl;
206 std::cout << " Forward input: " << input_num_channels << std::endl; 249 std::cout << " Forward input: " << input_num_channels << std::endl;
207 std::cout << " Forward output: " << output_num_channels << std::endl; 250 std::cout << " Forward output: " << output_num_channels << std::endl;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 static_cast<size_t>(reverse_out_config_.num_channels()))); 286 static_cast<size_t>(reverse_out_config_.num_channels())));
244 reverse_buffer_writer_.reset( 287 reverse_buffer_writer_.reset(
245 new ChannelBufferWavWriter(std::move(reverse_out_file))); 288 new ChannelBufferWavWriter(std::move(reverse_out_file)));
246 } 289 }
247 290
248 ++output_reset_counter_; 291 ++output_reset_counter_;
249 } 292 }
250 293
251 void AudioProcessingSimulator::DestroyAudioProcessor() { 294 void AudioProcessingSimulator::DestroyAudioProcessor() {
252 if (settings_.aec_dump_output_filename) { 295 if (settings_.aec_dump_output_filename) {
253 ap_->DetachAecDump(); 296 ap_->DetachAecDump();
AleBzk 2017/06/29 11:43:36 Unrelated (merge)
254 } 297 }
255 } 298 }
256 299
257 void AudioProcessingSimulator::CreateAudioProcessor() { 300 void AudioProcessingSimulator::CreateAudioProcessor() {
258 Config config; 301 Config config;
259 AudioProcessing::Config apm_config; 302 AudioProcessing::Config apm_config;
260 if (settings_.use_bf && *settings_.use_bf) { 303 if (settings_.use_bf && *settings_.use_bf) {
261 config.Set<Beamforming>(new Beamforming( 304 config.Set<Beamforming>(new Beamforming(
262 true, ParseArrayGeometry(*settings_.microphone_positions), 305 true, ParseArrayGeometry(*settings_.microphone_positions),
263 SphericalPointf(DegreesToRadians(settings_.target_angle_degrees), 0.f, 306 SphericalPointf(DegreesToRadians(settings_.target_angle_degrees), 0.f,
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
384 ap_->noise_suppression()->set_level( 427 ap_->noise_suppression()->set_level(
385 static_cast<NoiseSuppression::Level>(*settings_.ns_level))); 428 static_cast<NoiseSuppression::Level>(*settings_.ns_level)));
386 } 429 }
387 430
388 if (settings_.use_ts) { 431 if (settings_.use_ts) {
389 ap_->set_stream_key_pressed(*settings_.use_ts); 432 ap_->set_stream_key_pressed(*settings_.use_ts);
390 } 433 }
391 434
392 if (settings_.aec_dump_output_filename) { 435 if (settings_.aec_dump_output_filename) {
393 ap_->AttachAecDump(AecDumpFactory::Create( 436 ap_->AttachAecDump(AecDumpFactory::Create(
394 *settings_.aec_dump_output_filename, -1, &worker_queue_)); 437 *settings_.aec_dump_output_filename, -1, &worker_queue_));
AleBzk 2017/06/29 11:43:36 Unrelated (merge)
395 } 438 }
396 } 439 }
397 440
398 } // namespace test 441 } // namespace test
399 } // namespace webrtc 442 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698