/
xfade.c
249 lines (216 loc) · 5.35 KB
/
xfade.c
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
/* xfade -- LV2 stereo xfader control
*
* Copyright (C) 2013 Robin Gareus <robin@gareus.org>
*
* 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, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#ifdef HAVE_LV2_1_18_6
#include <lv2/core/lv2.h>
#else
#include <lv2/lv2plug.in/ns/lv2core/lv2.h>
#endif
#define XFC_URI "http://gareus.org/oss/lv2/xfade"
#define IPORTS (2)
#define CHANNELS (2)
#define C_LEFT (0)
#define C_RIGHT (1)
#define FADE_LEN (64)
#define MIN(a,b) ( (a) < (b) ? (a) : (b) )
#define MAX(a,b) ( (a) > (b) ? (a) : (b) )
#define RAIL(v, min, max) (MIN((max), MAX((min), (v))))
typedef enum {
XFC_XFADE,
XFC_SHAPE,
XFC_MODE,
XFC_IN0L,
XFC_IN0R,
XFC_IN1L,
XFC_IN1R,
XFC_OUTL,
XFC_OUTR
} PortIndex;
typedef struct {
/* control ports */
float* xfade;
float* shape;
float* mode;
float* input[IPORTS][CHANNELS];
float* output[CHANNELS];
/* current settings */
float c_amp[IPORTS];
} XfadeControl;
#define SMOOTHGAIN(AMP, TARGET_AMP) (AMP + (TARGET_AMP - AMP) * (float) MIN(pos, fade_len) / (float)fade_len)
static void
run(LV2_Handle instance, uint32_t n_samples)
{
XfadeControl* self = (XfadeControl*)instance;
const float xfade = *self->xfade;
const float shape = RAIL(*self->shape, 0.0, 1.0);
const int mode = (int) RAIL(*self->mode, 0.0, 1.0);
const uint32_t fade_len = (n_samples >= FADE_LEN) ? FADE_LEN : n_samples;
float gain[IPORTS];
float gainP[IPORTS];
float gainL[IPORTS];
if (mode == 1) { /* V-fade - non overlapping */
if (xfade < 0) {
gainL[0] = 1.0;
gainP[0] = 1.0;
gainL[1] = 1.0 + RAIL(xfade, -1.0, 0.0);
gainP[1] = sqrt(1.0 + RAIL(xfade, -1.0, 0.0));
} else if (xfade > 0) {
gainL[0] = 1.0 - RAIL(xfade, 0.0, 1.0);
gainP[0] = sqrt(1.0 - RAIL(xfade, 0.0, 1.0));
gainL[1] = 1.0;
gainP[1] = 1.0;
} else {
gainL[0] = 1.0;
gainL[1] = 1.0;
gainP[0] = 1.0;
gainP[1] = 1.0;
}
} else { /* X-fade overlapping */
gainL[1] = 0.5 + RAIL(xfade, -1.0, 1.0)/2.0;
gainL[0] = 1.0 - gainL[1];
/* equal power gain */
if (xfade == -1.0) {
gainP[0] = 1.0;
gainP[1] = 0.0;
} else if (xfade == 1.0) {
gainP[0] = 0.0;
gainP[1] = 1.0;
} else {
gainP[1] = sqrt(.5 + RAIL(xfade/2.0, -.5, .5));
gainP[0] = sqrt(.5 - RAIL(xfade/2.0, -.5, .5));
}
}
gain[0] = shape * gainP[0] + (1.0 - shape) * gainL[0];
gain[1] = shape * gainP[1] + (1.0 - shape) * gainL[1];
#if 0 // debug
#define VALTODB(V) (20.0f * log10f(V))
printf("%.2fdB %.2fdB ||A: %.2f %.2f || B: %.2f %.2f || s:%.2f m:%d\n",
VALTODB(gain[0]), VALTODB(gain[1]),
gainL[0], gainL[1],
gainP[0], gainP[1], shape, mode
);
#endif
for (int c = 0; c < CHANNELS; ++c) {
uint32_t pos = 0;
if (self->c_amp[0] == gain[0] && self->c_amp[1] == gain[1]) {
for (pos = 0; pos < n_samples; pos++) {
self->output[c][pos] =
self->input[0][c][pos] * gain[0]
+ self->input[1][c][pos] * gain[1];
}
} else {
for (pos = 0; pos < n_samples; pos++) {
self->output[c][pos] =
self->input[0][c][pos] * SMOOTHGAIN(self->c_amp[0], gain[0])
+ self->input[1][c][pos] * SMOOTHGAIN(self->c_amp[1], gain[1]);
}
}
}
self->c_amp[0] = gain[0];
self->c_amp[1] = gain[1];
}
static LV2_Handle
instantiate(const LV2_Descriptor* descriptor,
double rate,
const char* bundle_path,
const LV2_Feature* const* features)
{
int i;
XfadeControl* self = (XfadeControl*)calloc(1, sizeof(XfadeControl));
if (!self) return NULL;
for (i=0; i < IPORTS; ++i) {
self->c_amp[i] = 1.0;
}
return (LV2_Handle)self;
}
static void
connect_port(LV2_Handle instance,
uint32_t port,
void* data)
{
XfadeControl* self = (XfadeControl*)instance;
switch ((PortIndex)port) {
case XFC_XFADE:
self->xfade = data;
break;
case XFC_SHAPE:
self->shape = data;
break;
case XFC_MODE:
self->mode = data;
break;
case XFC_IN0L:
self->input[0][C_LEFT] = data;
break;
case XFC_IN0R:
self->input[0][C_RIGHT] = data;
break;
case XFC_IN1L:
self->input[1][C_LEFT] = data;
break;
case XFC_IN1R:
self->input[1][C_RIGHT] = data;
break;
case XFC_OUTL:
self->output[C_LEFT] = data;
break;
case XFC_OUTR:
self->output[C_RIGHT] = data;
break;
}
}
static void
cleanup(LV2_Handle instance)
{
free(instance);
}
static const void*
extension_data(const char* uri)
{
return NULL;
}
static const LV2_Descriptor descriptor = {
XFC_URI,
instantiate,
connect_port,
NULL,
run,
NULL,
cleanup,
extension_data
};
#undef LV2_SYMBOL_EXPORT
#ifdef _WIN32
# define LV2_SYMBOL_EXPORT __declspec(dllexport)
#else
# define LV2_SYMBOL_EXPORT __attribute__ ((visibility ("default")))
#endif
LV2_SYMBOL_EXPORT
const LV2_Descriptor*
lv2_descriptor(uint32_t index)
{
switch (index) {
case 0:
return &descriptor;
default:
return NULL;
}
}