summaryrefslogtreecommitdiff
path: root/muse_qt4_evolution/muse/vst.cpp
blob: f91d1322e221400967c73c2b5f5a888d92a04ab2 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
//=============================================================================
//  MusE
//  Linux Music Editor
//  $Id:$
//
//  Copyright (C) 2002-2006 by Werner Schweer and others
//   This code is based on jack_fst:
//   Copyright (C) 2004 Paul Davis <paul@linuxaudiosystems.com>
//                      Torben Hohn <torbenh@informatik.uni-bremen.de>
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License version 2.
//
//  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., 675 Mass Ave, Cambridge, MA 02139, USA.
//=============================================================================

#include "config.h"

#ifdef VST_SUPPORT

#include "al/al.h"
#include <fst.h>
#include <vst/aeffectx.h>
#include <jack/jack.h>

#include "vst.h"
#include "globals.h"
#include "synth.h"
#include "jackaudio.h"
#include "midi.h"
#include "al/xml.h"
#include "song.h"
#include "audio.h"
#include "widgets/utils.h"

extern "C" void fst_error(const char *fmt, ...);
extern JackAudio* jackAudio;

//---------------------------------------------------------
//   jfstReserveMem
//---------------------------------------------------------

static void jfstReserveMem(int size)
      {
      char buf[size];

      for (int i = 0; i < size; ++i)
            buf[i] = (char)(i % 256);
      }

//---------------------------------------------------------
//   vstHostCallback
//---------------------------------------------------------

