/
luamidi.cpp
711 lines (632 loc) · 15.3 KB
/
luamidi.cpp
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
extern "C"
{
#include "lua.h"
#include "lauxlib.h"
}
#include "RtMidi.h"
#include <map>
#include <iostream>
using namespace std;
#define LUAMIDI "LuaMIDI" // the name of the library
#define MIDIOUT "MidiOut" // the registered name of the MidiOut datatype
#define MIDIIN "MidiIn" // the registered name of the MidiIn datatype
static bool base0 = true; // the channels comming in are base0 or 1?
static RtMidiOut* OpenOut(lua_State *L, int port);
static void debugLua(lua_State *L)
{
for(int j = 0; j <= lua_gettop(L); ++j)
{
int i = j - lua_gettop(L);
cout << i << ": ";
if(lua_isboolean(L, i))
cout << "boolean " << lua_toboolean(L, i);
else if(lua_iscfunction(L, i))
cout << "cfunction " << lua_tocfunction(L, i);
else if(lua_isfunction(L, i))
cout << "function";
else if(lua_islightuserdata(L, i))
cout << "lightuserdata " << lua_touserdata(L, i);
else if(lua_isnil(L, i))
cout << "nil";
else if(lua_isnumber(L, i))
cout << "number " << lua_tonumber(L, i);
else if(lua_isstring(L, i))
cout << "string " << lua_tostring(L, i);
else if(lua_istable(L, i))
cout << "table";
else if(lua_isuserdata(L, i))
cout << "userdata " << lua_touserdata(L, i);
else
cout << "wtf?";
cout << endl;
}
cout << endl;
}
// nextout is used in the usual case where no port has yet been specified
// yet an RtMidiOut object still needs to be used for querying port counts.
// This way, we can create an object and have it ready for when the port is
// known
map<int, RtMidiOut*> outs; // outputs that have been opened
map<int, RtMidiIn*> ins; // inputs that have been opened
RtMidiOut* nextout = NULL; // an output that hasn't been opened
RtMidiIn* nextin = NULL;
// some basic lua helper methods:
// assume
// 0that table is at the top
void setfield(lua_State *L, const char *index, const char *value)
{
lua_pushstring(L, index);
lua_pushstring(L, value);
lua_settable(L, -3);
}
void setfield(lua_State *L, int index, const char *value)
{
lua_pushnumber(L, index);
lua_pushstring(L, value);
lua_settable(L, -3);
}
static int MidiIn_gc(lua_State *L);
static int MidiOut_gc(lua_State *L);
static int __gc(lua_State *L);
// some helper methods for accessing RtMidiOut:
static RtMidiOut* toMidiOut(lua_State *L, int index)
{
RtMidiOut **midiout = (RtMidiOut**)lua_touserdata(L, index);
if(midiout == NULL)
luaL_typerror(L, index, MIDIOUT);
return *midiout;
}
static RtMidiOut* checkMidiOut(lua_State* L, int index)
{
RtMidiOut** midiout;
RtMidiOut* ret;
luaL_checktype(L, index, LUA_TUSERDATA);
midiout = (RtMidiOut**)luaL_checkudata(L, index, MIDIOUT);
if(midiout == NULL)
luaL_typerror(L, index, MIDIOUT);
ret = *midiout;
if(!ret)
luaL_error(L, "null RtMidiOut*");
return ret;
}
static RtMidiOut** pushMidiOut(lua_State* L, RtMidiOut* mo)
{
#ifdef LUA_IS_LUAU
RtMidiOut** midiout = (RtMidiOut**)lua_newuserdatadtor(L, sizeof(RtMidiOut*),(void (*)(void*))MidiOut_gc);
#else
RtMidiOut** midiout = (RtMidiOut**)lua_newuserdata(L, sizeof(RtMidiOut*));
#endif
*midiout = mo;
luaL_getmetatable(L, MIDIOUT);
lua_setmetatable(L, -2);
return midiout;
}
// Some wrappers of the RtMidiOut class
static RtMidiOut* getGenericOutput()
{
// if a portless RtMidiOut has already been created, use it
if(nextout)
{
return nextout;
}
// if there aren't any MidiOuts yet, create a nextout
else if(outs.begin() == outs.end())
{
nextout = new RtMidiOut();
return nextout;
}
// we've got some ports opened already
else
{
return outs.begin()->second;
}
}
static RtMidiOut* OpenOut(lua_State *L, int port)
{
int portcount = getGenericOutput()->getPortCount();
if(port >= portcount)
luaL_error(L, "Invalid Port Number");
// if that port is already open
if(outs.find(port) != outs.end())
return outs[port];
else if(!nextout)
nextout = new RtMidiOut();
// if there is not a nextout make one.
if(port < 0)
nextout->openVirtualPort("mm looper");
else
nextout->openPort(port);
outs[port] = nextout;
nextout = NULL;
return outs[port];
}
// lua wrappers for methods that require a port and a RtMidiOut*
static int MidiOut_gc(lua_State *L)
{
//TODO: delete
return 0;
}
// some helper methods for accessing RtMidiIn:
static RtMidiIn* toMidiIn(lua_State *L, int index)
{
RtMidiIn **midiin = (RtMidiIn**)lua_touserdata(L, index);
if(midiin == NULL)
luaL_typerror(L, index, MIDIIN);
return *midiin;
}
static RtMidiIn* checkMidiIn(lua_State* L, int index)
{
RtMidiIn** midiin;
RtMidiIn* ret;
luaL_checktype(L, index, LUA_TUSERDATA);
midiin = (RtMidiIn**)luaL_checkudata(L, index, MIDIIN);
if(midiin == NULL)
luaL_typerror(L, index, MIDIIN);
ret = *midiin;
if(!ret)
luaL_error(L, "null RtMidiIn*");
return ret;
}
static RtMidiIn** pushMidiIn(lua_State* L, RtMidiIn* mi)
{
#ifdef LUA_IS_LUAU
RtMidiIn **midiin = (RtMidiIn**)lua_newuserdatadtor(L, sizeof(RtMidiIn*),(void (*)(void*))MidiIn_gc);
#else
RtMidiIn** midiin = (RtMidiIn**)lua_newuserdata(L, sizeof(RtMidiIn*));
#endif
*midiin = mi;
luaL_getmetatable(L, MIDIIN);
lua_setmetatable(L, -2);
return midiin;
}
// Some wrappers of the RtMidiIn class
static RtMidiIn* getGenericInput()
{
// if a portless RtMidiIn has already been created, use it
if(nextin)
{
return nextin;
}
// if there aren't any MidiIns yet, create a nextout
else if(ins.begin() == ins.end())
{
nextin = new RtMidiIn();
return nextin;
}
// we've got some ports opened already
else
{
return ins.begin()->second;
}
}
static int QUEUE_SIZE_LIMIT=1000;
static RtMidiIn* OpenIn(lua_State *L, int port)
{
int portcount = getGenericInput()->getPortCount();
if(port >= portcount)
luaL_error(L, "Invalid Port Number");
// if that port is already open
if(ins.find(port) != ins.end())
return ins[port];
else if(!nextin)
nextin = new RtMidiIn(RtMidi::UNSPECIFIED,"RtMidi Input Client",QUEUE_SIZE_LIMIT);
// if there is not a nextin make one.
if(port < 0)
nextin->openVirtualPort("in to MM");
else
nextin->openPort(port);
ins[port] = nextin;
nextin = NULL;
return ins[port];
}
// lua wrappers for methods that require a port and a RtMidiIn*
static int MidiIn_gc(lua_State *L)
{
//TODO: delete
return 0;
}
static int MidiOut_noteOn(lua_State *L)
{
try {
std::vector<unsigned char> message;
message.resize(3);
RtMidiOut* midiout = checkMidiOut(L, 1);
if(lua_isnumber(L, 4))
message[0] = 144;
message[1] = luaL_checkint(L, 2);
message[2] = 127;
if(lua_isnumber(L, 3))
message[2] = (unsigned char)lua_tonumber(L, 3);
if(lua_isnumber(L, 4))
{
int channel = luaL_checkint(L, 4);
if(!base0)
channel--;
if(channel < 0)
channel = 0;
else if(channel > 15)
channel = 15;
message[0] += channel;
}
midiout->sendMessage(&message);
} catch (RtMidiError &e) {
luaL_error(L,e.what());
}
return 0;
}
static int MidiOut_noteOff(lua_State *L)
{
try {
std::vector<unsigned char> message;
message.resize(3);
RtMidiOut* midiout = checkMidiOut(L, 1);
message[0] = 144;
message[1] = luaL_checkint(L, 2);
message[2] = 0;
if(lua_isnumber(L, 3))
{
int channel = luaL_checkint(L, 3);
if(!base0)
channel--;
if(channel < 0)
channel = 0;
else if(channel > 15)
channel = 15;
message[0] += channel;
}
midiout->sendMessage(&message);
} catch (RtMidiError &e) {
luaL_error(L,e.what());
}
return 0;
}
static int MidiOut_sendMessage(lua_State *L)
{
try {
std::vector<unsigned char> message;
message.resize(3);
RtMidiOut* midiout = checkMidiOut(L, 1);
message[0] = luaL_checkint(L, 2);
message[1] = luaL_checkint(L, 3);
message[2] = luaL_checkint(L, 4);
midiout->sendMessage(&message);
} catch (RtMidiError &e) {
luaL_error(L,e.what());
}
return 0;
}
// lua wrappers for the operations on RtMidiOut that do not require
// a port to be open.
static int luamidi_openout(lua_State* L)
{
try {
int port = luaL_checkint(L, 1);
pushMidiOut(L, OpenOut(L, port));
} catch (RtMidiError &e) {
luaL_error(L,e.what());
}
return 1;
}
static int luamidi_openin(lua_State* L)
{
try {
int port = luaL_checkint(L, 1);
pushMidiIn(L, OpenIn(L, port));
} catch (RtMidiError &e) {
luaL_error(L,e.what());
}
return 1;
}
static int luamidi_base0(lua_State *L)
{
luaL_checktype(L, 1, LUA_TBOOLEAN);
base0 = lua_toboolean(L, 1);
return 0;
}
static int luamidi_base1(lua_State *L)
{
luaL_checktype(L, 1, LUA_TBOOLEAN);
base0 = !lua_toboolean(L, 1);
return 0;
}
static int luamidi_noteOn(lua_State *L)
{
try {
std::vector<unsigned char> message;
message.resize(3);
int port = luaL_checkint(L, 1);
RtMidiOut* midiout = OpenOut(L, port);
message[0] = 144;
message[1] = luaL_checkint(L, 2);
message[2] = 127;
if(lua_isnumber(L, 3))
message[2] = (unsigned char)lua_tonumber(L, 3);
if(lua_isnumber(L, 4))
{
int channel = luaL_checkint(L, 4);
if(!base0)
channel--;
if(channel < 0)
channel = 0;
else if(channel > 15)
channel = 15;
message[0] += channel;
cout << "channel: " << channel << endl;
}
midiout->sendMessage(&message);
} catch (RtMidiError &e) {
luaL_error(L,e.what());
}
return 0;
}
static int luamidi_noteOff(lua_State *L)
{
try {
std::vector<unsigned char> message;
message.resize(3);
int port = luaL_checkint(L, 1);
RtMidiOut* midiout = OpenOut(L, port);
message[0] = 144;
message[1] = luaL_checkint(L, 2);
message[2] = 0;
if(lua_isnumber(L, 3))
{
int channel = luaL_checkint(L, 3);
if(!base0)
channel--;
if(channel < 0)
channel = 0;
else if(channel > 15)
channel = 15;
message[0] += channel;
}
midiout->sendMessage(&message);
} catch (RtMidiError &e) {
luaL_error(L,e.what());
}
return 0;
}
static int luamidi_sendMessage(lua_State *L)
{
try {
std::vector<unsigned char> message;
message.resize(3);
int port = luaL_checkint(L, 1);
RtMidiOut* midiout = OpenOut(L, port);
message[0] = luaL_checkint(L, 2);
message[1] = luaL_checkint(L, 3);
message[2] = luaL_checkint(L, 4);
if(lua_isnumber(L, 5))
{
int channel = luaL_checkint(L, 5);
if(!base0)
channel--;
if(channel < 0)
channel = 0;
else if(channel > 15)
channel = 15;
message[0] += channel;
}
midiout->sendMessage(&message);
} catch (RtMidiError &e) {
luaL_error(L,e.what());
}
return 0;
}
static int luamidi_getMessage(lua_State *L)
{
// cout << "getting..." << endl;
try {
std::vector<unsigned char> message;
int port = luaL_checkint(L, 1);
RtMidiIn* midiin = OpenIn(L, port);
double delta = midiin->getMessage(&message);
if(message.size() == 3)
{
lua_pushnumber(L, message[0]);
lua_pushnumber(L, message[1]);
lua_pushnumber(L, message[2]);
lua_pushnumber(L, delta);
return 4;
}
} catch (RtMidiError &e) {
luaL_error(L,e.what());
}
return 0;
}
static int luamidi_getoutportcount (lua_State *L)
{
// return true just to know that everything is working
lua_pushnumber(L, getGenericOutput()->getPortCount());
return 1;
}
static int luamidi_getinportcount(lua_State* L)
{
lua_pushnumber(L, getGenericInput()->getPortCount());
return 1;
}
//
// Better name pending.
// returns a table (array) where each key is a port number
// and each value is the name of that port
//
static int luamidi_enumerateoutports (lua_State *L)
{
// the return table
lua_newtable(L);
RtMidiOut* midi = getGenericOutput();
int portcount = midi->getPortCount();
std::string portname;
for(int i = 0; i < portcount; ++i)
{
setfield(L, i, midi->getPortName(i).c_str());
}
return 1;
}
//
// Better name pending.
// returns a table (array) where each key is a port number
// and each value is the name of that port
//
static int luamidi_enumerateinports (lua_State *L)
{
// the return table
lua_newtable(L);
RtMidiIn* midi = getGenericInput();
int portcount = midi->getPortCount();
std::string portname;
for(int i = 0; i < portcount; ++i)
{
setfield(L, i, midi->getPortName(i).c_str());
}
return 1;
}
// takes in a port number and returns the port's name
static int luamidi_getInPortName(lua_State *L)
{
int port = luaL_checkint(L, 1);
RtMidiIn* midi = getGenericInput();
if(port < midi->getPortCount())
lua_pushstring(L, midi->getPortName(port).c_str());
else
lua_pushstring(L, "");
return 1;
}
// takes in a port number and returns the port's name
static int luamidi_getOutPortName(lua_State *L)
{
int port = luaL_checkint(L, 1);
RtMidiOut* midi = getGenericOutput();
if(port < midi->getPortCount())
lua_pushstring(L, midi->getPortName(port).c_str());
else
lua_pushstring(L, "");
return 1;
}
static int luamidi_setQueueSizeLimit(lua_State *L)
{
QUEUE_SIZE_LIMIT = luaL_optinteger(L, 1, 1000);
return 0;
}
//
// Called when the module is cleaned up.
//
static int __gc(lua_State *L)
{
cout << "clean up" << endl;
// TODO: clean up here
// should all of the outs be cleaned up here or when they are garbade
// collected? Here for now.
for(map<int, RtMidiOut*>::iterator iter = outs.begin();
iter != outs.end(); ++iter)
{
delete iter->second;
}
outs.clear();
for(map<int, RtMidiIn*>::iterator iter = ins.begin();
iter != ins.end(); ++iter)
{
delete iter->second;
}
ins.clear();
if(nextout)
delete nextout;
if(nextin)
delete nextin;
return 0;
}
//
// Assumes the table is on top of the stack.
//
static void set_info (lua_State *L)
{
lua_pushliteral (L, "_COPYRIGHT");
lua_pushliteral (L, "Copyright (C) 2007 LuaMIDI");
lua_settable (L, -3);
lua_pushliteral (L, "_DESCRIPTION");
lua_pushliteral (L, "Provides a simple interface for MIDI I/O");
lua_settable (L, -3);
lua_pushliteral (L, "_VERSION");
lua_pushliteral (L, "LuaMIDI 0.1");
lua_settable (L, -3);
}
// these are methods that the library provides
static const luaL_reg luamidi_methods [] =
{
{"getoutportcount", luamidi_getoutportcount},
{"getinportcount", luamidi_getinportcount},
{"enumerateoutports", luamidi_enumerateoutports},
{"enumerateinports", luamidi_enumerateinports},
{"getInPortName", luamidi_getInPortName},
{"getOutPortName", luamidi_getOutPortName},
{"openout", luamidi_openout},
{"openin", luamidi_openin},
{"noteOn", luamidi_noteOn},
{"noteOff", luamidi_noteOff},
{"sendMessage", luamidi_sendMessage},
{"getMessage", luamidi_getMessage},
{"base0", luamidi_base0},
{"base1", luamidi_base1},
{"setQueueSizeLimit", luamidi_setQueueSizeLimit},
{"__gc", __gc},
{NULL, NULL}
};
// these are the methods that MidiOut objects have
static const luaL_reg MidiOut_meta[] =
{
{"__gc", MidiOut_gc},
{"noteOn", MidiOut_noteOn},
{"noteOff", MidiOut_noteOff},
{"sendMessage", MidiOut_sendMessage},
{NULL, NULL}
};
static int MidiOut_register(lua_State* L)
{
luaL_newmetatable(L, MIDIOUT);
lua_pushliteral(L, "__index");
lua_newtable(L);
luaL_register(L, 0, MidiOut_meta);
lua_rawset(L, -3);
lua_pop(L, 1);
return 1;
}
static const luaL_reg MidiIn_meta[] =
{
{"__gc", MidiIn_gc},
// {"getMessage", MidiIn_getMessage},
{NULL, NULL}
};
static int MidiIn_register(lua_State* L)
{
luaL_newmetatable(L, MIDIIN);
lua_pushliteral(L, "__index");
lua_newtable(L);
luaL_register(L, 0, MidiIn_meta);
lua_rawset(L, -3);
lua_pop(L, 1);
return 1;
}
extern "C"
{
int luaopen_luamidi (lua_State *L)
{
MidiOut_register(L);
MidiIn_register(L);
luaL_register (L, "luamidi", luamidi_methods);
set_info (L);
return 1;
}
}
static void g_initializePlugin(lua_State *L)
{
lua_getglobal(L, "package");
lua_getfield(L, -1, "preload");
lua_pushcnfunction(L, luaopen_luamidi,"luamidi_loader");
lua_setfield(L, -2, "luamidi");
lua_pop(L, 2);
}
static void g_deinitializePlugin(lua_State *L)
{
}
#include "gideros.h"
REGISTER_PLUGIN_NAMED("LuaMidi", "1.0", luamidi)