-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmidi_sender.c
372 lines (307 loc) · 12.8 KB
/
midi_sender.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include "midi_sender.h"
#define AUPROC_CAPI_IMPLEMENT_GET_CAPI 1
#include "auproc_capi.h"
#define SENDER_CAPI_IMPLEMENT_GET_CAPI 1
#include "sender_capi.h"
/* ============================================================================================ */
static const char* const MIDI_SENDER_CLASS_NAME = "auproc.midi_sender";
static const char* ERROR_INVALID_MIDI_SENDER = "invalid auproc.midi_sender";
/* ============================================================================================ */
typedef struct MidiSenderUserData MidiSenderUserData;
struct MidiSenderUserData
{
const char* className;
auproc_processor* processor;
bool closed;
bool activated;
const auproc_capi* auprocCapi;
auproc_engine* auprocEngine;
auproc_connector* midiOutConnector;
const auproc_midimeth* midiMethods;
const sender_capi* senderCapi;
sender_object* sender;
sender_reader* senderReader;
bool hasNextEvent;
uint32_t nextEventFrame;
sender_capi_value senderValue;
const void* nextEventBytes;
size_t nextEventBytesCount;
};
/* ============================================================================================ */
static void setupMidiSenderMeta(lua_State* L);
static int pushMidiSenderMeta(lua_State* L)
{
if (luaL_newmetatable(L, MIDI_SENDER_CLASS_NAME)) {
setupMidiSenderMeta(L);
}
return 1;
}
/* ============================================================================================ */
static MidiSenderUserData* checkMidiSenderUdata(lua_State* L, int arg)
{
MidiSenderUserData* udata = luaL_checkudata(L, arg, MIDI_SENDER_CLASS_NAME);
if (udata->auprocCapi) {
udata->auprocCapi->checkEngineIsNotClosed(L, udata->auprocEngine);
}
if (udata->closed) {
luaL_error(L, ERROR_INVALID_MIDI_SENDER);
return NULL;
}
return udata;
}
/* ============================================================================================ */
static int processCallback(uint32_t nframes, void* processorData)
{
MidiSenderUserData* udata = (MidiSenderUserData*) processorData;
const auproc_capi* auprocCapi = udata->auprocCapi;
const auproc_midimeth* methods = udata->midiMethods;
auproc_midibuf* outBuf = methods->getMidiBuffer(udata->midiOutConnector, nframes);
methods->clearBuffer(outBuf);
const sender_capi* senderCapi = udata->senderCapi;
sender_object* sender = udata->sender;
sender_reader* reader = udata->senderReader;
sender_capi_value* senderValue = &udata->senderValue;
uint32_t f0 = auprocCapi->getProcessBeginFrameTime(udata->auprocEngine);
uint32_t f1 = f0 + nframes;
nextEvent:
if (!udata->nextEventBytes) {
int rc = senderCapi->nextMessageFromSender(sender, reader,
false /* nonblock */, 0 /* timeout */,
NULL /* errorHandler */, NULL /* errorHandlerData */);
if (rc == 0) {
senderCapi->nextValueFromReader(reader, senderValue);
if (senderValue->type != SENDER_CAPI_TYPE_NONE) {
bool hasT = false;
uint32_t t = f0;
if (senderValue->type == SENDER_CAPI_TYPE_INTEGER) {
hasT = true;
t = senderValue->intVal;
} else if (senderValue->type == SENDER_CAPI_TYPE_NUMBER) {
hasT = true;
t = senderValue->numVal;
}
if (hasT) {
senderCapi->nextValueFromReader(reader, senderValue);
}
if (senderValue->type == SENDER_CAPI_TYPE_ARRAY) {
sender_array_type type = senderValue->arrayVal.type;
if (type == SENDER_UCHAR || type == SENDER_SCHAR) {
udata->nextEventFrame = t;
udata->nextEventBytes = senderValue->arrayVal.data;
udata->nextEventBytesCount = senderValue->arrayVal.elementCount;
}
}
else if (senderValue->type == SENDER_CAPI_TYPE_STRING) {
udata->nextEventFrame = t;
udata->nextEventBytes = senderValue->strVal.ptr;
udata->nextEventBytesCount = senderValue->strVal.len;
}
if (!udata->nextEventBytes) {
senderCapi->clearReader(reader);
}
}
}
}
if (udata->nextEventBytes) {
uint32_t f = udata->nextEventFrame;
if (f < f1) {
if (f >= f0) {
unsigned char* data = methods->reserveMidiEvent(outBuf, f-f0, udata->nextEventBytesCount);
if (data) {
memcpy(data, udata->nextEventBytes, udata->nextEventBytesCount);
}
f0 = f;
}
senderCapi->clearReader(reader);
udata->nextEventBytes = NULL;
goto nextEvent;
}
}
return 0;
}
/* ============================================================================================ */
static void engineClosedCallback(void* processorData)
{
MidiSenderUserData* udata = (MidiSenderUserData*) processorData;
udata->closed = true;
udata->activated = false;
}
static void engineReleasedCallback(void* processorData)
{
MidiSenderUserData* udata = (MidiSenderUserData*) processorData;
udata->closed = true;
udata->activated = false;
udata->auprocCapi = NULL;
udata->auprocEngine = NULL;
}
/* ============================================================================================ */
static int MidiSender_new(lua_State* L)
{
const int conArg = 1;
const int sndrArg = 2;
MidiSenderUserData* udata = lua_newuserdata(L, sizeof(MidiSenderUserData));
memset(udata, 0, sizeof(MidiSenderUserData));
udata->className = MIDI_SENDER_CLASS_NAME;
pushMidiSenderMeta(L); /* -> udata, meta */
lua_setmetatable(L, -2); /* -> udata */
int versionError = 0;
const auproc_capi* capi = auproc_get_capi(L, conArg, &versionError);
auproc_engine* engine = NULL;
if (capi) {
engine = capi->getEngine(L, conArg, NULL);
}
if (!capi || !engine) {
if (versionError) {
return luaL_argerror(L, conArg, "auproc version mismatch");
} else {
return luaL_argerror(L, conArg, "expected connector object");
}
}
int errReason = 0;
const sender_capi* senderCapi = sender_get_capi(L, sndrArg, &errReason);
if (!senderCapi) {
if (errReason == 1) {
return luaL_argerror(L, sndrArg, "sender capi version mismatch");
} else {
return luaL_argerror(L, sndrArg, "expected object with sender capi");
}
}
sender_object* sender = senderCapi->toSender(L, sndrArg);
if (!sender) {
return luaL_argerror(L, sndrArg, "expected object with sender capi");
}
udata->senderCapi = senderCapi;
udata->sender = sender;
senderCapi->retainSender(sender);
udata->senderReader = senderCapi->newReader(16 * 1024, 1);
if (!udata->senderReader) {
return luaL_error(L, "out of memory");
}
const char* processorName = lua_pushfstring(L, "%s: %p", MIDI_SENDER_CLASS_NAME, udata); /* -> udata, name */
auproc_con_reg conReg = {AUPROC_MIDI, AUPROC_OUT, NULL};
auproc_con_reg_err regError = {0};
auproc_processor* proc = capi->registerProcessor(L, conArg, 1, engine, processorName, udata,
processCallback, NULL, engineClosedCallback, engineReleasedCallback,
&conReg, ®Error);
lua_pop(L, 1); /* -> udata */
if (!proc)
{
if (regError.errorType == AUPROC_REG_ERR_CONNCTOR_INVALID) {
return luaL_argerror(L, conArg, "invalid connector object");
}
else if (regError.errorType == AUPROC_REG_ERR_ENGINE_MISMATCH)
{
const char* msg = lua_pushfstring(L, "connector belongs to other %s",
capi->engine_category_name);
return luaL_argerror(L, conArg, msg);
}
else if (regError.errorType == AUPROC_REG_ERR_ARG_INVALID
|| regError.errorType == AUPROC_REG_ERR_WRONG_DIRECTION
|| regError.errorType == AUPROC_REG_ERR_WRONG_CONNECTOR_TYPE)
{
return luaL_argerror(L, conArg, "expected MIDI OUT connector");
}
else {
return luaL_error(L, "cannot register processor (err=%d)", regError.errorType);
}
}
udata->processor = proc;
udata->activated = false;
udata->auprocCapi = capi;
udata->auprocEngine = engine;
udata->midiOutConnector = conReg.connector;
udata->midiMethods = conReg.midiMethods;
return 1;
}
/* ============================================================================================ */
static int MidiSender_release(lua_State* L)
{
MidiSenderUserData* udata = luaL_checkudata(L, 1, MIDI_SENDER_CLASS_NAME);
udata->closed = true;
udata->activated = false;
if (udata->auprocCapi) {
udata->auprocCapi->unregisterProcessor(L, udata->auprocEngine, udata->processor);
udata->processor = NULL;
udata->auprocCapi = NULL;
udata->auprocEngine = NULL;
}
if (udata->sender) {
if (udata->senderReader) {
udata->senderCapi->freeReader(udata->senderReader);
udata->senderReader = NULL;
}
udata->senderCapi->releaseSender(udata->sender);
udata->sender = NULL;
udata->senderCapi = NULL;
}
return 0;
}
/* ============================================================================================ */
static int MidiSender_toString(lua_State* L)
{
MidiSenderUserData* udata = luaL_checkudata(L, 1, MIDI_SENDER_CLASS_NAME);
lua_pushfstring(L, "%s: %p", MIDI_SENDER_CLASS_NAME, udata);
return 1;
}
/* ============================================================================================ */
static int MidiSender_activate(lua_State* L)
{
MidiSenderUserData* udata = checkMidiSenderUdata(L, 1);
if (!udata->activated) {
udata->auprocCapi->activateProcessor(L, udata->auprocEngine, udata->processor);
udata->activated = true;
}
return 0;
}
/* ============================================================================================ */
static int MidiSender_deactivate(lua_State* L)
{
MidiSenderUserData* udata = checkMidiSenderUdata(L, 1);
if (udata->activated) {
udata->auprocCapi->deactivateProcessor(L, udata->auprocEngine, udata->processor);
udata->activated = false;
}
return 0;
}
/* ============================================================================================ */
static const luaL_Reg MidiSenderMethods[] =
{
{ "activate", MidiSender_activate },
{ "deactivate", MidiSender_deactivate },
{ "close", MidiSender_release },
{ NULL, NULL } /* sentinel */
};
static const luaL_Reg MidiSenderMetaMethods[] =
{
{ "__tostring", MidiSender_toString },
{ "__gc", MidiSender_release },
{ NULL, NULL } /* sentinel */
};
static const luaL_Reg ModuleFunctions[] =
{
{ "new_midi_sender", MidiSender_new },
{ NULL, NULL } /* sentinel */
};
/* ============================================================================================ */
static void setupMidiSenderMeta(lua_State* L)
{ /* -> meta */
lua_pushstring(L, MIDI_SENDER_CLASS_NAME); /* -> meta, className */
lua_setfield(L, -2, "__metatable"); /* -> meta */
luaL_setfuncs(L, MidiSenderMetaMethods, 0); /* -> meta */
lua_newtable(L); /* -> meta, MidiSenderClass */
luaL_setfuncs(L, MidiSenderMethods, 0); /* -> meta, MidiSenderClass */
lua_setfield (L, -2, "__index"); /* -> meta */
}
/* ============================================================================================ */
int auproc_midi_sender_init_module(lua_State* L, int module)
{
if (luaL_newmetatable(L, MIDI_SENDER_CLASS_NAME)) {
setupMidiSenderMeta(L);
}
lua_pop(L, 1);
lua_pushvalue(L, module);
luaL_setfuncs(L, ModuleFunctions, 0);
lua_pop(L, 1);
return 0;
}
/* ============================================================================================ */