~ubuntu-branches/ubuntu/trusty/lmms/trusty

« back to all changes in this revision

Viewing changes to plugins/ladspa_effect/swh/smooth_decimate_1414.c

  • Committer: Charlie Smotherman
  • Date: 2012-12-05 22:08:38 UTC
  • mfrom: (33.1.7 lmms_0.4.13)
  • Revision ID: cjsmo@cableone.net-20121205220838-09pjfzew9m5023hr
* New  Upstream release.
  - Minor tweaking to ZynAddSubFX, CALF, SWH plugins  and Stefan Fendt's RC
    filters.
  - Added UI fixes: Magnentic effect of knobs and Piano-roll fixes
  - Updated German localization and copyright year
* debian/lmms-common.install:
  - added /usr/share/applications so the lmms.desktop file will correctly
    install (LP: #863366)
  - This should also fix the Software Center not displaying lmms in sound
    and video (LP: #824231)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdlib.h>
 
2
#include <string.h>
 
3
#ifndef WIN32
 
4
#include "config.h"
 
5
#endif
 
6
 
 
7
#ifdef ENABLE_NLS
 
8
#include <libintl.h>
 
9
#endif
 
10
 
 
11
#define         _ISOC9X_SOURCE  1
 
12
#define         _ISOC99_SOURCE  1
 
13
#define         __USE_ISOC99    1
 
14
#define         __USE_ISOC9X    1
 
15
 
 
16
#include <math.h>
 
17
 
 
18
#include "ladspa.h"
 
19
 
 
20
#ifdef WIN32
 
21
#define _WINDOWS_DLL_EXPORT_ __declspec(dllexport)
 
22
int bIsFirstTime = 1; 
 
23
void _init(); // forward declaration
 
24
#else
 
25
#define _WINDOWS_DLL_EXPORT_ 
 
26
#endif
 
27
 
 
28
#line 10 "smooth_decimate_1414.xml"
 
29
 
 
30
#include "ladspa-util.h"
 
31
 
 
32
#define SMOOTHDECIMATE_RATE            0
 
33
#define SMOOTHDECIMATE_SMOOTH          1
 
34
#define SMOOTHDECIMATE_INPUT           2
 
35
#define SMOOTHDECIMATE_OUTPUT          3
 
36
 
 
37
static LADSPA_Descriptor *smoothDecimateDescriptor = NULL;
 
38
 
 
39
typedef struct {
 
40
        LADSPA_Data *rate;
 
41
        LADSPA_Data *smooth;
 
42
        LADSPA_Data *input;
 
43
        LADSPA_Data *output;
 
44
        float        accum;
 
45
        float *      buffer;
 
46
        int          buffer_pos;
 
47
        float        fs;
 
48
        LADSPA_Data run_adding_gain;
 
49
} SmoothDecimate;
 
50
 
 
51
_WINDOWS_DLL_EXPORT_
 
52
const LADSPA_Descriptor *ladspa_descriptor(unsigned long index) {
 
53
 
 
54
#ifdef WIN32
 
55
        if (bIsFirstTime) {
 
56
                _init();
 
57
                bIsFirstTime = 0;
 
58
        }
 
59
#endif
 
60
        switch (index) {
 
61
        case 0:
 
62
                return smoothDecimateDescriptor;
 
63
        default:
 
64
                return NULL;
 
65
        }
 
66
}
 
67
 
 
68
static void activateSmoothDecimate(LADSPA_Handle instance) {
 
69
        SmoothDecimate *plugin_data = (SmoothDecimate *)instance;
 
70
        float accum = plugin_data->accum;
 
71
        float *buffer = plugin_data->buffer;
 
72
        int buffer_pos = plugin_data->buffer_pos;
 
73
        float fs = plugin_data->fs;
 
74
#line 26 "smooth_decimate_1414.xml"
 
75
        buffer_pos = 0;
 
76
        accum = 0.0f;
 
77
        plugin_data->accum = accum;
 
78
        plugin_data->buffer = buffer;
 
79
        plugin_data->buffer_pos = buffer_pos;
 
80
        plugin_data->fs = fs;
 
81
 
 
82
}
 
83
 
 
84
static void cleanupSmoothDecimate(LADSPA_Handle instance) {
 
85
#line 55 "smooth_decimate_1414.xml"
 
86
        SmoothDecimate *plugin_data = (SmoothDecimate *)instance;
 
87
        free(plugin_data->buffer);
 
88
        free(instance);
 
89
}
 
90
 
 
91
static void connectPortSmoothDecimate(
 
92
 LADSPA_Handle instance,
 
93
 unsigned long port,
 
94
 LADSPA_Data *data) {
 
95
        SmoothDecimate *plugin;
 
96
 
 
97
        plugin = (SmoothDecimate *)instance;
 
98
        switch (port) {
 
99
        case SMOOTHDECIMATE_RATE:
 
100
                plugin->rate = data;
 
101
                break;
 
102
        case SMOOTHDECIMATE_SMOOTH:
 
103
                plugin->smooth = data;
 
104
                break;
 
105
        case SMOOTHDECIMATE_INPUT:
 
106
                plugin->input = data;
 
107
                break;
 
108
        case SMOOTHDECIMATE_OUTPUT:
 
109
                plugin->output = data;
 
110
                break;
 
111
        }
 
112
}
 
113
 
 
114
static LADSPA_Handle instantiateSmoothDecimate(
 
115
 const LADSPA_Descriptor *descriptor,
 
116
 unsigned long s_rate) {
 
117
        SmoothDecimate *plugin_data = (SmoothDecimate *)malloc(sizeof(SmoothDecimate));
 
118
        float accum;
 
119
        float *buffer = NULL;
 
120
        int buffer_pos;
 
121
        float fs;
 
122
 
 
123
#line 19 "smooth_decimate_1414.xml"
 
124
        buffer = calloc(8, sizeof(float));
 
125
        buffer_pos = 0;
 
126
        accum = 0.0f;
 
127
        fs = (float)s_rate;
 
128
 
 
129
        plugin_data->accum = accum;
 
130
        plugin_data->buffer = buffer;
 
131
        plugin_data->buffer_pos = buffer_pos;
 
132
        plugin_data->fs = fs;
 
133
 
 
134
        return (LADSPA_Handle)plugin_data;
 
135
}
 
136
 
 
137
#undef buffer_write
 
138
#undef RUN_ADDING
 
139
#undef RUN_REPLACING
 
140
 
 
141
#define buffer_write(b, v) (b = v)
 
142
#define RUN_ADDING    0
 
143
#define RUN_REPLACING 1
 
144
 
 
145
static void runSmoothDecimate(LADSPA_Handle instance, unsigned long sample_count) {
 
146
        SmoothDecimate *plugin_data = (SmoothDecimate *)instance;
 
147
 
 
148
        /* Resample rate (float value) */
 
149
        const LADSPA_Data rate = *(plugin_data->rate);
 
150
 
 
151
        /* Smoothing (float value) */
 
152
        const LADSPA_Data smooth = *(plugin_data->smooth);
 
153
 
 
154
        /* Input (array of floats of length sample_count) */
 
155
        const LADSPA_Data * const input = plugin_data->input;
 
156
 
 
157
        /* Output (array of floats of length sample_count) */
 
158
        LADSPA_Data * const output = plugin_data->output;
 
159
        float accum = plugin_data->accum;
 
160
        float * buffer = plugin_data->buffer;
 
161
        int buffer_pos = plugin_data->buffer_pos;
 
162
        float fs = plugin_data->fs;
 
163
 
 
164
#line 31 "smooth_decimate_1414.xml"
 
165
        unsigned long pos;
 
166
        float smoothed;
 
167
        float inc = (rate / fs);
 
168
        inc = f_clamp(inc, 0.0f, 1.0f);
 
169
 
 
170
        for (pos = 0; pos < sample_count; pos++) {
 
171
          accum += inc;
 
172
          if (accum >= 1.0f) {
 
173
            accum -= 1.0f;
 
174
            buffer_pos = (buffer_pos + 1) & 7;
 
175
            buffer[buffer_pos] = input[pos];
 
176
          }
 
177
          smoothed = cube_interp(accum, buffer[(buffer_pos - 3) & 7],
 
178
                                        buffer[(buffer_pos - 2) & 7],
 
179
                                        buffer[(buffer_pos - 1) & 7],
 
180
                                        buffer[buffer_pos]);
 
181
          buffer_write(output[pos], LIN_INTERP(smooth, buffer[(buffer_pos - 3) & 7], smoothed));
 
182
        }
 
183
 
 
184
        plugin_data->accum = accum;
 
185
        plugin_data->buffer_pos = buffer_pos;
 
186
}
 
187
#undef buffer_write
 
188
#undef RUN_ADDING
 
189
#undef RUN_REPLACING
 
190
 
 
191
#define buffer_write(b, v) (b += (v) * run_adding_gain)
 
192
#define RUN_ADDING    1
 
193
#define RUN_REPLACING 0
 
194
 
 
195
static void setRunAddingGainSmoothDecimate(LADSPA_Handle instance, LADSPA_Data gain) {
 
196
        ((SmoothDecimate *)instance)->run_adding_gain = gain;
 
197
}
 
198
 
 
199
static void runAddingSmoothDecimate(LADSPA_Handle instance, unsigned long sample_count) {
 
200
        SmoothDecimate *plugin_data = (SmoothDecimate *)instance;
 
201
        LADSPA_Data run_adding_gain = plugin_data->run_adding_gain;
 
202
 
 
203
        /* Resample rate (float value) */
 
204
        const LADSPA_Data rate = *(plugin_data->rate);
 
205
 
 
206
        /* Smoothing (float value) */
 
207
        const LADSPA_Data smooth = *(plugin_data->smooth);
 
208
 
 
209
        /* Input (array of floats of length sample_count) */
 
210
        const LADSPA_Data * const input = plugin_data->input;
 
211
 
 
212
        /* Output (array of floats of length sample_count) */
 
213
        LADSPA_Data * const output = plugin_data->output;
 
214
        float accum = plugin_data->accum;
 
215
        float * buffer = plugin_data->buffer;
 
216
        int buffer_pos = plugin_data->buffer_pos;
 
217
        float fs = plugin_data->fs;
 
218
 
 
219
#line 31 "smooth_decimate_1414.xml"
 
220
        unsigned long pos;
 
221
        float smoothed;
 
222
        float inc = (rate / fs);
 
223
        inc = f_clamp(inc, 0.0f, 1.0f);
 
224
 
 
225
        for (pos = 0; pos < sample_count; pos++) {
 
226
          accum += inc;
 
227
          if (accum >= 1.0f) {
 
228
            accum -= 1.0f;
 
229
            buffer_pos = (buffer_pos + 1) & 7;
 
230
            buffer[buffer_pos] = input[pos];
 
231
          }
 
232
          smoothed = cube_interp(accum, buffer[(buffer_pos - 3) & 7],
 
233
                                        buffer[(buffer_pos - 2) & 7],
 
234
                                        buffer[(buffer_pos - 1) & 7],
 
235
                                        buffer[buffer_pos]);
 
236
          buffer_write(output[pos], LIN_INTERP(smooth, buffer[(buffer_pos - 3) & 7], smoothed));
 
237
        }
 
238
 
 
239
        plugin_data->accum = accum;
 
240
        plugin_data->buffer_pos = buffer_pos;
 
241
}
 
242
 
 
243
void _init() {
 
244
        char **port_names;
 
245
        LADSPA_PortDescriptor *port_descriptors;
 
246
        LADSPA_PortRangeHint *port_range_hints;
 
247
 
 
248
#ifdef ENABLE_NLS
 
249
#define D_(s) dgettext(PACKAGE, s)
 
250
        setlocale(LC_ALL, "");
 
251
        bindtextdomain(PACKAGE, PACKAGE_LOCALE_DIR);
 
252
#else
 
253
#define D_(s) (s)
 
254
#endif
 
255
 
 
256
 
 
257
        smoothDecimateDescriptor =
 
258
         (LADSPA_Descriptor *)malloc(sizeof(LADSPA_Descriptor));
 
259
 
 
260
        if (smoothDecimateDescriptor) {
 
261
                smoothDecimateDescriptor->UniqueID = 1414;
 
262
                smoothDecimateDescriptor->Label = "smoothDecimate";
 
263
                smoothDecimateDescriptor->Properties =
 
264
                 LADSPA_PROPERTY_HARD_RT_CAPABLE;
 
265
                smoothDecimateDescriptor->Name =
 
266
                 D_("Smooth Decimator");
 
267
                smoothDecimateDescriptor->Maker =
 
268
                 "Steve Harris <steve@plugin.org.uk>";
 
269
                smoothDecimateDescriptor->Copyright =
 
270
                 "GPL";
 
271
                smoothDecimateDescriptor->PortCount = 4;
 
272
 
 
273
                port_descriptors = (LADSPA_PortDescriptor *)calloc(4,
 
274
                 sizeof(LADSPA_PortDescriptor));
 
275
                smoothDecimateDescriptor->PortDescriptors =
 
276
                 (const LADSPA_PortDescriptor *)port_descriptors;
 
277
 
 
278
                port_range_hints = (LADSPA_PortRangeHint *)calloc(4,
 
279
                 sizeof(LADSPA_PortRangeHint));
 
280
                smoothDecimateDescriptor->PortRangeHints =
 
281
                 (const LADSPA_PortRangeHint *)port_range_hints;
 
282
 
 
283
                port_names = (char **)calloc(4, sizeof(char*));
 
284
                smoothDecimateDescriptor->PortNames =
 
285
                 (const char **)port_names;
 
286
 
 
287
                /* Parameters for Resample rate */
 
288
                port_descriptors[SMOOTHDECIMATE_RATE] =
 
289
                 LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
 
290
                port_names[SMOOTHDECIMATE_RATE] =
 
291
                 D_("Resample rate");
 
292
                port_range_hints[SMOOTHDECIMATE_RATE].HintDescriptor =
 
293
                 LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE | LADSPA_HINT_SAMPLE_RATE | LADSPA_HINT_DEFAULT_MAXIMUM;
 
294
                port_range_hints[SMOOTHDECIMATE_RATE].LowerBound = 0;
 
295
                port_range_hints[SMOOTHDECIMATE_RATE].UpperBound = 1;
 
296
 
 
297
                /* Parameters for Smoothing */
 
298
                port_descriptors[SMOOTHDECIMATE_SMOOTH] =
 
299
                 LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
 
300
                port_names[SMOOTHDECIMATE_SMOOTH] =
 
301
                 D_("Smoothing");
 
302
                port_range_hints[SMOOTHDECIMATE_SMOOTH].HintDescriptor =
 
303
                 LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE | LADSPA_HINT_DEFAULT_MAXIMUM;
 
304
                port_range_hints[SMOOTHDECIMATE_SMOOTH].LowerBound = 0;
 
305
                port_range_hints[SMOOTHDECIMATE_SMOOTH].UpperBound = 1;
 
306
 
 
307
                /* Parameters for Input */
 
308
                port_descriptors[SMOOTHDECIMATE_INPUT] =
 
309
                 LADSPA_PORT_INPUT | LADSPA_PORT_AUDIO;
 
310
                port_names[SMOOTHDECIMATE_INPUT] =
 
311
                 D_("Input");
 
312
                port_range_hints[SMOOTHDECIMATE_INPUT].HintDescriptor = 0;
 
313
 
 
314
                /* Parameters for Output */
 
315
                port_descriptors[SMOOTHDECIMATE_OUTPUT] =
 
316
                 LADSPA_PORT_OUTPUT | LADSPA_PORT_AUDIO;
 
317
                port_names[SMOOTHDECIMATE_OUTPUT] =
 
318
                 D_("Output");
 
319
                port_range_hints[SMOOTHDECIMATE_OUTPUT].HintDescriptor = 0;
 
320
 
 
321
                smoothDecimateDescriptor->activate = activateSmoothDecimate;
 
322
                smoothDecimateDescriptor->cleanup = cleanupSmoothDecimate;
 
323
                smoothDecimateDescriptor->connect_port = connectPortSmoothDecimate;
 
324
                smoothDecimateDescriptor->deactivate = NULL;
 
325
                smoothDecimateDescriptor->instantiate = instantiateSmoothDecimate;
 
326
                smoothDecimateDescriptor->run = runSmoothDecimate;
 
327
                smoothDecimateDescriptor->run_adding = runAddingSmoothDecimate;
 
328
                smoothDecimateDescriptor->set_run_adding_gain = setRunAddingGainSmoothDecimate;
 
329
        }
 
330
}
 
331
 
 
332
void _fini() {
 
333
        if (smoothDecimateDescriptor) {
 
334
                free((LADSPA_PortDescriptor *)smoothDecimateDescriptor->PortDescriptors);
 
335
                free((char **)smoothDecimateDescriptor->PortNames);
 
336
                free((LADSPA_PortRangeHint *)smoothDecimateDescriptor->PortRangeHints);
 
337
                free(smoothDecimateDescriptor);
 
338
        }
 
339
 
 
340
}