blob: bb76a2dc60785cff23a79738fc7e09aebb3cc153 [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>
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +000017#include <limits>
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000018
pbos@webrtc.org7fad4b82013-05-28 08:11:59 +000019#include "gflags/gflags.h"
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000020#include "webrtc/audio_processing/debug.pb.h"
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +000021#include "webrtc/common_audio/include/audio_util.h"
22#include "webrtc/common_audio/wav_writer.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/system_wrappers/interface/scoped_ptr.h"
25#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.
28DEFINE_string(input_file, "input.pcm", "The name of the input stream file.");
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +000029DEFINE_string(input_wav_file, "input.wav",
30 "The name of the WAV input stream file.");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +000031DEFINE_string(output_file, "ref_out.pcm",
32 "The name of the reference output stream file.");
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +000033DEFINE_string(output_wav_file, "ref_out.wav",
34 "The name of the WAV reference output stream file.");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +000035DEFINE_string(reverse_file, "reverse.pcm",
36 "The name of the reverse input stream file.");
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +000037DEFINE_string(reverse_wav_file, "reverse.wav",
38 "The name of the WAV reverse input stream file.");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +000039DEFINE_string(delay_file, "delay.int32", "The name of the delay file.");
40DEFINE_string(drift_file, "drift.int32", "The name of the drift file.");
41DEFINE_string(level_file, "level.int32", "The name of the level file.");
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +000042DEFINE_string(keypress_file, "keypress.bool", "The name of the keypress file.");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +000043DEFINE_string(settings_file, "settings.txt", "The name of the settings file.");
44DEFINE_bool(full, false,
45 "Unpack the full set of files (normally not needed).");
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +000046DEFINE_bool(pcm, false, "Write to PCM instead of WAV file.");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000047
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
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +000055class PcmFile {
56 public:
57 PcmFile(const std::string& filename)
58 : file_handle_(fopen(filename.c_str(), "wb")) {}
59
60 ~PcmFile() {
61 fclose(file_handle_);
62 }
63
64 void WriteSamples(const int16_t* samples, size_t num_samples) {
65#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
66#error "Need to convert samples to little-endian when writing to PCM file"
67#endif
68 fwrite(samples, sizeof(*samples), num_samples, file_handle_);
69 }
70
71 void WriteSamples(const float* samples, size_t num_samples) {
72 static const size_t kChunksize = 4096 / sizeof(uint16_t);
73 for (size_t i = 0; i < num_samples; i += kChunksize) {
74 int16_t isamples[kChunksize];
75 const size_t chunk = std::min(kChunksize, num_samples - i);
76 RoundToInt16(samples + i, chunk, isamples);
77 WriteSamples(isamples, chunk);
78 }
79 }
80
81 private:
82 FILE* file_handle_;
83};
84
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000085void WriteData(const void* data, size_t size, FILE* file,
86 const std::string& filename) {
87 if (fwrite(data, size, 1, file) != 1) {
88 printf("Error when writing to %s\n", filename.c_str());
89 exit(1);
90 }
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000091}
92
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +000093void WriteIntData(const int16_t* data,
94 size_t length,
95 WavFile* wav_file,
96 PcmFile* pcm_file) {
97 if (wav_file) {
98 wav_file->WriteSamples(data, length);
99 }
100 if (pcm_file) {
101 pcm_file->WriteSamples(data, length);
102 }
103}
104
105void WriteFloatData(const float* const* data,
106 size_t samples_per_channel,
107 int num_channels,
108 WavFile* wav_file,
109 PcmFile* pcm_file) {
110 size_t length = num_channels * samples_per_channel;
111 scoped_ptr<float[]> buffer(new float[length]);
112 Interleave(data, samples_per_channel, num_channels, buffer.get());
113 // TODO(aluebs): Use ScaleToInt16Range() from audio_util
114 for (size_t i = 0; i < length; ++i) {
115 buffer[i] = buffer[i] > 0 ?
116 buffer[i] * std::numeric_limits<int16_t>::max() :
117 -buffer[i] * std::numeric_limits<int16_t>::min();
118 }
119 if (wav_file) {
120 wav_file->WriteSamples(buffer.get(), length);
121 }
122 if (pcm_file) {
123 pcm_file->WriteSamples(buffer.get(), length);
124 }
125}
126
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000127int do_main(int argc, char* argv[]) {
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000128 std::string program_name = argv[0];
129 std::string usage = "Commandline tool to unpack audioproc debug files.\n"
130 "Example usage:\n" + program_name + " debug_dump.pb\n";
131 google::SetUsageMessage(usage);
132 google::ParseCommandLineFlags(&argc, &argv, true);
133
134 if (argc < 2) {
135 printf("%s", google::ProgramUsage());
136 return 1;
137 }
138
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000139 FILE* debug_file = OpenFile(argv[1], "rb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000140
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000141 Event event_msg;
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000142 int frame_count = 0;
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000143 int reverse_samples_per_channel = 0;
144 int input_samples_per_channel = 0;
145 int output_samples_per_channel = 0;
146 int num_reverse_channels = 0;
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000147 int num_input_channels = 0;
148 int num_output_channels = 0;
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000149 scoped_ptr<WavFile> reverse_wav_file;
150 scoped_ptr<WavFile> input_wav_file;
151 scoped_ptr<WavFile> output_wav_file;
152 scoped_ptr<PcmFile> reverse_pcm_file;
153 scoped_ptr<PcmFile> input_pcm_file;
154 scoped_ptr<PcmFile> output_pcm_file;
155 while (ReadMessageFromFile(debug_file, &event_msg)) {
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000156 if (event_msg.type() == Event::REVERSE_STREAM) {
157 if (!event_msg.has_reverse_stream()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000158 printf("Corrupt input file: ReverseStream missing.\n");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000159 return 1;
160 }
161
162 const ReverseStream msg = event_msg.reverse_stream();
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000163 if (msg.has_data()) {
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000164 // TODO(aluebs): Replace "num_reverse_channels *
165 // reverse_samples_per_channel" with "msg.data().size() /
166 // sizeof(int16_t)" and so on when this fix in audio_processing has made
167 // it into stable: https://webrtc-codereview.appspot.com/15299004/
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000168 WriteIntData(reinterpret_cast<const int16_t*>(msg.data().data()),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000169 num_reverse_channels * reverse_samples_per_channel,
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000170 reverse_wav_file.get(),
171 reverse_pcm_file.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000172 } else if (msg.channel_size() > 0) {
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000173 scoped_ptr<const float*[]> data(new const float*[num_reverse_channels]);
174 for (int i = 0; i < num_reverse_channels; ++i) {
175 data[i] = reinterpret_cast<const float*>(msg.channel(i).data());
176 }
177 WriteFloatData(data.get(),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000178 reverse_samples_per_channel,
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000179 num_reverse_channels,
180 reverse_wav_file.get(),
181 reverse_pcm_file.get());
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000182 }
183 } else if (event_msg.type() == Event::STREAM) {
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000184 frame_count++;
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000185 if (!event_msg.has_stream()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000186 printf("Corrupt input file: Stream missing.\n");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000187 return 1;
188 }
189
190 const Stream msg = event_msg.stream();
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000191 if (msg.has_input_data()) {
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000192 WriteIntData(reinterpret_cast<const int16_t*>(msg.input_data().data()),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000193 num_input_channels * input_samples_per_channel,
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000194 input_wav_file.get(),
195 input_pcm_file.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000196 } else if (msg.input_channel_size() > 0) {
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000197 scoped_ptr<const float*[]> data(new const float*[num_input_channels]);
198 for (int i = 0; i < num_input_channels; ++i) {
199 data[i] = reinterpret_cast<const float*>(msg.input_channel(i).data());
200 }
201 WriteFloatData(data.get(),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000202 input_samples_per_channel,
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000203 num_input_channels,
204 input_wav_file.get(),
205 input_pcm_file.get());
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000206 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000207
208 if (msg.has_output_data()) {
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000209 WriteIntData(reinterpret_cast<const int16_t*>(msg.output_data().data()),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000210 num_output_channels * output_samples_per_channel,
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000211 output_wav_file.get(),
212 output_pcm_file.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000213 } else if (msg.output_channel_size() > 0) {
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000214 scoped_ptr<const float*[]> data(new const float*[num_output_channels]);
215 for (int i = 0; i < num_output_channels; ++i) {
216 data[i] =
217 reinterpret_cast<const float*>(msg.output_channel(i).data());
218 }
219 WriteFloatData(data.get(),
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000220 output_samples_per_channel,
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000221 num_output_channels,
222 output_wav_file.get(),
223 output_pcm_file.get());
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000224 }
225
226 if (FLAGS_full) {
227 if (msg.has_delay()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000228 static FILE* delay_file = OpenFile(FLAGS_delay_file, "wb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000229 int32_t delay = msg.delay();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000230 WriteData(&delay, sizeof(delay), delay_file, FLAGS_delay_file);
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000231 }
232
233 if (msg.has_drift()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000234 static FILE* drift_file = OpenFile(FLAGS_drift_file, "wb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000235 int32_t drift = msg.drift();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000236 WriteData(&drift, sizeof(drift), drift_file, FLAGS_drift_file);
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000237 }
238
239 if (msg.has_level()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000240 static FILE* level_file = OpenFile(FLAGS_level_file, "wb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000241 int32_t level = msg.level();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000242 WriteData(&level, sizeof(level), level_file, FLAGS_level_file);
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000243 }
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000244
245 if (msg.has_keypress()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000246 static FILE* keypress_file = OpenFile(FLAGS_keypress_file, "wb");
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000247 bool keypress = msg.keypress();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000248 WriteData(&keypress, sizeof(keypress), keypress_file,
249 FLAGS_keypress_file);
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000250 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000251 }
252 } else if (event_msg.type() == Event::INIT) {
253 if (!event_msg.has_init()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000254 printf("Corrupt input file: Init missing.\n");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000255 return 1;
256 }
257
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000258 static FILE* settings_file = OpenFile(FLAGS_settings_file, "wb");
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.org841f58f2014-09-02 07:51:51 +0000290 if (FLAGS_pcm) {
291 if (!reverse_pcm_file.get()) {
292 reverse_pcm_file.reset(new PcmFile(FLAGS_reverse_file));
293 }
294 if (!input_pcm_file.get()) {
295 input_pcm_file.reset(new PcmFile(FLAGS_input_file));
296 }
297 if (!output_pcm_file.get()) {
298 output_pcm_file.reset(new PcmFile(FLAGS_output_file));
299 }
300 } else {
301 reverse_wav_file.reset(new WavFile(FLAGS_reverse_wav_file,
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000302 reverse_sample_rate,
303 num_reverse_channels));
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000304 input_wav_file.reset(new WavFile(FLAGS_input_wav_file,
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000305 input_sample_rate,
306 num_input_channels));
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000307 output_wav_file.reset(new WavFile(FLAGS_output_wav_file,
aluebs@webrtc.orgbac07262014-09-03 13:39:01 +0000308 output_sample_rate,
309 num_output_channels));
aluebs@webrtc.org841f58f2014-09-02 07:51:51 +0000310 }
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000311 }
312 }
313
314 return 0;
315}
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000316
317} // namespace webrtc
318
319int main(int argc, char* argv[]) {
320 return webrtc::do_main(argc, argv);
321}