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