blob: 727f5a6213eb87bcfc3ad77eb054923fb26b9e69 [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
32
33#include <sys/ioctl.h>
34#if defined(linux)
35#include <linux/soundcard.h>
36
37typedef unsigned long uint32_t;
38
39#elif defined(__FreeBSD__)
40#include <machine/soundcard.h>
41
42#ifndef SNDCTL_DSP_CHANNELS
43#define SNDCTL_DSP_CHANNELS SOUND_PCM_WRITE_CHANNELS
44#endif
45
46#endif
47
48typedef struct {
49 PyObject_HEAD;
Greg Ward7b43c682002-12-30 02:29:28 +000050 int fd; /* The open file */
51 int mode; /* file mode */
52 int icount; /* Input count */
53 int ocount; /* Output count */
54 uint32_t afmts; /* Audio formats supported by hardware */
Greg Ward499b73e2002-12-31 03:04:52 +000055} oss_audio_t;
Greg Ward04613a92002-11-30 22:47:45 +000056
Greg Ward3d9994d2002-12-11 15:12:01 +000057typedef struct {
58 PyObject_HEAD;
Greg Wardad4d9b92002-12-30 03:02:22 +000059 int fd; /* The open mixer device */
Greg Ward3d9994d2002-12-11 15:12:01 +000060} oss_mixer_t;
61
Greg Ward04613a92002-11-30 22:47:45 +000062/* XXX several format defined in soundcard.h are not supported,
63 including _NE (native endian) options and S32 options
64*/
65
66static struct {
Greg Wardad4d9b92002-12-30 03:02:22 +000067 int a_bps;
68 uint32_t a_fmt;
Greg Ward04613a92002-11-30 22:47:45 +000069 char *a_name;
70} audio_types[] = {
Greg Wardad4d9b92002-12-30 03:02:22 +000071 { 8, AFMT_MU_LAW, "logarithmic mu-law 8-bit audio" },
72 { 8, AFMT_A_LAW, "logarithmic A-law 8-bit audio" },
73 { 8, AFMT_U8, "linear unsigned 8-bit audio" },
74 { 8, AFMT_S8, "linear signed 8-bit audio" },
75 { 16, AFMT_U16_BE, "linear unsigned 16-bit big-endian audio" },
76 { 16, AFMT_U16_LE, "linear unsigned 16-bit little-endian audio" },
77 { 16, AFMT_S16_BE, "linear signed 16-bit big-endian audio" },
78 { 16, AFMT_S16_LE, "linear signed 16-bit little-endian audio" },
Neal Norwitzd156c2d2003-02-02 17:59:06 +000079#ifdef AFMT_S16_NE
Greg Wardad4d9b92002-12-30 03:02:22 +000080 { 16, AFMT_S16_NE, "linear signed 16-bit native-endian audio" },
Neal Norwitzd156c2d2003-02-02 17:59:06 +000081#endif
Greg Ward04613a92002-11-30 22:47:45 +000082};
83
84static int n_audio_types = sizeof(audio_types) / sizeof(audio_types[0]);
85
Greg Ward499b73e2002-12-31 03:04:52 +000086static PyTypeObject OSSAudioType;
Greg Ward3d9994d2002-12-11 15:12:01 +000087static PyTypeObject OSSMixerType;
Greg Ward04613a92002-11-30 22:47:45 +000088
Greg Ward97708bc2002-11-30 23:17:10 +000089static PyObject *OSSAudioError;
Greg Ward04613a92002-11-30 22:47:45 +000090
Greg Wardda1cacb2002-12-31 03:02:23 +000091
92/* ----------------------------------------------------------------------
93 * DSP object initialization/deallocation
94 */
95
Greg Ward499b73e2002-12-31 03:04:52 +000096static oss_audio_t *
Greg Ward8c6b6a92002-12-11 14:43:13 +000097newossobject(PyObject *arg)
Greg Ward04613a92002-11-30 22:47:45 +000098{
Greg Ward58ae13c2002-12-31 03:07:21 +000099 oss_audio_t *self;
Greg Ward04613a92002-11-30 22:47:45 +0000100 int fd, afmts, imode;
101 char *basedev = NULL;
102 char *mode = NULL;
103
Greg Ward9a568eb2002-11-30 23:20:09 +0000104 /* Two ways to call open():
Greg Ward04613a92002-11-30 22:47:45 +0000105 open(device, mode) (for consistency with builtin open())
106 open(mode) (for backwards compatibility)
107 because the *first* argument is optional, parsing args is
108 a wee bit tricky. */
109 if (!PyArg_ParseTuple(arg, "s|s:open", &basedev, &mode))
110 return NULL;
111 if (mode == NULL) { /* only one arg supplied */
112 mode = basedev;
113 basedev = NULL;
114 }
115
116 if (strcmp(mode, "r") == 0)
117 imode = O_RDONLY;
118 else if (strcmp(mode, "w") == 0)
119 imode = O_WRONLY;
Greg Ward1e0f57d2002-11-30 23:05:26 +0000120 else if (strcmp(mode, "rw") == 0)
121 imode = O_RDWR;
Greg Ward04613a92002-11-30 22:47:45 +0000122 else {
Greg Ward97708bc2002-11-30 23:17:10 +0000123 PyErr_SetString(OSSAudioError, "mode must be 'r', 'w', or 'rw'");
Greg Ward04613a92002-11-30 22:47:45 +0000124 return NULL;
125 }
126
Greg Ward5c5c5772002-12-30 02:58:04 +0000127 /* Open the correct device: either the 'device' argument,
128 or the AUDIODEV environment variable, or "/dev/dsp". */
Greg Ward04613a92002-11-30 22:47:45 +0000129 if (basedev == NULL) { /* called with one arg */
130 basedev = getenv("AUDIODEV");
131 if (basedev == NULL) /* $AUDIODEV not set */
132 basedev = "/dev/dsp";
133 }
134
135 if ((fd = open(basedev, imode)) == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000136 PyErr_SetFromErrnoWithFilename(PyExc_IOError, basedev);
Greg Ward04613a92002-11-30 22:47:45 +0000137 return NULL;
138 }
Greg Ward04613a92002-11-30 22:47:45 +0000139 if (ioctl(fd, SNDCTL_DSP_GETFMTS, &afmts) == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000140 PyErr_SetFromErrnoWithFilename(PyExc_IOError, basedev);
Greg Ward04613a92002-11-30 22:47:45 +0000141 return NULL;
142 }
143 /* Create and initialize the object */
Greg Ward58ae13c2002-12-31 03:07:21 +0000144 if ((self = PyObject_New(oss_audio_t, &OSSAudioType)) == NULL) {
Greg Ward04613a92002-11-30 22:47:45 +0000145 close(fd);
146 return NULL;
147 }
Greg Ward58ae13c2002-12-31 03:07:21 +0000148 self->fd = fd;
149 self->mode = imode;
150 self->icount = self->ocount = 0;
151 self->afmts = afmts;
152 return self;
Greg Ward04613a92002-11-30 22:47:45 +0000153}
154
155static void
Greg Ward58ae13c2002-12-31 03:07:21 +0000156oss_dealloc(oss_audio_t *self)
Greg Ward04613a92002-11-30 22:47:45 +0000157{
158 /* if already closed, don't reclose it */
Greg Ward58ae13c2002-12-31 03:07:21 +0000159 if (self->fd != -1)
160 close(self->fd);
161 PyObject_Del(self);
Greg Ward04613a92002-11-30 22:47:45 +0000162}
163
Greg Wardda1cacb2002-12-31 03:02:23 +0000164
165/* ----------------------------------------------------------------------
166 * Mixer object initialization/deallocation
167 */
168
Greg Ward3d9994d2002-12-11 15:12:01 +0000169static oss_mixer_t *
170newossmixerobject(PyObject *arg)
171{
172 char *basedev = NULL, *mode = NULL;
173 int fd, imode;
Greg Ward58ae13c2002-12-31 03:07:21 +0000174 oss_mixer_t *self;
Greg Ward3d9994d2002-12-11 15:12:01 +0000175
Greg Warde7037662002-12-30 03:01:48 +0000176 if (!PyArg_ParseTuple(arg, "|ss", &basedev, &mode)) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000177 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000178 }
179
180 if (basedev == NULL) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000181 basedev = getenv("MIXERDEV");
182 if (basedev == NULL) /* MIXERDEV not set */
183 basedev = "/dev/mixer";
Greg Ward3d9994d2002-12-11 15:12:01 +0000184 }
185
186 if (mode == NULL || strcmp(mode, "r") == 0)
187 imode = O_RDONLY;
188 else if (strcmp(mode, "w") == 0)
189 imode = O_WRONLY;
190 else if (strcmp(mode, "rw") == 0)
191 imode = O_RDWR;
192 else {
193 PyErr_SetString(OSSAudioError, "mode must be 'r', 'w', or 'rw'");
194 return NULL;
195 }
196
Greg Warde7037662002-12-30 03:01:48 +0000197 if ((fd = open(basedev, imode)) == -1) {
Greg Ward3d9994d2002-12-11 15:12:01 +0000198 PyErr_SetFromErrnoWithFilename(PyExc_IOError, basedev);
199 return NULL;
200 }
201
Greg Ward58ae13c2002-12-31 03:07:21 +0000202 if ((self = PyObject_New(oss_mixer_t, &OSSMixerType)) == NULL) {
Greg Ward3d9994d2002-12-11 15:12:01 +0000203 close(fd);
204 return NULL;
205 }
206
Greg Ward58ae13c2002-12-31 03:07:21 +0000207 self->fd = fd;
Greg Ward3d9994d2002-12-11 15:12:01 +0000208
Greg Ward58ae13c2002-12-31 03:07:21 +0000209 return self;
Greg Ward3d9994d2002-12-11 15:12:01 +0000210}
211
212static void
Greg Ward58ae13c2002-12-31 03:07:21 +0000213oss_mixer_dealloc(oss_mixer_t *self)
Greg Ward3d9994d2002-12-11 15:12:01 +0000214{
215 /* if already closed, don't reclose it */
Greg Ward58ae13c2002-12-31 03:07:21 +0000216 if (self->fd != -1)
217 close(self->fd);
218 PyObject_Del(self);
Greg Ward3d9994d2002-12-11 15:12:01 +0000219}
220
Greg Ward131bce02002-11-30 22:56:44 +0000221
222/* Methods to wrap the OSS ioctls. The calling convention is pretty
223 simple:
224 nonblock() -> ioctl(fd, SNDCTL_DSP_NONBLOCK)
225 fmt = setfmt(fmt) -> ioctl(fd, SNDCTL_DSP_SETFMT, &fmt)
226 etc.
227*/
228
229
230/* _do_ioctl_1() is a private helper function used for the OSS ioctls --
231 SNDCTL_DSP_{SETFMT,CHANNELS,SPEED} -- that that are called from C
232 like this:
233 ioctl(fd, SNDCTL_DSP_cmd, &arg)
234
235 where arg is the value to set, and on return the driver sets arg to
236 the value that was actually set. Mapping this to Python is obvious:
237 arg = dsp.xxx(arg)
238*/
239static PyObject *
Greg Wardda9f8532002-12-11 14:49:59 +0000240_do_ioctl_1(int fd, PyObject *args, char *fname, int cmd)
Greg Ward131bce02002-11-30 22:56:44 +0000241{
Greg Wardda9f8532002-12-11 14:49:59 +0000242 char argfmt[33] = "i:";
Greg Ward131bce02002-11-30 22:56:44 +0000243 int arg;
244
Greg Wardda9f8532002-12-11 14:49:59 +0000245 assert(strlen(fname) <= 30);
Greg Ward131bce02002-11-30 22:56:44 +0000246 strcat(argfmt, fname);
247 if (!PyArg_ParseTuple(args, argfmt, &arg))
Greg Wardad4d9b92002-12-30 03:02:22 +0000248 return NULL;
Greg Ward131bce02002-11-30 22:56:44 +0000249
Greg Wardda9f8532002-12-11 14:49:59 +0000250 if (ioctl(fd, cmd, &arg) == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000251 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000252 return PyInt_FromLong(arg);
253}
254
Greg Wardda1cacb2002-12-31 03:02:23 +0000255
256/* ----------------------------------------------------------------------
257 * Helper functions
258 */
259
Greg Ward3d9994d2002-12-11 15:12:01 +0000260/* _do_ioctl_1_internal() is a wrapper for ioctls that take no inputs
261 but return an output -- ie. we need to pass a pointer to a local C
262 variable so the driver can write its output there, but from Python
263 all we see is the return value. For example,
264 SOUND_MIXER_READ_DEVMASK returns a bitmask of available mixer
265 devices, but does not use the value of the parameter passed-in in any
266 way.
267*/
268
269static PyObject *
270_do_ioctl_1_internal(int fd, PyObject *args, char *fname, int cmd)
271{
272 char argfmt[32] = ":";
273 int arg = 0;
274
275 assert(strlen(fname) <= 30);
276 strcat(argfmt, fname);
277 if (!PyArg_ParseTuple(args, argfmt, &arg))
Greg Wardad4d9b92002-12-30 03:02:22 +0000278 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000279
280 if (ioctl(fd, cmd, &arg) == -1)
281 return PyErr_SetFromErrno(PyExc_IOError);
282 return PyInt_FromLong(arg);
283}
284
285
286
Greg Ward131bce02002-11-30 22:56:44 +0000287/* _do_ioctl_0() is a private helper for the no-argument ioctls:
288 SNDCTL_DSP_{SYNC,RESET,POST}. */
289static PyObject *
Greg Wardda9f8532002-12-11 14:49:59 +0000290_do_ioctl_0(int fd, PyObject *args, char *fname, int cmd)
Greg Ward131bce02002-11-30 22:56:44 +0000291{
Greg Wardda9f8532002-12-11 14:49:59 +0000292 char argfmt[32] = ":";
Greg Ward131bce02002-11-30 22:56:44 +0000293
Greg Wardda9f8532002-12-11 14:49:59 +0000294 assert(strlen(fname) <= 30);
Greg Ward131bce02002-11-30 22:56:44 +0000295 strcat(argfmt, fname);
296 if (!PyArg_ParseTuple(args, argfmt))
Greg Wardad4d9b92002-12-30 03:02:22 +0000297 return NULL;
Greg Ward131bce02002-11-30 22:56:44 +0000298
Greg Wardda9f8532002-12-11 14:49:59 +0000299 if (ioctl(fd, cmd, 0) == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000300 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000301 Py_INCREF(Py_None);
302 return Py_None;
303}
304
305
Greg Wardda1cacb2002-12-31 03:02:23 +0000306/* ----------------------------------------------------------------------
Greg Ward499b73e2002-12-31 03:04:52 +0000307 * Methods of DSP objects (OSSAudioType)
Greg Wardda1cacb2002-12-31 03:02:23 +0000308 */
309
Greg Ward131bce02002-11-30 22:56:44 +0000310static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000311oss_nonblock(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000312{
313 /* Hmmm: it doesn't appear to be possible to return to blocking
314 mode once we're in non-blocking mode! */
315 if (!PyArg_ParseTuple(args, ":nonblock"))
Greg Wardad4d9b92002-12-30 03:02:22 +0000316 return NULL;
Greg Ward7b43c682002-12-30 02:29:28 +0000317 if (ioctl(self->fd, SNDCTL_DSP_NONBLOCK, NULL) == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000318 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000319 Py_INCREF(Py_None);
320 return Py_None;
321}
322
323static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000324oss_setfmt(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000325{
Greg Ward7b43c682002-12-30 02:29:28 +0000326 return _do_ioctl_1(self->fd, args, "setfmt", SNDCTL_DSP_SETFMT);
Greg Ward131bce02002-11-30 22:56:44 +0000327}
328
329static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000330oss_getfmts(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000331{
332 int mask;
333 if (!PyArg_ParseTuple(args, ":getfmts"))
Greg Wardad4d9b92002-12-30 03:02:22 +0000334 return NULL;
Greg Ward7b43c682002-12-30 02:29:28 +0000335 if (ioctl(self->fd, SNDCTL_DSP_GETFMTS, &mask) == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000336 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000337 return PyInt_FromLong(mask);
338}
339
340static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000341oss_channels(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000342{
Greg Ward7b43c682002-12-30 02:29:28 +0000343 return _do_ioctl_1(self->fd, args, "channels", SNDCTL_DSP_CHANNELS);
Greg Ward131bce02002-11-30 22:56:44 +0000344}
345
346static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000347oss_speed(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000348{
Greg Ward7b43c682002-12-30 02:29:28 +0000349 return _do_ioctl_1(self->fd, args, "speed", SNDCTL_DSP_SPEED);
Greg Ward131bce02002-11-30 22:56:44 +0000350}
351
352static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000353oss_sync(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000354{
Greg Ward7b43c682002-12-30 02:29:28 +0000355 return _do_ioctl_0(self->fd, args, "sync", SNDCTL_DSP_SYNC);
Greg Ward131bce02002-11-30 22:56:44 +0000356}
357
358static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000359oss_reset(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000360{
Greg Ward7b43c682002-12-30 02:29:28 +0000361 return _do_ioctl_0(self->fd, args, "reset", SNDCTL_DSP_RESET);
Greg Ward131bce02002-11-30 22:56:44 +0000362}
363
364static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000365oss_post(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000366{
Greg Ward7b43c682002-12-30 02:29:28 +0000367 return _do_ioctl_0(self->fd, args, "post", SNDCTL_DSP_POST);
Greg Ward131bce02002-11-30 22:56:44 +0000368}
369
370
371/* Regular file methods: read(), write(), close(), etc. as well
372 as one convenience method, writeall(). */
373
Greg Ward04613a92002-11-30 22:47:45 +0000374static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000375oss_read(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000376{
377 int size, count;
378 char *cp;
379 PyObject *rv;
Greg Wardad4d9b92002-12-30 03:02:22 +0000380
Greg Ward04613a92002-11-30 22:47:45 +0000381 if (!PyArg_ParseTuple(args, "i:read", &size))
382 return NULL;
383 rv = PyString_FromStringAndSize(NULL, size);
384 if (rv == NULL)
385 return NULL;
386 cp = PyString_AS_STRING(rv);
Greg Ward7b43c682002-12-30 02:29:28 +0000387 if ((count = read(self->fd, cp, size)) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000388 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000389 Py_DECREF(rv);
390 return NULL;
391 }
Greg Ward7b43c682002-12-30 02:29:28 +0000392 self->icount += count;
Greg Ward04613a92002-11-30 22:47:45 +0000393 _PyString_Resize(&rv, count);
394 return rv;
395}
396
397static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000398oss_write(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000399{
400 char *cp;
401 int rv, size;
Greg Ward131bce02002-11-30 22:56:44 +0000402
403 if (!PyArg_ParseTuple(args, "s#:write", &cp, &size)) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000404 return NULL;
Greg Ward131bce02002-11-30 22:56:44 +0000405 }
Greg Ward7b43c682002-12-30 02:29:28 +0000406 if ((rv = write(self->fd, cp, size)) == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000407 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000408 } else {
Greg Ward7b43c682002-12-30 02:29:28 +0000409 self->ocount += rv;
Greg Ward131bce02002-11-30 22:56:44 +0000410 }
411 return PyInt_FromLong(rv);
412}
413
414static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000415oss_writeall(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000416{
417 char *cp;
418 int rv, size;
419 fd_set write_set_fds;
420 int select_rv;
421
422 /* NB. writeall() is only useful in non-blocking mode: according to
423 Guenter Geiger <geiger@xdv.org> on the linux-audio-dev list
424 (http://eca.cx/lad/2002/11/0380.html), OSS guarantees that
425 write() in blocking mode consumes the whole buffer. In blocking
426 mode, the behaviour of write() and writeall() from Python is
427 indistinguishable. */
428
429 if (!PyArg_ParseTuple(args, "s#:write", &cp, &size))
430 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000431
432 /* use select to wait for audio device to be available */
433 FD_ZERO(&write_set_fds);
Greg Ward7b43c682002-12-30 02:29:28 +0000434 FD_SET(self->fd, &write_set_fds);
Greg Ward04613a92002-11-30 22:47:45 +0000435
436 while (size > 0) {
Greg Ward7b43c682002-12-30 02:29:28 +0000437 select_rv = select(self->fd+1, NULL, &write_set_fds, NULL, NULL);
Greg Ward131bce02002-11-30 22:56:44 +0000438 assert(select_rv != 0); /* no timeout, can't expire */
439 if (select_rv == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000440 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000441
Greg Ward7b43c682002-12-30 02:29:28 +0000442 rv = write(self->fd, cp, size);
Greg Ward131bce02002-11-30 22:56:44 +0000443 if (rv == -1) {
444 if (errno == EAGAIN) { /* buffer is full, try again */
445 errno = 0;
446 continue;
447 } else /* it's a real error */
Greg Ward97708bc2002-11-30 23:17:10 +0000448 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000449 } else { /* wrote rv bytes */
Greg Ward7b43c682002-12-30 02:29:28 +0000450 self->ocount += rv;
Greg Ward131bce02002-11-30 22:56:44 +0000451 size -= rv;
452 cp += rv;
453 }
Greg Ward04613a92002-11-30 22:47:45 +0000454 }
455 Py_INCREF(Py_None);
456 return Py_None;
457}
458
459static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000460oss_close(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000461{
462 if (!PyArg_ParseTuple(args, ":close"))
Greg Wardad4d9b92002-12-30 03:02:22 +0000463 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000464
Greg Ward7b43c682002-12-30 02:29:28 +0000465 if (self->fd >= 0) {
466 close(self->fd);
467 self->fd = -1;
Greg Ward04613a92002-11-30 22:47:45 +0000468 }
469 Py_INCREF(Py_None);
470 return Py_None;
471}
472
473static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000474oss_fileno(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000475{
476 if (!PyArg_ParseTuple(args, ":fileno"))
Greg Wardad4d9b92002-12-30 03:02:22 +0000477 return NULL;
Greg Ward7b43c682002-12-30 02:29:28 +0000478 return PyInt_FromLong(self->fd);
Greg Ward04613a92002-11-30 22:47:45 +0000479}
480
Greg Ward131bce02002-11-30 22:56:44 +0000481
482/* Convenience methods: these generally wrap a couple of ioctls into one
483 common task. */
484
Greg Ward04613a92002-11-30 22:47:45 +0000485static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000486oss_setparameters(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000487{
488 int rate, ssize, nchannels, n, fmt, emulate=0;
489
490 if (!PyArg_ParseTuple(args, "iiii|i:setparameters",
491 &rate, &ssize, &nchannels, &fmt, &emulate))
492 return NULL;
493
494 if (rate < 0) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000495 PyErr_Format(PyExc_ValueError, "expected rate >= 0, not %d",
496 rate);
497 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000498 }
499 if (ssize < 0) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000500 PyErr_Format(PyExc_ValueError, "expected sample size >= 0, not %d",
501 ssize);
502 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000503 }
504 if (nchannels != 1 && nchannels != 2) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000505 PyErr_Format(PyExc_ValueError, "nchannels must be 1 or 2, not %d",
506 nchannels);
507 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000508 }
509
510 for (n = 0; n < n_audio_types; n++)
511 if (fmt == audio_types[n].a_fmt)
512 break;
513 if (n == n_audio_types) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000514 PyErr_Format(PyExc_ValueError, "unknown audio encoding: %d", fmt);
515 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000516 }
517 if (audio_types[n].a_bps != ssize) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000518 PyErr_Format(PyExc_ValueError,
519 "for %s, expected sample size %d, not %d",
520 audio_types[n].a_name, audio_types[n].a_bps, ssize);
521 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000522 }
523
524 if (emulate == 0) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000525 if ((self->afmts & audio_types[n].a_fmt) == 0) {
526 PyErr_Format(PyExc_ValueError,
527 "%s format not supported by device",
528 audio_types[n].a_name);
529 return NULL;
530 }
Greg Ward04613a92002-11-30 22:47:45 +0000531 }
Greg Ward7b43c682002-12-30 02:29:28 +0000532 if (ioctl(self->fd, SNDCTL_DSP_SETFMT,
Greg Wardad4d9b92002-12-30 03:02:22 +0000533 &audio_types[n].a_fmt) == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000534 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000535 return NULL;
536 }
Greg Ward7b43c682002-12-30 02:29:28 +0000537 if (ioctl(self->fd, SNDCTL_DSP_CHANNELS, &nchannels) == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000538 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000539 return NULL;
540 }
Greg Ward7b43c682002-12-30 02:29:28 +0000541 if (ioctl(self->fd, SNDCTL_DSP_SPEED, &rate) == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000542 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000543 return NULL;
544 }
545
546 Py_INCREF(Py_None);
547 return Py_None;
548}
549
550static int
Greg Ward499b73e2002-12-31 03:04:52 +0000551_ssize(oss_audio_t *self, int *nchannels, int *ssize)
Greg Ward04613a92002-11-30 22:47:45 +0000552{
553 int fmt;
554
555 fmt = 0;
Greg Ward7b43c682002-12-30 02:29:28 +0000556 if (ioctl(self->fd, SNDCTL_DSP_SETFMT, &fmt) < 0)
Greg Ward04613a92002-11-30 22:47:45 +0000557 return -errno;
558
559 switch (fmt) {
560 case AFMT_MU_LAW:
561 case AFMT_A_LAW:
562 case AFMT_U8:
563 case AFMT_S8:
564 *ssize = sizeof(char);
565 break;
566 case AFMT_S16_LE:
567 case AFMT_S16_BE:
568 case AFMT_U16_LE:
569 case AFMT_U16_BE:
570 *ssize = sizeof(short);
571 break;
572 case AFMT_MPEG:
573 case AFMT_IMA_ADPCM:
574 default:
575 return -EOPNOTSUPP;
576 }
577 *nchannels = 0;
Greg Ward7b43c682002-12-30 02:29:28 +0000578 if (ioctl(self->fd, SNDCTL_DSP_CHANNELS, nchannels) < 0)
Greg Ward04613a92002-11-30 22:47:45 +0000579 return -errno;
580 return 0;
581}
582
583
584/* bufsize returns the size of the hardware audio buffer in number
585 of samples */
586static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000587oss_bufsize(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000588{
589 audio_buf_info ai;
590 int nchannels, ssize;
591
592 if (!PyArg_ParseTuple(args, ":bufsize")) return NULL;
593
594 if (_ssize(self, &nchannels, &ssize) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000595 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000596 return NULL;
597 }
Greg Ward7b43c682002-12-30 02:29:28 +0000598 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000599 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000600 return NULL;
601 }
602 return PyInt_FromLong((ai.fragstotal * ai.fragsize) / (nchannels * ssize));
603}
604
605/* obufcount returns the number of samples that are available in the
606 hardware for playing */
607static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000608oss_obufcount(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000609{
610 audio_buf_info ai;
611 int nchannels, ssize;
612
613 if (!PyArg_ParseTuple(args, ":obufcount"))
614 return NULL;
615
616 if (_ssize(self, &nchannels, &ssize) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000617 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000618 return NULL;
619 }
Greg Ward7b43c682002-12-30 02:29:28 +0000620 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000621 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000622 return NULL;
623 }
624 return PyInt_FromLong((ai.fragstotal * ai.fragsize - ai.bytes) /
625 (ssize * nchannels));
626}
627
628/* obufcount returns the number of samples that can be played without
629 blocking */
630static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000631oss_obuffree(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000632{
633 audio_buf_info ai;
634 int nchannels, ssize;
635
636 if (!PyArg_ParseTuple(args, ":obuffree"))
637 return NULL;
638
639 if (_ssize(self, &nchannels, &ssize) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000640 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000641 return NULL;
642 }
Greg Ward7b43c682002-12-30 02:29:28 +0000643 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000644 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000645 return NULL;
646 }
647 return PyInt_FromLong(ai.bytes / (ssize * nchannels));
648}
649
Greg Ward04613a92002-11-30 22:47:45 +0000650static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000651oss_getptr(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000652{
653 count_info info;
654 int req;
655
656 if (!PyArg_ParseTuple(args, ":getptr"))
Greg Wardad4d9b92002-12-30 03:02:22 +0000657 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000658
Greg Ward7b43c682002-12-30 02:29:28 +0000659 if (self->mode == O_RDONLY)
Greg Wardad4d9b92002-12-30 03:02:22 +0000660 req = SNDCTL_DSP_GETIPTR;
Greg Ward04613a92002-11-30 22:47:45 +0000661 else
Greg Wardad4d9b92002-12-30 03:02:22 +0000662 req = SNDCTL_DSP_GETOPTR;
Greg Ward7b43c682002-12-30 02:29:28 +0000663 if (ioctl(self->fd, req, &info) == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000664 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000665 return NULL;
666 }
667 return Py_BuildValue("iii", info.bytes, info.blocks, info.ptr);
668}
669
Greg Wardda1cacb2002-12-31 03:02:23 +0000670
671/* ----------------------------------------------------------------------
672 * Methods of mixer objects (OSSMixerType)
673 */
674
Greg Ward3d9994d2002-12-11 15:12:01 +0000675static PyObject *
676oss_mixer_close(oss_mixer_t *self, PyObject *args)
677{
678 if (!PyArg_ParseTuple(args, ":close"))
Greg Wardad4d9b92002-12-30 03:02:22 +0000679 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000680
Greg Ward7b43c682002-12-30 02:29:28 +0000681 if (self->fd >= 0) {
682 close(self->fd);
683 self->fd = -1;
Greg Ward3d9994d2002-12-11 15:12:01 +0000684 }
685 Py_INCREF(Py_None);
686 return Py_None;
687}
688
689static PyObject *
690oss_mixer_fileno(oss_mixer_t *self, PyObject *args)
691{
692 if (!PyArg_ParseTuple(args, ":fileno"))
Greg Wardad4d9b92002-12-30 03:02:22 +0000693 return NULL;
Greg Ward7b43c682002-12-30 02:29:28 +0000694 return PyInt_FromLong(self->fd);
Greg Ward3d9994d2002-12-11 15:12:01 +0000695}
696
697/* Simple mixer interface methods */
698
699static PyObject *
Greg Ward2d6f9a92002-12-31 02:54:43 +0000700oss_mixer_controls(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000701{
Greg Ward2d6f9a92002-12-31 02:54:43 +0000702 return _do_ioctl_1_internal(self->fd, args, "controls",
Greg Ward3d9994d2002-12-11 15:12:01 +0000703 SOUND_MIXER_READ_DEVMASK);
704}
705
706static PyObject *
Greg Ward2d6f9a92002-12-31 02:54:43 +0000707oss_mixer_stereocontrols(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000708{
Greg Ward2d6f9a92002-12-31 02:54:43 +0000709 return _do_ioctl_1_internal(self->fd, args, "stereocontrols",
Greg Ward3d9994d2002-12-11 15:12:01 +0000710 SOUND_MIXER_READ_STEREODEVS);
711}
712
713static PyObject *
Greg Ward2d6f9a92002-12-31 02:54:43 +0000714oss_mixer_reccontrols(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000715{
Greg Ward2d6f9a92002-12-31 02:54:43 +0000716 return _do_ioctl_1_internal(self->fd, args, "reccontrols",
Greg Wardb69bb3d2002-12-12 17:35:45 +0000717 SOUND_MIXER_READ_RECMASK);
Greg Ward3d9994d2002-12-11 15:12:01 +0000718}
719
720static PyObject *
Greg Warde7037662002-12-30 03:01:48 +0000721oss_mixer_get(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000722{
723 int channel, volume;
724
725 /* Can't use _do_ioctl_1 because of encoded arg thingy. */
Greg Warde7037662002-12-30 03:01:48 +0000726 if (!PyArg_ParseTuple(args, "i:get", &channel))
Greg Wardad4d9b92002-12-30 03:02:22 +0000727 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000728
729 if (channel < 0 || channel > SOUND_MIXER_NRDEVICES) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000730 PyErr_SetString(OSSAudioError, "Invalid mixer channel specified.");
731 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000732 }
733
Greg Warde7037662002-12-30 03:01:48 +0000734 if (ioctl(self->fd, MIXER_READ(channel), &volume) == -1)
Greg Wardad4d9b92002-12-30 03:02:22 +0000735 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward3d9994d2002-12-11 15:12:01 +0000736
Greg Warde7037662002-12-30 03:01:48 +0000737 return Py_BuildValue("(ii)", volume & 0xff, (volume & 0xff00) >> 8);
Greg Ward3d9994d2002-12-11 15:12:01 +0000738}
739
740static PyObject *
Greg Warde7037662002-12-30 03:01:48 +0000741oss_mixer_set(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000742{
743 int channel, volume, leftVol, rightVol;
744
745 /* Can't use _do_ioctl_1 because of encoded arg thingy. */
Greg Warde7037662002-12-30 03:01:48 +0000746 if (!PyArg_ParseTuple(args, "i(ii):set", &channel, &leftVol, &rightVol))
Greg Wardad4d9b92002-12-30 03:02:22 +0000747 return NULL;
748
Greg Ward3d9994d2002-12-11 15:12:01 +0000749 if (channel < 0 || channel > SOUND_MIXER_NRDEVICES) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000750 PyErr_SetString(OSSAudioError, "Invalid mixer channel specified.");
751 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000752 }
753
754 if (leftVol < 0 || rightVol < 0 || leftVol > 100 || rightVol > 100) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000755 PyErr_SetString(OSSAudioError, "Volumes must be between 0 and 100.");
756 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000757 }
758
759 volume = (rightVol << 8) | leftVol;
760
Greg Warde7037662002-12-30 03:01:48 +0000761 if (ioctl(self->fd, MIXER_WRITE(channel), &volume) == -1)
Greg Wardad4d9b92002-12-30 03:02:22 +0000762 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward3d9994d2002-12-11 15:12:01 +0000763
Greg Warde7037662002-12-30 03:01:48 +0000764 return Py_BuildValue("(ii)", volume & 0xff, (volume & 0xff00) >> 8);
Greg Ward3d9994d2002-12-11 15:12:01 +0000765}
766
767static PyObject *
Greg Wardf05aa102002-12-30 23:19:32 +0000768oss_mixer_get_recsrc(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000769{
Greg Wardf05aa102002-12-30 23:19:32 +0000770 return _do_ioctl_1_internal(self->fd, args, "get_recsrc",
Greg Ward3d9994d2002-12-11 15:12:01 +0000771 SOUND_MIXER_READ_RECSRC);
772}
773
774static PyObject *
Greg Wardf05aa102002-12-30 23:19:32 +0000775oss_mixer_set_recsrc(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000776{
Greg Wardf05aa102002-12-30 23:19:32 +0000777 return _do_ioctl_1(self->fd, args, "set_recsrc",
Greg Ward3d9994d2002-12-11 15:12:01 +0000778 SOUND_MIXER_WRITE_RECSRC);
779}
780
781
Greg Wardda1cacb2002-12-31 03:02:23 +0000782/* ----------------------------------------------------------------------
783 * Method tables and other bureaucracy
784 */
785
Greg Ward8c6b6a92002-12-11 14:43:13 +0000786static PyMethodDef oss_methods[] = {
Greg Ward131bce02002-11-30 22:56:44 +0000787 /* Regular file methods */
Greg Wardad4d9b92002-12-30 03:02:22 +0000788 { "read", (PyCFunction)oss_read, METH_VARARGS },
789 { "write", (PyCFunction)oss_write, METH_VARARGS },
790 { "writeall", (PyCFunction)oss_writeall, METH_VARARGS },
791 { "close", (PyCFunction)oss_close, METH_VARARGS },
792 { "fileno", (PyCFunction)oss_fileno, METH_VARARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000793
794 /* Simple ioctl wrappers */
Greg Ward8c6b6a92002-12-11 14:43:13 +0000795 { "nonblock", (PyCFunction)oss_nonblock, METH_VARARGS },
Greg Wardad4d9b92002-12-30 03:02:22 +0000796 { "setfmt", (PyCFunction)oss_setfmt, METH_VARARGS },
Greg Ward8c6b6a92002-12-11 14:43:13 +0000797 { "getfmts", (PyCFunction)oss_getfmts, METH_VARARGS },
798 { "channels", (PyCFunction)oss_channels, METH_VARARGS },
799 { "speed", (PyCFunction)oss_speed, METH_VARARGS },
Greg Wardad4d9b92002-12-30 03:02:22 +0000800 { "sync", (PyCFunction)oss_sync, METH_VARARGS },
801 { "reset", (PyCFunction)oss_reset, METH_VARARGS },
802 { "post", (PyCFunction)oss_post, METH_VARARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000803
804 /* Convenience methods -- wrap a couple of ioctls together */
Greg Wardad4d9b92002-12-30 03:02:22 +0000805 { "setparameters", (PyCFunction)oss_setparameters, METH_VARARGS },
806 { "bufsize", (PyCFunction)oss_bufsize, METH_VARARGS },
807 { "obufcount", (PyCFunction)oss_obufcount, METH_VARARGS },
808 { "obuffree", (PyCFunction)oss_obuffree, METH_VARARGS },
Greg Ward8c6b6a92002-12-11 14:43:13 +0000809 { "getptr", (PyCFunction)oss_getptr, METH_VARARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000810
811 /* Aliases for backwards compatibility */
Greg Wardad4d9b92002-12-30 03:02:22 +0000812 { "flush", (PyCFunction)oss_sync, METH_VARARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000813
Greg Wardad4d9b92002-12-30 03:02:22 +0000814 { NULL, NULL} /* sentinel */
Greg Ward04613a92002-11-30 22:47:45 +0000815};
816
Greg Ward3d9994d2002-12-11 15:12:01 +0000817static PyMethodDef oss_mixer_methods[] = {
818 /* Regular file method - OSS mixers are ioctl-only interface */
Greg Wardad4d9b92002-12-30 03:02:22 +0000819 { "close", (PyCFunction)oss_mixer_close, METH_VARARGS },
820 { "fileno", (PyCFunction)oss_mixer_fileno, METH_VARARGS },
Greg Ward3d9994d2002-12-11 15:12:01 +0000821
822 /* Simple ioctl wrappers */
Greg Ward2d6f9a92002-12-31 02:54:43 +0000823 { "controls", (PyCFunction)oss_mixer_controls, METH_VARARGS },
824 { "stereocontrols", (PyCFunction)oss_mixer_stereocontrols, METH_VARARGS},
825 { "reccontrols", (PyCFunction)oss_mixer_reccontrols, METH_VARARGS},
Greg Wardad4d9b92002-12-30 03:02:22 +0000826 { "get", (PyCFunction)oss_mixer_get, METH_VARARGS },
827 { "set", (PyCFunction)oss_mixer_set, METH_VARARGS },
Greg Wardf05aa102002-12-30 23:19:32 +0000828 { "get_recsrc", (PyCFunction)oss_mixer_get_recsrc, METH_VARARGS },
829 { "set_recsrc", (PyCFunction)oss_mixer_set_recsrc, METH_VARARGS },
Greg Ward3d9994d2002-12-11 15:12:01 +0000830
Greg Wardad4d9b92002-12-30 03:02:22 +0000831 { NULL, NULL}
Greg Ward3d9994d2002-12-11 15:12:01 +0000832};
833
Greg Ward04613a92002-11-30 22:47:45 +0000834static PyObject *
Greg Ward58ae13c2002-12-31 03:07:21 +0000835oss_getattr(oss_audio_t *self, char *name)
Greg Ward04613a92002-11-30 22:47:45 +0000836{
Greg Ward58ae13c2002-12-31 03:07:21 +0000837 return Py_FindMethod(oss_methods, (PyObject *)self, name);
Greg Ward04613a92002-11-30 22:47:45 +0000838}
839
Greg Ward3d9994d2002-12-11 15:12:01 +0000840static PyObject *
Greg Ward58ae13c2002-12-31 03:07:21 +0000841oss_mixer_getattr(oss_mixer_t *self, char *name)
Greg Ward3d9994d2002-12-11 15:12:01 +0000842{
Greg Ward58ae13c2002-12-31 03:07:21 +0000843 return Py_FindMethod(oss_mixer_methods, (PyObject *)self, name);
Greg Ward3d9994d2002-12-11 15:12:01 +0000844}
845
Greg Ward499b73e2002-12-31 03:04:52 +0000846static PyTypeObject OSSAudioType = {
Greg Ward04613a92002-11-30 22:47:45 +0000847 PyObject_HEAD_INIT(&PyType_Type)
Greg Wardad4d9b92002-12-30 03:02:22 +0000848 0, /*ob_size*/
Greg Ward9a568eb2002-11-30 23:20:09 +0000849 "ossaudiodev.oss_audio_device", /*tp_name*/
Greg Ward499b73e2002-12-31 03:04:52 +0000850 sizeof(oss_audio_t), /*tp_size*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000851 0, /*tp_itemsize*/
Greg Ward04613a92002-11-30 22:47:45 +0000852 /* methods */
Greg Wardad4d9b92002-12-30 03:02:22 +0000853 (destructor)oss_dealloc, /*tp_dealloc*/
854 0, /*tp_print*/
855 (getattrfunc)oss_getattr, /*tp_getattr*/
856 0, /*tp_setattr*/
857 0, /*tp_compare*/
858 0, /*tp_repr*/
Greg Ward04613a92002-11-30 22:47:45 +0000859};
860
Greg Ward3d9994d2002-12-11 15:12:01 +0000861static PyTypeObject OSSMixerType = {
862 PyObject_HEAD_INIT(&PyType_Type)
Greg Wardad4d9b92002-12-30 03:02:22 +0000863 0, /*ob_size*/
Greg Ward3d9994d2002-12-11 15:12:01 +0000864 "ossaudiodev.oss_mixer_device", /*tp_name*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000865 sizeof(oss_mixer_t), /*tp_size*/
866 0, /*tp_itemsize*/
Greg Ward3d9994d2002-12-11 15:12:01 +0000867 /* methods */
868 (destructor)oss_mixer_dealloc, /*tp_dealloc*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000869 0, /*tp_print*/
Greg Ward3d9994d2002-12-11 15:12:01 +0000870 (getattrfunc)oss_mixer_getattr, /*tp_getattr*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000871 0, /*tp_setattr*/
872 0, /*tp_compare*/
873 0, /*tp_repr*/
Greg Ward3d9994d2002-12-11 15:12:01 +0000874};
875
876
Greg Ward04613a92002-11-30 22:47:45 +0000877static PyObject *
Greg Ward9a568eb2002-11-30 23:20:09 +0000878ossopen(PyObject *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000879{
Greg Ward8c6b6a92002-12-11 14:43:13 +0000880 return (PyObject *)newossobject(args);
Greg Ward04613a92002-11-30 22:47:45 +0000881}
882
Greg Ward3d9994d2002-12-11 15:12:01 +0000883static PyObject *
884ossopenmixer(PyObject *self, PyObject *args)
885{
886 return (PyObject *)newossmixerobject(args);
887}
888
Greg Ward9a568eb2002-11-30 23:20:09 +0000889static PyMethodDef ossaudiodev_methods[] = {
890 { "open", ossopen, METH_VARARGS },
Greg Ward3d9994d2002-12-11 15:12:01 +0000891 { "openmixer", ossopenmixer, METH_VARARGS },
Greg Ward04613a92002-11-30 22:47:45 +0000892 { 0, 0 },
893};
894
Greg Ward1e0f57d2002-11-30 23:05:26 +0000895
896#define _EXPORT_INT(mod, name) \
897 if (PyModule_AddIntConstant(mod, #name, (long) (name)) == -1) return;
898
Greg Ward744f0fd2002-12-31 03:23:59 +0000899
900static char *control_labels[] = SOUND_DEVICE_LABELS;
901static char *control_names[] = SOUND_DEVICE_NAMES;
902
903
904static int
905build_namelists (PyObject *module)
906{
907 PyObject *labels;
908 PyObject *names;
909 PyObject *s;
910 int num_controls;
911 int i;
912
913 num_controls = sizeof(control_labels) / sizeof(control_labels[0]);
914 assert(num_controls == sizeof(control_names) / sizeof(control_names[0]));
915
916 labels = PyList_New(num_controls);
917 names = PyList_New(num_controls);
918 for (i = 0; i < num_controls; i++) {
919 s = PyString_FromString(control_labels[i]);
920 if (s == NULL)
921 return -1;
922 PyList_SET_ITEM(labels, i, s);
923
924 s = PyString_FromString(control_names[i]);
925 if (s == NULL)
926 return -1;
927 PyList_SET_ITEM(names, i, s);
928 }
929
930 if (PyModule_AddObject(module, "control_labels", labels) == -1)
931 return -1;
932 if (PyModule_AddObject(module, "control_names", names) == -1)
933 return -1;
934
935 return 0;
936}
937
938
Greg Ward04613a92002-11-30 22:47:45 +0000939void
Greg Ward9a568eb2002-11-30 23:20:09 +0000940initossaudiodev(void)
Greg Ward04613a92002-11-30 22:47:45 +0000941{
942 PyObject *m;
943
Greg Ward9a568eb2002-11-30 23:20:09 +0000944 m = Py_InitModule("ossaudiodev", ossaudiodev_methods);
Greg Ward04613a92002-11-30 22:47:45 +0000945
Greg Ward97708bc2002-11-30 23:17:10 +0000946 OSSAudioError = PyErr_NewException("ossaudiodev.error", NULL, NULL);
947 if (OSSAudioError)
Greg Wardad4d9b92002-12-30 03:02:22 +0000948 PyModule_AddObject(m, "error", OSSAudioError);
Greg Ward04613a92002-11-30 22:47:45 +0000949
Greg Ward744f0fd2002-12-31 03:23:59 +0000950 /* Build 'control_labels' and 'control_names' lists and add them
951 to the module. */
952 if (build_namelists(m) == -1) /* XXX what to do here? */
953 return;
954
Greg Ward1e0f57d2002-11-30 23:05:26 +0000955 /* Expose the audio format numbers -- essential! */
956 _EXPORT_INT(m, AFMT_QUERY);
957 _EXPORT_INT(m, AFMT_MU_LAW);
958 _EXPORT_INT(m, AFMT_A_LAW);
959 _EXPORT_INT(m, AFMT_IMA_ADPCM);
960 _EXPORT_INT(m, AFMT_U8);
961 _EXPORT_INT(m, AFMT_S16_LE);
962 _EXPORT_INT(m, AFMT_S16_BE);
963 _EXPORT_INT(m, AFMT_S8);
964 _EXPORT_INT(m, AFMT_U16_LE);
965 _EXPORT_INT(m, AFMT_U16_BE);
966 _EXPORT_INT(m, AFMT_MPEG);
967 _EXPORT_INT(m, AFMT_AC3);
Neal Norwitzd156c2d2003-02-02 17:59:06 +0000968#ifdef AFMT_S16_NE
Greg Ward1e0f57d2002-11-30 23:05:26 +0000969 _EXPORT_INT(m, AFMT_S16_NE);
Neal Norwitzd156c2d2003-02-02 17:59:06 +0000970#endif
Greg Wardad4d9b92002-12-30 03:02:22 +0000971
Greg Ward37897c22002-12-30 02:43:36 +0000972 /* Expose the sound mixer device numbers. */
Greg Ward7b43c682002-12-30 02:29:28 +0000973 _EXPORT_INT(m, SOUND_MIXER_NRDEVICES);
Greg Ward3d9994d2002-12-11 15:12:01 +0000974 _EXPORT_INT(m, SOUND_MIXER_VOLUME);
975 _EXPORT_INT(m, SOUND_MIXER_BASS);
976 _EXPORT_INT(m, SOUND_MIXER_TREBLE);
977 _EXPORT_INT(m, SOUND_MIXER_SYNTH);
978 _EXPORT_INT(m, SOUND_MIXER_PCM);
979 _EXPORT_INT(m, SOUND_MIXER_SPEAKER);
980 _EXPORT_INT(m, SOUND_MIXER_LINE);
981 _EXPORT_INT(m, SOUND_MIXER_MIC);
982 _EXPORT_INT(m, SOUND_MIXER_CD);
983 _EXPORT_INT(m, SOUND_MIXER_IMIX);
984 _EXPORT_INT(m, SOUND_MIXER_ALTPCM);
985 _EXPORT_INT(m, SOUND_MIXER_RECLEV);
986 _EXPORT_INT(m, SOUND_MIXER_IGAIN);
987 _EXPORT_INT(m, SOUND_MIXER_OGAIN);
988 _EXPORT_INT(m, SOUND_MIXER_LINE1);
989 _EXPORT_INT(m, SOUND_MIXER_LINE2);
990 _EXPORT_INT(m, SOUND_MIXER_LINE3);
991 _EXPORT_INT(m, SOUND_MIXER_DIGITAL1);
992 _EXPORT_INT(m, SOUND_MIXER_DIGITAL2);
993 _EXPORT_INT(m, SOUND_MIXER_DIGITAL3);
994 _EXPORT_INT(m, SOUND_MIXER_PHONEIN);
995 _EXPORT_INT(m, SOUND_MIXER_PHONEOUT);
996 _EXPORT_INT(m, SOUND_MIXER_VIDEO);
997 _EXPORT_INT(m, SOUND_MIXER_RADIO);
998 _EXPORT_INT(m, SOUND_MIXER_MONITOR);
Greg Ward04613a92002-11-30 22:47:45 +0000999
Greg Ward1e0f57d2002-11-30 23:05:26 +00001000 /* Expose all the ioctl numbers for masochists who like to do this
1001 stuff directly. */
1002 _EXPORT_INT(m, SNDCTL_COPR_HALT);
1003 _EXPORT_INT(m, SNDCTL_COPR_LOAD);
1004 _EXPORT_INT(m, SNDCTL_COPR_RCODE);
1005 _EXPORT_INT(m, SNDCTL_COPR_RCVMSG);
1006 _EXPORT_INT(m, SNDCTL_COPR_RDATA);
1007 _EXPORT_INT(m, SNDCTL_COPR_RESET);
1008 _EXPORT_INT(m, SNDCTL_COPR_RUN);
1009 _EXPORT_INT(m, SNDCTL_COPR_SENDMSG);
1010 _EXPORT_INT(m, SNDCTL_COPR_WCODE);
1011 _EXPORT_INT(m, SNDCTL_COPR_WDATA);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001012#ifdef SNDCTL_DSP_BIND_CHANNEL
Greg Ward1e0f57d2002-11-30 23:05:26 +00001013 _EXPORT_INT(m, SNDCTL_DSP_BIND_CHANNEL);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001014#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001015 _EXPORT_INT(m, SNDCTL_DSP_CHANNELS);
1016 _EXPORT_INT(m, SNDCTL_DSP_GETBLKSIZE);
1017 _EXPORT_INT(m, SNDCTL_DSP_GETCAPS);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001018#ifdef SNDCTL_DSP_GETCHANNELMASK
Greg Ward1e0f57d2002-11-30 23:05:26 +00001019 _EXPORT_INT(m, SNDCTL_DSP_GETCHANNELMASK);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001020#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001021 _EXPORT_INT(m, SNDCTL_DSP_GETFMTS);
1022 _EXPORT_INT(m, SNDCTL_DSP_GETIPTR);
1023 _EXPORT_INT(m, SNDCTL_DSP_GETISPACE);
1024 _EXPORT_INT(m, SNDCTL_DSP_GETODELAY);
1025 _EXPORT_INT(m, SNDCTL_DSP_GETOPTR);
1026 _EXPORT_INT(m, SNDCTL_DSP_GETOSPACE);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001027#ifdef SNDCTL_DSP_GETSPDIF
Greg Ward1e0f57d2002-11-30 23:05:26 +00001028 _EXPORT_INT(m, SNDCTL_DSP_GETSPDIF);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001029#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001030 _EXPORT_INT(m, SNDCTL_DSP_GETTRIGGER);
1031 _EXPORT_INT(m, SNDCTL_DSP_MAPINBUF);
1032 _EXPORT_INT(m, SNDCTL_DSP_MAPOUTBUF);
1033 _EXPORT_INT(m, SNDCTL_DSP_NONBLOCK);
1034 _EXPORT_INT(m, SNDCTL_DSP_POST);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001035#ifdef SNDCTL_DSP_PROFILE
Greg Ward1e0f57d2002-11-30 23:05:26 +00001036 _EXPORT_INT(m, SNDCTL_DSP_PROFILE);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001037#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001038 _EXPORT_INT(m, SNDCTL_DSP_RESET);
1039 _EXPORT_INT(m, SNDCTL_DSP_SAMPLESIZE);
1040 _EXPORT_INT(m, SNDCTL_DSP_SETDUPLEX);
1041 _EXPORT_INT(m, SNDCTL_DSP_SETFMT);
1042 _EXPORT_INT(m, SNDCTL_DSP_SETFRAGMENT);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001043#ifdef SNDCTL_DSP_SETSPDIF
Greg Ward1e0f57d2002-11-30 23:05:26 +00001044 _EXPORT_INT(m, SNDCTL_DSP_SETSPDIF);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001045#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001046 _EXPORT_INT(m, SNDCTL_DSP_SETSYNCRO);
1047 _EXPORT_INT(m, SNDCTL_DSP_SETTRIGGER);
1048 _EXPORT_INT(m, SNDCTL_DSP_SPEED);
1049 _EXPORT_INT(m, SNDCTL_DSP_STEREO);
1050 _EXPORT_INT(m, SNDCTL_DSP_SUBDIVIDE);
1051 _EXPORT_INT(m, SNDCTL_DSP_SYNC);
1052 _EXPORT_INT(m, SNDCTL_FM_4OP_ENABLE);
1053 _EXPORT_INT(m, SNDCTL_FM_LOAD_INSTR);
1054 _EXPORT_INT(m, SNDCTL_MIDI_INFO);
1055 _EXPORT_INT(m, SNDCTL_MIDI_MPUCMD);
1056 _EXPORT_INT(m, SNDCTL_MIDI_MPUMODE);
1057 _EXPORT_INT(m, SNDCTL_MIDI_PRETIME);
1058 _EXPORT_INT(m, SNDCTL_SEQ_CTRLRATE);
1059 _EXPORT_INT(m, SNDCTL_SEQ_GETINCOUNT);
1060 _EXPORT_INT(m, SNDCTL_SEQ_GETOUTCOUNT);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001061#ifdef SNDCTL_SEQ_GETTIME
Greg Ward1e0f57d2002-11-30 23:05:26 +00001062 _EXPORT_INT(m, SNDCTL_SEQ_GETTIME);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001063#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001064 _EXPORT_INT(m, SNDCTL_SEQ_NRMIDIS);
1065 _EXPORT_INT(m, SNDCTL_SEQ_NRSYNTHS);
1066 _EXPORT_INT(m, SNDCTL_SEQ_OUTOFBAND);
1067 _EXPORT_INT(m, SNDCTL_SEQ_PANIC);
1068 _EXPORT_INT(m, SNDCTL_SEQ_PERCMODE);
1069 _EXPORT_INT(m, SNDCTL_SEQ_RESET);
1070 _EXPORT_INT(m, SNDCTL_SEQ_RESETSAMPLES);
1071 _EXPORT_INT(m, SNDCTL_SEQ_SYNC);
1072 _EXPORT_INT(m, SNDCTL_SEQ_TESTMIDI);
1073 _EXPORT_INT(m, SNDCTL_SEQ_THRESHOLD);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001074#ifdef SNDCTL_SYNTH_CONTROL
Greg Ward1e0f57d2002-11-30 23:05:26 +00001075 _EXPORT_INT(m, SNDCTL_SYNTH_CONTROL);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001076#endif
1077#ifdef SNDCTL_SYNTH_ID
Greg Ward1e0f57d2002-11-30 23:05:26 +00001078 _EXPORT_INT(m, SNDCTL_SYNTH_ID);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001079#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001080 _EXPORT_INT(m, SNDCTL_SYNTH_INFO);
1081 _EXPORT_INT(m, SNDCTL_SYNTH_MEMAVL);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001082#ifdef SNDCTL_SYNTH_REMOVESAMPLE
Greg Ward1e0f57d2002-11-30 23:05:26 +00001083 _EXPORT_INT(m, SNDCTL_SYNTH_REMOVESAMPLE);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001084#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001085 _EXPORT_INT(m, SNDCTL_TMR_CONTINUE);
1086 _EXPORT_INT(m, SNDCTL_TMR_METRONOME);
1087 _EXPORT_INT(m, SNDCTL_TMR_SELECT);
1088 _EXPORT_INT(m, SNDCTL_TMR_SOURCE);
1089 _EXPORT_INT(m, SNDCTL_TMR_START);
1090 _EXPORT_INT(m, SNDCTL_TMR_STOP);
1091 _EXPORT_INT(m, SNDCTL_TMR_TEMPO);
1092 _EXPORT_INT(m, SNDCTL_TMR_TIMEBASE);
Greg Ward04613a92002-11-30 22:47:45 +00001093}