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.

174 lines
4.8KB

  1. /**
  2. * Copyright (c) 2015, Martin Roth (mhroth@gmail.com)
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
  9. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  10. * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  12. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  13. * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  14. * PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifndef _TINY_OSC_
  17. #define _TINY_OSC_
  18. #include <stdbool.h>
  19. #include <stdint.h>
  20. #define TINYOSC_TIMETAG_IMMEDIATELY 1L
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. typedef struct tosc_message {
  25. char *format; // a pointer to the format field
  26. char *marker; // the current read head
  27. char *buffer; // the original message data (also points to the address)
  28. uint32_t len; // length of the buffer data
  29. } tosc_message;
  30. typedef struct tosc_bundle {
  31. char *marker; // the current write head (where the next message will be written)
  32. char *buffer; // the original buffer
  33. uint32_t bufLen; // the byte length of the original buffer
  34. uint32_t bundleLen; // the byte length of the total bundle
  35. } tosc_bundle;
  36. /**
  37. * Returns true if the buffer refers to a bundle of OSC messages. False otherwise.
  38. */
  39. bool tosc_isBundle(const char *buffer);
  40. /**
  41. * Reads a buffer containing a bundle of OSC messages.
  42. */
  43. void tosc_parseBundle(tosc_bundle *b, char *buffer, const int len);
  44. /**
  45. * Returns the timetag of an OSC bundle.
  46. */
  47. uint64_t tosc_getTimetag(tosc_bundle *b);
  48. /**
  49. * Parses the next message in a bundle. Returns true if successful.
  50. * False otherwise.
  51. */
  52. bool tosc_getNextMessage(tosc_bundle *b, tosc_message *o);
  53. /**
  54. * Returns a point to the address block of the OSC buffer.
  55. * This is also the start of the buffer.
  56. */
  57. char *tosc_getAddress(tosc_message *o);
  58. /**
  59. * Returns a pointer to the format block of the OSC buffer.
  60. */
  61. char *tosc_getFormat(tosc_message *o);
  62. /**
  63. * Returns the length in bytes of this message.
  64. */
  65. uint32_t tosc_getLength(tosc_message *o);
  66. /**
  67. * Returns the next 32-bit int. Does not check buffer bounds.
  68. */
  69. int32_t tosc_getNextInt32(tosc_message *o);
  70. /**
  71. * Returns the next 64-bit int. Does not check buffer bounds.
  72. */
  73. int64_t tosc_getNextInt64(tosc_message *o);
  74. /**
  75. * Returns the next 64-bit timetag. Does not check buffer bounds.
  76. */
  77. uint64_t tosc_getNextTimetag(tosc_message *o);
  78. /**
  79. * Returns the next 32-bit float. Does not check buffer bounds.
  80. */
  81. float tosc_getNextFloat(tosc_message *o);
  82. /**
  83. * Returns the next 64-bit float. Does not check buffer bounds.
  84. */
  85. double tosc_getNextDouble(tosc_message *o);
  86. /**
  87. * Returns the next string, or NULL if the buffer length is exceeded.
  88. */
  89. const char *tosc_getNextString(tosc_message *o);
  90. /**
  91. * Points the given buffer pointer to the next blob.
  92. * The len pointer is set to the length of the blob.
  93. * Returns NULL and 0 if the OSC buffer bounds are exceeded.
  94. */
  95. void tosc_getNextBlob(tosc_message *o, const char **buffer, int *len);
  96. /**
  97. * Returns the next set of midi bytes. Does not check bounds.
  98. * Bytes from MSB to LSB are: port id, status byte, data1, data2.
  99. */
  100. unsigned char *tosc_getNextMidi(tosc_message *o);
  101. /**
  102. * Parse a buffer containing an OSC message.
  103. * The contents of the buffer are NOT copied.
  104. * The tosc_message struct only points at relevant parts of the original buffer.
  105. * Returns 0 if there is no error. An error code (a negative number) otherwise.
  106. */
  107. int tosc_parseMessage(tosc_message *o, char *buffer, const int len);
  108. /**
  109. * Starts writing a bundle to the given buffer with length.
  110. */
  111. void tosc_writeBundle(tosc_bundle *b, uint64_t timetag, char *buffer, const int len);
  112. /**
  113. * Write a message to a bundle buffer. Returns the number of bytes written.
  114. */
  115. uint32_t tosc_writeNextMessage(tosc_bundle *b,
  116. const char *address, const char *format, ...);
  117. /**
  118. * Returns the length in bytes of the bundle.
  119. */
  120. uint32_t tosc_getBundleLength(tosc_bundle *b);
  121. /**
  122. * Writes an OSC packet to a buffer. Returns the total number of bytes written.
  123. * The entire buffer is cleared before writing.
  124. */
  125. uint32_t tosc_writeMessage(char *buffer, const int len, const char *address,
  126. const char *fmt, ...);
  127. /**
  128. * A convenience function to (non-destructively) print a buffer containing
  129. * an OSC message to stdout.
  130. */
  131. void tosc_printOscBuffer(char *buffer, const int len);
  132. /**
  133. * A convenience function to (non-destructively) print a pre-parsed OSC message
  134. * to stdout.
  135. */
  136. void tosc_printMessage(tosc_message *o);
  137. #ifdef __cplusplus
  138. }
  139. #endif
  140. #endif // _TINY_OSC_