1 // Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
2 //
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the LICENSE file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS.  All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
8 
9 #include <algorithm>
10 #include <array>
11 #include <cmath>
12 #include <fstream>
13 
14 #include "absl/flags/flag.h"
15 #include "absl/flags/parse.h"
16 #include "common_audio/include/audio_util.h"
17 #include "common_audio/wav_file.h"
18 #include "rtc_base/logging.h"
19 
20 ABSL_FLAG(std::string, i, "", "Input wav file");
21 ABSL_FLAG(std::string, oc, "", "Config output file");
22 ABSL_FLAG(std::string, ol, "", "Levels output file");
23 ABSL_FLAG(float, a, 5.f, "Attack (ms)");
24 ABSL_FLAG(float, d, 20.f, "Decay (ms)");
25 ABSL_FLAG(int, f, 10, "Frame length (ms)");
26 
27 namespace webrtc {
28 namespace test {
29 namespace {
30 
31 constexpr int kMaxSampleRate = 48000;
32 constexpr uint8_t kMaxFrameLenMs = 30;
33 constexpr size_t kMaxFrameLen = kMaxFrameLenMs * kMaxSampleRate / 1000;
34 
35 const double kOneDbReduction = DbToRatio(-1.0);
36 
main(int argc,char * argv[])37 int main(int argc, char* argv[]) {
38   absl::ParseCommandLine(argc, argv);
39   // Check parameters.
40   if (absl::GetFlag(FLAGS_f) < 1 || absl::GetFlag(FLAGS_f) > kMaxFrameLenMs) {
41     RTC_LOG(LS_ERROR) << "Invalid frame length (min: 1, max: " << kMaxFrameLenMs
42                       << ")";
43     return 1;
44   }
45   if (absl::GetFlag(FLAGS_a) < 0 || absl::GetFlag(FLAGS_d) < 0) {
46     RTC_LOG(LS_ERROR) << "Attack and decay must be non-negative";
47     return 1;
48   }
49 
50   // Open wav input file and check properties.
51   const std::string input_file = absl::GetFlag(FLAGS_i);
52   const std::string config_output_file = absl::GetFlag(FLAGS_oc);
53   const std::string levels_output_file = absl::GetFlag(FLAGS_ol);
54   WavReader wav_reader(input_file);
55   if (wav_reader.num_channels() != 1) {
56     RTC_LOG(LS_ERROR) << "Only mono wav files supported";
57     return 1;
58   }
59   if (wav_reader.sample_rate() > kMaxSampleRate) {
60     RTC_LOG(LS_ERROR) << "Beyond maximum sample rate (" << kMaxSampleRate
61                       << ")";
62     return 1;
63   }
64 
65   // Map from milliseconds to samples.
66   const size_t audio_frame_length = rtc::CheckedDivExact(
67       absl::GetFlag(FLAGS_f) * wav_reader.sample_rate(), 1000);
68   auto time_const = [](double c) {
69     return std::pow(kOneDbReduction, absl::GetFlag(FLAGS_f) / c);
70   };
71   const float attack =
72       absl::GetFlag(FLAGS_a) == 0.0 ? 0.0 : time_const(absl::GetFlag(FLAGS_a));
73   const float decay =
74       absl::GetFlag(FLAGS_d) == 0.0 ? 0.0 : time_const(absl::GetFlag(FLAGS_d));
75 
76   // Write config to file.
77   std::ofstream out_config(config_output_file);
78   out_config << "{"
79                 "'frame_len_ms': "
80              << absl::GetFlag(FLAGS_f)
81              << ", "
82                 "'attack_ms': "
83              << absl::GetFlag(FLAGS_a)
84              << ", "
85                 "'decay_ms': "
86              << absl::GetFlag(FLAGS_d) << "}\n";
87   out_config.close();
88 
89   // Measure level frame-by-frame.
90   std::ofstream out_levels(levels_output_file, std::ofstream::binary);
91   std::array<int16_t, kMaxFrameLen> samples;
92   float level_prev = 0.f;
93   while (true) {
94     // Process frame.
95     const auto read_samples =
96         wav_reader.ReadSamples(audio_frame_length, samples.data());
97     if (read_samples < audio_frame_length)
98       break;  // EOF.
99 
100     // Frame peak level.
101     std::transform(samples.begin(), samples.begin() + audio_frame_length,
102                    samples.begin(), [](int16_t s) { return std::abs(s); });
103     const int16_t peak_level = *std::max_element(
104         samples.cbegin(), samples.cbegin() + audio_frame_length);
105     const float level_curr = static_cast<float>(peak_level) / 32768.f;
106 
107     // Temporal smoothing.
108     auto smooth = [&level_prev, &level_curr](float c) {
109       return (1.0 - c) * level_curr + c * level_prev;
110     };
111     level_prev = smooth(level_curr > level_prev ? attack : decay);
112 
113     // Write output.
114     out_levels.write(reinterpret_cast<const char*>(&level_prev), sizeof(float));
115   }
116   out_levels.close();
117 
118   return 0;
119 }
120 
121 }  // namespace
122 }  // namespace test
123 }  // namespace webrtc
124 
main(int argc,char * argv[])125 int main(int argc, char* argv[]) {
126   return webrtc::test::main(argc, argv);
127 }
128