annotate projects/heavy/circularBuffer/HvBase.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_BASE_H_
chris@163 18 #define _HEAVY_BASE_H_
chris@163 19
chris@163 20 #include "MessageQueue.h"
chris@163 21 #include "Utils.h"
chris@163 22
chris@163 23 #define Base(_x) ((HvBase *) _x)
chris@163 24
chris@163 25 typedef struct HvBase {
chris@163 26 int numInputChannels;
chris@163 27 int numOutputChannels;
chris@163 28 double sampleRate;
chris@163 29 hv_uint32_t blockStartTimestamp;
chris@163 30 unsigned int numBytes; // the total number of bytes allocated for this patch
chris@163 31 void (*f_scheduleMessageForReceiver)(struct HvBase *const, const char *, HvMessage *);
chris@163 32 struct HvTable *(*f_getTableForHash)(struct HvBase *const, hv_uint32_t);
chris@163 33 MessageQueue mq;
chris@163 34 void (*printHook)(double, const char *, const char *, void *);
chris@163 35 void (*sendHook)(double, const char *, const HvMessage *const, void *);
chris@163 36 char *basePath;
chris@163 37 void *userData;
chris@163 38 const char *name;
chris@163 39 } HvBase;
chris@163 40
chris@163 41 /**
chris@163 42 * Schedule a message in the message queue according to its timestamp.
chris@163 43 * The copy of the message added to the queue is returned.
chris@163 44 */
chris@163 45 static inline HvMessage *ctx_scheduleMessage(HvBase *_c, const HvMessage *const m,
chris@163 46 void (*sendMessage)(HvBase *, int, const HvMessage *), int outletIndex) {
chris@163 47 return mq_addMessageByTimestamp(&_c->mq, (HvMessage *) m, outletIndex, sendMessage);
chris@163 48 }
chris@163 49
chris@163 50 static inline void ctx_scheduleMessageForReceiver(HvBase *const _c,
chris@163 51 const char *name, HvMessage *m) {
chris@163 52 _c->f_scheduleMessageForReceiver(_c, name, m);
chris@163 53 }
chris@163 54
chris@163 55 void ctx_scheduleMessageForReceiverV(HvBase *const _c, const char *name,
chris@163 56 const hv_uint32_t timestamp, const char *format, ...);
chris@163 57
chris@163 58 void ctx_cancelMessage(HvBase *_c, HvMessage *m,
chris@163 59 void (*sendMessage)(HvBase *, int, const HvMessage *));
chris@163 60
chris@163 61 static inline int ctx_millisecondsToSamples(HvBase *_c, float timeInMs) {
chris@163 62 return (int) (timeInMs * _c->sampleRate / 1000.0);
chris@163 63 }
chris@163 64
chris@163 65 static inline double ctx_samplesToMilliseconds(HvBase *_c, int samples) {
chris@163 66 return 1000.0 * samples / _c->sampleRate;
chris@163 67 }
chris@163 68
chris@163 69 static inline double ctx_getSampleRate(HvBase *_c) {
chris@163 70 return _c->sampleRate;
chris@163 71 }
chris@163 72
chris@163 73 static inline int ctx_getNumInputChannels(HvBase *_c) {
chris@163 74 return _c->numInputChannels;
chris@163 75 }
chris@163 76
chris@163 77 static inline int ctx_getNumOutputChannels(HvBase *_c) {
chris@163 78 return _c->numOutputChannels;
chris@163 79 }
chris@163 80
chris@163 81 static inline const char *ctx_getName(HvBase *_c) {
chris@163 82 return _c->name;
chris@163 83 }
chris@163 84
chris@163 85 /** Returns the first sample of the block. */
chris@163 86 static inline hv_uint32_t ctx_getBlockStartTimestamp(HvBase *_c) {
chris@163 87 return _c->blockStartTimestamp;
chris@163 88 }
chris@163 89
chris@163 90 static inline void ctx_setPrintHook(HvBase *const _c, void (*f)(double,
chris@163 91 const char *, const char *, void *)) {
chris@163 92 _c->printHook = f;
chris@163 93 }
chris@163 94
chris@163 95 static inline void ctx_setSendHook(HvBase *const _c, void (*f)(double, const char *, const HvMessage *const, void *)) {
chris@163 96 _c->sendHook = f;
chris@163 97 }
chris@163 98
chris@163 99 static inline void *ctx_getUserData(HvBase *const _c) {
chris@163 100 return _c->userData;
chris@163 101 }
chris@163 102
chris@163 103 static inline void ctx_setUserData(HvBase *const _c, void *userData) {
chris@163 104 _c->userData = userData;
chris@163 105 }
chris@163 106
chris@163 107 void ctx_setBasePath(HvBase *const _c, const char *basePath);
chris@163 108
chris@163 109 static inline const char *ctx_getBasePath(HvBase *const _c) {
chris@163 110 return _c->basePath;
chris@163 111 }
chris@163 112
chris@163 113 static inline struct HvTable *ctx_getTableForHash(HvBase *const _c, hv_uint32_t h) {
chris@163 114 return _c->f_getTableForHash(_c, h);
chris@163 115 }
chris@163 116
chris@163 117 static inline struct HvTable *ctx_getTableForName(HvBase *const _c, const char *tableName) {
chris@163 118 return ctx_getTableForHash(_c, msg_symbolToHash(tableName));
chris@163 119 }
chris@163 120
chris@163 121 /** Returns the total number of bytes allocated for this patch. */
chris@163 122 static inline unsigned int ctx_getNumBytes(HvBase *_c) {
chris@163 123 return _c->numBytes;
chris@163 124 }
chris@163 125
chris@163 126 #endif // _HEAVY_BASE_H_