summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2025-04-18 22:07:10 +0200
committerCarl Hetherington <cth@carlh.net>2025-04-20 21:18:19 +0200
commit7c0d335f918821038d957bfe1bfacb7bfb7e227d (patch)
treeaa1562dedf98c016a3d8f40a4fa2cc4bd6fc6357
parenta1976ec37c923e4760584d3e4b178300e8e6d6d2 (diff)
add fall-off
-rw-r--r--src/lib/level_calculator.cc18
-rw-r--r--src/lib/level_calculator.h8
-rw-r--r--test/level_calculator_test.cc43
-rw-r--r--test/wscript1
4 files changed, 61 insertions, 9 deletions
diff --git a/src/lib/level_calculator.cc b/src/lib/level_calculator.cc
index 0563d4ed8..8ec5684f2 100644
--- a/src/lib/level_calculator.cc
+++ b/src/lib/level_calculator.cc
@@ -28,11 +28,10 @@ using std::shared_ptr;
using boost::optional;
-constexpr int frames_per_measurement = 48000 / 30;
-
-
-LevelCalculator::LevelCalculator()
- : _enabled(false)
+LevelCalculator::LevelCalculator(int calculation_frame_rate, int falloff)
+ : _calculation_frame_rate(calculation_frame_rate)
+ , _falloff_linear(pow(10, -falloff / (calculation_frame_rate * 20.0f)))
+ , _enabled(false)
{
}
@@ -42,10 +41,11 @@ void
LevelCalculator::put(shared_ptr<const AudioBuffers> audio, dcpomatic::DCPTime time, int frame_rate)
{
if (!_enabled) {
- std::cout << "no calcs for me.\n";
return;
}
+ int const frames_per_measurement = frame_rate / _calculation_frame_rate;
+
boost::mutex::scoped_lock lm(_current_mutex);
auto const channels = audio->channels();
@@ -65,9 +65,11 @@ LevelCalculator::put(shared_ptr<const AudioBuffers> audio, dcpomatic::DCPTime ti
if (_current_frames == frames_per_measurement) {
{
boost::mutex::scoped_lock lm(_store_mutex);
- _peaks.emplace_back(time + dcpomatic::DCPTime::from_frames(frame, frame_rate), _current_peaks);
+ _peaks.emplace_back(time + dcpomatic::DCPTime::from_frames(frame + 1, frame_rate), _current_peaks);
+ }
+ for (auto channel = 0; channel < channels; ++channel) {
+ _current_peaks[channel] *= _falloff_linear;
}
- std::fill(_current_peaks.begin(), _current_peaks.end(), 0.0f);
_current_frames = 0;
}
}
diff --git a/src/lib/level_calculator.h b/src/lib/level_calculator.h
index 9eeb5b9b8..e4d571cd4 100644
--- a/src/lib/level_calculator.h
+++ b/src/lib/level_calculator.h
@@ -34,7 +34,10 @@ class AudioBuffers;
class LevelCalculator
{
public:
- LevelCalculator();
+ /** @param calculation_frame_rate number of peak calculations to make per second
+ * @param falloff meter falloff in db/sec
+ */
+ LevelCalculator(int calculation_frame_rate = 30, int falloff = 15);
LevelCalculator(LevelCalculator const&) = delete;
LevelCalculator& operator=(LevelCalculator const&) = delete;
@@ -46,6 +49,9 @@ public:
void enable(bool e);
private:
+ int const _calculation_frame_rate;
+ float const _falloff_linear;
+
boost::mutex _current_mutex;
std::vector<float> _current_peaks;
int _current_frames = 0;
diff --git a/test/level_calculator_test.cc b/test/level_calculator_test.cc
new file mode 100644
index 000000000..828f834b4
--- /dev/null
+++ b/test/level_calculator_test.cc
@@ -0,0 +1,43 @@
+/*
+ Copyright (C) 2025 Carl Hetherington <cth@carlh.net>
+
+ This file is part of DCP-o-matic.
+
+ DCP-o-matic is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ DCP-o-matic is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+
+#include "lib/audio_buffers.h"
+#include "lib/level_calculator.h"
+#include <boost/test/unit_test.hpp>
+
+
+using std::make_shared;
+
+
+BOOST_AUTO_TEST_CASE(level_calculator_falloff_correct)
+{
+ auto pulse = make_shared<AudioBuffers>(6, 96000);
+ pulse->make_silent();
+
+ pulse->data()[0][0] = 1;
+
+ LevelCalculator calc(30, 15);
+ calc.enable(true);
+ calc.put(pulse, {}, 48000);
+
+ BOOST_CHECK_CLOSE(calc.get(dcpomatic::DCPTime::from_frames(31, 30))[0], pow(10, -15.0 / 20), 0.0001);
+}
+
diff --git a/test/wscript b/test/wscript
index b568872a3..b9eaad41f 100644
--- a/test/wscript
+++ b/test/wscript
@@ -128,6 +128,7 @@ def build(bld):
kdm_cli_test.cc
kdm_naming_test.cc
kdm_util_test.cc
+ level_calculator_test.cc
low_bitrate_test.cc
markers_test.cc
map_cli_test.cc