[0ed57a]: trunk / src / LV2 / gxamp.lv2 / gxamp.cpp  Maximize  Restore  History

Download this file

414 lines (370 with data), 10.9 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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
* Copyright (C) 2012 Hermann Meyer, Andreas Degert, Pete Shorthose
*
* 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.
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* --------------------------------------------------------------------------
*/
#include <cstdlib>
#include <cmath>
#ifdef __SSE__
/* On Intel set FZ (Flush to Zero) and DAZ (Denormals Are Zero)
flags to avoid costly denormals */
#ifdef __SSE3__
#include <pmmintrin.h>
inline void AVOIDDENORMALS()
{
_MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);
_MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON);
}
#else
#include <xmmintrin.h>
inline void AVOIDDENORMALS()
{
_MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);
}
#endif //__SSE3__
#else
inline void AVOIDDENORMALS() {}
#endif //__SSE__
// faust support
#define FAUSTFLOAT float
#ifndef N_
#define N_(String) (String)
#endif
#define max(x, y) (((x) > (y)) ? (x) : (y))
#define min(x, y) (((x) < (y)) ? (x) : (y))
template <int N> inline float faustpower(float x)
{
return powf(x, N);
}
template <int N> inline double faustpower(double x)
{
return pow(x, N);
}
template <int N> inline int faustpower(int x)
{
return faustpower<N/2>(x) * faustpower<N-N/2>(x);
}
template <> inline int faustpower<0>(int x)
{
return 1;
}
template <> inline int faustpower<1>(int x)
{
return x;
}
struct GXPlugin;
#include "gxamp.h"
#include "gx_resampler.h"
#include "gx_convolver.h"
#include "gx_tonestack.h"
#include "gx_amp.h"
#include "impulse_former.h"
#include "ampulse_former.h"
struct GXPlugin
{
// LV2 stuff
LV2_Atom_Sequence* c_notice;
LV2_Atom_Sequence* n_notice;
LV2_URID_Map* map;
LV2_Atom_Forge forge;
GXPluginURIs uris;
LV2_Atom_Forge_Frame notify_frame;
LV2_Worker_Schedule* schedule;
// internal stuff
Tonestack *ts;
GxAmp *amplifier;
gx_resample::BufferResampler resamp;
GxSimpleConvolver *cabconv;
Impf *impf;
gx_resample::BufferResampler resamp1;
GxSimpleConvolver *ampconv;
Ampf *ampf;
uint32_t bufsize;
bool schedule_wait;
static void connect(uint32_t port,void* data, GXPlugin* self);
GXPlugin() {}
};
#include "gx_tonestack.cc"
#include "gx_amp.cc"
#include "gx_convolver.cc"
#include "gx_resampler.cc"
#include "impulse_former.cc"
#include "ampulse_former.cc"
static LV2_Worker_Status
work(LV2_Handle instance,
LV2_Worker_Respond_Function respond,
LV2_Worker_Respond_Handle handle,
uint32_t size,
const void* data)
{
GXPlugin* self = (GXPlugin*)instance;
GXPluginURIs* uris = &self->uris;
const LV2_Atom_Object* obj = (LV2_Atom_Object*)data;
if (obj->body.otype == uris->gx_cab)
{
//printf("worker run. %d id= %d type= %d\n", obj->body.otype, obj->body.id, obj->atom.type);
float cab_irdata_c[cab_data_HighGain.ir_count];
self->impf->compute(cab_data_HighGain.ir_count, cab_data_HighGain.ir_data, cab_irdata_c);
// cab_data_HighGain.ir_count, cab_data_HighGain.ir_data,cab_data_HighGain.ir_sr
if (!self->cabconv->update(cab_data_HighGain.ir_count, cab_irdata_c, cab_data_HighGain.ir_sr))
printf("cabconv->update fail.\n");
//printf("worker 1 ready.\n");
}
else if (obj->body.otype == uris->gx_pre)
{
//printf("worker run. %d id= %d type= %d\n", obj->body.otype, obj->body.id, obj->atom.type);
float pre_irdata_c[contrast_ir_desc.ir_count];
self->ampf->compute(contrast_ir_desc.ir_count,contrast_ir_desc.ir_data, pre_irdata_c);
// cab_data_HighGain.ir_count, cab_data_HighGain.ir_data,cab_data_HighGain.ir_sr
if (!self->ampconv->update(contrast_ir_desc.ir_count, pre_irdata_c, contrast_ir_desc.ir_sr))
printf("cabconv->update fail.\n");
//printf("worker 2 ready.\n");
}
self->schedule_wait = false;
return LV2_WORKER_SUCCESS;
}
static LV2_Worker_Status
work_response(LV2_Handle instance,
uint32_t size,
const void* data)
{
printf("worker respose.\n");
return LV2_WORKER_SUCCESS;
}
// LV2 defines
static LV2_Handle
instantiate(const LV2_Descriptor* descriptor,
double rate,
const char* bundle_path,
const LV2_Feature* const* features)
{
GXPlugin *self = new GXPlugin();
if (!self)
{
return NULL;
}
const LV2_Options_Option* options = NULL;
uint32_t bufsize = 0;
for (int i = 0; features[i]; ++i)
{
if (!strcmp(features[i]->URI, LV2_URID__map))
{
self->map = (LV2_URID_Map*)features[i]->data;
}
else if (!strcmp(features[i]->URI, LV2_WORKER__schedule))
{
self->schedule = (LV2_Worker_Schedule*)features[i]->data;
}
else if (!strcmp(features[i]->URI, LV2_OPTIONS__options))
{
options = (const LV2_Options_Option*)features[i]->data;
}
}
if (!self->schedule)
{
fprintf(stderr, "Missing feature work:schedule.\n");
delete self;
return NULL;
}
if (!self->map)
{
fprintf(stderr, "Missing feature uri:map.\n");
}
else if (!options)
{
fprintf(stderr, "Missing feature options.\n");
}
else
{
LV2_URID bufsz_max = self->map->map(self->map->handle, LV2_BUF_SIZE__maxBlockLength);
LV2_URID atom_Int = self->map->map(self->map->handle, LV2_ATOM__Int);
for (const LV2_Options_Option* o = options; o->key; ++o)
{
if (o->context == LV2_OPTIONS_INSTANCE &&
o->key == bufsz_max &&
o->type == atom_Int)
{
bufsize = *(const int32_t*)o->value;
}
}
if (bufsize == 0)
{
fprintf(stderr, "No maximum buffer size given.\n");
}
//if (bufsize < 64 || bufsize > 8192 ) {
// fprintf(stderr, "unsupported block-size: %d\n", bufsize);
// bufsize = 0;
//}
printf("using block size: %d\n", bufsize);
}
AVOIDDENORMALS();
LV2_URID_Map* map = self->map;
map_gx_uris(map, &self->uris);
lv2_atom_forge_init(&self->forge, self->map);
self->bufsize = bufsize;
self->schedule_wait = false;
self->amplifier = new GxAmp();
self->ts = new Tonestack();
self->amplifier->init_static(rate, self->amplifier);
self->ts->init_static(rate, self->ts);
self->cabconv = new GxSimpleConvolver(self->resamp);
self->cabconv->set_samplerate(rate);
self->impf = new Impf();
self->impf->init_static(rate, self->impf);
self->ampconv = new GxSimpleConvolver(self->resamp1);
self->ampconv->set_samplerate(rate);
self->ampf = new Ampf();
self->ampf->init_static(rate, self->ampf);
if (self->bufsize )
{
self->cabconv->set_buffersize(self->bufsize);
self->cabconv->configure(cab_data_HighGain.ir_count, cab_data_HighGain.ir_data, cab_data_HighGain.ir_sr);
self->cabconv->start(0, SCHED_FIFO);
self->ampconv->set_buffersize(self->bufsize);
self->ampconv->configure(contrast_ir_desc.ir_count, contrast_ir_desc.ir_data, contrast_ir_desc.ir_sr);
self->ampconv->start(0, SCHED_FIFO);
}
else
{
printf("convolver disabled\n");
}
return (LV2_Handle)self;
}
void GXPlugin::connect(uint32_t port,void* data, GXPlugin* self)
{
switch ((PortIndex)port)
{
case AMP_MASTERGAIN:
break;
case AMP_PREGAIN:
break;
case AMP_WET_DRY:
break;
case AMP_DRIVE:
break;
case MID:
break;
case BASS:
break;
case TREBLE:
break;
case CLevel:
break;
case ALevel:
break;
case AMP_OUTPUT:
break;
case AMP_INPUT:
break;
case AMP_CONTROL:
self->c_notice = (LV2_Atom_Sequence*)data;
break;
case AMP_NOTIFY:
self->n_notice = (LV2_Atom_Sequence*)data;
break;
}
}
static void
connect_port(LV2_Handle instance,
uint32_t port,
void* data)
{
GXPlugin* self = (GXPlugin*)instance;
self->connect(port,data, self);
self->amplifier->connect_static(port,data, self->amplifier);
self->ts->connect_static(port,data, self->ts);
self->impf->connect_static(port,data, self->impf);
self->ampf->connect_static(port,data, self->ampf);
}
static void
activate(LV2_Handle instance)
{
}
static void
run(LV2_Handle instance, uint32_t n_samples)
{
GXPlugin* self = (GXPlugin*)instance;
/* Set up forge to write directly to notify output port. */
const uint32_t notify_capacity = self->n_notice->atom.size;
lv2_atom_forge_set_buffer(&self->forge,
(uint8_t*)self->n_notice,
notify_capacity);
/* Start a sequence in the notify output port. */
lv2_atom_forge_sequence_head(&self->forge, &self->notify_frame, 0);
/* Read incoming events if scheduler is free*/
if (self->schedule_wait == false)
{
LV2_ATOM_SEQUENCE_FOREACH(self->c_notice, ev)
{
self->schedule_wait = true;
self->schedule->schedule_work(self->schedule->handle,
lv2_atom_total_size(&ev->body), &ev->body);
}
}
self->amplifier->run_static(n_samples, self);
}
static void
deactivate(LV2_Handle instance)
{
}
static void
cleanup(LV2_Handle instance)
{
GXPlugin* self = (GXPlugin*)instance;
self->cabconv->stop_process();
self->ampconv->stop_process();
delete self->amplifier;
delete self->ts;
delete self->cabconv;
delete self->impf;
delete self->ampconv;
delete self->ampf;
delete self;
}
const void*
extension_data(const char* uri)
{
static const LV2_Worker_Interface worker = { work, work_response, NULL };
if (!strcmp(uri, LV2_WORKER__interface))
{
return &worker;
}
return NULL;
}
static const LV2_Descriptor descriptor =
{
GXPLUGIN_URI,
instantiate,
connect_port,
activate,
run,
deactivate,
cleanup,
extension_data
};
extern "C"
LV2_SYMBOL_EXPORT
const LV2_Descriptor*
lv2_descriptor(uint32_t index)
{
switch (index)
{
case 0:
return &descriptor;
default:
return NULL;
}
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks