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