blob: d045027b354165a02419ba9d44b768f6313efc2a [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
14#include <stdio.h>
15
16#include <memory>
17#include <string>
18#include <unordered_map>
19
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "api/array_view.h"
21#include "common_audio/wav_file.h"
22#include "rtc_base/constructormagic.h"
peahb46083e2016-05-03 07:01:18 -070023
24// Check to verify that the define is properly set.
peahf28a3892016-09-01 08:58:21 -070025#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"
peahb46083e2016-05-03 07:01:18 -070028#endif
29
30namespace webrtc {
31
peahf28a3892016-09-01 08:58:21 -070032#if WEBRTC_APM_DEBUG_DUMP == 1
peahb46083e2016-05-03 07:01:18 -070033// Functor used to use as a custom deleter in the map of file pointers to raw
34// files.
35struct RawFileCloseFunctor {
36 void operator()(FILE* f) const { fclose(f); }
37};
38#endif
39
40// Class that handles dumping of variables into files.
41class ApmDataDumper {
42 public:
peahf8f754a2016-08-30 10:31:45 -070043 // 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();
peahb46083e2016-05-03 07:01:18 -070049
50 // Reinitializes the data dumping such that new versions
51 // of all files being dumped to are created.
52 void InitiateNewSetOfRecordings() {
peahf28a3892016-09-01 08:58:21 -070053#if WEBRTC_APM_DEBUG_DUMP == 1
peahb46083e2016-05-03 07:01:18 -070054 ++recording_set_index_;
55#endif
56 }
57
58 // Methods for performing dumping of data of various types into
59 // various formats.
peah69221db2017-01-27 03:28:19 -080060 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) {
peahf28a3892016-09-01 08:58:21 -070088#if WEBRTC_APM_DEBUG_DUMP == 1
peahb46083e2016-05-03 07:01:18 -070089 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) {
peahf28a3892016-09-01 08:58:21 -070095#if WEBRTC_APM_DEBUG_DUMP == 1
peahb46083e2016-05-03 07:01:18 -070096 DumpRaw(name, v.size(), v.data());
97#endif
98 }
99
peah69221db2017-01-27 03:28:19 -0800100 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) {
peahf28a3892016-09-01 08:58:21 -0700107#if WEBRTC_APM_DEBUG_DUMP == 1
peahca4cac72016-06-29 15:26:12 -0700108 FILE* file = GetRawFile(name);
peah69221db2017-01-27 03:28:19 -0800109 for (size_t k = 0; k < v_length; ++k) {
peahca4cac72016-06-29 15:26:12 -0700110 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) {
peahf28a3892016-09-01 08:58:21 -0700117#if WEBRTC_APM_DEBUG_DUMP == 1
peahca4cac72016-06-29 15:26:12 -0700118 DumpRaw(name, v.size(), v.data());
119#endif
120 }
121
peah69221db2017-01-27 03:28:19 -0800122 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) {
peahf28a3892016-09-01 08:58:21 -0700130#if WEBRTC_APM_DEBUG_DUMP == 1
peah3f08dc62016-05-05 03:03:55 -0700131 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) {
peahf28a3892016-09-01 08:58:21 -0700137#if WEBRTC_APM_DEBUG_DUMP == 1
peah3f08dc62016-05-05 03:03:55 -0700138 DumpRaw(name, v.size(), v.data());
139#endif
140 }
141
peah69221db2017-01-27 03:28:19 -0800142 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) {
peahf28a3892016-09-01 08:58:21 -0700164#if WEBRTC_APM_DEBUG_DUMP == 1
peah3f08dc62016-05-05 03:03:55 -0700165 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) {
peahf28a3892016-09-01 08:58:21 -0700171#if WEBRTC_APM_DEBUG_DUMP == 1
peah3f08dc62016-05-05 03:03:55 -0700172 DumpRaw(name, v.size(), v.data());
173#endif
174 }
175
peahb46083e2016-05-03 07:01:18 -0700176 void DumpWav(const char* name,
peah69221db2017-01-27 03:28:19 -0800177 size_t v_length,
peahb46083e2016-05-03 07:01:18 -0700178 const float* v,
179 int sample_rate_hz,
180 int num_channels) {
peahf28a3892016-09-01 08:58:21 -0700181#if WEBRTC_APM_DEBUG_DUMP == 1
peahb46083e2016-05-03 07:01:18 -0700182 WavWriter* file = GetWavFile(name, sample_rate_hz, num_channels);
183 file->WriteSamples(v, v_length);
184#endif
185 }
186
peahca4cac72016-06-29 15:26:12 -0700187 void DumpWav(const char* name,
188 rtc::ArrayView<const float> v,
189 int sample_rate_hz,
190 int num_channels) {
peahf28a3892016-09-01 08:58:21 -0700191#if WEBRTC_APM_DEBUG_DUMP == 1
peahca4cac72016-06-29 15:26:12 -0700192 DumpWav(name, v.size(), v.data(), sample_rate_hz, num_channels);
193#endif
194 }
195
peahb46083e2016-05-03 07:01:18 -0700196 private:
peahf28a3892016-09-01 08:58:21 -0700197#if WEBRTC_APM_DEBUG_DUMP == 1
peahb46083e2016-05-03 07:01:18 -0700198 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 Bonadei92ea95e2017-09-15 06:47:31 +0200212#endif // MODULES_AUDIO_PROCESSING_LOGGING_APM_DATA_DUMPER_H_