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

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

#include "vsthost.h"


const t_symbol 
    *VSTPlugin::sym_event,
    *VSTPlugin::sym_evmidi,
    *VSTPlugin::sym_evaudio,
    *VSTPlugin::sym_evvideo,
    *VSTPlugin::sym_evparam,
    *VSTPlugin::sym_evtrigger,
    *VSTPlugin::sym_evsysex,
    *VSTPlugin::sym_ev_,
    *VSTPlugin::sym_midi[8];

void VSTPlugin::Setup()
{
    sym_event = flext::MakeSymbol("event");
    sym_evmidi = flext::MakeSymbol("midi");
    sym_evaudio = flext::MakeSymbol("audio");
    sym_evvideo = flext::MakeSymbol("video");
    sym_evparam = flext::MakeSymbol("param");
    sym_evtrigger = flext::MakeSymbol("trigger");
    sym_evsysex = flext::MakeSymbol("sysex");
    sym_ev_ = flext::MakeSymbol("???");

    sym_midi[0] = flext::MakeSymbol("noteon");
    sym_midi[1] = flext::MakeSymbol("noteoff");
    sym_midi[2] = flext::MakeSymbol("polyafter");
    sym_midi[3] = flext::MakeSymbol("cntl");
    sym_midi[4] = flext::MakeSymbol("progchg");
    sym_midi[5] = flext::MakeSymbol("chnafter");
    sym_midi[6] = flext::MakeSymbol("pitchbend");
    sym_midi[7] = sym__;
}

VSTPlugin::VSTPlugin(Responder *resp)
    : hdll(NULL),hwnd(NULL)
    , effect(NULL),pluginmain(NULL),audiomaster(NULL)
    , responder(resp)
    , posx(0),posy(0),caption(true)
    , midichannel(0),eventqusz(0)
    , paramnamecnt(0)
    , transchg(true)
    , playing(false),looping(false)
    , samplerate(0)
    , samplepos(0),ppqpos(0)
    , tempo(120)
    , timesignom(4),timesigden(4)
    , barstartpos(0)
    , cyclestartpos(0),cycleendpos(0)
    , smpteoffset(0),smpterate(0)
{}

VSTPlugin::~VSTPlugin()
{
	Free();
}


#if FLEXT_OS == FLEXT_OS_MAC
OSStatus FSPathMakeFSSpec(const UInt8 *path,FSSpec *spec,Boolean *isDirectory)  /* can be NULL */
{
    OSStatus  result;
    FSRef    ref;

    /* check parameters */
    require_action(NULL != spec, BadParameter, result = paramErr);

    /* convert the POSIX path to an FSRef */
    result = FSPathMakeRef(path, &ref, isDirectory);
    require_noerr(result, FSPathMakeRef);

    /* and then convert the FSRef to an FSSpec */
    result = FSGetCatalogInfo(&ref, kFSCatInfoNone, NULL, NULL, spec, NULL);
    require_noerr(result, FSGetCatalogInfo);
  
FSGetCatalogInfo:
FSPathMakeRef:
BadParameter:
    return result;
}
#endif

// hdll, pluginmain and audiomaster are set here
// must be NULL beforehand!
bool VSTPlugin::NewPlugin(const char *plugname)
{
    FLEXT_ASSERT(!hdll && !pluginmain && !audiomaster);

    dllname = plugname;

#if FLEXT_OS == FLEXT_OS_WIN
    hdll = LoadLibrary(dllname.c_str());
    if(hdll) pluginmain = (PVSTMAIN)GetProcAddress(hdll,"main");
    audiomaster = Master;  

#elif FLEXT_OS == FLEXT_OS_MAC
    short   resFileID;
    FSSpec  spec;
    OSErr err;

    err = FSPathMakeFSSpec(dllname.c_str(),&spec,NULL);
    resFileID = FSpOpenResFile(&spec, fsRdPerm);
    short cResCB = Count1Resources('aEff');

    for(int i = 0; i < cResCB; i++) {
        Handle             codeH;
        CFragConnectionID  connID;
        Ptr                mainAddr;
        Str255             errName;
        Str255             fragName;
        char               fragNameCStr[256];
        short              resID;
        OSType             resType;

        codeH = Get1IndResource('aEff', short(i+1));
        if(!codeH) continue;

        GetResInfo(codeH, &resID, &resType, fragName);
        DetachResource(codeH);
        HLock(codeH);

        err = GetMemFragment(*codeH,
                             GetHandleSize(codeH),
                             fragName,
                             kPrivateCFragCopy,
                             &connID, (Ptr *) & mainAddr, errName);

        if(!err) {
           #ifdef __CFM__
           pluginmain = (PVSTMAIN)NewMachOFromCFM(mainAddr);
           #else
           pluginmain = (PVSTMAIN)mainAddr;
           #endif
        }
    }
    CloseResFile(resFileID);

    audiomaster = 
#ifdef __CFM__
        NewCFMFromMachO(Master);
#else
        Master;
#endif

#else
#error Platform not supported
#endif    

    if(hdll && pluginmain && audiomaster)
        return true;
    else {
        FreePlugin();
        return false;
    }
}

