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.

rtosc.h 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. typedef struct {
  110. const char *type_pos;
  111. const uint8_t *value_pos;
  112. } rtosc_arg_itr_t;
  113. typedef struct {
  114. char type;
  115. rtosc_arg_t val;
  116. } rtosc_arg_val_t;
  117. /**
  118. * Create an argument iterator for a message
  119. * @param msg OSC message
  120. * @returns an initialized iterator
  121. */
  122. rtosc_arg_itr_t rtosc_itr_begin(const char *msg);
  123. /**
  124. * Gets the next argument in a message
  125. * @param itr OSC message iterator
  126. * @returns a type value pair from the message
  127. */
  128. rtosc_arg_val_t rtosc_itr_next(rtosc_arg_itr_t *itr);
  129. /**
  130. * Determines if the iterator is at the end of the argument list
  131. * @param itr OSC message iterator
  132. * @returns 1 if there are no more elements, 0 otherwise
  133. */
  134. int rtosc_itr_end(rtosc_arg_itr_t itr);
  135. /**
  136. * Blob data may be safely written to
  137. * @param msg OSC message
  138. * @param i index of argument
  139. * @returns an argument by value via the rtosc_arg_t union
  140. */
  141. rtosc_arg_t rtosc_argument(const char *msg, unsigned i);
  142. /**
  143. * @param msg OSC message
  144. * @param len Message length upper bound
  145. * @returns the size of a message given a chunk of memory.
  146. */
  147. size_t rtosc_message_length(const char *msg, size_t len);
  148. typedef struct {
  149. char *data;
  150. size_t len;
  151. } ring_t;
  152. /**
  153. * Finds the length of the next message inside a ringbuffer structure.
  154. *
  155. * @param ring The addresses and lengths of the split buffer, in a compatible
  156. * format to jack's ringbuffer
  157. * @returns size of message stored in ring datastructure
  158. */
  159. size_t rtosc_message_ring_length(ring_t *ring);
  160. /**
  161. * Validate if an arbitrary byte sequence is an OSC message.
  162. * @param msg pointer to memory buffer
  163. * @param len length of buffer
  164. */
  165. bool rtosc_valid_message_p(const char *msg, size_t len);
  166. /**
  167. * @param OSC message
  168. * @returns the argument string of a given message
  169. */
  170. const char *rtosc_argument_string(const char *msg);
  171. /**
  172. * Generate a bundle from sub-messages
  173. *
  174. * @param buffer Destination buffer
  175. * @param len Length of buffer
  176. * @param tt OSC time tag
  177. * @param elms Number of sub messages
  178. * @param ... Messages
  179. * @returns legnth of generated bundle or zero on failure
  180. */
  181. size_t rtosc_bundle(char *buffer, size_t len, uint64_t tt, int elms, ...);
  182. /**
  183. * Find the elements in a bundle
  184. *
  185. * @param msg OSC bundle
  186. * @param len Upper bound on the length of the bundle
  187. * @returns The number of messages contained within the bundle
  188. */
  189. size_t rtosc_bundle_elements(const char *msg, size_t len);
  190. /**
  191. * Fetch a message within the bundle
  192. *
  193. * @param msg OSC bundle
  194. * @param i index of sub-message
  195. * @returns The ith message within the bundle
  196. */
  197. const char *rtosc_bundle_fetch(const char *msg, unsigned i);
  198. /**
  199. * Get the size of a particular bundle element
  200. *
  201. * @param msg OSC bundle
  202. * @param i Index of sub-message
  203. * @returns The size of the ith sub-message in bytes
  204. */
  205. size_t rtosc_bundle_size(const char *msg, unsigned i);
  206. /**
  207. * Test if the buffer contains a bundle
  208. *
  209. * @param msg OSC message
  210. * @returns true if message is a bundle
  211. */
  212. int rtosc_bundle_p(const char *msg);
  213. /**
  214. * @returns Time Tag for a bundle
  215. */
  216. uint64_t rtosc_bundle_timetag(const char *msg);
  217. /**
  218. * This is a non-compliant pattern matcher for dispatching OSC messages
  219. *
  220. * Overall the pattern specification is
  221. * (normal-path)(\#digit-specifier)?(/)?(:argument-restrictor)*
  222. *
  223. * @param pattern The pattern string stored in the Port
  224. * @param msg The OSC message to be matched
  225. * @returns true if a normal match and false if unmatched
  226. */
  227. bool rtosc_match(const char *pattern, const char *msg);
  228. /**
  229. * Attempt to match a rtosc style path while ignoring arguments
  230. *
  231. * @param pattern rtosc pattern
  232. * @param msg a normal C string or a rtosc message
  233. */
  234. const char *rtosc_match_path(const char *pattern, const char *msg);
  235. #ifdef __cplusplus
  236. };
  237. #endif
  238. #endif