blob: d3f163383246fb3b7dad913f7e9c780960496309 [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
kwiberg62eaacf2016-02-17 06:39:05 -080018#include <memory>
19
pbos@webrtc.org7fad4b82013-05-28 08:11:59 +000020#include "gflags/gflags.h"
Peter Kasting69558702016-01-12 16:26:35 -080021#include "webrtc/base/format_macros.h"
kjellander78ddd732016-02-09 08:13:06 -080022#include "webrtc/modules/audio_processing/debug.pb.h"
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070023#include "webrtc/modules/audio_processing/test/protobuf_utils.h"
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000024#include "webrtc/modules/audio_processing/test/test_utils.h"
pbos@webrtc.org7fad4b82013-05-28 08:11:59 +000025#include "webrtc/typedefs.h"
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000026
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000027// TODO(andrew): unpack more of the data.
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000028DEFINE_string(input_file, "input", "The name of the input stream file.");
29DEFINE_string(output_file, "ref_out",
andrew@webrtc.orgcd824382011-11-11 19:13:36 +000030 "The name of the reference output stream file.");
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000031DEFINE_string(reverse_file, "reverse",
andrew@webrtc.orgcd824382011-11-11 19:13:36 +000032 "The name of the reverse input stream file.");
33DEFINE_string(delay_file, "delay.int32", "The name of the delay file.");
34DEFINE_string(drift_file, "drift.int32", "The name of the drift file.");
35DEFINE_string(level_file, "level.int32", "The name of the level file.");
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +000036DEFINE_string(keypress_file, "keypress.bool", "The name of the keypress file.");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +000037DEFINE_string(settings_file, "settings.txt", "The name of the settings file.");
38DEFINE_bool(full, false,
39 "Unpack the full set of files (normally not needed).");
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000040DEFINE_bool(raw, false, "Write raw data instead of a WAV file.");
Bjorn Volcker40a6d592015-05-06 10:51:34 +020041DEFINE_bool(text,
42 false,
43 "Write non-audio files as text files instead of binary files.");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000044
Minyue13b96ba2015-10-03 00:39:14 +020045#define PRINT_CONFIG(field_name) \
46 if (msg.has_##field_name()) { \
47 fprintf(settings_file, " " #field_name ": %d\n", msg.field_name()); \
48 }
49
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000050namespace webrtc {
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000051
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000052using audioproc::Event;
53using audioproc::ReverseStream;
54using audioproc::Stream;
55using audioproc::Init;
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000056
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000057void WriteData(const void* data, size_t size, FILE* file,
58 const std::string& filename) {
59 if (fwrite(data, size, 1, file) != 1) {
60 printf("Error when writing to %s\n", filename.c_str());
61 exit(1);
62 }
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000063}
64
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000065int do_main(int argc, char* argv[]) {
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000066 std::string program_name = argv[0];
67 std::string usage = "Commandline tool to unpack audioproc debug files.\n"
68 "Example usage:\n" + program_name + " debug_dump.pb\n";
69 google::SetUsageMessage(usage);
70 google::ParseCommandLineFlags(&argc, &argv, true);
71
72 if (argc < 2) {
73 printf("%s", google::ProgramUsage());
74 return 1;
75 }
76
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000077 FILE* debug_file = OpenFile(argv[1], "rb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +000078
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000079 Event event_msg;
andrew@webrtc.orgcd824382011-11-11 19:13:36 +000080 int frame_count = 0;
pkasting25702cb2016-01-08 13:50:27 -080081 size_t reverse_samples_per_channel = 0;
82 size_t input_samples_per_channel = 0;
83 size_t output_samples_per_channel = 0;
Peter Kasting69558702016-01-12 16:26:35 -080084 size_t num_reverse_channels = 0;
85 size_t num_input_channels = 0;
86 size_t num_output_channels = 0;
kwiberg62eaacf2016-02-17 06:39:05 -080087 std::unique_ptr<WavWriter> reverse_wav_file;
88 std::unique_ptr<WavWriter> input_wav_file;
89 std::unique_ptr<WavWriter> output_wav_file;
90 std::unique_ptr<RawFile> reverse_raw_file;
91 std::unique_ptr<RawFile> input_raw_file;
92 std::unique_ptr<RawFile> output_raw_file;
Minyue13b96ba2015-10-03 00:39:14 +020093
94 FILE* settings_file = OpenFile(FLAGS_settings_file, "wb");
95
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +000096 while (ReadMessageFromFile(debug_file, &event_msg)) {
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000097 if (event_msg.type() == Event::REVERSE_STREAM) {
98 if (!event_msg.has_reverse_stream()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000099 printf("Corrupt input file: ReverseStream missing.\n");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000100 return 1;
101 }
102
103 const ReverseStream msg = event_msg.reverse_stream();
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000104 if (msg.has_data()) {
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000105 if (FLAGS_raw && !reverse_raw_file) {
106 reverse_raw_file.reset(new RawFile(FLAGS_reverse_file + ".pcm"));
107 }
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000108 // TODO(aluebs): Replace "num_reverse_channels *
109 // reverse_samples_per_channel" with "msg.data().size() /
110 // sizeof(int16_t)" and so on when this fix in audio_processing has made
111 // it into stable: https://webrtc-codereview.appspot.com/15299004/
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000112 WriteIntData(reinterpret_cast<const int16_t*>(msg.data().data()),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000113 num_reverse_channels * reverse_samples_per_channel,
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000114 reverse_wav_file.get(),
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000115 reverse_raw_file.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000116 } else if (msg.channel_size() > 0) {
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000117 if (FLAGS_raw && !reverse_raw_file) {
118 reverse_raw_file.reset(new RawFile(FLAGS_reverse_file + ".float"));
119 }
kwiberg62eaacf2016-02-17 06:39:05 -0800120 std::unique_ptr<const float* []> data(
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000121 new const float* [num_reverse_channels]);
Peter Kasting69558702016-01-12 16:26:35 -0800122 for (size_t i = 0; i < num_reverse_channels; ++i) {
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000123 data[i] = reinterpret_cast<const float*>(msg.channel(i).data());
124 }
125 WriteFloatData(data.get(),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000126 reverse_samples_per_channel,
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000127 num_reverse_channels,
128 reverse_wav_file.get(),
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000129 reverse_raw_file.get());
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000130 }
131 } else if (event_msg.type() == Event::STREAM) {
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000132 frame_count++;
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000133 if (!event_msg.has_stream()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000134 printf("Corrupt input file: Stream missing.\n");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000135 return 1;
136 }
137
138 const Stream msg = event_msg.stream();
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000139 if (msg.has_input_data()) {
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000140 if (FLAGS_raw && !input_raw_file) {
141 input_raw_file.reset(new RawFile(FLAGS_input_file + ".pcm"));
142 }
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000143 WriteIntData(reinterpret_cast<const int16_t*>(msg.input_data().data()),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000144 num_input_channels * input_samples_per_channel,
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000145 input_wav_file.get(),
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000146 input_raw_file.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000147 } else if (msg.input_channel_size() > 0) {
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000148 if (FLAGS_raw && !input_raw_file) {
149 input_raw_file.reset(new RawFile(FLAGS_input_file + ".float"));
150 }
kwiberg62eaacf2016-02-17 06:39:05 -0800151 std::unique_ptr<const float* []> data(
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000152 new const float* [num_input_channels]);
Peter Kasting69558702016-01-12 16:26:35 -0800153 for (size_t i = 0; i < num_input_channels; ++i) {
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000154 data[i] = reinterpret_cast<const float*>(msg.input_channel(i).data());
155 }
156 WriteFloatData(data.get(),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000157 input_samples_per_channel,
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000158 num_input_channels,
159 input_wav_file.get(),
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000160 input_raw_file.get());
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000161 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000162
163 if (msg.has_output_data()) {
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000164 if (FLAGS_raw && !output_raw_file) {
165 output_raw_file.reset(new RawFile(FLAGS_output_file + ".pcm"));
166 }
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000167 WriteIntData(reinterpret_cast<const int16_t*>(msg.output_data().data()),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000168 num_output_channels * output_samples_per_channel,
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000169 output_wav_file.get(),
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000170 output_raw_file.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000171 } else if (msg.output_channel_size() > 0) {
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000172 if (FLAGS_raw && !output_raw_file) {
173 output_raw_file.reset(new RawFile(FLAGS_output_file + ".float"));
174 }
kwiberg62eaacf2016-02-17 06:39:05 -0800175 std::unique_ptr<const float* []> data(
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000176 new const float* [num_output_channels]);
Peter Kasting69558702016-01-12 16:26:35 -0800177 for (size_t i = 0; i < num_output_channels; ++i) {
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000178 data[i] =
179 reinterpret_cast<const float*>(msg.output_channel(i).data());
180 }
181 WriteFloatData(data.get(),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000182 output_samples_per_channel,
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000183 num_output_channels,
184 output_wav_file.get(),
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000185 output_raw_file.get());
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000186 }
187
188 if (FLAGS_full) {
189 if (msg.has_delay()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000190 static FILE* delay_file = OpenFile(FLAGS_delay_file, "wb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000191 int32_t delay = msg.delay();
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200192 if (FLAGS_text) {
193 fprintf(delay_file, "%d\n", delay);
194 } else {
195 WriteData(&delay, sizeof(delay), delay_file, FLAGS_delay_file);
196 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000197 }
198
199 if (msg.has_drift()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000200 static FILE* drift_file = OpenFile(FLAGS_drift_file, "wb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000201 int32_t drift = msg.drift();
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200202 if (FLAGS_text) {
203 fprintf(drift_file, "%d\n", drift);
204 } else {
205 WriteData(&drift, sizeof(drift), drift_file, FLAGS_drift_file);
206 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000207 }
208
209 if (msg.has_level()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000210 static FILE* level_file = OpenFile(FLAGS_level_file, "wb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000211 int32_t level = msg.level();
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200212 if (FLAGS_text) {
213 fprintf(level_file, "%d\n", level);
214 } else {
215 WriteData(&level, sizeof(level), level_file, FLAGS_level_file);
216 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000217 }
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000218
219 if (msg.has_keypress()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000220 static FILE* keypress_file = OpenFile(FLAGS_keypress_file, "wb");
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000221 bool keypress = msg.keypress();
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200222 if (FLAGS_text) {
223 fprintf(keypress_file, "%d\n", keypress);
224 } else {
225 WriteData(&keypress, sizeof(keypress), keypress_file,
226 FLAGS_keypress_file);
227 }
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000228 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000229 }
Minyue13b96ba2015-10-03 00:39:14 +0200230 } else if (event_msg.type() == Event::CONFIG) {
231 if (!event_msg.has_config()) {
232 printf("Corrupt input file: Config missing.\n");
233 return 1;
234 }
235 const audioproc::Config msg = event_msg.config();
236
237 fprintf(settings_file, "APM re-config at frame: %d\n", frame_count);
238
239 PRINT_CONFIG(aec_enabled);
240 PRINT_CONFIG(aec_delay_agnostic_enabled);
241 PRINT_CONFIG(aec_drift_compensation_enabled);
242 PRINT_CONFIG(aec_extended_filter_enabled);
243 PRINT_CONFIG(aec_suppression_level);
244 PRINT_CONFIG(aecm_enabled);
245 PRINT_CONFIG(aecm_comfort_noise_enabled);
246 PRINT_CONFIG(aecm_routing_mode);
247 PRINT_CONFIG(agc_enabled);
248 PRINT_CONFIG(agc_mode);
249 PRINT_CONFIG(agc_limiter_enabled);
250 PRINT_CONFIG(noise_robust_agc_enabled);
251 PRINT_CONFIG(hpf_enabled);
252 PRINT_CONFIG(ns_enabled);
253 PRINT_CONFIG(ns_level);
254 PRINT_CONFIG(transient_suppression_enabled);
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700255 PRINT_CONFIG(intelligibility_enhancer_enabled);
peah7789fe72016-04-15 01:19:44 -0700256 if (msg.has_experiments_description()) {
257 fprintf(settings_file, " experiments_description: %s\n",
258 msg.experiments_description().c_str());
259 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000260 } else if (event_msg.type() == Event::INIT) {
261 if (!event_msg.has_init()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000262 printf("Corrupt input file: Init missing.\n");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000263 return 1;
264 }
265
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000266 const Init msg = event_msg.init();
267 // These should print out zeros if they're missing.
268 fprintf(settings_file, "Init at frame: %d\n", frame_count);
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000269 int input_sample_rate = msg.sample_rate();
270 fprintf(settings_file, " Input sample rate: %d\n", input_sample_rate);
271 int output_sample_rate = msg.output_sample_rate();
272 fprintf(settings_file, " Output sample rate: %d\n", output_sample_rate);
273 int reverse_sample_rate = msg.reverse_sample_rate();
274 fprintf(settings_file,
275 " Reverse sample rate: %d\n",
276 reverse_sample_rate);
277 num_input_channels = msg.num_input_channels();
Peter Kasting69558702016-01-12 16:26:35 -0800278 fprintf(settings_file, " Input channels: %" PRIuS "\n",
279 num_input_channels);
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000280 num_output_channels = msg.num_output_channels();
Peter Kasting69558702016-01-12 16:26:35 -0800281 fprintf(settings_file, " Output channels: %" PRIuS "\n",
282 num_output_channels);
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000283 num_reverse_channels = msg.num_reverse_channels();
Peter Kasting69558702016-01-12 16:26:35 -0800284 fprintf(settings_file, " Reverse channels: %" PRIuS "\n",
285 num_reverse_channels);
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000286
287 fprintf(settings_file, "\n");
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000288
289 if (reverse_sample_rate == 0) {
290 reverse_sample_rate = input_sample_rate;
291 }
292 if (output_sample_rate == 0) {
293 output_sample_rate = input_sample_rate;
294 }
295
pkasting25702cb2016-01-08 13:50:27 -0800296 reverse_samples_per_channel =
297 static_cast<size_t>(reverse_sample_rate / 100);
298 input_samples_per_channel =
299 static_cast<size_t>(input_sample_rate / 100);
300 output_samples_per_channel =
301 static_cast<size_t>(output_sample_rate / 100);
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000302
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000303 if (!FLAGS_raw) {
304 // The WAV files need to be reset every time, because they cant change
305 // their sample rate or number of channels.
aluebsf03a8d42016-06-17 09:41:41 -0700306 std::stringstream reverse_name;
307 reverse_name << FLAGS_reverse_file << frame_count << ".wav";
308 reverse_wav_file.reset(new WavWriter(reverse_name.str(),
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000309 reverse_sample_rate,
310 num_reverse_channels));
aluebsf03a8d42016-06-17 09:41:41 -0700311 std::stringstream input_name;
312 input_name << FLAGS_input_file << frame_count << ".wav";
313 input_wav_file.reset(new WavWriter(input_name.str(),
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000314 input_sample_rate,
315 num_input_channels));
aluebsf03a8d42016-06-17 09:41:41 -0700316 std::stringstream output_name;
317 output_name << FLAGS_output_file << frame_count << ".wav";
318 output_wav_file.reset(new WavWriter(output_name.str(),
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000319 output_sample_rate,
320 num_output_channels));
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000321 }
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000322 }
323 }
324
325 return 0;
326}
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000327
328} // namespace webrtc
329
330int main(int argc, char* argv[]) {
331 return webrtc::do_main(argc, argv);
332}