LadspaManager.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * LadspaManager.h - declaration of class ladspaManager
  3. * a class to manage loading and instantiation
  4. * of ladspa plugins
  5. *
  6. * Copyright (c) 2005-2008 Danny McRae <khjklujn@netscape.net>
  7. *
  8. * This file is part of LMMS - https://lmms.io
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2 of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public
  21. * License along with this program (see COPYING); if not, write to the
  22. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  23. * Boston, MA 02110-1301 USA.
  24. *
  25. */
  26. #ifndef LADSPA_MANAGER_H
  27. #define LADSPA_MANAGER_H
  28. #include <ladspa.h>
  29. #include <QtCore/QMap>
  30. #include <QtCore/QPair>
  31. #include <QtCore/QString>
  32. #include <QtCore/QStringList>
  33. #include "export.h"
  34. #include "lmms_basics.h"
  35. const float NOHINT = -99342.2243f;
  36. typedef QPair<QString, QString> ladspa_key_t;
  37. typedef QPair<QString, ladspa_key_t> sortable_plugin_t;
  38. typedef QList<sortable_plugin_t> l_sortable_plugin_t;
  39. typedef QList<ladspa_key_t> l_ladspa_key_t;
  40. /* ladspaManager provides a database of LADSPA plug-ins. Upon instantiation,
  41. it loads all of the plug-ins found in the LADSPA_PATH environmental variable
  42. and stores their access descriptors according in a dictionary keyed on
  43. the filename the plug-in was loaded from and the label of the plug-in.
  44. The can be retrieved by using ladspa_key_t. For example, to get the
  45. "Phase Modulated Voice" plug-in from the cmt library, you would perform the
  46. calls using:
  47. ladspa_key_t key( "cmt.so", "phasemod" )
  48. as the plug-in key. */
  49. enum ladspaPluginType
  50. {
  51. SOURCE,
  52. TRANSFER,
  53. VALID,
  54. INVALID,
  55. SINK,
  56. OTHER
  57. };
  58. typedef struct ladspaManagerStorage
  59. {
  60. LADSPA_Descriptor_Function descriptorFunction;
  61. uint32_t index;
  62. ladspaPluginType type;
  63. uint16_t inputChannels;
  64. uint16_t outputChannels;
  65. } ladspaManagerDescription;
  66. class EXPORT LadspaManager
  67. {
  68. public:
  69. LadspaManager();
  70. virtual ~LadspaManager();
  71. l_sortable_plugin_t getSortedPlugins();
  72. ladspaManagerDescription * getDescription( const ladspa_key_t &
  73. _plugin );
  74. /* This identifier can be used as a unique, case-sensitive
  75. identifier for the plugin type within the plugin file. Plugin
  76. types should be identified by file and label rather than by index
  77. or plugin name, which may be changed in new plugin
  78. versions. Labels must not contain white-space characters. */
  79. QString getLabel( const ladspa_key_t & _plugin );
  80. /* Indicates that the plugin has a real-time dependency
  81. (e.g. listens to a MIDI device) and so its output must not
  82. be cached or subject to significant latency. */
  83. bool hasRealTimeDependency( const ladspa_key_t & _plugin );
  84. /* Indicates that the plugin may cease to work correctly if the
  85. host elects to use the same data location for both input and output
  86. (see connectPort). */
  87. bool isInplaceBroken( const ladspa_key_t & _plugin );
  88. /* Indicates that the plugin is capable of running not only in a
  89. conventional host but also in a 'hard real-time' environment. */
  90. bool isRealTimeCapable( const ladspa_key_t & _plugin );
  91. /* Returns the name of the plug-in */
  92. QString getName( const ladspa_key_t & _plugin );
  93. /* Returns the the plug-in's author */
  94. QString getMaker( const ladspa_key_t & _plugin );
  95. /* Returns the copyright for the plug-in */
  96. QString getCopyright( const ladspa_key_t & _plugin );
  97. /* This indicates the number of ports (input AND output) present on
  98. the plugin. */
  99. uint32_t getPortCount( const ladspa_key_t & _plugin );
  100. /* Indicates that the port is an input. */
  101. bool isPortInput( const ladspa_key_t & _plugin, uint32_t _port );
  102. /* Indicates that the port is an output. */
  103. bool isPortOutput( const ladspa_key_t & _plugin, uint32_t _port );
  104. /* Indicates that the port is an audio. */
  105. bool isPortAudio( const ladspa_key_t & _plugin, uint32_t _port );
  106. /* Indicates that the port is an control. */
  107. bool isPortControl( const ladspa_key_t & _plugin, uint32_t _port );
  108. /* Indicates that any bounds specified should be interpreted as
  109. multiples of the sample rate. For instance, a frequency range from
  110. 0Hz to the Nyquist frequency (half the sample rate) could be requested
  111. by this hint in conjunction with LowerBound = 0 and UpperBound = 0.5.
  112. Hosts that support bounds at all must support this hint to retain
  113. meaning. */
  114. bool areHintsSampleRateDependent( const ladspa_key_t & _plugin,
  115. uint32_t _port );
  116. /* Returns the lower boundary value for the given port. If
  117. no lower bound is provided by the plug-in, returns -999e-99. When
  118. areHintsSampleRateDependent() is also true then this value should be
  119. multiplied by the relevant sample rate. */
  120. float getLowerBound( const ladspa_key_t & _plugin, uint32_t _port );
  121. /* Returns the upper boundary value for the given port. If
  122. no upper bound is provided by the plug-in, returns -999e-99. When
  123. areHintsSampleRateDependent() is also true then this value should be
  124. multiplied by the relevant sample rate. */
  125. float getUpperBound( const ladspa_key_t & _plugin, uint32_t _port );
  126. /* Indicates whether the given port should be considered 0 or 1
  127. boolean switch. */
  128. bool isPortToggled( const ladspa_key_t & _plugin, uint32_t _port );
  129. /* Retrieves any default setting hints offered by the plug-in for
  130. the given port. */
  131. float getDefaultSetting( const ladspa_key_t & _plugin, uint32_t _port );
  132. /* Indicates that it is likely that the user will find it more
  133. intuitive to view values using a logarithmic scale. This is
  134. particularly useful for frequencies and gains. */
  135. bool isLogarithmic( const ladspa_key_t & _plugin, uint32_t _port );
  136. /* Indicates that a user interface would probably wish to provide a
  137. stepped control taking only integer values. Any bounds set should be
  138. slightly wider than the actual integer range required to avoid floating
  139. point rounding errors. For instance, the integer set {0,1,2,3} might
  140. be described as [-0.1, 3.1]. */
  141. bool isInteger( const ladspa_key_t & _plugin, uint32_t _port );
  142. /* Returns the name of the port. */
  143. QString getPortName( const ladspa_key_t & _plugin, uint32_t _port );
  144. /* This may be used by the plugin developer to pass any custom
  145. implementation data into an instantiate call. It must not be used
  146. or interpreted by the host. It is expected that most plugin
  147. writers will not use this facility as LADSPA_Handle should be
  148. used to hold instance data. */
  149. const void * getImplementationData(
  150. const ladspa_key_t & _plugin );
  151. /* Returns a pointer to the plug-in's descriptor from which control
  152. of the plug-in is accessible */
  153. const LADSPA_Descriptor * getDescriptor(
  154. const ladspa_key_t & _plugin );
  155. /* The following methods are convenience functions for use during
  156. development. A real instrument should use the getDescriptor()
  157. method and implement the plug-in manipulations internally to avoid
  158. the overhead associated with QMap lookups. */
  159. /* Returns a handle to an instantiation of the given plug-in. */
  160. LADSPA_Handle instantiate( const ladspa_key_t & _plugin,
  161. uint32_t _sample_rate );
  162. /* This method calls a function pointer that connects a port on an
  163. instantiated plugin to a memory location at which a block of data
  164. for the port will be read/written. The data location is expected
  165. to be an array of LADSPA_Data for audio ports or a single
  166. LADSPA_Data value for control ports. Memory issues will be
  167. managed by the host. The plugin must read/write the data at these
  168. locations every time run() or runAdding() is called and the data
  169. present at the time of this connection call should not be
  170. considered meaningful.
  171. connectPort() may be called more than once for a plugin instance
  172. to allow the host to change the buffers that the plugin is
  173. reading or writing. These calls may be made before or after
  174. activate() or deactivate() calls.
  175. connectPort() must be called at least once for each port before
  176. run() or runAdding() is called. */
  177. bool connectPort( const ladspa_key_t & _plugin,
  178. LADSPA_Handle _instance,
  179. uint32_t _port,
  180. LADSPA_Data * _data_location );
  181. /* This method calls a function pointer that initialises a plugin
  182. instance and activates it for use. This is separated from
  183. instantiate() to aid real-time support and so that hosts can
  184. reinitialise a plugin instance by calling deactivate() and then
  185. activate(). In this case the plugin instance must reset all state
  186. information dependent on the history of the plugin instance
  187. except for any data locations provided by connectPort() and any
  188. gain set by setRunAddingGain(). If there is nothing for
  189. activate() to do then the plugin writer may provide a NULL rather
  190. than an empty function.
  191. When present, hosts must call this function once before run() (or
  192. runAdding()) is called for the first time. This call should be
  193. made as close to the run() call as possible and indicates to
  194. real-time plugins that they are now live. Plugins should not rely
  195. on a prompt call to run() after activate(). activate() may not be
  196. called again unless deactivate() is called first. Note that
  197. connectPort() may be called before or after a call to
  198. activate(). */
  199. bool activate( const ladspa_key_t & _plugin,
  200. LADSPA_Handle _instance );
  201. /* This method calls a function pointer that runs an instance of a
  202. plugin for a block. Two parameters are required: the first is a
  203. handle to the particular instance to be run and the second
  204. indicates the block size (in samples) for which the plugin
  205. instance may run.
  206. Note that if an activate() function exists then it must be called
  207. before run() or run_adding(). If deactivate() is called for a
  208. plugin instance then the plugin instance may not be reused until
  209. activate() has been called again. */
  210. bool run( const ladspa_key_t & _plugin,
  211. LADSPA_Handle _instance,
  212. uint32_t _sample_count );
  213. /* This method calls a function pointer that runs an instance of a
  214. plugin for a block. This has identical behaviour to run() except
  215. in the way data is output from the plugin. When run() is used,
  216. values are written directly to the memory areas associated with
  217. the output ports. However when runAdding() is called, values
  218. must be added to the values already present in the memory
  219. areas. Furthermore, output values written must be scaled by the
  220. current gain set by setRunAddingGain() (see below) before
  221. addition.
  222. runAdding() is optional. When it is not provided by a plugin,
  223. this function pointer must be set to NULL. When it is provided,
  224. the function setRunAddingGain() must be provided also. */
  225. bool runAdding( const ladspa_key_t & _plugin,
  226. LADSPA_Handle _instance,
  227. uint32_t _sample_count );
  228. /* This method calls a function pointer that sets the output gain for
  229. use when runAdding() is called (see above). If this function is
  230. never called the gain is assumed to default to 1. Gain
  231. information should be retained when activate() or deactivate()
  232. are called.
  233. This function should be provided by the plugin if and only if the
  234. runAdding() function is provided. When it is absent this
  235. function pointer must be set to NULL. */
  236. bool setRunAddingGain( const ladspa_key_t & _plugin,
  237. LADSPA_Handle _instance,
  238. LADSPA_Data _gain );
  239. /* This is the counterpart to activate() (see above). If there is
  240. nothing for deactivate() to do then the plugin writer may provide
  241. a NULL rather than an empty function.
  242. Hosts must deactivate all activated units after they have been
  243. run() (or run_adding()) for the last time. This call should be
  244. made as close to the last run() call as possible and indicates to
  245. real-time plugins that they are no longer live. Plugins should
  246. not rely on prompt deactivation. Note that connect_port() may be
  247. called before or after a call to deactivate().
  248. Deactivation is not similar to pausing as the plugin instance
  249. will be reinitialised when activate() is called to reuse it. */
  250. bool deactivate( const ladspa_key_t & _plugin,
  251. LADSPA_Handle _instance );
  252. /* Once an instance of a plugin has been finished with it can be
  253. deleted using the following function. The instance handle passed
  254. ceases to be valid after this call.
  255. If activate() was called for a plugin instance then a
  256. corresponding call to deactivate() must be made before cleanup()
  257. is called. */
  258. bool cleanup( const ladspa_key_t & _plugin,
  259. LADSPA_Handle _instance );
  260. private:
  261. void addPlugins( LADSPA_Descriptor_Function _descriptor_func,
  262. const QString & _file );
  263. uint16_t getPluginInputs( const LADSPA_Descriptor * _descriptor );
  264. uint16_t getPluginOutputs( const LADSPA_Descriptor * _descriptor );
  265. typedef QMap<ladspa_key_t, ladspaManagerDescription *>
  266. ladspaManagerMapType;
  267. ladspaManagerMapType m_ladspaManagerMap;
  268. l_sortable_plugin_t m_sortedPlugins;
  269. } ;
  270. #endif