Debian Codegraph @

modules/lsp-plugins-beat-breather/src/main/plug/beat_breather.cpp

raw
1 /*
2 * Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2025 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-plugins-beat-breather
6 * Created on: 14 авг 2023 г.
7 *
8 * lsp-plugins-beat-breather is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-plugins-beat-breather is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-plugins-beat-breather. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <lsp-plug.in/common/alloc.h>
23 #include <lsp-plug.in/common/bits.h>
24 #include <lsp-plug.in/common/debug.h>
25 #include <lsp-plug.in/dsp/dsp.h>
26 #include <lsp-plug.in/dsp-units/misc/envelope.h>
27 #include <lsp-plug.in/dsp-units/units.h>
28 #include <lsp-plug.in/plug-fw/meta/func.h>
29 #include <lsp-plug.in/shared/debug.h>
30 #include <lsp-plug.in/shared/id_colors.h>
31
32 #include <private/plugins/beat_breather.h>
33
34 namespace lsp
35 {
36 namespace plugins
37 {
38 /* The size of temporary buffer for audio processing */
39 static constexpr size_t BUFFER_SIZE = 0x1000;
40
41 //---------------------------------------------------------------------
42 // Plugin factory
43 static const meta::plugin_t *plugins[] =
44 {
45 &meta::beat_breather_mono,
46 &meta::beat_breather_stereo
47 };
48
49 static plug::Module *plugin_factory(const meta::plugin_t *meta)
50 {
51 return new beat_breather(meta);
52 }
53
54 static plug::Factory factory(plugin_factory, plugins, 2);
55
56 //---------------------------------------------------------------------
57 // Implementation
58 beat_breather::beat_breather(const meta::plugin_t *meta):
59 Module(meta)
60 {
61 nChannels = 1;
62 if (!strcmp(meta->uid, meta::beat_breather_stereo.uid))
63 nChannels = 2;
64
65 vChannels = NULL;
66 bStereoSplit = false;
67 fInGain = GAIN_AMP_0_DB;
68 fDryGain = GAIN_AMP_M_INF_DB;
69 fWetGain = GAIN_AMP_0_DB;
70 fZoom = GAIN_AMP_0_DB;
71
72 vAnalyze[0] = NULL;
73 vAnalyze[1] = NULL;
74 vAnalyze[2] = NULL;
75 vAnalyze[3] = NULL;
76
77 for (size_t i=0; i<meta::beat_breather::BANDS_MAX-1; ++i)
78 {
79 split_t *s = &vSplits[i];
80
81 s->nBandId = i + 1;
82 s->fFrequency = 0.0f;
83 s->bEnabled = false;
84
85 s->pEnable = NULL;
86 s->pFrequency = NULL;
87 }
88
89 vBuffer = NULL;
90 vFftFreqs = NULL;
91 vFftIndexes = NULL;
92 vPdMesh = NULL;
93 vPfMesh = NULL;
94 vBpMesh = NULL;
95
96 pBypass = NULL;
97 pInGain = NULL;
98 pDryGain = NULL;
99 pWetGain = NULL;
100 pDryWet = NULL;
101 pOutGain = NULL;
102 pStereoSplit = NULL;
103 pFFTReactivity = NULL;
104 pFFTShift = NULL;
105 pZoom = NULL;
106
107 pIDisplay = NULL;
108
109 pData = NULL;
110 }
111
112 beat_breather::~beat_breather()
113 {
114 do_destroy();
115 }
116
117 void beat_breather::destroy()
118 {
119 Module::destroy();
120 do_destroy();
121 }
122
123 void beat_breather::do_destroy()
124 {
125 // Destroy channels
126 if (vChannels != NULL)
127 {
128 for (size_t i=0; i<nChannels; ++i)
129 {
130 channel_t *c = &vChannels[i];
131
132 c->sBypass.destroy();
133 c->sCrossover.destroy();
134 c->sDelay.destroy();
135 c->sDryDelay.destroy();
136
137 for (size_t j=0; j<meta::beat_breather::BANDS_MAX; ++j)
138 {
139 band_t *b = &c->vBands[j];
140
141 b->sDelay.destroy();
142 b->sPdLong.destroy();
143 b->sPdShort.destroy();
144 b->sPdDelay.destroy();
145 b->sPdMeter.destroy();
146 b->sPf.destroy();
147 b->sPfDelay.destroy();
148 b->sBp.destroy();
149 b->sBpScDelay.destroy();
150 b->sBpDelay.destroy();
151 }
152 }
153 vChannels = NULL;
154 }
155
156 // Destroy inline display buffer
157 if (pIDisplay != NULL)
158 {
159 pIDisplay->destroy();
160 pIDisplay = NULL;
161 }
162
163 // Free previously allocated data chunk
164 if (pData != NULL)
165 {
166 free_aligned(pData);
167 pData = NULL;
168 }
169 }
170
171 void beat_breather::init(plug::IWrapper *wrapper, plug::IPort **ports)
172 {
173 // Call parent class for initialization
174 Module::init(wrapper, ports);
175
176 // Initialize analyzer
177 size_t an_cid = 0;
178 if (!sAnalyzer.init(2*nChannels, meta::beat_breather::FFT_ANALYZER_RANK,
179 MAX_SAMPLE_RATE, meta::beat_breather::FFT_ANALYZER_RATE))
180 return;
181
182 sAnalyzer.set_rank(meta::beat_breather::FFT_ANALYZER_RANK);
183 sAnalyzer.set_activity(false);
184 sAnalyzer.set_envelope(dspu::envelope::WHITE_NOISE);
185 sAnalyzer.set_window(meta::beat_breather::FFT_ANALYZER_WINDOW);
186 sAnalyzer.set_rate(meta::beat_breather::FFT_ANALYZER_RATE);
187
188 sCounter.set_frequency(meta::beat_breather::FFT_ANALYZER_RATE, true);
189
190 // Allocate data
191 const size_t szof_channels = align_size(sizeof(channel_t) * nChannels, DEFAULT_ALIGN);
192 const size_t szof_buffer = align_size(sizeof(float) * BUFFER_SIZE, DEFAULT_ALIGN);
193 const size_t szof_fft = align_size(sizeof(float) * meta::beat_breather::FFT_MESH_POINTS, DEFAULT_ALIGN);
194 const size_t szof_ffti = align_size(sizeof(uint32_t) * meta::beat_breather::FFT_MESH_POINTS, DEFAULT_ALIGN);
195 const size_t szof_curve = align_size(sizeof(float) * meta::beat_breather::CURVE_MESH_POINTS, DEFAULT_ALIGN);
196 const size_t szof_time = align_size(sizeof(float) * meta::beat_breather::TIME_MESH_POINTS, DEFAULT_ALIGN);
197 const size_t to_alloc =
198 szof_channels + // vChannels
199 szof_buffer + // vBuffer
200 szof_fft + // vFftFreqs
201 szof_ffti + // vFftIndexes
202 szof_time + // vPdMesh
203 szof_curve + // vPfMesh
204 szof_curve + // vBpMesh
205 nChannels * (
206 szof_buffer + // channel_t::vInData
207 szof_buffer + // channel_t::vOutData
208 szof_fft + // channel_t::vFreqChart
209 meta::beat_breather::BANDS_MAX * (
210 szof_buffer + // band_t::vInData
211 szof_buffer + // band_t::vPdData
212 szof_buffer + // band_t::vPfData
213 szof_buffer // band_t::vBpData
214 )
215 ) +
216 meta::beat_breather::BANDS_MAX * (
217 szof_fft + // band_t::vFreqChart (only for left channel)
218 szof_curve + // band_t::vPfMesh (only for left channel)
219 szof_curve // band_t::vBpMesh (only for left channel)
220 );
221
222 uint8_t *ptr = alloc_aligned<uint8_t>(pData, to_alloc);
223 if (ptr == NULL)
224 return;
225 lsp_guard_assert(uint8_t *ptr_check = &ptr[to_alloc]);
226
227 vChannels = advance_ptr_bytes<channel_t>(ptr, szof_channels);
228 vBuffer = advance_ptr_bytes<float>(ptr, szof_buffer);
229 vFftFreqs = advance_ptr_bytes<float>(ptr, szof_fft);
230 vFftIndexes = advance_ptr_bytes<uint32_t>(ptr, szof_ffti);
231 vPdMesh = advance_ptr_bytes<float>(ptr, szof_time);
232 vPfMesh = advance_ptr_bytes<float>(ptr, szof_curve);
233 vBpMesh = advance_ptr_bytes<float>(ptr, szof_curve);
234
235 // Initialize channels
236 for (size_t i=0; i<nChannels; ++i)
237 {
238 channel_t *c = &vChannels[i];
239
240 c->sBypass.construct();
241 c->sCrossover.construct();
242 c->sDelay.construct();
243 c->sDryDelay.construct();
244
245 for (size_t j=0; j<meta::beat_breather::BANDS_MAX; ++j)
246 {
247 band_t *b = &c->vBands[j];
248
249 b->sDelay.construct();
250 b->sPdLong.construct();
251 if (!b->sPdLong.init(1, meta::beat_breather::PD_LONG_RMS_MAX))
252 return;
253 b->sPdShort.construct();
254 if (!b->sPdShort.init(1, meta::beat_breather::PD_SHORT_RMS_MAX))
255 return;
256 b->sPdDelay.construct();
257 b->sPdMeter.construct();
258 b->sPf.construct();
259 b->sPfDelay.construct();
260 b->sBp.construct();
261 b->sBpScDelay.construct();
262 b->sBpDelay.construct();
263
264 b->nOldMode = BAND_OFF;
265 b->nMode = BAND_OFF;
266 b->fGain = GAIN_AMP_0_DB;
267 b->fInLevel = GAIN_AMP_M_INF_DB;
268 b->fOutLevel = GAIN_AMP_M_INF_DB;
269 b->fReduction = GAIN_AMP_0_DB;
270 b->nSync = SYNC_ALL;
271 b->fPdMakeup = GAIN_AMP_0_DB;
272 b->fPdLevel = GAIN_AMP_M_INF_DB;
273 b->fPfInGain = GAIN_AMP_M_INF_DB;
274 b->fPfOutGain = GAIN_AMP_M_INF_DB;
275 b->fPfReduction = GAIN_AMP_M_INF_DB;
276 b->fBpMakeup = GAIN_AMP_0_DB;
277 b->fBpInGain = GAIN_AMP_M_INF_DB;
278 b->fBpOutGain = GAIN_AMP_M_INF_DB;
279 b->fBpReduction = GAIN_AMP_M_INF_DB;
280
281 b->vInData = advance_ptr_bytes<float>(ptr, szof_buffer);
282 b->vPdData = advance_ptr_bytes<float>(ptr, szof_buffer);
283 b->vPfData = advance_ptr_bytes<float>(ptr, szof_buffer);
284 b->vBpData = advance_ptr_bytes<float>(ptr, szof_buffer);
285 if (i == 0)
286 {
287 b->vFreqChart = advance_ptr_bytes<float>(ptr, szof_fft);
288 b->vPfMesh = advance_ptr_bytes<float>(ptr, szof_curve);
289 b->vBpMesh = advance_ptr_bytes<float>(ptr, szof_curve);
290 }
291 else
292 {
293 b->vFreqChart = NULL;
294 b->vPfMesh = NULL;
295 b->vBpMesh = NULL;
296 }
297
298 b->pSolo = NULL;
299 b->pMute = NULL;
300 b->pListen = NULL;
301 b->pLpfSlope = NULL;
302 b->pHpfSlope = NULL;
303 b->pFlatten = NULL;
304 b->pOutGain = NULL;
305 b->pFreqEnd = NULL;
306 b->pFreqMesh = NULL;
307
308 b->pInLevel = NULL;
309 b->pOutLevel = NULL;
310
311 b->pPdLongTime = NULL;
312 b->pPdShortTime = NULL;
313 b->pPdBias = NULL;
314 b->pPdMakeup = NULL;
315 b->pPdMesh = NULL;
316
317 b->pPfLookahead = NULL;
318 b->pPfAttack = NULL;
319 b->pPfRelease = NULL;
320 b->pPfThreshold = NULL;
321 b->pPfReduction = NULL;
322 b->pPfZone = NULL;
323 b->pPfMesh = NULL;
324 b->pPfEnvLevel = NULL;
325 b->pPfCurveLevel = NULL;
326 b->pPfGainLevel = NULL;
327
328 b->pBpAttack = NULL;
329 b->pBpRelease = NULL;
330 b->pBpTimeShift = NULL;
331 b->pBpThreshold = NULL;
332 b->pBpRatio = NULL;
333 b->pBpMaxGain = NULL;
334 b->pBpMesh = NULL;
335 b->pBpEnvLevel = NULL;
336 b->pBpCurveLevel = NULL;
337 b->pBpGainLevel = NULL;
338 }
339
340 c->nAnIn = an_cid++;
341 c->nAnOut = an_cid++;
342 c->fInLevel = GAIN_AMP_M_INF_DB;
343 c->fOutLevel = GAIN_AMP_M_INF_DB;
344
345 c->vIn = NULL;
346 c->vOut = NULL;
347 c->vInData = advance_ptr_bytes<float>(ptr, szof_buffer);
348 c->vOutData = advance_ptr_bytes<float>(ptr, szof_buffer);
349 c->vFreqChart = advance_ptr_bytes<float>(ptr, szof_fft);
350
351 vAnalyze[c->nAnIn] = c->vInData;
352 vAnalyze[c->nAnOut] = c->vOutData;
353
354 c->pIn = NULL;
355 c->pOut = NULL;
356
357 c->pInLevel = NULL;
358 c->pOutLevel = NULL;
359 c->pInFft = NULL;
360 c->pOutFft = NULL;
361 c->pInMesh = NULL;
362 c->pOutMesh = NULL;
363 c->pFreqMesh = NULL;
364 }
365
366 // Check bounds
367 lsp_assert(ptr <= ptr_check);
368
369 // Bind ports
370 size_t port_id = 0;
371
372 // Input ports
373 lsp_trace("Binding input ports");
374 for (size_t i=0; i<nChannels; ++i)
375 BIND_PORT(vChannels[i].pIn);
376
377 // Output ports
378 lsp_trace("Binding output ports");
379 for (size_t i=0; i<nChannels; ++i)
380 BIND_PORT(vChannels[i].pOut);
381
382 // Common ports
383 lsp_trace("Binding common ports");
384 BIND_PORT(pBypass);
385 BIND_PORT(pInGain);
386 BIND_PORT(pDryGain);
387 BIND_PORT(pWetGain);
388 BIND_PORT(pDryWet);
389 BIND_PORT(pOutGain);
390 SKIP_PORT("Tab selector"); // skip tab selector
391 BIND_PORT(pFFTReactivity);
392 BIND_PORT(pFFTShift);
393 BIND_PORT(pZoom);
394 SKIP_PORT("Show filters"); // skip show filters
395 if (nChannels > 1)
396 BIND_PORT(pStereoSplit);
397
398 // Channel meters
399 lsp_trace("Binding channel meters");
400 for (size_t i=0; i<nChannels; ++i)
401 {
402 channel_t *c = &vChannels[i];
403
404 BIND_PORT(c->pInLevel);
405 BIND_PORT(c->pOutLevel);
406 BIND_PORT(c->pInFft);
407 BIND_PORT(c->pOutFft);
408 BIND_PORT(c->pInMesh);
409 BIND_PORT(c->pOutMesh);
410 BIND_PORT(c->pFreqMesh);
411 }
412
413 // Splits
414 lsp_trace("Binding split ports");
415 for (size_t i=0; i<meta::beat_breather::BANDS_MAX-1; ++i)
416 {
417 split_t *s = &vSplits[i];
418
419 BIND_PORT(s->pEnable);
420 BIND_PORT(s->pFrequency);
421 }
422
423 // Band controls
424 lsp_trace("Binding band ports");
425 for (size_t i=0; i<nChannels; ++i)
426 {
427 channel_t *c = &vChannels[i];
428
429 for (size_t j=0; j<meta::beat_breather::BANDS_MAX; ++j)
430 {
431 band_t *b = &c->vBands[j];
432
433 if (i > 0)
434 {
435 band_t *sb = &vChannels[0].vBands[j];
436
437 b->pSolo = sb->pSolo;
438 b->pMute = sb->pMute;
439 b->pListen = sb->pListen;
440 b->pLpfSlope = sb->pLpfSlope;
441 b->pHpfSlope = sb->pHpfSlope;
442 b->pFlatten = sb->pFlatten;
443 b->pOutGain = sb->pOutGain;
444 b->pFreqEnd = sb->pFreqEnd;
445 b->pFreqMesh = NULL;
446
447 b->pPdLongTime = sb->pPdLongTime;
448 b->pPdShortTime = sb->pPdShortTime;
449 b->pPdBias = sb->pPdBias;
450 b->pPdMakeup = sb->pPdMakeup;
451
452 b->pPfLookahead = sb->pPfLookahead;
453 b->pPfAttack = sb->pPfAttack;
454 b->pPfRelease = sb->pPfRelease;
455 b->pPfThreshold = sb->pPfThreshold;
456 b->pPfReduction = sb->pPfReduction;
457 b->pPfZone = sb->pPfZone;
458 b->pPfMesh = sb->pPfMesh;
459
460 b->pBpAttack = sb->pBpAttack;
461 b->pBpRelease = sb->pBpRelease;
462 b->pBpTimeShift = sb->pBpTimeShift;
463 b->pBpThreshold = sb->pBpThreshold;
464 b->pBpRatio = sb->pBpRatio;
465 b->pBpMaxGain = sb->pBpMaxGain;
466 b->pBpMesh = sb->pBpMesh;
467 }
468 else
469 {
470 BIND_PORT(b->pSolo);
471 BIND_PORT(b->pMute);
472 BIND_PORT(b->pListen);
473 BIND_PORT(b->pLpfSlope);
474 BIND_PORT(b->pHpfSlope);
475 BIND_PORT(b->pFlatten);
476 BIND_PORT(b->pOutGain);
477 BIND_PORT(b->pFreqEnd);
478 BIND_PORT(b->pFreqMesh);
479
480 BIND_PORT(b->pPdLongTime);
481 BIND_PORT(b->pPdShortTime);
482 BIND_PORT(b->pPdBias);
483 BIND_PORT(b->pPdMakeup);
484
485 BIND_PORT(b->pPfLookahead);
486 BIND_PORT(b->pPfAttack);
487 BIND_PORT(b->pPfRelease);
488 BIND_PORT(b->pPfThreshold);
489 BIND_PORT(b->pPfReduction);
490 BIND_PORT(b->pPfZone);
491 BIND_PORT(b->pPfMesh);
492
493 BIND_PORT(b->pBpAttack);
494 BIND_PORT(b->pBpRelease);
495 BIND_PORT(b->pBpTimeShift);
496 BIND_PORT(b->pBpThreshold);
497 BIND_PORT(b->pBpRatio);
498 BIND_PORT(b->pBpMaxGain);
499 BIND_PORT(b->pBpMesh);
500 }
501 }
502 }
503
504 // Band meters
505 lsp_trace("Binding band meters");
506 for (size_t i=0; i<nChannels; ++i)
507 {
508 channel_t *c = &vChannels[i];
509
510 for (size_t j=0; j<meta::beat_breather::BANDS_MAX; ++j)
511 {
512 band_t *b = &c->vBands[j];
513
514 BIND_PORT(b->pInLevel);
515 BIND_PORT(b->pOutLevel);
516
517 BIND_PORT(b->pPdMesh);
518
519 BIND_PORT(b->pPfEnvLevel);
520 BIND_PORT(b->pPfCurveLevel);
521 BIND_PORT(b->pPfGainLevel);
522
523 BIND_PORT(b->pBpEnvLevel);
524 BIND_PORT(b->pBpCurveLevel);
525 BIND_PORT(b->pBpGainLevel);
526 }
527 }
528
529 float delta;
530
531 // Initialize coordinates for Peak Detector mesh
532 delta = meta::beat_breather::TIME_HISTORY_MAX / (meta::beat_breather::TIME_MESH_POINTS-1);
533 for (size_t i=0; i<meta::beat_breather::TIME_MESH_POINTS; ++i)
534 vPdMesh[i] = meta::beat_breather::TIME_HISTORY_MAX - delta * i;
535
536 // Initialize coordinates for Peak Filter mesh
537 delta = (meta::beat_breather::PF_CURVE_MAX - meta::beat_breather::PF_CURVE_MIN) / (meta::beat_breather::CURVE_MESH_POINTS-1);
538 for (size_t i=0; i<meta::beat_breather::CURVE_MESH_POINTS; ++i)
539 vPfMesh[i] = dspu::db_to_gain(meta::beat_breather::PF_CURVE_MIN + delta * i);
540
541 // Initialize coordinates for Beat Processor mesh
542 delta = (meta::beat_breather::BP_CURVE_MAX - meta::beat_breather::BP_CURVE_MIN) / (meta::beat_breather::CURVE_MESH_POINTS-1);
543 for (size_t i=0; i<meta::beat_breather::CURVE_MESH_POINTS; ++i)
544 vBpMesh[i] = dspu::db_to_gain(meta::beat_breather::BP_CURVE_MIN + delta * i);
545 }
546
547 size_t beat_breather::select_fft_rank(size_t sample_rate)
548 {
549 const size_t k = (sample_rate + meta::beat_breather::FFT_XOVER_FREQ_MIN/2) / meta::beat_breather::FFT_XOVER_FREQ_MIN;
550 const size_t n = int_log2(k);
551 return meta::beat_breather::FFT_XOVER_RANK_MIN + n;
552 }
553
554 void beat_breather::update_sample_rate(long sr)
555 {
556 const size_t fft_rank = select_fft_rank(sr);
557 const size_t max_delay_pd = dspu::millis_to_samples(sr,
558 (lsp_max(meta::beat_breather::PD_SHORT_RMS_MAX, meta::beat_breather::PD_LONG_RMS_MAX) + 1)/2);
559 const size_t max_delay_pf = dspu::millis_to_samples(sr, meta::beat_breather::PF_LOOKAHEAD_MAX);
560 const size_t max_delay_bp = dspu::millis_to_samples(sr, meta::beat_breather::BP_TIME_SHIFT_MAX);
561 const size_t max_delay_fft = (1 << fft_rank);
562 const size_t samples_per_dot= dspu::seconds_to_samples(sr, meta::beat_breather::TIME_HISTORY_MAX / meta::beat_breather::TIME_MESH_POINTS);
563
564 sCounter.set_sample_rate(sr, true);
565
566 for (size_t i=0; i<nChannels; ++i)
567 {
568 channel_t *c = &vChannels[i];
569
570 c->sBypass.init(sr);
571 c->sDelay.init(max_delay_fft + max_delay_pd + max_delay_pf + max_delay_bp + BUFFER_SIZE);
572 c->sDryDelay.init(max_delay_fft + max_delay_pd + max_delay_pf + max_delay_bp + BUFFER_SIZE);
573
574 if (fft_rank != c->sCrossover.rank())
575 {
576 c->sCrossover.init(fft_rank, meta::beat_breather::BANDS_MAX);
577 for (size_t j=0; j<meta::beat_breather::BANDS_MAX; ++j)
578 c->sCrossover.set_handler(j, process_band, this, c);
579 c->sCrossover.set_rank(fft_rank);
580 c->sCrossover.set_phase(float(i) / float(nChannels));
581 }
582 c->sCrossover.set_sample_rate(sr);
583
584 for (size_t j=0; j<meta::beat_breather::BANDS_MAX; ++j)
585 {
586 band_t *b = &c->vBands[j];
587
588 b->sDelay.init(max_delay_pd + max_delay_pf + max_delay_bp);
589 b->sPdLong.set_sample_rate(sr);
590 b->sPdShort.set_sample_rate(sr);
591 b->sPdDelay.init(max_delay_pd);
592 b->sPdMeter.init(meta::beat_breather::TIME_MESH_POINTS, samples_per_dot);
593 b->sPf.set_sample_rate(sr);
594 b->sPfDelay.init(max_delay_pf);
595 b->sBp.set_sample_rate(sr);
596 b->sBpScDelay.init(max_delay_bp);
597 b->sBpDelay.init(max_delay_pd + max_delay_pf + max_delay_bp);
598 }
599 }
600
601 // Commit sample rate to analyzer
602 sAnalyzer.set_sample_rate(sr);
603 }
604
605 int beat_breather::compare_splits(const void *a1, const void *a2, void *data)
606 {
607 const split_t * const * s1 = reinterpret_cast<const split_t * const *>(a1);
608 const split_t * const * s2 = reinterpret_cast<const split_t * const *>(a2);
609 if ((*s1)->fFrequency < (*s2)->fFrequency)
610 return -1;
611 else if ((*s1)->fFrequency > (*s2)->fFrequency)
612 return 1;
613 return 0;
614 }
615
616 beat_breather::band_mode_t beat_breather::decode_band_mode(size_t mode)
617 {
618 switch (mode)
619 {
620 case meta::beat_breather::LISTEN_CROSSOVER:
621 return BAND_BF;
622 case meta::beat_breather::LISTEN_RMS:
623 return BAND_PD;
624 case meta::beat_breather::LISTEN_PUNCH:
625 return BAND_PF;
626 case meta::beat_breather::LISTEN_BEAT:
627 return BAND_BP;
628 default:
629 break;
630 }
631 return BAND_OFF;
632 }
633
634 void beat_breather::update_settings()
635 {
636 // Configure global parameters
637 float dry_gain = pDryGain->value();
638 float wet_gain = pWetGain->value();
639 float drywet = pDryWet->value() * 0.01f;
640 float out_gain = pOutGain->value();
641 bStereoSplit = ((nChannels > 1) && (pStereoSplit != NULL)) ? pStereoSplit->value() >= 0.5f : false;
642 fInGain = pInGain->value();
643 fDryGain = (dry_gain * drywet + 1.0f - drywet) * out_gain;
644 fWetGain = wet_gain * drywet * out_gain;
645 fZoom = pZoom->value();
646 size_t an_channels = 0;
647 bool bypass = pBypass->value() >= 0.5f;
648 bool sync = false;
649
650 // Update analyzer settings
651 for (size_t i=0; i<nChannels; ++i)
652 {
653 channel_t *c = &vChannels[i];
654
655 // Update analyzer settings
656 sAnalyzer.enable_channel(c->nAnIn, c->pInFft->value() >= 0.5f);
657 sAnalyzer.enable_channel(c->nAnOut, c->pOutFft->value() >= 0.5f);
658
659 if (sAnalyzer.channel_active(c->nAnIn))
660 an_channels ++;
661 if (sAnalyzer.channel_active(c->nAnOut))
662 an_channels ++;
663 }
664
665 // Update analyzer parameters
666 sAnalyzer.set_reactivity(pFFTReactivity->value());
667 if (pFFTShift != NULL)
668 sAnalyzer.set_shift(dspu::db_to_gain(pFFTShift->value()) * 100.0f);
669 sAnalyzer.set_activity(an_channels > 0);
670
671 if (sAnalyzer.needs_reconfiguration())
672 {
673 sAnalyzer.reconfigure();
674 sAnalyzer.get_frequencies(
675 vFftFreqs,
676 vFftIndexes,
677 SPEC_FREQ_MIN,
678 SPEC_FREQ_MAX,
679 meta::beat_breather::FFT_MESH_POINTS);
680 sync = true;
681 }
682
683 // Configure splits and their order
684 size_t nsplits = 0;
685 split_t *vsplits[meta::beat_breather::BANDS_MAX];
686
687 for (size_t i=0; i<meta::beat_breather::BANDS_MAX-1; ++i)
688 {
689 split_t *sp = &vSplits[i];
690 sp->nBandId = i + 1;
691 sp->bEnabled = sp->pEnable->value() >= 0.5f;
692 sp->fFrequency = sp->pFrequency->value();
693 if (sp->bEnabled)
694 vsplits[nsplits++] = sp;
695 }
696 if (nsplits > 1)
697 lsp::qsort_r(vsplits, nsplits, sizeof(split_t *), compare_splits, NULL);
698
699 // Configure channels
700 size_t max_band_latency = 0;
701 for (size_t i=0; i<nChannels; ++i)
702 {
703 channel_t *c = &vChannels[i];
704 bool has_solo = false;
705
706 c->sBypass.set_bypass(bypass);
707
708 // Form the list of bands
709 for (size_t j=0; j<meta::beat_breather::BANDS_MAX; ++j)
710 {
711 band_t *b = &c->vBands[j];
712 b->nOldMode = b->nMode;
713 b->nMode = BAND_OFF;
714 }
715
716 // Configure active frequency bands
717 for (size_t j=0; j<=nsplits; ++j)
718 {
719 size_t band_id = (j > 0) ? vsplits[j-1]->nBandId : 0;
720 band_t *b = &c->vBands[band_id];
721 b->nMode = decode_band_mode(b->pListen->value());
722
723 // Configure hi-pass filter
724 if (j > 0)
725 {
726 c->sCrossover.enable_hpf(band_id, true);
727 c->sCrossover.set_hpf_frequency(band_id, vsplits[j-1]->fFrequency);
728 c->sCrossover.set_hpf_slope(band_id, - b->pHpfSlope->value());
729 }
730 else
731 c->sCrossover.disable_hpf(band_id);
732
733 // Configure lo-pass filter
734 if (j < nsplits)
735 {
736 c->sCrossover.enable_lpf(band_id, true);
737 c->sCrossover.set_lpf_frequency(band_id, vsplits[j]->fFrequency);
738 c->sCrossover.set_lpf_slope(band_id, - b->pLpfSlope->value());
739 b->pFreqEnd->set_value(vsplits[j]->fFrequency);
740 }
741 else
742 {
743 c->sCrossover.disable_lpf(band_id);
744 b->pFreqEnd->set_value(fSampleRate * 0.5f);
745 }
746
747 c->sCrossover.set_flatten(band_id, dspu::db_to_gain(-b->pFlatten->value()));
748
749 // Check solo option
750 if (b->pSolo->value() >= 0.5f)
751 has_solo = true;
752 }
753
754 // Configure bands
755 for (size_t j=0; j<meta::beat_breather::BANDS_MAX; ++j)
756 {
757 band_t *b = &c->vBands[j];
758
759 bool solo = b->pSolo->value() >= 0.5f;
760 bool mute = ((has_solo) && (!solo)) ? true : b->pMute->value() >= 0.5f;
761 if ((mute) && (b->nMode != BAND_OFF))
762 b->nMode = BAND_MUTE;
763
764 b->fGain = b->pOutGain->value();
765
766 // Do we have hi-pass filter?
767 c->sCrossover.enable_band(j, b->nMode != BAND_OFF);
768
769 // Update Peak detector configuration
770 float pd_long = b->pPdLongTime->value();
771 float pd_short = b->pPdShortTime->value();
772 size_t pd_short_latency = dspu::millis_to_samples(fSampleRate, pd_long - pd_short) / 2;
773 size_t pd_latency = dspu::millis_to_samples(fSampleRate, pd_long) / 2;
774 float pd_bias = dspu::db_to_gain(b->pPdBias->value());
775 b->fPdMakeup = dspu::db_to_gain(b->pPdMakeup->value() + meta::beat_breather::PD_MAKEUP_SHIFT);
776
777 b->sPdLong.set_mode(dspu::SCM_RMS);
778 b->sPdLong.set_source(dspu::SCS_MIDDLE);
779 b->sPdLong.set_reactivity(pd_long);
780 b->sPdLong.set_gain(GAIN_AMP_0_DB);
781
782 b->sPdShort.set_mode(dspu::SCM_RMS);
783 b->sPdShort.set_source(dspu::SCS_MIDDLE);
784 b->sPdShort.set_reactivity(pd_short);
785 b->sPdShort.set_gain(pd_bias);
786
787 b->sPdDelay.set_delay(pd_short_latency);
788
789 b->sPdMeter.set_method(dspu::MM_ABS_MAXIMUM);
790 if ((b->nOldMode != BAND_OFF) && (b->nMode == BAND_OFF))
791 b->sPdMeter.clear();
792
793 // Update peak filter configuration
794 float pf_thresh = b->pPfThreshold->value();
795 float pf_zone = b->pPfZone->value();
796 size_t pf_latency = dspu::millis_to_samples(fSampleRate, b->pPfLookahead->value());
797
798 b->sPf.set_attack(b->pPfAttack->value());
799 b->sPf.set_release(b->pPfRelease->value());
800 b->sPf.set_threshold(pf_thresh, pf_thresh);
801 b->sPf.set_zone(pf_zone, pf_zone);
802 b->sPf.set_reduction(b->pPfReduction->value());
803 if (b->sPf.modified())
804 {
805 b->sPf.update_settings();
806 if (i == 0)
807 b->sPf.curve(b->vPfMesh, vPfMesh, meta::beat_breather::CURVE_MESH_POINTS, false);
808 b->nSync |= SYNC_PEAK_FILTER;
809 }
810
811 b->sPfDelay.set_delay(pf_latency);
812
813 // Update beat processor configuration
814 float bp_ratio = b->pBpRatio->value() - 1.0f;
815 float bp_shift = b->pBpTimeShift->value();
816 size_t bp_sc_latency = dspu::millis_to_samples(fSampleRate, lsp_max(bp_shift, 0.0f));
817 size_t bp_latency = dspu::millis_to_samples(fSampleRate, lsp_max(-bp_shift, 0.0f));
818 if (bp_ratio >= 1e-3f)
819 {
820 float bp_max_gain = b->pBpMaxGain->value();
821 float bp_user_th = b->pBpThreshold->value();
822 float bp_log_th = logf(bp_user_th);
823 float bp_log_gate_th = logf(bp_max_gain) / bp_ratio + bp_log_th;
824 float bp_th = expf(bp_log_gate_th);
825 float bp_zone = bp_user_th / bp_th;
826
827 b->sBp.set_threshold(bp_th, bp_th);
828 b->sBp.set_reduction(1.0f / bp_max_gain);
829 b->sBp.set_zone(bp_zone, bp_zone);
830 b->fBpMakeup = bp_max_gain;
831 }
832 else
833 {
834 float bp_th = b->pBpThreshold->value();
835 b->sBp.set_threshold(bp_th, bp_th);
836 b->sBp.set_reduction(GAIN_AMP_0_DB);
837 b->sBp.set_zone(GAIN_AMP_0_DB, GAIN_AMP_0_DB);
838 b->fBpMakeup = GAIN_AMP_0_DB;
839 }
840
841
842 b->sBp.set_attack(b->pBpAttack->value());
843 b->sBp.set_release(b->pBpRelease->value());
844
845 if (b->sBp.modified())
846 {
847 b->sBp.update_settings();
848 if (i == 0)
849 b->sBp.curve(b->vBpMesh, vBpMesh, meta::beat_breather::CURVE_MESH_POINTS, false);
850 b->nSync |= SYNC_BEAT_PROC;
851 }
852
853 b->sBpScDelay.set_delay(bp_sc_latency);
854 b->sBpDelay.set_delay(pd_latency + pf_latency + bp_latency);
855
856 // Compute the overall latency of the band
857 if (b->nMode != BAND_OFF)
858 max_band_latency = lsp_max(max_band_latency, b->sBpDelay.delay());
859 }
860
861 // Reconfigure the crossover
862 bool csync = (sync) || (c->sCrossover.needs_update());
863 c->sCrossover.update_settings();
864
865 if ((csync) && (i == 0))
866 {
867 // Output band parameters and update sync curve flag
868 for (size_t j=0; j<meta::beat_breather::BANDS_MAX; ++j)
869 {
870 band_t *b = &c->vBands[j];
871
872 // Get frequency response for band
873 c->sCrossover.freq_chart(j, b->vFreqChart, vFftFreqs, meta::beat_breather::FFT_MESH_POINTS);
874 b->nSync |= SYNC_BAND_FILTER;
875 }
876 }
877 }
878
879 // Apply latency compensations and report latency
880 for (size_t i=0; i<nChannels; ++i)
881 {
882 channel_t *c = &vChannels[i];
883 for (size_t j=0; j<meta::beat_breather::BANDS_MAX; ++j)
884 {
885 band_t *b = &c->vBands[j];
886 if (b->nMode == BAND_OFF)
887 continue;
888 b->sDelay.set_delay(max_band_latency - b->sBpDelay.delay());
889
890 lsp_trace("Band #%d: latency=%d, compensation=%d, overall=%d",
891 int(i),
892 int(b->sBpDelay.delay()),
893 int(b->sDelay.delay()),
894 int(max_band_latency));
895 }
896
897 size_t overall_latency = max_band_latency + c->sCrossover.latency();
898 c->sDelay.set_delay(overall_latency);
899 c->sDryDelay.set_delay(overall_latency);
900
901 if (i == 0)
902 set_latency(overall_latency);
903 }
904 }
905
906 void beat_breather::process_band(void *object, void *subject, size_t band, const float *data, size_t sample, size_t count)
907 {
908 channel_t *c = static_cast<channel_t *>(subject);
909 band_t *b = &c->vBands[band];
910
911 // Apply delay compensation and store to band's data buffer.
912 b->sDelay.process(&b->vInData[sample], data, count);
913 // Measure the input level
914 b->fInLevel = lsp_max(dsp::abs_max(&b->vInData[sample], count), b->fInLevel);
915 }
916
917 void beat_breather::process(size_t samples)
918 {
919 bind_inputs();
920
921 for (size_t offset = 0; offset < samples; )
922 {
923 size_t to_do = lsp_min(samples - offset, BUFFER_SIZE);
924
925 // Stores band data to band_t::vIn
926 split_signal(to_do);
927 // Stores normalized RMS difference to band_t::vPdData
928 apply_peak_detector(to_do);
929 // Stores processed data to band_t::vPfData
930 apply_punch_filter(to_do);
931 // Stores the processed band data to band_t::vBpData
932 apply_beat_processor(to_do);
933
934 // Stores the processed band data to channel_t::vOutData
935 mix_bands(to_do);
936
937 // Post-process data
938 post_process_block(to_do);
939
940 // Update pointers and the offset
941 update_pointers(to_do);
942 offset += to_do;
943 }
944
945 sCounter.submit(samples);
946
947 output_meters();
948
949 if ((pWrapper != NULL) && (sCounter.fired()))
950 pWrapper->query_display_draw();
951 sCounter.commit();
952 }
953
954 void beat_breather::bind_inputs()
955 {
956 for (size_t i=0; i<nChannels; ++i)
957 {
958 channel_t *c = &vChannels[i];
959 c->vIn = c->pIn->buffer<float>();
960 c->vOut = c->pOut->buffer<float>();
961
962 c->fInLevel = GAIN_AMP_M_INF_DB;
963 c->fOutLevel = GAIN_AMP_M_INF_DB;
964
965 for (size_t j=0; j<meta::beat_breather::BANDS_MAX; ++j)
966 {
967 band_t *b = &c->vBands[j];
968
969 b->fInLevel = GAIN_AMP_M_INF_DB;
970 b->fOutLevel = GAIN_AMP_M_INF_DB;
971
972 b->fPdLevel = GAIN_AMP_M_INF_DB;
973
974 b->fPfInGain = GAIN_AMP_M_INF_DB;
975 b->fPfOutGain = GAIN_AMP_M_INF_DB;
976 b->fPfReduction = GAIN_AMP_0_DB;
977
978 b->fBpInGain = GAIN_AMP_M_INF_DB;
979 b->fBpOutGain = GAIN_AMP_M_INF_DB;
980 b->fBpReduction = GAIN_AMP_M_INF_DB;
981 }
982 }
983 }
984
985 void beat_breather::split_signal(size_t samples)
986 {
987 for (size_t i=0; i<nChannels; ++i)
988 {
989 channel_t *c = &vChannels[i];
990
991 // Apply gain to input signal
992 dsp::mul_k3(c->vInData, c->vIn, fInGain, samples);
993 // Pass the input signal to crossover
994 c->sCrossover.process(c->vInData, samples);
995 }
996 }
997
998 void beat_breather::normalize_rms(float *dst, const float *lrms, const float *srms, float gain, size_t samples)
999 {
1000 for (size_t i=0; i<samples; ++i)
1001 {
1002 const float l = lrms[i];
1003 const float s = srms[i];
1004 if ((s > l) && (l >= GAIN_AMP_M_140_DB))
1005 dst[i] = (s * gain) / l;
1006 else
1007 dst[i] = gain;
1008 }
1009 }
1010
1011 void beat_breather::apply_peak_detector(size_t samples)
1012 {
1013 // Esimate RMS for all bands
1014 for (size_t i=0; i<nChannels; ++i)
1015 {
1016 channel_t *c = &vChannels[i];
1017
1018 for (size_t j=0; j<meta::beat_breather::BANDS_MAX; ++j)
1019 {
1020 band_t *b = &c->vBands[j];
1021 if (b->nMode == BAND_OFF)
1022 continue;
1023
1024 // Estimate long-time RMS
1025 b->sPdLong.process(b->vPdData, const_cast<const float **>(&b->vInData), samples);
1026 // Estimate short-time RMS
1027 b->sPdShort.process(b->vPfData, const_cast<const float **>(&b->vInData), samples);
1028 // Apply delay compensation to short-time RMS estimation
1029 b->sPdDelay.process(b->vPfData, b->vPfData, samples);
1030 }
1031 }
1032
1033 // Mix sidechain if 'Stereo Split' is not enabled
1034 if ((nChannels > 1) && (!bStereoSplit))
1035 {
1036 for (size_t j=0; j<meta::beat_breather::BANDS_MAX; ++j)
1037 {
1038 band_t *left = &vChannels[0].vBands[j];
1039 band_t *right = &vChannels[1].vBands[j];
1040 if ((left->nMode == BAND_OFF) || (right->nMode == BAND_OFF))
1041 continue;
1042
1043 // Convert stereo long-time RMS to mono
1044 dsp::lr_to_mid(left->vPdData, left->vPdData, right->vPdData, samples);
1045 // Duplicate long-time RMS to second channel
1046 dsp::copy(right->vPdData, left->vPdData, samples);
1047 // Convert stereo short-time RMS to mono
1048 dsp::lr_to_mid(left->vPfData, left->vPfData, right->vPfData, samples);
1049 // Duplicate short-time RMS to second channel
1050 dsp::copy(right->vPfData, left->vPfData, samples);
1051 }
1052 }
1053
1054 // Do post-processing and normalization
1055 for (size_t i=0; i<nChannels; ++i)
1056 {
1057 channel_t *c = &vChannels[i];
1058
1059 for (size_t j=0; j<meta::beat_breather::BANDS_MAX; ++j)
1060 {
1061 band_t *b = &c->vBands[j];
1062 if (b->nMode == BAND_OFF)
1063 continue;
1064
1065 // Produce normalized Peak/RMS signal
1066 normalize_rms(b->vPdData, b->vPdData, b->vPfData, b->fPdMakeup, samples);
1067 b->sPdMeter.process(b->vPdData, samples);
1068 b->fPdLevel = lsp_max(b->fPdLevel, dsp::abs_max(b->vPdData, samples));
1069 }
1070 }
1071 }
1072
1073 void beat_breather::apply_punch_filter(size_t samples)
1074 {
1075 // Do post-processing and normalization
1076 for (size_t i=0; i<nChannels; ++i)
1077 {
1078 channel_t *c = &vChannels[i];
1079
1080 for (size_t j=0; j<meta::beat_breather::BANDS_MAX; ++j)
1081 {
1082 band_t *b = &c->vBands[j];
1083 if (b->nMode == BAND_OFF)
1084 continue;
1085
1086 // Process sidechain signal and produce VCA
1087 b->sPf.process(b->vPfData, vBuffer, b->vPdData, samples);
1088 // Update gating dot meter
1089 size_t idx = dsp::max_index(b->vPfData, samples);
1090 if (vBuffer[idx] > b->fPfInGain)
1091 {
1092 b->fPfInGain = vBuffer[idx];
1093 b->fPfOutGain = b->vPfData[idx] * b->fPfInGain;
1094 }
1095 b->fPfReduction = lsp_min(dsp::abs_min(b->vPfData, samples), b->fPfReduction);
1096 // Apply lookahead delay to Peak/RMS signal
1097 b->sPfDelay.process(vBuffer, b->vPdData, samples);
1098 // Apply VCA to peak signal
1099 dsp::mul2(b->vPfData, vBuffer, samples);
1100 }
1101 }
1102 }
1103
1104 void beat_breather::apply_beat_processor(size_t samples)
1105 {
1106 // Process the data stored in band_t::vPfData and band_t::vIn and store result to band_t::vData
1107 for (size_t i=0; i<nChannels; ++i)
1108 {
1109 channel_t *c = &vChannels[i];
1110
1111 for (size_t j=0; j<meta::beat_breather::BANDS_MAX; ++j)
1112 {
1113 band_t *b = &c->vBands[j];
1114 if (b->nMode == BAND_OFF)
1115 continue;
1116
1117 // Apply time shift delay to the sidechain
1118 b->sBpScDelay.process(b->vBpData, b->vPfData, samples);
1119 // Process sidechain signal and produce VCA
1120 b->sBp.process(b->vBpData, vBuffer, b->vBpData, samples);
1121 // Update gating dot meter
1122 size_t idx = dsp::max_index(b->vBpData, samples);
1123 if (vBuffer[idx] > b->fBpInGain)
1124 {
1125 b->fBpInGain = vBuffer[idx];
1126 b->fBpOutGain = b->vBpData[idx] * b->fBpInGain * b->fBpMakeup;
1127 }
1128 b->fBpReduction = lsp_max(dsp::abs_max(b->vBpData, samples) * b->fBpMakeup, b->fBpReduction);
1129 // Apply time shift delay + latency compensation to the signal of the band
1130 b->sBpDelay.process(b->vInData, b->vInData, samples);
1131 // Apply VCA to original signal
1132 dsp::fmmul_k3(b->vBpData, b->vInData, b->fBpMakeup, samples);
1133 }
1134 }
1135 }
1136
1137 void beat_breather::mix_bands(size_t samples)
1138 {
1139 // Mix bands depending on the band listen mode
1140 for (size_t i=0; i<nChannels; ++i)
1141 {
1142 channel_t *c = &vChannels[i];
1143
1144 // Compute the averaging value for all peak-detected and peak-filtered tracks
1145 // Because peak-detected tracks have constant bias, they should be mixed in different way
1146 ssize_t num_pd = 0;
1147 for (size_t j=0; j<meta::beat_breather::BANDS_MAX; ++j)
1148 {
1149 switch (c->vBands[j].nMode)
1150 {
1151 case BAND_PD:
1152 case BAND_PF:
1153 ++num_pd;
1154 break;
1155 default:
1156 break;
1157 }
1158 }
1159 float pd_makeup = (num_pd > 0) ? 1.0f / num_pd : 1.0f;
1160
1161 // Mix the tracks
1162 size_t mixed = 0;
1163 for (size_t j=0; j<meta::beat_breather::BANDS_MAX; ++j)
1164 {
1165 band_t *b = &c->vBands[j];
1166 switch (b->nMode)
1167 {
1168 case BAND_BF:
1169 b->fOutLevel = lsp_max(dsp::abs_max(b->vInData, samples) * b->fGain, b->fOutLevel);
1170 b->fReduction = b->fGain;
1171 if (mixed++)
1172 dsp::fmadd_k3(c->vOutData, b->vInData, b->fGain, samples);
1173 else
1174 dsp::mul_k3(c->vOutData, b->vInData, b->fGain, samples);
1175 break;
1176 case BAND_PD:
1177 b->fOutLevel = lsp_max(dsp::abs_max(b->vPdData, samples) * b->fGain * pd_makeup, b->fOutLevel);
1178 b->fReduction = b->fPdLevel * b->fGain;
1179 if (mixed++)
1180 dsp::fmadd_k3(c->vOutData, b->vPdData, b->fGain * pd_makeup, samples);
1181 else
1182 dsp::mul_k3(c->vOutData, b->vPdData, b->fGain * pd_makeup, samples);
1183 break;
1184 case BAND_PF:
1185 b->fOutLevel = lsp_max(dsp::abs_max(b->vPfData, samples) * b->fGain * pd_makeup, b->fOutLevel);
1186 b->fReduction = b->fPfReduction * b->fGain;
1187 if (mixed++)
1188 dsp::fmadd_k3(c->vOutData, b->vPfData, b->fGain * pd_makeup, samples);
1189 else
1190 dsp::mul_k3(c->vOutData, b->vPfData, b->fGain * pd_makeup, samples);
1191 break;
1192 case BAND_BP:
1193 b->fOutLevel = lsp_max(dsp::abs_max(b->vBpData, samples) * b->fGain, b->fOutLevel);
1194 b->fReduction = b->fBpReduction * b->fGain;
1195 if (mixed++)
1196 dsp::fmadd_k3(c->vOutData, b->vBpData, b->fGain, samples);
1197 else
1198 dsp::mul_k3(c->vOutData, b->vBpData, b->fGain, samples);
1199 break;
1200
1201 case BAND_MUTE:
1202 case BAND_OFF:
1203 default:
1204 break;
1205 }
1206 }
1207
1208 // Clear audio output if there is no one band active
1209 if (!mixed)
1210 dsp::fill_zero(c->vOutData, samples);
1211 }
1212 }
1213
1214 void beat_breather::post_process_block(size_t samples)
1215 {
1216 // Apply delay compensation to input data and measure level
1217 for (size_t i=0; i<nChannels; ++i)
1218 {
1219 channel_t *c = &vChannels[i];
1220
1221 // Delay the channel's input data
1222 c->sDelay.process(c->vInData, c->vInData, samples);
1223 // Measure the input level
1224 c->fInLevel = lsp_max(dsp::abs_max(c->vInData, samples), c->fInLevel);
1225
1226 // Mix dry/wet into channel_t::vOutData
1227 dsp::mix2(c->vOutData, c->vInData, fWetGain, fDryGain, samples);
1228 // Measure the output level
1229 c->fOutLevel = lsp_max(dsp::abs_max(c->vOutData, samples), c->fOutLevel);
1230 }
1231
1232 // Measure levels
1233 if (sAnalyzer.activity())
1234 sAnalyzer.process(vAnalyze, samples);
1235
1236 // Apply bypass switch
1237 for (size_t i=0; i<nChannels; ++i)
1238 {
1239 channel_t *c = &vChannels[i];
1240
1241 // Delay the dry signal
1242 c->sDryDelay.process(vBuffer, c->vIn, samples);
1243 // Apply bypass and output data
1244 c->sBypass.process(c->vOut, vBuffer, c->vOutData, samples);
1245 }
1246 }
1247
1248 void beat_breather::update_pointers(size_t samples)
1249 {
1250 for (size_t i=0; i<nChannels; ++i)
1251 {
1252 channel_t *c = &vChannels[i];
1253 c->vIn += samples;
1254 c->vOut += samples;
1255 }
1256 }
1257
1258 void beat_breather::output_meters()
1259 {
1260 // Output meshes
1261 plug::mesh_t *mesh;
1262 for (size_t i=0; i<nChannels; ++i)
1263 {
1264 channel_t *c = &vChannels[i];
1265
1266 // Compute transfer curve
1267 if (sCounter.fired())
1268 {
1269 for (size_t offset=0; offset<meta::beat_breather::FFT_MESH_POINTS; )
1270 {
1271 size_t samples = lsp_min(meta::beat_breather::FFT_MESH_POINTS - offset, BUFFER_SIZE);
1272
1273 // Perform mix of band transfer characteristics
1274 size_t mixed = 0;
1275 for (size_t j=0; j<meta::beat_breather::BANDS_MAX; ++j)
1276 {
1277 band_t *b = &c->vBands[j];
1278 if ((b->nMode != BAND_OFF) && (b->nMode != BAND_MUTE))
1279 {
1280 if (mixed++)
1281 dsp::fmadd_k3(
1282 vBuffer,
1283 &vChannels[0].vBands[j].vFreqChart[offset],
1284 b->fReduction,
1285 samples);
1286 else
1287 dsp::mul_k3(
1288 vBuffer,
1289 &vChannels[0].vBands[j].vFreqChart[offset],
1290 b->fReduction,
1291 samples);
1292 }
1293 }
1294
1295 // Fill the output frequency chart
1296 if (mixed)
1297 dsp::copy(&c->vFreqChart[offset], vBuffer, samples);
1298 else
1299 dsp::fill_zero(&c->vFreqChart[offset], samples);
1300
1301 offset += samples;
1302 }
1303 }
1304
1305 // Output input and output level meters
1306 c->pInLevel->set_value(c->fInLevel);
1307 c->pOutLevel->set_value(c->fOutLevel);
1308
1309 // Output transfer function of the channel
1310 mesh = (c->pFreqMesh != NULL) ? c->pFreqMesh->buffer<plug::mesh_t>() : NULL;
1311 if ((mesh != NULL) && (mesh->isEmpty()))
1312 {
1313 dsp::copy(mesh->pvData[0], vFftFreqs, meta::beat_breather::FFT_MESH_POINTS);
1314 dsp::copy(mesh->pvData[1], c->vFreqChart, meta::beat_breather::FFT_MESH_POINTS);
1315 mesh->data(2, meta::beat_breather::FFT_MESH_POINTS);
1316 }
1317
1318 // Sync filter characteristics for each band
1319 for (size_t j=0; j<meta::beat_breather::BANDS_MAX; ++j)
1320 {
1321 band_t *b = &c->vBands[j];
1322
1323 // Output meters
1324 b->pInLevel->set_value(b->fInLevel);
1325 b->pOutLevel->set_value(b->fOutLevel);
1326 b->pPfEnvLevel->set_value(b->fPfInGain);
1327 b->pPfCurveLevel->set_value(b->fPfOutGain);
1328 b->pPfGainLevel->set_value(b->fPfReduction);
1329 b->pBpEnvLevel->set_value(b->fBpInGain);
1330 b->pBpCurveLevel->set_value(b->fBpOutGain);
1331 b->pBpGainLevel->set_value(b->fBpReduction);
1332
1333 // Synchronize band transfer function mesh
1334 if ((i == 0) && (b->nSync & SYNC_BAND_FILTER))
1335 {
1336 mesh = (b->pFreqMesh != NULL) ? b->pFreqMesh->buffer<plug::mesh_t>() : NULL;
1337 if ((mesh != NULL) && (mesh->isEmpty()))
1338 {
1339 mesh->pvData[0][0] = SPEC_FREQ_MIN*0.5f;
1340 mesh->pvData[0][meta::beat_breather::FFT_MESH_POINTS+1] = SPEC_FREQ_MAX * 2.0f;
1341 mesh->pvData[1][0] = 0.0f;
1342 mesh->pvData[1][meta::beat_breather::FFT_MESH_POINTS+1] = 0.0f;
1343
1344 dsp::copy(&mesh->pvData[0][1], vFftFreqs, meta::beat_breather::FFT_MESH_POINTS);
1345 dsp::copy(&mesh->pvData[1][1], b->vFreqChart, meta::beat_breather::FFT_MESH_POINTS);
1346 mesh->data(2, meta::beat_breather::FFT_MESH_POINTS + 2);
1347
1348 b->nSync &= ~SYNC_BAND_FILTER;
1349 }
1350 }
1351
1352 // Synchronize peak detector mesh
1353 mesh = b->pPdMesh->buffer<plug::mesh_t>();
1354 if ((mesh != NULL) && (mesh->isEmpty()))
1355 {
1356 // Fill mesh with new values
1357 dsp::copy(mesh->pvData[0], vPdMesh, meta::beat_breather::TIME_MESH_POINTS);
1358 b->sPdMeter.read(mesh->pvData[1], meta::beat_breather::TIME_MESH_POINTS);
1359 mesh->data(2, meta::beat_breather::TIME_MESH_POINTS);
1360 }
1361
1362 // Synchronize peak filter function mesh
1363 if ((i == 0) && (b->nSync & SYNC_PEAK_FILTER))
1364 {
1365 mesh = (b->pPfMesh != NULL) ? b->pPfMesh->buffer<plug::mesh_t>() : NULL;
1366 if ((mesh != NULL) && (mesh->isEmpty()))
1367 {
1368 dsp::copy(mesh->pvData[0], vPfMesh, meta::beat_breather::CURVE_MESH_POINTS);
1369 dsp::copy(mesh->pvData[1], b->vPfMesh, meta::beat_breather::CURVE_MESH_POINTS);
1370 mesh->data(2, meta::beat_breather::CURVE_MESH_POINTS);
1371
1372 b->nSync &= ~SYNC_PEAK_FILTER;
1373 }
1374 }
1375
1376 // Synchronize beat processor function mesh
1377 if ((i == 0) && (b->nSync & SYNC_BEAT_PROC))
1378 {
1379 mesh = (b->pBpMesh != NULL) ? b->pBpMesh->buffer<plug::mesh_t>() : NULL;
1380 if ((mesh != NULL) && (mesh->isEmpty()))
1381 {
1382 dsp::copy(mesh->pvData[0], vBpMesh, meta::beat_breather::CURVE_MESH_POINTS);
1383 dsp::mul_k3(mesh->pvData[1], b->vBpMesh, b->fBpMakeup, meta::beat_breather::CURVE_MESH_POINTS);
1384 mesh->data(2, meta::beat_breather::CURVE_MESH_POINTS);
1385
1386 b->nSync &= ~SYNC_BEAT_PROC;
1387 }
1388 }
1389 }
1390
1391 // Output spectrum analysis for input channel
1392 mesh = ((sAnalyzer.channel_active(c->nAnIn)) && (c->pInMesh != NULL)) ? c->pInMesh->buffer<plug::mesh_t>() : NULL;
1393 if ((mesh != NULL) && (mesh->isEmpty()))
1394 {
1395 // Add extra points
1396 mesh->pvData[0][0] = SPEC_FREQ_MIN*0.5f;
1397 mesh->pvData[0][meta::beat_breather::FFT_MESH_POINTS+1] = SPEC_FREQ_MAX * 2.0f;
1398 mesh->pvData[1][0] = 0.0f;
1399 mesh->pvData[1][meta::beat_breather::FFT_MESH_POINTS+1] = 0.0f;
1400
1401 dsp::copy(&mesh->pvData[0][1], vFftFreqs, meta::beat_breather::FFT_MESH_POINTS);
1402 sAnalyzer.get_spectrum(c->nAnIn, &mesh->pvData[1][1], vFftIndexes, meta::beat_breather::FFT_MESH_POINTS);
1403
1404 // Mark mesh containing data
1405 mesh->data(2, meta::beat_breather::FFT_MESH_POINTS + 2);
1406 }
1407
1408 // Output spectrum analysis for output channel
1409 mesh = ((sAnalyzer.channel_active(c->nAnOut)) && (c->pOutMesh != NULL)) ? c->pOutMesh->buffer<plug::mesh_t>() : NULL;
1410 if ((mesh != NULL) && (mesh->isEmpty()))
1411 {
1412 dsp::copy(mesh->pvData[0], vFftFreqs, meta::beat_breather::FFT_MESH_POINTS);
1413 sAnalyzer.get_spectrum(c->nAnOut, mesh->pvData[1], vFftIndexes, meta::beat_breather::FFT_MESH_POINTS);
1414
1415 // Mark mesh containing data
1416 mesh->data(2, meta::beat_breather::FFT_MESH_POINTS);
1417 }
1418 }
1419 }
1420
1421 void beat_breather::ui_activated()
1422 {
1423 // Determine number of channels
1424 for (size_t i=0; i<nChannels; ++i)
1425 {
1426 channel_t *c = &vChannels[i];
1427
1428 for (size_t i=0; i<meta::beat_breather::BANDS_MAX; ++i)
1429 {
1430 band_t *b = &c->vBands[i];
1431 b->nSync = SYNC_ALL;
1432 }
1433 }
1434 }
1435
1436 bool beat_breather::inline_display(plug::ICanvas *cv, size_t width, size_t height)
1437 {
1438 // Check proportions
1439 if (height > (M_RGOLD_RATIO * width))
1440 height = M_RGOLD_RATIO * width;
1441
1442 // Init canvas
1443 if (!cv->init(width, height))
1444 return false;
1445 width = cv->width();
1446 height = cv->height();
1447
1448 // Clear background
1449 bool bypassing = vChannels[0].sBypass.bypassing();
1450 cv->set_color_rgb((bypassing) ? CV_DISABLED : CV_BACKGROUND);
1451 cv->paint();
1452
1453 // Draw axis
1454 cv->set_line_width(1.0);
1455
1456 // "-72 db / (:zoom ** 3)" max="24 db * :zoom"
1457
1458 float miny = logf(GAIN_AMP_M_72_DB / dsp::ipowf(fZoom, 3));
1459 float maxy = logf(GAIN_AMP_P_24_DB * fZoom);
1460
1461 float zx = 1.0f/SPEC_FREQ_MIN;
1462 float zy = dsp::ipowf(fZoom, 3)/GAIN_AMP_M_72_DB;
1463 float dx = width/(logf(SPEC_FREQ_MAX)-logf(SPEC_FREQ_MIN));
1464 float dy = height/(miny-maxy);
1465
1466 // Draw vertical lines
1467 cv->set_color_rgb(CV_YELLOW, 0.5f);
1468 for (float i=100.0f; i<SPEC_FREQ_MAX; i *= 10.0f)
1469 {
1470 float ax = dx*(logf(i*zx));
1471 cv->line(ax, 0, ax, height);
1472 }
1473
1474 // Draw horizontal lines
1475 cv->set_color_rgb(CV_WHITE, 0.5f);
1476 for (float i=GAIN_AMP_M_72_DB; i<GAIN_AMP_P_24_DB; i *= GAIN_AMP_P_12_DB)
1477 {
1478 float ay = height + dy*(logf(i*zy));
1479 cv->line(0, ay, width, ay);
1480 }
1481
1482 // Allocate buffer: f, x, y, tr
1483 pIDisplay = core::IDBuffer::reuse(pIDisplay, 4, width+2);
1484 core::IDBuffer *b = pIDisplay;
1485 if (b == NULL)
1486 return false;
1487
1488 // Initialize mesh
1489 b->v[0][0] = SPEC_FREQ_MIN*0.5f;
1490 b->v[0][width+1] = SPEC_FREQ_MAX*2.0f;
1491 b->v[3][0] = 1.0f;
1492 b->v[3][width+1] = 1.0f;
1493
1494 static const uint32_t c_colors[] =
1495 {
1496 CV_MIDDLE_CHANNEL,
1497 CV_LEFT_CHANNEL, CV_RIGHT_CHANNEL
1498 };
1499
1500 size_t channels = ((nChannels < 2) || (!bStereoSplit)) ? 1 : 2;
1501 const uint32_t *vc = (channels == 1) ? &c_colors[0] : &c_colors[1];
1502
1503 bool aa = cv->set_anti_aliasing(true);
1504 lsp_finally { cv->set_anti_aliasing(aa); };
1505 cv->set_line_width(2);
1506
1507 for (size_t i=0; i<channels; ++i)
1508 {
1509 channel_t *c = &vChannels[i];
1510
1511 for (size_t j=0; j<width; ++j)
1512 {
1513 size_t k = (j*meta::beat_breather::FFT_MESH_POINTS)/width;
1514 b->v[0][j+1] = vFftFreqs[k];
1515 b->v[3][j+1] = c->vFreqChart[k];
1516 }
1517
1518 dsp::fill(b->v[1], 0.0f, width+2);
1519 dsp::fill(b->v[2], height, width+2);
1520 dsp::axis_apply_log1(b->v[1], b->v[0], zx, dx, width+2);
1521 dsp::axis_apply_log1(b->v[2], b->v[3], zy, dy, width+2);
1522
1523 // Draw mesh
1524 uint32_t color = (bypassing || !(active())) ? CV_SILVER : vc[i];
1525 Color stroke(color), fill(color, 0.5f);
1526 cv->draw_poly(b->v[1], b->v[2], width+2, stroke, fill);
1527 }
1528
1529 return true;
1530 }
1531
1532 void beat_breather::dump(dspu::IStateDumper *v) const
1533 {
1534 plug::Module::dump(v);
1535
1536 v->write("nChannels", nChannels);
1537 v->begin_array("vChannels", vChannels, nChannels);
1538 {
1539 for (size_t i=0; i<nChannels; ++i)
1540 {
1541 const channel_t *c = &vChannels[i];
1542
1543 v->write_object("sBypass", &c->sBypass);
1544 v->write_object("sCrossover", &c->sCrossover);
1545 v->write_object("sDelay", &c->sDelay);
1546 v->write_object("sDryDelay", &c->sDryDelay);
1547
1548 v->begin_array("vBands", c->vBands, meta::beat_breather::BANDS_MAX);
1549 {
1550 for (size_t j=0; j<meta::beat_breather::BANDS_MAX; ++j)
1551 {
1552 const band_t *b = &c->vBands[j];
1553
1554 v->write_object("sDelay", &b->sDelay);
1555 v->write_object("sPdLong", &b->sPdLong);
1556 v->write_object("sPdShort", &b->sPdShort);
1557 v->write_object("sPdDelay", &b->sPdDelay);
1558 v->write_object("sPdMeter", &b->sPdMeter);
1559 v->write_object("sPf", &b->sPf);
1560 v->write_object("sPfDelay", &b->sPfDelay);
1561 v->write_object("sBp", &b->sBp);
1562 v->write_object("sBpScDelay", &b->sBpScDelay);
1563 v->write_object("sBpDelay", &b->sBpDelay);
1564
1565 v->write("nOldMode", b->nOldMode);
1566 v->write("nMode", b->nMode);
1567 v->write("fGain", b->fGain);
1568 v->write("fInLevel", b->fInLevel);
1569 v->write("fOutLevel", b->fOutLevel);
1570 v->write("fReduction", b->fReduction);
1571 v->write("nSync", b->nSync);
1572 v->write("fPdMakeup", b->fPdMakeup);
1573 v->write("fPdLevel", b->fPdLevel);
1574 v->write("fPfInGain", b->fPfInGain);
1575 v->write("fPfOutGain", b->fPfOutGain);
1576 v->write("fPfReduction", b->fPfReduction);
1577 v->write("fBpMakeup", b->fBpMakeup);
1578 v->write("fBpInGain", b->fBpInGain);
1579 v->write("fBpOutGain", b->fBpOutGain);
1580 v->write("fBpReduction", b->fBpReduction);
1581
1582 v->write("vInData", b->vInData);
1583 v->write("vPdData", b->vPdData);
1584 v->write("vPfData", b->vPfData);
1585 v->write("vBpData", b->vBpData);
1586 v->write("vFreqChart", b->vFreqChart);
1587 v->write("vPfMesh", b->vPfMesh);
1588 v->write("vBpMesh", b->vBpMesh);
1589
1590 v->write("pSolo", b->pSolo);
1591 v->write("pMute", b->pMute);
1592 v->write("pListen", b->pListen);
1593 v->write("pLpfSlope", b->pLpfSlope);
1594 v->write("pHpfSlope", b->pHpfSlope);
1595 v->write("pFlatten", b->pFlatten);
1596 v->write("pOutGain", b->pOutGain);
1597 v->write("pFreqEnd", b->pFreqEnd);
1598 v->write("pFreqMesh", b->pFreqMesh);
1599
1600 v->write("pInLevel", b->pInLevel);
1601 v->write("pOutLevel", b->pOutLevel);
1602
1603 v->write("pPdLongTime", b->pPdLongTime);
1604 v->write("pPdShortTime", b->pPdShortTime);
1605 v->write("pPdBias", b->pPdBias);
1606 v->write("pPdMakeup", b->pPdMakeup);
1607 v->write("pPdMesh", b->pPdMesh);
1608
1609 v->write("pPfLookahead", b->pPfLookahead);
1610 v->write("pPfAttack", b->pPfAttack);
1611 v->write("pPfRelease", b->pPfRelease);
1612 v->write("pPfThreshold", b->pPfThreshold);
1613 v->write("pPfReduction", b->pPfReduction);
1614 v->write("pPfZone", b->pPfZone);
1615 v->write("pPfMesh", b->pPfMesh);
1616 v->write("pPfEnvLevel", b->pPfEnvLevel);
1617 v->write("pPfCurveLevel", b->pPfCurveLevel);
1618 v->write("pPfGainLevel", b->pPfGainLevel);
1619
1620 v->write("pBpAttack", b->pBpAttack);
1621 v->write("pBpRelease", b->pBpRelease);
1622 v->write("pBpTimeShift", b->pBpTimeShift);
1623 v->write("pBpThreshold", b->pBpThreshold);
1624 v->write("pBpRatio", b->pBpRatio);
1625 v->write("pBpMaxGain", b->pBpMaxGain);
1626 v->write("pBpMesh", b->pBpMesh);
1627 v->write("pBpEnvLevel", b->pBpEnvLevel);
1628 v->write("pBpCurveLevel", b->pBpCurveLevel);
1629 v->write("pBpGainLevel", b->pBpGainLevel);
1630 }
1631 }
1632 v->end_array();
1633
1634 v->write("nAnIn", c->nAnIn);
1635 v->write("nAnOut", c->nAnOut);
1636 v->write("fInLevel", c->fInLevel);
1637 v->write("fOutLevel", c->fOutLevel);
1638
1639 v->write("vIn", c->vIn);
1640 v->write("vOut", c->vOut);
1641 v->write("vInData", c->vInData);
1642 v->write("vOutData", c->vOutData);
1643 v->write("vFreqChart", c->vFreqChart);
1644
1645 v->write("pIn", c->pIn);
1646 v->write("pOut", c->pOut);
1647
1648 v->write("pInLevel", c->pInLevel);
1649 v->write("pOutLevel", c->pOutLevel);
1650 v->write("pInFft", c->pInFft);
1651 v->write("pOutFft", c->pOutFft);
1652 v->write("pInMesh", c->pInMesh);
1653 v->write("pOutMesh", c->pOutMesh);
1654 v->write("pFreqMesh", c->pFreqMesh);
1655 }
1656 }
1657 v->end_array();
1658
1659 v->write("bStereoSplit", bStereoSplit);
1660 v->write("fInGain", fInGain);
1661 v->write("fDryGain", fDryGain);
1662 v->write("fWetGain", fWetGain);
1663 v->write("fZoom", fZoom);
1664 v->writev("vAnalyze", vAnalyze, 4);
1665
1666 v->write_object("sAnalyzer", &sAnalyzer);
1667 v->write_object("sCounter", &sCounter);
1668
1669 v->begin_array("vSplits", vSplits, meta::beat_breather::BANDS_MAX-1);
1670 {
1671 for (size_t i=0; i<meta::beat_breather::BANDS_MAX-1; ++i)
1672 {
1673 const split_t *sp = &vSplits[i];
1674 v->begin_object(sp, sizeof(split_t));
1675 {
1676 v->write("nBandId", sp->nBandId);
1677 v->write("fFrequency", sp->fFrequency);
1678 v->write("bEnabled", sp->bEnabled);
1679 v->write("pEnable", sp->pEnable);
1680 v->write("pFrequency", sp->pFrequency);
1681 }
1682 v->end_object();
1683 }
1684 }
1685 v->end_array();
1686
1687 v->write("vBuffer", vBuffer);
1688 v->write("vFftFreqs", vFftFreqs);
1689 v->write("vFftIndexes", vFftIndexes);
1690 v->write("vPdMesh", vPdMesh);
1691 v->write("vPfMesh", vPfMesh);
1692 v->write("vBpMesh", vBpMesh);
1693
1694 v->write("pBypass", pBypass);
1695 v->write("pInGain", pInGain);
1696 v->write("pDryGain", pDryGain);
1697 v->write("pWetGain", pWetGain);
1698 v->write("pDryWet", pDryWet);
1699 v->write("pOutGain", pOutGain);
1700 v->write("pStereoSplit", pStereoSplit);
1701 v->write("pFFTReactivity", pFFTReactivity);
1702 v->write("pFFTShift", pFFTShift);
1703 v->write("pZoom", pZoom);
1704 v->write("pIDisplay", pIDisplay);
1705 v->write("pData", pData);
1706 }
1707
1708 } /* namespace plugins */
1709 } /* namespace lsp */
1710
1711