1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
/*
leqm-nrt is a non-real-time implementation
of Leq(M) measurement according to ISO 21727:2004(E)
"Cinematography -- Method of measurement of perceived
loudness of motion-picture audio material"
Copyright (C) 2011-2013, 2017-2018 Luca Trisciani
This program 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 3 of the License, or
(at your option) any later version.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <boost/asio.hpp>
#include <string>
#include <vector>
#include <functional>
#include <stdexcept>
#include <mutex>
#include <cmath>
#include <memory>
namespace leqm_nrt {
class Sum
{
public:
void sum_samples(std::vector<double> const& input_samples, std::vector<double> const& c_input_samples, int nsamples)
{
_mutex.lock();
_nsamples += nsamples;
for (auto i = 0; i < nsamples; i++) {
_sum += input_samples[i];
_csum += c_input_samples[i];
}
_mutex.unlock();
}
int nsamples() const
{
return _nsamples;
}
/*
How the final offset is calculated without reference to a test tone:
P0 is the SPL reference 20 uPa
Reference SPL is RMS ! So 85 SPL over 20 uPa is 10^4.25 x 0.000020 = 0.355655882 Pa (RMS),
but Peak value is 0.355655882 x sqr(2) = 0.502973372 that is 20 x log ( 0.502973372 / 0.000020) = 88.010299957
To that one has to add the 20 dB offset of the reference -20dBFS: 88.010299957 + 20.00 = 108.010299957
But ISO 21727:2004(E) ask for a reference level "measured using an average responding meter". So reference level is not 0.707, but 0.637 = 2/pi
*/
double rms() const
{
return 20 * log10(mean()) + 108.010299957;
}
double leqm() const
{
return 20 * log10(cmean()) + 108.010299957;
}
private:
double mean() const
{
return pow(_sum / _nsamples, 0.500);
}
double cmean() const
{
return pow(_csum / _nsamples, 0.500);
}
double _csum = 0.0; // convolved sum
double _sum = 0.0; // flat sum
int _nsamples = 0;
std::mutex _mutex;
};
struct Result
{
Result(int status_)
: status(status_)
{}
Result(double leq_m_, double leq_nw_)
: status(0)
, leq_m(leq_m_)
, leq_nw(leq_nw_)
{}
/** 0 on success, or
*
* -100: Either channel_corrections contained a different number of
* calibrations than; number of channels in the file or it was empty and the
* program cannot infer one from the number of channels. Please specify a
* values in channel_corrections.
*
* -101: Failed to open the sound file.
*
* -102: buffer_size_ms is not an integer number of samples at the sound file's rate.
*/
int status;
double leq_m;
double leq_nw;
};
#ifdef LEQM_NRT_WITH_LIBSNDFILE
Result calculate_file(
std::string sound_filename,
std::vector<double> channel_corrections,
int buffer_size_ms,
int number_of_filter_interpolation_points,
int num_cpu
);
#endif
double convert_log_to_linear_single(double in);
class BadBufferSizeError : public std::runtime_error
{
public:
BadBufferSizeError()
: std::runtime_error("Buffer size does not correspond to an integer number of samples")
{}
};
class BadChannelCorrectionsError : public std::runtime_error
{
public:
BadChannelCorrectionsError()
: std::runtime_error("Incorrect number of channel corrections given, and no defaults are available")
{}
};
class Calculator
{
public:
Calculator(
int channels,
int sample_rate,
int bits_per_sample,
std::vector<double> channel_corrections,
int buffer_size_ms,
int number_of_filter_interpolation_points,
int num_cpu
);
~Calculator()
{
_pool.join();
}
Calculator(Calculator&) = delete;
Calculator(Calculator&&) = delete;
bool operator=(Calculator&) = delete;
bool operator=(Calculator&&) = delete;
void add(std::vector<double> samples);
double leq_m();
double leq_nw();
private:
void process_buffer();
int _channels;
std::vector<double> _channel_corrections;
int _num_cpu;
boost::asio::thread_pool _pool;
Sum _sum;
std::vector<double> _ir;
std::vector<double> _buffer;
size_t _buffer_free_offset = 0;
};
}
|