hotpy_2 / Modules / almodule.c

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
/**********************************************************
Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
Netherlands.

                        All Rights Reserved

Permission to use, copy, modify, and distribute this software and its 
documentation for any purpose and without fee is hereby granted, 
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in 
supporting documentation, and that the names of Stichting Mathematisch
Centrum or CWI not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior permission.

STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

******************************************************************/

/* AL module -- interface to Mark Callow's Audio Library (AL). */

#include "audio.h"

#include "allobjects.h"
#include "import.h"
#include "modsupport.h"
#include "structmember.h"
#include "ceval.h"


/* Config objects */

typedef struct {
	OB_HEAD
	ALconfig ob_config;
} configobject;

extern typeobject Configtype; /* Forward */

#define is_configobject(v) ((v)->ob_type == &Configtype)

/* Forward */
static int getconfigarg PROTO((object *, ALconfig *));
static int getstrstrconfigarg PROTO((object *, char **, char **, ALconfig *));

static object *
setConfig (self, args, func)
	configobject *self;
	object *args;
	void (*func)(ALconfig, long);
{
	long par;

	if (!getlongarg (args, &par)) return NULL;

	(*func) (self-> ob_config, par);

	INCREF (None);
	return None;
}

static object *
getConfig (self, args, func)
	configobject *self;
	object *args;
	long (*func)(ALconfig);
{	
	long par;

	if (!getnoarg (args)) return NULL;
	
	par = (*func) (self-> ob_config);

	return newintobject (par);
}

static object *
al_setqueuesize (self, args)
	configobject *self;
	object *args;
{
	return (setConfig (self, args, ALsetqueuesize));
}

static object *
al_getqueuesize (self, args)
	configobject *self;
	object *args;
{
	return (getConfig (self, args, ALgetqueuesize));
}

static object *
al_setwidth (self, args)
	configobject *self;
	object *args;
{
	return (setConfig (self, args, ALsetwidth));
}

static object *
al_getwidth (self, args)
	configobject *self;
	object *args;
{
	return (getConfig (self, args, ALgetwidth));	
}

static object *
al_getchannels (self, args)
	configobject *self;
	object *args;
{
	return (getConfig (self, args, ALgetchannels));	
}

static object *
al_setchannels (self, args)
	configobject *self;
	object *args;
{
	return (setConfig (self, args, ALsetchannels));
}

static struct methodlist config_methods[] = {
	{"getqueuesize",	al_getqueuesize},
	{"setqueuesize",	al_setqueuesize},
	{"getwidth",		al_getwidth},
	{"setwidth",		al_setwidth},
	{"getchannels",		al_getchannels},
	{"setchannels",		al_setchannels},
	{NULL,			NULL}		/* sentinel */
};

static void
config_dealloc(self)
	configobject *self;
{
	ALfreeconfig(self->ob_config);
	DEL(self);
}

static object *
config_getattr(self, name)
	configobject *self;
	char *name;
{
	return findmethod(config_methods, (object *)self, name);
}

typeobject Configtype = {
	OB_HEAD_INIT(&Typetype)
	0,			/*ob_size*/
	"config",		/*tp_name*/
	sizeof(configobject),	/*tp_size*/
	0,			/*tp_itemsize*/
	/* methods */
	config_dealloc,		/*tp_dealloc*/
	0,			/*tp_print*/
	config_getattr,		/*tp_getattr*/
	0,			/*tp_setattr*/
	0,			/*tp_compare*/
	0,			/*tp_repr*/
};

static object *
newconfigobject(config)
	ALconfig config;
{
	configobject *p;
	
	p = NEWOBJ(configobject, &Configtype);
	if (p == NULL)
		return NULL;
	p->ob_config = config;
	return (object *)p;
}

/* Port objects */

typedef struct {
	OB_HEAD
	ALport ob_port;
} portobject;

extern typeobject Porttype; /* Forward */

#define is_portobject(v) ((v)->ob_type == &Porttype)

static object *
al_closeport (self, args)
	portobject *self;
	object *args;
{
	if (!getnoarg (args)) return NULL;

	if (self->ob_port != NULL) {
		ALcloseport (self-> ob_port);
		self->ob_port = NULL;
		/* XXX Using a closed port may dump core! */
	}

	INCREF (None);
	return None;
}

static object *
al_getfd (self, args)
	portobject *self;
	object *args;
{
	int fd;

	if (!getnoarg (args)) return NULL;

	fd = ALgetfd (self-> ob_port);

	return newintobject (fd);
}

static object *
al_getfilled (self, args)
	portobject *self;
	object *args;
{
	long count;

