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.

216 lines
8.9KB

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