blob: 948ffe7e1e6c41377b17f1ef58b5b18ba32938ab [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 Ward7b43c682002-12-30 02:29:28 +0000388 if ((count = read(self->fd, cp, size)) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000389 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000390 Py_DECREF(rv);
391 return NULL;
392 }
Greg Ward7b43c682002-12-30 02:29:28 +0000393 self->icount += count;
Greg Ward04613a92002-11-30 22:47:45 +0000394 _PyString_Resize(&rv, count);
395 return rv;
396}
397
398static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000399oss_write(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000400{
401 char *cp;
402 int rv, size;
Greg Ward131bce02002-11-30 22:56:44 +0000403
404 if (!PyArg_ParseTuple(args, "s#:write", &cp, &size)) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000405 return NULL;
Greg Ward131bce02002-11-30 22:56:44 +0000406 }
Greg Ward7b43c682002-12-30 02:29:28 +0000407 if ((rv = write(self->fd, cp, size)) == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000408 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000409 } else {
Greg Ward7b43c682002-12-30 02:29:28 +0000410 self->ocount += rv;
Greg Ward131bce02002-11-30 22:56:44 +0000411 }
412 return PyInt_FromLong(rv);
413}
414
415static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000416oss_writeall(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000417{
418 char *cp;
419 int rv, size;
420 fd_set write_set_fds;
421 int select_rv;
422
423 /* NB. writeall() is only useful in non-blocking mode: according to
424 Guenter Geiger <geiger@xdv.org> on the linux-audio-dev list
425 (http://eca.cx/lad/2002/11/0380.html), OSS guarantees that
426 write() in blocking mode consumes the whole buffer. In blocking
427 mode, the behaviour of write() and writeall() from Python is
428 indistinguishable. */
429
430 if (!PyArg_ParseTuple(args, "s#:write", &cp, &size))
431 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000432
433 /* use select to wait for audio device to be available */
434 FD_ZERO(&write_set_fds);
Greg Ward7b43c682002-12-30 02:29:28 +0000435 FD_SET(self->fd, &write_set_fds);
Greg Ward04613a92002-11-30 22:47:45 +0000436
437 while (size > 0) {
Greg Ward7b43c682002-12-30 02:29:28 +0000438 select_rv = select(self->fd+1, NULL, &write_set_fds, NULL, NULL);
Greg Ward131bce02002-11-30 22:56:44 +0000439 assert(select_rv != 0); /* no timeout, can't expire */
440 if (select_rv == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000441 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000442
Greg Ward7b43c682002-12-30 02:29:28 +0000443 rv = write(self->fd, cp, size);
Greg Ward131bce02002-11-30 22:56:44 +0000444 if (rv == -1) {
445 if (errno == EAGAIN) { /* buffer is full, try again */
446 errno = 0;
447 continue;
448 } else /* it's a real error */
Greg Ward97708bc2002-11-30 23:17:10 +0000449 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000450 } else { /* wrote rv bytes */
Greg Ward7b43c682002-12-30 02:29:28 +0000451 self->ocount += rv;
Greg Ward131bce02002-11-30 22:56:44 +0000452 size -= rv;
453 cp += rv;
454 }
Greg Ward04613a92002-11-30 22:47:45 +0000455 }
456 Py_INCREF(Py_None);
457 return Py_None;
458}
459
460static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000461oss_close(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000462{
463 if (!PyArg_ParseTuple(args, ":close"))
Greg Wardad4d9b92002-12-30 03:02:22 +0000464 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000465
Greg Ward7b43c682002-12-30 02:29:28 +0000466 if (self->fd >= 0) {
467 close(self->fd);
468 self->fd = -1;
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_fileno(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000476{
477 if (!PyArg_ParseTuple(args, ":fileno"))
Greg Wardad4d9b92002-12-30 03:02:22 +0000478 return NULL;
Greg Ward7b43c682002-12-30 02:29:28 +0000479 return PyInt_FromLong(self->fd);
Greg Ward04613a92002-11-30 22:47:45 +0000480}
481
Greg Ward131bce02002-11-30 22:56:44 +0000482
483/* Convenience methods: these generally wrap a couple of ioctls into one
484 common task. */
485
Greg Ward04613a92002-11-30 22:47:45 +0000486static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000487oss_setparameters(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000488{
489 int rate, ssize, nchannels, n, fmt, emulate=0;
490
491 if (!PyArg_ParseTuple(args, "iiii|i:setparameters",
492 &rate, &ssize, &nchannels, &fmt, &emulate))
493 return NULL;
494
495 if (rate < 0) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000496 PyErr_Format(PyExc_ValueError, "expected rate >= 0, not %d",
497 rate);
498 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000499 }
500 if (ssize < 0) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000501 PyErr_Format(PyExc_ValueError, "expected sample size >= 0, not %d",
502 ssize);
503 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000504 }
505 if (nchannels != 1 && nchannels != 2) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000506 PyErr_Format(PyExc_ValueError, "nchannels must be 1 or 2, not %d",
507 nchannels);
508 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000509 }
510
511 for (n = 0; n < n_audio_types; n++)
512 if (fmt == audio_types[n].a_fmt)
513 break;
514 if (n == n_audio_types) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000515 PyErr_Format(PyExc_ValueError, "unknown audio encoding: %d", fmt);
516 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000517 }
518 if (audio_types[n].a_bps != ssize) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000519 PyErr_Format(PyExc_ValueError,
520 "for %s, expected sample size %d, not %d",
521 audio_types[n].a_name, audio_types[n].a_bps, ssize);
522 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000523 }
524
525 if (emulate == 0) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000526 if ((self->afmts & audio_types[n].a_fmt) == 0) {
527 PyErr_Format(PyExc_ValueError,
528 "%s format not supported by device",
529 audio_types[n].a_name);
530 return NULL;
531 }
Greg Ward04613a92002-11-30 22:47:45 +0000532 }
Greg Ward7b43c682002-12-30 02:29:28 +0000533 if (ioctl(self->fd, SNDCTL_DSP_SETFMT,
Greg Wardad4d9b92002-12-30 03:02:22 +0000534 &audio_types[n].a_fmt) == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000535 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000536 return NULL;
537 }
Greg Ward7b43c682002-12-30 02:29:28 +0000538 if (ioctl(self->fd, SNDCTL_DSP_CHANNELS, &nchannels) == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000539 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000540 return NULL;
541 }
Greg Ward7b43c682002-12-30 02:29:28 +0000542 if (ioctl(self->fd, SNDCTL_DSP_SPEED, &rate) == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000543 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000544 return NULL;
545 }
546
547 Py_INCREF(Py_None);
548 return Py_None;
549}
550
551static int
Greg Ward499b73e2002-12-31 03:04:52 +0000552_ssize(oss_audio_t *self, int *nchannels, int *ssize)
Greg Ward04613a92002-11-30 22:47:45 +0000553{
554 int fmt;
555
556 fmt = 0;
Greg Ward7b43c682002-12-30 02:29:28 +0000557 if (ioctl(self->fd, SNDCTL_DSP_SETFMT, &fmt) < 0)
Greg Ward04613a92002-11-30 22:47:45 +0000558 return -errno;
559
560 switch (fmt) {
561 case AFMT_MU_LAW:
562 case AFMT_A_LAW:
563 case AFMT_U8:
564 case AFMT_S8:
565 *ssize = sizeof(char);
566 break;
567 case AFMT_S16_LE:
568 case AFMT_S16_BE:
569 case AFMT_U16_LE:
570 case AFMT_U16_BE:
571 *ssize = sizeof(short);
572 break;
573 case AFMT_MPEG:
574 case AFMT_IMA_ADPCM:
575 default:
576 return -EOPNOTSUPP;
577 }
578 *nchannels = 0;
Greg Ward7b43c682002-12-30 02:29:28 +0000579 if (ioctl(self->fd, SNDCTL_DSP_CHANNELS, nchannels) < 0)
Greg Ward04613a92002-11-30 22:47:45 +0000580 return -errno;
581 return 0;
582}
583
584
585/* bufsize returns the size of the hardware audio buffer in number
586 of samples */
587static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000588oss_bufsize(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000589{
590 audio_buf_info ai;
591 int nchannels, ssize;
592
593 if (!PyArg_ParseTuple(args, ":bufsize")) return NULL;
594
595 if (_ssize(self, &nchannels, &ssize) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000596 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000597 return NULL;
598 }
Greg Ward7b43c682002-12-30 02:29:28 +0000599 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000600 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000601 return NULL;
602 }
603 return PyInt_FromLong((ai.fragstotal * ai.fragsize) / (nchannels * ssize));
604}
605
606/* obufcount returns the number of samples that are available in the
607 hardware for playing */
608static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000609oss_obufcount(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000610{
611 audio_buf_info ai;
612 int nchannels, ssize;
613
614 if (!PyArg_ParseTuple(args, ":obufcount"))
615 return NULL;
616
617 if (_ssize(self, &nchannels, &ssize) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000618 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000619 return NULL;
620 }
Greg Ward7b43c682002-12-30 02:29:28 +0000621 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000622 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000623 return NULL;
624 }
625 return PyInt_FromLong((ai.fragstotal * ai.fragsize - ai.bytes) /
626 (ssize * nchannels));
627}
628
629/* obufcount returns the number of samples that can be played without
630 blocking */
631static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000632oss_obuffree(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000633{
634 audio_buf_info ai;
635 int nchannels, ssize;
636
637 if (!PyArg_ParseTuple(args, ":obuffree"))
638 return NULL;
639
640 if (_ssize(self, &nchannels, &ssize) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000641 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000642 return NULL;
643 }
Greg Ward7b43c682002-12-30 02:29:28 +0000644 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000645 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000646 return NULL;
647 }
648 return PyInt_FromLong(ai.bytes / (ssize * nchannels));
649}
650
Greg Ward04613a92002-11-30 22:47:45 +0000651static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000652oss_getptr(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000653{
654 count_info info;
655 int req;
656
657 if (!PyArg_ParseTuple(args, ":getptr"))
Greg Wardad4d9b92002-12-30 03:02:22 +0000658 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000659
Greg Ward7b43c682002-12-30 02:29:28 +0000660 if (self->mode == O_RDONLY)
Greg Wardad4d9b92002-12-30 03:02:22 +0000661 req = SNDCTL_DSP_GETIPTR;
Greg Ward04613a92002-11-30 22:47:45 +0000662 else
Greg Wardad4d9b92002-12-30 03:02:22 +0000663 req = SNDCTL_DSP_GETOPTR;
Greg Ward7b43c682002-12-30 02:29:28 +0000664 if (ioctl(self->fd, req, &info) == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000665 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000666 return NULL;
667 }
668 return Py_BuildValue("iii", info.bytes, info.blocks, info.ptr);
669}
670
Greg Wardda1cacb2002-12-31 03:02:23 +0000671
672/* ----------------------------------------------------------------------
673 * Methods of mixer objects (OSSMixerType)
674 */
675
Greg Ward3d9994d2002-12-11 15:12:01 +0000676static PyObject *
677oss_mixer_close(oss_mixer_t *self, PyObject *args)
678{
679 if (!PyArg_ParseTuple(args, ":close"))
Greg Wardad4d9b92002-12-30 03:02:22 +0000680 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000681
Greg Ward7b43c682002-12-30 02:29:28 +0000682 if (self->fd >= 0) {
683 close(self->fd);
684 self->fd = -1;
Greg Ward3d9994d2002-12-11 15:12:01 +0000685 }
686 Py_INCREF(Py_None);
687 return Py_None;
688}
689
690static PyObject *
691oss_mixer_fileno(oss_mixer_t *self, PyObject *args)
692{
693 if (!PyArg_ParseTuple(args, ":fileno"))
Greg Wardad4d9b92002-12-30 03:02:22 +0000694 return NULL;
Greg Ward7b43c682002-12-30 02:29:28 +0000695 return PyInt_FromLong(self->fd);
Greg Ward3d9994d2002-12-11 15:12:01 +0000696}
697
698/* Simple mixer interface methods */
699
700static PyObject *
Greg Ward2d6f9a92002-12-31 02:54:43 +0000701oss_mixer_controls(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000702{
Greg Ward2d6f9a92002-12-31 02:54:43 +0000703 return _do_ioctl_1_internal(self->fd, args, "controls",
Greg Ward3d9994d2002-12-11 15:12:01 +0000704 SOUND_MIXER_READ_DEVMASK);
705}
706
707static PyObject *
Greg Ward2d6f9a92002-12-31 02:54:43 +0000708oss_mixer_stereocontrols(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000709{
Greg Ward2d6f9a92002-12-31 02:54:43 +0000710 return _do_ioctl_1_internal(self->fd, args, "stereocontrols",
Greg Ward3d9994d2002-12-11 15:12:01 +0000711 SOUND_MIXER_READ_STEREODEVS);
712}
713
714static PyObject *
Greg Ward2d6f9a92002-12-31 02:54:43 +0000715oss_mixer_reccontrols(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, "reccontrols",
Greg Wardb69bb3d2002-12-12 17:35:45 +0000718 SOUND_MIXER_READ_RECMASK);
Greg Ward3d9994d2002-12-11 15:12:01 +0000719}
720
721static PyObject *
Greg Warde7037662002-12-30 03:01:48 +0000722oss_mixer_get(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000723{
724 int channel, volume;
725
726 /* Can't use _do_ioctl_1 because of encoded arg thingy. */
Greg Warde7037662002-12-30 03:01:48 +0000727 if (!PyArg_ParseTuple(args, "i:get", &channel))
Greg Wardad4d9b92002-12-30 03:02:22 +0000728 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000729
730 if (channel < 0 || channel > SOUND_MIXER_NRDEVICES) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000731 PyErr_SetString(OSSAudioError, "Invalid mixer channel specified.");
732 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000733 }
734
Greg Warde7037662002-12-30 03:01:48 +0000735 if (ioctl(self->fd, MIXER_READ(channel), &volume) == -1)
Greg Wardad4d9b92002-12-30 03:02:22 +0000736 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward3d9994d2002-12-11 15:12:01 +0000737
Greg Warde7037662002-12-30 03:01:48 +0000738 return Py_BuildValue("(ii)", volume & 0xff, (volume & 0xff00) >> 8);
Greg Ward3d9994d2002-12-11 15:12:01 +0000739}
740
741static PyObject *
Greg Warde7037662002-12-30 03:01:48 +0000742oss_mixer_set(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000743{
744 int channel, volume, leftVol, rightVol;
745
746 /* Can't use _do_ioctl_1 because of encoded arg thingy. */
Greg Warde7037662002-12-30 03:01:48 +0000747 if (!PyArg_ParseTuple(args, "i(ii):set", &channel, &leftVol, &rightVol))
Greg Wardad4d9b92002-12-30 03:02:22 +0000748 return NULL;
749
Greg Ward3d9994d2002-12-11 15:12:01 +0000750 if (channel < 0 || channel > SOUND_MIXER_NRDEVICES) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000751 PyErr_SetString(OSSAudioError, "Invalid mixer channel specified.");
752 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000753 }
754
755 if (leftVol < 0 || rightVol < 0 || leftVol > 100 || rightVol > 100) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000756 PyErr_SetString(OSSAudioError, "Volumes must be between 0 and 100.");
757 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000758 }
759
760 volume = (rightVol << 8) | leftVol;
761
Greg Warde7037662002-12-30 03:01:48 +0000762 if (ioctl(self->fd, MIXER_WRITE(channel), &volume) == -1)
Greg Wardad4d9b92002-12-30 03:02:22 +0000763 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward3d9994d2002-12-11 15:12:01 +0000764
Greg Warde7037662002-12-30 03:01:48 +0000765 return Py_BuildValue("(ii)", volume & 0xff, (volume & 0xff00) >> 8);
Greg Ward3d9994d2002-12-11 15:12:01 +0000766}
767
768static PyObject *
Greg Wardf05aa102002-12-30 23:19:32 +0000769oss_mixer_get_recsrc(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000770{
Greg Wardf05aa102002-12-30 23:19:32 +0000771 return _do_ioctl_1_internal(self->fd, args, "get_recsrc",
Greg Ward3d9994d2002-12-11 15:12:01 +0000772 SOUND_MIXER_READ_RECSRC);
773}
774
775static PyObject *
Greg Wardf05aa102002-12-30 23:19:32 +0000776oss_mixer_set_recsrc(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000777{
Greg Wardf05aa102002-12-30 23:19:32 +0000778 return _do_ioctl_1(self->fd, args, "set_recsrc",
Greg Ward3d9994d2002-12-11 15:12:01 +0000779 SOUND_MIXER_WRITE_RECSRC);
780}
781
782
Greg Wardda1cacb2002-12-31 03:02:23 +0000783/* ----------------------------------------------------------------------
784 * Method tables and other bureaucracy
785 */
786
Greg Ward8c6b6a92002-12-11 14:43:13 +0000787static PyMethodDef oss_methods[] = {
Greg Ward131bce02002-11-30 22:56:44 +0000788 /* Regular file methods */
Greg Wardad4d9b92002-12-30 03:02:22 +0000789 { "read", (PyCFunction)oss_read, METH_VARARGS },
790 { "write", (PyCFunction)oss_write, METH_VARARGS },
791 { "writeall", (PyCFunction)oss_writeall, METH_VARARGS },
792 { "close", (PyCFunction)oss_close, METH_VARARGS },
793 { "fileno", (PyCFunction)oss_fileno, METH_VARARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000794
795 /* Simple ioctl wrappers */
Greg Ward8c6b6a92002-12-11 14:43:13 +0000796 { "nonblock", (PyCFunction)oss_nonblock, METH_VARARGS },
Greg Wardad4d9b92002-12-30 03:02:22 +0000797 { "setfmt", (PyCFunction)oss_setfmt, METH_VARARGS },
Greg Ward8c6b6a92002-12-11 14:43:13 +0000798 { "getfmts", (PyCFunction)oss_getfmts, METH_VARARGS },
799 { "channels", (PyCFunction)oss_channels, METH_VARARGS },
800 { "speed", (PyCFunction)oss_speed, METH_VARARGS },
Greg Wardad4d9b92002-12-30 03:02:22 +0000801 { "sync", (PyCFunction)oss_sync, METH_VARARGS },
802 { "reset", (PyCFunction)oss_reset, METH_VARARGS },
803 { "post", (PyCFunction)oss_post, METH_VARARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000804
805 /* Convenience methods -- wrap a couple of ioctls together */
Greg Wardad4d9b92002-12-30 03:02:22 +0000806 { "setparameters", (PyCFunction)oss_setparameters, METH_VARARGS },
807 { "bufsize", (PyCFunction)oss_bufsize, METH_VARARGS },
808 { "obufcount", (PyCFunction)oss_obufcount, METH_VARARGS },
809 { "obuffree", (PyCFunction)oss_obuffree, METH_VARARGS },
Greg Ward8c6b6a92002-12-11 14:43:13 +0000810 { "getptr", (PyCFunction)oss_getptr, METH_VARARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000811
812 /* Aliases for backwards compatibility */
Greg Wardad4d9b92002-12-30 03:02:22 +0000813 { "flush", (PyCFunction)oss_sync, METH_VARARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000814
Greg Wardad4d9b92002-12-30 03:02:22 +0000815 { NULL, NULL} /* sentinel */
Greg Ward04613a92002-11-30 22:47:45 +0000816};
817
Greg Ward3d9994d2002-12-11 15:12:01 +0000818static PyMethodDef oss_mixer_methods[] = {
819 /* Regular file method - OSS mixers are ioctl-only interface */
Greg Wardad4d9b92002-12-30 03:02:22 +0000820 { "close", (PyCFunction)oss_mixer_close, METH_VARARGS },
821 { "fileno", (PyCFunction)oss_mixer_fileno, METH_VARARGS },
Greg Ward3d9994d2002-12-11 15:12:01 +0000822
823 /* Simple ioctl wrappers */
Greg Ward2d6f9a92002-12-31 02:54:43 +0000824 { "controls", (PyCFunction)oss_mixer_controls, METH_VARARGS },
825 { "stereocontrols", (PyCFunction)oss_mixer_stereocontrols, METH_VARARGS},
826 { "reccontrols", (PyCFunction)oss_mixer_reccontrols, METH_VARARGS},
Greg Wardad4d9b92002-12-30 03:02:22 +0000827 { "get", (PyCFunction)oss_mixer_get, METH_VARARGS },
828 { "set", (PyCFunction)oss_mixer_set, METH_VARARGS },
Greg Wardf05aa102002-12-30 23:19:32 +0000829 { "get_recsrc", (PyCFunction)oss_mixer_get_recsrc, METH_VARARGS },
830 { "set_recsrc", (PyCFunction)oss_mixer_set_recsrc, METH_VARARGS },
Greg Ward3d9994d2002-12-11 15:12:01 +0000831
Greg Wardad4d9b92002-12-30 03:02:22 +0000832 { NULL, NULL}
Greg Ward3d9994d2002-12-11 15:12:01 +0000833};
834
Greg Ward04613a92002-11-30 22:47:45 +0000835static PyObject *
Greg Ward58ae13c2002-12-31 03:07:21 +0000836oss_getattr(oss_audio_t *self, char *name)
Greg Ward04613a92002-11-30 22:47:45 +0000837{
Greg Ward58ae13c2002-12-31 03:07:21 +0000838 return Py_FindMethod(oss_methods, (PyObject *)self, name);
Greg Ward04613a92002-11-30 22:47:45 +0000839}
840
Greg Ward3d9994d2002-12-11 15:12:01 +0000841static PyObject *
Greg Ward58ae13c2002-12-31 03:07:21 +0000842oss_mixer_getattr(oss_mixer_t *self, char *name)
Greg Ward3d9994d2002-12-11 15:12:01 +0000843{
Greg Ward58ae13c2002-12-31 03:07:21 +0000844 return Py_FindMethod(oss_mixer_methods, (PyObject *)self, name);
Greg Ward3d9994d2002-12-11 15:12:01 +0000845}
846
Greg Ward499b73e2002-12-31 03:04:52 +0000847static PyTypeObject OSSAudioType = {
Greg Ward04613a92002-11-30 22:47:45 +0000848 PyObject_HEAD_INIT(&PyType_Type)
Greg Wardad4d9b92002-12-30 03:02:22 +0000849 0, /*ob_size*/
Greg Ward9a568eb2002-11-30 23:20:09 +0000850 "ossaudiodev.oss_audio_device", /*tp_name*/
Greg Ward499b73e2002-12-31 03:04:52 +0000851 sizeof(oss_audio_t), /*tp_size*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000852 0, /*tp_itemsize*/
Greg Ward04613a92002-11-30 22:47:45 +0000853 /* methods */
Greg Wardad4d9b92002-12-30 03:02:22 +0000854 (destructor)oss_dealloc, /*tp_dealloc*/
855 0, /*tp_print*/
856 (getattrfunc)oss_getattr, /*tp_getattr*/
857 0, /*tp_setattr*/
858 0, /*tp_compare*/
859 0, /*tp_repr*/
Greg Ward04613a92002-11-30 22:47:45 +0000860};
861
Greg Ward3d9994d2002-12-11 15:12:01 +0000862static PyTypeObject OSSMixerType = {
863 PyObject_HEAD_INIT(&PyType_Type)
Greg Wardad4d9b92002-12-30 03:02:22 +0000864 0, /*ob_size*/
Greg Ward3d9994d2002-12-11 15:12:01 +0000865 "ossaudiodev.oss_mixer_device", /*tp_name*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000866 sizeof(oss_mixer_t), /*tp_size*/
867 0, /*tp_itemsize*/
Greg Ward3d9994d2002-12-11 15:12:01 +0000868 /* methods */
869 (destructor)oss_mixer_dealloc, /*tp_dealloc*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000870 0, /*tp_print*/
Greg Ward3d9994d2002-12-11 15:12:01 +0000871 (getattrfunc)oss_mixer_getattr, /*tp_getattr*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000872 0, /*tp_setattr*/
873 0, /*tp_compare*/
874 0, /*tp_repr*/
Greg Ward3d9994d2002-12-11 15:12:01 +0000875};
876
877
Greg Ward04613a92002-11-30 22:47:45 +0000878static PyObject *
Greg Ward9a568eb2002-11-30 23:20:09 +0000879ossopen(PyObject *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000880{
Greg Ward8c6b6a92002-12-11 14:43:13 +0000881 return (PyObject *)newossobject(args);
Greg Ward04613a92002-11-30 22:47:45 +0000882}
883
Greg Ward3d9994d2002-12-11 15:12:01 +0000884static PyObject *
885ossopenmixer(PyObject *self, PyObject *args)
886{
887 return (PyObject *)newossmixerobject(args);
888}
889
Greg Ward9a568eb2002-11-30 23:20:09 +0000890static PyMethodDef ossaudiodev_methods[] = {
891 { "open", ossopen, METH_VARARGS },
Greg Ward3d9994d2002-12-11 15:12:01 +0000892 { "openmixer", ossopenmixer, METH_VARARGS },
Greg Ward04613a92002-11-30 22:47:45 +0000893 { 0, 0 },
894};
895
Greg Ward1e0f57d2002-11-30 23:05:26 +0000896
897#define _EXPORT_INT(mod, name) \
898 if (PyModule_AddIntConstant(mod, #name, (long) (name)) == -1) return;
899
Greg Ward744f0fd2002-12-31 03:23:59 +0000900
901static char *control_labels[] = SOUND_DEVICE_LABELS;
902static char *control_names[] = SOUND_DEVICE_NAMES;
903
904
905static int
906build_namelists (PyObject *module)
907{
908 PyObject *labels;
909 PyObject *names;
910 PyObject *s;
911 int num_controls;
912 int i;
913
914 num_controls = sizeof(control_labels) / sizeof(control_labels[0]);
915 assert(num_controls == sizeof(control_names) / sizeof(control_names[0]));
916
917 labels = PyList_New(num_controls);
918 names = PyList_New(num_controls);
919 for (i = 0; i < num_controls; i++) {
920 s = PyString_FromString(control_labels[i]);
921 if (s == NULL)
922 return -1;
923 PyList_SET_ITEM(labels, i, s);
924
925 s = PyString_FromString(control_names[i]);
926 if (s == NULL)
927 return -1;
928 PyList_SET_ITEM(names, i, s);
929 }
930
931 if (PyModule_AddObject(module, "control_labels", labels) == -1)
932 return -1;
933 if (PyModule_AddObject(module, "control_names", names) == -1)
934 return -1;
935
936 return 0;
937}
938
939
Greg Ward04613a92002-11-30 22:47:45 +0000940void
Greg Ward9a568eb2002-11-30 23:20:09 +0000941initossaudiodev(void)
Greg Ward04613a92002-11-30 22:47:45 +0000942{
943 PyObject *m;
944
Greg Ward9a568eb2002-11-30 23:20:09 +0000945 m = Py_InitModule("ossaudiodev", ossaudiodev_methods);
Greg Ward04613a92002-11-30 22:47:45 +0000946
Greg Ward97708bc2002-11-30 23:17:10 +0000947 OSSAudioError = PyErr_NewException("ossaudiodev.error", NULL, NULL);
948 if (OSSAudioError)
Greg Wardad4d9b92002-12-30 03:02:22 +0000949 PyModule_AddObject(m, "error", OSSAudioError);
Greg Ward04613a92002-11-30 22:47:45 +0000950
Greg Ward744f0fd2002-12-31 03:23:59 +0000951 /* Build 'control_labels' and 'control_names' lists and add them
952 to the module. */
953 if (build_namelists(m) == -1) /* XXX what to do here? */
954 return;
955
Greg Ward1e0f57d2002-11-30 23:05:26 +0000956 /* Expose the audio format numbers -- essential! */
957 _EXPORT_INT(m, AFMT_QUERY);
958 _EXPORT_INT(m, AFMT_MU_LAW);
959 _EXPORT_INT(m, AFMT_A_LAW);
960 _EXPORT_INT(m, AFMT_IMA_ADPCM);
961 _EXPORT_INT(m, AFMT_U8);
962 _EXPORT_INT(m, AFMT_S16_LE);
963 _EXPORT_INT(m, AFMT_S16_BE);
964 _EXPORT_INT(m, AFMT_S8);
965 _EXPORT_INT(m, AFMT_U16_LE);
966 _EXPORT_INT(m, AFMT_U16_BE);
967 _EXPORT_INT(m, AFMT_MPEG);
Andrew M. Kuchlingfe62bc92003-02-13 13:27:07 +0000968#ifdef AFMT_AC3
Greg Ward1e0f57d2002-11-30 23:05:26 +0000969 _EXPORT_INT(m, AFMT_AC3);
Andrew M. Kuchlingfe62bc92003-02-13 13:27:07 +0000970#endif
Neal Norwitzd156c2d2003-02-02 17:59:06 +0000971#ifdef AFMT_S16_NE
Greg Ward1e0f57d2002-11-30 23:05:26 +0000972 _EXPORT_INT(m, AFMT_S16_NE);
Neal Norwitzd156c2d2003-02-02 17:59:06 +0000973#endif
Greg Wardad4d9b92002-12-30 03:02:22 +0000974
Greg Ward37897c22002-12-30 02:43:36 +0000975 /* Expose the sound mixer device numbers. */
Greg Ward7b43c682002-12-30 02:29:28 +0000976 _EXPORT_INT(m, SOUND_MIXER_NRDEVICES);
Greg Ward3d9994d2002-12-11 15:12:01 +0000977 _EXPORT_INT(m, SOUND_MIXER_VOLUME);
978 _EXPORT_INT(m, SOUND_MIXER_BASS);
979 _EXPORT_INT(m, SOUND_MIXER_TREBLE);
980 _EXPORT_INT(m, SOUND_MIXER_SYNTH);
981 _EXPORT_INT(m, SOUND_MIXER_PCM);
982 _EXPORT_INT(m, SOUND_MIXER_SPEAKER);
983 _EXPORT_INT(m, SOUND_MIXER_LINE);
984 _EXPORT_INT(m, SOUND_MIXER_MIC);
985 _EXPORT_INT(m, SOUND_MIXER_CD);
986 _EXPORT_INT(m, SOUND_MIXER_IMIX);
987 _EXPORT_INT(m, SOUND_MIXER_ALTPCM);
988 _EXPORT_INT(m, SOUND_MIXER_RECLEV);
989 _EXPORT_INT(m, SOUND_MIXER_IGAIN);
990 _EXPORT_INT(m, SOUND_MIXER_OGAIN);
991 _EXPORT_INT(m, SOUND_MIXER_LINE1);
992 _EXPORT_INT(m, SOUND_MIXER_LINE2);
993 _EXPORT_INT(m, SOUND_MIXER_LINE3);
994 _EXPORT_INT(m, SOUND_MIXER_DIGITAL1);
995 _EXPORT_INT(m, SOUND_MIXER_DIGITAL2);
996 _EXPORT_INT(m, SOUND_MIXER_DIGITAL3);
997 _EXPORT_INT(m, SOUND_MIXER_PHONEIN);
998 _EXPORT_INT(m, SOUND_MIXER_PHONEOUT);
999 _EXPORT_INT(m, SOUND_MIXER_VIDEO);
1000 _EXPORT_INT(m, SOUND_MIXER_RADIO);
1001 _EXPORT_INT(m, SOUND_MIXER_MONITOR);
Greg Ward04613a92002-11-30 22:47:45 +00001002
Greg Ward1e0f57d2002-11-30 23:05:26 +00001003 /* Expose all the ioctl numbers for masochists who like to do this
1004 stuff directly. */
1005 _EXPORT_INT(m, SNDCTL_COPR_HALT);
1006 _EXPORT_INT(m, SNDCTL_COPR_LOAD);
1007 _EXPORT_INT(m, SNDCTL_COPR_RCODE);
1008 _EXPORT_INT(m, SNDCTL_COPR_RCVMSG);
1009 _EXPORT_INT(m, SNDCTL_COPR_RDATA);
1010 _EXPORT_INT(m, SNDCTL_COPR_RESET);
1011 _EXPORT_INT(m, SNDCTL_COPR_RUN);
1012 _EXPORT_INT(m, SNDCTL_COPR_SENDMSG);
1013 _EXPORT_INT(m, SNDCTL_COPR_WCODE);
1014 _EXPORT_INT(m, SNDCTL_COPR_WDATA);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001015#ifdef SNDCTL_DSP_BIND_CHANNEL
Greg Ward1e0f57d2002-11-30 23:05:26 +00001016 _EXPORT_INT(m, SNDCTL_DSP_BIND_CHANNEL);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001017#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001018 _EXPORT_INT(m, SNDCTL_DSP_CHANNELS);
1019 _EXPORT_INT(m, SNDCTL_DSP_GETBLKSIZE);
1020 _EXPORT_INT(m, SNDCTL_DSP_GETCAPS);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001021#ifdef SNDCTL_DSP_GETCHANNELMASK
Greg Ward1e0f57d2002-11-30 23:05:26 +00001022 _EXPORT_INT(m, SNDCTL_DSP_GETCHANNELMASK);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001023#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001024 _EXPORT_INT(m, SNDCTL_DSP_GETFMTS);
1025 _EXPORT_INT(m, SNDCTL_DSP_GETIPTR);
1026 _EXPORT_INT(m, SNDCTL_DSP_GETISPACE);
1027 _EXPORT_INT(m, SNDCTL_DSP_GETODELAY);
1028 _EXPORT_INT(m, SNDCTL_DSP_GETOPTR);
1029 _EXPORT_INT(m, SNDCTL_DSP_GETOSPACE);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001030#ifdef SNDCTL_DSP_GETSPDIF
Greg Ward1e0f57d2002-11-30 23:05:26 +00001031 _EXPORT_INT(m, SNDCTL_DSP_GETSPDIF);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001032#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001033 _EXPORT_INT(m, SNDCTL_DSP_GETTRIGGER);
1034 _EXPORT_INT(m, SNDCTL_DSP_MAPINBUF);
1035 _EXPORT_INT(m, SNDCTL_DSP_MAPOUTBUF);
1036 _EXPORT_INT(m, SNDCTL_DSP_NONBLOCK);
1037 _EXPORT_INT(m, SNDCTL_DSP_POST);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001038#ifdef SNDCTL_DSP_PROFILE
Greg Ward1e0f57d2002-11-30 23:05:26 +00001039 _EXPORT_INT(m, SNDCTL_DSP_PROFILE);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001040#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001041 _EXPORT_INT(m, SNDCTL_DSP_RESET);
1042 _EXPORT_INT(m, SNDCTL_DSP_SAMPLESIZE);
1043 _EXPORT_INT(m, SNDCTL_DSP_SETDUPLEX);
1044 _EXPORT_INT(m, SNDCTL_DSP_SETFMT);
1045 _EXPORT_INT(m, SNDCTL_DSP_SETFRAGMENT);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001046#ifdef SNDCTL_DSP_SETSPDIF
Greg Ward1e0f57d2002-11-30 23:05:26 +00001047 _EXPORT_INT(m, SNDCTL_DSP_SETSPDIF);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001048#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001049 _EXPORT_INT(m, SNDCTL_DSP_SETSYNCRO);
1050 _EXPORT_INT(m, SNDCTL_DSP_SETTRIGGER);
1051 _EXPORT_INT(m, SNDCTL_DSP_SPEED);
1052 _EXPORT_INT(m, SNDCTL_DSP_STEREO);
1053 _EXPORT_INT(m, SNDCTL_DSP_SUBDIVIDE);
1054 _EXPORT_INT(m, SNDCTL_DSP_SYNC);
1055 _EXPORT_INT(m, SNDCTL_FM_4OP_ENABLE);
1056 _EXPORT_INT(m, SNDCTL_FM_LOAD_INSTR);
1057 _EXPORT_INT(m, SNDCTL_MIDI_INFO);
1058 _EXPORT_INT(m, SNDCTL_MIDI_MPUCMD);
1059 _EXPORT_INT(m, SNDCTL_MIDI_MPUMODE);
1060 _EXPORT_INT(m, SNDCTL_MIDI_PRETIME);
1061 _EXPORT_INT(m, SNDCTL_SEQ_CTRLRATE);
1062 _EXPORT_INT(m, SNDCTL_SEQ_GETINCOUNT);
1063 _EXPORT_INT(m, SNDCTL_SEQ_GETOUTCOUNT);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001064#ifdef SNDCTL_SEQ_GETTIME
Greg Ward1e0f57d2002-11-30 23:05:26 +00001065 _EXPORT_INT(m, SNDCTL_SEQ_GETTIME);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001066#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001067 _EXPORT_INT(m, SNDCTL_SEQ_NRMIDIS);
1068 _EXPORT_INT(m, SNDCTL_SEQ_NRSYNTHS);
1069 _EXPORT_INT(m, SNDCTL_SEQ_OUTOFBAND);
1070 _EXPORT_INT(m, SNDCTL_SEQ_PANIC);
1071 _EXPORT_INT(m, SNDCTL_SEQ_PERCMODE);
1072 _EXPORT_INT(m, SNDCTL_SEQ_RESET);
1073 _EXPORT_INT(m, SNDCTL_SEQ_RESETSAMPLES);
1074 _EXPORT_INT(m, SNDCTL_SEQ_SYNC);
1075 _EXPORT_INT(m, SNDCTL_SEQ_TESTMIDI);
1076 _EXPORT_INT(m, SNDCTL_SEQ_THRESHOLD);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001077#ifdef SNDCTL_SYNTH_CONTROL
Greg Ward1e0f57d2002-11-30 23:05:26 +00001078 _EXPORT_INT(m, SNDCTL_SYNTH_CONTROL);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001079#endif
1080#ifdef SNDCTL_SYNTH_ID
Greg Ward1e0f57d2002-11-30 23:05:26 +00001081 _EXPORT_INT(m, SNDCTL_SYNTH_ID);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001082#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001083 _EXPORT_INT(m, SNDCTL_SYNTH_INFO);
1084 _EXPORT_INT(m, SNDCTL_SYNTH_MEMAVL);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001085#ifdef SNDCTL_SYNTH_REMOVESAMPLE
Greg Ward1e0f57d2002-11-30 23:05:26 +00001086 _EXPORT_INT(m, SNDCTL_SYNTH_REMOVESAMPLE);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001087#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001088 _EXPORT_INT(m, SNDCTL_TMR_CONTINUE);
1089 _EXPORT_INT(m, SNDCTL_TMR_METRONOME);
1090 _EXPORT_INT(m, SNDCTL_TMR_SELECT);
1091 _EXPORT_INT(m, SNDCTL_TMR_SOURCE);
1092 _EXPORT_INT(m, SNDCTL_TMR_START);
1093 _EXPORT_INT(m, SNDCTL_TMR_STOP);
1094 _EXPORT_INT(m, SNDCTL_TMR_TEMPO);
1095 _EXPORT_INT(m, SNDCTL_TMR_TIMEBASE);
Greg Ward04613a92002-11-30 22:47:45 +00001096}