blob: 15057314497ba8d07c1147edb697b28ce03da78c [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
477 /* use select to wait for audio device to be available */
478 FD_ZERO(&write_set_fds);
Greg Ward7b43c682002-12-30 02:29:28 +0000479 FD_SET(self->fd, &write_set_fds);
Greg Ward04613a92002-11-30 22:47:45 +0000480
481 while (size > 0) {
Greg Ward64927852003-05-23 01:50:37 +0000482 Py_BEGIN_ALLOW_THREADS
Greg Ward7b43c682002-12-30 02:29:28 +0000483 select_rv = select(self->fd+1, NULL, &write_set_fds, NULL, NULL);
Greg Ward64927852003-05-23 01:50:37 +0000484 Py_END_ALLOW_THREADS
Greg Ward131bce02002-11-30 22:56:44 +0000485 assert(select_rv != 0); /* no timeout, can't expire */
486 if (select_rv == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000487 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000488
Greg Ward64927852003-05-23 01:50:37 +0000489 Py_BEGIN_ALLOW_THREADS
Greg Ward7b43c682002-12-30 02:29:28 +0000490 rv = write(self->fd, cp, size);
Greg Ward64927852003-05-23 01:50:37 +0000491 Py_END_ALLOW_THREADS
Greg Ward131bce02002-11-30 22:56:44 +0000492 if (rv == -1) {
493 if (errno == EAGAIN) { /* buffer is full, try again */
494 errno = 0;
495 continue;
496 } else /* it's a real error */
Greg Ward97708bc2002-11-30 23:17:10 +0000497 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000498 } else { /* wrote rv bytes */
Greg Ward7b43c682002-12-30 02:29:28 +0000499 self->ocount += rv;
Greg Ward131bce02002-11-30 22:56:44 +0000500 size -= rv;
501 cp += rv;
502 }
Greg Ward04613a92002-11-30 22:47:45 +0000503 }
504 Py_INCREF(Py_None);
505 return Py_None;
506}
507
508static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000509oss_close(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000510{
Greg Ward7b43c682002-12-30 02:29:28 +0000511 if (self->fd >= 0) {
Greg Wardb8043902003-05-26 22:47:30 +0000512 Py_BEGIN_ALLOW_THREADS
Greg Ward7b43c682002-12-30 02:29:28 +0000513 close(self->fd);
Greg Wardb8043902003-05-26 22:47:30 +0000514 Py_END_ALLOW_THREADS
Greg Ward7b43c682002-12-30 02:29:28 +0000515 self->fd = -1;
Greg Ward04613a92002-11-30 22:47:45 +0000516 }
517 Py_INCREF(Py_None);
518 return Py_None;
519}
520
521static PyObject *
Georg Brandl770a2be2010-10-24 20:47:32 +0000522oss_self(PyObject *self, PyObject *unused)
Georg Brandl1e908af2010-10-23 17:31:52 +0000523{
524 Py_INCREF(self);
525 return self;
526}
527
528static PyObject *
529oss_exit(PyObject *self, PyObject *unused)
530{
531 PyObject *ret = PyObject_CallMethod(self, "close", NULL);
532 if (!ret)
533 return NULL;
534 Py_DECREF(ret);
535 Py_RETURN_NONE;
536}
537
538static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000539oss_fileno(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000540{
Charles-François Natalia5293082011-06-11 18:58:24 +0200541 if (!_is_fd_valid(self->fd))
542 return NULL;
543
Christian Heimes217cfd12007-12-02 14:31:20 +0000544 return PyLong_FromLong(self->fd);
Greg Ward04613a92002-11-30 22:47:45 +0000545}
546
Greg Ward131bce02002-11-30 22:56:44 +0000547
548/* Convenience methods: these generally wrap a couple of ioctls into one
549 common task. */
550
Greg Ward04613a92002-11-30 22:47:45 +0000551static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000552oss_setparameters(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000553{
Greg Wardd6769062003-05-29 21:53:06 +0000554 int wanted_fmt, wanted_channels, wanted_rate, strict=0;
555 int fmt, channels, rate;
556 PyObject * rv; /* return tuple (fmt, channels, rate) */
Greg Ward04613a92002-11-30 22:47:45 +0000557
Charles-François Natalia5293082011-06-11 18:58:24 +0200558 if (!_is_fd_valid(self->fd))
559 return NULL;
560
Greg Wardd6769062003-05-29 21:53:06 +0000561 if (!PyArg_ParseTuple(args, "iii|i:setparameters",
562 &wanted_fmt, &wanted_channels, &wanted_rate,
563 &strict))
Greg Ward04613a92002-11-30 22:47:45 +0000564 return NULL;
Greg Wardd6769062003-05-29 21:53:06 +0000565
566 fmt = wanted_fmt;
567 if (ioctl(self->fd, SNDCTL_DSP_SETFMT, &fmt) == -1) {
568 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000569 }
Greg Wardd6769062003-05-29 21:53:06 +0000570 if (strict && fmt != wanted_fmt) {
571 return PyErr_Format
572 (OSSAudioError,
573 "unable to set requested format (wanted %d, got %d)",
574 wanted_fmt, fmt);
Greg Ward04613a92002-11-30 22:47:45 +0000575 }
576
Greg Wardd6769062003-05-29 21:53:06 +0000577 channels = wanted_channels;
578 if (ioctl(self->fd, SNDCTL_DSP_CHANNELS, &channels) == -1) {
579 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000580 }
Greg Wardd6769062003-05-29 21:53:06 +0000581 if (strict && channels != wanted_channels) {
582 return PyErr_Format
583 (OSSAudioError,
584 "unable to set requested channels (wanted %d, got %d)",
585 wanted_channels, channels);
Greg Ward04613a92002-11-30 22:47:45 +0000586 }
587
Greg Wardd6769062003-05-29 21:53:06 +0000588 rate = wanted_rate;
Greg Ward7b43c682002-12-30 02:29:28 +0000589 if (ioctl(self->fd, SNDCTL_DSP_SPEED, &rate) == -1) {
Greg Wardd6769062003-05-29 21:53:06 +0000590 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000591 }
Greg Wardd6769062003-05-29 21:53:06 +0000592 if (strict && rate != wanted_rate) {
593 return PyErr_Format
594 (OSSAudioError,
595 "unable to set requested rate (wanted %d, got %d)",
596 wanted_rate, rate);
597 }
Guido van Rossum0741f802003-06-02 14:15:34 +0000598
Greg Wardd6769062003-05-29 21:53:06 +0000599 /* Construct the return value: a (fmt, channels, rate) tuple that
600 tells what the audio hardware was actually set to. */
601 rv = PyTuple_New(3);
602 if (rv == NULL)
603 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +0000604 PyTuple_SET_ITEM(rv, 0, PyLong_FromLong(fmt));
605 PyTuple_SET_ITEM(rv, 1, PyLong_FromLong(channels));
606 PyTuple_SET_ITEM(rv, 2, PyLong_FromLong(rate));
Greg Wardd6769062003-05-29 21:53:06 +0000607 return rv;
Greg Ward04613a92002-11-30 22:47:45 +0000608}
609
610static int
Greg Ward499b73e2002-12-31 03:04:52 +0000611_ssize(oss_audio_t *self, int *nchannels, int *ssize)
Greg Ward04613a92002-11-30 22:47:45 +0000612{
613 int fmt;
614
615 fmt = 0;
Guido van Rossum0741f802003-06-02 14:15:34 +0000616 if (ioctl(self->fd, SNDCTL_DSP_SETFMT, &fmt) < 0)
Greg Ward04613a92002-11-30 22:47:45 +0000617 return -errno;
618
619 switch (fmt) {
620 case AFMT_MU_LAW:
621 case AFMT_A_LAW:
622 case AFMT_U8:
623 case AFMT_S8:
Greg Ward38c92662003-05-29 21:55:41 +0000624 *ssize = 1; /* 8 bit formats: 1 byte */
Greg Ward04613a92002-11-30 22:47:45 +0000625 break;
626 case AFMT_S16_LE:
627 case AFMT_S16_BE:
628 case AFMT_U16_LE:
629 case AFMT_U16_BE:
Greg Ward84f8ecd2003-05-29 23:44:44 +0000630 *ssize = 2; /* 16 bit formats: 2 byte */
Greg Ward04613a92002-11-30 22:47:45 +0000631 break;
632 case AFMT_MPEG:
633 case AFMT_IMA_ADPCM:
634 default:
635 return -EOPNOTSUPP;
636 }
Greg Ward7b43c682002-12-30 02:29:28 +0000637 if (ioctl(self->fd, SNDCTL_DSP_CHANNELS, nchannels) < 0)
Greg Ward04613a92002-11-30 22:47:45 +0000638 return -errno;
639 return 0;
640}
641
642
Guido van Rossum0741f802003-06-02 14:15:34 +0000643/* bufsize returns the size of the hardware audio buffer in number
Greg Ward04613a92002-11-30 22:47:45 +0000644 of samples */
645static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000646oss_bufsize(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000647{
648 audio_buf_info ai;
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000649 int nchannels=0, ssize=0;
Greg Ward04613a92002-11-30 22:47:45 +0000650
Charles-François Natalia5293082011-06-11 18:58:24 +0200651 if (!_is_fd_valid(self->fd))
652 return NULL;
653
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000654 if (_ssize(self, &nchannels, &ssize) < 0 || !nchannels || !ssize) {
Greg Ward97708bc2002-11-30 23:17:10 +0000655 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000656 return NULL;
657 }
Greg Ward7b43c682002-12-30 02:29:28 +0000658 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000659 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000660 return NULL;
661 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000662 return PyLong_FromLong((ai.fragstotal * ai.fragsize) / (nchannels * ssize));
Greg Ward04613a92002-11-30 22:47:45 +0000663}
664
Guido van Rossum0741f802003-06-02 14:15:34 +0000665/* obufcount returns the number of samples that are available in the
Greg Ward04613a92002-11-30 22:47:45 +0000666 hardware for playing */
667static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000668oss_obufcount(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000669{
670 audio_buf_info ai;
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000671 int nchannels=0, ssize=0;
Greg Ward04613a92002-11-30 22:47:45 +0000672
Charles-François Natalia5293082011-06-11 18:58:24 +0200673 if (!_is_fd_valid(self->fd))
674 return NULL;
675
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000676 if (_ssize(self, &nchannels, &ssize) < 0 || !nchannels || !ssize) {
Greg Ward97708bc2002-11-30 23:17:10 +0000677 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000678 return NULL;
679 }
Greg Ward7b43c682002-12-30 02:29:28 +0000680 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000681 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000682 return NULL;
683 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000684 return PyLong_FromLong((ai.fragstotal * ai.fragsize - ai.bytes) /
Greg Ward04613a92002-11-30 22:47:45 +0000685 (ssize * nchannels));
686}
687
688/* obufcount returns the number of samples that can be played without
689 blocking */
690static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000691oss_obuffree(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000692{
693 audio_buf_info ai;
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000694 int nchannels=0, ssize=0;
Greg Ward04613a92002-11-30 22:47:45 +0000695
Charles-François Natalia5293082011-06-11 18:58:24 +0200696 if (!_is_fd_valid(self->fd))
697 return NULL;
698
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000699 if (_ssize(self, &nchannels, &ssize) < 0 || !nchannels || !ssize) {
Greg Ward97708bc2002-11-30 23:17:10 +0000700 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000701 return NULL;
702 }
Greg Ward7b43c682002-12-30 02:29:28 +0000703 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000704 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000705 return NULL;
706 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000707 return PyLong_FromLong(ai.bytes / (ssize * nchannels));
Greg Ward04613a92002-11-30 22:47:45 +0000708}
709
Greg Ward04613a92002-11-30 22:47:45 +0000710static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000711oss_getptr(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000712{
713 count_info info;
714 int req;
715
Charles-François Natalia5293082011-06-11 18:58:24 +0200716 if (!_is_fd_valid(self->fd))
717 return NULL;
718
Greg Ward7b43c682002-12-30 02:29:28 +0000719 if (self->mode == O_RDONLY)
Greg Wardad4d9b92002-12-30 03:02:22 +0000720 req = SNDCTL_DSP_GETIPTR;
Greg Ward04613a92002-11-30 22:47:45 +0000721 else
Greg Wardad4d9b92002-12-30 03:02:22 +0000722 req = SNDCTL_DSP_GETOPTR;
Greg Ward7b43c682002-12-30 02:29:28 +0000723 if (ioctl(self->fd, req, &info) == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000724 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000725 return NULL;
726 }
727 return Py_BuildValue("iii", info.bytes, info.blocks, info.ptr);
728}
729
Greg Wardda1cacb2002-12-31 03:02:23 +0000730
731/* ----------------------------------------------------------------------
732 * Methods of mixer objects (OSSMixerType)
733 */
734
Greg Ward3d9994d2002-12-11 15:12:01 +0000735static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000736oss_mixer_close(oss_mixer_t *self, PyObject *unused)
Greg Ward3d9994d2002-12-11 15:12:01 +0000737{
Greg Ward7b43c682002-12-30 02:29:28 +0000738 if (self->fd >= 0) {
739 close(self->fd);
740 self->fd = -1;
Greg Ward3d9994d2002-12-11 15:12:01 +0000741 }
742 Py_INCREF(Py_None);
743 return Py_None;
744}
745
746static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000747oss_mixer_fileno(oss_mixer_t *self, PyObject *unused)
Greg Ward3d9994d2002-12-11 15:12:01 +0000748{
Charles-François Natalia5293082011-06-11 18:58:24 +0200749 if (!_is_fd_valid(self->fd))
750 return NULL;
751
Christian Heimes217cfd12007-12-02 14:31:20 +0000752 return PyLong_FromLong(self->fd);
Greg Ward3d9994d2002-12-11 15:12:01 +0000753}
754
755/* Simple mixer interface methods */
756
757static PyObject *
Greg Ward2d6f9a92002-12-31 02:54:43 +0000758oss_mixer_controls(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000759{
Charles-François Natalia5293082011-06-11 18:58:24 +0200760 if (!_is_fd_valid(self->fd))
761 return NULL;
762
Greg Ward2d6f9a92002-12-31 02:54:43 +0000763 return _do_ioctl_1_internal(self->fd, args, "controls",
Greg Ward3d9994d2002-12-11 15:12:01 +0000764 SOUND_MIXER_READ_DEVMASK);
765}
766
767static PyObject *
Greg Ward2d6f9a92002-12-31 02:54:43 +0000768oss_mixer_stereocontrols(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000769{
Charles-François Natalia5293082011-06-11 18:58:24 +0200770 if (!_is_fd_valid(self->fd))
771 return NULL;
772
Greg Ward2d6f9a92002-12-31 02:54:43 +0000773 return _do_ioctl_1_internal(self->fd, args, "stereocontrols",
Greg Ward3d9994d2002-12-11 15:12:01 +0000774 SOUND_MIXER_READ_STEREODEVS);
775}
776
777static PyObject *
Greg Ward2d6f9a92002-12-31 02:54:43 +0000778oss_mixer_reccontrols(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000779{
Charles-François Natalia5293082011-06-11 18:58:24 +0200780 if (!_is_fd_valid(self->fd))
781 return NULL;
782
Greg Ward2d6f9a92002-12-31 02:54:43 +0000783 return _do_ioctl_1_internal(self->fd, args, "reccontrols",
Greg Wardb69bb3d2002-12-12 17:35:45 +0000784 SOUND_MIXER_READ_RECMASK);
Greg Ward3d9994d2002-12-11 15:12:01 +0000785}
786
787static PyObject *
Greg Warde7037662002-12-30 03:01:48 +0000788oss_mixer_get(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000789{
790 int channel, volume;
Guido van Rossum0741f802003-06-02 14:15:34 +0000791
Charles-François Natalia5293082011-06-11 18:58:24 +0200792 if (!_is_fd_valid(self->fd))
793 return NULL;
794
Greg Ward3d9994d2002-12-11 15:12:01 +0000795 /* Can't use _do_ioctl_1 because of encoded arg thingy. */
Greg Warde7037662002-12-30 03:01:48 +0000796 if (!PyArg_ParseTuple(args, "i:get", &channel))
Greg Wardad4d9b92002-12-30 03:02:22 +0000797 return NULL;
Guido van Rossum0741f802003-06-02 14:15:34 +0000798
Greg Ward3d9994d2002-12-11 15:12:01 +0000799 if (channel < 0 || channel > SOUND_MIXER_NRDEVICES) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000800 PyErr_SetString(OSSAudioError, "Invalid mixer channel specified.");
801 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000802 }
Guido van Rossum0741f802003-06-02 14:15:34 +0000803
Greg Warde7037662002-12-30 03:01:48 +0000804 if (ioctl(self->fd, MIXER_READ(channel), &volume) == -1)
Greg Wardad4d9b92002-12-30 03:02:22 +0000805 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum0741f802003-06-02 14:15:34 +0000806
Greg Warde7037662002-12-30 03:01:48 +0000807 return Py_BuildValue("(ii)", volume & 0xff, (volume & 0xff00) >> 8);
Greg Ward3d9994d2002-12-11 15:12:01 +0000808}
809
810static PyObject *
Greg Warde7037662002-12-30 03:01:48 +0000811oss_mixer_set(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000812{
813 int channel, volume, leftVol, rightVol;
Guido van Rossum0741f802003-06-02 14:15:34 +0000814
Charles-François Natalia5293082011-06-11 18:58:24 +0200815 if (!_is_fd_valid(self->fd))
816 return NULL;
817
Greg Ward3d9994d2002-12-11 15:12:01 +0000818 /* Can't use _do_ioctl_1 because of encoded arg thingy. */
Greg Warde7037662002-12-30 03:01:48 +0000819 if (!PyArg_ParseTuple(args, "i(ii):set", &channel, &leftVol, &rightVol))
Greg Wardad4d9b92002-12-30 03:02:22 +0000820 return NULL;
Guido van Rossum0741f802003-06-02 14:15:34 +0000821
Greg Ward3d9994d2002-12-11 15:12:01 +0000822 if (channel < 0 || channel > SOUND_MIXER_NRDEVICES) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000823 PyErr_SetString(OSSAudioError, "Invalid mixer channel specified.");
824 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000825 }
Guido van Rossum0741f802003-06-02 14:15:34 +0000826
Greg Ward3d9994d2002-12-11 15:12:01 +0000827 if (leftVol < 0 || rightVol < 0 || leftVol > 100 || rightVol > 100) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000828 PyErr_SetString(OSSAudioError, "Volumes must be between 0 and 100.");
829 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000830 }
831
832 volume = (rightVol << 8) | leftVol;
Guido van Rossum0741f802003-06-02 14:15:34 +0000833
Greg Warde7037662002-12-30 03:01:48 +0000834 if (ioctl(self->fd, MIXER_WRITE(channel), &volume) == -1)
Greg Wardad4d9b92002-12-30 03:02:22 +0000835 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum0741f802003-06-02 14:15:34 +0000836
Greg Warde7037662002-12-30 03:01:48 +0000837 return Py_BuildValue("(ii)", volume & 0xff, (volume & 0xff00) >> 8);
Greg Ward3d9994d2002-12-11 15:12:01 +0000838}
839
840static PyObject *
Greg Wardf05aa102002-12-30 23:19:32 +0000841oss_mixer_get_recsrc(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000842{
Charles-François Natalia5293082011-06-11 18:58:24 +0200843 if (!_is_fd_valid(self->fd))
844 return NULL;
845
Greg Wardf05aa102002-12-30 23:19:32 +0000846 return _do_ioctl_1_internal(self->fd, args, "get_recsrc",
Greg Ward3d9994d2002-12-11 15:12:01 +0000847 SOUND_MIXER_READ_RECSRC);
848}
849
850static PyObject *
Greg Wardf05aa102002-12-30 23:19:32 +0000851oss_mixer_set_recsrc(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000852{
Charles-François Natalia5293082011-06-11 18:58:24 +0200853 if (!_is_fd_valid(self->fd))
854 return NULL;
855
Greg Wardf05aa102002-12-30 23:19:32 +0000856 return _do_ioctl_1(self->fd, args, "set_recsrc",
Greg Ward3d9994d2002-12-11 15:12:01 +0000857 SOUND_MIXER_WRITE_RECSRC);
858}
859
860
Greg Wardda1cacb2002-12-31 03:02:23 +0000861/* ----------------------------------------------------------------------
862 * Method tables and other bureaucracy
863 */
864
Greg Ward8c6b6a92002-12-11 14:43:13 +0000865static PyMethodDef oss_methods[] = {
Greg Ward131bce02002-11-30 22:56:44 +0000866 /* Regular file methods */
Greg Wardad4d9b92002-12-30 03:02:22 +0000867 { "read", (PyCFunction)oss_read, METH_VARARGS },
868 { "write", (PyCFunction)oss_write, METH_VARARGS },
869 { "writeall", (PyCFunction)oss_writeall, METH_VARARGS },
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000870 { "close", (PyCFunction)oss_close, METH_NOARGS },
871 { "fileno", (PyCFunction)oss_fileno, METH_NOARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000872
873 /* Simple ioctl wrappers */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000874 { "nonblock", (PyCFunction)oss_nonblock, METH_NOARGS },
Greg Wardad4d9b92002-12-30 03:02:22 +0000875 { "setfmt", (PyCFunction)oss_setfmt, METH_VARARGS },
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000876 { "getfmts", (PyCFunction)oss_getfmts, METH_NOARGS },
Greg Ward8c6b6a92002-12-11 14:43:13 +0000877 { "channels", (PyCFunction)oss_channels, METH_VARARGS },
878 { "speed", (PyCFunction)oss_speed, METH_VARARGS },
Greg Wardad4d9b92002-12-30 03:02:22 +0000879 { "sync", (PyCFunction)oss_sync, METH_VARARGS },
880 { "reset", (PyCFunction)oss_reset, METH_VARARGS },
881 { "post", (PyCFunction)oss_post, METH_VARARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000882
883 /* Convenience methods -- wrap a couple of ioctls together */
Greg Wardad4d9b92002-12-30 03:02:22 +0000884 { "setparameters", (PyCFunction)oss_setparameters, METH_VARARGS },
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000885 { "bufsize", (PyCFunction)oss_bufsize, METH_NOARGS },
886 { "obufcount", (PyCFunction)oss_obufcount, METH_NOARGS },
887 { "obuffree", (PyCFunction)oss_obuffree, METH_NOARGS },
888 { "getptr", (PyCFunction)oss_getptr, METH_NOARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000889
890 /* Aliases for backwards compatibility */
Greg Wardad4d9b92002-12-30 03:02:22 +0000891 { "flush", (PyCFunction)oss_sync, METH_VARARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000892
Georg Brandl1e908af2010-10-23 17:31:52 +0000893 /* Support for the context manager protocol */
Georg Brandl770a2be2010-10-24 20:47:32 +0000894 { "__enter__", oss_self, METH_NOARGS },
895 { "__exit__", oss_exit, METH_VARARGS },
Georg Brandl1e908af2010-10-23 17:31:52 +0000896
Greg Wardad4d9b92002-12-30 03:02:22 +0000897 { NULL, NULL} /* sentinel */
Greg Ward04613a92002-11-30 22:47:45 +0000898};
899
Greg Ward3d9994d2002-12-11 15:12:01 +0000900static PyMethodDef oss_mixer_methods[] = {
901 /* Regular file method - OSS mixers are ioctl-only interface */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000902 { "close", (PyCFunction)oss_mixer_close, METH_NOARGS },
903 { "fileno", (PyCFunction)oss_mixer_fileno, METH_NOARGS },
Greg Ward3d9994d2002-12-11 15:12:01 +0000904
Georg Brandl1e908af2010-10-23 17:31:52 +0000905 /* Support for the context manager protocol */
Georg Brandl770a2be2010-10-24 20:47:32 +0000906 { "__enter__", oss_self, METH_NOARGS },
907 { "__exit__", oss_exit, METH_VARARGS },
Georg Brandl1e908af2010-10-23 17:31:52 +0000908
Greg Ward3d9994d2002-12-11 15:12:01 +0000909 /* Simple ioctl wrappers */
Guido van Rossum0741f802003-06-02 14:15:34 +0000910 { "controls", (PyCFunction)oss_mixer_controls, METH_VARARGS },
Greg Ward2d6f9a92002-12-31 02:54:43 +0000911 { "stereocontrols", (PyCFunction)oss_mixer_stereocontrols, METH_VARARGS},
Guido van Rossum0741f802003-06-02 14:15:34 +0000912 { "reccontrols", (PyCFunction)oss_mixer_reccontrols, METH_VARARGS},
Greg Wardad4d9b92002-12-30 03:02:22 +0000913 { "get", (PyCFunction)oss_mixer_get, METH_VARARGS },
914 { "set", (PyCFunction)oss_mixer_set, METH_VARARGS },
Greg Wardf05aa102002-12-30 23:19:32 +0000915 { "get_recsrc", (PyCFunction)oss_mixer_get_recsrc, METH_VARARGS },
916 { "set_recsrc", (PyCFunction)oss_mixer_set_recsrc, METH_VARARGS },
Guido van Rossum0741f802003-06-02 14:15:34 +0000917
Greg Wardad4d9b92002-12-30 03:02:22 +0000918 { NULL, NULL}
Greg Ward3d9994d2002-12-11 15:12:01 +0000919};
920
Greg Ward04613a92002-11-30 22:47:45 +0000921static PyObject *
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000922oss_getattro(oss_audio_t *self, PyObject *nameobj)
Greg Ward04613a92002-11-30 22:47:45 +0000923{
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000924 char *name = "";
Greg Ward50682d02005-03-07 01:41:11 +0000925 PyObject * rval = NULL;
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000926
927 if (PyUnicode_Check(nameobj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 name = _PyUnicode_AsString(nameobj);
929
Greg Ward50682d02005-03-07 01:41:11 +0000930 if (strcmp(name, "closed") == 0) {
931 rval = (self->fd == -1) ? Py_True : Py_False;
932 Py_INCREF(rval);
933 }
934 else if (strcmp(name, "name") == 0) {
Neal Norwitz8abd2e62007-08-26 04:57:08 +0000935 rval = PyUnicode_FromString(self->devicename);
Greg Ward50682d02005-03-07 01:41:11 +0000936 }
937 else if (strcmp(name, "mode") == 0) {
938 /* No need for a "default" in this switch: from newossobject(),
939 self->mode can only be one of these three values. */
940 switch(self->mode) {
941 case O_RDONLY:
Neal Norwitz8abd2e62007-08-26 04:57:08 +0000942 rval = PyUnicode_FromString("r");
Greg Ward50682d02005-03-07 01:41:11 +0000943 break;
944 case O_RDWR:
Neal Norwitz8abd2e62007-08-26 04:57:08 +0000945 rval = PyUnicode_FromString("rw");
Greg Ward50682d02005-03-07 01:41:11 +0000946 break;
947 case O_WRONLY:
Neal Norwitz8abd2e62007-08-26 04:57:08 +0000948 rval = PyUnicode_FromString("w");
Greg Ward50682d02005-03-07 01:41:11 +0000949 break;
950 }
951 }
952 else {
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000953 rval = PyObject_GenericGetAttr((PyObject *)self, nameobj);
Greg Ward50682d02005-03-07 01:41:11 +0000954 }
955 return rval;
Greg Ward04613a92002-11-30 22:47:45 +0000956}
957
Greg Ward499b73e2002-12-31 03:04:52 +0000958static PyTypeObject OSSAudioType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000959 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Greg Ward9a568eb2002-11-30 23:20:09 +0000960 "ossaudiodev.oss_audio_device", /*tp_name*/
Greg Ward499b73e2002-12-31 03:04:52 +0000961 sizeof(oss_audio_t), /*tp_size*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000962 0, /*tp_itemsize*/
Greg Ward04613a92002-11-30 22:47:45 +0000963 /* methods */
Greg Wardad4d9b92002-12-30 03:02:22 +0000964 (destructor)oss_dealloc, /*tp_dealloc*/
965 0, /*tp_print*/
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000966 0, /*tp_getattr*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000967 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000968 0, /*tp_reserved*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000969 0, /*tp_repr*/
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000970 0, /*tp_as_number*/
971 0, /*tp_as_sequence*/
972 0, /*tp_as_mapping*/
973 0, /*tp_hash*/
974 0, /*tp_call*/
975 0, /*tp_str*/
976 (getattrofunc)oss_getattro, /*tp_getattro*/
977 0, /*tp_setattro*/
978 0, /*tp_as_buffer*/
979 Py_TPFLAGS_DEFAULT, /*tp_flags*/
980 0, /*tp_doc*/
981 0, /*tp_traverse*/
982 0, /*tp_clear*/
983 0, /*tp_richcompare*/
984 0, /*tp_weaklistoffset*/
985 0, /*tp_iter*/
986 0, /*tp_iternext*/
987 oss_methods, /*tp_methods*/
Greg Ward04613a92002-11-30 22:47:45 +0000988};
989
Greg Ward3d9994d2002-12-11 15:12:01 +0000990static PyTypeObject OSSMixerType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000991 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Greg Ward3d9994d2002-12-11 15:12:01 +0000992 "ossaudiodev.oss_mixer_device", /*tp_name*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000993 sizeof(oss_mixer_t), /*tp_size*/
994 0, /*tp_itemsize*/
Greg Ward3d9994d2002-12-11 15:12:01 +0000995 /* methods */
996 (destructor)oss_mixer_dealloc, /*tp_dealloc*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000997 0, /*tp_print*/
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000998 0, /*tp_getattr*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000999 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001000 0, /*tp_reserved*/
Greg Wardad4d9b92002-12-30 03:02:22 +00001001 0, /*tp_repr*/
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +00001002 0, /*tp_as_number*/
1003 0, /*tp_as_sequence*/
1004 0, /*tp_as_mapping*/
1005 0, /*tp_hash*/
1006 0, /*tp_call*/
1007 0, /*tp_str*/
1008 0, /*tp_getattro*/
1009 0, /*tp_setattro*/
1010 0, /*tp_as_buffer*/
1011 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1012 0, /*tp_doc*/
1013 0, /*tp_traverse*/
1014 0, /*tp_clear*/
1015 0, /*tp_richcompare*/
1016 0, /*tp_weaklistoffset*/
1017 0, /*tp_iter*/
1018 0, /*tp_iternext*/
1019 oss_mixer_methods, /*tp_methods*/
Greg Ward3d9994d2002-12-11 15:12:01 +00001020};
1021
1022
Greg Ward04613a92002-11-30 22:47:45 +00001023static PyObject *
Greg Ward9a568eb2002-11-30 23:20:09 +00001024ossopen(PyObject *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +00001025{
Greg Ward8c6b6a92002-12-11 14:43:13 +00001026 return (PyObject *)newossobject(args);
Greg Ward04613a92002-11-30 22:47:45 +00001027}
1028
Greg Ward3d9994d2002-12-11 15:12:01 +00001029static PyObject *
1030ossopenmixer(PyObject *self, PyObject *args)
1031{
1032 return (PyObject *)newossmixerobject(args);
1033}
1034
Greg Ward9a568eb2002-11-30 23:20:09 +00001035static PyMethodDef ossaudiodev_methods[] = {
1036 { "open", ossopen, METH_VARARGS },
Greg Ward3d9994d2002-12-11 15:12:01 +00001037 { "openmixer", ossopenmixer, METH_VARARGS },
Greg Ward04613a92002-11-30 22:47:45 +00001038 { 0, 0 },
1039};
1040
Greg Ward1e0f57d2002-11-30 23:05:26 +00001041
1042#define _EXPORT_INT(mod, name) \
Martin v. Löwis1a214512008-06-11 05:26:20 +00001043 if (PyModule_AddIntConstant(mod, #name, (long) (name)) == -1) return NULL;
Greg Ward1e0f57d2002-11-30 23:05:26 +00001044
Greg Ward744f0fd2002-12-31 03:23:59 +00001045
1046static char *control_labels[] = SOUND_DEVICE_LABELS;
1047static char *control_names[] = SOUND_DEVICE_NAMES;
1048
1049
1050static int
1051build_namelists (PyObject *module)
1052{
1053 PyObject *labels;
1054 PyObject *names;
1055 PyObject *s;
1056 int num_controls;
1057 int i;
1058
1059 num_controls = sizeof(control_labels) / sizeof(control_labels[0]);
1060 assert(num_controls == sizeof(control_names) / sizeof(control_names[0]));
1061
1062 labels = PyList_New(num_controls);
1063 names = PyList_New(num_controls);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001064 if (labels == NULL || names == NULL)
1065 goto error2;
Greg Ward744f0fd2002-12-31 03:23:59 +00001066 for (i = 0; i < num_controls; i++) {
Neal Norwitz8abd2e62007-08-26 04:57:08 +00001067 s = PyUnicode_FromString(control_labels[i]);
Greg Ward744f0fd2002-12-31 03:23:59 +00001068 if (s == NULL)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001069 goto error2;
Greg Ward744f0fd2002-12-31 03:23:59 +00001070 PyList_SET_ITEM(labels, i, s);
Guido van Rossum0741f802003-06-02 14:15:34 +00001071
Neal Norwitz8abd2e62007-08-26 04:57:08 +00001072 s = PyUnicode_FromString(control_names[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(names, i, s);
1076 }
1077
1078 if (PyModule_AddObject(module, "control_labels", labels) == -1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001079 goto error2;
Greg Ward744f0fd2002-12-31 03:23:59 +00001080 if (PyModule_AddObject(module, "control_names", names) == -1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001081 goto error1;
Greg Ward744f0fd2002-12-31 03:23:59 +00001082
1083 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001084
1085error2:
1086 Py_XDECREF(labels);
1087error1:
1088 Py_XDECREF(names);
1089 return -1;
Guido van Rossum0741f802003-06-02 14:15:34 +00001090}
Greg Ward744f0fd2002-12-31 03:23:59 +00001091
1092
Martin v. Löwis1a214512008-06-11 05:26:20 +00001093static struct PyModuleDef ossaudiodevmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 PyModuleDef_HEAD_INIT,
1095 "ossaudiodev",
1096 NULL,
1097 -1,
1098 ossaudiodev_methods,
1099 NULL,
1100 NULL,
1101 NULL,
1102 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001103};
1104
Antoine Pitrou39b35432010-03-23 00:25:54 +00001105PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001106PyInit_ossaudiodev(void)
Greg Ward04613a92002-11-30 22:47:45 +00001107{
1108 PyObject *m;
Guido van Rossum0741f802003-06-02 14:15:34 +00001109
Antoine Pitrou39b35432010-03-23 00:25:54 +00001110 if (PyType_Ready(&OSSAudioType) < 0)
1111 return NULL;
1112
1113 if (PyType_Ready(&OSSMixerType) < 0)
1114 return NULL;
1115
Martin v. Löwis1a214512008-06-11 05:26:20 +00001116 m = PyModule_Create(&ossaudiodevmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001117 if (m == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +00001119
Guido van Rossum0741f802003-06-02 14:15:34 +00001120 OSSAudioError = PyErr_NewException("ossaudiodev.OSSAudioError",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 NULL, NULL);
Greg Wardd6769062003-05-29 21:53:06 +00001122 if (OSSAudioError) {
Guido van Rossum0741f802003-06-02 14:15:34 +00001123 /* Each call to PyModule_AddObject decrefs it; compensate: */
1124 Py_INCREF(OSSAudioError);
1125 Py_INCREF(OSSAudioError);
Greg Wardad4d9b92002-12-30 03:02:22 +00001126 PyModule_AddObject(m, "error", OSSAudioError);
Greg Wardd6769062003-05-29 21:53:06 +00001127 PyModule_AddObject(m, "OSSAudioError", OSSAudioError);
1128 }
Greg Ward04613a92002-11-30 22:47:45 +00001129
Greg Ward744f0fd2002-12-31 03:23:59 +00001130 /* Build 'control_labels' and 'control_names' lists and add them
1131 to the module. */
1132 if (build_namelists(m) == -1) /* XXX what to do here? */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001133 return NULL;
Greg Ward744f0fd2002-12-31 03:23:59 +00001134
Greg Ward1e0f57d2002-11-30 23:05:26 +00001135 /* Expose the audio format numbers -- essential! */
1136 _EXPORT_INT(m, AFMT_QUERY);
1137 _EXPORT_INT(m, AFMT_MU_LAW);
1138 _EXPORT_INT(m, AFMT_A_LAW);
1139 _EXPORT_INT(m, AFMT_IMA_ADPCM);
1140 _EXPORT_INT(m, AFMT_U8);
1141 _EXPORT_INT(m, AFMT_S16_LE);
1142 _EXPORT_INT(m, AFMT_S16_BE);
1143 _EXPORT_INT(m, AFMT_S8);
1144 _EXPORT_INT(m, AFMT_U16_LE);
1145 _EXPORT_INT(m, AFMT_U16_BE);
1146 _EXPORT_INT(m, AFMT_MPEG);
Andrew M. Kuchlingfe62bc92003-02-13 13:27:07 +00001147#ifdef AFMT_AC3
Greg Ward1e0f57d2002-11-30 23:05:26 +00001148 _EXPORT_INT(m, AFMT_AC3);
Andrew M. Kuchlingfe62bc92003-02-13 13:27:07 +00001149#endif
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001150#ifdef AFMT_S16_NE
Greg Ward1e0f57d2002-11-30 23:05:26 +00001151 _EXPORT_INT(m, AFMT_S16_NE);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001152#endif
Greg Ward0f260542005-03-28 02:40:46 +00001153#ifdef AFMT_U16_NE
1154 _EXPORT_INT(m, AFMT_U16_NE);
1155#endif
1156#ifdef AFMT_S32_LE
1157 _EXPORT_INT(m, AFMT_S32_LE);
1158#endif
1159#ifdef AFMT_S32_BE
1160 _EXPORT_INT(m, AFMT_S32_BE);
1161#endif
1162#ifdef AFMT_MPEG
1163 _EXPORT_INT(m, AFMT_MPEG);
1164#endif
Guido van Rossum0741f802003-06-02 14:15:34 +00001165
Greg Ward37897c22002-12-30 02:43:36 +00001166 /* Expose the sound mixer device numbers. */
Greg Ward7b43c682002-12-30 02:29:28 +00001167 _EXPORT_INT(m, SOUND_MIXER_NRDEVICES);
Greg Ward3d9994d2002-12-11 15:12:01 +00001168 _EXPORT_INT(m, SOUND_MIXER_VOLUME);
1169 _EXPORT_INT(m, SOUND_MIXER_BASS);
1170 _EXPORT_INT(m, SOUND_MIXER_TREBLE);
1171 _EXPORT_INT(m, SOUND_MIXER_SYNTH);
1172 _EXPORT_INT(m, SOUND_MIXER_PCM);
1173 _EXPORT_INT(m, SOUND_MIXER_SPEAKER);
1174 _EXPORT_INT(m, SOUND_MIXER_LINE);
1175 _EXPORT_INT(m, SOUND_MIXER_MIC);
1176 _EXPORT_INT(m, SOUND_MIXER_CD);
1177 _EXPORT_INT(m, SOUND_MIXER_IMIX);
1178 _EXPORT_INT(m, SOUND_MIXER_ALTPCM);
1179 _EXPORT_INT(m, SOUND_MIXER_RECLEV);
1180 _EXPORT_INT(m, SOUND_MIXER_IGAIN);
1181 _EXPORT_INT(m, SOUND_MIXER_OGAIN);
1182 _EXPORT_INT(m, SOUND_MIXER_LINE1);
1183 _EXPORT_INT(m, SOUND_MIXER_LINE2);
1184 _EXPORT_INT(m, SOUND_MIXER_LINE3);
Greg Wardfd0283e2004-05-11 01:34:55 +00001185#ifdef SOUND_MIXER_DIGITAL1
Greg Ward3d9994d2002-12-11 15:12:01 +00001186 _EXPORT_INT(m, SOUND_MIXER_DIGITAL1);
Greg Wardfd0283e2004-05-11 01:34:55 +00001187#endif
1188#ifdef SOUND_MIXER_DIGITAL2
Greg Ward3d9994d2002-12-11 15:12:01 +00001189 _EXPORT_INT(m, SOUND_MIXER_DIGITAL2);
Greg Wardfd0283e2004-05-11 01:34:55 +00001190#endif
1191#ifdef SOUND_MIXER_DIGITAL3
Greg Ward3d9994d2002-12-11 15:12:01 +00001192 _EXPORT_INT(m, SOUND_MIXER_DIGITAL3);
Greg Wardfd0283e2004-05-11 01:34:55 +00001193#endif
1194#ifdef SOUND_MIXER_PHONEIN
Greg Ward3d9994d2002-12-11 15:12:01 +00001195 _EXPORT_INT(m, SOUND_MIXER_PHONEIN);
Greg Wardfd0283e2004-05-11 01:34:55 +00001196#endif
1197#ifdef SOUND_MIXER_PHONEOUT
Greg Ward3d9994d2002-12-11 15:12:01 +00001198 _EXPORT_INT(m, SOUND_MIXER_PHONEOUT);
Greg Wardfd0283e2004-05-11 01:34:55 +00001199#endif
1200#ifdef SOUND_MIXER_VIDEO
Greg Ward3d9994d2002-12-11 15:12:01 +00001201 _EXPORT_INT(m, SOUND_MIXER_VIDEO);
Greg Wardfd0283e2004-05-11 01:34:55 +00001202#endif
1203#ifdef SOUND_MIXER_RADIO
Greg Ward3d9994d2002-12-11 15:12:01 +00001204 _EXPORT_INT(m, SOUND_MIXER_RADIO);
Greg Wardfd0283e2004-05-11 01:34:55 +00001205#endif
1206#ifdef SOUND_MIXER_MONITOR
Greg Ward3d9994d2002-12-11 15:12:01 +00001207 _EXPORT_INT(m, SOUND_MIXER_MONITOR);
Greg Wardfd0283e2004-05-11 01:34:55 +00001208#endif
Greg Ward04613a92002-11-30 22:47:45 +00001209
Greg Ward1e0f57d2002-11-30 23:05:26 +00001210 /* Expose all the ioctl numbers for masochists who like to do this
1211 stuff directly. */
1212 _EXPORT_INT(m, SNDCTL_COPR_HALT);
1213 _EXPORT_INT(m, SNDCTL_COPR_LOAD);
1214 _EXPORT_INT(m, SNDCTL_COPR_RCODE);
1215 _EXPORT_INT(m, SNDCTL_COPR_RCVMSG);
1216 _EXPORT_INT(m, SNDCTL_COPR_RDATA);
1217 _EXPORT_INT(m, SNDCTL_COPR_RESET);
1218 _EXPORT_INT(m, SNDCTL_COPR_RUN);
1219 _EXPORT_INT(m, SNDCTL_COPR_SENDMSG);
1220 _EXPORT_INT(m, SNDCTL_COPR_WCODE);
1221 _EXPORT_INT(m, SNDCTL_COPR_WDATA);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001222#ifdef SNDCTL_DSP_BIND_CHANNEL
Greg Ward1e0f57d2002-11-30 23:05:26 +00001223 _EXPORT_INT(m, SNDCTL_DSP_BIND_CHANNEL);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001224#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001225 _EXPORT_INT(m, SNDCTL_DSP_CHANNELS);
1226 _EXPORT_INT(m, SNDCTL_DSP_GETBLKSIZE);
1227 _EXPORT_INT(m, SNDCTL_DSP_GETCAPS);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001228#ifdef SNDCTL_DSP_GETCHANNELMASK
Greg Ward1e0f57d2002-11-30 23:05:26 +00001229 _EXPORT_INT(m, SNDCTL_DSP_GETCHANNELMASK);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001230#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001231 _EXPORT_INT(m, SNDCTL_DSP_GETFMTS);
1232 _EXPORT_INT(m, SNDCTL_DSP_GETIPTR);
1233 _EXPORT_INT(m, SNDCTL_DSP_GETISPACE);
Greg Wardfd0283e2004-05-11 01:34:55 +00001234#ifdef SNDCTL_DSP_GETODELAY
Greg Ward1e0f57d2002-11-30 23:05:26 +00001235 _EXPORT_INT(m, SNDCTL_DSP_GETODELAY);
Greg Wardfd0283e2004-05-11 01:34:55 +00001236#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001237 _EXPORT_INT(m, SNDCTL_DSP_GETOPTR);
1238 _EXPORT_INT(m, SNDCTL_DSP_GETOSPACE);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001239#ifdef SNDCTL_DSP_GETSPDIF
Greg Ward1e0f57d2002-11-30 23:05:26 +00001240 _EXPORT_INT(m, SNDCTL_DSP_GETSPDIF);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001241#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001242 _EXPORT_INT(m, SNDCTL_DSP_GETTRIGGER);
1243 _EXPORT_INT(m, SNDCTL_DSP_MAPINBUF);
1244 _EXPORT_INT(m, SNDCTL_DSP_MAPOUTBUF);
1245 _EXPORT_INT(m, SNDCTL_DSP_NONBLOCK);
1246 _EXPORT_INT(m, SNDCTL_DSP_POST);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001247#ifdef SNDCTL_DSP_PROFILE
Greg Ward1e0f57d2002-11-30 23:05:26 +00001248 _EXPORT_INT(m, SNDCTL_DSP_PROFILE);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001249#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001250 _EXPORT_INT(m, SNDCTL_DSP_RESET);
1251 _EXPORT_INT(m, SNDCTL_DSP_SAMPLESIZE);
1252 _EXPORT_INT(m, SNDCTL_DSP_SETDUPLEX);
1253 _EXPORT_INT(m, SNDCTL_DSP_SETFMT);
1254 _EXPORT_INT(m, SNDCTL_DSP_SETFRAGMENT);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001255#ifdef SNDCTL_DSP_SETSPDIF
Greg Ward1e0f57d2002-11-30 23:05:26 +00001256 _EXPORT_INT(m, SNDCTL_DSP_SETSPDIF);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001257#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001258 _EXPORT_INT(m, SNDCTL_DSP_SETSYNCRO);
1259 _EXPORT_INT(m, SNDCTL_DSP_SETTRIGGER);
1260 _EXPORT_INT(m, SNDCTL_DSP_SPEED);
1261 _EXPORT_INT(m, SNDCTL_DSP_STEREO);
1262 _EXPORT_INT(m, SNDCTL_DSP_SUBDIVIDE);
1263 _EXPORT_INT(m, SNDCTL_DSP_SYNC);
1264 _EXPORT_INT(m, SNDCTL_FM_4OP_ENABLE);
1265 _EXPORT_INT(m, SNDCTL_FM_LOAD_INSTR);
1266 _EXPORT_INT(m, SNDCTL_MIDI_INFO);
1267 _EXPORT_INT(m, SNDCTL_MIDI_MPUCMD);
1268 _EXPORT_INT(m, SNDCTL_MIDI_MPUMODE);
1269 _EXPORT_INT(m, SNDCTL_MIDI_PRETIME);
1270 _EXPORT_INT(m, SNDCTL_SEQ_CTRLRATE);
1271 _EXPORT_INT(m, SNDCTL_SEQ_GETINCOUNT);
1272 _EXPORT_INT(m, SNDCTL_SEQ_GETOUTCOUNT);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001273#ifdef SNDCTL_SEQ_GETTIME
Greg Ward1e0f57d2002-11-30 23:05:26 +00001274 _EXPORT_INT(m, SNDCTL_SEQ_GETTIME);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001275#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001276 _EXPORT_INT(m, SNDCTL_SEQ_NRMIDIS);
1277 _EXPORT_INT(m, SNDCTL_SEQ_NRSYNTHS);
1278 _EXPORT_INT(m, SNDCTL_SEQ_OUTOFBAND);
1279 _EXPORT_INT(m, SNDCTL_SEQ_PANIC);
1280 _EXPORT_INT(m, SNDCTL_SEQ_PERCMODE);
1281 _EXPORT_INT(m, SNDCTL_SEQ_RESET);
1282 _EXPORT_INT(m, SNDCTL_SEQ_RESETSAMPLES);
1283 _EXPORT_INT(m, SNDCTL_SEQ_SYNC);
1284 _EXPORT_INT(m, SNDCTL_SEQ_TESTMIDI);
1285 _EXPORT_INT(m, SNDCTL_SEQ_THRESHOLD);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001286#ifdef SNDCTL_SYNTH_CONTROL
Greg Ward1e0f57d2002-11-30 23:05:26 +00001287 _EXPORT_INT(m, SNDCTL_SYNTH_CONTROL);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001288#endif
1289#ifdef SNDCTL_SYNTH_ID
Greg Ward1e0f57d2002-11-30 23:05:26 +00001290 _EXPORT_INT(m, SNDCTL_SYNTH_ID);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001291#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001292 _EXPORT_INT(m, SNDCTL_SYNTH_INFO);
1293 _EXPORT_INT(m, SNDCTL_SYNTH_MEMAVL);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001294#ifdef SNDCTL_SYNTH_REMOVESAMPLE
Greg Ward1e0f57d2002-11-30 23:05:26 +00001295 _EXPORT_INT(m, SNDCTL_SYNTH_REMOVESAMPLE);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001296#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001297 _EXPORT_INT(m, SNDCTL_TMR_CONTINUE);
1298 _EXPORT_INT(m, SNDCTL_TMR_METRONOME);
1299 _EXPORT_INT(m, SNDCTL_TMR_SELECT);
1300 _EXPORT_INT(m, SNDCTL_TMR_SOURCE);
1301 _EXPORT_INT(m, SNDCTL_TMR_START);
1302 _EXPORT_INT(m, SNDCTL_TMR_STOP);
1303 _EXPORT_INT(m, SNDCTL_TMR_TEMPO);
1304 _EXPORT_INT(m, SNDCTL_TMR_TIMEBASE);
Martin v. Löwis1a214512008-06-11 05:26:20 +00001305 return m;
Greg Ward04613a92002-11-30 22:47:45 +00001306}