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