blob: 24578e240c60404ac0e7df76917dc83b53952aac [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"
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000019#include "webrtc/audio_processing/debug.pb.h"
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000020#include "webrtc/base/scoped_ptr.h"
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070021#include "webrtc/modules/audio_processing/test/protobuf_utils.h"
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000022#include "webrtc/modules/audio_processing/test/test_utils.h"
pbos@webrtc.org7fad4b82013-05-28 08:11:59 +000023#include "webrtc/typedefs.h"
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000024
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000025// TODO(andrew): unpack more of the data.
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000026DEFINE_string(input_file, "input", "The name of the input stream file.");
27DEFINE_string(output_file, "ref_out",
andrew@webrtc.orgcd824382011-11-11 19:13:36 +000028 "The name of the reference output stream file.");
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000029DEFINE_string(reverse_file, "reverse",
andrew@webrtc.orgcd824382011-11-11 19:13:36 +000030 "The name of the reverse input stream file.");
31DEFINE_string(delay_file, "delay.int32", "The name of the delay file.");
32DEFINE_string(drift_file, "drift.int32", "The name of the drift file.");
33DEFINE_string(level_file, "level.int32", "The name of the level file.");
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +000034DEFINE_string(keypress_file, "keypress.bool", "The name of the keypress file.");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +000035DEFINE_string(settings_file, "settings.txt", "The name of the settings file.");
36DEFINE_bool(full, false,
37 "Unpack the full set of files (normally not needed).");
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000038DEFINE_bool(raw, false, "Write raw data instead of a WAV file.");
Bjorn Volcker40a6d592015-05-06 10:51:34 +020039DEFINE_bool(text,
40 false,
41 "Write non-audio files as text files instead of binary files.");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000042
Minyue13b96ba2015-10-03 00:39:14 +020043#define PRINT_CONFIG(field_name) \
44 if (msg.has_##field_name()) { \
45 fprintf(settings_file, " " #field_name ": %d\n", msg.field_name()); \
46 }
47
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000048namespace webrtc {
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000049
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000050using audioproc::Event;
51using audioproc::ReverseStream;
52using audioproc::Stream;
53using audioproc::Init;
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000054
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000055void WriteData(const void* data, size_t size, FILE* file,
56 const std::string& filename) {
57 if (fwrite(data, size, 1, file) != 1) {
58 printf("Error when writing to %s\n", filename.c_str());
59 exit(1);
60 }
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000061}
62
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000063int do_main(int argc, char* argv[]) {
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000064 std::string program_name = argv[0];
65 std::string usage = "Commandline tool to unpack audioproc debug files.\n"
66 "Example usage:\n" + program_name + " debug_dump.pb\n";
67 google::SetUsageMessage(usage);
68 google::ParseCommandLineFlags(&argc, &argv, true);
69
70 if (argc < 2) {
71 printf("%s", google::ProgramUsage());
72 return 1;
73 }
74
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000075 FILE* debug_file = OpenFile(argv[1], "rb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +000076
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000077 Event event_msg;
andrew@webrtc.orgcd824382011-11-11 19:13:36 +000078 int frame_count = 0;
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +000079 int reverse_samples_per_channel = 0;
80 int input_samples_per_channel = 0;
81 int output_samples_per_channel = 0;
82 int num_reverse_channels = 0;
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +000083 int num_input_channels = 0;
84 int num_output_channels = 0;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000085 rtc::scoped_ptr<WavWriter> reverse_wav_file;
86 rtc::scoped_ptr<WavWriter> input_wav_file;
87 rtc::scoped_ptr<WavWriter> output_wav_file;
88 rtc::scoped_ptr<RawFile> reverse_raw_file;
89 rtc::scoped_ptr<RawFile> input_raw_file;
90 rtc::scoped_ptr<RawFile> output_raw_file;
Minyue13b96ba2015-10-03 00:39:14 +020091
92 FILE* settings_file = OpenFile(FLAGS_settings_file, "wb");
93
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +000094 while (ReadMessageFromFile(debug_file, &event_msg)) {
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000095 if (event_msg.type() == Event::REVERSE_STREAM) {
96 if (!event_msg.has_reverse_stream()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000097 printf("Corrupt input file: ReverseStream missing.\n");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000098 return 1;
99 }
100
101 const ReverseStream msg = event_msg.reverse_stream();
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000102 if (msg.has_data()) {
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000103 if (FLAGS_raw && !reverse_raw_file) {
104 reverse_raw_file.reset(new RawFile(FLAGS_reverse_file + ".pcm"));
105 }
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000106 // TODO(aluebs): Replace "num_reverse_channels *
107 // reverse_samples_per_channel" with "msg.data().size() /
108 // sizeof(int16_t)" and so on when this fix in audio_processing has made
109 // it into stable: https://webrtc-codereview.appspot.com/15299004/
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000110 WriteIntData(reinterpret_cast<const int16_t*>(msg.data().data()),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000111 num_reverse_channels * reverse_samples_per_channel,
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000112 reverse_wav_file.get(),
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000113 reverse_raw_file.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000114 } else if (msg.channel_size() > 0) {
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000115 if (FLAGS_raw && !reverse_raw_file) {
116 reverse_raw_file.reset(new RawFile(FLAGS_reverse_file + ".float"));
117 }
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000118 rtc::scoped_ptr<const float* []> data(
119 new const float* [num_reverse_channels]);
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000120 for (int i = 0; i < num_reverse_channels; ++i) {
121 data[i] = reinterpret_cast<const float*>(msg.channel(i).data());
122 }
123 WriteFloatData(data.get(),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000124 reverse_samples_per_channel,
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000125 num_reverse_channels,
126 reverse_wav_file.get(),
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000127 reverse_raw_file.get());
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000128 }
129 } else if (event_msg.type() == Event::STREAM) {
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000130 frame_count++;
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000131 if (!event_msg.has_stream()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000132 printf("Corrupt input file: Stream missing.\n");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000133 return 1;
134 }
135
136 const Stream msg = event_msg.stream();
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000137 if (msg.has_input_data()) {
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000138 if (FLAGS_raw && !input_raw_file) {
139 input_raw_file.reset(new RawFile(FLAGS_input_file + ".pcm"));
140 }
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000141 WriteIntData(reinterpret_cast<const int16_t*>(msg.input_data().data()),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000142 num_input_channels * input_samples_per_channel,
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000143 input_wav_file.get(),
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000144 input_raw_file.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000145 } else if (msg.input_channel_size() > 0) {
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000146 if (FLAGS_raw && !input_raw_file) {
147 input_raw_file.reset(new RawFile(FLAGS_input_file + ".float"));
148 }
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000149 rtc::scoped_ptr<const float* []> data(
150 new const float* [num_input_channels]);
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000151 for (int i = 0; i < num_input_channels; ++i) {
152 data[i] = reinterpret_cast<const float*>(msg.input_channel(i).data());
153 }
154 WriteFloatData(data.get(),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000155 input_samples_per_channel,
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000156 num_input_channels,
157 input_wav_file.get(),
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000158 input_raw_file.get());
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000159 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000160
161 if (msg.has_output_data()) {
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000162 if (FLAGS_raw && !output_raw_file) {
163 output_raw_file.reset(new RawFile(FLAGS_output_file + ".pcm"));
164 }
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000165 WriteIntData(reinterpret_cast<const int16_t*>(msg.output_data().data()),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000166 num_output_channels * output_samples_per_channel,
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000167 output_wav_file.get(),
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000168 output_raw_file.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000169 } else if (msg.output_channel_size() > 0) {
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000170 if (FLAGS_raw && !output_raw_file) {
171 output_raw_file.reset(new RawFile(FLAGS_output_file + ".float"));
172 }
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000173 rtc::scoped_ptr<const float* []> data(
174 new const float* [num_output_channels]);
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000175 for (int i = 0; i < num_output_channels; ++i) {
176 data[i] =
177 reinterpret_cast<const float*>(msg.output_channel(i).data());
178 }
179 WriteFloatData(data.get(),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000180 output_samples_per_channel,
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000181 num_output_channels,
182 output_wav_file.get(),
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000183 output_raw_file.get());
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000184 }
185
186 if (FLAGS_full) {
187 if (msg.has_delay()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000188 static FILE* delay_file = OpenFile(FLAGS_delay_file, "wb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000189 int32_t delay = msg.delay();
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200190 if (FLAGS_text) {
191 fprintf(delay_file, "%d\n", delay);
192 } else {
193 WriteData(&delay, sizeof(delay), delay_file, FLAGS_delay_file);
194 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000195 }
196
197 if (msg.has_drift()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000198 static FILE* drift_file = OpenFile(FLAGS_drift_file, "wb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000199 int32_t drift = msg.drift();
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200200 if (FLAGS_text) {
201 fprintf(drift_file, "%d\n", drift);
202 } else {
203 WriteData(&drift, sizeof(drift), drift_file, FLAGS_drift_file);
204 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000205 }
206
207 if (msg.has_level()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000208 static FILE* level_file = OpenFile(FLAGS_level_file, "wb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000209 int32_t level = msg.level();
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200210 if (FLAGS_text) {
211 fprintf(level_file, "%d\n", level);
212 } else {
213 WriteData(&level, sizeof(level), level_file, FLAGS_level_file);
214 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000215 }
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000216
217 if (msg.has_keypress()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000218 static FILE* keypress_file = OpenFile(FLAGS_keypress_file, "wb");
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000219 bool keypress = msg.keypress();
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200220 if (FLAGS_text) {
221 fprintf(keypress_file, "%d\n", keypress);
222 } else {
223 WriteData(&keypress, sizeof(keypress), keypress_file,
224 FLAGS_keypress_file);
225 }
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000226 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000227 }
Minyue13b96ba2015-10-03 00:39:14 +0200228 } else if (event_msg.type() == Event::CONFIG) {
229 if (!event_msg.has_config()) {
230 printf("Corrupt input file: Config missing.\n");
231 return 1;
232 }
233 const audioproc::Config msg = event_msg.config();
234
235 fprintf(settings_file, "APM re-config at frame: %d\n", frame_count);
236
237 PRINT_CONFIG(aec_enabled);
238 PRINT_CONFIG(aec_delay_agnostic_enabled);
239 PRINT_CONFIG(aec_drift_compensation_enabled);
240 PRINT_CONFIG(aec_extended_filter_enabled);
241 PRINT_CONFIG(aec_suppression_level);
242 PRINT_CONFIG(aecm_enabled);
243 PRINT_CONFIG(aecm_comfort_noise_enabled);
244 PRINT_CONFIG(aecm_routing_mode);
245 PRINT_CONFIG(agc_enabled);
246 PRINT_CONFIG(agc_mode);
247 PRINT_CONFIG(agc_limiter_enabled);
248 PRINT_CONFIG(noise_robust_agc_enabled);
249 PRINT_CONFIG(hpf_enabled);
250 PRINT_CONFIG(ns_enabled);
251 PRINT_CONFIG(ns_level);
252 PRINT_CONFIG(transient_suppression_enabled);
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000253 } else if (event_msg.type() == Event::INIT) {
254 if (!event_msg.has_init()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000255 printf("Corrupt input file: Init missing.\n");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000256 return 1;
257 }
258
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000259 const Init msg = event_msg.init();
260 // These should print out zeros if they're missing.
261 fprintf(settings_file, "Init at frame: %d\n", frame_count);
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000262 int input_sample_rate = msg.sample_rate();
263 fprintf(settings_file, " Input sample rate: %d\n", input_sample_rate);
264 int output_sample_rate = msg.output_sample_rate();
265 fprintf(settings_file, " Output sample rate: %d\n", output_sample_rate);
266 int reverse_sample_rate = msg.reverse_sample_rate();
267 fprintf(settings_file,
268 " Reverse sample rate: %d\n",
269 reverse_sample_rate);
270 num_input_channels = msg.num_input_channels();
271 fprintf(settings_file, " Input channels: %d\n", num_input_channels);
272 num_output_channels = msg.num_output_channels();
273 fprintf(settings_file, " Output channels: %d\n", num_output_channels);
274 num_reverse_channels = msg.num_reverse_channels();
275 fprintf(settings_file, " Reverse channels: %d\n", num_reverse_channels);
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000276
277 fprintf(settings_file, "\n");
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000278
279 if (reverse_sample_rate == 0) {
280 reverse_sample_rate = input_sample_rate;
281 }
282 if (output_sample_rate == 0) {
283 output_sample_rate = input_sample_rate;
284 }
285
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000286 reverse_samples_per_channel = reverse_sample_rate / 100;
287 input_samples_per_channel = input_sample_rate / 100;
288 output_samples_per_channel = output_sample_rate / 100;
289
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000290 if (!FLAGS_raw) {
291 // The WAV files need to be reset every time, because they cant change
292 // their sample rate or number of channels.
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000293 reverse_wav_file.reset(new WavWriter(FLAGS_reverse_file + ".wav",
294 reverse_sample_rate,
295 num_reverse_channels));
296 input_wav_file.reset(new WavWriter(FLAGS_input_file + ".wav",
297 input_sample_rate,
298 num_input_channels));
299 output_wav_file.reset(new WavWriter(FLAGS_output_file + ".wav",
300 output_sample_rate,
301 num_output_channels));
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000302 }
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000303 }
304 }
305
306 return 0;
307}
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000308
309} // namespace webrtc
310
311int main(int argc, char* argv[]) {
312 return webrtc::do_main(argc, argv);
313}