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