Add forgotten initialization. Fixes bug #120994, "Traceback with
[python/dscho.git] / Mac / Modules / snd / Sndihooks.c
bloba4aa609d03d239195b52c4fc45061e67c4e3a943
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 "pymactoolbox.h"
35 #include <Sound.h>
37 #pragma options align=mac68k
38 struct SampleRateAvailable_arg {
39 short numrates;
40 Handle rates;
43 struct SampleSizeAvailable_arg {
44 short numsizes;
45 Handle sizes;
48 #pragma options align=reset
50 static PyObject *ErrorObject;
53 /* Convert Python object to unsigned Fixed */
54 static int
55 PyMac_GetUFixed(PyObject *v, Fixed *f)
57 double d;
58 unsigned long uns;
60 if( !PyArg_Parse(v, "d", &d))
61 return 0;
62 uns = (unsigned long)(d * 0x10000);
63 *f = (Fixed)uns;
64 return 1;
67 /* Convert a Point to a Python object */
68 static PyObject *
69 PyMac_BuildUFixed(Fixed f)
71 double d;
72 unsigned long funs;
74 funs = (unsigned long)f;
76 d = funs;
77 d = d / 0x10000;
78 return Py_BuildValue("d", d);
82 /* ----------------------------------------------------- */
84 static char sndih_getChannelAvailable__doc__[] =
88 static PyObject *
89 sndih_getChannelAvailable(self, args)
90 PyObject *self; /* Not used */
91 PyObject *args;
93 long inRefNum;
94 short nchannel;
95 OSErr err;
97 if (!PyArg_ParseTuple(args, "l", &inRefNum))
98 return NULL;
100 if( (err=SPBGetDeviceInfo(inRefNum, siChannelAvailable, (Ptr)&nchannel)) != noErr )
101 return PyMac_Error(err);
102 return Py_BuildValue("h", nchannel);
105 static char sndih_getNumberChannels__doc__[] =
109 static PyObject *
110 sndih_getNumberChannels(self, args)
111 PyObject *self; /* Not used */
112 PyObject *args;
114 long inRefNum;
115 short nchannel;
116 OSErr err;
118 if (!PyArg_ParseTuple(args, "l", &inRefNum))
119 return NULL;
121 if( (err=SPBGetDeviceInfo(inRefNum, siNumberChannels, (Ptr)&nchannel)) != noErr )
122 return PyMac_Error(err);
123 return Py_BuildValue("h", nchannel);
126 static char sndih_setNumberChannels__doc__[] =
130 static PyObject *
131 sndih_setNumberChannels(self, args)
132 PyObject *self; /* Not used */
133 PyObject *args;
135 long inRefNum;
136 short nchannel;
137 OSErr err;
139 if (!PyArg_ParseTuple(args, "lh", &inRefNum, &nchannel))
140 return NULL;
142 if( (err=SPBSetDeviceInfo(inRefNum, siNumberChannels, (Ptr)&nchannel)) != noErr )
143 return PyMac_Error(err);
144 Py_INCREF(Py_None);
145 return Py_None;
148 static char sndih_getContinuous__doc__[] =
152 static PyObject *
153 sndih_getContinuous(self, args)
154 PyObject *self; /* Not used */
155 PyObject *args;
157 long inRefNum;
158 short onoff;
159 OSErr err;
161 if (!PyArg_ParseTuple(args, "l", &inRefNum))
162 return NULL;
164 if( (err=SPBGetDeviceInfo(inRefNum, siContinuous, (Ptr)&onoff)) != noErr )
165 return PyMac_Error(err);
166 return Py_BuildValue("h", onoff);
169 static char sndih_setContinuous__doc__[] =
173 static PyObject *
174 sndih_setContinuous(self, args)
175 PyObject *self; /* Not used */
176 PyObject *args;
178 long inRefNum;
179 short onoff;
180 OSErr err;
182 if (!PyArg_ParseTuple(args, "lh", &inRefNum, &onoff))
183 return NULL;
185 if( (err=SPBSetDeviceInfo(inRefNum, siContinuous, (Ptr)&onoff)) != noErr )
186 return PyMac_Error(err);
187 Py_INCREF(Py_None);
188 return Py_None;
191 static char sndih_getInputSourceNames__doc__[] =
195 static PyObject *
196 sndih_getInputSourceNames(self, args)
197 PyObject *self; /* Not used */
198 PyObject *args;
200 long inRefNum;
201 Handle names;
202 OSErr err;
204 if (!PyArg_ParseTuple(args, "l", &inRefNum))
205 return NULL;
207 if( (err=SPBGetDeviceInfo(inRefNum, siInputSourceNames, (Ptr)&names)) != noErr )
208 return PyMac_Error(err);
209 return Py_BuildValue("O&", ResObj_New, names);
212 static char sndih_getInputSource__doc__[] =
216 static PyObject *
217 sndih_getInputSource(self, args)
218 PyObject *self; /* Not used */
219 PyObject *args;
221 long inRefNum;
222 short source;
223 OSErr err;
225 if (!PyArg_ParseTuple(args, "l", &inRefNum))
226 return NULL;
228 if( (err=SPBGetDeviceInfo(inRefNum, siInputSource, (Ptr)&source)) != noErr )
229 return PyMac_Error(err);
230 return Py_BuildValue("h", source);
233 static char sndih_setInputSource__doc__[] =
237 static PyObject *
238 sndih_setInputSource(self, args)
239 PyObject *self; /* Not used */
240 PyObject *args;
242 long inRefNum;
243 short source;
244 OSErr err;
246 if (!PyArg_ParseTuple(args, "lh", &inRefNum, &source))
247 return NULL;
249 if( (err=SPBSetDeviceInfo(inRefNum, siInputSource, (Ptr)&source)) != noErr )
250 return PyMac_Error(err);
251 Py_INCREF(Py_None);
252 return Py_None;
255 static char sndih_getPlayThruOnOff__doc__[] =
259 static PyObject *
260 sndih_getPlayThruOnOff(self, args)
261 PyObject *self; /* Not used */
262 PyObject *args;
264 long inRefNum;
265 short onoff;
266 OSErr err;
268 if (!PyArg_ParseTuple(args, "l", &inRefNum))
269 return NULL;
271 if( (err=SPBGetDeviceInfo(inRefNum, siPlayThruOnOff, (Ptr)&onoff)) != noErr )
272 return PyMac_Error(err);
273 return Py_BuildValue("h", onoff);
276 static char sndih_setPlayThruOnOff__doc__[] =
280 static PyObject *
281 sndih_setPlayThruOnOff(self, args)
282 PyObject *self; /* Not used */
283 PyObject *args;
285 long inRefNum;
286 short onoff;
287 OSErr err;
289 if (!PyArg_ParseTuple(args, "lh", &inRefNum, &onoff))
290 return NULL;
292 if( (err=SPBSetDeviceInfo(inRefNum, siPlayThruOnOff, (Ptr)&onoff)) != noErr )
293 return PyMac_Error(err);
294 Py_INCREF(Py_None);
295 return Py_None;
298 static char sndih_getSampleRate__doc__[] =
302 static PyObject *
303 sndih_getSampleRate(self, args)
304 PyObject *self; /* Not used */
305 PyObject *args;
307 long inRefNum;
308 Fixed sample_rate;
309 OSErr err;
311 if (!PyArg_ParseTuple(args, "l", &inRefNum))
312 return NULL;
314 if( (err=SPBGetDeviceInfo(inRefNum, siSampleRate, (Ptr)&sample_rate)) != noErr )
315 return PyMac_Error(err);
316 return Py_BuildValue("O&", PyMac_BuildUFixed, sample_rate);
319 static char sndih_setSampleRate__doc__[] =
323 static PyObject *
324 sndih_setSampleRate(self, args)
325 PyObject *self; /* Not used */
326 PyObject *args;
328 long inRefNum;
329 Fixed sample_rate;
330 OSErr err;
332 if (!PyArg_ParseTuple(args, "lO&", &inRefNum, PyMac_GetUFixed, &sample_rate))
333 return NULL;
335 if( (err=SPBSetDeviceInfo(inRefNum, siSampleRate, (Ptr)&sample_rate)) != noErr )
336 return PyMac_Error(err);
337 Py_INCREF(Py_None);
338 return Py_None;
341 static char sndih_getSampleSize__doc__[] =
345 static PyObject *
346 sndih_getSampleSize(self, args)
347 PyObject *self; /* Not used */
348 PyObject *args;
350 long inRefNum;
351 short bits;
352 OSErr err;
354 if (!PyArg_ParseTuple(args, "l", &inRefNum))
355 return NULL;
357 if( (err=SPBGetDeviceInfo(inRefNum, siSampleSize, (Ptr)&bits)) != noErr )
358 return PyMac_Error(err);
359 return Py_BuildValue("h", bits);
362 static char sndih_setSampleSize__doc__[] =
366 static PyObject *
367 sndih_setSampleSize(self, args)
368 PyObject *self; /* Not used */
369 PyObject *args;
371 long inRefNum;
372 short size;
373 OSErr err;
375 if (!PyArg_ParseTuple(args, "lh", &inRefNum, &size))
376 return NULL;
378 if( (err=SPBSetDeviceInfo(inRefNum, siSampleSize, (Ptr)&size)) != noErr )
379 return PyMac_Error(err);
380 Py_INCREF(Py_None);
381 return Py_None;
384 static char sndih_getSampleSizeAvailable__doc__[] =
388 static PyObject *
389 sndih_getSampleSizeAvailable(self, args)
390 PyObject *self; /* Not used */
391 PyObject *args;
393 long inRefNum;
394 struct SampleSizeAvailable_arg arg;
395 OSErr err;
396 PyObject *rsizes;
397 short *fsizes;
398 int i;
400 arg.sizes = NULL;
401 rsizes = NULL;
402 if (!PyArg_ParseTuple(args, "l", &inRefNum))
403 return NULL;
405 if( (err=SPBGetDeviceInfo(inRefNum, siSampleSizeAvailable, (Ptr)&arg)) != noErr ) {
406 return PyMac_Error(err);
408 fsizes = (short *)*(arg.sizes);
409 /* Handle contains a list of rates */
410 if( (rsizes = PyTuple_New(arg.numsizes)) == NULL)
411 return NULL;
412 for( i=0; i<arg.numsizes; i++ )
413 PyTuple_SetItem(rsizes, i, PyInt_FromLong((long)fsizes[i]));
414 return rsizes;
417 static char sndih_getSampleRateAvailable__doc__[] =
421 static PyObject *
422 sndih_getSampleRateAvailable(self, args)
423 PyObject *self; /* Not used */
424 PyObject *args;
426 long inRefNum;
427 struct SampleRateAvailable_arg arg;
428 OSErr err;
429 PyObject *rrates, *obj;
430 Fixed *frates;
431 int i;
433 arg.rates = NULL;
434 rrates = NULL;
435 if (!PyArg_ParseTuple(args, "l", &inRefNum))
436 return NULL;
438 if( (err=SPBGetDeviceInfo(inRefNum, siSampleRateAvailable, (Ptr)&arg)) != noErr ) {
439 return PyMac_Error(err);
441 frates = (Fixed *)*(arg.rates);
442 if( arg.numrates == 0 ) {
443 /* The handle contains upper and lowerbound */
444 rrates = Py_BuildValue("O&O&", frates[0], frates[1]);
445 if (rrates == NULL) return NULL;
446 } else {
447 /* Handle contains a list of rates */
448 if( (rrates = PyTuple_New(arg.numrates)) == NULL)
449 return NULL;
450 for( i=0; i<arg.numrates; i++ ) {
451 if( (obj = Py_BuildValue("O&", PyMac_BuildUFixed, frates[i]))==NULL)
452 goto out;
453 PyTuple_SetItem(rrates, i, obj);
456 return Py_BuildValue("hO", arg.numrates, rrates);
457 out:
458 Py_XDECREF(rrates);
459 return NULL;
462 /* List of methods defined in the module */
464 static struct PyMethodDef sndih_methods[] = {
465 {"getChannelAvailable", (PyCFunction)sndih_getChannelAvailable, METH_VARARGS, sndih_getChannelAvailable__doc__},
466 {"getNumberChannels", (PyCFunction)sndih_getNumberChannels, METH_VARARGS, sndih_getNumberChannels__doc__},
467 {"setNumberChannels", (PyCFunction)sndih_setNumberChannels, METH_VARARGS, sndih_setNumberChannels__doc__},
468 {"getContinuous", (PyCFunction)sndih_getContinuous, METH_VARARGS, sndih_getContinuous__doc__},
469 {"setContinuous", (PyCFunction)sndih_setContinuous, METH_VARARGS, sndih_setContinuous__doc__},
470 {"getInputSourceNames", (PyCFunction)sndih_getInputSourceNames, METH_VARARGS, sndih_getInputSourceNames__doc__},
471 {"getInputSource", (PyCFunction)sndih_getInputSource, METH_VARARGS, sndih_getInputSource__doc__},
472 {"setInputSource", (PyCFunction)sndih_setInputSource, METH_VARARGS, sndih_setInputSource__doc__},
473 {"getPlayThruOnOff", (PyCFunction)sndih_getPlayThruOnOff, METH_VARARGS, sndih_getPlayThruOnOff__doc__},
474 {"setPlayThruOnOff", (PyCFunction)sndih_setPlayThruOnOff, METH_VARARGS, sndih_setPlayThruOnOff__doc__},
475 {"getSampleRate", (PyCFunction)sndih_getSampleRate, METH_VARARGS, sndih_getSampleRate__doc__},
476 {"setSampleRate", (PyCFunction)sndih_setSampleRate, METH_VARARGS, sndih_setSampleRate__doc__},
477 {"getSampleSize", (PyCFunction)sndih_getSampleSize, METH_VARARGS, sndih_getSampleSize__doc__},
478 {"setSampleSize", (PyCFunction)sndih_setSampleSize, METH_VARARGS, sndih_setSampleSize__doc__},
479 {"getSampleSizeAvailable", (PyCFunction)sndih_getSampleSizeAvailable, METH_VARARGS, sndih_getSampleSizeAvailable__doc__},
480 {"getSampleRateAvailable", (PyCFunction)sndih_getSampleRateAvailable, METH_VARARGS, sndih_getSampleRateAvailable__doc__},
482 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
486 /* Initialization function for the module (*must* be called initSndihooks) */
488 static char Sndihooks_module_documentation[] =
492 void
493 initSndihooks()
495 PyObject *m, *d;
497 /* Create the module and add the functions */
498 m = Py_InitModule4("Sndihooks", sndih_methods,
499 Sndihooks_module_documentation,
500 (PyObject*)NULL,PYTHON_API_VERSION);
502 /* Add some symbolic constants to the module */
503 d = PyModule_GetDict(m);
504 ErrorObject = PyString_FromString("Sndihooks.error");
505 PyDict_SetItemString(d, "error", ErrorObject);
507 /* XXXX Add constants here */
509 /* Check for errors */
510 if (PyErr_Occurred())
511 Py_FatalError("can't initialize module Sndihooks");