blob: a1e594ced34930dbe3cc005dcd23b120ea080b97 [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
16#include <stdio.h>
17
pbos@webrtc.org7fad4b82013-05-28 08:11:59 +000018#include "gflags/gflags.h"
Peter Kasting69558702016-01-12 16:26:35 -080019#include "webrtc/base/format_macros.h"
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000020#include "webrtc/base/scoped_ptr.h"
kjellander78ddd732016-02-09 08:13:06 -080021#include "webrtc/modules/audio_processing/debug.pb.h"
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070022#include "webrtc/modules/audio_processing/test/protobuf_utils.h"
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000023#include "webrtc/modules/audio_processing/test/test_utils.h"
pbos@webrtc.org7fad4b82013-05-28 08:11:59 +000024#include "webrtc/typedefs.h"
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000025
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000026// TODO(andrew): unpack more of the data.
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000027DEFINE_string(input_file, "input", "The name of the input stream file.");
28DEFINE_string(output_file, "ref_out",
andrew@webrtc.orgcd824382011-11-11 19:13:36 +000029 "The name of the reference output stream file.");
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000030DEFINE_string(reverse_file, "reverse",
andrew@webrtc.orgcd824382011-11-11 19:13:36 +000031 "The name of the reverse input stream file.");
32DEFINE_string(delay_file, "delay.int32", "The name of the delay file.");
33DEFINE_string(drift_file, "drift.int32", "The name of the drift file.");
34DEFINE_string(level_file, "level.int32", "The name of the level file.");
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +000035DEFINE_string(keypress_file, "keypress.bool", "The name of the keypress file.");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +000036DEFINE_string(settings_file, "settings.txt", "The name of the settings file.");
37DEFINE_bool(full, false,
38 "Unpack the full set of files (normally not needed).");
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000039DEFINE_bool(raw, false, "Write raw data instead of a WAV file.");
Bjorn Volcker40a6d592015-05-06 10:51:34 +020040DEFINE_bool(text,
41 false,
42 "Write non-audio files as text files instead of binary files.");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000043
Minyue13b96ba2015-10-03 00:39:14 +020044#define PRINT_CONFIG(field_name) \
45 if (msg.has_##field_name()) { \
46 fprintf(settings_file, " " #field_name ": %d\n", msg.field_name()); \
47 }
48
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000049namespace webrtc {
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000050
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000051using audioproc::Event;
52using audioproc::ReverseStream;
53using audioproc::Stream;
54using audioproc::Init;
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000055
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000056void WriteData(const void* data, size_t size, FILE* file,
57 const std::string& filename) {
58 if (fwrite(data, size, 1, file) != 1) {
59 printf("Error when writing to %s\n", filename.c_str());
60 exit(1);
61 }
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000062}
63
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000064int do_main(int argc, char* argv[]) {
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000065 std::string program_name = argv[0];
66 std::string usage = "Commandline tool to unpack audioproc debug files.\n"
67 "Example usage:\n" + program_name + " debug_dump.pb\n";
68 google::SetUsageMessage(usage);
69 google::ParseCommandLineFlags(&argc, &argv, true);
70
71 if (argc < 2) {
72 printf("%s", google::ProgramUsage());
73 return 1;
74 }
75
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000076 FILE* debug_file = OpenFile(argv[1], "rb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +000077
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000078 Event event_msg;
andrew@webrtc.orgcd824382011-11-11 19:13:36 +000079 int frame_count = 0;
pkasting25702cb2016-01-08 13:50:27 -080080 size_t reverse_samples_per_channel = 0;
81 size_t input_samples_per_channel = 0;
82 size_t output_samples_per_channel = 0;
Peter Kasting69558702016-01-12 16:26:35 -080083 size_t num_reverse_channels = 0;
84 size_t num_input_channels = 0;
85 size_t num_output_channels = 0;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000086 rtc::scoped_ptr<WavWriter> reverse_wav_file;
87 rtc::scoped_ptr<WavWriter> input_wav_file;
88 rtc::scoped_ptr<WavWriter> output_wav_file;
89 rtc::scoped_ptr<RawFile> reverse_raw_file;
90 rtc::scoped_ptr<RawFile> input_raw_file;
91 rtc::scoped_ptr<RawFile> output_raw_file;
Minyue13b96ba2015-10-03 00:39:14 +020092
93 FILE* settings_file = OpenFile(FLAGS_settings_file, "wb");
94
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +000095 while (ReadMessageFromFile(debug_file, &event_msg)) {
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000096 if (event_msg.type() == Event::REVERSE_STREAM) {
97 if (!event_msg.has_reverse_stream()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000098 printf("Corrupt input file: ReverseStream missing.\n");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000099 return 1;
100 }
101
102 const ReverseStream msg = event_msg.reverse_stream();
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000103 if (msg.has_data()) {
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000104 if (FLAGS_raw && !reverse_raw_file) {
105 reverse_raw_file.reset(new RawFile(FLAGS_reverse_file + ".pcm"));
106 }
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000107 // TODO(aluebs): Replace "num_reverse_channels *
108 // reverse_samples_per_channel" with "msg.data().size() /
109 // sizeof(int16_t)" and so on when this fix in audio_processing has made
110 // it into stable: https://webrtc-codereview.appspot.com/15299004/
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000111 WriteIntData(reinterpret_cast<const int16_t*>(msg.data().data()),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000112 num_reverse_channels * reverse_samples_per_channel,
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000113 reverse_wav_file.get(),
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000114 reverse_raw_file.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000115 } else if (msg.channel_size() > 0) {
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000116 if (FLAGS_raw && !reverse_raw_file) {
117 reverse_raw_file.reset(new RawFile(FLAGS_reverse_file + ".float"));
118 }
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000119 rtc::scoped_ptr<const float* []> data(
120 new const float* [num_reverse_channels]);
Peter Kasting69558702016-01-12 16:26:35 -0800121 for (size_t i = 0; i < num_reverse_channels; ++i) {
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000122 data[i] = reinterpret_cast<const float*>(msg.channel(i).data());
123 }
124 WriteFloatData(data.get(),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000125 reverse_samples_per_channel,
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000126 num_reverse_channels,
127 reverse_wav_file.get(),
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000128 reverse_raw_file.get());
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000129 }
130 } else if (event_msg.type() == Event::STREAM) {
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000131 frame_count++;
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000132 if (!event_msg.has_stream()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000133 printf("Corrupt input file: Stream missing.\n");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000134 return 1;
135 }
136
137 const Stream msg = event_msg.stream();
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000138 if (msg.has_input_data()) {
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000139 if (FLAGS_raw && !input_raw_file) {
140 input_raw_file.reset(new RawFile(FLAGS_input_file + ".pcm"));
141 }
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000142 WriteIntData(reinterpret_cast<const int16_t*>(msg.input_data().data()),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000143 num_input_channels * input_samples_per_channel,
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000144 input_wav_file.get(),
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000145 input_raw_file.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000146 } else if (msg.input_channel_size() > 0) {
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000147 if (FLAGS_raw && !input_raw_file) {
148 input_raw_file.reset(new RawFile(FLAGS_input_file + ".float"));
149 }
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000150 rtc::scoped_ptr<const float* []> data(
151 new const float* [num_input_channels]);
Peter Kasting69558702016-01-12 16:26:35 -0800152 for (size_t i = 0; i < num_input_channels; ++i) {
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000153 data[i] = reinterpret_cast<const float*>(msg.input_channel(i).data());
154 }
155 WriteFloatData(data.get(),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000156 input_samples_per_channel,
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000157 num_input_channels,
158 input_wav_file.get(),
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000159 input_raw_file.get());
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000160 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000161
162 if (msg.has_output_data()) {
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000163 if (FLAGS_raw && !output_raw_file) {
164 output_raw_file.reset(new RawFile(FLAGS_output_file + ".pcm"));
165 }
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000166 WriteIntData(reinterpret_cast<const int16_t*>(msg.output_data().data()),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000167 num_output_channels * output_samples_per_channel,
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000168 output_wav_file.get(),
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000169 output_raw_file.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000170 } else if (msg.output_channel_size() > 0) {
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000171 if (FLAGS_raw && !output_raw_file) {
172 output_raw_file.reset(new RawFile(FLAGS_output_file + ".float"));
173 }
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000174 rtc::scoped_ptr<const float* []> data(
175 new const float* [num_output_channels]);
Peter Kasting69558702016-01-12 16:26:35 -0800176 for (size_t i = 0; i < num_output_channels; ++i) {
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000177 data[i] =
178 reinterpret_cast<const float*>(msg.output_channel(i).data());
179 }
180 WriteFloatData(data.get(),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000181 output_samples_per_channel,
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000182 num_output_channels,
183 output_wav_file.get(),
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000184 output_raw_file.get());
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000185 }
186
187 if (FLAGS_full) {
188 if (msg.has_delay()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000189 static FILE* delay_file = OpenFile(FLAGS_delay_file, "wb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000190 int32_t delay = msg.delay();
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200191 if (FLAGS_text) {
192 fprintf(delay_file, "%d\n", delay);
193 } else {
194 WriteData(&delay, sizeof(delay), delay_file, FLAGS_delay_file);
195 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000196 }
197
198 if (msg.has_drift()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000199 static FILE* drift_file = OpenFile(FLAGS_drift_file, "wb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000200 int32_t drift = msg.drift();
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200201 if (FLAGS_text) {
202 fprintf(drift_file, "%d\n", drift);
203 } else {
204 WriteData(&drift, sizeof(drift), drift_file, FLAGS_drift_file);
205 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000206 }
207
208 if (msg.has_level()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000209 static FILE* level_file = OpenFile(FLAGS_level_file, "wb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000210 int32_t level = msg.level();
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200211 if (FLAGS_text) {
212 fprintf(level_file, "%d\n", level);
213 } else {
214 WriteData(&level, sizeof(level), level_file, FLAGS_level_file);
215 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000216 }
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000217
218 if (msg.has_keypress()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000219 static FILE* keypress_file = OpenFile(FLAGS_keypress_file, "wb");
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000220 bool keypress = msg.keypress();
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200221 if (FLAGS_text) {
222 fprintf(keypress_file, "%d\n", keypress);
223 } else {
224 WriteData(&keypress, sizeof(keypress), keypress_file,
225 FLAGS_keypress_file);
226 }
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000227 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000228 }
Minyue13b96ba2015-10-03 00:39:14 +0200229 } else if (event_msg.type() == Event::CONFIG) {
230 if (!event_msg.has_config()) {
231 printf("Corrupt input file: Config missing.\n");
232 return 1;
233 }
234 const audioproc::Config msg = event_msg.config();
235
236 fprintf(settings_file, "APM re-config at frame: %d\n", frame_count);
237
238 PRINT_CONFIG(aec_enabled);
239 PRINT_CONFIG(aec_delay_agnostic_enabled);
240 PRINT_CONFIG(aec_drift_compensation_enabled);
241 PRINT_CONFIG(aec_extended_filter_enabled);
242 PRINT_CONFIG(aec_suppression_level);
243 PRINT_CONFIG(aecm_enabled);
244 PRINT_CONFIG(aecm_comfort_noise_enabled);
245 PRINT_CONFIG(aecm_routing_mode);
246 PRINT_CONFIG(agc_enabled);
247 PRINT_CONFIG(agc_mode);
248 PRINT_CONFIG(agc_limiter_enabled);
249 PRINT_CONFIG(noise_robust_agc_enabled);
250 PRINT_CONFIG(hpf_enabled);
251 PRINT_CONFIG(ns_enabled);
252 PRINT_CONFIG(ns_level);
253 PRINT_CONFIG(transient_suppression_enabled);
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000254 } else if (event_msg.type() == Event::INIT) {
255 if (!event_msg.has_init()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000256 printf("Corrupt input file: Init missing.\n");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000257 return 1;
258 }
259
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000260 const Init msg = event_msg.init();
261 // These should print out zeros if they're missing.
262 fprintf(settings_file, "Init at frame: %d\n", frame_count);
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000263 int input_sample_rate = msg.sample_rate();
264 fprintf(settings_file, " Input sample rate: %d\n", input_sample_rate);
265 int output_sample_rate = msg.output_sample_rate();
266 fprintf(settings_file, " Output sample rate: %d\n", output_sample_rate);
267 int reverse_sample_rate = msg.reverse_sample_rate();
268 fprintf(settings_file,
269 " Reverse sample rate: %d\n",
270 reverse_sample_rate);
271 num_input_channels = msg.num_input_channels();
Peter Kasting69558702016-01-12 16:26:35 -0800272 fprintf(settings_file, " Input channels: %" PRIuS "\n",
273 num_input_channels);
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000274 num_output_channels = msg.num_output_channels();
Peter Kasting69558702016-01-12 16:26:35 -0800275 fprintf(settings_file, " Output channels: %" PRIuS "\n",
276 num_output_channels);
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000277 num_reverse_channels = msg.num_reverse_channels();
Peter Kasting69558702016-01-12 16:26:35 -0800278 fprintf(settings_file, " Reverse channels: %" PRIuS "\n",
279 num_reverse_channels);
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000280
281 fprintf(settings_file, "\n");
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000282
283 if (reverse_sample_rate == 0) {
284 reverse_sample_rate = input_sample_rate;
285 }
286 if (output_sample_rate == 0) {
287 output_sample_rate = input_sample_rate;
288 }
289
pkasting25702cb2016-01-08 13:50:27 -0800290 reverse_samples_per_channel =
291 static_cast<size_t>(reverse_sample_rate / 100);
292 input_samples_per_channel =
293 static_cast<size_t>(input_sample_rate / 100);
294 output_samples_per_channel =
295 static_cast<size_t>(output_sample_rate / 100);
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000296
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000297 if (!FLAGS_raw) {
298 // The WAV files need to be reset every time, because they cant change
299 // their sample rate or number of channels.
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000300 reverse_wav_file.reset(new WavWriter(FLAGS_reverse_file + ".wav",
301 reverse_sample_rate,
302 num_reverse_channels));
303 input_wav_file.reset(new WavWriter(FLAGS_input_file + ".wav",
304 input_sample_rate,
305 num_input_channels));
306 output_wav_file.reset(new WavWriter(FLAGS_output_file + ".wav",
307 output_sample_rate,
308 num_output_channels));
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000309 }
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000310 }
311 }
312
313 return 0;
314}
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000315
316} // namespace webrtc
317
318int main(int argc, char* argv[]) {
319 return webrtc::do_main(argc, argv);
320}