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