jack.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. #include <python2.7/Python.h>
  2. #include <jack/jack.h>
  3. typedef struct {
  4. PyObject_HEAD
  5. jack_client_t* client;
  6. PyObject* port_registered_callback;
  7. PyObject* port_registered_callback_argument;
  8. PyObject* port_unregistered_callback;
  9. PyObject* port_unregistered_callback_argument;
  10. PyObject* port_renamed_callback;
  11. PyObject* port_renamed_callback_argument;
  12. } Client;
  13. typedef struct {
  14. PyObject_HEAD
  15. jack_port_t* port;
  16. } Port;
  17. static PyObject* error;
  18. static PyObject* failure;
  19. static PyTypeObject port_type = {
  20. PyObject_HEAD_INIT(NULL)
  21. };
  22. static void jack_registration_callback(jack_port_id_t port_id, int registered, void* arg)
  23. {
  24. // register: non-zero if the port is being registered, zero if the port is being unregistered
  25. Client* client = (Client*)arg;
  26. PyObject* callback;
  27. PyObject* callback_argument;
  28. if(registered) {
  29. callback = client->port_registered_callback;
  30. callback_argument = client->port_registered_callback_argument;
  31. } else {
  32. callback = client->port_unregistered_callback;
  33. callback_argument = client->port_unregistered_callback_argument;
  34. }
  35. if(callback) {
  36. // Ensure that the current thread is ready to call the Python API.
  37. // No Python API calls are allowed before this call.
  38. PyGILState_STATE gil_state = PyGILState_Ensure();
  39. Port* port = PyObject_New(Port, &port_type);
  40. port->port = jack_port_by_id(client->client, port_id);
  41. // 'O' increases reference count
  42. PyObject* callback_argument_list;
  43. if(callback_argument) {
  44. callback_argument_list = Py_BuildValue("(O,O)", (PyObject*)port, callback_argument);
  45. } else {
  46. callback_argument_list = Py_BuildValue("(O)", (PyObject*)port);
  47. }
  48. PyObject* result = PyObject_CallObject(callback, callback_argument_list);
  49. Py_DECREF(callback_argument_list);
  50. if(!result) {
  51. PyErr_PrintEx(0);
  52. } else {
  53. Py_DECREF(result);
  54. }
  55. // Release the thread. No Python API calls are allowed beyond this point.
  56. PyGILState_Release(gil_state);
  57. }
  58. }
  59. // typedef void(* JackPortRenameCallback)(jack_port_id_t port, const char *old_name, const char *new_name, void *arg)
  60. static void jack_port_renamed_callback(jack_port_id_t port_id, const char* old_name, const char* new_name, void* arg)
  61. {
  62. Client* client = (Client*)arg;
  63. if(client->port_renamed_callback) {
  64. // Ensure that the current thread is ready to call the Python API.
  65. // No Python API calls are allowed before this call.
  66. PyGILState_STATE gil_state = PyGILState_Ensure();
  67. Port* port = PyObject_New(Port, &port_type);
  68. port->port = jack_port_by_id(client->client, port_id);
  69. // 'O' increases reference count
  70. PyObject* callback_argument_list = Py_BuildValue(
  71. "(O,s,s,O)",
  72. (PyObject*)port,
  73. old_name,
  74. new_name,
  75. client->port_renamed_callback_argument
  76. );
  77. PyObject* result = PyObject_CallObject(client->port_renamed_callback, callback_argument_list);
  78. Py_DECREF(callback_argument_list);
  79. if(!result) {
  80. PyErr_PrintEx(0);
  81. } else {
  82. Py_DECREF(result);
  83. }
  84. // Release the thread. No Python API calls are allowed beyond this point.
  85. PyGILState_Release(gil_state);
  86. }
  87. }
  88. static PyObject* client___new__(PyTypeObject* type, PyObject* args, PyObject* kwargs)
  89. {
  90. Client* self = (Client*)type->tp_alloc(type, 0);
  91. if(self) {
  92. const char* name;
  93. if(!PyArg_ParseTuple(args, "s", &name)) {
  94. return NULL;
  95. }
  96. jack_status_t status;
  97. self->client = jack_client_open(name, JackNullOption, &status);
  98. if(!self->client) {
  99. if(status & JackFailure) {
  100. PyErr_SetString(failure, "Overall operation failed.");
  101. }
  102. return NULL;
  103. }
  104. self->port_registered_callback = NULL;
  105. self->port_unregistered_callback = NULL;
  106. int error_code = jack_set_port_registration_callback(
  107. self->client,
  108. jack_registration_callback,
  109. (void*)self
  110. );
  111. if(error_code) {
  112. PyErr_SetString(error, "Could not set port registration callback.");
  113. return NULL;
  114. }
  115. self->port_renamed_callback = NULL;
  116. self->port_renamed_callback_argument = NULL;
  117. error_code = jack_set_port_rename_callback(
  118. self->client,
  119. jack_port_renamed_callback,
  120. (void*)self
  121. );
  122. if(error_code) {
  123. PyErr_SetString(error, "Could not set port rename callback.");
  124. return NULL;
  125. }
  126. }
  127. return (PyObject*)self;
  128. }
  129. static PyObject* client_activate(Client* self) {
  130. int error_code = jack_activate(self->client);
  131. if(error_code) {
  132. PyErr_SetString(error, "");
  133. return NULL;
  134. } else {
  135. Py_INCREF(Py_None);
  136. return Py_None;
  137. }
  138. }
  139. static PyObject* client_get_name(Client* self)
  140. {
  141. return (PyObject*)PyString_FromString(jack_get_client_name(self->client));
  142. }
  143. static PyObject* client_get_ports(Client* self)
  144. {
  145. PyObject* ports = PyList_New(0);
  146. if(!ports) {
  147. return NULL;
  148. }
  149. const char** port_names = jack_get_ports(self->client, NULL, NULL, 0);
  150. int port_index;
  151. for(port_index = 0; port_names[port_index] != NULL; port_index++) {
  152. Port* port = PyObject_New(Port, &port_type);
  153. port->port = jack_port_by_name(self->client, port_names[port_index]);
  154. if(PyList_Append(ports, (PyObject*)port)) {
  155. return NULL;
  156. }
  157. }
  158. jack_free(port_names);
  159. return ports;
  160. }
  161. static PyObject* client_set_port_registered_callback(Client* self, PyObject* args)
  162. {
  163. PyObject* callback = 0;
  164. PyObject* callback_argument = 0;
  165. if(!PyArg_ParseTuple(args, "O|O", &callback, &callback_argument)) {
  166. return NULL;
  167. }
  168. if(!PyCallable_Check(callback)) {
  169. PyErr_SetString(PyExc_TypeError, "Parameter must be callable.");
  170. return NULL;
  171. }
  172. Py_XINCREF(callback);
  173. Py_XDECREF(self->port_registered_callback);
  174. self->port_registered_callback = callback;
  175. Py_XINCREF(callback_argument);
  176. Py_XDECREF(self->port_registered_callback_argument);
  177. self->port_registered_callback_argument = callback_argument;
  178. Py_INCREF(Py_None);
  179. return Py_None;
  180. }
  181. static PyObject* client_set_port_unregistered_callback(Client* self, PyObject* args)
  182. {
  183. PyObject* callback = 0;
  184. PyObject* callback_argument = 0;
  185. if(!PyArg_ParseTuple(args, "O|O", &callback, &callback_argument)) {
  186. return NULL;
  187. }
  188. if(!PyCallable_Check(callback)) {
  189. PyErr_SetString(PyExc_TypeError, "Parameter must be callable.");
  190. return NULL;
  191. }
  192. Py_XINCREF(callback);
  193. Py_XDECREF(self->port_unregistered_callback);
  194. self->port_unregistered_callback = callback;
  195. Py_XINCREF(callback_argument);
  196. Py_XDECREF(self->port_unregistered_callback_argument);
  197. self->port_unregistered_callback_argument = callback_argument;
  198. Py_INCREF(Py_None);
  199. return Py_None;
  200. }
  201. static PyObject* client_set_port_renamed_callback(Client* self, PyObject* args)
  202. {
  203. PyObject* callback = 0;
  204. PyObject* callback_argument = 0;
  205. if(!PyArg_ParseTuple(args, "O|O", &callback, &callback_argument)) {
  206. return NULL;
  207. }
  208. if(!PyCallable_Check(callback)) {
  209. PyErr_SetString(PyExc_TypeError, "Parameter must be callable.");
  210. return NULL;
  211. }
  212. Py_XINCREF(callback);
  213. Py_XDECREF(self->port_renamed_callback);
  214. self->port_renamed_callback = callback;
  215. Py_XINCREF(callback_argument);
  216. Py_XDECREF(self->port_renamed_callback_argument);
  217. self->port_renamed_callback_argument = callback_argument;
  218. Py_INCREF(Py_None);
  219. return Py_None;
  220. }
  221. static void client_dealloc(Client* self)
  222. {
  223. jack_client_close(self->client);
  224. self->ob_type->tp_free((PyObject*)self);
  225. }
  226. static PyMethodDef client_methods[] = {
  227. {
  228. "activate",
  229. (PyCFunction)client_activate,
  230. METH_NOARGS,
  231. "Tell the Jack server that the program is ready to start processing audio.",
  232. },
  233. {
  234. "get_name",
  235. (PyCFunction)client_get_name,
  236. METH_NOARGS,
  237. "Return client's actual name.",
  238. },
  239. {
  240. "get_ports",
  241. (PyCFunction)client_get_ports,
  242. METH_NOARGS,
  243. "Return list of ports.",
  244. },
  245. {
  246. "set_port_registered_callback",
  247. (PyCFunction)client_set_port_registered_callback,
  248. METH_VARARGS,
  249. "Tell the JACK server to call a function whenever a port is registered.",
  250. },
  251. {
  252. "set_port_unregistered_callback",
  253. (PyCFunction)client_set_port_unregistered_callback,
  254. METH_VARARGS,
  255. "Tell the JACK server to call a function whenever a port is unregistered.",
  256. },
  257. {
  258. "set_port_renamed_callback",
  259. (PyCFunction)client_set_port_renamed_callback,
  260. METH_VARARGS,
  261. "Tell the JACK server to call a function whenever a port is renamed.",
  262. },
  263. {NULL},
  264. };
  265. static PyObject* port_is_input(Port* self)
  266. {
  267. // The flags "JackPortIsInput" and "JackPortIsOutput" are mutually exclusive.
  268. return (PyObject*)PyBool_FromLong(jack_port_flags(self->port) & JackPortIsInput);
  269. }
  270. static PyObject* port_is_output(Port* self)
  271. {
  272. // The flags "JackPortIsInput" and "JackPortIsOutput" are mutually exclusive.
  273. return (PyObject*)PyBool_FromLong(jack_port_flags(self->port) & JackPortIsOutput);
  274. }
  275. static PyObject* port_get_name(Port* self)
  276. {
  277. return (PyObject*)PyString_FromString(jack_port_name(self->port));
  278. }
  279. static PyObject* port_get_short_name(Port* self)
  280. {
  281. return (PyObject*)PyString_FromString(jack_port_short_name(self->port));
  282. }
  283. static PyObject* port_get_type(Port* self)
  284. {
  285. return (PyObject*)PyString_FromString(jack_port_type(self->port));
  286. }
  287. static PyObject* port_set_short_name(Port* self, PyObject* args)
  288. {
  289. const char* name;
  290. if(!PyArg_ParseTuple(args, "s", &name)) {
  291. return NULL;
  292. }
  293. // 0 on success, otherwise a non-zero error code.
  294. if(jack_port_set_name(self->port, name)) {
  295. return NULL;
  296. }
  297. Py_INCREF(Py_None);
  298. return Py_None;
  299. }
  300. static PyObject* port_get_client_name(Port* self)
  301. {
  302. // e.g. "PulseAudio JACK Sink:front-left"
  303. const char* port_name = jack_port_name(self->port);
  304. int client_name_length = strlen(port_name) - strlen(jack_port_short_name(self->port)) - 1;
  305. // e.g. "PulseAudio JACK Sink"
  306. char* client_name = (char*) malloc(jack_port_name_size());
  307. strncpy(client_name, port_name, client_name_length);
  308. client_name[client_name_length] = '\0';
  309. PyObject* client_name_python = PyString_FromString(client_name);
  310. free(client_name);
  311. return client_name_python;
  312. }
  313. static PyObject* port_get_aliases(Port* self)
  314. {
  315. PyObject* aliases_list = PyList_New(0);
  316. if(!aliases_list) {
  317. return NULL;
  318. }
  319. char* aliases[2];
  320. aliases[0] = (char*) malloc(jack_port_name_size());
  321. aliases[1] = (char*) malloc(jack_port_name_size());
  322. int alias_count = jack_port_get_aliases(self->port, aliases);
  323. int alias_index;
  324. for(alias_index = 0; alias_index < alias_count; alias_index++) {
  325. PyList_Append(aliases_list, PyString_FromString(aliases[alias_index]));
  326. }
  327. free(aliases[0]);
  328. free(aliases[1]);
  329. return aliases_list;
  330. }
  331. static PyObject* port___repr__(Port* self)
  332. {
  333. static PyObject* format;
  334. if(!format) {
  335. format = PyString_FromString("jack.Port(name = '%s', type = '%s')");
  336. }
  337. PyObject* args = Py_BuildValue(
  338. "(s,s)",
  339. jack_port_name(self->port),
  340. jack_port_type(self->port)
  341. );
  342. PyObject* repr = PyString_Format(format, args);
  343. Py_DECREF(args);
  344. return repr;
  345. }
  346. static PyMethodDef port_methods[] = {
  347. {
  348. "is_input",
  349. (PyCFunction)port_is_input,
  350. METH_NOARGS,
  351. "Return true if the port can receive data.",
  352. },
  353. {
  354. "is_output",
  355. (PyCFunction)port_is_output,
  356. METH_NOARGS,
  357. "Return true if data can be read from the port.",
  358. },
  359. {
  360. "get_name",
  361. (PyCFunction)port_get_name,
  362. METH_NOARGS,
  363. "Return port's name.",
  364. },
  365. {
  366. "get_short_name",
  367. (PyCFunction)port_get_short_name,
  368. METH_NOARGS,
  369. "Return port's name without the preceding name of the associated client.",
  370. },
  371. {
  372. "get_type",
  373. (PyCFunction)port_get_type,
  374. METH_NOARGS,
  375. "Return port's type.",
  376. },
  377. {
  378. "get_client_name",
  379. (PyCFunction)port_get_client_name,
  380. METH_NOARGS,
  381. "Return the name of the associated client.",
  382. },
  383. {
  384. "set_short_name",
  385. (PyCFunction)port_set_short_name,
  386. METH_VARARGS,
  387. "Modify a port's short name. May be called at any time.",
  388. },
  389. {
  390. "get_aliases",
  391. (PyCFunction)port_get_aliases,
  392. METH_NOARGS,
  393. "Return list of assigned aliases.",
  394. },
  395. {NULL},
  396. };
  397. PyMODINIT_FUNC initjack()
  398. {
  399. // Initialize and acquire the global interpreter lock.
  400. // This must be done in the main thread before creating engaging in any thread operations.
  401. PyEval_InitThreads();
  402. PyObject* module = Py_InitModule("jack", NULL);
  403. if(!module) {
  404. return;
  405. }
  406. error = PyErr_NewException((char*)"jack.Error", NULL, NULL);
  407. Py_INCREF(error);
  408. PyModule_AddObject(module, "Error", error);
  409. failure = PyErr_NewException((char*)"jack.Failure", error, NULL);
  410. Py_INCREF(failure);
  411. PyModule_AddObject(module, "Failure", failure);
  412. static PyTypeObject client_type = {
  413. PyObject_HEAD_INIT(NULL)
  414. };
  415. client_type.tp_name = "jack.Client";
  416. client_type.tp_basicsize = sizeof(Client);
  417. client_type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
  418. client_type.tp_new = client___new__;
  419. client_type.tp_dealloc = (destructor)client_dealloc;
  420. client_type.tp_methods = client_methods;
  421. if(PyType_Ready(&client_type) < 0) {
  422. return;
  423. }
  424. Py_INCREF(&client_type);
  425. PyModule_AddObject(module, "Client", (PyObject*)&client_type);
  426. port_type.tp_name = "jack.Port";
  427. port_type.tp_basicsize = sizeof(Port);
  428. port_type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
  429. // Forbid direct instantiation.
  430. port_type.tp_new = NULL;
  431. port_type.tp_repr = (reprfunc)port___repr__;
  432. port_type.tp_methods = port_methods;
  433. if(PyType_Ready(&port_type) < 0) {
  434. return;
  435. }
  436. Py_INCREF(&port_type);
  437. PyModule_AddObject(module, "Port", (PyObject*)&port_type);
  438. }