blob: ba3af129bf2453f310ef32c0ca29ceb1a47c5eda [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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "rtc_base/format_macros.h"
32#include "rtc_base/ignore_wundef.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020033#include "rtc_base/strings/string_builder.h"
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000034
kwiberg77eab702016-09-28 17:42:01 -070035RTC_PUSH_IGNORING_WUNDEF()
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "modules/audio_processing/debug.pb.h"
kwiberg77eab702016-09-28 17:42:01 -070037RTC_POP_IGNORING_WUNDEF()
38
Mirko Bonadei9d962092019-07-04 10:19:10 +020039ABSL_FLAG(std::string,
40 input_file,
41 "input",
42 "The name of the input stream file.");
43ABSL_FLAG(std::string,
44 output_file,
45 "ref_out",
46 "The name of the reference output stream file.");
47ABSL_FLAG(std::string,
48 reverse_file,
49 "reverse",
50 "The name of the reverse input stream file.");
51ABSL_FLAG(std::string,
52 delay_file,
53 "delay.int32",
54 "The name of the delay file.");
55ABSL_FLAG(std::string,
56 drift_file,
57 "drift.int32",
58 "The name of the drift file.");
59ABSL_FLAG(std::string,
60 level_file,
61 "level.int32",
62 "The name of the level file.");
63ABSL_FLAG(std::string,
64 keypress_file,
65 "keypress.bool",
66 "The name of the keypress file.");
67ABSL_FLAG(std::string,
68 callorder_file,
69 "callorder",
70 "The name of the render/capture call order file.");
71ABSL_FLAG(std::string,
72 settings_file,
73 "settings.txt",
74 "The name of the settings file.");
75ABSL_FLAG(bool,
76 full,
77 false,
78 "Unpack the full set of files (normally not needed).");
79ABSL_FLAG(bool, raw, false, "Write raw data instead of a WAV file.");
80ABSL_FLAG(bool,
81 text,
82 false,
83 "Write non-audio files as text files instead of binary files.");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000084
Yves Gerey665174f2018-06-19 15:03:05 +020085#define PRINT_CONFIG(field_name) \
86 if (msg.has_##field_name()) { \
Minyue13b96ba2015-10-03 00:39:14 +020087 fprintf(settings_file, " " #field_name ": %d\n", msg.field_name()); \
88 }
89
Alex Loiko5feb30e2018-04-16 13:52:32 +020090#define PRINT_CONFIG_FLOAT(field_name) \
91 if (msg.has_##field_name()) { \
92 fprintf(settings_file, " " #field_name ": %f\n", msg.field_name()); \
93 }
94
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000095namespace webrtc {
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000096
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000097using audioproc::Event;
Jonas Olssona4d87372019-07-05 19:08:33 +020098using audioproc::Init;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000099using audioproc::ReverseStream;
100using audioproc::Stream;
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000101
Sam Zackrissonf61475d2018-05-11 11:34:49 +0200102namespace {
103
Yves Gerey665174f2018-06-19 15:03:05 +0200104void WriteData(const void* data,
105 size_t size,
106 FILE* file,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000107 const std::string& filename) {
108 if (fwrite(data, size, 1, file) != 1) {
109 printf("Error when writing to %s\n", filename.c_str());
110 exit(1);
111 }
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000112}
113
Sam Zackrissonf61475d2018-05-11 11:34:49 +0200114void WriteCallOrderData(const bool render_call,
115 FILE* file,
116 const std::string& filename) {
117 const char call_type = render_call ? 'r' : 'c';
118 WriteData(&call_type, sizeof(call_type), file, filename.c_str());
119}
120
Per Åhgrenc1bfe1a2018-09-21 16:23:50 +0200121bool WritingCallOrderFile() {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200122 return absl::GetFlag(FLAGS_full);
Per Åhgrenc1bfe1a2018-09-21 16:23:50 +0200123}
124
Fredrik Hernqvist3be9da32019-05-21 12:23:31 +0200125bool WritingRuntimeSettingFiles() {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200126 return absl::GetFlag(FLAGS_full);
Fredrik Hernqvist3be9da32019-05-21 12:23:31 +0200127}
128
129// Exports RuntimeSetting AEC dump events to Audacity-readable files.
130// This class is not RAII compliant.
131class RuntimeSettingWriter {
132 public:
133 RuntimeSettingWriter(
134 std::string name,
135 rtc::FunctionView<bool(const Event)> is_exporter_for,
136 rtc::FunctionView<std::string(const Event)> get_timeline_label)
137 : setting_name_(std::move(name)),
138 is_exporter_for_(is_exporter_for),
139 get_timeline_label_(get_timeline_label) {}
140 ~RuntimeSettingWriter() { Flush(); }
141
142 bool IsExporterFor(const Event& event) const {
143 return is_exporter_for_(event);
144 }
145
146 // Writes to file the payload of |event| using |frame_count| to calculate
147 // timestamp.
148 void WriteEvent(const Event& event, int frame_count) {
149 RTC_DCHECK(is_exporter_for_(event));
150 if (file_ == nullptr) {
151 rtc::StringBuilder file_name;
152 file_name << setting_name_ << frame_offset_ << ".txt";
153 file_ = OpenFile(file_name.str(), "wb");
154 }
155
156 // Time in the current WAV file, in seconds.
157 double time = (frame_count - frame_offset_) / 100.0;
158 std::string label = get_timeline_label_(event);
159 // In Audacity, all annotations are encoded as intervals.
160 fprintf(file_, "%.6f\t%.6f\t%s \n", time, time, label.c_str());
161 }
162
163 // Handles an AEC dump initialization event, occurring at frame
164 // |frame_offset|.
165 void HandleInitEvent(int frame_offset) {
166 Flush();
167 frame_offset_ = frame_offset;
168 }
169
170 private:
171 void Flush() {
172 if (file_ != nullptr) {
173 fclose(file_);
174 file_ = nullptr;
175 }
176 }
177
178 FILE* file_ = nullptr;
179 int frame_offset_ = 0;
180 const std::string setting_name_;
181 const rtc::FunctionView<bool(Event)> is_exporter_for_;
182 const rtc::FunctionView<std::string(Event)> get_timeline_label_;
183};
184
185// Returns RuntimeSetting exporters for runtime setting types defined in
186// debug.proto.
187std::vector<RuntimeSettingWriter> RuntimeSettingWriters() {
188 return {
189 RuntimeSettingWriter(
190 "CapturePreGain",
191 [](const Event& event) -> bool {
192 return event.runtime_setting().has_capture_pre_gain();
193 },
194 [](const Event& event) -> std::string {
195 return std::to_string(event.runtime_setting().capture_pre_gain());
196 }),
197 RuntimeSettingWriter(
198 "CustomRenderProcessingRuntimeSetting",
199 [](const Event& event) -> bool {
200 return event.runtime_setting()
201 .has_custom_render_processing_setting();
202 },
203 [](const Event& event) -> std::string {
204 return std::to_string(
205 event.runtime_setting().custom_render_processing_setting());
206 }),
207 RuntimeSettingWriter(
208 "CaptureFixedPostGain",
209 [](const Event& event) -> bool {
210 return event.runtime_setting().has_capture_fixed_post_gain();
211 },
212 [](const Event& event) -> std::string {
213 return std::to_string(
214 event.runtime_setting().capture_fixed_post_gain());
215 }),
216 RuntimeSettingWriter(
217 "PlayoutVolumeChange",
218 [](const Event& event) -> bool {
219 return event.runtime_setting().has_playout_volume_change();
220 },
221 [](const Event& event) -> std::string {
222 return std::to_string(
223 event.runtime_setting().playout_volume_change());
224 })};
225}
226
Sam Zackrissonf61475d2018-05-11 11:34:49 +0200227} // namespace
228
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000229int do_main(int argc, char* argv[]) {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200230 std::vector<char*> args = absl::ParseCommandLine(argc, argv);
231 std::string program_name = args[0];
Yves Gerey665174f2018-06-19 15:03:05 +0200232 std::string usage =
233 "Commandline tool to unpack audioproc debug files.\n"
234 "Example usage:\n" +
235 program_name + " debug_dump.pb\n";
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000236
Mirko Bonadei9d962092019-07-04 10:19:10 +0200237 if (args.size() < 2) {
oprypin6e09d872017-08-31 03:21:39 -0700238 printf("%s", usage.c_str());
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000239 return 1;
240 }
241
Mirko Bonadei9d962092019-07-04 10:19:10 +0200242 FILE* debug_file = OpenFile(args[1], "rb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000243
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000244 Event event_msg;
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000245 int frame_count = 0;
pkasting25702cb2016-01-08 13:50:27 -0800246 size_t reverse_samples_per_channel = 0;
247 size_t input_samples_per_channel = 0;
248 size_t output_samples_per_channel = 0;
Peter Kasting69558702016-01-12 16:26:35 -0800249 size_t num_reverse_channels = 0;
250 size_t num_input_channels = 0;
251 size_t num_output_channels = 0;
kwiberg62eaacf2016-02-17 06:39:05 -0800252 std::unique_ptr<WavWriter> reverse_wav_file;
253 std::unique_ptr<WavWriter> input_wav_file;
254 std::unique_ptr<WavWriter> output_wav_file;
255 std::unique_ptr<RawFile> reverse_raw_file;
256 std::unique_ptr<RawFile> input_raw_file;
257 std::unique_ptr<RawFile> output_raw_file;
Minyue13b96ba2015-10-03 00:39:14 +0200258
Jonas Olsson366a50c2018-09-06 13:41:30 +0200259 rtc::StringBuilder callorder_raw_name;
Mirko Bonadei9d962092019-07-04 10:19:10 +0200260 callorder_raw_name << absl::GetFlag(FLAGS_callorder_file) << ".char";
Per Åhgrenc1bfe1a2018-09-21 16:23:50 +0200261 FILE* callorder_char_file = WritingCallOrderFile()
262 ? OpenFile(callorder_raw_name.str(), "wb")
263 : nullptr;
Mirko Bonadei9d962092019-07-04 10:19:10 +0200264 FILE* settings_file = OpenFile(absl::GetFlag(FLAGS_settings_file), "wb");
Minyue13b96ba2015-10-03 00:39:14 +0200265
Fredrik Hernqvist3be9da32019-05-21 12:23:31 +0200266 std::vector<RuntimeSettingWriter> runtime_setting_writers =
267 RuntimeSettingWriters();
268
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000269 while (ReadMessageFromFile(debug_file, &event_msg)) {
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000270 if (event_msg.type() == Event::REVERSE_STREAM) {
271 if (!event_msg.has_reverse_stream()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000272 printf("Corrupt input file: ReverseStream missing.\n");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000273 return 1;
274 }
275
276 const ReverseStream msg = event_msg.reverse_stream();
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000277 if (msg.has_data()) {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200278 if (absl::GetFlag(FLAGS_raw) && !reverse_raw_file) {
Yves Gerey665174f2018-06-19 15:03:05 +0200279 reverse_raw_file.reset(
Mirko Bonadei9d962092019-07-04 10:19:10 +0200280 new RawFile(absl::GetFlag(FLAGS_reverse_file) + ".pcm"));
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000281 }
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000282 // TODO(aluebs): Replace "num_reverse_channels *
283 // reverse_samples_per_channel" with "msg.data().size() /
284 // sizeof(int16_t)" and so on when this fix in audio_processing has made
285 // it into stable: https://webrtc-codereview.appspot.com/15299004/
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000286 WriteIntData(reinterpret_cast<const int16_t*>(msg.data().data()),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000287 num_reverse_channels * reverse_samples_per_channel,
Yves Gerey665174f2018-06-19 15:03:05 +0200288 reverse_wav_file.get(), reverse_raw_file.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000289 } else if (msg.channel_size() > 0) {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200290 if (absl::GetFlag(FLAGS_raw) && !reverse_raw_file) {
Yves Gerey665174f2018-06-19 15:03:05 +0200291 reverse_raw_file.reset(
Mirko Bonadei9d962092019-07-04 10:19:10 +0200292 new RawFile(absl::GetFlag(FLAGS_reverse_file) + ".float"));
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000293 }
Jonas Olssona4d87372019-07-05 19:08:33 +0200294 std::unique_ptr<const float*[]> data(
Yves Gerey665174f2018-06-19 15:03:05 +0200295 new const float*[num_reverse_channels]);
Peter Kasting69558702016-01-12 16:26:35 -0800296 for (size_t i = 0; i < num_reverse_channels; ++i) {
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000297 data[i] = reinterpret_cast<const float*>(msg.channel(i).data());
298 }
Yves Gerey665174f2018-06-19 15:03:05 +0200299 WriteFloatData(data.get(), reverse_samples_per_channel,
300 num_reverse_channels, reverse_wav_file.get(),
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000301 reverse_raw_file.get());
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000302 }
Mirko Bonadei9d962092019-07-04 10:19:10 +0200303 if (absl::GetFlag(FLAGS_full)) {
Per Åhgrenc1bfe1a2018-09-21 16:23:50 +0200304 if (WritingCallOrderFile()) {
305 WriteCallOrderData(true /* render_call */, callorder_char_file,
Mirko Bonadei9d962092019-07-04 10:19:10 +0200306 absl::GetFlag(FLAGS_callorder_file));
Per Åhgrenc1bfe1a2018-09-21 16:23:50 +0200307 }
Sam Zackrissonf61475d2018-05-11 11:34:49 +0200308 }
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000309 } else if (event_msg.type() == Event::STREAM) {
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000310 frame_count++;
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000311 if (!event_msg.has_stream()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000312 printf("Corrupt input file: Stream missing.\n");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000313 return 1;
314 }
315
316 const Stream msg = event_msg.stream();
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000317 if (msg.has_input_data()) {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200318 if (absl::GetFlag(FLAGS_raw) && !input_raw_file) {
Yves Gerey665174f2018-06-19 15:03:05 +0200319 input_raw_file.reset(
Mirko Bonadei9d962092019-07-04 10:19:10 +0200320 new RawFile(absl::GetFlag(FLAGS_input_file) + ".pcm"));
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000321 }
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000322 WriteIntData(reinterpret_cast<const int16_t*>(msg.input_data().data()),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000323 num_input_channels * input_samples_per_channel,
Yves Gerey665174f2018-06-19 15:03:05 +0200324 input_wav_file.get(), input_raw_file.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000325 } else if (msg.input_channel_size() > 0) {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200326 if (absl::GetFlag(FLAGS_raw) && !input_raw_file) {
Yves Gerey665174f2018-06-19 15:03:05 +0200327 input_raw_file.reset(
Mirko Bonadei9d962092019-07-04 10:19:10 +0200328 new RawFile(absl::GetFlag(FLAGS_input_file) + ".float"));
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000329 }
Jonas Olssona4d87372019-07-05 19:08:33 +0200330 std::unique_ptr<const float*[]> data(
Yves Gerey665174f2018-06-19 15:03:05 +0200331 new const float*[num_input_channels]);
Peter Kasting69558702016-01-12 16:26:35 -0800332 for (size_t i = 0; i < num_input_channels; ++i) {
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000333 data[i] = reinterpret_cast<const float*>(msg.input_channel(i).data());
334 }
Yves Gerey665174f2018-06-19 15:03:05 +0200335 WriteFloatData(data.get(), input_samples_per_channel,
336 num_input_channels, input_wav_file.get(),
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000337 input_raw_file.get());
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000338 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000339
340 if (msg.has_output_data()) {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200341 if (absl::GetFlag(FLAGS_raw) && !output_raw_file) {
Yves Gerey665174f2018-06-19 15:03:05 +0200342 output_raw_file.reset(
Mirko Bonadei9d962092019-07-04 10:19:10 +0200343 new RawFile(absl::GetFlag(FLAGS_output_file) + ".pcm"));
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000344 }
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000345 WriteIntData(reinterpret_cast<const int16_t*>(msg.output_data().data()),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000346 num_output_channels * output_samples_per_channel,
Yves Gerey665174f2018-06-19 15:03:05 +0200347 output_wav_file.get(), output_raw_file.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000348 } else if (msg.output_channel_size() > 0) {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200349 if (absl::GetFlag(FLAGS_raw) && !output_raw_file) {
Yves Gerey665174f2018-06-19 15:03:05 +0200350 output_raw_file.reset(
Mirko Bonadei9d962092019-07-04 10:19:10 +0200351 new RawFile(absl::GetFlag(FLAGS_output_file) + ".float"));
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000352 }
Jonas Olssona4d87372019-07-05 19:08:33 +0200353 std::unique_ptr<const float*[]> data(
Yves Gerey665174f2018-06-19 15:03:05 +0200354 new const float*[num_output_channels]);
Peter Kasting69558702016-01-12 16:26:35 -0800355 for (size_t i = 0; i < num_output_channels; ++i) {
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000356 data[i] =
357 reinterpret_cast<const float*>(msg.output_channel(i).data());
358 }
Yves Gerey665174f2018-06-19 15:03:05 +0200359 WriteFloatData(data.get(), output_samples_per_channel,
360 num_output_channels, output_wav_file.get(),
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000361 output_raw_file.get());
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000362 }
363
Mirko Bonadei9d962092019-07-04 10:19:10 +0200364 if (absl::GetFlag(FLAGS_full)) {
Per Åhgrenc1bfe1a2018-09-21 16:23:50 +0200365 if (WritingCallOrderFile()) {
366 WriteCallOrderData(false /* render_call */, callorder_char_file,
Mirko Bonadei9d962092019-07-04 10:19:10 +0200367 absl::GetFlag(FLAGS_callorder_file));
Per Åhgrenc1bfe1a2018-09-21 16:23:50 +0200368 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000369 if (msg.has_delay()) {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200370 static FILE* delay_file =
371 OpenFile(absl::GetFlag(FLAGS_delay_file), "wb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000372 int32_t delay = msg.delay();
Mirko Bonadei9d962092019-07-04 10:19:10 +0200373 if (absl::GetFlag(FLAGS_text)) {
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200374 fprintf(delay_file, "%d\n", delay);
375 } else {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200376 WriteData(&delay, sizeof(delay), delay_file,
377 absl::GetFlag(FLAGS_delay_file));
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200378 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000379 }
380
381 if (msg.has_drift()) {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200382 static FILE* drift_file =
383 OpenFile(absl::GetFlag(FLAGS_drift_file), "wb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000384 int32_t drift = msg.drift();
Mirko Bonadei9d962092019-07-04 10:19:10 +0200385 if (absl::GetFlag(FLAGS_text)) {
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200386 fprintf(drift_file, "%d\n", drift);
387 } else {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200388 WriteData(&drift, sizeof(drift), drift_file,
389 absl::GetFlag(FLAGS_drift_file));
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200390 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000391 }
392
393 if (msg.has_level()) {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200394 static FILE* level_file =
395 OpenFile(absl::GetFlag(FLAGS_level_file), "wb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000396 int32_t level = msg.level();
Mirko Bonadei9d962092019-07-04 10:19:10 +0200397 if (absl::GetFlag(FLAGS_text)) {
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200398 fprintf(level_file, "%d\n", level);
399 } else {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200400 WriteData(&level, sizeof(level), level_file,
401 absl::GetFlag(FLAGS_level_file));
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200402 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000403 }
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000404
405 if (msg.has_keypress()) {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200406 static FILE* keypress_file =
407 OpenFile(absl::GetFlag(FLAGS_keypress_file), "wb");
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000408 bool keypress = msg.keypress();
Mirko Bonadei9d962092019-07-04 10:19:10 +0200409 if (absl::GetFlag(FLAGS_text)) {
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200410 fprintf(keypress_file, "%d\n", keypress);
411 } else {
412 WriteData(&keypress, sizeof(keypress), keypress_file,
Mirko Bonadei9d962092019-07-04 10:19:10 +0200413 absl::GetFlag(FLAGS_keypress_file));
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200414 }
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000415 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000416 }
Minyue13b96ba2015-10-03 00:39:14 +0200417 } else if (event_msg.type() == Event::CONFIG) {
418 if (!event_msg.has_config()) {
419 printf("Corrupt input file: Config missing.\n");
420 return 1;
421 }
422 const audioproc::Config msg = event_msg.config();
423
424 fprintf(settings_file, "APM re-config at frame: %d\n", frame_count);
425
426 PRINT_CONFIG(aec_enabled);
427 PRINT_CONFIG(aec_delay_agnostic_enabled);
428 PRINT_CONFIG(aec_drift_compensation_enabled);
429 PRINT_CONFIG(aec_extended_filter_enabled);
430 PRINT_CONFIG(aec_suppression_level);
431 PRINT_CONFIG(aecm_enabled);
432 PRINT_CONFIG(aecm_comfort_noise_enabled);
433 PRINT_CONFIG(aecm_routing_mode);
434 PRINT_CONFIG(agc_enabled);
435 PRINT_CONFIG(agc_mode);
436 PRINT_CONFIG(agc_limiter_enabled);
437 PRINT_CONFIG(noise_robust_agc_enabled);
438 PRINT_CONFIG(hpf_enabled);
439 PRINT_CONFIG(ns_enabled);
440 PRINT_CONFIG(ns_level);
441 PRINT_CONFIG(transient_suppression_enabled);
Alex Loiko5feb30e2018-04-16 13:52:32 +0200442 PRINT_CONFIG(pre_amplifier_enabled);
443 PRINT_CONFIG_FLOAT(pre_amplifier_fixed_gain_factor);
444
peah7789fe72016-04-15 01:19:44 -0700445 if (msg.has_experiments_description()) {
446 fprintf(settings_file, " experiments_description: %s\n",
447 msg.experiments_description().c_str());
448 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000449 } else if (event_msg.type() == Event::INIT) {
450 if (!event_msg.has_init()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000451 printf("Corrupt input file: Init missing.\n");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000452 return 1;
453 }
454
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000455 const Init msg = event_msg.init();
456 // These should print out zeros if they're missing.
457 fprintf(settings_file, "Init at frame: %d\n", frame_count);
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000458 int input_sample_rate = msg.sample_rate();
459 fprintf(settings_file, " Input sample rate: %d\n", input_sample_rate);
460 int output_sample_rate = msg.output_sample_rate();
461 fprintf(settings_file, " Output sample rate: %d\n", output_sample_rate);
462 int reverse_sample_rate = msg.reverse_sample_rate();
Yves Gerey665174f2018-06-19 15:03:05 +0200463 fprintf(settings_file, " Reverse sample rate: %d\n",
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000464 reverse_sample_rate);
465 num_input_channels = msg.num_input_channels();
Oleh Prypinb1686782019-08-02 09:36:47 +0200466 fprintf(settings_file, " Input channels: %" RTC_PRIuS "\n",
Peter Kasting69558702016-01-12 16:26:35 -0800467 num_input_channels);
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000468 num_output_channels = msg.num_output_channels();
Oleh Prypinb1686782019-08-02 09:36:47 +0200469 fprintf(settings_file, " Output channels: %" RTC_PRIuS "\n",
Peter Kasting69558702016-01-12 16:26:35 -0800470 num_output_channels);
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000471 num_reverse_channels = msg.num_reverse_channels();
Oleh Prypinb1686782019-08-02 09:36:47 +0200472 fprintf(settings_file, " Reverse channels: %" RTC_PRIuS "\n",
Peter Kasting69558702016-01-12 16:26:35 -0800473 num_reverse_channels);
Minyue Li656d6092018-08-10 15:38:52 +0200474 if (msg.has_timestamp_ms()) {
475 const int64_t timestamp = msg.timestamp_ms();
476 fprintf(settings_file, " Timestamp in millisecond: %" PRId64 "\n",
477 timestamp);
478 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000479
480 fprintf(settings_file, "\n");
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000481
482 if (reverse_sample_rate == 0) {
483 reverse_sample_rate = input_sample_rate;
484 }
485 if (output_sample_rate == 0) {
486 output_sample_rate = input_sample_rate;
487 }
488
pkasting25702cb2016-01-08 13:50:27 -0800489 reverse_samples_per_channel =
490 static_cast<size_t>(reverse_sample_rate / 100);
Yves Gerey665174f2018-06-19 15:03:05 +0200491 input_samples_per_channel = static_cast<size_t>(input_sample_rate / 100);
pkasting25702cb2016-01-08 13:50:27 -0800492 output_samples_per_channel =
493 static_cast<size_t>(output_sample_rate / 100);
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000494
Mirko Bonadei9d962092019-07-04 10:19:10 +0200495 if (!absl::GetFlag(FLAGS_raw)) {
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000496 // The WAV files need to be reset every time, because they cant change
497 // their sample rate or number of channels.
Jonas Olsson366a50c2018-09-06 13:41:30 +0200498 rtc::StringBuilder reverse_name;
Mirko Bonadei9d962092019-07-04 10:19:10 +0200499 reverse_name << absl::GetFlag(FLAGS_reverse_file) << frame_count
500 << ".wav";
Yves Gerey665174f2018-06-19 15:03:05 +0200501 reverse_wav_file.reset(new WavWriter(
502 reverse_name.str(), reverse_sample_rate, num_reverse_channels));
Jonas Olsson366a50c2018-09-06 13:41:30 +0200503 rtc::StringBuilder input_name;
Mirko Bonadei9d962092019-07-04 10:19:10 +0200504 input_name << absl::GetFlag(FLAGS_input_file) << frame_count << ".wav";
Yves Gerey665174f2018-06-19 15:03:05 +0200505 input_wav_file.reset(new WavWriter(input_name.str(), input_sample_rate,
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000506 num_input_channels));
Jonas Olsson366a50c2018-09-06 13:41:30 +0200507 rtc::StringBuilder output_name;
Mirko Bonadei9d962092019-07-04 10:19:10 +0200508 output_name << absl::GetFlag(FLAGS_output_file) << frame_count
509 << ".wav";
Yves Gerey665174f2018-06-19 15:03:05 +0200510 output_wav_file.reset(new WavWriter(
511 output_name.str(), output_sample_rate, num_output_channels));
Sam Zackrissonf61475d2018-05-11 11:34:49 +0200512
Per Åhgrenc1bfe1a2018-09-21 16:23:50 +0200513 if (WritingCallOrderFile()) {
514 rtc::StringBuilder callorder_name;
Mirko Bonadei9d962092019-07-04 10:19:10 +0200515 callorder_name << absl::GetFlag(FLAGS_callorder_file) << frame_count
516 << ".char";
Per Åhgrenc1bfe1a2018-09-21 16:23:50 +0200517 callorder_char_file = OpenFile(callorder_name.str(), "wb");
518 }
Fredrik Hernqvist3be9da32019-05-21 12:23:31 +0200519
520 if (WritingRuntimeSettingFiles()) {
521 for (RuntimeSettingWriter& writer : runtime_setting_writers) {
522 writer.HandleInitEvent(frame_count);
523 }
524 }
525 }
526 } else if (event_msg.type() == Event::RUNTIME_SETTING) {
527 if (WritingRuntimeSettingFiles()) {
528 for (RuntimeSettingWriter& writer : runtime_setting_writers) {
529 if (writer.IsExporterFor(event_msg)) {
530 writer.WriteEvent(event_msg, frame_count);
531 }
532 }
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000533 }
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000534 }
535 }
536
537 return 0;
538}
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000539
540} // namespace webrtc
541
542int main(int argc, char* argv[]) {
543 return webrtc::do_main(argc, argv);
544}