blob: 95a23b7ea34a06486f32551a957b00fd60768202 [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) {
Greg Ward50682d02005-03-07 01:41:11 +0000132 PyErr_SetFromErrnoWithFilename(PyExc_IOError, devicename);
Greg Ward04613a92002-11-30 22:47:45 +0000133 return NULL;
134 }
135 /* Create and initialize the object */
Greg Ward58ae13c2002-12-31 03:07:21 +0000136 if ((self = PyObject_New(oss_audio_t, &OSSAudioType)) == NULL) {
Greg Ward04613a92002-11-30 22:47:45 +0000137 close(fd);
138 return NULL;
139 }
Greg Ward50682d02005-03-07 01:41:11 +0000140 self->devicename = devicename;
Greg Ward58ae13c2002-12-31 03:07:21 +0000141 self->fd = fd;
142 self->mode = imode;
143 self->icount = self->ocount = 0;
144 self->afmts = afmts;
145 return self;
Greg Ward04613a92002-11-30 22:47:45 +0000146}
147
148static void
Greg Ward58ae13c2002-12-31 03:07:21 +0000149oss_dealloc(oss_audio_t *self)
Greg Ward04613a92002-11-30 22:47:45 +0000150{
151 /* if already closed, don't reclose it */
Greg Ward58ae13c2002-12-31 03:07:21 +0000152 if (self->fd != -1)
153 close(self->fd);
154 PyObject_Del(self);
Greg Ward04613a92002-11-30 22:47:45 +0000155}
156
Greg Wardda1cacb2002-12-31 03:02:23 +0000157
158/* ----------------------------------------------------------------------
159 * Mixer object initialization/deallocation
160 */
161
Greg Ward3d9994d2002-12-11 15:12:01 +0000162static oss_mixer_t *
163newossmixerobject(PyObject *arg)
164{
Greg Ward50682d02005-03-07 01:41:11 +0000165 char *devicename = NULL;
Greg Ward0b6dfb82003-03-10 03:17:06 +0000166 int fd;
Greg Ward58ae13c2002-12-31 03:07:21 +0000167 oss_mixer_t *self;
Guido van Rossum0741f802003-06-02 14:15:34 +0000168
Greg Ward50682d02005-03-07 01:41:11 +0000169 if (!PyArg_ParseTuple(arg, "|s", &devicename)) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000170 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000171 }
Guido van Rossum0741f802003-06-02 14:15:34 +0000172
Greg Ward50682d02005-03-07 01:41:11 +0000173 if (devicename == NULL) {
174 devicename = getenv("MIXERDEV");
175 if (devicename == NULL) /* MIXERDEV not set */
176 devicename = "/dev/mixer";
Greg Ward3d9994d2002-12-11 15:12:01 +0000177 }
178
Greg Ward50682d02005-03-07 01:41:11 +0000179 if ((fd = open(devicename, O_RDWR)) == -1) {
180 PyErr_SetFromErrnoWithFilename(PyExc_IOError, devicename);
Greg Ward3d9994d2002-12-11 15:12:01 +0000181 return NULL;
182 }
Greg Ward0b6dfb82003-03-10 03:17:06 +0000183
Greg Ward58ae13c2002-12-31 03:07:21 +0000184 if ((self = PyObject_New(oss_mixer_t, &OSSMixerType)) == NULL) {
Greg Ward3d9994d2002-12-11 15:12:01 +0000185 close(fd);
186 return NULL;
187 }
Guido van Rossum0741f802003-06-02 14:15:34 +0000188
Greg Ward58ae13c2002-12-31 03:07:21 +0000189 self->fd = fd;
Guido van Rossum0741f802003-06-02 14:15:34 +0000190
Greg Ward58ae13c2002-12-31 03:07:21 +0000191 return self;
Greg Ward3d9994d2002-12-11 15:12:01 +0000192}
193
194static void
Greg Ward58ae13c2002-12-31 03:07:21 +0000195oss_mixer_dealloc(oss_mixer_t *self)
Greg Ward3d9994d2002-12-11 15:12:01 +0000196{
197 /* if already closed, don't reclose it */
Greg Ward58ae13c2002-12-31 03:07:21 +0000198 if (self->fd != -1)
199 close(self->fd);
200 PyObject_Del(self);
Greg Ward3d9994d2002-12-11 15:12:01 +0000201}
202
Greg Ward131bce02002-11-30 22:56:44 +0000203
204/* Methods to wrap the OSS ioctls. The calling convention is pretty
205 simple:
206 nonblock() -> ioctl(fd, SNDCTL_DSP_NONBLOCK)
207 fmt = setfmt(fmt) -> ioctl(fd, SNDCTL_DSP_SETFMT, &fmt)
208 etc.
209*/
210
211
Greg Wardd6769062003-05-29 21:53:06 +0000212/* ----------------------------------------------------------------------
213 * Helper functions
214 */
215
Charles-François Natalia5293082011-06-11 18:58:24 +0200216/* Check if a given file descriptor is valid (i.e. hasn't been closed).
217 * If true, return 1. Otherwise, raise ValueError and return 0.
218 */
219static int _is_fd_valid(int fd)
220{
221 /* the FD is set to -1 in oss_close()/oss_mixer_close() */
222 if (fd >= 0) {
223 return 1;
224 } else {
225 PyErr_SetString(PyExc_ValueError,
226 "Operation on closed OSS device.");
227 return 0;
228 }
229}
230
Greg Ward131bce02002-11-30 22:56:44 +0000231/* _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);
Christian Heimes217cfd12007-12-02 14:31:20 +0000253 return PyLong_FromLong(arg);
Greg Ward131bce02002-11-30 22:56:44 +0000254}
255
Greg Wardda1cacb2002-12-31 03:02:23 +0000256
Greg Ward3d9994d2002-12-11 15:12:01 +0000257/* _do_ioctl_1_internal() is a wrapper for ioctls that take no inputs
258 but return an output -- ie. we need to pass a pointer to a local C
259 variable so the driver can write its output there, but from Python
260 all we see is the return value. For example,
261 SOUND_MIXER_READ_DEVMASK returns a bitmask of available mixer
262 devices, but does not use the value of the parameter passed-in in any
263 way.
264*/
Greg Ward3d9994d2002-12-11 15:12:01 +0000265static PyObject *
266_do_ioctl_1_internal(int fd, PyObject *args, char *fname, int cmd)
267{
268 char argfmt[32] = ":";
269 int arg = 0;
270
271 assert(strlen(fname) <= 30);
272 strcat(argfmt, fname);
273 if (!PyArg_ParseTuple(args, argfmt, &arg))
Greg Wardad4d9b92002-12-30 03:02:22 +0000274 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000275
276 if (ioctl(fd, cmd, &arg) == -1)
277 return PyErr_SetFromErrno(PyExc_IOError);
Christian Heimes217cfd12007-12-02 14:31:20 +0000278 return PyLong_FromLong(arg);
Greg Ward3d9994d2002-12-11 15:12:01 +0000279}
280
281
282
Greg Ward131bce02002-11-30 22:56:44 +0000283/* _do_ioctl_0() is a private helper for the no-argument ioctls:
284 SNDCTL_DSP_{SYNC,RESET,POST}. */
285static PyObject *
Greg Wardda9f8532002-12-11 14:49:59 +0000286_do_ioctl_0(int fd, PyObject *args, char *fname, int cmd)
Greg Ward131bce02002-11-30 22:56:44 +0000287{
Greg Wardda9f8532002-12-11 14:49:59 +0000288 char argfmt[32] = ":";
Greg Wardd0d592f2003-05-27 01:57:21 +0000289 int rv;
Greg Ward131bce02002-11-30 22:56:44 +0000290
Greg Wardda9f8532002-12-11 14:49:59 +0000291 assert(strlen(fname) <= 30);
Greg Ward131bce02002-11-30 22:56:44 +0000292 strcat(argfmt, fname);
293 if (!PyArg_ParseTuple(args, argfmt))
Greg Wardad4d9b92002-12-30 03:02:22 +0000294 return NULL;
Greg Ward131bce02002-11-30 22:56:44 +0000295
Greg Wardd0d592f2003-05-27 01:57:21 +0000296 /* According to hannu@opensound.com, all three of the ioctls that
297 use this function can block, so release the GIL. This is
298 especially important for SYNC, which can block for several
299 seconds. */
300 Py_BEGIN_ALLOW_THREADS
301 rv = ioctl(fd, cmd, 0);
302 Py_END_ALLOW_THREADS
303
304 if (rv == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000305 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000306 Py_INCREF(Py_None);
307 return Py_None;
308}
309
310
Greg Wardda1cacb2002-12-31 03:02:23 +0000311/* ----------------------------------------------------------------------
Greg Ward499b73e2002-12-31 03:04:52 +0000312 * Methods of DSP objects (OSSAudioType)
Greg Wardda1cacb2002-12-31 03:02:23 +0000313 */
314
Greg Ward131bce02002-11-30 22:56:44 +0000315static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000316oss_nonblock(oss_audio_t *self, PyObject *unused)
Greg Ward131bce02002-11-30 22:56:44 +0000317{
Charles-François Natalia5293082011-06-11 18:58:24 +0200318 if (!_is_fd_valid(self->fd))
319 return NULL;
320
Greg Ward131bce02002-11-30 22:56:44 +0000321 /* Hmmm: it doesn't appear to be possible to return to blocking
322 mode once we're in non-blocking mode! */
Greg Ward7b43c682002-12-30 02:29:28 +0000323 if (ioctl(self->fd, SNDCTL_DSP_NONBLOCK, NULL) == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000324 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000325 Py_INCREF(Py_None);
326 return Py_None;
327}
328
329static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000330oss_setfmt(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000331{
Charles-François Natalia5293082011-06-11 18:58:24 +0200332 if (!_is_fd_valid(self->fd))
333 return NULL;
334
Greg Ward7b43c682002-12-30 02:29:28 +0000335 return _do_ioctl_1(self->fd, args, "setfmt", SNDCTL_DSP_SETFMT);
Greg Ward131bce02002-11-30 22:56:44 +0000336}
337
338static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000339oss_getfmts(oss_audio_t *self, PyObject *unused)
Greg Ward131bce02002-11-30 22:56:44 +0000340{
341 int mask;
Charles-François Natalia5293082011-06-11 18:58:24 +0200342
343 if (!_is_fd_valid(self->fd))
344 return NULL;
345
Greg Ward7b43c682002-12-30 02:29:28 +0000346 if (ioctl(self->fd, SNDCTL_DSP_GETFMTS, &mask) == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000347 return PyErr_SetFromErrno(PyExc_IOError);
Christian Heimes217cfd12007-12-02 14:31:20 +0000348 return PyLong_FromLong(mask);
Greg Ward131bce02002-11-30 22:56:44 +0000349}
350
351static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000352oss_channels(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000353{
Charles-François Natalia5293082011-06-11 18:58:24 +0200354 if (!_is_fd_valid(self->fd))
355 return NULL;
356
Greg Ward7b43c682002-12-30 02:29:28 +0000357 return _do_ioctl_1(self->fd, args, "channels", SNDCTL_DSP_CHANNELS);
Greg Ward131bce02002-11-30 22:56:44 +0000358}
359
360static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000361oss_speed(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000362{
Charles-François Natalia5293082011-06-11 18:58:24 +0200363 if (!_is_fd_valid(self->fd))
364 return NULL;
365
Greg Ward7b43c682002-12-30 02:29:28 +0000366 return _do_ioctl_1(self->fd, args, "speed", SNDCTL_DSP_SPEED);
Greg Ward131bce02002-11-30 22:56:44 +0000367}
368
369static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000370oss_sync(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000371{
Charles-François Natalia5293082011-06-11 18:58:24 +0200372 if (!_is_fd_valid(self->fd))
373 return NULL;
374
Greg Wardd0d592f2003-05-27 01:57:21 +0000375 return _do_ioctl_0(self->fd, args, "sync", SNDCTL_DSP_SYNC);
Greg Ward131bce02002-11-30 22:56:44 +0000376}
Guido van Rossum0741f802003-06-02 14:15:34 +0000377
Greg Ward131bce02002-11-30 22:56:44 +0000378static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000379oss_reset(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000380{
Charles-François Natalia5293082011-06-11 18:58:24 +0200381 if (!_is_fd_valid(self->fd))
382 return NULL;
383
Greg Ward7b43c682002-12-30 02:29:28 +0000384 return _do_ioctl_0(self->fd, args, "reset", SNDCTL_DSP_RESET);
Greg Ward131bce02002-11-30 22:56:44 +0000385}
Guido van Rossum0741f802003-06-02 14:15:34 +0000386
Greg Ward131bce02002-11-30 22:56:44 +0000387static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000388oss_post(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000389{
Charles-François Natalia5293082011-06-11 18:58:24 +0200390 if (!_is_fd_valid(self->fd))
391 return NULL;
392
Greg Ward7b43c682002-12-30 02:29:28 +0000393 return _do_ioctl_0(self->fd, args, "post", SNDCTL_DSP_POST);
Greg Ward131bce02002-11-30 22:56:44 +0000394}
395
396
397/* Regular file methods: read(), write(), close(), etc. as well
398 as one convenience method, writeall(). */
399
Greg Ward04613a92002-11-30 22:47:45 +0000400static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000401oss_read(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000402{
403 int size, count;
404 char *cp;
405 PyObject *rv;
Guido van Rossum0741f802003-06-02 14:15:34 +0000406
Charles-François Natalia5293082011-06-11 18:58:24 +0200407 if (!_is_fd_valid(self->fd))
408 return NULL;
409
Greg Ward04613a92002-11-30 22:47:45 +0000410 if (!PyArg_ParseTuple(args, "i:read", &size))
411 return NULL;
Gregory P. Smith0a608fd2008-09-06 21:34:51 +0000412 rv = PyBytes_FromStringAndSize(NULL, size);
Greg Ward04613a92002-11-30 22:47:45 +0000413 if (rv == NULL)
414 return NULL;
Gregory P. Smith0a608fd2008-09-06 21:34:51 +0000415 cp = PyBytes_AS_STRING(rv);
Greg Ward64927852003-05-23 01:50:37 +0000416
417 Py_BEGIN_ALLOW_THREADS
418 count = read(self->fd, cp, size);
419 Py_END_ALLOW_THREADS
420
421 if (count < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000422 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000423 Py_DECREF(rv);
424 return NULL;
425 }
Greg Ward7b43c682002-12-30 02:29:28 +0000426 self->icount += count;
Gregory P. Smith0a608fd2008-09-06 21:34:51 +0000427 _PyBytes_Resize(&rv, count);
Greg Ward04613a92002-11-30 22:47:45 +0000428 return rv;
429}
430
431static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000432oss_write(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000433{
434 char *cp;
435 int rv, size;
Greg Ward131bce02002-11-30 22:56:44 +0000436
Charles-François Natalia5293082011-06-11 18:58:24 +0200437 if (!_is_fd_valid(self->fd))
438 return NULL;
439
Neal Norwitz8abd2e62007-08-26 04:57:08 +0000440 if (!PyArg_ParseTuple(args, "y#:write", &cp, &size)) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000441 return NULL;
Greg Ward131bce02002-11-30 22:56:44 +0000442 }
Greg Ward64927852003-05-23 01:50:37 +0000443
444 Py_BEGIN_ALLOW_THREADS
445 rv = write(self->fd, cp, size);
446 Py_END_ALLOW_THREADS
447
448 if (rv == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000449 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000450 } else {
Greg Ward7b43c682002-12-30 02:29:28 +0000451 self->ocount += rv;
Greg Ward131bce02002-11-30 22:56:44 +0000452 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000453 return PyLong_FromLong(rv);
Greg Ward131bce02002-11-30 22:56:44 +0000454}
455
456static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000457oss_writeall(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000458{
459 char *cp;
460 int rv, size;
461 fd_set write_set_fds;
462 int select_rv;
Guido van Rossum0741f802003-06-02 14:15:34 +0000463
Greg Ward131bce02002-11-30 22:56:44 +0000464 /* NB. writeall() is only useful in non-blocking mode: according to
465 Guenter Geiger <geiger@xdv.org> on the linux-audio-dev list
466 (http://eca.cx/lad/2002/11/0380.html), OSS guarantees that
467 write() in blocking mode consumes the whole buffer. In blocking
468 mode, the behaviour of write() and writeall() from Python is
469 indistinguishable. */
470
Charles-François Natalia5293082011-06-11 18:58:24 +0200471 if (!_is_fd_valid(self->fd))
472 return NULL;
473
Neal Norwitz8abd2e62007-08-26 04:57:08 +0000474 if (!PyArg_ParseTuple(args, "y#:write", &cp, &size))
Greg Ward131bce02002-11-30 22:56:44 +0000475 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000476
Charles-François Nataliaa26b272011-08-28 17:51:43 +0200477 if (!_PyIsSelectable_fd(self->fd)) {
478 PyErr_SetString(PyExc_ValueError,
479 "file descriptor out of range for select");
480 return NULL;
481 }
Greg Ward04613a92002-11-30 22:47:45 +0000482 /* use select to wait for audio device to be available */
483 FD_ZERO(&write_set_fds);
Greg Ward7b43c682002-12-30 02:29:28 +0000484 FD_SET(self->fd, &write_set_fds);
Greg Ward04613a92002-11-30 22:47:45 +0000485
486 while (size > 0) {
Greg Ward64927852003-05-23 01:50:37 +0000487 Py_BEGIN_ALLOW_THREADS
Greg Ward7b43c682002-12-30 02:29:28 +0000488 select_rv = select(self->fd+1, NULL, &write_set_fds, NULL, NULL);
Greg Ward64927852003-05-23 01:50:37 +0000489 Py_END_ALLOW_THREADS
Greg Ward131bce02002-11-30 22:56:44 +0000490 assert(select_rv != 0); /* no timeout, can't expire */
491 if (select_rv == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000492 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000493
Greg Ward64927852003-05-23 01:50:37 +0000494 Py_BEGIN_ALLOW_THREADS
Greg Ward7b43c682002-12-30 02:29:28 +0000495 rv = write(self->fd, cp, size);
Greg Ward64927852003-05-23 01:50:37 +0000496 Py_END_ALLOW_THREADS
Greg Ward131bce02002-11-30 22:56:44 +0000497 if (rv == -1) {
498 if (errno == EAGAIN) { /* buffer is full, try again */
499 errno = 0;
500 continue;
501 } else /* it's a real error */
Greg Ward97708bc2002-11-30 23:17:10 +0000502 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000503 } else { /* wrote rv bytes */
Greg Ward7b43c682002-12-30 02:29:28 +0000504 self->ocount += rv;
Greg Ward131bce02002-11-30 22:56:44 +0000505 size -= rv;
506 cp += rv;
507 }
Greg Ward04613a92002-11-30 22:47:45 +0000508 }
509 Py_INCREF(Py_None);
510 return Py_None;
511}
512
513static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000514oss_close(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000515{
Greg Ward7b43c682002-12-30 02:29:28 +0000516 if (self->fd >= 0) {
Greg Wardb8043902003-05-26 22:47:30 +0000517 Py_BEGIN_ALLOW_THREADS
Greg Ward7b43c682002-12-30 02:29:28 +0000518 close(self->fd);
Greg Wardb8043902003-05-26 22:47:30 +0000519 Py_END_ALLOW_THREADS
Greg Ward7b43c682002-12-30 02:29:28 +0000520 self->fd = -1;
Greg Ward04613a92002-11-30 22:47:45 +0000521 }
522 Py_INCREF(Py_None);
523 return Py_None;
524}
525
526static PyObject *
Georg Brandl770a2be2010-10-24 20:47:32 +0000527oss_self(PyObject *self, PyObject *unused)
Georg Brandl1e908af2010-10-23 17:31:52 +0000528{
529 Py_INCREF(self);
530 return self;
531}
532
533static PyObject *
534oss_exit(PyObject *self, PyObject *unused)
535{
536 PyObject *ret = PyObject_CallMethod(self, "close", NULL);
537 if (!ret)
538 return NULL;
539 Py_DECREF(ret);
540 Py_RETURN_NONE;
541}
542
543static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000544oss_fileno(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000545{
Charles-François Natalia5293082011-06-11 18:58:24 +0200546 if (!_is_fd_valid(self->fd))
547 return NULL;
548
Christian Heimes217cfd12007-12-02 14:31:20 +0000549 return PyLong_FromLong(self->fd);
Greg Ward04613a92002-11-30 22:47:45 +0000550}
551
Greg Ward131bce02002-11-30 22:56:44 +0000552
553/* Convenience methods: these generally wrap a couple of ioctls into one
554 common task. */
555
Greg Ward04613a92002-11-30 22:47:45 +0000556static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000557oss_setparameters(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000558{
Greg Wardd6769062003-05-29 21:53:06 +0000559 int wanted_fmt, wanted_channels, wanted_rate, strict=0;
560 int fmt, channels, rate;
561 PyObject * rv; /* return tuple (fmt, channels, rate) */
Greg Ward04613a92002-11-30 22:47:45 +0000562
Charles-François Natalia5293082011-06-11 18:58:24 +0200563 if (!_is_fd_valid(self->fd))
564 return NULL;
565
Greg Wardd6769062003-05-29 21:53:06 +0000566 if (!PyArg_ParseTuple(args, "iii|i:setparameters",
567 &wanted_fmt, &wanted_channels, &wanted_rate,
568 &strict))
Greg Ward04613a92002-11-30 22:47:45 +0000569 return NULL;
Greg Wardd6769062003-05-29 21:53:06 +0000570
571 fmt = wanted_fmt;
572 if (ioctl(self->fd, SNDCTL_DSP_SETFMT, &fmt) == -1) {
573 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000574 }
Greg Wardd6769062003-05-29 21:53:06 +0000575 if (strict && fmt != wanted_fmt) {
576 return PyErr_Format
577 (OSSAudioError,
578 "unable to set requested format (wanted %d, got %d)",
579 wanted_fmt, fmt);
Greg Ward04613a92002-11-30 22:47:45 +0000580 }
581
Greg Wardd6769062003-05-29 21:53:06 +0000582 channels = wanted_channels;
583 if (ioctl(self->fd, SNDCTL_DSP_CHANNELS, &channels) == -1) {
584 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000585 }
Greg Wardd6769062003-05-29 21:53:06 +0000586 if (strict && channels != wanted_channels) {
587 return PyErr_Format
588 (OSSAudioError,
589 "unable to set requested channels (wanted %d, got %d)",
590 wanted_channels, channels);
Greg Ward04613a92002-11-30 22:47:45 +0000591 }
592
Greg Wardd6769062003-05-29 21:53:06 +0000593 rate = wanted_rate;
Greg Ward7b43c682002-12-30 02:29:28 +0000594 if (ioctl(self->fd, SNDCTL_DSP_SPEED, &rate) == -1) {
Greg Wardd6769062003-05-29 21:53:06 +0000595 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000596 }
Greg Wardd6769062003-05-29 21:53:06 +0000597 if (strict && rate != wanted_rate) {
598 return PyErr_Format
599 (OSSAudioError,
600 "unable to set requested rate (wanted %d, got %d)",
601 wanted_rate, rate);
602 }
Guido van Rossum0741f802003-06-02 14:15:34 +0000603
Greg Wardd6769062003-05-29 21:53:06 +0000604 /* Construct the return value: a (fmt, channels, rate) tuple that
605 tells what the audio hardware was actually set to. */
606 rv = PyTuple_New(3);
607 if (rv == NULL)
608 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +0000609 PyTuple_SET_ITEM(rv, 0, PyLong_FromLong(fmt));
610 PyTuple_SET_ITEM(rv, 1, PyLong_FromLong(channels));
611 PyTuple_SET_ITEM(rv, 2, PyLong_FromLong(rate));
Greg Wardd6769062003-05-29 21:53:06 +0000612 return rv;
Greg Ward04613a92002-11-30 22:47:45 +0000613}
614
615static int
Greg Ward499b73e2002-12-31 03:04:52 +0000616_ssize(oss_audio_t *self, int *nchannels, int *ssize)
Greg Ward04613a92002-11-30 22:47:45 +0000617{
618 int fmt;
619
620 fmt = 0;
Guido van Rossum0741f802003-06-02 14:15:34 +0000621 if (ioctl(self->fd, SNDCTL_DSP_SETFMT, &fmt) < 0)
Greg Ward04613a92002-11-30 22:47:45 +0000622 return -errno;
623
624 switch (fmt) {
625 case AFMT_MU_LAW:
626 case AFMT_A_LAW:
627 case AFMT_U8:
628 case AFMT_S8:
Greg Ward38c92662003-05-29 21:55:41 +0000629 *ssize = 1; /* 8 bit formats: 1 byte */
Greg Ward04613a92002-11-30 22:47:45 +0000630 break;
631 case AFMT_S16_LE:
632 case AFMT_S16_BE:
633 case AFMT_U16_LE:
634 case AFMT_U16_BE:
Greg Ward84f8ecd2003-05-29 23:44:44 +0000635 *ssize = 2; /* 16 bit formats: 2 byte */
Greg Ward04613a92002-11-30 22:47:45 +0000636 break;
637 case AFMT_MPEG:
638 case AFMT_IMA_ADPCM:
639 default:
640 return -EOPNOTSUPP;
641 }
Greg Ward7b43c682002-12-30 02:29:28 +0000642 if (ioctl(self->fd, SNDCTL_DSP_CHANNELS, nchannels) < 0)
Greg Ward04613a92002-11-30 22:47:45 +0000643 return -errno;
644 return 0;
645}
646
647
Guido van Rossum0741f802003-06-02 14:15:34 +0000648/* bufsize returns the size of the hardware audio buffer in number
Greg Ward04613a92002-11-30 22:47:45 +0000649 of samples */
650static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000651oss_bufsize(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000652{
653 audio_buf_info ai;
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000654 int nchannels=0, ssize=0;
Greg Ward04613a92002-11-30 22:47:45 +0000655
Charles-François Natalia5293082011-06-11 18:58:24 +0200656 if (!_is_fd_valid(self->fd))
657 return NULL;
658
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000659 if (_ssize(self, &nchannels, &ssize) < 0 || !nchannels || !ssize) {
Greg Ward97708bc2002-11-30 23:17:10 +0000660 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000661 return NULL;
662 }
Greg Ward7b43c682002-12-30 02:29:28 +0000663 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000664 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000665 return NULL;
666 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000667 return PyLong_FromLong((ai.fragstotal * ai.fragsize) / (nchannels * ssize));
Greg Ward04613a92002-11-30 22:47:45 +0000668}
669
Guido van Rossum0741f802003-06-02 14:15:34 +0000670/* obufcount returns the number of samples that are available in the
Greg Ward04613a92002-11-30 22:47:45 +0000671 hardware for playing */
672static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000673oss_obufcount(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000674{
675 audio_buf_info ai;
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000676 int nchannels=0, ssize=0;
Greg Ward04613a92002-11-30 22:47:45 +0000677
Charles-François Natalia5293082011-06-11 18:58:24 +0200678 if (!_is_fd_valid(self->fd))
679 return NULL;
680
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000681 if (_ssize(self, &nchannels, &ssize) < 0 || !nchannels || !ssize) {
Greg Ward97708bc2002-11-30 23:17:10 +0000682 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000683 return NULL;
684 }
Greg Ward7b43c682002-12-30 02:29:28 +0000685 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000686 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000687 return NULL;
688 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000689 return PyLong_FromLong((ai.fragstotal * ai.fragsize - ai.bytes) /
Greg Ward04613a92002-11-30 22:47:45 +0000690 (ssize * nchannels));
691}
692
693/* obufcount returns the number of samples that can be played without
694 blocking */
695static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000696oss_obuffree(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000697{
698 audio_buf_info ai;
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000699 int nchannels=0, ssize=0;
Greg Ward04613a92002-11-30 22:47:45 +0000700
Charles-François Natalia5293082011-06-11 18:58:24 +0200701 if (!_is_fd_valid(self->fd))
702 return NULL;
703
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000704 if (_ssize(self, &nchannels, &ssize) < 0 || !nchannels || !ssize) {
Greg Ward97708bc2002-11-30 23:17:10 +0000705 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000706 return NULL;
707 }
Greg Ward7b43c682002-12-30 02:29:28 +0000708 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000709 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000710 return NULL;
711 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000712 return PyLong_FromLong(ai.bytes / (ssize * nchannels));
Greg Ward04613a92002-11-30 22:47:45 +0000713}
714
Greg Ward04613a92002-11-30 22:47:45 +0000715static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000716oss_getptr(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000717{
718 count_info info;
719 int req;
720
Charles-François Natalia5293082011-06-11 18:58:24 +0200721 if (!_is_fd_valid(self->fd))
722 return NULL;
723
Greg Ward7b43c682002-12-30 02:29:28 +0000724 if (self->mode == O_RDONLY)
Greg Wardad4d9b92002-12-30 03:02:22 +0000725 req = SNDCTL_DSP_GETIPTR;
Greg Ward04613a92002-11-30 22:47:45 +0000726 else
Greg Wardad4d9b92002-12-30 03:02:22 +0000727 req = SNDCTL_DSP_GETOPTR;
Greg Ward7b43c682002-12-30 02:29:28 +0000728 if (ioctl(self->fd, req, &info) == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000729 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000730 return NULL;
731 }
732 return Py_BuildValue("iii", info.bytes, info.blocks, info.ptr);
733}
734
Greg Wardda1cacb2002-12-31 03:02:23 +0000735
736/* ----------------------------------------------------------------------
737 * Methods of mixer objects (OSSMixerType)
738 */
739
Greg Ward3d9994d2002-12-11 15:12:01 +0000740static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000741oss_mixer_close(oss_mixer_t *self, PyObject *unused)
Greg Ward3d9994d2002-12-11 15:12:01 +0000742{
Greg Ward7b43c682002-12-30 02:29:28 +0000743 if (self->fd >= 0) {
744 close(self->fd);
745 self->fd = -1;
Greg Ward3d9994d2002-12-11 15:12:01 +0000746 }
747 Py_INCREF(Py_None);
748 return Py_None;
749}
750
751static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000752oss_mixer_fileno(oss_mixer_t *self, PyObject *unused)
Greg Ward3d9994d2002-12-11 15:12:01 +0000753{
Charles-François Natalia5293082011-06-11 18:58:24 +0200754 if (!_is_fd_valid(self->fd))
755 return NULL;
756
Christian Heimes217cfd12007-12-02 14:31:20 +0000757 return PyLong_FromLong(self->fd);
Greg Ward3d9994d2002-12-11 15:12:01 +0000758}
759
760/* Simple mixer interface methods */
761
762static PyObject *
Greg Ward2d6f9a92002-12-31 02:54:43 +0000763oss_mixer_controls(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000764{
Charles-François Natalia5293082011-06-11 18:58:24 +0200765 if (!_is_fd_valid(self->fd))
766 return NULL;
767
Greg Ward2d6f9a92002-12-31 02:54:43 +0000768 return _do_ioctl_1_internal(self->fd, args, "controls",
Greg Ward3d9994d2002-12-11 15:12:01 +0000769 SOUND_MIXER_READ_DEVMASK);
770}
771
772static PyObject *
Greg Ward2d6f9a92002-12-31 02:54:43 +0000773oss_mixer_stereocontrols(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000774{
Charles-François Natalia5293082011-06-11 18:58:24 +0200775 if (!_is_fd_valid(self->fd))
776 return NULL;
777
Greg Ward2d6f9a92002-12-31 02:54:43 +0000778 return _do_ioctl_1_internal(self->fd, args, "stereocontrols",
Greg Ward3d9994d2002-12-11 15:12:01 +0000779 SOUND_MIXER_READ_STEREODEVS);
780}
781
782static PyObject *
Greg Ward2d6f9a92002-12-31 02:54:43 +0000783oss_mixer_reccontrols(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000784{
Charles-François Natalia5293082011-06-11 18:58:24 +0200785 if (!_is_fd_valid(self->fd))
786 return NULL;
787
Greg Ward2d6f9a92002-12-31 02:54:43 +0000788 return _do_ioctl_1_internal(self->fd, args, "reccontrols",
Greg Wardb69bb3d2002-12-12 17:35:45 +0000789 SOUND_MIXER_READ_RECMASK);
Greg Ward3d9994d2002-12-11 15:12:01 +0000790}
791
792static PyObject *
Greg Warde7037662002-12-30 03:01:48 +0000793oss_mixer_get(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000794{
795 int channel, volume;
Guido van Rossum0741f802003-06-02 14:15:34 +0000796
Charles-François Natalia5293082011-06-11 18:58:24 +0200797 if (!_is_fd_valid(self->fd))
798 return NULL;
799
Greg Ward3d9994d2002-12-11 15:12:01 +0000800 /* Can't use _do_ioctl_1 because of encoded arg thingy. */
Greg Warde7037662002-12-30 03:01:48 +0000801 if (!PyArg_ParseTuple(args, "i:get", &channel))
Greg Wardad4d9b92002-12-30 03:02:22 +0000802 return NULL;
Guido van Rossum0741f802003-06-02 14:15:34 +0000803
Greg Ward3d9994d2002-12-11 15:12:01 +0000804 if (channel < 0 || channel > SOUND_MIXER_NRDEVICES) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000805 PyErr_SetString(OSSAudioError, "Invalid mixer channel specified.");
806 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000807 }
Guido van Rossum0741f802003-06-02 14:15:34 +0000808
Greg Warde7037662002-12-30 03:01:48 +0000809 if (ioctl(self->fd, MIXER_READ(channel), &volume) == -1)
Greg Wardad4d9b92002-12-30 03:02:22 +0000810 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum0741f802003-06-02 14:15:34 +0000811
Greg Warde7037662002-12-30 03:01:48 +0000812 return Py_BuildValue("(ii)", volume & 0xff, (volume & 0xff00) >> 8);
Greg Ward3d9994d2002-12-11 15:12:01 +0000813}
814
815static PyObject *
Greg Warde7037662002-12-30 03:01:48 +0000816oss_mixer_set(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000817{
818 int channel, volume, leftVol, rightVol;
Guido van Rossum0741f802003-06-02 14:15:34 +0000819
Charles-François Natalia5293082011-06-11 18:58:24 +0200820 if (!_is_fd_valid(self->fd))
821 return NULL;
822
Greg Ward3d9994d2002-12-11 15:12:01 +0000823 /* Can't use _do_ioctl_1 because of encoded arg thingy. */
Greg Warde7037662002-12-30 03:01:48 +0000824 if (!PyArg_ParseTuple(args, "i(ii):set", &channel, &leftVol, &rightVol))
Greg Wardad4d9b92002-12-30 03:02:22 +0000825 return NULL;
Guido van Rossum0741f802003-06-02 14:15:34 +0000826
Greg Ward3d9994d2002-12-11 15:12:01 +0000827 if (channel < 0 || channel > SOUND_MIXER_NRDEVICES) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000828 PyErr_SetString(OSSAudioError, "Invalid mixer channel specified.");
829 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000830 }
Guido van Rossum0741f802003-06-02 14:15:34 +0000831
Greg Ward3d9994d2002-12-11 15:12:01 +0000832 if (leftVol < 0 || rightVol < 0 || leftVol > 100 || rightVol > 100) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000833 PyErr_SetString(OSSAudioError, "Volumes must be between 0 and 100.");
834 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000835 }
836
837 volume = (rightVol << 8) | leftVol;
Guido van Rossum0741f802003-06-02 14:15:34 +0000838
Greg Warde7037662002-12-30 03:01:48 +0000839 if (ioctl(self->fd, MIXER_WRITE(channel), &volume) == -1)
Greg Wardad4d9b92002-12-30 03:02:22 +0000840 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum0741f802003-06-02 14:15:34 +0000841
Greg Warde7037662002-12-30 03:01:48 +0000842 return Py_BuildValue("(ii)", volume & 0xff, (volume & 0xff00) >> 8);
Greg Ward3d9994d2002-12-11 15:12:01 +0000843}
844
845static PyObject *
Greg Wardf05aa102002-12-30 23:19:32 +0000846oss_mixer_get_recsrc(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000847{
Charles-François Natalia5293082011-06-11 18:58:24 +0200848 if (!_is_fd_valid(self->fd))
849 return NULL;
850
Greg Wardf05aa102002-12-30 23:19:32 +0000851 return _do_ioctl_1_internal(self->fd, args, "get_recsrc",
Greg Ward3d9994d2002-12-11 15:12:01 +0000852 SOUND_MIXER_READ_RECSRC);
853}
854
855static PyObject *
Greg Wardf05aa102002-12-30 23:19:32 +0000856oss_mixer_set_recsrc(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000857{
Charles-François Natalia5293082011-06-11 18:58:24 +0200858 if (!_is_fd_valid(self->fd))
859 return NULL;
860
Greg Wardf05aa102002-12-30 23:19:32 +0000861 return _do_ioctl_1(self->fd, args, "set_recsrc",
Greg Ward3d9994d2002-12-11 15:12:01 +0000862 SOUND_MIXER_WRITE_RECSRC);
863}
864
865
Greg Wardda1cacb2002-12-31 03:02:23 +0000866/* ----------------------------------------------------------------------
867 * Method tables and other bureaucracy
868 */
869
Greg Ward8c6b6a92002-12-11 14:43:13 +0000870static PyMethodDef oss_methods[] = {
Greg Ward131bce02002-11-30 22:56:44 +0000871 /* Regular file methods */
Greg Wardad4d9b92002-12-30 03:02:22 +0000872 { "read", (PyCFunction)oss_read, METH_VARARGS },
873 { "write", (PyCFunction)oss_write, METH_VARARGS },
874 { "writeall", (PyCFunction)oss_writeall, METH_VARARGS },
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000875 { "close", (PyCFunction)oss_close, METH_NOARGS },
876 { "fileno", (PyCFunction)oss_fileno, METH_NOARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000877
878 /* Simple ioctl wrappers */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000879 { "nonblock", (PyCFunction)oss_nonblock, METH_NOARGS },
Greg Wardad4d9b92002-12-30 03:02:22 +0000880 { "setfmt", (PyCFunction)oss_setfmt, METH_VARARGS },
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000881 { "getfmts", (PyCFunction)oss_getfmts, METH_NOARGS },
Greg Ward8c6b6a92002-12-11 14:43:13 +0000882 { "channels", (PyCFunction)oss_channels, METH_VARARGS },
883 { "speed", (PyCFunction)oss_speed, METH_VARARGS },
Greg Wardad4d9b92002-12-30 03:02:22 +0000884 { "sync", (PyCFunction)oss_sync, METH_VARARGS },
885 { "reset", (PyCFunction)oss_reset, METH_VARARGS },
886 { "post", (PyCFunction)oss_post, METH_VARARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000887
888 /* Convenience methods -- wrap a couple of ioctls together */
Greg Wardad4d9b92002-12-30 03:02:22 +0000889 { "setparameters", (PyCFunction)oss_setparameters, METH_VARARGS },
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000890 { "bufsize", (PyCFunction)oss_bufsize, METH_NOARGS },
891 { "obufcount", (PyCFunction)oss_obufcount, METH_NOARGS },
892 { "obuffree", (PyCFunction)oss_obuffree, METH_NOARGS },
893 { "getptr", (PyCFunction)oss_getptr, METH_NOARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000894
895 /* Aliases for backwards compatibility */
Greg Wardad4d9b92002-12-30 03:02:22 +0000896 { "flush", (PyCFunction)oss_sync, METH_VARARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000897
Georg Brandl1e908af2010-10-23 17:31:52 +0000898 /* Support for the context manager protocol */
Georg Brandl770a2be2010-10-24 20:47:32 +0000899 { "__enter__", oss_self, METH_NOARGS },
900 { "__exit__", oss_exit, METH_VARARGS },
Georg Brandl1e908af2010-10-23 17:31:52 +0000901
Greg Wardad4d9b92002-12-30 03:02:22 +0000902 { NULL, NULL} /* sentinel */
Greg Ward04613a92002-11-30 22:47:45 +0000903};
904
Greg Ward3d9994d2002-12-11 15:12:01 +0000905static PyMethodDef oss_mixer_methods[] = {
906 /* Regular file method - OSS mixers are ioctl-only interface */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000907 { "close", (PyCFunction)oss_mixer_close, METH_NOARGS },
908 { "fileno", (PyCFunction)oss_mixer_fileno, METH_NOARGS },
Greg Ward3d9994d2002-12-11 15:12:01 +0000909
Georg Brandl1e908af2010-10-23 17:31:52 +0000910 /* Support for the context manager protocol */
Georg Brandl770a2be2010-10-24 20:47:32 +0000911 { "__enter__", oss_self, METH_NOARGS },
912 { "__exit__", oss_exit, METH_VARARGS },
Georg Brandl1e908af2010-10-23 17:31:52 +0000913
Greg Ward3d9994d2002-12-11 15:12:01 +0000914 /* Simple ioctl wrappers */
Guido van Rossum0741f802003-06-02 14:15:34 +0000915 { "controls", (PyCFunction)oss_mixer_controls, METH_VARARGS },
Greg Ward2d6f9a92002-12-31 02:54:43 +0000916 { "stereocontrols", (PyCFunction)oss_mixer_stereocontrols, METH_VARARGS},
Guido van Rossum0741f802003-06-02 14:15:34 +0000917 { "reccontrols", (PyCFunction)oss_mixer_reccontrols, METH_VARARGS},
Greg Wardad4d9b92002-12-30 03:02:22 +0000918 { "get", (PyCFunction)oss_mixer_get, METH_VARARGS },
919 { "set", (PyCFunction)oss_mixer_set, METH_VARARGS },
Greg Wardf05aa102002-12-30 23:19:32 +0000920 { "get_recsrc", (PyCFunction)oss_mixer_get_recsrc, METH_VARARGS },
921 { "set_recsrc", (PyCFunction)oss_mixer_set_recsrc, METH_VARARGS },
Guido van Rossum0741f802003-06-02 14:15:34 +0000922
Greg Wardad4d9b92002-12-30 03:02:22 +0000923 { NULL, NULL}
Greg Ward3d9994d2002-12-11 15:12:01 +0000924};
925
Greg Ward04613a92002-11-30 22:47:45 +0000926static PyObject *
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000927oss_getattro(oss_audio_t *self, PyObject *nameobj)
Greg Ward04613a92002-11-30 22:47:45 +0000928{
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000929 char *name = "";
Greg Ward50682d02005-03-07 01:41:11 +0000930 PyObject * rval = NULL;
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000931
932 if (PyUnicode_Check(nameobj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 name = _PyUnicode_AsString(nameobj);
934
Greg Ward50682d02005-03-07 01:41:11 +0000935 if (strcmp(name, "closed") == 0) {
936 rval = (self->fd == -1) ? Py_True : Py_False;
937 Py_INCREF(rval);
938 }
939 else if (strcmp(name, "name") == 0) {
Neal Norwitz8abd2e62007-08-26 04:57:08 +0000940 rval = PyUnicode_FromString(self->devicename);
Greg Ward50682d02005-03-07 01:41:11 +0000941 }
942 else if (strcmp(name, "mode") == 0) {
943 /* No need for a "default" in this switch: from newossobject(),
944 self->mode can only be one of these three values. */
945 switch(self->mode) {
946 case O_RDONLY:
Neal Norwitz8abd2e62007-08-26 04:57:08 +0000947 rval = PyUnicode_FromString("r");
Greg Ward50682d02005-03-07 01:41:11 +0000948 break;
949 case O_RDWR:
Neal Norwitz8abd2e62007-08-26 04:57:08 +0000950 rval = PyUnicode_FromString("rw");
Greg Ward50682d02005-03-07 01:41:11 +0000951 break;
952 case O_WRONLY:
Neal Norwitz8abd2e62007-08-26 04:57:08 +0000953 rval = PyUnicode_FromString("w");
Greg Ward50682d02005-03-07 01:41:11 +0000954 break;
955 }
956 }
957 else {
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000958 rval = PyObject_GenericGetAttr((PyObject *)self, nameobj);
Greg Ward50682d02005-03-07 01:41:11 +0000959 }
960 return rval;
Greg Ward04613a92002-11-30 22:47:45 +0000961}
962
Greg Ward499b73e2002-12-31 03:04:52 +0000963static PyTypeObject OSSAudioType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000964 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Greg Ward9a568eb2002-11-30 23:20:09 +0000965 "ossaudiodev.oss_audio_device", /*tp_name*/
Greg Ward499b73e2002-12-31 03:04:52 +0000966 sizeof(oss_audio_t), /*tp_size*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000967 0, /*tp_itemsize*/
Greg Ward04613a92002-11-30 22:47:45 +0000968 /* methods */
Greg Wardad4d9b92002-12-30 03:02:22 +0000969 (destructor)oss_dealloc, /*tp_dealloc*/
970 0, /*tp_print*/
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000971 0, /*tp_getattr*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000972 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000973 0, /*tp_reserved*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000974 0, /*tp_repr*/
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000975 0, /*tp_as_number*/
976 0, /*tp_as_sequence*/
977 0, /*tp_as_mapping*/
978 0, /*tp_hash*/
979 0, /*tp_call*/
980 0, /*tp_str*/
981 (getattrofunc)oss_getattro, /*tp_getattro*/
982 0, /*tp_setattro*/
983 0, /*tp_as_buffer*/
984 Py_TPFLAGS_DEFAULT, /*tp_flags*/
985 0, /*tp_doc*/
986 0, /*tp_traverse*/
987 0, /*tp_clear*/
988 0, /*tp_richcompare*/
989 0, /*tp_weaklistoffset*/
990 0, /*tp_iter*/
991 0, /*tp_iternext*/
992 oss_methods, /*tp_methods*/
Greg Ward04613a92002-11-30 22:47:45 +0000993};
994
Greg Ward3d9994d2002-12-11 15:12:01 +0000995static PyTypeObject OSSMixerType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000996 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Greg Ward3d9994d2002-12-11 15:12:01 +0000997 "ossaudiodev.oss_mixer_device", /*tp_name*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000998 sizeof(oss_mixer_t), /*tp_size*/
999 0, /*tp_itemsize*/
Greg Ward3d9994d2002-12-11 15:12:01 +00001000 /* methods */
1001 (destructor)oss_mixer_dealloc, /*tp_dealloc*/
Greg Wardad4d9b92002-12-30 03:02:22 +00001002 0, /*tp_print*/
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +00001003 0, /*tp_getattr*/
Greg Wardad4d9b92002-12-30 03:02:22 +00001004 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001005 0, /*tp_reserved*/
Greg Wardad4d9b92002-12-30 03:02:22 +00001006 0, /*tp_repr*/
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +00001007 0, /*tp_as_number*/
1008 0, /*tp_as_sequence*/
1009 0, /*tp_as_mapping*/
1010 0, /*tp_hash*/
1011 0, /*tp_call*/
1012 0, /*tp_str*/
1013 0, /*tp_getattro*/
1014 0, /*tp_setattro*/
1015 0, /*tp_as_buffer*/
1016 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1017 0, /*tp_doc*/
1018 0, /*tp_traverse*/
1019 0, /*tp_clear*/
1020 0, /*tp_richcompare*/
1021 0, /*tp_weaklistoffset*/
1022 0, /*tp_iter*/
1023 0, /*tp_iternext*/
1024 oss_mixer_methods, /*tp_methods*/
Greg Ward3d9994d2002-12-11 15:12:01 +00001025};
1026
1027
Greg Ward04613a92002-11-30 22:47:45 +00001028static PyObject *
Greg Ward9a568eb2002-11-30 23:20:09 +00001029ossopen(PyObject *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +00001030{
Greg Ward8c6b6a92002-12-11 14:43:13 +00001031 return (PyObject *)newossobject(args);
Greg Ward04613a92002-11-30 22:47:45 +00001032}
1033
Greg Ward3d9994d2002-12-11 15:12:01 +00001034static PyObject *
1035ossopenmixer(PyObject *self, PyObject *args)
1036{
1037 return (PyObject *)newossmixerobject(args);
1038}
1039
Greg Ward9a568eb2002-11-30 23:20:09 +00001040static PyMethodDef ossaudiodev_methods[] = {
1041 { "open", ossopen, METH_VARARGS },
Greg Ward3d9994d2002-12-11 15:12:01 +00001042 { "openmixer", ossopenmixer, METH_VARARGS },
Greg Ward04613a92002-11-30 22:47:45 +00001043 { 0, 0 },
1044};
1045
Greg Ward1e0f57d2002-11-30 23:05:26 +00001046
1047#define _EXPORT_INT(mod, name) \
Martin v. Löwis1a214512008-06-11 05:26:20 +00001048 if (PyModule_AddIntConstant(mod, #name, (long) (name)) == -1) return NULL;
Greg Ward1e0f57d2002-11-30 23:05:26 +00001049
Greg Ward744f0fd2002-12-31 03:23:59 +00001050
1051static char *control_labels[] = SOUND_DEVICE_LABELS;
1052static char *control_names[] = SOUND_DEVICE_NAMES;
1053
1054
1055static int
1056build_namelists (PyObject *module)
1057{
1058 PyObject *labels;
1059 PyObject *names;
1060 PyObject *s;
1061 int num_controls;
1062 int i;
1063
1064 num_controls = sizeof(control_labels) / sizeof(control_labels[0]);
1065 assert(num_controls == sizeof(control_names) / sizeof(control_names[0]));
1066
1067 labels = PyList_New(num_controls);
1068 names = PyList_New(num_controls);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001069 if (labels == NULL || names == NULL)
1070 goto error2;
Greg Ward744f0fd2002-12-31 03:23:59 +00001071 for (i = 0; i < num_controls; i++) {
Neal Norwitz8abd2e62007-08-26 04:57:08 +00001072 s = PyUnicode_FromString(control_labels[i]);
Greg Ward744f0fd2002-12-31 03:23:59 +00001073 if (s == NULL)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001074 goto error2;
Greg Ward744f0fd2002-12-31 03:23:59 +00001075 PyList_SET_ITEM(labels, i, s);
Guido van Rossum0741f802003-06-02 14:15:34 +00001076
Neal Norwitz8abd2e62007-08-26 04:57:08 +00001077 s = PyUnicode_FromString(control_names[i]);
Greg Ward744f0fd2002-12-31 03:23:59 +00001078 if (s == NULL)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001079 goto error2;
Greg Ward744f0fd2002-12-31 03:23:59 +00001080 PyList_SET_ITEM(names, i, s);
1081 }
1082
1083 if (PyModule_AddObject(module, "control_labels", labels) == -1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001084 goto error2;
Greg Ward744f0fd2002-12-31 03:23:59 +00001085 if (PyModule_AddObject(module, "control_names", names) == -1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001086 goto error1;
Greg Ward744f0fd2002-12-31 03:23:59 +00001087
1088 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001089
1090error2:
1091 Py_XDECREF(labels);
1092error1:
1093 Py_XDECREF(names);
1094 return -1;
Guido van Rossum0741f802003-06-02 14:15:34 +00001095}
Greg Ward744f0fd2002-12-31 03:23:59 +00001096
1097
Martin v. Löwis1a214512008-06-11 05:26:20 +00001098static struct PyModuleDef ossaudiodevmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 PyModuleDef_HEAD_INIT,
1100 "ossaudiodev",
1101 NULL,
1102 -1,
1103 ossaudiodev_methods,
1104 NULL,
1105 NULL,
1106 NULL,
1107 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001108};
1109
Antoine Pitrou39b35432010-03-23 00:25:54 +00001110PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001111PyInit_ossaudiodev(void)
Greg Ward04613a92002-11-30 22:47:45 +00001112{
1113 PyObject *m;
Guido van Rossum0741f802003-06-02 14:15:34 +00001114
Antoine Pitrou39b35432010-03-23 00:25:54 +00001115 if (PyType_Ready(&OSSAudioType) < 0)
1116 return NULL;
1117
1118 if (PyType_Ready(&OSSMixerType) < 0)
1119 return NULL;
1120
Martin v. Löwis1a214512008-06-11 05:26:20 +00001121 m = PyModule_Create(&ossaudiodevmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001122 if (m == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +00001124
Guido van Rossum0741f802003-06-02 14:15:34 +00001125 OSSAudioError = PyErr_NewException("ossaudiodev.OSSAudioError",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 NULL, NULL);
Greg Wardd6769062003-05-29 21:53:06 +00001127 if (OSSAudioError) {
Guido van Rossum0741f802003-06-02 14:15:34 +00001128 /* Each call to PyModule_AddObject decrefs it; compensate: */
1129 Py_INCREF(OSSAudioError);
1130 Py_INCREF(OSSAudioError);
Greg Wardad4d9b92002-12-30 03:02:22 +00001131 PyModule_AddObject(m, "error", OSSAudioError);
Greg Wardd6769062003-05-29 21:53:06 +00001132 PyModule_AddObject(m, "OSSAudioError", OSSAudioError);
1133 }
Greg Ward04613a92002-11-30 22:47:45 +00001134
Greg Ward744f0fd2002-12-31 03:23:59 +00001135 /* Build 'control_labels' and 'control_names' lists and add them
1136 to the module. */
1137 if (build_namelists(m) == -1) /* XXX what to do here? */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001138 return NULL;
Greg Ward744f0fd2002-12-31 03:23:59 +00001139
Greg Ward1e0f57d2002-11-30 23:05:26 +00001140 /* Expose the audio format numbers -- essential! */
1141 _EXPORT_INT(m, AFMT_QUERY);
1142 _EXPORT_INT(m, AFMT_MU_LAW);
1143 _EXPORT_INT(m, AFMT_A_LAW);
1144 _EXPORT_INT(m, AFMT_IMA_ADPCM);
1145 _EXPORT_INT(m, AFMT_U8);
1146 _EXPORT_INT(m, AFMT_S16_LE);
1147 _EXPORT_INT(m, AFMT_S16_BE);
1148 _EXPORT_INT(m, AFMT_S8);
1149 _EXPORT_INT(m, AFMT_U16_LE);
1150 _EXPORT_INT(m, AFMT_U16_BE);
1151 _EXPORT_INT(m, AFMT_MPEG);
Andrew M. Kuchlingfe62bc92003-02-13 13:27:07 +00001152#ifdef AFMT_AC3
Greg Ward1e0f57d2002-11-30 23:05:26 +00001153 _EXPORT_INT(m, AFMT_AC3);
Andrew M. Kuchlingfe62bc92003-02-13 13:27:07 +00001154#endif
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001155#ifdef AFMT_S16_NE
Greg Ward1e0f57d2002-11-30 23:05:26 +00001156 _EXPORT_INT(m, AFMT_S16_NE);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001157#endif
Greg Ward0f260542005-03-28 02:40:46 +00001158#ifdef AFMT_U16_NE
1159 _EXPORT_INT(m, AFMT_U16_NE);
1160#endif
1161#ifdef AFMT_S32_LE
1162 _EXPORT_INT(m, AFMT_S32_LE);
1163#endif
1164#ifdef AFMT_S32_BE
1165 _EXPORT_INT(m, AFMT_S32_BE);
1166#endif
1167#ifdef AFMT_MPEG
1168 _EXPORT_INT(m, AFMT_MPEG);
1169#endif
Guido van Rossum0741f802003-06-02 14:15:34 +00001170
Greg Ward37897c22002-12-30 02:43:36 +00001171 /* Expose the sound mixer device numbers. */
Greg Ward7b43c682002-12-30 02:29:28 +00001172 _EXPORT_INT(m, SOUND_MIXER_NRDEVICES);
Greg Ward3d9994d2002-12-11 15:12:01 +00001173 _EXPORT_INT(m, SOUND_MIXER_VOLUME);
1174 _EXPORT_INT(m, SOUND_MIXER_BASS);
1175 _EXPORT_INT(m, SOUND_MIXER_TREBLE);
1176 _EXPORT_INT(m, SOUND_MIXER_SYNTH);
1177 _EXPORT_INT(m, SOUND_MIXER_PCM);
1178 _EXPORT_INT(m, SOUND_MIXER_SPEAKER);
1179 _EXPORT_INT(m, SOUND_MIXER_LINE);
1180 _EXPORT_INT(m, SOUND_MIXER_MIC);
1181 _EXPORT_INT(m, SOUND_MIXER_CD);
1182 _EXPORT_INT(m, SOUND_MIXER_IMIX);
1183 _EXPORT_INT(m, SOUND_MIXER_ALTPCM);
1184 _EXPORT_INT(m, SOUND_MIXER_RECLEV);
1185 _EXPORT_INT(m, SOUND_MIXER_IGAIN);
1186 _EXPORT_INT(m, SOUND_MIXER_OGAIN);
1187 _EXPORT_INT(m, SOUND_MIXER_LINE1);
1188 _EXPORT_INT(m, SOUND_MIXER_LINE2);
1189 _EXPORT_INT(m, SOUND_MIXER_LINE3);
Greg Wardfd0283e2004-05-11 01:34:55 +00001190#ifdef SOUND_MIXER_DIGITAL1
Greg Ward3d9994d2002-12-11 15:12:01 +00001191 _EXPORT_INT(m, SOUND_MIXER_DIGITAL1);
Greg Wardfd0283e2004-05-11 01:34:55 +00001192#endif
1193#ifdef SOUND_MIXER_DIGITAL2
Greg Ward3d9994d2002-12-11 15:12:01 +00001194 _EXPORT_INT(m, SOUND_MIXER_DIGITAL2);
Greg Wardfd0283e2004-05-11 01:34:55 +00001195#endif
1196#ifdef SOUND_MIXER_DIGITAL3
Greg Ward3d9994d2002-12-11 15:12:01 +00001197 _EXPORT_INT(m, SOUND_MIXER_DIGITAL3);
Greg Wardfd0283e2004-05-11 01:34:55 +00001198#endif
1199#ifdef SOUND_MIXER_PHONEIN
Greg Ward3d9994d2002-12-11 15:12:01 +00001200 _EXPORT_INT(m, SOUND_MIXER_PHONEIN);
Greg Wardfd0283e2004-05-11 01:34:55 +00001201#endif
1202#ifdef SOUND_MIXER_PHONEOUT
Greg Ward3d9994d2002-12-11 15:12:01 +00001203 _EXPORT_INT(m, SOUND_MIXER_PHONEOUT);
Greg Wardfd0283e2004-05-11 01:34:55 +00001204#endif
1205#ifdef SOUND_MIXER_VIDEO
Greg Ward3d9994d2002-12-11 15:12:01 +00001206 _EXPORT_INT(m, SOUND_MIXER_VIDEO);
Greg Wardfd0283e2004-05-11 01:34:55 +00001207#endif
1208#ifdef SOUND_MIXER_RADIO
Greg Ward3d9994d2002-12-11 15:12:01 +00001209 _EXPORT_INT(m, SOUND_MIXER_RADIO);
Greg Wardfd0283e2004-05-11 01:34:55 +00001210#endif
1211#ifdef SOUND_MIXER_MONITOR
Greg Ward3d9994d2002-12-11 15:12:01 +00001212 _EXPORT_INT(m, SOUND_MIXER_MONITOR);
Greg Wardfd0283e2004-05-11 01:34:55 +00001213#endif
Greg Ward04613a92002-11-30 22:47:45 +00001214
Greg Ward1e0f57d2002-11-30 23:05:26 +00001215 /* Expose all the ioctl numbers for masochists who like to do this
1216 stuff directly. */
1217 _EXPORT_INT(m, SNDCTL_COPR_HALT);
1218 _EXPORT_INT(m, SNDCTL_COPR_LOAD);
1219 _EXPORT_INT(m, SNDCTL_COPR_RCODE);
1220 _EXPORT_INT(m, SNDCTL_COPR_RCVMSG);
1221 _EXPORT_INT(m, SNDCTL_COPR_RDATA);
1222 _EXPORT_INT(m, SNDCTL_COPR_RESET);
1223 _EXPORT_INT(m, SNDCTL_COPR_RUN);
1224 _EXPORT_INT(m, SNDCTL_COPR_SENDMSG);
1225 _EXPORT_INT(m, SNDCTL_COPR_WCODE);
1226 _EXPORT_INT(m, SNDCTL_COPR_WDATA);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001227#ifdef SNDCTL_DSP_BIND_CHANNEL
Greg Ward1e0f57d2002-11-30 23:05:26 +00001228 _EXPORT_INT(m, SNDCTL_DSP_BIND_CHANNEL);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001229#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001230 _EXPORT_INT(m, SNDCTL_DSP_CHANNELS);
1231 _EXPORT_INT(m, SNDCTL_DSP_GETBLKSIZE);
1232 _EXPORT_INT(m, SNDCTL_DSP_GETCAPS);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001233#ifdef SNDCTL_DSP_GETCHANNELMASK
Greg Ward1e0f57d2002-11-30 23:05:26 +00001234 _EXPORT_INT(m, SNDCTL_DSP_GETCHANNELMASK);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001235#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001236 _EXPORT_INT(m, SNDCTL_DSP_GETFMTS);
1237 _EXPORT_INT(m, SNDCTL_DSP_GETIPTR);
1238 _EXPORT_INT(m, SNDCTL_DSP_GETISPACE);
Greg Wardfd0283e2004-05-11 01:34:55 +00001239#ifdef SNDCTL_DSP_GETODELAY
Greg Ward1e0f57d2002-11-30 23:05:26 +00001240 _EXPORT_INT(m, SNDCTL_DSP_GETODELAY);
Greg Wardfd0283e2004-05-11 01:34:55 +00001241#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001242 _EXPORT_INT(m, SNDCTL_DSP_GETOPTR);
1243 _EXPORT_INT(m, SNDCTL_DSP_GETOSPACE);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001244#ifdef SNDCTL_DSP_GETSPDIF
Greg Ward1e0f57d2002-11-30 23:05:26 +00001245 _EXPORT_INT(m, SNDCTL_DSP_GETSPDIF);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001246#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001247 _EXPORT_INT(m, SNDCTL_DSP_GETTRIGGER);
1248 _EXPORT_INT(m, SNDCTL_DSP_MAPINBUF);
1249 _EXPORT_INT(m, SNDCTL_DSP_MAPOUTBUF);
1250 _EXPORT_INT(m, SNDCTL_DSP_NONBLOCK);
1251 _EXPORT_INT(m, SNDCTL_DSP_POST);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001252#ifdef SNDCTL_DSP_PROFILE
Greg Ward1e0f57d2002-11-30 23:05:26 +00001253 _EXPORT_INT(m, SNDCTL_DSP_PROFILE);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001254#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001255 _EXPORT_INT(m, SNDCTL_DSP_RESET);
1256 _EXPORT_INT(m, SNDCTL_DSP_SAMPLESIZE);
1257 _EXPORT_INT(m, SNDCTL_DSP_SETDUPLEX);
1258 _EXPORT_INT(m, SNDCTL_DSP_SETFMT);
1259 _EXPORT_INT(m, SNDCTL_DSP_SETFRAGMENT);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001260#ifdef SNDCTL_DSP_SETSPDIF
Greg Ward1e0f57d2002-11-30 23:05:26 +00001261 _EXPORT_INT(m, SNDCTL_DSP_SETSPDIF);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001262#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001263 _EXPORT_INT(m, SNDCTL_DSP_SETSYNCRO);
1264 _EXPORT_INT(m, SNDCTL_DSP_SETTRIGGER);
1265 _EXPORT_INT(m, SNDCTL_DSP_SPEED);
1266 _EXPORT_INT(m, SNDCTL_DSP_STEREO);
1267 _EXPORT_INT(m, SNDCTL_DSP_SUBDIVIDE);
1268 _EXPORT_INT(m, SNDCTL_DSP_SYNC);
1269 _EXPORT_INT(m, SNDCTL_FM_4OP_ENABLE);
1270 _EXPORT_INT(m, SNDCTL_FM_LOAD_INSTR);
1271 _EXPORT_INT(m, SNDCTL_MIDI_INFO);
1272 _EXPORT_INT(m, SNDCTL_MIDI_MPUCMD);
1273 _EXPORT_INT(m, SNDCTL_MIDI_MPUMODE);
1274 _EXPORT_INT(m, SNDCTL_MIDI_PRETIME);
1275 _EXPORT_INT(m, SNDCTL_SEQ_CTRLRATE);
1276 _EXPORT_INT(m, SNDCTL_SEQ_GETINCOUNT);
1277 _EXPORT_INT(m, SNDCTL_SEQ_GETOUTCOUNT);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001278#ifdef SNDCTL_SEQ_GETTIME
Greg Ward1e0f57d2002-11-30 23:05:26 +00001279 _EXPORT_INT(m, SNDCTL_SEQ_GETTIME);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001280#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001281 _EXPORT_INT(m, SNDCTL_SEQ_NRMIDIS);
1282 _EXPORT_INT(m, SNDCTL_SEQ_NRSYNTHS);
1283 _EXPORT_INT(m, SNDCTL_SEQ_OUTOFBAND);
1284 _EXPORT_INT(m, SNDCTL_SEQ_PANIC);
1285 _EXPORT_INT(m, SNDCTL_SEQ_PERCMODE);
1286 _EXPORT_INT(m, SNDCTL_SEQ_RESET);
1287 _EXPORT_INT(m, SNDCTL_SEQ_RESETSAMPLES);
1288 _EXPORT_INT(m, SNDCTL_SEQ_SYNC);
1289 _EXPORT_INT(m, SNDCTL_SEQ_TESTMIDI);
1290 _EXPORT_INT(m, SNDCTL_SEQ_THRESHOLD);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001291#ifdef SNDCTL_SYNTH_CONTROL
Greg Ward1e0f57d2002-11-30 23:05:26 +00001292 _EXPORT_INT(m, SNDCTL_SYNTH_CONTROL);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001293#endif
1294#ifdef SNDCTL_SYNTH_ID
Greg Ward1e0f57d2002-11-30 23:05:26 +00001295 _EXPORT_INT(m, SNDCTL_SYNTH_ID);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001296#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001297 _EXPORT_INT(m, SNDCTL_SYNTH_INFO);
1298 _EXPORT_INT(m, SNDCTL_SYNTH_MEMAVL);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001299#ifdef SNDCTL_SYNTH_REMOVESAMPLE
Greg Ward1e0f57d2002-11-30 23:05:26 +00001300 _EXPORT_INT(m, SNDCTL_SYNTH_REMOVESAMPLE);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001301#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001302 _EXPORT_INT(m, SNDCTL_TMR_CONTINUE);
1303 _EXPORT_INT(m, SNDCTL_TMR_METRONOME);
1304 _EXPORT_INT(m, SNDCTL_TMR_SELECT);
1305 _EXPORT_INT(m, SNDCTL_TMR_SOURCE);
1306 _EXPORT_INT(m, SNDCTL_TMR_START);
1307 _EXPORT_INT(m, SNDCTL_TMR_STOP);
1308 _EXPORT_INT(m, SNDCTL_TMR_TEMPO);
1309 _EXPORT_INT(m, SNDCTL_TMR_TIMEBASE);
Martin v. Löwis1a214512008-06-11 05:26:20 +00001310 return m;
Greg Ward04613a92002-11-30 22:47:45 +00001311}