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.

218 lines
9.0KB

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