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.

250 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. LV2_Atom_Sequence atoms;
  48. } LV2_Atom_Buffer;
  49. // Clear and initialize an existing LV2 atom:Sequenece buffer.
  50. //
  51. static inline
  52. void lv2_atom_buffer_reset ( LV2_Atom_Buffer *buf, bool input )
  53. {
  54. if (input) {
  55. buf->atoms.atom.size = sizeof(LV2_Atom_Sequence_Body);
  56. buf->atoms.atom.type = buf->sequence_type;
  57. } else {
  58. buf->atoms.atom.size = buf->capacity;
  59. buf->atoms.atom.type = buf->chunk_type;
  60. }
  61. }
  62. // Allocate a new, empty LV2 atom:Sequence buffer.
  63. //
  64. static inline
  65. LV2_Atom_Buffer *lv2_atom_buffer_new (
  66. uint32_t capacity, uint32_t chunk_type, uint32_t sequence_type, bool input )
  67. {
  68. LV2_Atom_Buffer *buf = (LV2_Atom_Buffer *)
  69. malloc(sizeof(LV2_Atom_Buffer) + sizeof(LV2_Atom_Sequence) + capacity);
  70. buf->capacity = capacity;
  71. buf->chunk_type = chunk_type;
  72. buf->sequence_type = sequence_type;
  73. lv2_atom_buffer_reset(buf, input);
  74. return buf;
  75. }
  76. // Free an LV2 atom:Sequenece buffer allocated with lv2_atome_buffer_new.
  77. //
  78. static inline
  79. void lv2_atom_buffer_free ( LV2_Atom_Buffer *buf )
  80. {
  81. free(buf);
  82. }
  83. // Return the total padded size of events stored in a LV2 atom:Sequence buffer.
  84. //
  85. static inline
  86. uint32_t lv2_atom_buffer_get_size ( LV2_Atom_Buffer *buf )
  87. {
  88. if (buf->atoms.atom.type == buf->sequence_type)
  89. return buf->atoms.atom.size - uint32_t(sizeof(LV2_Atom_Sequence_Body));
  90. else
  91. return 0;
  92. }
  93. // Return the actual LV2 atom:Sequence implementation.
  94. //
  95. static inline
  96. LV2_Atom_Sequence *lv2_atom_buffer_get_sequence ( LV2_Atom_Buffer *buf )
  97. {
  98. return &buf->atoms;
  99. }
  100. // An iterator over an atom:Sequence buffer.
  101. //
  102. typedef
  103. struct _LV2_Atom_Buffer_Iterator
  104. {
  105. LV2_Atom_Buffer *buf;
  106. uint32_t offset;
  107. } LV2_Atom_Buffer_Iterator;
  108. // Reset an iterator to point to the start of an LV2 atom:Sequence buffer.
  109. //
  110. static inline
  111. bool lv2_atom_buffer_begin (
  112. LV2_Atom_Buffer_Iterator *iter, LV2_Atom_Buffer *buf )
  113. {
  114. iter->buf = buf;
  115. iter->offset = 0;
  116. return (buf->atoms.atom.size > 0);
  117. }
  118. // Reset an iterator to point to the end of an LV2 atom:Sequence buffer.
  119. //
  120. static inline
  121. bool lv2_atom_buffer_end (
  122. LV2_Atom_Buffer_Iterator *iter, LV2_Atom_Buffer *buf )
  123. {
  124. iter->buf = buf;
  125. iter->offset = lv2_atom_pad_size(lv2_atom_buffer_get_size(buf));
  126. return (iter->offset < buf->capacity - sizeof(LV2_Atom_Event));
  127. }
  128. // Check if a LV2 atom:Sequenece buffer iterator is valid.
  129. //
  130. static inline
  131. bool lv2_atom_buffer_is_valid ( LV2_Atom_Buffer_Iterator *iter )
  132. {
  133. return iter->offset < lv2_atom_buffer_get_size(iter->buf);
  134. }
  135. // Advance a LV2 atom:Sequenece buffer iterator forward one event.
  136. //
  137. static inline
  138. bool lv2_atom_buffer_increment ( LV2_Atom_Buffer_Iterator *iter )
  139. {
  140. if (!lv2_atom_buffer_is_valid(iter))
  141. return false;
  142. LV2_Atom_Buffer *buf = iter->buf;
  143. LV2_Atom_Sequence *atoms = &buf->atoms;
  144. uint32_t size = ((LV2_Atom_Event *) ((char *)
  145. LV2_ATOM_CONTENTS(LV2_Atom_Sequence, atoms) + iter->offset))->body.size;
  146. iter->offset += lv2_atom_pad_size(uint32_t(sizeof(LV2_Atom_Event)) + size);
  147. return true;
  148. }
  149. // Get the event currently pointed at a LV2 atom:Sequence buffer iterator.
  150. //
  151. static inline
  152. LV2_Atom_Event *lv2_atom_buffer_get (
  153. LV2_Atom_Buffer_Iterator *iter, uint8_t **data )
  154. {
  155. if (!lv2_atom_buffer_is_valid(iter))
  156. return NULL;
  157. LV2_Atom_Buffer *buf = iter->buf;
  158. LV2_Atom_Sequence *atoms = &buf->atoms;
  159. LV2_Atom_Event *ev = (LV2_Atom_Event *) ((char *)
  160. LV2_ATOM_CONTENTS(LV2_Atom_Sequence, atoms) + iter->offset);
  161. *data = (uint8_t *) LV2_ATOM_BODY(&ev->body);
  162. return ev;
  163. }
  164. // Write an event at a LV2 atom:Sequence buffer iterator.
  165. static inline
  166. bool lv2_atom_buffer_write (
  167. LV2_Atom_Buffer_Iterator *iter,
  168. uint32_t frames,
  169. uint32_t /*subframes*/,
  170. uint32_t type,
  171. uint32_t size,
  172. const uint8_t *data )
  173. {
  174. LV2_Atom_Buffer *buf = iter->buf;
  175. LV2_Atom_Sequence *atoms = &buf->atoms;
  176. if (buf->capacity - sizeof(LV2_Atom) - atoms->atom.size
  177. < sizeof(LV2_Atom_Event) + size)
  178. return false;
  179. LV2_Atom_Event *ev = (LV2_Atom_Event*) ((char *)
  180. LV2_ATOM_CONTENTS(LV2_Atom_Sequence, atoms) + iter->offset);
  181. ev->time.frames = frames;
  182. ev->body.type = type;
  183. ev->body.size = size;
  184. memcpy(LV2_ATOM_BODY(&ev->body), data, size);
  185. size = lv2_atom_pad_size(uint32_t(sizeof(LV2_Atom_Event)) + size);
  186. atoms->atom.size += size;
  187. iter->offset += size;
  188. return true;
  189. }
  190. #ifdef __cplusplus
  191. } /* extern "C" */
  192. #endif
  193. #endif // LV2_ATOM_HELPERS_H
  194. // end of lv2_atom_helpers.h