annotate projects/heavy/envelopeTrigger/MessageQueue.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
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 #ifndef _MESSAGE_QUEUE_H_
chris@162 18 #define _MESSAGE_QUEUE_H_
chris@162 19
chris@162 20 #include "HvMessage.h"
chris@162 21 #include "MessagePool.h"
chris@162 22
chris@162 23 struct HvBase;
chris@162 24
chris@162 25 typedef struct MessageNode {
chris@162 26 struct MessageNode *prev; // doubly linked list
chris@162 27 struct MessageNode *next;
chris@162 28 HvMessage *m;
chris@162 29 void (*sendMessage)(struct HvBase *, int, const HvMessage *);
chris@162 30 int let;
chris@162 31 } MessageNode;
chris@162 32
chris@162 33 /** A doubly linked list containing scheduled messages. */
chris@162 34 typedef struct MessageQueue {
chris@162 35 MessageNode *head; // the head of the queue
chris@162 36 MessageNode *tail; // the tail of the queue
chris@162 37 MessageNode *pool; // the head of the reserve pool
chris@162 38 MessagePool mp;
chris@162 39 } MessageQueue;
chris@162 40
chris@162 41 hv_size_t mq_init(MessageQueue *q);
chris@162 42
chris@162 43 void mq_initWithPoolSize(MessageQueue *q, hv_size_t poolSizeKB);
chris@162 44
chris@162 45 void mq_free(MessageQueue *q);
chris@162 46
chris@162 47 int mq_size(MessageQueue *q);
chris@162 48
chris@162 49 static inline HvMessage *mq_node_getMessage(MessageNode *n) {
chris@162 50 return n->m;
chris@162 51 }
chris@162 52
chris@162 53 static inline int mq_node_getLet(MessageNode *n) {
chris@162 54 return n->let;
chris@162 55 }
chris@162 56
chris@162 57 static inline bool mq_hasMessage(MessageQueue *q) {
chris@162 58 return (q->head != NULL);
chris@162 59 }
chris@162 60
chris@162 61 // true if there is a message and it occurs before (<) timestamp
chris@162 62 static inline bool mq_hasMessageBefore(MessageQueue *const q, const hv_uint32_t timestamp) {
chris@162 63 return mq_hasMessage(q) && (msg_getTimestamp(mq_node_getMessage(q->head)) < timestamp);
chris@162 64 }
chris@162 65
chris@162 66 static inline MessageNode *mq_peek(MessageQueue *q) {
chris@162 67 return q->head;
chris@162 68 }
chris@162 69
chris@162 70 /** Appends the message to the end of the queue. */
chris@162 71 HvMessage *mq_addMessage(MessageQueue *q, const HvMessage *m, int let,
chris@162 72 void (*sendMessage)(struct HvBase *, int, const HvMessage *));
chris@162 73
chris@162 74 /** Insert in ascending order the message acccording to its timestamp. */
chris@162 75 HvMessage *mq_addMessageByTimestamp(MessageQueue *q, HvMessage *m, int let,
chris@162 76 void (*sendMessage)(struct HvBase *, int, const HvMessage *));
chris@162 77
chris@162 78 /** Pop the message at the head of the queue (and free its memory). */
chris@162 79 void mq_pop(MessageQueue *q);
chris@162 80
chris@162 81 /** Remove a message from the queue (and free its memory) */
chris@162 82 void mq_removeMessage(MessageQueue *q, HvMessage *m,
chris@162 83 void (*sendMessage)(struct HvBase *, int, const HvMessage *));
chris@162 84
chris@162 85 /** Clears (and frees) all messages in the queue. */
chris@162 86 void mq_clear(MessageQueue *q);
chris@162 87
chris@162 88 /** Removes all messages occuring at or after the given timestamp. */
chris@162 89 void mq_clearAfter(MessageQueue *q, const double timestamp);
chris@162 90
chris@162 91 #endif // _MESSAGE_QUEUE_H_