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