static long vstHostCallback(AEffect* effect,
   long opcode, long index, long value, void* ptr, float opt)
      {
      static VstTimeInfo _timeInfo;

      VstPluginIF* pluginIF = effect ? (VstPluginIF*) effect->user : 0;

      jack_position_t jack_pos;
      jack_transport_state_t tstate;

      switch (opcode) {
            case audioMasterAutomate:
                  // index, value, returns 0
                  if (pluginIF) {
                        PluginI* pi = pluginIF->pluginInstance();
                        // pi->setParam(index, opt);
                        CVal cval;
                        cval.f = opt;
                        song->setControllerVal(pi->track(), pi->controller(index), cval);
                        }
                  return 0;

            case audioMasterVersion:
                  // vst version, currently 2 (0 for older)
                  return 2200;

            case audioMasterCurrentId:
                  // returns the unique id of a plug that's currently
                  // loading
                  return 0;

            case audioMasterIdle:
                  // call application idle routine (this will
                  // call effEditIdle for all open editors too)
                  effect->dispatcher(effect, effEditIdle, 0, 0, NULL, 0.0f);
                  return 0;

            case audioMasterPinConnected:
                  // inquire if an input or output is beeing connected;
                  // index enumerates input or output counting from zero:
                  // value is 0 for input and != 0 otherwise. note: the
                  // return value is 0 for <true> such that older versions
                  // will always return true.
                  return 1;

            case audioMasterWantMidi:
                  // <value> is a filter which is currently ignored
                  return 0;

            case audioMasterGetTime:
                  // returns const VstTimeInfo* (or 0 if not supported)
                  // <value> should contain a mask indicating which fields are required
                  // (see valid masks above), as some items may require extensive
                  // conversions

                  memset(&_timeInfo, 0, sizeof(_timeInfo));

                  if (effect) {
                        tstate = jackAudio->transportQuery(&jack_pos);

                        _timeInfo.samplePos  = jack_pos.frame;
                        _timeInfo.sampleRate = jack_pos.frame_rate;
                        _timeInfo.flags = 0;

                        if ((value & (kVstBarsValid|kVstTempoValid)) && (jack_pos.valid & JackPositionBBT)) {
                              _timeInfo.tempo = jack_pos.beats_per_minute;
                              _timeInfo.timeSigNumerator = (long) floor (jack_pos.beats_per_bar);
                              _timeInfo.timeSigDenominator = (long) floor (jack_pos.beat_type);
                              _timeInfo.flags |= (kVstBarsValid|kVstTempoValid);
                              }
                        if (tstate == JackTransportRolling) {
                              _timeInfo.flags |= kVstTransportPlaying;
                              }
                        }
                  else {
                        _timeInfo.samplePos  = 0;
                        _timeInfo.sampleRate = AL::sampleRate;
                        }
                  return (long)&_timeInfo;

            case audioMasterProcessEvents:
                  // VstEvents* in <ptr>
                  return 0;

            case audioMasterSetTime:
                  // VstTimenfo* in <ptr>, filter in <value>, not supported

            case audioMasterTempoAt:
                  // returns tempo (in bpm * 10000) at sample frame location passed in <value>
                  return 0;

            case audioMasterGetNumAutomatableParameters:
                  return 0;

            case audioMasterGetParameterQuantization:
                     // returns the integer value for +1.0 representation,
                   // or 1 if full single float precision is maintained
                     // in automation. parameter index in <value> (-1: all, any)
                  return 0;

            case audioMasterIOChanged:
                   // numInputs and/or numOutputs has changed
                  return 0;

            case audioMasterNeedIdle:
                   // plug needs idle calls (outside its editor window)
                  return 0;

            case audioMasterSizeWindow:
                  // index: width, value: height
                  return 0;

            case audioMasterGetSampleRate:
                  return 0;

            case audioMasterGetBlockSize:
                  return 0;

            case audioMasterGetInputLatency:
                  return 0;

            case audioMasterGetOutputLatency:
                  return 0;

            case audioMasterGetPreviousPlug:
                   // input pin in <value> (-1: first to come), returns cEffect*
                  return 0;

            case audioMasterGetNextPlug:
                   // output pin in <value> (-1: first to come), returns cEffect*

            case audioMasterWillReplaceOrAccumulate:
                   // returns: 0: not supported, 1: replace, 2: accumulate
                  return 0;

            case audioMasterGetCurrentProcessLevel:
                  // returns: 0: not supported,
                  // 1: currently in user thread (gui)
                  // 2: currently in audio thread (where process is called)
                  // 3: currently in 'sequencer' thread (midi, timer etc)
                  // 4: currently offline processing and thus in user thread
                  // other: not defined, but probably pre-empting user thread.
                  return 0;

            case audioMasterGetAutomationState:
                  // returns 0: not supported, 1: off, 2:read, 3:write, 4:read/write
                  // offline
                  return 0;

            case audioMasterOfflineStart:
            case audioMasterOfflineRead:
                   // ptr points to offline structure, see below. return 0: error, 1 ok
                  return 0;

            case audioMasterOfflineWrite:
                  // same as read
                  return 0;

            case audioMasterOfflineGetCurrentPass:
            case audioMasterOfflineGetCurrentMetaPass:
                  return 0;

            case audioMasterSetOutputSampleRate:
                  // for variable i/o, sample rate in <opt>
                  return 0;

            case audioMasterGetSpeakerArrangement:
                  // (long)input in <value>, output in <ptr>
                  return 0;

            case audioMasterGetVendorString:
                  // fills <ptr> with a string identifying the vendor (max 64 char)
                  strcpy ((char*) ptr, "LAD");
                  return 0;

            case audioMasterGetProductString:
                  // fills <ptr> with a string with product name (max 64 char)
                  strcpy ((char*) ptr, "FreeST");

            case audioMasterGetVendorVersion:
                  // returns vendor-specific version
                  return 1000;

            case audioMasterVendorSpecific:
                  // no definition, vendor specific handling
                  return 0;

            case audioMasterSetIcon:
                  // void* in <ptr>, format not defined yet
                  return 0;

            case audioMasterCanDo:
                  // string in ptr, see below
                  return 0;

            case audioMasterGetLanguage:
                  // see enum
                  return 0;

            case audioMasterOpenWindow:
                  // returns platform specific ptr
                  return 0;

            case audioMasterCloseWindow:
                  // close window, platform specific handle in <ptr>
                  return 0;

            case audioMasterGetDirectory:
                  // get plug directory, FSSpec on MAC, else char*
                  return 0;

            case audioMasterUpdateDisplay:
                  // something has changed, update 'multi-fx' display
                  effect->dispatcher(effect, effEditIdle, 0, 0, NULL, 0.0f);
                  return 0;

            case audioMasterBeginEdit:
                  // begin of automation session (when mouse down), parameter index in <index>
                  return 0;

            case audioMasterEndEdit:
                  // end of automation session (when mouse up),     parameter index in <index>
                  return 0;

            case audioMasterOpenFileSelector:
                  // open a fileselector window with VstFileSelect* in <ptr>
                  return 0;

            default:
                  break;
            }
      return 0;
      }


