annotate projects/heavy/circularBuffer/HvMessage.h @ 163:20b52283c7b4 heavy-updated

- added circular buffer pd/heavy example (works but process needs to be killed manually if launched via ssh?)
author chnrx <chris.heinrichs@gmail.com>
date Thu, 12 Nov 2015 15:55:30 +0000
parents
children
rev   line source
chris@163 1 /**
chris@163 2 * Copyright (c) 2014, 2015, Enzien Audio Ltd.
chris@163 3 *
chris@163 4 * Permission to use, copy, modify, and/or distribute this software for any
chris@163 5 * purpose with or without fee is hereby granted, provided that the above
chris@163 6 * copyright notice and this permission notice appear in all copies.
chris@163 7 *
chris@163 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
chris@163 9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
chris@163 10 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
chris@163 11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
chris@163 12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
chris@163 13 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
chris@163 14 * PERFORMANCE OF THIS SOFTWARE.
chris@163 15 */
chris@163 16
chris@163 17 #ifndef _HEAVY_MESSAGE_H_
chris@163 18 #define _HEAVY_MESSAGE_H_
chris@163 19
chris@163 20 #include "Utils.h"
chris@163 21
chris@163 22 typedef enum ElementType {
chris@163 23 BANG,
chris@163 24 FLOAT,
chris@163 25 SYMBOL,
chris@163 26 HASH
chris@163 27 } ElementType;
chris@163 28
chris@163 29 typedef struct Element {
chris@163 30 ElementType type;
chris@163 31 union {
chris@163 32 float f; // float
chris@163 33 char *s; // symbol
chris@163 34 hv_uint32_t h; // hash
chris@163 35 } data;
chris@163 36 } Element;
chris@163 37
chris@163 38 typedef struct HvMessage {
chris@163 39 hv_uint32_t timestamp; // the sample at which this message should be processed
chris@163 40 hv_uint16_t numElements;
chris@163 41 hv_uint16_t numBytes; // the number of bytes that this message occupies in memory
chris@163 42 Element elem;
chris@163 43 } HvMessage;
chris@163 44
chris@163 45 #define HV_MESSAGE_ON_STACK(_x) (HvMessage *) hv_alloca(msg_getByteSize(_x))
chris@163 46
chris@163 47 /** Returns the total length in bytes of this message for a given number of elements. */
chris@163 48 static inline hv_size_t msg_getByteSize(hv_size_t numElements) {
chris@163 49 hv_assert(numElements > 0);
chris@163 50 return sizeof(HvMessage) + ((numElements-1) * sizeof(Element));
chris@163 51 }
chris@163 52
chris@163 53 HvMessage *msg_copy(const HvMessage *m);
chris@163 54
chris@163 55 /** Returns the number of bytes that this message would occupy on the heap. */
chris@163 56 hv_size_t msg_getNumHeapBytes(const HvMessage *m);
chris@163 57
chris@163 58 /** Copies the message into the given buffer. The buffer must be at least as large as msg_getNumHeapBytes(). */
chris@163 59 void msg_copyToBuffer(const HvMessage *m, char *buffer, hv_size_t len);
chris@163 60
chris@163 61 void msg_setElementToFrom(HvMessage *n, int indexN, const HvMessage *const m, int indexM);
chris@163 62
chris@163 63 /** Frees a message on the heap. Does nothing if argument is NULL. */
chris@163 64 void msg_free(HvMessage *m);
chris@163 65
chris@163 66 HvMessage *msg_init(HvMessage *m, hv_size_t numElements, hv_uint32_t timestamp);
chris@163 67
chris@163 68 HvMessage *msg_initWithFloat(HvMessage *m, hv_uint32_t timestamp, float f);
chris@163 69
chris@163 70 HvMessage *msg_initWithBang(HvMessage *m, hv_uint32_t timestamp);
chris@163 71
chris@163 72 HvMessage *msg_initWithSymbol(HvMessage *m, hv_uint32_t timestamp, char *s);
chris@163 73
chris@163 74 HvMessage *msg_initWithHash(HvMessage *m, hv_uint32_t timestamp, hv_uint32_t h);
chris@163 75
chris@163 76 HvMessage *msg_initV(HvMessage *const m, const hv_uint32_t timestamp, const char *format, ...);
chris@163 77
chris@163 78 static inline hv_uint32_t msg_getTimestamp(const HvMessage *m) {
chris@163 79 return m->timestamp;
chris@163 80 }
chris@163 81
chris@163 82 static inline void msg_setTimestamp(HvMessage *m, hv_uint32_t timestamp) {
chris@163 83 m->timestamp = timestamp;
chris@163 84 }
chris@163 85
chris@163 86 static inline int msg_getNumElements(const HvMessage *m) {
chris@163 87 return (int) m->numElements;
chris@163 88 }
chris@163 89
chris@163 90 /** Returns the number of bytes this message in memory. */
chris@163 91 static inline hv_size_t msg_getNumBytes(const HvMessage *m) {
chris@163 92 return m->numBytes;
chris@163 93 }
chris@163 94
chris@163 95 static inline ElementType msg_getType(const HvMessage *m, int index) {
chris@163 96 hv_assert(index < msg_getNumElements(m)); // invalid index
chris@163 97 return (&(m->elem)+index)->type;
chris@163 98 }
chris@163 99
chris@163 100 static inline void msg_setBang(HvMessage *m, int index) {
chris@163 101 hv_assert(index < msg_getNumElements(m)); // invalid index
chris@163 102 (&(m->elem)+index)->type = BANG;
chris@163 103 (&(m->elem)+index)->data.s = NULL;
chris@163 104 }
chris@163 105
chris@163 106 static inline bool msg_isBang(const HvMessage *m, int index) {
chris@163 107 return (index < msg_getNumElements(m)) ? (msg_getType(m,index) == BANG) : false;
chris@163 108 }
chris@163 109
chris@163 110 static inline void msg_setFloat(HvMessage *m, int index, float f) {
chris@163 111 hv_assert(index < msg_getNumElements(m)); // invalid index
chris@163 112 (&(m->elem)+index)->type = FLOAT;
chris@163 113 (&(m->elem)+index)->data.f = f;
chris@163 114 }
chris@163 115
chris@163 116 static inline float msg_getFloat(const HvMessage *const m, int index) {
chris@163 117 hv_assert(index < msg_getNumElements(m)); // invalid index
chris@163 118 return (&(m->elem)+index)->data.f;
chris@163 119 }
chris@163 120
chris@163 121 static inline bool msg_isFloat(const HvMessage *const m, int index) {
chris@163 122 return (index < msg_getNumElements(m)) ? (msg_getType(m,index) == FLOAT) : false;
chris@163 123 }
chris@163 124
chris@163 125 static inline void msg_setHash(HvMessage *m, int index, hv_uint32_t h) {
chris@163 126 hv_assert(index < msg_getNumElements(m)); // invalid index
chris@163 127 (&(m->elem)+index)->type = HASH;
chris@163 128 (&(m->elem)+index)->data.h = h;
chris@163 129 }
chris@163 130
chris@163 131 static inline bool msg_isHash(const HvMessage *m, int index) {
chris@163 132 return (index < msg_getNumElements(m)) ? (msg_getType(m, index) == HASH) : false;
chris@163 133 }
chris@163 134
chris@163 135 /** Returns true if the element is a hash or symbol. False otherwise. */
chris@163 136 static inline bool msg_isHashLike(const HvMessage *m, int index) {
chris@163 137 return (index < msg_getNumElements(m)) ? ((msg_getType(m, index) == HASH) || (msg_getType(m, index) == SYMBOL)) : false;
chris@163 138 }
chris@163 139
chris@163 140 /** Returns a 32-bit hash of the given string. */
chris@163 141 hv_uint32_t msg_symbolToHash(const char *s);
chris@163 142
chris@163 143 /** Returns a 32-bit hash of the given element. */
chris@163 144 hv_uint32_t msg_getHash(const HvMessage *const m, int i);
chris@163 145
chris@163 146 static inline void msg_setSymbol(HvMessage *m, int index, char *s) {
chris@163 147 hv_assert(index < msg_getNumElements(m)); // invalid index
chris@163 148 (&(m->elem)+index)->type = SYMBOL;
chris@163 149 (&(m->elem)+index)->data.s = s;
chris@163 150 }
chris@163 151
chris@163 152 static inline char *msg_getSymbol(const HvMessage *m, int index) {
chris@163 153 hv_assert(index < msg_getNumElements(m)); // invalid index
chris@163 154 return (&(m->elem)+index)->data.s;
chris@163 155 }
chris@163 156
chris@163 157 static inline bool msg_isSymbol(const HvMessage *m, int index) {
chris@163 158 return (index < msg_getNumElements(m)) ? (msg_getType(m, index) == SYMBOL) : false;
chris@163 159 }
chris@163 160
chris@163 161 bool msg_compareSymbol(const HvMessage *m, int i, const char *s);
chris@163 162
chris@163 163 /** Returns 1 if the element i_m of message m is equal to element i_n of message n. */
chris@163 164 bool msg_equalsElement(const HvMessage *m, int i_m, const HvMessage *n, int i_n);
chris@163 165
chris@163 166 bool msg_hasFormat(const HvMessage *m, const char *fmt);
chris@163 167
chris@163 168 /**
chris@163 169 * Create a string representation of the message. Suitable for use by the print object.
chris@163 170 * The resulting string must be freed by the caller.
chris@163 171 */
chris@163 172 char *msg_toString(const HvMessage *msg);
chris@163 173
chris@163 174 /**
chris@163 175 * Resolves any number of dollar arguments and generates a string based on the arguments.
chris@163 176 * @param m The message from which to take values
chris@163 177 * @param n The message to fill in
chris@163 178 * @param z The element index to resolve
chris@163 179 * @param buf The scratch (i.e. resolution) buffer
chris@163 180 * @param len The length of the scratch buffer
chris@163 181 * @param args A string of 'i' and 's' chars indicating the type of the arguments, either indicies or strings
chris@163 182 * @param varargs The components to resolve, either dollar indicies or strings.
chris@163 183 * If the index is negative, the graph id is used
chris@163 184 * @return true if the resolution buffer is needed, false otherwise.
chris@163 185 */
chris@163 186 // bool msg_resolveDollarArguments(HvMessage *m, HvMessage *n, int z, char *buf, hv_size_t len, const char *args, ...);
chris@163 187
chris@163 188 #endif // _HEAVY_MESSAGE_H_