Debian Codegraph @

myladspa.cpp

raw symbols SCIP
1 /************************************************************************
2
3 IMPORTANT NOTE : this file contains two clearly delimited sections :
4 the ARCHITECTURE section (in two parts) and the USER section. Each section
5 is governed by its own copyright and license. Please check individually
6 each section for license and copyright information.
7 *************************************************************************/
8
9 /*******************BEGIN ARCHITECTURE SECTION (part 1/2)****************/
10
11 /************************************************************************
12 FAUST Architecture File
13 Copyright (C) 2003-2011 GRAME, Centre National de Creation Musicale
14 ---------------------------------------------------------------------
15 This Architecture section is free software; you can redistribute it
16 and/or modify it under the terms of the GNU General Public License
17 as published by the Free Software Foundation; either version 3 of
18 the License, or (at your option) any later version.
19
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with this program; If not, see <http://www.gnu.org/licenses/>.
27
28 EXCEPTION : As a special exception, you may create a larger work
29 that contains this FAUST architecture section and distribute
30 that work under terms of your choice, so long as this FAUST
31 architecture section is not modified.
32
33
34 ************************************************************************
35 ************************************************************************/
36
37 #include <math.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <stdio.h>
41 #include <unistd.h>
42
43 #include <stack>
44 #include <string>
45 #include <iostream>
46 #include <map>
47
48 #include <QSharedMemory>
49 #include <QDateTime>
50
51 #include "ladspa.h"
52 #include "faust/gui/GUI.h"
53 #include "faust/misc.h"
54 #include "faust/audio/dsp.h"
55
56 // On Intel set FZ (Flush to Zero) and DAZ (Denormals Are Zero)
57 // flags to avoid costly denormals
58 #ifdef __SSE__
59 #include <xmmintrin.h>
60 #ifdef __SSE2__
61 #define AVOIDDENORMALS _mm_setcsr(_mm_getcsr() | 0x8040)
62 #else
63 #define AVOIDDENORMALS _mm_setcsr(_mm_getcsr() | 0x8000)
64 #endif
65 #else
66 #warning *** ladspa.cpp: NO SSE FLAG (denormals may slow things down) ***
67 #define AVOIDDENORMALS
68 #endif
69
70 #define sym(name) xsym(name)
71 #define xsym(name) #name
72
73 /******************************************************************************
74 *******************************************************************************
75
76 VECTOR INTRINSICS
77
78 *******************************************************************************
79 *******************************************************************************/
80 #include "common/nsmtracker.h"
81 #include "audio/system_compressor_wrapper_proc.h"
82
83 #include "audio/system_compressor.cpp"
84
85 #define mydsp Faust_system_compressor
86
87 //<<includeIntrinsic>>
88
89 /********************END ARCHITECTURE SECTION (part 1/2)****************/
90
91 /**************************BEGIN USER SECTION **************************/
92
93 //<<includeclass>>
94
95 /***************************END USER SECTION ***************************/
96
97 /*******************BEGIN ARCHITECTURE SECTION (part 2/2)***************/
98
99 //-----------------------------------portCollector--------------------------------------
100 //
101 // portCollector is passed to the buildUserInterface method of a dsp object
102 // in order to build a description of its inputs, outputs and control ports.
103 // This description is used to fill a LADSPA_Descriptor
104 //
105 //--------------------------------------------------------------------------------------
106
107 //--------------------------------useful constants--------------------------------------
108
109 #define MAXPORT 1024
110 static const int ICONTROL = LADSPA_PORT_INPUT|LADSPA_PORT_CONTROL;
111 static const int OCONTROL = LADSPA_PORT_OUTPUT|LADSPA_PORT_CONTROL;
112 static const int RANGE = LADSPA_PORT_INPUT|LADSPA_PORT_CONTROL;
113
114 static const char* inames[] = {
115 "input00", "input01", "input02", "input03", "input04",
116 "input05", "input06", "input07", "input08", "input09",
117 "input10", "input11", "input12", "input13", "input14",
118 "input15", "input16", "input17", "input18", "input19",
119 "input20", "input21", "input22", "input23", "input24",
120 "input25", "input26", "input27", "input28", "input29",
121 "input30", "input31", "input32", "input33", "input34",
122 "input35", "input36", "input37", "input38", "input39"
123 };
124
125 static const char* onames[] = {
126 "output00", "output01", "output02", "output03", "output04",
127 "output05", "output06", "output07", "output08", "output09",
128 "output10", "output11", "output12", "output13", "output14",
129 "output15", "output16", "output17", "output18", "output19",
130 "output20", "output21", "output22", "output23", "output24",
131 "output25", "output26", "output27", "output28", "output29",
132 "output30", "output31", "output32", "output33", "output34",
133 "output35", "output36", "output37", "output38", "output39"
134 };
135
136 class portCollector : public UI
137 {
138 private:
139
140 //--------------------------------------------------------------------------------------
141
142 const int fInsCount; // number of audio input ports
143 const int fOutsCount; // number of audio output ports
144 int fCtrlCount; // number of control ports
145
146 LADSPA_PortDescriptor fPortDescs[MAXPORT]; // table of port descriptors to be used in a LADSPA_Descriptor
147 const char* fPortNames[MAXPORT]; // table of port names to be used in a LADSPA_Descriptor
148 LADSPA_PortRangeHint fPortHints[MAXPORT]; // table of port hints to be used in a LADSPA_Descriptor
149
150 string fPluginName; // toplevel prefix used as plugin name
151 stack<string> fPrefix; // current prefix for controls name
152
153
154 //--------------------------------------------------------------------------------------
155 string simplify(const string& src)
156 {
157 int i=0;
158 int level=2;
159 string dst;
160
161 while (src[i] ) {
162
163 switch (level) {
164
165 case 0 :
166 case 1 :
167 case 2 :
168 // Skip the begin of the label "--foo-"
169 // until 3 '-' have been read
170 if (src[i]=='-') { level++; }
171 break;
172
173 case 3 :
174 // copy the content, but skip non alphnum
175 // and content in parenthesis
176 switch (src[i]) {
177 case '(' :
178 case '[' :
179 level++;
180 break;
181
182 case '-' :
183 dst += '-';
184 break;
185
186 default :
187 if (isalnum(src[i])) {
188 dst+= tolower(src[i]);
189 }
190
191 }
192 break;
193
194 default :
195 // here we are inside parenthesis and
196 // we skip the content until we are back to
197 // level 3
198 switch (src[i]) {
199
200 case '(' :
201 case '[' :
202 level++;
203 break;
204
205 case ')' :
206 case ']' :
207 level--;
208 break;
209
210 default :
211 break;
212 }
213
214 }
215 i++;
216 }
217 return (dst.size() > 0) ? dst :src;
218 }
219
220 void addPortDescr(int type, const char* label, int hint, float min=0.0, float max=0.0)
221 {
222 string fullname = simplify(fPrefix.top() + "-" + label);
223 char * str = strdup(fullname.c_str());
224
225 fPortDescs[fInsCount + fOutsCount + fCtrlCount] = type;
226 fPortNames[fInsCount + fOutsCount + fCtrlCount] = str;
227 fPortHints[fInsCount + fOutsCount + fCtrlCount].HintDescriptor = hint;
228 fPortHints[fInsCount + fOutsCount + fCtrlCount].LowerBound = min;
229 fPortHints[fInsCount + fOutsCount + fCtrlCount].UpperBound = max;
230 fCtrlCount++;
231 }
232
233 void openAnyBox(const char* label)
234 {
235 if (fPrefix.size() == 0) {
236 // top level label is used as plugin name
237 fPluginName = label;
238 fPrefix.push(label);
239
240 } else {
241 string s;
242 if (label && label[0]) {
243 s = fPrefix.top() + "-" + label;
244 } else {
245 s = fPrefix.top();
246 }
247 fPrefix.push(s);
248 }
249 }
250
251 public:
252
253 //--------------------------------Collect the audio ports-------------------------------
254
255 portCollector(int ins, int outs) : UI(), fInsCount(ins), fOutsCount(outs), fCtrlCount(0)
256 {
257 for (int i = 0; i < ins; i++) {
258 fPortDescs[i] = LADSPA_PORT_INPUT | LADSPA_PORT_AUDIO;
259 fPortNames[i] = inames[i];
260 fPortHints[i].HintDescriptor = 0;
261 }
262 for (int j = 0; j < outs; j++) {
263 fPortDescs[ins + j] = LADSPA_PORT_OUTPUT | LADSPA_PORT_AUDIO;
264 fPortNames[ins + j] = onames[j];
265 fPortHints[ins + j].HintDescriptor = 0;
266 }
267 };
268
269 virtual ~portCollector() {}
270
271 //------------------------------Collect the control ports-------------------------------
272
273 virtual void addButton(const char* label, float* zone) {
274 addPortDescr(ICONTROL, label, LADSPA_HINT_TOGGLED);
275 }
276
277 virtual void addToggleButton(const char* label, float* zone) {
278 addPortDescr(ICONTROL, label, LADSPA_HINT_TOGGLED);
279 }
280
281 virtual void addCheckButton(const char* label, float* zone) {
282 addPortDescr(ICONTROL, label, LADSPA_HINT_TOGGLED);
283 }
284
285 virtual void addVerticalSlider(const char* label, float* zone, float init, float min, float max, float step) {
286 addPortDescr(ICONTROL, label, LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE, min, max);
287 }
288
289 virtual void addHorizontalSlider(const char* label, float* zone, float init, float min, float max, float step) {
290 addPortDescr(ICONTROL, label, LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE, min, max);
291 }
292
293 virtual void addNumEntry(const char* label, float* zone, float init, float min, float max, float step) {
294 addPortDescr(ICONTROL, label, LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE, min, max);
295 }
296
297 // -- passive widgets
298
299 virtual void addNumDisplay(const char* label, float* zone, int precision) {
300 addPortDescr(OCONTROL, label, 0, -10000, +10000);
301 }
302 virtual void addTextDisplay(const char* label, float* zone, const char* names[], float min, float max) {
303 addPortDescr(OCONTROL, label, LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE, min, max);
304 }
305 virtual void addHorizontalBargraph(const char* label, float* zone, float min, float max) {
306 addPortDescr(OCONTROL, label, LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE, min, max);
307 }
308 virtual void addVerticalBargraph(const char* label, float* zone, float min, float max){
309 addPortDescr(OCONTROL, label, LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE, min, max);
310 }
311
312 virtual void openFrameBox(const char* label) { openAnyBox(label); }
313 virtual void openTabBox(const char* label) { openAnyBox(label); }
314 virtual void openHorizontalBox(const char* label) { openAnyBox(label); }
315 virtual void openVerticalBox(const char* label) { openAnyBox(label); }
316
317 virtual void closeBox() { fPrefix.pop(); }
318
319 virtual void show() {}
320 virtual void run() {}
321
322 //---------------------------------Fill the LADSPA descriptor---------------------------
323
324 // generate an ID from a plugin name
325 int makeID (const char* s) {
326 int h = 0;
327 for (int i = 0; s[i]; i++) {
328 h = (h << 3) + (s[i] & 7);
329 }
330 return 1+h%1000;
331 }
332
333 // fill a ladspa descriptor with the information collected on ports
334 void fillPortDescription (LADSPA_Descriptor * descriptor) {
335 const char* name = sym(mydsp);
336 descriptor->PortCount = fCtrlCount+fInsCount+fOutsCount;
337 descriptor->PortDescriptors = fPortDescs;
338 descriptor->PortNames = fPortNames;
339 descriptor->PortRangeHints = fPortHints;
340
341 descriptor->Label = strdup(name);
342 descriptor->UniqueID = makeID(name);
343 // descriptor->Label = strdup(fPluginName.c_str());
344 // descriptor->UniqueID = makeID(fPluginName.c_str());
345 descriptor->Properties = LADSPA_PROPERTY_HARD_RT_CAPABLE;
346 descriptor->Name = name;
347 // descriptor->Name = strdup(fPluginName.c_str());
348 descriptor->Maker = "undefined";
349 descriptor->Copyright = "undefined";
350 }
351 };
352
353 //--------------------------------------portData----------------------------------------
354 //
355 // portData : a user interface used to associate the data buffers and the ports
356 //
357 //--------------------------------------------------------------------------------------
358
359 class portData : public UI
360 {
361
362 public:
363
364 //--------------------------------------------------------------------------------------
365
366 const int fInsCount; // number of audio input ports
367 const int fOutsCount; // number of audio output ports
368 int fCtrlCount; // number of control ports
369 int num_in_zones;
370
371 float* fPortZone[MAXPORT]; //
372 float* fPortData[MAXPORT];
373
374 //--------------------------------------------------------------------------------------
375
376 void addZone(float* zone)
377 {
378 fPortZone[fInsCount + fOutsCount + fCtrlCount] = zone;
379 fCtrlCount++;
380 num_in_zones++;
381 }
382
383 float *getInputZone(int num){
384 return fPortZone[fInsCount + fOutsCount + num];
385 }
386
387 float *getOutputZone(int num){
388 return fPortZone[fInsCount + fOutsCount + num_in_zones + num];
389 }
390
391 //--------------------------------Collect the audio ports-------------------------------
392
393 portData(int ins, int outs) : UI(), fInsCount(ins), fOutsCount(outs), fCtrlCount(0), num_in_zones(0) {};
394 virtual ~portData() {}
395
396 //------------------------------Collect the control zones-------------------------------
397
398 virtual void addButton(const char* label, float* zone) { addZone(zone); }
399 virtual void addToggleButton(const char* label, float* zone) { addZone(zone); }
400 virtual void addCheckButton(const char* label, float* zone) { addZone(zone); }
401
402 virtual void addVerticalSlider(const char* label, float* zone, float init, float min, float max, float step) { addZone(zone); }
403 virtual void addHorizontalSlider(const char* label, float* zone, float init, float min, float max, float step) { addZone(zone); }
404 virtual void addNumEntry(const char* label, float* zone, float init, float min, float max, float step) { addZone(zone); }
405
406 // -- passive widgets
407
408 virtual void addNumDisplay(const char* label, float* zone, int precision) { addZone(zone); num_in_zones--; }
409 virtual void addTextDisplay(const char* label, float* zone, const char* names[], float min, float max) { addZone(zone); num_in_zones--;}
410 virtual void addHorizontalBargraph(const char* label, float* zone, float min, float max) { addZone(zone); num_in_zones--;}
411 virtual void addVerticalBargraph(const char* label, float* zone, float min, float max) { addZone(zone); num_in_zones--;}
412
413 virtual void openFrameBox(const char* label) { }
414 virtual void openTabBox(const char* label) { }
415 virtual void openHorizontalBox(const char* label) { }
416 virtual void openVerticalBox(const char* label) { }
417 virtual void closeBox() { }
418
419 virtual void show() {}
420 virtual void run() {}
421
422 //---------------------------------interaction with LADSPA------------------------------
423
424 void setPortData (unsigned long port, LADSPA_Data* data) {
425 fPortData[port] = data;
426 }
427
428 void updateCtrlZones() {
429 for (int i = fInsCount+fOutsCount; i < fInsCount+fOutsCount+fCtrlCount; i++)
430 *fPortZone[i] = *fPortData[i];
431 }
432
433 float** getInputs() {
434 return &fPortData[0];
435 }
436
437 float** getOutputs() {
438 return &fPortData[fInsCount];
439 }
440 };
441
442 //--------------------------------Faust-LADSPA plugin-----------------------------------
443 //
444 // Plugin structure, callbacks and LADSPA_descriptor(i) entry point
445 //
446 //--------------------------------------------------------------------------------------
447
448 static mydsp *create_shared_dsp();
449
450 LADSPA_Descriptor* gDescriptor = 0;
451
452 struct PLUGIN
453 {
454 unsigned long fSampleRate;
455 portData* fPortData;
456 dsp* fDsp;
457
458 PLUGIN(unsigned long r, portData* d, dsp* p) : fSampleRate(r), fPortData(d), fDsp(p) {}
459 };
460
461 LADSPA_Handle instantiate_method (const struct _LADSPA_Descriptor * Descriptor, unsigned long SampleRate)
462 {
463 dsp* p = create_shared_dsp(); //new mydsp();
464 portData* d = new portData(p->getNumInputs(), p->getNumOutputs());
465
466 p->buildUserInterface(d);
467 return new PLUGIN (SampleRate, d, p);
468 }
469
470 void connect_method (LADSPA_Handle Instance, unsigned long Port, LADSPA_Data * DataLocation)
471 {
472 PLUGIN* p = (PLUGIN*) Instance;
473 p->fPortData->setPortData(Port, DataLocation);
474 }
475
476 void activate_method (LADSPA_Handle Instance)
477 {
478 PLUGIN* p = (PLUGIN*) Instance;
479 p->fDsp->init(p->fSampleRate);
480 }
481
482 void run_method (LADSPA_Handle Instance, unsigned long SampleCount)
483 {
484 PLUGIN* p = (PLUGIN*) Instance;
485 p->fPortData->updateCtrlZones();
486 AVOIDDENORMALS;
487 p->fDsp->compute(SampleCount, p->fPortData->getInputs(), p->fPortData->getOutputs());
488 }
489
490 void deactivate_method (LADSPA_Handle Instance)
491 {}
492
493 void cleanup_method (LADSPA_Handle Instance)
494 {
495 PLUGIN* p = (PLUGIN*) Instance;
496 delete p->fPortData;
497 delete p->fDsp;
498 delete p;
499 }
500
501 //--------------------------------------------------------------------------------------
502
503 void init_descriptor(LADSPA_Descriptor* descriptor)
504 {
505 descriptor->UniqueID = 123456;
506 descriptor->Label = "none";
507 descriptor->Properties = LADSPA_PROPERTY_HARD_RT_CAPABLE;
508 descriptor->Name = "none";
509 descriptor->Maker = "Yann Orlarey";
510 descriptor->Copyright = "GPL";
511
512 descriptor->ImplementationData = 0;
513
514 // description des methods
515 descriptor->instantiate = instantiate_method;
516 descriptor->connect_port = connect_method;
517 descriptor->activate = activate_method;
518 descriptor->run = run_method;
519 descriptor->run_adding = 0;
520 descriptor->set_run_adding_gain = 0;
521 descriptor->deactivate = deactivate_method;
522 descriptor->cleanup = cleanup_method;
523 }
524
525 //--------------------------------------------------------------------------------------
526
527
528 const LADSPA_Descriptor * ladspa_descriptor(unsigned long Index)
529 {
530 if (Index == 0) {
531 if (gDescriptor == 0)
532 {
533 // allocate temporaries dsp and portCollector to build the plugin description
534 mydsp* p = new mydsp();
535 if (p) {
536 portCollector* c=new portCollector(p->getNumInputs(), p->getNumOutputs());
537 p->buildUserInterface(c);
538 gDescriptor = new LADSPA_Descriptor;
539 init_descriptor(gDescriptor);
540 c->fillPortDescription(gDescriptor);
541 delete p;
542 } else {
543 printf("Memory Error : unable to allocate the dsp object\n");
544 }
545 }
546 return gDescriptor;
547 } else {
548 return NULL;
549 }
550 }
551
552 /********************END ARCHITECTURE SECTION (part 2/2)****************/
553
554 #if 0
555 extern void start_program(int argc, char **argv, void *compressor);
556 #endif
557
558 void *COMPRESSOR_create_ladspa(const char *key){
559 QSharedMemory *shared = new QSharedMemory(key);
560
561 if(shared->attach()==false){
562 fprintf(stderr,"Ladspa compressor: Couldn't attach... Error: %s\n",shared->error()==QSharedMemory::NoError?"No error (?)":shared->errorString().toUtf8().constData());
563 exit(0);
564 }
565
566 mydsp *dsp = (mydsp*)shared->data();
567
568 fprintf(stderr,"dsp: %p.\n",dsp);
569 portData* port_data = new portData(dsp->mydsp::getNumInputs(), dsp->mydsp::getNumOutputs());
570 dsp->mydsp::buildUserInterface(port_data); // buildUserInterface is called twice on the dsp data. One time in each process.
571
572 return new PLUGIN(0, port_data, dsp);
573 }
574
575 static mydsp *create_shared_dsp(){
576 QString key = "radium_crashreporter_" + QString::number(QDateTime::currentMSecsSinceEpoch());
577 QSharedMemory *shared = new QSharedMemory(key);
578
579 if(shared->create(sizeof(mydsp))==false){
580 fprintf(stderr,"COMPRESSOR_create_shared: Couldn't create... Error: %s\n",shared->error()==QSharedMemory::NoError?"No error (?)":shared->errorString().toUtf8().constData());
581 return NULL;
582 }
583
584 void *memory = shared->data();
585
586 mydsp *dsp = new(memory) mydsp();
587
588 #if 0
589 printf("memory: %p, shared: %p\n",memory,shared);
590 dsp->key = key;
591 dsp->shared = shared;
592 #endif
593
594 //printf("system: %d\n",system(QString(QString("xterm -e gdb /home/kjetil/radium_compressor/radium_compressor --args --ladspa-slave ")+key).toAscii()));
595 printf("system: %d\n",system(QString(QString("xterm -e gdb --args /home/kjetil/radium_compressor/radium_compressor --ladspa-slave ")+key+" &").toUtf8().constData()));
596
597
598 return dsp;
599
600 #if 0
601 if(fork()!=0){
602
603 return dsp;
604
605 }else{
606
607 QSharedMemory *shared = new QSharedMemory(key);
608
609 if(shared->attach()==false){
610 fprintf(stderr,"Ladspa compressor: Couldn't attach... Error: %s\n",shared->error()==QSharedMemory::NoError?"No error (?)":shared->errorString().toAscii().data());
611 exit(0);
612 }
613
614 char *argv[1]={(char*)"radium_compressor"};
615
616 mydsp *dsp = (mydsp*)shared->data();
617 fprintf(stderr,"dsp: %p.\n",dsp);
618 portData* port_data = new portData(dsp->getNumInputs(), dsp->getNumOutputs());
619 dsp->buildUserInterface(port_data); // This is the second time buildUserInterface is called twice on the dsp data. One time in each process.
620
621 start_program(1,argv,new PLUGIN(0, port_data, dsp));
622
623 exit(0);
624 return NULL;
625 }
626 #endif
627 }
628
629
630 float COMPRESSOR_get_ladspa_parameter(void *Instance,int num){
631 PLUGIN* p = (PLUGIN*) Instance;
632 portData *port_data = p->fPortData;
633
634 float *controller = port_data->getInputZone(num);
635 return *controller;
636 }
637
638 void COMPRESSOR_set_ladspa_parameter(void *Instance,int num,float value){
639 PLUGIN* p = (PLUGIN*) Instance;
640 portData *port_data = p->fPortData;
641
642 float *controller = port_data->getInputZone(num);
643 fprintf(stderr,"port_data: %p, controller: %p, num_zones: %d, num: %d\n",port_data,controller,port_data->fCtrlCount,num);
644 *controller = value;
645 //printf("Setting controller %d (%p) to %f (%f)\n",num,controller,value,*controller);
646 }
647
648 float COMPRESSOR_get_ladspa_graph_value(void *Instance, int num){
649 PLUGIN* p = (PLUGIN*) Instance;
650 portData *port_data = p->fPortData;
651
652 float *controller = port_data->getOutputZone(num);
653 return *controller;
654 }
655
656 void COMPRESSOR_delete_ladspa(void *Instance){
657 //PLUGIN* p = (PLUGIN*) Instance;
658 // fix.
659 }
660