The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

225 lines
9.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. //==============================================================================
  21. /**
  22. A class for receiving OSC data.
  23. An OSCReceiver object allows you to receive OSC bundles and messages.
  24. It can connect to a network port, receive incoming OSC packets from the
  25. network via UDP, parse them, and forward the included OSCMessage and OSCBundle
  26. objects to its listeners.
  27. @tags{OSC}
  28. */
  29. class JUCE_API OSCReceiver
  30. {
  31. public:
  32. //==============================================================================
  33. /** Creates an OSCReceiver. */
  34. OSCReceiver();
  35. /** Creates an OSCReceiver with a specific name for its thread. */
  36. OSCReceiver (const String& threadName);
  37. /** Destructor. */
  38. ~OSCReceiver();
  39. //==============================================================================
  40. /** Connects to the specified UDP port using a datagram socket,
  41. and starts listening to OSC packets arriving on this port.
  42. @returns true if the connection was successful; false otherwise.
  43. */
  44. bool connect (int portNumber);
  45. /** Connects to a UDP datagram socket that is already set up,
  46. and starts listening to OSC packets arriving on this port.
  47. Make sure that the object you give it doesn't get deleted while this
  48. object is still using it!
  49. @returns true if the connection was successful; false otherwise.
  50. */
  51. bool connectToSocket (DatagramSocket& socketToUse);
  52. //==============================================================================
  53. /** Disconnects from the currently used UDP port.
  54. @returns true if the disconnection was successful; false otherwise.
  55. */
  56. bool disconnect();
  57. //==============================================================================
  58. /** Use this struct as the template parameter for Listener and
  59. ListenerWithOSCAddress to receive incoming OSC data on the message thread.
  60. This should be used by OSC callbacks that are not realtime-critical, but
  61. have significant work to do, for example updating Components in your app's
  62. user interface.
  63. This is the default type of OSC listener.
  64. */
  65. struct JUCE_API MessageLoopCallback {};
  66. /** Use this struct as the template parameter for Listener and
  67. ListenerWithOSCAddress to receive incoming OSC data immediately after it
  68. arrives, called directly on the network thread that listens to incoming
  69. OSC traffic.
  70. This type can be used by OSC callbacks that don't do much, but are
  71. realtime-critical, for example, setting real-time audio parameters.
  72. */
  73. struct JUCE_API RealtimeCallback {};
  74. //==============================================================================
  75. /** A class for receiving OSC data from an OSCReceiver.
  76. The template argument CallbackType determines how the callback will be called
  77. and has to be either MessageLoopCallback or RealtimeCallback. If not specified,
  78. MessageLoopCallback will be used by default.
  79. @see OSCReceiver::addListener, OSCReceiver::ListenerWithOSCAddress,
  80. OSCReceiver::MessageLoopCallback, OSCReceiver::RealtimeCallback
  81. */
  82. template <typename CallbackType = MessageLoopCallback>
  83. class JUCE_API Listener
  84. {
  85. public:
  86. /** Destructor. */
  87. virtual ~Listener() = default;
  88. /** Called when the OSCReceiver receives a new OSC message.
  89. You must implement this function.
  90. */
  91. virtual void oscMessageReceived (const OSCMessage& message) = 0;
  92. /** Called when the OSCReceiver receives a new OSC bundle.
  93. If you are not interested in OSC bundles, just ignore this method.
  94. The default implementation provided here will simply do nothing.
  95. */
  96. virtual void oscBundleReceived (const OSCBundle& /*bundle*/) {}
  97. };
  98. //==============================================================================
  99. /** A class for receiving only those OSC messages from an OSCReceiver that match a
  100. given OSC address.
  101. Use this class if your app receives OSC messages with different address patterns
  102. (for example "/juce/fader1", /juce/knob2" etc.) and you want to route those to
  103. different objects. This class contains pre-build functionality for that OSC
  104. address routing, including wildcard pattern matching (e.g. "/juce/fader[0-9]").
  105. This class implements the concept of an "OSC Method" from the OpenSoundControl 1.0
  106. specification.
  107. The template argument CallbackType determines how the callback will be called
  108. and has to be either MessageLoopCallback or RealtimeCallback. If not specified,
  109. MessageLoopCallback will be used by default.
  110. Note: This type of listener will ignore OSC bundles.
  111. @see OSCReceiver::addListener, OSCReceiver::Listener,
  112. OSCReceiver::MessageLoopCallback, OSCReceiver::RealtimeCallback
  113. */
  114. template <typename CallbackType = MessageLoopCallback>
  115. class JUCE_API ListenerWithOSCAddress
  116. {
  117. public:
  118. /** Destructor. */
  119. virtual ~ListenerWithOSCAddress() = default;
  120. /** Called when the OSCReceiver receives an OSC message with an OSC address
  121. pattern that matches the OSC address with which this listener was added.
  122. */
  123. virtual void oscMessageReceived (const OSCMessage& message) = 0;
  124. };
  125. //==============================================================================
  126. /** Adds a listener that listens to OSC messages and bundles.
  127. This listener will be called on the application's message loop.
  128. */
  129. void addListener (Listener<MessageLoopCallback>* listenerToAdd);
  130. /** Adds a listener that listens to OSC messages and bundles.
  131. This listener will be called in real-time directly on the network thread
  132. that receives OSC data.
  133. */
  134. void addListener (Listener<RealtimeCallback>* listenerToAdd);
  135. /** Adds a filtered listener that listens to OSC messages matching the address
  136. used to register the listener here.
  137. The listener will be called on the application's message loop.
  138. */
  139. void addListener (ListenerWithOSCAddress<MessageLoopCallback>* listenerToAdd,
  140. OSCAddress addressToMatch);
  141. /** Adds a filtered listener that listens to OSC messages matching the address
  142. used to register the listener here.
  143. The listener will be called on the application's message loop.
  144. */
  145. void addListener (ListenerWithOSCAddress<RealtimeCallback>* listenerToAdd,
  146. OSCAddress addressToMatch);
  147. /** Removes a previously-registered listener. */
  148. void removeListener (Listener<MessageLoopCallback>* listenerToRemove);
  149. /** Removes a previously-registered listener. */
  150. void removeListener (Listener<RealtimeCallback>* listenerToRemove);
  151. /** Removes a previously-registered listener. */
  152. void removeListener (ListenerWithOSCAddress<MessageLoopCallback>* listenerToRemove);
  153. /** Removes a previously-registered listener. */
  154. void removeListener (ListenerWithOSCAddress<RealtimeCallback>* listenerToRemove);
  155. //==============================================================================
  156. /** An error handler function for OSC format errors that can be called by the
  157. OSCReceiver.
  158. The arguments passed are the pointer to and the data of the buffer that
  159. the OSCReceiver has failed to parse.
  160. */
  161. using FormatErrorHandler = std::function<void (const char* data, int dataSize)>;
  162. /** Installs a custom error handler which is called in case the receiver
  163. encounters a stream it cannot parse as an OSC bundle or OSC message.
  164. By default (i.e. if you never use this method), in case of a parsing error
  165. nothing happens and the invalid packet is simply discarded.
  166. */
  167. void registerFormatErrorHandler (FormatErrorHandler handler);
  168. private:
  169. //==============================================================================
  170. struct Pimpl;
  171. std::unique_ptr<Pimpl> pimpl;
  172. friend struct OSCReceiverCallbackMessage;
  173. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OSCReceiver)
  174. };
  175. } // namespace juce