-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompressor.c
More file actions
213 lines (182 loc) · 7.53 KB
/
compressor.c
File metadata and controls
213 lines (182 loc) · 7.53 KB
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
/*
* VEJA Compressor
* Copyright (C) 2021 Jan Janssen <jan@moddevices.com>
*
* Permission to use, copy, modify, and/or distribute this software for any purpose with
* or without fee is hereby granted, provided that the above copyright notice and this
* permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
* TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
* NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdbool.h>
#include "lv2/lv2plug.in/ns/lv2core/lv2.h"
#include "compressor_core.h"
/**********************************************************************************************************************************************************/
#define PLUGIN_URI "http://VeJaPlugins.com/plugins/Release/Compressor"
#define MAP(x, Imin, Imax, Omin, Omax) ( x - Imin ) * (Omax - Omin) / (Imax - Imin) + Omin;
#define COMP_BUF_SIZE 256
#define SAMPLERATE 48000
typedef enum {
INPUT_L,
INPUT_R,
OUTPUT_L,
OUTPUT_R,
THRES,
KNEE,
ATTACK,
RELEASE,
RATIO,
MAKEUP,
MASTER_VOL
}PortIndex;
/**********************************************************************************************************************************************************/
typedef struct{
//ports
float* input_left;
float* input_right;
float* output_left;
float* output_right;
float* threshold;
float* knee;
float* attack;
float* release;
float* ratio;
float* makeup;
float* volume;
float *bfr_l;
float *bfr_r;
float prev_threshold;
float prev_knee;
float prev_attack;
float prev_release;
float prev_ratio;
float prev_makeup;
sf_compressor_state_st compressor_state;
} Compressor;
/**********************************************************************************************************************************************************/
// local functions //
/**********************************************************************************************************************************************************/
/**********************************************************************************************************************************************************/
static LV2_Handle
instantiate(const LV2_Descriptor* descriptor,
double samplerate,
const char* bundle_path,
const LV2_Feature* const* features)
{
Compressor* self = (Compressor*)malloc(sizeof(Compressor));
self->bfr_l = (float*)malloc(256*sizeof(float));
self->bfr_r = (float*)malloc(256*sizeof(float));
compressor_init(&self->compressor_state, samplerate);
return (LV2_Handle)self;
}
/**********************************************************************************************************************************************************/
static void connect_port(LV2_Handle instance, uint32_t port, void *data)
{
Compressor* self = (Compressor*)instance;
switch ((PortIndex)port)
{
case INPUT_L:
self->input_left = (float*) data;
break;
case INPUT_R:
self->input_right = (float*) data;
break;
case OUTPUT_L:
self->output_left = (float*) data;
break;
case OUTPUT_R:
self->output_right = (float*) data;
break;
case THRES:
self->threshold = (float*) data;
break;
case KNEE:
self->knee = (float*) data;
break;
case ATTACK:
self->attack = (float*) data;
break;
case RELEASE:
self->release = (float*) data;
break;
case RATIO:
self->ratio = (float*) data;
break;
case MAKEUP:
self->makeup = (float*) data;
break;
case MASTER_VOL:
self->volume = (float*) data;
break;
}
}
/**********************************************************************************************************************************************************/
void activate(LV2_Handle instance)
{
// TODO: include the activate function code here
}
/**********************************************************************************************************************************************************/
void run(LV2_Handle instance, uint32_t n_samples)
{
Compressor* self = (Compressor*)instance;
if ((self->prev_threshold != (float)*self->threshold) || (self->prev_knee != (float)*self->knee) || (self->prev_attack != (float)*self->attack) || (self->prev_release != (float)*self->release) || (self->prev_ratio != (float)*self->ratio) || (self->prev_makeup != (float)*self->makeup) )
{
compressor_set_params(&self->compressor_state, (float)*self->threshold,
(float)*self->knee, (float)*self->ratio, ((float)*self->attack / 1000), ((float)*self->release / 1000), (float)*self->makeup);
self->prev_threshold = (float)*self->threshold;
self->prev_knee = (float)*self->knee;
self->prev_attack = (float)*self->attack;
self->prev_release = (float)*self->release;
self->prev_ratio = (float)*self->ratio;
self->prev_makeup = (float)*self->makeup;
}
compressor_process(&self->compressor_state, n_samples, self->input_left, self->input_left, self->bfr_l, self->bfr_r);
for (uint32_t i = 0; i < n_samples; i++)
{
self->output_left[i] = self->bfr_l[i] * cmop_db2lin((float)*self->volume);
self->output_right[i] = self->bfr_r[i] * cmop_db2lin((float)*self->volume);
}
}
/**********************************************************************************************************************************************************/
void deactivate(LV2_Handle instance)
{
// TODO: include the deactivate function code here
}
/**********************************************************************************************************************************************************/
void cleanup(LV2_Handle instance)
{
free(instance);
}
/**********************************************************************************************************************************************************/
const void* extension_data(const char* uri)
{
return NULL;
}
/**********************************************************************************************************************************************************/
static const LV2_Descriptor Descriptor = {
PLUGIN_URI,
instantiate,
connect_port,
activate,
run,
deactivate,
cleanup,
extension_data
};
/**********************************************************************************************************************************************************/
LV2_SYMBOL_EXPORT
const LV2_Descriptor* lv2_descriptor(uint32_t index)
{
if (index == 0) return &Descriptor;
else return NULL;
}
/**********************************************************************************************************************************************************/