jack.c 20 KB

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