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