	if (!getnoarg (args)) return NULL;
	
	count = ALgetfilled (self-> ob_port);

	return newintobject (count);
}

static object *
al_getfillable (self, args)
	portobject *self;
	object *args;
{
	long count;

	if (!getnoarg (args)) return NULL;
	
	count = ALgetfillable (self-> ob_port);

	return newintobject (count);
}

static object *
al_readsamps (self, args)
	portobject *self;
	object *args;
{
	long count;
	object *v;
	ALconfig c;
	int width;

	if (!getlongarg (args, &count)) return NULL;

	if (count <= 0)
	{
		err_setstr (RuntimeError, "al.readsamps : arg <= 0");
		return NULL;
	}

	c = ALgetconfig(self->ob_port);
	width = ALgetwidth(c);
	ALfreeconfig(c);
	v = newsizedstringobject ((char *)NULL, width * count);
	if (v == NULL) return NULL;

	BGN_SAVE
	ALreadsamps (self-> ob_port, (void *) getstringvalue(v), count);
	END_SAVE

	return (v);
}

static object *
al_writesamps (self, args)
	portobject *self;
	object *args;
{
	long count;
	char *buf;
	int size, width;
	ALconfig c;

	if (!getargs (args, "s#", &buf, &size)) return NULL;

	c = ALgetconfig(self->ob_port);
	width = ALgetwidth(c);
	ALfreeconfig(c);
	BGN_SAVE
	ALwritesamps (self-> ob_port, (void *) buf, (long) size / width);
	END_SAVE

	INCREF (None);
	return None;
}

static object *
al_getfillpoint (self, args)
	portobject *self;
	object *args;
{
	long count;

	if (!getnoarg (args)) return NULL;
	
	count = ALgetfillpoint (self-> ob_port);

	return newintobject (count);
}

static object *
al_setfillpoint (self, args)
	portobject *self;
	object *args;
{
	long count;

	if (!getlongarg (args, &count)) return NULL;
	
	ALsetfillpoint (self-> ob_port, count);

	INCREF (None);
	return (None);
}

static object *
al_setconfig (self, args)
	portobject *self;
	object *args;
{
	ALconfig config;

	if (!getconfigarg (args, &config)) return NULL;
	
	ALsetconfig (self-> ob_port, config);

	INCREF (None);
	return (None);
}

static object *
al_getconfig (self, args)
	portobject *self;
	object *args;
{
	ALconfig config;

	if (!getnoarg (args)) return NULL;
	
	config = ALgetconfig (self-> ob_port);

	return newconfigobject (config);
}

static struct methodlist port_methods[] = {
	{"closeport",		al_closeport},
	{"getfd",		al_getfd},
        {"fileno",		al_getfd},
	{"getfilled",		al_getfilled},
	{"getfillable",		al_getfillable},
	{"readsamps",		al_readsamps},
	{"writesamps",		al_writesamps},
	{"setfillpoint",	al_setfillpoint},
	{"getfillpoint",	al_getfillpoint},
	{"setconfig",		al_setconfig},
	{"getconfig",		al_getconfig},
	{NULL,			NULL}		/* sentinel */
};

static void
port_dealloc(p)
	portobject *p;
{
	if (p->ob_port != NULL)
		ALcloseport(p->ob_port);
	DEL(p);
}

static object *
port_getattr(p, name)
	portobject *p;
	char *name;
{
	return findmethod(port_methods, (object *)p, name);
}

typeobject Porttype = {
	OB_HEAD_INIT(&Typetype)
	0,			/*ob_size*/
	"port",			/*tp_name*/
	sizeof(portobject),	/*tp_size*/
	0,			/*tp_itemsize*/
	/* methods */
	port_dealloc,		/*tp_dealloc*/
	0,			/*tp_print*/
	port_getattr,		/*tp_getattr*/
	0,			/*tp_setattr*/
	0,			/*tp_compare*/
	0,			/*tp_repr*/
};

static object *
newportobject(port)
	ALport port;
{
	portobject *p;
	
	p = NEWOBJ(portobject, &Porttype);
	if (p == NULL)
		return NULL;
	p->ob_port = port;
	return (object *)p;
}

/* the module al */

static object *
al_openport (self, args)
	object *self, *args;
{
	char *name, *dir;
	ALport port;
	ALconfig config = NULL;
	int size;

