forked from grame-cncm/faust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdssi.cpp
1262 lines (1102 loc) · 38.5 KB
/
dssi.cpp
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/************************************************************************
IMPORTANT NOTE : this file contains two clearly delimited sections :
the ARCHITECTURE section (in two parts) and the USER section. Each section
is governed by its own copyright and license. Please check individually
each section for license and copyright information.
*************************************************************************/
/******************* BEGIN dssi.cpp ****************/
/************************************************************************
FAUST Architecture File
Copyright (C) 2003-2019 GRAME, Centre National de Creation Musicale
---------------------------------------------------------------------
This Architecture section 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 3 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, see <http://www.gnu.org/licenses/>.
EXCEPTION : As a special exception, you may create a larger work
that contains this FAUST architecture section and distribute
that work under terms of your choice, so long as this FAUST
architecture section is not modified.
************************************************************************
************************************************************************/
/********************************************************************
* dssi.cpp - Polyphonic dssi wrapper for the FAUST language.
*
* Usage: faust -a dssi.cpp myfaustprog.dsp
*
* By Michael J. Wilson ([email protected])
*
* Made with reference to:
* - vsti-mono.cpp by Julius Smith (http://ccrma.stanford.edu/~jos/)
* - ladspa.cpp by GRAME, Centre National de Creation Musicale
* - karplong.cpp by Chris Cannam, Steve Harris, Sean Bolton
*
* Because of the inclusion of code from ladspa.cpp, this architecture
* file is also released under the GNU General Public Licenses version
* 3. Sections which were taken from ladspa.cpp are clearly marked
* below, in order to trace the GPL dependency.
* As with faust2pd and vsti-mono.cpp, to obtain MIDI control via
* NoteOn/Off, Velocity, and KeyNumber, there must be a button named
* "gate" and sliders (or numeric entries) named "gain" and "freq" in
* the Faust patch specified in myfaustprog.dsp.
*
* FAUST
* Copyright (C) 2003-2007 GRAME, Centre National de Creation Musicale
* http://www.grame.fr/
*
********************************************************************/
#include "dssi.h"
#include "ladspa.h"
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <stack>
#include <string>
#include <map>
#include <vector>
#include <list>
using namespace std;
// On Intel set FZ (Flush to Zero) and DAZ (Denormals Are Zero)
// flags to avoid costly denormals
#ifdef __SSE__
#include <xmmintrin.h>
#ifdef __SSE2__
#define AVOIDDENORMALS _mm_setcsr(_mm_getcsr() | 0x8040)
#else
#define AVOIDDENORMALS _mm_setcsr(_mm_getcsr() | 0x8000)
#endif
#else
#warning *** dssi.cpp: NO SSE FLAG (denormals may slow things down) ***
#define AVOIDDENORMALS
#endif
struct Meta : std::map<const char*, const char*>
{
void declare (const char* key, const char* value) { (*this)[key]=value; }
};
#define sym(name) xsym(name)
#define xsym(name) #name
inline int lsr (int x, int n) { return int(((unsigned int)x) >> n); }
inline int int2pow2 (int x) { int r=0; while ((1<<r)<x) r++; return r; }
/******************************************************************************
*******************************************************************************
VECTOR INTRINSICS
*******************************************************************************
*******************************************************************************/
<<includeIntrinsic>>
//---------------------Abstract User Interface--------------------
//
// Abstract definition of a User Interface to be passed to the
// buildUserInterface method of a Faust Signal Processor
//
//----------------------------------------------------------------
class UI
{
bool fStopped;
public:
UI() : fStopped(false) {}
virtual ~UI() {}
virtual void addButton(const char* label, float* zone) = 0;
virtual void addToggleButton(const char* label, float* zone) = 0;
virtual void addCheckButton(const char* label, float* zone) = 0;
virtual void addVerticalSlider(const char* label, float* zone, float init, float min, float max, float step) = 0;
virtual void addHorizontalSlider(const char* label, float* zone, float init, float min, float max, float step) = 0;
virtual void addNumEntry(const char* label, float* zone, float init, float min, float max, float step) = 0;
// -- passive widgets
virtual void addNumDisplay(const char* label, float* zone, int precision) = 0;
virtual void addTextDisplay(const char* label, float* zone, char* names[], float min, float max) = 0;
virtual void addHorizontalBargraph(const char* label, float* zone, float min, float max) = 0;
virtual void addVerticalBargraph(const char* label, float* zone, float min, float max) = 0;
// -- frames and labels
virtual void openFrameBox(const char* label) = 0;
virtual void openTabBox(const char* label) = 0;
virtual void openHorizontalBox(const char* label) = 0;
virtual void openVerticalBox(const char* label) = 0;
virtual void closeBox() = 0;
virtual void show() = 0;
virtual void run() = 0;
void stop() { fStopped = true; }
bool stopped() { return fStopped; }
virtual void declare(float* zone, const char* key, const char* value) {}
};
//------------------Abstract Signal Processor---------------------
//
// Abstract definition of a Faust Signal Processor
//
//----------------------------------------------------------------
class dsp
{
protected:
int fSampleRate;
public:
dsp() {}
virtual ~dsp() {}
virtual int getNumInputs() = 0;
virtual int getNumOutputs() = 0;
virtual void buildUserInterface(UI* interface) = 0;
virtual void init(int sample_rate) = 0;
virtual void compute(int len, float** inputs, float** outputs)= 0;
};
/********************END ARCHITECTURE SECTION (part 1/2)****************/
/**************************BEGIN USER SECTION **************************/
<<includeclass>>
/***************************END USER SECTION ***************************/
/*******************BEGIN ARCHITECTURE SECTION (part 2/2)***************/
////////////////////////////////////////////////////////////////////////////////
// Forward declarations
////////////////////////////////////////////////////////////////////////////////
class Plugin;
class DescriptorUI;
class Voice;
////////////////////////////////////////////////////////////////////////////////
// Global data
////////////////////////////////////////////////////////////////////////////////
// Maximum polyphony, must be at least 1 (TODO make this configurable at compile / runtime?)
const int MAX_POLYPHONY = 64;
// Descriptor
DSSI_Descriptor* g_dssi_descriptor;
// Additional data for descriptor:
LADSPA_Descriptor* g_ladspa_descriptor;
// Program descriptor:
DSSI_Program_Descriptor g_program_descriptor;
// Global data for the descriptor:
std::vector<LADSPA_PortDescriptor> g_port_descriptors;
std::vector<LADSPA_PortRangeHint> g_port_range_hints;
std::vector<const char*> g_port_names;
const char* g_name;
////////////////////////////////////////////////////////////////////////////////
// The enclosed code is from ladspa.cpp
// TODO groups of port names
static const char* inames[] =
{
"input00", "input01", "input02", "input03", "input04",
"input05", "input06", "input07", "input08", "input09",
"input10", "input11", "input12", "input13", "input14",
"input15", "input16", "input17", "input18", "input19",
"input20", "input21", "input22", "input23", "input24",
"input25", "input26", "input27", "input28", "input29",
"input30", "input31", "input32", "input33", "input34",
"input35", "input36", "input37", "input38", "input39"
};
static const char* onames[] =
{
"output00", "output01", "output02", "output03", "output04",
"output05", "output06", "output07", "output08", "output09",
"output10", "output11", "output12", "output13", "output14",
"output15", "output16", "output17", "output18", "output19",
"output20", "output21", "output22", "output23", "output24",
"output25", "output26", "output27", "output28", "output29",
"output30", "output31", "output32", "output33", "output34",
"output35", "output36", "output37", "output38", "output39"
};
// END code from ladspa.cpp
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Global helper functions
////////////////////////////////////////////////////////////////////////////////
char* get_metadata_if_exists(const char* key, const char* default_string)
{
// TODO probably want to free these somehow. Currently only used for ladspa descriptor
Meta meta;
static mydsp dsp;
dsp.metadata(&meta);
if(meta.find(key) != meta.end())
{
return strdup(meta[key]);
}
else
{
return strdup(default_string);
}
}
////////////////////////////////////////////////////////////////////////////////
// The enclosed code is from ladspa.cpp
std::string simplify(const std::string& src)
{
int i=0;
int level=2;
std::string dst;
while (src[i] ) {
switch (level) {
case 0 :
case 1 :
case 2 :
// Skip the begin of the label "--foo-"
// until 3 '-' have been read
if (src[i]=='-') { level++; }
break;
case 3 :
// copy the content, but skip non alphnum
// and content in parenthesis
switch (src[i]) {
case '(' :
case '[' :
level++;
break;
case '-' :
dst += '-';
break;
default :
if (isalnum(src[i])) {
dst+= tolower(src[i]);
}
}
break;
default :
// here we are inside parenthesis and
// we skip the content until we are back to
// level 3
switch (src[i]) {
case '(' :
case '[' :
level++;
break;
case ')' :
case ']' :
level--;
break;
default :
break;
}
}
i++;
}
return (dst.size() > 0) ? dst :src;
}
// END code from ladspa.cpp
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Single voice; use multiple for polyphony
////////////////////////////////////////////////////////////////////////////////
class Voice : public UI
{
public:
Voice(int sampleRate);
~Voice();
// UI methods:
// TODO don't hardcode these so hard maybe
void setFreq(float val);
void setGate(float val);
void setGain(float val);
virtual void addButton(const char* label, float* zone);
virtual void addToggleButton(const char* label, float* zone);
virtual void addCheckButton(const char* label, float* zone);
virtual void addVerticalSlider(const char* label, float* zone, float init, float min, float max, float step);
virtual void addHorizontalSlider(const char* label, float* zone, float init, float min, float max, float step);
virtual void addNumEntry(const char* label, float* zone, float init, float min, float max, float step);
virtual void addNumDisplay(const char* label, float* zone, int precision);
virtual void addTextDisplay(const char* label, float* zone, char* names[], float min, float max);
virtual void addHorizontalBargraph(const char* label, float* zone, float min, float max);
virtual void addVerticalBargraph(const char* label, float* zone, float min, float max);
virtual void openFrameBox(const char* label);
virtual void openTabBox(const char* label);
virtual void openHorizontalBox(const char* label);
virtual void openVerticalBox(const char* label);
virtual void closeBox();
virtual void show();
virtual void run();
// Internal control (to Faust DSP)
std::vector<float*> m_controls;
// TODO maybe don't make this public eventually
mydsp* m_mydsp;
private:
// Helpers for UI building:
// TODO organize this a bit better later...
bool ckAnyMatch(const char* label, const char* indexName, float **zone, float* newZone);
bool ckAllMatches(const char* label, float* zone);
void addZone(float* zone);
int m_samplerate;
float* m_freq_zone;
float* m_gate_zone;
float* m_gain_zone;
};
////////////////////////////////////////////////////////////////////////////////
// Plugin class to handle DSSI methods:
////////////////////////////////////////////////////////////////////////////////
class Plugin : public UI
{
public:
// LADSPA methods:
static LADSPA_Handle instantiate(const LADSPA_Descriptor *, unsigned long);
static void connectPort(LADSPA_Handle, unsigned long, LADSPA_Data *);
static void activate(LADSPA_Handle);
static void run(LADSPA_Handle, unsigned long);
static void cleanup(LADSPA_Handle);
// DSSI methods:
static const DSSI_Program_Descriptor* getProgram(LADSPA_Handle, unsigned long);
static void selectProgram(LADSPA_Handle, unsigned long, unsigned long);
static int getMidiController(LADSPA_Handle, unsigned long);
static void runSynth(LADSPA_Handle, unsigned long, snd_seq_event_t *, unsigned long);
// UI methods:
// TODO don't hardcode these so hard maybe
void setFreq(float val, int voice = 0);
void setGate(float val, int voice = 0);
void setGain(float val, int voice = 0);
virtual void addButton(const char* label, float* zone);
virtual void addToggleButton(const char* label, float* zone);
virtual void addCheckButton(const char* label, float* zone);
virtual void addVerticalSlider(const char* label, float* zone, float init, float min, float max, float step);
virtual void addHorizontalSlider(const char* label, float* zone, float init, float min, float max, float step);
virtual void addNumEntry(const char* label, float* zone, float init, float min, float max, float step);
virtual void addNumDisplay(const char* label, float* zone, int precision);
virtual void addTextDisplay(const char* label, float* zone, char* names[], float min, float max);
virtual void addHorizontalBargraph(const char* label, float* zone, float min, float max);
virtual void addVerticalBargraph(const char* label, float* zone, float min, float max);
virtual void openFrameBox(const char* label);
virtual void openTabBox(const char* label);
virtual void openHorizontalBox(const char* label);
virtual void openVerticalBox(const char* label);
virtual void closeBox();
virtual void show();
virtual void run();
private:
Plugin(int sampleRate);
~Plugin();
// Helper methods:
void updateControlZones();
void runImpl(unsigned long, snd_seq_event_t *, unsigned long);
void addSamples(size_t);
// Helpers for UI building:
// TODO organize this a bit better later...
bool ckAnyMatch(const char* label, const char* indexName);
bool ckAllMatches(const char* label);
void add_control_with_default(float default_value);
int m_samplerate;
// Voice allocation memebers (TODO maybe break voice allocator into separate class later)
// Note each voice is playing
std::vector<int> voice_notes;
// Queue of free voices
std::list<size_t> voice_free;
// Voices for polyphony
std::vector<Voice*> m_voices;
// Top-level inputs (from DSSI host)
std::vector<float*> m_inputs;
// Top-level outputs (to DSSI host)
std::vector<float*> m_outputs;
// Temp vector for collecting outputs TODO this is an ugly way to do it...
std::vector<std::vector<float> > m_temp_outputs;
// External control (from DSSI host)
std::vector<float*> m_controls;
// Control default values
std::vector<float> m_control_defaults;
};
////////////////////////////////////////////////////////////////////////////////
// UI class to build descriptor, analogous to ladspa.cpp's portCollector
////////////////////////////////////////////////////////////////////////////////
const int ICONTROL = LADSPA_PORT_INPUT|LADSPA_PORT_CONTROL;
const int OCONTROL = LADSPA_PORT_OUTPUT|LADSPA_PORT_CONTROL;
class DescriptorUI : public UI
{
public:
DescriptorUI(int ins, int outs);
virtual void addButton(const char* label, float* zone) {
if (!ckAnyMatch(label, "gate"))
{
addPortDescr(ICONTROL, label, LADSPA_HINT_TOGGLED);
}
}
virtual void addToggleButton(const char* label, float* zone) {
addPortDescr(ICONTROL, label, LADSPA_HINT_TOGGLED);
}
virtual void addCheckButton(const char* label, float* zone) {
addPortDescr(ICONTROL, label, LADSPA_HINT_TOGGLED);
}
virtual void addVerticalSlider(const char* label, float* zone, float init, float min, float max, float step) {
if (!ckAllMatches(label))
{
addPortDescr(ICONTROL, label, LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE, min, max);
}
}
virtual void addHorizontalSlider(const char* label, float* zone, float init, float min, float max, float step) {
if (!ckAllMatches(label))
{
addPortDescr(ICONTROL, label, LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE, min, max);
}
}
virtual void addNumEntry(const char* label, float* zone, float init, float min, float max, float step) {
if (!ckAllMatches(label))
{
addPortDescr(ICONTROL, label, LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE, min, max);
}
}
virtual void addNumDisplay(const char* label, float* zone, int precision) {
addPortDescr(OCONTROL, label, 0, -10000, +10000);
}
virtual void addTextDisplay(const char* label, float* zone, char* names[], float min, float max) {
addPortDescr(OCONTROL, label, LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE, min, max);
}
virtual void addHorizontalBargraph(const char* label, float* zone, float min, float max) {
addPortDescr(OCONTROL, label, LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE, min, max);
}
virtual void addVerticalBargraph(const char* label, float* zone, float min, float max){
addPortDescr(OCONTROL, label, LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE, min, max);
}
virtual void openFrameBox(const char* label) { openAnyBox(label); }
virtual void openTabBox(const char* label) { openAnyBox(label); }
virtual void openHorizontalBox(const char* label) { openAnyBox(label); }
virtual void openVerticalBox(const char* label) { openAnyBox(label); }
virtual void closeBox() { fPrefix.pop(); }
virtual void show() {}
virtual void run() {}
private:
std::stack<std::string> fPrefix;
void addPortDescr(int type, const char* label, int hint, float min=0.0, float max=0.0);
void openAnyBox(const char* label);
bool ckAnyMatch(const char* label, const char* indexName)
{
// TODO do case-insensitive here...
if (strcmp(label,indexName)==0)
{
// Don't set values in the DescriptorUI
// TODO consolidate this later
return true;
}
return false;
}
bool ckAllMatches(const char* label)
{
bool result = false;
result = result || ckAnyMatch(label,"gain");
result = result || ckAnyMatch(label,"gate");
result = result || ckAnyMatch(label,"freq");
return result;
}
};
////////////////////////////////////////////////////////////////////////////////
// DescriptorUI methods
////////////////////////////////////////////////////////////////////////////////
DescriptorUI::DescriptorUI(int ins, int outs)
{
// Note inputs and outputs
for (int i = 0; i < ins; i++)
{
g_port_descriptors.push_back(LADSPA_PORT_INPUT | LADSPA_PORT_AUDIO);
LADSPA_PortRangeHint temp;
temp.HintDescriptor = 0;
temp.LowerBound = 0;
temp.UpperBound = 0;
g_port_range_hints.push_back(temp);
g_port_names.push_back(inames[i]);
}
for (int j = 0; j < outs; j++)
{
g_port_descriptors.push_back(LADSPA_PORT_OUTPUT | LADSPA_PORT_AUDIO);
LADSPA_PortRangeHint temp;
temp.HintDescriptor = 0;
temp.LowerBound = 0;
temp.UpperBound = 0;
g_port_range_hints.push_back(temp);
g_port_names.push_back(onames[j]);
}
}
void DescriptorUI::addPortDescr(int type, const char* label, int hint, float min, float max)
{
////////////////////////////////////////////////////////////////////////////////
// The enclosed code is derived from ladspa.cpp
std::string fullname = simplify(fPrefix.top() + "-" + label);
// TODO for debugging, I'm just using the label for now instead of the full name (since fullname can get long)
// char * str = strdup(fullname.c_str());
char * str = strdup(label);
// END code from ladspa.cpp
////////////////////////////////////////////////////////////////////////////////
g_port_descriptors.push_back(type);
LADSPA_PortRangeHint temp;
temp.HintDescriptor = hint;
temp.LowerBound = min;
temp.UpperBound = max;
g_port_range_hints.push_back(temp);
g_port_names.push_back(str); // TODO memory leak; need to free
}
void DescriptorUI::openAnyBox(const char* label)
{
////////////////////////////////////////////////////////////////////////////////
// The enclosed code is from ladspa.cpp
if (fPrefix.size() == 0)
{
// top level label is used as plugin name
g_name = label;
fPrefix.push(label);
}
else
{
std::string s;
if (label && label[0])
{
s = fPrefix.top() + "-" + label;
}
else
{
s = fPrefix.top();
}
fPrefix.push(s);
}
// END code from ladspa.cpp
////////////////////////////////////////////////////////////////////////////////
}
////////////////////////////////////////////////////////////////////////////////
// Plugin methods
////////////////////////////////////////////////////////////////////////////////
Plugin::Plugin(int sampleRate) :
m_samplerate(sampleRate)
{
int i;
mydsp temp_mydsp;
for (i = 0; i < MAX_POLYPHONY; i++)
{
m_voices.push_back(new Voice(m_samplerate));
// Voice parameters:
voice_free.push_back(i);
// TODO using -1 to represent nothing; think of a more clear way to do this
voice_notes.push_back(-1);
}
for (i = 0; i < temp_mydsp.getNumInputs(); i++)
{
m_inputs.push_back(0);
}
for (i = 0; i < temp_mydsp.getNumOutputs(); i++)
{
m_outputs.push_back(0);
std::vector<float> temp;
temp.push_back(0.0);
m_temp_outputs.push_back(temp);
}
}
Plugin::~Plugin()
{
size_t i;
for (i = 0; i < m_voices.size(); i++)
{
delete m_voices[i];
}
}
LADSPA_Handle Plugin::instantiate(const LADSPA_Descriptor *, unsigned long rate)
{
Plugin *plugin = new Plugin(rate);
size_t i;
mydsp* temp_mydsp = new mydsp();
temp_mydsp->buildUserInterface(plugin);
delete temp_mydsp;
for (i = 0; i < plugin->m_voices.size(); i++)
{
plugin->m_voices[i]->m_mydsp->buildUserInterface(plugin->m_voices[i]);
}
return plugin;
}
void Plugin::connectPort(LADSPA_Handle handle, unsigned long port, LADSPA_Data *location)
{
Plugin *plugin = (Plugin *)handle;
// Map inputs, then outputs, then controls:
if (port < plugin->m_inputs.size())
{
*(&plugin->m_inputs[port]) = (float *)location;
}
else if (port < plugin->m_inputs.size() + plugin->m_outputs.size())
{
*(&plugin->m_outputs[port - plugin->m_inputs.size()]) = (float *)location;
}
else
{
plugin->m_controls[port - plugin->m_inputs.size() - plugin->m_outputs.size()] = (float *)location;
}
}
void Plugin::activate(LADSPA_Handle handle)
{
Plugin *plugin = (Plugin *)handle;
for (size_t i = 0; i < plugin->m_voices.size(); i++)
{
plugin->m_voices[i]->m_mydsp->init(plugin->m_samplerate);
}
}
void Plugin::run(LADSPA_Handle handle, unsigned long samples)
{
runSynth(handle, samples, 0, 0);
}
void Plugin::cleanup(LADSPA_Handle handle)
{
delete (Plugin *)handle;
}
const DSSI_Program_Descriptor* Plugin::getProgram(LADSPA_Handle handle, unsigned long index)
{
if (index == 0)
{
return &g_program_descriptor;
}
else
{
return NULL;
}
}
void Plugin::selectProgram(LADSPA_Handle handle, unsigned long bank, unsigned long program)
{
Plugin *plugin = (Plugin *)handle;
for (size_t i = 0; i < plugin->m_controls.size(); i++)
{
*(plugin->m_controls[i]) = plugin->m_control_defaults[i];
}
}
int Plugin::getMidiController(LADSPA_Handle, unsigned long port)
{
// TODO this is where we need to map MIDI controllers to ports
return DSSI_NONE;
}
void Plugin::updateControlZones()
{
for (size_t i = 0; i < m_controls.size(); i++)
{
for (size_t j = 0; j < m_voices.size(); j++)
{
*(m_voices[j]->m_controls[i]) = *(m_controls[i]);
}
}
}
void Plugin::runSynth(LADSPA_Handle handle, unsigned long samples, snd_seq_event_t *events, unsigned long eventCount)
{
Plugin *plugin = (Plugin *)handle;
plugin->updateControlZones();
plugin->runImpl(samples, events, eventCount);
}
void Plugin::runImpl(unsigned long sampleCount, snd_seq_event_t *events, unsigned long eventCount)
{
unsigned long pos;
unsigned long count;
unsigned long eventPos;
snd_seq_ev_note_t n;
size_t voice_index;
pos = 0;
eventPos = 0;
while (pos < sampleCount)
{
while ((eventPos < eventCount) &&
(pos >= events[eventPos].time.tick))
{
switch (events[eventPos].type)
{
case SND_SEQ_EVENT_NOTEON:
n = events[eventPos].data.note;
if (n.velocity > 0)
{
// Look for the next free voice:
if (!voice_free.empty())
{
// Get the index of the first free string and remove it from the list
voice_index = voice_free.front();
voice_free.pop_front();
// Play the note on that voice
voice_notes[voice_index] = n.note;
float freq = 440.0f * powf(2.0f,(((float)n.note)-69.0f)/12.0f);
float gain = n.velocity/127.0f;
setFreq(freq, voice_index); // Hz - requires Faust control-signal "freq"
setGain(gain, voice_index); // 0-1 - requires Faust control-signal "gain"
setGate(1.0f, voice_index); // 0 or 1 - requires Faust button-signal "gate"
}
}
break;
case SND_SEQ_EVENT_NOTEOFF:
for (voice_index = 0; voice_index < voice_notes.size(); voice_index++)
{
if (voice_notes[voice_index] == events[eventPos].data.note.note)
{
setGate(0, voice_index);
// TODO using -1 to represent nothing; think of a more clear way to do this
voice_notes[voice_index] = -1;
voice_free.push_back(voice_index);
}
}
break;
default:
break;
}
++eventPos;
}
if ((eventPos < eventCount) && (events[eventPos].time.tick < sampleCount))
{
count = events[eventPos].time.tick - pos;
}
else
{
count = sampleCount - pos;
}
addSamples(count);
pos += count;
}
}
void Plugin::addSamples(size_t samples)
{
size_t i;
size_t j;
size_t v;
// TODO this isn't very efficient right now...
// Grow temp buffers to appropriate size / zero them:
for (i = 0; i < m_temp_outputs.size(); i++)
{
for (j = 0; j < samples; j++)
{
if (m_temp_outputs[i].size() < (j+1))
{
m_temp_outputs[i].push_back(0.0);
}
else
{
m_temp_outputs[i][j] = 0.0;
}
}
}
AVOIDDENORMALS;
// Add all voices together
for (v = 0; v < m_voices.size(); v++)
{
m_voices[v]->m_mydsp->compute(samples, &m_inputs[0], &m_outputs[0]);
// Accumulate in temp buffer
for (i = 0; i < m_outputs.size(); i++)
{
for (j = 0; j < samples; j++)
{
m_temp_outputs[i][j] += m_outputs[i][j];
}
}
}
// Transfer accumulator to actual outputs
for (i = 0; i < m_outputs.size(); i++)
{
for (j = 0; j < samples; j++)
{
// TODO find a better way to protect against clipping.
m_outputs[i][j] = m_temp_outputs[i][j] / (float)MAX_POLYPHONY;
}
}
}
void Plugin::setFreq(float val, int voice)
{
m_voices[voice]->setFreq(val);
}
void Plugin::setGate(float val, int voice)
{
m_voices[voice]->setGate(val);
}
void Plugin::setGain(float val, int voice)
{
m_voices[voice]->setGain(val);
}
void Plugin::addButton(const char* label, float* zone)
{
if (!ckAnyMatch(label, "gate"))
{
add_control_with_default(0);
}
}
void Plugin::addToggleButton(const char* label, float* zone)
{
add_control_with_default(0);
}
void Plugin::addCheckButton(const char* label, float* zone)
{
add_control_with_default(0);
}
void Plugin::addVerticalSlider(const char* label, float* zone, float init, float min, float max, float step)
{
if (!ckAllMatches(label))
{
add_control_with_default(init);
}
}
void Plugin::addHorizontalSlider(const char* label, float* zone, float init, float min, float max, float step)
{
if (!ckAllMatches(label))
{
add_control_with_default(init);
}
}
void Plugin::addNumEntry(const char* label, float* zone, float init, float min, float max, float step)
{
if (!ckAllMatches(label))
{
add_control_with_default(init);
}
}
void Plugin::addNumDisplay(const char* label, float* zone, int precision)
{
add_control_with_default(0);
}
void Plugin::addTextDisplay(const char* label, float* zone, char* names[], float min, float max)
{
add_control_with_default(0);
}
void Plugin::addHorizontalBargraph(const char* label, float* zone, float min, float max)
{
add_control_with_default(min);
}
void Plugin::addVerticalBargraph(const char* label, float* zone, float min, float max)
{
add_control_with_default(min);
}
void Plugin::openFrameBox(const char* label)
{
}
void Plugin::openTabBox(const char* label)
{
}
void Plugin::openHorizontalBox(const char* label)
{
}
void Plugin::openVerticalBox(const char* label)
{
}
void Plugin::closeBox()
{
}
void Plugin::show()
{
}
void Plugin::run()
{
}
bool Plugin::ckAnyMatch(const char* label, const char* indexName)
{
// TODO do case-insensitive here...
if (strcmp(label,indexName)==0)
{
return true;
}
return false;
}