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.

event-helpers.h 7.5KB

12 years ago
9 years ago
12 years ago
12 years ago
12 years ago
9 years ago
12 years ago
9 years ago
12 years ago
9 years ago
12 years ago
12 years ago
9 years ago
12 years ago
9 years ago
12 years ago
12 years ago
9 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
9 years ago
12 years ago
12 years ago
12 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. Copyright 2008-2014 David Robillard <http://drobilla.net>
  3. Permission to use, copy, modify, and/or distribute this software for any
  4. purpose with or without fee is hereby granted, provided that the above
  5. copyright notice and this permission notice appear in all copies.
  6. THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  7. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  8. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  9. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  10. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  11. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  12. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  13. */
  14. /**
  15. @file event-helpers.h Helper functions for the LV2 Event extension
  16. <http://lv2plug.in/ns/ext/event>.
  17. */
  18. #ifndef LV2_EVENT_HELPERS_H
  19. #define LV2_EVENT_HELPERS_H
  20. #include <stdint.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "event.h"
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #else
  27. # include <stdbool.h>
  28. #endif
  29. /** @file
  30. * Helper functions for the LV2 Event extension
  31. * <http://lv2plug.in/ns/ext/event>.
  32. *
  33. * These functions are provided for convenience only, use of them is not
  34. * required for supporting lv2ev (i.e. the events extension is defined by the
  35. * raw buffer format described in lv2_event.h and NOT by this API).
  36. *
  37. * Note that these functions are all static inline which basically means:
  38. * do not take the address of these functions. */
  39. /** Pad a size to 64 bits (for event sizes) */
  40. static inline uint16_t
  41. lv2_event_pad_size(uint16_t size)
  42. {
  43. return (uint16_t)(size + 7U) & (uint16_t)(~7U);
  44. }
  45. /** Initialize (empty, reset..) an existing event buffer.
  46. * The contents of buf are ignored entirely and overwritten, except capacity
  47. * which is unmodified. */
  48. static inline void
  49. lv2_event_buffer_reset(LV2_Event_Buffer* buf,
  50. uint16_t stamp_type,
  51. uint8_t* data)
  52. {
  53. buf->data = data;
  54. buf->header_size = sizeof(LV2_Event_Buffer);
  55. buf->stamp_type = stamp_type;
  56. buf->event_count = 0;
  57. buf->size = 0;
  58. }
  59. /** Allocate a new, empty event buffer. */
  60. static inline LV2_Event_Buffer*
  61. lv2_event_buffer_new(uint32_t capacity, uint16_t stamp_type)
  62. {
  63. const size_t size = sizeof(LV2_Event_Buffer) + capacity;
  64. LV2_Event_Buffer* buf = (LV2_Event_Buffer*)malloc(size);
  65. if (buf != NULL) {
  66. buf->capacity = capacity;
  67. lv2_event_buffer_reset(buf, stamp_type, (uint8_t *)(buf + 1));
  68. return buf;
  69. } else {
  70. return NULL;
  71. }
  72. }
  73. /** An iterator over an LV2_Event_Buffer.
  74. *
  75. * Multiple simultaneous read iterators over a single buffer is fine,
  76. * but changing the buffer invalidates all iterators (e.g. RW Lock). */
  77. typedef struct {
  78. LV2_Event_Buffer* buf;
  79. uint32_t offset;
  80. } LV2_Event_Iterator;
  81. /** Reset an iterator to point to the start of `buf`.
  82. * @return True if `iter` is valid, otherwise false (buffer is empty) */
  83. static inline bool
  84. lv2_event_begin(LV2_Event_Iterator* iter,
  85. LV2_Event_Buffer* buf)
  86. {
  87. iter->buf = buf;
  88. iter->offset = 0;
  89. return (buf->size > 0);
  90. }
  91. /** Check if `iter` is valid.
  92. * @return True if `iter` is valid, otherwise false (past end of buffer) */
  93. static inline bool
  94. lv2_event_is_valid(LV2_Event_Iterator* iter)
  95. {
  96. return (iter->buf && (iter->offset < iter->buf->size));
  97. }
  98. /** Advance `iter` forward one event.
  99. * `iter` must be valid.
  100. * @return True if `iter` is valid, otherwise false (reached end of buffer) */
  101. static inline bool
  102. lv2_event_increment(LV2_Event_Iterator* iter)
  103. {
  104. if (!lv2_event_is_valid(iter)) {
  105. return false;
  106. }
  107. LV2_Event* const ev = (LV2_Event*)(
  108. (uint8_t*)iter->buf->data + iter->offset);
  109. iter->offset += lv2_event_pad_size(
  110. (uint16_t)((uint16_t)sizeof(LV2_Event) + ev->size));
  111. return true;
  112. }
  113. /** Dereference an event iterator (get the event currently pointed at).
  114. * `iter` must be valid.
  115. * `data` if non-NULL, will be set to point to the contents of the event
  116. * returned.
  117. * @return A Pointer to the event `iter` is currently pointing at, or NULL
  118. * if the end of the buffer is reached (in which case `data` is
  119. * also set to NULL). */
  120. static inline LV2_Event*
  121. lv2_event_get(LV2_Event_Iterator* iter,
  122. uint8_t** data)
  123. {
  124. if (!lv2_event_is_valid(iter)) {
  125. return NULL;
  126. }
  127. LV2_Event* const ev = (LV2_Event*)(
  128. (uint8_t*)iter->buf->data + iter->offset);
  129. if (data)
  130. *data = (uint8_t*)ev + sizeof(LV2_Event);
  131. return ev;
  132. }
  133. /** Write an event at `iter`.
  134. * The event (if any) pointed to by `iter` will be overwritten, and `iter`
  135. * incremented to point to the following event (i.e. several calls to this
  136. * function can be done in sequence without twiddling iter in-between).
  137. * @return True if event was written, otherwise false (buffer is full). */
  138. static inline bool
  139. lv2_event_write(LV2_Event_Iterator* iter,
  140. uint32_t frames,
  141. uint32_t subframes,
  142. uint16_t type,
  143. uint16_t size,
  144. const uint8_t* data)
  145. {
  146. if (!iter->buf)
  147. return false;
  148. if (iter->buf->capacity - iter->buf->size < sizeof(LV2_Event) + size)
  149. return false;
  150. LV2_Event* const ev = (LV2_Event*)(
  151. (uint8_t*)iter->buf->data + iter->offset);
  152. ev->frames = frames;
  153. ev->subframes = subframes;
  154. ev->type = type;
  155. ev->size = size;
  156. memcpy((uint8_t*)ev + sizeof(LV2_Event), data, size);
  157. ++iter->buf->event_count;
  158. size = lv2_event_pad_size((uint16_t)(sizeof(LV2_Event) + size));
  159. iter->buf->size += size;
  160. iter->offset += size;
  161. return true;
  162. }
  163. /** Reserve space for an event in the buffer and return a pointer to
  164. the memory where the caller can write the event data, or NULL if there
  165. is not enough room in the buffer. */
  166. static inline uint8_t*
  167. lv2_event_reserve(LV2_Event_Iterator* iter,
  168. uint32_t frames,
  169. uint32_t subframes,
  170. uint16_t type,
  171. uint16_t size)
  172. {
  173. const uint16_t total_size = (uint16_t)(sizeof(LV2_Event) + size);
  174. if (iter->buf->capacity - iter->buf->size < total_size)
  175. return NULL;
  176. LV2_Event* const ev = (LV2_Event*)(
  177. (uint8_t*)iter->buf->data + iter->offset);
  178. ev->frames = frames;
  179. ev->subframes = subframes;
  180. ev->type = type;
  181. ev->size = size;
  182. ++iter->buf->event_count;
  183. const uint16_t padded_size = lv2_event_pad_size(total_size);
  184. iter->buf->size += padded_size;
  185. iter->offset += padded_size;
  186. return (uint8_t*)ev + sizeof(LV2_Event);
  187. }
  188. /** Write an event at `iter`.
  189. * The event (if any) pointed to by `iter` will be overwritten, and `iter`
  190. * incremented to point to the following event (i.e. several calls to this
  191. * function can be done in sequence without twiddling iter in-between).
  192. * @return True if event was written, otherwise false (buffer is full). */
  193. static inline bool
  194. lv2_event_write_event(LV2_Event_Iterator* iter,
  195. const LV2_Event* ev,
  196. const uint8_t* data)
  197. {
  198. const uint16_t total_size = (uint16_t)(sizeof(LV2_Event) + ev->size);
  199. if (iter->buf->capacity - iter->buf->size < total_size)
  200. return false;
  201. LV2_Event* const write_ev = (LV2_Event*)(
  202. (uint8_t*)iter->buf->data + iter->offset);
  203. *write_ev = *ev;
  204. memcpy((uint8_t*)write_ev + sizeof(LV2_Event), data, ev->size);
  205. ++iter->buf->event_count;
  206. const uint16_t padded_size = lv2_event_pad_size(total_size);
  207. iter->buf->size += padded_size;
  208. iter->offset += padded_size;
  209. return true;
  210. }
  211. #ifdef __cplusplus
  212. } /* extern "C" */
  213. #endif
  214. #endif /* LV2_EVENT_HELPERS_H */