blob: 506ee33c59a58b46502e635cffb7866e99c9deef [file] [log] [blame]
Guido van Rossum9c52e0f1992-06-03 17:06:14 +00001
2/* Sad objects */
3
Barry Warsaw8496d391996-12-13 00:11:04 +00004#include "Python.h"
Guido van Rossum9c52e0f1992-06-03 17:06:14 +00005#include "structmember.h"
6
Guido van Rossumb6775db1994-08-01 11:34:53 +00007#ifdef HAVE_SYS_AUDIOIO_H
8#define SOLARIS
9#endif
10
Guido van Rossume29ed8f1996-12-10 15:21:30 +000011#ifdef HAVE_FCNTL_H
12#include <fcntl.h>
13#endif
14
Guido van Rossum234f9421993-06-17 12:35:49 +000015#include <stropts.h>
Guido van Rossum9c52e0f1992-06-03 17:06:14 +000016#include <sys/ioctl.h>
Sjoerd Mullender66bca321993-12-03 16:54:45 +000017#ifdef SOLARIS
Guido van Rossumb6775db1994-08-01 11:34:53 +000018#include <sys/audioio.h>
Sjoerd Mullender66bca321993-12-03 16:54:45 +000019#else
Guido van Rossum9c52e0f1992-06-03 17:06:14 +000020#include <sun/audioio.h>
Sjoerd Mullender66bca321993-12-03 16:54:45 +000021#endif
Guido van Rossum9c52e0f1992-06-03 17:06:14 +000022
23/* #define offsetof(str,mem) ((int)(((str *)0)->mem)) */
24
25typedef struct {
Barry Warsaw8496d391996-12-13 00:11:04 +000026 PyObject_HEAD
Guido van Rossum9c52e0f1992-06-03 17:06:14 +000027 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 */
31
32} sadobject;
33
34typedef struct {
Barry Warsaw8496d391996-12-13 00:11:04 +000035 PyObject_HEAD
Guido van Rossum9c52e0f1992-06-03 17:06:14 +000036 audio_info_t ai;
37} sadstatusobject;
38
Barry Warsaw8496d391996-12-13 00:11:04 +000039staticforward PyTypeObject Sadtype;
40staticforward PyTypeObject Sadstatustype;
Thomas Wouters58d05102000-07-24 14:43:35 +000041static sadstatusobject *sads_alloc(void); /* Forward */
Guido van Rossum9c52e0f1992-06-03 17:06:14 +000042
Barry Warsaw8496d391996-12-13 00:11:04 +000043static PyObject *SunAudioError;
Guido van Rossum9c52e0f1992-06-03 17:06:14 +000044
Guido van Rossum9c52e0f1992-06-03 17:06:14 +000045#define is_sadobject(v) ((v)->ob_type == &Sadtype)
46#define is_sadstatusobject(v) ((v)->ob_type == &Sadstatustype)
47
Barry Warsawb0d1b061998-10-31 22:52:54 +000048
Guido van Rossum9c52e0f1992-06-03 17:06:14 +000049static sadobject *
Peter Schneider-Kamp3e3175b2000-07-10 17:25:37 +000050newsadobject(PyObject *args)
Guido van Rossum9c52e0f1992-06-03 17:06:14 +000051{
52 sadobject *xp;
53 int fd;
54 char *mode;
55 int imode;
Barry Warsawb0d1b061998-10-31 22:52:54 +000056 char* basedev;
57 char* ctldev;
58 char* opendev;
Guido van Rossum9c52e0f1992-06-03 17:06:14 +000059
60 /* Check arg for r/w/rw */
Neal Norwitzba3a16c2002-03-31 15:27:00 +000061 if (!PyArg_ParseTuple(args, "s", &mode))
Barry Warsaw8496d391996-12-13 00:11:04 +000062 return NULL;
63 if (strcmp(mode, "r") == 0)
64 imode = 0;
65 else if (strcmp(mode, "w") == 0)
66 imode = 1;
67 else if (strcmp(mode, "rw") == 0)
68 imode = 2;
69 else if (strcmp(mode, "control") == 0)
70 imode = -1;
Guido van Rossum9c52e0f1992-06-03 17:06:14 +000071 else {
Barry Warsaw8496d391996-12-13 00:11:04 +000072 PyErr_SetString(SunAudioError,
73 "Mode should be one of 'r', 'w', 'rw' or 'control'");
74 return NULL;
Guido van Rossum9c52e0f1992-06-03 17:06:14 +000075 }
76
Barry Warsawb0d1b061998-10-31 22:52:54 +000077 /* 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.
80 */
81 basedev = getenv("AUDIODEV");
82 if (!basedev)
83 basedev = "/dev/audio";
84 ctldev = PyMem_NEW(char, strlen(basedev) + 4);
85 if (!ctldev) {
86 PyErr_NoMemory();
Barry Warsaw8496d391996-12-13 00:11:04 +000087 return NULL;
Guido van Rossum9c52e0f1992-06-03 17:06:14 +000088 }
Barry Warsawb0d1b061998-10-31 22:52:54 +000089 strcpy(ctldev, basedev);
90 strcat(ctldev, "ctl");
91
92 if (imode < 0) {
93 opendev = ctldev;
94 fd = open(ctldev, 2);
95 }
96 else {
97 opendev = basedev;
98 fd = open(basedev, imode);
99 }
100 if (fd < 0) {
101 PyErr_SetFromErrnoWithFilename(SunAudioError, opendev);
Fred Drake03459a52001-11-09 16:00:41 +0000102 PyMem_DEL(ctldev);
Barry Warsawb0d1b061998-10-31 22:52:54 +0000103 return NULL;
104 }
105 PyMem_DEL(ctldev);
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000106
107 /* Create and initialize the object */
Guido van Rossumb18618d2000-05-03 23:44:39 +0000108 xp = PyObject_New(sadobject, &Sadtype);
Barry Warsaw42185a21997-01-09 22:29:57 +0000109 if (xp == NULL) {
110 close(fd);
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000111 return NULL;
Barry Warsaw42185a21997-01-09 22:29:57 +0000112 }
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000113 xp->x_fd = fd;
114 xp->x_icount = xp->x_ocount = 0;
115 xp->x_isctl = (imode < 0);
116
117 return xp;
118}
119
120/* Sad methods */
121
122static void
Peter Schneider-Kamp3e3175b2000-07-10 17:25:37 +0000123sad_dealloc(sadobject *xp)
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000124{
125 close(xp->x_fd);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000126 PyObject_Del(xp);
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000127}
128
Barry Warsaw8496d391996-12-13 00:11:04 +0000129static PyObject *
Peter Schneider-Kamp3e3175b2000-07-10 17:25:37 +0000130sad_read(sadobject *self, PyObject *args)
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000131{
132 int size, count;
133 char *cp;
Barry Warsaw8496d391996-12-13 00:11:04 +0000134 PyObject *rv;
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000135
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000136 if (!PyArg_ParseTuple(args, "i:read", &size))
Barry Warsaw8496d391996-12-13 00:11:04 +0000137 return NULL;
138 rv = PyString_FromStringAndSize(NULL, size);
139 if (rv == NULL)
140 return NULL;
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000141
Barry Warsaw8496d391996-12-13 00:11:04 +0000142 if (!(cp = PyString_AsString(rv)))
143 goto finally;
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000144
145 count = read(self->x_fd, cp, size);
Barry Warsaw8496d391996-12-13 00:11:04 +0000146 if (count < 0) {
147 PyErr_SetFromErrno(SunAudioError);
148 goto finally;
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000149 }
Barry Warsaw8496d391996-12-13 00:11:04 +0000150#if 0
151 /* TBD: why print this message if you can handle the condition?
Barry Warsaw42185a21997-01-09 22:29:57 +0000152 * 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!
154 */
Barry Warsaw8496d391996-12-13 00:11:04 +0000155 if (count != size)
156 printf("sunaudio: funny read rv %d wtd %d\n", count, size);
157#endif
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000158 self->x_icount += count;
159 return rv;
Barry Warsaw8496d391996-12-13 00:11:04 +0000160
161 finally:
162 Py_DECREF(rv);
163 return NULL;
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000164}
165
Barry Warsaw8496d391996-12-13 00:11:04 +0000166static PyObject *
Peter Schneider-Kamp3e3175b2000-07-10 17:25:37 +0000167sad_write(sadobject *self, PyObject *args)
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000168{
169 char *cp;
170 int count, size;
171
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000172 if (!PyArg_ParseTuple(args, "s#:write", &cp, &size))
Barry Warsaw8496d391996-12-13 00:11:04 +0000173 return NULL;
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000174
175 count = write(self->x_fd, cp, size);
Barry Warsaw8496d391996-12-13 00:11:04 +0000176 if (count < 0) {
177 PyErr_SetFromErrno(SunAudioError);
178 return NULL;
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000179 }
Barry Warsaw8496d391996-12-13 00:11:04 +0000180#if 0
181 if (count != size)
182 printf("sunaudio: funny write rv %d wanted %d\n", count, size);
183#endif
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000184 self->x_ocount += count;
185
Barry Warsaw8496d391996-12-13 00:11:04 +0000186 Py_INCREF(Py_None);
187 return Py_None;
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000188}
189
Barry Warsaw8496d391996-12-13 00:11:04 +0000190static PyObject *
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000191sad_getinfo(sadobject *self)
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000192{
193 sadstatusobject *rv;
194
Barry Warsaw42185a21997-01-09 22:29:57 +0000195 if (!(rv = sads_alloc()))
196 return NULL;
197
Barry Warsaw8496d391996-12-13 00:11:04 +0000198 if (ioctl(self->x_fd, AUDIO_GETINFO, &rv->ai) < 0) {
199 PyErr_SetFromErrno(SunAudioError);
200 Py_DECREF(rv);
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000201 return NULL;
202 }
Barry Warsaw8496d391996-12-13 00:11:04 +0000203 return (PyObject *)rv;
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000204}
205
Barry Warsaw8496d391996-12-13 00:11:04 +0000206static PyObject *
Peter Schneider-Kamp3e3175b2000-07-10 17:25:37 +0000207sad_setinfo(sadobject *self, sadstatusobject *arg)
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000208{
Barry Warsaw8496d391996-12-13 00:11:04 +0000209 if (!is_sadstatusobject(arg)) {
210 PyErr_SetString(PyExc_TypeError,
211 "Must be sun audio status object");
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000212 return NULL;
213 }
Barry Warsaw8496d391996-12-13 00:11:04 +0000214 if (ioctl(self->x_fd, AUDIO_SETINFO, &arg->ai) < 0) {
215 PyErr_SetFromErrno(SunAudioError);
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000216 return NULL;
217 }
Barry Warsaw8496d391996-12-13 00:11:04 +0000218 Py_INCREF(Py_None);
219 return Py_None;
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000220}
221
Barry Warsaw8496d391996-12-13 00:11:04 +0000222static PyObject *
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000223sad_ibufcount(sadobject *self)
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000224{
Barry Warsaw8496d391996-12-13 00:11:04 +0000225 audio_info_t ai;
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000226
Barry Warsaw8496d391996-12-13 00:11:04 +0000227 if (ioctl(self->x_fd, AUDIO_GETINFO, &ai) < 0) {
228 PyErr_SetFromErrno(SunAudioError);
229 return NULL;
230 }
231 return PyInt_FromLong(ai.record.samples - self->x_icount);
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000232}
233
Barry Warsaw8496d391996-12-13 00:11:04 +0000234static PyObject *
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000235sad_obufcount(sadobject *self)
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000236{
Barry Warsaw8496d391996-12-13 00:11:04 +0000237 audio_info_t ai;
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000238
Barry Warsaw8496d391996-12-13 00:11:04 +0000239 if (ioctl(self->x_fd, AUDIO_GETINFO, &ai) < 0) {
240 PyErr_SetFromErrno(SunAudioError);
241 return NULL;
242 }
Thomas Wouters7e474022000-07-16 12:04:32 +0000243 /* x_ocount is in bytes, whereas play.samples is in frames */
Guido van Rossum1e0b19e1997-04-11 15:23:52 +0000244 /* we want frames */
245 return PyInt_FromLong(self->x_ocount / (ai.play.channels *
246 ai.play.precision / 8) -
247 ai.play.samples);
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000248}
249
Barry Warsaw8496d391996-12-13 00:11:04 +0000250static PyObject *
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000251sad_drain(sadobject *self)
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000252{
Barry Warsaw8496d391996-12-13 00:11:04 +0000253 if (ioctl(self->x_fd, AUDIO_DRAIN, 0) < 0) {
254 PyErr_SetFromErrno(SunAudioError);
255 return NULL;
256 }
257 Py_INCREF(Py_None);
258 return Py_None;
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000259}
260
Sjoerd Mullender66bca321993-12-03 16:54:45 +0000261#ifdef SOLARIS
Barry Warsaw8496d391996-12-13 00:11:04 +0000262static PyObject *
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000263sad_getdev(sadobject *self)
Sjoerd Mullender66bca321993-12-03 16:54:45 +0000264{
Barry Warsaw8496d391996-12-13 00:11:04 +0000265 struct audio_device ad;
Sjoerd Mullender66bca321993-12-03 16:54:45 +0000266
Barry Warsaw8496d391996-12-13 00:11:04 +0000267 if (ioctl(self->x_fd, AUDIO_GETDEV, &ad) < 0) {
268 PyErr_SetFromErrno(SunAudioError);
269 return NULL;
270 }
271 return Py_BuildValue("(sss)", ad.name, ad.version, ad.config);
Sjoerd Mullender66bca321993-12-03 16:54:45 +0000272}
273#endif
274
Barry Warsaw8496d391996-12-13 00:11:04 +0000275static PyObject *
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000276sad_flush(sadobject *self)
Guido van Rossum234f9421993-06-17 12:35:49 +0000277{
Barry Warsaw8496d391996-12-13 00:11:04 +0000278 if (ioctl(self->x_fd, I_FLUSH, FLUSHW) < 0) {
279 PyErr_SetFromErrno(SunAudioError);
280 return NULL;
281 }
282 Py_INCREF(Py_None);
283 return Py_None;
Guido van Rossum234f9421993-06-17 12:35:49 +0000284}
285
Barry Warsaw8496d391996-12-13 00:11:04 +0000286static PyObject *
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000287sad_close(sadobject *self)
Guido van Rossum234f9421993-06-17 12:35:49 +0000288{
289
Barry Warsaw8496d391996-12-13 00:11:04 +0000290 if (self->x_fd >= 0) {
291 close(self->x_fd);
292 self->x_fd = -1;
293 }
294 Py_INCREF(Py_None);
295 return Py_None;
Guido van Rossum234f9421993-06-17 12:35:49 +0000296}
297
Barry Warsawb0d1b061998-10-31 22:52:54 +0000298static PyObject *
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000299sad_fileno(sadobject *self)
Barry Warsawb0d1b061998-10-31 22:52:54 +0000300{
Barry Warsawb0d1b061998-10-31 22:52:54 +0000301 return PyInt_FromLong(self->x_fd);
302}
303
304
Barry Warsaw8496d391996-12-13 00:11:04 +0000305static PyMethodDef sad_methods[] = {
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000306 { "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 },
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000310#define CTL_METHODS 4
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000311 { "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 },
Sjoerd Mullender66bca321993-12-03 16:54:45 +0000315#ifdef SOLARIS
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000316 { "getdev", (PyCFunction)sad_getdev, METH_NOARGS },
Sjoerd Mullender66bca321993-12-03 16:54:45 +0000317#endif
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000318 { "close", (PyCFunction)sad_close, METH_NOARGS },
319 { "fileno", (PyCFunction)sad_fileno, METH_NOARGS },
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000320 {NULL, NULL} /* sentinel */
321};
322
Barry Warsaw8496d391996-12-13 00:11:04 +0000323static PyObject *
Peter Schneider-Kamp3e3175b2000-07-10 17:25:37 +0000324sad_getattr(sadobject *xp, char *name)
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000325{
Barry Warsaw8496d391996-12-13 00:11:04 +0000326 if (xp->x_isctl)
327 return Py_FindMethod(sad_methods+CTL_METHODS,
328 (PyObject *)xp, name);
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000329 else
Barry Warsaw8496d391996-12-13 00:11:04 +0000330 return Py_FindMethod(sad_methods, (PyObject *)xp, name);
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000331}
332
333/* ----------------------------------------------------------------- */
334
335static sadstatusobject *
Thomas Wouters58d05102000-07-24 14:43:35 +0000336sads_alloc(void) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000337 return PyObject_New(sadstatusobject, &Sadstatustype);
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000338}
339
340static void
Peter Schneider-Kamp3e3175b2000-07-10 17:25:37 +0000341sads_dealloc(sadstatusobject *xp)
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000342{
Barry Warsaw8496d391996-12-13 00:11:04 +0000343 PyMem_DEL(xp);
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000344}
345
346#define OFF(x) offsetof(audio_info_t,x)
347static 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},
Sjoerd Mullender66bca321993-12-03 16:54:45 +0000361#ifdef SOLARIS
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) },
365#endif
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000366
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},
Sjoerd Mullender66bca321993-12-03 16:54:45 +0000380#ifdef SOLARIS
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) },
384#endif
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000385
386 { "monitor_gain", T_UINT, OFF(monitor_gain) },
387 { NULL, 0, 0},
388};
389
Barry Warsaw8496d391996-12-13 00:11:04 +0000390static PyObject *
Peter Schneider-Kamp3e3175b2000-07-10 17:25:37 +0000391sads_getattr(sadstatusobject *xp, char *name)
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000392{
Barry Warsaw8496d391996-12-13 00:11:04 +0000393 return PyMember_Get((char *)&xp->ai, sads_ml, name);
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000394}
395
396static int
Peter Schneider-Kamp3e3175b2000-07-10 17:25:37 +0000397sads_setattr(sadstatusobject *xp, char *name, PyObject *v)
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000398{
Guido van Rossum94472a01992-09-04 09:45:18 +0000399
400 if (v == NULL) {
Barry Warsaw8496d391996-12-13 00:11:04 +0000401 PyErr_SetString(PyExc_TypeError,
402 "can't delete sun audio status attributes");
Guido van Rossumf156f501996-09-11 23:31:23 +0000403 return -1;
Guido van Rossum94472a01992-09-04 09:45:18 +0000404 }
Barry Warsaw8496d391996-12-13 00:11:04 +0000405 return PyMember_Set((char *)&xp->ai, sads_ml, name, v);
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000406}
407
408/* ------------------------------------------------------------------- */
409
410
Barry Warsaw8496d391996-12-13 00:11:04 +0000411static PyTypeObject Sadtype = {
412 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000413 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +0000414 "sunaudiodev.sun_audio_device", /*tp_name*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000415 sizeof(sadobject), /*tp_size*/
416 0, /*tp_itemsize*/
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000417 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000418 (destructor)sad_dealloc, /*tp_dealloc*/
419 0, /*tp_print*/
420 (getattrfunc)sad_getattr, /*tp_getattr*/
421 0, /*tp_setattr*/
422 0, /*tp_compare*/
423 0, /*tp_repr*/
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000424};
425
Barry Warsaw8496d391996-12-13 00:11:04 +0000426static PyTypeObject Sadstatustype = {
427 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000428 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +0000429 "sunaudiodev.sun_audio_device_status", /*tp_name*/
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000430 sizeof(sadstatusobject), /*tp_size*/
431 0, /*tp_itemsize*/
432 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000433 (destructor)sads_dealloc, /*tp_dealloc*/
434 0, /*tp_print*/
435 (getattrfunc)sads_getattr, /*tp_getattr*/
436 (setattrfunc)sads_setattr, /*tp_setattr*/
437 0, /*tp_compare*/
438 0, /*tp_repr*/
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000439};
440/* ------------------------------------------------------------------- */
441
Barry Warsaw8496d391996-12-13 00:11:04 +0000442static PyObject *
Peter Schneider-Kamp3e3175b2000-07-10 17:25:37 +0000443sadopen(PyObject *self, PyObject *args)
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000444{
Barry Warsaw8496d391996-12-13 00:11:04 +0000445 return (PyObject *)newsadobject(args);
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000446}
447
Barry Warsaw8496d391996-12-13 00:11:04 +0000448static PyMethodDef sunaudiodev_methods[] = {
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000449 { "open", sadopen, METH_VARARGS },
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000450 { 0, 0 },
451};
452
453void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000454initsunaudiodev(void)
Barry Warsaw8496d391996-12-13 00:11:04 +0000455{
456 PyObject *m, *d;
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000457
Barry Warsaw8496d391996-12-13 00:11:04 +0000458 m = Py_InitModule("sunaudiodev", sunaudiodev_methods);
459 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000460 SunAudioError = PyErr_NewException("sunaudiodev.error", NULL, NULL);
Barry Warsaw42185a21997-01-09 22:29:57 +0000461 if (SunAudioError)
462 PyDict_SetItemString(d, "error", SunAudioError);
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000463}