aboutsummaryrefslogtreecommitdiff
path: root/externals/grill/vst/src/VstHost.cpp
blob: 75cd3d35cf4842464399faa33b4c61ef3177e3df (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
/* 
vst~ - VST plugin object for PD 
based on the work of Jarno Seppänen and Mark Williamson

Copyright (c)2003-2004 Thomas Grill (xovo@gmx.net)
For information on usage and redistribution, and for a DISCLAIMER OF ALL
WARRANTIES, see the file, "license.txt," in this distribution.  
*/

#include "Editor.h"
#include "VstHost.h"
#include "vst\aeffectx.h"

using namespace std;

static VstTimeInfo _timeInfo;

typedef AEffect *(*PVSTMAIN)(audioMasterCallback audioMaster);


VSTPlugin::VSTPlugin():
    h_dll(NULL),hwnd(NULL),_pEffect(NULL),
    posx(0),posy(0),
	_midichannel(0),queue_size(0)
{}

VSTPlugin::~VSTPlugin()
{
	Free();				// Call free
}
 
int VSTPlugin::Instance(const char *dllname)
{
	h_dll = LoadLibrary(dllname);
	if(!h_dll)
		return VSTINSTANCE_ERR_NO_VALID_FILE;

	PVSTMAIN main = (PVSTMAIN)GetProcAddress(h_dll,"main");
	if(!main) {	
		FreeLibrary(h_dll);
		_pEffect = NULL;
		return VSTINSTANCE_ERR_NO_VST_PLUGIN;
	}

	//This calls the "main" function and receives the pointer to the AEffect structure.
	_pEffect = main((audioMasterCallback)Master);
	
	if(!_pEffect || _pEffect->magic != kEffectMagic) {
		post("VST plugin : Unable to create effect");

	    _pEffect = NULL;
		FreeLibrary(h_dll); h_dll = NULL;
	    return VSTINSTANCE_ERR_REJECTED;
    }
	
	//init plugin 
	_pEffect->user = this;
    FLEXT_ASSERT(Dispatch( effOpen ));
//	Dispatch( effMainsChanged,  0, 1);

	if (!Dispatch( effGetProductString, 0, 0, &_sProductName, 0.0f)) {
		string str1(dllname);
		string str2 = str1.substr(str1.rfind('\\')+1);
		int snip = str2.find('.');
        if( snip != string::npos )
			str1 = str2.substr(0,snip);
		else
			str1 = str2;
		strcpy(_sProductName,str1.c_str());
	}
	
	if(!Dispatch( effGetVendorString, 0, 0, &_sVendorName, 0.0f))
		strcpy(_sVendorName, "Unknown vendor");

	_sDllName = dllname;
	
	return VSTINSTANCE_NO_ERROR;
}


/*
void VSTPlugin::Create(VSTPlugin *plug)
{
	h_dll = plug->h_dll;
	_pEffect = plug->_pEffect;
	_pEffect->user = this;

	Dispatch( effMainsChanged,  0, 1);
//	strcpy(_editName,plug->_editName); On current implementation, this replaces the right one. 
	strcpy(_sProductName,plug->_sProductName);
	strcpy(_sVendorName,plug->_sVendorName);
	
	_sDllName = new char[strlen(plug->_sDllName)+1];
	strcpy(_sDllName,plug->_sDllName);

	_isSynth=plug->_isSynth;
	_version=plug->_version;

	plug->instantiated=false;	// We are "stoling" the plugin from the "plug" object so this
								// is just a "trick" so that when destructing the "plug", it
								// doesn't unload the Dll.
	instantiated=true;
}
*/

void VSTPlugin::Free() // Called also in destruction
{
	if(Is()) {
        if(IsEdited()) StopEditor(this);

        // shut down plugin
		Dispatch(effMainsChanged, 0, 0);
		Dispatch(effClose);

//		delete _pEffect; // <-  Should check for the necessity of this command.
		_pEffect = NULL;
        if(h_dll) { FreeLibrary(h_dll); h_dll = NULL; }
	}
}

void VSTPlugin::DspInit(float samplerate,int blocksize)
{
//	sample_rate = samplerate;

    Dispatch(effMainsChanged,  0, 1);
	Dispatch(effSetSampleRate, 0, 0,NULL,samplerate);
	Dispatch(effSetBlockSize,  0, blocksize);
}

void VSTPlugin::GetParamName(int numparam,char *name) const
{
	if(numparam < GetNumParams()) 
        Dispatch(effGetParamName,numparam,0,name,0.0f);
	else 
        strcpy(name,"Index out of Range");
}

bool VSTPlugin::SetParamFloat(int parameter,float value)
{
	if(Is() && parameter >= 0 && parameter < GetNumParams()) {
		_pEffect->setParameter(_pEffect,parameter,value);
		return true;
	}
    else
	    return false;
}

void VSTPlugin::GetParamValue(int numparam,char *parval) const
{
    if(Is()) {
        if(numparam < GetNumParams()) {
//			char par_name[64];
			char par_display[64];
			char par_label[64];

//			Dispatch(effGetParamName,parameter,0,par_name,0.0f);
			Dispatch(effGetParamDisplay,numparam,0,par_display,0.0f);
			Dispatch(effGetParamLabel,numparam,0,par_label,0.0f);
//			sprintf(psTxt,"%s:%s%s",par_name,par_display,par_label);
			sprintf(parval,"%s%s",par_display,par_label);
        }
	    else 
            strcpy(parval,"Index out of range");
    }
	else		
        strcpy(parval,"Plugin not loaded");
}

float VSTPlugin::GetParamValue(int numparam) const
{
	if(Is() && numparam < GetNumParams()) 
        return _pEffect->getParameter(_pEffect, numparam);
	else 
        return -1.0;
}

void VSTPlugin::Edit(bool open)
{	
	if(Is()) { 	
        if(open) {
		    if(HasEditor() && !IsEdited())
                StartEditor(this);
        }
        else if(IsEdited())
            StopEditor(this);
	}
}

void VSTPlugin::StartEditing(WHandle h)
{
    FLEXT_ASSERT(h != NULL);
	Dispatch(effEditOpen,0,0,hwnd = h);
}

void VSTPlugin::StopEditing() 
{ 
	Dispatch(effEditClose);					
    hwnd = NULL; 
}

void VSTPlugin::Visible(bool vis)
{	
	if(Is() && IsEdited()) ShowEditor(this,vis);
}

bool VSTPlugin::IsVisible() const
{	
	return Is() && IsEdited() && IsEditorShown(this);
}

bool VSTPlugin::AddMIDI(unsigned char data0,unsigned char data1,unsigned char data2)
{
	if(Is()) {
		VstMidiEvent* pevent = &midievent[queue_size];

		pevent->type = kVstMidiType;
		pevent->byteSize = 24;
		pevent->deltaFrames = 0;
		pevent->flags = 0;
		pevent->detune = 0;
		pevent->noteLength = 0;
		pevent->noteOffset = 0;
		pevent->reserved1 = 0;
		pevent->reserved2 = 0;
		pevent->noteOffVelocity = 0;
		pevent->midiData[0] = data0;
		pevent->midiData[1] = data1;
		pevent->midiData[2] = data2;
		pevent->midiData[3] = 0;

		if ( queue_size < MAX_EVENTS ) queue_size++;
		SendMidi();
		return true;
	}
	else return false;
}


void VSTPlugin::SendMidi()
{
	if(Is() && queue_size > 0) {
		// Prepare MIDI events and free queue dispatching all events
		events.numEvents = queue_size;
		events.reserved  = 0;
		for(int q = 0; q < queue_size; q++) 
            events.events[q] = (VstEvent*)&midievent[q];
		
		Dispatch(effProcessEvents, 0, 0, &events, 0.0f);
		queue_size = 0;
	}
}

static int range(int value,int mn = 0,int mx = 127) 
{
    return value < mn?mn:(value > mx?mx:value);
}

bool VSTPlugin::AddNoteOn( unsigned char note,unsigned char speed,unsigned char midichannel)
{
    return AddMIDI((char)MIDI_NOTEON | midichannel,note,speed);
}

bool VSTPlugin::AddNoteOff( unsigned char note,unsigned char midichannel)
{
    return AddMIDI((char)MIDI_NOTEOFF | midichannel,note,0);
}

void VSTPlugin::AddAftertouch(int value)
{
 	AddMIDI( (char)MIDI_NOTEOFF | _midichannel , range(value) );
}

void VSTPlugin::AddPitchBend(int value)
{
	AddMIDI( MIDI_PITCHBEND + (_midichannel & 0xf) , ((value>>7) & 127), (value & 127));
}

void VSTPlugin::AddProgramChange(int value)
{
    AddMIDI( MIDI_PROGRAMCHANGE + (_midichannel & 0xf), range(value), 0);
}

void VSTPlugin::AddControlChange(int control, int value)
{
    AddMIDI( MIDI_CONTROLCHANGE + (_midichannel & 0xf), range(control), range(value));
}


bool VSTPlugin::GetProgramName( int cat , int p, char *buf) const
{
	int parameter = p;
	if(parameter < GetNumPrograms()) {
		Dispatch(effGetProgramNameIndexed,parameter,cat,buf,0.0f);
		return true;
	}
	else
        return false;
}

void VSTPlugin::SetPos(int x,int y,bool upd) 
{
    if(Is()) {
        posx = x; posy = y; 
        if(upd && IsEdited()) MoveEditor(this,posx,posy);
    }
}



void VSTPlugin::processReplacing( float **inputs, float **outputs, long sampleframes )
{
	_pEffect->processReplacing( _pEffect , inputs , outputs , sampleframes );

}

void VSTPlugin::process( float **inputs, float **outputs, long sampleframes )
{
	_pEffect->process( _pEffect , inputs , outputs , sampleframes );
}

#if 1

// Host callback dispatcher
long VSTPlugin::Master(AEffect *effect, long opcode, long index, long value, void *ptr, float opt)
{
    switch (opcode) {
    case audioMasterVersion:
        return 2;
    case audioMasterCurrentId:
        return 'AASH';
    default:
#ifdef FLEXT_DEBUG
    	post("VST -> host: Eff = 0x%.8X, Opcode = %d, Index = %d, Value = %d, PTR = %.8X, OPT = %.3f\n",(int)effect, opcode,index,value,(int)ptr,opt);
#endif
        return 0;
    }
}

#else

// Host callback dispatcher
long VSTPlugin::Master(AEffect *effect, long opcode, long index, long value, void *ptr, float opt)
{
    VSTPlugin *th = effect?(VSTPlugin *)effect->user:NULL;
    if(!th) FLEXT_LOG("No this");

#ifdef FLEXT_DEBUG
    if(opcode != audioMasterGetTime)
	post("VST plugin call to host dispatcher: Eff: 0x%.8X, Opcode = %d, Index = %d, Value = %d, PTR = %.8X, OPT = %.3f\n",(int)effect, opcode,index,value,(int)ptr,opt);
	//st( "audioMasterWantMidi %d " , audioMasterWantMidi);
#endif

	// Support opcodes
	switch(opcode)
	{
	case audioMasterAutomate:			
		return 0;		// index, value, returns 0
		
	case audioMasterVersion:			
		return 2;		// vst version, currently 7 (0 for older)
		
	case audioMasterCurrentId:			
		return 'AASH';	// returns the unique id of a plug that's currently loading

	case audioMasterIdle:
		effect->dispatcher(effect, effEditIdle, 0, 0, NULL, 0.0f);
		return 0;		// call application idle routine (this will call effEditIdle for all open editors too) 
		
	case audioMasterPinConnected:	
		if(value == 0)
            return index < 2?0:1; //input
		else 
            return index < 2?0:1; //output

/*
	case audioMasterWantMidi:			
		return 0;

	case audioMasterProcessEvents:		
		return 0; 	// Support of vst events to host is not available
*/
	case audioMasterGetTime:
		memset(&_timeInfo, 0, sizeof(_timeInfo));
		_timeInfo.samplePos = 0;
        _timeInfo.sampleRate = th?th->sample_rate:0;
		return (long)&_timeInfo;
		
		
	case audioMasterTempoAt:			
		return 0;

	case audioMasterNeedIdle:	
//		effect->dispatcher(effect, effIdle, 0, 0, NULL, 0.0f);
		return 1;

	case audioMasterGetSampleRate:		
        return th?(long)th->sample_rate:0;	

	case audioMasterGetVendorString:	// Just fooling version string
		strcpy((char*)ptr,"Steinberg");
		return 0;
	
	case audioMasterGetVendorVersion:	
		return 5000;	// HOST version 5000

	case audioMasterGetProductString:	// Just fooling product string
		strcpy((char *)ptr,"Cubase 5.0");
		return 0;

	case audioMasterVendorSpecific:		
		return 0;

	case audioMasterGetLanguage:		
		return kVstLangEnglish;
	
	case audioMasterUpdateDisplay:
		FLEXT_LOG("audioMasterUpdateDisplay");
		effect->dispatcher(effect, effEditIdle, 0, 0, NULL, 0.0f);
		return 0;

	case 	audioMasterCanDo:
		if (!strcmp((char *)ptr,"sendVstEvents")) return 1;
		else if (!strcmp((char *)ptr,"sendVstMidiEvent")) return 1;
		else if (!strcmp((char *)ptr,"sendVstTimeInfo")) return 1;
//			"receiveVstEvents",
//			"receiveVstMidiEvent",
//			"receiveVstTimeInfo",
		
//			"reportConnectionChanges",
//			"acceptIOChanges",
//		else if (!strcmp((char*)ptr,"sizeWindow")) return 1;
		else if (!strcmp((char*)ptr,"supplyIdle")) return 1;
		return -1;
		
	case 	audioMasterSetTime:						FLEXT_LOG("VST master dispatcher: Set Time");break;
	case 	audioMasterGetNumAutomatableParameters:	FLEXT_LOG("VST master dispatcher: GetNumAutPar");break;
	case 	audioMasterGetParameterQuantization:	FLEXT_LOG("VST master dispatcher: ParamQuant");break;
	case 	audioMasterIOChanged:					FLEXT_LOG("VST master dispatcher: IOchanged");break;
	case 	audioMasterSizeWindow:					FLEXT_LOG("VST master dispatcher: Size Window");break;
	case 	audioMasterGetBlockSize:				FLEXT_LOG("VST master dispatcher: GetBlockSize");break;
	case 	audioMasterGetInputLatency:				FLEXT_LOG("VST master dispatcher: GetInLatency");break;
	case 	audioMasterGetOutputLatency:			FLEXT_LOG("VST master dispatcher: GetOutLatency");break;
	case 	audioMasterGetPreviousPlug:				FLEXT_LOG("VST master dispatcher: PrevPlug");break;
	case 	audioMasterGetNextPlug:					FLEXT_LOG("VST master dispatcher: NextPlug");break;
	case 	audioMasterWillReplaceOrAccumulate:		FLEXT_LOG("VST master dispatcher: WillReplace"); break;
	case 	audioMasterGetCurrentProcessLevel:		return 0; break;
	case 	audioMasterGetAutomationState:			FLEXT_LOG("VST master dispatcher: GetAutState");break;
	case 	audioMasterOfflineStart:				FLEXT_LOG("VST master dispatcher: Offlinestart");break;
	case 	audioMasterOfflineRead:					FLEXT_LOG("VST master dispatcher: Offlineread");break;
	case 	audioMasterOfflineWrite:				FLEXT_LOG("VST master dispatcher: Offlinewrite");break;
	case 	audioMasterOfflineGetCurrentPass:		FLEXT_LOG("VST master dispatcher: OfflineGetcurrentpass");break;
	case 	audioMasterOfflineGetCurrentMetaPass:	FLEXT_LOG("VST master dispatcher: GetGetCurrentMetapass");break;
	case 	audioMasterSetOutputSampleRate:			FLEXT_LOG("VST master dispatcher: Setsamplerate");break;
	case 	audioMasterGetSpeakerArrangement:		FLEXT_LOG("VST master dispatcher: Getspeaker");break;
	case 	audioMasterSetIcon:						FLEXT_LOG("VST master dispatcher: seticon");break;
	case 	audioMasterOpenWindow:					FLEXT_LOG("VST master dispatcher: OpenWindow");break;
	case 	audioMasterCloseWindow:					FLEXT_LOG("VST master dispatcher: CloseWindow");break;
	case 	audioMasterGetDirectory:				FLEXT_LOG("VST master dispatcher: GetDirectory");break;
//	case		audioMasterUpdateDisplay:				post("VST master dispatcher: audioMasterUpdateDisplay");break;

#ifdef FLEXT_DEBUG
	default: 
        post("VST master dispatcher: undefed: %d , %d",opcode , effKeysRequired );
#endif
	}	
	
	return 0;
}

#endif