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.

346 lines
15KB

  1. //------------------------------------------------------------------------
  2. // Project : VST SDK
  3. //
  4. // Category : Helpers
  5. // Filename : public.sdk/source/vst/vsteditcontroller.h
  6. // Created by : Steinberg, 04/2005
  7. // Description : VST Edit Controller Implementation
  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. #pragma once
  37. #include "public.sdk/source/vst/vstcomponentbase.h"
  38. #include "public.sdk/source/vst/vstparameters.h"
  39. #include "public.sdk/source/common/pluginview.h"
  40. #include "base/source/fstring.h"
  41. #include "pluginterfaces/vst/ivsteditcontroller.h"
  42. #include "pluginterfaces/vst/ivstunits.h"
  43. #include <vector>
  44. #include <map>
  45. //------------------------------------------------------------------------
  46. namespace Steinberg {
  47. namespace Vst {
  48. class EditorView;
  49. //------------------------------------------------------------------------
  50. /** Default implementation for a VST 3 edit controller.
  51. \ingroup vstClasses
  52. Can be used as base class for a specific controller implementation */
  53. //------------------------------------------------------------------------
  54. class EditController: public ComponentBase,
  55. public IEditController,
  56. public IEditController2
  57. {
  58. public:
  59. //------------------------------------------------------------------------
  60. EditController ();
  61. //---from IEditController-------
  62. virtual tresult PLUGIN_API setComponentState (IBStream* state) SMTG_OVERRIDE;
  63. virtual tresult PLUGIN_API setState (IBStream* state) SMTG_OVERRIDE;
  64. virtual tresult PLUGIN_API getState (IBStream* state) SMTG_OVERRIDE;
  65. virtual int32 PLUGIN_API getParameterCount () SMTG_OVERRIDE;
  66. virtual tresult PLUGIN_API getParameterInfo (int32 paramIndex, ParameterInfo& info) SMTG_OVERRIDE;
  67. virtual tresult PLUGIN_API getParamStringByValue (ParamID tag, ParamValue valueNormalized, String128 string) SMTG_OVERRIDE;
  68. virtual tresult PLUGIN_API getParamValueByString (ParamID tag, TChar* string, ParamValue& valueNormalized) SMTG_OVERRIDE;
  69. virtual ParamValue PLUGIN_API normalizedParamToPlain (ParamID tag, ParamValue valueNormalized) SMTG_OVERRIDE;
  70. virtual ParamValue PLUGIN_API plainParamToNormalized (ParamID tag, ParamValue plainValue) SMTG_OVERRIDE;
  71. virtual ParamValue PLUGIN_API getParamNormalized (ParamID tag) SMTG_OVERRIDE;
  72. virtual tresult PLUGIN_API setParamNormalized (ParamID tag, ParamValue value) SMTG_OVERRIDE;
  73. virtual tresult PLUGIN_API setComponentHandler (IComponentHandler* handler) SMTG_OVERRIDE;
  74. virtual IPlugView* PLUGIN_API createView (FIDString name) SMTG_OVERRIDE;
  75. //---from IEditController2-------
  76. virtual tresult PLUGIN_API setKnobMode (KnobMode mode) SMTG_OVERRIDE { hostKnobMode = mode; return kResultTrue; }
  77. virtual tresult PLUGIN_API openHelp (TBool /*onlyCheck*/) SMTG_OVERRIDE {return kResultFalse;}
  78. virtual tresult PLUGIN_API openAboutBox (TBool /*onlyCheck*/) SMTG_OVERRIDE {return kResultFalse;}
  79. //---from ComponentBase---------
  80. virtual tresult PLUGIN_API initialize (FUnknown* context) SMTG_OVERRIDE;
  81. virtual tresult PLUGIN_API terminate () SMTG_OVERRIDE;
  82. //---Internal Methods-------
  83. virtual tresult beginEdit (ParamID tag); ///< to be called before a serie of performEdit
  84. virtual tresult performEdit (ParamID tag, ParamValue valueNormalized); ///< will inform the host about the value change
  85. virtual tresult endEdit (ParamID tag); ///< to be called after a serie of performEdit
  86. virtual tresult startGroupEdit (); ///< calls IComponentHandler2::startGroupEdit() if host supports it
  87. virtual tresult finishGroupEdit (); ///< calls IComponentHandler2::finishGroupEdit() if host supports it
  88. virtual void editorDestroyed (EditorView* /*editor*/) {} ///< called from EditorView if it was destroyed
  89. virtual void editorAttached (EditorView* /*editor*/) {} ///< called from EditorView if it was attached to a parent
  90. virtual void editorRemoved (EditorView* /*editor*/) {} ///< called from EditorView if it was removed from a parent
  91. static KnobMode getHostKnobMode () { return hostKnobMode; } ///< return host knob mode
  92. /** Gets for a given tag the parameter object. */
  93. virtual Parameter* getParameterObject (ParamID tag) { return parameters.getParameter (tag); }
  94. /** Gets for a given tag the parameter information. */
  95. virtual tresult getParameterInfoByTag (ParamID tag, ParameterInfo& info);
  96. /** Calls IComponentHandler2::setDirty (state) if host supports it. */
  97. virtual tresult setDirty (TBool state);
  98. /** Calls IComponentHandler2::requestOpenEditor (name) if host supports it. */
  99. virtual tresult requestOpenEditor (FIDString name = ViewType::kEditor);
  100. //---Accessor Methods-------
  101. IComponentHandler* getComponentHandler () const { return componentHandler; }
  102. //---Interface---------
  103. OBJ_METHODS (EditController, ComponentBase)
  104. DEFINE_INTERFACES
  105. DEF_INTERFACE (IEditController)
  106. DEF_INTERFACE (IEditController2)
  107. END_DEFINE_INTERFACES (ComponentBase)
  108. REFCOUNT_METHODS(ComponentBase)
  109. //------------------------------------------------------------------------
  110. protected:
  111. IComponentHandler* componentHandler;
  112. IComponentHandler2* componentHandler2;
  113. ParameterContainer parameters;
  114. static KnobMode hostKnobMode;
  115. };
  116. //------------------------------------------------------------------------
  117. /** View related to an edit controller.
  118. \ingroup vstClasses */
  119. //------------------------------------------------------------------------
  120. class EditorView: public CPluginView
  121. {
  122. public:
  123. //------------------------------------------------------------------------
  124. EditorView (EditController* controller, ViewRect* size = 0);
  125. virtual ~EditorView ();
  126. /** Gets its controller part. */
  127. EditController* getController () { return controller; }
  128. //---from CPluginView-------------
  129. virtual void attachedToParent () SMTG_OVERRIDE;
  130. virtual void removedFromParent () SMTG_OVERRIDE;
  131. //------------------------------------------------------------------------
  132. protected:
  133. EditController* controller;
  134. };
  135. //------------------------------------------------------------------------
  136. /** Unit element.
  137. \ingroup vstClasses */
  138. //------------------------------------------------------------------------
  139. class Unit: public FObject
  140. {
  141. public:
  142. //------------------------------------------------------------------------
  143. Unit (const String128 name, UnitID unitId, UnitID parentUnitId = kRootUnitId, ProgramListID programListId = kNoProgramListId);
  144. Unit (const UnitInfo& unit);
  145. /** Returns its info. */
  146. const UnitInfo& getInfo () const {return info;}
  147. /** Returns its Unit ID. */
  148. UnitID getID () const {return info.id;}
  149. /** Sets a new Unit ID. */
  150. void setID (UnitID newID) {info.id = newID;}
  151. /** Returns its Unit Name. */
  152. const TChar* getName () const {return info.name;}
  153. /** Sets a new Unit Name. */
  154. void setName (const String128 newName);
  155. /** Returns its ProgramList ID. */
  156. ProgramListID getProgramListID () const {return info.programListId;}
  157. /** Sets a new ProgramList ID. */
  158. void setProgramListID (ProgramListID newID) {info.programListId = newID;}
  159. OBJ_METHODS (Unit, FObject)
  160. //------------------------------------------------------------------------
  161. protected:
  162. Unit ();
  163. UnitInfo info;
  164. };
  165. //------------------------------------------------------------------------
  166. /** ProgramList element.
  167. \ingroup vstClasses */
  168. //------------------------------------------------------------------------
  169. class ProgramList: public FObject
  170. {
  171. public:
  172. //------------------------------------------------------------------------
  173. ProgramList (const String128 name, ProgramListID listId, UnitID unitId);
  174. ProgramList (const ProgramList& programList);
  175. const ProgramListInfo& getInfo () const { return info; }
  176. ProgramListID getID () const { return info.id; }
  177. const TChar* getName () const { return info.name; }
  178. int32 getCount () const { return info.programCount; }
  179. virtual tresult getProgramName (int32 programIndex, String128 name /*out*/);
  180. virtual tresult setProgramName (int32 programIndex, const String128 name /*in*/);
  181. virtual tresult getProgramInfo (int32 programIndex, CString attributeId, String128 value /*out*/);
  182. virtual tresult hasPitchNames (int32 programIndex) { return kResultFalse; }
  183. virtual tresult getPitchName (int32 programIndex, int16 midiPitch, String128 name /*out*/) { return kResultFalse; }
  184. /** Adds a program to the end of the list. returns the index of the program. */
  185. virtual int32 addProgram (const String128 name);
  186. /** Sets a program attribute value. */
  187. virtual bool setProgramInfo (int32 programIndex, CString attributeId, const String128 value);
  188. /** Creates and returns the program parameter. */
  189. virtual Parameter* getParameter ();
  190. OBJ_METHODS (ProgramList, FObject)
  191. //------------------------------------------------------------------------
  192. protected:
  193. typedef std::map<String, String> StringMap;
  194. typedef std::vector<String> StringVector;
  195. typedef std::vector<StringMap> ProgramInfoVector;
  196. ProgramListInfo info;
  197. UnitID unitId;
  198. StringVector programNames;
  199. ProgramInfoVector programInfos;
  200. Parameter* parameter;
  201. };
  202. //------------------------------------------------------------------------
  203. /** ProgramListWithPitchNames element.
  204. \ingroup vstClasses */
  205. //-----------------------------------------------------------------------------
  206. class ProgramListWithPitchNames: public ProgramList
  207. {
  208. public:
  209. ProgramListWithPitchNames (const String128 name, ProgramListID listId, UnitID unitId);
  210. /** Sets a name for the given program index and a given pitch. */
  211. bool setPitchName (int32 programIndex, int16 pitch, const String128 pitchName);
  212. /** Removes the PitchName entry for the given program index and a given pitch. Returns true if it was found and removed. */
  213. bool removePitchName (int32 programIndex, int16 pitch);
  214. //---from ProgramList---------
  215. int32 addProgram (const String128 name) SMTG_OVERRIDE;
  216. tresult hasPitchNames (int32 programIndex) SMTG_OVERRIDE;
  217. tresult getPitchName (int32 programIndex, int16 midiPitch, String128 name /*out*/) SMTG_OVERRIDE;
  218. OBJ_METHODS (ProgramListWithPitchNames, ProgramList)
  219. protected:
  220. typedef std::map<int16, String> PitchNameMap;
  221. typedef std::vector<PitchNameMap> PitchNamesVector;
  222. PitchNamesVector pitchNames;
  223. };
  224. //------------------------------------------------------------------------
  225. /** Advanced implementation (support IUnitInfo) for a VST 3 edit controller.
  226. \ingroup vstClasses
  227. - [extends EditController]
  228. */
  229. //------------------------------------------------------------------------
  230. class EditControllerEx1: public EditController, public IUnitInfo
  231. {
  232. public:
  233. EditControllerEx1 ();
  234. virtual ~EditControllerEx1 ();
  235. /** Adds a given unit. */
  236. bool addUnit (Unit* unit);
  237. /** Adds a given program list. */
  238. bool addProgramList (ProgramList* list);
  239. /** Returns the ProgramList associated to a given listId. */
  240. ProgramList* getProgramList (ProgramListID listId) const;
  241. /** Notifies the host about program list changes. */
  242. tresult notifyProgramListChange (ProgramListID listId, int32 programIndex = kAllProgramInvalid);
  243. //---from IUnitInfo------------------
  244. virtual int32 PLUGIN_API getUnitCount () SMTG_OVERRIDE { return static_cast<int32> (units.size ()); }
  245. virtual tresult PLUGIN_API getUnitInfo (int32 unitIndex, UnitInfo& info /*out*/) SMTG_OVERRIDE;
  246. virtual int32 PLUGIN_API getProgramListCount () SMTG_OVERRIDE;
  247. virtual tresult PLUGIN_API getProgramListInfo (int32 listIndex, ProgramListInfo& info /*out*/) SMTG_OVERRIDE;
  248. virtual tresult PLUGIN_API getProgramName (ProgramListID listId, int32 programIndex, String128 name /*out*/) SMTG_OVERRIDE;
  249. virtual tresult PLUGIN_API getProgramInfo (ProgramListID listId, int32 programIndex, CString attributeId /*in*/, String128 attributeValue /*out*/) SMTG_OVERRIDE;
  250. virtual tresult PLUGIN_API hasProgramPitchNames (ProgramListID listId, int32 programIndex) SMTG_OVERRIDE;
  251. virtual tresult PLUGIN_API getProgramPitchName (ProgramListID listId, int32 programIndex, int16 midiPitch, String128 name /*out*/) SMTG_OVERRIDE;
  252. virtual tresult setProgramName (ProgramListID listId, int32 programIndex, const String128 name /*in*/);
  253. // units selection --------------------
  254. virtual UnitID PLUGIN_API getSelectedUnit () SMTG_OVERRIDE {return selectedUnit;}
  255. virtual tresult PLUGIN_API selectUnit (UnitID unitId) SMTG_OVERRIDE {selectedUnit = unitId; return kResultTrue;}
  256. virtual tresult PLUGIN_API getUnitByBus (MediaType /*type*/, BusDirection /*dir*/, int32 /*busIndex*/,
  257. int32 /*channel*/, UnitID& /*unitId*/ /*out*/) SMTG_OVERRIDE {return kResultFalse;}
  258. virtual tresult PLUGIN_API setUnitProgramData (int32 /*listOrUnitId*/, int32 /*programIndex*/, IBStream* /*data*/) SMTG_OVERRIDE {return kResultFalse;}
  259. /** Notifies the host about the selected Unit. */
  260. virtual tresult notifyUnitSelection ();
  261. //---from IDependent------------------
  262. virtual void PLUGIN_API update (FUnknown* changedUnknown, int32 message) SMTG_OVERRIDE;
  263. //---Interface---------
  264. OBJ_METHODS (EditControllerEx1, EditController)
  265. DEFINE_INTERFACES
  266. DEF_INTERFACE (IUnitInfo)
  267. END_DEFINE_INTERFACES (EditController)
  268. REFCOUNT_METHODS(EditController)
  269. protected:
  270. typedef std::vector<IPtr<ProgramList> > ProgramListVector;
  271. typedef std::map<ProgramListID, ProgramListVector::size_type> ProgramIndexMap;
  272. typedef std::vector<IPtr<Unit> > UnitVector;
  273. UnitVector units;
  274. ProgramListVector programLists;
  275. ProgramIndexMap programIndexMap;
  276. UnitID selectedUnit;
  277. };
  278. //------------------------------------------------------------------------
  279. } // namespace Vst
  280. } // namespace Steinberg