jack.c 20 KB

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