Ardour  9.10-48-g0f45032f6d
lufs_meter.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016-2026 Robin Gareus <robin@gareus.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 #ifndef _lufs_meter_h_
19 #define _lufs_meter_h_
20 
21 #include <atomic>
22 #include <cstdint>
23 #include <functional>
24 #include <map>
25 
26 #include "pbd/stack_allocator.h"
27 #include "pbd/ringbuffer.h"
28 
30 
31 namespace ARDOUR {
32 
34 {
35 public:
36  LUFSMeter (double samplerate, uint32_t n_channels, bool with_hist = false);
37  LUFSMeter (LUFSMeter const& other) = delete;
39 
40  void run (float const** data, uint32_t n_samples);
41  void reset ();
42  void integrate (bool);
43  bool integrating () const;
44 
45  float integrated_loudness () const;
46  float momentary () const;
47  float max_momentary () const;
48  float short_term () const;
49  float max_short_term () const;
50  float lu_range () const;
51  void lu_range_min_max (float& range_min, float& range_max) const;
52  float dbtp ();
53  float max_dbtp () const;
54 
55  struct LUHistory {
56  float integrated;
57  float short_term;
58  float momentary;
59  };
60 
61  size_t hist_read (LUHistory* dest, size_t cnt) {
62  return _rb.read (dest, cnt);
63  }
64 
65  size_t hist_read_space () const {
66  return _rb.read_space ();
67  }
68 
69  void host_increment_read_idx (size_t cnt) {
70  _rb.increment_read_idx (cnt);
71  }
72 
73 private:
74  void init ();
75  void clear ();
76 
77  float process (float const** data, const uint32_t n_samples, uint32_t offset);
78  float sumfrag (uint32_t) const;
79 
80  void calc_true_peak (float const** data, const uint32_t n_samples);
81  float upsample_x4 (int chn, float const x);
82  float upsample_x2 (int chn, float const x);
83 
84  std::function< float(int, const float) > upsample;
85 
86  const float _g[5] = { 1.0, 1.0, 1.0, 1.41, 1.41 };
87 
88  /* config */
89  double _samplerate;
90  uint32_t _n_channels;
91  uint32_t _n_fragment;
92 
93  /* filter coeff */
94  float _a0, _a1, _a2;
95  float _b1, _b2;
96  float _c3, _c4;
97 
98  /* state */
99  uint32_t _frag_pos;
100  float _frag_pwr;
101  uint32_t _block_cnt;
102  float _block_pwr;
103  float _power[32];
104  uint32_t _pow_idx;
105  float _thresh_rel;
106  uint32_t _lu_s_div;
107 
109  float _short_l;
110 
111  float _range_min;
112  float _range_max;
113 
114  float _maxloudn_M;
115  float _maxloudn_S;
116  float _integrated;
117  float _dbtp;
118  float _max_dbtp;
119 
120  std::atomic<bool> _rst_dbtp;
121  std::atomic<bool> _rst_lufs;
122  std::atomic<bool> _integrate;
123 
124 #if defined(_MSC_VER)
125  typedef std::map<int, uint32_t> History;
126 #else
127  typedef std::map<int, uint32_t, std::less<int>, PBD::StackAllocator<std::pair<const int, uint32_t>, 1000>> History;
128 #endif
129 
132 
133  std::map<int, float> power_table;
134 
135  struct FilterState {
136  void reset ();
137  void sanitize ();
138 
139  float z1, z2, z3, z4;
140  };
141 
142  FilterState _fst[5];
143  float* _z[5];
144 
146 
148 };
149 
150 } // namespace ARDOUR
151 #endif
float integrated_loudness() const
uint32_t _n_channels
Definition: lufs_meter.h:90
float max_dbtp() const
uint32_t _n_fragment
Definition: lufs_meter.h:91
std::atomic< bool > _rst_dbtp
Definition: lufs_meter.h:120
size_t hist_read(LUHistory *dest, size_t cnt)
Definition: lufs_meter.h:61
PBD::RingBuffer< LUHistory > _rb
Definition: lufs_meter.h:147
float process(float const **data, const uint32_t n_samples, uint32_t offset)
void host_increment_read_idx(size_t cnt)
Definition: lufs_meter.h:69
uint32_t _lu_s_div
Definition: lufs_meter.h:106
float short_term() const
float max_momentary() const
void calc_true_peak(float const **data, const uint32_t n_samples)
double _samplerate
Definition: lufs_meter.h:89
bool integrating() const
uint32_t _frag_pos
Definition: lufs_meter.h:99
float max_short_term() const
float upsample_x2(int chn, float const x)
float momentary() const
void lu_range_min_max(float &range_min, float &range_max) const
size_t hist_read_space() const
Definition: lufs_meter.h:65
LUFSMeter(double samplerate, uint32_t n_channels, bool with_hist=false)
void integrate(bool)
std::map< int, uint32_t, std::less< int >, PBD::StackAllocator< std::pair< const int, uint32_t >, 1000 > > History
Definition: lufs_meter.h:127
LUFSMeter(LUFSMeter const &other)=delete
float sumfrag(uint32_t) const
std::map< int, float > power_table
Definition: lufs_meter.h:133
uint32_t _block_cnt
Definition: lufs_meter.h:101
std::atomic< bool > _integrate
Definition: lufs_meter.h:122
uint32_t _pow_idx
Definition: lufs_meter.h:104
std::atomic< bool > _rst_lufs
Definition: lufs_meter.h:121
float lu_range() const
float upsample_x4(int chn, float const x)
void run(float const **data, uint32_t n_samples)
std::function< float(int, const float) > upsample
Definition: lufs_meter.h:84
#define LIBARDOUR_API