-
Notifications
You must be signed in to change notification settings - Fork 13
/
VSTCollection.cpp
1031 lines (884 loc) · 28.3 KB
/
VSTCollection.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
/*==================================================================================================
Module Name: VSTCollection.cpp
General Description: VSTCollection class implementation. This class represents abstract element
which behaves as a black box having input stream, output stream and
set of parameters/settings which can be manipulated by client app.
Changing of available characteristics of the plugin instance will
influence output stream.
====================================================================================================
Revision History:
Modification
Author Date Major Changes
---------------------- ------------ -------------
Holy Spirit 07/23/2008 Initial version
==================================================================================================*/
//=================================================================================================
// Include Section
//=================================================================================================
#include "VSTCollection.h"
#include "awful_effects.h"
#include "awful_logger.h"
//=================================================================================================
// Global/Static Variable Section
//=================================================================================================
//=================================================================================================
// Local functions declaration
//=================================================================================================
//=================================================================================================
// CChaoticVSTHost Class Implementation
//=================================================================================================
CChaoticVSTHost::CChaoticVSTHost()
{
lBlockSize = gBuffLen;
vstTimeInfo.samplePos = 0.0;
vstTimeInfo.sampleRate = fSampleRate;
vstTimeInfo.nanoSeconds = 0.0;
vstTimeInfo.ppqPos = 0.0;
vstTimeInfo.tempo = beats_per_minute;
vstTimeInfo.barStartPos = 0.0;
vstTimeInfo.cycleStartPos = 0.0;
vstTimeInfo.cycleEndPos = 0.0;
vstTimeInfo.timeSigNumerator = 4;
vstTimeInfo.timeSigDenominator = 4;
vstTimeInfo.smpteOffset = 0;
vstTimeInfo.smpteFrameRate = 1;
vstTimeInfo.samplesToNextClock = 0;
vstTimeInfo.flags = kVstTempoValid | kVstTimeSigValid;
}
void CChaoticVSTHost::SetSampleRate(float fSampleRate)
{
this->fSampleRate = fSampleRate;
this->vstTimeInfo.sampleRate = fSampleRate;
}
void CChaoticVSTHost::SetTempo(float fBPM)
{
this->vstTimeInfo.tempo = fBPM;
}
bool CChaoticVSTHost::OnSetParameterAutomated(int nEffect,long index,float value)
{
CChaoticEffect *pEffect = (CChaoticEffect*)this->pHost->GetAt(nEffect);
if (pEffect)
{
if (pEffect->owner != NULL)
{
((VSTEffect*)(pEffect->owner))->OnSetParameterAutomated(nEffect, index, value);
}
}
return true;
//return CVSTHost::OnSetParameterAutomated(nEffect, index, value);
}
bool CChaoticVSTHost::OnUpdateDisplay(int nEffect)
{
CChaoticEffect *pEffect = (CChaoticEffect*)this->pHost->GetAt(nEffect);
if (pEffect)
{
if (pEffect->owner != NULL)
{
((VSTEffect*)(pEffect->owner))->OnUpdateDisplay();
}
}
return true;
}
long CChaoticVSTHost::OnIdle(int nEffect)
{
#if (defined (USE_WIN32))
MSG msg;
while ((::PeekMessage(&msg, NULL, WM_TIMER, WM_TIMER, PM_REMOVE)) ||
(::PeekMessage(&msg, NULL, WM_PAINT, WM_PAINT, PM_REMOVE)) ||
(::PeekMessage(&msg, NULL, WM_NCPAINT, WM_NCPAINT, PM_REMOVE)))
{
// if (!::PreTranslateMessage(&msg))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
}
#elif (defined (USE_LINUX))
//TODO: Implement this for linux
#elif (defined (USE_MAC))
//TODO: Implement this for MAC
#endif
return 0;
}
bool CChaoticVSTHost::OnCanDo(const char *ptr)
{
if (!strcmp(ptr, "supplyIdle"))
return true;
return CVSTHost::OnCanDo(ptr);
}
/*****************************************************************************/
/* OnAudioMasterCallback : redefine the callback a bit */
/*****************************************************************************/
long CChaoticVSTHost::OnAudioMasterCallback
(
int nEffect,
long opcode,
long index,
long value,
void *ptr,
float opt
)
{
return CVSTHost::OnAudioMasterCallback(nEffect, opcode, index, value, ptr, opt);
}
//=================================================================================================
// VST Plugin Class Implementation
//=================================================================================================
CVSTPlugin::CVSTPlugin(Object * owner, int index, CChaoticVSTHost* pHost, void* ParentWindow)
{
ERect *pRC = NULL;
this->eff_index = index;
this->pHost = pHost;
this->ParentHWND = ParentWindow;
// this->type = EffType_VSTPlugin;
this->isReplacing = true;
this->hasEditor = false;
this->isWindowActive = false;
this->isGenerator = false;
this->pWnd = NULL;
this->inBufs = NULL;
this->outBufs = NULL;
this->nAllocatedInbufs = 0;
this->nAllocatedOutbufs = 0;
this->pEffect = (CChaoticEffect*)this->pHost->GetAt(this->eff_index);
this->pEffect->owner = owner;
this->pEffect->EffEditGetRect(&pRC);
if (pRC)
{
this->hasEditor = true;
// no need for releaseing of prc since it points to a memory allocated by plugin
}
this->usesChunks = this->pEffect->EffUsesChunks();
int i, j; /* loop counter */
if (this->pEffect->pEffect->numInputs) /* allocate input pointers */
{
//We need to allocate input and output buffer arrays on plugin initialization
this->inBufs = new float *[this->pEffect->pEffect->numInputs];
for (i = 0; i < this->pEffect->pEffect->numInputs; ++i)
{
inBufs[i] = new float[24000];
for (j = 0; j < 24000; ++j)
{
inBufs[i][j] = 0;
}
}
long catg = this->pEffect->EffGetPlugCategory();
if (catg == kPlugCategGenerator || catg == kPlugCategSynth)
{
this->isGenerator = true;
}
}
else
{
this->isGenerator = true;
}
nAllocatedInbufs = this->pEffect->pEffect->numInputs;
if (this->pEffect->pEffect->numOutputs) /* allocate output pointers */
{
int nAlloc;
if (this->pEffect->pEffect->numOutputs < 2)
nAlloc = 2;
else
nAlloc = this->pEffect->pEffect->numOutputs;
outBufs = new float *[nAlloc];
/* and the buffers pointed to */
for (i = 0; i < this->pEffect->pEffect->numOutputs; i++)
{
// for this sample project, I've assumed a maximum buffer size
// of 1/4 second at 96KHz - presumably, this is MUCH too much
// and should be tweaked to more reasonable values, since it
// requires a machine with lots of main memory this way...
// user configurability would be nice, of course.
outBufs[i] = new float[24000];
for (j = 0; j < 24000; j++)
outBufs[i][j] = 0.f;
}
for (; i < nAlloc; ++i) /* if less than 2, fill up */
outBufs[i] = outBufs[0]; /* by replicating buffer 0 pointer */
nAllocatedOutbufs = nAlloc;
}
}
CVSTPlugin::~CVSTPlugin()
{
int i;
WaitForSingleObject(this->hMutex,INFINITE);
if (this->inBufs != NULL)
{
for (i = 0; i < this->nAllocatedInbufs; ++i)
{
if(this->inBufs[i] != NULL)
{
delete((void*)this->inBufs[i]);
this->inBufs[i] = NULL;
}
}
delete((void *) this->inBufs);
this->inBufs = NULL;
}
for (i = 0; i < this->pEffect->pEffect->numOutputs; ++i)
{
if (this->outBufs[i] != NULL)
{
delete((void*)this->outBufs[i]);
this->outBufs[i] = NULL;
}
}
for (; i < this->nAllocatedOutbufs; ++i)
{
this->outBufs[i] = NULL;
}
delete((void *) this->outBufs);
this->outBufs = NULL;
this->pHost->RemoveAt(this->eff_index);
//if (this->pWnd != NULL)
//{
// delete this->pWnd;
//}
ReleaseMutex(this->hMutex);
}
bool CVSTPlugin::ShowEditor()
{
bool ret_val = false;
if (this->hasEditor == true)
{
if (this->isWindowActive == true)
{
this->pWnd->ShowWindow();
this->pWnd->UpdateWindow();
this->pWnd->SetFocus();
}
else
{
//if (this->pWnd != NULL)
//{
// delete (this->pWnd);
//}
if (this->pWnd == NULL)
{
#ifdef USE_JUCE
this->pWnd = new PluginEditWindow(this->ParentHWND, this);
#else
this->pWnd = new CVSTEffWnd(this->pEffect, this->ParentHWND, this);
#endif
}
this->pWnd->ShowWindow();
this->pWnd->UpdateWindow();
this->pWnd->SetFocus();
this->isWindowActive = true;
}
ret_val = true;
}
return ret_val;
}
bool CVSTPlugin::CloseEditor()
{
this->pEffect->EffEditClose();
if (this->pWnd != NULL)
{
this->pWnd->Close();
}
// don't delete it to preserve coordinates of the window
//delete (this->pWnd);
//this->pWnd = NULL;
this->isWindowActive = false;
return true;
}
void CVSTPlugin::Open()
{
this->pEffect->EffOpen();
}
void CVSTPlugin::SetSampleRate(float SampleRate)
{
this->pEffect->EffStopProcess();
this->pEffect->EffSuspend();
this->pEffect->EffSetSampleRate(SampleRate);
this->pEffect->EffResume();
this->pEffect->EffStartProcess();
}
void CVSTPlugin::Close()
{
if (this->pEffect != NULL)
{
this->pEffect->EffClose();
}
}
void CVSTPlugin::Idle()
{
//WaitForSingleObject(this->hMutex,INFINITE);
this->pEffect->EffIdle();
//ReleaseMutex(this->hMutex);
}
void CVSTPlugin::EditIdle()
{
//WaitForSingleObject(this->hMutex,INFINITE);
this->pEffect->EffEditIdle();
//ReleaseMutex(this->hMutex);
}
/*==================================================================================================
FUNCTION: ProcessData
DESCRIPTION: This is a wrapper for standart VST ProcessData function.
input and outpud flows passed in has the following format:
frame[0], frame[1],...,frame[buff_size-1]
Where frame is: float(L), float(R) pair.
Currently process function supports only mono and stereo data to in and out.
All extra channels will be ignored or filled in by duplication of first two channels.
ARGUMENTS PASSED:
in_buff - pointer to the data to be processed (in internal program format)
out_buff - pointer to the processed data (in internal program format)
buff_size - num of data frames (pair of L & R channel value) in incoming stream buffer
RETURN VALUE:
PRE-CONDITIONS:
None.
POST-CONDITIONS:
None.
==================================================================================================*/
void CVSTPlugin::ProcessData(float* in_buff, float* out_buff, int buff_size)
{
int i,j;
WaitForSingleObject(this->hMutex,INFINITE);
//If effect has only one input then mix both L and R to one mono channel
if (in_buff != NULL)
{
if (this->nAllocatedInbufs == 1)
{
for (j = 0; j < buff_size; ++j)
{
this->inBufs[0][j] = (in_buff[j*2] + in_buff[j*2+1])/2;
}
}
// if effect has more than one input
else if (this->nAllocatedInbufs > 1)
{
for (j = 0; j < buff_size; ++j)
{
//fill in only first two channels
this->inBufs[0][j] = in_buff[j*2];
this->inBufs[1][j] = in_buff[j*2+1];
}
//all extra inputs will be duplicated to firts input (we don't support them)
for (i = 2; i < this->nAllocatedInbufs; ++i)
{
this->inBufs[i] = this->inBufs[0];
}
}
}
//sanity action: just clear output buffer
memset(out_buff, 0, (sizeof(float)*2)*buff_size);
//Actually, plugins can support two data processing methods:
// First is Process - out += process(in)
// Second is ProcessReplacing - out = process(in)
if ((this->pEffect->pEffect->flags & effFlagsCanDoubleReplacing)&& (this->isReplacing == true))
{
/* this->pEffect->EffProcessDoubleReplacing(
(double *)this->inBufs,
(double *)this->outBufs,
buff_size);
*/
}
else if ((this->pEffect->pEffect->flags & effFlagsCanReplacing) && (this->isReplacing == true))
{
this->pEffect->EffProcessReplacing(this->inBufs, this->outBufs, buff_size);
}
else
{
this->pEffect->EffProcess(this->inBufs, this->outBufs, buff_size);
}
for (i = 0; i < buff_size; ++i)
{
//lets duplicate output if effect has only one
if (this->nAllocatedOutbufs == 1)
{
out_buff[i*2] = out_buff[i*2+1] = this->outBufs[0][i];
}
else if (this->nAllocatedOutbufs > 1)//effect has more than one buffer
{
//use only first two channels and ignore all other if any
out_buff[i*2] = this->outBufs[0][i];
out_buff[i*2+1] = this->outBufs[1][i];
}
}
ReleaseMutex(this->hMutex);
}
void CVSTPlugin::SetParam(long index, float Value)
{
this->pEffect->EffSetParameter(index, Value);
}
float CVSTPlugin::GetParam(long index)
{
return (this->pEffect->EffGetParameter(index));
}
unsigned int CVSTPlugin::GetNumParams()
{
return (this->pEffect->pEffect->numParams);
}
void CVSTPlugin::GetDisplayValue(long index, char** ppValDisp)
{
if (ppValDisp != NULL)
{
if (*ppValDisp != NULL)
{
free(*ppValDisp);
}
*ppValDisp = (char*)malloc(sizeof(char) * VST_MAX_PARAM_VALUE_LENGTH);
memset(*ppValDisp, 0, VST_MAX_PARAM_VALUE_LENGTH);
this->pEffect->EffGetParamDisplay(index, *ppValDisp);
}
}
void CVSTPlugin::GetParamLabel(long index, char **ppLabel)
{
if (ppLabel != NULL)
{
if (*ppLabel != NULL)
{
free(*ppLabel);
}
*ppLabel = (char*)malloc(sizeof(char) * VST_MAX_PARAM_LABEL_LENGTH);
memset(*ppLabel, 0, VST_MAX_PARAM_LABEL_LENGTH);
this->pEffect->EffGetParamLabel(index, *ppLabel);
}
}
void CVSTPlugin::GetParamName(long index, char** ppName)
{
if (ppName != NULL)
{
if (*ppName != NULL)
{
free(*ppName);
}
*ppName = (char*) malloc(sizeof(char) * VST_MAX_NAME_LENGTH);
this->pEffect->EffGetParamName(index, *ppName);
}
}
/* If length is 0 it means we must return whole name without extension */
void CVSTPlugin::GetDisplayName(char *name, unsigned int length)
{
char szBuf[256]="";
size_t len = 0;
WaitForSingleObject(this->hMutex,INFINITE);
this->pEffect->EffGetProductString(szBuf);
if (strlen(szBuf) == 0)
{
size_t i = strlen(this->pEffect->sName);
unsigned int j = 0;
bool dots = false;
/* Go backward till find a dot in file name */
while (this->pEffect->sName[i--] != '.');
/* Go backward until reach the first "\\" */
while (this->pEffect->sName[i--] != '\\')
{
++len; // Here we calc length of file name (without extension)
}
i+=2;
if ((length != 0) && (len > length))
{
len = length - 3; // reserve space for three dotes in the end
dots = true;
}
j = 0;
while ((len--)!= 0)
{
name[j++] = this->pEffect->sName[i++];
}
if (dots == true)
{
strcat(name, "...");
}
}
else
{
len = strlen(szBuf);
if (length != 0)
{
len = (length > strlen(szBuf) ? strlen(szBuf) : length);
}
strncpy(name, szBuf, len);
}
ReleaseMutex(this->hMutex);
}
long CVSTPlugin::GetNumPresets()
{
return (this->pEffect->pEffect->numPrograms);
}
void CVSTPlugin::GetProgramName(char *pName)
{
this->pEffect->EffGetProgramName(pName);
}
long CVSTPlugin::GetProgram()
{
return this->pEffect->EffGetProgram();
}
void CVSTPlugin::SetProgram(long index)
{
void * pChunk = NULL;
this->pEffect->EffSetProgram(index);
this->pEffect->EffGetChunk(&pChunk, true);
if ((this->pWnd != NULL) && (this->isWindowActive == true))
{
this->pWnd->SetTitle();
}
}
void CVSTPlugin::ProcessEvents(VstEvents* pEvents)
{
this->pEffect->EffProcessEvents(pEvents);
}
void CVSTPlugin::SetBPM(float bpm)
{
//CChaoticEffect* pEffect = this->pEffect;
}
void CVSTPlugin::SetBufferSize(unsigned int uiBufferSize)
{
CChaoticEffect* pEffect = this->pEffect;
pEffect->EffStopProcess();
pEffect->EffSuspend();
pEffect->EffSetBlockSize(uiBufferSize);
pEffect->EffResume();
pEffect->EffStartProcess();
}
void CVSTPlugin::GetErrorText()
{
CChaoticEffect* pEffect = this->pEffect;
char ptrtext[256];
pEffect->EffGetErrorText(ptrtext);
}
void CVSTPlugin::TurnOffProcessing()
{
//That's strange - VST SDK says that Processing should be stopped
// However, I noticed that it causes FX's and synths to keep their processing state
// So, when you restart playback for example, you can hear that synthesis continues but not starts from beginning
//pEffect->EffStopProcess();
pEffect->EffSuspend();
}
void CVSTPlugin::TurnOnProcessing()
{
pEffect->EffResume();
//pEffect->EffStartProcess();
}
//When build with JUCE, we use juce's windows
#ifndef USE_JUCE
//=================================================================================================
// VST Effect Window Class Implementation
//=================================================================================================
CVSTEffWnd::CVSTEffWnd(CChaoticEffect* pEffect, void* ParentWnd, CVSTPlugin* pPlug)
{
ERect *prc = NULL;
ERect myRC = {0};
long lResult = 0;
this->pPlugin = pPlug;
this->pEffect = pEffect;
this->ParentHWND = ParentWnd;
this->height = 0;
this->width = 0;
lResult = this->pEffect->EffEditGetRect(&prc);
if (prc)
{
myRC = *prc;
myRC.bottom += GetSystemMetrics(SM_CYCAPTION) + (2 * GetSystemMetrics(SM_CYBORDER));
myRC.right += (2 * GetSystemMetrics(SM_CXBORDER));
this->width = myRC.right - myRC.left;
this->height = myRC.bottom - myRC.top;
this->hWndHandle = CreateWindowEx(WS_EX_DLGMODALFRAME | WS_EX_DLGMODALFRAME |\
WS_EX_CONTROLPARENT,
"_VSTEditor",
"Plugin",
//WS_CHILD |
WS_CLIPSIBLINGS |\
WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
myRC.left, myRC.top,
myRC.right - myRC.left, myRC.bottom - myRC.top,
this->ParentHWND,
NULL,
NULL,
NULL);
SetWindowLongPtr(this->hWndHandle, GWL_USERDATA, (LONG_PTR)this);
this->SetTitle();
}
}
void CVSTEffWnd::SetTitle()
{
if (this->pEffect != NULL)
{
char szBuf[256] = "";
unsigned int len = 0;
/* if V2 plugin */
this->pEffect->EffGetProductString(szBuf);
if (strlen(szBuf) == 0)
{
unsigned long i = strlen(this->pEffect->sName);
unsigned int j = 0;
while (this->pEffect->sName[i--] != '\\' );
i += 2;
while (i < strlen(this->pEffect->sName))
{
szBuf[j++] = this->pEffect->sName[i++];
}
szBuf[j] = ' ';
len = j;
}
char szProg[256] = {0};
this->pEffect->EffGetProgramName(szProg);
if ( (len < 255) && ((255 - len) >= strlen(szProg) + 3))
{
strcat(szBuf," - ");
strcat(szBuf, szProg);
}
//::SetWindowText(this->hWnd, szBuf);
}
}
//=================================================================================================
// WND PROC used by VSt Edit window class
//Description: Static members cannot have pointer "this" but we need it in order to properly update
// isWindowActive. This flag is used to prevent multiple Edit windows for one plugin.
// Every window has special field for various user data to store. So, we can save
// pointer to this in this location during window creation and further extract it.
//=================================================================================================
LRESULT CALLBACK CVSTEffWnd::VSTWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
HWND ParentHWND;
CVSTEffWnd* pThis; // Fake pointer to this
//Let's extract previously stored "this" pointer
pThis = (CVSTEffWnd*) GetWindowLongPtr(hWnd, GWL_USERDATA);
ParentHWND = GetParent(hWnd);
switch (message) // ��� ���������
{
case WM_CREATE:
// InvalidateRect(hWnd, NULL, false);
SetTimer(hWnd, IDLE_TIMER, IDLE_TIMER_INTERVAL, NULL);
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
EndPaint(hWnd, &ps);
break;
// case WM_DESTROY:
case WM_CLOSE:
KillTimer(hWnd, IDLE_TIMER);
pThis->pEffect->EffEditClose();
pThis->pPlugin->isWindowActive = false;
//return (DefWindowProc(hWnd, message, wParam, lParam));
SendMessage((HWND)pThis->ParentHWND, WM_VST_PLUGEDITOR_CLOSED, 0, (LPARAM)(pThis->pPlugin));
DestroyWindow(hWnd);
break;
// case WM_MOUSEWHEEL:
// break;
case WM_CHAR:
case WM_KEYDOWN:
case WM_KEYUP:
SendMessage(ParentHWND, message,wParam,lParam);
break;
case WM_SIZE:
/* Need this, it enables parent window when eff edit is minimized */
if (wParam == SIZE_MINIMIZED)
{
::ShowWindow(ParentHWND, SW_SHOW);
}
break;
case WM_SHOWWINDOW:
{
ERect *prc = NULL;
ERect myRC = {0};
if(pThis != NULL)
{
pThis->pEffect->EffEditGetRect(&prc);
if (prc)
{
RECT rgn = {0};
unsigned int x_pos, y_pos;
::GetWindowRect((HWND)pThis->ParentHWND, &rgn);
myRC = *prc;
myRC.bottom += GetSystemMetrics(SM_CYCAPTION) + (2 * GetSystemMetrics(SM_CYBORDER));
myRC.right += (2 * GetSystemMetrics(SM_CXBORDER));
x_pos = ((rgn.right - rgn.left) - (myRC.right - myRC.left))/2;
y_pos = ((rgn.bottom - rgn.top) - (myRC.bottom - myRC.top))/3;
::SetWindowPos(hWnd, HWND_TOPMOST, x_pos,y_pos, myRC.right - myRC.left, myRC.bottom - myRC.top,
SWP_NOACTIVATE | SWP_NOZORDER);
}
}
}
break;
case WM_TIMER:
{
switch(wParam)
{
case IDLE_TIMER:
{
pThis->pPlugin->Idle();
pThis->pPlugin->EditIdle();
}
break;
}
break;
}
default:
return (DefWindowProc(hWnd, message, wParam, lParam));
}
return (0);
}
#endif // USE_JUCE
//=================================================================================================
// VST Collection Class Implementation
//=================================================================================================
VSTCollection::VSTCollection(void* MainWindowHandle)
{
this->pMainHost = new CChaoticVSTHost();
this->pMainHost->SetBlockSize(gBuffLen);
this->pMainHost->SetSampleRate(fSampleRate);
this->pMainHost->SetTempo(beats_per_minute);
this->ParentHWND = MainWindowHandle;
this->hMutex = CreateMutex(NULL, FALSE, NULL);
#ifdef USE_WIN32
#ifndef USE_JUCE
// ����� VST-����, ������� ����� �������������� ��� ��������� ���������� � VST ������� ��������
WNDCLASS vst_wc;
//��������� ���� ���������
vst_wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
vst_wc.lpfnWndProc = (WNDPROC) (CVSTEffWnd::VSTWndProc);
vst_wc.cbClsExtra = 0;
vst_wc.cbWndExtra = 0;
vst_wc.hInstance = NULL;
vst_wc.hIcon = NULL;
vst_wc.hCursor = NULL;
vst_wc.hbrBackground = NULL;
vst_wc.lpszMenuName = NULL;
vst_wc.lpszClassName = "_VSTEditor";
//������������ ����� �����.
if(!RegisterClass(&vst_wc))
{
MessageBox(0,"Failed To Register The Window Class.Damn it! Fuck it all!!!",
"Error",MB_OK|MB_ICONERROR);
}
#endif
#endif
}
VSTCollection::~VSTCollection()
{
delete this->pMainHost;
ReleaseMutex(this->hMutex);
}
void VSTCollection::AcquireSema()
{
WaitForSingleObject(this->hMutex,INFINITE);
}
void VSTCollection::ReleaseSema()
{
ReleaseMutex(this->hMutex);
}
CVSTPlugin* VSTCollection::LoadPlugin(Object* owner, char* path)
{
int index = 0;
CVSTPlugin *pPlugin = NULL;
// OPENFILENAME ofn; // common dialog box structure
char szFile[260]; // buffer for file name
if (path == NULL)
{
String strPlugDir(szWorkingDirectory);
strPlugDir += LOCAL_PLUGIN_FOLDER;
juce::File defDir(strPlugDir);
//Let's show a dialog and allow user to choose a directory
FileChooser *pFileSelector = new FileChooser(T("Open a file"),
defDir, T("*.dll;*.DLL"), true);
if (true == pFileSelector->browseForFileToOpen())
{
juce::File vstFile = pFileSelector->getResult();
vstFile.getFullPathName().copyToBuffer(szFile, 260 - 1);
index = pMainHost->LoadPlugin(szFile);
if (index != -1)
{
pPlugin = new CVSTPlugin(owner, index, this->pMainHost, this->ParentHWND);
if (NULL == pPlugin)
{
MessageBox(0,"Internal error occured on plugin loading.",
"Internal error",MB_OK|MB_ICONERROR);
}
else
{
pPlugin->hMutex = this->hMutex;
}
}
else
{
AlertWindow::showMessageBox(AlertWindow::InfoIcon,
T(""),
T("Can't load this dll as a plugin."),
T("OK"));
}
delete pFileSelector;
}
}
else
{
index = pMainHost->LoadPlugin(path);
if (index != -1)
{
pPlugin = new CVSTPlugin(owner, index, this->pMainHost, this->ParentHWND);
if (NULL == pPlugin)
{
MessageBox(0,"Internal error occured on plugin loading.",
"Internal error",MB_OK|MB_ICONERROR);
}
else
{
pPlugin->hMutex = this->hMutex;
}
}
else
{
/*
AlertWindow::showMessageBox(AlertWindow::InfoIcon,
T(""),
T("Can't load this dll as a plugin."),
T("OK"));
*/
}
}
return pPlugin;
}
void VSTCollection::RemovePlugin(CVSTPlugin *pPlug)
{
if (pPlug != NULL)
{
if (pPlug->isWindowActive == true)
{
pPlug->CloseEditor();
}
// pPlug->Close();
delete pPlug;
pPlug = NULL;
}
}
bool VSTCollection::CheckPlugin(char *path, bool *isGenerator, char* name)
{
bool ret_val = false;
long index = 0;
index = this->pMainHost->LoadPlugin(path);
if (index != -1)
{
CEffect *pEff = this->pMainHost->GetAt(index);
memset(name, 0, MAX_NAME_STRING * sizeof(char));