Added 'list_only' option (and modified 'run()' to respect it).
[python/dscho.git] / Mac / Modules / snd / Sndihooks.c
blob5c152d736470325432b0c7da15bc04d0848c9c5a
1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
5 All Rights Reserved
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
15 permission.
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
32 #include "Python.h"
33 #include "macglue.h"
34 #include <Sound.h>
36 #pragma options align=mac68k
37 struct SampleRateAvailable_arg {
38 short numrates;
39 Handle rates;
42 struct SampleSizeAvailable_arg {
43 short numsizes;
44 Handle sizes;
47 #pragma options align=reset
49 extern PyObject *ResObj_New(Handle);
51 static PyObject *ErrorObject;
54 /* Convert Python object to unsigned Fixed */
55 static int
56 PyMac_GetUFixed(PyObject *v, Fixed *f)
58 double d;
59 unsigned long uns;
61 if( !PyArg_Parse(v, "d", &d))
62 return 0;
63 uns = (unsigned long)(d * 0x10000);
64 *f = (Fixed)uns;
65 return 1;
68 /* Convert a Point to a Python object */
69 static PyObject *
70 PyMac_BuildUFixed(Fixed f)
72 double d;
73 unsigned long funs;
75 funs = (unsigned long)f;
77 d = funs;
78 d = d / 0x10000;
79 return Py_BuildValue("d", d);
83 /* ----------------------------------------------------- */
85 static char sndih_getChannelAvailable__doc__[] =
89 static PyObject *
90 sndih_getChannelAvailable(self, args)
91 PyObject *self; /* Not used */
92 PyObject *args;
94 long inRefNum;
95 short nchannel;
96 OSErr err;
98 if (!PyArg_ParseTuple(args, "l", &inRefNum))
99 return NULL;
101 if( (err=SPBGetDeviceInfo(inRefNum, siChannelAvailable, (Ptr)&nchannel)) != noErr )
102 return PyMac_Error(err);
103 return Py_BuildValue("h", nchannel);
106 static char sndih_getNumberChannels__doc__[] =
110 static PyObject *
111 sndih_getNumberChannels(self, args)
112 PyObject *self; /* Not used */
113 PyObject *args;
115 long inRefNum;
116 short nchannel;
117 OSErr err;
119 if (!PyArg_ParseTuple(args, "l", &inRefNum))
120 return NULL;
122 if( (err=SPBGetDeviceInfo(inRefNum, siNumberChannels, (Ptr)&nchannel)) != noErr )
123 return PyMac_Error(err);
124 return Py_BuildValue("h", nchannel);
127 static char sndih_setNumberChannels__doc__[] =
131 static PyObject *
132 sndih_setNumberChannels(self, args)
133 PyObject *self; /* Not used */
134 PyObject *args;
136 long inRefNum;
137 short nchannel;
138 OSErr err;
140 if (!PyArg_ParseTuple(args, "lh", &inRefNum, &nchannel))
141 return NULL;
143 if( (err=SPBSetDeviceInfo(inRefNum, siNumberChannels, (Ptr)&nchannel)) != noErr )
144 return PyMac_Error(err);
145 Py_INCREF(Py_None);
146 return Py_None;
149 static char sndih_getContinuous__doc__[] =
153 static PyObject *
154 sndih_getContinuous(self, args)
155 PyObject *self; /* Not used */
156 PyObject *args;
158 long inRefNum;
159 short onoff;
160 OSErr err;
162 if (!PyArg_ParseTuple(args, "l", &inRefNum))
163 return NULL;
165 if( (err=SPBGetDeviceInfo(inRefNum, siContinuous, (Ptr)&onoff)) != noErr )
166 return PyMac_Error(err);
167 return Py_BuildValue("h", onoff);
170 static char sndih_setContinuous__doc__[] =
174 static PyObject *
175 sndih_setContinuous(self, args)
176 PyObject *self; /* Not used */
177 PyObject *args;
179 long inRefNum;
180 short onoff;
181 OSErr err;
183 if (!PyArg_ParseTuple(args, "lh", &inRefNum, &onoff))
184 return NULL;
186 if( (err=SPBSetDeviceInfo(inRefNum, siContinuous, (Ptr)&onoff)) != noErr )
187 return PyMac_Error(err);
188 Py_INCREF(Py_None);
189 return Py_None;
192 static char sndih_getInputSourceNames__doc__[] =
196 static PyObject *
197 sndih_getInputSourceNames(self, args)
198 PyObject *self; /* Not used */
199 PyObject *args;
201 long inRefNum;
202 Handle names;
203 OSErr err;
205 if (!PyArg_ParseTuple(args, "l", &inRefNum))
206 return NULL;
208 if( (err=SPBGetDeviceInfo(inRefNum, siInputSourceNames, (Ptr)&names)) != noErr )
209 return PyMac_Error(err);
210 return Py_BuildValue("O&", ResObj_New, names);
213 static char sndih_getInputSource__doc__[] =
217 static PyObject *
218 sndih_getInputSource(self, args)
219 PyObject *self; /* Not used */
220 PyObject *args;
222 long inRefNum;
223 short source;
224 OSErr err;
226 if (!PyArg_ParseTuple(args, "l", &inRefNum))
227 return NULL;
229 if( (err=SPBGetDeviceInfo(inRefNum, siInputSource, (Ptr)&source)) != noErr )
230 return PyMac_Error(err);
231 return Py_BuildValue("h", source);
234 static char sndih_setInputSource__doc__[] =
238 static PyObject *
239 sndih_setInputSource(self, args)
240 PyObject *self; /* Not used */
241 PyObject *args;
243 long inRefNum;
244 short source;
245 OSErr err;
247 if (!PyArg_ParseTuple(args, "lh", &inRefNum, &source))
248 return NULL;
250 if( (err=SPBSetDeviceInfo(inRefNum, siInputSource, (Ptr)&source)) != noErr )
251 return PyMac_Error(err);
252 Py_INCREF(Py_None);
253 return Py_None;
256 static char sndih_getPlayThruOnOff__doc__[] =
260 static PyObject *
261 sndih_getPlayThruOnOff(self, args)
262 PyObject *self; /* Not used */
263 PyObject *args;
265 long inRefNum;
266 short onoff;
267 OSErr err;
269 if (!PyArg_ParseTuple(args, "l", &inRefNum))
270 return NULL;
272 if( (err=SPBGetDeviceInfo(inRefNum, siPlayThruOnOff, (Ptr)&onoff)) != noErr )
273 return PyMac_Error(err);
274 return Py_BuildValue("h", onoff);
277 static char sndih_setPlayThruOnOff__doc__[] =
281 static PyObject *
282 sndih_setPlayThruOnOff(self, args)
283 PyObject *self; /* Not used */
284 PyObject *args;
286 long inRefNum;
287 short onoff;
288 OSErr err;
290 if (!PyArg_ParseTuple(args, "lh", &inRefNum, &onoff))
291 return NULL;
293 if( (err=SPBSetDeviceInfo(inRefNum, siPlayThruOnOff, (Ptr)&onoff)) != noErr )
294 return PyMac_Error(err);
295 Py_INCREF(Py_None);
296 return Py_None;
299 static char sndih_getSampleRate__doc__[] =
303 static PyObject *
304 sndih_getSampleRate(self, args)
305 PyObject *self; /* Not used */
306 PyObject *args;
308 long inRefNum;
309 Fixed sample_rate;
310 OSErr err;
312 if (!PyArg_ParseTuple(args, "l", &inRefNum))
313 return NULL;
315 if( (err=SPBGetDeviceInfo(inRefNum, siSampleRate, (Ptr)&sample_rate)) != noErr )
316 return PyMac_Error(err);
317 return Py_BuildValue("O&", PyMac_BuildUFixed, sample_rate);
320 static char sndih_setSampleRate__doc__[] =
324 static PyObject *
325 sndih_setSampleRate(self, args)
326 PyObject *self; /* Not used */
327 PyObject *args;
329 long inRefNum;
330 Fixed sample_rate;
331 OSErr err;
333 if (!PyArg_ParseTuple(args, "lO&", &inRefNum, PyMac_GetUFixed, &sample_rate))
334 return NULL;
336 if( (err=SPBSetDeviceInfo(inRefNum, siSampleRate, (Ptr)&sample_rate)) != noErr )
337 return PyMac_Error(err);
338 Py_INCREF(Py_None);
339 return Py_None;
342 static char sndih_getSampleSize__doc__[] =
346 static PyObject *
347 sndih_getSampleSize(self, args)
348 PyObject *self; /* Not used */
349 PyObject *args;
351 long inRefNum;
352 short bits;
353 OSErr err;
355 if (!PyArg_ParseTuple(args, "l", &inRefNum))
356 return NULL;
358 if( (err=SPBGetDeviceInfo(inRefNum, siSampleSize, (Ptr)&bits)) != noErr )
359 return PyMac_Error(err);
360 return Py_BuildValue("h", bits);
363 static char sndih_setSampleSize__doc__[] =
367 static PyObject *
368 sndih_setSampleSize(self, args)
369 PyObject *self; /* Not used */
370 PyObject *args;
372 long inRefNum;
373 short size;
374 OSErr err;
376 if (!PyArg_ParseTuple(args, "lh", &inRefNum, &size))
377 return NULL;
379 if( (err=SPBSetDeviceInfo(inRefNum, siSampleSize, (Ptr)&size)) != noErr )
380 return PyMac_Error(err);
381 Py_INCREF(Py_None);
382 return Py_None;
385 static char sndih_getSampleSizeAvailable__doc__[] =
389 static PyObject *
390 sndih_getSampleSizeAvailable(self, args)
391 PyObject *self; /* Not used */
392 PyObject *args;
394 long inRefNum;
395 struct SampleSizeAvailable_arg arg;
396 OSErr err;
397 PyObject *rsizes;
398 short *fsizes;
399 int i;
401 arg.sizes = NULL;
402 rsizes = NULL;
403 if (!PyArg_ParseTuple(args, "l", &inRefNum))
404 return NULL;
406 if( (err=SPBGetDeviceInfo(inRefNum, siSampleSizeAvailable, (Ptr)&arg)) != noErr ) {
407 return PyMac_Error(err);
409 fsizes = (short *)*(arg.sizes);
410 /* Handle contains a list of rates */
411 if( (rsizes = PyTuple_New(arg.numsizes)) == NULL)
412 return NULL;
413 for( i=0; i<arg.numsizes; i++ )
414 PyTuple_SetItem(rsizes, i, PyInt_FromLong((long)fsizes[i]));
415 return rsizes;
418 static char sndih_getSampleRateAvailable__doc__[] =
422 static PyObject *
423 sndih_getSampleRateAvailable(self, args)
424 PyObject *self; /* Not used */
425 PyObject *args;
427 long inRefNum;
428 struct SampleRateAvailable_arg arg;
429 OSErr err;
430 PyObject *rrates, *obj;
431 Fixed *frates;
432 int i;
434 arg.rates = NULL;
435 rrates = NULL;
436 if (!PyArg_ParseTuple(args, "l", &inRefNum))
437 return NULL;
439 if( (err=SPBGetDeviceInfo(inRefNum, siSampleRateAvailable, (Ptr)&arg)) != noErr ) {
440 return PyMac_Error(err);
442 frates = (Fixed *)*(arg.rates);
443 if( arg.numrates == 0 ) {
444 /* The handle contains upper and lowerbound */
445 rrates = Py_BuildValue("O&O&", frates[0], frates[1]);
446 if (rrates == NULL) return NULL;
447 } else {
448 /* Handle contains a list of rates */
449 if( (rrates = PyTuple_New(arg.numrates)) == NULL)
450 return NULL;
451 for( i=0; i<arg.numrates; i++ ) {
452 if( (obj = Py_BuildValue("O&", PyMac_BuildUFixed, frates[i]))==NULL)
453 goto out;
454 PyTuple_SetItem(rrates, i, obj);
457 return Py_BuildValue("hO", arg.numrates, rrates);
458 out:
459 Py_XDECREF(rrates);
460 return NULL;
463 /* List of methods defined in the module */
465 static struct PyMethodDef sndih_methods[] = {
466 {"getChannelAvailable", (PyCFunction)sndih_getChannelAvailable, METH_VARARGS, sndih_getChannelAvailable__doc__},
467 {"getNumberChannels", (PyCFunction)sndih_getNumberChannels, METH_VARARGS, sndih_getNumberChannels__doc__},
468 {"setNumberChannels", (PyCFunction)sndih_setNumberChannels, METH_VARARGS, sndih_setNumberChannels__doc__},
469 {"getContinuous", (PyCFunction)sndih_getContinuous, METH_VARARGS, sndih_getContinuous__doc__},
470 {"setContinuous", (PyCFunction)sndih_setContinuous, METH_VARARGS, sndih_setContinuous__doc__},
471 {"getInputSourceNames", (PyCFunction)sndih_getInputSourceNames, METH_VARARGS, sndih_getInputSourceNames__doc__},
472 {"getInputSource", (PyCFunction)sndih_getInputSource, METH_VARARGS, sndih_getInputSource__doc__},
473 {"setInputSource", (PyCFunction)sndih_setInputSource, METH_VARARGS, sndih_setInputSource__doc__},
474 {"getPlayThruOnOff", (PyCFunction)sndih_getPlayThruOnOff, METH_VARARGS, sndih_getPlayThruOnOff__doc__},
475 {"setPlayThruOnOff", (PyCFunction)sndih_setPlayThruOnOff, METH_VARARGS, sndih_setPlayThruOnOff__doc__},
476 {"getSampleRate", (PyCFunction)sndih_getSampleRate, METH_VARARGS, sndih_getSampleRate__doc__},
477 {"setSampleRate", (PyCFunction)sndih_setSampleRate, METH_VARARGS, sndih_setSampleRate__doc__},
478 {"getSampleSize", (PyCFunction)sndih_getSampleSize, METH_VARARGS, sndih_getSampleSize__doc__},
479 {"setSampleSize", (PyCFunction)sndih_setSampleSize, METH_VARARGS, sndih_setSampleSize__doc__},
480 {"getSampleSizeAvailable", (PyCFunction)sndih_getSampleSizeAvailable, METH_VARARGS, sndih_getSampleSizeAvailable__doc__},
481 {"getSampleRateAvailable", (PyCFunction)sndih_getSampleRateAvailable, METH_VARARGS, sndih_getSampleRateAvailable__doc__},
483 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
487 /* Initialization function for the module (*must* be called initSndihooks) */
489 static char Sndihooks_module_documentation[] =
493 void
494 initSndihooks()
496 PyObject *m, *d;
498 /* Create the module and add the functions */
499 m = Py_InitModule4("Sndihooks", sndih_methods,
500 Sndihooks_module_documentation,
501 (PyObject*)NULL,PYTHON_API_VERSION);
503 /* Add some symbolic constants to the module */
504 d = PyModule_GetDict(m);
505 ErrorObject = PyString_FromString("Sndihooks.error");
506 PyDict_SetItemString(d, "error", ErrorObject);
508 /* XXXX Add constants here */
510 /* Check for errors */
511 if (PyErr_Occurred())
512 Py_FatalError("can't initialize module Sndihooks");