1
0

jack.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. } Client;
  11. typedef struct {
  12. PyObject_HEAD
  13. jack_port_t* port;
  14. } Port;
  15. static PyObject* error;
  16. static PyObject* failure;
  17. static PyTypeObject port_type = {
  18. PyObject_HEAD_INIT(NULL)
  19. };
  20. static void jack_registration_callback(jack_port_id_t port_id, int registered, void* arg)
  21. {
  22. // register: non-zero if the port is being registered, zero if the port is being unregistered
  23. Client* client = (Client*)arg;
  24. PyObject* callback;
  25. PyObject* callback_argument;
  26. if(registered) {
  27. callback = client->port_registered_callback;
  28. callback_argument = client->port_registered_callback_argument;
  29. } else {
  30. callback = client->port_unregistered_callback;
  31. callback_argument = client->port_unregistered_callback_argument;
  32. }
  33. if(callback) {
  34. // Ensure that the current thread is ready to call the Python API.
  35. // No Python API calls are allowed before this call.
  36. PyGILState_STATE gil_state = PyGILState_Ensure();
  37. Port* port = PyObject_New(Port, &port_type);
  38. port->port = jack_port_by_id(client->client, port_id);
  39. // 'O' increases reference count
  40. PyObject* callback_argument_list;
  41. if(callback_argument) {
  42. callback_argument_list = Py_BuildValue("(O,O)", (PyObject*)port, callback_argument);
  43. } else {
  44. callback_argument_list = Py_BuildValue("(O)", (PyObject*)port);
  45. }
  46. PyObject* result = PyObject_CallObject(callback, callback_argument_list);
  47. Py_DECREF(callback_argument_list);
  48. if(!result) {
  49. PyErr_PrintEx(0);
  50. } else {
  51. Py_DECREF(result);
  52. }
  53. // Release the thread. No Python API calls are allowed beyond this point.
  54. PyGILState_Release(gil_state);
  55. }
  56. }
  57. static PyObject* client___new__(PyTypeObject* type, PyObject* args, PyObject* kwargs)
  58. {
  59. Client* self = (Client*)type->tp_alloc(type, 0);
  60. if(self) {
  61. const char* name;
  62. if(!PyArg_ParseTuple(args, "s", &name)) {
  63. return NULL;
  64. }
  65. jack_status_t status;
  66. self->client = jack_client_open(name, JackNullOption, &status);
  67. if(!self->client) {
  68. if(status & JackFailure) {
  69. PyErr_SetString(failure, "Overall operation failed.");
  70. }
  71. return NULL;
  72. }
  73. self->port_registered_callback = NULL;
  74. self->port_unregistered_callback = NULL;
  75. int error_code = jack_set_port_registration_callback(
  76. self->client,
  77. jack_registration_callback,
  78. (void*)self
  79. );
  80. if(error_code) {
  81. PyErr_SetString(error, "Could not set port registration callback.");
  82. return NULL;
  83. }
  84. }
  85. return (PyObject*)self;
  86. }
  87. static PyObject* client_activate(Client* self) {
  88. int error_code = jack_activate(self->client);
  89. if(error_code) {
  90. PyErr_SetString(error, "");
  91. return NULL;
  92. } else {
  93. Py_INCREF(Py_None);
  94. return Py_None;
  95. }
  96. }
  97. static PyObject* client_get_name(Client* self)
  98. {
  99. return (PyObject*)PyString_FromString(jack_get_client_name(self->client));
  100. }
  101. static PyObject* client_get_ports(Client* self)
  102. {
  103. PyObject* ports = PyList_New(0);
  104. if(!ports) {
  105. return NULL;
  106. }
  107. const char** port_names = jack_get_ports(self->client, NULL, NULL, 0);
  108. int port_index;
  109. for(port_index = 0; port_names[port_index] != NULL; port_index++) {
  110. Port* port = PyObject_New(Port, &port_type);
  111. port->port = jack_port_by_name(self->client, port_names[port_index]);
  112. if(PyList_Append(ports, (PyObject*)port)) {
  113. return NULL;
  114. }
  115. }
  116. jack_free(port_names);
  117. return ports;
  118. }
  119. static PyObject* client_set_port_registered_callback(Client* self, PyObject* args)
  120. {
  121. PyObject* callback = 0;
  122. PyObject* callback_argument = 0;
  123. if(!PyArg_ParseTuple(args, "O|O", &callback, &callback_argument)) {
  124. return NULL;
  125. }
  126. if(!PyCallable_Check(callback)) {
  127. PyErr_SetString(PyExc_TypeError, "Parameter must be callable.");
  128. return NULL;
  129. }
  130. Py_XINCREF(callback);
  131. Py_XDECREF(self->port_registered_callback);
  132. self->port_registered_callback = callback;
  133. Py_XINCREF(callback_argument);
  134. Py_XDECREF(self->port_registered_callback_argument);
  135. self->port_registered_callback_argument = callback_argument;
  136. Py_INCREF(Py_None);
  137. return Py_None;
  138. }
  139. static PyObject* client_set_port_unregistered_callback(Client* self, PyObject* args)
  140. {
  141. PyObject* callback = 0;
  142. PyObject* callback_argument = 0;
  143. if(!PyArg_ParseTuple(args, "O|O", &callback, &callback_argument)) {
  144. return NULL;
  145. }
  146. if(!PyCallable_Check(callback)) {
  147. PyErr_SetString(PyExc_TypeError, "Parameter must be callable.");
  148. return NULL;
  149. }
  150. Py_XINCREF(callback);
  151. Py_XDECREF(self->port_unregistered_callback);
  152. self->port_unregistered_callback = callback;
  153. Py_XINCREF(callback_argument);
  154. Py_XDECREF(self->port_unregistered_callback_argument);
  155. self->port_unregistered_callback_argument = callback_argument;
  156. Py_INCREF(Py_None);
  157. return Py_None;
  158. }
  159. static void client_dealloc(Client* self)
  160. {
  161. jack_client_close(self->client);
  162. self->ob_type->tp_free((PyObject*)self);
  163. }
  164. static PyMethodDef client_methods[] = {
  165. {
  166. "activate",
  167. (PyCFunction)client_activate,
  168. METH_NOARGS,
  169. "Tell the Jack server that the program is ready to start processing audio.",
  170. },
  171. {
  172. "get_name",
  173. (PyCFunction)client_get_name,
  174. METH_NOARGS,
  175. "Return client's actual name.",
  176. },
  177. {
  178. "get_ports",
  179. (PyCFunction)client_get_ports,
  180. METH_NOARGS,
  181. "Return list of ports.",
  182. },
  183. {
  184. "set_port_registered_callback",
  185. (PyCFunction)client_set_port_registered_callback,
  186. METH_VARARGS,
  187. "Tell the JACK server to call a function whenever a port is registered.",
  188. },
  189. {
  190. "set_port_unregistered_callback",
  191. (PyCFunction)client_set_port_unregistered_callback,
  192. METH_VARARGS,
  193. "Tell the JACK server to call a function whenever a port is unregistered.",
  194. },
  195. {NULL},
  196. };
  197. static PyObject* port_get_name(Port* self)
  198. {
  199. return (PyObject*)PyString_FromString(jack_port_name(self->port));
  200. }
  201. static PyObject* port_get_short_name(Port* self)
  202. {
  203. return (PyObject*)PyString_FromString(jack_port_short_name(self->port));
  204. }
  205. static PyObject* port_set_short_name(Port* self, PyObject* args)
  206. {
  207. const char* name;
  208. if(!PyArg_ParseTuple(args, "s", &name)) {
  209. return NULL;
  210. }
  211. // 0 on success, otherwise a non-zero error code.
  212. if(jack_port_set_name(self->port, name)) {
  213. return NULL;
  214. }
  215. Py_INCREF(Py_None);
  216. return Py_None;
  217. }
  218. static PyObject* port_get_client_name(Port* self)
  219. {
  220. // e.g. "PulseAudio JACK Sink:front-left"
  221. const char* port_name = jack_port_name(self->port);
  222. int client_name_length = strlen(port_name) - strlen(jack_port_short_name(self->port)) - 1;
  223. // e.g. "PulseAudio JACK Sink"
  224. char* client_name = (char*) malloc(jack_port_name_size());
  225. strncpy(client_name, port_name, client_name_length);
  226. client_name[client_name_length] = '\0';
  227. PyObject* client_name_python = PyString_FromString(client_name);
  228. free(client_name);
  229. return client_name_python;
  230. }
  231. static PyObject* port_get_aliases(Port* self)
  232. {
  233. PyObject* aliases_list = PyList_New(0);
  234. if(!aliases_list) {
  235. return NULL;
  236. }
  237. char* aliases[2];
  238. aliases[0] = (char*) malloc(jack_port_name_size());
  239. aliases[1] = (char*) malloc(jack_port_name_size());
  240. int alias_count = jack_port_get_aliases(self->port, aliases);
  241. int alias_index;
  242. for(alias_index = 0; alias_index < alias_count; alias_index++) {
  243. PyList_Append(aliases_list, PyString_FromString(aliases[alias_index]));
  244. }
  245. free(aliases[0]);
  246. free(aliases[1]);
  247. return aliases_list;
  248. }
  249. static PyObject* port___repr__(Port* self)
  250. {
  251. static PyObject* format;
  252. if(!format) {
  253. format = PyString_FromString("jack.Port(name = '%s')");
  254. }
  255. PyObject* name = PyString_FromString(jack_port_name(self->port));
  256. PyObject* repr = PyString_Format(format, name);
  257. Py_DECREF(name);
  258. return repr;
  259. }
  260. static PyMethodDef port_methods[] = {
  261. {
  262. "get_name",
  263. (PyCFunction)port_get_name,
  264. METH_NOARGS,
  265. "Return port's name.",
  266. },
  267. {
  268. "get_short_name",
  269. (PyCFunction)port_get_short_name,
  270. METH_NOARGS,
  271. "Return port's name without the preceding name of the associated client.",
  272. },
  273. {
  274. "get_client_name",
  275. (PyCFunction)port_get_client_name,
  276. METH_NOARGS,
  277. "Return the name of the associated client.",
  278. },
  279. {
  280. "set_short_name",
  281. (PyCFunction)port_set_short_name,
  282. METH_VARARGS,
  283. "Modify a port's short name. May be called at any time.",
  284. },
  285. {
  286. "get_aliases",
  287. (PyCFunction)port_get_aliases,
  288. METH_NOARGS,
  289. "Return list of assigned aliases.",
  290. },
  291. {NULL},
  292. };
  293. PyMODINIT_FUNC initjack()
  294. {
  295. // Initialize and acquire the global interpreter lock.
  296. // This must be done in the main thread before creating engaging in any thread operations.
  297. PyEval_InitThreads();
  298. PyObject* module = Py_InitModule("jack", NULL);
  299. if(!module) {
  300. return;
  301. }
  302. error = PyErr_NewException((char*)"jack.Error", NULL, NULL);
  303. Py_INCREF(error);
  304. PyModule_AddObject(module, "Error", error);
  305. failure = PyErr_NewException((char*)"jack.Failure", error, NULL);
  306. Py_INCREF(failure);
  307. PyModule_AddObject(module, "Failure", failure);
  308. static PyTypeObject client_type = {
  309. PyObject_HEAD_INIT(NULL)
  310. };
  311. client_type.tp_name = "jack.Client";
  312. client_type.tp_basicsize = sizeof(Client);
  313. client_type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
  314. client_type.tp_new = client___new__;
  315. client_type.tp_dealloc = (destructor)client_dealloc;
  316. client_type.tp_methods = client_methods;
  317. if(PyType_Ready(&client_type) < 0) {
  318. return;
  319. }
  320. Py_INCREF(&client_type);
  321. PyModule_AddObject(module, "Client", (PyObject*)&client_type);
  322. port_type.tp_name = "jack.Port";
  323. port_type.tp_basicsize = sizeof(Port);
  324. port_type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
  325. // Forbid direct instantiation.
  326. port_type.tp_new = NULL;
  327. port_type.tp_repr = (reprfunc)port___repr__;
  328. port_type.tp_methods = port_methods;
  329. if(PyType_Ready(&port_type) < 0) {
  330. return;
  331. }
  332. Py_INCREF(&port_type);
  333. PyModule_AddObject(module, "Port", (PyObject*)&port_type);
  334. }