andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 1 | /* |
| 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.org | 841f58f | 2014-09-02 07:51:51 +0000 | [diff] [blame] | 17 | #include <limits> |
andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 18 | |
pbos@webrtc.org | 7fad4b8 | 2013-05-28 08:11:59 +0000 | [diff] [blame] | 19 | #include "gflags/gflags.h" |
andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 20 | #include "webrtc/audio_processing/debug.pb.h" |
aluebs@webrtc.org | 841f58f | 2014-09-02 07:51:51 +0000 | [diff] [blame] | 21 | #include "webrtc/common_audio/include/audio_util.h" |
| 22 | #include "webrtc/common_audio/wav_writer.h" |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 23 | #include "webrtc/modules/audio_processing/test/test_utils.h" |
pbos@webrtc.org | 7fad4b8 | 2013-05-28 08:11:59 +0000 | [diff] [blame] | 24 | #include "webrtc/system_wrappers/interface/scoped_ptr.h" |
| 25 | #include "webrtc/typedefs.h" |
andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 26 | |
andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 27 | // TODO(andrew): unpack more of the data. |
| 28 | DEFINE_string(input_file, "input.pcm", "The name of the input stream file."); |
aluebs@webrtc.org | 841f58f | 2014-09-02 07:51:51 +0000 | [diff] [blame] | 29 | DEFINE_string(input_wav_file, "input.wav", |
| 30 | "The name of the WAV input stream file."); |
andrew@webrtc.org | cd82438 | 2011-11-11 19:13:36 +0000 | [diff] [blame] | 31 | DEFINE_string(output_file, "ref_out.pcm", |
| 32 | "The name of the reference output stream file."); |
aluebs@webrtc.org | 841f58f | 2014-09-02 07:51:51 +0000 | [diff] [blame] | 33 | DEFINE_string(output_wav_file, "ref_out.wav", |
| 34 | "The name of the WAV reference output stream file."); |
andrew@webrtc.org | cd82438 | 2011-11-11 19:13:36 +0000 | [diff] [blame] | 35 | DEFINE_string(reverse_file, "reverse.pcm", |
| 36 | "The name of the reverse input stream file."); |
aluebs@webrtc.org | 841f58f | 2014-09-02 07:51:51 +0000 | [diff] [blame] | 37 | DEFINE_string(reverse_wav_file, "reverse.wav", |
| 38 | "The name of the WAV reverse input stream file."); |
andrew@webrtc.org | cd82438 | 2011-11-11 19:13:36 +0000 | [diff] [blame] | 39 | DEFINE_string(delay_file, "delay.int32", "The name of the delay file."); |
| 40 | DEFINE_string(drift_file, "drift.int32", "The name of the drift file."); |
| 41 | DEFINE_string(level_file, "level.int32", "The name of the level file."); |
andrew@webrtc.org | ce8e077 | 2014-02-12 15:28:30 +0000 | [diff] [blame] | 42 | DEFINE_string(keypress_file, "keypress.bool", "The name of the keypress file."); |
andrew@webrtc.org | cd82438 | 2011-11-11 19:13:36 +0000 | [diff] [blame] | 43 | DEFINE_string(settings_file, "settings.txt", "The name of the settings file."); |
| 44 | DEFINE_bool(full, false, |
| 45 | "Unpack the full set of files (normally not needed)."); |
aluebs@webrtc.org | 841f58f | 2014-09-02 07:51:51 +0000 | [diff] [blame] | 46 | DEFINE_bool(pcm, false, "Write to PCM instead of WAV file."); |
andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 47 | |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 48 | namespace webrtc { |
andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 49 | |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 50 | using audioproc::Event; |
| 51 | using audioproc::ReverseStream; |
| 52 | using audioproc::Stream; |
| 53 | using audioproc::Init; |
andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 54 | |
aluebs@webrtc.org | 841f58f | 2014-09-02 07:51:51 +0000 | [diff] [blame] | 55 | class 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.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 85 | void 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.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 91 | } |
| 92 | |
aluebs@webrtc.org | 841f58f | 2014-09-02 07:51:51 +0000 | [diff] [blame] | 93 | void 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 | |
| 105 | void 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.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 127 | int do_main(int argc, char* argv[]) { |
andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 128 | 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.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 139 | FILE* debug_file = OpenFile(argv[1], "rb"); |
andrew@webrtc.org | cd82438 | 2011-11-11 19:13:36 +0000 | [diff] [blame] | 140 | |
andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 141 | Event event_msg; |
andrew@webrtc.org | cd82438 | 2011-11-11 19:13:36 +0000 | [diff] [blame] | 142 | int frame_count = 0; |
aluebs@webrtc.org | 841f58f | 2014-09-02 07:51:51 +0000 | [diff] [blame] | 143 | int num_input_channels = 0; |
| 144 | int num_output_channels = 0; |
| 145 | int num_reverse_channels = 0; |
| 146 | scoped_ptr<WavFile> reverse_wav_file; |
| 147 | scoped_ptr<WavFile> input_wav_file; |
| 148 | scoped_ptr<WavFile> output_wav_file; |
| 149 | scoped_ptr<PcmFile> reverse_pcm_file; |
| 150 | scoped_ptr<PcmFile> input_pcm_file; |
| 151 | scoped_ptr<PcmFile> output_pcm_file; |
| 152 | while (ReadMessageFromFile(debug_file, &event_msg)) { |
andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 153 | if (event_msg.type() == Event::REVERSE_STREAM) { |
| 154 | if (!event_msg.has_reverse_stream()) { |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 155 | printf("Corrupt input file: ReverseStream missing.\n"); |
andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 156 | return 1; |
| 157 | } |
| 158 | |
| 159 | const ReverseStream msg = event_msg.reverse_stream(); |
andrew@webrtc.org | cd82438 | 2011-11-11 19:13:36 +0000 | [diff] [blame] | 160 | if (msg.has_data()) { |
aluebs@webrtc.org | 841f58f | 2014-09-02 07:51:51 +0000 | [diff] [blame] | 161 | WriteIntData(reinterpret_cast<const int16_t*>(msg.data().data()), |
| 162 | msg.data().size() / sizeof(int16_t), |
| 163 | reverse_wav_file.get(), |
| 164 | reverse_pcm_file.get()); |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 165 | } else if (msg.channel_size() > 0) { |
aluebs@webrtc.org | 841f58f | 2014-09-02 07:51:51 +0000 | [diff] [blame] | 166 | scoped_ptr<const float*[]> data(new const float*[num_reverse_channels]); |
| 167 | for (int i = 0; i < num_reverse_channels; ++i) { |
| 168 | data[i] = reinterpret_cast<const float*>(msg.channel(i).data()); |
| 169 | } |
| 170 | WriteFloatData(data.get(), |
| 171 | msg.channel(0).size() / sizeof(float), |
| 172 | num_reverse_channels, |
| 173 | reverse_wav_file.get(), |
| 174 | reverse_pcm_file.get()); |
andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 175 | } |
| 176 | } else if (event_msg.type() == Event::STREAM) { |
andrew@webrtc.org | cd82438 | 2011-11-11 19:13:36 +0000 | [diff] [blame] | 177 | frame_count++; |
andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 178 | if (!event_msg.has_stream()) { |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 179 | printf("Corrupt input file: Stream missing.\n"); |
andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 180 | return 1; |
| 181 | } |
| 182 | |
| 183 | const Stream msg = event_msg.stream(); |
andrew@webrtc.org | cd82438 | 2011-11-11 19:13:36 +0000 | [diff] [blame] | 184 | if (msg.has_input_data()) { |
aluebs@webrtc.org | 841f58f | 2014-09-02 07:51:51 +0000 | [diff] [blame] | 185 | WriteIntData(reinterpret_cast<const int16_t*>(msg.input_data().data()), |
| 186 | msg.input_data().size() / sizeof(int16_t), |
| 187 | input_wav_file.get(), |
| 188 | input_pcm_file.get()); |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 189 | } else if (msg.input_channel_size() > 0) { |
aluebs@webrtc.org | 841f58f | 2014-09-02 07:51:51 +0000 | [diff] [blame] | 190 | scoped_ptr<const float*[]> data(new const float*[num_input_channels]); |
| 191 | for (int i = 0; i < num_input_channels; ++i) { |
| 192 | data[i] = reinterpret_cast<const float*>(msg.input_channel(i).data()); |
| 193 | } |
| 194 | WriteFloatData(data.get(), |
| 195 | msg.input_channel(0).size() / sizeof(float), |
| 196 | num_input_channels, |
| 197 | input_wav_file.get(), |
| 198 | input_pcm_file.get()); |
andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 199 | } |
andrew@webrtc.org | cd82438 | 2011-11-11 19:13:36 +0000 | [diff] [blame] | 200 | |
| 201 | if (msg.has_output_data()) { |
aluebs@webrtc.org | 841f58f | 2014-09-02 07:51:51 +0000 | [diff] [blame] | 202 | WriteIntData(reinterpret_cast<const int16_t*>(msg.output_data().data()), |
| 203 | msg.output_data().size() / sizeof(int16_t), |
| 204 | output_wav_file.get(), |
| 205 | output_pcm_file.get()); |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 206 | } else if (msg.output_channel_size() > 0) { |
aluebs@webrtc.org | 841f58f | 2014-09-02 07:51:51 +0000 | [diff] [blame] | 207 | scoped_ptr<const float*[]> data(new const float*[num_output_channels]); |
| 208 | for (int i = 0; i < num_output_channels; ++i) { |
| 209 | data[i] = |
| 210 | reinterpret_cast<const float*>(msg.output_channel(i).data()); |
| 211 | } |
| 212 | WriteFloatData(data.get(), |
| 213 | msg.output_channel(0).size() / sizeof(float), |
| 214 | num_output_channels, |
| 215 | output_wav_file.get(), |
| 216 | output_pcm_file.get()); |
andrew@webrtc.org | cd82438 | 2011-11-11 19:13:36 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | if (FLAGS_full) { |
| 220 | if (msg.has_delay()) { |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 221 | static FILE* delay_file = OpenFile(FLAGS_delay_file, "wb"); |
andrew@webrtc.org | cd82438 | 2011-11-11 19:13:36 +0000 | [diff] [blame] | 222 | int32_t delay = msg.delay(); |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 223 | WriteData(&delay, sizeof(delay), delay_file, FLAGS_delay_file); |
andrew@webrtc.org | cd82438 | 2011-11-11 19:13:36 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | if (msg.has_drift()) { |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 227 | static FILE* drift_file = OpenFile(FLAGS_drift_file, "wb"); |
andrew@webrtc.org | cd82438 | 2011-11-11 19:13:36 +0000 | [diff] [blame] | 228 | int32_t drift = msg.drift(); |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 229 | WriteData(&drift, sizeof(drift), drift_file, FLAGS_drift_file); |
andrew@webrtc.org | cd82438 | 2011-11-11 19:13:36 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | if (msg.has_level()) { |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 233 | static FILE* level_file = OpenFile(FLAGS_level_file, "wb"); |
andrew@webrtc.org | cd82438 | 2011-11-11 19:13:36 +0000 | [diff] [blame] | 234 | int32_t level = msg.level(); |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 235 | WriteData(&level, sizeof(level), level_file, FLAGS_level_file); |
andrew@webrtc.org | cd82438 | 2011-11-11 19:13:36 +0000 | [diff] [blame] | 236 | } |
andrew@webrtc.org | ce8e077 | 2014-02-12 15:28:30 +0000 | [diff] [blame] | 237 | |
| 238 | if (msg.has_keypress()) { |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 239 | static FILE* keypress_file = OpenFile(FLAGS_keypress_file, "wb"); |
andrew@webrtc.org | ce8e077 | 2014-02-12 15:28:30 +0000 | [diff] [blame] | 240 | bool keypress = msg.keypress(); |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 241 | WriteData(&keypress, sizeof(keypress), keypress_file, |
| 242 | FLAGS_keypress_file); |
andrew@webrtc.org | ce8e077 | 2014-02-12 15:28:30 +0000 | [diff] [blame] | 243 | } |
andrew@webrtc.org | cd82438 | 2011-11-11 19:13:36 +0000 | [diff] [blame] | 244 | } |
| 245 | } else if (event_msg.type() == Event::INIT) { |
| 246 | if (!event_msg.has_init()) { |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 247 | printf("Corrupt input file: Init missing.\n"); |
andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 248 | return 1; |
| 249 | } |
| 250 | |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 251 | static FILE* settings_file = OpenFile(FLAGS_settings_file, "wb"); |
andrew@webrtc.org | cd82438 | 2011-11-11 19:13:36 +0000 | [diff] [blame] | 252 | const Init msg = event_msg.init(); |
| 253 | // These should print out zeros if they're missing. |
| 254 | fprintf(settings_file, "Init at frame: %d\n", frame_count); |
aluebs@webrtc.org | 841f58f | 2014-09-02 07:51:51 +0000 | [diff] [blame] | 255 | int input_sample_rate = msg.sample_rate(); |
| 256 | fprintf(settings_file, " Input sample rate: %d\n", input_sample_rate); |
| 257 | int output_sample_rate = msg.output_sample_rate(); |
| 258 | fprintf(settings_file, " Output sample rate: %d\n", output_sample_rate); |
| 259 | int reverse_sample_rate = msg.reverse_sample_rate(); |
| 260 | fprintf(settings_file, |
| 261 | " Reverse sample rate: %d\n", |
| 262 | reverse_sample_rate); |
| 263 | num_input_channels = msg.num_input_channels(); |
| 264 | fprintf(settings_file, " Input channels: %d\n", num_input_channels); |
| 265 | num_output_channels = msg.num_output_channels(); |
| 266 | fprintf(settings_file, " Output channels: %d\n", num_output_channels); |
| 267 | num_reverse_channels = msg.num_reverse_channels(); |
| 268 | fprintf(settings_file, " Reverse channels: %d\n", num_reverse_channels); |
andrew@webrtc.org | cd82438 | 2011-11-11 19:13:36 +0000 | [diff] [blame] | 269 | |
| 270 | fprintf(settings_file, "\n"); |
aluebs@webrtc.org | 841f58f | 2014-09-02 07:51:51 +0000 | [diff] [blame] | 271 | |
| 272 | if (reverse_sample_rate == 0) { |
| 273 | reverse_sample_rate = input_sample_rate; |
| 274 | } |
| 275 | if (output_sample_rate == 0) { |
| 276 | output_sample_rate = input_sample_rate; |
| 277 | } |
| 278 | |
| 279 | if (FLAGS_pcm) { |
| 280 | if (!reverse_pcm_file.get()) { |
| 281 | reverse_pcm_file.reset(new PcmFile(FLAGS_reverse_file)); |
| 282 | } |
| 283 | if (!input_pcm_file.get()) { |
| 284 | input_pcm_file.reset(new PcmFile(FLAGS_input_file)); |
| 285 | } |
| 286 | if (!output_pcm_file.get()) { |
| 287 | output_pcm_file.reset(new PcmFile(FLAGS_output_file)); |
| 288 | } |
| 289 | } else { |
| 290 | reverse_wav_file.reset(new WavFile(FLAGS_reverse_wav_file, |
| 291 | reverse_sample_rate, |
| 292 | num_reverse_channels)); |
| 293 | input_wav_file.reset(new WavFile(FLAGS_input_wav_file, |
| 294 | input_sample_rate, |
| 295 | num_input_channels)); |
| 296 | output_wav_file.reset(new WavFile(FLAGS_output_wav_file, |
| 297 | output_sample_rate, |
| 298 | num_output_channels)); |
| 299 | } |
andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | |
| 303 | return 0; |
| 304 | } |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 305 | |
| 306 | } // namespace webrtc |
| 307 | |
| 308 | int main(int argc, char* argv[]) { |
| 309 | return webrtc::do_main(argc, argv); |
| 310 | } |