void VSTPlugin::FreePlugin()
{
#if FLEXT_OS == FLEXT_OS_WIN
    if(hdll) FreeLibrary(hdll); 
#elif FLEXT_OS == FLEXT_OS_MAC
	
#ifdef __MACOSX__
#ifdef __CFM__
    if(audiomaster) DisposeCFMFromMachO(audiomaster);
    if(pluginmain) DisposeMachOFromCFM(pluginmain);
#endif
#endif

#else
#error Platform not supported
#endif    

    effect = NULL;
    audiomaster = NULL;
    pluginmain = NULL;
    hdll = NULL;
} 

/*
This is static to be able to communicate between the plugin methods 
and the static Audiomaster function
the this (plugin->user) pointer has not been initialized at the point it is needed
static should not be a problem, as we are single-threaded and it is immediately 
queried in a called function
*/
long VSTPlugin::uniqueid = 0;

std::string VSTPlugin::dllloading;

bool VSTPlugin::InstPlugin(long plugid)
{
    uniqueid = plugid;
    dllloading = dllname;

    FLEXT_ASSERT(pluginmain && audiomaster);

	//This calls the "main" function and receives the pointer to the AEffect structure.
	effect = pluginmain(audiomaster);
	if(!effect || effect->magic != kEffectMagic) {
		post("VST plugin : Unable to create effect");
		effect = NULL; 
	    return false;
    }
    return true;
}

bool VSTPlugin::Instance(const char *name,const char *subname)
{
    bool ok = effect != NULL;
    
    if(!ok && dllname != name) {
        FreePlugin();
        // freshly load plugin
        ok = NewPlugin(name) && InstPlugin();
    }

    if(ok && subname && *subname && Dispatch(effGetPlugCategory) == kPlugCategShell) {
        // sub plugin-name given -> scan plugs

        long plugid;
	    char tmp[64];
	    // scan shell for subplugins
	    while((plugid = Dispatch(effShellGetNextPlugin,0,0,tmp))) { 
		    // subplug needs a name
            FLEXT_LOG1("subplug %s",tmp);
            if(!strcmp(subname,tmp)) 
                // found
                break;
	    }

        // re-init with plugid set
        if(plugid) ok = InstPlugin(plugid);
    }

    if(ok) {
	    //init plugin 
	    effect->user = this;
	    ok = Dispatch(effOpen) == 0;
    }

    if(ok) {
	    ok = Dispatch(effIdentify) == 'NvEf';
    }

    if(ok) {
        *productname = 0;
	    long ret = Dispatch(effGetProductString,0,0,productname);

        if(!*productname) {
		    // no product name given by plugin -> extract it from the filename

		    std::string str1(dllname);
		    std::string::size_type slpos = str1.rfind('\\');
		    if(slpos == std::string::npos) {
			    slpos = str1.rfind('/');
			    if(slpos == std::string::npos)
				    slpos = 0;
			    else
				    ++slpos;
		    }
		    else
			    ++slpos;
		    std::string str2 = str1.substr(slpos);
		    int snip = str2.find('.');
            if( snip != std::string::npos )
			    str1 = str2.substr(0,snip);
		    else
			    str1 = str2;
		    strcpy(productname,str1.c_str());
	    }
    	
        if(*productname) {
            char tmp[512];
            sprintf(tmp,"vst~ - %s",productname);
            title = tmp;
        }
        else
            title = "vst~";

	    *vendorname = 0;
	    Dispatch(effGetVendorString,0,0,vendorname);
    }

    if(!ok) Free();
	return ok;
}

void VSTPlugin::Free() // Called also in destruction
{
	if(effect) {
        Edit(false);

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

    // \TODO
    // Here, we really have to wait until the editor thread has terminated
    // otherwise WM_DESTROY etc. messages may still be pending
    // in other words: this is a design flaw
    // There should be a data stub accessible from the plugin object and the thread
    // holding the necessary data, so that both can operate independently

    FreePlugin(); 
}

void VSTPlugin::DspInit(float sr,int blsz)
{
    // sample rate and block size must _first_ be set
	Dispatch(effSetSampleRate,0,0,NULL,samplerate = sr);
	Dispatch(effSetBlockSize, 0,blsz);
    // then signal that mains have changed!
    Dispatch(effMainsChanged,0,1);
}

void VSTPlugin::ListPlugs(const t_symbol *sym) const
{
    if(responder) {
        if(Is() && Dispatch(effGetPlugCategory) == kPlugCategShell) {
            t_atom at;
            // sub plugin-name given -> scan plugs
	        char tmp[64];
	        // scan shell for subplugins
            while(Dispatch(effShellGetNextPlugin,0,0,tmp)) {
                SetString(at,tmp);
                responder->Respond(sym,1,&at);
            }
        }

        // bang
        responder->Respond(sym);
    }
}