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