blob: 8a751c64789c4e6ffcfdbc67671692607468d805 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/array_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/constructormagic.h"
peahb46083e2016-05-03 07:01:18 -070019
20// Check to verify that the define is properly set.
peahf28a3892016-09-01 08:58:21 -070021#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"
peahb46083e2016-05-03 07:01:18 -070024#endif
25
26namespace webrtc {
27
peahf28a3892016-09-01 08:58:21 -070028#if WEBRTC_APM_DEBUG_DUMP == 1
peahb46083e2016-05-03 07:01:18 -070029// Functor used to use as a custom deleter in the map of file pointers to raw
30// files.
31struct RawFileCloseFunctor {
32 void operator()(FILE* f) const { fclose(f); }
33};
34#endif
35
36// Class that handles dumping of variables into files.
37class ApmDataDumper {
38 public:
peahf8f754a2016-08-30 10:31:45 -070039 // 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();
peahb46083e2016-05-03 07:01:18 -070045
46 // Reinitializes the data dumping such that new versions
47 // of all files being dumped to are created.
48 void InitiateNewSetOfRecordings() {
peahf28a3892016-09-01 08:58:21 -070049#if WEBRTC_APM_DEBUG_DUMP == 1
peahb46083e2016-05-03 07:01:18 -070050 ++recording_set_index_;
51#endif
52 }
53
54 // Methods for performing dumping of data of various types into
55 // various formats.
peah69221db2017-01-27 03:28:19 -080056 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) {
peahf28a3892016-09-01 08:58:21 -070084#if WEBRTC_APM_DEBUG_DUMP == 1
peahb46083e2016-05-03 07:01:18 -070085 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) {
peahf28a3892016-09-01 08:58:21 -070091#if WEBRTC_APM_DEBUG_DUMP == 1
peahb46083e2016-05-03 07:01:18 -070092 DumpRaw(name, v.size(), v.data());
93#endif
94 }
95
peah69221db2017-01-27 03:28:19 -080096 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) {
peahf28a3892016-09-01 08:58:21 -0700103#if WEBRTC_APM_DEBUG_DUMP == 1
peahca4cac72016-06-29 15:26:12 -0700104 FILE* file = GetRawFile(name);
peah69221db2017-01-27 03:28:19 -0800105 for (size_t k = 0; k < v_length; ++k) {
peahca4cac72016-06-29 15:26:12 -0700106 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) {
peahf28a3892016-09-01 08:58:21 -0700113#if WEBRTC_APM_DEBUG_DUMP == 1
peahca4cac72016-06-29 15:26:12 -0700114 DumpRaw(name, v.size(), v.data());
115#endif
116 }
117
peah69221db2017-01-27 03:28:19 -0800118 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) {
peahf28a3892016-09-01 08:58:21 -0700126#if WEBRTC_APM_DEBUG_DUMP == 1
peah3f08dc62016-05-05 03:03:55 -0700127 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) {
peahf28a3892016-09-01 08:58:21 -0700133#if WEBRTC_APM_DEBUG_DUMP == 1
peah3f08dc62016-05-05 03:03:55 -0700134 DumpRaw(name, v.size(), v.data());
135#endif
136 }
137
peah69221db2017-01-27 03:28:19 -0800138 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) {
peahf28a3892016-09-01 08:58:21 -0700160#if WEBRTC_APM_DEBUG_DUMP == 1
peah3f08dc62016-05-05 03:03:55 -0700161 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) {
peahf28a3892016-09-01 08:58:21 -0700167#if WEBRTC_APM_DEBUG_DUMP == 1
peah3f08dc62016-05-05 03:03:55 -0700168 DumpRaw(name, v.size(), v.data());
169#endif
170 }
171
peahb46083e2016-05-03 07:01:18 -0700172 void DumpWav(const char* name,
peah69221db2017-01-27 03:28:19 -0800173 size_t v_length,
peahb46083e2016-05-03 07:01:18 -0700174 const float* v,
175 int sample_rate_hz,
176 int num_channels) {
peahf28a3892016-09-01 08:58:21 -0700177#if WEBRTC_APM_DEBUG_DUMP == 1
peahb46083e2016-05-03 07:01:18 -0700178 WavWriter* file = GetWavFile(name, sample_rate_hz, num_channels);
179 file->WriteSamples(v, v_length);
180#endif
181 }
182
peahca4cac72016-06-29 15:26:12 -0700183 void DumpWav(const char* name,
184 rtc::ArrayView<const float> v,
185 int sample_rate_hz,
186 int num_channels) {
peahf28a3892016-09-01 08:58:21 -0700187#if WEBRTC_APM_DEBUG_DUMP == 1
peahca4cac72016-06-29 15:26:12 -0700188 DumpWav(name, v.size(), v.data(), sample_rate_hz, num_channels);
189#endif
190 }
191
peahb46083e2016-05-03 07:01:18 -0700192 private:
peahf28a3892016-09-01 08:58:21 -0700193#if WEBRTC_APM_DEBUG_DUMP == 1
peahb46083e2016-05-03 07:01:18 -0700194 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 Bonadei92ea95e2017-09-15 06:47:31 +0200208#endif // MODULES_AUDIO_PROCESSING_LOGGING_APM_DATA_DUMPER_H_