File: audio_effect_provider_vst2.cpp

package info (click to toggle)
zytrax 0%2Bgit20201215-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 2,488 kB
  • sloc: cpp: 41,800; ansic: 3,387; makefile: 8; sh: 3
file content (294 lines) | stat: -rw-r--r-- 7,647 bytes parent folder | download | duplicates (2)
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
#include "audio_effect_provider_vst2.h"
#include "vestige.h"
#include <dirent.h>
/////////////////////////////////////////////

#ifdef WINDOWS_ENABLED
AEffect *AudioEffectProviderVST2::open_vst_from_lib_handle(HINSTANCE libhandle, audioMasterCallback p_master_callback) {

	if (libhandle == NULL) {
		//printf("invalid file: %s\n",lib_name);
		return NULL;
	}

	AEffect *(__cdecl * getNewPlugInstance)(audioMasterCallback);

	getNewPlugInstance = (AEffect * (__cdecl *)(audioMasterCallback)) GetProcAddress(libhandle, "VSTPluginMain");
	if (!getNewPlugInstance) {
		getNewPlugInstance = (AEffect * (__cdecl *)(audioMasterCallback)) GetProcAddress(libhandle, "main");
	}

	if (getNewPlugInstance == NULL) {
		FreeLibrary(libhandle);
		//WARN_PRINT("Can't find symbol 'main'");
		return NULL;
	}
	return getNewPlugInstance(p_master_callback);
}
#else
AEffect *AudioEffectProviderVST2::open_vst_from_lib_handle(void *libhandle, audioMasterCallback p_master_callback) {

	if (libhandle == NULL) {
		//printf("invalid file: %s\n",lib_name);
		return NULL;
	}

	AEffect *(*getNewPlugInstance)(audioMasterCallback);

	getNewPlugInstance = (AEffect * (*)(audioMasterCallback)) dlsym(libhandle, "VSTPluginMain");
	if (!getNewPlugInstance) {
		getNewPlugInstance = (AEffect * (*)(audioMasterCallback)) dlsym(libhandle, "main");
	}

	if (getNewPlugInstance == NULL) {
		dlclose(libhandle);
		//WARN_PRINT("Can't find symbol 'main'");
		return NULL;
	}
	return getNewPlugInstance(p_master_callback);
}
#endif

String AudioEffectProviderVST2::get_id() const {
	return "VST2";
}

AudioEffect *AudioEffectProviderVST2::instantiate_effect(const AudioEffectInfo *p_info) {

	AudioEffectVST2 *fx_vst2 = new AudioEffectVST2;
	if (fx_vst2->open(p_info->path, p_info->unique_ID, p_info->caption, p_info->provider_id) != OK) {
		return NULL;
	}

	return fx_vst2;
}

