peah | b46083e | 2016-05-03 07:01:18 -0700 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_processing/logging/apm_data_dumper.h" |
peah | b46083e | 2016-05-03 07:01:18 -0700 | [diff] [blame] | 12 | |
Per Åhgren | f0c449e | 2018-10-23 20:17:18 +0200 | [diff] [blame] | 13 | #include "rtc_base/strings/string_builder.h" |
| 14 | |
peah | b46083e | 2016-05-03 07:01:18 -0700 | [diff] [blame] | 15 | // Check to verify that the define is properly set. |
peah | f28a389 | 2016-09-01 08:58:21 -0700 | [diff] [blame] | 16 | #if !defined(WEBRTC_APM_DEBUG_DUMP) || \ |
| 17 | (WEBRTC_APM_DEBUG_DUMP != 0 && WEBRTC_APM_DEBUG_DUMP != 1) |
| 18 | #error "Set WEBRTC_APM_DEBUG_DUMP to either 0 or 1" |
peah | b46083e | 2016-05-03 07:01:18 -0700 | [diff] [blame] | 19 | #endif |
| 20 | |
| 21 | namespace webrtc { |
peah | b46083e | 2016-05-03 07:01:18 -0700 | [diff] [blame] | 22 | namespace { |
| 23 | |
peah | f28a389 | 2016-09-01 08:58:21 -0700 | [diff] [blame] | 24 | #if WEBRTC_APM_DEBUG_DUMP == 1 |
Alessio Bazzica | 4bc6045 | 2018-11-20 12:44:15 +0100 | [diff] [blame] | 25 | |
| 26 | #if defined(WEBRTC_WIN) |
| 27 | constexpr char kPathDelimiter = '\\'; |
| 28 | #else |
| 29 | constexpr char kPathDelimiter = '/'; |
| 30 | #endif |
| 31 | |
| 32 | std::string FormFileName(const char* output_dir, |
| 33 | const char* name, |
peah | b46083e | 2016-05-03 07:01:18 -0700 | [diff] [blame] | 34 | int instance_index, |
| 35 | int reinit_index, |
| 36 | const std::string& suffix) { |
Jonas Olsson | 18f151a | 2018-04-05 11:34:56 +0200 | [diff] [blame] | 37 | char buf[1024]; |
| 38 | rtc::SimpleStringBuilder ss(buf); |
Alessio Bazzica | 4bc6045 | 2018-11-20 12:44:15 +0100 | [diff] [blame] | 39 | const size_t output_dir_size = strlen(output_dir); |
| 40 | if (output_dir_size > 0) { |
| 41 | ss << output_dir; |
| 42 | if (output_dir[output_dir_size - 1] != kPathDelimiter) { |
| 43 | ss << kPathDelimiter; |
| 44 | } |
| 45 | } |
peah | b46083e | 2016-05-03 07:01:18 -0700 | [diff] [blame] | 46 | ss << name << "_" << instance_index << "-" << reinit_index << suffix; |
| 47 | return ss.str(); |
| 48 | } |
| 49 | #endif |
| 50 | |
| 51 | } // namespace |
| 52 | |
peah | f28a389 | 2016-09-01 08:58:21 -0700 | [diff] [blame] | 53 | #if WEBRTC_APM_DEBUG_DUMP == 1 |
peah | f8f754a | 2016-08-30 10:31:45 -0700 | [diff] [blame] | 54 | ApmDataDumper::ApmDataDumper(int instance_index) |
| 55 | : instance_index_(instance_index) {} |
| 56 | #else |
Alessio Bazzica | 4bc6045 | 2018-11-20 12:44:15 +0100 | [diff] [blame] | 57 | ApmDataDumper::ApmDataDumper(int instance_index){}; |
peah | f8f754a | 2016-08-30 10:31:45 -0700 | [diff] [blame] | 58 | #endif |
| 59 | |
Alessio Bazzica | 4bc6045 | 2018-11-20 12:44:15 +0100 | [diff] [blame] | 60 | ApmDataDumper::~ApmDataDumper() = default; |
peah | f8f754a | 2016-08-30 10:31:45 -0700 | [diff] [blame] | 61 | |
peah | f28a389 | 2016-09-01 08:58:21 -0700 | [diff] [blame] | 62 | #if WEBRTC_APM_DEBUG_DUMP == 1 |
Per Åhgren | 7a95e0f | 2018-10-25 09:56:49 +0200 | [diff] [blame] | 63 | bool ApmDataDumper::recording_activated_ = false; |
Alessio Bazzica | 4bc6045 | 2018-11-20 12:44:15 +0100 | [diff] [blame] | 64 | char ApmDataDumper::output_dir_[] = ""; |
| 65 | |
peah | b46083e | 2016-05-03 07:01:18 -0700 | [diff] [blame] | 66 | FILE* ApmDataDumper::GetRawFile(const char* name) { |
Alessio Bazzica | 4bc6045 | 2018-11-20 12:44:15 +0100 | [diff] [blame] | 67 | std::string filename = FormFileName(output_dir_, name, instance_index_, |
| 68 | recording_set_index_, ".dat"); |
peah | b46083e | 2016-05-03 07:01:18 -0700 | [diff] [blame] | 69 | auto& f = raw_files_[filename]; |
| 70 | if (!f) { |
| 71 | f.reset(fopen(filename.c_str(), "wb")); |
Alessio Bazzica | 4bc6045 | 2018-11-20 12:44:15 +0100 | [diff] [blame] | 72 | RTC_CHECK(f.get()) << "Cannot write to " << filename << "."; |
peah | b46083e | 2016-05-03 07:01:18 -0700 | [diff] [blame] | 73 | } |
| 74 | return f.get(); |
| 75 | } |
| 76 | |
| 77 | WavWriter* ApmDataDumper::GetWavFile(const char* name, |
| 78 | int sample_rate_hz, |
| 79 | int num_channels) { |
Alessio Bazzica | 4bc6045 | 2018-11-20 12:44:15 +0100 | [diff] [blame] | 80 | std::string filename = FormFileName(output_dir_, name, instance_index_, |
| 81 | recording_set_index_, ".wav"); |
peah | b46083e | 2016-05-03 07:01:18 -0700 | [diff] [blame] | 82 | auto& f = wav_files_[filename]; |
| 83 | if (!f) { |
| 84 | f.reset(new WavWriter(filename.c_str(), sample_rate_hz, num_channels)); |
| 85 | } |
| 86 | return f.get(); |
| 87 | } |
peah | b46083e | 2016-05-03 07:01:18 -0700 | [diff] [blame] | 88 | #endif |
| 89 | |
| 90 | } // namespace webrtc |