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