//---------------------------------------------------------
//   FstPlugin
//---------------------------------------------------------

FstPlugin::FstPlugin()
      {
      _fst = 0;
      }

//---------------------------------------------------------
//   ~FstPlugin
//---------------------------------------------------------

FstPlugin::~FstPlugin()
      {
      if (_fst) {
            destroyEditor();
            fst_close(_fst);
            _fst = 0;
            }
      }

//---------------------------------------------------------
//   instantiate
//---------------------------------------------------------

void FstPlugin::instantiate(FSTHandle* fstHandle, void* p)
      {
      _fst = fst_instantiate(fstHandle, vstHostCallback, p);
      if (_fst == 0) {
            printf("FstPlugin::instantiate:: cannot instantiate plugin\n");
            return;
            }
      AEffect* plugin = _fst->plugin;
      version = plugin->dispatcher (plugin, effGetVstVersion, 0, 0, 0, 0.0f);
      }

//---------------------------------------------------------
//   numInputs
//---------------------------------------------------------

int FstPlugin::numInputs() const
      {
      return _fst->plugin->numInputs;
      }

//---------------------------------------------------------
//   numOutputs
//---------------------------------------------------------

int FstPlugin::numOutputs() const
      {
      return _fst->plugin->numOutputs;
      }

//---------------------------------------------------------
//   numParameter
//---------------------------------------------------------

int FstPlugin::numParameter() const
      {
      return _fst->plugin->numParams;
      }

//---------------------------------------------------------
//   setSampleRate
//---------------------------------------------------------

void FstPlugin::setSampleRate(float sr)
      {
      AEffect* plugin = _fst->plugin;
      plugin->dispatcher(plugin, effSetSampleRate, 0, 0, 0, sr);
      }

//---------------------------------------------------------
//   setBlockSize
//---------------------------------------------------------

void FstPlugin::setBlockSize(int bs)
      {
      AEffect* plugin = _fst->plugin;
      plugin->dispatcher(plugin, effSetBlockSize,  0, bs, 0, 0.0f);
      }

//---------------------------------------------------------
//   mainsChanged
//---------------------------------------------------------

void FstPlugin::mainsChanged(bool on)
      {
      AEffect* plugin = _fst->plugin;
      plugin->dispatcher(plugin, effMainsChanged, 0, on, 0, 0.0f);
      }

//---------------------------------------------------------
//   setProgram
//---------------------------------------------------------

void FstPlugin::setProgram(int p)
      {
      AEffect* plugin = _fst->plugin;
      plugin->dispatcher(plugin, effSetProgram, 0, p, 0, 0.0f);
      }

//---------------------------------------------------------
//   hasGui
//---------------------------------------------------------

bool FstPlugin::hasGui() const
      {
      return _fst->plugin->flags & effFlagsHasEditor;
      }

//---------------------------------------------------------
//   canReplacing
//---------------------------------------------------------

bool FstPlugin::canReplacing()
      {
      return _fst->plugin->flags & effFlagsCanReplacing;
      }

//---------------------------------------------------------
//   runEditor
//---------------------------------------------------------

bool FstPlugin::runEditor()
      {
      return fst_run_editor(_fst);
      }

