blob: 10deb35edd0fab5e5f9e797ded3564b9f4fe14b6 [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
53 // Reinitializes the data dumping such that new versions
54 // of all files being dumped to are created.
55 void InitiateNewSetOfRecordings() {
peahf28a3892016-09-01 08:58:21 -070056#if WEBRTC_APM_DEBUG_DUMP == 1
peahb46083e2016-05-03 07:01:18 -070057 ++recording_set_index_;
58#endif
59 }
60
61 // Methods for performing dumping of data of various types into
62 // various formats.
peah69221db2017-01-27 03:28:19 -080063 void DumpRaw(const char* name, double v) {
64#if WEBRTC_APM_DEBUG_DUMP == 1
65 FILE* file = GetRawFile(name);
66 fwrite(&v, sizeof(v), 1, file);
67#endif
68 }
69
70 void DumpRaw(const char* name, size_t v_length, const double* v) {
71#if WEBRTC_APM_DEBUG_DUMP == 1
72 FILE* file = GetRawFile(name);
73 fwrite(v, sizeof(v[0]), v_length, file);
74#endif
75 }
76
77 void DumpRaw(const char* name, rtc::ArrayView<const double> v) {
78#if WEBRTC_APM_DEBUG_DUMP == 1
79 DumpRaw(name, v.size(), v.data());
80#endif
81 }
82
83 void DumpRaw(const char* name, float v) {
84#if WEBRTC_APM_DEBUG_DUMP == 1
85 FILE* file = GetRawFile(name);
86 fwrite(&v, sizeof(v), 1, file);
87#endif
88 }
89
90 void DumpRaw(const char* name, size_t v_length, const float* v) {
peahf28a3892016-09-01 08:58:21 -070091#if WEBRTC_APM_DEBUG_DUMP == 1
peahb46083e2016-05-03 07:01:18 -070092 FILE* file = GetRawFile(name);
93 fwrite(v, sizeof(v[0]), v_length, file);
94#endif
95 }
96
97 void DumpRaw(const char* name, rtc::ArrayView<const float> v) {
peahf28a3892016-09-01 08:58:21 -070098#if WEBRTC_APM_DEBUG_DUMP == 1
peahb46083e2016-05-03 07:01:18 -070099 DumpRaw(name, v.size(), v.data());
100#endif
101 }
102
peah69221db2017-01-27 03:28:19 -0800103 void DumpRaw(const char* name, bool v) {
104#if WEBRTC_APM_DEBUG_DUMP == 1
105 DumpRaw(name, static_cast<int16_t>(v));
106#endif
107 }
108
109 void DumpRaw(const char* name, size_t v_length, const bool* v) {
peahf28a3892016-09-01 08:58:21 -0700110#if WEBRTC_APM_DEBUG_DUMP == 1
peahca4cac72016-06-29 15:26:12 -0700111 FILE* file = GetRawFile(name);
peah69221db2017-01-27 03:28:19 -0800112 for (size_t k = 0; k < v_length; ++k) {
peahca4cac72016-06-29 15:26:12 -0700113 int16_t value = static_cast<int16_t>(v[k]);
114 fwrite(&value, sizeof(value), 1, file);
115 }
116#endif
117 }
118
119 void DumpRaw(const char* name, rtc::ArrayView<const bool> v) {
peahf28a3892016-09-01 08:58:21 -0700120#if WEBRTC_APM_DEBUG_DUMP == 1
peahca4cac72016-06-29 15:26:12 -0700121 DumpRaw(name, v.size(), v.data());
122#endif
123 }
124
peah69221db2017-01-27 03:28:19 -0800125 void DumpRaw(const char* name, int16_t v) {
126#if WEBRTC_APM_DEBUG_DUMP == 1
127 FILE* file = GetRawFile(name);
128 fwrite(&v, sizeof(v), 1, file);
129#endif
130 }
131
132 void DumpRaw(const char* name, size_t v_length, const int16_t* v) {
peahf28a3892016-09-01 08:58:21 -0700133#if WEBRTC_APM_DEBUG_DUMP == 1
peah3f08dc62016-05-05 03:03:55 -0700134 FILE* file = GetRawFile(name);
135 fwrite(v, sizeof(v[0]), v_length, file);
136#endif
137 }
138
139 void DumpRaw(const char* name, rtc::ArrayView<const int16_t> v) {
peahf28a3892016-09-01 08:58:21 -0700140#if WEBRTC_APM_DEBUG_DUMP == 1
peah3f08dc62016-05-05 03:03:55 -0700141 DumpRaw(name, v.size(), v.data());
142#endif
143 }
144
peah69221db2017-01-27 03:28:19 -0800145 void DumpRaw(const char* name, int32_t v) {
146#if WEBRTC_APM_DEBUG_DUMP == 1
147 FILE* file = GetRawFile(name);
148 fwrite(&v, sizeof(v), 1, file);
149#endif
150 }
151
152 void DumpRaw(const char* name, size_t v_length, const int32_t* v) {
153#if WEBRTC_APM_DEBUG_DUMP == 1
154 FILE* file = GetRawFile(name);
155 fwrite(v, sizeof(v[0]), v_length, file);
156#endif
157 }
158
159 void DumpRaw(const char* name, size_t v) {
160#if WEBRTC_APM_DEBUG_DUMP == 1
161 FILE* file = GetRawFile(name);
162 fwrite(&v, sizeof(v), 1, file);
163#endif
164 }
165
166 void DumpRaw(const char* name, size_t v_length, const size_t* v) {
peahf28a3892016-09-01 08:58:21 -0700167#if WEBRTC_APM_DEBUG_DUMP == 1
peah3f08dc62016-05-05 03:03:55 -0700168 FILE* file = GetRawFile(name);
169 fwrite(v, sizeof(v[0]), v_length, file);
170#endif
171 }
172
173 void DumpRaw(const char* name, rtc::ArrayView<const int32_t> v) {
peahf28a3892016-09-01 08:58:21 -0700174#if WEBRTC_APM_DEBUG_DUMP == 1
peah3f08dc62016-05-05 03:03:55 -0700175 DumpRaw(name, v.size(), v.data());
176#endif
177 }
178
peahb46083e2016-05-03 07:01:18 -0700179 void DumpWav(const char* name,
peah69221db2017-01-27 03:28:19 -0800180 size_t v_length,
peahb46083e2016-05-03 07:01:18 -0700181 const float* v,
182 int sample_rate_hz,
183 int num_channels) {
peahf28a3892016-09-01 08:58:21 -0700184#if WEBRTC_APM_DEBUG_DUMP == 1
peahb46083e2016-05-03 07:01:18 -0700185 WavWriter* file = GetWavFile(name, sample_rate_hz, num_channels);
186 file->WriteSamples(v, v_length);
187#endif
188 }
189
peahca4cac72016-06-29 15:26:12 -0700190 void DumpWav(const char* name,
191 rtc::ArrayView<const float> v,
192 int sample_rate_hz,
193 int num_channels) {
peahf28a3892016-09-01 08:58:21 -0700194#if WEBRTC_APM_DEBUG_DUMP == 1
peahca4cac72016-06-29 15:26:12 -0700195 DumpWav(name, v.size(), v.data(), sample_rate_hz, num_channels);
196#endif
197 }
198
peahb46083e2016-05-03 07:01:18 -0700199 private:
peahf28a3892016-09-01 08:58:21 -0700200#if WEBRTC_APM_DEBUG_DUMP == 1
peahb46083e2016-05-03 07:01:18 -0700201 const int instance_index_;
202 int recording_set_index_ = 0;
203 std::unordered_map<std::string, std::unique_ptr<FILE, RawFileCloseFunctor>>
204 raw_files_;
205 std::unordered_map<std::string, std::unique_ptr<WavWriter>> wav_files_;
206
207 FILE* GetRawFile(const char* name);
208 WavWriter* GetWavFile(const char* name, int sample_rate_hz, int num_channels);
209#endif
210 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(ApmDataDumper);
211};
212
213} // namespace webrtc
214
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200215#endif // MODULES_AUDIO_PROCESSING_LOGGING_APM_DATA_DUMPER_H_