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.

298 lines
8.0KB

  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 <QtCore/QMutex>
  19. #include <QtCore/QString>
  20. // Global variables (shared)
  21. const char* unique_names[MAX_PLUGINS] = { nullptr };
  22. CarlaPlugin* CarlaPlugins[MAX_PLUGINS] = { nullptr };
  23. volatile double ains_peak[MAX_PLUGINS*2] = { 0.0 };
  24. volatile double aouts_peak[MAX_PLUGINS*2] = { 0.0 };
  25. // Global options
  26. carla_options_t carla_options = {
  27. /* initiated */ false,
  28. #ifdef BUILD_BRIDGE
  29. /* global_jack_client */ false,
  30. /* use_dssi_chunks */ false,
  31. /* prefer_ui_bridges */ false,
  32. #else
  33. /* global_jack_client */ true,
  34. /* use_dssi_chunks */ false,
  35. /* prefer_ui_bridges */ true,
  36. #endif
  37. /* bridge_unix32 */ nullptr,
  38. /* bridge_unix64 */ nullptr,
  39. /* bridge_win32 */ nullptr,
  40. /* bridge_win64 */ nullptr
  41. };
  42. CallbackFunc Callback = nullptr;
  43. const char* last_error = nullptr;
  44. QMutex carla_proc_lock_var;
  45. QMutex carla_midi_lock_var;
  46. const unsigned short max_client_name_size = 32 - 5; // 32 = jack1 max name; 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* binarytype2str(BinaryType type)
  69. {
  70. switch (type)
  71. {
  72. case BINARY_UNIX32:
  73. return carla_options.bridge_unix32;
  74. case BINARY_UNIX64:
  75. return carla_options.bridge_unix64;
  76. case BINARY_WIN32:
  77. return carla_options.bridge_win32;
  78. case BINARY_WIN64:
  79. return carla_options.bridge_win64;
  80. default:
  81. return nullptr;
  82. }
  83. }
  84. const char* plugintype2str(PluginType type)
  85. {
  86. switch (type)
  87. {
  88. case PLUGIN_LADSPA:
  89. return "LADSPA";
  90. case PLUGIN_DSSI:
  91. return "DSSI";
  92. case PLUGIN_LV2:
  93. return "LV2";
  94. case PLUGIN_VST:
  95. return "VST";
  96. case PLUGIN_SF2:
  97. return "SF2";
  98. default:
  99. return "Unknown";
  100. }
  101. }
  102. // -------------------------------------------------------------------------------------------------------------------
  103. short get_new_plugin_id()
  104. {
  105. for (unsigned short i=0; i<MAX_PLUGINS; i++)
  106. {
  107. if (CarlaPlugins[i] == nullptr)
  108. return i;
  109. }
  110. return -1;
  111. }
  112. const char* get_unique_name(const char* name)
  113. {
  114. QString qname(name);
  115. if (qname.isEmpty())
  116. qname = "(No name)";
  117. qname.truncate(max_client_name_size);
  118. //qname.replace(":", "."); // ":" is used in JACK to split client/port names
  119. for (unsigned short i=0; i<MAX_PLUGINS; i++)
  120. {
  121. // Check if unique name already exists
  122. if (unique_names[i] && qname == unique_names[i])
  123. {
  124. // Check if string has already been modified
  125. uint len = qname.size();
  126. if (qname.at(len-3) == QChar('(') && qname.at(len-2).isDigit() && qname.at(len-1) == QChar(')'))
  127. {
  128. int number = qname.at(len-2).toAscii()-'0';
  129. if (number == 9)
  130. // next number is 10, 2 digits
  131. qname.replace(" (9)", " (10)");
  132. else
  133. qname[len-2] = QChar('0'+number+1);
  134. continue;
  135. }
  136. else if (qname.at(len-4) == QChar('(') && qname.at(len-3).isDigit() && qname.at(len-2).isDigit() && qname.at(len-1) == QChar(')'))
  137. {
  138. QChar n2 = qname.at(len-2);
  139. QChar n3 = qname.at(len-3);
  140. if (n2 == QChar('9'))
  141. {
  142. n2 = QChar('0');
  143. n3 = QChar(n3.toAscii()+1);
  144. }
  145. else
  146. n2 = QChar(n2.toAscii()+1);
  147. qname[len-2] = n2;
  148. qname[len-3] = n3;
  149. continue;
  150. }
  151. // Modify string if not
  152. qname += " (2)";
  153. }
  154. }
  155. return strdup(qname.toUtf8().constData());
  156. }
  157. PluginCategory get_category_from_name(const char* name)
  158. {
  159. QString qname(name);
  160. if (qname.isEmpty())
  161. return PLUGIN_CATEGORY_NONE;
  162. else
  163. qname = qname.toLower();
  164. // generic tags first
  165. if (qname.contains("delay", Qt::CaseSensitive))
  166. return PLUGIN_CATEGORY_DELAY;
  167. if (qname.contains("reverb", Qt::CaseSensitive))
  168. return PLUGIN_CATEGORY_DELAY;
  169. if (qname.contains("filter", Qt::CaseSensitive))
  170. return PLUGIN_CATEGORY_FILTER;
  171. if (qname.contains("dynamics", Qt::CaseSensitive))
  172. return PLUGIN_CATEGORY_DYNAMICS;
  173. if (qname.contains("amplifier", Qt::CaseSensitive))
  174. return PLUGIN_CATEGORY_DYNAMICS;
  175. if (qname.contains("compressor", Qt::CaseSensitive))
  176. return PLUGIN_CATEGORY_DYNAMICS;
  177. if (qname.contains("enhancer", Qt::CaseSensitive))
  178. return PLUGIN_CATEGORY_DYNAMICS;
  179. if (qname.contains("exciter", Qt::CaseSensitive))
  180. return PLUGIN_CATEGORY_DYNAMICS;
  181. if (qname.contains("gate", Qt::CaseSensitive))
  182. return PLUGIN_CATEGORY_DYNAMICS;
  183. if (qname.contains("limiter", Qt::CaseSensitive))
  184. return PLUGIN_CATEGORY_DYNAMICS;
  185. if (qname.contains("modulator", Qt::CaseSensitive))
  186. return PLUGIN_CATEGORY_MODULATOR;
  187. if (qname.contains("chorus", Qt::CaseSensitive))
  188. return PLUGIN_CATEGORY_MODULATOR;
  189. if (qname.contains("flanger", Qt::CaseSensitive))
  190. return PLUGIN_CATEGORY_MODULATOR;
  191. if (qname.contains("phaser", Qt::CaseSensitive))
  192. return PLUGIN_CATEGORY_MODULATOR;
  193. if (qname.contains("saturator", Qt::CaseSensitive))
  194. return PLUGIN_CATEGORY_MODULATOR;
  195. if (qname.contains("utility", Qt::CaseSensitive))
  196. return PLUGIN_CATEGORY_UTILITY;
  197. if (qname.contains("analyzer", Qt::CaseSensitive))
  198. return PLUGIN_CATEGORY_UTILITY;
  199. if (qname.contains("converter", Qt::CaseSensitive))
  200. return PLUGIN_CATEGORY_UTILITY;
  201. if (qname.contains("deesser", Qt::CaseSensitive))
  202. return PLUGIN_CATEGORY_UTILITY;
  203. if (qname.contains("mixer", Qt::CaseSensitive))
  204. return PLUGIN_CATEGORY_UTILITY;
  205. // common tags
  206. if (qname.contains("verb", Qt::CaseSensitive))
  207. return PLUGIN_CATEGORY_DELAY;
  208. if (qname.contains("eq", Qt::CaseSensitive))
  209. return PLUGIN_CATEGORY_EQ;
  210. if (qname.contains("tool", Qt::CaseSensitive))
  211. return PLUGIN_CATEGORY_UTILITY;
  212. return PLUGIN_CATEGORY_NONE;
  213. }
  214. void* get_pointer(intptr_t ptr_addr)
  215. {
  216. intptr_t* ptr = (intptr_t*)ptr_addr;
  217. return (void*)ptr;
  218. }
  219. void set_last_error(const char* error)
  220. {
  221. if (last_error)
  222. free((void*)last_error);
  223. if (error)
  224. last_error = strdup(error);
  225. else
  226. last_error = nullptr;
  227. }
  228. void carla_proc_lock()
  229. {
  230. carla_proc_lock_var.lock();
  231. }
  232. void carla_proc_unlock()
  233. {
  234. carla_proc_lock_var.unlock();
  235. }
  236. void carla_midi_lock()
  237. {
  238. carla_midi_lock_var.lock();
  239. }
  240. void carla_midi_unlock()
  241. {
  242. carla_midi_lock_var.unlock();
  243. }
  244. void callback_action(CallbackType action, unsigned short plugin_id, int value1, int value2, double value3)
  245. {
  246. if (Callback)
  247. Callback(action, plugin_id, value1, value2, value3);
  248. }