blob: 5a8a3899cfccd67b3381058c14e57d4dd8727ed5 [file] [log] [blame]
peahb46083e2016-05-03 07:01:18 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_PROCESSING_LOGGING_APM_DATA_DUMPER_H_
12#define MODULES_AUDIO_PROCESSING_LOGGING_APM_DATA_DUMPER_H_
peahb46083e2016-05-03 07:01:18 -070013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stdint.h>
peahb46083e2016-05-03 07:01:18 -070015#include <stdio.h>
16
Per Åhgrenf0c449e2018-10-23 20:17:18 +020017#if WEBRTC_APM_DEBUG_DUMP == 1
18#include <unordered_map>
19#endif
20
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "api/array_view.h"
Per Åhgrenf0c449e2018-10-23 20:17:18 +020022#if WEBRTC_APM_DEBUG_DUMP == 1
23#include "common_audio/wav_file.h"
24#endif
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "rtc_base/constructormagic.h"
peahb46083e2016-05-03 07:01:18 -070026
27// Check to verify that the define is properly set.
peahf28a3892016-09-01 08:58:21 -070028#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"
peahb46083e2016-05-03 07:01:18 -070031#endif
32
33namespace webrtc {
34
peahf28a3892016-09-01 08:58:21 -070035#if WEBRTC_APM_DEBUG_DUMP == 1
peahb46083e2016-05-03 07:01:18 -070036// Functor used to use as a custom deleter in the map of file pointers to raw
37// files.
38struct RawFileCloseFunctor {
39 void operator()(FILE* f) const { fclose(f); }
40};
41#endif
42
43// Class that handles dumping of variables into files.
44class ApmDataDumper {
45 public:
peahf8f754a2016-08-30 10:31:45 -070046 // 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();
peahb46083e2016-05-03 07:01:18 -070052
Per Åhgren7a95e0f2018-10-25 09:56:49 +020053 // 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
peahb46083e2016-05-03 07:01:18 -070060 // Reinitializes the data dumping such that new versions
61 // of all files being dumped to are created.
62 void InitiateNewSetOfRecordings() {
peahf28a3892016-09-01 08:58:21 -070063#if WEBRTC_APM_DEBUG_DUMP == 1
peahb46083e2016-05-03 07:01:18 -070064 ++recording_set_index_;
65#endif
66 }
67
68 // Methods for performing dumping of data of various types into
69 // various formats.
peah69221db2017-01-27 03:28:19 -080070 void DumpRaw(const char* name, double v) {
71#if WEBRTC_APM_DEBUG_DUMP == 1
Per Åhgren7a95e0f2018-10-25 09:56:49 +020072 if (recording_activated_) {
73 FILE* file = GetRawFile(name);
74 fwrite(&v, sizeof(v), 1, file);
75 }
peah69221db2017-01-27 03:28:19 -080076#endif
77 }
78
79 void DumpRaw(const char* name, size_t v_length, const double* v) {
80#if WEBRTC_APM_DEBUG_DUMP == 1
Per Åhgren7a95e0f2018-10-25 09:56:49 +020081 if (recording_activated_) {
82 FILE* file = GetRawFile(name);
83 fwrite(v, sizeof(v[0]), v_length, file);
84 }
peah69221db2017-01-27 03:28:19 -080085#endif
86 }
87
88 void DumpRaw(const char* name, rtc::ArrayView<const double> v) {
89#if WEBRTC_APM_DEBUG_DUMP == 1
Per Åhgren7a95e0f2018-10-25 09:56:49 +020090 if (recording_activated_) {
91 DumpRaw(name, v.size(), v.data());
92 }
peah69221db2017-01-27 03:28:19 -080093#endif
94 }
95
96 void DumpRaw(const char* name, float v) {
97#if WEBRTC_APM_DEBUG_DUMP == 1
Per Åhgren7a95e0f2018-10-25 09:56:49 +020098 if (recording_activated_) {
99 FILE* file = GetRawFile(name);
100 fwrite(&v, sizeof(v), 1, file);
101 }
peah69221db2017-01-27 03:28:19 -0800102#endif
103 }
104
105 void DumpRaw(const char* name, size_t v_length, const float* v) {
peahf28a3892016-09-01 08:58:21 -0700106#if WEBRTC_APM_DEBUG_DUMP == 1
Per Åhgren7a95e0f2018-10-25 09:56:49 +0200107 if (recording_activated_) {
108 FILE* file = GetRawFile(name);
109 fwrite(v, sizeof(v[0]), v_length, file);
110 }
peahb46083e2016-05-03 07:01:18 -0700111#endif
112 }
113
114 void DumpRaw(const char* name, rtc::ArrayView<const float> v) {
peahf28a3892016-09-01 08:58:21 -0700115#if WEBRTC_APM_DEBUG_DUMP == 1
Per Åhgren7a95e0f2018-10-25 09:56:49 +0200116 if (recording_activated_) {
117 DumpRaw(name, v.size(), v.data());
118 }
peahb46083e2016-05-03 07:01:18 -0700119#endif
120 }
121
peah69221db2017-01-27 03:28:19 -0800122 void DumpRaw(const char* name, bool v) {
123#if WEBRTC_APM_DEBUG_DUMP == 1
Per Åhgren7a95e0f2018-10-25 09:56:49 +0200124 if (recording_activated_) {
125 DumpRaw(name, static_cast<int16_t>(v));
126 }
peah69221db2017-01-27 03:28:19 -0800127#endif
128 }
129
130 void DumpRaw(const char* name, size_t v_length, const bool* v) {
peahf28a3892016-09-01 08:58:21 -0700131#if WEBRTC_APM_DEBUG_DUMP == 1
Per Åhgren7a95e0f2018-10-25 09:56:49 +0200132 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 }
peahca4cac72016-06-29 15:26:12 -0700138 }
139#endif
140 }
141
142 void DumpRaw(const char* name, rtc::ArrayView<const bool> v) {
peahf28a3892016-09-01 08:58:21 -0700143#if WEBRTC_APM_DEBUG_DUMP == 1
Per Åhgren7a95e0f2018-10-25 09:56:49 +0200144 if (recording_activated_) {
145 DumpRaw(name, v.size(), v.data());
146 }
peahca4cac72016-06-29 15:26:12 -0700147#endif
148 }
149
peah69221db2017-01-27 03:28:19 -0800150 void DumpRaw(const char* name, int16_t v) {
151#if WEBRTC_APM_DEBUG_DUMP == 1
Per Åhgren7a95e0f2018-10-25 09:56:49 +0200152 if (recording_activated_) {
153 FILE* file = GetRawFile(name);
154 fwrite(&v, sizeof(v), 1, file);
155 }
peah69221db2017-01-27 03:28:19 -0800156#endif
157 }
158
159 void DumpRaw(const char* name, size_t v_length, const int16_t* v) {
peahf28a3892016-09-01 08:58:21 -0700160#if WEBRTC_APM_DEBUG_DUMP == 1
Per Åhgren7a95e0f2018-10-25 09:56:49 +0200161 if (recording_activated_) {
162 FILE* file = GetRawFile(name);
163 fwrite(v, sizeof(v[0]), v_length, file);
164 }
peah3f08dc62016-05-05 03:03:55 -0700165#endif
166 }
167
168 void DumpRaw(const char* name, rtc::ArrayView<const int16_t> v) {
peahf28a3892016-09-01 08:58:21 -0700169#if WEBRTC_APM_DEBUG_DUMP == 1
Per Åhgren7a95e0f2018-10-25 09:56:49 +0200170 if (recording_activated_) {
171 DumpRaw(name, v.size(), v.data());
172 }
peah3f08dc62016-05-05 03:03:55 -0700173#endif
174 }
175
peah69221db2017-01-27 03:28:19 -0800176 void DumpRaw(const char* name, int32_t v) {
177#if WEBRTC_APM_DEBUG_DUMP == 1
Per Åhgren7a95e0f2018-10-25 09:56:49 +0200178 if (recording_activated_) {
179 FILE* file = GetRawFile(name);
180 fwrite(&v, sizeof(v), 1, file);
181 }
peah69221db2017-01-27 03:28:19 -0800182#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 Åhgren7a95e0f2018-10-25 09:56:49 +0200187 if (recording_activated_) {
188 FILE* file = GetRawFile(name);
189 fwrite(v, sizeof(v[0]), v_length, file);
190 }
peah69221db2017-01-27 03:28:19 -0800191#endif
192 }
193
194 void DumpRaw(const char* name, size_t v) {
195#if WEBRTC_APM_DEBUG_DUMP == 1
Per Åhgren7a95e0f2018-10-25 09:56:49 +0200196 if (recording_activated_) {
197 FILE* file = GetRawFile(name);
198 fwrite(&v, sizeof(v), 1, file);
199 }
peah69221db2017-01-27 03:28:19 -0800200#endif
201 }
202
203 void DumpRaw(const char* name, size_t v_length, const size_t* v) {
peahf28a3892016-09-01 08:58:21 -0700204#if WEBRTC_APM_DEBUG_DUMP == 1
Per Åhgren7a95e0f2018-10-25 09:56:49 +0200205 if (recording_activated_) {
206 FILE* file = GetRawFile(name);
207 fwrite(v, sizeof(v[0]), v_length, file);
208 }
peah3f08dc62016-05-05 03:03:55 -0700209#endif
210 }
211
212 void DumpRaw(const char* name, rtc::ArrayView<const int32_t> v) {
peahf28a3892016-09-01 08:58:21 -0700213#if WEBRTC_APM_DEBUG_DUMP == 1
Per Åhgren7a95e0f2018-10-25 09:56:49 +0200214 if (recording_activated_) {
215 DumpRaw(name, v.size(), v.data());
216 }
peah3f08dc62016-05-05 03:03:55 -0700217#endif
218 }
219
Jesús de Vicente Peña44974e12018-11-20 12:54:23 +0100220 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
peahb46083e2016-05-03 07:01:18 -0700226 void DumpWav(const char* name,
peah69221db2017-01-27 03:28:19 -0800227 size_t v_length,
peahb46083e2016-05-03 07:01:18 -0700228 const float* v,
229 int sample_rate_hz,
230 int num_channels) {
peahf28a3892016-09-01 08:58:21 -0700231#if WEBRTC_APM_DEBUG_DUMP == 1
Per Åhgren7a95e0f2018-10-25 09:56:49 +0200232 if (recording_activated_) {
233 WavWriter* file = GetWavFile(name, sample_rate_hz, num_channels);
234 file->WriteSamples(v, v_length);
235 }
peahb46083e2016-05-03 07:01:18 -0700236#endif
237 }
238
peahca4cac72016-06-29 15:26:12 -0700239 void DumpWav(const char* name,
240 rtc::ArrayView<const float> v,
241 int sample_rate_hz,
242 int num_channels) {
peahf28a3892016-09-01 08:58:21 -0700243#if WEBRTC_APM_DEBUG_DUMP == 1
Per Åhgren7a95e0f2018-10-25 09:56:49 +0200244 if (recording_activated_) {
245 DumpWav(name, v.size(), v.data(), sample_rate_hz, num_channels);
246 }
peahca4cac72016-06-29 15:26:12 -0700247#endif
248 }
249
peahb46083e2016-05-03 07:01:18 -0700250 private:
peahf28a3892016-09-01 08:58:21 -0700251#if WEBRTC_APM_DEBUG_DUMP == 1
Per Åhgren7a95e0f2018-10-25 09:56:49 +0200252 static bool recording_activated_;
peahb46083e2016-05-03 07:01:18 -0700253 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 Bonadei92ea95e2017-09-15 06:47:31 +0200267#endif // MODULES_AUDIO_PROCESSING_LOGGING_APM_DATA_DUMPER_H_