5 #include "structmember.h"
7 #ifdef HAVE_SYS_AUDIOIO_H
16 #include <sys/ioctl.h>
18 #include <sys/audioio.h>
20 #include <sun/audioio.h>
23 /* #define offsetof(str,mem) ((int)(((str *)0)->mem)) */
27 int x_fd
; /* The open file */
28 int x_icount
; /* # samples read */
29 int x_ocount
; /* # samples written */
30 int x_isctl
; /* True if control device */
39 static PyTypeObject Sadtype
;
40 static PyTypeObject Sadstatustype
;
41 static sadstatusobject
*sads_alloc(void); /* Forward */
43 static PyObject
*SunAudioError
;
45 #define is_sadobject(v) (Py_TYPE(v) == &Sadtype)
46 #define is_sadstatusobject(v) (Py_TYPE(v) == &Sadstatustype)
50 newsadobject(PyObject
*args
)
60 /* Check arg for r/w/rw */
61 if (!PyArg_ParseTuple(args
, "s", &mode
))
63 if (strcmp(mode
, "r") == 0)
65 else if (strcmp(mode
, "w") == 0)
67 else if (strcmp(mode
, "rw") == 0)
69 else if (strcmp(mode
, "control") == 0)
72 PyErr_SetString(SunAudioError
,
73 "Mode should be one of 'r', 'w', 'rw' or 'control'");
77 /* Open the correct device. The base device name comes from the
78 * AUDIODEV environment variable first, then /dev/audio. The
79 * control device tacks "ctl" onto the base device name.
81 basedev
= getenv("AUDIODEV");
83 basedev
= "/dev/audio";
84 ctldev
= PyMem_NEW(char, strlen(basedev
) + 4);
89 strcpy(ctldev
, basedev
);
90 strcat(ctldev
, "ctl");
98 fd
= open(basedev
, imode
);
101 PyErr_SetFromErrnoWithFilename(SunAudioError
, opendev
);
107 /* Create and initialize the object */
108 xp
= PyObject_New(sadobject
, &Sadtype
);
114 xp
->x_icount
= xp
->x_ocount
= 0;
115 xp
->x_isctl
= (imode
< 0);
123 sad_dealloc(sadobject
*xp
)
130 sad_read(sadobject
*self
, PyObject
*args
)
136 if (!PyArg_ParseTuple(args
, "i:read", &size
))
138 rv
= PyString_FromStringAndSize(NULL
, size
);
142 if (!(cp
= PyString_AsString(rv
)))
145 count
= read(self
->x_fd
, cp
, size
);
147 PyErr_SetFromErrno(SunAudioError
);
151 /* TBD: why print this message if you can handle the condition?
152 * assume it's debugging info which we can just as well get rid
153 * of. in any case this message should *not* be using printf!
156 printf("sunaudio: funny read rv %d wtd %d\n", count
, size
);
158 self
->x_icount
+= count
;
167 sad_write(sadobject
*self
, PyObject
*args
)
172 if (!PyArg_ParseTuple(args
, "s#:write", &cp
, &size
))
175 count
= write(self
->x_fd
, cp
, size
);
177 PyErr_SetFromErrno(SunAudioError
);
182 printf("sunaudio: funny write rv %d wanted %d\n", count
, size
);
184 self
->x_ocount
+= count
;
191 sad_getinfo(sadobject
*self
)
195 if (!(rv
= sads_alloc()))
198 if (ioctl(self
->x_fd
, AUDIO_GETINFO
, &rv
->ai
) < 0) {
199 PyErr_SetFromErrno(SunAudioError
);
203 return (PyObject
*)rv
;
207 sad_setinfo(sadobject
*self
, sadstatusobject
*arg
)
209 if (!is_sadstatusobject(arg
)) {
210 PyErr_SetString(PyExc_TypeError
,
211 "Must be sun audio status object");
214 if (ioctl(self
->x_fd
, AUDIO_SETINFO
, &arg
->ai
) < 0) {
215 PyErr_SetFromErrno(SunAudioError
);
223 sad_ibufcount(sadobject
*self
)
227 if (ioctl(self
->x_fd
, AUDIO_GETINFO
, &ai
) < 0) {
228 PyErr_SetFromErrno(SunAudioError
);
231 return PyInt_FromLong(ai
.record
.samples
- self
->x_icount
);
235 sad_obufcount(sadobject
*self
)
239 if (ioctl(self
->x_fd
, AUDIO_GETINFO
, &ai
) < 0) {
240 PyErr_SetFromErrno(SunAudioError
);
243 /* x_ocount is in bytes, whereas play.samples is in frames */
245 return PyInt_FromLong(self
->x_ocount
/ (ai
.play
.channels
*
246 ai
.play
.precision
/ 8) -
251 sad_drain(sadobject
*self
)
253 if (ioctl(self
->x_fd
, AUDIO_DRAIN
, 0) < 0) {
254 PyErr_SetFromErrno(SunAudioError
);
263 sad_getdev(sadobject
*self
)
265 struct audio_device ad
;
267 if (ioctl(self
->x_fd
, AUDIO_GETDEV
, &ad
) < 0) {
268 PyErr_SetFromErrno(SunAudioError
);
271 return Py_BuildValue("(sss)", ad
.name
, ad
.version
, ad
.config
);
276 sad_flush(sadobject
*self
)
278 if (ioctl(self
->x_fd
, I_FLUSH
, FLUSHW
) < 0) {
279 PyErr_SetFromErrno(SunAudioError
);
287 sad_close(sadobject
*self
)
290 if (self
->x_fd
>= 0) {
299 sad_fileno(sadobject
*self
)
301 return PyInt_FromLong(self
->x_fd
);
305 static PyMethodDef sad_methods
[] = {
306 { "read", (PyCFunction
)sad_read
, METH_VARARGS
},
307 { "write", (PyCFunction
)sad_write
, METH_VARARGS
},
308 { "ibufcount", (PyCFunction
)sad_ibufcount
, METH_NOARGS
},
309 { "obufcount", (PyCFunction
)sad_obufcount
, METH_NOARGS
},
310 #define CTL_METHODS 4
311 { "getinfo", (PyCFunction
)sad_getinfo
, METH_NOARGS
},
312 { "setinfo", (PyCFunction
)sad_setinfo
, METH_O
},
313 { "drain", (PyCFunction
)sad_drain
, METH_NOARGS
},
314 { "flush", (PyCFunction
)sad_flush
, METH_NOARGS
},
316 { "getdev", (PyCFunction
)sad_getdev
, METH_NOARGS
},
318 { "close", (PyCFunction
)sad_close
, METH_NOARGS
},
319 { "fileno", (PyCFunction
)sad_fileno
, METH_NOARGS
},
320 {NULL
, NULL
} /* sentinel */
324 sad_getattr(sadobject
*xp
, char *name
)
327 return Py_FindMethod(sad_methods
+CTL_METHODS
,
328 (PyObject
*)xp
, name
);
330 return Py_FindMethod(sad_methods
, (PyObject
*)xp
, name
);
333 /* ----------------------------------------------------------------- */
335 static sadstatusobject
*
337 return PyObject_New(sadstatusobject
, &Sadstatustype
);
341 sads_dealloc(sadstatusobject
*xp
)
346 #define OFF(x) offsetof(audio_info_t,x)
347 static struct memberlist sads_ml
[] = {
348 { "i_sample_rate", T_UINT
, OFF(record
.sample_rate
) },
349 { "i_channels", T_UINT
, OFF(record
.channels
) },
350 { "i_precision", T_UINT
, OFF(record
.precision
) },
351 { "i_encoding", T_UINT
, OFF(record
.encoding
) },
352 { "i_gain", T_UINT
, OFF(record
.gain
) },
353 { "i_port", T_UINT
, OFF(record
.port
) },
354 { "i_samples", T_UINT
, OFF(record
.samples
) },
355 { "i_eof", T_UINT
, OFF(record
.eof
) },
356 { "i_pause", T_UBYTE
, OFF(record
.pause
) },
357 { "i_error", T_UBYTE
, OFF(record
.error
) },
358 { "i_waiting", T_UBYTE
, OFF(record
.waiting
) },
359 { "i_open", T_UBYTE
, OFF(record
.open
) , RO
},
360 { "i_active", T_UBYTE
, OFF(record
.active
) , RO
},
362 { "i_buffer_size", T_UINT
, OFF(record
.buffer_size
) },
363 { "i_balance", T_UBYTE
, OFF(record
.balance
) },
364 { "i_avail_ports", T_UINT
, OFF(record
.avail_ports
) },
367 { "o_sample_rate", T_UINT
, OFF(play
.sample_rate
) },
368 { "o_channels", T_UINT
, OFF(play
.channels
) },
369 { "o_precision", T_UINT
, OFF(play
.precision
) },
370 { "o_encoding", T_UINT
, OFF(play
.encoding
) },
371 { "o_gain", T_UINT
, OFF(play
.gain
) },
372 { "o_port", T_UINT
, OFF(play
.port
) },
373 { "o_samples", T_UINT
, OFF(play
.samples
) },
374 { "o_eof", T_UINT
, OFF(play
.eof
) },
375 { "o_pause", T_UBYTE
, OFF(play
.pause
) },
376 { "o_error", T_UBYTE
, OFF(play
.error
) },
377 { "o_waiting", T_UBYTE
, OFF(play
.waiting
) },
378 { "o_open", T_UBYTE
, OFF(play
.open
) , RO
},
379 { "o_active", T_UBYTE
, OFF(play
.active
) , RO
},
381 { "o_buffer_size", T_UINT
, OFF(play
.buffer_size
) },
382 { "o_balance", T_UBYTE
, OFF(play
.balance
) },
383 { "o_avail_ports", T_UINT
, OFF(play
.avail_ports
) },
386 { "monitor_gain", T_UINT
, OFF(monitor_gain
) },
391 sads_getattr(sadstatusobject
*xp
, char *name
)
393 return PyMember_Get((char *)&xp
->ai
, sads_ml
, name
);
397 sads_setattr(sadstatusobject
*xp
, char *name
, PyObject
*v
)
401 PyErr_SetString(PyExc_TypeError
,
402 "can't delete sun audio status attributes");
405 return PyMember_Set((char *)&xp
->ai
, sads_ml
, name
, v
);
408 /* ------------------------------------------------------------------- */
411 static PyTypeObject Sadtype
= {
412 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
413 "sunaudiodev.sun_audio_device", /*tp_name*/
414 sizeof(sadobject
), /*tp_size*/
417 (destructor
)sad_dealloc
, /*tp_dealloc*/
419 (getattrfunc
)sad_getattr
, /*tp_getattr*/
425 static PyTypeObject Sadstatustype
= {
426 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
427 "sunaudiodev.sun_audio_device_status", /*tp_name*/
428 sizeof(sadstatusobject
), /*tp_size*/
431 (destructor
)sads_dealloc
, /*tp_dealloc*/
433 (getattrfunc
)sads_getattr
, /*tp_getattr*/
434 (setattrfunc
)sads_setattr
, /*tp_setattr*/
438 /* ------------------------------------------------------------------- */
441 sadopen(PyObject
*self
, PyObject
*args
)
443 return (PyObject
*)newsadobject(args
);
446 static PyMethodDef sunaudiodev_methods
[] = {
447 { "open", sadopen
, METH_VARARGS
},
452 initsunaudiodev(void)
456 m
= Py_InitModule("sunaudiodev", sunaudiodev_methods
);
459 d
= PyModule_GetDict(m
);
460 SunAudioError
= PyErr_NewException("sunaudiodev.error", NULL
, NULL
);
462 PyDict_SetItemString(d
, "error", SunAudioError
);