Audio plugin host https://kx.studio/carla
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.

181 lines
8.5KB

  1. // SPDX-FileCopyrightText: 2011-2025 Filipe Coelho <falktx@falktx.com>
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #ifndef CARLA_ENGINE_OSC_HPP_INCLUDED
  4. #define CARLA_ENGINE_OSC_HPP_INCLUDED
  5. #include "CarlaDefines.h"
  6. #ifdef HAVE_LIBLO
  7. #include "CarlaBackend.h"
  8. #include "CarlaPlugin.hpp"
  9. #include "CarlaJuceUtils.hpp"
  10. #include "CarlaOscUtils.hpp"
  11. #include "distrho/extra/String.hpp"
  12. #define CARLA_ENGINE_OSC_HANDLE_ARGS const CarlaPluginPtr& plugin, \
  13. const int argc, const lo_arg* const* const argv, const char* const types
  14. #define CARLA_ENGINE_OSC_CHECK_OSC_TYPES(/* argc, types, */ argcToCompare, typesToCompare) \
  15. /* check argument count */ \
  16. if (argc != argcToCompare) \
  17. { \
  18. carla_stderr("CarlaEngineOsc::%s() - argument count mismatch: %i != %i", __FUNCTION__, argc, argcToCompare); \
  19. return 1; \
  20. } \
  21. if (argc > 0) \
  22. { \
  23. /* check for nullness */ \
  24. if (types == nullptr || typesToCompare == nullptr) \
  25. { \
  26. carla_stderr("CarlaEngineOsc::%s() - argument types are null", __FUNCTION__); \
  27. return 1; \
  28. } \
  29. /* check argument types */ \
  30. if (std::strcmp(types, typesToCompare) != 0) \
  31. { \
  32. carla_stderr("CarlaEngineOsc::%s() - argument types mismatch: '%s' != '%s'", __FUNCTION__, types, typesToCompare); \
  33. return 1; \
  34. } \
  35. }
  36. CARLA_BACKEND_START_NAMESPACE
  37. // -----------------------------------------------------------------------
  38. class CarlaEngineOsc
  39. {
  40. public:
  41. CarlaEngineOsc(CarlaEngine* engine) noexcept;
  42. ~CarlaEngineOsc() noexcept;
  43. void init(const char* name, int tcpPort, int udpPort) noexcept;
  44. void idle() const noexcept;
  45. void close() noexcept;
  46. // -------------------------------------------------------------------
  47. const String& getServerPathTCP() const noexcept
  48. {
  49. return fServerPathTCP;
  50. }
  51. const String& getServerPathUDP() const noexcept
  52. {
  53. return fServerPathUDP;
  54. }
  55. // -------------------------------------------------------------------
  56. bool isControlRegisteredForTCP() const noexcept
  57. {
  58. return fControlDataTCP.target != nullptr;
  59. }
  60. bool isControlRegisteredForUDP() const noexcept
  61. {
  62. return fControlDataUDP.target != nullptr;
  63. }
  64. // -------------------------------------------------------------------
  65. // TCP
  66. void sendCallback(EngineCallbackOpcode action, uint pluginId,
  67. int value1, int value2, int value3,
  68. float valuef, const char* valueStr) const noexcept;
  69. void sendPluginInfo(const CarlaPluginPtr& plugin) const noexcept;
  70. void sendPluginPortCount(const CarlaPluginPtr& plugin) const noexcept;
  71. void sendPluginParameterInfo(const CarlaPluginPtr& plugin, uint32_t index) const noexcept;
  72. void sendPluginDataCount(const CarlaPluginPtr& plugin) const noexcept;
  73. void sendPluginProgramCount(const CarlaPluginPtr& plugin) const noexcept;
  74. void sendPluginProgram(const CarlaPluginPtr& plugin, uint32_t index) const noexcept;
  75. void sendPluginMidiProgram(const CarlaPluginPtr& plugin, uint32_t index) const noexcept;
  76. void sendPluginCustomData(const CarlaPluginPtr& plugin, uint32_t index) const noexcept;
  77. void sendPluginInternalParameterValues(const CarlaPluginPtr& plugin) const noexcept;
  78. void sendPing() const noexcept;
  79. void sendResponse(int messageId, const char* error) const noexcept;
  80. void sendExit() const noexcept;
  81. // -------------------------------------------------------------------
  82. // UDP
  83. void sendRuntimeInfo() const noexcept;
  84. void sendParameterValue(uint pluginId, uint32_t index, float value) const noexcept;
  85. void sendPeaks(uint pluginId, const float peaks[4]) const noexcept;
  86. // -------------------------------------------------------------------
  87. private:
  88. CarlaEngine* const fEngine;
  89. // for carla-control
  90. CarlaOscData fControlDataTCP;
  91. CarlaOscData fControlDataUDP;
  92. String fName;
  93. String fServerPathTCP;
  94. String fServerPathUDP;
  95. lo_server fServerTCP;
  96. lo_server fServerUDP;
  97. // -------------------------------------------------------------------
  98. int handleMessage(bool isTCP, const char* path,
  99. int argc, const lo_arg* const* argv, const char* types, lo_message msg);
  100. int handleMsgRegister(bool isTCP, int argc, const lo_arg* const* argv, const char* types, lo_address source);
  101. int handleMsgUnregister(bool isTCP, int argc, const lo_arg* const* argv, const char* types, lo_address source);
  102. int handleMsgControl(const char* method,
  103. int argc, const lo_arg* const* argv, const char* types);
  104. // Internal methods
  105. int handleMsgSetActive(CARLA_ENGINE_OSC_HANDLE_ARGS);
  106. int handleMsgSetDryWet(CARLA_ENGINE_OSC_HANDLE_ARGS);
  107. int handleMsgSetVolume(CARLA_ENGINE_OSC_HANDLE_ARGS);
  108. int handleMsgSetBalanceLeft(CARLA_ENGINE_OSC_HANDLE_ARGS);
  109. int handleMsgSetBalanceRight(CARLA_ENGINE_OSC_HANDLE_ARGS);
  110. int handleMsgSetPanning(CARLA_ENGINE_OSC_HANDLE_ARGS);
  111. int handleMsgSetParameterValue(CARLA_ENGINE_OSC_HANDLE_ARGS);
  112. int handleMsgSetParameterMappedControlIndex(CARLA_ENGINE_OSC_HANDLE_ARGS);
  113. int handleMsgSetParameterMappedRange(CARLA_ENGINE_OSC_HANDLE_ARGS);
  114. int handleMsgSetParameterMidiChannel(CARLA_ENGINE_OSC_HANDLE_ARGS);
  115. int handleMsgSetProgram(CARLA_ENGINE_OSC_HANDLE_ARGS);
  116. int handleMsgSetMidiProgram(CARLA_ENGINE_OSC_HANDLE_ARGS);
  117. int handleMsgNoteOn(CARLA_ENGINE_OSC_HANDLE_ARGS);
  118. int handleMsgNoteOff(CARLA_ENGINE_OSC_HANDLE_ARGS);
  119. // -----------------------------------------------------------------------
  120. static void osc_error_handler_TCP(int num, const char* msg, const char* path)
  121. {
  122. carla_stderr("CarlaEngineOsc::osc_error_handler_TCP(%i, \"%s\", \"%s\")", num, msg, path);
  123. }
  124. static void osc_error_handler_UDP(int num, const char* msg, const char* path)
  125. {
  126. carla_stderr("CarlaEngineOsc::osc_error_handler_UDP(%i, \"%s\", \"%s\")", num, msg, path);
  127. }
  128. static int osc_message_handler_TCP(const char* path, const char* types, lo_arg** argv, int argc, lo_message msg, void* userData)
  129. {
  130. return ((CarlaEngineOsc*)userData)->handleMessage(true, path, argc, argv, types, msg);
  131. }
  132. static int osc_message_handler_UDP(const char* path, const char* types, lo_arg** argv, int argc, lo_message msg, void* userData)
  133. {
  134. return ((CarlaEngineOsc*)userData)->handleMessage(false, path, argc, argv, types, msg);
  135. }
  136. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineOsc)
  137. };
  138. // -----------------------------------------------------------------------
  139. CARLA_BACKEND_END_NAMESPACE
  140. #endif // HAVE_LIBLO
  141. #endif // CARLA_ENGINE_OSC_HPP_INCLUDED