	if (args == NULL || !is_tupleobject(args)) {
		err_badarg ();
		return NULL;
	}
	size = gettuplesize(args);
	if (size == 2) {
		if (!getstrstrarg (args, &name, &dir))
			return NULL;
	}
	else if (size == 3) {
		if (!getstrstrconfigarg (args, &name, &dir, &config))
			return NULL;
	}
	else {
		err_badarg ();
		return NULL;
	}

	port = ALopenport(name, dir, config);

	if (port == NULL) {
		err_errno(RuntimeError);
		return NULL;
	}

	return newportobject (port);
}

static object *
al_newconfig (self, args)
	object *self, *args;
{
	ALconfig config;

	if (!getnoarg (args)) return NULL;

	config = ALnewconfig ();
	if (config == NULL) {
		err_errno(RuntimeError);
		return NULL;
	}

	return newconfigobject (config);
}

static object *
al_queryparams(self, args)
	object *self, *args;
{
	long device;
	long length;
	long *PVbuffer;
	long PVdummy[2];
	object *v;
	object *w;

	if (!getlongarg (args, &device))
		return NULL;
	length = ALqueryparams(device, PVdummy, 2L);
	PVbuffer = NEW(long, length);
	if (PVbuffer == NULL)
		return err_nomem();
	(void) ALqueryparams(device, PVbuffer, length);
	v = newlistobject((int)length);
	if (v != NULL) {
		int i;
		for (i = 0; i < length; i++)
			setlistitem(v, i, newintobject(PVbuffer[i]));
	}
	DEL(PVbuffer);
	return v;
}

static object *
doParams(args, func, modified)
	object *args;
	void (*func)(long, long *, long);
	int modified;
{
	long device;
	object *list, *v;
	long *PVbuffer;
	long length;
	int i;
	
	if (!getargs(args, "(lO)", &device, &list))
		return NULL;
	if (!is_listobject(list)) {
		err_badarg();
		return NULL;
	}
	length = getlistsize(list);
	PVbuffer = NEW(long, length);
	if (PVbuffer == NULL)
		return err_nomem();
	for (i = 0; i < length; i++) {
		v = getlistitem(list, i);
		if (!is_intobject(v)) {
			DEL(PVbuffer);
			err_badarg();
			return NULL;
		}
		PVbuffer[i] = getintvalue(v);
	}

	(*func)(device, PVbuffer, length);

	if (modified) {
		for (i = 0; i < length; i++)
			setlistitem(list, i, newintobject(PVbuffer[i]));
	}

	DEL(PVbuffer);

	INCREF(None);
	return None;
}

static object *
al_getparams(self, args)
	object *self, *args;
{
	return doParams(args, ALgetparams, 1);
}

static object *
al_setparams(self, args)
	object *self, *args;
{
	return doParams(args, ALsetparams, 0);
}

static object *
al_getname(self, args)
	object *self, *args;
{
	long device, descriptor;
	char *name;
	if (!getargs(args, "(ll)", &device, &descriptor))
		return NULL;
	name = ALgetname(device, descriptor);
	if (name == NULL) {
		err_setstr(ValueError, "al.getname: bad descriptor");
		return NULL;
	}
	return newstringobject(name);
}

static object *
al_getdefault(self, args)
	object *self, *args;
{
	long device, descriptor, value;
	if (!getargs(args, "(ll)", &device, &descriptor))
		return NULL;
	value = ALgetdefault(device, descriptor);
	return newlongobject(value);
}

static object *
al_getminmax(self, args)
	object *self, *args;
{
	long device, descriptor, min, max;
	if (!getargs(args, "(ll)", &device, &descriptor))
		return NULL;
	min = -1;
	max = -1;
	ALgetminmax(device, descriptor, &min, &max);
	return mkvalue("ll", min, max);
}

static struct methodlist al_methods[] = {
	{"openport",		al_openport},
	{"newconfig",		al_newconfig},
	{"queryparams",		al_queryparams},
	{"getparams",		al_getparams},
	{"setparams",		al_setparams},
	{"getname",		al_getname},
	{"getdefault",		al_getdefault},
	{"getminmax",		al_getminmax},
	{NULL,			NULL}		/* sentinel */
};

void
inital()
{
	initmodule("al", al_methods);
}

static int
getconfigarg(o, conf)
	object *o;
	ALconfig *conf;
{
	if (o == NULL || !is_configobject(o))
		return err_badarg ();
	
	*conf = ((configobject *) o) -> ob_config;
	
	return 1;
}

static int
getstrstrconfigarg(v, a, b, c)
	object *v;
	char **a;
	char **b;
	ALconfig *c;
{
	object *o;
	return getargs(v, "(ssO)", a, b, &o) && getconfigarg(o, c);
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.