/Mac/Modules/snd/_Sndihooks.c

http://unladen-swallow.googlecode.com/ · C · 512 lines · 381 code · 89 blank · 42 comment · 62 complexity · f5f722c29010ea556675d4e8275ea2e8 MD5 · raw file

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4. All Rights Reserved
  5. Permission to use, copy, modify, and distribute this software and its
  6. documentation for any purpose and without fee is hereby granted,
  7. provided that the above copyright notice appear in all copies and that
  8. both that copyright notice and this permission notice appear in
  9. supporting documentation, and that the names of Stichting Mathematisch
  10. Centrum or CWI or Corporation for National Research Initiatives or
  11. CNRI not be used in advertising or publicity pertaining to
  12. distribution of the software without specific, written prior
  13. permission.
  14. While CWI is the initial source for this software, a modified version
  15. is made available by the Corporation for National Research Initiatives
  16. (CNRI) at the Internet address ftp://ftp.python.org.
  17. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  18. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  19. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  20. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  21. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  22. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  23. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  24. PERFORMANCE OF THIS SOFTWARE.
  25. ******************************************************************/
  26. #include "Python.h"
  27. #include "pymactoolbox.h"
  28. #include <Sound.h>
  29. #pragma options align=mac68k
  30. struct SampleRateAvailable_arg {
  31. short numrates;
  32. Handle rates;
  33. };
  34. struct SampleSizeAvailable_arg {
  35. short numsizes;
  36. Handle sizes;
  37. };
  38. #pragma options align=reset
  39. static PyObject *ErrorObject;
  40. /* Convert Python object to unsigned Fixed */
  41. static int
  42. PyMac_GetUFixed(PyObject *v, Fixed *f)
  43. {
  44. double d;
  45. unsigned long uns;
  46. if( !PyArg_Parse(v, "d", &d))
  47. return 0;
  48. uns = (unsigned long)(d * 0x10000);
  49. *f = (Fixed)uns;
  50. return 1;
  51. }
  52. /* Convert a Point to a Python object */
  53. static PyObject *
  54. PyMac_BuildUFixed(Fixed f)
  55. {
  56. double d;
  57. unsigned long funs;
  58. funs = (unsigned long)f;
  59. d = funs;
  60. d = d / 0x10000;
  61. return Py_BuildValue("d", d);
  62. }
  63. /* ----------------------------------------------------- */
  64. static char sndih_getChannelAvailable__doc__[] =
  65. ""
  66. ;
  67. static PyObject *
  68. sndih_getChannelAvailable(self, args)
  69. PyObject *self; /* Not used */
  70. PyObject *args;
  71. {
  72. long inRefNum;
  73. short nchannel;
  74. OSErr err;
  75. if (!PyArg_ParseTuple(args, "l", &inRefNum))
  76. return NULL;
  77. if( (err=SPBGetDeviceInfo(inRefNum, siChannelAvailable, (Ptr)&nchannel)) != noErr )
  78. return PyMac_Error(err);
  79. return Py_BuildValue("h", nchannel);
  80. }
  81. static char sndih_getNumberChannels__doc__[] =
  82. ""
  83. ;
  84. static PyObject *
  85. sndih_getNumberChannels(self, args)
  86. PyObject *self; /* Not used */
  87. PyObject *args;
  88. {
  89. long inRefNum;
  90. short nchannel;
  91. OSErr err;
  92. if (!PyArg_ParseTuple(args, "l", &inRefNum))
  93. return NULL;
  94. if( (err=SPBGetDeviceInfo(inRefNum, siNumberChannels, (Ptr)&nchannel)) != noErr )
  95. return PyMac_Error(err);
  96. return Py_BuildValue("h", nchannel);
  97. }
  98. static char sndih_setNumberChannels__doc__[] =
  99. ""
  100. ;
  101. static PyObject *
  102. sndih_setNumberChannels(self, args)
  103. PyObject *self; /* Not used */
  104. PyObject *args;
  105. {
  106. long inRefNum;
  107. short nchannel;
  108. OSErr err;
  109. if (!PyArg_ParseTuple(args, "lh", &inRefNum, &nchannel))
  110. return NULL;
  111. if( (err=SPBSetDeviceInfo(inRefNum, siNumberChannels, (Ptr)&nchannel)) != noErr )
  112. return PyMac_Error(err);
  113. Py_INCREF(Py_None);
  114. return Py_None;
  115. }
  116. static char sndih_getContinuous__doc__[] =
  117. ""
  118. ;
  119. static PyObject *
  120. sndih_getContinuous(self, args)
  121. PyObject *self; /* Not used */
  122. PyObject *args;
  123. {
  124. long inRefNum;
  125. short onoff;
  126. OSErr err;
  127. if (!PyArg_ParseTuple(args, "l", &inRefNum))
  128. return NULL;
  129. if( (err=SPBGetDeviceInfo(inRefNum, siContinuous, (Ptr)&onoff)) != noErr )
  130. return PyMac_Error(err);
  131. return Py_BuildValue("h", onoff);
  132. }
  133. static char sndih_setContinuous__doc__[] =
  134. ""
  135. ;
  136. static PyObject *
  137. sndih_setContinuous(self, args)
  138. PyObject *self; /* Not used */
  139. PyObject *args;
  140. {
  141. long inRefNum;
  142. short onoff;
  143. OSErr err;
  144. if (!PyArg_ParseTuple(args, "lh", &inRefNum, &onoff))
  145. return NULL;
  146. if( (err=SPBSetDeviceInfo(inRefNum, siContinuous, (Ptr)&onoff)) != noErr )
  147. return PyMac_Error(err);
  148. Py_INCREF(Py_None);
  149. return Py_None;
  150. }
  151. static char sndih_getInputSourceNames__doc__[] =
  152. ""
  153. ;
  154. static PyObject *
  155. sndih_getInputSourceNames(self, args)
  156. PyObject *self; /* Not used */
  157. PyObject *args;
  158. {
  159. long inRefNum;
  160. Handle names;
  161. OSErr err;
  162. if (!PyArg_ParseTuple(args, "l", &inRefNum))
  163. return NULL;
  164. if( (err=SPBGetDeviceInfo(inRefNum, siInputSourceNames, (Ptr)&names)) != noErr )
  165. return PyMac_Error(err);
  166. return Py_BuildValue("O&", ResObj_New, names);
  167. }
  168. static char sndih_getInputSource__doc__[] =
  169. ""
  170. ;
  171. static PyObject *
  172. sndih_getInputSource(self, args)
  173. PyObject *self; /* Not used */
  174. PyObject *args;
  175. {
  176. long inRefNum;
  177. short source;
  178. OSErr err;
  179. if (!PyArg_ParseTuple(args, "l", &inRefNum))
  180. return NULL;
  181. if( (err=SPBGetDeviceInfo(inRefNum, siInputSource, (Ptr)&source)) != noErr )
  182. return PyMac_Error(err);
  183. return Py_BuildValue("h", source);
  184. }
  185. static char sndih_setInputSource__doc__[] =
  186. ""
  187. ;
  188. static PyObject *
  189. sndih_setInputSource(self, args)
  190. PyObject *self; /* Not used */
  191. PyObject *args;
  192. {
  193. long inRefNum;
  194. short source;
  195. OSErr err;
  196. if (!PyArg_ParseTuple(args, "lh", &inRefNum, &source))
  197. return NULL;
  198. if( (err=SPBSetDeviceInfo(inRefNum, siInputSource, (Ptr)&source)) != noErr )
  199. return PyMac_Error(err);
  200. Py_INCREF(Py_None);
  201. return Py_None;
  202. }
  203. static char sndih_getPlayThruOnOff__doc__[] =
  204. ""
  205. ;
  206. static PyObject *
  207. sndih_getPlayThruOnOff(self, args)
  208. PyObject *self; /* Not used */
  209. PyObject *args;
  210. {
  211. long inRefNum;
  212. short onoff;
  213. OSErr err;
  214. if (!PyArg_ParseTuple(args, "l", &inRefNum))
  215. return NULL;
  216. if( (err=SPBGetDeviceInfo(inRefNum, siPlayThruOnOff, (Ptr)&onoff)) != noErr )
  217. return PyMac_Error(err);
  218. return Py_BuildValue("h", onoff);
  219. }
  220. static char sndih_setPlayThruOnOff__doc__[] =
  221. ""
  222. ;
  223. static PyObject *
  224. sndih_setPlayThruOnOff(self, args)
  225. PyObject *self; /* Not used */
  226. PyObject *args;
  227. {
  228. long inRefNum;
  229. short onoff;
  230. OSErr err;
  231. if (!PyArg_ParseTuple(args, "lh", &inRefNum, &onoff))
  232. return NULL;
  233. if( (err=SPBSetDeviceInfo(inRefNum, siPlayThruOnOff, (Ptr)&onoff)) != noErr )
  234. return PyMac_Error(err);
  235. Py_INCREF(Py_None);
  236. return Py_None;
  237. }
  238. static char sndih_getSampleRate__doc__[] =
  239. ""
  240. ;
  241. static PyObject *
  242. sndih_getSampleRate(self, args)
  243. PyObject *self; /* Not used */
  244. PyObject *args;
  245. {
  246. long inRefNum;
  247. Fixed sample_rate;
  248. OSErr err;
  249. if (!PyArg_ParseTuple(args, "l", &inRefNum))
  250. return NULL;
  251. if( (err=SPBGetDeviceInfo(inRefNum, siSampleRate, (Ptr)&sample_rate)) != noErr )
  252. return PyMac_Error(err);
  253. return Py_BuildValue("O&", PyMac_BuildUFixed, sample_rate);
  254. }
  255. static char sndih_setSampleRate__doc__[] =
  256. ""
  257. ;
  258. static PyObject *
  259. sndih_setSampleRate(self, args)
  260. PyObject *self; /* Not used */
  261. PyObject *args;
  262. {
  263. long inRefNum;
  264. Fixed sample_rate;
  265. OSErr err;
  266. if (!PyArg_ParseTuple(args, "lO&", &inRefNum, PyMac_GetUFixed, &sample_rate))
  267. return NULL;
  268. if( (err=SPBSetDeviceInfo(inRefNum, siSampleRate, (Ptr)&sample_rate)) != noErr )
  269. return PyMac_Error(err);
  270. Py_INCREF(Py_None);
  271. return Py_None;
  272. }
  273. static char sndih_getSampleSize__doc__[] =
  274. ""
  275. ;
  276. static PyObject *
  277. sndih_getSampleSize(self, args)
  278. PyObject *self; /* Not used */
  279. PyObject *args;
  280. {
  281. long inRefNum;
  282. short bits;
  283. OSErr err;
  284. if (!PyArg_ParseTuple(args, "l", &inRefNum))
  285. return NULL;
  286. if( (err=SPBGetDeviceInfo(inRefNum, siSampleSize, (Ptr)&bits)) != noErr )
  287. return PyMac_Error(err);
  288. return Py_BuildValue("h", bits);
  289. }
  290. static char sndih_setSampleSize__doc__[] =
  291. ""
  292. ;
  293. static PyObject *
  294. sndih_setSampleSize(self, args)
  295. PyObject *self; /* Not used */
  296. PyObject *args;
  297. {
  298. long inRefNum;
  299. short size;
  300. OSErr err;
  301. if (!PyArg_ParseTuple(args, "lh", &inRefNum, &size))
  302. return NULL;
  303. if( (err=SPBSetDeviceInfo(inRefNum, siSampleSize, (Ptr)&size)) != noErr )
  304. return PyMac_Error(err);
  305. Py_INCREF(Py_None);
  306. return Py_None;
  307. }
  308. static char sndih_getSampleSizeAvailable__doc__[] =
  309. ""
  310. ;
  311. static PyObject *
  312. sndih_getSampleSizeAvailable(self, args)
  313. PyObject *self; /* Not used */
  314. PyObject *args;
  315. {
  316. long inRefNum;
  317. struct SampleSizeAvailable_arg arg;
  318. OSErr err;
  319. PyObject *rsizes;
  320. short *fsizes;
  321. int i;
  322. arg.sizes = NULL;
  323. rsizes = NULL;
  324. if (!PyArg_ParseTuple(args, "l", &inRefNum))
  325. return NULL;
  326. if( (err=SPBGetDeviceInfo(inRefNum, siSampleSizeAvailable, (Ptr)&arg)) != noErr ) {
  327. return PyMac_Error(err);
  328. }
  329. fsizes = (short *)*(arg.sizes);
  330. /* Handle contains a list of rates */
  331. if( (rsizes = PyTuple_New(arg.numsizes)) == NULL)
  332. return NULL;
  333. for( i=0; i<arg.numsizes; i++ )
  334. PyTuple_SetItem(rsizes, i, PyInt_FromLong((long)fsizes[i]));
  335. return rsizes;
  336. }
  337. static char sndih_getSampleRateAvailable__doc__[] =
  338. ""
  339. ;
  340. static PyObject *
  341. sndih_getSampleRateAvailable(self, args)
  342. PyObject *self; /* Not used */
  343. PyObject *args;
  344. {
  345. long inRefNum;
  346. struct SampleRateAvailable_arg arg;
  347. OSErr err;
  348. PyObject *rrates, *obj;
  349. Fixed *frates;
  350. int i;
  351. arg.rates = NULL;
  352. rrates = NULL;
  353. if (!PyArg_ParseTuple(args, "l", &inRefNum))
  354. return NULL;
  355. if( (err=SPBGetDeviceInfo(inRefNum, siSampleRateAvailable, (Ptr)&arg)) != noErr ) {
  356. return PyMac_Error(err);
  357. }
  358. frates = (Fixed *)*(arg.rates);
  359. if( arg.numrates == 0 ) {
  360. /* The handle contains upper and lowerbound */
  361. rrates = Py_BuildValue("O&O&", frates[0], frates[1]);
  362. if (rrates == NULL) return NULL;
  363. } else {
  364. /* Handle contains a list of rates */
  365. if( (rrates = PyTuple_New(arg.numrates)) == NULL)
  366. return NULL;
  367. for( i=0; i<arg.numrates; i++ ) {
  368. if( (obj = Py_BuildValue("O&", PyMac_BuildUFixed, frates[i]))==NULL)
  369. goto out;
  370. PyTuple_SetItem(rrates, i, obj);
  371. }
  372. }
  373. return Py_BuildValue("hO", arg.numrates, rrates);
  374. out:
  375. Py_XDECREF(rrates);
  376. return NULL;
  377. }
  378. /* List of methods defined in the module */
  379. static struct PyMethodDef sndih_methods[] = {
  380. {"getChannelAvailable", (PyCFunction)sndih_getChannelAvailable, METH_VARARGS, sndih_getChannelAvailable__doc__},
  381. {"getNumberChannels", (PyCFunction)sndih_getNumberChannels, METH_VARARGS, sndih_getNumberChannels__doc__},
  382. {"setNumberChannels", (PyCFunction)sndih_setNumberChannels, METH_VARARGS, sndih_setNumberChannels__doc__},
  383. {"getContinuous", (PyCFunction)sndih_getContinuous, METH_VARARGS, sndih_getContinuous__doc__},
  384. {"setContinuous", (PyCFunction)sndih_setContinuous, METH_VARARGS, sndih_setContinuous__doc__},
  385. {"getInputSourceNames", (PyCFunction)sndih_getInputSourceNames, METH_VARARGS, sndih_getInputSourceNames__doc__},
  386. {"getInputSource", (PyCFunction)sndih_getInputSource, METH_VARARGS, sndih_getInputSource__doc__},
  387. {"setInputSource", (PyCFunction)sndih_setInputSource, METH_VARARGS, sndih_setInputSource__doc__},
  388. {"getPlayThruOnOff", (PyCFunction)sndih_getPlayThruOnOff, METH_VARARGS, sndih_getPlayThruOnOff__doc__},
  389. {"setPlayThruOnOff", (PyCFunction)sndih_setPlayThruOnOff, METH_VARARGS, sndih_setPlayThruOnOff__doc__},
  390. {"getSampleRate", (PyCFunction)sndih_getSampleRate, METH_VARARGS, sndih_getSampleRate__doc__},
  391. {"setSampleRate", (PyCFunction)sndih_setSampleRate, METH_VARARGS, sndih_setSampleRate__doc__},
  392. {"getSampleSize", (PyCFunction)sndih_getSampleSize, METH_VARARGS, sndih_getSampleSize__doc__},
  393. {"setSampleSize", (PyCFunction)sndih_setSampleSize, METH_VARARGS, sndih_setSampleSize__doc__},
  394. {"getSampleSizeAvailable", (PyCFunction)sndih_getSampleSizeAvailable, METH_VARARGS, sndih_getSampleSizeAvailable__doc__},
  395. {"getSampleRateAvailable", (PyCFunction)sndih_getSampleRateAvailable, METH_VARARGS, sndih_getSampleRateAvailable__doc__},
  396. {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
  397. };
  398. /* Initialization function for the module (*must* be called initSndihooks) */
  399. static char Sndihooks_module_documentation[] =
  400. ""
  401. ;
  402. void
  403. init_Sndihooks()
  404. {
  405. PyObject *m, *d;
  406. /* Create the module and add the functions */
  407. m = Py_InitModule4("_Sndihooks", sndih_methods,
  408. Sndihooks_module_documentation,
  409. (PyObject*)NULL,PYTHON_API_VERSION);
  410. /* Add some symbolic constants to the module */
  411. d = PyModule_GetDict(m);
  412. ErrorObject = PyString_FromString("Sndihooks.error");
  413. PyDict_SetItemString(d, "error", ErrorObject);
  414. /* XXXX Add constants here */
  415. /* Check for errors */
  416. if (PyErr_Occurred())
  417. Py_FatalError("can't initialize module Sndihooks");
  418. }