void AudioEffectProviderVST2::scan_effects(AudioEffectFactory *p_factory, ScanCallback p_callback, void *p_userdata) {

	for (int i = 0; i < MAX_SCAN_PATHS; i++) {

		String p = get_scan_path(i).strip_edges();
		if (p == String()) {
			continue;
		}
		printf("scanning path: %s\n", p.utf8().get_data());
#ifdef WINDOWS_ENABLED
		_WDIR *dir;
		struct _wdirent *dirent;
		dir = _wopendir(p.c_str());
#else
		DIR *dir;
		struct dirent *dirent;
		dir = opendir(p.utf8().get_data());

#endif

		if (dir == NULL) {
			printf("failed?\n");
			return;
		}

		printf("opened dir\n");
#ifdef WINDOWS_ENABLED

		while ((dirent = _wreaddir(dir))) {
#else
		while ((dirent = readdir(dir))) {
#endif
			String lib_name = p + "/" + String(dirent->d_name);

#ifdef WINDOWS_ENABLED
			if (lib_name.get_extension() != "dll") {
#elif defined(OSX_ENABLED)
			if (lib_name.get_extension() != "dylib") {
#else
		if (lib_name.get_extension() != "so") {

#endif
				continue;
			}
			printf("opening plugin: %s\n", lib_name.utf8().get_data());
#ifdef WINDOWS_ENABLED

			HINSTANCE libhandle = LoadLibraryW(lib_name.c_str());

			AEffect *ptrPlug = open_vst_from_lib_handle(libhandle, &host);
#else

			void *libhandle = dlopen(lib_name.utf8().get_data(), RTLD_LOCAL | RTLD_LAZY);
			if (!libhandle) {
				continue;
			}
			AEffect *ptrPlug = open_vst_from_lib_handle(libhandle, &host);

#endif

			if (ptrPlug == NULL) {
#ifdef WINDOWS_ENABLED
				FreeLibrary(libhandle);
#else
				dlclose(libhandle);
#endif
				continue;
			}

			if (ptrPlug->magic != kEffectMagic) {
#ifdef WINDOWS_ENABLED
				FreeLibrary(libhandle);
#else
				dlclose(libhandle);
#endif
				continue;
			}

			ptrPlug->dispatcher(ptrPlug, effOpen, 0, 0, NULL, 0.0f);

			if (ptrPlug->numOutputs >= 2) { //needs to have at least 2 outputs
				AudioEffectInfo info;

				String name = dirent->d_name;
				name = name.substr(0, name.find("."));
				info.caption = name;
				printf("plugin name: %s\n", info.caption.utf8().get_data());
				info.description = "VST Info:\n Name: " + info.caption + "\n ID: " + String::num(ptrPlug->uniqueID) + "\n Version: " + String(ptrPlug->version);
				info.unique_ID = "VST_" + String::num(ptrPlug->uniqueID);
				info.synth = /*(ptrPlug->dispatcher(ptrPlug,effGetVstVersion,0,0,NULL,0.0f)==2  */ ptrPlug->flags & effFlagsIsSynth;
				info.category = info.synth ? "VST Instruments" : "VST Effects";
				info.has_ui = (ptrPlug->flags & effFlagsHasEditor);

				info.provider_caption = "VST2";
				info.version = String::num(ptrPlug->version);

				info.provider_id = get_id();
				info.path = lib_name;

				if (ptrPlug->flags & effFlagsProgramChunks) {

					info.description += " (CS)";
				}

				/* Perform the "write only" test */

				//plugin_data->write_only=true;	//i cant really be certain of anything with VST plugins, so this is always true
				/*
				if (ptrPlug->numParams) {

					ptrPlug->setParameter(ptrPlug,0,1.0); //set 1.0
					float res=ptrPlug->getParameter(ptrPlug,0);
					if (res<0.8) { //try if it's not near 1.0, with some threshold, then no reading (far most of the ones that dont support this will just return 0)


					}
			} */
				p_factory->add_audio_effect(info);
				if (p_callback) {
					p_callback(info.caption, p_userdata);
				}
			}
			ptrPlug->dispatcher(ptrPlug, effClose, 0, 0, NULL, 0.0f);
#ifdef WINDOWS_ENABLED
			FreeLibrary(libhandle);
#else
			dlclose(libhandle);
#endif
		}
	}
}

intptr_t VESTIGECALLBACK AudioEffectProviderVST2::host(AEffect *effect, int32_t opcode, int32_t index, intptr_t value, void *ptr, float opt) {
	long retval = 0;
	//simple host for exploring plugin
	switch (opcode) {
		//VST 1.0 opcodes
		case audioMasterVersion:
			//Input values:
			//none

			//Return Value:
			//0 or 1 for old version
			//2 or higher for VST2.0 host?
			retval = 2;
			break;
		case audioMasterGetSampleRate:

			effect->dispatcher(effect, effSetSampleRate, 0, 0, NULL, 44100); //just crap
			break;
		case audioMasterGetBlockSize:
			//Input Values:
			//None

			//Return Value:
			//not tested, always return 0

			//NB - Host must despatch effSetBlockSize to the plug in response
			//to this call
			//Check despatcher notes for any return codes from effSetBlockSize
			effect->dispatcher(effect, effSetBlockSize, 0, 1024, NULL, 0.0f);

			break;
		case audioMasterCanDo:
			//Input Values:
			//<ptr> predefined "canDo" string

			//Return Value:
			//0 = Not Supported
			//non-zero value if host supports that feature

			//NB - Possible Can Do strings are:
			//"sendVstEvents",
			//"sendVstMidiEvent",
			//"sendVstTimeInfo",
			//"receiveVstEvents",
			//"receiveVstMidiEvent",
			//"receiveVstTimeInfo",
			//"reportConnectionChanges",
			//"acceptIOChanges",
			//"sizeWindow",
			//"asyncProcessing",
			//"offline",
			//"supplyIdle",
			//"supportShell"
			if (strcmp((char *)ptr, "supplyIdle") == 0 ||
					strcmp((char *)ptr, "sendVstTimeInfo") == 0 ||
					strcmp((char *)ptr, "sendVstEvents") == 0 ||
					strcmp((char *)ptr, "sendVstMidiEvent") == 0 ||
					strcmp((char *)ptr, "sizeWindow") == 0) {
				retval = 1;
			} else {
				retval = 0;
			}

			break;
		case audioMasterGetLanguage:
			//Input Values:
			//None

			//Return Value:
			//kVstLangEnglish
			//kVstLangGerman
			//kVstLangFrench
			//kVstLangItalian
			//kVstLangSpanish
			//kVstLangJapanese

			retval = kVstLangEnglish;
			break;
	}
	return retval;
}

String AudioEffectProviderVST2::get_name() const {
	return "VST2";
}

AudioEffectProviderVST2 *AudioEffectProviderVST2::singleton = NULL;

AudioEffectProviderVST2::AudioEffectProviderVST2() {
	//paths = "C:\\Program Files\\Synister64";
	//paths = "C:\\Program Files\\Synister64";
	//paths = "C:\\Program Files\\Common Files\\VST2\\SonicCat";
	singleton = this;
}

AudioEffectProviderVST2::~AudioEffectProviderVST2() {
}