Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 1 | /********************************************************** |
Guido van Rossum | 524b588 | 1995-01-04 19:10:35 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 5773798 | 1992-05-15 11:06:29 +0000 | [diff] [blame] | 25 | /* AL module -- interface to Mark Callow's Audio Library (AL). */ |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 26 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 27 | #include <audio.h> |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 28 | |
Jack Jansen | e8a3c28 | 1993-02-10 14:10:56 +0000 | [diff] [blame] | 29 | /* Check which version audio library we have: */ |
| 30 | #ifdef AL_ERROR_NUMBER |
| 31 | #define AL_405 |
| 32 | /* XXXX 4.0.5 libaudio also allows us to provide better error |
| 33 | ** handling (with ALseterrorhandler). We should implement that |
| 34 | ** sometime. |
| 35 | */ |
| 36 | |
| 37 | #endif |
| 38 | |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 39 | #include "allobjects.h" |
| 40 | #include "import.h" |
| 41 | #include "modsupport.h" |
| 42 | #include "structmember.h" |
Jack Jansen | 743db36 | 1992-08-13 14:13:11 +0000 | [diff] [blame] | 43 | #include "ceval.h" |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 44 | |
| 45 | |
| 46 | /* Config objects */ |
| 47 | |
| 48 | typedef struct { |
| 49 | OB_HEAD |
| 50 | ALconfig ob_config; |
| 51 | } configobject; |
| 52 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 53 | staticforward typeobject Configtype; |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 54 | |
| 55 | #define is_configobject(v) ((v)->ob_type == &Configtype) |
| 56 | |
Guido van Rossum | fc58e58 | 1992-01-27 16:45:55 +0000 | [diff] [blame] | 57 | /* Forward */ |
| 58 | static int getconfigarg PROTO((object *, ALconfig *)); |
| 59 | static int getstrstrconfigarg PROTO((object *, char **, char **, ALconfig *)); |
| 60 | |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 61 | static object * |
| 62 | setConfig (self, args, func) |
| 63 | configobject *self; |
| 64 | object *args; |
| 65 | void (*func)(ALconfig, long); |
| 66 | { |
| 67 | long par; |
| 68 | |
Guido van Rossum | fc58e58 | 1992-01-27 16:45:55 +0000 | [diff] [blame] | 69 | if (!getlongarg (args, &par)) return NULL; |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 70 | |
| 71 | (*func) (self-> ob_config, par); |
| 72 | |
| 73 | INCREF (None); |
| 74 | return None; |
| 75 | } |
| 76 | |
| 77 | static object * |
| 78 | getConfig (self, args, func) |
| 79 | configobject *self; |
| 80 | object *args; |
| 81 | long (*func)(ALconfig); |
| 82 | { |
| 83 | long par; |
| 84 | |
Guido van Rossum | fc58e58 | 1992-01-27 16:45:55 +0000 | [diff] [blame] | 85 | if (!getnoarg (args)) return NULL; |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 86 | |
| 87 | par = (*func) (self-> ob_config); |
| 88 | |
| 89 | return newintobject (par); |
| 90 | } |
| 91 | |
| 92 | static object * |
| 93 | al_setqueuesize (self, args) |
| 94 | configobject *self; |
| 95 | object *args; |
| 96 | { |
| 97 | return (setConfig (self, args, ALsetqueuesize)); |
| 98 | } |
| 99 | |
| 100 | static object * |
| 101 | al_getqueuesize (self, args) |
| 102 | configobject *self; |
| 103 | object *args; |
| 104 | { |
| 105 | return (getConfig (self, args, ALgetqueuesize)); |
| 106 | } |
| 107 | |
| 108 | static object * |
| 109 | al_setwidth (self, args) |
| 110 | configobject *self; |
| 111 | object *args; |
| 112 | { |
| 113 | return (setConfig (self, args, ALsetwidth)); |
| 114 | } |
| 115 | |
| 116 | static object * |
| 117 | al_getwidth (self, args) |
| 118 | configobject *self; |
| 119 | object *args; |
| 120 | { |
| 121 | return (getConfig (self, args, ALgetwidth)); |
| 122 | } |
| 123 | |
| 124 | static object * |
| 125 | al_getchannels (self, args) |
| 126 | configobject *self; |
| 127 | object *args; |
| 128 | { |
| 129 | return (getConfig (self, args, ALgetchannels)); |
| 130 | } |
| 131 | |
| 132 | static object * |
| 133 | al_setchannels (self, args) |
| 134 | configobject *self; |
| 135 | object *args; |
| 136 | { |
| 137 | return (setConfig (self, args, ALsetchannels)); |
| 138 | } |
| 139 | |
Jack Jansen | e8a3c28 | 1993-02-10 14:10:56 +0000 | [diff] [blame] | 140 | #ifdef AL_405 |
| 141 | |
| 142 | static object * |
| 143 | al_getsampfmt (self, args) |
| 144 | configobject *self; |
| 145 | object *args; |
| 146 | { |
| 147 | return (getConfig (self, args, ALgetsampfmt)); |
| 148 | } |
| 149 | |
| 150 | static object * |
| 151 | al_setsampfmt (self, args) |
| 152 | configobject *self; |
| 153 | object *args; |
| 154 | { |
| 155 | return (setConfig (self, args, ALsetsampfmt)); |
| 156 | } |
| 157 | |
| 158 | static object * |
| 159 | al_getfloatmax(self, args) |
| 160 | configobject *self; |
| 161 | object *args; |
| 162 | { |
| 163 | double arg; |
| 164 | |
| 165 | if ( !getnoarg(args) ) |
| 166 | return 0; |
| 167 | arg = ALgetfloatmax(self->ob_config); |
| 168 | return newfloatobject(arg); |
| 169 | } |
| 170 | |
| 171 | static object * |
| 172 | al_setfloatmax(self, args) |
| 173 | configobject *self; |
| 174 | object *args; |
| 175 | { |
| 176 | double arg; |
| 177 | |
| 178 | if ( !getargs(args, "d", &arg) ) |
| 179 | return 0; |
| 180 | ALsetfloatmax(self->ob_config, arg); |
| 181 | INCREF(None); |
| 182 | return None; |
| 183 | } |
| 184 | #endif /* AL_405 */ |
| 185 | |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 186 | static struct methodlist config_methods[] = { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 187 | {"getqueuesize", (method)al_getqueuesize}, |
| 188 | {"setqueuesize", (method)al_setqueuesize}, |
| 189 | {"getwidth", (method)al_getwidth}, |
| 190 | {"setwidth", (method)al_setwidth}, |
| 191 | {"getchannels", (method)al_getchannels}, |
| 192 | {"setchannels", (method)al_setchannels}, |
Jack Jansen | e8a3c28 | 1993-02-10 14:10:56 +0000 | [diff] [blame] | 193 | #ifdef AL_405 |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 194 | {"getsampfmt", (method)al_getsampfmt}, |
| 195 | {"setsampfmt", (method)al_setsampfmt}, |
| 196 | {"getfloatmax", (method)al_getfloatmax}, |
| 197 | {"setfloatmax", (method)al_setfloatmax}, |
Jack Jansen | e8a3c28 | 1993-02-10 14:10:56 +0000 | [diff] [blame] | 198 | #endif /* AL_405 */ |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 199 | {NULL, NULL} /* sentinel */ |
| 200 | }; |
| 201 | |
| 202 | static void |
| 203 | config_dealloc(self) |
| 204 | configobject *self; |
| 205 | { |
| 206 | ALfreeconfig(self->ob_config); |
| 207 | DEL(self); |
| 208 | } |
| 209 | |
| 210 | static object * |
| 211 | config_getattr(self, name) |
| 212 | configobject *self; |
| 213 | char *name; |
| 214 | { |
| 215 | return findmethod(config_methods, (object *)self, name); |
| 216 | } |
| 217 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 218 | static typeobject Configtype = { |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 219 | OB_HEAD_INIT(&Typetype) |
| 220 | 0, /*ob_size*/ |
| 221 | "config", /*tp_name*/ |
| 222 | sizeof(configobject), /*tp_size*/ |
| 223 | 0, /*tp_itemsize*/ |
| 224 | /* methods */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 225 | (destructor)config_dealloc, /*tp_dealloc*/ |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 226 | 0, /*tp_print*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 227 | (getattrfunc)config_getattr, /*tp_getattr*/ |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 228 | 0, /*tp_setattr*/ |
| 229 | 0, /*tp_compare*/ |
| 230 | 0, /*tp_repr*/ |
| 231 | }; |
| 232 | |
| 233 | static object * |
| 234 | newconfigobject(config) |
| 235 | ALconfig config; |
| 236 | { |
| 237 | configobject *p; |
| 238 | |
| 239 | p = NEWOBJ(configobject, &Configtype); |
| 240 | if (p == NULL) |
| 241 | return NULL; |
| 242 | p->ob_config = config; |
| 243 | return (object *)p; |
| 244 | } |
| 245 | |
| 246 | /* Port objects */ |
| 247 | |
| 248 | typedef struct { |
| 249 | OB_HEAD |
| 250 | ALport ob_port; |
| 251 | } portobject; |
| 252 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 253 | staticforward typeobject Porttype; |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 254 | |
| 255 | #define is_portobject(v) ((v)->ob_type == &Porttype) |
| 256 | |
| 257 | static object * |
| 258 | al_closeport (self, args) |
| 259 | portobject *self; |
| 260 | object *args; |
| 261 | { |
Guido van Rossum | fc58e58 | 1992-01-27 16:45:55 +0000 | [diff] [blame] | 262 | if (!getnoarg (args)) return NULL; |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 263 | |
| 264 | if (self->ob_port != NULL) { |
| 265 | ALcloseport (self-> ob_port); |
| 266 | self->ob_port = NULL; |
| 267 | /* XXX Using a closed port may dump core! */ |
| 268 | } |
| 269 | |
| 270 | INCREF (None); |
| 271 | return None; |
| 272 | } |
| 273 | |
| 274 | static object * |
| 275 | al_getfd (self, args) |
| 276 | portobject *self; |
| 277 | object *args; |
| 278 | { |
| 279 | int fd; |
| 280 | |
Guido van Rossum | fc58e58 | 1992-01-27 16:45:55 +0000 | [diff] [blame] | 281 | if (!getnoarg (args)) return NULL; |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 282 | |
| 283 | fd = ALgetfd (self-> ob_port); |
| 284 | |
| 285 | return newintobject (fd); |
| 286 | } |
| 287 | |
| 288 | static object * |
| 289 | al_getfilled (self, args) |
| 290 | portobject *self; |
| 291 | object *args; |
| 292 | { |
| 293 | long count; |
| 294 | |
Guido van Rossum | fc58e58 | 1992-01-27 16:45:55 +0000 | [diff] [blame] | 295 | if (!getnoarg (args)) return NULL; |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 296 | |
| 297 | count = ALgetfilled (self-> ob_port); |
| 298 | |
| 299 | return newintobject (count); |
| 300 | } |
| 301 | |
| 302 | static object * |
| 303 | al_getfillable (self, args) |
| 304 | portobject *self; |
| 305 | object *args; |
| 306 | { |
| 307 | long count; |
| 308 | |
Guido van Rossum | fc58e58 | 1992-01-27 16:45:55 +0000 | [diff] [blame] | 309 | if (!getnoarg (args)) return NULL; |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 310 | |
| 311 | count = ALgetfillable (self-> ob_port); |
| 312 | |
| 313 | return newintobject (count); |
| 314 | } |
| 315 | |
| 316 | static object * |
| 317 | al_readsamps (self, args) |
| 318 | portobject *self; |
| 319 | object *args; |
| 320 | { |
| 321 | long count; |
| 322 | object *v; |
Guido van Rossum | b3a5b9b | 1991-09-10 14:54:05 +0000 | [diff] [blame] | 323 | ALconfig c; |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 324 | int width; |
| 325 | |
| 326 | if (!getlongarg (args, &count)) return NULL; |
| 327 | |
| 328 | if (count <= 0) |
| 329 | { |
| 330 | err_setstr (RuntimeError, "al.readsamps : arg <= 0"); |
| 331 | return NULL; |
| 332 | } |
| 333 | |
Guido van Rossum | b3a5b9b | 1991-09-10 14:54:05 +0000 | [diff] [blame] | 334 | c = ALgetconfig(self->ob_port); |
Jack Jansen | e8a3c28 | 1993-02-10 14:10:56 +0000 | [diff] [blame] | 335 | #ifdef AL_405 |
| 336 | width = ALgetsampfmt(c); |
| 337 | if ( width == AL_SAMPFMT_FLOAT ) |
| 338 | width = sizeof(float); |
| 339 | else if ( width == AL_SAMPFMT_DOUBLE ) |
| 340 | width = sizeof(double); |
| 341 | else |
| 342 | width = ALgetwidth(c); |
| 343 | #else |
Guido van Rossum | b3a5b9b | 1991-09-10 14:54:05 +0000 | [diff] [blame] | 344 | width = ALgetwidth(c); |
Jack Jansen | e8a3c28 | 1993-02-10 14:10:56 +0000 | [diff] [blame] | 345 | #endif /* AL_405 */ |
Guido van Rossum | b3a5b9b | 1991-09-10 14:54:05 +0000 | [diff] [blame] | 346 | ALfreeconfig(c); |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 347 | v = newsizedstringobject ((char *)NULL, width * count); |
| 348 | if (v == NULL) return NULL; |
| 349 | |
Jack Jansen | 743db36 | 1992-08-13 14:13:11 +0000 | [diff] [blame] | 350 | BGN_SAVE |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 351 | ALreadsamps (self-> ob_port, (void *) getstringvalue(v), count); |
Jack Jansen | 743db36 | 1992-08-13 14:13:11 +0000 | [diff] [blame] | 352 | END_SAVE |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 353 | |
| 354 | return (v); |
| 355 | } |
| 356 | |
| 357 | static object * |
| 358 | al_writesamps (self, args) |
| 359 | portobject *self; |
| 360 | object *args; |
| 361 | { |
| 362 | long count; |
Guido van Rossum | fc58e58 | 1992-01-27 16:45:55 +0000 | [diff] [blame] | 363 | char *buf; |
| 364 | int size, width; |
Guido van Rossum | b3a5b9b | 1991-09-10 14:54:05 +0000 | [diff] [blame] | 365 | ALconfig c; |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 366 | |
Guido van Rossum | fc58e58 | 1992-01-27 16:45:55 +0000 | [diff] [blame] | 367 | if (!getargs (args, "s#", &buf, &size)) return NULL; |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 368 | |
Guido van Rossum | b3a5b9b | 1991-09-10 14:54:05 +0000 | [diff] [blame] | 369 | c = ALgetconfig(self->ob_port); |
Jack Jansen | e8a3c28 | 1993-02-10 14:10:56 +0000 | [diff] [blame] | 370 | #ifdef AL_405 |
| 371 | width = ALgetsampfmt(c); |
| 372 | if ( width == AL_SAMPFMT_FLOAT ) |
| 373 | width = sizeof(float); |
| 374 | else if ( width == AL_SAMPFMT_DOUBLE ) |
| 375 | width = sizeof(double); |
| 376 | else |
| 377 | width = ALgetwidth(c); |
| 378 | #else |
Guido van Rossum | b3a5b9b | 1991-09-10 14:54:05 +0000 | [diff] [blame] | 379 | width = ALgetwidth(c); |
Jack Jansen | e8a3c28 | 1993-02-10 14:10:56 +0000 | [diff] [blame] | 380 | #endif /* AL_405 */ |
Guido van Rossum | b3a5b9b | 1991-09-10 14:54:05 +0000 | [diff] [blame] | 381 | ALfreeconfig(c); |
Jack Jansen | 743db36 | 1992-08-13 14:13:11 +0000 | [diff] [blame] | 382 | BGN_SAVE |
Guido van Rossum | fc58e58 | 1992-01-27 16:45:55 +0000 | [diff] [blame] | 383 | ALwritesamps (self-> ob_port, (void *) buf, (long) size / width); |
Jack Jansen | 743db36 | 1992-08-13 14:13:11 +0000 | [diff] [blame] | 384 | END_SAVE |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 385 | |
| 386 | INCREF (None); |
| 387 | return None; |
| 388 | } |
| 389 | |
| 390 | static object * |
| 391 | al_getfillpoint (self, args) |
| 392 | portobject *self; |
| 393 | object *args; |
| 394 | { |
| 395 | long count; |
| 396 | |
Guido van Rossum | fc58e58 | 1992-01-27 16:45:55 +0000 | [diff] [blame] | 397 | if (!getnoarg (args)) return NULL; |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 398 | |
| 399 | count = ALgetfillpoint (self-> ob_port); |
| 400 | |
| 401 | return newintobject (count); |
| 402 | } |
| 403 | |
| 404 | static object * |
| 405 | al_setfillpoint (self, args) |
| 406 | portobject *self; |
| 407 | object *args; |
| 408 | { |
| 409 | long count; |
| 410 | |
Guido van Rossum | fc58e58 | 1992-01-27 16:45:55 +0000 | [diff] [blame] | 411 | if (!getlongarg (args, &count)) return NULL; |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 412 | |
| 413 | ALsetfillpoint (self-> ob_port, count); |
| 414 | |
| 415 | INCREF (None); |
| 416 | return (None); |
| 417 | } |
| 418 | |
| 419 | static object * |
| 420 | al_setconfig (self, args) |
| 421 | portobject *self; |
| 422 | object *args; |
| 423 | { |
| 424 | ALconfig config; |
| 425 | |
Guido van Rossum | fc58e58 | 1992-01-27 16:45:55 +0000 | [diff] [blame] | 426 | if (!getconfigarg (args, &config)) return NULL; |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 427 | |
| 428 | ALsetconfig (self-> ob_port, config); |
| 429 | |
| 430 | INCREF (None); |
| 431 | return (None); |
| 432 | } |
| 433 | |
| 434 | static object * |
| 435 | al_getconfig (self, args) |
| 436 | portobject *self; |
| 437 | object *args; |
| 438 | { |
| 439 | ALconfig config; |
| 440 | |
Guido van Rossum | fc58e58 | 1992-01-27 16:45:55 +0000 | [diff] [blame] | 441 | if (!getnoarg (args)) return NULL; |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 442 | |
| 443 | config = ALgetconfig (self-> ob_port); |
| 444 | |
| 445 | return newconfigobject (config); |
| 446 | } |
| 447 | |
Jack Jansen | e8a3c28 | 1993-02-10 14:10:56 +0000 | [diff] [blame] | 448 | #ifdef AL_405 |
| 449 | static object * |
| 450 | al_getstatus (self, args) |
| 451 | portobject *self; |
| 452 | object *args; |
| 453 | { |
| 454 | object *list, *v; |
| 455 | long *PVbuffer; |
| 456 | long length; |
| 457 | int i; |
| 458 | |
| 459 | if (!getargs(args, "O", &list)) |
| 460 | return NULL; |
| 461 | if (!is_listobject(list)) { |
| 462 | err_badarg(); |
| 463 | return NULL; |
| 464 | } |
| 465 | length = getlistsize(list); |
| 466 | PVbuffer = NEW(long, length); |
| 467 | if (PVbuffer == NULL) |
| 468 | return err_nomem(); |
| 469 | for (i = 0; i < length; i++) { |
| 470 | v = getlistitem(list, i); |
| 471 | if (!is_intobject(v)) { |
| 472 | DEL(PVbuffer); |
| 473 | err_badarg(); |
| 474 | return NULL; |
| 475 | } |
| 476 | PVbuffer[i] = getintvalue(v); |
| 477 | } |
| 478 | |
| 479 | ALgetstatus(self->ob_port, PVbuffer, length); |
| 480 | |
| 481 | for (i = 0; i < length; i++) |
| 482 | setlistitem(list, i, newintobject(PVbuffer[i])); |
| 483 | |
| 484 | DEL(PVbuffer); |
| 485 | |
| 486 | INCREF(None); |
| 487 | return None; |
| 488 | } |
| 489 | #endif /* AL_405 */ |
| 490 | |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 491 | static struct methodlist port_methods[] = { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 492 | {"closeport", (method)al_closeport}, |
| 493 | {"getfd", (method)al_getfd}, |
| 494 | {"fileno", (method)al_getfd}, |
| 495 | {"getfilled", (method)al_getfilled}, |
| 496 | {"getfillable", (method)al_getfillable}, |
| 497 | {"readsamps", (method)al_readsamps}, |
| 498 | {"writesamps", (method)al_writesamps}, |
| 499 | {"setfillpoint", (method)al_setfillpoint}, |
| 500 | {"getfillpoint", (method)al_getfillpoint}, |
| 501 | {"setconfig", (method)al_setconfig}, |
| 502 | {"getconfig", (method)al_getconfig}, |
Jack Jansen | e8a3c28 | 1993-02-10 14:10:56 +0000 | [diff] [blame] | 503 | #ifdef AL_405 |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 504 | {"getstatus", (method)al_getstatus}, |
Jack Jansen | e8a3c28 | 1993-02-10 14:10:56 +0000 | [diff] [blame] | 505 | #endif /* AL_405 */ |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 506 | {NULL, NULL} /* sentinel */ |
| 507 | }; |
| 508 | |
| 509 | static void |
| 510 | port_dealloc(p) |
| 511 | portobject *p; |
| 512 | { |
| 513 | if (p->ob_port != NULL) |
| 514 | ALcloseport(p->ob_port); |
| 515 | DEL(p); |
| 516 | } |
| 517 | |
| 518 | static object * |
| 519 | port_getattr(p, name) |
| 520 | portobject *p; |
| 521 | char *name; |
| 522 | { |
| 523 | return findmethod(port_methods, (object *)p, name); |
| 524 | } |
| 525 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 526 | static typeobject Porttype = { |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 527 | OB_HEAD_INIT(&Typetype) |
| 528 | 0, /*ob_size*/ |
| 529 | "port", /*tp_name*/ |
| 530 | sizeof(portobject), /*tp_size*/ |
| 531 | 0, /*tp_itemsize*/ |
| 532 | /* methods */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 533 | (destructor)port_dealloc, /*tp_dealloc*/ |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 534 | 0, /*tp_print*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 535 | (getattrfunc)port_getattr, /*tp_getattr*/ |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 536 | 0, /*tp_setattr*/ |
| 537 | 0, /*tp_compare*/ |
| 538 | 0, /*tp_repr*/ |
| 539 | }; |
| 540 | |
| 541 | static object * |
| 542 | newportobject(port) |
| 543 | ALport port; |
| 544 | { |
| 545 | portobject *p; |
| 546 | |
| 547 | p = NEWOBJ(portobject, &Porttype); |
| 548 | if (p == NULL) |
| 549 | return NULL; |
| 550 | p->ob_port = port; |
| 551 | return (object *)p; |
| 552 | } |
| 553 | |
| 554 | /* the module al */ |
| 555 | |
| 556 | static object * |
| 557 | al_openport (self, args) |
| 558 | object *self, *args; |
| 559 | { |
Guido van Rossum | fc58e58 | 1992-01-27 16:45:55 +0000 | [diff] [blame] | 560 | char *name, *dir; |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 561 | ALport port; |
| 562 | ALconfig config = NULL; |
Guido van Rossum | c0aab89 | 1991-10-20 20:10:46 +0000 | [diff] [blame] | 563 | int size; |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 564 | |
Guido van Rossum | c0aab89 | 1991-10-20 20:10:46 +0000 | [diff] [blame] | 565 | if (args == NULL || !is_tupleobject(args)) { |
Guido van Rossum | fc58e58 | 1992-01-27 16:45:55 +0000 | [diff] [blame] | 566 | err_badarg (); |
Guido van Rossum | c0aab89 | 1991-10-20 20:10:46 +0000 | [diff] [blame] | 567 | return NULL; |
| 568 | } |
| 569 | size = gettuplesize(args); |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 570 | if (size == 2) { |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 571 | if (!getargs (args, "(ss)", &name, &dir)) |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 572 | return NULL; |
| 573 | } |
| 574 | else if (size == 3) { |
| 575 | if (!getstrstrconfigarg (args, &name, &dir, &config)) |
| 576 | return NULL; |
| 577 | } |
| 578 | else { |
Guido van Rossum | fc58e58 | 1992-01-27 16:45:55 +0000 | [diff] [blame] | 579 | err_badarg (); |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 580 | return NULL; |
| 581 | } |
| 582 | |
Guido van Rossum | fc58e58 | 1992-01-27 16:45:55 +0000 | [diff] [blame] | 583 | port = ALopenport(name, dir, config); |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 584 | |
Guido van Rossum | c0aab89 | 1991-10-20 20:10:46 +0000 | [diff] [blame] | 585 | if (port == NULL) { |
| 586 | err_errno(RuntimeError); |
| 587 | return NULL; |
| 588 | } |
| 589 | |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 590 | return newportobject (port); |
| 591 | } |
| 592 | |
| 593 | static object * |
| 594 | al_newconfig (self, args) |
| 595 | object *self, *args; |
| 596 | { |
| 597 | ALconfig config; |
| 598 | |
| 599 | if (!getnoarg (args)) return NULL; |
| 600 | |
| 601 | config = ALnewconfig (); |
Guido van Rossum | c0aab89 | 1991-10-20 20:10:46 +0000 | [diff] [blame] | 602 | if (config == NULL) { |
| 603 | err_errno(RuntimeError); |
| 604 | return NULL; |
| 605 | } |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 606 | |
| 607 | return newconfigobject (config); |
| 608 | } |
Guido van Rossum | b3a5b9b | 1991-09-10 14:54:05 +0000 | [diff] [blame] | 609 | |
| 610 | static object * |
| 611 | al_queryparams(self, args) |
| 612 | object *self, *args; |
| 613 | { |
| 614 | long device; |
| 615 | long length; |
| 616 | long *PVbuffer; |
| 617 | long PVdummy[2]; |
| 618 | object *v; |
| 619 | object *w; |
| 620 | |
Guido van Rossum | fc58e58 | 1992-01-27 16:45:55 +0000 | [diff] [blame] | 621 | if (!getlongarg (args, &device)) |
Guido van Rossum | b3a5b9b | 1991-09-10 14:54:05 +0000 | [diff] [blame] | 622 | return NULL; |
| 623 | length = ALqueryparams(device, PVdummy, 2L); |
| 624 | PVbuffer = NEW(long, length); |
| 625 | if (PVbuffer == NULL) |
| 626 | return err_nomem(); |
| 627 | (void) ALqueryparams(device, PVbuffer, length); |
| 628 | v = newlistobject((int)length); |
| 629 | if (v != NULL) { |
| 630 | int i; |
| 631 | for (i = 0; i < length; i++) |
| 632 | setlistitem(v, i, newintobject(PVbuffer[i])); |
| 633 | } |
| 634 | DEL(PVbuffer); |
| 635 | return v; |
| 636 | } |
| 637 | |
| 638 | static object * |
| 639 | doParams(args, func, modified) |
| 640 | object *args; |
| 641 | void (*func)(long, long *, long); |
| 642 | int modified; |
| 643 | { |
| 644 | long device; |
| 645 | object *list, *v; |
| 646 | long *PVbuffer; |
| 647 | long length; |
| 648 | int i; |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 649 | |
Guido van Rossum | fc58e58 | 1992-01-27 16:45:55 +0000 | [diff] [blame] | 650 | if (!getargs(args, "(lO)", &device, &list)) |
Guido van Rossum | b3a5b9b | 1991-09-10 14:54:05 +0000 | [diff] [blame] | 651 | return NULL; |
| 652 | if (!is_listobject(list)) { |
| 653 | err_badarg(); |
| 654 | return NULL; |
| 655 | } |
| 656 | length = getlistsize(list); |
| 657 | PVbuffer = NEW(long, length); |
| 658 | if (PVbuffer == NULL) |
| 659 | return err_nomem(); |
| 660 | for (i = 0; i < length; i++) { |
| 661 | v = getlistitem(list, i); |
| 662 | if (!is_intobject(v)) { |
| 663 | DEL(PVbuffer); |
| 664 | err_badarg(); |
| 665 | return NULL; |
| 666 | } |
| 667 | PVbuffer[i] = getintvalue(v); |
| 668 | } |
| 669 | |
Guido van Rossum | 8db0307 | 1991-09-13 15:31:47 +0000 | [diff] [blame] | 670 | (*func)(device, PVbuffer, length); |
Guido van Rossum | b3a5b9b | 1991-09-10 14:54:05 +0000 | [diff] [blame] | 671 | |
| 672 | if (modified) { |
| 673 | for (i = 0; i < length; i++) |
| 674 | setlistitem(list, i, newintobject(PVbuffer[i])); |
| 675 | } |
| 676 | |
Guido van Rossum | c0aab89 | 1991-10-20 20:10:46 +0000 | [diff] [blame] | 677 | DEL(PVbuffer); |
| 678 | |
Guido van Rossum | b3a5b9b | 1991-09-10 14:54:05 +0000 | [diff] [blame] | 679 | INCREF(None); |
| 680 | return None; |
| 681 | } |
| 682 | |
| 683 | static object * |
| 684 | al_getparams(self, args) |
| 685 | object *self, *args; |
| 686 | { |
| 687 | return doParams(args, ALgetparams, 1); |
| 688 | } |
| 689 | |
| 690 | static object * |
| 691 | al_setparams(self, args) |
| 692 | object *self, *args; |
| 693 | { |
| 694 | return doParams(args, ALsetparams, 0); |
| 695 | } |
| 696 | |
Guido van Rossum | 448f4bf | 1992-08-19 16:41:15 +0000 | [diff] [blame] | 697 | static object * |
| 698 | al_getname(self, args) |
| 699 | object *self, *args; |
| 700 | { |
| 701 | long device, descriptor; |
| 702 | char *name; |
| 703 | if (!getargs(args, "(ll)", &device, &descriptor)) |
| 704 | return NULL; |
| 705 | name = ALgetname(device, descriptor); |
| 706 | if (name == NULL) { |
| 707 | err_setstr(ValueError, "al.getname: bad descriptor"); |
| 708 | return NULL; |
| 709 | } |
| 710 | return newstringobject(name); |
| 711 | } |
| 712 | |
| 713 | static object * |
| 714 | al_getdefault(self, args) |
| 715 | object *self, *args; |
| 716 | { |
| 717 | long device, descriptor, value; |
| 718 | if (!getargs(args, "(ll)", &device, &descriptor)) |
| 719 | return NULL; |
| 720 | value = ALgetdefault(device, descriptor); |
| 721 | return newlongobject(value); |
| 722 | } |
| 723 | |
| 724 | static object * |
| 725 | al_getminmax(self, args) |
| 726 | object *self, *args; |
| 727 | { |
| 728 | long device, descriptor, min, max; |
| 729 | if (!getargs(args, "(ll)", &device, &descriptor)) |
| 730 | return NULL; |
| 731 | min = -1; |
| 732 | max = -1; |
| 733 | ALgetminmax(device, descriptor, &min, &max); |
| 734 | return mkvalue("ll", min, max); |
| 735 | } |
| 736 | |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 737 | static struct methodlist al_methods[] = { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 738 | {"openport", (method)al_openport}, |
| 739 | {"newconfig", (method)al_newconfig}, |
| 740 | {"queryparams", (method)al_queryparams}, |
| 741 | {"getparams", (method)al_getparams}, |
| 742 | {"setparams", (method)al_setparams}, |
| 743 | {"getname", (method)al_getname}, |
| 744 | {"getdefault", (method)al_getdefault}, |
| 745 | {"getminmax", (method)al_getminmax}, |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 746 | {NULL, NULL} /* sentinel */ |
| 747 | }; |
| 748 | |
| 749 | void |
| 750 | inital() |
| 751 | { |
| 752 | initmodule("al", al_methods); |
| 753 | } |
| 754 | |
Guido van Rossum | fc58e58 | 1992-01-27 16:45:55 +0000 | [diff] [blame] | 755 | static int |
| 756 | getconfigarg(o, conf) |
| 757 | object *o; |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 758 | ALconfig *conf; |
| 759 | { |
| 760 | if (o == NULL || !is_configobject(o)) |
| 761 | return err_badarg (); |
| 762 | |
Guido van Rossum | fc58e58 | 1992-01-27 16:45:55 +0000 | [diff] [blame] | 763 | *conf = ((configobject *) o) -> ob_config; |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 764 | |
| 765 | return 1; |
| 766 | } |
| 767 | |
Guido van Rossum | fc58e58 | 1992-01-27 16:45:55 +0000 | [diff] [blame] | 768 | static int |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 769 | getstrstrconfigarg(v, a, b, c) |
| 770 | object *v; |
Guido van Rossum | fc58e58 | 1992-01-27 16:45:55 +0000 | [diff] [blame] | 771 | char **a; |
| 772 | char **b; |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 773 | ALconfig *c; |
| 774 | { |
Guido van Rossum | fc58e58 | 1992-01-27 16:45:55 +0000 | [diff] [blame] | 775 | object *o; |
| 776 | return getargs(v, "(ssO)", a, b, &o) && getconfigarg(o, c); |
Guido van Rossum | e3db862 | 1991-09-09 23:33:34 +0000 | [diff] [blame] | 777 | } |