iPlug2 - C++ Audio Plug-in Framework
IPlug_include_in_plug_src.h
Go to the documentation of this file.
1 /*
2  ==============================================================================
3 
4  This file is part of the iPlug 2 library. Copyright (C) the iPlug 2 developers.
5 
6  See LICENSE.txt for more info.
7 
8  ==============================================================================
9 */
10 
11 #pragma once
12 
21 #pragma mark - OS_WIN
22 
23 #if defined OS_WIN && !defined VST3C_API
24  HINSTANCE gHINSTANCE = 0;
25  #if defined(VST2_API) || defined(AAX_API)
26  #ifdef __MINGW32__
27  extern "C"
28  #endif
29  BOOL WINAPI DllMain(HINSTANCE hDllInst, DWORD fdwReason, LPVOID res)
30  {
31  gHINSTANCE = hDllInst;
32  return true;
33  }
34  #endif
35 
36  UINT(WINAPI *__GetDpiForWindow)(HWND);
37 
38  float GetScaleForHWND(HWND hWnd)
39  {
40  if (!__GetDpiForWindow)
41  {
42  HINSTANCE h = LoadLibraryA("user32.dll");
43  if (h) *(void **)&__GetDpiForWindow = GetProcAddress(h, "GetDpiForWindow");
44 
45  if (!__GetDpiForWindow)
46  return 1;
47  }
48 
49  int dpi = __GetDpiForWindow(hWnd);
50 
51  if (dpi != USER_DEFAULT_SCREEN_DPI)
52  {
53 #if defined IGRAPHICS_QUANTISE_SCREENSCALE
54  return std::round(static_cast<float>(dpi) / USER_DEFAULT_SCREEN_DPI);
55 #else
56  return static_cast<float>(dpi) / USER_DEFAULT_SCREEN_DPI;
57 #endif
58  }
59 
60 
61  return 1;
62  }
63 
64 #endif
65 
66 #pragma mark - ** Global Functions and Defines **
67 
68 #pragma mark - VST2
69 #if defined VST2_API
70  extern "C"
71  {
72  EXPORT void* VSTPluginMain(audioMasterCallback hostCallback)
73  {
74  using namespace iplug;
75 
76  IPlugVST2* pPlug = MakePlug(InstanceInfo{hostCallback});
77 
78  if (pPlug)
79  {
80  AEffect& aEffect = pPlug->GetAEffect();
81  pPlug->EnsureDefaultPreset();
82  aEffect.numPrograms = std::max(aEffect.numPrograms, 1); // some hosts don't like 0 presets
83  return &aEffect;
84  }
85  return 0;
86  }
87 
88  EXPORT int main(int hostCallback)
89  {
90  #if defined OS_MAC
91  return (VstIntPtr) VSTPluginMain((audioMasterCallback)hostCallback);
92  #else
93  return (int) VSTPluginMain((audioMasterCallback)hostCallback);
94  #endif
95  }
96  };
97 #pragma mark - VST3 (All)
98 #elif defined VST3_API || VST3C_API || defined VST3P_API
99  #include "public.sdk/source/main/pluginfactory.h"
100  #include "pluginterfaces/vst/ivstcomponent.h"
101  #include "pluginterfaces/vst/ivsteditcontroller.h"
102 
103 #if !defined VST3_PROCESSOR_UID && !defined VST3_CONTROLLER_UID
104 #define VST3_PROCESSOR_UID 0xF2AEE70D, 0x00DE4F4E, PLUG_MFR_ID, PLUG_UNIQUE_ID
105 #define VST3_CONTROLLER_UID 0xF2AEE70E, 0x00DE4F4F, PLUG_MFR_ID, PLUG_UNIQUE_ID
106 #endif
107 
108  #ifndef EFFECT_TYPE_VST3
109  #if PLUG_TYPE == 1
110  #define EFFECT_TYPE_VST3 kInstrumentSynth
111  #else
112  #define EFFECT_TYPE_VST3 kFx
113  #endif
114  #endif
115 
116  #if defined VST3P_API || defined VST3_API
117  bool InitModule()
118  {
119  #ifdef OS_WIN
120  extern void* moduleHandle;
121  gHINSTANCE = (HINSTANCE) moduleHandle;
122  #endif
123  return true;
124  }
125 
126  // called after library is unloaded
127  bool DeinitModule()
128  {
129  return true;
130  }
131  #endif
132  #pragma mark - VST3
133  #if defined VST3_API
134  static Steinberg::FUnknown* createInstance(void*)
135  {
136  return (Steinberg::Vst::IAudioProcessor*) MakePlug(InstanceInfo());
137  }
138 
139  BEGIN_FACTORY_DEF(PLUG_MFR, PLUG_URL_STR, PLUG_EMAIL_STR)
140 
141  DEF_CLASS2(INLINE_UID_FROM_FUID(FUID(VST3_PROCESSOR_UID)),
142  Steinberg::PClassInfo::kManyInstances, // cardinality
143  kVstAudioEffectClass, // the component category (don't change this)
144  PLUG_NAME, // plug-in name
145  Steinberg::Vst::kSimpleModeSupported, // means gui and plugin aren't split
146  VST3_SUBCATEGORY, // Subcategory for this plug-in
147  PLUG_VERSION_STR, // plug-in version
148  kVstVersionString, // the VST 3 SDK version (don't change - use define)
149  createInstance) // function pointer called to be instantiate
150 
151  END_FACTORY
152  #pragma mark - VST3 Processor
153  #elif defined VST3P_API
154  static Steinberg::FUnknown* createProcessorInstance(void*)
155  {
156  return MakeProcessor();
157  }
158 
159  static Steinberg::FUnknown* createControllerInstance(void*)
160  {
161  return MakeController();
162  }
163 
164  BEGIN_FACTORY_DEF(PLUG_MFR, PLUG_URL_STR, PLUG_EMAIL_STR)
165 
166  DEF_CLASS2 (INLINE_UID_FROM_FUID(FUID(VST3_PROCESSOR_UID)),
167  PClassInfo::kManyInstances, // cardinality
168  kVstAudioEffectClass, // the component category (do not changed this)
169  PLUG_NAME, // here the Plug-in name (to be changed)
170  Vst::kDistributable, // means component/controller can on different computers
171  VST3_SUBCATEGORY, // Subcategory for this Plug-in (to be changed)
172  PLUG_VERSION_STR, // Plug-in version (to be changed)
173  kVstVersionString, // the VST 3 SDK version (don't change - use define)
174  createProcessorInstance) // function pointer called to be instantiate
175 
176  DEF_CLASS2(INLINE_UID_FROM_FUID(FUID(VST3_CONTROLLER_UID)),
177  PClassInfo::kManyInstances, // cardinality
178  kVstComponentControllerClass, // the Controller category (do not changed this)
179  PLUG_NAME " Controller", // controller name (could be the same than component name)
180  0, // not used here
181  "", // not used here
182  PLUG_VERSION_STR, // Plug-in version (to be changed)
183  kVstVersionString, // the VST 3 SDK version (don't change - use define)
184  createControllerInstance) // function pointer called to be instantiate
185 
186  END_FACTORY
187  #endif
188 #pragma mark - AUv2
189 #elif defined AU_API
190  extern "C"
191  {
192  #ifndef AU_NO_COMPONENT_ENTRY
193  //Component Manager
194  EXPORT ComponentResult AUV2_ENTRY(ComponentParameters* pParams, void* pPlug)
195  {
196  return IPlugAU::IPlugAUEntry(pParams, pPlug);
197  }
198  #endif
199 
200  //>10.7 SDK AUPlugin
201  EXPORT void* AUV2_FACTORY(const AudioComponentDescription* pInDesc)
202  {
204  }
205  };
206 #pragma mark - WAM
207 #elif defined WAM_API
208  extern "C"
209  {
210  EMSCRIPTEN_KEEPALIVE void* createModule()
211  {
212  Processor* pWAM = dynamic_cast<Processor*>(iplug::MakePlug(InstanceInfo()));
213  return (void*) pWAM;
214  }
215  }
216 #pragma mark - WEB
217 #elif defined WEB_API
218 #include <memory>
219 #include "config.h"
220  std::unique_ptr<iplug::IPlugWeb> gPlug;
221  extern void StartMainLoopTimer();
222 
223  extern "C"
224  {
225  EMSCRIPTEN_KEEPALIVE void iplug_syncfs()
226  {
227  EM_ASM({
228  if(Module.syncdone == 1) {
229  Module.syncdone = 0;
230  FS.syncfs(false, function (err) {
231  assert(!err);
232  console.log("Synced to IDBFS...");
233  Module.syncdone = 1;
234  });
235  }
236  });
237  }
238 
239  EMSCRIPTEN_KEEPALIVE void iplug_fsready()
240  {
241  gPlug = std::unique_ptr<iplug::IPlugWeb>(iplug::MakePlug(InstanceInfo()));
242  gPlug->SetHost("www", 0);
243  gPlug->OpenWindow(nullptr);
244  iplug_syncfs(); // plug in may initialise settings in constructor, write to persistent data after init
245  }
246  }
247 
248  int main()
249  {
250  //create persistent data file system and synchronise
251  EM_ASM(
252  var name = '/' + UTF8ToString($0) + '_data';
253  FS.mkdir(name);
254  FS.mount(IDBFS, {}, name);
255 
256  Module.syncdone = 0;
257  FS.syncfs(true, function (err) {
258  assert(!err);
259  console.log("Synced from IDBFS...");
260  Module.syncdone = 1;
261  ccall('iplug_fsready', 'v');
262  });
263  , PLUG_NAME);
264 
265  StartMainLoopTimer();
266 
267  // TODO: this code never runs, so when do we delete?!
268  gPlug = nullptr;
269 
270  return 0;
271  }
272 #elif defined AUv3_API || defined AAX_API || defined APP_API
273 // Nothing to do here
274 #else
275  #error "No API defined!"
276 #endif
277 
278 #pragma mark - ** Instantiation **
279 
280 BEGIN_IPLUG_NAMESPACE
281 
282 #pragma mark -
283 #pragma mark VST2, VST3, AAX, AUv3, APP, WAM, WEB
284 
285 #if defined VST2_API || defined VST3_API || defined AAX_API || defined AUv3_API || defined APP_API || defined WAM_API || defined WEB_API
286 
287 Plugin* MakePlug(const InstanceInfo& info)
288 {
289  // From VST3 - is this necessary?
290  static WDL_Mutex sMutex;
291  WDL_MutexLock lock(&sMutex);
292 
293  return new PLUG_CLASS_NAME(info);
294 }
295 
296 #pragma mark - AUv2
297 #elif defined AU_API
298 
299 Plugin* MakePlug(void* pMemory)
300 {
301  InstanceInfo info;
302  info.mCocoaViewFactoryClassName.Set(AUV2_VIEW_CLASS_STR);
303 
304  if (pMemory)
305  return new(pMemory) PLUG_CLASS_NAME(info);
306  else
307  return new PLUG_CLASS_NAME(info);
308 }
309 
310 #pragma mark - VST3 Controller
311 #elif defined VST3C_API
312 
313 Steinberg::FUnknown* MakeController()
314 {
315  static WDL_Mutex sMutex;
316  WDL_MutexLock lock(&sMutex);
317  IPlugVST3Controller::InstanceInfo info;
318  info.mOtherGUID = Steinberg::FUID(VST3_PROCESSOR_UID);
319  //If you are trying to build a distributed VST3 plug-in and you hit an error here "no matching constructor...",
320  //you need to replace all instances of PLUG_CLASS_NAME in your plug-in class, with the macro PLUG_CLASS_NAME
321  return static_cast<Steinberg::Vst::IEditController*>(new PLUG_CLASS_NAME(info));
322 }
323 
324 #pragma mark - VST3 Processor
325 #elif defined VST3P_API
326 
327 Steinberg::FUnknown* MakeProcessor()
328 {
329  static WDL_Mutex sMutex;
330  WDL_MutexLock lock(&sMutex);
331  IPlugVST3Processor::InstanceInfo info;
332  info.mOtherGUID = Steinberg::FUID(VST3_CONTROLLER_UID);
333  return static_cast<Steinberg::Vst::IAudioProcessor*>(new PLUG_CLASS_NAME(info));
334 }
335 
336 #else
337 #error "No API defined!"
338 #endif
339 
340 #pragma mark - ** Config Utility **
341 
342 static Config MakeConfig(int nParams, int nPresets)
343 {
344  return Config(nParams, nPresets, PLUG_CHANNEL_IO, PLUG_NAME, PLUG_NAME, PLUG_MFR, PLUG_VERSION_HEX, PLUG_UNIQUE_ID, PLUG_MFR_ID, PLUG_LATENCY, PLUG_DOES_MIDI_IN, PLUG_DOES_MIDI_OUT, PLUG_DOES_MPE, PLUG_DOES_STATE_CHUNKS, PLUG_TYPE, PLUG_HAS_UI, PLUG_WIDTH, PLUG_HEIGHT, PLUG_HOST_RESIZE, PLUG_MIN_WIDTH, PLUG_MAX_WIDTH, PLUG_MIN_HEIGHT, PLUG_MAX_HEIGHT, BUNDLE_ID); // TODO: Product Name?
345 }
346 
347 END_IPLUG_NAMESPACE
348 
349 /*
350  #if defined _DEBUG
351  #define PLUG_NAME APPEND_TIMESTAMP(PLUG_NAME " DEBUG")
352  #elif defined TRACER_BUILD
353  #define PLUG_NAME APPEND_TIMESTAMP(PLUG_NAME " TRACER")
354  #elif defined TIMESTAMP_PLUG_NAME
355  #pragma REMINDER("plug name is timestamped")
356  #define PLUG_NAME APPEND_TIMESTAMP(PLUG_NAME)
357  #else
358  #define PLUG_NAME PLUG_NAME
359  #endif
360  */
361 
362 #if !defined NO_IGRAPHICS && !defined VST3P_API
363 #include "IGraphics_include_in_plug_src.h"
364 #endif
void EnsureDefaultPreset()
[VST2 only] Called to fill uninitialzed presets
VST2.4 API base class for an IPlug plug-in.
Definition: IPlugVST2.h:34
AudioUnit v2 Factory Class Template.
Definition: IPlugAU.h:229