2019-08-03 08:34:29 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016-2017 Damien Zammit <damien@zamaudio.com>
|
|
|
|
* Copyright (C) 2016-2017 Robin Gareus <robin@gareus.org>
|
|
|
|
* Copyright (C) 2017-2019 Johannes Mueller <github@johannes-mueller.org>
|
2016-07-02 21:21:44 -04:00
|
|
|
*
|
2019-08-03 08:34:29 -04:00
|
|
|
* 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 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2016-07-02 21:21:44 -04:00
|
|
|
*
|
|
|
|
* 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.
|
2019-08-03 08:34:29 -04:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2016-07-02 21:21:44 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
2016-07-04 06:59:04 -04:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
#ifdef LV2_EXTENDED
|
|
|
|
#include <cairo/cairo.h>
|
|
|
|
#include "ardour/lv2_extensions.h"
|
|
|
|
#endif
|
2016-07-02 21:21:44 -04:00
|
|
|
|
2023-05-06 10:18:24 -04:00
|
|
|
#ifdef HAVE_LV2_1_18_6
|
2023-05-06 12:28:35 -04:00
|
|
|
#include <lv2/core/lv2.h>
|
2023-05-06 10:18:24 -04:00
|
|
|
#else
|
|
|
|
#include <lv2/lv2plug.in/ns/lv2core/lv2.h>
|
|
|
|
#endif
|
2016-07-02 21:21:44 -04:00
|
|
|
|
2016-07-04 12:13:22 -04:00
|
|
|
#define ACOMP_URI "urn:ardour:a-comp"
|
|
|
|
#define ACOMP_STEREO_URI "urn:ardour:a-comp#stereo"
|
2016-07-02 21:21:44 -04:00
|
|
|
|
2017-08-07 07:23:31 -04:00
|
|
|
#define RESET_PEAK_AFTER_SECONDS 3
|
|
|
|
|
2016-08-27 11:14:49 -04:00
|
|
|
#ifndef M_PI
|
|
|
|
# define M_PI 3.14159265358979323846
|
|
|
|
#endif
|
|
|
|
|
2020-08-07 10:30:46 -04:00
|
|
|
#define MINUS_60 0.0001f
|
|
|
|
|
2016-11-29 03:04:13 -05:00
|
|
|
#ifdef COMPILER_MSVC
|
|
|
|
#include <float.h>
|
|
|
|
#define isfinite_local(val) (bool)_finite((double)val)
|
|
|
|
#else
|
|
|
|
#define isfinite_local isfinite
|
|
|
|
#endif
|
|
|
|
|
2018-06-09 12:02:05 -04:00
|
|
|
#ifndef FLT_EPSILON
|
|
|
|
# define FLT_EPSILON 1.192093e-07
|
|
|
|
#endif
|
|
|
|
|
2016-07-02 21:21:44 -04:00
|
|
|
typedef enum {
|
2016-07-04 12:13:22 -04:00
|
|
|
ACOMP_ATTACK = 0,
|
2016-07-02 21:21:44 -04:00
|
|
|
ACOMP_RELEASE,
|
|
|
|
ACOMP_KNEE,
|
|
|
|
ACOMP_RATIO,
|
|
|
|
ACOMP_THRESHOLD,
|
|
|
|
ACOMP_MAKEUP,
|
|
|
|
|
|
|
|
ACOMP_GAINR,
|
2017-08-04 08:07:01 -04:00
|
|
|
ACOMP_INLEVEL,
|
2017-08-14 14:22:05 -04:00
|
|
|
ACOMP_OUTLEVEL,
|
2016-07-02 21:21:44 -04:00
|
|
|
ACOMP_SIDECHAIN,
|
2016-08-27 11:14:49 -04:00
|
|
|
ACOMP_ENABLE,
|
2016-07-02 21:21:44 -04:00
|
|
|
|
2016-08-27 12:08:46 -04:00
|
|
|
ACOMP_A0,
|
|
|
|
ACOMP_A1,
|
|
|
|
ACOMP_A2,
|
|
|
|
ACOMP_A3,
|
|
|
|
ACOMP_A4,
|
2016-08-27 11:14:49 -04:00
|
|
|
} PortIndex;
|
2016-07-02 21:21:44 -04:00
|
|
|
|
2016-07-04 12:13:22 -04:00
|
|
|
typedef struct {
|
2016-07-02 21:21:44 -04:00
|
|
|
float* attack;
|
|
|
|
float* release;
|
|
|
|
float* knee;
|
|
|
|
float* ratio;
|
|
|
|
float* thresdb;
|
|
|
|
float* makeup;
|
|
|
|
|
|
|
|
float* gainr;
|
|
|
|
float* outlevel;
|
2017-08-04 08:07:01 -04:00
|
|
|
float* inlevel;
|
2016-07-02 21:21:44 -04:00
|
|
|
float* sidechain;
|
2016-08-27 11:14:49 -04:00
|
|
|
float* enable;
|
2016-07-02 21:21:44 -04:00
|
|
|
|
2016-07-04 12:13:22 -04:00
|
|
|
float* input0;
|
|
|
|
float* input1;
|
|
|
|
float* sc;
|
|
|
|
float* output0;
|
|
|
|
float* output1;
|
|
|
|
|
2018-06-14 16:29:13 -04:00
|
|
|
uint32_t n_channels;
|
|
|
|
|
2016-07-02 21:21:44 -04:00
|
|
|
float srate;
|
2017-08-03 06:05:46 -04:00
|
|
|
|
2016-08-27 11:14:49 -04:00
|
|
|
float makeup_gain;
|
|
|
|
|
2016-07-04 06:59:04 -04:00
|
|
|
#ifdef LV2_EXTENDED
|
|
|
|
LV2_Inline_Display_Image_Surface surf;
|
2016-07-12 07:44:55 -04:00
|
|
|
bool need_expose;
|
2016-07-04 06:59:04 -04:00
|
|
|
cairo_surface_t* display;
|
|
|
|
LV2_Inline_Display* queue_draw;
|
|
|
|
uint32_t w, h;
|
|
|
|
|
|
|
|
/* ports pointers are only valid during run so we'll
|
|
|
|
* have to cache them for the display, besides
|
|
|
|
* we do want to check for changes
|
|
|
|
*/
|
|
|
|
float v_knee;
|
|
|
|
float v_ratio;
|
|
|
|
float v_thresdb;
|
2017-07-13 09:35:12 -04:00
|
|
|
float v_gainr;
|
2017-06-18 17:41:23 -04:00
|
|
|
float v_makeup;
|
2016-07-04 09:49:39 -04:00
|
|
|
float v_lvl_in;
|
|
|
|
float v_lvl_out;
|
2017-08-04 05:07:33 -04:00
|
|
|
float v_state_x;
|
2017-08-07 07:23:31 -04:00
|
|
|
|
|
|
|
float v_peakdb;
|
|
|
|
uint32_t peakdb_samples;
|
2016-07-04 06:59:04 -04:00
|
|
|
#endif
|
2016-07-02 21:21:44 -04:00
|
|
|
} AComp;
|
|
|
|
|
|
|
|
static LV2_Handle
|
|
|
|
instantiate(const LV2_Descriptor* descriptor,
|
|
|
|
double rate,
|
|
|
|
const char* bundle_path,
|
|
|
|
const LV2_Feature* const* features)
|
|
|
|
{
|
2016-07-04 06:59:04 -04:00
|
|
|
AComp* acomp = (AComp*)calloc(1, sizeof(AComp));
|
|
|
|
|
2018-06-14 16:29:13 -04:00
|
|
|
if (!strcmp (descriptor->URI, ACOMP_URI)) {
|
|
|
|
acomp->n_channels = 1;
|
|
|
|
} else if (!strcmp (descriptor->URI, ACOMP_STEREO_URI)) {
|
|
|
|
acomp->n_channels = 2;
|
|
|
|
} else {
|
|
|
|
free (acomp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-07-04 06:59:04 -04:00
|
|
|
for (int i=0; features[i]; ++i) {
|
|
|
|
#ifdef LV2_EXTENDED
|
|
|
|
if (!strcmp(features[i]->URI, LV2_INLINEDISPLAY__queue_draw)) {
|
|
|
|
acomp->queue_draw = (LV2_Inline_Display*) features[i]->data;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2016-07-02 21:21:44 -04:00
|
|
|
|
2016-07-04 06:59:04 -04:00
|
|
|
acomp->srate = rate;
|
2017-11-10 21:59:41 -05:00
|
|
|
acomp->makeup_gain = 1.f;
|
2016-07-12 07:44:55 -04:00
|
|
|
#ifdef LV2_EXTENDED
|
2016-07-04 06:59:04 -04:00
|
|
|
acomp->need_expose = true;
|
2017-07-13 15:28:17 -04:00
|
|
|
acomp->v_lvl_out = -70.f;
|
2016-07-12 07:44:55 -04:00
|
|
|
#endif
|
2016-07-02 21:21:44 -04:00
|
|
|
|
|
|
|
return (LV2_Handle)acomp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-08-27 11:14:49 -04:00
|
|
|
connect_port(LV2_Handle instance,
|
2016-07-02 21:21:44 -04:00
|
|
|
uint32_t port,
|
|
|
|
void* data)
|
|
|
|
{
|
|
|
|
AComp* acomp = (AComp*)instance;
|
|
|
|
|
2016-08-27 11:14:49 -04:00
|
|
|
switch ((PortIndex)port) {
|
2016-08-27 12:08:46 -04:00
|
|
|
case ACOMP_ATTACK:
|
|
|
|
acomp->attack = (float*)data;
|
|
|
|
break;
|
|
|
|
case ACOMP_RELEASE:
|
|
|
|
acomp->release = (float*)data;
|
|
|
|
break;
|
|
|
|
case ACOMP_KNEE:
|
|
|
|
acomp->knee = (float*)data;
|
|
|
|
break;
|
|
|
|
case ACOMP_RATIO:
|
|
|
|
acomp->ratio = (float*)data;
|
|
|
|
break;
|
|
|
|
case ACOMP_THRESHOLD:
|
|
|
|
acomp->thresdb = (float*)data;
|
|
|
|
break;
|
|
|
|
case ACOMP_MAKEUP:
|
|
|
|
acomp->makeup = (float*)data;
|
|
|
|
break;
|
|
|
|
case ACOMP_GAINR:
|
|
|
|
acomp->gainr = (float*)data;
|
|
|
|
break;
|
|
|
|
case ACOMP_OUTLEVEL:
|
|
|
|
acomp->outlevel = (float*)data;
|
|
|
|
break;
|
2017-08-04 08:07:01 -04:00
|
|
|
case ACOMP_INLEVEL:
|
|
|
|
acomp->inlevel = (float*)data;
|
|
|
|
break;
|
2016-08-27 12:08:46 -04:00
|
|
|
case ACOMP_SIDECHAIN:
|
|
|
|
acomp->sidechain = (float*)data;
|
|
|
|
break;
|
|
|
|
case ACOMP_ENABLE:
|
|
|
|
acomp->enable = (float*)data;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
connect_mono(LV2_Handle instance,
|
|
|
|
uint32_t port,
|
|
|
|
void* data)
|
|
|
|
{
|
|
|
|
AComp* acomp = (AComp*)instance;
|
|
|
|
connect_port (instance, port, data);
|
|
|
|
|
|
|
|
switch ((PortIndex)port) {
|
|
|
|
case ACOMP_A0:
|
|
|
|
acomp->input0 = (float*)data;
|
|
|
|
break;
|
|
|
|
case ACOMP_A1:
|
|
|
|
acomp->sc = (float*)data;
|
|
|
|
break;
|
|
|
|
case ACOMP_A2:
|
|
|
|
acomp->output0 = (float*)data;
|
|
|
|
break;
|
|
|
|
default:
|
2016-07-04 12:13:22 -04:00
|
|
|
break;
|
2016-08-27 12:08:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
connect_stereo(LV2_Handle instance,
|
|
|
|
uint32_t port,
|
|
|
|
void* data)
|
|
|
|
{
|
|
|
|
AComp* acomp = (AComp*)instance;
|
|
|
|
connect_port (instance, port, data);
|
|
|
|
|
|
|
|
switch ((PortIndex)port) {
|
|
|
|
case ACOMP_A0:
|
|
|
|
acomp->input0 = (float*)data;
|
|
|
|
break;
|
|
|
|
case ACOMP_A1:
|
|
|
|
acomp->input1 = (float*)data;
|
|
|
|
break;
|
|
|
|
case ACOMP_A2:
|
|
|
|
acomp->sc = (float*)data;
|
|
|
|
break;
|
|
|
|
case ACOMP_A3:
|
|
|
|
acomp->output0 = (float*)data;
|
|
|
|
break;
|
|
|
|
case ACOMP_A4:
|
|
|
|
acomp->output1 = (float*)data;
|
|
|
|
break;
|
|
|
|
default:
|
2016-07-02 21:21:44 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force already-denormal float value to zero
|
|
|
|
static inline float
|
|
|
|
sanitize_denormal(float value) {
|
|
|
|
if (!isnormal(value)) {
|
|
|
|
value = 0.f;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline float
|
|
|
|
from_dB(float gdb) {
|
2020-03-30 15:59:07 -04:00
|
|
|
return powf (10.0f, 0.05f * gdb);
|
2016-07-02 21:21:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline float
|
|
|
|
to_dB(float g) {
|
2020-03-30 15:59:07 -04:00
|
|
|
return (20.f * log10f (g));
|
2016-07-02 21:21:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
activate(LV2_Handle instance)
|
|
|
|
{
|
|
|
|
AComp* acomp = (AComp*)instance;
|
|
|
|
|
|
|
|
*(acomp->gainr) = 0.0f;
|
2017-08-04 08:05:16 -04:00
|
|
|
*(acomp->outlevel) = -70.0f;
|
2017-08-04 08:07:01 -04:00
|
|
|
*(acomp->inlevel) = -160.f;
|
2017-08-07 07:23:31 -04:00
|
|
|
|
|
|
|
#ifdef LV2_EXTENDED
|
|
|
|
acomp->v_peakdb = -160.f;
|
|
|
|
acomp->peakdb_samples = 0;
|
|
|
|
#endif
|
2016-07-02 21:21:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-06-14 16:29:13 -04:00
|
|
|
run(LV2_Handle instance, uint32_t n_samples)
|
2016-07-02 21:21:44 -04:00
|
|
|
{
|
|
|
|
AComp* acomp = (AComp*)instance;
|
|
|
|
|
2018-06-14 16:29:13 -04:00
|
|
|
const float* const ins[2] = { acomp->input0, acomp->input1 };
|
2016-07-04 12:13:22 -04:00
|
|
|
const float* const sc = acomp->sc;
|
2018-06-14 16:29:13 -04:00
|
|
|
float* const outs[2] = { acomp->output0, acomp->output1 };
|
2016-07-02 21:21:44 -04:00
|
|
|
|
|
|
|
float srate = acomp->srate;
|
|
|
|
float width = (6.f * *(acomp->knee)) + 0.01;
|
2020-03-30 15:59:07 -04:00
|
|
|
float attack_coeff = expf (-1000.f / (*(acomp->attack) * srate));
|
|
|
|
float release_coeff = expf (-1000.f / (*(acomp->release) * srate));
|
2016-07-02 21:21:44 -04:00
|
|
|
|
2018-06-14 16:29:13 -04:00
|
|
|
float max_out = 0.f;
|
2016-07-02 21:21:44 -04:00
|
|
|
float Lgain = 1.f;
|
2017-08-03 06:05:46 -04:00
|
|
|
float Lxg, Lyg;
|
|
|
|
float current_gainr;
|
|
|
|
float old_gainr = *acomp->gainr;
|
|
|
|
|
2016-08-27 11:14:49 -04:00
|
|
|
int usesidechain = (*(acomp->sidechain) <= 0.f) ? 0 : 1;
|
2016-07-02 21:21:44 -04:00
|
|
|
uint32_t i;
|
|
|
|
float ingain;
|
2016-07-04 12:13:22 -04:00
|
|
|
float sc0;
|
2018-06-14 16:29:13 -04:00
|
|
|
float maxabs;
|
2016-08-27 11:14:49 -04:00
|
|
|
|
2018-06-14 16:29:13 -04:00
|
|
|
uint32_t n_channels = acomp->n_channels;
|
2016-08-27 11:14:49 -04:00
|
|
|
|
|
|
|
float ratio = *acomp->ratio;
|
|
|
|
float thresdb = *acomp->thresdb;
|
2017-06-18 17:56:23 -04:00
|
|
|
float makeup = *acomp->makeup;
|
|
|
|
float makeup_target = from_dB(makeup);
|
2016-08-27 11:14:49 -04:00
|
|
|
float makeup_gain = acomp->makeup_gain;
|
|
|
|
|
2020-03-30 15:59:07 -04:00
|
|
|
const float tau = (1.f - expf (-2.f * M_PI * 25.f / acomp->srate));
|
2016-08-27 11:14:49 -04:00
|
|
|
|
|
|
|
if (*acomp->enable <= 0) {
|
|
|
|
ratio = 1.f;
|
|
|
|
thresdb = 0.f;
|
2017-06-25 03:54:05 -04:00
|
|
|
makeup = 0.f;
|
2016-08-27 11:14:49 -04:00
|
|
|
makeup_target = 1.f;
|
|
|
|
}
|
2016-07-04 12:13:22 -04:00
|
|
|
|
|
|
|
#ifdef LV2_EXTENDED
|
|
|
|
if (acomp->v_knee != *acomp->knee) {
|
|
|
|
acomp->v_knee = *acomp->knee;
|
|
|
|
acomp->need_expose = true;
|
|
|
|
}
|
|
|
|
|
2016-08-27 11:14:49 -04:00
|
|
|
if (acomp->v_ratio != ratio) {
|
|
|
|
acomp->v_ratio = ratio;
|
2016-07-04 12:13:22 -04:00
|
|
|
acomp->need_expose = true;
|
|
|
|
}
|
|
|
|
|
2016-08-27 11:14:49 -04:00
|
|
|
if (acomp->v_thresdb != thresdb) {
|
|
|
|
acomp->v_thresdb = thresdb;
|
2016-07-04 12:13:22 -04:00
|
|
|
acomp->need_expose = true;
|
|
|
|
}
|
2017-06-18 17:56:23 -04:00
|
|
|
|
|
|
|
if (acomp->v_makeup != makeup) {
|
|
|
|
acomp->v_makeup = makeup;
|
|
|
|
acomp->need_expose = true;
|
|
|
|
}
|
2016-07-04 12:13:22 -04:00
|
|
|
#endif
|
|
|
|
|
2017-08-04 05:07:33 -04:00
|
|
|
float in_peak_db = -160.f;
|
2017-08-07 05:10:54 -04:00
|
|
|
float max_gainr = 0.f;
|
2016-07-04 12:13:22 -04:00
|
|
|
|
|
|
|
for (i = 0; i < n_samples; i++) {
|
2018-06-14 16:29:13 -04:00
|
|
|
maxabs = 0.f;
|
|
|
|
for (uint32_t c=0; c<n_channels; ++c) {
|
|
|
|
maxabs = fmaxf(fabsf(ins[c][i]), maxabs);
|
|
|
|
}
|
2016-07-04 12:13:22 -04:00
|
|
|
sc0 = sc[i];
|
2018-06-14 16:29:13 -04:00
|
|
|
ingain = usesidechain ? fabs(sc0) : maxabs;
|
2016-07-04 12:13:22 -04:00
|
|
|
Lyg = 0.f;
|
|
|
|
Lxg = (ingain==0.f) ? -160.f : to_dB(ingain);
|
|
|
|
Lxg = sanitize_denormal(Lxg);
|
2019-04-15 09:33:44 -04:00
|
|
|
|
2017-08-04 05:07:33 -04:00
|
|
|
if (Lxg > in_peak_db) {
|
|
|
|
in_peak_db = Lxg;
|
|
|
|
}
|
2016-07-04 12:13:22 -04:00
|
|
|
|
|
|
|
if (2.f*(Lxg-thresdb) < -width) {
|
|
|
|
Lyg = Lxg;
|
2016-07-20 06:10:46 -04:00
|
|
|
} else if (2.f*(Lxg-thresdb) > width) {
|
2016-07-04 12:13:22 -04:00
|
|
|
Lyg = thresdb + (Lxg-thresdb)/ratio;
|
|
|
|
Lyg = sanitize_denormal(Lyg);
|
2016-07-20 06:10:46 -04:00
|
|
|
} else {
|
|
|
|
Lyg = Lxg + (1.f/ratio-1.f)*(Lxg-thresdb+width/2.f)*(Lxg-thresdb+width/2.f)/(2.f*width);
|
2016-07-04 12:13:22 -04:00
|
|
|
}
|
|
|
|
|
2017-08-03 06:05:46 -04:00
|
|
|
current_gainr = Lxg - Lyg;
|
|
|
|
|
|
|
|
if (current_gainr < old_gainr) {
|
|
|
|
current_gainr = release_coeff*old_gainr + (1.f-release_coeff)*current_gainr;
|
|
|
|
} else if (current_gainr > old_gainr) {
|
|
|
|
current_gainr = attack_coeff*old_gainr + (1.f-attack_coeff)*current_gainr;
|
|
|
|
}
|
2016-07-04 12:13:22 -04:00
|
|
|
|
2017-08-03 06:05:46 -04:00
|
|
|
current_gainr = sanitize_denormal(current_gainr);
|
2016-07-04 12:13:22 -04:00
|
|
|
|
2017-08-03 06:05:46 -04:00
|
|
|
Lgain = from_dB(-current_gainr);
|
2016-07-04 12:13:22 -04:00
|
|
|
|
2017-08-03 06:05:46 -04:00
|
|
|
old_gainr = current_gainr;
|
|
|
|
|
|
|
|
*(acomp->gainr) = current_gainr;
|
|
|
|
if (current_gainr > max_gainr) {
|
|
|
|
max_gainr = current_gainr;
|
2017-07-13 09:35:12 -04:00
|
|
|
}
|
2016-07-04 12:13:22 -04:00
|
|
|
|
2018-06-09 12:02:05 -04:00
|
|
|
makeup_gain += tau * (makeup_target - makeup_gain);
|
|
|
|
|
2018-06-14 16:29:13 -04:00
|
|
|
for (uint32_t c=0; c<n_channels; ++c) {
|
|
|
|
float out = ins[c][i] * Lgain * makeup_gain;
|
|
|
|
outs[c][i] = out;
|
|
|
|
out = fabsf (out);
|
|
|
|
if (out > max_out) {
|
|
|
|
max_out = out;
|
|
|
|
sanitize_denormal(max_out);
|
|
|
|
}
|
|
|
|
}
|
2016-07-04 12:13:22 -04:00
|
|
|
}
|
|
|
|
|
2018-06-09 12:02:05 -04:00
|
|
|
if (fabsf(tau * (makeup_gain - makeup_target)) < FLT_EPSILON*makeup_gain) {
|
2017-11-11 08:16:44 -05:00
|
|
|
makeup_gain = makeup_target;
|
2017-11-10 21:59:41 -05:00
|
|
|
}
|
|
|
|
|
2020-08-07 10:30:46 -04:00
|
|
|
*(acomp->outlevel) = (max_out < MINUS_60) ? -60.f : to_dB(max_out);
|
2017-08-04 08:07:01 -04:00
|
|
|
*(acomp->inlevel) = in_peak_db;
|
2016-08-27 11:14:49 -04:00
|
|
|
acomp->makeup_gain = makeup_gain;
|
2016-07-04 12:13:22 -04:00
|
|
|
|
|
|
|
#ifdef LV2_EXTENDED
|
2017-08-07 05:10:54 -04:00
|
|
|
acomp->v_gainr = max_gainr;
|
|
|
|
|
2017-08-07 07:23:31 -04:00
|
|
|
if (in_peak_db > acomp->v_peakdb) {
|
|
|
|
acomp->v_peakdb = in_peak_db;
|
|
|
|
acomp->peakdb_samples = 0;
|
|
|
|
} else {
|
|
|
|
acomp->peakdb_samples += n_samples;
|
|
|
|
if ((float)acomp->peakdb_samples/acomp->srate > RESET_PEAK_AFTER_SECONDS) {
|
|
|
|
acomp->v_peakdb = in_peak_db;
|
|
|
|
acomp->peakdb_samples = 0;
|
|
|
|
acomp->need_expose = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-04 05:07:33 -04:00
|
|
|
const float v_lvl_in = in_peak_db;
|
|
|
|
const float v_lvl_out = *acomp->outlevel;
|
2017-08-03 06:05:46 -04:00
|
|
|
|
2017-08-04 05:07:33 -04:00
|
|
|
float state_x;
|
2017-08-03 06:05:46 -04:00
|
|
|
|
2017-08-04 05:07:33 -04:00
|
|
|
const float knee_lim_gr = (1.f - 1.f/ratio) * width/2.f;
|
2017-08-03 06:05:46 -04:00
|
|
|
|
2017-08-04 05:07:33 -04:00
|
|
|
if (acomp->v_gainr > knee_lim_gr) {
|
|
|
|
state_x = acomp->v_gainr / (1.f - 1.f/ratio) + thresdb;
|
|
|
|
} else {
|
2020-03-30 15:59:07 -04:00
|
|
|
state_x = sqrtf ( (2.f*width*acomp->v_gainr) / (1.f-1.f/ratio) ) + thresdb - width/2.f;
|
2016-11-29 03:04:13 -05:00
|
|
|
}
|
2017-08-03 06:05:46 -04:00
|
|
|
|
2017-08-04 05:07:33 -04:00
|
|
|
if (fabsf (acomp->v_lvl_out - v_lvl_out) >= .1f ||
|
|
|
|
fabsf (acomp->v_lvl_in - v_lvl_in) >= .1f ||
|
|
|
|
fabsf (acomp->v_state_x - state_x) >= .1f ) {
|
|
|
|
// >= 0.1dB difference
|
2016-07-04 12:13:22 -04:00
|
|
|
acomp->need_expose = true;
|
2017-08-07 07:23:31 -04:00
|
|
|
acomp->v_lvl_in = v_lvl_in;
|
|
|
|
acomp->v_lvl_out = v_lvl_out;
|
2017-08-04 05:07:33 -04:00
|
|
|
acomp->v_state_x = state_x;
|
2016-07-04 12:13:22 -04:00
|
|
|
}
|
|
|
|
if (acomp->need_expose && acomp->queue_draw) {
|
|
|
|
acomp->need_expose = false;
|
|
|
|
acomp->queue_draw->queue_draw (acomp->queue_draw->handle);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-07-02 21:21:44 -04:00
|
|
|
static void
|
|
|
|
deactivate(LV2_Handle instance)
|
|
|
|
{
|
2016-07-04 05:41:15 -04:00
|
|
|
activate(instance);
|
2016-07-02 21:21:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cleanup(LV2_Handle instance)
|
|
|
|
{
|
2016-07-04 06:59:04 -04:00
|
|
|
#ifdef LV2_EXTENDED
|
|
|
|
AComp* acomp = (AComp*)instance;
|
|
|
|
if (acomp->display) {
|
|
|
|
cairo_surface_destroy (acomp->display);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-07-02 21:21:44 -04:00
|
|
|
free(instance);
|
|
|
|
}
|
|
|
|
|
2016-07-04 06:59:04 -04:00
|
|
|
|
|
|
|
#ifndef MIN
|
|
|
|
#define MIN(A,B) ((A) < (B)) ? (A) : (B)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef LV2_EXTENDED
|
|
|
|
static float
|
2017-07-13 15:28:17 -04:00
|
|
|
comp_curve (const AComp* self, float xg) {
|
2016-07-04 06:59:04 -04:00
|
|
|
const float knee = self->v_knee;
|
|
|
|
const float ratio = self->v_ratio;
|
|
|
|
const float thresdb = self->v_thresdb;
|
2017-06-18 17:41:23 -04:00
|
|
|
const float makeup = self->v_makeup;
|
2016-07-04 06:59:04 -04:00
|
|
|
|
|
|
|
const float width = 6.f * knee + 0.01f;
|
2016-07-04 08:07:05 -04:00
|
|
|
float yg = 0.f;
|
2016-07-04 06:59:04 -04:00
|
|
|
|
|
|
|
if (2.f * (xg - thresdb) < -width) {
|
|
|
|
yg = xg;
|
|
|
|
} else if (2.f * (xg - thresdb) > width) {
|
|
|
|
yg = thresdb + (xg - thresdb) / ratio;
|
2016-07-20 06:10:46 -04:00
|
|
|
} else {
|
|
|
|
yg = xg + (1.f / ratio - 1.f ) * (xg - thresdb + width / 2.f) * (xg - thresdb + width / 2.f) / (2.f * width);
|
2016-07-04 06:59:04 -04:00
|
|
|
}
|
2017-06-18 17:41:23 -04:00
|
|
|
|
|
|
|
yg += makeup;
|
|
|
|
|
2016-07-04 08:07:05 -04:00
|
|
|
return yg;
|
2016-07-04 06:59:04 -04:00
|
|
|
}
|
|
|
|
|
2018-06-17 05:42:49 -04:00
|
|
|
|
|
|
|
#include "dynamic_display.c"
|
|
|
|
|
2017-07-13 15:28:17 -04:00
|
|
|
static void
|
|
|
|
render_inline_full (cairo_t* cr, const AComp* self)
|
2016-07-04 06:59:04 -04:00
|
|
|
{
|
2017-07-13 15:28:17 -04:00
|
|
|
const float w = self->w;
|
|
|
|
const float h = self->h;
|
2016-07-04 06:59:04 -04:00
|
|
|
|
2017-06-18 18:06:10 -04:00
|
|
|
const float makeup_thres = self->v_thresdb + self->v_makeup;
|
|
|
|
|
2018-06-17 05:42:49 -04:00
|
|
|
draw_grid (cr, w,h);
|
2016-07-04 08:07:05 -04:00
|
|
|
|
2016-07-04 09:49:39 -04:00
|
|
|
if (self->v_thresdb < 0) {
|
2020-08-11 10:04:23 -04:00
|
|
|
const float x = w * (1.f - (10.f-self->v_thresdb)/70.f) + 0.5;
|
|
|
|
cairo_move_to (cr, x, 0);
|
|
|
|
cairo_line_to (cr, x, h);
|
2016-07-04 09:49:39 -04:00
|
|
|
cairo_stroke (cr);
|
|
|
|
}
|
2016-07-04 08:07:05 -04:00
|
|
|
|
2018-06-17 05:42:49 -04:00
|
|
|
draw_GR_bar (cr, w,h, self->v_gainr);
|
2016-07-04 08:07:05 -04:00
|
|
|
|
2017-08-04 05:07:33 -04:00
|
|
|
// draw state
|
|
|
|
cairo_set_source_rgba (cr, .8, .8, .8, 1.0);
|
|
|
|
|
2020-08-11 09:16:45 -04:00
|
|
|
const float state_x = w * (1.f - (10.f-(*self->inlevel))/70.f);
|
|
|
|
const float state_y = h * ((*self->outlevel) - 10.f) / -70.f;
|
2017-08-04 05:07:33 -04:00
|
|
|
|
2020-08-11 09:16:45 -04:00
|
|
|
cairo_arc (cr, state_x, state_y, 6.f, 0.f, 2.f*M_PI);
|
2017-08-04 05:07:33 -04:00
|
|
|
cairo_fill (cr);
|
|
|
|
|
2016-07-04 08:07:05 -04:00
|
|
|
// draw curve
|
2016-07-04 06:59:04 -04:00
|
|
|
cairo_set_source_rgba (cr, .8, .8, .8, 1.0);
|
2016-07-04 08:07:05 -04:00
|
|
|
cairo_move_to (cr, 0, h);
|
2016-07-04 06:59:04 -04:00
|
|
|
|
|
|
|
for (uint32_t x = 0; x < w; ++x) {
|
2017-07-13 17:05:49 -04:00
|
|
|
// plot -60..+10 dB
|
|
|
|
const float x_db = 70.f * (-1.f + x / (float)w) + 10.f;
|
|
|
|
const float y_db = comp_curve (self, x_db) - 10.f;
|
|
|
|
const float y = h * (y_db / -70.f);
|
2016-07-04 06:59:04 -04:00
|
|
|
cairo_line_to (cr, x, y);
|
|
|
|
}
|
2016-07-04 08:07:05 -04:00
|
|
|
cairo_stroke_preserve (cr);
|
|
|
|
|
|
|
|
cairo_line_to (cr, w, h);
|
|
|
|
cairo_close_path (cr);
|
|
|
|
cairo_clip (cr);
|
|
|
|
|
2016-07-04 09:49:39 -04:00
|
|
|
// draw signal level & reduction/gradient
|
2017-07-13 17:05:49 -04:00
|
|
|
const float top = comp_curve (self, 0) - 10.f;
|
2016-07-04 09:49:39 -04:00
|
|
|
cairo_pattern_t* pat = cairo_pattern_create_linear (0.0, 0.0, 0.0, h);
|
2017-07-13 17:05:49 -04:00
|
|
|
if (top > makeup_thres - 10.f) {
|
2016-07-04 09:49:39 -04:00
|
|
|
cairo_pattern_add_color_stop_rgba (pat, 0.0, 0.8, 0.1, 0.1, 0.5);
|
2017-07-13 17:05:49 -04:00
|
|
|
cairo_pattern_add_color_stop_rgba (pat, top / -70.f, 0.8, 0.1, 0.1, 0.5);
|
2016-07-04 09:49:39 -04:00
|
|
|
}
|
|
|
|
if (self->v_knee > 0) {
|
2017-07-13 17:05:49 -04:00
|
|
|
cairo_pattern_add_color_stop_rgba (pat, ((makeup_thres -10.f) / -70.f), 0.7, 0.7, 0.2, 0.5);
|
|
|
|
cairo_pattern_add_color_stop_rgba (pat, ((makeup_thres - self->v_knee - 10.f) / -70.f), 0.5, 0.5, 0.5, 0.5);
|
2016-07-04 09:49:39 -04:00
|
|
|
} else {
|
2017-07-13 17:05:49 -04:00
|
|
|
cairo_pattern_add_color_stop_rgba (pat, ((makeup_thres - 10.f)/ -70.f), 0.7, 0.7, 0.2, 0.5);
|
|
|
|
cairo_pattern_add_color_stop_rgba (pat, ((makeup_thres - 10.01f) / -70.f), 0.5, 0.5, 0.5, 0.5);
|
2016-07-04 09:49:39 -04:00
|
|
|
}
|
|
|
|
cairo_pattern_add_color_stop_rgba (pat, 1.0, 0.5, 0.5, 0.5, 0.5);
|
|
|
|
|
2016-07-04 08:26:37 -04:00
|
|
|
// maybe cut off at x-position?
|
2017-07-13 17:05:49 -04:00
|
|
|
const float x = w * (self->v_lvl_in + 60) / 70.f;
|
2017-06-18 17:41:23 -04:00
|
|
|
const float y = x + h*self->v_makeup;
|
|
|
|
cairo_rectangle (cr, 0, h - y, x, y);
|
2016-07-04 09:49:39 -04:00
|
|
|
if (self->v_ratio > 1.0) {
|
|
|
|
cairo_set_source (cr, pat);
|
|
|
|
} else {
|
|
|
|
cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
|
|
|
|
}
|
2016-07-04 08:07:05 -04:00
|
|
|
cairo_fill (cr);
|
2016-07-04 06:59:04 -04:00
|
|
|
|
2016-07-04 09:49:39 -04:00
|
|
|
cairo_pattern_destroy (pat); // TODO cache pattern
|
2017-07-13 15:28:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
render_inline_only_bars (cairo_t* cr, const AComp* self)
|
|
|
|
{
|
2018-06-17 05:42:49 -04:00
|
|
|
draw_inline_bars (cr, self->w, self->h,
|
|
|
|
self->v_thresdb, self->v_ratio,
|
|
|
|
self->v_peakdb, self->v_gainr,
|
|
|
|
self->v_lvl_in, self->v_lvl_out);
|
2017-07-13 15:28:17 -04:00
|
|
|
}
|
2016-07-04 09:49:39 -04:00
|
|
|
|
2017-07-13 15:28:17 -04:00
|
|
|
static LV2_Inline_Display_Image_Surface *
|
|
|
|
render_inline (LV2_Handle instance, uint32_t w, uint32_t max_h)
|
|
|
|
{
|
|
|
|
AComp* self = (AComp*)instance;
|
|
|
|
|
|
|
|
uint32_t h = MIN (w, max_h);
|
2019-04-15 06:11:38 -04:00
|
|
|
if (w < 200) {
|
|
|
|
h = 40;
|
2017-07-13 15:28:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!self->display || self->w != w || self->h != h) {
|
|
|
|
if (self->display) cairo_surface_destroy(self->display);
|
|
|
|
self->display = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);
|
|
|
|
self->w = w;
|
|
|
|
self->h = h;
|
|
|
|
}
|
|
|
|
|
|
|
|
cairo_t* cr = cairo_create (self->display);
|
|
|
|
|
2019-04-15 06:11:38 -04:00
|
|
|
if (w >= 200) {
|
2017-07-13 15:28:17 -04:00
|
|
|
render_inline_full (cr, self);
|
|
|
|
} else {
|
|
|
|
render_inline_only_bars (cr, self);
|
|
|
|
}
|
2016-07-04 06:59:04 -04:00
|
|
|
|
|
|
|
cairo_destroy (cr);
|
2017-07-13 15:28:17 -04:00
|
|
|
|
2016-07-04 06:59:04 -04:00
|
|
|
cairo_surface_flush (self->display);
|
|
|
|
self->surf.width = cairo_image_surface_get_width (self->display);
|
|
|
|
self->surf.height = cairo_image_surface_get_height (self->display);
|
|
|
|
self->surf.stride = cairo_image_surface_get_stride (self->display);
|
|
|
|
self->surf.data = cairo_image_surface_get_data (self->display);
|
|
|
|
|
|
|
|
return &self->surf;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-07-02 21:21:44 -04:00
|
|
|
static const void*
|
|
|
|
extension_data(const char* uri)
|
|
|
|
{
|
2016-07-12 07:44:55 -04:00
|
|
|
#ifdef LV2_EXTENDED
|
2016-07-04 06:59:04 -04:00
|
|
|
static const LV2_Inline_Display_Interface display = { render_inline };
|
|
|
|
if (!strcmp(uri, LV2_INLINEDISPLAY__interface)) {
|
|
|
|
return &display;
|
|
|
|
}
|
2016-07-12 07:44:55 -04:00
|
|
|
#endif
|
2016-07-02 21:21:44 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-07-04 12:13:22 -04:00
|
|
|
static const LV2_Descriptor descriptor_mono = {
|
2016-07-02 21:21:44 -04:00
|
|
|
ACOMP_URI,
|
|
|
|
instantiate,
|
2016-08-27 12:08:46 -04:00
|
|
|
connect_mono,
|
2016-07-04 12:13:22 -04:00
|
|
|
activate,
|
2018-06-14 16:29:13 -04:00
|
|
|
run,
|
2016-07-04 12:13:22 -04:00
|
|
|
deactivate,
|
|
|
|
cleanup,
|
|
|
|
extension_data
|
|
|
|
};
|
|
|
|
|
|
|
|
static const LV2_Descriptor descriptor_stereo = {
|
|
|
|
ACOMP_STEREO_URI,
|
|
|
|
instantiate,
|
2016-08-27 12:08:46 -04:00
|
|
|
connect_stereo,
|
2016-07-02 21:21:44 -04:00
|
|
|
activate,
|
2018-06-14 16:29:13 -04:00
|
|
|
run,
|
2016-07-02 21:21:44 -04:00
|
|
|
deactivate,
|
|
|
|
cleanup,
|
|
|
|
extension_data
|
|
|
|
};
|
|
|
|
|
|
|
|
LV2_SYMBOL_EXPORT
|
|
|
|
const LV2_Descriptor*
|
|
|
|
lv2_descriptor(uint32_t index)
|
|
|
|
{
|
|
|
|
switch (index) {
|
|
|
|
case 0:
|
2016-07-04 12:13:22 -04:00
|
|
|
return &descriptor_mono;
|
|
|
|
case 1:
|
|
|
|
return &descriptor_stereo;
|
2016-07-02 21:21:44 -04:00
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|