Collection of tools useful for audio production
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.

282 lines
6.9KB

  1. /*
  2. * Carla Backend
  3. * Copyright (C) 2012 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the COPYING file
  16. */
  17. #include "carla_plugin.h"
  18. #include "plugins/carla_native.h"
  19. CARLA_BACKEND_START_NAMESPACE
  20. class NativePluginScopedInitiliazer
  21. {
  22. public:
  23. NativePluginScopedInitiliazer()
  24. {
  25. }
  26. ~NativePluginScopedInitiliazer()
  27. {
  28. for (size_t i=0; i < descriptors.size(); i++)
  29. {
  30. const PluginDescriptor* const desc = descriptors[i];
  31. if (desc->_fini)
  32. desc->_fini((struct _PluginDescriptor*)desc);
  33. }
  34. descriptors.clear();
  35. }
  36. void initializeIfNeeded(const PluginDescriptor* const desc)
  37. {
  38. if (descriptors.empty() || std::find(descriptors.begin(), descriptors.end(), desc) == descriptors.end())
  39. {
  40. if (desc->_init)
  41. desc->_init((struct _PluginDescriptor*)desc);
  42. descriptors.push_back(desc);
  43. }
  44. }
  45. private:
  46. std::vector<const PluginDescriptor*> descriptors;
  47. };
  48. static NativePluginScopedInitiliazer scopedInitliazer;
  49. class NativePlugin : public CarlaPlugin
  50. {
  51. public:
  52. NativePlugin(CarlaEngine* const engine, const unsigned short id)
  53. : CarlaPlugin(engine, id)
  54. {
  55. qDebug("NativePlugin::NativePlugin()");
  56. m_type = PLUGIN_INTERNAL;
  57. descriptor = nullptr;
  58. handle = nullptr;
  59. }
  60. ~NativePlugin()
  61. {
  62. qDebug("NativePlugin::~NativePlugin()");
  63. if (handle)
  64. pass();
  65. }
  66. // -------------------------------------------------------------------
  67. // Information (base)
  68. PluginCategory category()
  69. {
  70. Q_ASSERT(descriptor);
  71. if (descriptor)
  72. return (PluginCategory)descriptor->category;
  73. return getPluginCategoryFromName(m_name);
  74. }
  75. // -------------------------------------------------------------------
  76. // Information (per-plugin data)
  77. double getParameterValue(const uint32_t parameterId)
  78. {
  79. Q_ASSERT(descriptor);
  80. Q_ASSERT(parameterId < param.count);
  81. //return descriptor->get_parameter_value();
  82. return 0.0;
  83. }
  84. void getLabel(char* const strBuf)
  85. {
  86. Q_ASSERT(descriptor);
  87. if (descriptor && descriptor->label)
  88. strncpy(strBuf, descriptor->label, STR_MAX);
  89. else
  90. CarlaPlugin::getLabel(strBuf);
  91. }
  92. void getMaker(char* const strBuf)
  93. {
  94. Q_ASSERT(descriptor);
  95. if (descriptor && descriptor->maker)
  96. strncpy(strBuf, descriptor->maker, STR_MAX);
  97. else
  98. CarlaPlugin::getMaker(strBuf);
  99. }
  100. void getCopyright(char* const strBuf)
  101. {
  102. Q_ASSERT(descriptor);
  103. if (descriptor && descriptor->copyright)
  104. strncpy(strBuf, descriptor->copyright, STR_MAX);
  105. else
  106. CarlaPlugin::getCopyright(strBuf);
  107. }
  108. void getRealName(char* const strBuf)
  109. {
  110. Q_ASSERT(descriptor);
  111. if (descriptor && descriptor->name)
  112. strncpy(strBuf, descriptor->name, STR_MAX);
  113. else
  114. CarlaPlugin::getRealName(strBuf);
  115. }
  116. void getParameterName(const uint32_t parameterId, char* const strBuf)
  117. {
  118. Q_ASSERT(descriptor);
  119. Q_ASSERT(parameterId < param.count);
  120. int32_t rindex = param.data[parameterId].rindex;
  121. if (descriptor && rindex < (int32_t)descriptor->portCount)
  122. strncpy(strBuf, descriptor->ports[rindex].name, STR_MAX);
  123. else
  124. CarlaPlugin::getParameterName(parameterId, strBuf);
  125. }
  126. // -------------------------------------------------------------------
  127. static size_t getPluginCount()
  128. {
  129. return pluginDescriptors.size();
  130. }
  131. static const PluginDescriptor* getPlugin(size_t index)
  132. {
  133. Q_ASSERT(index < pluginDescriptors.size());
  134. if (index < pluginDescriptors.size())
  135. return pluginDescriptors[index];
  136. return nullptr;
  137. }
  138. static void registerPlugin(const PluginDescriptor* desc)
  139. {
  140. pluginDescriptors.push_back(desc);
  141. }
  142. // -------------------------------------------------------------------
  143. bool init(const char* const name, const char* const label)
  144. {
  145. // ---------------------------------------------------------------
  146. // get plugin
  147. for (size_t i=0; i < pluginDescriptors.size(); i++)
  148. {
  149. descriptor = pluginDescriptors[i];
  150. if (! descriptor)
  151. break;
  152. if (strcmp(descriptor->label, label) == 0)
  153. break;
  154. descriptor = nullptr;
  155. }
  156. if (! descriptor)
  157. {
  158. setLastError("Invalid internal plugin");
  159. return false;
  160. }
  161. scopedInitliazer.initializeIfNeeded(descriptor);
  162. // ---------------------------------------------------------------
  163. // get info
  164. if (name)
  165. m_name = x_engine->getUniqueName(name);
  166. else
  167. m_name = x_engine->getUniqueName(descriptor->name);
  168. // ---------------------------------------------------------------
  169. // register client
  170. x_client = x_engine->addClient(this);
  171. if (! x_client->isOk())
  172. {
  173. setLastError("Failed to register plugin client");
  174. return false;
  175. }
  176. return true;
  177. }
  178. private:
  179. const PluginDescriptor* descriptor;
  180. PluginHandle handle;
  181. static std::vector<const PluginDescriptor*> pluginDescriptors;
  182. };
  183. std::vector<const PluginDescriptor*> NativePlugin::pluginDescriptors;
  184. CarlaPlugin* CarlaPlugin::newNative(const initializer& init)
  185. {
  186. qDebug("CarlaPlugin::newNative(%p, \"%s\", \"%s\", \"%s\")", init.engine, init.filename, init.name, init.label);
  187. short id = init.engine->getNewPluginId();
  188. if (id < 0 || id > CarlaEngine::maxPluginNumber())
  189. {
  190. setLastError("Maximum number of plugins reached");
  191. return nullptr;
  192. }
  193. NativePlugin* const plugin = new NativePlugin(init.engine, id);
  194. if (! plugin->init(init.name, init.label))
  195. {
  196. delete plugin;
  197. return nullptr;
  198. }
  199. plugin->reload();
  200. plugin->registerToOsc();
  201. return plugin;
  202. }
  203. size_t CarlaPlugin::getNativePluginCount()
  204. {
  205. return NativePlugin::getPluginCount();
  206. }
  207. const PluginDescriptor* CarlaPlugin::getNativePlugin(size_t index)
  208. {
  209. return NativePlugin::getPlugin(index);
  210. }
  211. CARLA_BACKEND_END_NAMESPACE
  212. void carla_register_native_plugin(const PluginDescriptor* desc)
  213. {
  214. CarlaBackend::NativePlugin::registerPlugin(desc);
  215. }