blob: 817e20428c2f5dbf6c34546d9ef121d4157254cf [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.
Guido van Rossum0741f802003-06-02 14:15:34 +000012 *
Greg Ward04613a92002-11-30 22:47:45 +000013 * (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
Thomas Wouters89f507f2006-12-13 04:49:30 +000037#ifndef HAVE_STDINT_H
Greg Ward04613a92002-11-30 22:47:45 +000038typedef unsigned long uint32_t;
Thomas Wouters89f507f2006-12-13 04:49:30 +000039#endif
Greg Ward04613a92002-11-30 22:47:45 +000040
41#elif defined(__FreeBSD__)
Greg Ward04613a92002-11-30 22:47:45 +000042
Greg Ward0b6dfb82003-03-10 03:17:06 +000043# ifndef SNDCTL_DSP_CHANNELS
44# define SNDCTL_DSP_CHANNELS SOUND_PCM_WRITE_CHANNELS
45# endif
Greg Ward04613a92002-11-30 22:47:45 +000046
47#endif
48
49typedef struct {
Neal Norwitz3f046482006-01-07 21:19:49 +000050 PyObject_HEAD
Greg Ward50682d02005-03-07 01:41:11 +000051 char *devicename; /* name of the device file */
52 int fd; /* file descriptor */
53 int mode; /* file mode (O_RDONLY, etc.) */
54 int icount; /* input count */
55 int ocount; /* output count */
56 uint32_t afmts; /* audio formats supported by hardware */
Greg Ward499b73e2002-12-31 03:04:52 +000057} oss_audio_t;
Greg Ward04613a92002-11-30 22:47:45 +000058
Greg Ward3d9994d2002-12-11 15:12:01 +000059typedef struct {
Neal Norwitz3f046482006-01-07 21:19:49 +000060 PyObject_HEAD
Greg Wardad4d9b92002-12-30 03:02:22 +000061 int fd; /* The open mixer device */
Greg Ward3d9994d2002-12-11 15:12:01 +000062} oss_mixer_t;
63
Greg Ward04613a92002-11-30 22:47:45 +000064
Greg Ward499b73e2002-12-31 03:04:52 +000065static PyTypeObject OSSAudioType;
Greg Ward3d9994d2002-12-11 15:12:01 +000066static PyTypeObject OSSMixerType;
Greg Ward04613a92002-11-30 22:47:45 +000067
Greg Ward97708bc2002-11-30 23:17:10 +000068static PyObject *OSSAudioError;
Greg Ward04613a92002-11-30 22:47:45 +000069
Greg Wardda1cacb2002-12-31 03:02:23 +000070
71/* ----------------------------------------------------------------------
72 * DSP object initialization/deallocation
73 */
74
Greg Ward499b73e2002-12-31 03:04:52 +000075static oss_audio_t *
Greg Ward8c6b6a92002-12-11 14:43:13 +000076newossobject(PyObject *arg)
Greg Ward04613a92002-11-30 22:47:45 +000077{
Greg Ward58ae13c2002-12-31 03:07:21 +000078 oss_audio_t *self;
Greg Ward04613a92002-11-30 22:47:45 +000079 int fd, afmts, imode;
Greg Ward50682d02005-03-07 01:41:11 +000080 char *devicename = NULL;
Greg Ward04613a92002-11-30 22:47:45 +000081 char *mode = NULL;
82
Greg Ward9a568eb2002-11-30 23:20:09 +000083 /* Two ways to call open():
Greg Ward04613a92002-11-30 22:47:45 +000084 open(device, mode) (for consistency with builtin open())
85 open(mode) (for backwards compatibility)
86 because the *first* argument is optional, parsing args is
87 a wee bit tricky. */
Greg Ward50682d02005-03-07 01:41:11 +000088 if (!PyArg_ParseTuple(arg, "s|s:open", &devicename, &mode))
Greg Ward04613a92002-11-30 22:47:45 +000089 return NULL;
90 if (mode == NULL) { /* only one arg supplied */
Greg Ward50682d02005-03-07 01:41:11 +000091 mode = devicename;
92 devicename = NULL;
Greg Ward04613a92002-11-30 22:47:45 +000093 }
94
95 if (strcmp(mode, "r") == 0)
96 imode = O_RDONLY;
97 else if (strcmp(mode, "w") == 0)
98 imode = O_WRONLY;
Greg Ward1e0f57d2002-11-30 23:05:26 +000099 else if (strcmp(mode, "rw") == 0)
100 imode = O_RDWR;
Greg Ward04613a92002-11-30 22:47:45 +0000101 else {
Greg Ward97708bc2002-11-30 23:17:10 +0000102 PyErr_SetString(OSSAudioError, "mode must be 'r', 'w', or 'rw'");
Greg Ward04613a92002-11-30 22:47:45 +0000103 return NULL;
104 }
105
Greg Ward5c5c5772002-12-30 02:58:04 +0000106 /* Open the correct device: either the 'device' argument,
107 or the AUDIODEV environment variable, or "/dev/dsp". */
Greg Ward50682d02005-03-07 01:41:11 +0000108 if (devicename == NULL) { /* called with one arg */
109 devicename = getenv("AUDIODEV");
110 if (devicename == NULL) /* $AUDIODEV not set */
111 devicename = "/dev/dsp";
Greg Ward04613a92002-11-30 22:47:45 +0000112 }
113
Greg Ward5c49ef22003-03-11 16:53:13 +0000114 /* Open with O_NONBLOCK to avoid hanging on devices that only allow
115 one open at a time. This does *not* affect later I/O; OSS
116 provides a special ioctl() for non-blocking read/write, which is
117 exposed via oss_nonblock() below. */
Greg Ward50682d02005-03-07 01:41:11 +0000118 if ((fd = open(devicename, imode|O_NONBLOCK)) == -1) {
119 PyErr_SetFromErrnoWithFilename(PyExc_IOError, devicename);
Greg Ward04613a92002-11-30 22:47:45 +0000120 return NULL;
121 }
Greg Ward76ffb192003-04-04 01:47:42 +0000122
123 /* And (try to) put it back in blocking mode so we get the
124 expected write() semantics. */
125 if (fcntl(fd, F_SETFL, 0) == -1) {
126 close(fd);
Greg Ward50682d02005-03-07 01:41:11 +0000127 PyErr_SetFromErrnoWithFilename(PyExc_IOError, devicename);
Greg Ward76ffb192003-04-04 01:47:42 +0000128 return NULL;
129 }
130
Greg Ward04613a92002-11-30 22:47:45 +0000131 if (ioctl(fd, SNDCTL_DSP_GETFMTS, &afmts) == -1) {
Charles-François Natali5a4a1092011-09-29 19:46:37 +0200132 close(fd);
Greg Ward50682d02005-03-07 01:41:11 +0000133 PyErr_SetFromErrnoWithFilename(PyExc_IOError, devicename);
Greg Ward04613a92002-11-30 22:47:45 +0000134 return NULL;
135 }
136 /* Create and initialize the object */
Greg Ward58ae13c2002-12-31 03:07:21 +0000137 if ((self = PyObject_New(oss_audio_t, &OSSAudioType)) == NULL) {
Greg Ward04613a92002-11-30 22:47:45 +0000138 close(fd);
139 return NULL;
140 }
Greg Ward50682d02005-03-07 01:41:11 +0000141 self->devicename = devicename;
Greg Ward58ae13c2002-12-31 03:07:21 +0000142 self->fd = fd;
143 self->mode = imode;
144 self->icount = self->ocount = 0;
145 self->afmts = afmts;
146 return self;
Greg Ward04613a92002-11-30 22:47:45 +0000147}
148
149static void
Greg Ward58ae13c2002-12-31 03:07:21 +0000150oss_dealloc(oss_audio_t *self)
Greg Ward04613a92002-11-30 22:47:45 +0000151{
152 /* if already closed, don't reclose it */
Greg Ward58ae13c2002-12-31 03:07:21 +0000153 if (self->fd != -1)
154 close(self->fd);
155 PyObject_Del(self);
Greg Ward04613a92002-11-30 22:47:45 +0000156}
157
Greg Wardda1cacb2002-12-31 03:02:23 +0000158
159/* ----------------------------------------------------------------------
160 * Mixer object initialization/deallocation
161 */
162
Greg Ward3d9994d2002-12-11 15:12:01 +0000163static oss_mixer_t *
164newossmixerobject(PyObject *arg)
165{
Greg Ward50682d02005-03-07 01:41:11 +0000166 char *devicename = NULL;
Greg Ward0b6dfb82003-03-10 03:17:06 +0000167 int fd;
Greg Ward58ae13c2002-12-31 03:07:21 +0000168 oss_mixer_t *self;
Guido van Rossum0741f802003-06-02 14:15:34 +0000169
Greg Ward50682d02005-03-07 01:41:11 +0000170 if (!PyArg_ParseTuple(arg, "|s", &devicename)) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000171 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000172 }
Guido van Rossum0741f802003-06-02 14:15:34 +0000173
Greg Ward50682d02005-03-07 01:41:11 +0000174 if (devicename == NULL) {
175 devicename = getenv("MIXERDEV");
176 if (devicename == NULL) /* MIXERDEV not set */
177 devicename = "/dev/mixer";
Greg Ward3d9994d2002-12-11 15:12:01 +0000178 }
179
Greg Ward50682d02005-03-07 01:41:11 +0000180 if ((fd = open(devicename, O_RDWR)) == -1) {
181 PyErr_SetFromErrnoWithFilename(PyExc_IOError, devicename);
Greg Ward3d9994d2002-12-11 15:12:01 +0000182 return NULL;
183 }
Greg Ward0b6dfb82003-03-10 03:17:06 +0000184
Greg Ward58ae13c2002-12-31 03:07:21 +0000185 if ((self = PyObject_New(oss_mixer_t, &OSSMixerType)) == NULL) {
Greg Ward3d9994d2002-12-11 15:12:01 +0000186 close(fd);
187 return NULL;
188 }
Guido van Rossum0741f802003-06-02 14:15:34 +0000189
Greg Ward58ae13c2002-12-31 03:07:21 +0000190 self->fd = fd;
Guido van Rossum0741f802003-06-02 14:15:34 +0000191
Greg Ward58ae13c2002-12-31 03:07:21 +0000192 return self;
Greg Ward3d9994d2002-12-11 15:12:01 +0000193}
194
195static void
Greg Ward58ae13c2002-12-31 03:07:21 +0000196oss_mixer_dealloc(oss_mixer_t *self)
Greg Ward3d9994d2002-12-11 15:12:01 +0000197{
198 /* if already closed, don't reclose it */
Greg Ward58ae13c2002-12-31 03:07:21 +0000199 if (self->fd != -1)
200 close(self->fd);
201 PyObject_Del(self);
Greg Ward3d9994d2002-12-11 15:12:01 +0000202}
203
Greg Ward131bce02002-11-30 22:56:44 +0000204
205/* Methods to wrap the OSS ioctls. The calling convention is pretty
206 simple:
207 nonblock() -> ioctl(fd, SNDCTL_DSP_NONBLOCK)
208 fmt = setfmt(fmt) -> ioctl(fd, SNDCTL_DSP_SETFMT, &fmt)
209 etc.
210*/
211
212
Greg Wardd6769062003-05-29 21:53:06 +0000213/* ----------------------------------------------------------------------
214 * Helper functions
215 */
216
Charles-François Natalia5293082011-06-11 18:58:24 +0200217/* Check if a given file descriptor is valid (i.e. hasn't been closed).
218 * If true, return 1. Otherwise, raise ValueError and return 0.
219 */
220static int _is_fd_valid(int fd)
221{
222 /* the FD is set to -1 in oss_close()/oss_mixer_close() */
223 if (fd >= 0) {
224 return 1;
225 } else {
226 PyErr_SetString(PyExc_ValueError,
227 "Operation on closed OSS device.");
228 return 0;
229 }
230}
231
Greg Ward131bce02002-11-30 22:56:44 +0000232/* _do_ioctl_1() is a private helper function used for the OSS ioctls --
Terry Jan Reedy0158af32013-03-11 17:42:46 -0400233 SNDCTL_DSP_{SETFMT,CHANNELS,SPEED} -- that are called from C
Greg Ward131bce02002-11-30 22:56:44 +0000234 like this:
235 ioctl(fd, SNDCTL_DSP_cmd, &arg)
236
237 where arg is the value to set, and on return the driver sets arg to
238 the value that was actually set. Mapping this to Python is obvious:
239 arg = dsp.xxx(arg)
240*/
241static PyObject *
Greg Wardda9f8532002-12-11 14:49:59 +0000242_do_ioctl_1(int fd, PyObject *args, char *fname, int cmd)
Greg Ward131bce02002-11-30 22:56:44 +0000243{
Greg Wardda9f8532002-12-11 14:49:59 +0000244 char argfmt[33] = "i:";
Greg Ward131bce02002-11-30 22:56:44 +0000245 int arg;
246
Greg Wardda9f8532002-12-11 14:49:59 +0000247 assert(strlen(fname) <= 30);
Greg Ward131bce02002-11-30 22:56:44 +0000248 strcat(argfmt, fname);
249 if (!PyArg_ParseTuple(args, argfmt, &arg))
Greg Wardad4d9b92002-12-30 03:02:22 +0000250 return NULL;
Greg Ward131bce02002-11-30 22:56:44 +0000251
Greg Wardda9f8532002-12-11 14:49:59 +0000252 if (ioctl(fd, cmd, &arg) == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000253 return PyErr_SetFromErrno(PyExc_IOError);
Christian Heimes217cfd12007-12-02 14:31:20 +0000254 return PyLong_FromLong(arg);
Greg Ward131bce02002-11-30 22:56:44 +0000255}
256
Greg Wardda1cacb2002-12-31 03:02:23 +0000257
Greg Ward3d9994d2002-12-11 15:12:01 +0000258/* _do_ioctl_1_internal() is a wrapper for ioctls that take no inputs
259 but return an output -- ie. we need to pass a pointer to a local C
260 variable so the driver can write its output there, but from Python
261 all we see is the return value. For example,
262 SOUND_MIXER_READ_DEVMASK returns a bitmask of available mixer
263 devices, but does not use the value of the parameter passed-in in any
264 way.
265*/
Greg Ward3d9994d2002-12-11 15:12:01 +0000266static PyObject *
267_do_ioctl_1_internal(int fd, PyObject *args, char *fname, int cmd)
268{
269 char argfmt[32] = ":";
270 int arg = 0;
271
272 assert(strlen(fname) <= 30);
273 strcat(argfmt, fname);
274 if (!PyArg_ParseTuple(args, argfmt, &arg))
Greg Wardad4d9b92002-12-30 03:02:22 +0000275 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000276
277 if (ioctl(fd, cmd, &arg) == -1)
278 return PyErr_SetFromErrno(PyExc_IOError);
Christian Heimes217cfd12007-12-02 14:31:20 +0000279 return PyLong_FromLong(arg);
Greg Ward3d9994d2002-12-11 15:12:01 +0000280}
281
282
283
Greg Ward131bce02002-11-30 22:56:44 +0000284/* _do_ioctl_0() is a private helper for the no-argument ioctls:
285 SNDCTL_DSP_{SYNC,RESET,POST}. */
286static PyObject *
Greg Wardda9f8532002-12-11 14:49:59 +0000287_do_ioctl_0(int fd, PyObject *args, char *fname, int cmd)
Greg Ward131bce02002-11-30 22:56:44 +0000288{
Greg Wardda9f8532002-12-11 14:49:59 +0000289 char argfmt[32] = ":";
Greg Wardd0d592f2003-05-27 01:57:21 +0000290 int rv;
Greg Ward131bce02002-11-30 22:56:44 +0000291
Greg Wardda9f8532002-12-11 14:49:59 +0000292 assert(strlen(fname) <= 30);
Greg Ward131bce02002-11-30 22:56:44 +0000293 strcat(argfmt, fname);
294 if (!PyArg_ParseTuple(args, argfmt))
Greg Wardad4d9b92002-12-30 03:02:22 +0000295 return NULL;
Greg Ward131bce02002-11-30 22:56:44 +0000296
Greg Wardd0d592f2003-05-27 01:57:21 +0000297 /* According to hannu@opensound.com, all three of the ioctls that
298 use this function can block, so release the GIL. This is
299 especially important for SYNC, which can block for several
300 seconds. */
301 Py_BEGIN_ALLOW_THREADS
302 rv = ioctl(fd, cmd, 0);
303 Py_END_ALLOW_THREADS
304
305 if (rv == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000306 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000307 Py_INCREF(Py_None);
308 return Py_None;
309}
310
311
Greg Wardda1cacb2002-12-31 03:02:23 +0000312/* ----------------------------------------------------------------------
Greg Ward499b73e2002-12-31 03:04:52 +0000313 * Methods of DSP objects (OSSAudioType)
Greg Wardda1cacb2002-12-31 03:02:23 +0000314 */
315
Greg Ward131bce02002-11-30 22:56:44 +0000316static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000317oss_nonblock(oss_audio_t *self, PyObject *unused)
Greg Ward131bce02002-11-30 22:56:44 +0000318{
Charles-François Natalia5293082011-06-11 18:58:24 +0200319 if (!_is_fd_valid(self->fd))
320 return NULL;
321
Greg Ward131bce02002-11-30 22:56:44 +0000322 /* Hmmm: it doesn't appear to be possible to return to blocking
323 mode once we're in non-blocking mode! */
Greg Ward7b43c682002-12-30 02:29:28 +0000324 if (ioctl(self->fd, SNDCTL_DSP_NONBLOCK, NULL) == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000325 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000326 Py_INCREF(Py_None);
327 return Py_None;
328}
329
330static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000331oss_setfmt(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000332{
Charles-François Natalia5293082011-06-11 18:58:24 +0200333 if (!_is_fd_valid(self->fd))
334 return NULL;
335
Greg Ward7b43c682002-12-30 02:29:28 +0000336 return _do_ioctl_1(self->fd, args, "setfmt", SNDCTL_DSP_SETFMT);
Greg Ward131bce02002-11-30 22:56:44 +0000337}
338
339static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000340oss_getfmts(oss_audio_t *self, PyObject *unused)
Greg Ward131bce02002-11-30 22:56:44 +0000341{
342 int mask;
Charles-François Natalia5293082011-06-11 18:58:24 +0200343
344 if (!_is_fd_valid(self->fd))
345 return NULL;
346
Greg Ward7b43c682002-12-30 02:29:28 +0000347 if (ioctl(self->fd, SNDCTL_DSP_GETFMTS, &mask) == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000348 return PyErr_SetFromErrno(PyExc_IOError);
Christian Heimes217cfd12007-12-02 14:31:20 +0000349 return PyLong_FromLong(mask);
Greg Ward131bce02002-11-30 22:56:44 +0000350}
351
352static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000353oss_channels(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000354{
Charles-François Natalia5293082011-06-11 18:58:24 +0200355 if (!_is_fd_valid(self->fd))
356 return NULL;
357
Greg Ward7b43c682002-12-30 02:29:28 +0000358 return _do_ioctl_1(self->fd, args, "channels", SNDCTL_DSP_CHANNELS);
Greg Ward131bce02002-11-30 22:56:44 +0000359}
360
361static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000362oss_speed(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000363{
Charles-François Natalia5293082011-06-11 18:58:24 +0200364 if (!_is_fd_valid(self->fd))
365 return NULL;
366
Greg Ward7b43c682002-12-30 02:29:28 +0000367 return _do_ioctl_1(self->fd, args, "speed", SNDCTL_DSP_SPEED);
Greg Ward131bce02002-11-30 22:56:44 +0000368}
369
370static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000371oss_sync(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000372{
Charles-François Natalia5293082011-06-11 18:58:24 +0200373 if (!_is_fd_valid(self->fd))
374 return NULL;
375
Greg Wardd0d592f2003-05-27 01:57:21 +0000376 return _do_ioctl_0(self->fd, args, "sync", SNDCTL_DSP_SYNC);
Greg Ward131bce02002-11-30 22:56:44 +0000377}
Guido van Rossum0741f802003-06-02 14:15:34 +0000378
Greg Ward131bce02002-11-30 22:56:44 +0000379static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000380oss_reset(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000381{
Charles-François Natalia5293082011-06-11 18:58:24 +0200382 if (!_is_fd_valid(self->fd))
383 return NULL;
384
Greg Ward7b43c682002-12-30 02:29:28 +0000385 return _do_ioctl_0(self->fd, args, "reset", SNDCTL_DSP_RESET);
Greg Ward131bce02002-11-30 22:56:44 +0000386}
Guido van Rossum0741f802003-06-02 14:15:34 +0000387
Greg Ward131bce02002-11-30 22:56:44 +0000388static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000389oss_post(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000390{
Charles-François Natalia5293082011-06-11 18:58:24 +0200391 if (!_is_fd_valid(self->fd))
392 return NULL;
393
Greg Ward7b43c682002-12-30 02:29:28 +0000394 return _do_ioctl_0(self->fd, args, "post", SNDCTL_DSP_POST);
Greg Ward131bce02002-11-30 22:56:44 +0000395}
396
397
398/* Regular file methods: read(), write(), close(), etc. as well
399 as one convenience method, writeall(). */
400
Greg Ward04613a92002-11-30 22:47:45 +0000401static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000402oss_read(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000403{
404 int size, count;
405 char *cp;
406 PyObject *rv;
Guido van Rossum0741f802003-06-02 14:15:34 +0000407
Charles-François Natalia5293082011-06-11 18:58:24 +0200408 if (!_is_fd_valid(self->fd))
409 return NULL;
410
Greg Ward04613a92002-11-30 22:47:45 +0000411 if (!PyArg_ParseTuple(args, "i:read", &size))
412 return NULL;
Gregory P. Smith0a608fd2008-09-06 21:34:51 +0000413 rv = PyBytes_FromStringAndSize(NULL, size);
Greg Ward04613a92002-11-30 22:47:45 +0000414 if (rv == NULL)
415 return NULL;
Gregory P. Smith0a608fd2008-09-06 21:34:51 +0000416 cp = PyBytes_AS_STRING(rv);
Greg Ward64927852003-05-23 01:50:37 +0000417
418 Py_BEGIN_ALLOW_THREADS
419 count = read(self->fd, cp, size);
420 Py_END_ALLOW_THREADS
421
422 if (count < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000423 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000424 Py_DECREF(rv);
425 return NULL;
426 }
Greg Ward7b43c682002-12-30 02:29:28 +0000427 self->icount += count;
Gregory P. Smith0a608fd2008-09-06 21:34:51 +0000428 _PyBytes_Resize(&rv, count);
Greg Ward04613a92002-11-30 22:47:45 +0000429 return rv;
430}
431
432static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000433oss_write(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000434{
435 char *cp;
436 int rv, size;
Greg Ward131bce02002-11-30 22:56:44 +0000437
Charles-François Natalia5293082011-06-11 18:58:24 +0200438 if (!_is_fd_valid(self->fd))
439 return NULL;
440
Neal Norwitz8abd2e62007-08-26 04:57:08 +0000441 if (!PyArg_ParseTuple(args, "y#:write", &cp, &size)) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000442 return NULL;
Greg Ward131bce02002-11-30 22:56:44 +0000443 }
Greg Ward64927852003-05-23 01:50:37 +0000444
445 Py_BEGIN_ALLOW_THREADS
446 rv = write(self->fd, cp, size);
447 Py_END_ALLOW_THREADS
448
449 if (rv == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000450 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000451 } else {
Greg Ward7b43c682002-12-30 02:29:28 +0000452 self->ocount += rv;
Greg Ward131bce02002-11-30 22:56:44 +0000453 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000454 return PyLong_FromLong(rv);
Greg Ward131bce02002-11-30 22:56:44 +0000455}
456
457static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000458oss_writeall(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000459{
460 char *cp;
461 int rv, size;
462 fd_set write_set_fds;
463 int select_rv;
Guido van Rossum0741f802003-06-02 14:15:34 +0000464
Greg Ward131bce02002-11-30 22:56:44 +0000465 /* NB. writeall() is only useful in non-blocking mode: according to
466 Guenter Geiger <geiger@xdv.org> on the linux-audio-dev list
467 (http://eca.cx/lad/2002/11/0380.html), OSS guarantees that
468 write() in blocking mode consumes the whole buffer. In blocking
469 mode, the behaviour of write() and writeall() from Python is
470 indistinguishable. */
471
Charles-François Natalia5293082011-06-11 18:58:24 +0200472 if (!_is_fd_valid(self->fd))
473 return NULL;
474
Neal Norwitz8abd2e62007-08-26 04:57:08 +0000475 if (!PyArg_ParseTuple(args, "y#:write", &cp, &size))
Greg Ward131bce02002-11-30 22:56:44 +0000476 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000477
Charles-François Nataliaa26b272011-08-28 17:51:43 +0200478 if (!_PyIsSelectable_fd(self->fd)) {
479 PyErr_SetString(PyExc_ValueError,
480 "file descriptor out of range for select");
481 return NULL;
482 }
Greg Ward04613a92002-11-30 22:47:45 +0000483 /* use select to wait for audio device to be available */
484 FD_ZERO(&write_set_fds);
Greg Ward7b43c682002-12-30 02:29:28 +0000485 FD_SET(self->fd, &write_set_fds);
Greg Ward04613a92002-11-30 22:47:45 +0000486
487 while (size > 0) {
Greg Ward64927852003-05-23 01:50:37 +0000488 Py_BEGIN_ALLOW_THREADS
Greg Ward7b43c682002-12-30 02:29:28 +0000489 select_rv = select(self->fd+1, NULL, &write_set_fds, NULL, NULL);
Greg Ward64927852003-05-23 01:50:37 +0000490 Py_END_ALLOW_THREADS
Greg Ward131bce02002-11-30 22:56:44 +0000491 assert(select_rv != 0); /* no timeout, can't expire */
492 if (select_rv == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000493 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000494
Greg Ward64927852003-05-23 01:50:37 +0000495 Py_BEGIN_ALLOW_THREADS
Greg Ward7b43c682002-12-30 02:29:28 +0000496 rv = write(self->fd, cp, size);
Greg Ward64927852003-05-23 01:50:37 +0000497 Py_END_ALLOW_THREADS
Greg Ward131bce02002-11-30 22:56:44 +0000498 if (rv == -1) {
499 if (errno == EAGAIN) { /* buffer is full, try again */
500 errno = 0;
501 continue;
502 } else /* it's a real error */
Greg Ward97708bc2002-11-30 23:17:10 +0000503 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000504 } else { /* wrote rv bytes */
Greg Ward7b43c682002-12-30 02:29:28 +0000505 self->ocount += rv;
Greg Ward131bce02002-11-30 22:56:44 +0000506 size -= rv;
507 cp += rv;
508 }
Greg Ward04613a92002-11-30 22:47:45 +0000509 }
510 Py_INCREF(Py_None);
511 return Py_None;
512}
513
514static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000515oss_close(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000516{
Greg Ward7b43c682002-12-30 02:29:28 +0000517 if (self->fd >= 0) {
Greg Wardb8043902003-05-26 22:47:30 +0000518 Py_BEGIN_ALLOW_THREADS
Greg Ward7b43c682002-12-30 02:29:28 +0000519 close(self->fd);
Greg Wardb8043902003-05-26 22:47:30 +0000520 Py_END_ALLOW_THREADS
Greg Ward7b43c682002-12-30 02:29:28 +0000521 self->fd = -1;
Greg Ward04613a92002-11-30 22:47:45 +0000522 }
523 Py_INCREF(Py_None);
524 return Py_None;
525}
526
527static PyObject *
Georg Brandl770a2be2010-10-24 20:47:32 +0000528oss_self(PyObject *self, PyObject *unused)
Georg Brandl1e908af2010-10-23 17:31:52 +0000529{
530 Py_INCREF(self);
531 return self;
532}
533
Victor Stinner63941882011-09-29 00:42:28 +0200534static PyObject *
Georg Brandl1e908af2010-10-23 17:31:52 +0000535oss_exit(PyObject *self, PyObject *unused)
536{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200537 _Py_IDENTIFIER(close);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200538
539 PyObject *ret = _PyObject_CallMethodId(self, &PyId_close, NULL);
Georg Brandl1e908af2010-10-23 17:31:52 +0000540 if (!ret)
541 return NULL;
542 Py_DECREF(ret);
543 Py_RETURN_NONE;
544}
545
546static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000547oss_fileno(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000548{
Charles-François Natalia5293082011-06-11 18:58:24 +0200549 if (!_is_fd_valid(self->fd))
550 return NULL;
551
Christian Heimes217cfd12007-12-02 14:31:20 +0000552 return PyLong_FromLong(self->fd);
Greg Ward04613a92002-11-30 22:47:45 +0000553}
554
Greg Ward131bce02002-11-30 22:56:44 +0000555
556/* Convenience methods: these generally wrap a couple of ioctls into one
557 common task. */
558
Greg Ward04613a92002-11-30 22:47:45 +0000559static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000560oss_setparameters(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000561{
Greg Wardd6769062003-05-29 21:53:06 +0000562 int wanted_fmt, wanted_channels, wanted_rate, strict=0;
563 int fmt, channels, rate;
Greg Ward04613a92002-11-30 22:47:45 +0000564
Charles-François Natalia5293082011-06-11 18:58:24 +0200565 if (!_is_fd_valid(self->fd))
566 return NULL;
567
Greg Wardd6769062003-05-29 21:53:06 +0000568 if (!PyArg_ParseTuple(args, "iii|i:setparameters",
569 &wanted_fmt, &wanted_channels, &wanted_rate,
570 &strict))
Greg Ward04613a92002-11-30 22:47:45 +0000571 return NULL;
Greg Wardd6769062003-05-29 21:53:06 +0000572
573 fmt = wanted_fmt;
574 if (ioctl(self->fd, SNDCTL_DSP_SETFMT, &fmt) == -1) {
575 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000576 }
Greg Wardd6769062003-05-29 21:53:06 +0000577 if (strict && fmt != wanted_fmt) {
578 return PyErr_Format
579 (OSSAudioError,
580 "unable to set requested format (wanted %d, got %d)",
581 wanted_fmt, fmt);
Greg Ward04613a92002-11-30 22:47:45 +0000582 }
583
Greg Wardd6769062003-05-29 21:53:06 +0000584 channels = wanted_channels;
585 if (ioctl(self->fd, SNDCTL_DSP_CHANNELS, &channels) == -1) {
586 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000587 }
Greg Wardd6769062003-05-29 21:53:06 +0000588 if (strict && channels != wanted_channels) {
589 return PyErr_Format
590 (OSSAudioError,
591 "unable to set requested channels (wanted %d, got %d)",
592 wanted_channels, channels);
Greg Ward04613a92002-11-30 22:47:45 +0000593 }
594
Greg Wardd6769062003-05-29 21:53:06 +0000595 rate = wanted_rate;
Greg Ward7b43c682002-12-30 02:29:28 +0000596 if (ioctl(self->fd, SNDCTL_DSP_SPEED, &rate) == -1) {
Greg Wardd6769062003-05-29 21:53:06 +0000597 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000598 }
Greg Wardd6769062003-05-29 21:53:06 +0000599 if (strict && rate != wanted_rate) {
600 return PyErr_Format
601 (OSSAudioError,
602 "unable to set requested rate (wanted %d, got %d)",
603 wanted_rate, rate);
604 }
Guido van Rossum0741f802003-06-02 14:15:34 +0000605
Greg Wardd6769062003-05-29 21:53:06 +0000606 /* Construct the return value: a (fmt, channels, rate) tuple that
607 tells what the audio hardware was actually set to. */
Serhiy Storchaka48d761e2013-12-17 15:11:24 +0200608 return Py_BuildValue("(iii)", fmt, channels, rate);
Greg Ward04613a92002-11-30 22:47:45 +0000609}
610
611static int
Greg Ward499b73e2002-12-31 03:04:52 +0000612_ssize(oss_audio_t *self, int *nchannels, int *ssize)
Greg Ward04613a92002-11-30 22:47:45 +0000613{
614 int fmt;
615
616 fmt = 0;
Guido van Rossum0741f802003-06-02 14:15:34 +0000617 if (ioctl(self->fd, SNDCTL_DSP_SETFMT, &fmt) < 0)
Greg Ward04613a92002-11-30 22:47:45 +0000618 return -errno;
619
620 switch (fmt) {
621 case AFMT_MU_LAW:
622 case AFMT_A_LAW:
623 case AFMT_U8:
624 case AFMT_S8:
Greg Ward38c92662003-05-29 21:55:41 +0000625 *ssize = 1; /* 8 bit formats: 1 byte */
Greg Ward04613a92002-11-30 22:47:45 +0000626 break;
627 case AFMT_S16_LE:
628 case AFMT_S16_BE:
629 case AFMT_U16_LE:
630 case AFMT_U16_BE:
Greg Ward84f8ecd2003-05-29 23:44:44 +0000631 *ssize = 2; /* 16 bit formats: 2 byte */
Greg Ward04613a92002-11-30 22:47:45 +0000632 break;
633 case AFMT_MPEG:
634 case AFMT_IMA_ADPCM:
635 default:
636 return -EOPNOTSUPP;
637 }
Greg Ward7b43c682002-12-30 02:29:28 +0000638 if (ioctl(self->fd, SNDCTL_DSP_CHANNELS, nchannels) < 0)
Greg Ward04613a92002-11-30 22:47:45 +0000639 return -errno;
640 return 0;
641}
642
643
Guido van Rossum0741f802003-06-02 14:15:34 +0000644/* bufsize returns the size of the hardware audio buffer in number
Greg Ward04613a92002-11-30 22:47:45 +0000645 of samples */
646static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000647oss_bufsize(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000648{
649 audio_buf_info ai;
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000650 int nchannels=0, ssize=0;
Greg Ward04613a92002-11-30 22:47:45 +0000651
Charles-François Natalia5293082011-06-11 18:58:24 +0200652 if (!_is_fd_valid(self->fd))
653 return NULL;
654
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000655 if (_ssize(self, &nchannels, &ssize) < 0 || !nchannels || !ssize) {
Greg Ward97708bc2002-11-30 23:17:10 +0000656 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000657 return NULL;
658 }
Greg Ward7b43c682002-12-30 02:29:28 +0000659 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000660 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000661 return NULL;
662 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000663 return PyLong_FromLong((ai.fragstotal * ai.fragsize) / (nchannels * ssize));
Greg Ward04613a92002-11-30 22:47:45 +0000664}
665
Guido van Rossum0741f802003-06-02 14:15:34 +0000666/* obufcount returns the number of samples that are available in the
Greg Ward04613a92002-11-30 22:47:45 +0000667 hardware for playing */
668static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000669oss_obufcount(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000670{
671 audio_buf_info ai;
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000672 int nchannels=0, ssize=0;
Greg Ward04613a92002-11-30 22:47:45 +0000673
Charles-François Natalia5293082011-06-11 18:58:24 +0200674 if (!_is_fd_valid(self->fd))
675 return NULL;
676
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000677 if (_ssize(self, &nchannels, &ssize) < 0 || !nchannels || !ssize) {
Greg Ward97708bc2002-11-30 23:17:10 +0000678 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000679 return NULL;
680 }
Greg Ward7b43c682002-12-30 02:29:28 +0000681 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000682 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000683 return NULL;
684 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000685 return PyLong_FromLong((ai.fragstotal * ai.fragsize - ai.bytes) /
Greg Ward04613a92002-11-30 22:47:45 +0000686 (ssize * nchannels));
687}
688
689/* obufcount returns the number of samples that can be played without
690 blocking */
691static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000692oss_obuffree(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000693{
694 audio_buf_info ai;
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000695 int nchannels=0, ssize=0;
Greg Ward04613a92002-11-30 22:47:45 +0000696
Charles-François Natalia5293082011-06-11 18:58:24 +0200697 if (!_is_fd_valid(self->fd))
698 return NULL;
699
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000700 if (_ssize(self, &nchannels, &ssize) < 0 || !nchannels || !ssize) {
Greg Ward97708bc2002-11-30 23:17:10 +0000701 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000702 return NULL;
703 }
Greg Ward7b43c682002-12-30 02:29:28 +0000704 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000705 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000706 return NULL;
707 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000708 return PyLong_FromLong(ai.bytes / (ssize * nchannels));
Greg Ward04613a92002-11-30 22:47:45 +0000709}
710
Greg Ward04613a92002-11-30 22:47:45 +0000711static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000712oss_getptr(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000713{
714 count_info info;
715 int req;
716
Charles-François Natalia5293082011-06-11 18:58:24 +0200717 if (!_is_fd_valid(self->fd))
718 return NULL;
719
Greg Ward7b43c682002-12-30 02:29:28 +0000720 if (self->mode == O_RDONLY)
Greg Wardad4d9b92002-12-30 03:02:22 +0000721 req = SNDCTL_DSP_GETIPTR;
Greg Ward04613a92002-11-30 22:47:45 +0000722 else
Greg Wardad4d9b92002-12-30 03:02:22 +0000723 req = SNDCTL_DSP_GETOPTR;
Greg Ward7b43c682002-12-30 02:29:28 +0000724 if (ioctl(self->fd, req, &info) == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000725 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000726 return NULL;
727 }
728 return Py_BuildValue("iii", info.bytes, info.blocks, info.ptr);
729}
730
Greg Wardda1cacb2002-12-31 03:02:23 +0000731
732/* ----------------------------------------------------------------------
733 * Methods of mixer objects (OSSMixerType)
734 */
735
Greg Ward3d9994d2002-12-11 15:12:01 +0000736static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000737oss_mixer_close(oss_mixer_t *self, PyObject *unused)
Greg Ward3d9994d2002-12-11 15:12:01 +0000738{
Greg Ward7b43c682002-12-30 02:29:28 +0000739 if (self->fd >= 0) {
740 close(self->fd);
741 self->fd = -1;
Greg Ward3d9994d2002-12-11 15:12:01 +0000742 }
743 Py_INCREF(Py_None);
744 return Py_None;
745}
746
747static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000748oss_mixer_fileno(oss_mixer_t *self, PyObject *unused)
Greg Ward3d9994d2002-12-11 15:12:01 +0000749{
Charles-François Natalia5293082011-06-11 18:58:24 +0200750 if (!_is_fd_valid(self->fd))
751 return NULL;
752
Christian Heimes217cfd12007-12-02 14:31:20 +0000753 return PyLong_FromLong(self->fd);
Greg Ward3d9994d2002-12-11 15:12:01 +0000754}
755
756/* Simple mixer interface methods */
757
758static PyObject *
Greg Ward2d6f9a92002-12-31 02:54:43 +0000759oss_mixer_controls(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000760{
Charles-François Natalia5293082011-06-11 18:58:24 +0200761 if (!_is_fd_valid(self->fd))
762 return NULL;
763
Greg Ward2d6f9a92002-12-31 02:54:43 +0000764 return _do_ioctl_1_internal(self->fd, args, "controls",
Greg Ward3d9994d2002-12-11 15:12:01 +0000765 SOUND_MIXER_READ_DEVMASK);
766}
767
768static PyObject *
Greg Ward2d6f9a92002-12-31 02:54:43 +0000769oss_mixer_stereocontrols(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000770{
Charles-François Natalia5293082011-06-11 18:58:24 +0200771 if (!_is_fd_valid(self->fd))
772 return NULL;
773
Greg Ward2d6f9a92002-12-31 02:54:43 +0000774 return _do_ioctl_1_internal(self->fd, args, "stereocontrols",
Greg Ward3d9994d2002-12-11 15:12:01 +0000775 SOUND_MIXER_READ_STEREODEVS);
776}
777
778static PyObject *
Greg Ward2d6f9a92002-12-31 02:54:43 +0000779oss_mixer_reccontrols(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000780{
Charles-François Natalia5293082011-06-11 18:58:24 +0200781 if (!_is_fd_valid(self->fd))
782 return NULL;
783
Greg Ward2d6f9a92002-12-31 02:54:43 +0000784 return _do_ioctl_1_internal(self->fd, args, "reccontrols",
Greg Wardb69bb3d2002-12-12 17:35:45 +0000785 SOUND_MIXER_READ_RECMASK);
Greg Ward3d9994d2002-12-11 15:12:01 +0000786}
787
788static PyObject *
Greg Warde7037662002-12-30 03:01:48 +0000789oss_mixer_get(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000790{
791 int channel, volume;
Guido van Rossum0741f802003-06-02 14:15:34 +0000792
Charles-François Natalia5293082011-06-11 18:58:24 +0200793 if (!_is_fd_valid(self->fd))
794 return NULL;
795
Greg Ward3d9994d2002-12-11 15:12:01 +0000796 /* Can't use _do_ioctl_1 because of encoded arg thingy. */
Greg Warde7037662002-12-30 03:01:48 +0000797 if (!PyArg_ParseTuple(args, "i:get", &channel))
Greg Wardad4d9b92002-12-30 03:02:22 +0000798 return NULL;
Guido van Rossum0741f802003-06-02 14:15:34 +0000799
Greg Ward3d9994d2002-12-11 15:12:01 +0000800 if (channel < 0 || channel > SOUND_MIXER_NRDEVICES) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000801 PyErr_SetString(OSSAudioError, "Invalid mixer channel specified.");
802 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000803 }
Guido van Rossum0741f802003-06-02 14:15:34 +0000804
Greg Warde7037662002-12-30 03:01:48 +0000805 if (ioctl(self->fd, MIXER_READ(channel), &volume) == -1)
Greg Wardad4d9b92002-12-30 03:02:22 +0000806 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum0741f802003-06-02 14:15:34 +0000807
Greg Warde7037662002-12-30 03:01:48 +0000808 return Py_BuildValue("(ii)", volume & 0xff, (volume & 0xff00) >> 8);
Greg Ward3d9994d2002-12-11 15:12:01 +0000809}
810
811static PyObject *
Greg Warde7037662002-12-30 03:01:48 +0000812oss_mixer_set(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000813{
814 int channel, volume, leftVol, rightVol;
Guido van Rossum0741f802003-06-02 14:15:34 +0000815
Charles-François Natalia5293082011-06-11 18:58:24 +0200816 if (!_is_fd_valid(self->fd))
817 return NULL;
818
Greg Ward3d9994d2002-12-11 15:12:01 +0000819 /* Can't use _do_ioctl_1 because of encoded arg thingy. */
Greg Warde7037662002-12-30 03:01:48 +0000820 if (!PyArg_ParseTuple(args, "i(ii):set", &channel, &leftVol, &rightVol))
Greg Wardad4d9b92002-12-30 03:02:22 +0000821 return NULL;
Guido van Rossum0741f802003-06-02 14:15:34 +0000822
Greg Ward3d9994d2002-12-11 15:12:01 +0000823 if (channel < 0 || channel > SOUND_MIXER_NRDEVICES) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000824 PyErr_SetString(OSSAudioError, "Invalid mixer channel specified.");
825 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000826 }
Guido van Rossum0741f802003-06-02 14:15:34 +0000827
Greg Ward3d9994d2002-12-11 15:12:01 +0000828 if (leftVol < 0 || rightVol < 0 || leftVol > 100 || rightVol > 100) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000829 PyErr_SetString(OSSAudioError, "Volumes must be between 0 and 100.");
830 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000831 }
832
833 volume = (rightVol << 8) | leftVol;
Guido van Rossum0741f802003-06-02 14:15:34 +0000834
Greg Warde7037662002-12-30 03:01:48 +0000835 if (ioctl(self->fd, MIXER_WRITE(channel), &volume) == -1)
Greg Wardad4d9b92002-12-30 03:02:22 +0000836 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum0741f802003-06-02 14:15:34 +0000837
Greg Warde7037662002-12-30 03:01:48 +0000838 return Py_BuildValue("(ii)", volume & 0xff, (volume & 0xff00) >> 8);
Greg Ward3d9994d2002-12-11 15:12:01 +0000839}
840
841static PyObject *
Greg Wardf05aa102002-12-30 23:19:32 +0000842oss_mixer_get_recsrc(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000843{
Charles-François Natalia5293082011-06-11 18:58:24 +0200844 if (!_is_fd_valid(self->fd))
845 return NULL;
846
Greg Wardf05aa102002-12-30 23:19:32 +0000847 return _do_ioctl_1_internal(self->fd, args, "get_recsrc",
Greg Ward3d9994d2002-12-11 15:12:01 +0000848 SOUND_MIXER_READ_RECSRC);
849}
850
851static PyObject *
Greg Wardf05aa102002-12-30 23:19:32 +0000852oss_mixer_set_recsrc(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000853{
Charles-François Natalia5293082011-06-11 18:58:24 +0200854 if (!_is_fd_valid(self->fd))
855 return NULL;
856
Greg Wardf05aa102002-12-30 23:19:32 +0000857 return _do_ioctl_1(self->fd, args, "set_recsrc",
Greg Ward3d9994d2002-12-11 15:12:01 +0000858 SOUND_MIXER_WRITE_RECSRC);
859}
860
861
Greg Wardda1cacb2002-12-31 03:02:23 +0000862/* ----------------------------------------------------------------------
863 * Method tables and other bureaucracy
864 */
865
Greg Ward8c6b6a92002-12-11 14:43:13 +0000866static PyMethodDef oss_methods[] = {
Greg Ward131bce02002-11-30 22:56:44 +0000867 /* Regular file methods */
Greg Wardad4d9b92002-12-30 03:02:22 +0000868 { "read", (PyCFunction)oss_read, METH_VARARGS },
869 { "write", (PyCFunction)oss_write, METH_VARARGS },
870 { "writeall", (PyCFunction)oss_writeall, METH_VARARGS },
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000871 { "close", (PyCFunction)oss_close, METH_NOARGS },
872 { "fileno", (PyCFunction)oss_fileno, METH_NOARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000873
874 /* Simple ioctl wrappers */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000875 { "nonblock", (PyCFunction)oss_nonblock, METH_NOARGS },
Greg Wardad4d9b92002-12-30 03:02:22 +0000876 { "setfmt", (PyCFunction)oss_setfmt, METH_VARARGS },
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000877 { "getfmts", (PyCFunction)oss_getfmts, METH_NOARGS },
Greg Ward8c6b6a92002-12-11 14:43:13 +0000878 { "channels", (PyCFunction)oss_channels, METH_VARARGS },
879 { "speed", (PyCFunction)oss_speed, METH_VARARGS },
Greg Wardad4d9b92002-12-30 03:02:22 +0000880 { "sync", (PyCFunction)oss_sync, METH_VARARGS },
881 { "reset", (PyCFunction)oss_reset, METH_VARARGS },
882 { "post", (PyCFunction)oss_post, METH_VARARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000883
884 /* Convenience methods -- wrap a couple of ioctls together */
Greg Wardad4d9b92002-12-30 03:02:22 +0000885 { "setparameters", (PyCFunction)oss_setparameters, METH_VARARGS },
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000886 { "bufsize", (PyCFunction)oss_bufsize, METH_NOARGS },
887 { "obufcount", (PyCFunction)oss_obufcount, METH_NOARGS },
888 { "obuffree", (PyCFunction)oss_obuffree, METH_NOARGS },
889 { "getptr", (PyCFunction)oss_getptr, METH_NOARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000890
891 /* Aliases for backwards compatibility */
Greg Wardad4d9b92002-12-30 03:02:22 +0000892 { "flush", (PyCFunction)oss_sync, METH_VARARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000893
Georg Brandl1e908af2010-10-23 17:31:52 +0000894 /* Support for the context manager protocol */
Georg Brandl770a2be2010-10-24 20:47:32 +0000895 { "__enter__", oss_self, METH_NOARGS },
896 { "__exit__", oss_exit, METH_VARARGS },
Georg Brandl1e908af2010-10-23 17:31:52 +0000897
Greg Wardad4d9b92002-12-30 03:02:22 +0000898 { NULL, NULL} /* sentinel */
Greg Ward04613a92002-11-30 22:47:45 +0000899};
900
Greg Ward3d9994d2002-12-11 15:12:01 +0000901static PyMethodDef oss_mixer_methods[] = {
902 /* Regular file method - OSS mixers are ioctl-only interface */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000903 { "close", (PyCFunction)oss_mixer_close, METH_NOARGS },
904 { "fileno", (PyCFunction)oss_mixer_fileno, METH_NOARGS },
Greg Ward3d9994d2002-12-11 15:12:01 +0000905
Georg Brandl1e908af2010-10-23 17:31:52 +0000906 /* Support for the context manager protocol */
Georg Brandl770a2be2010-10-24 20:47:32 +0000907 { "__enter__", oss_self, METH_NOARGS },
908 { "__exit__", oss_exit, METH_VARARGS },
Georg Brandl1e908af2010-10-23 17:31:52 +0000909
Greg Ward3d9994d2002-12-11 15:12:01 +0000910 /* Simple ioctl wrappers */
Guido van Rossum0741f802003-06-02 14:15:34 +0000911 { "controls", (PyCFunction)oss_mixer_controls, METH_VARARGS },
Greg Ward2d6f9a92002-12-31 02:54:43 +0000912 { "stereocontrols", (PyCFunction)oss_mixer_stereocontrols, METH_VARARGS},
Guido van Rossum0741f802003-06-02 14:15:34 +0000913 { "reccontrols", (PyCFunction)oss_mixer_reccontrols, METH_VARARGS},
Greg Wardad4d9b92002-12-30 03:02:22 +0000914 { "get", (PyCFunction)oss_mixer_get, METH_VARARGS },
915 { "set", (PyCFunction)oss_mixer_set, METH_VARARGS },
Greg Wardf05aa102002-12-30 23:19:32 +0000916 { "get_recsrc", (PyCFunction)oss_mixer_get_recsrc, METH_VARARGS },
917 { "set_recsrc", (PyCFunction)oss_mixer_set_recsrc, METH_VARARGS },
Guido van Rossum0741f802003-06-02 14:15:34 +0000918
Greg Wardad4d9b92002-12-30 03:02:22 +0000919 { NULL, NULL}
Greg Ward3d9994d2002-12-11 15:12:01 +0000920};
921
Greg Ward04613a92002-11-30 22:47:45 +0000922static PyObject *
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000923oss_getattro(oss_audio_t *self, PyObject *nameobj)
Greg Ward04613a92002-11-30 22:47:45 +0000924{
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000925 char *name = "";
Greg Ward50682d02005-03-07 01:41:11 +0000926 PyObject * rval = NULL;
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000927
928 if (PyUnicode_Check(nameobj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 name = _PyUnicode_AsString(nameobj);
930
Greg Ward50682d02005-03-07 01:41:11 +0000931 if (strcmp(name, "closed") == 0) {
932 rval = (self->fd == -1) ? Py_True : Py_False;
933 Py_INCREF(rval);
934 }
935 else if (strcmp(name, "name") == 0) {
Neal Norwitz8abd2e62007-08-26 04:57:08 +0000936 rval = PyUnicode_FromString(self->devicename);
Greg Ward50682d02005-03-07 01:41:11 +0000937 }
938 else if (strcmp(name, "mode") == 0) {
939 /* No need for a "default" in this switch: from newossobject(),
940 self->mode can only be one of these three values. */
941 switch(self->mode) {
942 case O_RDONLY:
Neal Norwitz8abd2e62007-08-26 04:57:08 +0000943 rval = PyUnicode_FromString("r");
Greg Ward50682d02005-03-07 01:41:11 +0000944 break;
945 case O_RDWR:
Neal Norwitz8abd2e62007-08-26 04:57:08 +0000946 rval = PyUnicode_FromString("rw");
Greg Ward50682d02005-03-07 01:41:11 +0000947 break;
948 case O_WRONLY:
Neal Norwitz8abd2e62007-08-26 04:57:08 +0000949 rval = PyUnicode_FromString("w");
Greg Ward50682d02005-03-07 01:41:11 +0000950 break;
951 }
952 }
953 else {
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000954 rval = PyObject_GenericGetAttr((PyObject *)self, nameobj);
Greg Ward50682d02005-03-07 01:41:11 +0000955 }
956 return rval;
Greg Ward04613a92002-11-30 22:47:45 +0000957}
958
Greg Ward499b73e2002-12-31 03:04:52 +0000959static PyTypeObject OSSAudioType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000960 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Greg Ward9a568eb2002-11-30 23:20:09 +0000961 "ossaudiodev.oss_audio_device", /*tp_name*/
Greg Ward499b73e2002-12-31 03:04:52 +0000962 sizeof(oss_audio_t), /*tp_size*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000963 0, /*tp_itemsize*/
Greg Ward04613a92002-11-30 22:47:45 +0000964 /* methods */
Greg Wardad4d9b92002-12-30 03:02:22 +0000965 (destructor)oss_dealloc, /*tp_dealloc*/
966 0, /*tp_print*/
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000967 0, /*tp_getattr*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000968 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000969 0, /*tp_reserved*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000970 0, /*tp_repr*/
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000971 0, /*tp_as_number*/
972 0, /*tp_as_sequence*/
973 0, /*tp_as_mapping*/
974 0, /*tp_hash*/
975 0, /*tp_call*/
976 0, /*tp_str*/
977 (getattrofunc)oss_getattro, /*tp_getattro*/
978 0, /*tp_setattro*/
979 0, /*tp_as_buffer*/
980 Py_TPFLAGS_DEFAULT, /*tp_flags*/
981 0, /*tp_doc*/
982 0, /*tp_traverse*/
983 0, /*tp_clear*/
984 0, /*tp_richcompare*/
985 0, /*tp_weaklistoffset*/
986 0, /*tp_iter*/
987 0, /*tp_iternext*/
988 oss_methods, /*tp_methods*/
Greg Ward04613a92002-11-30 22:47:45 +0000989};
990
Greg Ward3d9994d2002-12-11 15:12:01 +0000991static PyTypeObject OSSMixerType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000992 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Greg Ward3d9994d2002-12-11 15:12:01 +0000993 "ossaudiodev.oss_mixer_device", /*tp_name*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000994 sizeof(oss_mixer_t), /*tp_size*/
995 0, /*tp_itemsize*/
Greg Ward3d9994d2002-12-11 15:12:01 +0000996 /* methods */
997 (destructor)oss_mixer_dealloc, /*tp_dealloc*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000998 0, /*tp_print*/
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000999 0, /*tp_getattr*/
Greg Wardad4d9b92002-12-30 03:02:22 +00001000 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001001 0, /*tp_reserved*/
Greg Wardad4d9b92002-12-30 03:02:22 +00001002 0, /*tp_repr*/
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +00001003 0, /*tp_as_number*/
1004 0, /*tp_as_sequence*/
1005 0, /*tp_as_mapping*/
1006 0, /*tp_hash*/
1007 0, /*tp_call*/
1008 0, /*tp_str*/
1009 0, /*tp_getattro*/
1010 0, /*tp_setattro*/
1011 0, /*tp_as_buffer*/
1012 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1013 0, /*tp_doc*/
1014 0, /*tp_traverse*/
1015 0, /*tp_clear*/
1016 0, /*tp_richcompare*/
1017 0, /*tp_weaklistoffset*/
1018 0, /*tp_iter*/
1019 0, /*tp_iternext*/
1020 oss_mixer_methods, /*tp_methods*/
Greg Ward3d9994d2002-12-11 15:12:01 +00001021};
1022
1023
Greg Ward04613a92002-11-30 22:47:45 +00001024static PyObject *
Greg Ward9a568eb2002-11-30 23:20:09 +00001025ossopen(PyObject *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +00001026{
Greg Ward8c6b6a92002-12-11 14:43:13 +00001027 return (PyObject *)newossobject(args);
Greg Ward04613a92002-11-30 22:47:45 +00001028}
1029
Greg Ward3d9994d2002-12-11 15:12:01 +00001030static PyObject *
1031ossopenmixer(PyObject *self, PyObject *args)
1032{
1033 return (PyObject *)newossmixerobject(args);
1034}
1035
Greg Ward9a568eb2002-11-30 23:20:09 +00001036static PyMethodDef ossaudiodev_methods[] = {
1037 { "open", ossopen, METH_VARARGS },
Greg Ward3d9994d2002-12-11 15:12:01 +00001038 { "openmixer", ossopenmixer, METH_VARARGS },
Greg Ward04613a92002-11-30 22:47:45 +00001039 { 0, 0 },
1040};
1041
Greg Ward1e0f57d2002-11-30 23:05:26 +00001042
1043#define _EXPORT_INT(mod, name) \
Martin v. Löwis1a214512008-06-11 05:26:20 +00001044 if (PyModule_AddIntConstant(mod, #name, (long) (name)) == -1) return NULL;
Greg Ward1e0f57d2002-11-30 23:05:26 +00001045
Greg Ward744f0fd2002-12-31 03:23:59 +00001046
1047static char *control_labels[] = SOUND_DEVICE_LABELS;
1048static char *control_names[] = SOUND_DEVICE_NAMES;
1049
1050
1051static int
1052build_namelists (PyObject *module)
1053{
1054 PyObject *labels;
1055 PyObject *names;
1056 PyObject *s;
1057 int num_controls;
1058 int i;
1059
Victor Stinner63941882011-09-29 00:42:28 +02001060 num_controls = Py_ARRAY_LENGTH(control_labels);
1061 assert(num_controls == Py_ARRAY_LENGTH(control_names));
Greg Ward744f0fd2002-12-31 03:23:59 +00001062
1063 labels = PyList_New(num_controls);
1064 names = PyList_New(num_controls);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001065 if (labels == NULL || names == NULL)
1066 goto error2;
Greg Ward744f0fd2002-12-31 03:23:59 +00001067 for (i = 0; i < num_controls; i++) {
Neal Norwitz8abd2e62007-08-26 04:57:08 +00001068 s = PyUnicode_FromString(control_labels[i]);
Greg Ward744f0fd2002-12-31 03:23:59 +00001069 if (s == NULL)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001070 goto error2;
Greg Ward744f0fd2002-12-31 03:23:59 +00001071 PyList_SET_ITEM(labels, i, s);
Guido van Rossum0741f802003-06-02 14:15:34 +00001072
Neal Norwitz8abd2e62007-08-26 04:57:08 +00001073 s = PyUnicode_FromString(control_names[i]);
Greg Ward744f0fd2002-12-31 03:23:59 +00001074 if (s == NULL)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001075 goto error2;
Greg Ward744f0fd2002-12-31 03:23:59 +00001076 PyList_SET_ITEM(names, i, s);
1077 }
1078
1079 if (PyModule_AddObject(module, "control_labels", labels) == -1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001080 goto error2;
Greg Ward744f0fd2002-12-31 03:23:59 +00001081 if (PyModule_AddObject(module, "control_names", names) == -1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001082 goto error1;
Greg Ward744f0fd2002-12-31 03:23:59 +00001083
1084 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001085
1086error2:
1087 Py_XDECREF(labels);
1088error1:
1089 Py_XDECREF(names);
1090 return -1;
Guido van Rossum0741f802003-06-02 14:15:34 +00001091}
Greg Ward744f0fd2002-12-31 03:23:59 +00001092
1093
Martin v. Löwis1a214512008-06-11 05:26:20 +00001094static struct PyModuleDef ossaudiodevmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 PyModuleDef_HEAD_INIT,
1096 "ossaudiodev",
1097 NULL,
1098 -1,
1099 ossaudiodev_methods,
1100 NULL,
1101 NULL,
1102 NULL,
1103 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001104};
1105
Antoine Pitrou39b35432010-03-23 00:25:54 +00001106PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001107PyInit_ossaudiodev(void)
Greg Ward04613a92002-11-30 22:47:45 +00001108{
1109 PyObject *m;
Guido van Rossum0741f802003-06-02 14:15:34 +00001110
Antoine Pitrou39b35432010-03-23 00:25:54 +00001111 if (PyType_Ready(&OSSAudioType) < 0)
1112 return NULL;
1113
1114 if (PyType_Ready(&OSSMixerType) < 0)
1115 return NULL;
1116
Martin v. Löwis1a214512008-06-11 05:26:20 +00001117 m = PyModule_Create(&ossaudiodevmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001118 if (m == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +00001120
Guido van Rossum0741f802003-06-02 14:15:34 +00001121 OSSAudioError = PyErr_NewException("ossaudiodev.OSSAudioError",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 NULL, NULL);
Greg Wardd6769062003-05-29 21:53:06 +00001123 if (OSSAudioError) {
Guido van Rossum0741f802003-06-02 14:15:34 +00001124 /* Each call to PyModule_AddObject decrefs it; compensate: */
1125 Py_INCREF(OSSAudioError);
1126 Py_INCREF(OSSAudioError);
Greg Wardad4d9b92002-12-30 03:02:22 +00001127 PyModule_AddObject(m, "error", OSSAudioError);
Greg Wardd6769062003-05-29 21:53:06 +00001128 PyModule_AddObject(m, "OSSAudioError", OSSAudioError);
1129 }
Greg Ward04613a92002-11-30 22:47:45 +00001130
Greg Ward744f0fd2002-12-31 03:23:59 +00001131 /* Build 'control_labels' and 'control_names' lists and add them
1132 to the module. */
1133 if (build_namelists(m) == -1) /* XXX what to do here? */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001134 return NULL;
Greg Ward744f0fd2002-12-31 03:23:59 +00001135
Greg Ward1e0f57d2002-11-30 23:05:26 +00001136 /* Expose the audio format numbers -- essential! */
1137 _EXPORT_INT(m, AFMT_QUERY);
1138 _EXPORT_INT(m, AFMT_MU_LAW);
1139 _EXPORT_INT(m, AFMT_A_LAW);
1140 _EXPORT_INT(m, AFMT_IMA_ADPCM);
1141 _EXPORT_INT(m, AFMT_U8);
1142 _EXPORT_INT(m, AFMT_S16_LE);
1143 _EXPORT_INT(m, AFMT_S16_BE);
1144 _EXPORT_INT(m, AFMT_S8);
1145 _EXPORT_INT(m, AFMT_U16_LE);
1146 _EXPORT_INT(m, AFMT_U16_BE);
1147 _EXPORT_INT(m, AFMT_MPEG);
Andrew M. Kuchlingfe62bc92003-02-13 13:27:07 +00001148#ifdef AFMT_AC3
Greg Ward1e0f57d2002-11-30 23:05:26 +00001149 _EXPORT_INT(m, AFMT_AC3);
Andrew M. Kuchlingfe62bc92003-02-13 13:27:07 +00001150#endif
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001151#ifdef AFMT_S16_NE
Greg Ward1e0f57d2002-11-30 23:05:26 +00001152 _EXPORT_INT(m, AFMT_S16_NE);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001153#endif
Greg Ward0f260542005-03-28 02:40:46 +00001154#ifdef AFMT_U16_NE
1155 _EXPORT_INT(m, AFMT_U16_NE);
1156#endif
1157#ifdef AFMT_S32_LE
1158 _EXPORT_INT(m, AFMT_S32_LE);
1159#endif
1160#ifdef AFMT_S32_BE
1161 _EXPORT_INT(m, AFMT_S32_BE);
1162#endif
1163#ifdef AFMT_MPEG
1164 _EXPORT_INT(m, AFMT_MPEG);
1165#endif
Guido van Rossum0741f802003-06-02 14:15:34 +00001166
Greg Ward37897c22002-12-30 02:43:36 +00001167 /* Expose the sound mixer device numbers. */
Greg Ward7b43c682002-12-30 02:29:28 +00001168 _EXPORT_INT(m, SOUND_MIXER_NRDEVICES);
Greg Ward3d9994d2002-12-11 15:12:01 +00001169 _EXPORT_INT(m, SOUND_MIXER_VOLUME);
1170 _EXPORT_INT(m, SOUND_MIXER_BASS);
1171 _EXPORT_INT(m, SOUND_MIXER_TREBLE);
1172 _EXPORT_INT(m, SOUND_MIXER_SYNTH);
1173 _EXPORT_INT(m, SOUND_MIXER_PCM);
1174 _EXPORT_INT(m, SOUND_MIXER_SPEAKER);
1175 _EXPORT_INT(m, SOUND_MIXER_LINE);
1176 _EXPORT_INT(m, SOUND_MIXER_MIC);
1177 _EXPORT_INT(m, SOUND_MIXER_CD);
1178 _EXPORT_INT(m, SOUND_MIXER_IMIX);
1179 _EXPORT_INT(m, SOUND_MIXER_ALTPCM);
1180 _EXPORT_INT(m, SOUND_MIXER_RECLEV);
1181 _EXPORT_INT(m, SOUND_MIXER_IGAIN);
1182 _EXPORT_INT(m, SOUND_MIXER_OGAIN);
1183 _EXPORT_INT(m, SOUND_MIXER_LINE1);
1184 _EXPORT_INT(m, SOUND_MIXER_LINE2);
1185 _EXPORT_INT(m, SOUND_MIXER_LINE3);
Greg Wardfd0283e2004-05-11 01:34:55 +00001186#ifdef SOUND_MIXER_DIGITAL1
Greg Ward3d9994d2002-12-11 15:12:01 +00001187 _EXPORT_INT(m, SOUND_MIXER_DIGITAL1);
Greg Wardfd0283e2004-05-11 01:34:55 +00001188#endif
1189#ifdef SOUND_MIXER_DIGITAL2
Greg Ward3d9994d2002-12-11 15:12:01 +00001190 _EXPORT_INT(m, SOUND_MIXER_DIGITAL2);
Greg Wardfd0283e2004-05-11 01:34:55 +00001191#endif
1192#ifdef SOUND_MIXER_DIGITAL3
Greg Ward3d9994d2002-12-11 15:12:01 +00001193 _EXPORT_INT(m, SOUND_MIXER_DIGITAL3);
Greg Wardfd0283e2004-05-11 01:34:55 +00001194#endif
1195#ifdef SOUND_MIXER_PHONEIN
Greg Ward3d9994d2002-12-11 15:12:01 +00001196 _EXPORT_INT(m, SOUND_MIXER_PHONEIN);
Greg Wardfd0283e2004-05-11 01:34:55 +00001197#endif
1198#ifdef SOUND_MIXER_PHONEOUT
Greg Ward3d9994d2002-12-11 15:12:01 +00001199 _EXPORT_INT(m, SOUND_MIXER_PHONEOUT);
Greg Wardfd0283e2004-05-11 01:34:55 +00001200#endif
1201#ifdef SOUND_MIXER_VIDEO
Greg Ward3d9994d2002-12-11 15:12:01 +00001202 _EXPORT_INT(m, SOUND_MIXER_VIDEO);
Greg Wardfd0283e2004-05-11 01:34:55 +00001203#endif
1204#ifdef SOUND_MIXER_RADIO
Greg Ward3d9994d2002-12-11 15:12:01 +00001205 _EXPORT_INT(m, SOUND_MIXER_RADIO);
Greg Wardfd0283e2004-05-11 01:34:55 +00001206#endif
1207#ifdef SOUND_MIXER_MONITOR
Greg Ward3d9994d2002-12-11 15:12:01 +00001208 _EXPORT_INT(m, SOUND_MIXER_MONITOR);
Greg Wardfd0283e2004-05-11 01:34:55 +00001209#endif
Greg Ward04613a92002-11-30 22:47:45 +00001210
Greg Ward1e0f57d2002-11-30 23:05:26 +00001211 /* Expose all the ioctl numbers for masochists who like to do this
1212 stuff directly. */
1213 _EXPORT_INT(m, SNDCTL_COPR_HALT);
1214 _EXPORT_INT(m, SNDCTL_COPR_LOAD);
1215 _EXPORT_INT(m, SNDCTL_COPR_RCODE);
1216 _EXPORT_INT(m, SNDCTL_COPR_RCVMSG);
1217 _EXPORT_INT(m, SNDCTL_COPR_RDATA);
1218 _EXPORT_INT(m, SNDCTL_COPR_RESET);
1219 _EXPORT_INT(m, SNDCTL_COPR_RUN);
1220 _EXPORT_INT(m, SNDCTL_COPR_SENDMSG);
1221 _EXPORT_INT(m, SNDCTL_COPR_WCODE);
1222 _EXPORT_INT(m, SNDCTL_COPR_WDATA);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001223#ifdef SNDCTL_DSP_BIND_CHANNEL
Greg Ward1e0f57d2002-11-30 23:05:26 +00001224 _EXPORT_INT(m, SNDCTL_DSP_BIND_CHANNEL);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001225#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001226 _EXPORT_INT(m, SNDCTL_DSP_CHANNELS);
1227 _EXPORT_INT(m, SNDCTL_DSP_GETBLKSIZE);
1228 _EXPORT_INT(m, SNDCTL_DSP_GETCAPS);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001229#ifdef SNDCTL_DSP_GETCHANNELMASK
Greg Ward1e0f57d2002-11-30 23:05:26 +00001230 _EXPORT_INT(m, SNDCTL_DSP_GETCHANNELMASK);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001231#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001232 _EXPORT_INT(m, SNDCTL_DSP_GETFMTS);
1233 _EXPORT_INT(m, SNDCTL_DSP_GETIPTR);
1234 _EXPORT_INT(m, SNDCTL_DSP_GETISPACE);
Greg Wardfd0283e2004-05-11 01:34:55 +00001235#ifdef SNDCTL_DSP_GETODELAY
Greg Ward1e0f57d2002-11-30 23:05:26 +00001236 _EXPORT_INT(m, SNDCTL_DSP_GETODELAY);
Greg Wardfd0283e2004-05-11 01:34:55 +00001237#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001238 _EXPORT_INT(m, SNDCTL_DSP_GETOPTR);
1239 _EXPORT_INT(m, SNDCTL_DSP_GETOSPACE);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001240#ifdef SNDCTL_DSP_GETSPDIF
Greg Ward1e0f57d2002-11-30 23:05:26 +00001241 _EXPORT_INT(m, SNDCTL_DSP_GETSPDIF);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001242#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001243 _EXPORT_INT(m, SNDCTL_DSP_GETTRIGGER);
1244 _EXPORT_INT(m, SNDCTL_DSP_MAPINBUF);
1245 _EXPORT_INT(m, SNDCTL_DSP_MAPOUTBUF);
1246 _EXPORT_INT(m, SNDCTL_DSP_NONBLOCK);
1247 _EXPORT_INT(m, SNDCTL_DSP_POST);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001248#ifdef SNDCTL_DSP_PROFILE
Greg Ward1e0f57d2002-11-30 23:05:26 +00001249 _EXPORT_INT(m, SNDCTL_DSP_PROFILE);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001250#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001251 _EXPORT_INT(m, SNDCTL_DSP_RESET);
1252 _EXPORT_INT(m, SNDCTL_DSP_SAMPLESIZE);
1253 _EXPORT_INT(m, SNDCTL_DSP_SETDUPLEX);
1254 _EXPORT_INT(m, SNDCTL_DSP_SETFMT);
1255 _EXPORT_INT(m, SNDCTL_DSP_SETFRAGMENT);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001256#ifdef SNDCTL_DSP_SETSPDIF
Greg Ward1e0f57d2002-11-30 23:05:26 +00001257 _EXPORT_INT(m, SNDCTL_DSP_SETSPDIF);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001258#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001259 _EXPORT_INT(m, SNDCTL_DSP_SETSYNCRO);
1260 _EXPORT_INT(m, SNDCTL_DSP_SETTRIGGER);
1261 _EXPORT_INT(m, SNDCTL_DSP_SPEED);
1262 _EXPORT_INT(m, SNDCTL_DSP_STEREO);
1263 _EXPORT_INT(m, SNDCTL_DSP_SUBDIVIDE);
1264 _EXPORT_INT(m, SNDCTL_DSP_SYNC);
1265 _EXPORT_INT(m, SNDCTL_FM_4OP_ENABLE);
1266 _EXPORT_INT(m, SNDCTL_FM_LOAD_INSTR);
1267 _EXPORT_INT(m, SNDCTL_MIDI_INFO);
1268 _EXPORT_INT(m, SNDCTL_MIDI_MPUCMD);
1269 _EXPORT_INT(m, SNDCTL_MIDI_MPUMODE);
1270 _EXPORT_INT(m, SNDCTL_MIDI_PRETIME);
1271 _EXPORT_INT(m, SNDCTL_SEQ_CTRLRATE);
1272 _EXPORT_INT(m, SNDCTL_SEQ_GETINCOUNT);
1273 _EXPORT_INT(m, SNDCTL_SEQ_GETOUTCOUNT);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001274#ifdef SNDCTL_SEQ_GETTIME
Greg Ward1e0f57d2002-11-30 23:05:26 +00001275 _EXPORT_INT(m, SNDCTL_SEQ_GETTIME);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001276#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001277 _EXPORT_INT(m, SNDCTL_SEQ_NRMIDIS);
1278 _EXPORT_INT(m, SNDCTL_SEQ_NRSYNTHS);
1279 _EXPORT_INT(m, SNDCTL_SEQ_OUTOFBAND);
1280 _EXPORT_INT(m, SNDCTL_SEQ_PANIC);
1281 _EXPORT_INT(m, SNDCTL_SEQ_PERCMODE);
1282 _EXPORT_INT(m, SNDCTL_SEQ_RESET);
1283 _EXPORT_INT(m, SNDCTL_SEQ_RESETSAMPLES);
1284 _EXPORT_INT(m, SNDCTL_SEQ_SYNC);
1285 _EXPORT_INT(m, SNDCTL_SEQ_TESTMIDI);
1286 _EXPORT_INT(m, SNDCTL_SEQ_THRESHOLD);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001287#ifdef SNDCTL_SYNTH_CONTROL
Greg Ward1e0f57d2002-11-30 23:05:26 +00001288 _EXPORT_INT(m, SNDCTL_SYNTH_CONTROL);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001289#endif
1290#ifdef SNDCTL_SYNTH_ID
Greg Ward1e0f57d2002-11-30 23:05:26 +00001291 _EXPORT_INT(m, SNDCTL_SYNTH_ID);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001292#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001293 _EXPORT_INT(m, SNDCTL_SYNTH_INFO);
1294 _EXPORT_INT(m, SNDCTL_SYNTH_MEMAVL);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001295#ifdef SNDCTL_SYNTH_REMOVESAMPLE
Greg Ward1e0f57d2002-11-30 23:05:26 +00001296 _EXPORT_INT(m, SNDCTL_SYNTH_REMOVESAMPLE);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001297#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001298 _EXPORT_INT(m, SNDCTL_TMR_CONTINUE);
1299 _EXPORT_INT(m, SNDCTL_TMR_METRONOME);
1300 _EXPORT_INT(m, SNDCTL_TMR_SELECT);
1301 _EXPORT_INT(m, SNDCTL_TMR_SOURCE);
1302 _EXPORT_INT(m, SNDCTL_TMR_START);
1303 _EXPORT_INT(m, SNDCTL_TMR_STOP);
1304 _EXPORT_INT(m, SNDCTL_TMR_TEMPO);
1305 _EXPORT_INT(m, SNDCTL_TMR_TIMEBASE);
Martin v. Löwis1a214512008-06-11 05:26:20 +00001306 return m;
Greg Ward04613a92002-11-30 22:47:45 +00001307}