//---------------------------------------------------------
//   destroyEditor
//---------------------------------------------------------

void FstPlugin::destroyEditor()
      {
      fst_destroy_editor(_fst);
      }

//---------------------------------------------------------
//   getVstVersion
//---------------------------------------------------------

int FstPlugin::getVstVersion()
      {
      return version;
      }

//---------------------------------------------------------
//   setParameter
//---------------------------------------------------------

void FstPlugin::setParameter(int idx, float value)
      {
      AEffect* plugin = _fst->plugin;
      plugin->setParameter(plugin, idx, value);
      }

//---------------------------------------------------------
//   getParameter
//---------------------------------------------------------

float FstPlugin::getParameter(int idx)
      {
      AEffect* plugin = _fst->plugin;
      return plugin->getParameter(plugin, idx);
      }

//---------------------------------------------------------
//   processReplacing
//---------------------------------------------------------

void FstPlugin::processReplacing(float** ins, float** outs, int n)
      {
      AEffect* plugin = _fst->plugin;
      plugin->processReplacing(plugin, ins, outs, n);
      }

//---------------------------------------------------------
//   process
//---------------------------------------------------------

void FstPlugin::process(float** ins, float** outs, int n)
      {
      AEffect* plugin = _fst->plugin;
      plugin->process(plugin, ins, outs, n);
      }

//---------------------------------------------------------
//   putEvent
//---------------------------------------------------------

void FstPlugin::putEvent(const MidiEvent& ev)
      {
      AEffect* plugin = _fst->plugin;
      static struct VstEvents events;
      static struct VstMidiEvent event;
      events.numEvents = 1;
      events.reserved  = 0;
      events.events[0] = (VstEvent*)(&event);

      event.type         = kVstMidiType;
      event.byteSize     = 24;
      event.deltaFrames  = 0;
      event.flags        = 0;
      event.detune       = 0;
      event.noteLength   = 0;
      event.noteOffset   = 0;
      event.reserved1    = 0;
      event.reserved2    = 0;
      event.noteOffVelocity = 0;
      switch (ev.type()) {
            case ME_PITCHBEND:
                  {
                  int a = ev.dataA() + 8192;
                  int b = a >> 7;
                  event.midiData[0]  = (ev.type() | ev.channel()) & 0xff;
                  event.midiData[1]  = a & 0x7f;
                  event.midiData[2]  = b & 0x7f;
                  event.midiData[3]  = 0;
                  }
                  break;

            case ME_CONTROLLER:
            case ME_NOTEON:
            default:
                  event.midiData[0]  = (ev.type() | ev.channel()) & 0xff;
                  event.midiData[1]  = ev.dataA() & 0xff;
                  event.midiData[2]  = ev.dataB() & 0xff;
                  event.midiData[3]  = 0;
                  break;
            }
      plugin->dispatcher(plugin, effProcessEvents, 0, 0, &events, 0.0f);
      }

//---------------------------------------------------------
//   getParameterName
//---------------------------------------------------------

const char* FstPlugin::getParameterName(int idx) const
      {
      static char dsp[8];
      dsp[0] = 0;
      AEffect* effect = _fst->plugin;
      effect->dispatcher(effect, effGetParamName, idx, 0, &dsp, 0.0f);
      return dsp;
      }

//---------------------------------------------------------
//   getParameterLabel
//---------------------------------------------------------

const char* FstPlugin::getParameterLabel(int idx) const
      {
      static char dsp[8];
      dsp[0] = 0;
      AEffect* effect = _fst->plugin;
      effect->dispatcher(effect, effGetParamLabel, idx, 0, &dsp, 0.0f);
      return dsp;
      }

//---------------------------------------------------------
//   getParameterDisplay
//---------------------------------------------------------

const char* FstPlugin::getParameterDisplay(int idx, float val) const
      {
      static char dsp[8];
      dsp[0] = 0;
      AEffect* effect = _fst->plugin;
      effect->dispatcher(effect, effGetParamDisplay, idx, 0, &dsp, val);
      return dsp;
      }

