diff --git a/modules/audio_processing/test/py_quality_assessment/BUILD.gn b/modules/audio_processing/test/py_quality_assessment/BUILD.gn index a23151c010..45e5f30760 100644 --- a/modules/audio_processing/test/py_quality_assessment/BUILD.gn +++ b/modules/audio_processing/test/py_quality_assessment/BUILD.gn @@ -146,6 +146,19 @@ rtc_executable("apm_vad") { ] } +rtc_executable("sound_level") { + sources = [ + "quality_assessment/sound_level.cc", + ] + deps = [ + "../..", + "../../../..:webrtc_common", + "../../../../common_audio", + "../../../../rtc_base:rtc_base_approved", + "../../../../system_wrappers:metrics_default", + ] +} + copy("lib_unit_tests") { testonly = true sources = [ diff --git a/modules/audio_processing/test/py_quality_assessment/quality_assessment/sound_level.cc b/modules/audio_processing/test/py_quality_assessment/quality_assessment/sound_level.cc new file mode 100644 index 0000000000..8d2ef2b334 --- /dev/null +++ b/modules/audio_processing/test/py_quality_assessment/quality_assessment/sound_level.cc @@ -0,0 +1,124 @@ +// Copyright (c) 2018 The WebRTC project authors. All Rights Reserved. +// +// Use of this source code is governed by a BSD-style license +// that can be found in the LICENSE file in the root of the source +// tree. An additional intellectual property rights grant can be found +// in the file PATENTS. All contributing project authors may +// be found in the AUTHORS file in the root of the source tree. + +#include +#include +#include +#include + +#include "common_audio/wav_file.h" +#include "rtc_base/flags.h" +#include "rtc_base/logging.h" + +namespace webrtc { +namespace test { +namespace { + +constexpr int kMaxSampleRate = 48000; +constexpr uint8_t kMaxFrameLenMs = 30; +constexpr size_t kMaxFrameLen = kMaxFrameLenMs * kMaxSampleRate / 1000; + +const double kOneDbReduction = std::pow(10.0, -1.0 / 20.0); + +DEFINE_string(i, "", "Input wav file"); +DEFINE_string(oc, "", "Config output file"); +DEFINE_string(ol, "", "Levels output file"); +DEFINE_float(a, 5.f, "Attack (ms)"); +DEFINE_float(d, 20.f, "Decay (ms)"); +DEFINE_int(f, 10, "Frame length (ms)"); +DEFINE_bool(help, false, "prints this message"); + +int main(int argc, char* argv[]) { + if (rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, true)) { + rtc::FlagList::Print(nullptr, false); + return 1; + } + if (FLAG_help) { + rtc::FlagList::Print(nullptr, false); + return 0; + } + + // Check parameters. + if (FLAG_f < 1 || FLAG_f > kMaxFrameLenMs) { + RTC_LOG(LS_ERROR) << "Invalid frame length (min: 1, max: " << kMaxFrameLenMs + << ")"; + return 1; + } + if (FLAG_a < 0 || FLAG_d < 0) { + RTC_LOG(LS_ERROR) << "Attack and decay must be non-negative"; + return 1; + } + + // Open wav input file and check properties. + WavReader wav_reader(FLAG_i); + if (wav_reader.num_channels() != 1) { + RTC_LOG(LS_ERROR) << "Only mono wav files supported"; + return 1; + } + if (wav_reader.sample_rate() > kMaxSampleRate) { + RTC_LOG(LS_ERROR) << "Beyond maximum sample rate (" << kMaxSampleRate + << ")"; + return 1; + } + + // Map from milliseconds to samples. + const size_t audio_frame_length = + rtc::CheckedDivExact(FLAG_f * wav_reader.sample_rate(), 1000); + auto time_const = [](double c) { + return std::pow(kOneDbReduction, FLAG_f / c); + }; + const float attack = FLAG_a == 0.0 ? 0.0 : time_const(FLAG_a); + const float decay = FLAG_d == 0.0 ? 0.0 : time_const(FLAG_d); + + // Write config to file. + std::ofstream out_config(FLAG_oc); + out_config << "{" + << "'frame_len_ms': " << FLAG_f << ", " + << "'attack_ms': " << FLAG_a << ", " + << "'decay_ms': " << FLAG_d << "}\n"; + out_config.close(); + + // Measure level frame-by-frame. + std::ofstream out_levels(FLAG_ol, std::ofstream::binary); + std::array samples; + float level_prev = 0.f; + while (true) { + // Process frame. + const auto read_samples = + wav_reader.ReadSamples(audio_frame_length, samples.data()); + if (read_samples < audio_frame_length) + break; // EOF. + + // Frame peak level. + std::transform(samples.begin(), samples.begin() + audio_frame_length, + samples.begin(), [](int16_t s) { return std::abs(s); }); + const auto* peak_level = + std::max_element(samples.begin(), samples.begin() + audio_frame_length); + const float level_curr = static_cast(*peak_level) / 32768.f; + + // Temporal smoothing. + auto smooth = [&level_prev, &level_curr](float c) { + return (1.0 - c) * level_curr + c * level_prev; + }; + level_prev = smooth(level_curr > level_prev ? attack : decay); + + // Write output. + out_levels.write(reinterpret_cast(&level_prev), sizeof(float)); + } + out_levels.close(); + + return 0; +} + +} // namespace +} // namespace test +} // namespace webrtc + +int main(int argc, char* argv[]) { + return webrtc::test::main(argc, argv); +}