jack.c 16 KB

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