blob: 4d2184ef262004d64005db83286365d20221da2b [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 Ward76ffb192003-04-04 01:47:42 +0000142
143 /* And (try to) put it back in blocking mode so we get the
144 expected write() semantics. */
145 if (fcntl(fd, F_SETFL, 0) == -1) {
146 close(fd);
147 PyErr_SetFromErrnoWithFilename(PyExc_IOError, basedev);
148 return NULL;
149 }
150
Greg Ward04613a92002-11-30 22:47:45 +0000151 if (ioctl(fd, SNDCTL_DSP_GETFMTS, &afmts) == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000152 PyErr_SetFromErrnoWithFilename(PyExc_IOError, basedev);
Greg Ward04613a92002-11-30 22:47:45 +0000153 return NULL;
154 }
155 /* Create and initialize the object */
Greg Ward58ae13c2002-12-31 03:07:21 +0000156 if ((self = PyObject_New(oss_audio_t, &OSSAudioType)) == NULL) {
Greg Ward04613a92002-11-30 22:47:45 +0000157 close(fd);
158 return NULL;
159 }
Greg Ward58ae13c2002-12-31 03:07:21 +0000160 self->fd = fd;
161 self->mode = imode;
162 self->icount = self->ocount = 0;
163 self->afmts = afmts;
164 return self;
Greg Ward04613a92002-11-30 22:47:45 +0000165}
166
167static void
Greg Ward58ae13c2002-12-31 03:07:21 +0000168oss_dealloc(oss_audio_t *self)
Greg Ward04613a92002-11-30 22:47:45 +0000169{
170 /* if already closed, don't reclose it */
Greg Ward58ae13c2002-12-31 03:07:21 +0000171 if (self->fd != -1)
172 close(self->fd);
173 PyObject_Del(self);
Greg Ward04613a92002-11-30 22:47:45 +0000174}
175
Greg Wardda1cacb2002-12-31 03:02:23 +0000176
177/* ----------------------------------------------------------------------
178 * Mixer object initialization/deallocation
179 */
180
Greg Ward3d9994d2002-12-11 15:12:01 +0000181static oss_mixer_t *
182newossmixerobject(PyObject *arg)
183{
Greg Ward0b6dfb82003-03-10 03:17:06 +0000184 char *basedev = NULL;
185 int fd;
Greg Ward58ae13c2002-12-31 03:07:21 +0000186 oss_mixer_t *self;
Greg Ward3d9994d2002-12-11 15:12:01 +0000187
Greg Ward0b6dfb82003-03-10 03:17:06 +0000188 if (!PyArg_ParseTuple(arg, "|s", &basedev)) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000189 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000190 }
191
192 if (basedev == NULL) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000193 basedev = getenv("MIXERDEV");
194 if (basedev == NULL) /* MIXERDEV not set */
195 basedev = "/dev/mixer";
Greg Ward3d9994d2002-12-11 15:12:01 +0000196 }
197
Greg Ward0b6dfb82003-03-10 03:17:06 +0000198 if ((fd = open(basedev, O_RDWR)) == -1) {
Greg Ward3d9994d2002-12-11 15:12:01 +0000199 PyErr_SetFromErrnoWithFilename(PyExc_IOError, basedev);
200 return NULL;
201 }
Greg Ward0b6dfb82003-03-10 03:17:06 +0000202
Greg Ward58ae13c2002-12-31 03:07:21 +0000203 if ((self = PyObject_New(oss_mixer_t, &OSSMixerType)) == NULL) {
Greg Ward3d9994d2002-12-11 15:12:01 +0000204 close(fd);
205 return NULL;
206 }
207
Greg Ward58ae13c2002-12-31 03:07:21 +0000208 self->fd = fd;
Greg Ward3d9994d2002-12-11 15:12:01 +0000209
Greg Ward58ae13c2002-12-31 03:07:21 +0000210 return self;
Greg Ward3d9994d2002-12-11 15:12:01 +0000211}
212
213static void
Greg Ward58ae13c2002-12-31 03:07:21 +0000214oss_mixer_dealloc(oss_mixer_t *self)
Greg Ward3d9994d2002-12-11 15:12:01 +0000215{
216 /* if already closed, don't reclose it */
Greg Ward58ae13c2002-12-31 03:07:21 +0000217 if (self->fd != -1)
218 close(self->fd);
219 PyObject_Del(self);
Greg Ward3d9994d2002-12-11 15:12:01 +0000220}
221
Greg Ward131bce02002-11-30 22:56:44 +0000222
223/* Methods to wrap the OSS ioctls. The calling convention is pretty
224 simple:
225 nonblock() -> ioctl(fd, SNDCTL_DSP_NONBLOCK)
226 fmt = setfmt(fmt) -> ioctl(fd, SNDCTL_DSP_SETFMT, &fmt)
227 etc.
228*/
229
230
231/* _do_ioctl_1() is a private helper function used for the OSS ioctls --
232 SNDCTL_DSP_{SETFMT,CHANNELS,SPEED} -- that that are called from C
233 like this:
234 ioctl(fd, SNDCTL_DSP_cmd, &arg)
235
236 where arg is the value to set, and on return the driver sets arg to
237 the value that was actually set. Mapping this to Python is obvious:
238 arg = dsp.xxx(arg)
239*/
240static PyObject *
Greg Wardda9f8532002-12-11 14:49:59 +0000241_do_ioctl_1(int fd, PyObject *args, char *fname, int cmd)
Greg Ward131bce02002-11-30 22:56:44 +0000242{
Greg Wardda9f8532002-12-11 14:49:59 +0000243 char argfmt[33] = "i:";
Greg Ward131bce02002-11-30 22:56:44 +0000244 int arg;
245
Greg Wardda9f8532002-12-11 14:49:59 +0000246 assert(strlen(fname) <= 30);
Greg Ward131bce02002-11-30 22:56:44 +0000247 strcat(argfmt, fname);
248 if (!PyArg_ParseTuple(args, argfmt, &arg))
Greg Wardad4d9b92002-12-30 03:02:22 +0000249 return NULL;
Greg Ward131bce02002-11-30 22:56:44 +0000250
Greg Wardda9f8532002-12-11 14:49:59 +0000251 if (ioctl(fd, cmd, &arg) == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000252 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000253 return PyInt_FromLong(arg);
254}
255
Greg Wardda1cacb2002-12-31 03:02:23 +0000256
257/* ----------------------------------------------------------------------
258 * Helper functions
259 */
260
Greg Ward3d9994d2002-12-11 15:12:01 +0000261/* _do_ioctl_1_internal() is a wrapper for ioctls that take no inputs
262 but return an output -- ie. we need to pass a pointer to a local C
263 variable so the driver can write its output there, but from Python
264 all we see is the return value. For example,
265 SOUND_MIXER_READ_DEVMASK returns a bitmask of available mixer
266 devices, but does not use the value of the parameter passed-in in any
267 way.
268*/
269
270static PyObject *
271_do_ioctl_1_internal(int fd, PyObject *args, char *fname, int cmd)
272{
273 char argfmt[32] = ":";
274 int arg = 0;
275
276 assert(strlen(fname) <= 30);
277 strcat(argfmt, fname);
278 if (!PyArg_ParseTuple(args, argfmt, &arg))
Greg Wardad4d9b92002-12-30 03:02:22 +0000279 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000280
281 if (ioctl(fd, cmd, &arg) == -1)
282 return PyErr_SetFromErrno(PyExc_IOError);
283 return PyInt_FromLong(arg);
284}
285
286
287
Greg Ward131bce02002-11-30 22:56:44 +0000288/* _do_ioctl_0() is a private helper for the no-argument ioctls:
289 SNDCTL_DSP_{SYNC,RESET,POST}. */
290static PyObject *
Greg Wardda9f8532002-12-11 14:49:59 +0000291_do_ioctl_0(int fd, PyObject *args, char *fname, int cmd)
Greg Ward131bce02002-11-30 22:56:44 +0000292{
Greg Wardda9f8532002-12-11 14:49:59 +0000293 char argfmt[32] = ":";
Greg Wardd0d592f2003-05-27 01:57:21 +0000294 int rv;
Greg Ward131bce02002-11-30 22:56:44 +0000295
Greg Wardda9f8532002-12-11 14:49:59 +0000296 assert(strlen(fname) <= 30);
Greg Ward131bce02002-11-30 22:56:44 +0000297 strcat(argfmt, fname);
298 if (!PyArg_ParseTuple(args, argfmt))
Greg Wardad4d9b92002-12-30 03:02:22 +0000299 return NULL;
Greg Ward131bce02002-11-30 22:56:44 +0000300
Greg Wardd0d592f2003-05-27 01:57:21 +0000301 /* According to hannu@opensound.com, all three of the ioctls that
302 use this function can block, so release the GIL. This is
303 especially important for SYNC, which can block for several
304 seconds. */
305 Py_BEGIN_ALLOW_THREADS
306 rv = ioctl(fd, cmd, 0);
307 Py_END_ALLOW_THREADS
308
309 if (rv == -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
315
Greg Wardda1cacb2002-12-31 03:02:23 +0000316/* ----------------------------------------------------------------------
Greg Ward499b73e2002-12-31 03:04:52 +0000317 * Methods of DSP objects (OSSAudioType)
Greg Wardda1cacb2002-12-31 03:02:23 +0000318 */
319
Greg Ward131bce02002-11-30 22:56:44 +0000320static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000321oss_nonblock(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000322{
323 /* Hmmm: it doesn't appear to be possible to return to blocking
324 mode once we're in non-blocking mode! */
325 if (!PyArg_ParseTuple(args, ":nonblock"))
Greg Wardad4d9b92002-12-30 03:02:22 +0000326 return NULL;
Greg Ward7b43c682002-12-30 02:29:28 +0000327 if (ioctl(self->fd, SNDCTL_DSP_NONBLOCK, NULL) == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000328 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000329 Py_INCREF(Py_None);
330 return Py_None;
331}
332
333static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000334oss_setfmt(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000335{
Greg Ward7b43c682002-12-30 02:29:28 +0000336 return _do_ioctl_1(self->fd, args, "setfmt", SNDCTL_DSP_SETFMT);
Greg Ward131bce02002-11-30 22:56:44 +0000337}
338
339static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000340oss_getfmts(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000341{
342 int mask;
343 if (!PyArg_ParseTuple(args, ":getfmts"))
Greg Wardad4d9b92002-12-30 03:02:22 +0000344 return NULL;
Greg Ward7b43c682002-12-30 02:29:28 +0000345 if (ioctl(self->fd, SNDCTL_DSP_GETFMTS, &mask) == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000346 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000347 return PyInt_FromLong(mask);
348}
349
350static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000351oss_channels(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_1(self->fd, args, "channels", SNDCTL_DSP_CHANNELS);
Greg Ward131bce02002-11-30 22:56:44 +0000354}
355
356static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000357oss_speed(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_1(self->fd, args, "speed", SNDCTL_DSP_SPEED);
Greg Ward131bce02002-11-30 22:56:44 +0000360}
361
362static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000363oss_sync(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000364{
Greg Wardd0d592f2003-05-27 01:57:21 +0000365 return _do_ioctl_0(self->fd, args, "sync", SNDCTL_DSP_SYNC);
Greg Ward131bce02002-11-30 22:56:44 +0000366}
367
368static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000369oss_reset(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000370{
Greg Ward7b43c682002-12-30 02:29:28 +0000371 return _do_ioctl_0(self->fd, args, "reset", SNDCTL_DSP_RESET);
Greg Ward131bce02002-11-30 22:56:44 +0000372}
373
374static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000375oss_post(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000376{
Greg Ward7b43c682002-12-30 02:29:28 +0000377 return _do_ioctl_0(self->fd, args, "post", SNDCTL_DSP_POST);
Greg Ward131bce02002-11-30 22:56:44 +0000378}
379
380
381/* Regular file methods: read(), write(), close(), etc. as well
382 as one convenience method, writeall(). */
383
Greg Ward04613a92002-11-30 22:47:45 +0000384static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000385oss_read(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000386{
387 int size, count;
388 char *cp;
389 PyObject *rv;
Greg Wardad4d9b92002-12-30 03:02:22 +0000390
Greg Ward04613a92002-11-30 22:47:45 +0000391 if (!PyArg_ParseTuple(args, "i:read", &size))
392 return NULL;
393 rv = PyString_FromStringAndSize(NULL, size);
394 if (rv == NULL)
395 return NULL;
396 cp = PyString_AS_STRING(rv);
Greg Ward64927852003-05-23 01:50:37 +0000397
398 Py_BEGIN_ALLOW_THREADS
399 count = read(self->fd, cp, size);
400 Py_END_ALLOW_THREADS
401
402 if (count < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000403 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000404 Py_DECREF(rv);
405 return NULL;
406 }
Greg Ward7b43c682002-12-30 02:29:28 +0000407 self->icount += count;
Greg Ward04613a92002-11-30 22:47:45 +0000408 _PyString_Resize(&rv, count);
409 return rv;
410}
411
412static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000413oss_write(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000414{
415 char *cp;
416 int rv, size;
Greg Ward131bce02002-11-30 22:56:44 +0000417
418 if (!PyArg_ParseTuple(args, "s#:write", &cp, &size)) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000419 return NULL;
Greg Ward131bce02002-11-30 22:56:44 +0000420 }
Greg Ward64927852003-05-23 01:50:37 +0000421
422 Py_BEGIN_ALLOW_THREADS
423 rv = write(self->fd, cp, size);
424 Py_END_ALLOW_THREADS
425
426 if (rv == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000427 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000428 } else {
Greg Ward7b43c682002-12-30 02:29:28 +0000429 self->ocount += rv;
Greg Ward131bce02002-11-30 22:56:44 +0000430 }
431 return PyInt_FromLong(rv);
432}
433
434static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000435oss_writeall(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000436{
437 char *cp;
438 int rv, size;
439 fd_set write_set_fds;
440 int select_rv;
441
442 /* NB. writeall() is only useful in non-blocking mode: according to
443 Guenter Geiger <geiger@xdv.org> on the linux-audio-dev list
444 (http://eca.cx/lad/2002/11/0380.html), OSS guarantees that
445 write() in blocking mode consumes the whole buffer. In blocking
446 mode, the behaviour of write() and writeall() from Python is
447 indistinguishable. */
448
449 if (!PyArg_ParseTuple(args, "s#:write", &cp, &size))
450 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000451
452 /* use select to wait for audio device to be available */
453 FD_ZERO(&write_set_fds);
Greg Ward7b43c682002-12-30 02:29:28 +0000454 FD_SET(self->fd, &write_set_fds);
Greg Ward04613a92002-11-30 22:47:45 +0000455
456 while (size > 0) {
Greg Ward64927852003-05-23 01:50:37 +0000457 Py_BEGIN_ALLOW_THREADS
Greg Ward7b43c682002-12-30 02:29:28 +0000458 select_rv = select(self->fd+1, NULL, &write_set_fds, NULL, NULL);
Greg Ward64927852003-05-23 01:50:37 +0000459 Py_END_ALLOW_THREADS
Greg Ward131bce02002-11-30 22:56:44 +0000460 assert(select_rv != 0); /* no timeout, can't expire */
461 if (select_rv == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000462 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000463
Greg Ward64927852003-05-23 01:50:37 +0000464 Py_BEGIN_ALLOW_THREADS
Greg Ward7b43c682002-12-30 02:29:28 +0000465 rv = write(self->fd, cp, size);
Greg Ward64927852003-05-23 01:50:37 +0000466 Py_END_ALLOW_THREADS
Greg Ward131bce02002-11-30 22:56:44 +0000467 if (rv == -1) {
468 if (errno == EAGAIN) { /* buffer is full, try again */
469 errno = 0;
470 continue;
471 } else /* it's a real error */
Greg Ward97708bc2002-11-30 23:17:10 +0000472 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000473 } else { /* wrote rv bytes */
Greg Ward7b43c682002-12-30 02:29:28 +0000474 self->ocount += rv;
Greg Ward131bce02002-11-30 22:56:44 +0000475 size -= rv;
476 cp += rv;
477 }
Greg Ward04613a92002-11-30 22:47:45 +0000478 }
479 Py_INCREF(Py_None);
480 return Py_None;
481}
482
483static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000484oss_close(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000485{
486 if (!PyArg_ParseTuple(args, ":close"))
Greg Wardad4d9b92002-12-30 03:02:22 +0000487 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000488
Greg Ward7b43c682002-12-30 02:29:28 +0000489 if (self->fd >= 0) {
Greg Wardb8043902003-05-26 22:47:30 +0000490 Py_BEGIN_ALLOW_THREADS
Greg Ward7b43c682002-12-30 02:29:28 +0000491 close(self->fd);
Greg Wardb8043902003-05-26 22:47:30 +0000492 Py_END_ALLOW_THREADS
Greg Ward7b43c682002-12-30 02:29:28 +0000493 self->fd = -1;
Greg Ward04613a92002-11-30 22:47:45 +0000494 }
495 Py_INCREF(Py_None);
496 return Py_None;
497}
498
499static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000500oss_fileno(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000501{
502 if (!PyArg_ParseTuple(args, ":fileno"))
Greg Wardad4d9b92002-12-30 03:02:22 +0000503 return NULL;
Greg Ward7b43c682002-12-30 02:29:28 +0000504 return PyInt_FromLong(self->fd);
Greg Ward04613a92002-11-30 22:47:45 +0000505}
506
Greg Ward131bce02002-11-30 22:56:44 +0000507
508/* Convenience methods: these generally wrap a couple of ioctls into one
509 common task. */
510
Greg Ward04613a92002-11-30 22:47:45 +0000511static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000512oss_setparameters(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000513{
514 int rate, ssize, nchannels, n, fmt, emulate=0;
515
516 if (!PyArg_ParseTuple(args, "iiii|i:setparameters",
517 &rate, &ssize, &nchannels, &fmt, &emulate))
518 return NULL;
519
520 if (rate < 0) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000521 PyErr_Format(PyExc_ValueError, "expected rate >= 0, not %d",
522 rate);
523 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000524 }
525 if (ssize < 0) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000526 PyErr_Format(PyExc_ValueError, "expected sample size >= 0, not %d",
527 ssize);
528 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000529 }
530 if (nchannels != 1 && nchannels != 2) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000531 PyErr_Format(PyExc_ValueError, "nchannels must be 1 or 2, not %d",
532 nchannels);
533 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000534 }
535
536 for (n = 0; n < n_audio_types; n++)
537 if (fmt == audio_types[n].a_fmt)
538 break;
539 if (n == n_audio_types) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000540 PyErr_Format(PyExc_ValueError, "unknown audio encoding: %d", fmt);
541 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000542 }
543 if (audio_types[n].a_bps != ssize) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000544 PyErr_Format(PyExc_ValueError,
545 "for %s, expected sample size %d, not %d",
546 audio_types[n].a_name, audio_types[n].a_bps, ssize);
547 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000548 }
549
550 if (emulate == 0) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000551 if ((self->afmts & audio_types[n].a_fmt) == 0) {
552 PyErr_Format(PyExc_ValueError,
553 "%s format not supported by device",
554 audio_types[n].a_name);
555 return NULL;
556 }
Greg Ward04613a92002-11-30 22:47:45 +0000557 }
Greg Ward7b43c682002-12-30 02:29:28 +0000558 if (ioctl(self->fd, SNDCTL_DSP_SETFMT,
Greg Wardad4d9b92002-12-30 03:02:22 +0000559 &audio_types[n].a_fmt) == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000560 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000561 return NULL;
562 }
Greg Ward7b43c682002-12-30 02:29:28 +0000563 if (ioctl(self->fd, SNDCTL_DSP_CHANNELS, &nchannels) == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000564 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000565 return NULL;
566 }
Greg Ward7b43c682002-12-30 02:29:28 +0000567 if (ioctl(self->fd, SNDCTL_DSP_SPEED, &rate) == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000568 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000569 return NULL;
570 }
571
572 Py_INCREF(Py_None);
573 return Py_None;
574}
575
576static int
Greg Ward499b73e2002-12-31 03:04:52 +0000577_ssize(oss_audio_t *self, int *nchannels, int *ssize)
Greg Ward04613a92002-11-30 22:47:45 +0000578{
579 int fmt;
580
581 fmt = 0;
Greg Ward7b43c682002-12-30 02:29:28 +0000582 if (ioctl(self->fd, SNDCTL_DSP_SETFMT, &fmt) < 0)
Greg Ward04613a92002-11-30 22:47:45 +0000583 return -errno;
584
585 switch (fmt) {
586 case AFMT_MU_LAW:
587 case AFMT_A_LAW:
588 case AFMT_U8:
589 case AFMT_S8:
590 *ssize = sizeof(char);
591 break;
592 case AFMT_S16_LE:
593 case AFMT_S16_BE:
594 case AFMT_U16_LE:
595 case AFMT_U16_BE:
596 *ssize = sizeof(short);
597 break;
598 case AFMT_MPEG:
599 case AFMT_IMA_ADPCM:
600 default:
601 return -EOPNOTSUPP;
602 }
603 *nchannels = 0;
Greg Ward7b43c682002-12-30 02:29:28 +0000604 if (ioctl(self->fd, SNDCTL_DSP_CHANNELS, nchannels) < 0)
Greg Ward04613a92002-11-30 22:47:45 +0000605 return -errno;
606 return 0;
607}
608
609
610/* bufsize returns the size of the hardware audio buffer in number
611 of samples */
612static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000613oss_bufsize(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000614{
615 audio_buf_info ai;
616 int nchannels, ssize;
617
618 if (!PyArg_ParseTuple(args, ":bufsize")) return NULL;
619
620 if (_ssize(self, &nchannels, &ssize) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000621 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000622 return NULL;
623 }
Greg Ward7b43c682002-12-30 02:29:28 +0000624 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000625 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000626 return NULL;
627 }
628 return PyInt_FromLong((ai.fragstotal * ai.fragsize) / (nchannels * ssize));
629}
630
631/* obufcount returns the number of samples that are available in the
632 hardware for playing */
633static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000634oss_obufcount(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000635{
636 audio_buf_info ai;
637 int nchannels, ssize;
638
639 if (!PyArg_ParseTuple(args, ":obufcount"))
640 return NULL;
641
642 if (_ssize(self, &nchannels, &ssize) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000643 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000644 return NULL;
645 }
Greg Ward7b43c682002-12-30 02:29:28 +0000646 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000647 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000648 return NULL;
649 }
650 return PyInt_FromLong((ai.fragstotal * ai.fragsize - ai.bytes) /
651 (ssize * nchannels));
652}
653
654/* obufcount returns the number of samples that can be played without
655 blocking */
656static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000657oss_obuffree(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000658{
659 audio_buf_info ai;
660 int nchannels, ssize;
661
662 if (!PyArg_ParseTuple(args, ":obuffree"))
663 return NULL;
664
665 if (_ssize(self, &nchannels, &ssize) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000666 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000667 return NULL;
668 }
Greg Ward7b43c682002-12-30 02:29:28 +0000669 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000670 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000671 return NULL;
672 }
673 return PyInt_FromLong(ai.bytes / (ssize * nchannels));
674}
675
Greg Ward04613a92002-11-30 22:47:45 +0000676static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000677oss_getptr(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000678{
679 count_info info;
680 int req;
681
682 if (!PyArg_ParseTuple(args, ":getptr"))
Greg Wardad4d9b92002-12-30 03:02:22 +0000683 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000684
Greg Ward7b43c682002-12-30 02:29:28 +0000685 if (self->mode == O_RDONLY)
Greg Wardad4d9b92002-12-30 03:02:22 +0000686 req = SNDCTL_DSP_GETIPTR;
Greg Ward04613a92002-11-30 22:47:45 +0000687 else
Greg Wardad4d9b92002-12-30 03:02:22 +0000688 req = SNDCTL_DSP_GETOPTR;
Greg Ward7b43c682002-12-30 02:29:28 +0000689 if (ioctl(self->fd, req, &info) == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000690 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000691 return NULL;
692 }
693 return Py_BuildValue("iii", info.bytes, info.blocks, info.ptr);
694}
695
Greg Wardda1cacb2002-12-31 03:02:23 +0000696
697/* ----------------------------------------------------------------------
698 * Methods of mixer objects (OSSMixerType)
699 */
700
Greg Ward3d9994d2002-12-11 15:12:01 +0000701static PyObject *
702oss_mixer_close(oss_mixer_t *self, PyObject *args)
703{
704 if (!PyArg_ParseTuple(args, ":close"))
Greg Wardad4d9b92002-12-30 03:02:22 +0000705 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000706
Greg Ward7b43c682002-12-30 02:29:28 +0000707 if (self->fd >= 0) {
708 close(self->fd);
709 self->fd = -1;
Greg Ward3d9994d2002-12-11 15:12:01 +0000710 }
711 Py_INCREF(Py_None);
712 return Py_None;
713}
714
715static PyObject *
716oss_mixer_fileno(oss_mixer_t *self, PyObject *args)
717{
718 if (!PyArg_ParseTuple(args, ":fileno"))
Greg Wardad4d9b92002-12-30 03:02:22 +0000719 return NULL;
Greg Ward7b43c682002-12-30 02:29:28 +0000720 return PyInt_FromLong(self->fd);
Greg Ward3d9994d2002-12-11 15:12:01 +0000721}
722
723/* Simple mixer interface methods */
724
725static PyObject *
Greg Ward2d6f9a92002-12-31 02:54:43 +0000726oss_mixer_controls(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000727{
Greg Ward2d6f9a92002-12-31 02:54:43 +0000728 return _do_ioctl_1_internal(self->fd, args, "controls",
Greg Ward3d9994d2002-12-11 15:12:01 +0000729 SOUND_MIXER_READ_DEVMASK);
730}
731
732static PyObject *
Greg Ward2d6f9a92002-12-31 02:54:43 +0000733oss_mixer_stereocontrols(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000734{
Greg Ward2d6f9a92002-12-31 02:54:43 +0000735 return _do_ioctl_1_internal(self->fd, args, "stereocontrols",
Greg Ward3d9994d2002-12-11 15:12:01 +0000736 SOUND_MIXER_READ_STEREODEVS);
737}
738
739static PyObject *
Greg Ward2d6f9a92002-12-31 02:54:43 +0000740oss_mixer_reccontrols(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000741{
Greg Ward2d6f9a92002-12-31 02:54:43 +0000742 return _do_ioctl_1_internal(self->fd, args, "reccontrols",
Greg Wardb69bb3d2002-12-12 17:35:45 +0000743 SOUND_MIXER_READ_RECMASK);
Greg Ward3d9994d2002-12-11 15:12:01 +0000744}
745
746static PyObject *
Greg Warde7037662002-12-30 03:01:48 +0000747oss_mixer_get(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000748{
749 int channel, volume;
750
751 /* Can't use _do_ioctl_1 because of encoded arg thingy. */
Greg Warde7037662002-12-30 03:01:48 +0000752 if (!PyArg_ParseTuple(args, "i:get", &channel))
Greg Wardad4d9b92002-12-30 03:02:22 +0000753 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000754
755 if (channel < 0 || channel > SOUND_MIXER_NRDEVICES) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000756 PyErr_SetString(OSSAudioError, "Invalid mixer channel specified.");
757 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000758 }
759
Greg Warde7037662002-12-30 03:01:48 +0000760 if (ioctl(self->fd, MIXER_READ(channel), &volume) == -1)
Greg Wardad4d9b92002-12-30 03:02:22 +0000761 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward3d9994d2002-12-11 15:12:01 +0000762
Greg Warde7037662002-12-30 03:01:48 +0000763 return Py_BuildValue("(ii)", volume & 0xff, (volume & 0xff00) >> 8);
Greg Ward3d9994d2002-12-11 15:12:01 +0000764}
765
766static PyObject *
Greg Warde7037662002-12-30 03:01:48 +0000767oss_mixer_set(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000768{
769 int channel, volume, leftVol, rightVol;
770
771 /* Can't use _do_ioctl_1 because of encoded arg thingy. */
Greg Warde7037662002-12-30 03:01:48 +0000772 if (!PyArg_ParseTuple(args, "i(ii):set", &channel, &leftVol, &rightVol))
Greg Wardad4d9b92002-12-30 03:02:22 +0000773 return NULL;
774
Greg Ward3d9994d2002-12-11 15:12:01 +0000775 if (channel < 0 || channel > SOUND_MIXER_NRDEVICES) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000776 PyErr_SetString(OSSAudioError, "Invalid mixer channel specified.");
777 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000778 }
779
780 if (leftVol < 0 || rightVol < 0 || leftVol > 100 || rightVol > 100) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000781 PyErr_SetString(OSSAudioError, "Volumes must be between 0 and 100.");
782 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000783 }
784
785 volume = (rightVol << 8) | leftVol;
786
Greg Warde7037662002-12-30 03:01:48 +0000787 if (ioctl(self->fd, MIXER_WRITE(channel), &volume) == -1)
Greg Wardad4d9b92002-12-30 03:02:22 +0000788 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward3d9994d2002-12-11 15:12:01 +0000789
Greg Warde7037662002-12-30 03:01:48 +0000790 return Py_BuildValue("(ii)", volume & 0xff, (volume & 0xff00) >> 8);
Greg Ward3d9994d2002-12-11 15:12:01 +0000791}
792
793static PyObject *
Greg Wardf05aa102002-12-30 23:19:32 +0000794oss_mixer_get_recsrc(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000795{
Greg Wardf05aa102002-12-30 23:19:32 +0000796 return _do_ioctl_1_internal(self->fd, args, "get_recsrc",
Greg Ward3d9994d2002-12-11 15:12:01 +0000797 SOUND_MIXER_READ_RECSRC);
798}
799
800static PyObject *
Greg Wardf05aa102002-12-30 23:19:32 +0000801oss_mixer_set_recsrc(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000802{
Greg Wardf05aa102002-12-30 23:19:32 +0000803 return _do_ioctl_1(self->fd, args, "set_recsrc",
Greg Ward3d9994d2002-12-11 15:12:01 +0000804 SOUND_MIXER_WRITE_RECSRC);
805}
806
807
Greg Wardda1cacb2002-12-31 03:02:23 +0000808/* ----------------------------------------------------------------------
809 * Method tables and other bureaucracy
810 */
811
Greg Ward8c6b6a92002-12-11 14:43:13 +0000812static PyMethodDef oss_methods[] = {
Greg Ward131bce02002-11-30 22:56:44 +0000813 /* Regular file methods */
Greg Wardad4d9b92002-12-30 03:02:22 +0000814 { "read", (PyCFunction)oss_read, METH_VARARGS },
815 { "write", (PyCFunction)oss_write, METH_VARARGS },
816 { "writeall", (PyCFunction)oss_writeall, METH_VARARGS },
817 { "close", (PyCFunction)oss_close, METH_VARARGS },
818 { "fileno", (PyCFunction)oss_fileno, METH_VARARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000819
820 /* Simple ioctl wrappers */
Greg Ward8c6b6a92002-12-11 14:43:13 +0000821 { "nonblock", (PyCFunction)oss_nonblock, METH_VARARGS },
Greg Wardad4d9b92002-12-30 03:02:22 +0000822 { "setfmt", (PyCFunction)oss_setfmt, METH_VARARGS },
Greg Ward8c6b6a92002-12-11 14:43:13 +0000823 { "getfmts", (PyCFunction)oss_getfmts, METH_VARARGS },
824 { "channels", (PyCFunction)oss_channels, METH_VARARGS },
825 { "speed", (PyCFunction)oss_speed, METH_VARARGS },
Greg Wardad4d9b92002-12-30 03:02:22 +0000826 { "sync", (PyCFunction)oss_sync, METH_VARARGS },
827 { "reset", (PyCFunction)oss_reset, METH_VARARGS },
828 { "post", (PyCFunction)oss_post, METH_VARARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000829
830 /* Convenience methods -- wrap a couple of ioctls together */
Greg Wardad4d9b92002-12-30 03:02:22 +0000831 { "setparameters", (PyCFunction)oss_setparameters, METH_VARARGS },
832 { "bufsize", (PyCFunction)oss_bufsize, METH_VARARGS },
833 { "obufcount", (PyCFunction)oss_obufcount, METH_VARARGS },
834 { "obuffree", (PyCFunction)oss_obuffree, METH_VARARGS },
Greg Ward8c6b6a92002-12-11 14:43:13 +0000835 { "getptr", (PyCFunction)oss_getptr, METH_VARARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000836
837 /* Aliases for backwards compatibility */
Greg Wardad4d9b92002-12-30 03:02:22 +0000838 { "flush", (PyCFunction)oss_sync, METH_VARARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000839
Greg Wardad4d9b92002-12-30 03:02:22 +0000840 { NULL, NULL} /* sentinel */
Greg Ward04613a92002-11-30 22:47:45 +0000841};
842
Greg Ward3d9994d2002-12-11 15:12:01 +0000843static PyMethodDef oss_mixer_methods[] = {
844 /* Regular file method - OSS mixers are ioctl-only interface */
Greg Wardad4d9b92002-12-30 03:02:22 +0000845 { "close", (PyCFunction)oss_mixer_close, METH_VARARGS },
846 { "fileno", (PyCFunction)oss_mixer_fileno, METH_VARARGS },
Greg Ward3d9994d2002-12-11 15:12:01 +0000847
848 /* Simple ioctl wrappers */
Greg Ward2d6f9a92002-12-31 02:54:43 +0000849 { "controls", (PyCFunction)oss_mixer_controls, METH_VARARGS },
850 { "stereocontrols", (PyCFunction)oss_mixer_stereocontrols, METH_VARARGS},
851 { "reccontrols", (PyCFunction)oss_mixer_reccontrols, METH_VARARGS},
Greg Wardad4d9b92002-12-30 03:02:22 +0000852 { "get", (PyCFunction)oss_mixer_get, METH_VARARGS },
853 { "set", (PyCFunction)oss_mixer_set, METH_VARARGS },
Greg Wardf05aa102002-12-30 23:19:32 +0000854 { "get_recsrc", (PyCFunction)oss_mixer_get_recsrc, METH_VARARGS },
855 { "set_recsrc", (PyCFunction)oss_mixer_set_recsrc, METH_VARARGS },
Greg Ward3d9994d2002-12-11 15:12:01 +0000856
Greg Wardad4d9b92002-12-30 03:02:22 +0000857 { NULL, NULL}
Greg Ward3d9994d2002-12-11 15:12:01 +0000858};
859
Greg Ward04613a92002-11-30 22:47:45 +0000860static PyObject *
Greg Ward58ae13c2002-12-31 03:07:21 +0000861oss_getattr(oss_audio_t *self, char *name)
Greg Ward04613a92002-11-30 22:47:45 +0000862{
Greg Ward58ae13c2002-12-31 03:07:21 +0000863 return Py_FindMethod(oss_methods, (PyObject *)self, name);
Greg Ward04613a92002-11-30 22:47:45 +0000864}
865
Greg Ward3d9994d2002-12-11 15:12:01 +0000866static PyObject *
Greg Ward58ae13c2002-12-31 03:07:21 +0000867oss_mixer_getattr(oss_mixer_t *self, char *name)
Greg Ward3d9994d2002-12-11 15:12:01 +0000868{
Greg Ward58ae13c2002-12-31 03:07:21 +0000869 return Py_FindMethod(oss_mixer_methods, (PyObject *)self, name);
Greg Ward3d9994d2002-12-11 15:12:01 +0000870}
871
Greg Ward499b73e2002-12-31 03:04:52 +0000872static PyTypeObject OSSAudioType = {
Greg Ward04613a92002-11-30 22:47:45 +0000873 PyObject_HEAD_INIT(&PyType_Type)
Greg Wardad4d9b92002-12-30 03:02:22 +0000874 0, /*ob_size*/
Greg Ward9a568eb2002-11-30 23:20:09 +0000875 "ossaudiodev.oss_audio_device", /*tp_name*/
Greg Ward499b73e2002-12-31 03:04:52 +0000876 sizeof(oss_audio_t), /*tp_size*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000877 0, /*tp_itemsize*/
Greg Ward04613a92002-11-30 22:47:45 +0000878 /* methods */
Greg Wardad4d9b92002-12-30 03:02:22 +0000879 (destructor)oss_dealloc, /*tp_dealloc*/
880 0, /*tp_print*/
881 (getattrfunc)oss_getattr, /*tp_getattr*/
882 0, /*tp_setattr*/
883 0, /*tp_compare*/
884 0, /*tp_repr*/
Greg Ward04613a92002-11-30 22:47:45 +0000885};
886
Greg Ward3d9994d2002-12-11 15:12:01 +0000887static PyTypeObject OSSMixerType = {
888 PyObject_HEAD_INIT(&PyType_Type)
Greg Wardad4d9b92002-12-30 03:02:22 +0000889 0, /*ob_size*/
Greg Ward3d9994d2002-12-11 15:12:01 +0000890 "ossaudiodev.oss_mixer_device", /*tp_name*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000891 sizeof(oss_mixer_t), /*tp_size*/
892 0, /*tp_itemsize*/
Greg Ward3d9994d2002-12-11 15:12:01 +0000893 /* methods */
894 (destructor)oss_mixer_dealloc, /*tp_dealloc*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000895 0, /*tp_print*/
Greg Ward3d9994d2002-12-11 15:12:01 +0000896 (getattrfunc)oss_mixer_getattr, /*tp_getattr*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000897 0, /*tp_setattr*/
898 0, /*tp_compare*/
899 0, /*tp_repr*/
Greg Ward3d9994d2002-12-11 15:12:01 +0000900};
901
902
Greg Ward04613a92002-11-30 22:47:45 +0000903static PyObject *
Greg Ward9a568eb2002-11-30 23:20:09 +0000904ossopen(PyObject *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000905{
Greg Ward8c6b6a92002-12-11 14:43:13 +0000906 return (PyObject *)newossobject(args);
Greg Ward04613a92002-11-30 22:47:45 +0000907}
908
Greg Ward3d9994d2002-12-11 15:12:01 +0000909static PyObject *
910ossopenmixer(PyObject *self, PyObject *args)
911{
912 return (PyObject *)newossmixerobject(args);
913}
914
Greg Ward9a568eb2002-11-30 23:20:09 +0000915static PyMethodDef ossaudiodev_methods[] = {
916 { "open", ossopen, METH_VARARGS },
Greg Ward3d9994d2002-12-11 15:12:01 +0000917 { "openmixer", ossopenmixer, METH_VARARGS },
Greg Ward04613a92002-11-30 22:47:45 +0000918 { 0, 0 },
919};
920
Greg Ward1e0f57d2002-11-30 23:05:26 +0000921
922#define _EXPORT_INT(mod, name) \
923 if (PyModule_AddIntConstant(mod, #name, (long) (name)) == -1) return;
924
Greg Ward744f0fd2002-12-31 03:23:59 +0000925
926static char *control_labels[] = SOUND_DEVICE_LABELS;
927static char *control_names[] = SOUND_DEVICE_NAMES;
928
929
930static int
931build_namelists (PyObject *module)
932{
933 PyObject *labels;
934 PyObject *names;
935 PyObject *s;
936 int num_controls;
937 int i;
938
939 num_controls = sizeof(control_labels) / sizeof(control_labels[0]);
940 assert(num_controls == sizeof(control_names) / sizeof(control_names[0]));
941
942 labels = PyList_New(num_controls);
943 names = PyList_New(num_controls);
944 for (i = 0; i < num_controls; i++) {
945 s = PyString_FromString(control_labels[i]);
946 if (s == NULL)
947 return -1;
948 PyList_SET_ITEM(labels, i, s);
949
950 s = PyString_FromString(control_names[i]);
951 if (s == NULL)
952 return -1;
953 PyList_SET_ITEM(names, i, s);
954 }
955
956 if (PyModule_AddObject(module, "control_labels", labels) == -1)
957 return -1;
958 if (PyModule_AddObject(module, "control_names", names) == -1)
959 return -1;
960
961 return 0;
962}
963
964
Greg Ward04613a92002-11-30 22:47:45 +0000965void
Greg Ward9a568eb2002-11-30 23:20:09 +0000966initossaudiodev(void)
Greg Ward04613a92002-11-30 22:47:45 +0000967{
968 PyObject *m;
969
Greg Ward9a568eb2002-11-30 23:20:09 +0000970 m = Py_InitModule("ossaudiodev", ossaudiodev_methods);
Greg Ward04613a92002-11-30 22:47:45 +0000971
Greg Ward97708bc2002-11-30 23:17:10 +0000972 OSSAudioError = PyErr_NewException("ossaudiodev.error", NULL, NULL);
973 if (OSSAudioError)
Greg Wardad4d9b92002-12-30 03:02:22 +0000974 PyModule_AddObject(m, "error", OSSAudioError);
Greg Ward04613a92002-11-30 22:47:45 +0000975
Greg Ward744f0fd2002-12-31 03:23:59 +0000976 /* Build 'control_labels' and 'control_names' lists and add them
977 to the module. */
978 if (build_namelists(m) == -1) /* XXX what to do here? */
979 return;
980
Greg Ward1e0f57d2002-11-30 23:05:26 +0000981 /* Expose the audio format numbers -- essential! */
982 _EXPORT_INT(m, AFMT_QUERY);
983 _EXPORT_INT(m, AFMT_MU_LAW);
984 _EXPORT_INT(m, AFMT_A_LAW);
985 _EXPORT_INT(m, AFMT_IMA_ADPCM);
986 _EXPORT_INT(m, AFMT_U8);
987 _EXPORT_INT(m, AFMT_S16_LE);
988 _EXPORT_INT(m, AFMT_S16_BE);
989 _EXPORT_INT(m, AFMT_S8);
990 _EXPORT_INT(m, AFMT_U16_LE);
991 _EXPORT_INT(m, AFMT_U16_BE);
992 _EXPORT_INT(m, AFMT_MPEG);
Andrew M. Kuchlingfe62bc92003-02-13 13:27:07 +0000993#ifdef AFMT_AC3
Greg Ward1e0f57d2002-11-30 23:05:26 +0000994 _EXPORT_INT(m, AFMT_AC3);
Andrew M. Kuchlingfe62bc92003-02-13 13:27:07 +0000995#endif
Neal Norwitzd156c2d2003-02-02 17:59:06 +0000996#ifdef AFMT_S16_NE
Greg Ward1e0f57d2002-11-30 23:05:26 +0000997 _EXPORT_INT(m, AFMT_S16_NE);
Neal Norwitzd156c2d2003-02-02 17:59:06 +0000998#endif
Greg Wardad4d9b92002-12-30 03:02:22 +0000999
Greg Ward37897c22002-12-30 02:43:36 +00001000 /* Expose the sound mixer device numbers. */
Greg Ward7b43c682002-12-30 02:29:28 +00001001 _EXPORT_INT(m, SOUND_MIXER_NRDEVICES);
Greg Ward3d9994d2002-12-11 15:12:01 +00001002 _EXPORT_INT(m, SOUND_MIXER_VOLUME);
1003 _EXPORT_INT(m, SOUND_MIXER_BASS);
1004 _EXPORT_INT(m, SOUND_MIXER_TREBLE);
1005 _EXPORT_INT(m, SOUND_MIXER_SYNTH);
1006 _EXPORT_INT(m, SOUND_MIXER_PCM);
1007 _EXPORT_INT(m, SOUND_MIXER_SPEAKER);
1008 _EXPORT_INT(m, SOUND_MIXER_LINE);
1009 _EXPORT_INT(m, SOUND_MIXER_MIC);
1010 _EXPORT_INT(m, SOUND_MIXER_CD);
1011 _EXPORT_INT(m, SOUND_MIXER_IMIX);
1012 _EXPORT_INT(m, SOUND_MIXER_ALTPCM);
1013 _EXPORT_INT(m, SOUND_MIXER_RECLEV);
1014 _EXPORT_INT(m, SOUND_MIXER_IGAIN);
1015 _EXPORT_INT(m, SOUND_MIXER_OGAIN);
1016 _EXPORT_INT(m, SOUND_MIXER_LINE1);
1017 _EXPORT_INT(m, SOUND_MIXER_LINE2);
1018 _EXPORT_INT(m, SOUND_MIXER_LINE3);
1019 _EXPORT_INT(m, SOUND_MIXER_DIGITAL1);
1020 _EXPORT_INT(m, SOUND_MIXER_DIGITAL2);
1021 _EXPORT_INT(m, SOUND_MIXER_DIGITAL3);
1022 _EXPORT_INT(m, SOUND_MIXER_PHONEIN);
1023 _EXPORT_INT(m, SOUND_MIXER_PHONEOUT);
1024 _EXPORT_INT(m, SOUND_MIXER_VIDEO);
1025 _EXPORT_INT(m, SOUND_MIXER_RADIO);
1026 _EXPORT_INT(m, SOUND_MIXER_MONITOR);
Greg Ward04613a92002-11-30 22:47:45 +00001027
Greg Ward1e0f57d2002-11-30 23:05:26 +00001028 /* Expose all the ioctl numbers for masochists who like to do this
1029 stuff directly. */
1030 _EXPORT_INT(m, SNDCTL_COPR_HALT);
1031 _EXPORT_INT(m, SNDCTL_COPR_LOAD);
1032 _EXPORT_INT(m, SNDCTL_COPR_RCODE);
1033 _EXPORT_INT(m, SNDCTL_COPR_RCVMSG);
1034 _EXPORT_INT(m, SNDCTL_COPR_RDATA);
1035 _EXPORT_INT(m, SNDCTL_COPR_RESET);
1036 _EXPORT_INT(m, SNDCTL_COPR_RUN);
1037 _EXPORT_INT(m, SNDCTL_COPR_SENDMSG);
1038 _EXPORT_INT(m, SNDCTL_COPR_WCODE);
1039 _EXPORT_INT(m, SNDCTL_COPR_WDATA);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001040#ifdef SNDCTL_DSP_BIND_CHANNEL
Greg Ward1e0f57d2002-11-30 23:05:26 +00001041 _EXPORT_INT(m, SNDCTL_DSP_BIND_CHANNEL);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001042#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001043 _EXPORT_INT(m, SNDCTL_DSP_CHANNELS);
1044 _EXPORT_INT(m, SNDCTL_DSP_GETBLKSIZE);
1045 _EXPORT_INT(m, SNDCTL_DSP_GETCAPS);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001046#ifdef SNDCTL_DSP_GETCHANNELMASK
Greg Ward1e0f57d2002-11-30 23:05:26 +00001047 _EXPORT_INT(m, SNDCTL_DSP_GETCHANNELMASK);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001048#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001049 _EXPORT_INT(m, SNDCTL_DSP_GETFMTS);
1050 _EXPORT_INT(m, SNDCTL_DSP_GETIPTR);
1051 _EXPORT_INT(m, SNDCTL_DSP_GETISPACE);
1052 _EXPORT_INT(m, SNDCTL_DSP_GETODELAY);
1053 _EXPORT_INT(m, SNDCTL_DSP_GETOPTR);
1054 _EXPORT_INT(m, SNDCTL_DSP_GETOSPACE);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001055#ifdef SNDCTL_DSP_GETSPDIF
Greg Ward1e0f57d2002-11-30 23:05:26 +00001056 _EXPORT_INT(m, SNDCTL_DSP_GETSPDIF);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001057#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001058 _EXPORT_INT(m, SNDCTL_DSP_GETTRIGGER);
1059 _EXPORT_INT(m, SNDCTL_DSP_MAPINBUF);
1060 _EXPORT_INT(m, SNDCTL_DSP_MAPOUTBUF);
1061 _EXPORT_INT(m, SNDCTL_DSP_NONBLOCK);
1062 _EXPORT_INT(m, SNDCTL_DSP_POST);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001063#ifdef SNDCTL_DSP_PROFILE
Greg Ward1e0f57d2002-11-30 23:05:26 +00001064 _EXPORT_INT(m, SNDCTL_DSP_PROFILE);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001065#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001066 _EXPORT_INT(m, SNDCTL_DSP_RESET);
1067 _EXPORT_INT(m, SNDCTL_DSP_SAMPLESIZE);
1068 _EXPORT_INT(m, SNDCTL_DSP_SETDUPLEX);
1069 _EXPORT_INT(m, SNDCTL_DSP_SETFMT);
1070 _EXPORT_INT(m, SNDCTL_DSP_SETFRAGMENT);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001071#ifdef SNDCTL_DSP_SETSPDIF
Greg Ward1e0f57d2002-11-30 23:05:26 +00001072 _EXPORT_INT(m, SNDCTL_DSP_SETSPDIF);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001073#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001074 _EXPORT_INT(m, SNDCTL_DSP_SETSYNCRO);
1075 _EXPORT_INT(m, SNDCTL_DSP_SETTRIGGER);
1076 _EXPORT_INT(m, SNDCTL_DSP_SPEED);
1077 _EXPORT_INT(m, SNDCTL_DSP_STEREO);
1078 _EXPORT_INT(m, SNDCTL_DSP_SUBDIVIDE);
1079 _EXPORT_INT(m, SNDCTL_DSP_SYNC);
1080 _EXPORT_INT(m, SNDCTL_FM_4OP_ENABLE);
1081 _EXPORT_INT(m, SNDCTL_FM_LOAD_INSTR);
1082 _EXPORT_INT(m, SNDCTL_MIDI_INFO);
1083 _EXPORT_INT(m, SNDCTL_MIDI_MPUCMD);
1084 _EXPORT_INT(m, SNDCTL_MIDI_MPUMODE);
1085 _EXPORT_INT(m, SNDCTL_MIDI_PRETIME);
1086 _EXPORT_INT(m, SNDCTL_SEQ_CTRLRATE);
1087 _EXPORT_INT(m, SNDCTL_SEQ_GETINCOUNT);
1088 _EXPORT_INT(m, SNDCTL_SEQ_GETOUTCOUNT);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001089#ifdef SNDCTL_SEQ_GETTIME
Greg Ward1e0f57d2002-11-30 23:05:26 +00001090 _EXPORT_INT(m, SNDCTL_SEQ_GETTIME);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001091#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001092 _EXPORT_INT(m, SNDCTL_SEQ_NRMIDIS);
1093 _EXPORT_INT(m, SNDCTL_SEQ_NRSYNTHS);
1094 _EXPORT_INT(m, SNDCTL_SEQ_OUTOFBAND);
1095 _EXPORT_INT(m, SNDCTL_SEQ_PANIC);
1096 _EXPORT_INT(m, SNDCTL_SEQ_PERCMODE);
1097 _EXPORT_INT(m, SNDCTL_SEQ_RESET);
1098 _EXPORT_INT(m, SNDCTL_SEQ_RESETSAMPLES);
1099 _EXPORT_INT(m, SNDCTL_SEQ_SYNC);
1100 _EXPORT_INT(m, SNDCTL_SEQ_TESTMIDI);
1101 _EXPORT_INT(m, SNDCTL_SEQ_THRESHOLD);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001102#ifdef SNDCTL_SYNTH_CONTROL
Greg Ward1e0f57d2002-11-30 23:05:26 +00001103 _EXPORT_INT(m, SNDCTL_SYNTH_CONTROL);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001104#endif
1105#ifdef SNDCTL_SYNTH_ID
Greg Ward1e0f57d2002-11-30 23:05:26 +00001106 _EXPORT_INT(m, SNDCTL_SYNTH_ID);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001107#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001108 _EXPORT_INT(m, SNDCTL_SYNTH_INFO);
1109 _EXPORT_INT(m, SNDCTL_SYNTH_MEMAVL);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001110#ifdef SNDCTL_SYNTH_REMOVESAMPLE
Greg Ward1e0f57d2002-11-30 23:05:26 +00001111 _EXPORT_INT(m, SNDCTL_SYNTH_REMOVESAMPLE);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001112#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001113 _EXPORT_INT(m, SNDCTL_TMR_CONTINUE);
1114 _EXPORT_INT(m, SNDCTL_TMR_METRONOME);
1115 _EXPORT_INT(m, SNDCTL_TMR_SELECT);
1116 _EXPORT_INT(m, SNDCTL_TMR_SOURCE);
1117 _EXPORT_INT(m, SNDCTL_TMR_START);
1118 _EXPORT_INT(m, SNDCTL_TMR_STOP);
1119 _EXPORT_INT(m, SNDCTL_TMR_TEMPO);
1120 _EXPORT_INT(m, SNDCTL_TMR_TIMEBASE);
Greg Ward04613a92002-11-30 22:47:45 +00001121}