sunaudiodev.c 10.4 KB
Newer Older
Guido van Rossum's avatar
Guido van Rossum committed
1 2 3

/* Sad objects */

4
#include "Python.h"
Guido van Rossum's avatar
Guido van Rossum committed
5 6
#include "structmember.h"

7 8 9 10
#ifdef HAVE_SYS_AUDIOIO_H
#define SOLARIS
#endif

11 12 13 14
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif

15
#include <stropts.h>
Guido van Rossum's avatar
Guido van Rossum committed
16
#include <sys/ioctl.h>
Sjoerd Mullender's avatar
Sjoerd Mullender committed
17
#ifdef SOLARIS
18
#include <sys/audioio.h>
Sjoerd Mullender's avatar
Sjoerd Mullender committed
19
#else
Guido van Rossum's avatar
Guido van Rossum committed
20
#include <sun/audioio.h>
Sjoerd Mullender's avatar
Sjoerd Mullender committed
21
#endif
Guido van Rossum's avatar
Guido van Rossum committed
22 23 24 25

/* #define offsetof(str,mem) ((int)(((str *)0)->mem)) */

typedef struct {
26
	PyObject_HEAD
Guido van Rossum's avatar
Guido van Rossum committed
27 28 29 30 31 32 33 34
	int	x_fd;		/* The open file */
	int	x_icount;	/* # samples read */
	int	x_ocount;	/* # samples written */
	int	x_isctl;	/* True if control device */
	
} sadobject;

typedef struct {
35
	PyObject_HEAD
Guido van Rossum's avatar
Guido van Rossum committed
36 37 38
	audio_info_t ai;
} sadstatusobject;

39 40
staticforward PyTypeObject Sadtype;
staticforward PyTypeObject Sadstatustype;
41
static sadstatusobject *sads_alloc(void);	/* Forward */
Guido van Rossum's avatar
Guido van Rossum committed
42

43
static PyObject *SunAudioError;
Guido van Rossum's avatar
Guido van Rossum committed
44 45 46 47

#define is_sadobject(v)		((v)->ob_type == &Sadtype)
#define is_sadstatusobject(v)	((v)->ob_type == &Sadstatustype)

48

