comparison projects/heavy/envelopeTrigger/HvMessage.h @ 162:c3e8226a5651 heavy-updated

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