blob: 360e3d2af4b613e5386e0c338c72ff5190da443f [file] [log] [blame]
Guido van Rossume3db8621991-09-09 23:33:34 +00001/**********************************************************
2Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
3Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF 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
37typedef struct {
38 OB_HEAD
39 ALconfig ob_config;
40} configobject;
41
42extern typeobject Configtype; /* Forward */
43
44#define is_configobject(v) ((v)->ob_type == &Configtype)
45
46static object *
47setConfig (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
62static object *
63getConfig (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
77static object *
78al_setqueuesize (self, args)
79 configobject *self;
80 object *args;
81{
82 return (setConfig (self, args, ALsetqueuesize));
83}
84
85static object *
86al_getqueuesize (self, args)
87 configobject *self;
88 object *args;
89{
90 return (getConfig (self, args, ALgetqueuesize));
91}
92
93static object *
94al_setwidth (self, args)
95 configobject *self;
96 object *args;
97{
98 return (setConfig (self, args, ALsetwidth));
99}
100
101static object *
102al_getwidth (self, args)
103 configobject *self;
104 object *args;
105{
106 return (getConfig (self, args, ALgetwidth));
107}
108
109static object *
110al_getchannels (self, args)
111 configobject *self;
112 object *args;
113{
114 return (getConfig (self, args, ALgetchannels));
115}
116
117static object *
118al_setchannels (self, args)
119 configobject *self;
120 object *args;
121{
122 return (setConfig (self, args, ALsetchannels));
123}
124
125static 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
135static void
136config_dealloc(self)
137 configobject *self;
138{
139 ALfreeconfig(self->ob_config);
140 DEL(self);
141}
142
143static object *
144config_getattr(self, name)
145 configobject *self;
146 char *name;
147{
148 return findmethod(config_methods, (object *)self, name);
149}
150
151typeobject 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
166static object *
167newconfigobject(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
181typedef struct {
182 OB_HEAD
183 ALport ob_port;
184} portobject;
185
186extern typeobject Porttype; /* Forward */
187
188#define is_portobject(v) ((v)->ob_type == &Porttype)
189
190static object *
191al_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
207static object *
208al_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
221static object *
222al_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
235static object *
236al_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
249static object *
250al_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
275static object *
276al_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
294static object *
295al_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
308static object *
309al_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
323static object *
324al_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
338static object *
339al_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
352static 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
366static void
367port_dealloc(p)
368 portobject *p;
369{
370 if (p->ob_port != NULL)
371 ALcloseport(p->ob_port);
372 DEL(p);
373}
374
375static object *
376port_getattr(p, name)
377 portobject *p;
378 char *name;
379{
380 return findmethod(port_methods, (object *)p, name);
381}
382
383typeobject 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
398static object *
399newportobject(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
413static object *
414al_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
440static object *
441al_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
453static struct methodlist al_methods[] = {
454 {"openport", al_openport},
455 {"newconfig", al_newconfig},
456 {NULL, NULL} /* sentinel */
457};
458
459void
460inital()
461{
462 initmodule("al", al_methods);
463}
464
465int
466getconfigarg (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
478int
479getstrstrconfigarg(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}