Repository navigation
Expand file tree
/
Copy pathgainer.c
More file actions
112 lines (94 loc) · 2.77 KB
/
Copy pathgainer.c
File metadata and controls
112 lines (94 loc) · 2.77 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
/*
* Gainer LV2 Plugin - Simple stereo gain plugin
* Copyright (C) 2026 Simon Delaruotte
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* 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.
*/
#include <lv2/core/lv2.h>
#include <math.h>
#include <stdlib.h>
#define GAINER_URI "urn:simdott:gainer"
typedef enum {
INPUT_LEFT = 0,
INPUT_RIGHT = 1,
OUTPUT_LEFT = 2,
OUTPUT_RIGHT = 3,
VOLUME = 4
} PortIndex;
typedef struct {
const float* input_left;
const float* input_right;
const float* volume;
float* output_left;
float* output_right;
float prev_volume;
} Gainer;
static LV2_Handle instantiate(
const LV2_Descriptor* descriptor,
double sample_rate,
const char* bundle_path,
const LV2_Feature* const* features) {
Gainer* self = (Gainer*)malloc(sizeof(Gainer));
if (!self) return NULL;
self->prev_volume = 1.0f;
return (LV2_Handle)self;
}
static void connect_port(LV2_Handle instance, uint32_t port, void* data) {
Gainer* self = (Gainer*)instance;
switch ((PortIndex)port) {
case INPUT_LEFT:
self->input_left = (const float*)data;
break;
case INPUT_RIGHT:
self->input_right = (const float*)data;
break;
case OUTPUT_LEFT:
self->output_left = (float*)data;
break;
case OUTPUT_RIGHT:
self->output_right = (float*)data;
break;
case VOLUME:
self->volume = (const float*)data;
break;
}
}
static void run(LV2_Handle instance, uint32_t n_samples) {
Gainer* self = (Gainer*)instance;
float current_volume = *(self->volume);
float volume_gain = current_volume / 100.0f;
if (fabsf(volume_gain - self->prev_volume) < 0.0001f && fabsf(volume_gain - 1.0f) < 0.0001f) {
for (uint32_t i = 0; i < n_samples; i++) {
self->output_left[i] = self->input_left[i];
self->output_right[i] = self->input_right[i];
}
return;
}
self->prev_volume = volume_gain;
for (uint32_t i = 0; i < n_samples; i++) {
self->output_left[i] = self->input_left[i] * volume_gain;
self->output_right[i] = self->input_right[i] * volume_gain;
}
}
static void cleanup(LV2_Handle instance) {
free(instance);
}
static const LV2_Descriptor descriptor = {
GAINER_URI,
instantiate,
connect_port,
NULL,
run,
NULL,
cleanup,
NULL
};
LV2_SYMBOL_EXPORT
const LV2_Descriptor* lv2_descriptor(uint32_t index) {
return (index == 0) ? &descriptor : NULL;
}