1 /**********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
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.
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.
23 ******************************************************************/
25 /* AL module -- interface to Mark Callow's Audio Library (AL). */
29 /* Check which version audio library we have: */
30 #ifdef AL_ERROR_NUMBER
32 /* XXXX 4.0.5 libaudio also allows us to provide better error
33 ** handling (with ALseterrorhandler). We should implement that
39 #include "allobjects.h"
41 #include "modsupport.h"
42 #include "structmember.h"
53 staticforward typeobject Configtype
;
55 #define is_configobject(v) ((v)->ob_type == &Configtype)
58 static int getconfigarg
PROTO((object
*, ALconfig
*));
59 static int getstrstrconfigarg
PROTO((object
*, char **, char **, ALconfig
*));
62 setConfig (self
, args
, func
)
65 void (*func
)(ALconfig
, long);
69 if (!getlongarg (args
, &par
)) return NULL
;
71 (*func
) (self
-> ob_config
, par
);
78 getConfig (self
, args
, func
)
81 long (*func
)(ALconfig
);
85 if (!getnoarg (args
)) return NULL
;
87 par
= (*func
) (self
-> ob_config
);
89 return newintobject (par
);
93 al_setqueuesize (self
, args
)
97 return (setConfig (self
, args
, ALsetqueuesize
));
101 al_getqueuesize (self
, args
)
105 return (getConfig (self
, args
, ALgetqueuesize
));
109 al_setwidth (self
, args
)
113 return (setConfig (self
, args
, ALsetwidth
));
117 al_getwidth (self
, args
)
121 return (getConfig (self
, args
, ALgetwidth
));
125 al_getchannels (self
, args
)
129 return (getConfig (self
, args
, ALgetchannels
));
133 al_setchannels (self
, args
)
137 return (setConfig (self
, args
, ALsetchannels
));
143 al_getsampfmt (self
, args
)
147 return (getConfig (self
, args
, ALgetsampfmt
));
151 al_setsampfmt (self
, args
)
155 return (setConfig (self
, args
, ALsetsampfmt
));
159 al_getfloatmax(self
, args
)
165 if ( !getnoarg(args
) )
167 arg
= ALgetfloatmax(self
->ob_config
);
168 return newfloatobject(arg
);
172 al_setfloatmax(self
, args
)
178 if ( !getargs(args
, "d", &arg
) )
180 ALsetfloatmax(self
->ob_config
, arg
);
186 static struct methodlist config_methods
[] = {
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
},
194 {"getsampfmt", (method
)al_getsampfmt
},
195 {"setsampfmt", (method
)al_setsampfmt
},
196 {"getfloatmax", (method
)al_getfloatmax
},
197 {"setfloatmax", (method
)al_setfloatmax
},
199 {NULL
, NULL
} /* sentinel */
206 ALfreeconfig(self
->ob_config
);
211 config_getattr(self
, name
)
215 return findmethod(config_methods
, (object
*)self
, name
);
218 static typeobject Configtype
= {
219 OB_HEAD_INIT(&Typetype
)
221 "config", /*tp_name*/
222 sizeof(configobject
), /*tp_size*/
225 (destructor
)config_dealloc
, /*tp_dealloc*/
227 (getattrfunc
)config_getattr
, /*tp_getattr*/
234 newconfigobject(config
)
239 p
= NEWOBJ(configobject
, &Configtype
);
242 p
->ob_config
= config
;
253 staticforward typeobject Porttype
;
255 #define is_portobject(v) ((v)->ob_type == &Porttype)
258 al_closeport (self
, args
)
262 if (!getnoarg (args
)) return NULL
;
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! */
275 al_getfd (self
, args
)
281 if (!getnoarg (args
)) return NULL
;
283 fd
= ALgetfd (self
-> ob_port
);
285 return newintobject (fd
);
289 al_getfilled (self
, args
)
295 if (!getnoarg (args
)) return NULL
;
297 count
= ALgetfilled (self
-> ob_port
);
299 return newintobject (count
);
303 al_getfillable (self
, args
)
309 if (!getnoarg (args
)) return NULL
;
311 count
= ALgetfillable (self
-> ob_port
);
313 return newintobject (count
);
317 al_readsamps (self
, args
)
326 if (!getlongarg (args
, &count
)) return NULL
;
330 err_setstr (RuntimeError
, "al.readsamps : arg <= 0");
334 c
= ALgetconfig(self
->ob_port
);
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);
342 width
= ALgetwidth(c
);
344 width
= ALgetwidth(c
);
347 v
= newsizedstringobject ((char *)NULL
, width
* count
);
348 if (v
== NULL
) return NULL
;
351 ALreadsamps (self
-> ob_port
, (void *) getstringvalue(v
), count
);
358 al_writesamps (self
, args
)
367 if (!getargs (args
, "s#", &buf
, &size
)) return NULL
;
369 c
= ALgetconfig(self
->ob_port
);
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);
377 width
= ALgetwidth(c
);
379 width
= ALgetwidth(c
);
383 ALwritesamps (self
-> ob_port
, (void *) buf
, (long) size
/ width
);
391 al_getfillpoint (self
, args
)
397 if (!getnoarg (args
)) return NULL
;
399 count
= ALgetfillpoint (self
-> ob_port
);
401 return newintobject (count
);
405 al_setfillpoint (self
, args
)
411 if (!getlongarg (args
, &count
)) return NULL
;
413 ALsetfillpoint (self
-> ob_port
, count
);
420 al_setconfig (self
, args
)
426 if (!getconfigarg (args
, &config
)) return NULL
;
428 ALsetconfig (self
-> ob_port
, config
);
435 al_getconfig (self
, args
)
441 if (!getnoarg (args
)) return NULL
;
443 config
= ALgetconfig (self
-> ob_port
);
445 return newconfigobject (config
);
450 al_getstatus (self
, args
)
459 if (!getargs(args
, "O", &list
))
461 if (!is_listobject(list
)) {
465 length
= getlistsize(list
);
466 PVbuffer
= NEW(long, length
);
467 if (PVbuffer
== NULL
)
469 for (i
= 0; i
< length
; i
++) {
470 v
= getlistitem(list
, i
);
471 if (!is_intobject(v
)) {
476 PVbuffer
[i
] = getintvalue(v
);
479 ALgetstatus(self
->ob_port
, PVbuffer
, length
);
481 for (i
= 0; i
< length
; i
++)
482 setlistitem(list
, i
, newintobject(PVbuffer
[i
]));
491 static struct methodlist port_methods
[] = {
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
},
504 {"getstatus", (method
)al_getstatus
},
506 {NULL
, NULL
} /* sentinel */
513 if (p
->ob_port
!= NULL
)
514 ALcloseport(p
->ob_port
);
519 port_getattr(p
, name
)
523 return findmethod(port_methods
, (object
*)p
, name
);
526 static typeobject Porttype
= {
527 OB_HEAD_INIT(&Typetype
)
530 sizeof(portobject
), /*tp_size*/
533 (destructor
)port_dealloc
, /*tp_dealloc*/
535 (getattrfunc
)port_getattr
, /*tp_getattr*/
547 p
= NEWOBJ(portobject
, &Porttype
);
557 al_openport (self
, args
)
562 ALconfig config
= NULL
;
565 if (args
== NULL
|| !is_tupleobject(args
)) {
569 size
= gettuplesize(args
);
571 if (!getargs (args
, "(ss)", &name
, &dir
))
574 else if (size
== 3) {
575 if (!getstrstrconfigarg (args
, &name
, &dir
, &config
))
583 port
= ALopenport(name
, dir
, config
);
586 err_errno(RuntimeError
);
590 return newportobject (port
);
594 al_newconfig (self
, args
)
599 if (!getnoarg (args
)) return NULL
;
601 config
= ALnewconfig ();
602 if (config
== NULL
) {
603 err_errno(RuntimeError
);
607 return newconfigobject (config
);
611 al_queryparams(self
, args
)
621 if (!getlongarg (args
, &device
))
623 length
= ALqueryparams(device
, PVdummy
, 2L);
624 PVbuffer
= NEW(long, length
);
625 if (PVbuffer
== NULL
)
627 (void) ALqueryparams(device
, PVbuffer
, length
);
628 v
= newlistobject((int)length
);
631 for (i
= 0; i
< length
; i
++)
632 setlistitem(v
, i
, newintobject(PVbuffer
[i
]));
639 doParams(args
, func
, modified
)
641 void (*func
)(long, long *, long);
650 if (!getargs(args
, "(lO)", &device
, &list
))
652 if (!is_listobject(list
)) {
656 length
= getlistsize(list
);
657 PVbuffer
= NEW(long, length
);
658 if (PVbuffer
== NULL
)
660 for (i
= 0; i
< length
; i
++) {
661 v
= getlistitem(list
, i
);
662 if (!is_intobject(v
)) {
667 PVbuffer
[i
] = getintvalue(v
);
670 (*func
)(device
, PVbuffer
, length
);
673 for (i
= 0; i
< length
; i
++)
674 setlistitem(list
, i
, newintobject(PVbuffer
[i
]));
684 al_getparams(self
, args
)
687 return doParams(args
, ALgetparams
, 1);
691 al_setparams(self
, args
)
694 return doParams(args
, ALsetparams
, 0);
698 al_getname(self
, args
)
701 long device
, descriptor
;
703 if (!getargs(args
, "(ll)", &device
, &descriptor
))
705 name
= ALgetname(device
, descriptor
);
707 err_setstr(ValueError
, "al.getname: bad descriptor");
710 return newstringobject(name
);
714 al_getdefault(self
, args
)
717 long device
, descriptor
, value
;
718 if (!getargs(args
, "(ll)", &device
, &descriptor
))
720 value
= ALgetdefault(device
, descriptor
);
721 return newlongobject(value
);
725 al_getminmax(self
, args
)
728 long device
, descriptor
, min
, max
;
729 if (!getargs(args
, "(ll)", &device
, &descriptor
))
733 ALgetminmax(device
, descriptor
, &min
, &max
);
734 return mkvalue("ll", min
, max
);
737 static struct methodlist al_methods
[] = {
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
},
746 {NULL
, NULL
} /* sentinel */
752 initmodule("al", al_methods
);
756 getconfigarg(o
, conf
)
760 if (o
== NULL
|| !is_configobject(o
))
761 return err_badarg ();
763 *conf
= ((configobject
*) o
) -> ob_config
;
769 getstrstrconfigarg(v
, a
, b
, c
)
776 return getargs(v
, "(ssO)", a
, b
, &o
) && getconfigarg(o
, c
);