blob: 6196d367392e794b0abfd74de1e46caf16b399d8 [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 Ward131bce02002-11-30 22:56:44 +0000294
Greg Wardda9f8532002-12-11 14:49:59 +0000295 assert(strlen(fname) <= 30);
Greg Ward131bce02002-11-30 22:56:44 +0000296 strcat(argfmt, fname);
297 if (!PyArg_ParseTuple(args, argfmt))
Greg Wardad4d9b92002-12-30 03:02:22 +0000298 return NULL;
Greg Ward131bce02002-11-30 22:56:44 +0000299
Greg Wardda9f8532002-12-11 14:49:59 +0000300 if (ioctl(fd, cmd, 0) == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000301 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000302 Py_INCREF(Py_None);
303 return Py_None;
304}
305
306
Greg Wardda1cacb2002-12-31 03:02:23 +0000307/* ----------------------------------------------------------------------
Greg Ward499b73e2002-12-31 03:04:52 +0000308 * Methods of DSP objects (OSSAudioType)
Greg Wardda1cacb2002-12-31 03:02:23 +0000309 */
310
Greg Ward131bce02002-11-30 22:56:44 +0000311static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000312oss_nonblock(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000313{
314 /* Hmmm: it doesn't appear to be possible to return to blocking
315 mode once we're in non-blocking mode! */
316 if (!PyArg_ParseTuple(args, ":nonblock"))
Greg Wardad4d9b92002-12-30 03:02:22 +0000317 return NULL;
Greg Ward7b43c682002-12-30 02:29:28 +0000318 if (ioctl(self->fd, SNDCTL_DSP_NONBLOCK, NULL) == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000319 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000320 Py_INCREF(Py_None);
321 return Py_None;
322}
323
324static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000325oss_setfmt(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000326{
Greg Ward7b43c682002-12-30 02:29:28 +0000327 return _do_ioctl_1(self->fd, args, "setfmt", SNDCTL_DSP_SETFMT);
Greg Ward131bce02002-11-30 22:56:44 +0000328}
329
330static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000331oss_getfmts(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000332{
333 int mask;
334 if (!PyArg_ParseTuple(args, ":getfmts"))
Greg Wardad4d9b92002-12-30 03:02:22 +0000335 return NULL;
Greg Ward7b43c682002-12-30 02:29:28 +0000336 if (ioctl(self->fd, SNDCTL_DSP_GETFMTS, &mask) == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000337 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000338 return PyInt_FromLong(mask);
339}
340
341static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000342oss_channels(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000343{
Greg Ward7b43c682002-12-30 02:29:28 +0000344 return _do_ioctl_1(self->fd, args, "channels", SNDCTL_DSP_CHANNELS);
Greg Ward131bce02002-11-30 22:56:44 +0000345}
346
347static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000348oss_speed(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000349{
Greg Ward7b43c682002-12-30 02:29:28 +0000350 return _do_ioctl_1(self->fd, args, "speed", SNDCTL_DSP_SPEED);
Greg Ward131bce02002-11-30 22:56:44 +0000351}
352
353static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000354oss_sync(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000355{
Greg Ward7b43c682002-12-30 02:29:28 +0000356 return _do_ioctl_0(self->fd, args, "sync", SNDCTL_DSP_SYNC);
Greg Ward131bce02002-11-30 22:56:44 +0000357}
358
359static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000360oss_reset(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000361{
Greg Ward7b43c682002-12-30 02:29:28 +0000362 return _do_ioctl_0(self->fd, args, "reset", SNDCTL_DSP_RESET);
Greg Ward131bce02002-11-30 22:56:44 +0000363}
364
365static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000366oss_post(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000367{
Greg Ward7b43c682002-12-30 02:29:28 +0000368 return _do_ioctl_0(self->fd, args, "post", SNDCTL_DSP_POST);
Greg Ward131bce02002-11-30 22:56:44 +0000369}
370
371
372/* Regular file methods: read(), write(), close(), etc. as well
373 as one convenience method, writeall(). */
374
Greg Ward04613a92002-11-30 22:47:45 +0000375static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000376oss_read(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000377{
378 int size, count;
379 char *cp;
380 PyObject *rv;
Greg Wardad4d9b92002-12-30 03:02:22 +0000381
Greg Ward04613a92002-11-30 22:47:45 +0000382 if (!PyArg_ParseTuple(args, "i:read", &size))
383 return NULL;
384 rv = PyString_FromStringAndSize(NULL, size);
385 if (rv == NULL)
386 return NULL;
387 cp = PyString_AS_STRING(rv);
Greg Ward64927852003-05-23 01:50:37 +0000388
389 Py_BEGIN_ALLOW_THREADS
390 count = read(self->fd, cp, size);
391 Py_END_ALLOW_THREADS
392
393 if (count < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000394 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000395 Py_DECREF(rv);
396 return NULL;
397 }
Greg Ward7b43c682002-12-30 02:29:28 +0000398 self->icount += count;
Greg Ward04613a92002-11-30 22:47:45 +0000399 _PyString_Resize(&rv, count);
400 return rv;
401}
402
403static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000404oss_write(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000405{
406 char *cp;
407 int rv, size;
Greg Ward131bce02002-11-30 22:56:44 +0000408
409 if (!PyArg_ParseTuple(args, "s#:write", &cp, &size)) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000410 return NULL;
Greg Ward131bce02002-11-30 22:56:44 +0000411 }
Greg Ward64927852003-05-23 01:50:37 +0000412
413 Py_BEGIN_ALLOW_THREADS
414 rv = write(self->fd, cp, size);
415 Py_END_ALLOW_THREADS
416
417 if (rv == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000418 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000419 } else {
Greg Ward7b43c682002-12-30 02:29:28 +0000420 self->ocount += rv;
Greg Ward131bce02002-11-30 22:56:44 +0000421 }
422 return PyInt_FromLong(rv);
423}
424
425static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000426oss_writeall(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000427{
428 char *cp;
429 int rv, size;
430 fd_set write_set_fds;
431 int select_rv;
432
433 /* NB. writeall() is only useful in non-blocking mode: according to
434 Guenter Geiger <geiger@xdv.org> on the linux-audio-dev list
435 (http://eca.cx/lad/2002/11/0380.html), OSS guarantees that
436 write() in blocking mode consumes the whole buffer. In blocking
437 mode, the behaviour of write() and writeall() from Python is
438 indistinguishable. */
439
440 if (!PyArg_ParseTuple(args, "s#:write", &cp, &size))
441 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000442
443 /* use select to wait for audio device to be available */
444 FD_ZERO(&write_set_fds);
Greg Ward7b43c682002-12-30 02:29:28 +0000445 FD_SET(self->fd, &write_set_fds);
Greg Ward04613a92002-11-30 22:47:45 +0000446
447 while (size > 0) {
Greg Ward64927852003-05-23 01:50:37 +0000448 Py_BEGIN_ALLOW_THREADS
Greg Ward7b43c682002-12-30 02:29:28 +0000449 select_rv = select(self->fd+1, NULL, &write_set_fds, NULL, NULL);
Greg Ward64927852003-05-23 01:50:37 +0000450 Py_END_ALLOW_THREADS
Greg Ward131bce02002-11-30 22:56:44 +0000451 assert(select_rv != 0); /* no timeout, can't expire */
452 if (select_rv == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000453 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000454
Greg Ward64927852003-05-23 01:50:37 +0000455 Py_BEGIN_ALLOW_THREADS
Greg Ward7b43c682002-12-30 02:29:28 +0000456 rv = write(self->fd, cp, size);
Greg Ward64927852003-05-23 01:50:37 +0000457 Py_END_ALLOW_THREADS
Greg Ward131bce02002-11-30 22:56:44 +0000458 if (rv == -1) {
459 if (errno == EAGAIN) { /* buffer is full, try again */
460 errno = 0;
461 continue;
462 } else /* it's a real error */
Greg Ward97708bc2002-11-30 23:17:10 +0000463 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000464 } else { /* wrote rv bytes */
Greg Ward7b43c682002-12-30 02:29:28 +0000465 self->ocount += rv;
Greg Ward131bce02002-11-30 22:56:44 +0000466 size -= rv;
467 cp += rv;
468 }
Greg Ward04613a92002-11-30 22:47:45 +0000469 }
470 Py_INCREF(Py_None);
471 return Py_None;
472}
473
474static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000475oss_close(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000476{
477 if (!PyArg_ParseTuple(args, ":close"))
Greg Wardad4d9b92002-12-30 03:02:22 +0000478 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000479
Greg Ward7b43c682002-12-30 02:29:28 +0000480 if (self->fd >= 0) {
481 close(self->fd);
482 self->fd = -1;
Greg Ward04613a92002-11-30 22:47:45 +0000483 }
484 Py_INCREF(Py_None);
485 return Py_None;
486}
487
488static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000489oss_fileno(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000490{
491 if (!PyArg_ParseTuple(args, ":fileno"))
Greg Wardad4d9b92002-12-30 03:02:22 +0000492 return NULL;
Greg Ward7b43c682002-12-30 02:29:28 +0000493 return PyInt_FromLong(self->fd);
Greg Ward04613a92002-11-30 22:47:45 +0000494}
495
Greg Ward131bce02002-11-30 22:56:44 +0000496
497/* Convenience methods: these generally wrap a couple of ioctls into one
498 common task. */
499
Greg Ward04613a92002-11-30 22:47:45 +0000500static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000501oss_setparameters(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000502{
503 int rate, ssize, nchannels, n, fmt, emulate=0;
504
505 if (!PyArg_ParseTuple(args, "iiii|i:setparameters",
506 &rate, &ssize, &nchannels, &fmt, &emulate))
507 return NULL;
508
509 if (rate < 0) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000510 PyErr_Format(PyExc_ValueError, "expected rate >= 0, not %d",
511 rate);
512 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000513 }
514 if (ssize < 0) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000515 PyErr_Format(PyExc_ValueError, "expected sample size >= 0, not %d",
516 ssize);
517 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000518 }
519 if (nchannels != 1 && nchannels != 2) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000520 PyErr_Format(PyExc_ValueError, "nchannels must be 1 or 2, not %d",
521 nchannels);
522 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000523 }
524
525 for (n = 0; n < n_audio_types; n++)
526 if (fmt == audio_types[n].a_fmt)
527 break;
528 if (n == n_audio_types) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000529 PyErr_Format(PyExc_ValueError, "unknown audio encoding: %d", fmt);
530 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000531 }
532 if (audio_types[n].a_bps != ssize) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000533 PyErr_Format(PyExc_ValueError,
534 "for %s, expected sample size %d, not %d",
535 audio_types[n].a_name, audio_types[n].a_bps, ssize);
536 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000537 }
538
539 if (emulate == 0) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000540 if ((self->afmts & audio_types[n].a_fmt) == 0) {
541 PyErr_Format(PyExc_ValueError,
542 "%s format not supported by device",
543 audio_types[n].a_name);
544 return NULL;
545 }
Greg Ward04613a92002-11-30 22:47:45 +0000546 }
Greg Ward7b43c682002-12-30 02:29:28 +0000547 if (ioctl(self->fd, SNDCTL_DSP_SETFMT,
Greg Wardad4d9b92002-12-30 03:02:22 +0000548 &audio_types[n].a_fmt) == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000549 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000550 return NULL;
551 }
Greg Ward7b43c682002-12-30 02:29:28 +0000552 if (ioctl(self->fd, SNDCTL_DSP_CHANNELS, &nchannels) == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000553 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000554 return NULL;
555 }
Greg Ward7b43c682002-12-30 02:29:28 +0000556 if (ioctl(self->fd, SNDCTL_DSP_SPEED, &rate) == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000557 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000558 return NULL;
559 }
560
561 Py_INCREF(Py_None);
562 return Py_None;
563}
564
565static int
Greg Ward499b73e2002-12-31 03:04:52 +0000566_ssize(oss_audio_t *self, int *nchannels, int *ssize)
Greg Ward04613a92002-11-30 22:47:45 +0000567{
568 int fmt;
569
570 fmt = 0;
Greg Ward7b43c682002-12-30 02:29:28 +0000571 if (ioctl(self->fd, SNDCTL_DSP_SETFMT, &fmt) < 0)
Greg Ward04613a92002-11-30 22:47:45 +0000572 return -errno;
573
574 switch (fmt) {
575 case AFMT_MU_LAW:
576 case AFMT_A_LAW:
577 case AFMT_U8:
578 case AFMT_S8:
579 *ssize = sizeof(char);
580 break;
581 case AFMT_S16_LE:
582 case AFMT_S16_BE:
583 case AFMT_U16_LE:
584 case AFMT_U16_BE:
585 *ssize = sizeof(short);
586 break;
587 case AFMT_MPEG:
588 case AFMT_IMA_ADPCM:
589 default:
590 return -EOPNOTSUPP;
591 }
592 *nchannels = 0;
Greg Ward7b43c682002-12-30 02:29:28 +0000593 if (ioctl(self->fd, SNDCTL_DSP_CHANNELS, nchannels) < 0)
Greg Ward04613a92002-11-30 22:47:45 +0000594 return -errno;
595 return 0;
596}
597
598
599/* bufsize returns the size of the hardware audio buffer in number
600 of samples */
601static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000602oss_bufsize(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000603{
604 audio_buf_info ai;
605 int nchannels, ssize;
606
607 if (!PyArg_ParseTuple(args, ":bufsize")) return NULL;
608
609 if (_ssize(self, &nchannels, &ssize) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000610 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000611 return NULL;
612 }
Greg Ward7b43c682002-12-30 02:29:28 +0000613 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000614 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000615 return NULL;
616 }
617 return PyInt_FromLong((ai.fragstotal * ai.fragsize) / (nchannels * ssize));
618}
619
620/* obufcount returns the number of samples that are available in the
621 hardware for playing */
622static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000623oss_obufcount(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000624{
625 audio_buf_info ai;
626 int nchannels, ssize;
627
628 if (!PyArg_ParseTuple(args, ":obufcount"))
629 return NULL;
630
631 if (_ssize(self, &nchannels, &ssize) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000632 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000633 return NULL;
634 }
Greg Ward7b43c682002-12-30 02:29:28 +0000635 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000636 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000637 return NULL;
638 }
639 return PyInt_FromLong((ai.fragstotal * ai.fragsize - ai.bytes) /
640 (ssize * nchannels));
641}
642
643/* obufcount returns the number of samples that can be played without
644 blocking */
645static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000646oss_obuffree(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000647{
648 audio_buf_info ai;
649 int nchannels, ssize;
650
651 if (!PyArg_ParseTuple(args, ":obuffree"))
652 return NULL;
653
654 if (_ssize(self, &nchannels, &ssize) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000655 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000656 return NULL;
657 }
Greg Ward7b43c682002-12-30 02:29:28 +0000658 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000659 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000660 return NULL;
661 }
662 return PyInt_FromLong(ai.bytes / (ssize * nchannels));
663}
664
Greg Ward04613a92002-11-30 22:47:45 +0000665static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000666oss_getptr(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000667{
668 count_info info;
669 int req;
670
671 if (!PyArg_ParseTuple(args, ":getptr"))
Greg Wardad4d9b92002-12-30 03:02:22 +0000672 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000673
Greg Ward7b43c682002-12-30 02:29:28 +0000674 if (self->mode == O_RDONLY)
Greg Wardad4d9b92002-12-30 03:02:22 +0000675 req = SNDCTL_DSP_GETIPTR;
Greg Ward04613a92002-11-30 22:47:45 +0000676 else
Greg Wardad4d9b92002-12-30 03:02:22 +0000677 req = SNDCTL_DSP_GETOPTR;
Greg Ward7b43c682002-12-30 02:29:28 +0000678 if (ioctl(self->fd, req, &info) == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000679 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000680 return NULL;
681 }
682 return Py_BuildValue("iii", info.bytes, info.blocks, info.ptr);
683}
684
Greg Wardda1cacb2002-12-31 03:02:23 +0000685
686/* ----------------------------------------------------------------------
687 * Methods of mixer objects (OSSMixerType)
688 */
689
Greg Ward3d9994d2002-12-11 15:12:01 +0000690static PyObject *
691oss_mixer_close(oss_mixer_t *self, PyObject *args)
692{
693 if (!PyArg_ParseTuple(args, ":close"))
Greg Wardad4d9b92002-12-30 03:02:22 +0000694 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000695
Greg Ward7b43c682002-12-30 02:29:28 +0000696 if (self->fd >= 0) {
697 close(self->fd);
698 self->fd = -1;
Greg Ward3d9994d2002-12-11 15:12:01 +0000699 }
700 Py_INCREF(Py_None);
701 return Py_None;
702}
703
704static PyObject *
705oss_mixer_fileno(oss_mixer_t *self, PyObject *args)
706{
707 if (!PyArg_ParseTuple(args, ":fileno"))
Greg Wardad4d9b92002-12-30 03:02:22 +0000708 return NULL;
Greg Ward7b43c682002-12-30 02:29:28 +0000709 return PyInt_FromLong(self->fd);
Greg Ward3d9994d2002-12-11 15:12:01 +0000710}
711
712/* Simple mixer interface methods */
713
714static PyObject *
Greg Ward2d6f9a92002-12-31 02:54:43 +0000715oss_mixer_controls(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000716{
Greg Ward2d6f9a92002-12-31 02:54:43 +0000717 return _do_ioctl_1_internal(self->fd, args, "controls",
Greg Ward3d9994d2002-12-11 15:12:01 +0000718 SOUND_MIXER_READ_DEVMASK);
719}
720
721static PyObject *
Greg Ward2d6f9a92002-12-31 02:54:43 +0000722oss_mixer_stereocontrols(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000723{
Greg Ward2d6f9a92002-12-31 02:54:43 +0000724 return _do_ioctl_1_internal(self->fd, args, "stereocontrols",
Greg Ward3d9994d2002-12-11 15:12:01 +0000725 SOUND_MIXER_READ_STEREODEVS);
726}
727
728static PyObject *
Greg Ward2d6f9a92002-12-31 02:54:43 +0000729oss_mixer_reccontrols(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000730{
Greg Ward2d6f9a92002-12-31 02:54:43 +0000731 return _do_ioctl_1_internal(self->fd, args, "reccontrols",
Greg Wardb69bb3d2002-12-12 17:35:45 +0000732 SOUND_MIXER_READ_RECMASK);
Greg Ward3d9994d2002-12-11 15:12:01 +0000733}
734
735static PyObject *
Greg Warde7037662002-12-30 03:01:48 +0000736oss_mixer_get(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000737{
738 int channel, volume;
739
740 /* Can't use _do_ioctl_1 because of encoded arg thingy. */
Greg Warde7037662002-12-30 03:01:48 +0000741 if (!PyArg_ParseTuple(args, "i:get", &channel))
Greg Wardad4d9b92002-12-30 03:02:22 +0000742 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000743
744 if (channel < 0 || channel > SOUND_MIXER_NRDEVICES) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000745 PyErr_SetString(OSSAudioError, "Invalid mixer channel specified.");
746 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000747 }
748
Greg Warde7037662002-12-30 03:01:48 +0000749 if (ioctl(self->fd, MIXER_READ(channel), &volume) == -1)
Greg Wardad4d9b92002-12-30 03:02:22 +0000750 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward3d9994d2002-12-11 15:12:01 +0000751
Greg Warde7037662002-12-30 03:01:48 +0000752 return Py_BuildValue("(ii)", volume & 0xff, (volume & 0xff00) >> 8);
Greg Ward3d9994d2002-12-11 15:12:01 +0000753}
754
755static PyObject *
Greg Warde7037662002-12-30 03:01:48 +0000756oss_mixer_set(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000757{
758 int channel, volume, leftVol, rightVol;
759
760 /* Can't use _do_ioctl_1 because of encoded arg thingy. */
Greg Warde7037662002-12-30 03:01:48 +0000761 if (!PyArg_ParseTuple(args, "i(ii):set", &channel, &leftVol, &rightVol))
Greg Wardad4d9b92002-12-30 03:02:22 +0000762 return NULL;
763
Greg Ward3d9994d2002-12-11 15:12:01 +0000764 if (channel < 0 || channel > SOUND_MIXER_NRDEVICES) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000765 PyErr_SetString(OSSAudioError, "Invalid mixer channel specified.");
766 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000767 }
768
769 if (leftVol < 0 || rightVol < 0 || leftVol > 100 || rightVol > 100) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000770 PyErr_SetString(OSSAudioError, "Volumes must be between 0 and 100.");
771 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000772 }
773
774 volume = (rightVol << 8) | leftVol;
775
Greg Warde7037662002-12-30 03:01:48 +0000776 if (ioctl(self->fd, MIXER_WRITE(channel), &volume) == -1)
Greg Wardad4d9b92002-12-30 03:02:22 +0000777 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward3d9994d2002-12-11 15:12:01 +0000778
Greg Warde7037662002-12-30 03:01:48 +0000779 return Py_BuildValue("(ii)", volume & 0xff, (volume & 0xff00) >> 8);
Greg Ward3d9994d2002-12-11 15:12:01 +0000780}
781
782static PyObject *
Greg Wardf05aa102002-12-30 23:19:32 +0000783oss_mixer_get_recsrc(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000784{
Greg Wardf05aa102002-12-30 23:19:32 +0000785 return _do_ioctl_1_internal(self->fd, args, "get_recsrc",
Greg Ward3d9994d2002-12-11 15:12:01 +0000786 SOUND_MIXER_READ_RECSRC);
787}
788
789static PyObject *
Greg Wardf05aa102002-12-30 23:19:32 +0000790oss_mixer_set_recsrc(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000791{
Greg Wardf05aa102002-12-30 23:19:32 +0000792 return _do_ioctl_1(self->fd, args, "set_recsrc",
Greg Ward3d9994d2002-12-11 15:12:01 +0000793 SOUND_MIXER_WRITE_RECSRC);
794}
795
796
Greg Wardda1cacb2002-12-31 03:02:23 +0000797/* ----------------------------------------------------------------------
798 * Method tables and other bureaucracy
799 */
800
Greg Ward8c6b6a92002-12-11 14:43:13 +0000801static PyMethodDef oss_methods[] = {
Greg Ward131bce02002-11-30 22:56:44 +0000802 /* Regular file methods */
Greg Wardad4d9b92002-12-30 03:02:22 +0000803 { "read", (PyCFunction)oss_read, METH_VARARGS },
804 { "write", (PyCFunction)oss_write, METH_VARARGS },
805 { "writeall", (PyCFunction)oss_writeall, METH_VARARGS },
806 { "close", (PyCFunction)oss_close, METH_VARARGS },
807 { "fileno", (PyCFunction)oss_fileno, METH_VARARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000808
809 /* Simple ioctl wrappers */
Greg Ward8c6b6a92002-12-11 14:43:13 +0000810 { "nonblock", (PyCFunction)oss_nonblock, METH_VARARGS },
Greg Wardad4d9b92002-12-30 03:02:22 +0000811 { "setfmt", (PyCFunction)oss_setfmt, METH_VARARGS },
Greg Ward8c6b6a92002-12-11 14:43:13 +0000812 { "getfmts", (PyCFunction)oss_getfmts, METH_VARARGS },
813 { "channels", (PyCFunction)oss_channels, METH_VARARGS },
814 { "speed", (PyCFunction)oss_speed, METH_VARARGS },
Greg Wardad4d9b92002-12-30 03:02:22 +0000815 { "sync", (PyCFunction)oss_sync, METH_VARARGS },
816 { "reset", (PyCFunction)oss_reset, METH_VARARGS },
817 { "post", (PyCFunction)oss_post, METH_VARARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000818
819 /* Convenience methods -- wrap a couple of ioctls together */
Greg Wardad4d9b92002-12-30 03:02:22 +0000820 { "setparameters", (PyCFunction)oss_setparameters, METH_VARARGS },
821 { "bufsize", (PyCFunction)oss_bufsize, METH_VARARGS },
822 { "obufcount", (PyCFunction)oss_obufcount, METH_VARARGS },
823 { "obuffree", (PyCFunction)oss_obuffree, METH_VARARGS },
Greg Ward8c6b6a92002-12-11 14:43:13 +0000824 { "getptr", (PyCFunction)oss_getptr, METH_VARARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000825
826 /* Aliases for backwards compatibility */
Greg Wardad4d9b92002-12-30 03:02:22 +0000827 { "flush", (PyCFunction)oss_sync, METH_VARARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000828
Greg Wardad4d9b92002-12-30 03:02:22 +0000829 { NULL, NULL} /* sentinel */
Greg Ward04613a92002-11-30 22:47:45 +0000830};
831
Greg Ward3d9994d2002-12-11 15:12:01 +0000832static PyMethodDef oss_mixer_methods[] = {
833 /* Regular file method - OSS mixers are ioctl-only interface */
Greg Wardad4d9b92002-12-30 03:02:22 +0000834 { "close", (PyCFunction)oss_mixer_close, METH_VARARGS },
835 { "fileno", (PyCFunction)oss_mixer_fileno, METH_VARARGS },
Greg Ward3d9994d2002-12-11 15:12:01 +0000836
837 /* Simple ioctl wrappers */
Greg Ward2d6f9a92002-12-31 02:54:43 +0000838 { "controls", (PyCFunction)oss_mixer_controls, METH_VARARGS },
839 { "stereocontrols", (PyCFunction)oss_mixer_stereocontrols, METH_VARARGS},
840 { "reccontrols", (PyCFunction)oss_mixer_reccontrols, METH_VARARGS},
Greg Wardad4d9b92002-12-30 03:02:22 +0000841 { "get", (PyCFunction)oss_mixer_get, METH_VARARGS },
842 { "set", (PyCFunction)oss_mixer_set, METH_VARARGS },
Greg Wardf05aa102002-12-30 23:19:32 +0000843 { "get_recsrc", (PyCFunction)oss_mixer_get_recsrc, METH_VARARGS },
844 { "set_recsrc", (PyCFunction)oss_mixer_set_recsrc, METH_VARARGS },
Greg Ward3d9994d2002-12-11 15:12:01 +0000845
Greg Wardad4d9b92002-12-30 03:02:22 +0000846 { NULL, NULL}
Greg Ward3d9994d2002-12-11 15:12:01 +0000847};
848
Greg Ward04613a92002-11-30 22:47:45 +0000849static PyObject *
Greg Ward58ae13c2002-12-31 03:07:21 +0000850oss_getattr(oss_audio_t *self, char *name)
Greg Ward04613a92002-11-30 22:47:45 +0000851{
Greg Ward58ae13c2002-12-31 03:07:21 +0000852 return Py_FindMethod(oss_methods, (PyObject *)self, name);
Greg Ward04613a92002-11-30 22:47:45 +0000853}
854
Greg Ward3d9994d2002-12-11 15:12:01 +0000855static PyObject *
Greg Ward58ae13c2002-12-31 03:07:21 +0000856oss_mixer_getattr(oss_mixer_t *self, char *name)
Greg Ward3d9994d2002-12-11 15:12:01 +0000857{
Greg Ward58ae13c2002-12-31 03:07:21 +0000858 return Py_FindMethod(oss_mixer_methods, (PyObject *)self, name);
Greg Ward3d9994d2002-12-11 15:12:01 +0000859}
860
Greg Ward499b73e2002-12-31 03:04:52 +0000861static PyTypeObject OSSAudioType = {
Greg Ward04613a92002-11-30 22:47:45 +0000862 PyObject_HEAD_INIT(&PyType_Type)
Greg Wardad4d9b92002-12-30 03:02:22 +0000863 0, /*ob_size*/
Greg Ward9a568eb2002-11-30 23:20:09 +0000864 "ossaudiodev.oss_audio_device", /*tp_name*/
Greg Ward499b73e2002-12-31 03:04:52 +0000865 sizeof(oss_audio_t), /*tp_size*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000866 0, /*tp_itemsize*/
Greg Ward04613a92002-11-30 22:47:45 +0000867 /* methods */
Greg Wardad4d9b92002-12-30 03:02:22 +0000868 (destructor)oss_dealloc, /*tp_dealloc*/
869 0, /*tp_print*/
870 (getattrfunc)oss_getattr, /*tp_getattr*/
871 0, /*tp_setattr*/
872 0, /*tp_compare*/
873 0, /*tp_repr*/
Greg Ward04613a92002-11-30 22:47:45 +0000874};
875
Greg Ward3d9994d2002-12-11 15:12:01 +0000876static PyTypeObject OSSMixerType = {
877 PyObject_HEAD_INIT(&PyType_Type)
Greg Wardad4d9b92002-12-30 03:02:22 +0000878 0, /*ob_size*/
Greg Ward3d9994d2002-12-11 15:12:01 +0000879 "ossaudiodev.oss_mixer_device", /*tp_name*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000880 sizeof(oss_mixer_t), /*tp_size*/
881 0, /*tp_itemsize*/
Greg Ward3d9994d2002-12-11 15:12:01 +0000882 /* methods */
883 (destructor)oss_mixer_dealloc, /*tp_dealloc*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000884 0, /*tp_print*/
Greg Ward3d9994d2002-12-11 15:12:01 +0000885 (getattrfunc)oss_mixer_getattr, /*tp_getattr*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000886 0, /*tp_setattr*/
887 0, /*tp_compare*/
888 0, /*tp_repr*/
Greg Ward3d9994d2002-12-11 15:12:01 +0000889};
890
891
Greg Ward04613a92002-11-30 22:47:45 +0000892static PyObject *
Greg Ward9a568eb2002-11-30 23:20:09 +0000893ossopen(PyObject *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000894{
Greg Ward8c6b6a92002-12-11 14:43:13 +0000895 return (PyObject *)newossobject(args);
Greg Ward04613a92002-11-30 22:47:45 +0000896}
897
Greg Ward3d9994d2002-12-11 15:12:01 +0000898static PyObject *
899ossopenmixer(PyObject *self, PyObject *args)
900{
901 return (PyObject *)newossmixerobject(args);
902}
903
Greg Ward9a568eb2002-11-30 23:20:09 +0000904static PyMethodDef ossaudiodev_methods[] = {
905 { "open", ossopen, METH_VARARGS },
Greg Ward3d9994d2002-12-11 15:12:01 +0000906 { "openmixer", ossopenmixer, METH_VARARGS },
Greg Ward04613a92002-11-30 22:47:45 +0000907 { 0, 0 },
908};
909
Greg Ward1e0f57d2002-11-30 23:05:26 +0000910
911#define _EXPORT_INT(mod, name) \
912 if (PyModule_AddIntConstant(mod, #name, (long) (name)) == -1) return;
913
Greg Ward744f0fd2002-12-31 03:23:59 +0000914
915static char *control_labels[] = SOUND_DEVICE_LABELS;
916static char *control_names[] = SOUND_DEVICE_NAMES;
917
918
919static int
920build_namelists (PyObject *module)
921{
922 PyObject *labels;
923 PyObject *names;
924 PyObject *s;
925 int num_controls;
926 int i;
927
928 num_controls = sizeof(control_labels) / sizeof(control_labels[0]);
929 assert(num_controls == sizeof(control_names) / sizeof(control_names[0]));
930
931 labels = PyList_New(num_controls);
932 names = PyList_New(num_controls);
933 for (i = 0; i < num_controls; i++) {
934 s = PyString_FromString(control_labels[i]);
935 if (s == NULL)
936 return -1;
937 PyList_SET_ITEM(labels, i, s);
938
939 s = PyString_FromString(control_names[i]);
940 if (s == NULL)
941 return -1;
942 PyList_SET_ITEM(names, i, s);
943 }
944
945 if (PyModule_AddObject(module, "control_labels", labels) == -1)
946 return -1;
947 if (PyModule_AddObject(module, "control_names", names) == -1)
948 return -1;
949
950 return 0;
951}
952
953
Greg Ward04613a92002-11-30 22:47:45 +0000954void
Greg Ward9a568eb2002-11-30 23:20:09 +0000955initossaudiodev(void)
Greg Ward04613a92002-11-30 22:47:45 +0000956{
957 PyObject *m;
958
Greg Ward9a568eb2002-11-30 23:20:09 +0000959 m = Py_InitModule("ossaudiodev", ossaudiodev_methods);
Greg Ward04613a92002-11-30 22:47:45 +0000960
Greg Ward97708bc2002-11-30 23:17:10 +0000961 OSSAudioError = PyErr_NewException("ossaudiodev.error", NULL, NULL);
962 if (OSSAudioError)
Greg Wardad4d9b92002-12-30 03:02:22 +0000963 PyModule_AddObject(m, "error", OSSAudioError);
Greg Ward04613a92002-11-30 22:47:45 +0000964
Greg Ward744f0fd2002-12-31 03:23:59 +0000965 /* Build 'control_labels' and 'control_names' lists and add them
966 to the module. */
967 if (build_namelists(m) == -1) /* XXX what to do here? */
968 return;
969
Greg Ward1e0f57d2002-11-30 23:05:26 +0000970 /* Expose the audio format numbers -- essential! */
971 _EXPORT_INT(m, AFMT_QUERY);
972 _EXPORT_INT(m, AFMT_MU_LAW);
973 _EXPORT_INT(m, AFMT_A_LAW);
974 _EXPORT_INT(m, AFMT_IMA_ADPCM);
975 _EXPORT_INT(m, AFMT_U8);
976 _EXPORT_INT(m, AFMT_S16_LE);
977 _EXPORT_INT(m, AFMT_S16_BE);
978 _EXPORT_INT(m, AFMT_S8);
979 _EXPORT_INT(m, AFMT_U16_LE);
980 _EXPORT_INT(m, AFMT_U16_BE);
981 _EXPORT_INT(m, AFMT_MPEG);
Andrew M. Kuchlingfe62bc92003-02-13 13:27:07 +0000982#ifdef AFMT_AC3
Greg Ward1e0f57d2002-11-30 23:05:26 +0000983 _EXPORT_INT(m, AFMT_AC3);
Andrew M. Kuchlingfe62bc92003-02-13 13:27:07 +0000984#endif
Neal Norwitzd156c2d2003-02-02 17:59:06 +0000985#ifdef AFMT_S16_NE
Greg Ward1e0f57d2002-11-30 23:05:26 +0000986 _EXPORT_INT(m, AFMT_S16_NE);
Neal Norwitzd156c2d2003-02-02 17:59:06 +0000987#endif
Greg Wardad4d9b92002-12-30 03:02:22 +0000988
Greg Ward37897c22002-12-30 02:43:36 +0000989 /* Expose the sound mixer device numbers. */
Greg Ward7b43c682002-12-30 02:29:28 +0000990 _EXPORT_INT(m, SOUND_MIXER_NRDEVICES);
Greg Ward3d9994d2002-12-11 15:12:01 +0000991 _EXPORT_INT(m, SOUND_MIXER_VOLUME);
992 _EXPORT_INT(m, SOUND_MIXER_BASS);
993 _EXPORT_INT(m, SOUND_MIXER_TREBLE);
994 _EXPORT_INT(m, SOUND_MIXER_SYNTH);
995 _EXPORT_INT(m, SOUND_MIXER_PCM);
996 _EXPORT_INT(m, SOUND_MIXER_SPEAKER);
997 _EXPORT_INT(m, SOUND_MIXER_LINE);
998 _EXPORT_INT(m, SOUND_MIXER_MIC);
999 _EXPORT_INT(m, SOUND_MIXER_CD);
1000 _EXPORT_INT(m, SOUND_MIXER_IMIX);
1001 _EXPORT_INT(m, SOUND_MIXER_ALTPCM);
1002 _EXPORT_INT(m, SOUND_MIXER_RECLEV);
1003 _EXPORT_INT(m, SOUND_MIXER_IGAIN);
1004 _EXPORT_INT(m, SOUND_MIXER_OGAIN);
1005 _EXPORT_INT(m, SOUND_MIXER_LINE1);
1006 _EXPORT_INT(m, SOUND_MIXER_LINE2);
1007 _EXPORT_INT(m, SOUND_MIXER_LINE3);
1008 _EXPORT_INT(m, SOUND_MIXER_DIGITAL1);
1009 _EXPORT_INT(m, SOUND_MIXER_DIGITAL2);
1010 _EXPORT_INT(m, SOUND_MIXER_DIGITAL3);
1011 _EXPORT_INT(m, SOUND_MIXER_PHONEIN);
1012 _EXPORT_INT(m, SOUND_MIXER_PHONEOUT);
1013 _EXPORT_INT(m, SOUND_MIXER_VIDEO);
1014 _EXPORT_INT(m, SOUND_MIXER_RADIO);
1015 _EXPORT_INT(m, SOUND_MIXER_MONITOR);
Greg Ward04613a92002-11-30 22:47:45 +00001016
Greg Ward1e0f57d2002-11-30 23:05:26 +00001017 /* Expose all the ioctl numbers for masochists who like to do this
1018 stuff directly. */
1019 _EXPORT_INT(m, SNDCTL_COPR_HALT);
1020 _EXPORT_INT(m, SNDCTL_COPR_LOAD);
1021 _EXPORT_INT(m, SNDCTL_COPR_RCODE);
1022 _EXPORT_INT(m, SNDCTL_COPR_RCVMSG);
1023 _EXPORT_INT(m, SNDCTL_COPR_RDATA);
1024 _EXPORT_INT(m, SNDCTL_COPR_RESET);
1025 _EXPORT_INT(m, SNDCTL_COPR_RUN);
1026 _EXPORT_INT(m, SNDCTL_COPR_SENDMSG);
1027 _EXPORT_INT(m, SNDCTL_COPR_WCODE);
1028 _EXPORT_INT(m, SNDCTL_COPR_WDATA);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001029#ifdef SNDCTL_DSP_BIND_CHANNEL
Greg Ward1e0f57d2002-11-30 23:05:26 +00001030 _EXPORT_INT(m, SNDCTL_DSP_BIND_CHANNEL);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001031#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001032 _EXPORT_INT(m, SNDCTL_DSP_CHANNELS);
1033 _EXPORT_INT(m, SNDCTL_DSP_GETBLKSIZE);
1034 _EXPORT_INT(m, SNDCTL_DSP_GETCAPS);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001035#ifdef SNDCTL_DSP_GETCHANNELMASK
Greg Ward1e0f57d2002-11-30 23:05:26 +00001036 _EXPORT_INT(m, SNDCTL_DSP_GETCHANNELMASK);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001037#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001038 _EXPORT_INT(m, SNDCTL_DSP_GETFMTS);
1039 _EXPORT_INT(m, SNDCTL_DSP_GETIPTR);
1040 _EXPORT_INT(m, SNDCTL_DSP_GETISPACE);
1041 _EXPORT_INT(m, SNDCTL_DSP_GETODELAY);
1042 _EXPORT_INT(m, SNDCTL_DSP_GETOPTR);
1043 _EXPORT_INT(m, SNDCTL_DSP_GETOSPACE);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001044#ifdef SNDCTL_DSP_GETSPDIF
Greg Ward1e0f57d2002-11-30 23:05:26 +00001045 _EXPORT_INT(m, SNDCTL_DSP_GETSPDIF);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001046#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001047 _EXPORT_INT(m, SNDCTL_DSP_GETTRIGGER);
1048 _EXPORT_INT(m, SNDCTL_DSP_MAPINBUF);
1049 _EXPORT_INT(m, SNDCTL_DSP_MAPOUTBUF);
1050 _EXPORT_INT(m, SNDCTL_DSP_NONBLOCK);
1051 _EXPORT_INT(m, SNDCTL_DSP_POST);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001052#ifdef SNDCTL_DSP_PROFILE
Greg Ward1e0f57d2002-11-30 23:05:26 +00001053 _EXPORT_INT(m, SNDCTL_DSP_PROFILE);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001054#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001055 _EXPORT_INT(m, SNDCTL_DSP_RESET);
1056 _EXPORT_INT(m, SNDCTL_DSP_SAMPLESIZE);
1057 _EXPORT_INT(m, SNDCTL_DSP_SETDUPLEX);
1058 _EXPORT_INT(m, SNDCTL_DSP_SETFMT);
1059 _EXPORT_INT(m, SNDCTL_DSP_SETFRAGMENT);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001060#ifdef SNDCTL_DSP_SETSPDIF
Greg Ward1e0f57d2002-11-30 23:05:26 +00001061 _EXPORT_INT(m, SNDCTL_DSP_SETSPDIF);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001062#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001063 _EXPORT_INT(m, SNDCTL_DSP_SETSYNCRO);
1064 _EXPORT_INT(m, SNDCTL_DSP_SETTRIGGER);
1065 _EXPORT_INT(m, SNDCTL_DSP_SPEED);
1066 _EXPORT_INT(m, SNDCTL_DSP_STEREO);
1067 _EXPORT_INT(m, SNDCTL_DSP_SUBDIVIDE);
1068 _EXPORT_INT(m, SNDCTL_DSP_SYNC);
1069 _EXPORT_INT(m, SNDCTL_FM_4OP_ENABLE);
1070 _EXPORT_INT(m, SNDCTL_FM_LOAD_INSTR);
1071 _EXPORT_INT(m, SNDCTL_MIDI_INFO);
1072 _EXPORT_INT(m, SNDCTL_MIDI_MPUCMD);
1073 _EXPORT_INT(m, SNDCTL_MIDI_MPUMODE);
1074 _EXPORT_INT(m, SNDCTL_MIDI_PRETIME);
1075 _EXPORT_INT(m, SNDCTL_SEQ_CTRLRATE);
1076 _EXPORT_INT(m, SNDCTL_SEQ_GETINCOUNT);
1077 _EXPORT_INT(m, SNDCTL_SEQ_GETOUTCOUNT);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001078#ifdef SNDCTL_SEQ_GETTIME
Greg Ward1e0f57d2002-11-30 23:05:26 +00001079 _EXPORT_INT(m, SNDCTL_SEQ_GETTIME);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001080#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001081 _EXPORT_INT(m, SNDCTL_SEQ_NRMIDIS);
1082 _EXPORT_INT(m, SNDCTL_SEQ_NRSYNTHS);
1083 _EXPORT_INT(m, SNDCTL_SEQ_OUTOFBAND);
1084 _EXPORT_INT(m, SNDCTL_SEQ_PANIC);
1085 _EXPORT_INT(m, SNDCTL_SEQ_PERCMODE);
1086 _EXPORT_INT(m, SNDCTL_SEQ_RESET);
1087 _EXPORT_INT(m, SNDCTL_SEQ_RESETSAMPLES);
1088 _EXPORT_INT(m, SNDCTL_SEQ_SYNC);
1089 _EXPORT_INT(m, SNDCTL_SEQ_TESTMIDI);
1090 _EXPORT_INT(m, SNDCTL_SEQ_THRESHOLD);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001091#ifdef SNDCTL_SYNTH_CONTROL
Greg Ward1e0f57d2002-11-30 23:05:26 +00001092 _EXPORT_INT(m, SNDCTL_SYNTH_CONTROL);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001093#endif
1094#ifdef SNDCTL_SYNTH_ID
Greg Ward1e0f57d2002-11-30 23:05:26 +00001095 _EXPORT_INT(m, SNDCTL_SYNTH_ID);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001096#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001097 _EXPORT_INT(m, SNDCTL_SYNTH_INFO);
1098 _EXPORT_INT(m, SNDCTL_SYNTH_MEMAVL);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001099#ifdef SNDCTL_SYNTH_REMOVESAMPLE
Greg Ward1e0f57d2002-11-30 23:05:26 +00001100 _EXPORT_INT(m, SNDCTL_SYNTH_REMOVESAMPLE);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001101#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001102 _EXPORT_INT(m, SNDCTL_TMR_CONTINUE);
1103 _EXPORT_INT(m, SNDCTL_TMR_METRONOME);
1104 _EXPORT_INT(m, SNDCTL_TMR_SELECT);
1105 _EXPORT_INT(m, SNDCTL_TMR_SOURCE);
1106 _EXPORT_INT(m, SNDCTL_TMR_START);
1107 _EXPORT_INT(m, SNDCTL_TMR_STOP);
1108 _EXPORT_INT(m, SNDCTL_TMR_TEMPO);
1109 _EXPORT_INT(m, SNDCTL_TMR_TIMEBASE);
Greg Ward04613a92002-11-30 22:47:45 +00001110}