Ardour  9.8-93-g460ea8c013
lufs_meter.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016,2023 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 
29 
30 namespace ARDOUR {
31 
33 {
34 public:
35  LUFSMeter (double samplerate, uint32_t n_channels);
36  LUFSMeter (LUFSMeter const& other) = delete;
38 
39  void run (float const** data, uint32_t n_samples);
40  void reset ();
41 
42  float integrated_loudness () const;
43  float momentary () const;
44  float max_momentary () const;
45  float dbtp ();
46  float max_dbtp () const;
47 
48 private:
49  void init ();
50 
51  float process (float const** data, const uint32_t n_samples, uint32_t offset);
52  float sumfrag (uint32_t) const;
53 
54  void calc_true_peak (float const** data, const uint32_t n_samples);
55  float upsample_x4 (int chn, float const x);
56  float upsample_x2 (int chn, float const x);
57  std::function< float(int, const float) > upsample;
58 
59  const float _g[5] = { 1.0, 1.0, 1.0, 1.41, 1.41 };
60 
61  /* config */
62  double _samplerate;
63  uint32_t _n_channels;
64  uint32_t _n_fragment;
65 
66  /* filter coeff */
67  float _a0, _a1, _a2;
68  float _b1, _b2;
69  float _c3, _c4;
70 
71  /* state */
72  uint32_t _frag_pos;
73  float _frag_pwr;
74  uint32_t _block_cnt;
75  float _block_pwr;
76  float _power[8];
77  uint32_t _pow_idx;
78  float _thresh_rel;
79 
80  float _momentary_l;
81 
82  float _maxloudn_M;
83  float _integrated;
84  float _dbtp;
85  float _max_dbtp;
86 
87  std::atomic<bool> _rst_dbtp;
88 
89 #if defined(_MSC_VER)
90  typedef std::map<int, uint32_t> History;
91 #else
92  typedef std::map<int, uint32_t, std::less<int>, PBD::StackAllocator<std::pair<const int, uint32_t>, 1000>> History;
93 #endif
94 
96 
97  struct FilterState {
98  void reset ();
99  void sanitize ();
100 
101  float z1, z2, z3, z4;
102  };
103 
104  FilterState _fst[5];
105  float* _z[5];
106 };
107 
108 } // namespace ARDOUR
109 #endif
float integrated_loudness() const
uint32_t _n_channels
Definition: lufs_meter.h:63
float max_dbtp() const
uint32_t _n_fragment
Definition: lufs_meter.h:64
std::atomic< bool > _rst_dbtp
Definition: lufs_meter.h:87
float process(float const **data, const uint32_t n_samples, uint32_t offset)
float max_momentary() const
void calc_true_peak(float const **data, const uint32_t n_samples)
double _samplerate
Definition: lufs_meter.h:62
uint32_t _frag_pos
Definition: lufs_meter.h:72
float upsample_x2(int chn, float const x)
float momentary() const
std::map< int, uint32_t, std::less< int >, PBD::StackAllocator< std::pair< const int, uint32_t >, 1000 > > History
Definition: lufs_meter.h:92
LUFSMeter(LUFSMeter const &other)=delete
float sumfrag(uint32_t) const
uint32_t _block_cnt
Definition: lufs_meter.h:74
LUFSMeter(double samplerate, uint32_t n_channels)
uint32_t _pow_idx
Definition: lufs_meter.h:77
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:57
#define LIBARDOUR_API