apm_data_dumper.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. #ifndef MODULES_AUDIO_PROCESSING_LOGGING_APM_DATA_DUMPER_H_
  11. #define MODULES_AUDIO_PROCESSING_LOGGING_APM_DATA_DUMPER_H_
  12. #include <stdint.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <string>
  16. #if WEBRTC_APM_DEBUG_DUMP == 1
  17. #include <memory>
  18. #include <unordered_map>
  19. #endif
  20. #include "api/array_view.h"
  21. #if WEBRTC_APM_DEBUG_DUMP == 1
  22. #include "common_audio/wav_file.h"
  23. #include "rtc_base/checks.h"
  24. #endif
  25. // Check to verify that the define is properly set.
  26. #if !defined(WEBRTC_APM_DEBUG_DUMP) || \
  27. (WEBRTC_APM_DEBUG_DUMP != 0 && WEBRTC_APM_DEBUG_DUMP != 1)
  28. #error "Set WEBRTC_APM_DEBUG_DUMP to either 0 or 1"
  29. #endif
  30. namespace webrtc {
  31. #if WEBRTC_APM_DEBUG_DUMP == 1
  32. // Functor used to use as a custom deleter in the map of file pointers to raw
  33. // files.
  34. struct RawFileCloseFunctor {
  35. void operator()(FILE* f) const { fclose(f); }
  36. };
  37. #endif
  38. // Class that handles dumping of variables into files.
  39. class ApmDataDumper {
  40. public:
  41. // Constructor that takes an instance index that may
  42. // be used to distinguish data dumped from different
  43. // instances of the code.
  44. explicit ApmDataDumper(int instance_index);
  45. ApmDataDumper() = delete;
  46. ApmDataDumper(const ApmDataDumper&) = delete;
  47. ApmDataDumper& operator=(const ApmDataDumper&) = delete;
  48. ~ApmDataDumper();
  49. // Activates or deactivate the dumping functionality.
  50. static void SetActivated(bool activated) {
  51. #if WEBRTC_APM_DEBUG_DUMP == 1
  52. recording_activated_ = activated;
  53. #endif
  54. }
  55. // Set an optional output directory.
  56. static void SetOutputDirectory(const std::string& output_dir) {
  57. #if WEBRTC_APM_DEBUG_DUMP == 1
  58. RTC_CHECK_LT(output_dir.size(), kOutputDirMaxLength);
  59. strncpy(output_dir_, output_dir.c_str(), output_dir.size());
  60. #endif
  61. }
  62. // Reinitializes the data dumping such that new versions
  63. // of all files being dumped to are created.
  64. void InitiateNewSetOfRecordings() {
  65. #if WEBRTC_APM_DEBUG_DUMP == 1
  66. ++recording_set_index_;
  67. #endif
  68. }
  69. // Methods for performing dumping of data of various types into
  70. // various formats.
  71. void DumpRaw(const char* name, double v) {
  72. #if WEBRTC_APM_DEBUG_DUMP == 1
  73. if (recording_activated_) {
  74. FILE* file = GetRawFile(name);
  75. fwrite(&v, sizeof(v), 1, file);
  76. }
  77. #endif
  78. }
  79. void DumpRaw(const char* name, size_t v_length, const double* v) {
  80. #if WEBRTC_APM_DEBUG_DUMP == 1
  81. if (recording_activated_) {
  82. FILE* file = GetRawFile(name);
  83. fwrite(v, sizeof(v[0]), v_length, file);
  84. }
  85. #endif
  86. }
  87. void DumpRaw(const char* name, rtc::ArrayView<const double> v) {
  88. #if WEBRTC_APM_DEBUG_DUMP == 1
  89. if (recording_activated_) {
  90. DumpRaw(name, v.size(), v.data());
  91. }
  92. #endif
  93. }
  94. void DumpRaw(const char* name, float v) {
  95. #if WEBRTC_APM_DEBUG_DUMP == 1
  96. if (recording_activated_) {
  97. FILE* file = GetRawFile(name);
  98. fwrite(&v, sizeof(v), 1, file);
  99. }
  100. #endif
  101. }
  102. void DumpRaw(const char* name, size_t v_length, const float* v) {
  103. #if WEBRTC_APM_DEBUG_DUMP == 1
  104. if (recording_activated_) {
  105. FILE* file = GetRawFile(name);
  106. fwrite(v, sizeof(v[0]), v_length, file);
  107. }
  108. #endif
  109. }
  110. void DumpRaw(const char* name, rtc::ArrayView<const float> v) {
  111. #if WEBRTC_APM_DEBUG_DUMP == 1
  112. if (recording_activated_) {
  113. DumpRaw(name, v.size(), v.data());
  114. }
  115. #endif
  116. }
  117. void DumpRaw(const char* name, bool v) {
  118. #if WEBRTC_APM_DEBUG_DUMP == 1
  119. if (recording_activated_) {
  120. DumpRaw(name, static_cast<int16_t>(v));
  121. }
  122. #endif
  123. }
  124. void DumpRaw(const char* name, size_t v_length, const bool* v) {
  125. #if WEBRTC_APM_DEBUG_DUMP == 1
  126. if (recording_activated_) {
  127. FILE* file = GetRawFile(name);
  128. for (size_t k = 0; k < v_length; ++k) {
  129. int16_t value = static_cast<int16_t>(v[k]);
  130. fwrite(&value, sizeof(value), 1, file);
  131. }
  132. }
  133. #endif
  134. }
  135. void DumpRaw(const char* name, rtc::ArrayView<const bool> v) {
  136. #if WEBRTC_APM_DEBUG_DUMP == 1
  137. if (recording_activated_) {
  138. DumpRaw(name, v.size(), v.data());
  139. }
  140. #endif
  141. }
  142. void DumpRaw(const char* name, int16_t v) {
  143. #if WEBRTC_APM_DEBUG_DUMP == 1
  144. if (recording_activated_) {
  145. FILE* file = GetRawFile(name);
  146. fwrite(&v, sizeof(v), 1, file);
  147. }
  148. #endif
  149. }
  150. void DumpRaw(const char* name, size_t v_length, const int16_t* v) {
  151. #if WEBRTC_APM_DEBUG_DUMP == 1
  152. if (recording_activated_) {
  153. FILE* file = GetRawFile(name);
  154. fwrite(v, sizeof(v[0]), v_length, file);
  155. }
  156. #endif
  157. }
  158. void DumpRaw(const char* name, rtc::ArrayView<const int16_t> v) {
  159. #if WEBRTC_APM_DEBUG_DUMP == 1
  160. if (recording_activated_) {
  161. DumpRaw(name, v.size(), v.data());
  162. }
  163. #endif
  164. }
  165. void DumpRaw(const char* name, int32_t v) {
  166. #if WEBRTC_APM_DEBUG_DUMP == 1
  167. if (recording_activated_) {
  168. FILE* file = GetRawFile(name);
  169. fwrite(&v, sizeof(v), 1, file);
  170. }
  171. #endif
  172. }
  173. void DumpRaw(const char* name, size_t v_length, const int32_t* v) {
  174. #if WEBRTC_APM_DEBUG_DUMP == 1
  175. if (recording_activated_) {
  176. FILE* file = GetRawFile(name);
  177. fwrite(v, sizeof(v[0]), v_length, file);
  178. }
  179. #endif
  180. }
  181. void DumpRaw(const char* name, size_t v) {
  182. #if WEBRTC_APM_DEBUG_DUMP == 1
  183. if (recording_activated_) {
  184. FILE* file = GetRawFile(name);
  185. fwrite(&v, sizeof(v), 1, file);
  186. }
  187. #endif
  188. }
  189. void DumpRaw(const char* name, size_t v_length, const size_t* v) {
  190. #if WEBRTC_APM_DEBUG_DUMP == 1
  191. if (recording_activated_) {
  192. FILE* file = GetRawFile(name);
  193. fwrite(v, sizeof(v[0]), v_length, file);
  194. }
  195. #endif
  196. }
  197. void DumpRaw(const char* name, rtc::ArrayView<const int32_t> v) {
  198. #if WEBRTC_APM_DEBUG_DUMP == 1
  199. if (recording_activated_) {
  200. DumpRaw(name, v.size(), v.data());
  201. }
  202. #endif
  203. }
  204. void DumpRaw(const char* name, rtc::ArrayView<const size_t> v) {
  205. #if WEBRTC_APM_DEBUG_DUMP == 1
  206. DumpRaw(name, v.size(), v.data());
  207. #endif
  208. }
  209. void DumpWav(const char* name,
  210. size_t v_length,
  211. const float* v,
  212. int sample_rate_hz,
  213. int num_channels) {
  214. #if WEBRTC_APM_DEBUG_DUMP == 1
  215. if (recording_activated_) {
  216. WavWriter* file = GetWavFile(name, sample_rate_hz, num_channels,
  217. WavFile::SampleFormat::kFloat);
  218. file->WriteSamples(v, v_length);
  219. }
  220. #endif
  221. }
  222. void DumpWav(const char* name,
  223. rtc::ArrayView<const float> v,
  224. int sample_rate_hz,
  225. int num_channels) {
  226. #if WEBRTC_APM_DEBUG_DUMP == 1
  227. if (recording_activated_) {
  228. DumpWav(name, v.size(), v.data(), sample_rate_hz, num_channels);
  229. }
  230. #endif
  231. }
  232. private:
  233. #if WEBRTC_APM_DEBUG_DUMP == 1
  234. static bool recording_activated_;
  235. static constexpr size_t kOutputDirMaxLength = 1024;
  236. static char output_dir_[kOutputDirMaxLength];
  237. const int instance_index_;
  238. int recording_set_index_ = 0;
  239. std::unordered_map<std::string, std::unique_ptr<FILE, RawFileCloseFunctor>>
  240. raw_files_;
  241. std::unordered_map<std::string, std::unique_ptr<WavWriter>> wav_files_;
  242. FILE* GetRawFile(const char* name);
  243. WavWriter* GetWavFile(const char* name,
  244. int sample_rate_hz,
  245. int num_channels,
  246. WavFile::SampleFormat format);
  247. #endif
  248. };
  249. } // namespace webrtc
  250. #endif // MODULES_AUDIO_PROCESSING_LOGGING_APM_DATA_DUMPER_H_