Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

336 lines
14KB

  1. //------------------------------------------------------------------------
  2. // Project : VST SDK
  3. //
  4. // Category : Helpers
  5. // Filename : public.sdk/source/vst/vst2wrapper/vst2wrapper.h
  6. // Created by : Steinberg, 01/2009
  7. // Description : VST 3 -> VST 2 Wrapper
  8. //
  9. //-----------------------------------------------------------------------------
  10. // LICENSE
  11. // (c) 2017, Steinberg Media Technologies GmbH, All Rights Reserved
  12. //-----------------------------------------------------------------------------
  13. // Redistribution and use in source and binary forms, with or without modification,
  14. // are permitted provided that the following conditions are met:
  15. //
  16. // * Redistributions of source code must retain the above copyright notice,
  17. // this list of conditions and the following disclaimer.
  18. // * Redistributions in binary form must reproduce the above copyright notice,
  19. // this list of conditions and the following disclaimer in the documentation
  20. // and/or other materials provided with the distribution.
  21. // * Neither the name of the Steinberg Media Technologies nor the names of its
  22. // contributors may be used to endorse or promote products derived from this
  23. // software without specific prior written permission.
  24. //
  25. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  26. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  27. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  28. // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  29. // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  32. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  33. // OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  34. // OF THE POSSIBILITY OF SUCH DAMAGE.
  35. //-----------------------------------------------------------------------------
  36. /**
  37. **************************************
  38. \page vst2xwrapper VST 2.x Wrapper
  39. ***************************
  40. \section VST2Introduction Introduction
  41. ***************************
  42. The VST 3 SDK comes with a helper class which wraps one VST 3 Audio Processor and Edit Controller to
  43. a VST 2.x Plug-in.
  44. \n\n
  45. ***************************
  46. \section VST2howdoesitwork How does it work ?
  47. ***************************
  48. You just need to add public.sdk/source/vst/vst2wrapper/vst2wrapper.sdk.cpp to your project and add
  49. the following code somewhere in your sources:
  50. \code
  51. #include "public.sdk/source/vst/vst2wrapper/vst2wrapper.h"
  52. //------------------------------------------------------------------------
  53. ::AudioEffect* createEffectInstance (audioMasterCallback audioMaster)
  54. {
  55. return Steinberg::Vst::Vst2Wrapper::create (GetPluginFactory (), kAudioProcessorCID, kVst2UniqueID, audioMaster);
  56. }
  57. \endcode
  58. */
  59. /// \cond ignore
  60. #pragma once
  61. #include "pluginterfaces/base/ftypes.h"
  62. #include "pluginterfaces/vst/ivstaudioprocessor.h"
  63. #include "pluginterfaces/vst/ivsteditcontroller.h"
  64. #include "pluginterfaces/vst/ivsthostapplication.h"
  65. #include "pluginterfaces/vst/ivstprocesscontext.h"
  66. #include "pluginterfaces/vst/ivstunits.h"
  67. #include "public.sdk/source/common/memorystream.h"
  68. #include "public.sdk/source/vst/hosting/eventlist.h"
  69. #include "public.sdk/source/vst/hosting/parameterchanges.h"
  70. #include "public.sdk/source/vst/hosting/processdata.h"
  71. #include "public.sdk/source/vst2.x/audioeffectx.h"
  72. #include "base/source/fstring.h"
  73. #include "base/source/timer.h"
  74. #include <map>
  75. #include <vector>
  76. //------------------------------------------------------------------------
  77. namespace Steinberg {
  78. namespace Vst {
  79. class Vst2MidiEventQueue;
  80. //-------------------------------------------------------------------------------------------------------
  81. class Vst2Wrapper : public ::AudioEffectX,
  82. public IHostApplication,
  83. public IComponentHandler,
  84. public IUnitHandler,
  85. public ITimerCallback,
  86. public IVst3ToVst2Wrapper
  87. {
  88. public:
  89. //--- -------------------------------------------------------------------------------------------------
  90. // static creation method
  91. static AudioEffect* create (IPluginFactory* factory, const TUID vst3ComponentID,
  92. VstInt32 vst2ID, audioMasterCallback audioMaster);
  93. Vst2Wrapper (IAudioProcessor* processor, IEditController* controller,
  94. audioMasterCallback audioMaster, const TUID vst3ComponentID, VstInt32 vst2ID,
  95. IPluginFactory* factory = 0);
  96. ~Vst2Wrapper ();
  97. bool init ();
  98. // AudioEffectX overrides -----------------------------------------------
  99. virtual void suspend () SMTG_OVERRIDE; // Called when Plug-in is switched to off
  100. virtual void resume () SMTG_OVERRIDE; // Called when Plug-in is switched to on
  101. virtual VstInt32 startProcess () SMTG_OVERRIDE;
  102. virtual VstInt32 stopProcess () SMTG_OVERRIDE;
  103. virtual void setSampleRate (float newSamplerate)
  104. SMTG_OVERRIDE; // Called when the sample rate changes (always in a suspend state)
  105. virtual void setBlockSize (VstInt32 newBlockSize)
  106. SMTG_OVERRIDE; // Called when the maximum block size changes
  107. // (always in a suspend state). Note that the
  108. // sampleFrames in Process Calls could be
  109. // smaller than this block size, but NOT bigger.
  110. virtual float getParameter (VstInt32 index) SMTG_OVERRIDE;
  111. virtual void setParameter (VstInt32 index, float value) SMTG_OVERRIDE;
  112. virtual void setProgram (VstInt32 program) SMTG_OVERRIDE;
  113. virtual void setProgramName (char* name) SMTG_OVERRIDE;
  114. virtual void getProgramName (char* name) SMTG_OVERRIDE;
  115. virtual bool getProgramNameIndexed (VstInt32 category, VstInt32 index,
  116. char* text) SMTG_OVERRIDE;
  117. virtual void getParameterLabel (VstInt32 index, char* label) SMTG_OVERRIDE;
  118. virtual void getParameterDisplay (VstInt32 index, char* text) SMTG_OVERRIDE;
  119. virtual void getParameterName (VstInt32 index, char* text) SMTG_OVERRIDE;
  120. virtual bool canParameterBeAutomated (VstInt32 index) SMTG_OVERRIDE;
  121. virtual bool string2parameter (VstInt32 index, char* text) SMTG_OVERRIDE;
  122. virtual bool getParameterProperties (VstInt32 index, VstParameterProperties* p) SMTG_OVERRIDE;
  123. virtual VstInt32 getChunk (void** data, bool isPreset = false) SMTG_OVERRIDE;
  124. virtual VstInt32 setChunk (void* data, VstInt32 byteSize, bool isPreset = false) SMTG_OVERRIDE;
  125. virtual bool getInputProperties (VstInt32 index, VstPinProperties* properties) SMTG_OVERRIDE;
  126. virtual bool getOutputProperties (VstInt32 index, VstPinProperties* properties) SMTG_OVERRIDE;
  127. virtual bool setSpeakerArrangement (VstSpeakerArrangement* pluginInput,
  128. VstSpeakerArrangement* pluginOutput) SMTG_OVERRIDE;
  129. virtual bool getSpeakerArrangement (VstSpeakerArrangement** pluginInput,
  130. VstSpeakerArrangement** pluginOutput) SMTG_OVERRIDE;
  131. virtual bool setBypass (bool onOff) SMTG_OVERRIDE;
  132. virtual bool setProcessPrecision (VstInt32 precision) SMTG_OVERRIDE;
  133. virtual VstInt32 getNumMidiInputChannels () SMTG_OVERRIDE;
  134. virtual VstInt32 getNumMidiOutputChannels () SMTG_OVERRIDE;
  135. virtual VstInt32 getGetTailSize () SMTG_OVERRIDE;
  136. virtual bool getEffectName (char* name) SMTG_OVERRIDE;
  137. virtual bool getVendorString (char* text) SMTG_OVERRIDE;
  138. virtual VstInt32 getVendorVersion () SMTG_OVERRIDE;
  139. virtual VstIntPtr vendorSpecific (VstInt32 lArg, VstIntPtr lArg2, void* ptrArg,
  140. float floatArg) SMTG_OVERRIDE;
  141. virtual VstPlugCategory getPlugCategory () SMTG_OVERRIDE;
  142. virtual VstInt32 canDo (char* text) SMTG_OVERRIDE;
  143. virtual VstInt32 getMidiProgramName (VstInt32 channel,
  144. MidiProgramName* midiProgramName) SMTG_OVERRIDE;
  145. virtual VstInt32 getCurrentMidiProgram (VstInt32 channel,
  146. MidiProgramName* currentProgram) SMTG_OVERRIDE;
  147. virtual VstInt32 getMidiProgramCategory (VstInt32 channel,
  148. MidiProgramCategory* category) SMTG_OVERRIDE;
  149. virtual bool hasMidiProgramsChanged (VstInt32 channel) SMTG_OVERRIDE;
  150. virtual bool getMidiKeyName (VstInt32 channel, MidiKeyName* keyName) SMTG_OVERRIDE;
  151. // finally process...
  152. virtual void processReplacing (float** inputs, float** outputs,
  153. VstInt32 sampleFrames) SMTG_OVERRIDE;
  154. virtual void processDoubleReplacing (double** inputs, double** outputs,
  155. VstInt32 sampleFrames) SMTG_OVERRIDE;
  156. virtual VstInt32 processEvents (VstEvents* events) SMTG_OVERRIDE;
  157. // VST 3 Interfaces ------------------------------------------------------
  158. // FUnknown
  159. virtual tresult PLUGIN_API queryInterface (const char* iid, void** obj) SMTG_OVERRIDE;
  160. virtual uint32 PLUGIN_API addRef () SMTG_OVERRIDE { return 1; }
  161. virtual uint32 PLUGIN_API release () SMTG_OVERRIDE { return 1; }
  162. // IHostApplication
  163. virtual tresult PLUGIN_API getName (String128 name) SMTG_OVERRIDE;
  164. virtual tresult PLUGIN_API createInstance (TUID cid, TUID iid, void** obj) SMTG_OVERRIDE;
  165. // IComponentHandler
  166. virtual tresult PLUGIN_API beginEdit (ParamID tag) SMTG_OVERRIDE;
  167. virtual tresult PLUGIN_API performEdit (ParamID tag, ParamValue valueNormalized) SMTG_OVERRIDE;
  168. virtual tresult PLUGIN_API endEdit (ParamID tag) SMTG_OVERRIDE;
  169. virtual tresult PLUGIN_API restartComponent (int32 flags) SMTG_OVERRIDE;
  170. // IUnitHandler
  171. virtual tresult PLUGIN_API notifyUnitSelection (UnitID unitId) SMTG_OVERRIDE;
  172. virtual tresult PLUGIN_API notifyProgramListChange (ProgramListID listId,
  173. int32 programIndex) SMTG_OVERRIDE;
  174. void setVendorName (char* name);
  175. void setEffectName (char* name);
  176. void setEffectVersion (char* version);
  177. void setSubCategories (char* string);
  178. // ITimer
  179. virtual void onTimer (Timer* timer) SMTG_OVERRIDE;
  180. //-------------------------------------------------------------------------------------------------------
  181. protected:
  182. void setupBuses ();
  183. void setupParameters ();
  184. void initMidiCtrlerAssignment ();
  185. void getUnitPath (UnitID unitID, String& path);
  186. int32 countMainBusChannels (BusDirection dir, uint64& mainBusBitset);
  187. template <class T>
  188. void setProcessingBuffers (T** inputs, T** outputs);
  189. void setupProcessTimeInfo ();
  190. void doProcess (VstInt32 sampleFrames);
  191. void setEventPPQPositions ();
  192. void processOutputEvents ();
  193. void processMidiEvent (VstMidiEvent* midiEvent, int32 bus);
  194. /** Returns the last param change from guiTransfer queue. */
  195. bool getLastParamChange (ParamID id, ParamValue& value);
  196. bool setupProcessing (int32 processModeOverwrite = -1);
  197. void addParameterChange (ParamID id, ParamValue value, int32 sampleOffset);
  198. bool getProgramListAndUnit (int32 midiChannel, UnitID& unitId, ProgramListID& programListId);
  199. bool getProgramListInfoByProgramListID (ProgramListID programListId, ProgramListInfo& info);
  200. int32 lookupProgramCategory (int32 midiChannel, String128 instrumentAttribute);
  201. bool setupMidiProgram (int32 midiChannel, ProgramListID programListId,
  202. MidiProgramName& midiProgramName);
  203. bool getPinProperties (BusDirection dir, VstInt32 pinIndex, VstPinProperties* properties);
  204. bool pinIndexToBusChannel (BusDirection dir, VstInt32 pinIndex, int32& busIndex,
  205. int32& busChannel);
  206. static VstInt32 vst3ToVst2SpeakerArr (SpeakerArrangement vst3Arr);
  207. static SpeakerArrangement vst2ToVst3SpeakerArr (VstInt32 vst2Arr);
  208. static VstInt32 vst3ToVst2Speaker (Speaker vst3Speaker);
  209. static void setupVst2Arrangement (VstSpeakerArrangement*& vst2arr,
  210. Vst::SpeakerArrangement vst3Arrangement);
  211. struct ProgramCategory
  212. {
  213. MidiProgramCategory vst2Category;
  214. String128 vst3InstrumentAttribute;
  215. };
  216. std::vector<std::vector<ProgramCategory>> mProgramCategories;
  217. void setupProgramCategories ();
  218. static uint32 makeCategoriesRecursive (std::vector<ProgramCategory>& channelCategories,
  219. String128 vst3Category);
  220. static const int32 kMaxProgramChangeParameters = 16;
  221. ParamID mProgramChangeParameterIDs[kMaxProgramChangeParameters]; // for each midi channel
  222. int32 mProgramChangeParameterIdxs[kMaxProgramChangeParameters]; // for each midi channel
  223. VstSpeakerArrangement* mVst2InputArrangement;
  224. VstSpeakerArrangement* mVst2OutputArrangement;
  225. FUID mVst3EffectClassID;
  226. // vst3 data
  227. IAudioProcessor* mProcessor;
  228. IComponent* mComponent;
  229. IEditController* mController;
  230. IUnitInfo* mUnitInfo;
  231. IMidiMapping* mMidiMapping;
  232. bool componentInitialized;
  233. bool controllerInitialized;
  234. bool componentsConnected;
  235. bool processing;
  236. bool hasEventInputBuses;
  237. bool hasEventOutputBuses;
  238. int32 mVst3SampleSize;
  239. int32 mVst3processMode;
  240. char mName[PClassInfo::kNameSize];
  241. char mVendor[PFactoryInfo::kNameSize];
  242. char mSubCategories[PClassInfo2::kSubCategoriesSize];
  243. int32 mVersion;
  244. struct ParamMapEntry
  245. {
  246. ParamID vst3ID;
  247. int32 vst3Index;
  248. };
  249. std::vector<ParamMapEntry> mParameterMap;
  250. std::map<ParamID, int32> mParamIndexMap;
  251. ParamID mBypassParameterID;
  252. ParamID mProgramParameterID;
  253. int32 mProgramParameterIdx;
  254. HostProcessData mProcessData;
  255. ProcessContext mProcessContext;
  256. ParameterChanges mInputChanges;
  257. ParameterChanges mOutputChanges;
  258. EventList* mInputEvents;
  259. EventList* mOutputEvents;
  260. Vst2MidiEventQueue* mVst2OutputEvents;
  261. uint64 mMainAudioInputBuses;
  262. uint64 mMainAudioOutputBuses;
  263. ParameterChangeTransfer mInputTransfer;
  264. ParameterChangeTransfer mOutputTransfer;
  265. ParameterChangeTransfer mGuiTransfer;
  266. MemoryStream mChunk;
  267. Timer* mTimer;
  268. IPluginFactory* mFactory;
  269. enum
  270. {
  271. kMaxMidiMappingBusses = 4
  272. };
  273. int32* mMidiCCMapping[kMaxMidiMappingBusses][16];
  274. };
  275. //------------------------------------------------------------------------
  276. } // namespace Vst
  277. } // namespace Steinberg
  278. /** Must be implemented externally. */
  279. extern ::AudioEffect* createEffectInstance (audioMasterCallback audioMaster);
  280. /// \endcond