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