jack.c 19 KB

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