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.

324 lines
8.6KB

  1. /*
  2. * JACK Backend code for Carla
  3. * Copyright (C) 2011-2012 Filipe Coelho <falktx@gmail.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_shared.h"
  18. #include "carla_engine.h"
  19. #include <QtCore/QMutex>
  20. #include <QtCore/QString>
  21. CARLA_BACKEND_START_NAMESPACE
  22. // Global variables (shared)
  23. const char* unique_names[MAX_PLUGINS] = { nullptr };
  24. CarlaPlugin* CarlaPlugins[MAX_PLUGINS] = { nullptr };
  25. volatile double ains_peak[MAX_PLUGINS*2] = { 0.0 };
  26. volatile double aouts_peak[MAX_PLUGINS*2] = { 0.0 };
  27. #ifndef BUILD_BRIDGE
  28. // Global options
  29. carla_options_t carla_options = {
  30. /* prefer_ui_bridges */ true,
  31. /* proccess_32x */ false,
  32. /* bridge_unix32 */ nullptr,
  33. /* bridge_unix64 */ nullptr,
  34. /* bridge_win32 */ nullptr,
  35. /* bridge_win64 */ nullptr,
  36. /* bridge_lv2gtk2 */ nullptr,
  37. /* bridge_lv2qt4 */ nullptr,
  38. /* bridge_lv2x11 */ nullptr
  39. };
  40. #endif
  41. CallbackFunc Callback = nullptr;
  42. const char* last_error = nullptr;
  43. QMutex carla_proc_lock_var;
  44. QMutex carla_midi_lock_var;
  45. // define max possible client name
  46. const unsigned short max_client_name_size = CarlaEngine::maxClientNameSize() - 5; // 5 = strlen(" (10)")
  47. // -------------------------------------------------------------------------------------------------------------------
  48. // Exported symbols (API)
  49. void set_callback_function(CallbackFunc func)
  50. {
  51. qDebug("set_callback_function(%p)", func);
  52. Callback = func;
  53. }
  54. const char* get_last_error()
  55. {
  56. qDebug("get_last_error()");
  57. return last_error;
  58. }
  59. // End of exported symbols (API)
  60. // -------------------------------------------------------------------------------------------------------------------
  61. const char* bool2str(bool yesno)
  62. {
  63. if (yesno)
  64. return "true";
  65. else
  66. return "false";
  67. }
  68. const char* plugintype2str(PluginType type)
  69. {
  70. switch (type)
  71. {
  72. case PLUGIN_LADSPA:
  73. return "LADSPA";
  74. case PLUGIN_DSSI:
  75. return "DSSI";
  76. case PLUGIN_LV2:
  77. return "LV2";
  78. case PLUGIN_VST:
  79. return "VST";
  80. case PLUGIN_SF2:
  81. return "SF2";
  82. default:
  83. return "Unknown";
  84. }
  85. }
  86. const char* binarytype2str(BinaryType type)
  87. {
  88. switch (type)
  89. {
  90. #ifndef BUILD_BRIDGE
  91. case BINARY_UNIX32:
  92. return carla_options.bridge_unix32;
  93. case BINARY_UNIX64:
  94. return carla_options.bridge_unix64;
  95. case BINARY_WIN32:
  96. return carla_options.bridge_win32;
  97. case BINARY_WIN64:
  98. return carla_options.bridge_win64;
  99. #endif
  100. default:
  101. return nullptr;
  102. }
  103. }
  104. const char* customdatatype2str(CustomDataType type)
  105. {
  106. switch (type)
  107. {
  108. case CUSTOM_DATA_INVALID:
  109. return "invalid";
  110. case CUSTOM_DATA_STRING:
  111. return "string";
  112. case CUSTOM_DATA_PATH:
  113. return "path";
  114. case CUSTOM_DATA_CHUNK:
  115. return "chunk";
  116. case CUSTOM_DATA_BINARY:
  117. return "binary";
  118. default:
  119. return "null";
  120. }
  121. }
  122. // -------------------------------------------------------------------------------------------------------------------
  123. short get_new_plugin_id()
  124. {
  125. for (unsigned short i=0; i<MAX_PLUGINS; i++)
  126. {
  127. if (CarlaPlugins[i] == nullptr)
  128. return i;
  129. }
  130. return -1;
  131. }
  132. const char* get_unique_name(const char* name)
  133. {
  134. QString qname(name);
  135. if (qname.isEmpty())
  136. qname = "(No name)";
  137. qname.truncate(max_client_name_size-1);
  138. qname.replace(":", "."); // ":" is used in JACK to split client/port names
  139. for (unsigned short i=0; i<MAX_PLUGINS; i++)
  140. {
  141. // Check if unique name already exists
  142. if (unique_names[i] && qname == unique_names[i])
  143. {
  144. // Check if string has already been modified
  145. uint len = qname.size();
  146. // 1 digit, ex: " (2)"
  147. if (qname.at(len-4) == QChar(' ') && qname.at(len-3) == QChar('(') && qname.at(len-2).isDigit() && qname.at(len-1) == QChar(')'))
  148. {
  149. int number = qname.at(len-2).toAscii()-'0';
  150. if (number == 9)
  151. // next number is 10, 2 digits
  152. qname.replace(" (9)", " (10)");
  153. else
  154. qname[len-2] = QChar('0'+number+1);
  155. continue;
  156. }
  157. // 2 digits, ex: " (11)"
  158. else if (qname.at(len-5) == QChar(' ') && qname.at(len-4) == QChar('(') && qname.at(len-3).isDigit() && qname.at(len-2).isDigit() && qname.at(len-1) == QChar(')'))
  159. {
  160. QChar n2 = qname.at(len-2);
  161. QChar n3 = qname.at(len-3);
  162. if (n2 == QChar('9'))
  163. {
  164. n2 = QChar('0');
  165. n3 = QChar(n3.toAscii()+1);
  166. }
  167. else
  168. n2 = QChar(n2.toAscii()+1);
  169. qname[len-2] = n2;
  170. qname[len-3] = n3;
  171. continue;
  172. }
  173. // Modify string if not
  174. qname += " (2)";
  175. }
  176. }
  177. return strdup(qname.toUtf8().constData());
  178. }
  179. PluginCategory get_category_from_name(const char* name)
  180. {
  181. QString qname(name);
  182. if (qname.isEmpty())
  183. return PLUGIN_CATEGORY_NONE;
  184. else
  185. qname = qname.toLower();
  186. // generic tags first
  187. if (qname.contains("delay", Qt::CaseSensitive))
  188. return PLUGIN_CATEGORY_DELAY;
  189. if (qname.contains("reverb", Qt::CaseSensitive))
  190. return PLUGIN_CATEGORY_DELAY;
  191. if (qname.contains("filter", Qt::CaseSensitive))
  192. return PLUGIN_CATEGORY_FILTER;
  193. if (qname.contains("dynamics", Qt::CaseSensitive))
  194. return PLUGIN_CATEGORY_DYNAMICS;
  195. if (qname.contains("amplifier", Qt::CaseSensitive))
  196. return PLUGIN_CATEGORY_DYNAMICS;
  197. if (qname.contains("compressor", Qt::CaseSensitive))
  198. return PLUGIN_CATEGORY_DYNAMICS;
  199. if (qname.contains("enhancer", Qt::CaseSensitive))
  200. return PLUGIN_CATEGORY_DYNAMICS;
  201. if (qname.contains("exciter", Qt::CaseSensitive))
  202. return PLUGIN_CATEGORY_DYNAMICS;
  203. if (qname.contains("gate", Qt::CaseSensitive))
  204. return PLUGIN_CATEGORY_DYNAMICS;
  205. if (qname.contains("limiter", Qt::CaseSensitive))
  206. return PLUGIN_CATEGORY_DYNAMICS;
  207. if (qname.contains("modulator", Qt::CaseSensitive))
  208. return PLUGIN_CATEGORY_MODULATOR;
  209. if (qname.contains("chorus", Qt::CaseSensitive))
  210. return PLUGIN_CATEGORY_MODULATOR;
  211. if (qname.contains("flanger", Qt::CaseSensitive))
  212. return PLUGIN_CATEGORY_MODULATOR;
  213. if (qname.contains("phaser", Qt::CaseSensitive))
  214. return PLUGIN_CATEGORY_MODULATOR;
  215. if (qname.contains("saturator", Qt::CaseSensitive))
  216. return PLUGIN_CATEGORY_MODULATOR;
  217. if (qname.contains("utility", Qt::CaseSensitive))
  218. return PLUGIN_CATEGORY_UTILITY;
  219. if (qname.contains("analyzer", Qt::CaseSensitive))
  220. return PLUGIN_CATEGORY_UTILITY;
  221. if (qname.contains("converter", Qt::CaseSensitive))
  222. return PLUGIN_CATEGORY_UTILITY;
  223. if (qname.contains("deesser", Qt::CaseSensitive))
  224. return PLUGIN_CATEGORY_UTILITY;
  225. if (qname.contains("mixer", Qt::CaseSensitive))
  226. return PLUGIN_CATEGORY_UTILITY;
  227. // common tags
  228. if (qname.contains("verb", Qt::CaseSensitive))
  229. return PLUGIN_CATEGORY_DELAY;
  230. if (qname.contains("eq", Qt::CaseSensitive))
  231. return PLUGIN_CATEGORY_EQ;
  232. if (qname.contains("tool", Qt::CaseSensitive))
  233. return PLUGIN_CATEGORY_UTILITY;
  234. return PLUGIN_CATEGORY_NONE;
  235. }
  236. void* get_pointer(quintptr ptr_addr)
  237. {
  238. quintptr* ptr = (quintptr*)ptr_addr;
  239. return (void*)ptr;
  240. }
  241. void set_last_error(const char* error)
  242. {
  243. if (last_error)
  244. free((void*)last_error);
  245. if (error)
  246. last_error = strdup(error);
  247. else
  248. last_error = nullptr;
  249. }
  250. void carla_proc_lock()
  251. {
  252. carla_proc_lock_var.lock();
  253. }
  254. void carla_proc_unlock()
  255. {
  256. carla_proc_lock_var.unlock();
  257. }
  258. void carla_midi_lock()
  259. {
  260. carla_midi_lock_var.lock();
  261. }
  262. void carla_midi_unlock()
  263. {
  264. carla_midi_lock_var.unlock();
  265. }
  266. void callback_action(CallbackType action, unsigned short plugin_id, int value1, int value2, double value3)
  267. {
  268. if (Callback)
  269. Callback(action, plugin_id, value1, value2, value3);
  270. }
  271. CARLA_BACKEND_END_NAMESPACE