//---------------------------------------------------------
//   scanVstDir
//---------------------------------------------------------

static void scanVstDir(const QString& s)
      {
      if (debugMsg)
            printf("scan vst plugin dir <%s>\n", s.toLatin1().data());
      QDir pluginDir(s, QString("*.dll"), QDir::Unsorted, QDir::Files);
      if (pluginDir.exists()) {
            const QFileInfoList list = pluginDir.entryInfoList();
            for (int i = 0; i < list.size(); ++i) {
                  QFileInfo fi = list.at(i);
                  char* path = strdup(fi.filePath().toLatin1().data());
                  FSTInfo* info = fst_get_info(path);
                  if (info) {
                        //
                        // simple hack:
                        // plugins with no inputs are treated
                        // as software synthesizer
                        //
                        // if (info->wantEvents && info->numOutputs) {
                        if (info->numInputs == 0 && info->numOutputs) {
                              if (debugMsg)
                                    printf("  add vsti synti <%s>\n", fi.fileName().toLatin1().data());
                              synthis.push_back(new VstSynth(&fi));
                              fst_free_info(info);
                              }
                        else if (info->numInputs && info->numOutputs) {
                              if (debugMsg)
                                    printf("  add vst plugin <%s>\n", fi.fileName().toLatin1().data());
                              plugins.push_back(new VstPlugin(&fi, info));
                              }
                        }
                  free(path);
                  }
            }
      }

//---------------------------------------------------------
//   fstSignalHandler
//---------------------------------------------------------

static void fstSignalHandler(int sig, siginfo_t* /*info*/, void* /*context*/)
      {
      fst_error("fst signal handler %d, thread = 0x%x", sig, pthread_self ());
      //if (sig == SIGSEGV || sig == SIGABRT) {
            char*p = 0;
            *p = 0;
//            }
      fatalError("fst signal");
      }

//---------------------------------------------------------
//   initVST
//---------------------------------------------------------

void initVST()
      {
      jfstReserveMem(1000000);

      if (fst_init(fstSignalHandler)) {
            printf("initVST failed\n");
            return;
            }
      char* vstPath = getenv("VST_PATH");
      if (vstPath == 0)
            vstPath = "/usr/lib/vst:/usr/local/lib/vst";

      char* p = vstPath;
      while (*p != '\0') {
            char* pe = p;
            while (*pe != ':' && *pe != '\0')
                  pe++;

            int n = pe - p;
            if (n) {
                  char* buffer = new char[n + 1];
                  strncpy(buffer, p, n);
                  buffer[n] = '\0';
                  scanVstDir(QString(buffer));
                  delete[] buffer;
                  }
            p = pe;
            if (*p == ':')
                  p++;
            }
      }

//---------------------------------------------------------
//   guiVisible
//---------------------------------------------------------

bool VstSynthIF::guiVisible() const
      {
      return _guiVisible;
      }

//---------------------------------------------------------
//   showGui
//---------------------------------------------------------

void VstSynthIF::showGui(bool v)
      {
      if (v == guiVisible())
            return;
      if (v)
            _fst->runEditor();
      else
            _fst->destroyEditor();
      _guiVisible = v;
      }

//---------------------------------------------------------
//   receiveEvent
//---------------------------------------------------------

MidiEvent VstSynthIF::receiveEvent()
      {
      return MidiEvent();
      }

//---------------------------------------------------------
//   hasGui
//---------------------------------------------------------

bool VstSynthIF::hasGui() const
      {
      return _fst->hasGui();
      }

//---------------------------------------------------------
//   incInstances
//---------------------------------------------------------

void VstSynth::incInstances(int val)
      {
      _instances += val;
      if (_instances == 0 && fstHandle) {
            fst_unload(fstHandle);
            fstHandle = 0;
            }
      }

//---------------------------------------------------------
//   init
//---------------------------------------------------------

