annotate projects/heavy/envelopeTrigger/MessageQueue.c @ 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
rev   line source
chris@162 1 /**
chris@162 2 * Copyright (c) 2014, 2015, Enzien Audio Ltd.
chris@162 3 *
chris@162 4 * Permission to use, copy, modify, and/or distribute this software for any
chris@162 5 * purpose with or without fee is hereby granted, provided that the above
chris@162 6 * copyright notice and this permission notice appear in all copies.
chris@162 7 *
chris@162 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
chris@162 9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
chris@162 10 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
chris@162 11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
chris@162 12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
chris@162 13 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
chris@162 14 * PERFORMANCE OF THIS SOFTWARE.
chris@162 15 */
chris@162 16
chris@162 17 #include "MessageQueue.h"
chris@162 18 #include "Utils.h"
chris@162 19
chris@162 20 hv_size_t mq_init(MessageQueue *q) {
chris@162 21 q->head = NULL;
chris@162 22 q->tail = NULL;
chris@162 23 q->pool = NULL;
chris@162 24 return mp_init(&q->mp, 1);
chris@162 25 }
chris@162 26
chris@162 27 void mq_initWithPoolSize(MessageQueue *q, hv_size_t poolSizeKB) {
chris@162 28 q->head = NULL;
chris@162 29 q->tail = NULL;
chris@162 30 q->pool = NULL;
chris@162 31 mp_init(&q->mp, poolSizeKB);
chris@162 32 }
chris@162 33
chris@162 34 void mq_free(MessageQueue *q) {
chris@162 35 mq_clear(q);
chris@162 36 while (q->pool != NULL) {
chris@162 37 MessageNode *n = q->pool;
chris@162 38 q->pool = q->pool->next;
chris@162 39 hv_free(n);
chris@162 40 }
chris@162 41 mp_free(&q->mp);
chris@162 42 }
chris@162 43
chris@162 44 static MessageNode *mq_getOrCreateNodeFromPool(MessageQueue *q) {
chris@162 45 if (q->pool == NULL) {
chris@162 46 // if necessary, create a new empty node
chris@162 47 q->pool = (MessageNode *) hv_malloc(sizeof(MessageNode));
chris@162 48 q->pool->next = NULL;
chris@162 49 }
chris@162 50 MessageNode *node = q->pool;
chris@162 51 q->pool = q->pool->next;
chris@162 52 return node;
chris@162 53 }
chris@162 54
chris@162 55 int mq_size(MessageQueue *q) {
chris@162 56 int size = 0;
chris@162 57 MessageNode *n = q->head;
chris@162 58 while (n != NULL) {
chris@162 59 ++size;
chris@162 60 n = n->next;
chris@162 61 }
chris@162 62 return size;
chris@162 63 }
chris@162 64
chris@162 65 HvMessage *mq_addMessage(MessageQueue *q, const HvMessage *m, int let,
chris@162 66 void (*sendMessage)(struct HvBase *, int, const HvMessage *)) {
chris@162 67 MessageNode *node = mq_getOrCreateNodeFromPool(q);
chris@162 68 node->m = mp_addMessage(&q->mp, m);
chris@162 69 node->let = let;
chris@162 70 node->sendMessage = sendMessage;
chris@162 71 node->prev = NULL;
chris@162 72 node->next = NULL;
chris@162 73
chris@162 74 if (q->tail != NULL) {
chris@162 75 // the list already contains elements
chris@162 76 q->tail->next = node;
chris@162 77 node->prev = q->tail;
chris@162 78 q->tail = node;
chris@162 79 } else {
chris@162 80 // the list is empty
chris@162 81 node->prev = NULL;
chris@162 82 q->head = node;
chris@162 83 q->tail = node;
chris@162 84 }
chris@162 85 return mq_node_getMessage(node);
chris@162 86 }
chris@162 87
chris@162 88 HvMessage *mq_addMessageByTimestamp(MessageQueue *q, HvMessage *m, int let,
chris@162 89 void (*sendMessage)(struct HvBase *, int, const HvMessage *)) {
chris@162 90 if (mq_hasMessage(q)) {
chris@162 91 MessageNode *n = mq_getOrCreateNodeFromPool(q);
chris@162 92 n->m = mp_addMessage(&q->mp, m);
chris@162 93 n->let = let;
chris@162 94 n->sendMessage = sendMessage;
chris@162 95
chris@162 96 if (msg_getTimestamp(m) < msg_getTimestamp(q->head->m)) {
chris@162 97 // the message occurs before the current head
chris@162 98 n->next = q->head;
chris@162 99 q->head->prev = n;
chris@162 100 n->prev = NULL;
chris@162 101 q->head = n;
chris@162 102 } else if (msg_getTimestamp(m) >= msg_getTimestamp(q->tail->m)) {
chris@162 103 // the message occurs after the current tail
chris@162 104 n->next = NULL;
chris@162 105 n->prev = q->tail;
chris@162 106 q->tail->next = n;
chris@162 107 q->tail = n;
chris@162 108 } else {
chris@162 109 // the message occurs somewhere between the head and tail
chris@162 110 MessageNode *node = q->head;
chris@162 111 while (node != NULL) {
chris@162 112 if (m->timestamp < msg_getTimestamp(node->next->m)) {
chris@162 113 MessageNode *r = node->next;
chris@162 114 node->next = n;
chris@162 115 n->next = r;
chris@162 116 n->prev = node;
chris@162 117 r->prev = n;
chris@162 118 break;
chris@162 119 }
chris@162 120 node = node->next;
chris@162 121 }
chris@162 122 }
chris@162 123 return n->m;
chris@162 124 } else {
chris@162 125 // add a message to the head
chris@162 126 return mq_addMessage(q, m, let, sendMessage);
chris@162 127 }
chris@162 128 }
chris@162 129
chris@162 130 void mq_pop(MessageQueue *q) {
chris@162 131 if (mq_hasMessage(q)) {
chris@162 132 MessageNode *n = q->head;
chris@162 133
chris@162 134 mp_freeMessage(&q->mp, n->m);
chris@162 135 n->m = NULL;
chris@162 136
chris@162 137 n->let = 0;
chris@162 138 n->sendMessage = NULL;
chris@162 139
chris@162 140 q->head = n->next;
chris@162 141 if (q->head == NULL) {
chris@162 142 q->tail = NULL;
chris@162 143 } else {
chris@162 144 q->head->prev = NULL;
chris@162 145 }
chris@162 146 n->next = q->pool;
chris@162 147 n->prev = NULL;
chris@162 148 q->pool = n;
chris@162 149 }
chris@162 150 }
chris@162 151
chris@162 152 void mq_removeMessage(MessageQueue *q, HvMessage *m, void (*sendMessage)(struct HvBase *, int, const HvMessage *)) {
chris@162 153 if (mq_hasMessage(q)) {
chris@162 154 if (mq_node_getMessage(q->head) == m) { // msg in head node
chris@162 155 // only remove the message if sendMessage is the same as the stored one,
chris@162 156 // if the sendMessage argument is NULL, it is not checked and will remove any matching message pointer
chris@162 157 if (sendMessage == NULL || q->head->sendMessage == sendMessage) {
chris@162 158 mq_pop(q);
chris@162 159 }
chris@162 160 } else {
chris@162 161 MessageNode *prevNode = q->head;
chris@162 162 MessageNode *currNode = q->head->next;
chris@162 163 while ((currNode != NULL) && (currNode->m != m)) {
chris@162 164 prevNode = currNode;
chris@162 165 currNode = currNode->next;
chris@162 166 }
chris@162 167 if (currNode != NULL) {
chris@162 168 if (sendMessage == NULL || currNode->sendMessage == sendMessage) {
chris@162 169 mp_freeMessage(&q->mp, m);
chris@162 170 currNode->m = NULL;
chris@162 171 currNode->let = 0;
chris@162 172 currNode->sendMessage = NULL;
chris@162 173 if (currNode == q->tail) { // msg in tail node
chris@162 174 prevNode->next = NULL;
chris@162 175 q->tail = prevNode;
chris@162 176 } else { // msg in middle node
chris@162 177 prevNode->next = currNode->next;
chris@162 178 currNode->next->prev = prevNode;
chris@162 179 }
chris@162 180 currNode->next = (q->pool == NULL) ? NULL : q->pool;
chris@162 181 currNode->prev = NULL;
chris@162 182 q->pool = currNode;
chris@162 183 }
chris@162 184 }
chris@162 185 }
chris@162 186 }
chris@162 187 }
chris@162 188
chris@162 189 void mq_clear(MessageQueue *q) {
chris@162 190 while (mq_hasMessage(q)) {
chris@162 191 mq_pop(q);
chris@162 192 }
chris@162 193 }
chris@162 194
chris@162 195 void mq_clearAfter(MessageQueue *q, const double timestamp) {
chris@162 196 MessageNode *n = q->tail;
chris@162 197 while (n != NULL && timestamp <= msg_getTimestamp(n->m)) {
chris@162 198 // free the node's message
chris@162 199 mp_freeMessage(&q->mp, n->m);
chris@162 200 n->m = NULL;
chris@162 201 n->let = 0;
chris@162 202 n->sendMessage = NULL;
chris@162 203
chris@162 204 // the tail points at the previous node
chris@162 205 q->tail = n->prev;
chris@162 206
chris@162 207 // put the node back in the pool
chris@162 208 n->next = q->pool;
chris@162 209 n->prev = NULL;
chris@162 210 if (q->pool != NULL) q->pool->prev = n;
chris@162 211 q->pool = n;
chris@162 212
chris@162 213 // update the tail node
chris@162 214 n = q->tail;
chris@162 215 }
chris@162 216
chris@162 217 if (q->tail == NULL) q->head = NULL;
chris@162 218 }