Debian Codegraph @

CorsixTH/Src/th_lua_midi.cpp

raw
1 /*
2 Copyright (c) 2025 Stephen Baker
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23 #include "lua.hpp"
24 #include "midi_player.h"
25 #include "th_lua.h"
26 #include "th_lua_internal.h"
27
28 #ifdef WITH_MIDI_DEVICE
29
30 // Delegate object around midi_player with a close method
31 // suitable for adapting C++ RAII to lua garbage collection.
32 class th_lua_midi_player {
33 public:
34 th_lua_midi_player(std::string_view api, std::string_view port,
35 bool use_sysex)
36 : player(std::in_place, api, port, use_sysex) {}
37 th_lua_midi_player(const th_lua_midi_player&) = delete;
38 th_lua_midi_player& operator=(const th_lua_midi_player&) = delete;
39
40 std::vector<std::string> port_list() const {
41 if (!player.has_value()) {
42 throw std::runtime_error("th_lua_midi_player instance is closed");
43 }
44 return player->port_list();
45 }
46
47 void play_xmi(const unsigned char* xmiData, const size_t len) {
48 if (!player.has_value()) {
49 throw std::runtime_error("th_lua_midi_player instance is closed");
50 }
51 player->play_xmi(xmiData, len);
52 }
53
54 void set_volume(const double volume) {
55 if (!player.has_value()) {
56 throw std::runtime_error("th_lua_midi_player instance is closed");
57 }
58 player->set_volume(volume);
59 }
60
61 void stop() {
62 if (!player.has_value()) {
63 throw std::runtime_error("th_lua_midi_player instance is closed");
64 }
65 player->stop();
66 }
67
68 void pause() {
69 if (!player.has_value()) {
70 throw std::runtime_error("th_lua_midi_player instance is closed");
71 }
72 player->pause();
73 }
74
75 void resume() {
76 if (!player.has_value()) {
77 throw std::runtime_error("th_lua_midi_player instance is closed");
78 }
79 player->resume();
80 }
81
82 void close() { player.reset(); }
83
84 private:
85 std::optional<midi_player> player;
86 };
87 #else
88 class th_lua_midi_player {};
89 #endif
90
91 namespace {
92
93 #ifdef WITH_MIDI_DEVICE
94
95 int l_midi_player_api_list(lua_State* L) {
96 std::vector<std::string> apis = midi_player::api_list();
97
98 lua_createtable(L, static_cast<int>(apis.size()), 0);
99 for (size_t i = 0; i < apis.size(); ++i) {
100 lua_pushinteger(L, static_cast<lua_Integer>(i) + 1);
101 lua_pushstring(L, apis[i].c_str());
102 lua_settable(L, -3);
103 }
104
105 return 1;
106 }
107
108 int l_midi_player_new(lua_State* L) {
109 const char* apiChoice = luaL_optlstring(L, 2, "", nullptr);
110 const char* portChoice = luaL_optlstring(L, 3, "", nullptr);
111 bool sysexMasterVolume = lua_toboolean(L, 4);
112
113 try {
114 luaT_stdnew<th_lua_midi_player>(L, luaT_environindex, true, apiChoice,
115 portChoice, sysexMasterVolume);
116 } catch (const std::invalid_argument& e) {
117 return luaL_error(L, "Invalid MIDI API choice: %s", e.what());
118 }
119
120 return 1;
121 }
122
123 int l_midi_port_list(lua_State* L) {
124 th_lua_midi_player* midiPlayer = luaT_testuserdata<th_lua_midi_player>(L);
125 const std::vector<std::string> portList = midiPlayer->port_list();
126 lua_createtable(L, static_cast<int>(portList.size()), 0);
127 for (size_t i = 0; i < portList.size(); ++i) {
128 lua_pushinteger(L, static_cast<lua_Integer>(i) + 1);
129 lua_pushstring(L, portList[i].c_str());
130 lua_settable(L, -3);
131 }
132
133 return 1;
134 }
135
136 int l_midi_player_play_xmi(lua_State* L) {
137 th_lua_midi_player* midiPlayer = luaT_testuserdata<th_lua_midi_player>(L);
138
139 size_t xmiDataLength;
140 const uint8_t* xmiData = luaT_checkfile(L, 2, &xmiDataLength);
141
142 midiPlayer->play_xmi(xmiData, xmiDataLength);
143
144 return 0;
145 }
146
147 int l_midi_player_set_volume(lua_State* L) {
148 th_lua_midi_player* midiPlayer = luaT_testuserdata<th_lua_midi_player>(L);
149 double volume = static_cast<double>(luaL_checknumber(L, 2));
150 midiPlayer->set_volume(volume);
151
152 return 0;
153 }
154
155 int l_midi_player_stop(lua_State* L) {
156 th_lua_midi_player* midiPlayer = luaT_testuserdata<th_lua_midi_player>(L);
157 midiPlayer->stop();
158
159 return 0;
160 }
161
162 int l_midi_player_pause(lua_State* L) {
163 th_lua_midi_player* midiPlayer = luaT_testuserdata<th_lua_midi_player>(L);
164 midiPlayer->pause();
165 return 0;
166 }
167
168 int l_midi_player_resume(lua_State* L) {
169 th_lua_midi_player* midiPlayer = luaT_testuserdata<th_lua_midi_player>(L);
170 midiPlayer->resume();
171 return 0;
172 }
173
174 int l_midi_player_close(lua_State* L) {
175 th_lua_midi_player* midiPlayer = luaT_testuserdata<th_lua_midi_player>(L);
176 midiPlayer->close();
177 return 0;
178 }
179
180 #else // WITH_MIDI_DEVICE
181
182 int l_midi_player_api_list(lua_State* L) {
183 lua_newtable(L);
184
185 return 1;
186 }
187
188 int l_midi_player_new(lua_State* L) {
189 return luaL_error(L, "MIDI support not compiled in");
190 }
191
192 int l_midi_port_list(lua_State* L) {
193 return luaL_error(L, "MIDI support not compiled in");
194 }
195
196 int l_midi_player_play_xmi(lua_State* L) {
197 return luaL_error(L, "MIDI support not compiled in");
198 }
199
200 int l_midi_player_set_volume(lua_State* L) {
201 return luaL_error(L, "MIDI support not compiled in");
202 }
203
204 int l_midi_player_stop(lua_State* L) {
205 return luaL_error(L, "MIDI support not compiled in");
206 }
207
208 int l_midi_player_pause(lua_State* L) {
209 return luaL_error(L, "MIDI support not compiled in");
210 }
211
212 int l_midi_player_resume(lua_State* L) {
213 return luaL_error(L, "MIDI support not compiled in");
214 }
215
216 int l_midi_player_close(lua_State* L) { return 0; }
217
218 #endif // WITH_MIDI_DEVICE
219
220 } // namespace
221
222 void lua_register_midi(const lua_register_state* pState) {
223 lua_class_binding<th_lua_midi_player> lcb(
224 pState, "midiPlayer", l_midi_player_new, lua_metatable::midi_player);
225 // static
226 lcb.add_function(l_midi_player_api_list, "getAvailableApis");
227
228 // member
229 lcb.add_function(l_midi_port_list, "portList");
230 lcb.add_function(l_midi_player_play_xmi, "playXmi");
231 lcb.add_function(l_midi_player_set_volume, "setVolume");
232 lcb.add_function(l_midi_player_stop, "stop");
233 lcb.add_function(l_midi_player_pause, "pause");
234 lcb.add_function(l_midi_player_resume, "resume");
235 lcb.add_function(l_midi_player_close, "close");
236 }