-
Notifications
You must be signed in to change notification settings - Fork 1
/
midiout_windows.cpp
96 lines (95 loc) · 3.24 KB
/
midiout_windows.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
//=============================================================================
// ■ MIDIOut
//-----------------------------------------------------------------------------
// One of the Lua modules for communicating with MIDI devices.
//=============================================================================
namespace MIDIOut {
//-------------------------------------------------------------------------
// ● Module variables
//-------------------------------------------------------------------------
void (*const ensure_success)(MMRESULT result) = MIDIIn::ensure_success;
//-------------------------------------------------------------------------
// ● check_midiout
//-------------------------------------------------------------------------
inline HMIDIOUT check_midiout(lua_State* L, int index) {
return (HMIDIOUT) Util::check_usertable(L, index, "MIDIOut");
}
//-------------------------------------------------------------------------
// ● for index, name in devices()
// index is 1-based.
//-------------------------------------------------------------------------
int lua_devices_iterator(lua_State* L) {
int count = lua_tointeger(L, 1);
int index = lua_tointeger(L, 2);
if (index < count) {
MIDIOUTCAPS caps;
midiOutGetDevCaps(index, &caps, sizeof(caps));
lua_pushnumber(L, index + 1);
lua_pushstring(L, caps.szPname);
return 2;
} else {
return 0;
}
}
int lua_devices(lua_State* L) {
lua_pushcfunction(L, lua_devices_iterator);
lua_pushnumber(L, midiOutGetNumDevs());
lua_pushnumber(L, 0);
return 3;
}
//-------------------------------------------------------------------------
// ● close
//-------------------------------------------------------------------------
int lua_close(lua_State* L) {
ensure_success(midiOutClose(check_midiout(L, lua_upvalueindex(1))));
return 0;
}
//-------------------------------------------------------------------------
// ● send_short_message(byte1, byte2, byte3)
//-------------------------------------------------------------------------
int lua_send_short_message(lua_State* L) {
ensure_success(midiOutShortMsg(
check_midiout(L, lua_upvalueindex(1)),
luaL_checkinteger(L, 1)
| (luaL_checkinteger(L, 2) << 8)
| (luaL_checkinteger(L, 3) << 16)
));
return 0;
}
//-------------------------------------------------------------------------
// ● new(index)
//-------------------------------------------------------------------------
int lua_new_instance(lua_State* L) {
// t = {}
lua_createtable(L, 1, 4);
// t[0] = HMIDIOUT
HMIDIOUT hmo;
ensure_success(midiOutOpen(
&hmo,
luaL_checkinteger(L, 1) - 1,
0, 0, CALLBACK_NULL
));
lua_pushlightuserdata(L, hmo);
lua_rawseti(L, -2, 0);
const luaL_reg reg[] = {
{"close", lua_close},
{"send_short_message", lua_send_short_message},
{NULL, NULL}
};
Util::instance_register(L, reg);
return 1;
}
//-------------------------------------------------------------------------
// ● init
//-------------------------------------------------------------------------
void init() {
const luaL_reg reg[] = {
{"new", lua_new_instance},
{"open", lua_new_instance},
{"devices", lua_devices},
{NULL, NULL}
};
luaL_register(L, "MIDIOut", reg);
lua_pop(L, 1);
}
}