-
Notifications
You must be signed in to change notification settings - Fork 0
/
foo_r128meter.cpp
295 lines (248 loc) · 10.3 KB
/
foo_r128meter.cpp
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include "stdafx.h"
DECLARE_COMPONENT_VERSION(
COMPONENT_NAME,
COMPONENT_VERSION,
"Measures and displays loudness according to EBU R 128\n"
"\n"
"Copyright (C) " COMPONENT_COPYRIGHT_YEAR " Holger Stenger\n"
"- https://github.com/stengerh/foo_r128meter\n"
"\n"
"libebur128\n"
"Copyright (C) 2011 Jan Kokemüller\n"
"- https://github.com/jiixyj/libebur128\n"
)
class r128meter {
private:
unsigned m_samplerate;
unsigned m_channel_count;
unsigned m_channel_config;
double m_segment_duration;
ebur128_state * m_state;
public:
r128meter() : m_samplerate(0), m_channel_count(0), m_channel_config(0), m_segment_duration(0.0), m_state(nullptr) {
}
~r128meter() {
if (m_state) {
ebur128_destroy(&m_state);
}
}
void add_chunk(audio_chunk *p_chunk) {
update_parameters(p_chunk);
int rval = ebur128_add_frames_float(m_state, p_chunk->get_data(), p_chunk->get_sample_count());
if (rval == EBUR128_SUCCESS) {
double duration = p_chunk->get_duration();
m_segment_duration += duration;
}
}
bool get_momentary_loudness(double *p_loudness, double *p_stable_in = nullptr) {
if (!m_state) return false;
int rval = ebur128_loudness_momentary(m_state, p_loudness);
if (p_stable_in) *p_stable_in = pfc::max_t(0.0, 0.4 - m_segment_duration);
return (rval == EBUR128_SUCCESS);
}
bool get_shortterm_loudness(double *p_loudness, double *p_stable_in = nullptr) {
if (!m_state) return false;
int rval = ebur128_loudness_shortterm(m_state, p_loudness);
if (p_stable_in) *p_stable_in = pfc::max_t(0.0, 3.0 - m_segment_duration);
if (rval != EBUR128_SUCCESS) {
switch (rval) {
case EBUR128_SUCCESS:
console::formatter() << "EBUR128_SUCCESS";
break;
case EBUR128_ERROR_NOMEM:
console::formatter() << "EBUR128_ERROR_NOMEM";
break;
case EBUR128_ERROR_INVALID_MODE:
console::formatter() << "EBUR128_ERROR_INVALID_MODE";
break;
case EBUR128_ERROR_INVALID_CHANNEL_INDEX:
console::formatter() << "EBUR128_ERROR_INVALID_CHANNEL_INDEX";
break;
case EBUR128_ERROR_NO_CHANGE:
console::formatter() << "EBUR128_ERROR_NO_CHANGE";
break;
default:
console::formatter() << rval;
break;
}
}
return (rval == EBUR128_SUCCESS);
}
bool get_loudness_range(double *p_loudness_range) {
if (!m_state) return false;
int rval = ebur128_loudness_range(m_state, p_loudness_range);
return (rval == EBUR128_SUCCESS);
}
private:
bool update_parameters(audio_chunk *p_chunk) {
unsigned int sample_rate = p_chunk->get_sample_rate();
unsigned int channel_count = p_chunk->get_channel_count();
unsigned int channel_config = p_chunk->get_channel_config();
if ( !m_state ) {
m_state = ebur128_init(channel_count, sample_rate, EBUR128_MODE_M | EBUR128_MODE_S);
if ( !m_state ) return false;
}
return change_parameters(sample_rate, channel_count, channel_config);
}
bool change_parameters(unsigned int p_sample_rate, unsigned int p_channel_count, unsigned int p_channel_config) {
int rval = ebur128_change_parameters( m_state, p_channel_count, p_sample_rate );
if ( rval != EBUR128_SUCCESS && rval != EBUR128_ERROR_NO_CHANGE ) return false;
for ( unsigned int channel_index = 0; channel_index < p_channel_count; channel_index++ )
{
int channel = EBUR128_UNUSED;
switch ( audio_chunk::g_extract_channel_flag( p_channel_config, channel_index ) )
{
case audio_chunk::channel_front_left: channel = EBUR128_LEFT; break;
case audio_chunk::channel_front_right: channel = EBUR128_RIGHT; break;
case audio_chunk::channel_front_center: channel = EBUR128_CENTER; break;
case audio_chunk::channel_back_left: channel = EBUR128_LEFT_SURROUND; break;
case audio_chunk::channel_back_right: channel = EBUR128_RIGHT_SURROUND; break;
}
rval = ebur128_set_channel( m_state, channel_index, channel );
if ( rval != 0 ) return false;
}
return true;
}
};
class r128meter_ui_element : public ui_element_instance, public CWindowImpl<r128meter_ui_element> {
protected:
ui_element_instance_callback::ptr m_callback;
visualisation_stream_v2::ptr m_stream;
double m_last_time;
r128meter m_meter;
CStatic m_label;
CBrush m_brushBackground;
public:
BEGIN_MSG_MAP_EX(r128meter_ui_element)
MSG_WM_CREATE(OnCreate)
MSG_WM_DESTROY(OnDestroy)
MSG_WM_TIMER(OnTimer)
MSG_WM_SIZE(OnSize)
MSG_WM_CTLCOLORSTATIC(OnCtlColorStatic)
END_MSG_MAP()
static GUID g_get_guid() {
// {364EB73D-7E70-4DF3-B8D3-31F46B882FF5}
static const GUID r128meter_ui_element_guid =
{ 0x364eb73d, 0x7e70, 0x4df3, { 0xb8, 0xd3, 0x31, 0xf4, 0x6b, 0x88, 0x2f, 0xf5 } };
return r128meter_ui_element_guid;
}
static GUID g_get_subclass() {
return ui_element_subclass_playback_visualisation;
}
static void g_get_name(pfc::string_base & p_name) {
p_name.set_string("R128 Meter");
}
static const char * g_get_description() {
return "Measures loudness according to EBU R 128";
}
static ui_element_config::ptr g_get_default_configuration() {
ui_element_config_builder builder;
return builder.finish(g_get_guid());
}
r128meter_ui_element(ui_element_config::ptr p_config, ui_element_instance_callback::ptr p_callback) : m_callback(p_callback), m_last_time(0.0) {
set_configuration(p_config);
}
void initialize_window(HWND p_parent) {
Create(p_parent);
}
virtual HWND get_wnd() {
return NULL;
}
virtual void set_configuration(ui_element_config::ptr p_config) {
}
virtual ui_element_config::ptr get_configuration() {
return g_get_default_configuration();
}
virtual GUID get_guid() {
return g_get_guid();
}
virtual GUID get_subclass() {
return g_get_subclass();
}
virtual void notify(const GUID & p_what, t_size p_param1, const void * p_param2, t_size p_param2size) {
if (p_what == ui_element_notify_colors_changed) {
m_brushBackground = nullptr;
m_label.RedrawWindow();
} else if (p_what == ui_element_notify_font_changed) {
m_label.SetFont(m_callback->query_font_ex(ui_font_default));
}
}
enum {
ID_TIMER_UPDATE = 1
};
int OnCreate(LPCREATESTRUCT lpCreateStruct) {
visualisation_manager::ptr manager = standard_api_create_t<visualisation_manager>();
manager->create_stream(m_stream, 0);
m_stream->request_backlog(1.0);
m_stream->set_channel_mode(visualisation_stream_v2::channel_mode_default);
SetTimer(ID_TIMER_UPDATE, 100);
m_label.Create(*this, 0, TEXT("R128 Meter"), WS_CHILD | WS_VISIBLE | SS_LEFTNOWORDWRAP | SS_NOPREFIX);
notify(ui_element_notify_colors_changed, 0, nullptr, 0);
notify(ui_element_notify_font_changed, 0, nullptr, 0);
return 0;
}
void OnDestroy() {
KillTimer(ID_TIMER_UPDATE);
m_stream.release();
}
void OnTimer(UINT_PTR nIDEvent) {
switch (nIDEvent) {
case ID_TIMER_UPDATE:
{
double time;
if (m_stream->get_absolute_time(time)) {
if (time < m_last_time) {
m_last_time = 0.0;
}
if (time > m_last_time) {
audio_chunk_impl chunk;
if (m_stream->get_chunk_absolute(chunk, m_last_time, time - m_last_time)) {
m_meter.add_chunk(&chunk);
pfc::string_formatter formatter;
double loudness;
double stable_in;
if (m_meter.get_momentary_loudness(&loudness, &stable_in)) {
formatter << "momentary loudness: " << pfc::format_float(loudness, 0, 1) << " LUFS";
if (stable_in > 0.0) {
formatter << " (stable in " << pfc::format_float(ceil(stable_in), 0, 0) << " s)";
}
formatter << "\r\n";
}
if (m_meter.get_shortterm_loudness(&loudness, &stable_in)) {
formatter << "short-term loudness: " << pfc::format_float(loudness, 0, 1) << " LUFS";
if (stable_in > 0.0) {
formatter << " (stable in " << pfc::format_float(ceil(stable_in), 0, 0) << " s)";
}
formatter << "\r\n";
}
m_label.SetWindowText(pfc::stringcvt::string_os_from_utf8(formatter));
} else {
console::formatter() << "R128 Meter: no chunk available, time = " << time << ", last time = " << m_last_time;
}
}
m_last_time = time;
}
}
break;
default:
SetMsgHandled(FALSE);
break;
}
}
void OnSize(UINT nType, CSize size) {
m_label.SetWindowPos(nullptr, 0, 0, size.cx, size.cy, SWP_NOZORDER);
}
HBRUSH OnCtlColorStatic(CDCHandle dc, CStatic wndStatic) {
t_ui_color color_text = 0x000000;
t_ui_color color_background = 0xffffff;
m_callback->query_color(ui_color_text, color_text);
m_callback->query_color(ui_color_background, color_background);
dc.SetTextColor(color_text);
dc.SetBkColor(color_background);
if (m_brushBackground.IsNull()) {
m_brushBackground.CreateSolidBrush(color_background);
}
return m_brushBackground;
}
};
static service_factory_single_t< ui_element_impl_withpopup< r128meter_ui_element > > g_r128meter_ui_element_factory;