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.

244 lines
5.8KB

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