This repository has been archived by the owner on Feb 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
dssi-vst-server.cpp
1634 lines (1335 loc) · 43.4 KB
/
dssi-vst-server.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
// -*- c-basic-offset: 4 -*-
/*
dssi-vst: a DSSI plugin wrapper for VST effects and instruments
Copyright 2012-2013 Filipe Coelho
Copyright 2010-2011 Kristian Amlie
Copyright 2004-2010 Chris Cannam
*/
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <map>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <sys/un.h>
#include <sys/time.h>
#include <sys/poll.h>
#include <time.h>
#include <unistd.h>
#include <sched.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <jack/jack.h>
#define VST_FORCE_DEPRECATED 0
#include "aeffectx.h"
#include "remotepluginserver.h"
#include "paths.h"
#include "rdwrops.h"
#define APPLICATION_CLASS_NAME "dssi_vst"
#define OLD_PLUGIN_ENTRY_POINT "main"
#define NEW_PLUGIN_ENTRY_POINT "VSTPluginMain"
#if VST_FORCE_DEPRECATED
#define DEPRECATED_VST_SYMBOL(x) __##x##Deprecated
#else
#define DEPRECATED_VST_SYMBOL(x) x
#endif
#define effGetProgramNameIndexed 29
struct Rect {
short top;
short left;
short bottom;
short right;
};
static bool inProcessThread = false;
static HANDLE audioThreadHandle = 0;
static bool exiting = false;
static HWND hWnd = 0;
static double currentSamplePosition = 0.0;
static bool ready = false;
static bool alive = false;
static int bufferSize = 0;
static int sampleRate = 0;
static bool guiVisible = false;
static bool needIdle = false;
static RemotePluginDebugLevel debugLevel = RemotePluginDebugNone;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static jack_client_t* jack_client = 0;
static int jack_process_callback(jack_nframes_t, void*)
{ return 0; }
using namespace std;
class RemoteVSTServer : public RemotePluginServer
{
public:
RemoteVSTServer(std::string fileIdentifiers, AEffect *plugin, std::string fallbackName);
virtual ~RemoteVSTServer();
virtual bool isReady() { return ready; }
virtual std::string getName() { return m_name; }
virtual std::string getMaker() { return m_maker; }
virtual void setBufferSize(int);
virtual void setSampleRate(int);
virtual void reset();
virtual void terminate();
virtual int getInputCount() { return m_plugin->numInputs; }
virtual int getOutputCount() { return m_plugin->numOutputs; }
virtual int getParameterCount() { return m_plugin->numParams; }
virtual std::string getParameterName(int);
virtual void setParameter(int, float);
virtual float getParameter(int);
virtual float getParameterDefault(int);
virtual void getParameters(int, int, float *);
virtual int getProgramCount() { return m_plugin->numPrograms; }
virtual std::string getProgramName(int);
virtual void setCurrentProgram(int);
virtual bool hasMIDIInput() { return m_hasMIDI; }
virtual void sendMIDIData(unsigned char *data,
int *frameOffsets,
int events);
virtual void showGUI(std::string);
virtual void hideGUI();
//Deryabin Andrew: vst chunks support
virtual std::vector<char> getVSTChunk();
virtual bool setVSTChunk(std::vector<char>);
//Deryabin Andrew: vst chunks support: end code
virtual void process(float **inputs, float **outputs);
virtual void setDebugLevel(RemotePluginDebugLevel level) {
debugLevel = level;
}
virtual bool warn(std::string);
void startEdit();
void endEdit();
void monitorEdits();
void scheduleGUINotify(int index, float value);
void notifyGUI(int index, float value);
void checkGUIExited();
void terminateGUIProcess();
private:
AEffect *m_plugin;
std::string m_name;
std::string m_maker;
// These should be referred to from the GUI thread only
std::string m_guiFifoFile;
int m_guiFifoFd;
int m_guiEventsExpected;
struct timeval m_lastGuiComms;
// To be written by the audio thread and read by the GUI thread
#define PARAMETER_CHANGE_COUNT 200
int m_paramChangeIndices[PARAMETER_CHANGE_COUNT];
float m_paramChangeValues[PARAMETER_CHANGE_COUNT];
int m_paramChangeReadIndex;
int m_paramChangeWriteIndex;
enum {
EditNone,
EditStarted,
EditFinished
} m_editLevel;
float *m_defaults;
float *m_values;
bool m_hasMIDI;
};
static RemoteVSTServer *remoteVSTServerInstance = 0;
RemoteVSTServer::RemoteVSTServer(std::string fileIdentifiers,
AEffect *plugin, std::string fallbackName) :
RemotePluginServer(fileIdentifiers),
m_plugin(plugin),
m_name(fallbackName),
m_maker(""),
m_guiFifoFile(""),
m_guiFifoFd(-1),
m_guiEventsExpected(0),
m_paramChangeReadIndex(0),
m_paramChangeWriteIndex(0),
m_editLevel(EditNone)
{
pthread_mutex_lock(&mutex);
if (debugLevel > 0) {
cerr << "dssi-vst-server[1]: opening plugin" << endl;
}
m_plugin->dispatcher(m_plugin, effOpen, 0, 0, NULL, 0);
m_plugin->dispatcher(m_plugin, effMainsChanged, 0, 0, NULL, 0);
m_hasMIDI = false;
if (m_plugin->dispatcher(m_plugin, effGetVstVersion, 0, 0, NULL, 0) < 2) {
if (debugLevel > 0) {
cerr << "dssi-vst-server[1]: plugin is VST 1.x" << endl;
}
} else {
if (debugLevel > 0) {
cerr << "dssi-vst-server[1]: plugin is VST 2.0 or newer" << endl;
}
if ((m_plugin->flags & effFlagsIsSynth)) {
if (debugLevel > 0) {
cerr << "dssi-vst-server[1]: plugin is a synth" << endl;
}
m_hasMIDI = true;
} else {
if (debugLevel > 0) {
cerr << "dssi-vst-server[1]: plugin is not a synth" << endl;
}
if (m_plugin->dispatcher(m_plugin, effCanDo, 0, 0, (void *)"receiveVstMidiEvent", 0) > 0) {
if (debugLevel > 0) {
cerr << "dssi-vst-server[1]: plugin can receive MIDI anyway" << endl;
}
m_hasMIDI = true;
}
}
}
char buffer[65];
buffer[0] = '\0';
m_plugin->dispatcher(m_plugin, effGetEffectName, 0, 0, buffer, 0);
if (debugLevel > 0) {
cerr << "dssi-vst-server[1]: plugin name is \"" << buffer
<< "\"" << endl;
}
if (buffer[0]) m_name = buffer;
buffer[0] = '\0';
m_plugin->dispatcher(m_plugin, effGetVendorString, 0, 0, buffer, 0);
if (debugLevel > 0) {
cerr << "dssi-vst-server[1]: vendor string is \"" << buffer
<< "\"" << endl;
}
if (buffer[0]) m_maker = buffer;
m_plugin->dispatcher(m_plugin, effMainsChanged, 0, 1, NULL, 0);
m_defaults = new float[m_plugin->numParams];
m_values = new float[m_plugin->numParams];
for (int i = 0; i < m_plugin->numParams; ++i) {
m_defaults[i] = m_plugin->getParameter(m_plugin, i);
m_values[i] = m_defaults[i];
}
pthread_mutex_unlock(&mutex);
}
RemoteVSTServer::~RemoteVSTServer()
{
pthread_mutex_lock(&mutex);
if (m_guiFifoFd >= 0) {
try {
writeOpcode(m_guiFifoFd, RemotePluginTerminate);
} catch (...) { }
close(m_guiFifoFd);
}
if (guiVisible) {
ShowWindow(hWnd, SW_HIDE);
UpdateWindow(hWnd);
m_plugin->dispatcher(m_plugin, effEditClose, 0, 0, 0, 0);
guiVisible = false;
}
m_plugin->dispatcher(m_plugin, effMainsChanged, 0, 0, NULL, 0);
m_plugin->dispatcher(m_plugin, effClose, 0, 0, NULL, 0);
delete[] m_defaults;
pthread_mutex_unlock(&mutex);
}
void
RemoteVSTServer::process(float **inputs, float **outputs)
{
if (pthread_mutex_trylock(&mutex)) {
for (int i = 0; i < m_plugin->numOutputs; ++i) {
memset(outputs[i], 0, bufferSize * sizeof(float));
}
currentSamplePosition += bufferSize;
return;
}
inProcessThread = true;
// superclass guarantees setBufferSize will be called before this
m_plugin->processReplacing(m_plugin, inputs, outputs, bufferSize);
currentSamplePosition += bufferSize;
inProcessThread = false;
pthread_mutex_unlock(&mutex);
}
void
RemoteVSTServer::setBufferSize(int sz)
{
pthread_mutex_lock(&mutex);
if (bufferSize != sz) {
m_plugin->dispatcher(m_plugin, effMainsChanged, 0, 0, NULL, 0);
m_plugin->dispatcher(m_plugin, effSetBlockSize, 0, sz, NULL, 0);
m_plugin->dispatcher(m_plugin, effMainsChanged, 0, 1, NULL, 0);
bufferSize = sz;
}
if (debugLevel > 0) {
cerr << "dssi-vst-server[1]: set buffer size to " << sz << endl;
}
pthread_mutex_unlock(&mutex);
}
void
RemoteVSTServer::setSampleRate(int sr)
{
pthread_mutex_lock(&mutex);
if (sampleRate != sr) {
m_plugin->dispatcher(m_plugin, effMainsChanged, 0, 0, NULL, 0);
m_plugin->dispatcher(m_plugin, effSetSampleRate, 0, 0, NULL, (float)sr);
m_plugin->dispatcher(m_plugin, effMainsChanged, 0, 1, NULL, 0);
sampleRate = sr;
}
if (debugLevel > 0) {
cerr << "dssi-vst-server[1]: set sample rate to " << sr << endl;
}
pthread_mutex_unlock(&mutex);
}
void
RemoteVSTServer::reset()
{
pthread_mutex_lock(&mutex);
cerr << "dssi-vst-server[1]: reset" << endl;
m_plugin->dispatcher(m_plugin, effMainsChanged, 0, 0, NULL, 0);
m_plugin->dispatcher(m_plugin, effMainsChanged, 0, 1, NULL, 0);
pthread_mutex_unlock(&mutex);
}
void
RemoteVSTServer::terminate()
{
cerr << "RemoteVSTServer::terminate: setting exiting flag" << endl;
exiting = true;
}
std::string
RemoteVSTServer::getParameterName(int p)
{
char name[24];
m_plugin->dispatcher(m_plugin, effGetParamName, p, 0, name, 0);
return name;
}
void
RemoteVSTServer::setParameter(int p, float v)
{
if (debugLevel > 1) {
cerr << "dssi-vst-server[2]: setParameter (" << p << "," << v << ")" << endl;
}
pthread_mutex_lock(&mutex);
if (debugLevel > 1)
cerr << "RemoteVSTServer::setParameter (" << p << "," << v << "): " << m_guiEventsExpected << " events expected" << endl;
if (m_guiFifoFd < 0) {
m_guiEventsExpected = 0;
}
if (m_guiEventsExpected > 0) {
//!!! should be per-parameter of course!
struct timeval tv;
gettimeofday(&tv, NULL);
if (tv.tv_sec > m_lastGuiComms.tv_sec + 10) {
m_guiEventsExpected = 0;
} else {
--m_guiEventsExpected;
//cerr << "Reduced to " << m_guiEventsExpected << endl;
pthread_mutex_unlock(&mutex);
return;
}
}
pthread_mutex_unlock(&mutex);
m_plugin->setParameter(m_plugin, p, v);
}
float
RemoteVSTServer::getParameter(int p)
{
return m_plugin->getParameter(m_plugin, p);
}
float
RemoteVSTServer::getParameterDefault(int p)
{
return m_defaults[p];
}
void
RemoteVSTServer::getParameters(int p0, int pn, float *v)
{
for (int i = p0; i <= pn; ++i) {
v[i - p0] = m_plugin->getParameter(m_plugin, i);
}
}
std::string
RemoteVSTServer::getProgramName(int p)
{
if (debugLevel > 1) {
cerr << "dssi-vst-server[2]: getProgramName(" << p << ")" << endl;
}
pthread_mutex_lock(&mutex);
char name[24];
if (m_plugin->dispatcher(m_plugin, effGetVstVersion, 0, 0, NULL, 0) < 2) {
long prevProgram =
m_plugin->dispatcher(m_plugin, effGetProgram, 0, 0, NULL, 0);
m_plugin->dispatcher(m_plugin, effSetProgram, 0, p, NULL, 0);
m_plugin->dispatcher(m_plugin, effGetProgramName, p, 0, name, 0);
m_plugin->dispatcher(m_plugin, effSetProgram, 0, prevProgram, NULL, 0);
} else {
m_plugin->dispatcher(m_plugin, effGetProgramNameIndexed, p, 0, name, 0);
}
pthread_mutex_unlock(&mutex);
return name;
}
void
RemoteVSTServer::setCurrentProgram(int p)
{
if (debugLevel > 1) {
cerr << "dssi-vst-server[2]: setCurrentProgram(" << p << ")" << endl;
}
pthread_mutex_lock(&mutex);
m_plugin->dispatcher(m_plugin, effSetProgram, 0, p, 0, 0);
pthread_mutex_unlock(&mutex);
}
void
RemoteVSTServer::sendMIDIData(unsigned char *data, int *frameOffsets, int events)
{
#define MIDI_EVENT_BUFFER_COUNT 1024
static VstMidiEvent vme[MIDI_EVENT_BUFFER_COUNT];
static char evbuf[sizeof(VstMidiEvent *) * MIDI_EVENT_BUFFER_COUNT +
sizeof(VstEvents)];
VstEvents *vstev = (VstEvents *)evbuf;
vstev->reserved = 0;
int ix = 0;
if (events > MIDI_EVENT_BUFFER_COUNT) {
std::cerr << "vstserv: WARNING: " << events << " MIDI events received "
<< "for " << MIDI_EVENT_BUFFER_COUNT << "-event buffer"
<< std::endl;
events = MIDI_EVENT_BUFFER_COUNT;
}
while (ix < events) {
vme[ix].type = kVstMidiType;
vme[ix].byteSize = 24;
vme[ix].deltaFrames = (frameOffsets ? frameOffsets[ix] : 0);
vme[ix].flags = 0;
vme[ix].noteLength = 0;
vme[ix].noteOffset = 0;
vme[ix].detune = 0;
vme[ix].noteOffVelocity = 0;
vme[ix].reserved1 = 0;
vme[ix].reserved2 = 0;
vme[ix].midiData[0] = data[ix*3];
vme[ix].midiData[1] = data[ix*3+1];
vme[ix].midiData[2] = data[ix*3+2];
vme[ix].midiData[3] = 0;
vstev->events[ix] = (VstEvent *)&vme[ix];
if (debugLevel > 1) {
cerr << "dssi-vst-server[2]: MIDI event in: "
<< (int)data[ix*3] << " "
<< (int)data[ix*3+1] << " "
<< (int)data[ix*3+2] << endl;
}
++ix;
}
pthread_mutex_lock(&mutex);
vstev->numEvents = events;
m_plugin->dispatcher(m_plugin, effProcessEvents, 0, 0, vstev, 0);
pthread_mutex_unlock(&mutex);
}
bool
RemoteVSTServer::warn(std::string warning)
{
if (hWnd) MessageBox(hWnd, warning.c_str(), "Error", 0);
return true;
}
void
RemoteVSTServer::showGUI(std::string guiData)
{
if (debugLevel > 0) {
cerr << "RemoteVSTServer::showGUI(" << guiData << "): guiVisible is " << guiVisible << endl;
}
if (guiVisible) return;
const std::string guiTitle = guiData.substr(23, guiData.length());
const std::string guiFifoFile = guiData.erase(23, std::string::npos);
if (guiFifoFile != m_guiFifoFile || m_guiFifoFd < 0) {
if (m_guiFifoFd >= 0) {
close(m_guiFifoFd);
m_guiFifoFd = -1;
}
m_guiFifoFile = guiFifoFile;
if ((m_guiFifoFd = open(m_guiFifoFile.c_str(), O_WRONLY | O_NONBLOCK)) < 0) {
perror(m_guiFifoFile.c_str());
cerr << "WARNING: Failed to open FIFO to GUI manager process" << endl;
pthread_mutex_unlock(&mutex);
return;
}
writeOpcode(m_guiFifoFd, RemotePluginIsReady);
}
m_plugin->dispatcher(m_plugin, effEditOpen, 0, 0, hWnd, 0);
Rect *rect = 0;
m_plugin->dispatcher(m_plugin, effEditGetRect, 0, 0, &rect, 0);
if (!rect) {
cerr << "dssi-vst-server: ERROR: Plugin failed to report window size\n" << endl;
} else {
// Seems we need to provide space in here for the titlebar
// and frame, even though we don't know how big they'll
// be! How crap.
SetWindowPos(hWnd, 0, 0, 0,
rect->right - rect->left + 6,
rect->bottom - rect->top + 25,
SWP_NOACTIVATE | SWP_NOMOVE |
SWP_NOOWNERZORDER | SWP_NOZORDER);
if (debugLevel > 0) {
cerr << "dssi-vst-server[1]: sized window" << endl;
}
SetWindowTextA(hWnd, guiTitle.c_str());
ShowWindow(hWnd, SW_SHOWNORMAL);
UpdateWindow(hWnd);
guiVisible = true;
}
m_paramChangeReadIndex = m_paramChangeWriteIndex;
}
void
RemoteVSTServer::hideGUI()
{
if (!guiVisible) return;
if (m_guiFifoFd >= 0) {
int fd = m_guiFifoFd;
m_guiFifoFd = -1;
close(fd);
}
ShowWindow(hWnd, SW_HIDE);
UpdateWindow(hWnd);
m_plugin->dispatcher(m_plugin, effEditClose, 0, 0, 0, 0);
guiVisible = false;
}
//Deryabin Andrew: vst chunks support
std::vector<char> RemoteVSTServer::getVSTChunk()
{
cerr << "dssi-vst-server: Getting vst chunk from plugin.." << endl;
char * chunkraw = 0;
int len = m_plugin->dispatcher(m_plugin, 23, 0, 0, (void **)&chunkraw, 0);
std::vector<char> chunk;
for(int i = 0; i < len; i++)
{
chunk.push_back(chunkraw [i]);
}
if (len > 0)
{
cerr << "Got " << len << " bytes chunk." << endl;
}
return chunk;
}
bool RemoteVSTServer::setVSTChunk(std::vector<char> chunk)
{
cerr << "dssi-vst-server: Sending vst chunk to plugin. Size=" << chunk.size() << endl;
std::vector<char>::pointer ptr = &chunk [0];
pthread_mutex_lock(&mutex);
m_plugin->dispatcher(m_plugin, 24, 0, chunk.size(), (void *)ptr, 0);
pthread_mutex_unlock(&mutex);
return true;
}
//Deryabin Andrew: vst chunks support: end code
void
RemoteVSTServer::startEdit()
{
m_editLevel = EditStarted;
}
void
RemoteVSTServer::endEdit()
{
m_editLevel = EditFinished;
}
void
RemoteVSTServer::monitorEdits()
{
if (m_editLevel != EditNone) {
if (m_editLevel == EditFinished) m_editLevel = EditNone;
for (int i = 0; i < m_plugin->numParams; ++i) {
float actual = m_plugin->getParameter(m_plugin, i);
if (actual != m_values[i]) {
m_values[i] = actual;
notifyGUI(i, actual);
}
}
}
while (m_paramChangeReadIndex != m_paramChangeWriteIndex) {
int index = m_paramChangeIndices[m_paramChangeReadIndex];
float value = m_paramChangeValues[m_paramChangeReadIndex];
if (value != m_values[index]) {
m_values[index] = value;
notifyGUI(index, value);
}
m_paramChangeReadIndex =
(m_paramChangeReadIndex + 1) % PARAMETER_CHANGE_COUNT;
}
}
void
RemoteVSTServer::scheduleGUINotify(int index, float value)
{
int ni = (m_paramChangeWriteIndex + 1) % PARAMETER_CHANGE_COUNT;
if (ni == m_paramChangeReadIndex) return;
m_paramChangeIndices[m_paramChangeWriteIndex] = index;
m_paramChangeValues[m_paramChangeWriteIndex] = value;
m_paramChangeWriteIndex = ni;
}
void
RemoteVSTServer::notifyGUI(int index, float value)
{
if (m_guiFifoFd >= 0) {
if (debugLevel > 1) {
cerr << "RemoteVSTServer::notifyGUI(" << index << "," << value << "): about to lock" << endl;
}
try {
writeOpcode(m_guiFifoFd, RemotePluginSetParameter);
int i = (int)index;
writeInt(m_guiFifoFd, i);
writeFloat(m_guiFifoFd, value);
gettimeofday(&m_lastGuiComms, NULL);
++m_guiEventsExpected;
} catch (RemotePluginClosedException e) {
hideGUI();
}
if (debugLevel > 1) {
cerr << "wrote (" << index << "," << value << ") to gui (" << m_guiEventsExpected << " events expected now)" << endl;
}
}
}
void
RemoteVSTServer::checkGUIExited()
{
if (m_guiFifoFd >= 0) {
struct pollfd pfd;
pfd.fd = m_guiFifoFd;
pfd.events = POLLHUP;
if (poll(&pfd, 1, 0) != 0) {
m_guiFifoFd = -1;
}
}
}
void
RemoteVSTServer::terminateGUIProcess()
{
if (m_guiFifoFd >= 0) {
writeOpcode(m_guiFifoFd, RemotePluginTerminate);
m_guiFifoFd = -1;
}
}
#if 1 // vestige header
#define kVstTransportChanged 1
#define kVstVersion 2400
struct VstTimeInfo_R {
double samplePos, sampleRate, nanoSeconds, ppqPos, tempo, barStartPos, cycleStartPos, cycleEndPos;
int32_t timeSigNumerator, timeSigDenominator, smpteOffset, smpteFrameRate, samplesToNextClock, flags;
};
intptr_t
hostCallback(AEffect *plugin, int32_t opcode, int32_t index,
intptr_t value, void *ptr, float opt)
#elif VST_2_4_EXTENSIONS
typedef VstTimeInfo VstTimeInfo_R;
VstIntPtr VSTCALLBACK
hostCallback(AEffect *plugin, VstInt32 opcode, VstInt32 index,
VstIntPtr value, void *ptr, float opt)
#else
typedef VstTimeInfo VstTimeInfo_R;
long VSTCALLBACK
hostCallback(AEffect *plugin, long opcode, long index,
long value, void *ptr, float opt)
#endif
{
static VstTimeInfo_R timeInfo;
int rv = 0;
switch (opcode) {
case audioMasterAutomate:
{
/*!!! Automation:
When something changes here, we send it straight to the GUI
via our back channel. The GUI sends it back to the host via
configure; that comes to us; and we somehow need to know to
ignore it. Checking whether it's the same as the existing
param value won't cut it, as we might be changing that
continuously. (Shall we record that we're expecting the
configure call because we just sent to the GUI?)
*/
float v = plugin->getParameter(plugin, index);
if (debugLevel > 1)
cerr << "dssi-vst-server[2]: audioMasterAutomate(" << index << "," << v << ")" << endl;
if (remoteVSTServerInstance)
remoteVSTServerInstance->scheduleGUINotify(index, v);
break;
}
case audioMasterVersion:
if (debugLevel > 1)
cerr << "dssi-vst-server[2]: audioMasterVersion requested" << endl;
rv = kVstVersion;
break;
case audioMasterCurrentId:
if (debugLevel > 1)
cerr << "dssi-vst-server[2]: audioMasterCurrentId requested" << endl;
rv = 0;
break;
case audioMasterIdle:
if (debugLevel > 1)
cerr << "dssi-vst-server[2]: audioMasterIdle requested" << endl;
if (plugin)
plugin->dispatcher(plugin, effEditIdle, 0, 0, 0, 0);
break;
case DEPRECATED_VST_SYMBOL(audioMasterPinConnected):
if (debugLevel > 1)
cerr << "dssi-vst-server[2]: audioMasterPinConnected requested" << endl;
break;
case DEPRECATED_VST_SYMBOL(audioMasterWantMidi):
if (debugLevel > 1) {
cerr << "dssi-vst-server[2]: audioMasterWantMidi requested" << endl;
}
// happy to oblige
rv = 1;
break;
case audioMasterGetTime:
// if (debugLevel > 1)
// cerr << "dssi-vst-server[2]: audioMasterGetTime requested" << endl;
memset(&timeInfo, 0, sizeof(VstTimeInfo_R));
timeInfo.sampleRate = sampleRate;
timeInfo.samplePos = currentSamplePosition;
if (jack_client)
{
jack_position_t jack_pos;
jack_transport_state_t jack_state;
jack_state = jack_transport_query(jack_client, &jack_pos);
if (jack_pos.unique_1 == jack_pos.unique_2)
{
timeInfo.sampleRate = jack_pos.frame_rate;
timeInfo.samplePos = jack_pos.frame;
timeInfo.nanoSeconds = jack_pos.usecs/1000;
timeInfo.flags |= kVstTransportChanged;
timeInfo.flags |= kVstNanosValid;
if (jack_state == JackTransportRolling)
timeInfo.flags |= kVstTransportPlaying;
if (jack_pos.valid & JackPositionBBT)
{
double ppqBar = double(jack_pos.bar - 1) * jack_pos.beats_per_bar;
double ppqBeat = double(jack_pos.beat - 1);
double ppqTick = double(jack_pos.tick) / jack_pos.ticks_per_beat;
// PPQ Pos
timeInfo.ppqPos = ppqBar + ppqBeat + ppqTick;
timeInfo.flags |= kVstPpqPosValid;
// Tempo
timeInfo.tempo = jack_pos.beats_per_minute;
timeInfo.flags |= kVstTempoValid;
// Bars
timeInfo.barStartPos = ppqBar;
timeInfo.flags |= kVstBarsValid;
// Time Signature
timeInfo.timeSigNumerator = jack_pos.beats_per_bar;
timeInfo.timeSigDenominator = jack_pos.beat_type;
timeInfo.flags |= kVstTimeSigValid;
}
}
}
rv = (intptr_t)&timeInfo;
break;
case audioMasterProcessEvents:
if (debugLevel > 1)
cerr << "dssi-vst-server[2]: audioMasterProcessEvents requested" << endl;
break;
case DEPRECATED_VST_SYMBOL(audioMasterSetTime):
if (debugLevel > 1)
cerr << "dssi-vst-server[2]: audioMasterSetTime requested" << endl;
break;
case DEPRECATED_VST_SYMBOL(audioMasterTempoAt):
if (debugLevel > 1)
cerr << "dssi-vst-server[2]: audioMasterTempoAt requested" << endl;
// can't support this, return 120bpm
rv = 120 * 10000;
break;
case DEPRECATED_VST_SYMBOL(audioMasterGetNumAutomatableParameters):
if (debugLevel > 1)
cerr << "dssi-vst-server[2]: audioMasterGetNumAutomatableParameters requested" << endl;
rv = 5000;
break;
case DEPRECATED_VST_SYMBOL(audioMasterGetParameterQuantization):
if (debugLevel > 1)
cerr << "dssi-vst-server[2]: audioMasterGetParameterQuantization requested" << endl;
rv = 1;
break;
case audioMasterIOChanged:
if (debugLevel > 1)
cerr << "dssi-vst-server[2]: audioMasterIOChanged requested" << endl;
cerr << "WARNING: Plugin inputs and/or outputs changed: NOT SUPPORTED" << endl;
break;
case DEPRECATED_VST_SYMBOL(audioMasterNeedIdle):
if (debugLevel > 1) {
cerr << "dssi-vst-server[2]: audioMasterNeedIdle requested" << endl;
}
needIdle=true;
rv = 1;
break;
case audioMasterSizeWindow:
if (debugLevel > 1) {
cerr << "dssi-vst-server[2]: audioMasterSizeWindow requested" << endl;
}
if (hWnd) {
SetWindowPos(hWnd, 0, 0, 0,
index + 6,
value + 25,
SWP_NOACTIVATE | SWP_NOMOVE |
SWP_NOOWNERZORDER | SWP_NOZORDER);
}
rv = 1;
break;
case audioMasterGetSampleRate:
if (debugLevel > 1)
cerr << "dssi-vst-server[2]: audioMasterGetSampleRate requested" << endl;
if (!sampleRate) {
cerr << "WARNING: Sample rate requested but not yet set" << endl;
}
plugin->dispatcher(plugin, effSetSampleRate,
0, 0, NULL, (float)sampleRate);
rv = sampleRate;
break;
case audioMasterGetBlockSize:
if (debugLevel > 1)
cerr << "dssi-vst-server[2]: audioMasterGetBlockSize requested" << endl;
if (!bufferSize) {
cerr << "WARNING: Buffer size requested but not yet set" << endl;
}
plugin->dispatcher(plugin, effSetBlockSize,
0, bufferSize, NULL, 0);
rv = bufferSize;
break;
case audioMasterGetInputLatency:
if (debugLevel > 1)
cerr << "dssi-vst-server[2]: audioMasterGetInputLatency requested" << endl;
break;
case audioMasterGetOutputLatency:
if (debugLevel > 1)
cerr << "dssi-vst-server[2]: audioMasterGetOutputLatency requested" << endl;
break;
case DEPRECATED_VST_SYMBOL(audioMasterGetPreviousPlug):
if (debugLevel > 1)
cerr << "dssi-vst-server[2]: audioMasterGetPreviousPlug requested" << endl;
break;
case DEPRECATED_VST_SYMBOL(audioMasterGetNextPlug):
if (debugLevel > 1)
cerr << "dssi-vst-server[2]: audioMasterGetNextPlug requested" << endl;
break;
case DEPRECATED_VST_SYMBOL(audioMasterWillReplaceOrAccumulate):
if (debugLevel > 1)
cerr << "dssi-vst-server[2]: audioMasterWillReplaceOrAccumulate requested" << endl;
// 0 -> unsupported, 1 -> replace, 2 -> accumulate
rv = 1;
break;
case audioMasterGetCurrentProcessLevel:
if (debugLevel > 1) {
cerr << "dssi-vst-server[2]: audioMasterGetCurrentProcessLevel requested (level is " << (inProcessThread ? 2 : 1) << ")" << endl;
}
// 0 -> unsupported, 1 -> gui, 2 -> process, 3 -> midi/timer, 4 -> offline
if (inProcessThread) rv = 2;
else rv = 1;
break;
case audioMasterGetAutomationState:
if (debugLevel > 1)
cerr << "dssi-vst-server[2]: audioMasterGetAutomationState requested" << endl;
rv = 4; // read/write
break;
case audioMasterOfflineStart: