Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame^] | 1 | /********************************************************** |
| 2 | Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The |
| 3 | 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 not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
| 25 | /* AL module -- interface to Mark Calows' Auido Library (AL). */ |
| 26 | |
| 27 | #include "audio.h" |
| 28 | |
| 29 | #include "allobjects.h" |
| 30 | #include "import.h" |
| 31 | #include "modsupport.h" |
| 32 | #include "structmember.h" |
| 33 | |
| 34 | |
| 35 | /* Config objects */ |
| 36 | |
| 37 | typedef struct { |
| 38 | OB_HEAD |
| 39 | ALconfig ob_config; |
| 40 | } configobject; |
| 41 | |
| 42 | extern typeobject Configtype; /* Forward */ |
| 43 | |
| 44 | #define is_configobject(v) ((v)->ob_type == &Configtype) |
| 45 | |
| 46 | static object * |
| 47 | setConfig (self, args, func) |
| 48 | configobject *self; |
| 49 | object *args; |
| 50 | void (*func)(ALconfig, long); |
| 51 | { |
| 52 | long par; |
| 53 | |
| 54 | if (!getlongarg(args, &par)) return NULL; |
| 55 | |
| 56 | (*func) (self-> ob_config, par); |
| 57 | |
| 58 | INCREF (None); |
| 59 | return None; |
| 60 | } |
| 61 | |
| 62 | static object * |
| 63 | getConfig (self, args, func) |
| 64 | configobject *self; |
| 65 | object *args; |
| 66 | long (*func)(ALconfig); |
| 67 | { |
| 68 | long par; |
| 69 | |
| 70 | if (!getnoarg(args)) return NULL; |
| 71 | |
| 72 | par = (*func) (self-> ob_config); |
| 73 | |
| 74 | return newintobject (par); |
| 75 | } |
| 76 | |
| 77 | static object * |
| 78 | al_setqueuesize (self, args) |
| 79 | configobject *self; |
| 80 | object *args; |
| 81 | { |
| 82 | return (setConfig (self, args, ALsetqueuesize)); |
| 83 | } |
| 84 | |
| 85 | static object * |
| 86 | al_getqueuesize (self, args) |
| 87 | configobject *self; |
| 88 | object *args; |
| 89 | { |
| 90 | return (getConfig (self, args, ALgetqueuesize)); |
| 91 | } |
| 92 | |
| 93 | static object * |
| 94 | al_setwidth (self, args) |
| 95 | configobject *self; |
| 96 | object *args; |
| 97 | { |
| 98 | return (setConfig (self, args, ALsetwidth)); |
| 99 | } |
| 100 | |
| 101 | static object * |
| 102 | al_getwidth (self, args) |
| 103 | configobject *self; |
| 104 | object *args; |
| 105 | { |
| 106 | return (getConfig (self, args, ALgetwidth)); |
| 107 | } |
| 108 | |
| 109 | static object * |
| 110 | al_getchannels (self, args) |
| 111 | configobject *self; |
| 112 | object *args; |
| 113 | { |
| 114 | return (getConfig (self, args, ALgetchannels)); |
| 115 | } |
| 116 | |
| 117 | static object * |
| 118 | al_setchannels (self, args) |
| 119 | configobject *self; |
| 120 | object *args; |
| 121 | { |
| 122 | return (setConfig (self, args, ALsetchannels)); |
| 123 | } |
| 124 | |
| 125 | static struct methodlist config_methods[] = { |
| 126 | {"getqueuesize", al_getqueuesize}, |
| 127 | {"setqueuesize", al_setqueuesize}, |
| 128 | {"getwidth", al_getwidth}, |
| 129 | {"setwidth", al_setwidth}, |
| 130 | {"getchannels", al_getchannels}, |
| 131 | {"setchannels", al_setchannels}, |
| 132 | {NULL, NULL} /* sentinel */ |
| 133 | }; |
| 134 | |
| 135 | static void |
| 136 | config_dealloc(self) |
| 137 | configobject *self; |
| 138 | { |
| 139 | ALfreeconfig(self->ob_config); |
| 140 | DEL(self); |
| 141 | } |
| 142 | |
| 143 | static object * |
| 144 | config_getattr(self, name) |
| 145 | configobject *self; |
| 146 | char *name; |
| 147 | { |
| 148 | return findmethod(config_methods, (object *)self, name); |
| 149 | } |
| 150 | |
| 151 | typeobject Configtype = { |
| 152 | OB_HEAD_INIT(&Typetype) |
| 153 | 0, /*ob_size*/ |
| 154 | "config", /*tp_name*/ |
| 155 | sizeof(configobject), /*tp_size*/ |
| 156 | 0, /*tp_itemsize*/ |
| 157 | /* methods */ |
| 158 | config_dealloc, /*tp_dealloc*/ |
| 159 | 0, /*tp_print*/ |
| 160 | config_getattr, /*tp_getattr*/ |
| 161 | 0, /*tp_setattr*/ |
| 162 | 0, /*tp_compare*/ |
| 163 | 0, /*tp_repr*/ |
| 164 | }; |
| 165 | |
| 166 | static object * |
| 167 | newconfigobject(config) |
| 168 | ALconfig config; |
| 169 | { |
| 170 | configobject *p; |
| 171 | |
| 172 | p = NEWOBJ(configobject, &Configtype); |
| 173 | if (p == NULL) |
| 174 | return NULL; |
| 175 | p->ob_config = config; |
| 176 | return (object *)p; |
| 177 | } |
| 178 | |
| 179 | /* Port objects */ |
| 180 | |
| 181 | typedef struct { |
| 182 | OB_HEAD |
| 183 | ALport ob_port; |
| 184 | } portobject; |
| 185 | |
| 186 | extern typeobject Porttype; /* Forward */ |
| 187 | |
| 188 | #define is_portobject(v) ((v)->ob_type == &Porttype) |
| 189 | |
| 190 | static object * |
| 191 | al_closeport (self, args) |
| 192 | portobject *self; |
| 193 | object *args; |
| 194 | { |
| 195 | if (!getnoarg(args)) return NULL; |
| 196 | |
| 197 | if (self->ob_port != NULL) { |
| 198 | ALcloseport (self-> ob_port); |
| 199 | self->ob_port = NULL; |
| 200 | /* XXX Using a closed port may dump core! */ |
| 201 | } |
| 202 | |
| 203 | INCREF (None); |
| 204 | return None; |
| 205 | } |
| 206 | |
| 207 | static object * |
| 208 | al_getfd (self, args) |
| 209 | portobject *self; |
| 210 | object *args; |
| 211 | { |
| 212 | int fd; |
| 213 | |
| 214 | if (!getnoarg(args)) return NULL; |
| 215 | |
| 216 | fd = ALgetfd (self-> ob_port); |
| 217 | |
| 218 | return newintobject (fd); |
| 219 | } |
| 220 | |
| 221 | static object * |
| 222 | al_getfilled (self, args) |
| 223 | portobject *self; |
| 224 | object *args; |
| 225 | { |
| 226 | long count; |
| 227 | |
| 228 | if (!getnoarg(args)) return NULL; |
| 229 | |
| 230 | count = ALgetfilled (self-> ob_port); |
| 231 | |
| 232 | return newintobject (count); |
| 233 | } |
| 234 | |
| 235 | static object * |
| 236 | al_getfillable (self, args) |
| 237 | portobject *self; |
| 238 | object *args; |
| 239 | { |
| 240 | long count; |
| 241 | |
| 242 | if (!getnoarg(args)) return NULL; |
| 243 | |
| 244 | count = ALgetfillable (self-> ob_port); |
| 245 | |
| 246 | return newintobject (count); |
| 247 | } |
| 248 | |
| 249 | static object * |
| 250 | al_readsamps (self, args) |
| 251 | portobject *self; |
| 252 | object *args; |
| 253 | { |
| 254 | long count; |
| 255 | object *v; |
| 256 | int width; |
| 257 | |
| 258 | if (!getlongarg (args, &count)) return NULL; |
| 259 | |
| 260 | if (count <= 0) |
| 261 | { |
| 262 | err_setstr (RuntimeError, "al.readsamps : arg <= 0"); |
| 263 | return NULL; |
| 264 | } |
| 265 | |
| 266 | width = ALgetwidth(ALgetconfig(self->ob_port)); |
| 267 | v = newsizedstringobject ((char *)NULL, width * count); |
| 268 | if (v == NULL) return NULL; |
| 269 | |
| 270 | ALreadsamps (self-> ob_port, (void *) getstringvalue(v), count); |
| 271 | |
| 272 | return (v); |
| 273 | } |
| 274 | |
| 275 | static object * |
| 276 | al_writesamps (self, args) |
| 277 | portobject *self; |
| 278 | object *args; |
| 279 | { |
| 280 | long count; |
| 281 | object *v; |
| 282 | int width; |
| 283 | |
| 284 | if (!getstrarg (args, &v)) return NULL; |
| 285 | |
| 286 | width = ALgetwidth(ALgetconfig(self->ob_port)); |
| 287 | ALwritesamps (self-> ob_port, (void *) getstringvalue(v), |
| 288 | getstringsize(v) / width); |
| 289 | |
| 290 | INCREF (None); |
| 291 | return None; |
| 292 | } |
| 293 | |
| 294 | static object * |
| 295 | al_getfillpoint (self, args) |
| 296 | portobject *self; |
| 297 | object *args; |
| 298 | { |
| 299 | long count; |
| 300 | |
| 301 | if (!getnoarg(args)) return NULL; |
| 302 | |
| 303 | count = ALgetfillpoint (self-> ob_port); |
| 304 | |
| 305 | return newintobject (count); |
| 306 | } |
| 307 | |
| 308 | static object * |
| 309 | al_setfillpoint (self, args) |
| 310 | portobject *self; |
| 311 | object *args; |
| 312 | { |
| 313 | long count; |
| 314 | |
| 315 | if (!getlongarg(args, &count)) return NULL; |
| 316 | |
| 317 | ALsetfillpoint (self-> ob_port, count); |
| 318 | |
| 319 | INCREF (None); |
| 320 | return (None); |
| 321 | } |
| 322 | |
| 323 | static object * |
| 324 | al_setconfig (self, args) |
| 325 | portobject *self; |
| 326 | object *args; |
| 327 | { |
| 328 | ALconfig config; |
| 329 | |
| 330 | if (!getconfigarg(args, &config)) return NULL; |
| 331 | |
| 332 | ALsetconfig (self-> ob_port, config); |
| 333 | |
| 334 | INCREF (None); |
| 335 | return (None); |
| 336 | } |
| 337 | |
| 338 | static object * |
| 339 | al_getconfig (self, args) |
| 340 | portobject *self; |
| 341 | object *args; |
| 342 | { |
| 343 | ALconfig config; |
| 344 | |
| 345 | if (!getnoarg(args)) return NULL; |
| 346 | |
| 347 | config = ALgetconfig (self-> ob_port); |
| 348 | |
| 349 | return newconfigobject (config); |
| 350 | } |
| 351 | |
| 352 | static struct methodlist port_methods[] = { |
| 353 | {"closeport", al_closeport}, |
| 354 | {"getfd", al_getfd}, |
| 355 | {"getfilled", al_getfilled}, |
| 356 | {"getfillable", al_getfillable}, |
| 357 | {"readsamps", al_readsamps}, |
| 358 | {"writesamps", al_writesamps}, |
| 359 | {"setfillpoint", al_setfillpoint}, |
| 360 | {"getfillpoint", al_getfillpoint}, |
| 361 | {"setconfig", al_setconfig}, |
| 362 | {"getconfig", al_getconfig}, |
| 363 | {NULL, NULL} /* sentinel */ |
| 364 | }; |
| 365 | |
| 366 | static void |
| 367 | port_dealloc(p) |
| 368 | portobject *p; |
| 369 | { |
| 370 | if (p->ob_port != NULL) |
| 371 | ALcloseport(p->ob_port); |
| 372 | DEL(p); |
| 373 | } |
| 374 | |
| 375 | static object * |
| 376 | port_getattr(p, name) |
| 377 | portobject *p; |
| 378 | char *name; |
| 379 | { |
| 380 | return findmethod(port_methods, (object *)p, name); |
| 381 | } |
| 382 | |
| 383 | typeobject Porttype = { |
| 384 | OB_HEAD_INIT(&Typetype) |
| 385 | 0, /*ob_size*/ |
| 386 | "port", /*tp_name*/ |
| 387 | sizeof(portobject), /*tp_size*/ |
| 388 | 0, /*tp_itemsize*/ |
| 389 | /* methods */ |
| 390 | port_dealloc, /*tp_dealloc*/ |
| 391 | 0, /*tp_print*/ |
| 392 | port_getattr, /*tp_getattr*/ |
| 393 | 0, /*tp_setattr*/ |
| 394 | 0, /*tp_compare*/ |
| 395 | 0, /*tp_repr*/ |
| 396 | }; |
| 397 | |
| 398 | static object * |
| 399 | newportobject(port) |
| 400 | ALport port; |
| 401 | { |
| 402 | portobject *p; |
| 403 | |
| 404 | p = NEWOBJ(portobject, &Porttype); |
| 405 | if (p == NULL) |
| 406 | return NULL; |
| 407 | p->ob_port = port; |
| 408 | return (object *)p; |
| 409 | } |
| 410 | |
| 411 | /* the module al */ |
| 412 | |
| 413 | static object * |
| 414 | al_openport (self, args) |
| 415 | object *self, *args; |
| 416 | { |
| 417 | object *name, *dir; |
| 418 | ALport port; |
| 419 | ALconfig config = NULL; |
| 420 | int size = gettuplesize(args); |
| 421 | |
| 422 | if (size == 2) { |
| 423 | if (!getstrstrarg (args, &name, &dir)) |
| 424 | return NULL; |
| 425 | } |
| 426 | else if (size == 3) { |
| 427 | if (!getstrstrconfigarg (args, &name, &dir, &config)) |
| 428 | return NULL; |
| 429 | } |
| 430 | else { |
| 431 | err_badarg(); |
| 432 | return NULL; |
| 433 | } |
| 434 | |
| 435 | port = ALopenport(getstringvalue(name), getstringvalue(dir), config); |
| 436 | |
| 437 | return newportobject (port); |
| 438 | } |
| 439 | |
| 440 | static object * |
| 441 | al_newconfig (self, args) |
| 442 | object *self, *args; |
| 443 | { |
| 444 | ALconfig config; |
| 445 | |
| 446 | if (!getnoarg (args)) return NULL; |
| 447 | |
| 448 | config = ALnewconfig (); |
| 449 | |
| 450 | return newconfigobject (config); |
| 451 | } |
| 452 | |
| 453 | static struct methodlist al_methods[] = { |
| 454 | {"openport", al_openport}, |
| 455 | {"newconfig", al_newconfig}, |
| 456 | {NULL, NULL} /* sentinel */ |
| 457 | }; |
| 458 | |
| 459 | void |
| 460 | inital() |
| 461 | { |
| 462 | initmodule("al", al_methods); |
| 463 | } |
| 464 | |
| 465 | int |
| 466 | getconfigarg (o, conf) |
| 467 | configobject *o; |
| 468 | ALconfig *conf; |
| 469 | { |
| 470 | if (o == NULL || !is_configobject(o)) |
| 471 | return err_badarg (); |
| 472 | |
| 473 | *conf = o-> ob_config; |
| 474 | |
| 475 | return 1; |
| 476 | } |
| 477 | |
| 478 | int |
| 479 | getstrstrconfigarg(v, a, b, c) |
| 480 | object *v; |
| 481 | object **a; |
| 482 | object **b; |
| 483 | ALconfig *c; |
| 484 | { |
| 485 | if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 3) { |
| 486 | return err_badarg(); |
| 487 | } |
| 488 | |
| 489 | return getstrarg(gettupleitem(v, 0), a) && |
| 490 | getstrarg(gettupleitem(v, 1), b) && |
| 491 | getconfigarg (gettupleitem (v, 2), c); |
| 492 | } |