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.

251 lines
5.9KB

  1. // lv2_atom_helpers.h
  2. //
  3. /****************************************************************************
  4. Copyright (C) 2005-2013, rncbc aka Rui Nuno Capela. All rights reserved.
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. *****************************************************************************/
  17. /* Helper functions for LV2 atom:Sequence event buffer.
  18. *
  19. * tentatively adapted from:
  20. *
  21. * - lv2_evbuf.h,c - An abstract/opaque LV2 event buffer implementation.
  22. *
  23. * - event-helpers.h - Helper functions for the LV2 Event extension.
  24. * <http://lv2plug.in/ns/ext/event>
  25. *
  26. * Copyright 2008-2012 David Robillard <http://drobilla.net>
  27. */
  28. #ifndef LV2_ATOM_HELPERS_H
  29. #define LV2_ATOM_HELPERS_H
  30. #include <stdint.h>
  31. #include <stdbool.h>
  32. #include <string.h>
  33. #include <stdlib.h>
  34. #include <assert.h>
  35. #include "atom-util.h"
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. // An abstract/opaque LV2 atom:Sequence buffer.
  40. //
  41. typedef
  42. struct _LV2_Atom_Buffer
  43. {
  44. uint32_t capacity;
  45. uint32_t chunk_type;
  46. uint32_t sequence_type;
  47. uint32_t _alignment_padding;
  48. LV2_Atom_Sequence atoms;
  49. } LV2_Atom_Buffer;
  50. // Clear and initialize an existing LV2 atom:Sequenece buffer.
  51. //
  52. static inline
  53. void lv2_atom_buffer_reset ( LV2_Atom_Buffer *buf, bool input )
  54. {
  55. if (input) {
  56. buf->atoms.atom.size = sizeof(LV2_Atom_Sequence_Body);
  57. buf->atoms.atom.type = buf->sequence_type;
  58. } else {
  59. buf->atoms.atom.size = buf->capacity;
  60. buf->atoms.atom.type = buf->chunk_type;
  61. }
  62. }
  63. // Allocate a new, empty LV2 atom:Sequence buffer.
  64. //
  65. static inline
  66. LV2_Atom_Buffer *lv2_atom_buffer_new (
  67. uint32_t capacity, uint32_t chunk_type, uint32_t sequence_type, bool input )
  68. {
  69. LV2_Atom_Buffer *buf = (LV2_Atom_Buffer *)
  70. malloc(sizeof(LV2_Atom_Buffer) + sizeof(LV2_Atom_Sequence) + capacity);
  71. buf->capacity = capacity;
  72. buf->chunk_type = chunk_type;
  73. buf->sequence_type = sequence_type;
  74. lv2_atom_buffer_reset(buf, input);
  75. return buf;
  76. }
  77. // Free an LV2 atom:Sequenece buffer allocated with lv2_atome_buffer_new.
  78. //
  79. static inline
  80. void lv2_atom_buffer_free ( LV2_Atom_Buffer *buf )
  81. {
  82. free(buf);
  83. }
  84. // Return the total padded size of events stored in a LV2 atom:Sequence buffer.
  85. //
  86. static inline
  87. uint32_t lv2_atom_buffer_get_size ( LV2_Atom_Buffer *buf )
  88. {
  89. if (buf->atoms.atom.type == buf->sequence_type)
  90. return buf->atoms.atom.size - uint32_t(sizeof(LV2_Atom_Sequence_Body));
  91. else
  92. return 0;
  93. }
  94. // Return the actual LV2 atom:Sequence implementation.
  95. //
  96. static inline
  97. LV2_Atom_Sequence *lv2_atom_buffer_get_sequence ( LV2_Atom_Buffer *buf )
  98. {
  99. return &buf->atoms;
  100. }
  101. // An iterator over an atom:Sequence buffer.
  102. //
  103. typedef
  104. struct _LV2_Atom_Buffer_Iterator
  105. {
  106. LV2_Atom_Buffer *buf;
  107. uint32_t offset;
  108. } LV2_Atom_Buffer_Iterator;
  109. // Reset an iterator to point to the start of an LV2 atom:Sequence buffer.
  110. //
  111. static inline
  112. bool lv2_atom_buffer_begin (
  113. LV2_Atom_Buffer_Iterator *iter, LV2_Atom_Buffer *buf )
  114. {
  115. iter->buf = buf;
  116. iter->offset = 0;
  117. return (buf->atoms.atom.size > 0);
  118. }
  119. // Reset an iterator to point to the end of an LV2 atom:Sequence buffer.
  120. //
  121. static inline
  122. bool lv2_atom_buffer_end (
  123. LV2_Atom_Buffer_Iterator *iter, LV2_Atom_Buffer *buf )
  124. {
  125. iter->buf = buf;
  126. iter->offset = lv2_atom_pad_size(lv2_atom_buffer_get_size(buf));
  127. return (iter->offset < buf->capacity - sizeof(LV2_Atom_Event));
  128. }
  129. // Check if a LV2 atom:Sequenece buffer iterator is valid.
  130. //
  131. static inline
  132. bool lv2_atom_buffer_is_valid ( LV2_Atom_Buffer_Iterator *iter )
  133. {
  134. return iter->offset < lv2_atom_buffer_get_size(iter->buf);
  135. }
  136. // Advance a LV2 atom:Sequenece buffer iterator forward one event.
  137. //
  138. static inline
  139. bool lv2_atom_buffer_increment ( LV2_Atom_Buffer_Iterator *iter )
  140. {
  141. if (!lv2_atom_buffer_is_valid(iter))
  142. return false;
  143. LV2_Atom_Buffer *buf = iter->buf;
  144. LV2_Atom_Sequence *atoms = &buf->atoms;
  145. uint32_t size = ((LV2_Atom_Event *) ((char *)
  146. LV2_ATOM_CONTENTS(LV2_Atom_Sequence, atoms) + iter->offset))->body.size;
  147. iter->offset += lv2_atom_pad_size(uint32_t(sizeof(LV2_Atom_Event)) + size);
  148. return true;
  149. }
  150. // Get the event currently pointed at a LV2 atom:Sequence buffer iterator.
  151. //
  152. static inline
  153. LV2_Atom_Event *lv2_atom_buffer_get (
  154. LV2_Atom_Buffer_Iterator *iter, uint8_t **data )
  155. {
  156. if (!lv2_atom_buffer_is_valid(iter))
  157. return NULL;
  158. LV2_Atom_Buffer *buf = iter->buf;
  159. LV2_Atom_Sequence *atoms = &buf->atoms;
  160. LV2_Atom_Event *ev = (LV2_Atom_Event *) ((char *)
  161. LV2_ATOM_CONTENTS(LV2_Atom_Sequence, atoms) + iter->offset);
  162. *data = (uint8_t *) LV2_ATOM_BODY(&ev->body);
  163. return ev;
  164. }
  165. // Write an event at a LV2 atom:Sequence buffer iterator.
  166. static inline
  167. bool lv2_atom_buffer_write (
  168. LV2_Atom_Buffer_Iterator *iter,
  169. uint32_t frames,
  170. uint32_t /*subframes*/,
  171. uint32_t type,
  172. uint32_t size,
  173. const uint8_t *data )
  174. {
  175. LV2_Atom_Buffer *buf = iter->buf;
  176. LV2_Atom_Sequence *atoms = &buf->atoms;
  177. if (buf->capacity - sizeof(LV2_Atom) - atoms->atom.size
  178. < sizeof(LV2_Atom_Event) + size)
  179. return false;
  180. LV2_Atom_Event *ev = (LV2_Atom_Event*) ((char *)
  181. LV2_ATOM_CONTENTS(LV2_Atom_Sequence, atoms) + iter->offset);
  182. ev->time.frames = frames;
  183. ev->body.type = type;
  184. ev->body.size = size;
  185. memcpy(LV2_ATOM_BODY(&ev->body), data, size);
  186. size = lv2_atom_pad_size(uint32_t(sizeof(LV2_Atom_Event)) + size);
  187. atoms->atom.size += size;
  188. iter->offset += size;
  189. return true;
  190. }
  191. #ifdef __cplusplus
  192. } /* extern "C" */
  193. #endif
  194. #endif // LV2_ATOM_HELPERS_H
  195. // end of lv2_atom_helpers.h