Guido van Rossum's avatar
Guido van Rossum committed
49
static sadobject *
50
newsadobject(PyObject *args)
Guido van Rossum's avatar
Guido van Rossum committed
51 52 53 54 55
{
	sadobject *xp;
	int fd;
	char *mode;
	int imode;
56 57 58
	char* basedev;
	char* ctldev;
	char* opendev;
Guido van Rossum's avatar
Guido van Rossum committed
59 60

	/* Check arg for r/w/rw */
Neal Norwitz's avatar
Neal Norwitz committed
61
	if (!PyArg_ParseTuple(args, "s", &mode))
62 63 64 65 66 67 68 69 70
		return NULL;
	if (strcmp(mode, "r") == 0)
		imode = 0;
	else if (strcmp(mode, "w") == 0)
		imode = 1;
	else if (strcmp(mode, "rw") == 0)
		imode = 2;
	else if (strcmp(mode, "control") == 0)
		imode = -1;
Guido van Rossum's avatar
Guido van Rossum committed
71
	else {
72 73 74
		PyErr_SetString(SunAudioError,
			  "Mode should be one of 'r', 'w', 'rw' or 'control'");
		return NULL;
Guido van Rossum's avatar
Guido van Rossum committed
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
	/* Open the correct device.  The base device name comes from the
	 * AUDIODEV environment variable first, then /dev/audio.  The
	 * control device tacks "ctl" onto the base device name.
	 */
	basedev = getenv("AUDIODEV");
	if (!basedev)
		basedev = "/dev/audio";
	ctldev = PyMem_NEW(char, strlen(basedev) + 4);
	if (!ctldev) {
		PyErr_NoMemory();
		return NULL;
	}
	strcpy(ctldev, basedev);
	strcat(ctldev, "ctl");

	if (imode < 0) {
		opendev = ctldev;
		fd = open(ctldev, 2);
	}
	else {
		opendev = basedev;
		fd = open(basedev, imode);
	}
100
	if (fd < 0) {
101
		PyErr_SetFromErrnoWithFilename(SunAudioError, opendev);
102
		PyMem_DEL(ctldev);
103
		return NULL;
Guido van Rossum's avatar
Guido van Rossum committed
104
	}
105
	PyMem_DEL(ctldev);
Guido van Rossum's avatar
Guido van Rossum committed
106 107

	/* Create and initialize the object */
108
	xp = PyObject_New(sadobject, &Sadtype);
109 110
	if (xp == NULL) {
		close(fd);
Guido van Rossum's avatar
Guido van Rossum committed
111
		return NULL;
112
	}
Guido van Rossum's avatar
Guido van Rossum committed
113 114 115 116 117 118 119 120 121 122
	xp->x_fd = fd;
	xp->x_icount = xp->x_ocount = 0;
	xp->x_isctl = (imode < 0);
	
	return xp;
}

/* Sad methods */

static void
123
sad_dealloc(sadobject *xp)
Guido van Rossum's avatar
Guido van Rossum committed
124 125
{
        close(xp->x_fd);
126
	PyObject_Del(xp);
Guido van Rossum's avatar
Guido van Rossum committed
127 128
}

129
static PyObject *
130
sad_read(sadobject *self, PyObject *args)
Guido van Rossum's avatar
Guido van Rossum committed
131 132 133
{
        int size, count;
	char *cp;
134
	PyObject *rv;
Guido van Rossum's avatar
Guido van Rossum committed
135
	
Neal Norwitz's avatar
Neal Norwitz committed
136
        if (!PyArg_ParseTuple(args, "i:read", &size))
137 138 139 140
		return NULL;
	rv = PyString_FromStringAndSize(NULL, size);
	if (rv == NULL)
		return NULL;
Guido van Rossum's avatar
Guido van Rossum committed
141

142 143
	if (!(cp = PyString_AsString(rv)))
		goto finally;
Guido van Rossum's avatar
Guido van Rossum committed
144 145

	count = read(self->x_fd, cp, size);
146 147 148
	if (count < 0) {
		PyErr_SetFromErrno(SunAudioError);
		goto finally;
Guido van Rossum's avatar
Guido van Rossum committed
149
	}
150 151
#if 0
	/* TBD: why print this message if you can handle the condition?
152 153 154
	 * assume it's debugging info which we can just as well get rid
	 * of.  in any case this message should *not* be using printf!
	 */
155 156 157
	if (count != size)
		printf("sunaudio: funny read rv %d wtd %d\n", count, size);
#endif
Guido van Rossum's avatar
Guido van Rossum committed
158 159
	self->x_icount += count;
	return rv;
160 161 162 163

  finally:
	Py_DECREF(rv);
	return NULL;
Guido van Rossum's avatar
Guido van Rossum committed
164 165
}

166
static PyObject *
167
sad_write(sadobject *self, PyObject *args)
Guido van Rossum's avatar
Guido van Rossum committed
168 169 170 171
{
        char *cp;
	int count, size;
	
Neal Norwitz's avatar
Neal Norwitz committed
172
        if (!PyArg_ParseTuple(args, "s#:write", &cp, &size))
173
		return NULL;
Guido van Rossum's avatar
Guido van Rossum committed
174 175

	count = write(self->x_fd, cp, size);
176 177 178
	if (count < 0) {
		PyErr_SetFromErrno(SunAudioError);
		return NULL;
Guido van Rossum's avatar
Guido van Rossum committed
179
	}
180 181 182 183
#if 0
	if (count != size)
		printf("sunaudio: funny write rv %d wanted %d\n", count, size);
#endif
Guido van Rossum's avatar
Guido van Rossum committed
184 185
	self->x_ocount += count;
	
186 187
	Py_INCREF(Py_None);
	return Py_None;
Guido van Rossum's avatar
Guido van Rossum committed
188 189
}

190
static PyObject *
Neal Norwitz's avatar
Neal Norwitz committed
191
sad_getinfo(sadobject *self)
Guido van Rossum's avatar
Guido van Rossum committed
192 193 194
{
	sadstatusobject *rv;

195 196 197
	if (!(rv = sads_alloc()))
		return NULL;

198 199 200
	if (ioctl(self->x_fd, AUDIO_GETINFO, &rv->ai) < 0) {
		PyErr_SetFromErrno(SunAudioError);
		Py_DECREF(rv);
Guido van Rossum's avatar
Guido van Rossum committed
201 202
		return NULL;
	}
203
	return (PyObject *)rv;
Guido van Rossum's avatar
Guido van Rossum committed
204 205
}

206
static PyObject *
207
sad_setinfo(sadobject *self, sadstatusobject *arg)
Guido van Rossum's avatar
Guido van Rossum committed
208
{
209 210 211
	if (!is_sadstatusobject(arg)) {
		PyErr_SetString(PyExc_TypeError,
				"Must be sun audio status object");
Guido van Rossum's avatar
Guido van Rossum committed
212 213
		return NULL;
	}
214 215
	if (ioctl(self->x_fd, AUDIO_SETINFO, &arg->ai) < 0) {
		PyErr_SetFromErrno(SunAudioError);
Guido van Rossum's avatar
Guido van Rossum committed
216 217
		return NULL;
	}
218 219
	Py_INCREF(Py_None);
	return Py_None;
Guido van Rossum's avatar
Guido van Rossum committed
220 221
}

222
static PyObject *
Neal Norwitz's avatar
Neal Norwitz committed
223
sad_ibufcount(sadobject *self)
Guido van Rossum's avatar
Guido van Rossum committed
224
{
225
	audio_info_t ai;
Guido van Rossum's avatar
Guido van Rossum committed
226
    
227 228 229 230 231
	if (ioctl(self->x_fd, AUDIO_GETINFO, &ai) < 0) {
		PyErr_SetFromErrno(SunAudioError);
		return NULL;
	}
	return PyInt_FromLong(ai.record.samples - self->x_icount);
Guido van Rossum's avatar
Guido van Rossum committed
232 233
}

234
static PyObject *
Neal Norwitz's avatar
Neal Norwitz committed
235
sad_obufcount(sadobject *self)
Guido van Rossum's avatar
Guido van Rossum committed
236
{
237
	audio_info_t ai;
Guido van Rossum's avatar
Guido van Rossum committed
238
    
239 240 241 242
	if (ioctl(self->x_fd, AUDIO_GETINFO, &ai) < 0) {
		PyErr_SetFromErrno(SunAudioError);
		return NULL;
	}
243
	/* x_ocount is in bytes, whereas play.samples is in frames */
Guido van Rossum's avatar
Guido van Rossum committed
244 245 246 247
	/* we want frames */
	return PyInt_FromLong(self->x_ocount / (ai.play.channels *
						ai.play.precision / 8) -
			      ai.play.samples);
Guido van Rossum's avatar
Guido van Rossum committed
248 249
}

250
static PyObject *
Neal Norwitz's avatar
Neal Norwitz committed
251
sad_drain(sadobject *self)
Guido van Rossum's avatar
Guido van Rossum committed
252
{
253 254 255 256 257 258
	if (ioctl(self->x_fd, AUDIO_DRAIN, 0) < 0) {
		PyErr_SetFromErrno(SunAudioError);
		return NULL;
	}
	Py_INCREF(Py_None);
	return Py_None;
Guido van Rossum's avatar
Guido van Rossum committed
259 260
}

Sjoerd Mullender's avatar
Sjoerd Mullender committed
261
#ifdef SOLARIS
262
static PyObject *
Neal Norwitz's avatar
Neal Norwitz committed
263
sad_getdev(sadobject *self)
Sjoerd Mullender's avatar
Sjoerd Mullender committed
264
{
265
	struct audio_device ad;
Sjoerd Mullender's avatar
Sjoerd Mullender committed
266

267 268 269 270 271
	if (ioctl(self->x_fd, AUDIO_GETDEV, &ad) < 0) {
		PyErr_SetFromErrno(SunAudioError);
		return NULL;
	}
	return Py_BuildValue("(sss)", ad.name, ad.version, ad.config);
Sjoerd Mullender's avatar
Sjoerd Mullender committed
272 273 274
}
#endif

275
static PyObject *
Neal Norwitz's avatar
Neal Norwitz committed
276
sad_flush(sadobject *self)
277
{
278 279 280 281 282 283
	if (ioctl(self->x_fd, I_FLUSH, FLUSHW) < 0) {
		PyErr_SetFromErrno(SunAudioError);
		return NULL;
	}
	Py_INCREF(Py_None);
	return Py_None;
284 285
}

286
static PyObject *
Neal Norwitz's avatar
Neal Norwitz committed
287
sad_close(sadobject *self)
288 289
{
    
290 291 292 293 294 295
	if (self->x_fd >= 0) {
		close(self->x_fd);
		self->x_fd = -1;
	}
	Py_INCREF(Py_None);
	return Py_None;
296 297
}

298
static PyObject *
Neal Norwitz's avatar
Neal Norwitz committed
299
sad_fileno(sadobject *self)
300 301 302 303 304
{
	return PyInt_FromLong(self->x_fd);
}


305
static PyMethodDef sad_methods[] = {
Neal Norwitz's avatar
Neal Norwitz committed
306 307 308 309
        { "read",	(PyCFunction)sad_read, METH_VARARGS },
        { "write",	(PyCFunction)sad_write, METH_VARARGS },
        { "ibufcount",	(PyCFunction)sad_ibufcount, METH_NOARGS },
        { "obufcount",	(PyCFunction)sad_obufcount, METH_NOARGS },
Guido van Rossum's avatar
Guido van Rossum committed
310
#define CTL_METHODS 4
Neal Norwitz's avatar
Neal Norwitz committed
311 312 313 314
        { "getinfo",	(PyCFunction)sad_getinfo, METH_NOARGS },
        { "setinfo",	(PyCFunction)sad_setinfo, METH_O},
        { "drain",	(PyCFunction)sad_drain, METH_NOARGS },
        { "flush",	(PyCFunction)sad_flush, METH_NOARGS },
Sjoerd Mullender's avatar
Sjoerd Mullender committed
315
#ifdef SOLARIS
Neal Norwitz's avatar
Neal Norwitz committed
316
	{ "getdev",	(PyCFunction)sad_getdev, METH_NOARGS },
Sjoerd Mullender's avatar
Sjoerd Mullender committed
317
#endif
Neal Norwitz's avatar
Neal Norwitz committed
318 319
        { "close",	(PyCFunction)sad_close, METH_NOARGS },
	{ "fileno",     (PyCFunction)sad_fileno, METH_NOARGS },
Guido van Rossum's avatar
Guido van Rossum committed
320 321 322
	{NULL,		NULL}		/* sentinel */
};

323
static PyObject *
324
sad_getattr(sadobject *xp, char *name)
Guido van Rossum's avatar
Guido van Rossum committed
325
{
326 327 328
	if (xp->x_isctl)
		return Py_FindMethod(sad_methods+CTL_METHODS,
				     (PyObject *)xp, name);
Guido van Rossum's avatar
Guido van Rossum committed
329
	else
330
		return Py_FindMethod(sad_methods, (PyObject *)xp, name);
Guido van Rossum's avatar
Guido van Rossum committed
331 332 333 334 335
}

/* ----------------------------------------------------------------- */

static sadstatusobject *
336
sads_alloc(void) {
337
	return PyObject_New(sadstatusobject, &Sadstatustype);
Guido van Rossum's avatar
Guido van Rossum committed
338 339 340
}

static void
341
sads_dealloc(sadstatusobject *xp)
Guido van Rossum's avatar
Guido van Rossum committed
342
{
343
	PyMem_DEL(xp);
Guido van Rossum's avatar
Guido van Rossum committed
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
}

#define OFF(x) offsetof(audio_info_t,x)
static struct memberlist sads_ml[] = {
	{ "i_sample_rate",	T_UINT,		OFF(record.sample_rate) },
	{ "i_channels",		T_UINT,		OFF(record.channels) },
	{ "i_precision",	T_UINT,		OFF(record.precision) },
	{ "i_encoding",		T_UINT,		OFF(record.encoding) },
	{ "i_gain",		T_UINT,		OFF(record.gain) },
	{ "i_port",		T_UINT,		OFF(record.port) },
	{ "i_samples",		T_UINT,		OFF(record.samples) },
	{ "i_eof",		T_UINT,		OFF(record.eof) },
	{ "i_pause",		T_UBYTE,	OFF(record.pause) },
	{ "i_error",		T_UBYTE,	OFF(record.error) },
	{ "i_waiting",		T_UBYTE,	OFF(record.waiting) },
	{ "i_open",		T_UBYTE,	OFF(record.open) ,	 RO},
	{ "i_active",		T_UBYTE,	OFF(record.active) ,	 RO},
Sjoerd Mullender's avatar
Sjoerd Mullender committed
361 362 363 364 365
#ifdef SOLARIS
	{ "i_buffer_size",	T_UINT,		OFF(record.buffer_size) },
	{ "i_balance",		T_UBYTE,	OFF(record.balance) },
	{ "i_avail_ports",	T_UINT,		OFF(record.avail_ports) },
#endif
Guido van Rossum's avatar
Guido van Rossum committed
366 367 368 369 370 371 372 373 374 375 376 377 378 379

	{ "o_sample_rate",	T_UINT,		OFF(play.sample_rate) },
	{ "o_channels",		T_UINT,		OFF(play.channels) },
	{ "o_precision",	T_UINT,		OFF(play.precision) },
	{ "o_encoding",		T_UINT,		OFF(play.encoding) },
	{ "o_gain",		T_UINT,		OFF(play.gain) },
	{ "o_port",		T_UINT,		OFF(play.port) },
	{ "o_samples",		T_UINT,		OFF(play.samples) },
	{ "o_eof",		T_UINT,		OFF(play.eof) },
	{ "o_pause",		T_UBYTE,	OFF(play.pause) },
	{ "o_error",		T_UBYTE,	OFF(play.error) },
	{ "o_waiting",		T_UBYTE,	OFF(play.waiting) },
	{ "o_open",		T_UBYTE,	OFF(play.open) ,	 RO},
	{ "o_active",		T_UBYTE,	OFF(play.active) ,	 RO},
Sjoerd Mullender's avatar
Sjoerd Mullender committed
380 381 382 383 384
#ifdef SOLARIS
	{ "o_buffer_size",	T_UINT,		OFF(play.buffer_size) },
	{ "o_balance",		T_UBYTE,	OFF(play.balance) },
	{ "o_avail_ports",	T_UINT,		OFF(play.avail_ports) },
#endif
Guido van Rossum's avatar
Guido van Rossum committed
385 386 387 388 389

	{ "monitor_gain",	T_UINT,		OFF(monitor_gain) },
        { NULL,                 0,              0},
};

390
static PyObject *
391
sads_getattr(sadstatusobject *xp, char *name)
Guido van Rossum's avatar
Guido van Rossum committed
392
{
393
	return PyMember_Get((char *)&xp->ai, sads_ml, name);
Guido van Rossum's avatar
Guido van Rossum committed
394 395 396
}

static int
397
sads_setattr(sadstatusobject *xp, char *name, PyObject *v)
Guido van Rossum's avatar
Guido van Rossum committed
398
{
399 400

	if (v == NULL) {
401 402
		PyErr_SetString(PyExc_TypeError,
				"can't delete sun audio status attributes");
403
		return -1;
404
	}
405
	return PyMember_Set((char *)&xp->ai, sads_ml, name, v);
Guido van Rossum's avatar
Guido van Rossum committed
406 407 408 409 410
}

/* ------------------------------------------------------------------- */


411 412
static PyTypeObject Sadtype = {
	PyObject_HEAD_INIT(&PyType_Type)
413
	0,				/*ob_size*/
414
	"sunaudiodev.sun_audio_device",	/*tp_name*/
415 416
	sizeof(sadobject),		/*tp_size*/
	0,				/*tp_itemsize*/
Guido van Rossum's avatar
Guido van Rossum committed
417
	/* methods */
418 419 420 421 422 423
	(destructor)sad_dealloc,	/*tp_dealloc*/
	0,				/*tp_print*/
	(getattrfunc)sad_getattr,	/*tp_getattr*/
	0,				/*tp_setattr*/
	0,				/*tp_compare*/
	0,				/*tp_repr*/
Guido van Rossum's avatar
Guido van Rossum committed
424 425
};

426 427
static PyTypeObject Sadstatustype = {
	PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum's avatar
Guido van Rossum committed
428
	0,				/*ob_size*/
429
	"sunaudiodev.sun_audio_device_status", /*tp_name*/
Guido van Rossum's avatar
Guido van Rossum committed
430 431 432
	sizeof(sadstatusobject),	/*tp_size*/
	0,				/*tp_itemsize*/
	/* methods */
433 434 435 436 437 438
	(destructor)sads_dealloc,	/*tp_dealloc*/
	0,				/*tp_print*/
	(getattrfunc)sads_getattr,	/*tp_getattr*/
	(setattrfunc)sads_setattr,	/*tp_setattr*/
	0,				/*tp_compare*/
	0,				/*tp_repr*/
Guido van Rossum's avatar
Guido van Rossum committed
439 440 441
};
/* ------------------------------------------------------------------- */

442
static PyObject *
443
sadopen(PyObject *self, PyObject *args)
Guido van Rossum's avatar
Guido van Rossum committed
444
{
445
	return (PyObject *)newsadobject(args);
Guido van Rossum's avatar
Guido van Rossum committed
446 447
}
    
448
static PyMethodDef sunaudiodev_methods[] = {
Neal Norwitz's avatar
Neal Norwitz committed
449
    { "open", sadopen, METH_VARARGS },
Guido van Rossum's avatar
Guido van Rossum committed
450 451 452 453
    { 0, 0 },
};

void
454
initsunaudiodev(void)
455 456 457 458 459
{
	PyObject *m, *d;

	m = Py_InitModule("sunaudiodev", sunaudiodev_methods);
	d = PyModule_GetDict(m);
460
	SunAudioError = PyErr_NewException("sunaudiodev.error", NULL, NULL);
461 462
	if (SunAudioError)
		PyDict_SetItemString(d, "error", SunAudioError);
Guido van Rossum's avatar
Guido van Rossum committed
463
}