Debian Codegraph @

android/app/jni/extern/rtmidi/rtmidi_c.cpp

raw
1 #include <string.h>
2 #include <stdlib.h>
3 #include "rtmidi_c.h"
4 #include "RtMidi.h"
5
6 /* Compile-time assertions that will break if the enums are changed in
7 * the future without synchronizing them properly. If you get (g++)
8 * "error: ‘StaticAssert<b>::StaticAssert() [with bool b = false]’ is
9 * private within this context", it means enums are not aligned. */
10 template<bool b> class StaticAssert { private: StaticAssert() {} };
11 template<> class StaticAssert<true>{ public: StaticAssert() {} };
12 #define ENUM_EQUAL(x,y) StaticAssert<(int)x==(int)y>()
13 class StaticAssertions { StaticAssertions() {
14 ENUM_EQUAL( RTMIDI_API_UNSPECIFIED, RtMidi::UNSPECIFIED );
15 ENUM_EQUAL( RTMIDI_API_MACOSX_CORE, RtMidi::MACOSX_CORE );
16 ENUM_EQUAL( RTMIDI_API_LINUX_ALSA, RtMidi::LINUX_ALSA );
17 ENUM_EQUAL( RTMIDI_API_UNIX_JACK, RtMidi::UNIX_JACK );
18 ENUM_EQUAL( RTMIDI_API_WINDOWS_MM, RtMidi::WINDOWS_MM );
19 ENUM_EQUAL( RTMIDI_API_RTMIDI_DUMMY, RtMidi::RTMIDI_DUMMY );
20
21 ENUM_EQUAL( RTMIDI_ERROR_WARNING, RtMidiError::WARNING );
22 ENUM_EQUAL( RTMIDI_ERROR_DEBUG_WARNING, RtMidiError::DEBUG_WARNING );
23 ENUM_EQUAL( RTMIDI_ERROR_UNSPECIFIED, RtMidiError::UNSPECIFIED );
24 ENUM_EQUAL( RTMIDI_ERROR_NO_DEVICES_FOUND, RtMidiError::NO_DEVICES_FOUND );
25 ENUM_EQUAL( RTMIDI_ERROR_INVALID_DEVICE, RtMidiError::INVALID_DEVICE );
26 ENUM_EQUAL( RTMIDI_ERROR_MEMORY_ERROR, RtMidiError::MEMORY_ERROR );
27 ENUM_EQUAL( RTMIDI_ERROR_INVALID_PARAMETER, RtMidiError::INVALID_PARAMETER );
28 ENUM_EQUAL( RTMIDI_ERROR_INVALID_USE, RtMidiError::INVALID_USE );
29 ENUM_EQUAL( RTMIDI_ERROR_DRIVER_ERROR, RtMidiError::DRIVER_ERROR );
30 ENUM_EQUAL( RTMIDI_ERROR_SYSTEM_ERROR, RtMidiError::SYSTEM_ERROR );
31 ENUM_EQUAL( RTMIDI_ERROR_THREAD_ERROR, RtMidiError::THREAD_ERROR );
32 }};
33
34 class CallbackProxyUserData
35 {
36 public:
37 CallbackProxyUserData (RtMidiCCallback cCallback, void *userData)
38 : c_callback (cCallback), user_data (userData)
39 {
40 }
41 RtMidiCCallback c_callback;
42 void *user_data;
43 };
44
45 extern "C" const enum RtMidiApi rtmidi_compiled_apis[]; // casting from RtMidi::Api[]
46 extern "C" const unsigned int rtmidi_num_compiled_apis;
47
48 /* RtMidi API */
49 int rtmidi_get_compiled_api (enum RtMidiApi *apis, unsigned int apis_size)
50 {
51 unsigned num = rtmidi_num_compiled_apis;
52 if (apis) {
53 num = (num < apis_size) ? num : apis_size;
54 memcpy(apis, rtmidi_compiled_apis, num * sizeof(enum RtMidiApi));
55 }
56 return (int)num;
57 }
58
59 extern "C" const char* rtmidi_api_names[][2];
60 const char *rtmidi_api_name(enum RtMidiApi api) {
61 if (api < 0 || api >= RTMIDI_API_NUM)
62 return NULL;
63 return rtmidi_api_names[api][0];
64 }
65
66 const char *rtmidi_api_display_name(enum RtMidiApi api)
67 {
68 if (api < 0 || api >= RTMIDI_API_NUM)
69 return "Unknown";
70 return rtmidi_api_names[api][1];
71 }
72
73 enum RtMidiApi rtmidi_compiled_api_by_name(const char *name) {
74 RtMidi::Api api = RtMidi::UNSPECIFIED;
75 if (name) {
76 api = RtMidi::getCompiledApiByName(name);
77 }
78 return (enum RtMidiApi)api;
79 }
80
81 void rtmidi_error (MidiApi *api, enum RtMidiErrorType type, const char* errorString)
82 {
83 std::string msg = errorString;
84 api->error ((RtMidiError::Type) type, msg);
85 }
86
87 void rtmidi_open_port (RtMidiPtr device, unsigned int portNumber, const char *portName)
88 {
89 std::string name = portName;
90 try {
91 ((RtMidi*) device->ptr)->openPort (portNumber, name);
92
93 } catch (const RtMidiError & err) {
94 device->ok = false;
95 device->msg = err.what ();
96 }
97 }
98
99 void rtmidi_open_virtual_port (RtMidiPtr device, const char *portName)
100 {
101 std::string name = portName;
102 try {
103 ((RtMidi*) device->ptr)->openVirtualPort (name);
104
105 } catch (const RtMidiError & err) {
106 device->ok = false;
107 device->msg = err.what ();
108 }
109
110 }
111
112 void rtmidi_close_port (RtMidiPtr device)
113 {
114 try {
115 ((RtMidi*) device->ptr)->closePort ();
116
117 } catch (const RtMidiError & err) {
118 device->ok = false;
119 device->msg = err.what ();
120 }
121 }
122
123 unsigned int rtmidi_get_port_count (RtMidiPtr device)
124 {
125 try {
126 return ((RtMidi*) device->ptr)->getPortCount ();
127
128 } catch (const RtMidiError & err) {
129 device->ok = false;
130 device->msg = err.what ();
131 return -1;
132 }
133 }
134
135 int rtmidi_get_port_name (RtMidiPtr device, unsigned int portNumber, char * bufOut, int * bufLen)
136 {
137 if (bufOut == nullptr && bufLen == nullptr) {
138 return -1;
139 }
140
141 std::string name;
142 try {
143 name = ((RtMidi*) device->ptr)->getPortName (portNumber);
144 } catch (const RtMidiError & err) {
145 device->ok = false;
146 device->msg = err.what ();
147 return -1;
148 }
149
150 if (bufOut == nullptr) {
151 *bufLen = static_cast<int>(name.size()) + 1;
152 return 0;
153 }
154
155 return snprintf(bufOut, static_cast<size_t>(*bufLen), "%s", name.c_str());
156 }
157
158 /* RtMidiIn API */
159 RtMidiInPtr rtmidi_in_create_default ()
160 {
161 RtMidiWrapper* wrp = new RtMidiWrapper;
162
163 try {
164 RtMidiIn* rIn = new RtMidiIn ();
165
166 wrp->ptr = (void*) rIn;
167 wrp->data = 0;
168 wrp->ok = true;
169 wrp->msg = "";
170
171 } catch (const RtMidiError & err) {
172 wrp->ptr = 0;
173 wrp->data = 0;
174 wrp->ok = false;
175 wrp->msg = err.what ();
176 }
177
178 return wrp;
179 }
180
181 RtMidiInPtr rtmidi_in_create (enum RtMidiApi api, const char *clientName, unsigned int queueSizeLimit)
182 {
183 std::string name = clientName;
184 RtMidiWrapper* wrp = new RtMidiWrapper;
185
186 try {
187 RtMidiIn* rIn = new RtMidiIn ((RtMidi::Api) api, name, queueSizeLimit);
188
189 wrp->ptr = (void*) rIn;
190 wrp->data = 0;
191 wrp->ok = true;
192 wrp->msg = "";
193
194 } catch (const RtMidiError & err) {
195 wrp->ptr = 0;
196 wrp->data = 0;
197 wrp->ok = false;
198 wrp->msg = err.what ();
199 }
200
201 return wrp;
202 }
203
204 void rtmidi_in_free (RtMidiInPtr device)
205 {
206 if (device->data)
207 delete (CallbackProxyUserData*) device->data;
208 delete (RtMidiIn*) device->ptr;
209 delete device;
210 }
211
212 enum RtMidiApi rtmidi_in_get_current_api (RtMidiPtr device)
213 {
214 try {
215 return (RtMidiApi) ((RtMidiIn*) device->ptr)->getCurrentApi ();
216
217 } catch (const RtMidiError & err) {
218 device->ok = false;
219 device->msg = err.what ();
220
221 return RTMIDI_API_UNSPECIFIED;
222 }
223 }
224
225 static
226 void callback_proxy (double timeStamp, std::vector<unsigned char> *message, void *userData)
227 {
228 CallbackProxyUserData* data = reinterpret_cast<CallbackProxyUserData*> (userData);
229 data->c_callback (timeStamp, message->data (), message->size (), data->user_data);
230 }
231
232 void rtmidi_in_set_callback (RtMidiInPtr device, RtMidiCCallback callback, void *userData)
233 {
234 device->data = (void*) new CallbackProxyUserData (callback, userData);
235 try {
236 ((RtMidiIn*) device->ptr)->setCallback (callback_proxy, device->data);
237 } catch (const RtMidiError & err) {
238 device->ok = false;
239 device->msg = err.what ();
240 delete (CallbackProxyUserData*) device->data;
241 device->data = 0;
242 }
243 }
244
245 void rtmidi_in_cancel_callback (RtMidiInPtr device)
246 {
247 try {
248 ((RtMidiIn*) device->ptr)->cancelCallback ();
249 delete (CallbackProxyUserData*) device->data;
250 device->data = 0;
251 } catch (const RtMidiError & err) {
252 device->ok = false;
253 device->msg = err.what ();
254 }
255 }
256
257 void rtmidi_in_ignore_types (RtMidiInPtr device, bool midiSysex, bool midiTime, bool midiSense)
258 {
259 ((RtMidiIn*) device->ptr)->ignoreTypes (midiSysex, midiTime, midiSense);
260 }
261
262 double rtmidi_in_get_message (RtMidiInPtr device,
263 unsigned char *message,
264 size_t *size)
265 {
266 try {
267 // FIXME: use allocator to achieve efficient buffering
268 std::vector<unsigned char> v;
269 double ret = ((RtMidiIn*) device->ptr)->getMessage (&v);
270
271 if (v.size () > 0 && v.size() <= *size) {
272 memcpy (message, v.data (), (int) v.size ());
273 }
274
275 *size = v.size();
276 return ret;
277 }
278 catch (const RtMidiError & err) {
279 device->ok = false;
280 device->msg = err.what ();
281 return -1;
282 }
283 catch (...) {
284 device->ok = false;
285 device->msg = "Unknown error";
286 return -1;
287 }
288 }
289
290 /* RtMidiOut API */
291 RtMidiOutPtr rtmidi_out_create_default ()
292 {
293 RtMidiWrapper* wrp = new RtMidiWrapper;
294
295 try {
296 RtMidiOut* rOut = new RtMidiOut ();
297
298 wrp->ptr = (void*) rOut;
299 wrp->data = 0;
300 wrp->ok = true;
301 wrp->msg = "";
302
303 } catch (const RtMidiError & err) {
304 wrp->ptr = 0;
305 wrp->data = 0;
306 wrp->ok = false;
307 wrp->msg = err.what ();
308 }
309
310 return wrp;
311 }
312
313 RtMidiOutPtr rtmidi_out_create (enum RtMidiApi api, const char *clientName)
314 {
315 RtMidiWrapper* wrp = new RtMidiWrapper;
316 std::string name = clientName;
317
318 try {
319 RtMidiOut* rOut = new RtMidiOut ((RtMidi::Api) api, name);
320
321 wrp->ptr = (void*) rOut;
322 wrp->data = 0;
323 wrp->ok = true;
324 wrp->msg = "";
325
326 } catch (const RtMidiError & err) {
327 wrp->ptr = 0;
328 wrp->data = 0;
329 wrp->ok = false;
330 wrp->msg = err.what ();
331 }
332
333
334 return wrp;
335 }
336
337 void rtmidi_out_free (RtMidiOutPtr device)
338 {
339 delete (RtMidiOut*) device->ptr;
340 delete device;
341 }
342
343 enum RtMidiApi rtmidi_out_get_current_api (RtMidiPtr device)
344 {
345 try {
346 return (RtMidiApi) ((RtMidiOut*) device->ptr)->getCurrentApi ();
347
348 } catch (const RtMidiError & err) {
349 device->ok = false;
350 device->msg = err.what ();
351
352 return RTMIDI_API_UNSPECIFIED;
353 }
354 }
355
356 int rtmidi_out_send_message (RtMidiOutPtr device, const unsigned char *message, int length)
357 {
358 try {
359 ((RtMidiOut*) device->ptr)->sendMessage (message, length);
360 return 0;
361 }
362 catch (const RtMidiError & err) {
363 device->ok = false;
364 device->msg = err.what ();
365 return -1;
366 }
367 catch (...) {
368 device->ok = false;
369 device->msg = "Unknown error";
370 return -1;
371 }
372 }