blob: 077784088d1c5d90fef10b1b21cc0e7a05ec6046 [file] [log] [blame]
Greg Ward04613a92002-11-30 22:47:45 +00001/*
2 * ossaudiodev -- Python interface to the OSS (Open Sound System) API.
3 * This is the standard audio API for Linux and some
4 * flavours of BSD [XXX which ones?]; it is also available
5 * for a wide range of commercial Unices.
6 *
7 * Originally written by Peter Bosch, March 2000, as linuxaudiodev.
8 *
9 * Renamed to ossaudiodev and rearranged/revised/hacked up
10 * by Greg Ward <gward@python.net>, November 2002.
Greg Ward3d9994d2002-12-11 15:12:01 +000011 * Mixer interface by Nicholas FitzRoy-Dale <wzdd@lardcave.net>, Dec 2002.
Greg Ward04613a92002-11-30 22:47:45 +000012 *
13 * (c) 2000 Peter Bosch. All Rights Reserved.
14 * (c) 2002 Gregory P. Ward. All Rights Reserved.
15 * (c) 2002 Python Software Foundation. All Rights Reserved.
16 *
17 * XXX need a license statement
18 *
19 * $Id$
20 */
21
22#include "Python.h"
23#include "structmember.h"
24
25#ifdef HAVE_FCNTL_H
26#include <fcntl.h>
27#else
28#define O_RDONLY 00
29#define O_WRONLY 01
30#endif
31
Greg Ward04613a92002-11-30 22:47:45 +000032#include <sys/ioctl.h>
Greg Ward0b6dfb82003-03-10 03:17:06 +000033#include <sys/soundcard.h>
34
Greg Ward04613a92002-11-30 22:47:45 +000035#if defined(linux)
Greg Ward04613a92002-11-30 22:47:45 +000036
37typedef unsigned long uint32_t;
38
39#elif defined(__FreeBSD__)
Greg Ward04613a92002-11-30 22:47:45 +000040
Greg Ward0b6dfb82003-03-10 03:17:06 +000041# ifndef SNDCTL_DSP_CHANNELS
42# define SNDCTL_DSP_CHANNELS SOUND_PCM_WRITE_CHANNELS
43# endif
Greg Ward04613a92002-11-30 22:47:45 +000044
45#endif
46
47typedef struct {
48 PyObject_HEAD;
Greg Ward7b43c682002-12-30 02:29:28 +000049 int fd; /* The open file */
50 int mode; /* file mode */
51 int icount; /* Input count */
52 int ocount; /* Output count */
53 uint32_t afmts; /* Audio formats supported by hardware */
Greg Ward499b73e2002-12-31 03:04:52 +000054} oss_audio_t;
Greg Ward04613a92002-11-30 22:47:45 +000055
Greg Ward3d9994d2002-12-11 15:12:01 +000056typedef struct {
57 PyObject_HEAD;
Greg Wardad4d9b92002-12-30 03:02:22 +000058 int fd; /* The open mixer device */
Greg Ward3d9994d2002-12-11 15:12:01 +000059} oss_mixer_t;
60
Greg Ward04613a92002-11-30 22:47:45 +000061/* XXX several format defined in soundcard.h are not supported,
62 including _NE (native endian) options and S32 options
63*/
64
65static struct {
Greg Wardad4d9b92002-12-30 03:02:22 +000066 int a_bps;
67 uint32_t a_fmt;
Greg Ward04613a92002-11-30 22:47:45 +000068 char *a_name;
69} audio_types[] = {
Greg Wardad4d9b92002-12-30 03:02:22 +000070 { 8, AFMT_MU_LAW, "logarithmic mu-law 8-bit audio" },
71 { 8, AFMT_A_LAW, "logarithmic A-law 8-bit audio" },
72 { 8, AFMT_U8, "linear unsigned 8-bit audio" },
73 { 8, AFMT_S8, "linear signed 8-bit audio" },
74 { 16, AFMT_U16_BE, "linear unsigned 16-bit big-endian audio" },
75 { 16, AFMT_U16_LE, "linear unsigned 16-bit little-endian audio" },
76 { 16, AFMT_S16_BE, "linear signed 16-bit big-endian audio" },
77 { 16, AFMT_S16_LE, "linear signed 16-bit little-endian audio" },
Neal Norwitzd156c2d2003-02-02 17:59:06 +000078#ifdef AFMT_S16_NE
Greg Wardad4d9b92002-12-30 03:02:22 +000079 { 16, AFMT_S16_NE, "linear signed 16-bit native-endian audio" },
Neal Norwitzd156c2d2003-02-02 17:59:06 +000080#endif
Greg Ward04613a92002-11-30 22:47:45 +000081};
82
83static int n_audio_types = sizeof(audio_types) / sizeof(audio_types[0]);
84
Greg Ward499b73e2002-12-31 03:04:52 +000085static PyTypeObject OSSAudioType;
Greg Ward3d9994d2002-12-11 15:12:01 +000086static PyTypeObject OSSMixerType;
Greg Ward04613a92002-11-30 22:47:45 +000087
Greg Ward97708bc2002-11-30 23:17:10 +000088static PyObject *OSSAudioError;
Greg Ward04613a92002-11-30 22:47:45 +000089
Greg Wardda1cacb2002-12-31 03:02:23 +000090
91/* ----------------------------------------------------------------------
92 * DSP object initialization/deallocation
93 */
94
Greg Ward499b73e2002-12-31 03:04:52 +000095static oss_audio_t *
Greg Ward8c6b6a92002-12-11 14:43:13 +000096newossobject(PyObject *arg)
Greg Ward04613a92002-11-30 22:47:45 +000097{
Greg Ward58ae13c2002-12-31 03:07:21 +000098 oss_audio_t *self;
Greg Ward04613a92002-11-30 22:47:45 +000099 int fd, afmts, imode;
100 char *basedev = NULL;
101 char *mode = NULL;
102
Greg Ward9a568eb2002-11-30 23:20:09 +0000103 /* Two ways to call open():
Greg Ward04613a92002-11-30 22:47:45 +0000104 open(device, mode) (for consistency with builtin open())
105 open(mode) (for backwards compatibility)
106 because the *first* argument is optional, parsing args is
107 a wee bit tricky. */
108 if (!PyArg_ParseTuple(arg, "s|s:open", &basedev, &mode))
109 return NULL;
110 if (mode == NULL) { /* only one arg supplied */
111 mode = basedev;
112 basedev = NULL;
113 }
114
115 if (strcmp(mode, "r") == 0)
116 imode = O_RDONLY;
117 else if (strcmp(mode, "w") == 0)
118 imode = O_WRONLY;
Greg Ward1e0f57d2002-11-30 23:05:26 +0000119 else if (strcmp(mode, "rw") == 0)
120 imode = O_RDWR;
Greg Ward04613a92002-11-30 22:47:45 +0000121 else {
Greg Ward97708bc2002-11-30 23:17:10 +0000122 PyErr_SetString(OSSAudioError, "mode must be 'r', 'w', or 'rw'");
Greg Ward04613a92002-11-30 22:47:45 +0000123 return NULL;
124 }
125
Greg Ward5c5c5772002-12-30 02:58:04 +0000126 /* Open the correct device: either the 'device' argument,
127 or the AUDIODEV environment variable, or "/dev/dsp". */
Greg Ward04613a92002-11-30 22:47:45 +0000128 if (basedev == NULL) { /* called with one arg */
129 basedev = getenv("AUDIODEV");
130 if (basedev == NULL) /* $AUDIODEV not set */
131 basedev = "/dev/dsp";
132 }
133
Greg Ward5c49ef22003-03-11 16:53:13 +0000134 /* Open with O_NONBLOCK to avoid hanging on devices that only allow
135 one open at a time. This does *not* affect later I/O; OSS
136 provides a special ioctl() for non-blocking read/write, which is
137 exposed via oss_nonblock() below. */
138 if ((fd = open(basedev, imode|O_NONBLOCK)) == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000139 PyErr_SetFromErrnoWithFilename(PyExc_IOError, basedev);
Greg Ward04613a92002-11-30 22:47:45 +0000140 return NULL;
141 }
Greg Ward04613a92002-11-30 22:47:45 +0000142 if (ioctl(fd, SNDCTL_DSP_GETFMTS, &afmts) == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000143 PyErr_SetFromErrnoWithFilename(PyExc_IOError, basedev);
Greg Ward04613a92002-11-30 22:47:45 +0000144 return NULL;
145 }
146 /* Create and initialize the object */
Greg Ward58ae13c2002-12-31 03:07:21 +0000147 if ((self = PyObject_New(oss_audio_t, &OSSAudioType)) == NULL) {
Greg Ward04613a92002-11-30 22:47:45 +0000148 close(fd);
149 return NULL;
150 }
Greg Ward58ae13c2002-12-31 03:07:21 +0000151 self->fd = fd;
152 self->mode = imode;
153 self->icount = self->ocount = 0;
154 self->afmts = afmts;
155 return self;
Greg Ward04613a92002-11-30 22:47:45 +0000156}
157
158static void
Greg Ward58ae13c2002-12-31 03:07:21 +0000159oss_dealloc(oss_audio_t *self)
Greg Ward04613a92002-11-30 22:47:45 +0000160{
161 /* if already closed, don't reclose it */
Greg Ward58ae13c2002-12-31 03:07:21 +0000162 if (self->fd != -1)
163 close(self->fd);
164 PyObject_Del(self);
Greg Ward04613a92002-11-30 22:47:45 +0000165}
166
Greg Wardda1cacb2002-12-31 03:02:23 +0000167
168/* ----------------------------------------------------------------------
169 * Mixer object initialization/deallocation
170 */
171
Greg Ward3d9994d2002-12-11 15:12:01 +0000172static oss_mixer_t *
173newossmixerobject(PyObject *arg)
174{
Greg Ward0b6dfb82003-03-10 03:17:06 +0000175 char *basedev = NULL;
176 int fd;
Greg Ward58ae13c2002-12-31 03:07:21 +0000177 oss_mixer_t *self;
Greg Ward3d9994d2002-12-11 15:12:01 +0000178
Greg Ward0b6dfb82003-03-10 03:17:06 +0000179 if (!PyArg_ParseTuple(arg, "|s", &basedev)) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000180 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000181 }
182
183 if (basedev == NULL) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000184 basedev = getenv("MIXERDEV");
185 if (basedev == NULL) /* MIXERDEV not set */
186 basedev = "/dev/mixer";
Greg Ward3d9994d2002-12-11 15:12:01 +0000187 }
188
Greg Ward0b6dfb82003-03-10 03:17:06 +0000189 if ((fd = open(basedev, O_RDWR)) == -1) {
Greg Ward3d9994d2002-12-11 15:12:01 +0000190 PyErr_SetFromErrnoWithFilename(PyExc_IOError, basedev);
191 return NULL;
192 }
Greg Ward0b6dfb82003-03-10 03:17:06 +0000193
Greg Ward58ae13c2002-12-31 03:07:21 +0000194 if ((self = PyObject_New(oss_mixer_t, &OSSMixerType)) == NULL) {
Greg Ward3d9994d2002-12-11 15:12:01 +0000195 close(fd);
196 return NULL;
197 }
198
Greg Ward58ae13c2002-12-31 03:07:21 +0000199 self->fd = fd;
Greg Ward3d9994d2002-12-11 15:12:01 +0000200
Greg Ward58ae13c2002-12-31 03:07:21 +0000201 return self;
Greg Ward3d9994d2002-12-11 15:12:01 +0000202}
203
204static void
Greg Ward58ae13c2002-12-31 03:07:21 +0000205oss_mixer_dealloc(oss_mixer_t *self)
Greg Ward3d9994d2002-12-11 15:12:01 +0000206{
207 /* if already closed, don't reclose it */
Greg Ward58ae13c2002-12-31 03:07:21 +0000208 if (self->fd != -1)
209 close(self->fd);
210 PyObject_Del(self);
Greg Ward3d9994d2002-12-11 15:12:01 +0000211}
212
Greg Ward131bce02002-11-30 22:56:44 +0000213
214/* Methods to wrap the OSS ioctls. The calling convention is pretty
215 simple:
216 nonblock() -> ioctl(fd, SNDCTL_DSP_NONBLOCK)
217 fmt = setfmt(fmt) -> ioctl(fd, SNDCTL_DSP_SETFMT, &fmt)
218 etc.
219*/
220
221
222/* _do_ioctl_1() is a private helper function used for the OSS ioctls --
223 SNDCTL_DSP_{SETFMT,CHANNELS,SPEED} -- that that are called from C
224 like this:
225 ioctl(fd, SNDCTL_DSP_cmd, &arg)
226
227 where arg is the value to set, and on return the driver sets arg to
228 the value that was actually set. Mapping this to Python is obvious:
229 arg = dsp.xxx(arg)
230*/
231static PyObject *
Greg Wardda9f8532002-12-11 14:49:59 +0000232_do_ioctl_1(int fd, PyObject *args, char *fname, int cmd)
Greg Ward131bce02002-11-30 22:56:44 +0000233{
Greg Wardda9f8532002-12-11 14:49:59 +0000234 char argfmt[33] = "i:";
Greg Ward131bce02002-11-30 22:56:44 +0000235 int arg;
236
Greg Wardda9f8532002-12-11 14:49:59 +0000237 assert(strlen(fname) <= 30);
Greg Ward131bce02002-11-30 22:56:44 +0000238 strcat(argfmt, fname);
239 if (!PyArg_ParseTuple(args, argfmt, &arg))
Greg Wardad4d9b92002-12-30 03:02:22 +0000240 return NULL;
Greg Ward131bce02002-11-30 22:56:44 +0000241
Greg Wardda9f8532002-12-11 14:49:59 +0000242 if (ioctl(fd, cmd, &arg) == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000243 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000244 return PyInt_FromLong(arg);
245}
246
Greg Wardda1cacb2002-12-31 03:02:23 +0000247
248/* ----------------------------------------------------------------------
249 * Helper functions
250 */
251
Greg Ward3d9994d2002-12-11 15:12:01 +0000252/* _do_ioctl_1_internal() is a wrapper for ioctls that take no inputs
253 but return an output -- ie. we need to pass a pointer to a local C
254 variable so the driver can write its output there, but from Python
255 all we see is the return value. For example,
256 SOUND_MIXER_READ_DEVMASK returns a bitmask of available mixer
257 devices, but does not use the value of the parameter passed-in in any
258 way.
259*/
260
261static PyObject *
262_do_ioctl_1_internal(int fd, PyObject *args, char *fname, int cmd)
263{
264 char argfmt[32] = ":";
265 int arg = 0;
266
267 assert(strlen(fname) <= 30);
268 strcat(argfmt, fname);
269 if (!PyArg_ParseTuple(args, argfmt, &arg))
Greg Wardad4d9b92002-12-30 03:02:22 +0000270 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000271
272 if (ioctl(fd, cmd, &arg) == -1)
273 return PyErr_SetFromErrno(PyExc_IOError);
274 return PyInt_FromLong(arg);
275}
276
277
278
Greg Ward131bce02002-11-30 22:56:44 +0000279/* _do_ioctl_0() is a private helper for the no-argument ioctls:
280 SNDCTL_DSP_{SYNC,RESET,POST}. */
281static PyObject *
Greg Wardda9f8532002-12-11 14:49:59 +0000282_do_ioctl_0(int fd, PyObject *args, char *fname, int cmd)
Greg Ward131bce02002-11-30 22:56:44 +0000283{
Greg Wardda9f8532002-12-11 14:49:59 +0000284 char argfmt[32] = ":";
Greg Ward131bce02002-11-30 22:56:44 +0000285
Greg Wardda9f8532002-12-11 14:49:59 +0000286 assert(strlen(fname) <= 30);
Greg Ward131bce02002-11-30 22:56:44 +0000287 strcat(argfmt, fname);
288 if (!PyArg_ParseTuple(args, argfmt))
Greg Wardad4d9b92002-12-30 03:02:22 +0000289 return NULL;
Greg Ward131bce02002-11-30 22:56:44 +0000290
Greg Wardda9f8532002-12-11 14:49:59 +0000291 if (ioctl(fd, cmd, 0) == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000292 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000293 Py_INCREF(Py_None);
294 return Py_None;
295}
296
297
Greg Wardda1cacb2002-12-31 03:02:23 +0000298/* ----------------------------------------------------------------------
Greg Ward499b73e2002-12-31 03:04:52 +0000299 * Methods of DSP objects (OSSAudioType)
Greg Wardda1cacb2002-12-31 03:02:23 +0000300 */
301
Greg Ward131bce02002-11-30 22:56:44 +0000302static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000303oss_nonblock(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000304{
305 /* Hmmm: it doesn't appear to be possible to return to blocking
306 mode once we're in non-blocking mode! */
307 if (!PyArg_ParseTuple(args, ":nonblock"))
Greg Wardad4d9b92002-12-30 03:02:22 +0000308 return NULL;
Greg Ward7b43c682002-12-30 02:29:28 +0000309 if (ioctl(self->fd, SNDCTL_DSP_NONBLOCK, NULL) == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000310 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000311 Py_INCREF(Py_None);
312 return Py_None;
313}
314
315static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000316oss_setfmt(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000317{
Greg Ward7b43c682002-12-30 02:29:28 +0000318 return _do_ioctl_1(self->fd, args, "setfmt", SNDCTL_DSP_SETFMT);
Greg Ward131bce02002-11-30 22:56:44 +0000319}
320
321static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000322oss_getfmts(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000323{
324 int mask;
325 if (!PyArg_ParseTuple(args, ":getfmts"))
Greg Wardad4d9b92002-12-30 03:02:22 +0000326 return NULL;
Greg Ward7b43c682002-12-30 02:29:28 +0000327 if (ioctl(self->fd, SNDCTL_DSP_GETFMTS, &mask) == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000328 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000329 return PyInt_FromLong(mask);
330}
331
332static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000333oss_channels(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000334{
Greg Ward7b43c682002-12-30 02:29:28 +0000335 return _do_ioctl_1(self->fd, args, "channels", SNDCTL_DSP_CHANNELS);
Greg Ward131bce02002-11-30 22:56:44 +0000336}
337
338static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000339oss_speed(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000340{
Greg Ward7b43c682002-12-30 02:29:28 +0000341 return _do_ioctl_1(self->fd, args, "speed", SNDCTL_DSP_SPEED);
Greg Ward131bce02002-11-30 22:56:44 +0000342}
343
344static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000345oss_sync(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000346{
Greg Ward7b43c682002-12-30 02:29:28 +0000347 return _do_ioctl_0(self->fd, args, "sync", SNDCTL_DSP_SYNC);
Greg Ward131bce02002-11-30 22:56:44 +0000348}
349
350static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000351oss_reset(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000352{
Greg Ward7b43c682002-12-30 02:29:28 +0000353 return _do_ioctl_0(self->fd, args, "reset", SNDCTL_DSP_RESET);
Greg Ward131bce02002-11-30 22:56:44 +0000354}
355
356static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000357oss_post(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000358{
Greg Ward7b43c682002-12-30 02:29:28 +0000359 return _do_ioctl_0(self->fd, args, "post", SNDCTL_DSP_POST);
Greg Ward131bce02002-11-30 22:56:44 +0000360}
361
362
363/* Regular file methods: read(), write(), close(), etc. as well
364 as one convenience method, writeall(). */
365
Greg Ward04613a92002-11-30 22:47:45 +0000366static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000367oss_read(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000368{
369 int size, count;
370 char *cp;
371 PyObject *rv;
Greg Wardad4d9b92002-12-30 03:02:22 +0000372
Greg Ward04613a92002-11-30 22:47:45 +0000373 if (!PyArg_ParseTuple(args, "i:read", &size))
374 return NULL;
375 rv = PyString_FromStringAndSize(NULL, size);
376 if (rv == NULL)
377 return NULL;
378 cp = PyString_AS_STRING(rv);
Greg Ward7b43c682002-12-30 02:29:28 +0000379 if ((count = read(self->fd, cp, size)) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000380 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000381 Py_DECREF(rv);
382 return NULL;
383 }
Greg Ward7b43c682002-12-30 02:29:28 +0000384 self->icount += count;
Greg Ward04613a92002-11-30 22:47:45 +0000385 _PyString_Resize(&rv, count);
386 return rv;
387}
388
389static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000390oss_write(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000391{
392 char *cp;
393 int rv, size;
Greg Ward131bce02002-11-30 22:56:44 +0000394
395 if (!PyArg_ParseTuple(args, "s#:write", &cp, &size)) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000396 return NULL;
Greg Ward131bce02002-11-30 22:56:44 +0000397 }
Greg Ward7b43c682002-12-30 02:29:28 +0000398 if ((rv = write(self->fd, cp, size)) == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000399 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000400 } else {
Greg Ward7b43c682002-12-30 02:29:28 +0000401 self->ocount += rv;
Greg Ward131bce02002-11-30 22:56:44 +0000402 }
403 return PyInt_FromLong(rv);
404}
405
406static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000407oss_writeall(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000408{
409 char *cp;
410 int rv, size;
411 fd_set write_set_fds;
412 int select_rv;
413
414 /* NB. writeall() is only useful in non-blocking mode: according to
415 Guenter Geiger <geiger@xdv.org> on the linux-audio-dev list
416 (http://eca.cx/lad/2002/11/0380.html), OSS guarantees that
417 write() in blocking mode consumes the whole buffer. In blocking
418 mode, the behaviour of write() and writeall() from Python is
419 indistinguishable. */
420
421 if (!PyArg_ParseTuple(args, "s#:write", &cp, &size))
422 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000423
424 /* use select to wait for audio device to be available */
425 FD_ZERO(&write_set_fds);
Greg Ward7b43c682002-12-30 02:29:28 +0000426 FD_SET(self->fd, &write_set_fds);
Greg Ward04613a92002-11-30 22:47:45 +0000427
428 while (size > 0) {
Greg Ward7b43c682002-12-30 02:29:28 +0000429 select_rv = select(self->fd+1, NULL, &write_set_fds, NULL, NULL);
Greg Ward131bce02002-11-30 22:56:44 +0000430 assert(select_rv != 0); /* no timeout, can't expire */
431 if (select_rv == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000432 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000433
Greg Ward7b43c682002-12-30 02:29:28 +0000434 rv = write(self->fd, cp, size);
Greg Ward131bce02002-11-30 22:56:44 +0000435 if (rv == -1) {
436 if (errno == EAGAIN) { /* buffer is full, try again */
437 errno = 0;
438 continue;
439 } else /* it's a real error */
Greg Ward97708bc2002-11-30 23:17:10 +0000440 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000441 } else { /* wrote rv bytes */
Greg Ward7b43c682002-12-30 02:29:28 +0000442 self->ocount += rv;
Greg Ward131bce02002-11-30 22:56:44 +0000443 size -= rv;
444 cp += rv;
445 }
Greg Ward04613a92002-11-30 22:47:45 +0000446 }
447 Py_INCREF(Py_None);
448 return Py_None;
449}
450
451static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000452oss_close(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000453{
454 if (!PyArg_ParseTuple(args, ":close"))
Greg Wardad4d9b92002-12-30 03:02:22 +0000455 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000456
Greg Ward7b43c682002-12-30 02:29:28 +0000457 if (self->fd >= 0) {
458 close(self->fd);
459 self->fd = -1;
Greg Ward04613a92002-11-30 22:47:45 +0000460 }
461 Py_INCREF(Py_None);
462 return Py_None;
463}
464
465static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000466oss_fileno(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000467{
468 if (!PyArg_ParseTuple(args, ":fileno"))
Greg Wardad4d9b92002-12-30 03:02:22 +0000469 return NULL;
Greg Ward7b43c682002-12-30 02:29:28 +0000470 return PyInt_FromLong(self->fd);
Greg Ward04613a92002-11-30 22:47:45 +0000471}
472
Greg Ward131bce02002-11-30 22:56:44 +0000473
474/* Convenience methods: these generally wrap a couple of ioctls into one
475 common task. */
476
Greg Ward04613a92002-11-30 22:47:45 +0000477static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000478oss_setparameters(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000479{
480 int rate, ssize, nchannels, n, fmt, emulate=0;
481
482 if (!PyArg_ParseTuple(args, "iiii|i:setparameters",
483 &rate, &ssize, &nchannels, &fmt, &emulate))
484 return NULL;
485
486 if (rate < 0) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000487 PyErr_Format(PyExc_ValueError, "expected rate >= 0, not %d",
488 rate);
489 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000490 }
491 if (ssize < 0) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000492 PyErr_Format(PyExc_ValueError, "expected sample size >= 0, not %d",
493 ssize);
494 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000495 }
496 if (nchannels != 1 && nchannels != 2) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000497 PyErr_Format(PyExc_ValueError, "nchannels must be 1 or 2, not %d",
498 nchannels);
499 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000500 }
501
502 for (n = 0; n < n_audio_types; n++)
503 if (fmt == audio_types[n].a_fmt)
504 break;
505 if (n == n_audio_types) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000506 PyErr_Format(PyExc_ValueError, "unknown audio encoding: %d", fmt);
507 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000508 }
509 if (audio_types[n].a_bps != ssize) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000510 PyErr_Format(PyExc_ValueError,
511 "for %s, expected sample size %d, not %d",
512 audio_types[n].a_name, audio_types[n].a_bps, ssize);
513 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000514 }
515
516 if (emulate == 0) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000517 if ((self->afmts & audio_types[n].a_fmt) == 0) {
518 PyErr_Format(PyExc_ValueError,
519 "%s format not supported by device",
520 audio_types[n].a_name);
521 return NULL;
522 }
Greg Ward04613a92002-11-30 22:47:45 +0000523 }
Greg Ward7b43c682002-12-30 02:29:28 +0000524 if (ioctl(self->fd, SNDCTL_DSP_SETFMT,
Greg Wardad4d9b92002-12-30 03:02:22 +0000525 &audio_types[n].a_fmt) == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000526 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000527 return NULL;
528 }
Greg Ward7b43c682002-12-30 02:29:28 +0000529 if (ioctl(self->fd, SNDCTL_DSP_CHANNELS, &nchannels) == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000530 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000531 return NULL;
532 }
Greg Ward7b43c682002-12-30 02:29:28 +0000533 if (ioctl(self->fd, SNDCTL_DSP_SPEED, &rate) == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000534 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000535 return NULL;
536 }
537
538 Py_INCREF(Py_None);
539 return Py_None;
540}
541
542static int
Greg Ward499b73e2002-12-31 03:04:52 +0000543_ssize(oss_audio_t *self, int *nchannels, int *ssize)
Greg Ward04613a92002-11-30 22:47:45 +0000544{
545 int fmt;
546
547 fmt = 0;
Greg Ward7b43c682002-12-30 02:29:28 +0000548 if (ioctl(self->fd, SNDCTL_DSP_SETFMT, &fmt) < 0)
Greg Ward04613a92002-11-30 22:47:45 +0000549 return -errno;
550
551 switch (fmt) {
552 case AFMT_MU_LAW:
553 case AFMT_A_LAW:
554 case AFMT_U8:
555 case AFMT_S8:
556 *ssize = sizeof(char);
557 break;
558 case AFMT_S16_LE:
559 case AFMT_S16_BE:
560 case AFMT_U16_LE:
561 case AFMT_U16_BE:
562 *ssize = sizeof(short);
563 break;
564 case AFMT_MPEG:
565 case AFMT_IMA_ADPCM:
566 default:
567 return -EOPNOTSUPP;
568 }
569 *nchannels = 0;
Greg Ward7b43c682002-12-30 02:29:28 +0000570 if (ioctl(self->fd, SNDCTL_DSP_CHANNELS, nchannels) < 0)
Greg Ward04613a92002-11-30 22:47:45 +0000571 return -errno;
572 return 0;
573}
574
575
576/* bufsize returns the size of the hardware audio buffer in number
577 of samples */
578static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000579oss_bufsize(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000580{
581 audio_buf_info ai;
582 int nchannels, ssize;
583
584 if (!PyArg_ParseTuple(args, ":bufsize")) return NULL;
585
586 if (_ssize(self, &nchannels, &ssize) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000587 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000588 return NULL;
589 }
Greg Ward7b43c682002-12-30 02:29:28 +0000590 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000591 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000592 return NULL;
593 }
594 return PyInt_FromLong((ai.fragstotal * ai.fragsize) / (nchannels * ssize));
595}
596
597/* obufcount returns the number of samples that are available in the
598 hardware for playing */
599static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000600oss_obufcount(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000601{
602 audio_buf_info ai;
603 int nchannels, ssize;
604
605 if (!PyArg_ParseTuple(args, ":obufcount"))
606 return NULL;
607
608 if (_ssize(self, &nchannels, &ssize) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000609 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000610 return NULL;
611 }
Greg Ward7b43c682002-12-30 02:29:28 +0000612 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000613 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000614 return NULL;
615 }
616 return PyInt_FromLong((ai.fragstotal * ai.fragsize - ai.bytes) /
617 (ssize * nchannels));
618}
619
620/* obufcount returns the number of samples that can be played without
621 blocking */
622static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000623oss_obuffree(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000624{
625 audio_buf_info ai;
626 int nchannels, ssize;
627
628 if (!PyArg_ParseTuple(args, ":obuffree"))
629 return NULL;
630
631 if (_ssize(self, &nchannels, &ssize) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000632 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000633 return NULL;
634 }
Greg Ward7b43c682002-12-30 02:29:28 +0000635 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000636 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000637 return NULL;
638 }
639 return PyInt_FromLong(ai.bytes / (ssize * nchannels));
640}
641
Greg Ward04613a92002-11-30 22:47:45 +0000642static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000643oss_getptr(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000644{
645 count_info info;
646 int req;
647
648 if (!PyArg_ParseTuple(args, ":getptr"))
Greg Wardad4d9b92002-12-30 03:02:22 +0000649 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000650
Greg Ward7b43c682002-12-30 02:29:28 +0000651 if (self->mode == O_RDONLY)
Greg Wardad4d9b92002-12-30 03:02:22 +0000652 req = SNDCTL_DSP_GETIPTR;
Greg Ward04613a92002-11-30 22:47:45 +0000653 else
Greg Wardad4d9b92002-12-30 03:02:22 +0000654 req = SNDCTL_DSP_GETOPTR;
Greg Ward7b43c682002-12-30 02:29:28 +0000655 if (ioctl(self->fd, req, &info) == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000656 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000657 return NULL;
658 }
659 return Py_BuildValue("iii", info.bytes, info.blocks, info.ptr);
660}
661
Greg Wardda1cacb2002-12-31 03:02:23 +0000662
663/* ----------------------------------------------------------------------
664 * Methods of mixer objects (OSSMixerType)
665 */
666
Greg Ward3d9994d2002-12-11 15:12:01 +0000667static PyObject *
668oss_mixer_close(oss_mixer_t *self, PyObject *args)
669{
670 if (!PyArg_ParseTuple(args, ":close"))
Greg Wardad4d9b92002-12-30 03:02:22 +0000671 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000672
Greg Ward7b43c682002-12-30 02:29:28 +0000673 if (self->fd >= 0) {
674 close(self->fd);
675 self->fd = -1;
Greg Ward3d9994d2002-12-11 15:12:01 +0000676 }
677 Py_INCREF(Py_None);
678 return Py_None;
679}
680
681static PyObject *
682oss_mixer_fileno(oss_mixer_t *self, PyObject *args)
683{
684 if (!PyArg_ParseTuple(args, ":fileno"))
Greg Wardad4d9b92002-12-30 03:02:22 +0000685 return NULL;
Greg Ward7b43c682002-12-30 02:29:28 +0000686 return PyInt_FromLong(self->fd);
Greg Ward3d9994d2002-12-11 15:12:01 +0000687}
688
689/* Simple mixer interface methods */
690
691static PyObject *
Greg Ward2d6f9a92002-12-31 02:54:43 +0000692oss_mixer_controls(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000693{
Greg Ward2d6f9a92002-12-31 02:54:43 +0000694 return _do_ioctl_1_internal(self->fd, args, "controls",
Greg Ward3d9994d2002-12-11 15:12:01 +0000695 SOUND_MIXER_READ_DEVMASK);
696}
697
698static PyObject *
Greg Ward2d6f9a92002-12-31 02:54:43 +0000699oss_mixer_stereocontrols(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000700{
Greg Ward2d6f9a92002-12-31 02:54:43 +0000701 return _do_ioctl_1_internal(self->fd, args, "stereocontrols",
Greg Ward3d9994d2002-12-11 15:12:01 +0000702 SOUND_MIXER_READ_STEREODEVS);
703}
704
705static PyObject *
Greg Ward2d6f9a92002-12-31 02:54:43 +0000706oss_mixer_reccontrols(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000707{
Greg Ward2d6f9a92002-12-31 02:54:43 +0000708 return _do_ioctl_1_internal(self->fd, args, "reccontrols",
Greg Wardb69bb3d2002-12-12 17:35:45 +0000709 SOUND_MIXER_READ_RECMASK);
Greg Ward3d9994d2002-12-11 15:12:01 +0000710}
711
712static PyObject *
Greg Warde7037662002-12-30 03:01:48 +0000713oss_mixer_get(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000714{
715 int channel, volume;
716
717 /* Can't use _do_ioctl_1 because of encoded arg thingy. */
Greg Warde7037662002-12-30 03:01:48 +0000718 if (!PyArg_ParseTuple(args, "i:get", &channel))
Greg Wardad4d9b92002-12-30 03:02:22 +0000719 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000720
721 if (channel < 0 || channel > SOUND_MIXER_NRDEVICES) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000722 PyErr_SetString(OSSAudioError, "Invalid mixer channel specified.");
723 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000724 }
725
Greg Warde7037662002-12-30 03:01:48 +0000726 if (ioctl(self->fd, MIXER_READ(channel), &volume) == -1)
Greg Wardad4d9b92002-12-30 03:02:22 +0000727 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward3d9994d2002-12-11 15:12:01 +0000728
Greg Warde7037662002-12-30 03:01:48 +0000729 return Py_BuildValue("(ii)", volume & 0xff, (volume & 0xff00) >> 8);
Greg Ward3d9994d2002-12-11 15:12:01 +0000730}
731
732static PyObject *
Greg Warde7037662002-12-30 03:01:48 +0000733oss_mixer_set(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000734{
735 int channel, volume, leftVol, rightVol;
736
737 /* Can't use _do_ioctl_1 because of encoded arg thingy. */
Greg Warde7037662002-12-30 03:01:48 +0000738 if (!PyArg_ParseTuple(args, "i(ii):set", &channel, &leftVol, &rightVol))
Greg Wardad4d9b92002-12-30 03:02:22 +0000739 return NULL;
740
Greg Ward3d9994d2002-12-11 15:12:01 +0000741 if (channel < 0 || channel > SOUND_MIXER_NRDEVICES) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000742 PyErr_SetString(OSSAudioError, "Invalid mixer channel specified.");
743 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000744 }
745
746 if (leftVol < 0 || rightVol < 0 || leftVol > 100 || rightVol > 100) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000747 PyErr_SetString(OSSAudioError, "Volumes must be between 0 and 100.");
748 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000749 }
750
751 volume = (rightVol << 8) | leftVol;
752
Greg Warde7037662002-12-30 03:01:48 +0000753 if (ioctl(self->fd, MIXER_WRITE(channel), &volume) == -1)
Greg Wardad4d9b92002-12-30 03:02:22 +0000754 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward3d9994d2002-12-11 15:12:01 +0000755
Greg Warde7037662002-12-30 03:01:48 +0000756 return Py_BuildValue("(ii)", volume & 0xff, (volume & 0xff00) >> 8);
Greg Ward3d9994d2002-12-11 15:12:01 +0000757}
758
759static PyObject *
Greg Wardf05aa102002-12-30 23:19:32 +0000760oss_mixer_get_recsrc(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000761{
Greg Wardf05aa102002-12-30 23:19:32 +0000762 return _do_ioctl_1_internal(self->fd, args, "get_recsrc",
Greg Ward3d9994d2002-12-11 15:12:01 +0000763 SOUND_MIXER_READ_RECSRC);
764}
765
766static PyObject *
Greg Wardf05aa102002-12-30 23:19:32 +0000767oss_mixer_set_recsrc(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000768{
Greg Wardf05aa102002-12-30 23:19:32 +0000769 return _do_ioctl_1(self->fd, args, "set_recsrc",
Greg Ward3d9994d2002-12-11 15:12:01 +0000770 SOUND_MIXER_WRITE_RECSRC);
771}
772
773
Greg Wardda1cacb2002-12-31 03:02:23 +0000774/* ----------------------------------------------------------------------
775 * Method tables and other bureaucracy
776 */
777
Greg Ward8c6b6a92002-12-11 14:43:13 +0000778static PyMethodDef oss_methods[] = {
Greg Ward131bce02002-11-30 22:56:44 +0000779 /* Regular file methods */
Greg Wardad4d9b92002-12-30 03:02:22 +0000780 { "read", (PyCFunction)oss_read, METH_VARARGS },
781 { "write", (PyCFunction)oss_write, METH_VARARGS },
782 { "writeall", (PyCFunction)oss_writeall, METH_VARARGS },
783 { "close", (PyCFunction)oss_close, METH_VARARGS },
784 { "fileno", (PyCFunction)oss_fileno, METH_VARARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000785
786 /* Simple ioctl wrappers */
Greg Ward8c6b6a92002-12-11 14:43:13 +0000787 { "nonblock", (PyCFunction)oss_nonblock, METH_VARARGS },
Greg Wardad4d9b92002-12-30 03:02:22 +0000788 { "setfmt", (PyCFunction)oss_setfmt, METH_VARARGS },
Greg Ward8c6b6a92002-12-11 14:43:13 +0000789 { "getfmts", (PyCFunction)oss_getfmts, METH_VARARGS },
790 { "channels", (PyCFunction)oss_channels, METH_VARARGS },
791 { "speed", (PyCFunction)oss_speed, METH_VARARGS },
Greg Wardad4d9b92002-12-30 03:02:22 +0000792 { "sync", (PyCFunction)oss_sync, METH_VARARGS },
793 { "reset", (PyCFunction)oss_reset, METH_VARARGS },
794 { "post", (PyCFunction)oss_post, METH_VARARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000795
796 /* Convenience methods -- wrap a couple of ioctls together */
Greg Wardad4d9b92002-12-30 03:02:22 +0000797 { "setparameters", (PyCFunction)oss_setparameters, METH_VARARGS },
798 { "bufsize", (PyCFunction)oss_bufsize, METH_VARARGS },
799 { "obufcount", (PyCFunction)oss_obufcount, METH_VARARGS },
800 { "obuffree", (PyCFunction)oss_obuffree, METH_VARARGS },
Greg Ward8c6b6a92002-12-11 14:43:13 +0000801 { "getptr", (PyCFunction)oss_getptr, METH_VARARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000802
803 /* Aliases for backwards compatibility */
Greg Wardad4d9b92002-12-30 03:02:22 +0000804 { "flush", (PyCFunction)oss_sync, METH_VARARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000805
Greg Wardad4d9b92002-12-30 03:02:22 +0000806 { NULL, NULL} /* sentinel */
Greg Ward04613a92002-11-30 22:47:45 +0000807};
808
Greg Ward3d9994d2002-12-11 15:12:01 +0000809static PyMethodDef oss_mixer_methods[] = {
810 /* Regular file method - OSS mixers are ioctl-only interface */
Greg Wardad4d9b92002-12-30 03:02:22 +0000811 { "close", (PyCFunction)oss_mixer_close, METH_VARARGS },
812 { "fileno", (PyCFunction)oss_mixer_fileno, METH_VARARGS },
Greg Ward3d9994d2002-12-11 15:12:01 +0000813
814 /* Simple ioctl wrappers */
Greg Ward2d6f9a92002-12-31 02:54:43 +0000815 { "controls", (PyCFunction)oss_mixer_controls, METH_VARARGS },
816 { "stereocontrols", (PyCFunction)oss_mixer_stereocontrols, METH_VARARGS},
817 { "reccontrols", (PyCFunction)oss_mixer_reccontrols, METH_VARARGS},
Greg Wardad4d9b92002-12-30 03:02:22 +0000818 { "get", (PyCFunction)oss_mixer_get, METH_VARARGS },
819 { "set", (PyCFunction)oss_mixer_set, METH_VARARGS },
Greg Wardf05aa102002-12-30 23:19:32 +0000820 { "get_recsrc", (PyCFunction)oss_mixer_get_recsrc, METH_VARARGS },
821 { "set_recsrc", (PyCFunction)oss_mixer_set_recsrc, METH_VARARGS },
Greg Ward3d9994d2002-12-11 15:12:01 +0000822
Greg Wardad4d9b92002-12-30 03:02:22 +0000823 { NULL, NULL}
Greg Ward3d9994d2002-12-11 15:12:01 +0000824};
825
Greg Ward04613a92002-11-30 22:47:45 +0000826static PyObject *
Greg Ward58ae13c2002-12-31 03:07:21 +0000827oss_getattr(oss_audio_t *self, char *name)
Greg Ward04613a92002-11-30 22:47:45 +0000828{
Greg Ward58ae13c2002-12-31 03:07:21 +0000829 return Py_FindMethod(oss_methods, (PyObject *)self, name);
Greg Ward04613a92002-11-30 22:47:45 +0000830}
831
Greg Ward3d9994d2002-12-11 15:12:01 +0000832static PyObject *
Greg Ward58ae13c2002-12-31 03:07:21 +0000833oss_mixer_getattr(oss_mixer_t *self, char *name)
Greg Ward3d9994d2002-12-11 15:12:01 +0000834{
Greg Ward58ae13c2002-12-31 03:07:21 +0000835 return Py_FindMethod(oss_mixer_methods, (PyObject *)self, name);
Greg Ward3d9994d2002-12-11 15:12:01 +0000836}
837
Greg Ward499b73e2002-12-31 03:04:52 +0000838static PyTypeObject OSSAudioType = {
Greg Ward04613a92002-11-30 22:47:45 +0000839 PyObject_HEAD_INIT(&PyType_Type)
Greg Wardad4d9b92002-12-30 03:02:22 +0000840 0, /*ob_size*/
Greg Ward9a568eb2002-11-30 23:20:09 +0000841 "ossaudiodev.oss_audio_device", /*tp_name*/
Greg Ward499b73e2002-12-31 03:04:52 +0000842 sizeof(oss_audio_t), /*tp_size*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000843 0, /*tp_itemsize*/
Greg Ward04613a92002-11-30 22:47:45 +0000844 /* methods */
Greg Wardad4d9b92002-12-30 03:02:22 +0000845 (destructor)oss_dealloc, /*tp_dealloc*/
846 0, /*tp_print*/
847 (getattrfunc)oss_getattr, /*tp_getattr*/
848 0, /*tp_setattr*/
849 0, /*tp_compare*/
850 0, /*tp_repr*/
Greg Ward04613a92002-11-30 22:47:45 +0000851};
852
Greg Ward3d9994d2002-12-11 15:12:01 +0000853static PyTypeObject OSSMixerType = {
854 PyObject_HEAD_INIT(&PyType_Type)
Greg Wardad4d9b92002-12-30 03:02:22 +0000855 0, /*ob_size*/
Greg Ward3d9994d2002-12-11 15:12:01 +0000856 "ossaudiodev.oss_mixer_device", /*tp_name*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000857 sizeof(oss_mixer_t), /*tp_size*/
858 0, /*tp_itemsize*/
Greg Ward3d9994d2002-12-11 15:12:01 +0000859 /* methods */
860 (destructor)oss_mixer_dealloc, /*tp_dealloc*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000861 0, /*tp_print*/
Greg Ward3d9994d2002-12-11 15:12:01 +0000862 (getattrfunc)oss_mixer_getattr, /*tp_getattr*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000863 0, /*tp_setattr*/
864 0, /*tp_compare*/
865 0, /*tp_repr*/
Greg Ward3d9994d2002-12-11 15:12:01 +0000866};
867
868
Greg Ward04613a92002-11-30 22:47:45 +0000869static PyObject *
Greg Ward9a568eb2002-11-30 23:20:09 +0000870ossopen(PyObject *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000871{
Greg Ward8c6b6a92002-12-11 14:43:13 +0000872 return (PyObject *)newossobject(args);
Greg Ward04613a92002-11-30 22:47:45 +0000873}
874
Greg Ward3d9994d2002-12-11 15:12:01 +0000875static PyObject *
876ossopenmixer(PyObject *self, PyObject *args)
877{
878 return (PyObject *)newossmixerobject(args);
879}
880
Greg Ward9a568eb2002-11-30 23:20:09 +0000881static PyMethodDef ossaudiodev_methods[] = {
882 { "open", ossopen, METH_VARARGS },
Greg Ward3d9994d2002-12-11 15:12:01 +0000883 { "openmixer", ossopenmixer, METH_VARARGS },
Greg Ward04613a92002-11-30 22:47:45 +0000884 { 0, 0 },
885};
886
Greg Ward1e0f57d2002-11-30 23:05:26 +0000887
888#define _EXPORT_INT(mod, name) \
889 if (PyModule_AddIntConstant(mod, #name, (long) (name)) == -1) return;
890
Greg Ward744f0fd2002-12-31 03:23:59 +0000891
892static char *control_labels[] = SOUND_DEVICE_LABELS;
893static char *control_names[] = SOUND_DEVICE_NAMES;
894
895
896static int
897build_namelists (PyObject *module)
898{
899 PyObject *labels;
900 PyObject *names;
901 PyObject *s;
902 int num_controls;
903 int i;
904
905 num_controls = sizeof(control_labels) / sizeof(control_labels[0]);
906 assert(num_controls == sizeof(control_names) / sizeof(control_names[0]));
907
908 labels = PyList_New(num_controls);
909 names = PyList_New(num_controls);
910 for (i = 0; i < num_controls; i++) {
911 s = PyString_FromString(control_labels[i]);
912 if (s == NULL)
913 return -1;
914 PyList_SET_ITEM(labels, i, s);
915
916 s = PyString_FromString(control_names[i]);
917 if (s == NULL)
918 return -1;
919 PyList_SET_ITEM(names, i, s);
920 }
921
922 if (PyModule_AddObject(module, "control_labels", labels) == -1)
923 return -1;
924 if (PyModule_AddObject(module, "control_names", names) == -1)
925 return -1;
926
927 return 0;
928}
929
930
Greg Ward04613a92002-11-30 22:47:45 +0000931void
Greg Ward9a568eb2002-11-30 23:20:09 +0000932initossaudiodev(void)
Greg Ward04613a92002-11-30 22:47:45 +0000933{
934 PyObject *m;
935
Greg Ward9a568eb2002-11-30 23:20:09 +0000936 m = Py_InitModule("ossaudiodev", ossaudiodev_methods);
Greg Ward04613a92002-11-30 22:47:45 +0000937
Greg Ward97708bc2002-11-30 23:17:10 +0000938 OSSAudioError = PyErr_NewException("ossaudiodev.error", NULL, NULL);
939 if (OSSAudioError)
Greg Wardad4d9b92002-12-30 03:02:22 +0000940 PyModule_AddObject(m, "error", OSSAudioError);
Greg Ward04613a92002-11-30 22:47:45 +0000941
Greg Ward744f0fd2002-12-31 03:23:59 +0000942 /* Build 'control_labels' and 'control_names' lists and add them
943 to the module. */
944 if (build_namelists(m) == -1) /* XXX what to do here? */
945 return;
946
Greg Ward1e0f57d2002-11-30 23:05:26 +0000947 /* Expose the audio format numbers -- essential! */
948 _EXPORT_INT(m, AFMT_QUERY);
949 _EXPORT_INT(m, AFMT_MU_LAW);
950 _EXPORT_INT(m, AFMT_A_LAW);
951 _EXPORT_INT(m, AFMT_IMA_ADPCM);
952 _EXPORT_INT(m, AFMT_U8);
953 _EXPORT_INT(m, AFMT_S16_LE);
954 _EXPORT_INT(m, AFMT_S16_BE);
955 _EXPORT_INT(m, AFMT_S8);
956 _EXPORT_INT(m, AFMT_U16_LE);
957 _EXPORT_INT(m, AFMT_U16_BE);
958 _EXPORT_INT(m, AFMT_MPEG);
Andrew M. Kuchlingfe62bc92003-02-13 13:27:07 +0000959#ifdef AFMT_AC3
Greg Ward1e0f57d2002-11-30 23:05:26 +0000960 _EXPORT_INT(m, AFMT_AC3);
Andrew M. Kuchlingfe62bc92003-02-13 13:27:07 +0000961#endif
Neal Norwitzd156c2d2003-02-02 17:59:06 +0000962#ifdef AFMT_S16_NE
Greg Ward1e0f57d2002-11-30 23:05:26 +0000963 _EXPORT_INT(m, AFMT_S16_NE);
Neal Norwitzd156c2d2003-02-02 17:59:06 +0000964#endif
Greg Wardad4d9b92002-12-30 03:02:22 +0000965
Greg Ward37897c22002-12-30 02:43:36 +0000966 /* Expose the sound mixer device numbers. */
Greg Ward7b43c682002-12-30 02:29:28 +0000967 _EXPORT_INT(m, SOUND_MIXER_NRDEVICES);
Greg Ward3d9994d2002-12-11 15:12:01 +0000968 _EXPORT_INT(m, SOUND_MIXER_VOLUME);
969 _EXPORT_INT(m, SOUND_MIXER_BASS);
970 _EXPORT_INT(m, SOUND_MIXER_TREBLE);
971 _EXPORT_INT(m, SOUND_MIXER_SYNTH);
972 _EXPORT_INT(m, SOUND_MIXER_PCM);
973 _EXPORT_INT(m, SOUND_MIXER_SPEAKER);
974 _EXPORT_INT(m, SOUND_MIXER_LINE);
975 _EXPORT_INT(m, SOUND_MIXER_MIC);
976 _EXPORT_INT(m, SOUND_MIXER_CD);
977 _EXPORT_INT(m, SOUND_MIXER_IMIX);
978 _EXPORT_INT(m, SOUND_MIXER_ALTPCM);
979 _EXPORT_INT(m, SOUND_MIXER_RECLEV);
980 _EXPORT_INT(m, SOUND_MIXER_IGAIN);
981 _EXPORT_INT(m, SOUND_MIXER_OGAIN);
982 _EXPORT_INT(m, SOUND_MIXER_LINE1);
983 _EXPORT_INT(m, SOUND_MIXER_LINE2);
984 _EXPORT_INT(m, SOUND_MIXER_LINE3);
985 _EXPORT_INT(m, SOUND_MIXER_DIGITAL1);
986 _EXPORT_INT(m, SOUND_MIXER_DIGITAL2);
987 _EXPORT_INT(m, SOUND_MIXER_DIGITAL3);
988 _EXPORT_INT(m, SOUND_MIXER_PHONEIN);
989 _EXPORT_INT(m, SOUND_MIXER_PHONEOUT);
990 _EXPORT_INT(m, SOUND_MIXER_VIDEO);
991 _EXPORT_INT(m, SOUND_MIXER_RADIO);
992 _EXPORT_INT(m, SOUND_MIXER_MONITOR);
Greg Ward04613a92002-11-30 22:47:45 +0000993
Greg Ward1e0f57d2002-11-30 23:05:26 +0000994 /* Expose all the ioctl numbers for masochists who like to do this
995 stuff directly. */
996 _EXPORT_INT(m, SNDCTL_COPR_HALT);
997 _EXPORT_INT(m, SNDCTL_COPR_LOAD);
998 _EXPORT_INT(m, SNDCTL_COPR_RCODE);
999 _EXPORT_INT(m, SNDCTL_COPR_RCVMSG);
1000 _EXPORT_INT(m, SNDCTL_COPR_RDATA);
1001 _EXPORT_INT(m, SNDCTL_COPR_RESET);
1002 _EXPORT_INT(m, SNDCTL_COPR_RUN);
1003 _EXPORT_INT(m, SNDCTL_COPR_SENDMSG);
1004 _EXPORT_INT(m, SNDCTL_COPR_WCODE);
1005 _EXPORT_INT(m, SNDCTL_COPR_WDATA);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001006#ifdef SNDCTL_DSP_BIND_CHANNEL
Greg Ward1e0f57d2002-11-30 23:05:26 +00001007 _EXPORT_INT(m, SNDCTL_DSP_BIND_CHANNEL);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001008#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001009 _EXPORT_INT(m, SNDCTL_DSP_CHANNELS);
1010 _EXPORT_INT(m, SNDCTL_DSP_GETBLKSIZE);
1011 _EXPORT_INT(m, SNDCTL_DSP_GETCAPS);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001012#ifdef SNDCTL_DSP_GETCHANNELMASK
Greg Ward1e0f57d2002-11-30 23:05:26 +00001013 _EXPORT_INT(m, SNDCTL_DSP_GETCHANNELMASK);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001014#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001015 _EXPORT_INT(m, SNDCTL_DSP_GETFMTS);
1016 _EXPORT_INT(m, SNDCTL_DSP_GETIPTR);
1017 _EXPORT_INT(m, SNDCTL_DSP_GETISPACE);
1018 _EXPORT_INT(m, SNDCTL_DSP_GETODELAY);
1019 _EXPORT_INT(m, SNDCTL_DSP_GETOPTR);
1020 _EXPORT_INT(m, SNDCTL_DSP_GETOSPACE);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001021#ifdef SNDCTL_DSP_GETSPDIF
Greg Ward1e0f57d2002-11-30 23:05:26 +00001022 _EXPORT_INT(m, SNDCTL_DSP_GETSPDIF);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001023#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001024 _EXPORT_INT(m, SNDCTL_DSP_GETTRIGGER);
1025 _EXPORT_INT(m, SNDCTL_DSP_MAPINBUF);
1026 _EXPORT_INT(m, SNDCTL_DSP_MAPOUTBUF);
1027 _EXPORT_INT(m, SNDCTL_DSP_NONBLOCK);
1028 _EXPORT_INT(m, SNDCTL_DSP_POST);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001029#ifdef SNDCTL_DSP_PROFILE
Greg Ward1e0f57d2002-11-30 23:05:26 +00001030 _EXPORT_INT(m, SNDCTL_DSP_PROFILE);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001031#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001032 _EXPORT_INT(m, SNDCTL_DSP_RESET);
1033 _EXPORT_INT(m, SNDCTL_DSP_SAMPLESIZE);
1034 _EXPORT_INT(m, SNDCTL_DSP_SETDUPLEX);
1035 _EXPORT_INT(m, SNDCTL_DSP_SETFMT);
1036 _EXPORT_INT(m, SNDCTL_DSP_SETFRAGMENT);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001037#ifdef SNDCTL_DSP_SETSPDIF
Greg Ward1e0f57d2002-11-30 23:05:26 +00001038 _EXPORT_INT(m, SNDCTL_DSP_SETSPDIF);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001039#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001040 _EXPORT_INT(m, SNDCTL_DSP_SETSYNCRO);
1041 _EXPORT_INT(m, SNDCTL_DSP_SETTRIGGER);
1042 _EXPORT_INT(m, SNDCTL_DSP_SPEED);
1043 _EXPORT_INT(m, SNDCTL_DSP_STEREO);
1044 _EXPORT_INT(m, SNDCTL_DSP_SUBDIVIDE);
1045 _EXPORT_INT(m, SNDCTL_DSP_SYNC);
1046 _EXPORT_INT(m, SNDCTL_FM_4OP_ENABLE);
1047 _EXPORT_INT(m, SNDCTL_FM_LOAD_INSTR);
1048 _EXPORT_INT(m, SNDCTL_MIDI_INFO);
1049 _EXPORT_INT(m, SNDCTL_MIDI_MPUCMD);
1050 _EXPORT_INT(m, SNDCTL_MIDI_MPUMODE);
1051 _EXPORT_INT(m, SNDCTL_MIDI_PRETIME);
1052 _EXPORT_INT(m, SNDCTL_SEQ_CTRLRATE);
1053 _EXPORT_INT(m, SNDCTL_SEQ_GETINCOUNT);
1054 _EXPORT_INT(m, SNDCTL_SEQ_GETOUTCOUNT);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001055#ifdef SNDCTL_SEQ_GETTIME
Greg Ward1e0f57d2002-11-30 23:05:26 +00001056 _EXPORT_INT(m, SNDCTL_SEQ_GETTIME);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001057#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001058 _EXPORT_INT(m, SNDCTL_SEQ_NRMIDIS);
1059 _EXPORT_INT(m, SNDCTL_SEQ_NRSYNTHS);
1060 _EXPORT_INT(m, SNDCTL_SEQ_OUTOFBAND);
1061 _EXPORT_INT(m, SNDCTL_SEQ_PANIC);
1062 _EXPORT_INT(m, SNDCTL_SEQ_PERCMODE);
1063 _EXPORT_INT(m, SNDCTL_SEQ_RESET);
1064 _EXPORT_INT(m, SNDCTL_SEQ_RESETSAMPLES);
1065 _EXPORT_INT(m, SNDCTL_SEQ_SYNC);
1066 _EXPORT_INT(m, SNDCTL_SEQ_TESTMIDI);
1067 _EXPORT_INT(m, SNDCTL_SEQ_THRESHOLD);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001068#ifdef SNDCTL_SYNTH_CONTROL
Greg Ward1e0f57d2002-11-30 23:05:26 +00001069 _EXPORT_INT(m, SNDCTL_SYNTH_CONTROL);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001070#endif
1071#ifdef SNDCTL_SYNTH_ID
Greg Ward1e0f57d2002-11-30 23:05:26 +00001072 _EXPORT_INT(m, SNDCTL_SYNTH_ID);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001073#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001074 _EXPORT_INT(m, SNDCTL_SYNTH_INFO);
1075 _EXPORT_INT(m, SNDCTL_SYNTH_MEMAVL);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001076#ifdef SNDCTL_SYNTH_REMOVESAMPLE
Greg Ward1e0f57d2002-11-30 23:05:26 +00001077 _EXPORT_INT(m, SNDCTL_SYNTH_REMOVESAMPLE);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001078#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001079 _EXPORT_INT(m, SNDCTL_TMR_CONTINUE);
1080 _EXPORT_INT(m, SNDCTL_TMR_METRONOME);
1081 _EXPORT_INT(m, SNDCTL_TMR_SELECT);
1082 _EXPORT_INT(m, SNDCTL_TMR_SOURCE);
1083 _EXPORT_INT(m, SNDCTL_TMR_START);
1084 _EXPORT_INT(m, SNDCTL_TMR_STOP);
1085 _EXPORT_INT(m, SNDCTL_TMR_TEMPO);
1086 _EXPORT_INT(m, SNDCTL_TMR_TIMEBASE);
Greg Ward04613a92002-11-30 22:47:45 +00001087}