blob: f326b4c18315608750e4cbd190aa2c9532887c54 [file] [log] [blame]
andrew@webrtc.orgcb181212011-10-26 00:27:17 +00001/*
2 * Copyright (c) 2011 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// Commandline tool to unpack audioproc debug files.
12//
13// The debug files are dumped as protobuf blobs. For analysis, it's necessary
14// to unpack the file into its component parts: audio and other data.
15
Yves Gerey3e707812018-11-28 16:47:49 +010016#include <inttypes.h>
17#include <stdint.h>
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000018#include <stdio.h>
Yves Gerey3e707812018-11-28 16:47:49 +010019#include <stdlib.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020020
kwiberg62eaacf2016-02-17 06:39:05 -080021#include <memory>
Yves Gerey3e707812018-11-28 16:47:49 +010022#include <string>
Fredrik Hernqvist3be9da32019-05-21 12:23:31 +020023#include <vector>
kwiberg62eaacf2016-02-17 06:39:05 -080024
Mirko Bonadei9d962092019-07-04 10:19:10 +020025#include "absl/flags/flag.h"
26#include "absl/flags/parse.h"
Fredrik Hernqvist3be9da32019-05-21 12:23:31 +020027#include "api/function_view.h"
Yves Gerey3e707812018-11-28 16:47:49 +010028#include "common_audio/wav_file.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "modules/audio_processing/test/protobuf_utils.h"
30#include "modules/audio_processing/test/test_utils.h"
31#include "rtc_base/flags.h"
32#include "rtc_base/format_macros.h"
33#include "rtc_base/ignore_wundef.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020034#include "rtc_base/strings/string_builder.h"
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000035
kwiberg77eab702016-09-28 17:42:01 -070036RTC_PUSH_IGNORING_WUNDEF()
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020037#include "modules/audio_processing/debug.pb.h"
kwiberg77eab702016-09-28 17:42:01 -070038RTC_POP_IGNORING_WUNDEF()
39
Mirko Bonadei9d962092019-07-04 10:19:10 +020040ABSL_FLAG(std::string,
41 input_file,
42 "input",
43 "The name of the input stream file.");
44ABSL_FLAG(std::string,
45 output_file,
46 "ref_out",
47 "The name of the reference output stream file.");
48ABSL_FLAG(std::string,
49 reverse_file,
50 "reverse",
51 "The name of the reverse input stream file.");
52ABSL_FLAG(std::string,
53 delay_file,
54 "delay.int32",
55 "The name of the delay file.");
56ABSL_FLAG(std::string,
57 drift_file,
58 "drift.int32",
59 "The name of the drift file.");
60ABSL_FLAG(std::string,
61 level_file,
62 "level.int32",
63 "The name of the level file.");
64ABSL_FLAG(std::string,
65 keypress_file,
66 "keypress.bool",
67 "The name of the keypress file.");
68ABSL_FLAG(std::string,
69 callorder_file,
70 "callorder",
71 "The name of the render/capture call order file.");
72ABSL_FLAG(std::string,
73 settings_file,
74 "settings.txt",
75 "The name of the settings file.");
76ABSL_FLAG(bool,
77 full,
78 false,
79 "Unpack the full set of files (normally not needed).");
80ABSL_FLAG(bool, raw, false, "Write raw data instead of a WAV file.");
81ABSL_FLAG(bool,
82 text,
83 false,
84 "Write non-audio files as text files instead of binary files.");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000085
Yves Gerey665174f2018-06-19 15:03:05 +020086#define PRINT_CONFIG(field_name) \
87 if (msg.has_##field_name()) { \
Minyue13b96ba2015-10-03 00:39:14 +020088 fprintf(settings_file, " " #field_name ": %d\n", msg.field_name()); \
89 }
90
Alex Loiko5feb30e2018-04-16 13:52:32 +020091#define PRINT_CONFIG_FLOAT(field_name) \
92 if (msg.has_##field_name()) { \
93 fprintf(settings_file, " " #field_name ": %f\n", msg.field_name()); \
94 }
95
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000096namespace webrtc {
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000097
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000098using audioproc::Event;
Jonas Olssona4d87372019-07-05 19:08:33 +020099using audioproc::Init;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000100using audioproc::ReverseStream;
101using audioproc::Stream;
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000102
Sam Zackrissonf61475d2018-05-11 11:34:49 +0200103namespace {
104
Yves Gerey665174f2018-06-19 15:03:05 +0200105void WriteData(const void* data,
106 size_t size,
107 FILE* file,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000108 const std::string& filename) {
109 if (fwrite(data, size, 1, file) != 1) {
110 printf("Error when writing to %s\n", filename.c_str());
111 exit(1);
112 }
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000113}
114
Sam Zackrissonf61475d2018-05-11 11:34:49 +0200115void WriteCallOrderData(const bool render_call,
116 FILE* file,
117 const std::string& filename) {
118 const char call_type = render_call ? 'r' : 'c';
119 WriteData(&call_type, sizeof(call_type), file, filename.c_str());
120}
121
Per Åhgrenc1bfe1a2018-09-21 16:23:50 +0200122bool WritingCallOrderFile() {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200123 return absl::GetFlag(FLAGS_full);
Per Åhgrenc1bfe1a2018-09-21 16:23:50 +0200124}
125
Fredrik Hernqvist3be9da32019-05-21 12:23:31 +0200126bool WritingRuntimeSettingFiles() {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200127 return absl::GetFlag(FLAGS_full);
Fredrik Hernqvist3be9da32019-05-21 12:23:31 +0200128}
129
130// Exports RuntimeSetting AEC dump events to Audacity-readable files.
131// This class is not RAII compliant.
132class RuntimeSettingWriter {
133 public:
134 RuntimeSettingWriter(
135 std::string name,
136 rtc::FunctionView<bool(const Event)> is_exporter_for,
137 rtc::FunctionView<std::string(const Event)> get_timeline_label)
138 : setting_name_(std::move(name)),
139 is_exporter_for_(is_exporter_for),
140 get_timeline_label_(get_timeline_label) {}
141 ~RuntimeSettingWriter() { Flush(); }
142
143 bool IsExporterFor(const Event& event) const {
144 return is_exporter_for_(event);
145 }
146
147 // Writes to file the payload of |event| using |frame_count| to calculate
148 // timestamp.
149 void WriteEvent(const Event& event, int frame_count) {
150 RTC_DCHECK(is_exporter_for_(event));
151 if (file_ == nullptr) {
152 rtc::StringBuilder file_name;
153 file_name << setting_name_ << frame_offset_ << ".txt";
154 file_ = OpenFile(file_name.str(), "wb");
155 }
156
157 // Time in the current WAV file, in seconds.
158 double time = (frame_count - frame_offset_) / 100.0;
159 std::string label = get_timeline_label_(event);
160 // In Audacity, all annotations are encoded as intervals.
161 fprintf(file_, "%.6f\t%.6f\t%s \n", time, time, label.c_str());
162 }
163
164 // Handles an AEC dump initialization event, occurring at frame
165 // |frame_offset|.
166 void HandleInitEvent(int frame_offset) {
167 Flush();
168 frame_offset_ = frame_offset;
169 }
170
171 private:
172 void Flush() {
173 if (file_ != nullptr) {
174 fclose(file_);
175 file_ = nullptr;
176 }
177 }
178
179 FILE* file_ = nullptr;
180 int frame_offset_ = 0;
181 const std::string setting_name_;
182 const rtc::FunctionView<bool(Event)> is_exporter_for_;
183 const rtc::FunctionView<std::string(Event)> get_timeline_label_;
184};
185
186// Returns RuntimeSetting exporters for runtime setting types defined in
187// debug.proto.
188std::vector<RuntimeSettingWriter> RuntimeSettingWriters() {
189 return {
190 RuntimeSettingWriter(
191 "CapturePreGain",
192 [](const Event& event) -> bool {
193 return event.runtime_setting().has_capture_pre_gain();
194 },
195 [](const Event& event) -> std::string {
196 return std::to_string(event.runtime_setting().capture_pre_gain());
197 }),
198 RuntimeSettingWriter(
199 "CustomRenderProcessingRuntimeSetting",
200 [](const Event& event) -> bool {
201 return event.runtime_setting()
202 .has_custom_render_processing_setting();
203 },
204 [](const Event& event) -> std::string {
205 return std::to_string(
206 event.runtime_setting().custom_render_processing_setting());
207 }),
208 RuntimeSettingWriter(
209 "CaptureFixedPostGain",
210 [](const Event& event) -> bool {
211 return event.runtime_setting().has_capture_fixed_post_gain();
212 },
213 [](const Event& event) -> std::string {
214 return std::to_string(
215 event.runtime_setting().capture_fixed_post_gain());
216 }),
217 RuntimeSettingWriter(
218 "PlayoutVolumeChange",
219 [](const Event& event) -> bool {
220 return event.runtime_setting().has_playout_volume_change();
221 },
222 [](const Event& event) -> std::string {
223 return std::to_string(
224 event.runtime_setting().playout_volume_change());
225 })};
226}
227
Sam Zackrissonf61475d2018-05-11 11:34:49 +0200228} // namespace
229
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000230int do_main(int argc, char* argv[]) {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200231 std::vector<char*> args = absl::ParseCommandLine(argc, argv);
232 std::string program_name = args[0];
Yves Gerey665174f2018-06-19 15:03:05 +0200233 std::string usage =
234 "Commandline tool to unpack audioproc debug files.\n"
235 "Example usage:\n" +
236 program_name + " debug_dump.pb\n";
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000237
Mirko Bonadei9d962092019-07-04 10:19:10 +0200238 if (args.size() < 2) {
oprypin6e09d872017-08-31 03:21:39 -0700239 printf("%s", usage.c_str());
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000240 return 1;
241 }
242
Mirko Bonadei9d962092019-07-04 10:19:10 +0200243 FILE* debug_file = OpenFile(args[1], "rb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000244
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000245 Event event_msg;
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000246 int frame_count = 0;
pkasting25702cb2016-01-08 13:50:27 -0800247 size_t reverse_samples_per_channel = 0;
248 size_t input_samples_per_channel = 0;
249 size_t output_samples_per_channel = 0;
Peter Kasting69558702016-01-12 16:26:35 -0800250 size_t num_reverse_channels = 0;
251 size_t num_input_channels = 0;
252 size_t num_output_channels = 0;
kwiberg62eaacf2016-02-17 06:39:05 -0800253 std::unique_ptr<WavWriter> reverse_wav_file;
254 std::unique_ptr<WavWriter> input_wav_file;
255 std::unique_ptr<WavWriter> output_wav_file;
256 std::unique_ptr<RawFile> reverse_raw_file;
257 std::unique_ptr<RawFile> input_raw_file;
258 std::unique_ptr<RawFile> output_raw_file;
Minyue13b96ba2015-10-03 00:39:14 +0200259
Jonas Olsson366a50c2018-09-06 13:41:30 +0200260 rtc::StringBuilder callorder_raw_name;
Mirko Bonadei9d962092019-07-04 10:19:10 +0200261 callorder_raw_name << absl::GetFlag(FLAGS_callorder_file) << ".char";
Per Åhgrenc1bfe1a2018-09-21 16:23:50 +0200262 FILE* callorder_char_file = WritingCallOrderFile()
263 ? OpenFile(callorder_raw_name.str(), "wb")
264 : nullptr;
Mirko Bonadei9d962092019-07-04 10:19:10 +0200265 FILE* settings_file = OpenFile(absl::GetFlag(FLAGS_settings_file), "wb");
Minyue13b96ba2015-10-03 00:39:14 +0200266
Fredrik Hernqvist3be9da32019-05-21 12:23:31 +0200267 std::vector<RuntimeSettingWriter> runtime_setting_writers =
268 RuntimeSettingWriters();
269
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000270 while (ReadMessageFromFile(debug_file, &event_msg)) {
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000271 if (event_msg.type() == Event::REVERSE_STREAM) {
272 if (!event_msg.has_reverse_stream()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000273 printf("Corrupt input file: ReverseStream missing.\n");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000274 return 1;
275 }
276
277 const ReverseStream msg = event_msg.reverse_stream();
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000278 if (msg.has_data()) {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200279 if (absl::GetFlag(FLAGS_raw) && !reverse_raw_file) {
Yves Gerey665174f2018-06-19 15:03:05 +0200280 reverse_raw_file.reset(
Mirko Bonadei9d962092019-07-04 10:19:10 +0200281 new RawFile(absl::GetFlag(FLAGS_reverse_file) + ".pcm"));
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000282 }
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000283 // TODO(aluebs): Replace "num_reverse_channels *
284 // reverse_samples_per_channel" with "msg.data().size() /
285 // sizeof(int16_t)" and so on when this fix in audio_processing has made
286 // it into stable: https://webrtc-codereview.appspot.com/15299004/
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000287 WriteIntData(reinterpret_cast<const int16_t*>(msg.data().data()),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000288 num_reverse_channels * reverse_samples_per_channel,
Yves Gerey665174f2018-06-19 15:03:05 +0200289 reverse_wav_file.get(), reverse_raw_file.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000290 } else if (msg.channel_size() > 0) {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200291 if (absl::GetFlag(FLAGS_raw) && !reverse_raw_file) {
Yves Gerey665174f2018-06-19 15:03:05 +0200292 reverse_raw_file.reset(
Mirko Bonadei9d962092019-07-04 10:19:10 +0200293 new RawFile(absl::GetFlag(FLAGS_reverse_file) + ".float"));
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000294 }
Jonas Olssona4d87372019-07-05 19:08:33 +0200295 std::unique_ptr<const float*[]> data(
Yves Gerey665174f2018-06-19 15:03:05 +0200296 new const float*[num_reverse_channels]);
Peter Kasting69558702016-01-12 16:26:35 -0800297 for (size_t i = 0; i < num_reverse_channels; ++i) {
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000298 data[i] = reinterpret_cast<const float*>(msg.channel(i).data());
299 }
Yves Gerey665174f2018-06-19 15:03:05 +0200300 WriteFloatData(data.get(), reverse_samples_per_channel,
301 num_reverse_channels, reverse_wav_file.get(),
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000302 reverse_raw_file.get());
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000303 }
Mirko Bonadei9d962092019-07-04 10:19:10 +0200304 if (absl::GetFlag(FLAGS_full)) {
Per Åhgrenc1bfe1a2018-09-21 16:23:50 +0200305 if (WritingCallOrderFile()) {
306 WriteCallOrderData(true /* render_call */, callorder_char_file,
Mirko Bonadei9d962092019-07-04 10:19:10 +0200307 absl::GetFlag(FLAGS_callorder_file));
Per Åhgrenc1bfe1a2018-09-21 16:23:50 +0200308 }
Sam Zackrissonf61475d2018-05-11 11:34:49 +0200309 }
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000310 } else if (event_msg.type() == Event::STREAM) {
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000311 frame_count++;
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000312 if (!event_msg.has_stream()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000313 printf("Corrupt input file: Stream missing.\n");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000314 return 1;
315 }
316
317 const Stream msg = event_msg.stream();
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000318 if (msg.has_input_data()) {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200319 if (absl::GetFlag(FLAGS_raw) && !input_raw_file) {
Yves Gerey665174f2018-06-19 15:03:05 +0200320 input_raw_file.reset(
Mirko Bonadei9d962092019-07-04 10:19:10 +0200321 new RawFile(absl::GetFlag(FLAGS_input_file) + ".pcm"));
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000322 }
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000323 WriteIntData(reinterpret_cast<const int16_t*>(msg.input_data().data()),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000324 num_input_channels * input_samples_per_channel,
Yves Gerey665174f2018-06-19 15:03:05 +0200325 input_wav_file.get(), input_raw_file.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000326 } else if (msg.input_channel_size() > 0) {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200327 if (absl::GetFlag(FLAGS_raw) && !input_raw_file) {
Yves Gerey665174f2018-06-19 15:03:05 +0200328 input_raw_file.reset(
Mirko Bonadei9d962092019-07-04 10:19:10 +0200329 new RawFile(absl::GetFlag(FLAGS_input_file) + ".float"));
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000330 }
Jonas Olssona4d87372019-07-05 19:08:33 +0200331 std::unique_ptr<const float*[]> data(
Yves Gerey665174f2018-06-19 15:03:05 +0200332 new const float*[num_input_channels]);
Peter Kasting69558702016-01-12 16:26:35 -0800333 for (size_t i = 0; i < num_input_channels; ++i) {
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000334 data[i] = reinterpret_cast<const float*>(msg.input_channel(i).data());
335 }
Yves Gerey665174f2018-06-19 15:03:05 +0200336 WriteFloatData(data.get(), input_samples_per_channel,
337 num_input_channels, input_wav_file.get(),
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000338 input_raw_file.get());
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000339 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000340
341 if (msg.has_output_data()) {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200342 if (absl::GetFlag(FLAGS_raw) && !output_raw_file) {
Yves Gerey665174f2018-06-19 15:03:05 +0200343 output_raw_file.reset(
Mirko Bonadei9d962092019-07-04 10:19:10 +0200344 new RawFile(absl::GetFlag(FLAGS_output_file) + ".pcm"));
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000345 }
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000346 WriteIntData(reinterpret_cast<const int16_t*>(msg.output_data().data()),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000347 num_output_channels * output_samples_per_channel,
Yves Gerey665174f2018-06-19 15:03:05 +0200348 output_wav_file.get(), output_raw_file.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000349 } else if (msg.output_channel_size() > 0) {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200350 if (absl::GetFlag(FLAGS_raw) && !output_raw_file) {
Yves Gerey665174f2018-06-19 15:03:05 +0200351 output_raw_file.reset(
Mirko Bonadei9d962092019-07-04 10:19:10 +0200352 new RawFile(absl::GetFlag(FLAGS_output_file) + ".float"));
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000353 }
Jonas Olssona4d87372019-07-05 19:08:33 +0200354 std::unique_ptr<const float*[]> data(
Yves Gerey665174f2018-06-19 15:03:05 +0200355 new const float*[num_output_channels]);
Peter Kasting69558702016-01-12 16:26:35 -0800356 for (size_t i = 0; i < num_output_channels; ++i) {
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000357 data[i] =
358 reinterpret_cast<const float*>(msg.output_channel(i).data());
359 }
Yves Gerey665174f2018-06-19 15:03:05 +0200360 WriteFloatData(data.get(), output_samples_per_channel,
361 num_output_channels, output_wav_file.get(),
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000362 output_raw_file.get());
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000363 }
364
Mirko Bonadei9d962092019-07-04 10:19:10 +0200365 if (absl::GetFlag(FLAGS_full)) {
Per Åhgrenc1bfe1a2018-09-21 16:23:50 +0200366 if (WritingCallOrderFile()) {
367 WriteCallOrderData(false /* render_call */, callorder_char_file,
Mirko Bonadei9d962092019-07-04 10:19:10 +0200368 absl::GetFlag(FLAGS_callorder_file));
Per Åhgrenc1bfe1a2018-09-21 16:23:50 +0200369 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000370 if (msg.has_delay()) {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200371 static FILE* delay_file =
372 OpenFile(absl::GetFlag(FLAGS_delay_file), "wb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000373 int32_t delay = msg.delay();
Mirko Bonadei9d962092019-07-04 10:19:10 +0200374 if (absl::GetFlag(FLAGS_text)) {
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200375 fprintf(delay_file, "%d\n", delay);
376 } else {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200377 WriteData(&delay, sizeof(delay), delay_file,
378 absl::GetFlag(FLAGS_delay_file));
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200379 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000380 }
381
382 if (msg.has_drift()) {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200383 static FILE* drift_file =
384 OpenFile(absl::GetFlag(FLAGS_drift_file), "wb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000385 int32_t drift = msg.drift();
Mirko Bonadei9d962092019-07-04 10:19:10 +0200386 if (absl::GetFlag(FLAGS_text)) {
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200387 fprintf(drift_file, "%d\n", drift);
388 } else {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200389 WriteData(&drift, sizeof(drift), drift_file,
390 absl::GetFlag(FLAGS_drift_file));
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200391 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000392 }
393
394 if (msg.has_level()) {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200395 static FILE* level_file =
396 OpenFile(absl::GetFlag(FLAGS_level_file), "wb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000397 int32_t level = msg.level();
Mirko Bonadei9d962092019-07-04 10:19:10 +0200398 if (absl::GetFlag(FLAGS_text)) {
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200399 fprintf(level_file, "%d\n", level);
400 } else {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200401 WriteData(&level, sizeof(level), level_file,
402 absl::GetFlag(FLAGS_level_file));
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200403 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000404 }
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000405
406 if (msg.has_keypress()) {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200407 static FILE* keypress_file =
408 OpenFile(absl::GetFlag(FLAGS_keypress_file), "wb");
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000409 bool keypress = msg.keypress();
Mirko Bonadei9d962092019-07-04 10:19:10 +0200410 if (absl::GetFlag(FLAGS_text)) {
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200411 fprintf(keypress_file, "%d\n", keypress);
412 } else {
413 WriteData(&keypress, sizeof(keypress), keypress_file,
Mirko Bonadei9d962092019-07-04 10:19:10 +0200414 absl::GetFlag(FLAGS_keypress_file));
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200415 }
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000416 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000417 }
Minyue13b96ba2015-10-03 00:39:14 +0200418 } else if (event_msg.type() == Event::CONFIG) {
419 if (!event_msg.has_config()) {
420 printf("Corrupt input file: Config missing.\n");
421 return 1;
422 }
423 const audioproc::Config msg = event_msg.config();
424
425 fprintf(settings_file, "APM re-config at frame: %d\n", frame_count);
426
427 PRINT_CONFIG(aec_enabled);
428 PRINT_CONFIG(aec_delay_agnostic_enabled);
429 PRINT_CONFIG(aec_drift_compensation_enabled);
430 PRINT_CONFIG(aec_extended_filter_enabled);
431 PRINT_CONFIG(aec_suppression_level);
432 PRINT_CONFIG(aecm_enabled);
433 PRINT_CONFIG(aecm_comfort_noise_enabled);
434 PRINT_CONFIG(aecm_routing_mode);
435 PRINT_CONFIG(agc_enabled);
436 PRINT_CONFIG(agc_mode);
437 PRINT_CONFIG(agc_limiter_enabled);
438 PRINT_CONFIG(noise_robust_agc_enabled);
439 PRINT_CONFIG(hpf_enabled);
440 PRINT_CONFIG(ns_enabled);
441 PRINT_CONFIG(ns_level);
442 PRINT_CONFIG(transient_suppression_enabled);
Alex Loiko5feb30e2018-04-16 13:52:32 +0200443 PRINT_CONFIG(pre_amplifier_enabled);
444 PRINT_CONFIG_FLOAT(pre_amplifier_fixed_gain_factor);
445
peah7789fe72016-04-15 01:19:44 -0700446 if (msg.has_experiments_description()) {
447 fprintf(settings_file, " experiments_description: %s\n",
448 msg.experiments_description().c_str());
449 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000450 } else if (event_msg.type() == Event::INIT) {
451 if (!event_msg.has_init()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000452 printf("Corrupt input file: Init missing.\n");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000453 return 1;
454 }
455
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000456 const Init msg = event_msg.init();
457 // These should print out zeros if they're missing.
458 fprintf(settings_file, "Init at frame: %d\n", frame_count);
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000459 int input_sample_rate = msg.sample_rate();
460 fprintf(settings_file, " Input sample rate: %d\n", input_sample_rate);
461 int output_sample_rate = msg.output_sample_rate();
462 fprintf(settings_file, " Output sample rate: %d\n", output_sample_rate);
463 int reverse_sample_rate = msg.reverse_sample_rate();
Yves Gerey665174f2018-06-19 15:03:05 +0200464 fprintf(settings_file, " Reverse sample rate: %d\n",
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000465 reverse_sample_rate);
466 num_input_channels = msg.num_input_channels();
Peter Kasting69558702016-01-12 16:26:35 -0800467 fprintf(settings_file, " Input channels: %" PRIuS "\n",
468 num_input_channels);
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000469 num_output_channels = msg.num_output_channels();
Peter Kasting69558702016-01-12 16:26:35 -0800470 fprintf(settings_file, " Output channels: %" PRIuS "\n",
471 num_output_channels);
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000472 num_reverse_channels = msg.num_reverse_channels();
Peter Kasting69558702016-01-12 16:26:35 -0800473 fprintf(settings_file, " Reverse channels: %" PRIuS "\n",
474 num_reverse_channels);
Minyue Li656d6092018-08-10 15:38:52 +0200475 if (msg.has_timestamp_ms()) {
476 const int64_t timestamp = msg.timestamp_ms();
477 fprintf(settings_file, " Timestamp in millisecond: %" PRId64 "\n",
478 timestamp);
479 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000480
481 fprintf(settings_file, "\n");
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000482
483 if (reverse_sample_rate == 0) {
484 reverse_sample_rate = input_sample_rate;
485 }
486 if (output_sample_rate == 0) {
487 output_sample_rate = input_sample_rate;
488 }
489
pkasting25702cb2016-01-08 13:50:27 -0800490 reverse_samples_per_channel =
491 static_cast<size_t>(reverse_sample_rate / 100);
Yves Gerey665174f2018-06-19 15:03:05 +0200492 input_samples_per_channel = static_cast<size_t>(input_sample_rate / 100);
pkasting25702cb2016-01-08 13:50:27 -0800493 output_samples_per_channel =
494 static_cast<size_t>(output_sample_rate / 100);
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000495
Mirko Bonadei9d962092019-07-04 10:19:10 +0200496 if (!absl::GetFlag(FLAGS_raw)) {
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000497 // The WAV files need to be reset every time, because they cant change
498 // their sample rate or number of channels.
Jonas Olsson366a50c2018-09-06 13:41:30 +0200499 rtc::StringBuilder reverse_name;
Mirko Bonadei9d962092019-07-04 10:19:10 +0200500 reverse_name << absl::GetFlag(FLAGS_reverse_file) << frame_count
501 << ".wav";
Yves Gerey665174f2018-06-19 15:03:05 +0200502 reverse_wav_file.reset(new WavWriter(
503 reverse_name.str(), reverse_sample_rate, num_reverse_channels));
Jonas Olsson366a50c2018-09-06 13:41:30 +0200504 rtc::StringBuilder input_name;
Mirko Bonadei9d962092019-07-04 10:19:10 +0200505 input_name << absl::GetFlag(FLAGS_input_file) << frame_count << ".wav";
Yves Gerey665174f2018-06-19 15:03:05 +0200506 input_wav_file.reset(new WavWriter(input_name.str(), input_sample_rate,
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000507 num_input_channels));
Jonas Olsson366a50c2018-09-06 13:41:30 +0200508 rtc::StringBuilder output_name;
Mirko Bonadei9d962092019-07-04 10:19:10 +0200509 output_name << absl::GetFlag(FLAGS_output_file) << frame_count
510 << ".wav";
Yves Gerey665174f2018-06-19 15:03:05 +0200511 output_wav_file.reset(new WavWriter(
512 output_name.str(), output_sample_rate, num_output_channels));
Sam Zackrissonf61475d2018-05-11 11:34:49 +0200513
Per Åhgrenc1bfe1a2018-09-21 16:23:50 +0200514 if (WritingCallOrderFile()) {
515 rtc::StringBuilder callorder_name;
Mirko Bonadei9d962092019-07-04 10:19:10 +0200516 callorder_name << absl::GetFlag(FLAGS_callorder_file) << frame_count
517 << ".char";
Per Åhgrenc1bfe1a2018-09-21 16:23:50 +0200518 callorder_char_file = OpenFile(callorder_name.str(), "wb");
519 }
Fredrik Hernqvist3be9da32019-05-21 12:23:31 +0200520
521 if (WritingRuntimeSettingFiles()) {
522 for (RuntimeSettingWriter& writer : runtime_setting_writers) {
523 writer.HandleInitEvent(frame_count);
524 }
525 }
526 }
527 } else if (event_msg.type() == Event::RUNTIME_SETTING) {
528 if (WritingRuntimeSettingFiles()) {
529 for (RuntimeSettingWriter& writer : runtime_setting_writers) {
530 if (writer.IsExporterFor(event_msg)) {
531 writer.WriteEvent(event_msg, frame_count);
532 }
533 }
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000534 }
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000535 }
536 }
537
538 return 0;
539}
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000540
541} // namespace webrtc
542
543int main(int argc, char* argv[]) {
544 return webrtc::do_main(argc, argv);
545}