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-2012, 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.h"
  36. // An abstract/opaque LV2 atom:Sequence buffer.
  37. //
  38. typedef
  39. struct _LV2_Atom_Buffer
  40. {
  41. uint32_t capacity;
  42. uint32_t chunk_type;
  43. uint32_t sequence_type;
  44. LV2_Atom_Sequence atoms;
  45. } LV2_Atom_Buffer;
  46. // Pad a size to 64 bits (for LV2 atom:Sequence event sizes).
  47. //
  48. static inline
  49. uint32_t lv2_atom_buffer_pad_size ( uint32_t size )
  50. {
  51. return (size + 7) & (~7);
  52. }
  53. // Clear and initialize an existing LV2 atom:Sequenece buffer.
  54. //
  55. static inline
  56. void lv2_atom_buffer_reset ( LV2_Atom_Buffer *buf, bool input )
  57. {
  58. if (input)
  59. buf->atoms.atom.size = sizeof(LV2_Atom_Sequence_Body);
  60. else
  61. buf->atoms.atom.size = buf->capacity;
  62. buf->atoms.atom.type = buf->sequence_type;
  63. buf->atoms.body.unit = 0;
  64. buf->atoms.body.pad = 0;
  65. }
  66. // Allocate a new, empty LV2 atom:Sequence buffer.
  67. //
  68. static inline
  69. LV2_Atom_Buffer *lv2_atom_buffer_new (
  70. uint32_t capacity, uint32_t sequence_type, bool input )
  71. {
  72. LV2_Atom_Buffer *buf = (LV2_Atom_Buffer *)
  73. malloc(sizeof(LV2_Atom_Buffer) + sizeof(LV2_Atom_Sequence) + capacity);
  74. buf->capacity = capacity;
  75. buf->sequence_type = sequence_type;
  76. lv2_atom_buffer_reset(buf, input);
  77. return buf;
  78. }
  79. // Free an LV2 atom:Sequenece buffer allocated with lv2_atome_buffer_new.
  80. //
  81. static inline
  82. void lv2_atom_buffer_free ( LV2_Atom_Buffer *buf )
  83. {
  84. free(buf);
  85. }
  86. // Return the total padded size of events stored in a LV2 atom:Sequence buffer.
  87. //
  88. static inline
  89. uint32_t lv2_atom_buffer_get_size ( LV2_Atom_Buffer *buf )
  90. {
  91. return buf->atoms.atom.size - sizeof(LV2_Atom_Sequence_Body);
  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_buffer_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_buffer_pad_size(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_buffer_pad_size(sizeof(LV2_Atom_Event) + size);
  186. atoms->atom.size += size;
  187. iter->offset += size;
  188. return true;
  189. }
  190. #endif // LV2_ATOM_HELPERS_H
  191. // end of lv2_atom_helpers.h