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