blob: 0850e75377272da15e766cf2d720cbf3e4f6fcab [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.");
Per Åhgren879d33b2021-01-21 10:08:15 +010084ABSL_FLAG(bool,
85 use_init_suffix,
86 false,
87 "Use init index instead of capture frame count as file name suffix.");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000088
Yves Gerey665174f2018-06-19 15:03:05 +020089#define PRINT_CONFIG(field_name) \
90 if (msg.has_##field_name()) { \
Minyue13b96ba2015-10-03 00:39:14 +020091 fprintf(settings_file, " " #field_name ": %d\n", msg.field_name()); \
92 }
93
Alex Loiko5feb30e2018-04-16 13:52:32 +020094#define PRINT_CONFIG_FLOAT(field_name) \
95 if (msg.has_##field_name()) { \
96 fprintf(settings_file, " " #field_name ": %f\n", msg.field_name()); \
97 }
98
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000099namespace webrtc {
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000100
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000101using audioproc::Event;
Jonas Olssona4d87372019-07-05 19:08:33 +0200102using audioproc::Init;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000103using audioproc::ReverseStream;
104using audioproc::Stream;
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000105
Sam Zackrissonf61475d2018-05-11 11:34:49 +0200106namespace {
107
Yves Gerey665174f2018-06-19 15:03:05 +0200108void WriteData(const void* data,
109 size_t size,
110 FILE* file,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000111 const std::string& filename) {
112 if (fwrite(data, size, 1, file) != 1) {
113 printf("Error when writing to %s\n", filename.c_str());
114 exit(1);
115 }
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000116}
117
Sam Zackrissonf61475d2018-05-11 11:34:49 +0200118void WriteCallOrderData(const bool render_call,
119 FILE* file,
120 const std::string& filename) {
121 const char call_type = render_call ? 'r' : 'c';
122 WriteData(&call_type, sizeof(call_type), file, filename.c_str());
123}
124
Per Åhgrenc1bfe1a2018-09-21 16:23:50 +0200125bool WritingCallOrderFile() {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200126 return absl::GetFlag(FLAGS_full);
Per Åhgrenc1bfe1a2018-09-21 16:23:50 +0200127}
128
Fredrik Hernqvist3be9da32019-05-21 12:23:31 +0200129bool WritingRuntimeSettingFiles() {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200130 return absl::GetFlag(FLAGS_full);
Fredrik Hernqvist3be9da32019-05-21 12:23:31 +0200131}
132
133// Exports RuntimeSetting AEC dump events to Audacity-readable files.
134// This class is not RAII compliant.
135class RuntimeSettingWriter {
136 public:
137 RuntimeSettingWriter(
138 std::string name,
139 rtc::FunctionView<bool(const Event)> is_exporter_for,
140 rtc::FunctionView<std::string(const Event)> get_timeline_label)
141 : setting_name_(std::move(name)),
142 is_exporter_for_(is_exporter_for),
143 get_timeline_label_(get_timeline_label) {}
144 ~RuntimeSettingWriter() { Flush(); }
145
146 bool IsExporterFor(const Event& event) const {
147 return is_exporter_for_(event);
148 }
149
Artem Titov54500ad2021-07-26 15:51:27 +0200150 // Writes to file the payload of `event` using `frame_count` to calculate
Fredrik Hernqvist3be9da32019-05-21 12:23:31 +0200151 // timestamp.
152 void WriteEvent(const Event& event, int frame_count) {
153 RTC_DCHECK(is_exporter_for_(event));
154 if (file_ == nullptr) {
155 rtc::StringBuilder file_name;
156 file_name << setting_name_ << frame_offset_ << ".txt";
157 file_ = OpenFile(file_name.str(), "wb");
158 }
159
160 // Time in the current WAV file, in seconds.
161 double time = (frame_count - frame_offset_) / 100.0;
162 std::string label = get_timeline_label_(event);
163 // In Audacity, all annotations are encoded as intervals.
164 fprintf(file_, "%.6f\t%.6f\t%s \n", time, time, label.c_str());
165 }
166
167 // Handles an AEC dump initialization event, occurring at frame
Artem Titov54500ad2021-07-26 15:51:27 +0200168 // `frame_offset`.
Fredrik Hernqvist3be9da32019-05-21 12:23:31 +0200169 void HandleInitEvent(int frame_offset) {
170 Flush();
171 frame_offset_ = frame_offset;
172 }
173
174 private:
175 void Flush() {
176 if (file_ != nullptr) {
177 fclose(file_);
178 file_ = nullptr;
179 }
180 }
181
182 FILE* file_ = nullptr;
183 int frame_offset_ = 0;
184 const std::string setting_name_;
185 const rtc::FunctionView<bool(Event)> is_exporter_for_;
186 const rtc::FunctionView<std::string(Event)> get_timeline_label_;
187};
188
189// Returns RuntimeSetting exporters for runtime setting types defined in
190// debug.proto.
191std::vector<RuntimeSettingWriter> RuntimeSettingWriters() {
192 return {
193 RuntimeSettingWriter(
194 "CapturePreGain",
195 [](const Event& event) -> bool {
196 return event.runtime_setting().has_capture_pre_gain();
197 },
198 [](const Event& event) -> std::string {
199 return std::to_string(event.runtime_setting().capture_pre_gain());
200 }),
201 RuntimeSettingWriter(
202 "CustomRenderProcessingRuntimeSetting",
203 [](const Event& event) -> bool {
204 return event.runtime_setting()
205 .has_custom_render_processing_setting();
206 },
207 [](const Event& event) -> std::string {
208 return std::to_string(
209 event.runtime_setting().custom_render_processing_setting());
210 }),
211 RuntimeSettingWriter(
212 "CaptureFixedPostGain",
213 [](const Event& event) -> bool {
214 return event.runtime_setting().has_capture_fixed_post_gain();
215 },
216 [](const Event& event) -> std::string {
217 return std::to_string(
218 event.runtime_setting().capture_fixed_post_gain());
219 }),
220 RuntimeSettingWriter(
221 "PlayoutVolumeChange",
222 [](const Event& event) -> bool {
223 return event.runtime_setting().has_playout_volume_change();
224 },
225 [](const Event& event) -> std::string {
226 return std::to_string(
227 event.runtime_setting().playout_volume_change());
228 })};
229}
230
Per Åhgren879d33b2021-01-21 10:08:15 +0100231std::string GetWavFileIndex(int init_index, int frame_count) {
232 rtc::StringBuilder suffix;
233 if (absl::GetFlag(FLAGS_use_init_suffix)) {
234 suffix << "_" << init_index;
235 } else {
236 suffix << frame_count;
237 }
238 return suffix.str();
239}
240
Sam Zackrissonf61475d2018-05-11 11:34:49 +0200241} // namespace
242
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000243int do_main(int argc, char* argv[]) {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200244 std::vector<char*> args = absl::ParseCommandLine(argc, argv);
245 std::string program_name = args[0];
Yves Gerey665174f2018-06-19 15:03:05 +0200246 std::string usage =
247 "Commandline tool to unpack audioproc debug files.\n"
248 "Example usage:\n" +
249 program_name + " debug_dump.pb\n";
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000250
Mirko Bonadei9d962092019-07-04 10:19:10 +0200251 if (args.size() < 2) {
oprypin6e09d872017-08-31 03:21:39 -0700252 printf("%s", usage.c_str());
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000253 return 1;
254 }
255
Mirko Bonadei9d962092019-07-04 10:19:10 +0200256 FILE* debug_file = OpenFile(args[1], "rb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000257
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000258 Event event_msg;
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000259 int frame_count = 0;
Per Åhgren879d33b2021-01-21 10:08:15 +0100260 int init_count = 0;
pkasting25702cb2016-01-08 13:50:27 -0800261 size_t reverse_samples_per_channel = 0;
262 size_t input_samples_per_channel = 0;
263 size_t output_samples_per_channel = 0;
Peter Kasting69558702016-01-12 16:26:35 -0800264 size_t num_reverse_channels = 0;
265 size_t num_input_channels = 0;
266 size_t num_output_channels = 0;
kwiberg62eaacf2016-02-17 06:39:05 -0800267 std::unique_ptr<WavWriter> reverse_wav_file;
268 std::unique_ptr<WavWriter> input_wav_file;
269 std::unique_ptr<WavWriter> output_wav_file;
270 std::unique_ptr<RawFile> reverse_raw_file;
271 std::unique_ptr<RawFile> input_raw_file;
272 std::unique_ptr<RawFile> output_raw_file;
Minyue13b96ba2015-10-03 00:39:14 +0200273
Jonas Olsson366a50c2018-09-06 13:41:30 +0200274 rtc::StringBuilder callorder_raw_name;
Mirko Bonadei9d962092019-07-04 10:19:10 +0200275 callorder_raw_name << absl::GetFlag(FLAGS_callorder_file) << ".char";
Per Åhgrenc1bfe1a2018-09-21 16:23:50 +0200276 FILE* callorder_char_file = WritingCallOrderFile()
277 ? OpenFile(callorder_raw_name.str(), "wb")
278 : nullptr;
Mirko Bonadei9d962092019-07-04 10:19:10 +0200279 FILE* settings_file = OpenFile(absl::GetFlag(FLAGS_settings_file), "wb");
Minyue13b96ba2015-10-03 00:39:14 +0200280
Fredrik Hernqvist3be9da32019-05-21 12:23:31 +0200281 std::vector<RuntimeSettingWriter> runtime_setting_writers =
282 RuntimeSettingWriters();
283
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000284 while (ReadMessageFromFile(debug_file, &event_msg)) {
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000285 if (event_msg.type() == Event::REVERSE_STREAM) {
286 if (!event_msg.has_reverse_stream()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000287 printf("Corrupt input file: ReverseStream missing.\n");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000288 return 1;
289 }
290
291 const ReverseStream msg = event_msg.reverse_stream();
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000292 if (msg.has_data()) {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200293 if (absl::GetFlag(FLAGS_raw) && !reverse_raw_file) {
Yves Gerey665174f2018-06-19 15:03:05 +0200294 reverse_raw_file.reset(
Mirko Bonadei9d962092019-07-04 10:19:10 +0200295 new RawFile(absl::GetFlag(FLAGS_reverse_file) + ".pcm"));
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000296 }
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000297 // TODO(aluebs): Replace "num_reverse_channels *
298 // reverse_samples_per_channel" with "msg.data().size() /
299 // sizeof(int16_t)" and so on when this fix in audio_processing has made
300 // it into stable: https://webrtc-codereview.appspot.com/15299004/
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000301 WriteIntData(reinterpret_cast<const int16_t*>(msg.data().data()),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000302 num_reverse_channels * reverse_samples_per_channel,
Yves Gerey665174f2018-06-19 15:03:05 +0200303 reverse_wav_file.get(), reverse_raw_file.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000304 } else if (msg.channel_size() > 0) {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200305 if (absl::GetFlag(FLAGS_raw) && !reverse_raw_file) {
Yves Gerey665174f2018-06-19 15:03:05 +0200306 reverse_raw_file.reset(
Mirko Bonadei9d962092019-07-04 10:19:10 +0200307 new RawFile(absl::GetFlag(FLAGS_reverse_file) + ".float"));
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000308 }
Jonas Olssona4d87372019-07-05 19:08:33 +0200309 std::unique_ptr<const float*[]> data(
Yves Gerey665174f2018-06-19 15:03:05 +0200310 new const float*[num_reverse_channels]);
Peter Kasting69558702016-01-12 16:26:35 -0800311 for (size_t i = 0; i < num_reverse_channels; ++i) {
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000312 data[i] = reinterpret_cast<const float*>(msg.channel(i).data());
313 }
Yves Gerey665174f2018-06-19 15:03:05 +0200314 WriteFloatData(data.get(), reverse_samples_per_channel,
315 num_reverse_channels, reverse_wav_file.get(),
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000316 reverse_raw_file.get());
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000317 }
Mirko Bonadei9d962092019-07-04 10:19:10 +0200318 if (absl::GetFlag(FLAGS_full)) {
Per Åhgrenc1bfe1a2018-09-21 16:23:50 +0200319 if (WritingCallOrderFile()) {
320 WriteCallOrderData(true /* render_call */, callorder_char_file,
Mirko Bonadei9d962092019-07-04 10:19:10 +0200321 absl::GetFlag(FLAGS_callorder_file));
Per Åhgrenc1bfe1a2018-09-21 16:23:50 +0200322 }
Sam Zackrissonf61475d2018-05-11 11:34:49 +0200323 }
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000324 } else if (event_msg.type() == Event::STREAM) {
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000325 frame_count++;
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000326 if (!event_msg.has_stream()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000327 printf("Corrupt input file: Stream missing.\n");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000328 return 1;
329 }
330
331 const Stream msg = event_msg.stream();
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000332 if (msg.has_input_data()) {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200333 if (absl::GetFlag(FLAGS_raw) && !input_raw_file) {
Yves Gerey665174f2018-06-19 15:03:05 +0200334 input_raw_file.reset(
Mirko Bonadei9d962092019-07-04 10:19:10 +0200335 new RawFile(absl::GetFlag(FLAGS_input_file) + ".pcm"));
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000336 }
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000337 WriteIntData(reinterpret_cast<const int16_t*>(msg.input_data().data()),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000338 num_input_channels * input_samples_per_channel,
Yves Gerey665174f2018-06-19 15:03:05 +0200339 input_wav_file.get(), input_raw_file.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000340 } else if (msg.input_channel_size() > 0) {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200341 if (absl::GetFlag(FLAGS_raw) && !input_raw_file) {
Yves Gerey665174f2018-06-19 15:03:05 +0200342 input_raw_file.reset(
Mirko Bonadei9d962092019-07-04 10:19:10 +0200343 new RawFile(absl::GetFlag(FLAGS_input_file) + ".float"));
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000344 }
Jonas Olssona4d87372019-07-05 19:08:33 +0200345 std::unique_ptr<const float*[]> data(
Yves Gerey665174f2018-06-19 15:03:05 +0200346 new const float*[num_input_channels]);
Peter Kasting69558702016-01-12 16:26:35 -0800347 for (size_t i = 0; i < num_input_channels; ++i) {
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000348 data[i] = reinterpret_cast<const float*>(msg.input_channel(i).data());
349 }
Yves Gerey665174f2018-06-19 15:03:05 +0200350 WriteFloatData(data.get(), input_samples_per_channel,
351 num_input_channels, input_wav_file.get(),
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000352 input_raw_file.get());
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000353 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000354
355 if (msg.has_output_data()) {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200356 if (absl::GetFlag(FLAGS_raw) && !output_raw_file) {
Yves Gerey665174f2018-06-19 15:03:05 +0200357 output_raw_file.reset(
Mirko Bonadei9d962092019-07-04 10:19:10 +0200358 new RawFile(absl::GetFlag(FLAGS_output_file) + ".pcm"));
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000359 }
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000360 WriteIntData(reinterpret_cast<const int16_t*>(msg.output_data().data()),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000361 num_output_channels * output_samples_per_channel,
Yves Gerey665174f2018-06-19 15:03:05 +0200362 output_wav_file.get(), output_raw_file.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000363 } else if (msg.output_channel_size() > 0) {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200364 if (absl::GetFlag(FLAGS_raw) && !output_raw_file) {
Yves Gerey665174f2018-06-19 15:03:05 +0200365 output_raw_file.reset(
Mirko Bonadei9d962092019-07-04 10:19:10 +0200366 new RawFile(absl::GetFlag(FLAGS_output_file) + ".float"));
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000367 }
Jonas Olssona4d87372019-07-05 19:08:33 +0200368 std::unique_ptr<const float*[]> data(
Yves Gerey665174f2018-06-19 15:03:05 +0200369 new const float*[num_output_channels]);
Peter Kasting69558702016-01-12 16:26:35 -0800370 for (size_t i = 0; i < num_output_channels; ++i) {
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000371 data[i] =
372 reinterpret_cast<const float*>(msg.output_channel(i).data());
373 }
Yves Gerey665174f2018-06-19 15:03:05 +0200374 WriteFloatData(data.get(), output_samples_per_channel,
375 num_output_channels, output_wav_file.get(),
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000376 output_raw_file.get());
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000377 }
378
Mirko Bonadei9d962092019-07-04 10:19:10 +0200379 if (absl::GetFlag(FLAGS_full)) {
Per Åhgrenc1bfe1a2018-09-21 16:23:50 +0200380 if (WritingCallOrderFile()) {
381 WriteCallOrderData(false /* render_call */, callorder_char_file,
Mirko Bonadei9d962092019-07-04 10:19:10 +0200382 absl::GetFlag(FLAGS_callorder_file));
Per Åhgrenc1bfe1a2018-09-21 16:23:50 +0200383 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000384 if (msg.has_delay()) {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200385 static FILE* delay_file =
386 OpenFile(absl::GetFlag(FLAGS_delay_file), "wb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000387 int32_t delay = msg.delay();
Mirko Bonadei9d962092019-07-04 10:19:10 +0200388 if (absl::GetFlag(FLAGS_text)) {
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200389 fprintf(delay_file, "%d\n", delay);
390 } else {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200391 WriteData(&delay, sizeof(delay), delay_file,
392 absl::GetFlag(FLAGS_delay_file));
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200393 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000394 }
395
396 if (msg.has_drift()) {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200397 static FILE* drift_file =
398 OpenFile(absl::GetFlag(FLAGS_drift_file), "wb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000399 int32_t drift = msg.drift();
Mirko Bonadei9d962092019-07-04 10:19:10 +0200400 if (absl::GetFlag(FLAGS_text)) {
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200401 fprintf(drift_file, "%d\n", drift);
402 } else {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200403 WriteData(&drift, sizeof(drift), drift_file,
404 absl::GetFlag(FLAGS_drift_file));
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200405 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000406 }
407
408 if (msg.has_level()) {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200409 static FILE* level_file =
410 OpenFile(absl::GetFlag(FLAGS_level_file), "wb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000411 int32_t level = msg.level();
Mirko Bonadei9d962092019-07-04 10:19:10 +0200412 if (absl::GetFlag(FLAGS_text)) {
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200413 fprintf(level_file, "%d\n", level);
414 } else {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200415 WriteData(&level, sizeof(level), level_file,
416 absl::GetFlag(FLAGS_level_file));
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200417 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000418 }
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000419
420 if (msg.has_keypress()) {
Mirko Bonadei9d962092019-07-04 10:19:10 +0200421 static FILE* keypress_file =
422 OpenFile(absl::GetFlag(FLAGS_keypress_file), "wb");
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000423 bool keypress = msg.keypress();
Mirko Bonadei9d962092019-07-04 10:19:10 +0200424 if (absl::GetFlag(FLAGS_text)) {
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200425 fprintf(keypress_file, "%d\n", keypress);
426 } else {
427 WriteData(&keypress, sizeof(keypress), keypress_file,
Mirko Bonadei9d962092019-07-04 10:19:10 +0200428 absl::GetFlag(FLAGS_keypress_file));
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200429 }
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000430 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000431 }
Minyue13b96ba2015-10-03 00:39:14 +0200432 } else if (event_msg.type() == Event::CONFIG) {
433 if (!event_msg.has_config()) {
434 printf("Corrupt input file: Config missing.\n");
435 return 1;
436 }
437 const audioproc::Config msg = event_msg.config();
438
439 fprintf(settings_file, "APM re-config at frame: %d\n", frame_count);
440
441 PRINT_CONFIG(aec_enabled);
442 PRINT_CONFIG(aec_delay_agnostic_enabled);
443 PRINT_CONFIG(aec_drift_compensation_enabled);
444 PRINT_CONFIG(aec_extended_filter_enabled);
445 PRINT_CONFIG(aec_suppression_level);
446 PRINT_CONFIG(aecm_enabled);
447 PRINT_CONFIG(aecm_comfort_noise_enabled);
448 PRINT_CONFIG(aecm_routing_mode);
449 PRINT_CONFIG(agc_enabled);
450 PRINT_CONFIG(agc_mode);
451 PRINT_CONFIG(agc_limiter_enabled);
452 PRINT_CONFIG(noise_robust_agc_enabled);
453 PRINT_CONFIG(hpf_enabled);
454 PRINT_CONFIG(ns_enabled);
455 PRINT_CONFIG(ns_level);
456 PRINT_CONFIG(transient_suppression_enabled);
Alex Loiko5feb30e2018-04-16 13:52:32 +0200457 PRINT_CONFIG(pre_amplifier_enabled);
458 PRINT_CONFIG_FLOAT(pre_amplifier_fixed_gain_factor);
459
peah7789fe72016-04-15 01:19:44 -0700460 if (msg.has_experiments_description()) {
461 fprintf(settings_file, " experiments_description: %s\n",
462 msg.experiments_description().c_str());
463 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000464 } else if (event_msg.type() == Event::INIT) {
465 if (!event_msg.has_init()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000466 printf("Corrupt input file: Init missing.\n");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000467 return 1;
468 }
469
Per Åhgren879d33b2021-01-21 10:08:15 +0100470 ++init_count;
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000471 const Init msg = event_msg.init();
472 // These should print out zeros if they're missing.
Per Åhgren879d33b2021-01-21 10:08:15 +0100473 fprintf(settings_file, "Init #%d at frame: %d\n", init_count,
474 frame_count);
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000475 int input_sample_rate = msg.sample_rate();
476 fprintf(settings_file, " Input sample rate: %d\n", input_sample_rate);
477 int output_sample_rate = msg.output_sample_rate();
478 fprintf(settings_file, " Output sample rate: %d\n", output_sample_rate);
479 int reverse_sample_rate = msg.reverse_sample_rate();
Yves Gerey665174f2018-06-19 15:03:05 +0200480 fprintf(settings_file, " Reverse sample rate: %d\n",
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000481 reverse_sample_rate);
482 num_input_channels = msg.num_input_channels();
Oleh Prypinb1686782019-08-02 09:36:47 +0200483 fprintf(settings_file, " Input channels: %" RTC_PRIuS "\n",
Peter Kasting69558702016-01-12 16:26:35 -0800484 num_input_channels);
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000485 num_output_channels = msg.num_output_channels();
Oleh Prypinb1686782019-08-02 09:36:47 +0200486 fprintf(settings_file, " Output channels: %" RTC_PRIuS "\n",
Peter Kasting69558702016-01-12 16:26:35 -0800487 num_output_channels);
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000488 num_reverse_channels = msg.num_reverse_channels();
Oleh Prypinb1686782019-08-02 09:36:47 +0200489 fprintf(settings_file, " Reverse channels: %" RTC_PRIuS "\n",
Peter Kasting69558702016-01-12 16:26:35 -0800490 num_reverse_channels);
Minyue Li656d6092018-08-10 15:38:52 +0200491 if (msg.has_timestamp_ms()) {
492 const int64_t timestamp = msg.timestamp_ms();
493 fprintf(settings_file, " Timestamp in millisecond: %" PRId64 "\n",
494 timestamp);
495 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000496
497 fprintf(settings_file, "\n");
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000498
499 if (reverse_sample_rate == 0) {
500 reverse_sample_rate = input_sample_rate;
501 }
502 if (output_sample_rate == 0) {
503 output_sample_rate = input_sample_rate;
504 }
505
pkasting25702cb2016-01-08 13:50:27 -0800506 reverse_samples_per_channel =
507 static_cast<size_t>(reverse_sample_rate / 100);
Yves Gerey665174f2018-06-19 15:03:05 +0200508 input_samples_per_channel = static_cast<size_t>(input_sample_rate / 100);
pkasting25702cb2016-01-08 13:50:27 -0800509 output_samples_per_channel =
510 static_cast<size_t>(output_sample_rate / 100);
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000511
Mirko Bonadei9d962092019-07-04 10:19:10 +0200512 if (!absl::GetFlag(FLAGS_raw)) {
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000513 // The WAV files need to be reset every time, because they cant change
514 // their sample rate or number of channels.
Per Åhgren879d33b2021-01-21 10:08:15 +0100515
516 std::string suffix = GetWavFileIndex(init_count, frame_count);
Jonas Olsson366a50c2018-09-06 13:41:30 +0200517 rtc::StringBuilder reverse_name;
Per Åhgren879d33b2021-01-21 10:08:15 +0100518 reverse_name << absl::GetFlag(FLAGS_reverse_file) << suffix << ".wav";
Yves Gerey665174f2018-06-19 15:03:05 +0200519 reverse_wav_file.reset(new WavWriter(
520 reverse_name.str(), reverse_sample_rate, num_reverse_channels));
Jonas Olsson366a50c2018-09-06 13:41:30 +0200521 rtc::StringBuilder input_name;
Per Åhgren879d33b2021-01-21 10:08:15 +0100522 input_name << absl::GetFlag(FLAGS_input_file) << suffix << ".wav";
Yves Gerey665174f2018-06-19 15:03:05 +0200523 input_wav_file.reset(new WavWriter(input_name.str(), input_sample_rate,
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000524 num_input_channels));
Jonas Olsson366a50c2018-09-06 13:41:30 +0200525 rtc::StringBuilder output_name;
Per Åhgren879d33b2021-01-21 10:08:15 +0100526 output_name << absl::GetFlag(FLAGS_output_file) << suffix << ".wav";
Yves Gerey665174f2018-06-19 15:03:05 +0200527 output_wav_file.reset(new WavWriter(
528 output_name.str(), output_sample_rate, num_output_channels));
Sam Zackrissonf61475d2018-05-11 11:34:49 +0200529
Per Åhgrenc1bfe1a2018-09-21 16:23:50 +0200530 if (WritingCallOrderFile()) {
531 rtc::StringBuilder callorder_name;
Per Åhgren879d33b2021-01-21 10:08:15 +0100532 callorder_name << absl::GetFlag(FLAGS_callorder_file) << suffix
Mirko Bonadei9d962092019-07-04 10:19:10 +0200533 << ".char";
Per Åhgrenc1bfe1a2018-09-21 16:23:50 +0200534 callorder_char_file = OpenFile(callorder_name.str(), "wb");
535 }
Fredrik Hernqvist3be9da32019-05-21 12:23:31 +0200536
537 if (WritingRuntimeSettingFiles()) {
538 for (RuntimeSettingWriter& writer : runtime_setting_writers) {
539 writer.HandleInitEvent(frame_count);
540 }
541 }
542 }
543 } else if (event_msg.type() == Event::RUNTIME_SETTING) {
544 if (WritingRuntimeSettingFiles()) {
545 for (RuntimeSettingWriter& writer : runtime_setting_writers) {
546 if (writer.IsExporterFor(event_msg)) {
547 writer.WriteEvent(event_msg, frame_count);
548 }
549 }
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000550 }
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000551 }
552 }
553
554 return 0;
555}
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000556
557} // namespace webrtc
558
559int main(int argc, char* argv[]) {
560 return webrtc::do_main(argc, argv);
561}