blob: 49c8b3c979ed16cb7d4effdd04bc626403f38309 [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@webrtc.orga8b97372014-03-10 22:26:12 +000021#include "webrtc/modules/audio_processing/test/test_utils.h"
pbos@webrtc.org7fad4b82013-05-28 08:11:59 +000022#include "webrtc/typedefs.h"
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000023
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000024// TODO(andrew): unpack more of the data.
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000025DEFINE_string(input_file, "input", "The name of the input stream file.");
26DEFINE_string(output_file, "ref_out",
andrew@webrtc.orgcd824382011-11-11 19:13:36 +000027 "The name of the reference output stream file.");
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000028DEFINE_string(reverse_file, "reverse",
andrew@webrtc.orgcd824382011-11-11 19:13:36 +000029 "The name of the reverse input stream file.");
30DEFINE_string(delay_file, "delay.int32", "The name of the delay file.");
31DEFINE_string(drift_file, "drift.int32", "The name of the drift file.");
32DEFINE_string(level_file, "level.int32", "The name of the level file.");
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +000033DEFINE_string(keypress_file, "keypress.bool", "The name of the keypress file.");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +000034DEFINE_string(settings_file, "settings.txt", "The name of the settings file.");
35DEFINE_bool(full, false,
36 "Unpack the full set of files (normally not needed).");
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000037DEFINE_bool(raw, false, "Write raw data instead of a WAV file.");
Bjorn Volcker40a6d592015-05-06 10:51:34 +020038DEFINE_bool(text,
39 false,
40 "Write non-audio files as text files instead of binary files.");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000041
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000042namespace webrtc {
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000043
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000044using audioproc::Event;
45using audioproc::ReverseStream;
46using audioproc::Stream;
47using audioproc::Init;
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000048
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000049void WriteData(const void* data, size_t size, FILE* file,
50 const std::string& filename) {
51 if (fwrite(data, size, 1, file) != 1) {
52 printf("Error when writing to %s\n", filename.c_str());
53 exit(1);
54 }
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000055}
56
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000057int do_main(int argc, char* argv[]) {
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000058 std::string program_name = argv[0];
59 std::string usage = "Commandline tool to unpack audioproc debug files.\n"
60 "Example usage:\n" + program_name + " debug_dump.pb\n";
61 google::SetUsageMessage(usage);
62 google::ParseCommandLineFlags(&argc, &argv, true);
63
64 if (argc < 2) {
65 printf("%s", google::ProgramUsage());
66 return 1;
67 }
68
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000069 FILE* debug_file = OpenFile(argv[1], "rb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +000070
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000071 Event event_msg;
andrew@webrtc.orgcd824382011-11-11 19:13:36 +000072 int frame_count = 0;
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +000073 int reverse_samples_per_channel = 0;
74 int input_samples_per_channel = 0;
75 int output_samples_per_channel = 0;
76 int num_reverse_channels = 0;
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +000077 int num_input_channels = 0;
78 int num_output_channels = 0;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000079 rtc::scoped_ptr<WavWriter> reverse_wav_file;
80 rtc::scoped_ptr<WavWriter> input_wav_file;
81 rtc::scoped_ptr<WavWriter> output_wav_file;
82 rtc::scoped_ptr<RawFile> reverse_raw_file;
83 rtc::scoped_ptr<RawFile> input_raw_file;
84 rtc::scoped_ptr<RawFile> output_raw_file;
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +000085 while (ReadMessageFromFile(debug_file, &event_msg)) {
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000086 if (event_msg.type() == Event::REVERSE_STREAM) {
87 if (!event_msg.has_reverse_stream()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000088 printf("Corrupt input file: ReverseStream missing.\n");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000089 return 1;
90 }
91
92 const ReverseStream msg = event_msg.reverse_stream();
andrew@webrtc.orgcd824382011-11-11 19:13:36 +000093 if (msg.has_data()) {
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000094 if (FLAGS_raw && !reverse_raw_file) {
95 reverse_raw_file.reset(new RawFile(FLAGS_reverse_file + ".pcm"));
96 }
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +000097 // TODO(aluebs): Replace "num_reverse_channels *
98 // reverse_samples_per_channel" with "msg.data().size() /
99 // sizeof(int16_t)" and so on when this fix in audio_processing has made
100 // it into stable: https://webrtc-codereview.appspot.com/15299004/
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000101 WriteIntData(reinterpret_cast<const int16_t*>(msg.data().data()),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000102 num_reverse_channels * reverse_samples_per_channel,
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000103 reverse_wav_file.get(),
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000104 reverse_raw_file.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000105 } else if (msg.channel_size() > 0) {
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000106 if (FLAGS_raw && !reverse_raw_file) {
107 reverse_raw_file.reset(new RawFile(FLAGS_reverse_file + ".float"));
108 }
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000109 rtc::scoped_ptr<const float* []> data(
110 new const float* [num_reverse_channels]);
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000111 for (int i = 0; i < num_reverse_channels; ++i) {
112 data[i] = reinterpret_cast<const float*>(msg.channel(i).data());
113 }
114 WriteFloatData(data.get(),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000115 reverse_samples_per_channel,
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000116 num_reverse_channels,
117 reverse_wav_file.get(),
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000118 reverse_raw_file.get());
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000119 }
120 } else if (event_msg.type() == Event::STREAM) {
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000121 frame_count++;
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000122 if (!event_msg.has_stream()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000123 printf("Corrupt input file: Stream missing.\n");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000124 return 1;
125 }
126
127 const Stream msg = event_msg.stream();
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000128 if (msg.has_input_data()) {
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000129 if (FLAGS_raw && !input_raw_file) {
130 input_raw_file.reset(new RawFile(FLAGS_input_file + ".pcm"));
131 }
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000132 WriteIntData(reinterpret_cast<const int16_t*>(msg.input_data().data()),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000133 num_input_channels * input_samples_per_channel,
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000134 input_wav_file.get(),
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000135 input_raw_file.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000136 } else if (msg.input_channel_size() > 0) {
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000137 if (FLAGS_raw && !input_raw_file) {
138 input_raw_file.reset(new RawFile(FLAGS_input_file + ".float"));
139 }
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000140 rtc::scoped_ptr<const float* []> data(
141 new const float* [num_input_channels]);
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000142 for (int i = 0; i < num_input_channels; ++i) {
143 data[i] = reinterpret_cast<const float*>(msg.input_channel(i).data());
144 }
145 WriteFloatData(data.get(),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000146 input_samples_per_channel,
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000147 num_input_channels,
148 input_wav_file.get(),
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000149 input_raw_file.get());
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000150 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000151
152 if (msg.has_output_data()) {
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000153 if (FLAGS_raw && !output_raw_file) {
154 output_raw_file.reset(new RawFile(FLAGS_output_file + ".pcm"));
155 }
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000156 WriteIntData(reinterpret_cast<const int16_t*>(msg.output_data().data()),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000157 num_output_channels * output_samples_per_channel,
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000158 output_wav_file.get(),
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000159 output_raw_file.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000160 } else if (msg.output_channel_size() > 0) {
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000161 if (FLAGS_raw && !output_raw_file) {
162 output_raw_file.reset(new RawFile(FLAGS_output_file + ".float"));
163 }
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000164 rtc::scoped_ptr<const float* []> data(
165 new const float* [num_output_channels]);
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000166 for (int i = 0; i < num_output_channels; ++i) {
167 data[i] =
168 reinterpret_cast<const float*>(msg.output_channel(i).data());
169 }
170 WriteFloatData(data.get(),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000171 output_samples_per_channel,
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000172 num_output_channels,
173 output_wav_file.get(),
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000174 output_raw_file.get());
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000175 }
176
177 if (FLAGS_full) {
178 if (msg.has_delay()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000179 static FILE* delay_file = OpenFile(FLAGS_delay_file, "wb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000180 int32_t delay = msg.delay();
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200181 if (FLAGS_text) {
182 fprintf(delay_file, "%d\n", delay);
183 } else {
184 WriteData(&delay, sizeof(delay), delay_file, FLAGS_delay_file);
185 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000186 }
187
188 if (msg.has_drift()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000189 static FILE* drift_file = OpenFile(FLAGS_drift_file, "wb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000190 int32_t drift = msg.drift();
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200191 if (FLAGS_text) {
192 fprintf(drift_file, "%d\n", drift);
193 } else {
194 WriteData(&drift, sizeof(drift), drift_file, FLAGS_drift_file);
195 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000196 }
197
198 if (msg.has_level()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000199 static FILE* level_file = OpenFile(FLAGS_level_file, "wb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000200 int32_t level = msg.level();
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200201 if (FLAGS_text) {
202 fprintf(level_file, "%d\n", level);
203 } else {
204 WriteData(&level, sizeof(level), level_file, FLAGS_level_file);
205 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000206 }
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000207
208 if (msg.has_keypress()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000209 static FILE* keypress_file = OpenFile(FLAGS_keypress_file, "wb");
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000210 bool keypress = msg.keypress();
Bjorn Volcker40a6d592015-05-06 10:51:34 +0200211 if (FLAGS_text) {
212 fprintf(keypress_file, "%d\n", keypress);
213 } else {
214 WriteData(&keypress, sizeof(keypress), keypress_file,
215 FLAGS_keypress_file);
216 }
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000217 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000218 }
219 } else if (event_msg.type() == Event::INIT) {
220 if (!event_msg.has_init()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000221 printf("Corrupt input file: Init missing.\n");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000222 return 1;
223 }
224
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000225 static FILE* settings_file = OpenFile(FLAGS_settings_file, "wb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000226 const Init msg = event_msg.init();
227 // These should print out zeros if they're missing.
228 fprintf(settings_file, "Init at frame: %d\n", frame_count);
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000229 int input_sample_rate = msg.sample_rate();
230 fprintf(settings_file, " Input sample rate: %d\n", input_sample_rate);
231 int output_sample_rate = msg.output_sample_rate();
232 fprintf(settings_file, " Output sample rate: %d\n", output_sample_rate);
233 int reverse_sample_rate = msg.reverse_sample_rate();
234 fprintf(settings_file,
235 " Reverse sample rate: %d\n",
236 reverse_sample_rate);
237 num_input_channels = msg.num_input_channels();
238 fprintf(settings_file, " Input channels: %d\n", num_input_channels);
239 num_output_channels = msg.num_output_channels();
240 fprintf(settings_file, " Output channels: %d\n", num_output_channels);
241 num_reverse_channels = msg.num_reverse_channels();
242 fprintf(settings_file, " Reverse channels: %d\n", num_reverse_channels);
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000243
244 fprintf(settings_file, "\n");
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000245
246 if (reverse_sample_rate == 0) {
247 reverse_sample_rate = input_sample_rate;
248 }
249 if (output_sample_rate == 0) {
250 output_sample_rate = input_sample_rate;
251 }
252
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000253 reverse_samples_per_channel = reverse_sample_rate / 100;
254 input_samples_per_channel = input_sample_rate / 100;
255 output_samples_per_channel = output_sample_rate / 100;
256
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000257 if (!FLAGS_raw) {
258 // The WAV files need to be reset every time, because they cant change
259 // their sample rate or number of channels.
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000260 reverse_wav_file.reset(new WavWriter(FLAGS_reverse_file + ".wav",
261 reverse_sample_rate,
262 num_reverse_channels));
263 input_wav_file.reset(new WavWriter(FLAGS_input_file + ".wav",
264 input_sample_rate,
265 num_input_channels));
266 output_wav_file.reset(new WavWriter(FLAGS_output_file + ".wav",
267 output_sample_rate,
268 num_output_channels));
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000269 }
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000270 }
271 }
272
273 return 0;
274}
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000275
276} // namespace webrtc
277
278int main(int argc, char* argv[]) {
279 return webrtc::do_main(argc, argv);
280}