chris@163: /** chris@163: * Copyright (c) 2014, 2015, Enzien Audio Ltd. chris@163: * chris@163: * Permission to use, copy, modify, and/or distribute this software for any chris@163: * purpose with or without fee is hereby granted, provided that the above chris@163: * copyright notice and this permission notice appear in all copies. chris@163: * chris@163: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH chris@163: * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY chris@163: * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, chris@163: * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM chris@163: * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR chris@163: * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR chris@163: * PERFORMANCE OF THIS SOFTWARE. chris@163: */ chris@163: chris@163: #ifndef _HEAVY_BASE_H_ chris@163: #define _HEAVY_BASE_H_ chris@163: chris@163: #include "MessageQueue.h" chris@163: #include "Utils.h" chris@163: chris@163: #define Base(_x) ((HvBase *) _x) chris@163: chris@163: typedef struct HvBase { chris@163: int numInputChannels; chris@163: int numOutputChannels; chris@163: double sampleRate; chris@163: hv_uint32_t blockStartTimestamp; chris@163: unsigned int numBytes; // the total number of bytes allocated for this patch chris@163: void (*f_scheduleMessageForReceiver)(struct HvBase *const, const char *, HvMessage *); chris@163: struct HvTable *(*f_getTableForHash)(struct HvBase *const, hv_uint32_t); chris@163: MessageQueue mq; chris@163: void (*printHook)(double, const char *, const char *, void *); chris@163: void (*sendHook)(double, const char *, const HvMessage *const, void *); chris@163: char *basePath; chris@163: void *userData; chris@163: const char *name; chris@163: } HvBase; chris@163: chris@163: /** chris@163: * Schedule a message in the message queue according to its timestamp. chris@163: * The copy of the message added to the queue is returned. chris@163: */ chris@163: static inline HvMessage *ctx_scheduleMessage(HvBase *_c, const HvMessage *const m, chris@163: void (*sendMessage)(HvBase *, int, const HvMessage *), int outletIndex) { chris@163: return mq_addMessageByTimestamp(&_c->mq, (HvMessage *) m, outletIndex, sendMessage); chris@163: } chris@163: chris@163: static inline void ctx_scheduleMessageForReceiver(HvBase *const _c, chris@163: const char *name, HvMessage *m) { chris@163: _c->f_scheduleMessageForReceiver(_c, name, m); chris@163: } chris@163: chris@163: void ctx_scheduleMessageForReceiverV(HvBase *const _c, const char *name, chris@163: const hv_uint32_t timestamp, const char *format, ...); chris@163: chris@163: void ctx_cancelMessage(HvBase *_c, HvMessage *m, chris@163: void (*sendMessage)(HvBase *, int, const HvMessage *)); chris@163: chris@163: static inline int ctx_millisecondsToSamples(HvBase *_c, float timeInMs) { chris@163: return (int) (timeInMs * _c->sampleRate / 1000.0); chris@163: } chris@163: chris@163: static inline double ctx_samplesToMilliseconds(HvBase *_c, int samples) { chris@163: return 1000.0 * samples / _c->sampleRate; chris@163: } chris@163: chris@163: static inline double ctx_getSampleRate(HvBase *_c) { chris@163: return _c->sampleRate; chris@163: } chris@163: chris@163: static inline int ctx_getNumInputChannels(HvBase *_c) { chris@163: return _c->numInputChannels; chris@163: } chris@163: chris@163: static inline int ctx_getNumOutputChannels(HvBase *_c) { chris@163: return _c->numOutputChannels; chris@163: } chris@163: chris@163: static inline const char *ctx_getName(HvBase *_c) { chris@163: return _c->name; chris@163: } chris@163: chris@163: /** Returns the first sample of the block. */ chris@163: static inline hv_uint32_t ctx_getBlockStartTimestamp(HvBase *_c) { chris@163: return _c->blockStartTimestamp; chris@163: } chris@163: chris@163: static inline void ctx_setPrintHook(HvBase *const _c, void (*f)(double, chris@163: const char *, const char *, void *)) { chris@163: _c->printHook = f; chris@163: } chris@163: chris@163: static inline void ctx_setSendHook(HvBase *const _c, void (*f)(double, const char *, const HvMessage *const, void *)) { chris@163: _c->sendHook = f; chris@163: } chris@163: chris@163: static inline void *ctx_getUserData(HvBase *const _c) { chris@163: return _c->userData; chris@163: } chris@163: chris@163: static inline void ctx_setUserData(HvBase *const _c, void *userData) { chris@163: _c->userData = userData; chris@163: } chris@163: chris@163: void ctx_setBasePath(HvBase *const _c, const char *basePath); chris@163: chris@163: static inline const char *ctx_getBasePath(HvBase *const _c) { chris@163: return _c->basePath; chris@163: } chris@163: chris@163: static inline struct HvTable *ctx_getTableForHash(HvBase *const _c, hv_uint32_t h) { chris@163: return _c->f_getTableForHash(_c, h); chris@163: } chris@163: chris@163: static inline struct HvTable *ctx_getTableForName(HvBase *const _c, const char *tableName) { chris@163: return ctx_getTableForHash(_c, msg_symbolToHash(tableName)); chris@163: } chris@163: chris@163: /** Returns the total number of bytes allocated for this patch. */ chris@163: static inline unsigned int ctx_getNumBytes(HvBase *_c) { chris@163: return _c->numBytes; chris@163: } chris@163: chris@163: #endif // _HEAVY_BASE_H_