bool VstSynthIF::init(FSTHandle* h)
      {
      _fst = new FstPlugin();
      _fst->instantiate(h, 0);
      _fst->setSampleRate(AL::sampleRate);
      _fst->setBlockSize(segmentSize);
      _fst->mainsChanged(true);
      _fst->setProgram(0);
      return true;
      }

//---------------------------------------------------------
//   channels
//---------------------------------------------------------

int VstSynthIF::channels() const
      {
      return _fst->numOutputs();
      }

//---------------------------------------------------------
//   createSIF
//---------------------------------------------------------

SynthIF* VstSynth::createSIF(SynthI* s)
      {
      VstSynthIF* sif = new VstSynthIF(s);
      ++_instances;
      const char* path = info.filePath().toLatin1().data();

      if (fstHandle == 0) {
            fstHandle = fst_load(path);
            if (fstHandle == 0) {
                  printf("SynthIF:: cannot load vst plugin %s\n", path);
                  return 0;
                  }
            }
      sif->init(fstHandle);
      return sif;
      }

//---------------------------------------------------------
//   deactivate3
//---------------------------------------------------------

void VstSynthIF::deactivate3()
      {
      if (_fst) {
            delete _fst;
            _fst = 0;
            }
      }

//---------------------------------------------------------
//   ~VstSynthIF
//---------------------------------------------------------

VstSynthIF::~VstSynthIF()
      {
      deactivate3();
      }

//---------------------------------------------------------
//   setParameter
//---------------------------------------------------------

void VstSynthIF::setParameter(int idx, float value)
      {
      _fst->setParameter(idx, value);
      }

//---------------------------------------------------------
//   write
//---------------------------------------------------------

void VstSynthIF::write(Xml& xml) const
      {
      //---------------------------------------------
      // dump current state of synth
      //---------------------------------------------

      int params = _fst->numParameter();
      for (int i = 0; i < params; ++i) {
            float f = _fst->getParameter(i);
            xml.tag("param", f);
            }
      }

//---------------------------------------------------------
//   getData
//---------------------------------------------------------

void VstSynthIF::getData(MidiEventList* el, unsigned pos, int ports, unsigned n, float** buffer)
      {
      unsigned int endPos = pos + n;
      iMidiEvent i = el->begin();
      for (; i != el->end(); ++i) {
            if (i->time() >= endPos)
                  break;
            putEvent(*i);
            }
      el->erase(el->begin(), i);

      int outputs = _fst->numOutputs();
      if (ports < outputs) {
            float* ob[outputs];
            float fp[n * (outputs-ports)];   // dummy output buffer
            for (int i = 0; i < outputs; ++i) {
                  if (i < ports)
                        ob[i] = buffer[i];
                  else
                        ob[i] = fp + n * (i-ports);
                  }
            if (_fst->canReplacing())
                  _fst->processReplacing(0, ob, n);
            else {
                  for (int i = 0; i < outputs; ++i)
                        memset(ob[i], 0, n * sizeof(float));
                  _fst->process(0, ob, n);
                  }
            }
      else {
            if (_fst->canReplacing())
                  _fst->processReplacing(0, buffer, n);
            else {
                  for (int i = 0; i < outputs; ++i)
                        memset(buffer[i], 0, n * sizeof(float));
                  _fst->process(0, buffer, n);
                  }
            }
      }

//---------------------------------------------------------
//   putEvent
//---------------------------------------------------------

bool VstSynthIF::putEvent(const MidiEvent& ev)
      {
      if (midiOutputTrace)
            ev.dump();
      _fst->putEvent(ev);
      return false;
      }

//---------------------------------------------------------
//   VstPlugin
//---------------------------------------------------------

VstPlugin::VstPlugin(const QFileInfo* fi, FSTInfo* i)
   : Plugin(fi)
      {
      info = i;
      fstHandle = 0;
      }

//---------------------------------------------------------
//   VstPlugin
//---------------------------------------------------------

