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.

239 lines
6.5KB

  1. /*
  2. * Copyright (c) 2012 Mark McCurry
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  19. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. *
  24. * @file rtosc.h
  25. */
  26. #ifndef RTOSC_H
  27. #define RTOSC_H
  28. #include <stdarg.h>
  29. #include <stdint.h>
  30. #include <stddef.h>
  31. #include <stdbool.h>
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. typedef struct {
  36. int32_t len;
  37. uint8_t *data;
  38. } rtosc_blob_t;
  39. typedef union {
  40. int32_t i; //i,c,r
  41. char T; //I,T,F,N
  42. float f; //f
  43. double d; //d
  44. int64_t h; //h
  45. uint64_t t; //t
  46. uint8_t m[4];//m
  47. const char *s; //s,S
  48. rtosc_blob_t b; //b
  49. } rtosc_arg_t;
  50. /**
  51. * Write OSC message to fixed length buffer
  52. *
  53. * On error, buffer will be zeroed.
  54. * When buffer is NULL, the function returns the size of the buffer required to
  55. * store the message
  56. *
  57. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  58. * //Example messages
  59. * char buffer[128];
  60. * rtosc_message(buffer,128,"/path","TFI");
  61. * rtosc_message(buffer,128,"/path","s","foobar");
  62. * rtosc_message(buffer,128,"/path","i",128);
  63. * rtosc_message(buffer,128,"/path","f",128.0);
  64. * const char blob[4] = {'a','b','c','d'};
  65. * rtosc_message(buffer,128,"/path","b",4,blob);
  66. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  67. *
  68. * @param buffer Memory to write to
  69. * @param len Length of buffer
  70. * @param address OSC pattern to send message to
  71. * @param arguments String consisting of the types of the following arguments
  72. * @param ... OSC arguments to pass forward
  73. * @returns length of resulting message or zero if bounds exceeded
  74. */
  75. size_t rtosc_message(char *buffer,
  76. size_t len,
  77. const char *address,
  78. const char *arguments,
  79. ...);
  80. /**
  81. * @see rtosc_message()
  82. */
  83. size_t rtosc_vmessage(char *buffer,
  84. size_t len,
  85. const char *address,
  86. const char *arguments,
  87. va_list va);
  88. /**
  89. * @see rtosc_message()
  90. */
  91. size_t rtosc_amessage(char *buffer,
  92. size_t len,
  93. const char *address,
  94. const char *arguments,
  95. const rtosc_arg_t *args);
  96. /**
  97. * Returns the number of arguments found in a given message
  98. *
  99. * @param msg well formed OSC message
  100. * @returns number of arguments in message
  101. */
  102. unsigned rtosc_narguments(const char *msg);
  103. /**
  104. * @param msg well formed OSC message
  105. * @param i index of argument
  106. * @returns the type of the ith argument in msg
  107. */
  108. char rtosc_type(const char *msg, unsigned i);
  109. /**
  110. * Blob data may be safely written to
  111. * @param msg OSC message
  112. * @param i index of argument
  113. * @returns an argument by value via the rtosc_arg_t union
  114. */
  115. rtosc_arg_t rtosc_argument(const char *msg, unsigned i);
  116. /**
  117. * @param msg OSC message
  118. * @param len Message length upper bound
  119. * @returns the size of a message given a chunk of memory.
  120. */
  121. size_t rtosc_message_length(const char *msg, size_t len);
  122. typedef struct {
  123. char *data;
  124. size_t len;
  125. } ring_t;
  126. /**
  127. * Finds the length of the next message inside a ringbuffer structure.
  128. *
  129. * @param ring The addresses and lengths of the split buffer, in a compatible
  130. * format to jack's ringbuffer
  131. * @returns size of message stored in ring datastructure
  132. */
  133. size_t rtosc_message_ring_length(ring_t *ring);
  134. /**
  135. * Validate if an arbitrary byte sequence is an OSC message.
  136. * @param msg pointer to memory buffer
  137. * @param len length of buffer
  138. */
  139. bool rtosc_valid_message_p(const char *msg, size_t len);
  140. /**
  141. * @param OSC message
  142. * @returns the argument string of a given message
  143. */
  144. const char *rtosc_argument_string(const char *msg);
  145. /**
  146. * Generate a bundle from sub-messages
  147. *
  148. * @param buffer Destination buffer
  149. * @param len Length of buffer
  150. * @param tt OSC time tag
  151. * @param elms Number of sub messages
  152. * @param ... Messages
  153. * @returns legnth of generated bundle or zero on failure
  154. */
  155. size_t rtosc_bundle(char *buffer, size_t len, uint64_t tt, int elms, ...);
  156. /**
  157. * Find the elements in a bundle
  158. *
  159. * @param msg OSC bundle
  160. * @param len Upper bound on the length of the bundle
  161. * @returns The number of messages contained within the bundle
  162. */
  163. size_t rtosc_bundle_elements(const char *msg, size_t len);
  164. /**
  165. * Fetch a message within the bundle
  166. *
  167. * @param msg OSC bundle
  168. * @param i index of sub-message
  169. * @returns The ith message within the bundle
  170. */
  171. const char *rtosc_bundle_fetch(const char *msg, unsigned i);
  172. /**
  173. * Get the size of a particular bundle element
  174. *
  175. * @param msg OSC bundle
  176. * @param i Index of sub-message
  177. * @returns The size of the ith sub-message in bytes
  178. */
  179. size_t rtosc_bundle_size(const char *msg, unsigned i);
  180. /**
  181. * Test if the buffer contains a bundle
  182. *
  183. * @param msg OSC message
  184. * @returns true if message is a bundle
  185. */
  186. int rtosc_bundle_p(const char *msg);
  187. /**
  188. * @returns Time Tag for a bundle
  189. */
  190. uint64_t rtosc_bundle_timetag(const char *msg);
  191. /**
  192. * This is a non-compliant pattern matcher for dispatching OSC messages
  193. *
  194. * Overall the pattern specification is
  195. * (normal-path)(\#digit-specifier)?(/)?(:argument-restrictor)*
  196. *
  197. * @param pattern The pattern string stored in the Port
  198. * @param msg The OSC message to be matched
  199. * @returns true if a normal match and false if unmatched
  200. */
  201. bool rtosc_match(const char *pattern, const char *msg);
  202. /**
  203. * Attempt to match a rtosc style path while ignoring arguments
  204. *
  205. * @param pattern rtosc pattern
  206. * @param msg a normal C string or a rtosc message
  207. */
  208. const char *rtosc_match_path(const char *pattern, const char *msg);
  209. #ifdef __cplusplus
  210. };
  211. #endif
  212. #endif