peah | b46083e | 2016-05-03 07:01:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_AUDIO_PROCESSING_LOGGING_APM_DATA_DUMPER_H_ |
| 12 | #define MODULES_AUDIO_PROCESSING_LOGGING_APM_DATA_DUMPER_H_ |
peah | b46083e | 2016-05-03 07:01:18 -0700 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stdint.h> |
peah | b46083e | 2016-05-03 07:01:18 -0700 | [diff] [blame] | 15 | #include <stdio.h> |
Alessio Bazzica | 4bc6045 | 2018-11-20 12:44:15 +0100 | [diff] [blame^] | 16 | #include <string.h> |
peah | b46083e | 2016-05-03 07:01:18 -0700 | [diff] [blame] | 17 | |
Alessio Bazzica | 4bc6045 | 2018-11-20 12:44:15 +0100 | [diff] [blame^] | 18 | #include <string> |
Per Åhgren | f0c449e | 2018-10-23 20:17:18 +0200 | [diff] [blame] | 19 | #if WEBRTC_APM_DEBUG_DUMP == 1 |
| 20 | #include <unordered_map> |
| 21 | #endif |
| 22 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "api/array_view.h" |
Per Åhgren | f0c449e | 2018-10-23 20:17:18 +0200 | [diff] [blame] | 24 | #if WEBRTC_APM_DEBUG_DUMP == 1 |
| 25 | #include "common_audio/wav_file.h" |
Alessio Bazzica | 4bc6045 | 2018-11-20 12:44:15 +0100 | [diff] [blame^] | 26 | #include "rtc_base/checks.h" |
Per Åhgren | f0c449e | 2018-10-23 20:17:18 +0200 | [diff] [blame] | 27 | #endif |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 28 | #include "rtc_base/constructormagic.h" |
peah | b46083e | 2016-05-03 07:01:18 -0700 | [diff] [blame] | 29 | |
| 30 | // Check to verify that the define is properly set. |
peah | f28a389 | 2016-09-01 08:58:21 -0700 | [diff] [blame] | 31 | #if !defined(WEBRTC_APM_DEBUG_DUMP) || \ |
| 32 | (WEBRTC_APM_DEBUG_DUMP != 0 && WEBRTC_APM_DEBUG_DUMP != 1) |
| 33 | #error "Set WEBRTC_APM_DEBUG_DUMP to either 0 or 1" |
peah | b46083e | 2016-05-03 07:01:18 -0700 | [diff] [blame] | 34 | #endif |
| 35 | |
| 36 | namespace webrtc { |
| 37 | |
peah | f28a389 | 2016-09-01 08:58:21 -0700 | [diff] [blame] | 38 | #if WEBRTC_APM_DEBUG_DUMP == 1 |
peah | b46083e | 2016-05-03 07:01:18 -0700 | [diff] [blame] | 39 | // Functor used to use as a custom deleter in the map of file pointers to raw |
| 40 | // files. |
| 41 | struct RawFileCloseFunctor { |
| 42 | void operator()(FILE* f) const { fclose(f); } |
| 43 | }; |
| 44 | #endif |
| 45 | |
| 46 | // Class that handles dumping of variables into files. |
| 47 | class ApmDataDumper { |
| 48 | public: |
peah | f8f754a | 2016-08-30 10:31:45 -0700 | [diff] [blame] | 49 | // Constructor that takes an instance index that may |
| 50 | // be used to distinguish data dumped from different |
| 51 | // instances of the code. |
| 52 | explicit ApmDataDumper(int instance_index); |
| 53 | |
| 54 | ~ApmDataDumper(); |
peah | b46083e | 2016-05-03 07:01:18 -0700 | [diff] [blame] | 55 | |
Per Åhgren | 7a95e0f | 2018-10-25 09:56:49 +0200 | [diff] [blame] | 56 | // Activates or deactivate the dumping functionality. |
| 57 | static void SetActivated(bool activated) { |
| 58 | #if WEBRTC_APM_DEBUG_DUMP == 1 |
| 59 | recording_activated_ = activated; |
| 60 | #endif |
| 61 | } |
| 62 | |
Alessio Bazzica | 4bc6045 | 2018-11-20 12:44:15 +0100 | [diff] [blame^] | 63 | // Set an optional output directory. |
| 64 | static void SetOutputDirectory(const std::string& output_dir) { |
| 65 | #if WEBRTC_APM_DEBUG_DUMP == 1 |
| 66 | RTC_CHECK_LT(output_dir.size(), kOutputDirMaxLength); |
| 67 | strncpy(output_dir_, output_dir.c_str(), output_dir.size()); |
| 68 | #endif |
| 69 | } |
| 70 | |
peah | b46083e | 2016-05-03 07:01:18 -0700 | [diff] [blame] | 71 | // Reinitializes the data dumping such that new versions |
| 72 | // of all files being dumped to are created. |
| 73 | void InitiateNewSetOfRecordings() { |
peah | f28a389 | 2016-09-01 08:58:21 -0700 | [diff] [blame] | 74 | #if WEBRTC_APM_DEBUG_DUMP == 1 |
peah | b46083e | 2016-05-03 07:01:18 -0700 | [diff] [blame] | 75 | ++recording_set_index_; |
| 76 | #endif |
| 77 | } |
| 78 | |
| 79 | // Methods for performing dumping of data of various types into |
| 80 | // various formats. |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 81 | void DumpRaw(const char* name, double v) { |
| 82 | #if WEBRTC_APM_DEBUG_DUMP == 1 |
Per Åhgren | 7a95e0f | 2018-10-25 09:56:49 +0200 | [diff] [blame] | 83 | if (recording_activated_) { |
| 84 | FILE* file = GetRawFile(name); |
| 85 | fwrite(&v, sizeof(v), 1, file); |
| 86 | } |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 87 | #endif |
| 88 | } |
| 89 | |
| 90 | void DumpRaw(const char* name, size_t v_length, const double* v) { |
| 91 | #if WEBRTC_APM_DEBUG_DUMP == 1 |
Per Åhgren | 7a95e0f | 2018-10-25 09:56:49 +0200 | [diff] [blame] | 92 | if (recording_activated_) { |
| 93 | FILE* file = GetRawFile(name); |
| 94 | fwrite(v, sizeof(v[0]), v_length, file); |
| 95 | } |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 96 | #endif |
| 97 | } |
| 98 | |
| 99 | void DumpRaw(const char* name, rtc::ArrayView<const double> v) { |
| 100 | #if WEBRTC_APM_DEBUG_DUMP == 1 |
Per Åhgren | 7a95e0f | 2018-10-25 09:56:49 +0200 | [diff] [blame] | 101 | if (recording_activated_) { |
| 102 | DumpRaw(name, v.size(), v.data()); |
| 103 | } |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 104 | #endif |
| 105 | } |
| 106 | |
| 107 | void DumpRaw(const char* name, float v) { |
| 108 | #if WEBRTC_APM_DEBUG_DUMP == 1 |
Per Åhgren | 7a95e0f | 2018-10-25 09:56:49 +0200 | [diff] [blame] | 109 | if (recording_activated_) { |
| 110 | FILE* file = GetRawFile(name); |
| 111 | fwrite(&v, sizeof(v), 1, file); |
| 112 | } |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 113 | #endif |
| 114 | } |
| 115 | |
| 116 | void DumpRaw(const char* name, size_t v_length, const float* v) { |
peah | f28a389 | 2016-09-01 08:58:21 -0700 | [diff] [blame] | 117 | #if WEBRTC_APM_DEBUG_DUMP == 1 |
Per Åhgren | 7a95e0f | 2018-10-25 09:56:49 +0200 | [diff] [blame] | 118 | if (recording_activated_) { |
| 119 | FILE* file = GetRawFile(name); |
| 120 | fwrite(v, sizeof(v[0]), v_length, file); |
| 121 | } |
peah | b46083e | 2016-05-03 07:01:18 -0700 | [diff] [blame] | 122 | #endif |
| 123 | } |
| 124 | |
| 125 | void DumpRaw(const char* name, rtc::ArrayView<const float> v) { |
peah | f28a389 | 2016-09-01 08:58:21 -0700 | [diff] [blame] | 126 | #if WEBRTC_APM_DEBUG_DUMP == 1 |
Per Åhgren | 7a95e0f | 2018-10-25 09:56:49 +0200 | [diff] [blame] | 127 | if (recording_activated_) { |
| 128 | DumpRaw(name, v.size(), v.data()); |
| 129 | } |
peah | b46083e | 2016-05-03 07:01:18 -0700 | [diff] [blame] | 130 | #endif |
| 131 | } |
| 132 | |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 133 | void DumpRaw(const char* name, bool v) { |
| 134 | #if WEBRTC_APM_DEBUG_DUMP == 1 |
Per Åhgren | 7a95e0f | 2018-10-25 09:56:49 +0200 | [diff] [blame] | 135 | if (recording_activated_) { |
| 136 | DumpRaw(name, static_cast<int16_t>(v)); |
| 137 | } |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 138 | #endif |
| 139 | } |
| 140 | |
| 141 | void DumpRaw(const char* name, size_t v_length, const bool* v) { |
peah | f28a389 | 2016-09-01 08:58:21 -0700 | [diff] [blame] | 142 | #if WEBRTC_APM_DEBUG_DUMP == 1 |
Per Åhgren | 7a95e0f | 2018-10-25 09:56:49 +0200 | [diff] [blame] | 143 | if (recording_activated_) { |
| 144 | FILE* file = GetRawFile(name); |
| 145 | for (size_t k = 0; k < v_length; ++k) { |
| 146 | int16_t value = static_cast<int16_t>(v[k]); |
| 147 | fwrite(&value, sizeof(value), 1, file); |
| 148 | } |
peah | ca4cac7 | 2016-06-29 15:26:12 -0700 | [diff] [blame] | 149 | } |
| 150 | #endif |
| 151 | } |
| 152 | |
| 153 | void DumpRaw(const char* name, rtc::ArrayView<const bool> v) { |
peah | f28a389 | 2016-09-01 08:58:21 -0700 | [diff] [blame] | 154 | #if WEBRTC_APM_DEBUG_DUMP == 1 |
Per Åhgren | 7a95e0f | 2018-10-25 09:56:49 +0200 | [diff] [blame] | 155 | if (recording_activated_) { |
| 156 | DumpRaw(name, v.size(), v.data()); |
| 157 | } |
peah | ca4cac7 | 2016-06-29 15:26:12 -0700 | [diff] [blame] | 158 | #endif |
| 159 | } |
| 160 | |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 161 | void DumpRaw(const char* name, int16_t v) { |
| 162 | #if WEBRTC_APM_DEBUG_DUMP == 1 |
Per Åhgren | 7a95e0f | 2018-10-25 09:56:49 +0200 | [diff] [blame] | 163 | if (recording_activated_) { |
| 164 | FILE* file = GetRawFile(name); |
| 165 | fwrite(&v, sizeof(v), 1, file); |
| 166 | } |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 167 | #endif |
| 168 | } |
| 169 | |
| 170 | void DumpRaw(const char* name, size_t v_length, const int16_t* v) { |
peah | f28a389 | 2016-09-01 08:58:21 -0700 | [diff] [blame] | 171 | #if WEBRTC_APM_DEBUG_DUMP == 1 |
Per Åhgren | 7a95e0f | 2018-10-25 09:56:49 +0200 | [diff] [blame] | 172 | if (recording_activated_) { |
| 173 | FILE* file = GetRawFile(name); |
| 174 | fwrite(v, sizeof(v[0]), v_length, file); |
| 175 | } |
peah | 3f08dc6 | 2016-05-05 03:03:55 -0700 | [diff] [blame] | 176 | #endif |
| 177 | } |
| 178 | |
| 179 | void DumpRaw(const char* name, rtc::ArrayView<const int16_t> v) { |
peah | f28a389 | 2016-09-01 08:58:21 -0700 | [diff] [blame] | 180 | #if WEBRTC_APM_DEBUG_DUMP == 1 |
Per Åhgren | 7a95e0f | 2018-10-25 09:56:49 +0200 | [diff] [blame] | 181 | if (recording_activated_) { |
| 182 | DumpRaw(name, v.size(), v.data()); |
| 183 | } |
peah | 3f08dc6 | 2016-05-05 03:03:55 -0700 | [diff] [blame] | 184 | #endif |
| 185 | } |
| 186 | |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 187 | void DumpRaw(const char* name, int32_t v) { |
| 188 | #if WEBRTC_APM_DEBUG_DUMP == 1 |
Per Åhgren | 7a95e0f | 2018-10-25 09:56:49 +0200 | [diff] [blame] | 189 | if (recording_activated_) { |
| 190 | FILE* file = GetRawFile(name); |
| 191 | fwrite(&v, sizeof(v), 1, file); |
| 192 | } |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 193 | #endif |
| 194 | } |
| 195 | |
| 196 | void DumpRaw(const char* name, size_t v_length, const int32_t* v) { |
| 197 | #if WEBRTC_APM_DEBUG_DUMP == 1 |
Per Åhgren | 7a95e0f | 2018-10-25 09:56:49 +0200 | [diff] [blame] | 198 | if (recording_activated_) { |
| 199 | FILE* file = GetRawFile(name); |
| 200 | fwrite(v, sizeof(v[0]), v_length, file); |
| 201 | } |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 202 | #endif |
| 203 | } |
| 204 | |
| 205 | void DumpRaw(const char* name, size_t v) { |
| 206 | #if WEBRTC_APM_DEBUG_DUMP == 1 |
Per Åhgren | 7a95e0f | 2018-10-25 09:56:49 +0200 | [diff] [blame] | 207 | if (recording_activated_) { |
| 208 | FILE* file = GetRawFile(name); |
| 209 | fwrite(&v, sizeof(v), 1, file); |
| 210 | } |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 211 | #endif |
| 212 | } |
| 213 | |
| 214 | void DumpRaw(const char* name, size_t v_length, const size_t* v) { |
peah | f28a389 | 2016-09-01 08:58:21 -0700 | [diff] [blame] | 215 | #if WEBRTC_APM_DEBUG_DUMP == 1 |
Per Åhgren | 7a95e0f | 2018-10-25 09:56:49 +0200 | [diff] [blame] | 216 | if (recording_activated_) { |
| 217 | FILE* file = GetRawFile(name); |
| 218 | fwrite(v, sizeof(v[0]), v_length, file); |
| 219 | } |
peah | 3f08dc6 | 2016-05-05 03:03:55 -0700 | [diff] [blame] | 220 | #endif |
| 221 | } |
| 222 | |
| 223 | void DumpRaw(const char* name, rtc::ArrayView<const int32_t> v) { |
peah | f28a389 | 2016-09-01 08:58:21 -0700 | [diff] [blame] | 224 | #if WEBRTC_APM_DEBUG_DUMP == 1 |
Per Åhgren | 7a95e0f | 2018-10-25 09:56:49 +0200 | [diff] [blame] | 225 | if (recording_activated_) { |
| 226 | DumpRaw(name, v.size(), v.data()); |
| 227 | } |
peah | 3f08dc6 | 2016-05-05 03:03:55 -0700 | [diff] [blame] | 228 | #endif |
| 229 | } |
| 230 | |
Jesús de Vicente Peña | 44974e1 | 2018-11-20 12:54:23 +0100 | [diff] [blame] | 231 | void DumpRaw(const char* name, rtc::ArrayView<const size_t> v) { |
| 232 | #if WEBRTC_APM_DEBUG_DUMP == 1 |
| 233 | DumpRaw(name, v.size(), v.data()); |
| 234 | #endif |
| 235 | } |
| 236 | |
peah | b46083e | 2016-05-03 07:01:18 -0700 | [diff] [blame] | 237 | void DumpWav(const char* name, |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 238 | size_t v_length, |
peah | b46083e | 2016-05-03 07:01:18 -0700 | [diff] [blame] | 239 | const float* v, |
| 240 | int sample_rate_hz, |
| 241 | int num_channels) { |
peah | f28a389 | 2016-09-01 08:58:21 -0700 | [diff] [blame] | 242 | #if WEBRTC_APM_DEBUG_DUMP == 1 |
Per Åhgren | 7a95e0f | 2018-10-25 09:56:49 +0200 | [diff] [blame] | 243 | if (recording_activated_) { |
| 244 | WavWriter* file = GetWavFile(name, sample_rate_hz, num_channels); |
| 245 | file->WriteSamples(v, v_length); |
| 246 | } |
peah | b46083e | 2016-05-03 07:01:18 -0700 | [diff] [blame] | 247 | #endif |
| 248 | } |
| 249 | |
peah | ca4cac7 | 2016-06-29 15:26:12 -0700 | [diff] [blame] | 250 | void DumpWav(const char* name, |
| 251 | rtc::ArrayView<const float> v, |
| 252 | int sample_rate_hz, |
| 253 | int num_channels) { |
peah | f28a389 | 2016-09-01 08:58:21 -0700 | [diff] [blame] | 254 | #if WEBRTC_APM_DEBUG_DUMP == 1 |
Per Åhgren | 7a95e0f | 2018-10-25 09:56:49 +0200 | [diff] [blame] | 255 | if (recording_activated_) { |
| 256 | DumpWav(name, v.size(), v.data(), sample_rate_hz, num_channels); |
| 257 | } |
peah | ca4cac7 | 2016-06-29 15:26:12 -0700 | [diff] [blame] | 258 | #endif |
| 259 | } |
| 260 | |
peah | b46083e | 2016-05-03 07:01:18 -0700 | [diff] [blame] | 261 | private: |
peah | f28a389 | 2016-09-01 08:58:21 -0700 | [diff] [blame] | 262 | #if WEBRTC_APM_DEBUG_DUMP == 1 |
Per Åhgren | 7a95e0f | 2018-10-25 09:56:49 +0200 | [diff] [blame] | 263 | static bool recording_activated_; |
Alessio Bazzica | 4bc6045 | 2018-11-20 12:44:15 +0100 | [diff] [blame^] | 264 | static constexpr size_t kOutputDirMaxLength = 1024; |
| 265 | static char output_dir_[kOutputDirMaxLength]; |
peah | b46083e | 2016-05-03 07:01:18 -0700 | [diff] [blame] | 266 | const int instance_index_; |
| 267 | int recording_set_index_ = 0; |
| 268 | std::unordered_map<std::string, std::unique_ptr<FILE, RawFileCloseFunctor>> |
| 269 | raw_files_; |
| 270 | std::unordered_map<std::string, std::unique_ptr<WavWriter>> wav_files_; |
| 271 | |
| 272 | FILE* GetRawFile(const char* name); |
| 273 | WavWriter* GetWavFile(const char* name, int sample_rate_hz, int num_channels); |
| 274 | #endif |
| 275 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(ApmDataDumper); |
| 276 | }; |
| 277 | |
| 278 | } // namespace webrtc |
| 279 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 280 | #endif // MODULES_AUDIO_PROCESSING_LOGGING_APM_DATA_DUMPER_H_ |