-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdouble.c
More file actions
151 lines (123 loc) · 3.36 KB
/
Copy pathdouble.c
File metadata and controls
151 lines (123 loc) · 3.36 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
#include <stdlib.h>
#include <stdio.h>
#include <lv2/core/lv2.h>
#define URI "http://maxcutlyp.com/doubler"
#define MAX_SKIP_MS 500
typedef struct {
float *buf;
uint32_t end;
uint32_t length;
} RingBuffer;
static inline void rb_push(RingBuffer *rb, float value) {
rb->end = (rb->end + 1) % rb->length;
rb->buf[rb->end] = value;
}
static inline void rb_push_many(RingBuffer *rb, float *values, uint32_t n) {
for (uint32_t i = 0; i < n; i++) {
rb->buf[(rb->end + i) % rb->length] = values[i];
}
rb->end = (rb->end + n) % rb->length;
}
static inline float rb_get(RingBuffer *rb) {
return rb->buf[(rb->end + 1) % rb->length];
}
void rb_init(RingBuffer *rb, uint32_t length) {
rb->buf = (float *)calloc(length, sizeof(float));
rb->length = length;
rb->end = 0;
}
void rb_free(RingBuffer *rb) {
free(rb->buf);
}
typedef struct {
float *left_input;
float *right_input;
float *left_output;
float *right_output;
float *delay_ms;
double rate;
RingBuffer overflow;
} HelloWorld;
static LV2_Handle instantiate(
const LV2_Descriptor *descriptor,
double rate,
const char *bundle_path,
const LV2_Feature* const* features
) {
(void)descriptor;
(void)bundle_path;
(void)features;
HelloWorld *plugin = (HelloWorld *)malloc(sizeof(HelloWorld));
plugin->rate = rate;
rb_init(&plugin->overflow, rate * MAX_SKIP_MS * 0.001);
return (LV2_Handle)plugin;
}
static void connect_port(
LV2_Handle instance,
uint32_t port,
void *data_location
) {
HelloWorld *plugin = (HelloWorld *)instance;
switch (port) {
case 0: {
plugin->left_input = (float *)data_location;
}; break;
case 1: {
plugin->right_input = (float *)data_location;
}; break;
case 2: {
plugin->left_output = (float *)data_location;
}; break;
case 3: {
plugin->right_output = (float *)data_location;
}; break;
case 4: {
plugin->delay_ms = (float *)data_location;
}; break;
}
}
static void activate(LV2_Handle instance) {
(void)instance;
}
static void run(LV2_Handle instance, uint32_t n_samples) {
HelloWorld *plugin = (HelloWorld *)instance;
if (*plugin->delay_ms == 0) {
for (uint32_t i = 0; i < n_samples; i++) {
plugin->left_output[i] = plugin->left_input[i];
plugin->right_output[i] = plugin->right_input[i];
}
return;
}
plugin->overflow.length = plugin->rate * (*plugin->delay_ms) * 0.001;
for (uint32_t i = 0; i < n_samples; i++) {
plugin->left_output[i] = rb_get(&plugin->overflow);
plugin->right_output[i] = plugin->right_input[i];
rb_push(&plugin->overflow, plugin->left_input[i]);
}
}
static void deactivate(LV2_Handle instance) {
(void)instance;
}
static void cleanup(LV2_Handle instance) {
HelloWorld *plugin = (HelloWorld *)instance;
rb_free(&plugin->overflow);
free(instance);
}
static const void *extension_data(const char *uri) {
(void)uri;
return NULL;
}
static const LV2_Descriptor descriptor = {
URI,
instantiate,
connect_port,
activate,
run,
deactivate,
cleanup,
extension_data,
};
LV2_SYMBOL_EXPORT
const LV2_Descriptor *lv2_descriptor(uint32_t index) {
return (index == 0) ? &descriptor : NULL;
}