VstPlugin::~VstPlugin()
      {
      fst_free_info(info);
      }

//---------------------------------------------------------
//   createPIF
//---------------------------------------------------------

PluginIF* VstPlugin::createPIF(PluginI* pi)
      {
      VstPluginIF* pif = new VstPluginIF(pi);
      ++_instances;
      const char* path = fi.filePath().toLatin1().data();

      if (fstHandle == 0) {
            fstHandle = fst_load(path);
            if (fstHandle == 0) {
                  printf("SynthIF:: cannot load vst plugin %s\n", path);
                  return 0;
                  }
            }
      pif->init(fstHandle);
      return pif;
      }

//---------------------------------------------------------
//   parameter
//---------------------------------------------------------

int VstPlugin::parameter() const
      {
      return info->numParams;
      }

//---------------------------------------------------------
//   inports
//---------------------------------------------------------

int VstPlugin::inports() const
      {
      return info->numInputs;
      }

//---------------------------------------------------------
//   outports
//---------------------------------------------------------

int VstPlugin::outports() const
      {
      return info->numOutputs;
      }

//---------------------------------------------------------
//   id
//---------------------------------------------------------

unsigned long VstPlugin::id() const
      {
      return info->UniqueID;
      }

//---------------------------------------------------------
//   VstPluginIF
//---------------------------------------------------------

VstPluginIF::VstPluginIF(PluginI* pi)
   : PluginIF(pi)
      {
      _fst = 0;
      _guiVisible = false;
      }

VstPluginIF::~VstPluginIF()
      {
      if (_fst == 0)
            return;
      delete _fst;
      }

//---------------------------------------------------------
//   init
//---------------------------------------------------------

bool VstPluginIF::init(FSTHandle* h)
      {
      _fst = new FstPlugin();
      _fst->instantiate(h, this);
      _fst->setSampleRate(AL::sampleRate);
      _fst->setBlockSize(segmentSize);
      _fst->mainsChanged(true);
      _fst->setProgram(0);
      return true;
      }

//---------------------------------------------------------
//   hasGui
//---------------------------------------------------------

bool VstPluginIF::hasGui() const
      {
      return _fst->hasGui();
      }

//---------------------------------------------------------
//   showGui
//---------------------------------------------------------

void VstPluginIF::showGui(bool v)
      {
      if (v == guiVisible())
            return;
      if (v)
            _fst->runEditor();
      else
            _fst->destroyEditor();
      _guiVisible = v;
      }

//---------------------------------------------------------
//   getParameterName
//---------------------------------------------------------

const char* VstPluginIF::getParameterName(int idx) const
      {
      return _fst->getParameterName(idx);
      }

//---------------------------------------------------------
//   getParameterLabel
//---------------------------------------------------------

const char* VstPluginIF::getParameterLabel(int idx) const
      {
      return _fst->getParameterLabel(idx);
      }

//---------------------------------------------------------
//   getParameterDisplay
//---------------------------------------------------------

const char* VstPluginIF::getParameterDisplay(int idx, float val) const
      {
      return _fst->getParameterDisplay(idx, val);
      }

//---------------------------------------------------------
//   apply
//---------------------------------------------------------

void VstPluginIF::apply(unsigned nframes, float** src, float** dst)
      {
      if (_fst->canReplacing())
            _fst->processReplacing(src, dst, nframes);
      else {
            int n = _fst->numOutputs();

            for (int i = 0; i < n; ++i)
                  memset(dst[i], 0, sizeof(float) * nframes);
            _fst->process(src, dst, nframes);
            }
      }

//---------------------------------------------------------
//   setParam
//---------------------------------------------------------

void VstPluginIF::setParam(int i, double val)
      {
      _fst->setParameter(i, val);
      }

//---------------------------------------------------------
//   param
//---------------------------------------------------------

float VstPluginIF::param(int i) const
      {
      return _fst->getParameter(i);
      }

#else
void initVST() {}
#endif