niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
leozwang@webrtc.org | 9a85d8e | 2012-03-16 18:03:18 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
andrew@webrtc.org | 8186534 | 2012-10-27 00:28:27 +0000 | [diff] [blame] | 11 | #include <math.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | #include <stdio.h> |
| 13 | #include <string.h> |
| 14 | #ifdef WEBRTC_ANDROID |
| 15 | #include <sys/stat.h> |
| 16 | #endif |
| 17 | |
andrew@webrtc.org | 8186534 | 2012-10-27 00:28:27 +0000 | [diff] [blame] | 18 | #include <algorithm> |
| 19 | |
kma@webrtc.org | 0e73950 | 2012-12-07 15:26:28 +0000 | [diff] [blame] | 20 | #include "webrtc/modules/audio_processing/include/audio_processing.h" |
| 21 | #include "webrtc/modules/interface/module_common_types.h" |
| 22 | #include "webrtc/system_wrappers/interface/cpu_features_wrapper.h" |
| 23 | #include "webrtc/system_wrappers/interface/scoped_ptr.h" |
| 24 | #include "webrtc/system_wrappers/interface/tick_util.h" |
kjellander@webrtc.org | 10abe25 | 2012-12-17 18:28:07 +0000 | [diff] [blame] | 25 | #include "webrtc/test/testsupport/fileutils.h" |
kma@webrtc.org | 0e73950 | 2012-12-07 15:26:28 +0000 | [diff] [blame] | 26 | #include "webrtc/test/testsupport/perf_test.h" |
leozwang@webrtc.org | a373634 | 2012-03-16 21:36:00 +0000 | [diff] [blame] | 27 | #ifdef WEBRTC_ANDROID_PLATFORM_BUILD |
pbos@webrtc.org | 8c34cee | 2013-05-28 09:24:03 +0000 | [diff] [blame] | 28 | #include "gtest/gtest.h" |
leozwang@webrtc.org | 534e495 | 2012-10-22 21:21:52 +0000 | [diff] [blame] | 29 | #include "external/webrtc/webrtc/modules/audio_processing/debug.pb.h" |
leozwang@webrtc.org | a373634 | 2012-03-16 21:36:00 +0000 | [diff] [blame] | 30 | #else |
pbos@webrtc.org | 8c34cee | 2013-05-28 09:24:03 +0000 | [diff] [blame] | 31 | #include "testing/gtest/include/gtest/gtest.h" |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 32 | #include "webrtc/audio_processing/debug.pb.h" |
leozwang@webrtc.org | a373634 | 2012-03-16 21:36:00 +0000 | [diff] [blame] | 33 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 34 | |
| 35 | using webrtc::AudioFrame; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 36 | using webrtc::AudioProcessing; |
andrew@webrtc.org | 94c7413 | 2011-09-19 15:17:57 +0000 | [diff] [blame] | 37 | using webrtc::EchoCancellation; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 38 | using webrtc::GainControl; |
| 39 | using webrtc::NoiseSuppression; |
andrew@webrtc.org | 3119ecf | 2011-11-01 17:00:18 +0000 | [diff] [blame] | 40 | using webrtc::scoped_array; |
andrew@webrtc.org | f3930e9 | 2013-09-18 22:37:32 +0000 | [diff] [blame] | 41 | using webrtc::scoped_ptr; |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 42 | using webrtc::TickInterval; |
| 43 | using webrtc::TickTime; |
andrew@webrtc.org | 8975261 | 2012-10-12 16:41:45 +0000 | [diff] [blame] | 44 | using webrtc::VoiceDetection; |
andrew@webrtc.org | 3119ecf | 2011-11-01 17:00:18 +0000 | [diff] [blame] | 45 | |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 46 | using webrtc::audioproc::Event; |
| 47 | using webrtc::audioproc::Init; |
| 48 | using webrtc::audioproc::ReverseStream; |
| 49 | using webrtc::audioproc::Stream; |
| 50 | |
| 51 | namespace { |
| 52 | // Returns true on success, false on error or end-of-file. |
| 53 | bool ReadMessageFromFile(FILE* file, |
| 54 | ::google::protobuf::MessageLite* msg) { |
| 55 | // The "wire format" for the size is little-endian. |
| 56 | // Assume process_test is running on a little-endian machine. |
andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 57 | int32_t size = 0; |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 58 | if (fread(&size, sizeof(int32_t), 1, file) != 1) { |
| 59 | return false; |
| 60 | } |
| 61 | if (size <= 0) { |
| 62 | return false; |
| 63 | } |
andrew@webrtc.org | 3119ecf | 2011-11-01 17:00:18 +0000 | [diff] [blame] | 64 | const size_t usize = static_cast<size_t>(size); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 65 | |
andrew@webrtc.org | 3119ecf | 2011-11-01 17:00:18 +0000 | [diff] [blame] | 66 | scoped_array<char> array(new char[usize]); |
| 67 | if (fread(array.get(), sizeof(char), usize, file) != usize) { |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 68 | return false; |
| 69 | } |
| 70 | |
| 71 | msg->Clear(); |
andrew@webrtc.org | 3119ecf | 2011-11-01 17:00:18 +0000 | [diff] [blame] | 72 | return msg->ParseFromArray(array.get(), usize); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 73 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 74 | |
andrew@webrtc.org | 94c7413 | 2011-09-19 15:17:57 +0000 | [diff] [blame] | 75 | void PrintStat(const AudioProcessing::Statistic& stat) { |
| 76 | printf("%d, %d, %d\n", stat.average, |
| 77 | stat.maximum, |
| 78 | stat.minimum); |
| 79 | } |
| 80 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 81 | void usage() { |
| 82 | printf( |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 83 | "Usage: process_test [options] [-pb PROTOBUF_FILE]\n" |
| 84 | " [-ir REVERSE_FILE] [-i PRIMARY_FILE] [-o OUT_FILE]\n"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 85 | printf( |
| 86 | "process_test is a test application for AudioProcessing.\n\n" |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 87 | "When a protobuf debug file is available, specify it with -pb.\n" |
| 88 | "Alternately, when -ir or -i is used, the specified files will be\n" |
| 89 | "processed directly in a simulation mode. Otherwise the full set of\n" |
| 90 | "legacy test files is expected to be present in the working directory.\n"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 91 | printf("\n"); |
| 92 | printf("Options\n"); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 93 | printf("General configuration (only used for the simulation mode):\n"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 94 | printf(" -fs SAMPLE_RATE_HZ\n"); |
| 95 | printf(" -ch CHANNELS_IN CHANNELS_OUT\n"); |
| 96 | printf(" -rch REVERSE_CHANNELS\n"); |
| 97 | printf("\n"); |
| 98 | printf("Component configuration:\n"); |
| 99 | printf( |
| 100 | "All components are disabled by default. Each block below begins with a\n" |
| 101 | "flag to enable the component with default settings. The subsequent flags\n" |
| 102 | "in the block are used to provide configuration settings.\n"); |
| 103 | printf("\n -aec Echo cancellation\n"); |
| 104 | printf(" --drift_compensation\n"); |
| 105 | printf(" --no_drift_compensation\n"); |
andrew@webrtc.org | 94c7413 | 2011-09-19 15:17:57 +0000 | [diff] [blame] | 106 | printf(" --no_echo_metrics\n"); |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 107 | printf(" --no_delay_logging\n"); |
andrew@webrtc.org | dff69c5 | 2013-04-30 23:01:09 +0000 | [diff] [blame] | 108 | printf(" --aec_suppression_level LEVEL [0 - 2]\n"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 109 | printf("\n -aecm Echo control mobile\n"); |
bjornv@google.com | 238a022 | 2011-07-15 14:51:52 +0000 | [diff] [blame] | 110 | printf(" --aecm_echo_path_in_file FILE\n"); |
| 111 | printf(" --aecm_echo_path_out_file FILE\n"); |
andrew@webrtc.org | 1acb3b3 | 2013-04-26 00:39:27 +0000 | [diff] [blame] | 112 | printf(" --no_comfort_noise\n"); |
| 113 | printf(" --routing_mode MODE [0 - 4]\n"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 114 | printf("\n -agc Gain control\n"); |
| 115 | printf(" --analog\n"); |
| 116 | printf(" --adaptive_digital\n"); |
| 117 | printf(" --fixed_digital\n"); |
| 118 | printf(" --target_level LEVEL\n"); |
| 119 | printf(" --compression_gain GAIN\n"); |
| 120 | printf(" --limiter\n"); |
| 121 | printf(" --no_limiter\n"); |
| 122 | printf("\n -hpf High pass filter\n"); |
| 123 | printf("\n -ns Noise suppression\n"); |
| 124 | printf(" --ns_low\n"); |
| 125 | printf(" --ns_moderate\n"); |
| 126 | printf(" --ns_high\n"); |
| 127 | printf(" --ns_very_high\n"); |
bjornv@webrtc.org | 08329f4 | 2012-07-12 21:00:43 +0000 | [diff] [blame] | 128 | printf(" --ns_prob_file FILE\n"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 129 | printf("\n -vad Voice activity detection\n"); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 130 | printf(" --vad_out_file FILE\n"); |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 131 | printf("\n Level metrics (enabled by default)\n"); |
| 132 | printf(" --no_level_metrics\n"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 133 | printf("\n"); |
| 134 | printf("Modifiers:\n"); |
andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 135 | printf(" --noasm Disable SSE optimization.\n"); |
andrew@webrtc.org | ca764ab | 2013-10-07 16:44:32 +0000 | [diff] [blame] | 136 | printf(" --add_delay DELAY Add DELAY ms to input value.\n"); |
| 137 | printf(" --delay DELAY Override input delay with DELAY ms.\n"); |
andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 138 | printf(" --perf Measure performance.\n"); |
| 139 | printf(" --quiet Suppress text output.\n"); |
| 140 | printf(" --no_progress Suppress progress.\n"); |
| 141 | printf(" --debug_file FILE Dump a debug recording.\n"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 142 | } |
| 143 | |
andrew@webrtc.org | bafdae3 | 2013-01-11 23:11:29 +0000 | [diff] [blame] | 144 | static float MicLevel2Gain(int level) { |
| 145 | return pow(10.0f, ((level - 127.0f) / 128.0f * 40.0f) / 20.0f); |
andrew@webrtc.org | 8186534 | 2012-10-27 00:28:27 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | static void SimulateMic(int mic_level, AudioFrame* frame) { |
| 149 | mic_level = std::min(std::max(mic_level, 0), 255); |
andrew@webrtc.org | bafdae3 | 2013-01-11 23:11:29 +0000 | [diff] [blame] | 150 | float mic_gain = MicLevel2Gain(mic_level); |
andrew@webrtc.org | 8186534 | 2012-10-27 00:28:27 +0000 | [diff] [blame] | 151 | int num_samples = frame->samples_per_channel_ * frame->num_channels_; |
andrew@webrtc.org | bafdae3 | 2013-01-11 23:11:29 +0000 | [diff] [blame] | 152 | float v; |
andrew@webrtc.org | 8186534 | 2012-10-27 00:28:27 +0000 | [diff] [blame] | 153 | for (int n = 0; n < num_samples; n++) { |
| 154 | v = floor(frame->data_[n] * mic_gain + 0.5); |
andrew@webrtc.org | bafdae3 | 2013-01-11 23:11:29 +0000 | [diff] [blame] | 155 | v = std::max(std::min(32767.0f, v), -32768.0f); |
andrew@webrtc.org | 8186534 | 2012-10-27 00:28:27 +0000 | [diff] [blame] | 156 | frame->data_[n] = static_cast<int16_t>(v); |
| 157 | } |
| 158 | } |
| 159 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 160 | // void function for gtest. |
| 161 | void void_main(int argc, char* argv[]) { |
| 162 | if (argc > 1 && strcmp(argv[1], "--help") == 0) { |
| 163 | usage(); |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | if (argc < 2) { |
| 168 | printf("Did you mean to run without arguments?\n"); |
| 169 | printf("Try `process_test --help' for more information.\n\n"); |
| 170 | } |
| 171 | |
andrew@webrtc.org | f3930e9 | 2013-09-18 22:37:32 +0000 | [diff] [blame] | 172 | scoped_ptr<AudioProcessing> apm(AudioProcessing::Create(0)); |
| 173 | ASSERT_TRUE(apm.get() != NULL); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 174 | |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 175 | const char* pb_filename = NULL; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 176 | const char* far_filename = NULL; |
| 177 | const char* near_filename = NULL; |
| 178 | const char* out_filename = NULL; |
| 179 | const char* vad_out_filename = NULL; |
bjornv@webrtc.org | 08329f4 | 2012-07-12 21:00:43 +0000 | [diff] [blame] | 180 | const char* ns_prob_filename = NULL; |
bjornv@google.com | c4b939c | 2011-07-13 08:09:56 +0000 | [diff] [blame] | 181 | const char* aecm_echo_path_in_filename = NULL; |
| 182 | const char* aecm_echo_path_out_filename = NULL; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 183 | |
| 184 | int32_t sample_rate_hz = 16000; |
| 185 | int32_t device_sample_rate_hz = 16000; |
| 186 | |
| 187 | int num_capture_input_channels = 1; |
| 188 | int num_capture_output_channels = 1; |
| 189 | int num_render_channels = 1; |
| 190 | |
| 191 | int samples_per_channel = sample_rate_hz / 100; |
| 192 | |
| 193 | bool simulating = false; |
| 194 | bool perf_testing = false; |
| 195 | bool verbose = true; |
| 196 | bool progress = true; |
andrew@webrtc.org | 4b13fc9 | 2011-11-09 19:27:11 +0000 | [diff] [blame] | 197 | int extra_delay_ms = 0; |
andrew@webrtc.org | ca764ab | 2013-10-07 16:44:32 +0000 | [diff] [blame] | 198 | int override_delay_ms = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 199 | //bool interleaved = true; |
| 200 | |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 201 | ASSERT_EQ(apm->kNoError, apm->level_estimator()->Enable(true)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 202 | for (int i = 1; i < argc; i++) { |
andrew@webrtc.org | 94c7413 | 2011-09-19 15:17:57 +0000 | [diff] [blame] | 203 | if (strcmp(argv[i], "-pb") == 0) { |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 204 | i++; |
| 205 | ASSERT_LT(i, argc) << "Specify protobuf filename after -pb"; |
| 206 | pb_filename = argv[i]; |
| 207 | |
| 208 | } else if (strcmp(argv[i], "-ir") == 0) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 209 | i++; |
| 210 | ASSERT_LT(i, argc) << "Specify filename after -ir"; |
| 211 | far_filename = argv[i]; |
| 212 | simulating = true; |
| 213 | |
| 214 | } else if (strcmp(argv[i], "-i") == 0) { |
| 215 | i++; |
| 216 | ASSERT_LT(i, argc) << "Specify filename after -i"; |
| 217 | near_filename = argv[i]; |
| 218 | simulating = true; |
| 219 | |
| 220 | } else if (strcmp(argv[i], "-o") == 0) { |
| 221 | i++; |
| 222 | ASSERT_LT(i, argc) << "Specify filename after -o"; |
| 223 | out_filename = argv[i]; |
| 224 | |
| 225 | } else if (strcmp(argv[i], "-fs") == 0) { |
| 226 | i++; |
| 227 | ASSERT_LT(i, argc) << "Specify sample rate after -fs"; |
| 228 | ASSERT_EQ(1, sscanf(argv[i], "%d", &sample_rate_hz)); |
| 229 | samples_per_channel = sample_rate_hz / 100; |
| 230 | |
| 231 | ASSERT_EQ(apm->kNoError, |
| 232 | apm->set_sample_rate_hz(sample_rate_hz)); |
| 233 | |
| 234 | } else if (strcmp(argv[i], "-ch") == 0) { |
| 235 | i++; |
| 236 | ASSERT_LT(i + 1, argc) << "Specify number of channels after -ch"; |
| 237 | ASSERT_EQ(1, sscanf(argv[i], "%d", &num_capture_input_channels)); |
| 238 | i++; |
| 239 | ASSERT_EQ(1, sscanf(argv[i], "%d", &num_capture_output_channels)); |
| 240 | |
| 241 | ASSERT_EQ(apm->kNoError, |
| 242 | apm->set_num_channels(num_capture_input_channels, |
| 243 | num_capture_output_channels)); |
| 244 | |
| 245 | } else if (strcmp(argv[i], "-rch") == 0) { |
| 246 | i++; |
| 247 | ASSERT_LT(i, argc) << "Specify number of channels after -rch"; |
| 248 | ASSERT_EQ(1, sscanf(argv[i], "%d", &num_render_channels)); |
| 249 | |
| 250 | ASSERT_EQ(apm->kNoError, |
| 251 | apm->set_num_reverse_channels(num_render_channels)); |
| 252 | |
| 253 | } else if (strcmp(argv[i], "-aec") == 0) { |
| 254 | ASSERT_EQ(apm->kNoError, apm->echo_cancellation()->Enable(true)); |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 255 | ASSERT_EQ(apm->kNoError, |
| 256 | apm->echo_cancellation()->enable_metrics(true)); |
| 257 | ASSERT_EQ(apm->kNoError, |
| 258 | apm->echo_cancellation()->enable_delay_logging(true)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 259 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 260 | } else if (strcmp(argv[i], "--drift_compensation") == 0) { |
| 261 | ASSERT_EQ(apm->kNoError, apm->echo_cancellation()->Enable(true)); |
| 262 | // TODO(ajm): this is enabled in the VQE test app by default. Investigate |
| 263 | // why it can give better performance despite passing zeros. |
| 264 | ASSERT_EQ(apm->kNoError, |
| 265 | apm->echo_cancellation()->enable_drift_compensation(true)); |
| 266 | } else if (strcmp(argv[i], "--no_drift_compensation") == 0) { |
| 267 | ASSERT_EQ(apm->kNoError, apm->echo_cancellation()->Enable(true)); |
| 268 | ASSERT_EQ(apm->kNoError, |
| 269 | apm->echo_cancellation()->enable_drift_compensation(false)); |
| 270 | |
andrew@webrtc.org | 94c7413 | 2011-09-19 15:17:57 +0000 | [diff] [blame] | 271 | } else if (strcmp(argv[i], "--no_echo_metrics") == 0) { |
| 272 | ASSERT_EQ(apm->kNoError, apm->echo_cancellation()->Enable(true)); |
| 273 | ASSERT_EQ(apm->kNoError, |
| 274 | apm->echo_cancellation()->enable_metrics(false)); |
| 275 | |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 276 | } else if (strcmp(argv[i], "--no_delay_logging") == 0) { |
| 277 | ASSERT_EQ(apm->kNoError, apm->echo_cancellation()->Enable(true)); |
| 278 | ASSERT_EQ(apm->kNoError, |
| 279 | apm->echo_cancellation()->enable_delay_logging(false)); |
| 280 | |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 281 | } else if (strcmp(argv[i], "--no_level_metrics") == 0) { |
| 282 | ASSERT_EQ(apm->kNoError, apm->level_estimator()->Enable(false)); |
| 283 | |
andrew@webrtc.org | dff69c5 | 2013-04-30 23:01:09 +0000 | [diff] [blame] | 284 | } else if (strcmp(argv[i], "--aec_suppression_level") == 0) { |
| 285 | i++; |
| 286 | ASSERT_LT(i, argc) << "Specify level after --aec_suppression_level"; |
| 287 | int suppression_level; |
| 288 | ASSERT_EQ(1, sscanf(argv[i], "%d", &suppression_level)); |
| 289 | ASSERT_EQ(apm->kNoError, |
| 290 | apm->echo_cancellation()->set_suppression_level( |
| 291 | static_cast<webrtc::EchoCancellation::SuppressionLevel>( |
| 292 | suppression_level))); |
| 293 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 294 | } else if (strcmp(argv[i], "-aecm") == 0) { |
| 295 | ASSERT_EQ(apm->kNoError, apm->echo_control_mobile()->Enable(true)); |
| 296 | |
bjornv@google.com | c4b939c | 2011-07-13 08:09:56 +0000 | [diff] [blame] | 297 | } else if (strcmp(argv[i], "--aecm_echo_path_in_file") == 0) { |
| 298 | i++; |
| 299 | ASSERT_LT(i, argc) << "Specify filename after --aecm_echo_path_in_file"; |
| 300 | aecm_echo_path_in_filename = argv[i]; |
| 301 | |
| 302 | } else if (strcmp(argv[i], "--aecm_echo_path_out_file") == 0) { |
| 303 | i++; |
| 304 | ASSERT_LT(i, argc) << "Specify filename after --aecm_echo_path_out_file"; |
| 305 | aecm_echo_path_out_filename = argv[i]; |
| 306 | |
andrew@webrtc.org | 1acb3b3 | 2013-04-26 00:39:27 +0000 | [diff] [blame] | 307 | } else if (strcmp(argv[i], "--no_comfort_noise") == 0) { |
| 308 | ASSERT_EQ(apm->kNoError, |
| 309 | apm->echo_control_mobile()->enable_comfort_noise(false)); |
| 310 | |
| 311 | } else if (strcmp(argv[i], "--routing_mode") == 0) { |
| 312 | i++; |
| 313 | ASSERT_LT(i, argc) << "Specify mode after --routing_mode"; |
| 314 | int routing_mode; |
| 315 | ASSERT_EQ(1, sscanf(argv[i], "%d", &routing_mode)); |
| 316 | ASSERT_EQ(apm->kNoError, |
| 317 | apm->echo_control_mobile()->set_routing_mode( |
| 318 | static_cast<webrtc::EchoControlMobile::RoutingMode>( |
| 319 | routing_mode))); |
| 320 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 321 | } else if (strcmp(argv[i], "-agc") == 0) { |
| 322 | ASSERT_EQ(apm->kNoError, apm->gain_control()->Enable(true)); |
| 323 | |
| 324 | } else if (strcmp(argv[i], "--analog") == 0) { |
| 325 | ASSERT_EQ(apm->kNoError, apm->gain_control()->Enable(true)); |
| 326 | ASSERT_EQ(apm->kNoError, |
| 327 | apm->gain_control()->set_mode(GainControl::kAdaptiveAnalog)); |
| 328 | |
| 329 | } else if (strcmp(argv[i], "--adaptive_digital") == 0) { |
| 330 | ASSERT_EQ(apm->kNoError, apm->gain_control()->Enable(true)); |
| 331 | ASSERT_EQ(apm->kNoError, |
| 332 | apm->gain_control()->set_mode(GainControl::kAdaptiveDigital)); |
| 333 | |
| 334 | } else if (strcmp(argv[i], "--fixed_digital") == 0) { |
| 335 | ASSERT_EQ(apm->kNoError, apm->gain_control()->Enable(true)); |
| 336 | ASSERT_EQ(apm->kNoError, |
| 337 | apm->gain_control()->set_mode(GainControl::kFixedDigital)); |
| 338 | |
| 339 | } else if (strcmp(argv[i], "--target_level") == 0) { |
| 340 | i++; |
| 341 | int level; |
| 342 | ASSERT_EQ(1, sscanf(argv[i], "%d", &level)); |
| 343 | |
| 344 | ASSERT_EQ(apm->kNoError, apm->gain_control()->Enable(true)); |
| 345 | ASSERT_EQ(apm->kNoError, |
| 346 | apm->gain_control()->set_target_level_dbfs(level)); |
| 347 | |
| 348 | } else if (strcmp(argv[i], "--compression_gain") == 0) { |
| 349 | i++; |
| 350 | int gain; |
| 351 | ASSERT_EQ(1, sscanf(argv[i], "%d", &gain)); |
| 352 | |
| 353 | ASSERT_EQ(apm->kNoError, apm->gain_control()->Enable(true)); |
| 354 | ASSERT_EQ(apm->kNoError, |
| 355 | apm->gain_control()->set_compression_gain_db(gain)); |
| 356 | |
| 357 | } else if (strcmp(argv[i], "--limiter") == 0) { |
| 358 | ASSERT_EQ(apm->kNoError, apm->gain_control()->Enable(true)); |
| 359 | ASSERT_EQ(apm->kNoError, |
| 360 | apm->gain_control()->enable_limiter(true)); |
| 361 | |
| 362 | } else if (strcmp(argv[i], "--no_limiter") == 0) { |
| 363 | ASSERT_EQ(apm->kNoError, apm->gain_control()->Enable(true)); |
| 364 | ASSERT_EQ(apm->kNoError, |
| 365 | apm->gain_control()->enable_limiter(false)); |
| 366 | |
| 367 | } else if (strcmp(argv[i], "-hpf") == 0) { |
| 368 | ASSERT_EQ(apm->kNoError, apm->high_pass_filter()->Enable(true)); |
| 369 | |
| 370 | } else if (strcmp(argv[i], "-ns") == 0) { |
| 371 | ASSERT_EQ(apm->kNoError, apm->noise_suppression()->Enable(true)); |
| 372 | |
| 373 | } else if (strcmp(argv[i], "--ns_low") == 0) { |
| 374 | ASSERT_EQ(apm->kNoError, apm->noise_suppression()->Enable(true)); |
| 375 | ASSERT_EQ(apm->kNoError, |
| 376 | apm->noise_suppression()->set_level(NoiseSuppression::kLow)); |
| 377 | |
| 378 | } else if (strcmp(argv[i], "--ns_moderate") == 0) { |
| 379 | ASSERT_EQ(apm->kNoError, apm->noise_suppression()->Enable(true)); |
| 380 | ASSERT_EQ(apm->kNoError, |
| 381 | apm->noise_suppression()->set_level(NoiseSuppression::kModerate)); |
| 382 | |
| 383 | } else if (strcmp(argv[i], "--ns_high") == 0) { |
| 384 | ASSERT_EQ(apm->kNoError, apm->noise_suppression()->Enable(true)); |
| 385 | ASSERT_EQ(apm->kNoError, |
| 386 | apm->noise_suppression()->set_level(NoiseSuppression::kHigh)); |
| 387 | |
| 388 | } else if (strcmp(argv[i], "--ns_very_high") == 0) { |
| 389 | ASSERT_EQ(apm->kNoError, apm->noise_suppression()->Enable(true)); |
| 390 | ASSERT_EQ(apm->kNoError, |
| 391 | apm->noise_suppression()->set_level(NoiseSuppression::kVeryHigh)); |
| 392 | |
bjornv@webrtc.org | 08329f4 | 2012-07-12 21:00:43 +0000 | [diff] [blame] | 393 | } else if (strcmp(argv[i], "--ns_prob_file") == 0) { |
| 394 | i++; |
| 395 | ASSERT_LT(i, argc) << "Specify filename after --ns_prob_file"; |
| 396 | ns_prob_filename = argv[i]; |
| 397 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 398 | } else if (strcmp(argv[i], "-vad") == 0) { |
| 399 | ASSERT_EQ(apm->kNoError, apm->voice_detection()->Enable(true)); |
| 400 | |
andrew@webrtc.org | 8975261 | 2012-10-12 16:41:45 +0000 | [diff] [blame] | 401 | } else if (strcmp(argv[i], "--vad_very_low") == 0) { |
| 402 | ASSERT_EQ(apm->kNoError, apm->voice_detection()->Enable(true)); |
| 403 | ASSERT_EQ(apm->kNoError, |
| 404 | apm->voice_detection()->set_likelihood( |
| 405 | VoiceDetection::kVeryLowLikelihood)); |
| 406 | |
| 407 | } else if (strcmp(argv[i], "--vad_low") == 0) { |
| 408 | ASSERT_EQ(apm->kNoError, apm->voice_detection()->Enable(true)); |
| 409 | ASSERT_EQ(apm->kNoError, |
| 410 | apm->voice_detection()->set_likelihood( |
| 411 | VoiceDetection::kLowLikelihood)); |
| 412 | |
| 413 | } else if (strcmp(argv[i], "--vad_moderate") == 0) { |
| 414 | ASSERT_EQ(apm->kNoError, apm->voice_detection()->Enable(true)); |
| 415 | ASSERT_EQ(apm->kNoError, |
| 416 | apm->voice_detection()->set_likelihood( |
| 417 | VoiceDetection::kModerateLikelihood)); |
| 418 | |
| 419 | } else if (strcmp(argv[i], "--vad_high") == 0) { |
| 420 | ASSERT_EQ(apm->kNoError, apm->voice_detection()->Enable(true)); |
| 421 | ASSERT_EQ(apm->kNoError, |
| 422 | apm->voice_detection()->set_likelihood( |
| 423 | VoiceDetection::kHighLikelihood)); |
| 424 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 425 | } else if (strcmp(argv[i], "--vad_out_file") == 0) { |
| 426 | i++; |
| 427 | ASSERT_LT(i, argc) << "Specify filename after --vad_out_file"; |
| 428 | vad_out_filename = argv[i]; |
| 429 | |
andrew@webrtc.org | 94c7413 | 2011-09-19 15:17:57 +0000 | [diff] [blame] | 430 | } else if (strcmp(argv[i], "--noasm") == 0) { |
| 431 | WebRtc_GetCPUInfo = WebRtc_GetCPUInfoNoASM; |
| 432 | // We need to reinitialize here if components have already been enabled. |
| 433 | ASSERT_EQ(apm->kNoError, apm->Initialize()); |
| 434 | |
andrew@webrtc.org | ca764ab | 2013-10-07 16:44:32 +0000 | [diff] [blame] | 435 | } else if (strcmp(argv[i], "--add_delay") == 0) { |
andrew@webrtc.org | 4b13fc9 | 2011-11-09 19:27:11 +0000 | [diff] [blame] | 436 | i++; |
| 437 | ASSERT_EQ(1, sscanf(argv[i], "%d", &extra_delay_ms)); |
| 438 | |
andrew@webrtc.org | ca764ab | 2013-10-07 16:44:32 +0000 | [diff] [blame] | 439 | } else if (strcmp(argv[i], "--delay") == 0) { |
| 440 | i++; |
| 441 | ASSERT_EQ(1, sscanf(argv[i], "%d", &override_delay_ms)); |
| 442 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 443 | } else if (strcmp(argv[i], "--perf") == 0) { |
| 444 | perf_testing = true; |
| 445 | |
| 446 | } else if (strcmp(argv[i], "--quiet") == 0) { |
| 447 | verbose = false; |
| 448 | progress = false; |
| 449 | |
| 450 | } else if (strcmp(argv[i], "--no_progress") == 0) { |
| 451 | progress = false; |
| 452 | |
andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 453 | } else if (strcmp(argv[i], "--debug_file") == 0) { |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 454 | i++; |
andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 455 | ASSERT_LT(i, argc) << "Specify filename after --debug_file"; |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 456 | ASSERT_EQ(apm->kNoError, apm->StartDebugRecording(argv[i])); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 457 | } else { |
| 458 | FAIL() << "Unrecognized argument " << argv[i]; |
| 459 | } |
| 460 | } |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 461 | // If we're reading a protobuf file, ensure a simulation hasn't also |
| 462 | // been requested (which makes no sense...) |
| 463 | ASSERT_FALSE(pb_filename && simulating); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 464 | |
| 465 | if (verbose) { |
| 466 | printf("Sample rate: %d Hz\n", sample_rate_hz); |
| 467 | printf("Primary channels: %d (in), %d (out)\n", |
| 468 | num_capture_input_channels, |
| 469 | num_capture_output_channels); |
| 470 | printf("Reverse channels: %d \n", num_render_channels); |
| 471 | } |
| 472 | |
kjellander@webrtc.org | 10abe25 | 2012-12-17 18:28:07 +0000 | [diff] [blame] | 473 | const std::string out_path = webrtc::test::OutputPath(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 474 | const char far_file_default[] = "apm_far.pcm"; |
| 475 | const char near_file_default[] = "apm_near.pcm"; |
kjellander@webrtc.org | 10abe25 | 2012-12-17 18:28:07 +0000 | [diff] [blame] | 476 | const std::string out_file_default = out_path + "out.pcm"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 477 | const char event_filename[] = "apm_event.dat"; |
| 478 | const char delay_filename[] = "apm_delay.dat"; |
| 479 | const char drift_filename[] = "apm_drift.dat"; |
kjellander@webrtc.org | 10abe25 | 2012-12-17 18:28:07 +0000 | [diff] [blame] | 480 | const std::string vad_file_default = out_path + "vad_out.dat"; |
| 481 | const std::string ns_prob_file_default = out_path + "ns_prob.dat"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 482 | |
| 483 | if (!simulating) { |
| 484 | far_filename = far_file_default; |
| 485 | near_filename = near_file_default; |
| 486 | } |
| 487 | |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 488 | if (!out_filename) { |
kjellander@webrtc.org | 10abe25 | 2012-12-17 18:28:07 +0000 | [diff] [blame] | 489 | out_filename = out_file_default.c_str(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 490 | } |
| 491 | |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 492 | if (!vad_out_filename) { |
kjellander@webrtc.org | 10abe25 | 2012-12-17 18:28:07 +0000 | [diff] [blame] | 493 | vad_out_filename = vad_file_default.c_str(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 494 | } |
| 495 | |
bjornv@webrtc.org | 08329f4 | 2012-07-12 21:00:43 +0000 | [diff] [blame] | 496 | if (!ns_prob_filename) { |
kjellander@webrtc.org | 10abe25 | 2012-12-17 18:28:07 +0000 | [diff] [blame] | 497 | ns_prob_filename = ns_prob_file_default.c_str(); |
bjornv@webrtc.org | 08329f4 | 2012-07-12 21:00:43 +0000 | [diff] [blame] | 498 | } |
| 499 | |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 500 | FILE* pb_file = NULL; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 501 | FILE* far_file = NULL; |
| 502 | FILE* near_file = NULL; |
| 503 | FILE* out_file = NULL; |
| 504 | FILE* event_file = NULL; |
| 505 | FILE* delay_file = NULL; |
| 506 | FILE* drift_file = NULL; |
| 507 | FILE* vad_out_file = NULL; |
bjornv@webrtc.org | 08329f4 | 2012-07-12 21:00:43 +0000 | [diff] [blame] | 508 | FILE* ns_prob_file = NULL; |
bjornv@google.com | c4b939c | 2011-07-13 08:09:56 +0000 | [diff] [blame] | 509 | FILE* aecm_echo_path_in_file = NULL; |
| 510 | FILE* aecm_echo_path_out_file = NULL; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 511 | |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 512 | if (pb_filename) { |
| 513 | pb_file = fopen(pb_filename, "rb"); |
| 514 | ASSERT_TRUE(NULL != pb_file) << "Unable to open protobuf file " |
| 515 | << pb_filename; |
| 516 | } else { |
| 517 | if (far_filename) { |
| 518 | far_file = fopen(far_filename, "rb"); |
| 519 | ASSERT_TRUE(NULL != far_file) << "Unable to open far-end audio file " |
| 520 | << far_filename; |
| 521 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 522 | |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 523 | near_file = fopen(near_filename, "rb"); |
| 524 | ASSERT_TRUE(NULL != near_file) << "Unable to open near-end audio file " |
| 525 | << near_filename; |
| 526 | if (!simulating) { |
| 527 | event_file = fopen(event_filename, "rb"); |
| 528 | ASSERT_TRUE(NULL != event_file) << "Unable to open event file " |
| 529 | << event_filename; |
| 530 | |
| 531 | delay_file = fopen(delay_filename, "rb"); |
| 532 | ASSERT_TRUE(NULL != delay_file) << "Unable to open buffer file " |
| 533 | << delay_filename; |
| 534 | |
| 535 | drift_file = fopen(drift_filename, "rb"); |
| 536 | ASSERT_TRUE(NULL != drift_file) << "Unable to open drift file " |
| 537 | << drift_filename; |
| 538 | } |
| 539 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 540 | |
| 541 | out_file = fopen(out_filename, "wb"); |
| 542 | ASSERT_TRUE(NULL != out_file) << "Unable to open output audio file " |
| 543 | << out_filename; |
| 544 | |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 545 | int near_size_bytes = 0; |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 546 | if (pb_file) { |
| 547 | struct stat st; |
| 548 | stat(pb_filename, &st); |
| 549 | // Crude estimate, but should be good enough. |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 550 | near_size_bytes = st.st_size / 3; |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 551 | } else { |
| 552 | struct stat st; |
| 553 | stat(near_filename, &st); |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 554 | near_size_bytes = st.st_size; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | if (apm->voice_detection()->is_enabled()) { |
| 558 | vad_out_file = fopen(vad_out_filename, "wb"); |
| 559 | ASSERT_TRUE(NULL != vad_out_file) << "Unable to open VAD output file " |
| 560 | << vad_out_file; |
| 561 | } |
| 562 | |
bjornv@webrtc.org | 08329f4 | 2012-07-12 21:00:43 +0000 | [diff] [blame] | 563 | if (apm->noise_suppression()->is_enabled()) { |
| 564 | ns_prob_file = fopen(ns_prob_filename, "wb"); |
| 565 | ASSERT_TRUE(NULL != ns_prob_file) << "Unable to open NS output file " |
| 566 | << ns_prob_file; |
| 567 | } |
| 568 | |
bjornv@google.com | c4b939c | 2011-07-13 08:09:56 +0000 | [diff] [blame] | 569 | if (aecm_echo_path_in_filename != NULL) { |
| 570 | aecm_echo_path_in_file = fopen(aecm_echo_path_in_filename, "rb"); |
| 571 | ASSERT_TRUE(NULL != aecm_echo_path_in_file) << "Unable to open file " |
| 572 | << aecm_echo_path_in_filename; |
| 573 | |
ajm@google.com | 22e6515 | 2011-07-18 18:03:01 +0000 | [diff] [blame] | 574 | const size_t path_size = |
| 575 | apm->echo_control_mobile()->echo_path_size_bytes(); |
andrew@webrtc.org | 3119ecf | 2011-11-01 17:00:18 +0000 | [diff] [blame] | 576 | scoped_array<char> echo_path(new char[path_size]); |
| 577 | ASSERT_EQ(path_size, fread(echo_path.get(), |
| 578 | sizeof(char), |
bjornv@google.com | c4b939c | 2011-07-13 08:09:56 +0000 | [diff] [blame] | 579 | path_size, |
| 580 | aecm_echo_path_in_file)); |
| 581 | EXPECT_EQ(apm->kNoError, |
andrew@webrtc.org | 3119ecf | 2011-11-01 17:00:18 +0000 | [diff] [blame] | 582 | apm->echo_control_mobile()->SetEchoPath(echo_path.get(), |
| 583 | path_size)); |
bjornv@google.com | c4b939c | 2011-07-13 08:09:56 +0000 | [diff] [blame] | 584 | fclose(aecm_echo_path_in_file); |
| 585 | aecm_echo_path_in_file = NULL; |
| 586 | } |
| 587 | |
| 588 | if (aecm_echo_path_out_filename != NULL) { |
| 589 | aecm_echo_path_out_file = fopen(aecm_echo_path_out_filename, "wb"); |
| 590 | ASSERT_TRUE(NULL != aecm_echo_path_out_file) << "Unable to open file " |
| 591 | << aecm_echo_path_out_filename; |
bjornv@google.com | c4b939c | 2011-07-13 08:09:56 +0000 | [diff] [blame] | 592 | } |
| 593 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 594 | size_t read_count = 0; |
| 595 | int reverse_count = 0; |
| 596 | int primary_count = 0; |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 597 | int near_read_bytes = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 598 | TickInterval acc_ticks; |
| 599 | |
| 600 | AudioFrame far_frame; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 601 | AudioFrame near_frame; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 602 | |
| 603 | int delay_ms = 0; |
| 604 | int drift_samples = 0; |
| 605 | int capture_level = 127; |
| 606 | int8_t stream_has_voice = 0; |
bjornv@webrtc.org | 08329f4 | 2012-07-12 21:00:43 +0000 | [diff] [blame] | 607 | float ns_speech_prob = 0.0f; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 608 | |
| 609 | TickTime t0 = TickTime::Now(); |
| 610 | TickTime t1 = t0; |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 611 | int64_t max_time_us = 0; |
| 612 | int64_t max_time_reverse_us = 0; |
| 613 | int64_t min_time_us = 1e6; |
| 614 | int64_t min_time_reverse_us = 1e6; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 615 | |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 616 | // TODO(ajm): Ideally we would refactor this block into separate functions, |
| 617 | // but for now we want to share the variables. |
| 618 | if (pb_file) { |
| 619 | Event event_msg; |
| 620 | while (ReadMessageFromFile(pb_file, &event_msg)) { |
| 621 | std::ostringstream trace_stream; |
| 622 | trace_stream << "Processed frames: " << reverse_count << " (reverse), " |
| 623 | << primary_count << " (primary)"; |
| 624 | SCOPED_TRACE(trace_stream.str()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 625 | |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 626 | if (event_msg.type() == Event::INIT) { |
| 627 | ASSERT_TRUE(event_msg.has_init()); |
| 628 | const Init msg = event_msg.init(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 629 | |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 630 | ASSERT_TRUE(msg.has_sample_rate()); |
| 631 | ASSERT_EQ(apm->kNoError, |
| 632 | apm->set_sample_rate_hz(msg.sample_rate())); |
| 633 | |
| 634 | ASSERT_TRUE(msg.has_device_sample_rate()); |
| 635 | ASSERT_EQ(apm->kNoError, |
| 636 | apm->echo_cancellation()->set_device_sample_rate_hz( |
| 637 | msg.device_sample_rate())); |
| 638 | |
| 639 | ASSERT_TRUE(msg.has_num_input_channels()); |
| 640 | ASSERT_TRUE(msg.has_num_output_channels()); |
| 641 | ASSERT_EQ(apm->kNoError, |
| 642 | apm->set_num_channels(msg.num_input_channels(), |
| 643 | msg.num_output_channels())); |
| 644 | |
| 645 | ASSERT_TRUE(msg.has_num_reverse_channels()); |
| 646 | ASSERT_EQ(apm->kNoError, |
| 647 | apm->set_num_reverse_channels(msg.num_reverse_channels())); |
| 648 | |
| 649 | samples_per_channel = msg.sample_rate() / 100; |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 650 | far_frame.sample_rate_hz_ = msg.sample_rate(); |
| 651 | far_frame.samples_per_channel_ = samples_per_channel; |
| 652 | far_frame.num_channels_ = msg.num_reverse_channels(); |
| 653 | near_frame.sample_rate_hz_ = msg.sample_rate(); |
| 654 | near_frame.samples_per_channel_ = samples_per_channel; |
andrew@webrtc.org | bafdae3 | 2013-01-11 23:11:29 +0000 | [diff] [blame] | 655 | near_frame.num_channels_ = msg.num_input_channels(); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 656 | |
| 657 | if (verbose) { |
| 658 | printf("Init at frame: %d (primary), %d (reverse)\n", |
| 659 | primary_count, reverse_count); |
andrew@webrtc.org | ba028a3 | 2011-11-23 20:37:12 +0000 | [diff] [blame] | 660 | printf(" Sample rate: %d Hz\n", msg.sample_rate()); |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 661 | printf(" Primary channels: %d (in), %d (out)\n", |
| 662 | msg.num_input_channels(), |
| 663 | msg.num_output_channels()); |
| 664 | printf(" Reverse channels: %d \n", msg.num_reverse_channels()); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | } else if (event_msg.type() == Event::REVERSE_STREAM) { |
| 668 | ASSERT_TRUE(event_msg.has_reverse_stream()); |
| 669 | const ReverseStream msg = event_msg.reverse_stream(); |
| 670 | reverse_count++; |
| 671 | |
| 672 | ASSERT_TRUE(msg.has_data()); |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 673 | ASSERT_EQ(sizeof(int16_t) * samples_per_channel * |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 674 | far_frame.num_channels_, msg.data().size()); |
| 675 | memcpy(far_frame.data_, msg.data().data(), msg.data().size()); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 676 | |
| 677 | if (perf_testing) { |
| 678 | t0 = TickTime::Now(); |
| 679 | } |
| 680 | |
| 681 | ASSERT_EQ(apm->kNoError, |
| 682 | apm->AnalyzeReverseStream(&far_frame)); |
| 683 | |
| 684 | if (perf_testing) { |
| 685 | t1 = TickTime::Now(); |
| 686 | TickInterval tick_diff = t1 - t0; |
| 687 | acc_ticks += tick_diff; |
| 688 | if (tick_diff.Microseconds() > max_time_reverse_us) { |
| 689 | max_time_reverse_us = tick_diff.Microseconds(); |
| 690 | } |
| 691 | if (tick_diff.Microseconds() < min_time_reverse_us) { |
| 692 | min_time_reverse_us = tick_diff.Microseconds(); |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | } else if (event_msg.type() == Event::STREAM) { |
| 697 | ASSERT_TRUE(event_msg.has_stream()); |
| 698 | const Stream msg = event_msg.stream(); |
| 699 | primary_count++; |
| 700 | |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 701 | // ProcessStream could have changed this for the output frame. |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 702 | near_frame.num_channels_ = apm->num_input_channels(); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 703 | |
| 704 | ASSERT_TRUE(msg.has_input_data()); |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 705 | ASSERT_EQ(sizeof(int16_t) * samples_per_channel * |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 706 | near_frame.num_channels_, msg.input_data().size()); |
| 707 | memcpy(near_frame.data_, |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 708 | msg.input_data().data(), |
| 709 | msg.input_data().size()); |
| 710 | |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 711 | near_read_bytes += msg.input_data().size(); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 712 | if (progress && primary_count % 100 == 0) { |
| 713 | printf("%.0f%% complete\r", |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 714 | (near_read_bytes * 100.0) / near_size_bytes); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 715 | fflush(stdout); |
| 716 | } |
| 717 | |
| 718 | if (perf_testing) { |
| 719 | t0 = TickTime::Now(); |
| 720 | } |
| 721 | |
| 722 | ASSERT_EQ(apm->kNoError, |
| 723 | apm->gain_control()->set_stream_analog_level(msg.level())); |
andrew@webrtc.org | ca764ab | 2013-10-07 16:44:32 +0000 | [diff] [blame] | 724 | delay_ms = msg.delay() + extra_delay_ms; |
| 725 | if (override_delay_ms) { |
| 726 | delay_ms = override_delay_ms; |
| 727 | } |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 728 | ASSERT_EQ(apm->kNoError, |
andrew@webrtc.org | ca764ab | 2013-10-07 16:44:32 +0000 | [diff] [blame] | 729 | apm->set_stream_delay_ms(delay_ms)); |
andrew@webrtc.org | 6be1e93 | 2013-03-01 18:47:28 +0000 | [diff] [blame] | 730 | apm->echo_cancellation()->set_stream_drift_samples(msg.drift()); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 731 | |
| 732 | int err = apm->ProcessStream(&near_frame); |
| 733 | if (err == apm->kBadStreamParameterWarning) { |
| 734 | printf("Bad parameter warning. %s\n", trace_stream.str().c_str()); |
| 735 | } |
| 736 | ASSERT_TRUE(err == apm->kNoError || |
| 737 | err == apm->kBadStreamParameterWarning); |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 738 | ASSERT_TRUE(near_frame.num_channels_ == apm->num_output_channels()); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 739 | |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 740 | stream_has_voice = |
| 741 | static_cast<int8_t>(apm->voice_detection()->stream_has_voice()); |
| 742 | if (vad_out_file != NULL) { |
| 743 | ASSERT_EQ(1u, fwrite(&stream_has_voice, |
| 744 | sizeof(stream_has_voice), |
| 745 | 1, |
| 746 | vad_out_file)); |
| 747 | } |
| 748 | |
bjornv@webrtc.org | 08329f4 | 2012-07-12 21:00:43 +0000 | [diff] [blame] | 749 | if (ns_prob_file != NULL) { |
| 750 | ns_speech_prob = apm->noise_suppression()->speech_probability(); |
| 751 | ASSERT_EQ(1u, fwrite(&ns_speech_prob, |
| 752 | sizeof(ns_speech_prob), |
| 753 | 1, |
| 754 | ns_prob_file)); |
| 755 | } |
| 756 | |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 757 | if (perf_testing) { |
| 758 | t1 = TickTime::Now(); |
| 759 | TickInterval tick_diff = t1 - t0; |
| 760 | acc_ticks += tick_diff; |
| 761 | if (tick_diff.Microseconds() > max_time_us) { |
| 762 | max_time_us = tick_diff.Microseconds(); |
| 763 | } |
| 764 | if (tick_diff.Microseconds() < min_time_us) { |
| 765 | min_time_us = tick_diff.Microseconds(); |
| 766 | } |
| 767 | } |
| 768 | |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 769 | size_t size = samples_per_channel * near_frame.num_channels_; |
| 770 | ASSERT_EQ(size, fwrite(near_frame.data_, |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 771 | sizeof(int16_t), |
| 772 | size, |
| 773 | out_file)); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 774 | } |
| 775 | } |
| 776 | |
| 777 | ASSERT_TRUE(feof(pb_file)); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 778 | |
| 779 | } else { |
bjornv@google.com | a2c6ea0 | 2011-09-27 08:04:45 +0000 | [diff] [blame] | 780 | enum Events { |
| 781 | kInitializeEvent, |
| 782 | kRenderEvent, |
| 783 | kCaptureEvent, |
| 784 | kResetEventDeprecated |
| 785 | }; |
| 786 | int16_t event = 0; |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 787 | while (simulating || feof(event_file) == 0) { |
| 788 | std::ostringstream trace_stream; |
| 789 | trace_stream << "Processed frames: " << reverse_count << " (reverse), " |
| 790 | << primary_count << " (primary)"; |
| 791 | SCOPED_TRACE(trace_stream.str()); |
| 792 | |
| 793 | if (simulating) { |
| 794 | if (far_file == NULL) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 795 | event = kCaptureEvent; |
| 796 | } else { |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 797 | if (event == kRenderEvent) { |
| 798 | event = kCaptureEvent; |
| 799 | } else { |
| 800 | event = kRenderEvent; |
| 801 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 802 | } |
| 803 | } else { |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 804 | read_count = fread(&event, sizeof(event), 1, event_file); |
| 805 | if (read_count != 1) { |
| 806 | break; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 807 | } |
| 808 | } |
| 809 | |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 810 | far_frame.sample_rate_hz_ = sample_rate_hz; |
| 811 | far_frame.samples_per_channel_ = samples_per_channel; |
| 812 | far_frame.num_channels_ = num_render_channels; |
| 813 | near_frame.sample_rate_hz_ = sample_rate_hz; |
| 814 | near_frame.samples_per_channel_ = samples_per_channel; |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 815 | |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 816 | if (event == kInitializeEvent || event == kResetEventDeprecated) { |
| 817 | ASSERT_EQ(1u, |
| 818 | fread(&sample_rate_hz, sizeof(sample_rate_hz), 1, event_file)); |
| 819 | samples_per_channel = sample_rate_hz / 100; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 820 | |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 821 | ASSERT_EQ(1u, |
| 822 | fread(&device_sample_rate_hz, |
| 823 | sizeof(device_sample_rate_hz), |
| 824 | 1, |
| 825 | event_file)); |
| 826 | |
| 827 | ASSERT_EQ(apm->kNoError, |
| 828 | apm->set_sample_rate_hz(sample_rate_hz)); |
| 829 | |
| 830 | ASSERT_EQ(apm->kNoError, |
| 831 | apm->echo_cancellation()->set_device_sample_rate_hz( |
| 832 | device_sample_rate_hz)); |
| 833 | |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 834 | far_frame.sample_rate_hz_ = sample_rate_hz; |
| 835 | far_frame.samples_per_channel_ = samples_per_channel; |
| 836 | far_frame.num_channels_ = num_render_channels; |
| 837 | near_frame.sample_rate_hz_ = sample_rate_hz; |
| 838 | near_frame.samples_per_channel_ = samples_per_channel; |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 839 | |
| 840 | if (verbose) { |
| 841 | printf("Init at frame: %d (primary), %d (reverse)\n", |
| 842 | primary_count, reverse_count); |
| 843 | printf(" Sample rate: %d Hz\n", sample_rate_hz); |
| 844 | } |
| 845 | |
| 846 | } else if (event == kRenderEvent) { |
| 847 | reverse_count++; |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 848 | |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 849 | size_t size = samples_per_channel * num_render_channels; |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 850 | read_count = fread(far_frame.data_, |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 851 | sizeof(int16_t), |
| 852 | size, |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 853 | far_file); |
| 854 | |
| 855 | if (simulating) { |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 856 | if (read_count != size) { |
andrew@webrtc.org | 94c7413 | 2011-09-19 15:17:57 +0000 | [diff] [blame] | 857 | // Read an equal amount from the near file to avoid errors due to |
| 858 | // not reaching end-of-file. |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 859 | EXPECT_EQ(0, fseek(near_file, read_count * sizeof(int16_t), |
andrew@webrtc.org | 94c7413 | 2011-09-19 15:17:57 +0000 | [diff] [blame] | 860 | SEEK_CUR)); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 861 | break; // This is expected. |
| 862 | } |
| 863 | } else { |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 864 | ASSERT_EQ(size, read_count); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | if (perf_testing) { |
| 868 | t0 = TickTime::Now(); |
| 869 | } |
| 870 | |
| 871 | ASSERT_EQ(apm->kNoError, |
| 872 | apm->AnalyzeReverseStream(&far_frame)); |
| 873 | |
| 874 | if (perf_testing) { |
| 875 | t1 = TickTime::Now(); |
| 876 | TickInterval tick_diff = t1 - t0; |
| 877 | acc_ticks += tick_diff; |
| 878 | if (tick_diff.Microseconds() > max_time_reverse_us) { |
| 879 | max_time_reverse_us = tick_diff.Microseconds(); |
| 880 | } |
| 881 | if (tick_diff.Microseconds() < min_time_reverse_us) { |
| 882 | min_time_reverse_us = tick_diff.Microseconds(); |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | } else if (event == kCaptureEvent) { |
| 887 | primary_count++; |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 888 | near_frame.num_channels_ = num_capture_input_channels; |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 889 | |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 890 | size_t size = samples_per_channel * num_capture_input_channels; |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 891 | read_count = fread(near_frame.data_, |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 892 | sizeof(int16_t), |
| 893 | size, |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 894 | near_file); |
| 895 | |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 896 | near_read_bytes += read_count * sizeof(int16_t); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 897 | if (progress && primary_count % 100 == 0) { |
| 898 | printf("%.0f%% complete\r", |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 899 | (near_read_bytes * 100.0) / near_size_bytes); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 900 | fflush(stdout); |
| 901 | } |
| 902 | if (simulating) { |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 903 | if (read_count != size) { |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 904 | break; // This is expected. |
| 905 | } |
| 906 | |
| 907 | delay_ms = 0; |
| 908 | drift_samples = 0; |
| 909 | } else { |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 910 | ASSERT_EQ(size, read_count); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 911 | |
| 912 | // TODO(ajm): sizeof(delay_ms) for current files? |
| 913 | ASSERT_EQ(1u, |
| 914 | fread(&delay_ms, 2, 1, delay_file)); |
| 915 | ASSERT_EQ(1u, |
| 916 | fread(&drift_samples, sizeof(drift_samples), 1, drift_file)); |
| 917 | } |
| 918 | |
andrew@webrtc.org | bafdae3 | 2013-01-11 23:11:29 +0000 | [diff] [blame] | 919 | if (apm->gain_control()->is_enabled() && |
| 920 | apm->gain_control()->mode() == GainControl::kAdaptiveAnalog) { |
andrew@webrtc.org | 8186534 | 2012-10-27 00:28:27 +0000 | [diff] [blame] | 921 | SimulateMic(capture_level, &near_frame); |
| 922 | } |
| 923 | |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 924 | if (perf_testing) { |
| 925 | t0 = TickTime::Now(); |
| 926 | } |
| 927 | |
andrew@webrtc.org | bafdae3 | 2013-01-11 23:11:29 +0000 | [diff] [blame] | 928 | const int capture_level_in = capture_level; |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 929 | ASSERT_EQ(apm->kNoError, |
| 930 | apm->gain_control()->set_stream_analog_level(capture_level)); |
andrew@webrtc.org | ca764ab | 2013-10-07 16:44:32 +0000 | [diff] [blame] | 931 | delay_ms += extra_delay_ms; |
| 932 | if (override_delay_ms) { |
| 933 | delay_ms = override_delay_ms; |
| 934 | } |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 935 | ASSERT_EQ(apm->kNoError, |
andrew@webrtc.org | ca764ab | 2013-10-07 16:44:32 +0000 | [diff] [blame] | 936 | apm->set_stream_delay_ms(delay_ms)); |
andrew@webrtc.org | 6be1e93 | 2013-03-01 18:47:28 +0000 | [diff] [blame] | 937 | apm->echo_cancellation()->set_stream_drift_samples(drift_samples); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 938 | |
| 939 | int err = apm->ProcessStream(&near_frame); |
| 940 | if (err == apm->kBadStreamParameterWarning) { |
| 941 | printf("Bad parameter warning. %s\n", trace_stream.str().c_str()); |
| 942 | } |
| 943 | ASSERT_TRUE(err == apm->kNoError || |
| 944 | err == apm->kBadStreamParameterWarning); |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 945 | ASSERT_TRUE(near_frame.num_channels_ == apm->num_output_channels()); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 946 | |
| 947 | capture_level = apm->gain_control()->stream_analog_level(); |
| 948 | |
| 949 | stream_has_voice = |
| 950 | static_cast<int8_t>(apm->voice_detection()->stream_has_voice()); |
| 951 | if (vad_out_file != NULL) { |
| 952 | ASSERT_EQ(1u, fwrite(&stream_has_voice, |
| 953 | sizeof(stream_has_voice), |
| 954 | 1, |
| 955 | vad_out_file)); |
| 956 | } |
| 957 | |
bjornv@webrtc.org | 08329f4 | 2012-07-12 21:00:43 +0000 | [diff] [blame] | 958 | if (ns_prob_file != NULL) { |
| 959 | ns_speech_prob = apm->noise_suppression()->speech_probability(); |
| 960 | ASSERT_EQ(1u, fwrite(&ns_speech_prob, |
| 961 | sizeof(ns_speech_prob), |
| 962 | 1, |
| 963 | ns_prob_file)); |
| 964 | } |
| 965 | |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 966 | if (apm->gain_control()->mode() != GainControl::kAdaptiveAnalog) { |
| 967 | ASSERT_EQ(capture_level_in, capture_level); |
| 968 | } |
| 969 | |
| 970 | if (perf_testing) { |
| 971 | t1 = TickTime::Now(); |
| 972 | TickInterval tick_diff = t1 - t0; |
| 973 | acc_ticks += tick_diff; |
| 974 | if (tick_diff.Microseconds() > max_time_us) { |
| 975 | max_time_us = tick_diff.Microseconds(); |
| 976 | } |
| 977 | if (tick_diff.Microseconds() < min_time_us) { |
| 978 | min_time_us = tick_diff.Microseconds(); |
| 979 | } |
| 980 | } |
| 981 | |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 982 | size = samples_per_channel * near_frame.num_channels_; |
| 983 | ASSERT_EQ(size, fwrite(near_frame.data_, |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 984 | sizeof(int16_t), |
| 985 | size, |
| 986 | out_file)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 987 | } |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 988 | else { |
| 989 | FAIL() << "Event " << event << " is unrecognized"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 990 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 991 | } |
| 992 | } |
andrew@webrtc.org | 94c7413 | 2011-09-19 15:17:57 +0000 | [diff] [blame] | 993 | printf("100%% complete\r"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 994 | |
bjornv@google.com | c4b939c | 2011-07-13 08:09:56 +0000 | [diff] [blame] | 995 | if (aecm_echo_path_out_file != NULL) { |
ajm@google.com | 22e6515 | 2011-07-18 18:03:01 +0000 | [diff] [blame] | 996 | const size_t path_size = |
| 997 | apm->echo_control_mobile()->echo_path_size_bytes(); |
andrew@webrtc.org | 3119ecf | 2011-11-01 17:00:18 +0000 | [diff] [blame] | 998 | scoped_array<char> echo_path(new char[path_size]); |
| 999 | apm->echo_control_mobile()->GetEchoPath(echo_path.get(), path_size); |
| 1000 | ASSERT_EQ(path_size, fwrite(echo_path.get(), |
| 1001 | sizeof(char), |
bjornv@google.com | c4b939c | 2011-07-13 08:09:56 +0000 | [diff] [blame] | 1002 | path_size, |
| 1003 | aecm_echo_path_out_file)); |
| 1004 | fclose(aecm_echo_path_out_file); |
| 1005 | aecm_echo_path_out_file = NULL; |
| 1006 | } |
| 1007 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1008 | if (verbose) { |
| 1009 | printf("\nProcessed frames: %d (primary), %d (reverse)\n", |
| 1010 | primary_count, reverse_count); |
andrew@webrtc.org | 94c7413 | 2011-09-19 15:17:57 +0000 | [diff] [blame] | 1011 | |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 1012 | if (apm->level_estimator()->is_enabled()) { |
| 1013 | printf("\n--Level metrics--\n"); |
| 1014 | printf("RMS: %d dBFS\n", -apm->level_estimator()->RMS()); |
| 1015 | } |
andrew@webrtc.org | 94c7413 | 2011-09-19 15:17:57 +0000 | [diff] [blame] | 1016 | if (apm->echo_cancellation()->are_metrics_enabled()) { |
| 1017 | EchoCancellation::Metrics metrics; |
| 1018 | apm->echo_cancellation()->GetMetrics(&metrics); |
| 1019 | printf("\n--Echo metrics--\n"); |
| 1020 | printf("(avg, max, min)\n"); |
| 1021 | printf("ERL: "); |
| 1022 | PrintStat(metrics.echo_return_loss); |
| 1023 | printf("ERLE: "); |
| 1024 | PrintStat(metrics.echo_return_loss_enhancement); |
| 1025 | printf("ANLP: "); |
| 1026 | PrintStat(metrics.a_nlp); |
| 1027 | } |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 1028 | if (apm->echo_cancellation()->is_delay_logging_enabled()) { |
| 1029 | int median = 0; |
| 1030 | int std = 0; |
| 1031 | apm->echo_cancellation()->GetDelayMetrics(&median, &std); |
| 1032 | printf("\n--Delay metrics--\n"); |
| 1033 | printf("Median: %3d\n", median); |
| 1034 | printf("Standard deviation: %3d\n", std); |
| 1035 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 1038 | if (!pb_file) { |
| 1039 | int8_t temp_int8; |
| 1040 | if (far_file) { |
| 1041 | read_count = fread(&temp_int8, sizeof(temp_int8), 1, far_file); |
| 1042 | EXPECT_NE(0, feof(far_file)) << "Far-end file not fully processed"; |
| 1043 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1044 | |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 1045 | read_count = fread(&temp_int8, sizeof(temp_int8), 1, near_file); |
| 1046 | EXPECT_NE(0, feof(near_file)) << "Near-end file not fully processed"; |
| 1047 | |
| 1048 | if (!simulating) { |
| 1049 | read_count = fread(&temp_int8, sizeof(temp_int8), 1, event_file); |
| 1050 | EXPECT_NE(0, feof(event_file)) << "Event file not fully processed"; |
| 1051 | read_count = fread(&temp_int8, sizeof(temp_int8), 1, delay_file); |
| 1052 | EXPECT_NE(0, feof(delay_file)) << "Delay file not fully processed"; |
| 1053 | read_count = fread(&temp_int8, sizeof(temp_int8), 1, drift_file); |
| 1054 | EXPECT_NE(0, feof(drift_file)) << "Drift file not fully processed"; |
| 1055 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | if (perf_testing) { |
| 1059 | if (primary_count > 0) { |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 1060 | int64_t exec_time = acc_ticks.Milliseconds(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1061 | printf("\nTotal time: %.3f s, file time: %.2f s\n", |
| 1062 | exec_time * 0.001, primary_count * 0.01); |
| 1063 | printf("Time per frame: %.3f ms (average), %.3f ms (max)," |
| 1064 | " %.3f ms (min)\n", |
| 1065 | (exec_time * 1.0) / primary_count, |
| 1066 | (max_time_us + max_time_reverse_us) / 1000.0, |
| 1067 | (min_time_us + min_time_reverse_us) / 1000.0); |
kma@webrtc.org | 0e73950 | 2012-12-07 15:26:28 +0000 | [diff] [blame] | 1068 | // Record the results with Perf test tools. |
kjellander@webrtc.org | 00ab7cf | 2013-02-11 12:33:03 +0000 | [diff] [blame] | 1069 | webrtc::test::PrintResult("audioproc", "", "time_per_10ms_frame", |
kma@webrtc.org | 0e73950 | 2012-12-07 15:26:28 +0000 | [diff] [blame] | 1070 | (exec_time * 1000) / primary_count, "us", false); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1071 | } else { |
| 1072 | printf("Warning: no capture frames\n"); |
| 1073 | } |
| 1074 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1075 | } |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 1076 | } // namespace |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1077 | |
| 1078 | int main(int argc, char* argv[]) |
| 1079 | { |
| 1080 | void_main(argc, argv); |
| 1081 | |
andrew@webrtc.org | 6423509 | 2011-08-19 21:22:08 +0000 | [diff] [blame] | 1082 | // Optional, but removes memory leak noise from Valgrind. |
| 1083 | google::protobuf::ShutdownProtobufLibrary(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1084 | return 0; |
| 1085 | } |