You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

712 lines
25KB

  1. /************************************************************************/
  2. /* PyRtAudio: a python wrapper around RtAudio
  3. Copyright (c) 2011 Antoine Lefebvre
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation files
  6. (the "Software"), to deal in the Software without restriction,
  7. including without limitation the rights to use, copy, modify, merge,
  8. publish, distribute, sublicense, and/or sell copies of the Software,
  9. and to permit persons to whom the Software is furnished to do so,
  10. subject to the following conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. Any person wishing to distribute modifications to the Software is
  14. asked to send the modifications to the original developer so that
  15. they can be incorporated into the canonical version. This is,
  16. however, not a binding provision of this license.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  21. ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  22. CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. /************************************************************************/
  26. // This software is in the development stage
  27. // Do not expect compatibility with future versions.
  28. // Comments, suggestions, new features, bug fixes, etc. are welcome
  29. #include <Python.h>
  30. #include "RtAudio.h"
  31. extern "C" {
  32. typedef struct
  33. {
  34. PyObject_HEAD
  35. #if PY_MAJOR_VERSION < 3
  36. void *padding; // python 2.7 seems to set dac to bad value
  37. // after print_function, causing a crash, no
  38. // idea why, but this fixes it.
  39. #endif
  40. RtAudio *dac;
  41. RtAudioFormat _format;
  42. int _bufferSize;
  43. unsigned int inputChannels;
  44. PyObject *callback_func;
  45. } PyRtAudio;
  46. static PyObject *RtAudioErrorException;
  47. static int callback(void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
  48. double streamTime, RtAudioStreamStatus status, void *data )
  49. {
  50. PyRtAudio* self = (PyRtAudio*) data;
  51. if (status == RTAUDIO_OUTPUT_UNDERFLOW)
  52. printf("underflow.\n");
  53. if (self == NULL) return -1;
  54. float* in = (float *) inputBuffer;
  55. float* out = (float *) outputBuffer;
  56. PyObject *py_callback_func = self->callback_func;
  57. int retval = 0;
  58. if (py_callback_func) {
  59. PyGILState_STATE gstate = PyGILState_Ensure();
  60. #if PY_MAJOR_VERSION >= 3
  61. PyObject* iBuffer = PyMemoryView_FromMemory((char*)in, sizeof(float) * self->inputChannels * nBufferFrames, PyBUF_READ);
  62. PyObject* oBuffer = PyMemoryView_FromMemory((char*)out, sizeof(float) * nBufferFrames, PyBUF_WRITE);
  63. #else
  64. PyObject* iBuffer = PyBuffer_FromMemory(in, sizeof(float) * self->inputChannels * nBufferFrames);
  65. PyObject* oBuffer = PyBuffer_FromReadWriteMemory(out, sizeof(float) * nBufferFrames);
  66. #endif
  67. PyObject *arglist = Py_BuildValue("(O,O)", oBuffer, iBuffer);
  68. if (arglist == NULL) {
  69. printf("error.\n");
  70. PyErr_Print();
  71. PyGILState_Release(gstate);
  72. return 2;
  73. }
  74. // Calling the callback
  75. PyObject *result = PyEval_CallObject(py_callback_func, arglist);
  76. if (PyErr_Occurred() != NULL) {
  77. PyErr_Print();
  78. }
  79. #if PY_MAJOR_VERSION >= 3
  80. else if (result == NULL)
  81. retval = 0;
  82. else if (PyLong_Check(result)) {
  83. retval = PyLong_AsLong(result);
  84. }
  85. #else
  86. else if (PyInt_Check(result)) {
  87. retval = PyInt_AsLong(result);
  88. }
  89. #endif
  90. Py_DECREF(arglist);
  91. Py_DECREF(oBuffer);
  92. Py_DECREF(iBuffer);
  93. Py_XDECREF(result);
  94. PyGILState_Release(gstate);
  95. }
  96. return retval;
  97. }
  98. static void RtAudio_dealloc(PyRtAudio *self)
  99. {
  100. printf("RtAudio_dealloc.\n");
  101. if (self == NULL) return;
  102. if (self->dac) {
  103. self->dac->closeStream();
  104. Py_CLEAR(self->callback_func);
  105. delete self->dac;
  106. }
  107. Py_TYPE(self)->tp_free((PyObject *) self);
  108. }
  109. static PyObject* RtAudio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
  110. {
  111. printf("RtAudio_new.\n");
  112. PyRtAudio *self;
  113. char *api = NULL;
  114. if(!PyArg_ParseTuple(args, "|s", &api))
  115. return NULL;
  116. self = (PyRtAudio *) type->tp_alloc(type, 0);
  117. if(self == NULL) return NULL;
  118. self->dac = NULL;
  119. self->callback_func = NULL;
  120. try {
  121. if (api == NULL)
  122. self->dac = new RtAudio;
  123. else if(!strcmp(api, "jack"))
  124. self->dac = new RtAudio(RtAudio::UNIX_JACK);
  125. else if(!strcmp(api, "alsa"))
  126. self->dac = new RtAudio(RtAudio::LINUX_ALSA);
  127. else if(!strcmp(api, "oss"))
  128. self->dac = new RtAudio(RtAudio::LINUX_ALSA);
  129. else if(!strcmp(api, "core"))
  130. self->dac = new RtAudio(RtAudio::MACOSX_CORE);
  131. else if(!strcmp(api, "asio"))
  132. self->dac = new RtAudio(RtAudio::WINDOWS_ASIO);
  133. else if(!strcmp(api, "directsound"))
  134. self->dac = new RtAudio(RtAudio::WINDOWS_DS);
  135. }
  136. catch (RtAudioError &error) {
  137. PyErr_SetString(RtAudioErrorException, error.getMessage().c_str());
  138. Py_INCREF(RtAudioErrorException);
  139. return NULL;
  140. }
  141. self->dac->showWarnings(false);
  142. //Py_XINCREF(self);
  143. return (PyObject *) self;
  144. }
  145. static int RtAudio_init(PyRtAudio *self, PyObject *args, PyObject *kwds)
  146. {
  147. printf("RtAudio_init.\n");
  148. //if (self == NULL) return 0;
  149. return 0;
  150. }
  151. // This functions does not yet support all the features of the RtAudio::openStream method.
  152. // Please send your patches if you improves this.
  153. static PyObject* RtAudio_openStream(PyRtAudio *self, PyObject *args)
  154. {
  155. if (self == NULL) return NULL;
  156. if (self->dac == NULL) {
  157. printf("the dac is null.\n");
  158. Py_RETURN_NONE;
  159. }
  160. PyObject *oParamsObj;
  161. PyObject *iParamsObj;
  162. int fs;
  163. unsigned int bf;
  164. PyObject *pycallback;
  165. if (!PyArg_ParseTuple(args, "OOiiO", &oParamsObj, &iParamsObj, &fs, &bf, &pycallback))
  166. return NULL;
  167. RtAudio::StreamParameters oParams;
  168. oParams.deviceId = 1;
  169. oParams.nChannels = 1;
  170. oParams.firstChannel = 0;
  171. if (PyDict_Check(oParamsObj)) {
  172. #if PY_MAJOR_VERSION >= 3
  173. if (PyDict_Contains(oParamsObj, PyUnicode_FromString("deviceId"))) {
  174. PyObject *value = PyDict_GetItem(oParamsObj, PyUnicode_FromString("deviceId"));
  175. oParams.deviceId = PyLong_AsLong(value);
  176. }
  177. if (PyDict_Contains(oParamsObj, PyUnicode_FromString("nChannels"))) {
  178. PyObject *value = PyDict_GetItem(oParamsObj, PyUnicode_FromString("nChannels"));
  179. oParams.nChannels = PyLong_AsLong(value);
  180. }
  181. if (PyDict_Contains(oParamsObj, PyUnicode_FromString("firstChannel"))) {
  182. PyObject *value = PyDict_GetItem(oParamsObj, PyUnicode_FromString("firstChannel"));
  183. oParams.firstChannel = PyLong_AsLong(value);
  184. }
  185. #else
  186. if (PyDict_Contains(oParamsObj, PyString_FromString("deviceId"))) {
  187. PyObject *value = PyDict_GetItem(oParamsObj, PyString_FromString("deviceId"));
  188. oParams.deviceId = PyInt_AsLong(value);
  189. }
  190. if (PyDict_Contains(oParamsObj, PyString_FromString("nChannels"))) {
  191. PyObject *value = PyDict_GetItem(oParamsObj, PyString_FromString("nChannels"));
  192. oParams.nChannels = PyInt_AsLong(value);
  193. }
  194. if (PyDict_Contains(oParamsObj, PyString_FromString("firstChannel"))) {
  195. PyObject *value = PyDict_GetItem(oParamsObj, PyString_FromString("firstChannel"));
  196. oParams.firstChannel = PyInt_AsLong(value);
  197. }
  198. #endif
  199. }
  200. else {
  201. printf("First argument must be a dictionary. Default values will be used.\n");
  202. }
  203. RtAudio::StreamParameters iParams;
  204. iParams.deviceId = 1;
  205. iParams.nChannels = 2;
  206. iParams.firstChannel = 0;
  207. if (PyDict_Check(iParamsObj)) {
  208. #if PY_MAJOR_VERSION >= 3
  209. if (PyDict_Contains(iParamsObj, PyUnicode_FromString("deviceId"))) {
  210. PyObject *value = PyDict_GetItem(iParamsObj, PyUnicode_FromString("deviceId"));
  211. iParams.deviceId = PyLong_AsLong(value);
  212. }
  213. if (PyDict_Contains(iParamsObj, PyUnicode_FromString("nChannels"))) {
  214. PyObject *value = PyDict_GetItem(iParamsObj, PyUnicode_FromString("nChannels"));
  215. iParams.nChannels = PyLong_AsLong(value);
  216. }
  217. if (PyDict_Contains(iParamsObj, PyUnicode_FromString("firstChannel"))) {
  218. PyObject *value = PyDict_GetItem(iParamsObj, PyUnicode_FromString("firstChannel"));
  219. iParams.firstChannel = PyLong_AsLong(value);
  220. }
  221. #else
  222. if (PyDict_Contains(iParamsObj, PyString_FromString("deviceId"))) {
  223. PyObject *value = PyDict_GetItem(iParamsObj, PyString_FromString("deviceId"));
  224. iParams.deviceId = PyInt_AsLong(value);
  225. }
  226. if (PyDict_Contains(iParamsObj, PyString_FromString("nChannels"))) {
  227. PyObject *value = PyDict_GetItem(iParamsObj, PyString_FromString("nChannels"));
  228. iParams.nChannels = PyInt_AsLong(value);
  229. }
  230. if (PyDict_Contains(iParamsObj, PyString_FromString("firstChannel"))) {
  231. PyObject *value = PyDict_GetItem(iParamsObj, PyString_FromString("firstChannel"));
  232. iParams.firstChannel = PyInt_AsLong(value);
  233. }
  234. #endif
  235. }
  236. else {
  237. printf("Second argument must be a dictionary. Default values will be used.\n");
  238. }
  239. if (!PyCallable_Check(pycallback)) {
  240. PyErr_SetString(PyExc_TypeError, "Need a callable object!");
  241. Py_XINCREF(PyExc_TypeError);
  242. return NULL;
  243. }
  244. // sanity check the callback ?
  245. Py_INCREF(pycallback); /* Add a reference to new callback */
  246. self->callback_func = pycallback; /*Remember new callback */
  247. // add support for other format
  248. self->_format = RTAUDIO_FLOAT32;
  249. // add support for other options
  250. RtAudio::StreamOptions options;
  251. options.flags = RTAUDIO_NONINTERLEAVED;
  252. try {
  253. if (self->dac->isStreamOpen())
  254. self->dac->closeStream();
  255. self->dac->openStream(&oParams, &iParams, self->_format, fs, &bf, &callback, self, &options);
  256. }
  257. catch ( RtAudioError& error ) {
  258. PyErr_SetString(RtAudioErrorException, error.getMessage().c_str());
  259. Py_INCREF(RtAudioErrorException);
  260. return NULL;
  261. }
  262. self->inputChannels = iParams.nChannels;
  263. Py_RETURN_NONE;
  264. }
  265. static PyObject* RtAudio_closeStream(PyRtAudio *self, PyObject *args)
  266. {
  267. printf("RtAudio_closeStream.\n");
  268. if (self == NULL || self->dac == NULL) return NULL;
  269. try {
  270. self->dac->closeStream();
  271. Py_CLEAR(self->callback_func);
  272. }
  273. catch(RtAudioError &error) {
  274. PyErr_SetString(RtAudioErrorException, error.getMessage().c_str());
  275. Py_INCREF(RtAudioErrorException);
  276. return NULL;
  277. }
  278. Py_RETURN_NONE;
  279. }
  280. static PyObject* RtAudio_startStream(PyRtAudio *self, PyObject *args)
  281. {
  282. if (self == NULL || self->dac == NULL) return NULL;
  283. try {
  284. self->dac->startStream();
  285. }
  286. catch(RtAudioError &error) {
  287. PyErr_SetString(RtAudioErrorException, error.getMessage().c_str());
  288. Py_INCREF(RtAudioErrorException);
  289. return NULL;
  290. }
  291. Py_RETURN_NONE;
  292. }
  293. static PyObject* RtAudio_stopStream(PyRtAudio *self, PyObject *args)
  294. {
  295. printf("RtAudio_stopStream.\n");
  296. if (self == NULL || self->dac == NULL) return NULL;
  297. try {
  298. self->dac->stopStream();
  299. }
  300. catch(RtAudioError &error) {
  301. PyErr_SetString(RtAudioErrorException, error.getMessage().c_str());
  302. Py_INCREF(RtAudioErrorException);
  303. return NULL;
  304. }
  305. Py_RETURN_NONE;
  306. }
  307. static PyObject* RtAudio_abortStream(PyRtAudio *self, PyObject *args)
  308. {
  309. printf("RtAudio_abortStream.\n");
  310. if (self == NULL || self->dac == NULL) return NULL;
  311. try {
  312. self->dac->abortStream();
  313. }
  314. catch(RtAudioError &error) {
  315. PyErr_SetString(RtAudioErrorException, error.getMessage().c_str());
  316. Py_INCREF(RtAudioErrorException);
  317. return NULL;
  318. }
  319. Py_RETURN_NONE;
  320. }
  321. static PyObject* RtAudio_isStreamRunning(PyRtAudio *self, PyObject *args)
  322. {
  323. if (self == NULL || self->dac == NULL) return NULL;
  324. if (self->dac == NULL) {
  325. Py_RETURN_FALSE;
  326. }
  327. if (self->dac->isStreamRunning())
  328. Py_RETURN_TRUE;
  329. else
  330. Py_RETURN_FALSE;
  331. }
  332. static PyObject* RtAudio_isStreamOpen(PyRtAudio *self, PyObject *args)
  333. {
  334. if (self == NULL || self->dac == NULL) return NULL;
  335. if (self->dac == NULL) {
  336. Py_RETURN_FALSE;
  337. }
  338. if (self->dac->isStreamOpen())
  339. Py_RETURN_TRUE;
  340. else
  341. Py_RETURN_FALSE;
  342. }
  343. static PyObject* RtAudio_getDeviceCount(PyRtAudio *self, PyObject *args)
  344. {
  345. if (self == NULL || self->dac == NULL) return NULL;
  346. #if PY_MAJOR_VERSION >= 3
  347. return PyLong_FromLong(self->dac->getDeviceCount());
  348. #else
  349. return PyInt_FromLong(self->dac->getDeviceCount());
  350. #endif
  351. }
  352. static PyObject* RtAudio_getDeviceInfo(PyRtAudio *self, PyObject *args)
  353. {
  354. if (self == NULL || self->dac == NULL) return NULL;
  355. int device;
  356. if (!PyArg_ParseTuple(args, "i", &device))
  357. return NULL;
  358. try {
  359. RtAudio::DeviceInfo info = self->dac->getDeviceInfo(device);
  360. PyObject* info_dict = PyDict_New();
  361. if (info.probed) {
  362. Py_INCREF(Py_True);
  363. PyDict_SetItemString(info_dict, "probed", Py_True);
  364. }
  365. else {
  366. Py_INCREF(Py_False);
  367. PyDict_SetItemString(info_dict, "probed", Py_False);
  368. }
  369. PyObject* obj;
  370. #if PY_MAJOR_VERSION >= 3
  371. obj = PyUnicode_FromString(info.name.c_str());
  372. PyDict_SetItemString(info_dict, "name", obj);
  373. obj = PyLong_FromLong(info.outputChannels);
  374. PyDict_SetItemString(info_dict, "outputChannels", obj);
  375. obj = PyLong_FromLong(info.inputChannels);
  376. PyDict_SetItemString(info_dict, "inputChannels", obj);
  377. obj = PyLong_FromLong(info.duplexChannels);
  378. PyDict_SetItemString(info_dict, "duplexChannels", obj);
  379. #else
  380. obj = PyString_FromString(info.name.c_str());
  381. PyDict_SetItemString(info_dict, "name", obj);
  382. obj = PyInt_FromLong(info.outputChannels);
  383. PyDict_SetItemString(info_dict, "outputChannels", obj);
  384. obj = PyInt_FromLong(info.inputChannels);
  385. PyDict_SetItemString(info_dict, "inputChannels", obj);
  386. obj = PyInt_FromLong(info.duplexChannels);
  387. PyDict_SetItemString(info_dict, "duplexChannels", obj);
  388. #endif
  389. if (info.isDefaultOutput) {
  390. Py_INCREF(Py_True);
  391. PyDict_SetItemString(info_dict, "isDefaultOutput", Py_True);
  392. }
  393. else {
  394. Py_INCREF(Py_False);
  395. PyDict_SetItemString(info_dict, "isDefaultOutput", Py_False);
  396. }
  397. if (info.isDefaultInput) {
  398. Py_INCREF(Py_True);
  399. PyDict_SetItemString(info_dict, "isDefaultInput", Py_True);
  400. }
  401. else {
  402. Py_INCREF(Py_False);
  403. PyDict_SetItemString(info_dict, "isDefaultInput", Py_False);
  404. }
  405. return info_dict;
  406. }
  407. catch(RtAudioError &error) {
  408. PyErr_SetString(RtAudioErrorException, error.getMessage().c_str());
  409. Py_INCREF(RtAudioErrorException);
  410. return NULL;
  411. }
  412. }
  413. static PyObject* RtAudio_getDefaultOutputDevice(PyRtAudio *self, PyObject *args)
  414. {
  415. if (self == NULL || self->dac == NULL) return NULL;
  416. #if PY_MAJOR_VERSION >= 3
  417. return PyLong_FromLong(self->dac->getDefaultOutputDevice());
  418. #else
  419. return PyInt_FromLong(self->dac->getDefaultOutputDevice());
  420. #endif
  421. }
  422. static PyObject* RtAudio_getDefaultInputDevice(PyRtAudio *self, PyObject *args)
  423. {
  424. if (self == NULL || self->dac == NULL) return NULL;
  425. #if PY_MAJOR_VERSION >= 3
  426. return PyLong_FromLong(self->dac->getDefaultInputDevice());
  427. #else
  428. return PyInt_FromLong(self->dac->getDefaultInputDevice());
  429. #endif
  430. }
  431. static PyObject* RtAudio_getStreamTime(PyRtAudio *self, PyObject *args)
  432. {
  433. if (self == NULL || self->dac == NULL) return NULL;
  434. return PyFloat_FromDouble( self->dac->getStreamTime() );
  435. }
  436. static PyObject* RtAudio_getStreamLatency(PyRtAudio *self, PyObject *args)
  437. {
  438. if (self == NULL || self->dac == NULL) return NULL;
  439. #if PY_MAJOR_VERSION >= 3
  440. return PyLong_FromLong( self->dac->getStreamLatency() );
  441. #else
  442. return PyInt_FromLong( self->dac->getStreamLatency() );
  443. #endif
  444. }
  445. static PyObject* RtAudio_getStreamSampleRate(PyRtAudio *self, PyObject *args)
  446. {
  447. if (self == NULL || self->dac == NULL) return NULL;
  448. #if PY_MAJOR_VERSION >= 3
  449. return PyLong_FromLong( self->dac->getStreamSampleRate() );
  450. #else
  451. return PyInt_FromLong( self->dac->getStreamSampleRate() );
  452. #endif
  453. }
  454. static PyObject* RtAudio_showWarnings(PyRtAudio *self, PyObject *args)
  455. {
  456. if (self == NULL || self->dac == NULL) return NULL;
  457. PyObject *obj;
  458. if (!PyArg_ParseTuple(args, "O", &obj))
  459. return NULL;
  460. if (!PyBool_Check(obj))
  461. return NULL;
  462. if (obj == Py_True)
  463. self->dac->showWarnings(true);
  464. else if (obj == Py_False)
  465. self->dac->showWarnings(false);
  466. else {
  467. printf("not true nor false\n");
  468. }
  469. Py_RETURN_NONE;
  470. }
  471. static PyMethodDef RtAudio_methods[] =
  472. {
  473. // TO BE DONE: getCurrentApi(void)
  474. {"getDeviceCount", (PyCFunction) RtAudio_getDeviceCount, METH_NOARGS,
  475. "A public function that queries for the number of audio devices available."},
  476. {"getDeviceInfo", (PyCFunction) RtAudio_getDeviceInfo, METH_VARARGS,
  477. "Return a dictionary with information for a specified device number."},
  478. {"getDefaultOutputDevice", (PyCFunction) RtAudio_getDefaultOutputDevice, METH_NOARGS,
  479. "A function that returns the index of the default output device."},
  480. {"getDefaultInputDevice", (PyCFunction) RtAudio_getDefaultInputDevice, METH_NOARGS,
  481. "A function that returns the index of the default input device."},
  482. {"openStream", (PyCFunction) RtAudio_openStream, METH_VARARGS,
  483. "A public method for opening a stream with the specified parameters."},
  484. {"closeStream", (PyCFunction) RtAudio_closeStream, METH_NOARGS,
  485. "A function that closes a stream and frees any associated stream memory. "},
  486. {"startStream", (PyCFunction) RtAudio_startStream, METH_NOARGS,
  487. "A function that starts a stream. "},
  488. {"stopStream", (PyCFunction) RtAudio_stopStream, METH_NOARGS,
  489. "Stop a stream, allowing any samples remaining in the output queue to be played. "},
  490. {"abortStream", (PyCFunction) RtAudio_abortStream, METH_NOARGS,
  491. "Stop a stream, discarding any samples remaining in the input/output queue."},
  492. {"isStreamOpen", (PyCFunction) RtAudio_isStreamOpen, METH_NOARGS,
  493. "Returns true if a stream is open and false if not."},
  494. {"isStreamRunning", (PyCFunction) RtAudio_isStreamRunning, METH_NOARGS,
  495. "Returns true if the stream is running and false if it is stopped or not open."},
  496. {"getStreamTime", (PyCFunction) RtAudio_getStreamTime, METH_NOARGS,
  497. "Returns the number of elapsed seconds since the stream was started."},
  498. {"getStreamLatency", (PyCFunction) RtAudio_getStreamLatency, METH_NOARGS,
  499. "Returns the internal stream latency in sample frames."},
  500. {"getStreamSampleRate", (PyCFunction) RtAudio_getStreamSampleRate, METH_NOARGS,
  501. "Returns actual sample rate in use by the stream."},
  502. {"showWarnings", (PyCFunction) RtAudio_showWarnings, METH_VARARGS,
  503. "Specify whether warning messages should be printed to stderr."},
  504. // TO BE DONE: getCompiledApi (std::vector< RtAudio::Api > &apis) throw ()
  505. {NULL}
  506. };
  507. static PyTypeObject RtAudio_type = {
  508. PyVarObject_HEAD_INIT(NULL, 0)
  509. "rtaudio.RtAudio", /*tp_name*/
  510. sizeof(RtAudio), /*tp_basicsize*/
  511. 0, /*tp_itemsize*/
  512. (destructor) RtAudio_dealloc, /*tp_dealloc*/
  513. 0, /*tp_print*/
  514. 0, /*tp_getattr*/
  515. 0, /*tp_setattr*/
  516. 0, /*tp_compare*/
  517. 0, /*tp_repr*/
  518. 0, /*tp_as_number*/
  519. 0, /*tp_as_sequence*/
  520. 0, /*tp_as_mapping*/
  521. 0, /*tp_hash */
  522. 0, /*tp_call*/
  523. 0, /*tp_str*/
  524. 0, /*tp_getattro*/
  525. 0, /*tp_setattro*/
  526. 0, /*tp_as_buffer*/
  527. Py_TPFLAGS_DEFAULT, /*tp_flags*/
  528. "Audio input device", /* tp_doc */
  529. 0, /* tp_traverse */
  530. 0, /* tp_clear */
  531. 0, /* tp_richcompare */
  532. 0, /* tp_weaklistoffset */
  533. 0, /* tp_iter */
  534. 0, /* tp_iternext */
  535. RtAudio_methods, /* tp_methods */
  536. 0, /* tp_members */
  537. 0, /* tp_getset */
  538. 0, /* tp_base */
  539. 0, /* tp_dict */
  540. 0, /* tp_descr_get */
  541. 0, /* tp_descr_set */
  542. 0, /* tp_dictoffset */
  543. (initproc)RtAudio_init, /* tp_init */
  544. 0, /* tp_alloc */
  545. RtAudio_new, /* tp_new */
  546. 0, /* Low-level free-memory routine */
  547. 0, /* For PyObject_IS_GC */
  548. 0, // PyObject *tp_bases;
  549. 0, // PyObject *tp_mro; /* method resolution order */
  550. 0, //PyObject *tp_cache;
  551. 0, //PyObject *tp_subclasses;
  552. 0, //PyObject *tp_weaklist;
  553. 0, //destructor tp_del;
  554. //0, /* Type attribute cache version tag. Added in version 2.6 */
  555. };
  556. #if PY_MAJOR_VERSION >= 3
  557. static PyModuleDef RtAudio_module = {
  558. PyModuleDef_HEAD_INIT,
  559. "RtAudio",
  560. "RtAudio wrapper.",
  561. };
  562. #endif
  563. #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
  564. #define PyMODINIT_FUNC void
  565. #endif
  566. PyMODINIT_FUNC
  567. #if PY_MAJOR_VERSION >= 3
  568. PyInit_rtaudio(void)
  569. #else
  570. initrtaudio(void)
  571. #endif
  572. {
  573. if (!PyEval_ThreadsInitialized())
  574. PyEval_InitThreads();
  575. if (PyType_Ready(&RtAudio_type) < 0)
  576. #if PY_MAJOR_VERSION >= 3
  577. return NULL;
  578. #else
  579. return;
  580. #endif
  581. #if PY_MAJOR_VERSION >= 3
  582. PyObject* module = PyModule_Create(&RtAudio_module);
  583. if (module == NULL)
  584. return NULL;
  585. #else
  586. PyObject* module = Py_InitModule3("rtaudio", NULL, "RtAudio wrapper.");
  587. if (module == NULL)
  588. return;
  589. #endif
  590. Py_INCREF(&RtAudio_type);
  591. PyModule_AddObject(module, "RtAudio", (PyObject *)&RtAudio_type);
  592. RtAudioErrorException = PyErr_NewException("rtaudio.RtError", NULL, NULL);
  593. Py_INCREF(RtAudioErrorException);
  594. PyModule_AddObject(module, "RtError", RtAudioErrorException);
  595. #if PY_MAJOR_VERSION >= 3
  596. return module;
  597. #else
  598. return;
  599. #endif
  600. }
  601. }