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.

275 lines
6.7KB

  1. /*
  2. * Carla bridge code
  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. #ifndef CARLA_BRIDGE_CLIENT_H
  18. #define CARLA_BRIDGE_CLIENT_H
  19. #include "carla_bridge.h"
  20. #include "carla_bridge_osc.h"
  21. #include "carla_bridge_toolkit.h"
  22. #ifdef BUILD_BRIDGE_UI
  23. #include "carla_lib_includes.h"
  24. #endif
  25. #include <cstdio>
  26. #include <cstdint>
  27. #include <cstdlib>
  28. #include <QtCore/QMutex>
  29. CARLA_BRIDGE_START_NAMESPACE
  30. class CarlaBridgeClient
  31. {
  32. public:
  33. CarlaBridgeClient(CarlaBridgeToolkit* const toolkit) :
  34. m_toolkit(toolkit),
  35. #ifdef BUILD_BRIDGE_PLUGIN
  36. m_osc(this, "lv2-plugin-bridge")
  37. #else
  38. m_osc(this, "lv2-ui-bridge")
  39. #endif
  40. {
  41. #ifdef BUILD_BRIDGE_UI
  42. m_filename = nullptr;
  43. m_lib = nullptr;
  44. #endif
  45. }
  46. virtual ~CarlaBridgeClient()
  47. {
  48. #ifdef BUILD_BRIDGE_UI
  49. if (m_filename)
  50. free(m_filename);
  51. #endif
  52. }
  53. // ---------------------------------------------------------------------
  54. bool oscInit(const char* const url)
  55. {
  56. return m_osc.init(url);
  57. }
  58. void oscClose()
  59. {
  60. m_osc.close();
  61. }
  62. void sendOscConfigure(const char* const key, const char* const value)
  63. {
  64. qDebug("sendOscConfigure(\"%s\", \"%s\")", key, value);
  65. m_osc.sendOscConfigure(key, value);
  66. }
  67. void sendOscControl(int32_t index, float value)
  68. {
  69. qDebug("sendOscConfigure(%i, %f)", index, value);
  70. m_osc.sendOscControl(index, value);
  71. }
  72. void sendOscUpdate()
  73. {
  74. qDebug("sendOscUpdate()");
  75. m_osc.sendOscUpdate();
  76. }
  77. void sendOscExiting()
  78. {
  79. qDebug("sendOscExiting()");
  80. m_osc.sendOscExiting();
  81. }
  82. #ifdef BRIDGE_LV2
  83. void sendOscLv2TransferAtom(const char* const type, const char* const value)
  84. {
  85. qDebug("sendOscLv2TransferAtom(\"%s\", \"%s\")", type, value);
  86. m_osc.sendOscLv2TransferAtom(type, value);
  87. }
  88. void sendOscLv2TransferEvent(const char* const type, const char* const value)
  89. {
  90. qDebug("sendOscLv2TransferEvent(\"%s\", \"%s\")", type, value);
  91. m_osc.sendOscLv2TransferEvent(type, value);
  92. }
  93. #endif
  94. // ---------------------------------------------------------------------
  95. void quequeMessage(MessageType type, int32_t value1, int32_t value2, double value3)
  96. {
  97. const QMutexLocker locker(&m_messages.lock);
  98. for (unsigned int i=0; i < MAX_BRIDGE_MESSAGES; i++)
  99. {
  100. Message* const m = &m_messages.data[i];
  101. if (m->type == MESSAGE_NULL)
  102. {
  103. m->type = type;
  104. m->value1 = value1;
  105. m->value2 = value2;
  106. m->value3 = value3;
  107. break;
  108. }
  109. }
  110. }
  111. bool runMessages()
  112. {
  113. const QMutexLocker locker(&m_messages.lock);
  114. for (unsigned int i=0; i < MAX_BRIDGE_MESSAGES; i++)
  115. {
  116. Message* const m = &m_messages.data[i];
  117. switch (m->type)
  118. {
  119. case MESSAGE_NULL:
  120. return true;
  121. case MESSAGE_PARAMETER:
  122. setParameter(m->value1, m->value3);
  123. break;
  124. case MESSAGE_PROGRAM:
  125. setProgram(m->value1);
  126. break;
  127. case MESSAGE_MIDI_PROGRAM:
  128. setMidiProgram(m->value1, m->value2);
  129. break;
  130. case MESSAGE_NOTE_ON:
  131. noteOn(m->value1, m->value2);
  132. break;
  133. case MESSAGE_NOTE_OFF:
  134. noteOff(m->value1);
  135. break;
  136. case MESSAGE_SHOW_GUI:
  137. if (m->value1)
  138. m_toolkit->show();
  139. else
  140. m_toolkit->hide();
  141. break;
  142. case MESSAGE_RESIZE_GUI:
  143. m_toolkit->resize(m->value1, m->value2);
  144. break;
  145. case MESSAGE_SAVE_NOW:
  146. #ifdef BUILD_BRIDGE_PLUGIN
  147. saveNow();
  148. #endif
  149. break;
  150. case MESSAGE_QUIT:
  151. m_toolkit->quit();
  152. return false;
  153. }
  154. m->type = MESSAGE_NULL;
  155. }
  156. return true;
  157. }
  158. // ---------------------------------------------------------------------
  159. #ifdef BUILD_BRIDGE_UI
  160. // ui initialization
  161. virtual bool init(const char* const, const char* const) = 0;
  162. virtual void close() = 0;
  163. #endif
  164. // processing
  165. virtual void setParameter(const int32_t rindex, const double value) = 0;
  166. virtual void setProgram(const uint32_t index) = 0;
  167. virtual void setMidiProgram(const uint32_t bank, const uint32_t program) = 0;
  168. virtual void noteOn(const uint8_t note, const uint8_t velocity) = 0;
  169. virtual void noteOff(const uint8_t note) = 0;
  170. #ifdef BUILD_BRIDGE_PLUGIN
  171. // plugin
  172. virtual void saveNow() = 0;
  173. virtual void setCustomData(const char* const type, const char* const key, const char* const value) = 0;
  174. virtual void setChunkData(const char* const filePath) = 0;
  175. #else
  176. // gui
  177. virtual void* getWidget() const = 0;
  178. virtual bool isResizable() const = 0;
  179. virtual bool needsReparent() const = 0;
  180. #endif
  181. // ---------------------------------------------------------------------
  182. #ifdef BUILD_BRIDGE_UI
  183. protected:
  184. bool libOpen(const char* const filename)
  185. {
  186. m_lib = lib_open(filename);
  187. m_filename = strdup(filename);
  188. return bool(m_lib);
  189. }
  190. bool libClose()
  191. {
  192. if (m_lib)
  193. {
  194. const bool closed = lib_close(m_lib);
  195. m_lib = nullptr;
  196. return closed;
  197. }
  198. return false;
  199. }
  200. void* libSymbol(const char* const symbol)
  201. {
  202. if (m_lib)
  203. return lib_symbol(m_lib, symbol);
  204. return nullptr;
  205. }
  206. const char* libError()
  207. {
  208. return lib_error(m_filename ? m_filename : "");
  209. }
  210. #endif
  211. // ---------------------------------------------------------------------
  212. private:
  213. CarlaBridgeToolkit* const m_toolkit;
  214. CarlaBridgeOsc m_osc;
  215. struct {
  216. Message data[MAX_BRIDGE_MESSAGES];
  217. QMutex lock;
  218. } m_messages;
  219. #ifdef BUILD_BRIDGE_UI
  220. char* m_filename;
  221. void* m_lib;
  222. #endif
  223. };
  224. CARLA_BRIDGE_END_NAMESPACE
  225. #endif // CARLA_BRIDGE_CLIENT_H