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.

267 lines
6.6KB

  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_NONE;
  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 void registerPlugin(const PluginDescriptor* desc)
  132. {
  133. pluginDescriptors.push_back(desc);
  134. }
  135. // -------------------------------------------------------------------
  136. bool init(const char* const name, const char* const label)
  137. {
  138. // ---------------------------------------------------------------
  139. // get plugin
  140. for (size_t i=0; i < pluginDescriptors.size(); i++)
  141. {
  142. descriptor = pluginDescriptors[i];
  143. if (! descriptor)
  144. break;
  145. if (strcmp(descriptor->label, label) == 0)
  146. break;
  147. descriptor = nullptr;
  148. }
  149. if (! descriptor)
  150. {
  151. setLastError("Invalid internal plugin");
  152. return false;
  153. }
  154. scopedInitliazer.initializeIfNeeded(descriptor);
  155. // ---------------------------------------------------------------
  156. // get info
  157. if (name)
  158. m_name = x_engine->getUniqueName(name);
  159. else
  160. m_name = x_engine->getUniqueName(descriptor->name);
  161. // ---------------------------------------------------------------
  162. // register client
  163. x_client = x_engine->addClient(this);
  164. if (! x_client->isOk())
  165. {
  166. setLastError("Failed to register plugin client");
  167. return false;
  168. }
  169. return true;
  170. }
  171. private:
  172. const PluginDescriptor* descriptor;
  173. PluginHandle handle;
  174. static std::vector<const PluginDescriptor*> pluginDescriptors;
  175. };
  176. std::vector<const PluginDescriptor*> NativePlugin::pluginDescriptors;
  177. CarlaPlugin* CarlaPlugin::newNative(const initializer& init)
  178. {
  179. qDebug("CarlaPlugin::newNative(%p, \"%s\", \"%s\", \"%s\")", init.engine, init.filename, init.name, init.label);
  180. short id = init.engine->getNewPluginId();
  181. if (id < 0 || id > CarlaEngine::maxPluginNumber())
  182. {
  183. setLastError("Maximum number of plugins reached");
  184. return nullptr;
  185. }
  186. NativePlugin* const plugin = new NativePlugin(init.engine, id);
  187. if (! plugin->init(init.name, init.label))
  188. {
  189. delete plugin;
  190. return nullptr;
  191. }
  192. plugin->reload();
  193. plugin->registerToOsc();
  194. return plugin;
  195. }
  196. size_t CarlaPlugin::getNativePluginCount()
  197. {
  198. return NativePlugin::getPluginCount();
  199. }
  200. CARLA_BACKEND_END_NAMESPACE
  201. void carla_register_native_plugin(const PluginDescriptor* desc)
  202. {
  203. CarlaBackend::NativePlugin::registerPlugin(desc);
  204. }