blob: 34e68c7ad467a8484a074db825544ae5c5e90e39 [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. */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200118 fd = _Py_open(devicename, imode|O_NONBLOCK);
Victor Stinnera555cfc2015-03-18 00:22:14 +0100119 if (fd == -1)
Greg Ward04613a92002-11-30 22:47:45 +0000120 return NULL;
Greg Ward76ffb192003-04-04 01:47:42 +0000121
122 /* And (try to) put it back in blocking mode so we get the
123 expected write() semantics. */
124 if (fcntl(fd, F_SETFL, 0) == -1) {
125 close(fd);
Greg Ward50682d02005-03-07 01:41:11 +0000126 PyErr_SetFromErrnoWithFilename(PyExc_IOError, devicename);
Greg Ward76ffb192003-04-04 01:47:42 +0000127 return NULL;
128 }
129
Greg Ward04613a92002-11-30 22:47:45 +0000130 if (ioctl(fd, SNDCTL_DSP_GETFMTS, &afmts) == -1) {
Charles-François Natali5a4a1092011-09-29 19:46:37 +0200131 close(fd);
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
Victor Stinnerdaf45552013-08-28 00:53:59 +0200179 fd = _Py_open(devicename, O_RDWR);
Victor Stinnera555cfc2015-03-18 00:22:14 +0100180 if (fd == -1)
Greg Ward3d9994d2002-12-11 15:12:01 +0000181 return NULL;
Greg Ward0b6dfb82003-03-10 03:17:06 +0000182
Greg Ward58ae13c2002-12-31 03:07:21 +0000183 if ((self = PyObject_New(oss_mixer_t, &OSSMixerType)) == NULL) {
Greg Ward3d9994d2002-12-11 15:12:01 +0000184 close(fd);
185 return NULL;
186 }
Guido van Rossum0741f802003-06-02 14:15:34 +0000187
Greg Ward58ae13c2002-12-31 03:07:21 +0000188 self->fd = fd;
Guido van Rossum0741f802003-06-02 14:15:34 +0000189
Greg Ward58ae13c2002-12-31 03:07:21 +0000190 return self;
Greg Ward3d9994d2002-12-11 15:12:01 +0000191}
192
193static void
Greg Ward58ae13c2002-12-31 03:07:21 +0000194oss_mixer_dealloc(oss_mixer_t *self)
Greg Ward3d9994d2002-12-11 15:12:01 +0000195{
196 /* if already closed, don't reclose it */
Greg Ward58ae13c2002-12-31 03:07:21 +0000197 if (self->fd != -1)
198 close(self->fd);
199 PyObject_Del(self);
Greg Ward3d9994d2002-12-11 15:12:01 +0000200}
201
Greg Ward131bce02002-11-30 22:56:44 +0000202
203/* Methods to wrap the OSS ioctls. The calling convention is pretty
204 simple:
205 nonblock() -> ioctl(fd, SNDCTL_DSP_NONBLOCK)
206 fmt = setfmt(fmt) -> ioctl(fd, SNDCTL_DSP_SETFMT, &fmt)
207 etc.
208*/
209
210
Greg Wardd6769062003-05-29 21:53:06 +0000211/* ----------------------------------------------------------------------
212 * Helper functions
213 */
214
Charles-François Natalia5293082011-06-11 18:58:24 +0200215/* Check if a given file descriptor is valid (i.e. hasn't been closed).
216 * If true, return 1. Otherwise, raise ValueError and return 0.
217 */
218static int _is_fd_valid(int fd)
219{
220 /* the FD is set to -1 in oss_close()/oss_mixer_close() */
221 if (fd >= 0) {
222 return 1;
223 } else {
224 PyErr_SetString(PyExc_ValueError,
225 "Operation on closed OSS device.");
226 return 0;
227 }
228}
229
Greg Ward131bce02002-11-30 22:56:44 +0000230/* _do_ioctl_1() is a private helper function used for the OSS ioctls --
Terry Jan Reedy0158af32013-03-11 17:42:46 -0400231 SNDCTL_DSP_{SETFMT,CHANNELS,SPEED} -- that are called from C
Greg Ward131bce02002-11-30 22:56:44 +0000232 like this:
233 ioctl(fd, SNDCTL_DSP_cmd, &arg)
234
235 where arg is the value to set, and on return the driver sets arg to
236 the value that was actually set. Mapping this to Python is obvious:
237 arg = dsp.xxx(arg)
238*/
239static PyObject *
Greg Wardda9f8532002-12-11 14:49:59 +0000240_do_ioctl_1(int fd, PyObject *args, char *fname, int cmd)
Greg Ward131bce02002-11-30 22:56:44 +0000241{
Greg Wardda9f8532002-12-11 14:49:59 +0000242 char argfmt[33] = "i:";
Greg Ward131bce02002-11-30 22:56:44 +0000243 int arg;
244
Greg Wardda9f8532002-12-11 14:49:59 +0000245 assert(strlen(fname) <= 30);
Christian Heimese91ad502013-07-20 14:11:28 +0200246 strncat(argfmt, fname, 30);
Greg Ward131bce02002-11-30 22:56:44 +0000247 if (!PyArg_ParseTuple(args, argfmt, &arg))
Greg Wardad4d9b92002-12-30 03:02:22 +0000248 return NULL;
Greg Ward131bce02002-11-30 22:56:44 +0000249
Greg Wardda9f8532002-12-11 14:49:59 +0000250 if (ioctl(fd, cmd, &arg) == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000251 return PyErr_SetFromErrno(PyExc_IOError);
Christian Heimes217cfd12007-12-02 14:31:20 +0000252 return PyLong_FromLong(arg);
Greg Ward131bce02002-11-30 22:56:44 +0000253}
254
Greg Wardda1cacb2002-12-31 03:02:23 +0000255
Greg Ward3d9994d2002-12-11 15:12:01 +0000256/* _do_ioctl_1_internal() is a wrapper for ioctls that take no inputs
257 but return an output -- ie. we need to pass a pointer to a local C
258 variable so the driver can write its output there, but from Python
259 all we see is the return value. For example,
260 SOUND_MIXER_READ_DEVMASK returns a bitmask of available mixer
261 devices, but does not use the value of the parameter passed-in in any
262 way.
263*/
Greg Ward3d9994d2002-12-11 15:12:01 +0000264static PyObject *
265_do_ioctl_1_internal(int fd, PyObject *args, char *fname, int cmd)
266{
267 char argfmt[32] = ":";
268 int arg = 0;
269
270 assert(strlen(fname) <= 30);
Christian Heimese91ad502013-07-20 14:11:28 +0200271 strncat(argfmt, fname, 30);
Greg Ward3d9994d2002-12-11 15:12:01 +0000272 if (!PyArg_ParseTuple(args, argfmt, &arg))
Greg Wardad4d9b92002-12-30 03:02:22 +0000273 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000274
275 if (ioctl(fd, cmd, &arg) == -1)
276 return PyErr_SetFromErrno(PyExc_IOError);
Christian Heimes217cfd12007-12-02 14:31:20 +0000277 return PyLong_FromLong(arg);
Greg Ward3d9994d2002-12-11 15:12:01 +0000278}
279
280
281
Greg Ward131bce02002-11-30 22:56:44 +0000282/* _do_ioctl_0() is a private helper for the no-argument ioctls:
283 SNDCTL_DSP_{SYNC,RESET,POST}. */
284static PyObject *
Greg Wardda9f8532002-12-11 14:49:59 +0000285_do_ioctl_0(int fd, PyObject *args, char *fname, int cmd)
Greg Ward131bce02002-11-30 22:56:44 +0000286{
Greg Wardda9f8532002-12-11 14:49:59 +0000287 char argfmt[32] = ":";
Greg Wardd0d592f2003-05-27 01:57:21 +0000288 int rv;
Greg Ward131bce02002-11-30 22:56:44 +0000289
Greg Wardda9f8532002-12-11 14:49:59 +0000290 assert(strlen(fname) <= 30);
Christian Heimese91ad502013-07-20 14:11:28 +0200291 strncat(argfmt, fname, 30);
Greg Ward131bce02002-11-30 22:56:44 +0000292 if (!PyArg_ParseTuple(args, argfmt))
Greg Wardad4d9b92002-12-30 03:02:22 +0000293 return NULL;
Greg Ward131bce02002-11-30 22:56:44 +0000294
Greg Wardd0d592f2003-05-27 01:57:21 +0000295 /* According to hannu@opensound.com, all three of the ioctls that
296 use this function can block, so release the GIL. This is
297 especially important for SYNC, which can block for several
298 seconds. */
299 Py_BEGIN_ALLOW_THREADS
300 rv = ioctl(fd, cmd, 0);
301 Py_END_ALLOW_THREADS
302
303 if (rv == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000304 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000305 Py_INCREF(Py_None);
306 return Py_None;
307}
308
309
Greg Wardda1cacb2002-12-31 03:02:23 +0000310/* ----------------------------------------------------------------------
Greg Ward499b73e2002-12-31 03:04:52 +0000311 * Methods of DSP objects (OSSAudioType)
Greg Wardda1cacb2002-12-31 03:02:23 +0000312 */
313
Greg Ward131bce02002-11-30 22:56:44 +0000314static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000315oss_nonblock(oss_audio_t *self, PyObject *unused)
Greg Ward131bce02002-11-30 22:56:44 +0000316{
Charles-François Natalia5293082011-06-11 18:58:24 +0200317 if (!_is_fd_valid(self->fd))
318 return NULL;
319
Greg Ward131bce02002-11-30 22:56:44 +0000320 /* Hmmm: it doesn't appear to be possible to return to blocking
321 mode once we're in non-blocking mode! */
Greg Ward7b43c682002-12-30 02:29:28 +0000322 if (ioctl(self->fd, SNDCTL_DSP_NONBLOCK, NULL) == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000323 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000324 Py_INCREF(Py_None);
325 return Py_None;
326}
327
328static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000329oss_setfmt(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000330{
Charles-François Natalia5293082011-06-11 18:58:24 +0200331 if (!_is_fd_valid(self->fd))
332 return NULL;
333
Greg Ward7b43c682002-12-30 02:29:28 +0000334 return _do_ioctl_1(self->fd, args, "setfmt", SNDCTL_DSP_SETFMT);
Greg Ward131bce02002-11-30 22:56:44 +0000335}
336
337static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000338oss_getfmts(oss_audio_t *self, PyObject *unused)
Greg Ward131bce02002-11-30 22:56:44 +0000339{
340 int mask;
Charles-François Natalia5293082011-06-11 18:58:24 +0200341
342 if (!_is_fd_valid(self->fd))
343 return NULL;
344
Greg Ward7b43c682002-12-30 02:29:28 +0000345 if (ioctl(self->fd, SNDCTL_DSP_GETFMTS, &mask) == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000346 return PyErr_SetFromErrno(PyExc_IOError);
Christian Heimes217cfd12007-12-02 14:31:20 +0000347 return PyLong_FromLong(mask);
Greg Ward131bce02002-11-30 22:56:44 +0000348}
349
350static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000351oss_channels(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000352{
Charles-François Natalia5293082011-06-11 18:58:24 +0200353 if (!_is_fd_valid(self->fd))
354 return NULL;
355
Greg Ward7b43c682002-12-30 02:29:28 +0000356 return _do_ioctl_1(self->fd, args, "channels", SNDCTL_DSP_CHANNELS);
Greg Ward131bce02002-11-30 22:56:44 +0000357}
358
359static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000360oss_speed(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000361{
Charles-François Natalia5293082011-06-11 18:58:24 +0200362 if (!_is_fd_valid(self->fd))
363 return NULL;
364
Greg Ward7b43c682002-12-30 02:29:28 +0000365 return _do_ioctl_1(self->fd, args, "speed", SNDCTL_DSP_SPEED);
Greg Ward131bce02002-11-30 22:56:44 +0000366}
367
368static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000369oss_sync(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000370{
Charles-François Natalia5293082011-06-11 18:58:24 +0200371 if (!_is_fd_valid(self->fd))
372 return NULL;
373
Greg Wardd0d592f2003-05-27 01:57:21 +0000374 return _do_ioctl_0(self->fd, args, "sync", SNDCTL_DSP_SYNC);
Greg Ward131bce02002-11-30 22:56:44 +0000375}
Guido van Rossum0741f802003-06-02 14:15:34 +0000376
Greg Ward131bce02002-11-30 22:56:44 +0000377static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000378oss_reset(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000379{
Charles-François Natalia5293082011-06-11 18:58:24 +0200380 if (!_is_fd_valid(self->fd))
381 return NULL;
382
Greg Ward7b43c682002-12-30 02:29:28 +0000383 return _do_ioctl_0(self->fd, args, "reset", SNDCTL_DSP_RESET);
Greg Ward131bce02002-11-30 22:56:44 +0000384}
Guido van Rossum0741f802003-06-02 14:15:34 +0000385
Greg Ward131bce02002-11-30 22:56:44 +0000386static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000387oss_post(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000388{
Charles-François Natalia5293082011-06-11 18:58:24 +0200389 if (!_is_fd_valid(self->fd))
390 return NULL;
391
Greg Ward7b43c682002-12-30 02:29:28 +0000392 return _do_ioctl_0(self->fd, args, "post", SNDCTL_DSP_POST);
Greg Ward131bce02002-11-30 22:56:44 +0000393}
394
395
396/* Regular file methods: read(), write(), close(), etc. as well
397 as one convenience method, writeall(). */
398
Greg Ward04613a92002-11-30 22:47:45 +0000399static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000400oss_read(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000401{
402 int size, count;
403 char *cp;
404 PyObject *rv;
Guido van Rossum0741f802003-06-02 14:15:34 +0000405
Charles-François Natalia5293082011-06-11 18:58:24 +0200406 if (!_is_fd_valid(self->fd))
407 return NULL;
408
Greg Ward04613a92002-11-30 22:47:45 +0000409 if (!PyArg_ParseTuple(args, "i:read", &size))
410 return NULL;
Gregory P. Smith0a608fd2008-09-06 21:34:51 +0000411 rv = PyBytes_FromStringAndSize(NULL, size);
Greg Ward04613a92002-11-30 22:47:45 +0000412 if (rv == NULL)
413 return NULL;
Gregory P. Smith0a608fd2008-09-06 21:34:51 +0000414 cp = PyBytes_AS_STRING(rv);
Greg Ward64927852003-05-23 01:50:37 +0000415
416 Py_BEGIN_ALLOW_THREADS
417 count = read(self->fd, cp, size);
418 Py_END_ALLOW_THREADS
419
420 if (count < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000421 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000422 Py_DECREF(rv);
423 return NULL;
424 }
Greg Ward7b43c682002-12-30 02:29:28 +0000425 self->icount += count;
Gregory P. Smith0a608fd2008-09-06 21:34:51 +0000426 _PyBytes_Resize(&rv, count);
Greg Ward04613a92002-11-30 22:47:45 +0000427 return rv;
428}
429
430static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000431oss_write(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000432{
433 char *cp;
434 int rv, size;
Greg Ward131bce02002-11-30 22:56:44 +0000435
Charles-François Natalia5293082011-06-11 18:58:24 +0200436 if (!_is_fd_valid(self->fd))
437 return NULL;
438
Neal Norwitz8abd2e62007-08-26 04:57:08 +0000439 if (!PyArg_ParseTuple(args, "y#:write", &cp, &size)) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000440 return NULL;
Greg Ward131bce02002-11-30 22:56:44 +0000441 }
Greg Ward64927852003-05-23 01:50:37 +0000442
443 Py_BEGIN_ALLOW_THREADS
444 rv = write(self->fd, cp, size);
445 Py_END_ALLOW_THREADS
446
447 if (rv == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000448 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000449 } else {
Greg Ward7b43c682002-12-30 02:29:28 +0000450 self->ocount += rv;
Greg Ward131bce02002-11-30 22:56:44 +0000451 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000452 return PyLong_FromLong(rv);
Greg Ward131bce02002-11-30 22:56:44 +0000453}
454
455static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000456oss_writeall(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000457{
458 char *cp;
459 int rv, size;
460 fd_set write_set_fds;
461 int select_rv;
Guido van Rossum0741f802003-06-02 14:15:34 +0000462
Greg Ward131bce02002-11-30 22:56:44 +0000463 /* NB. writeall() is only useful in non-blocking mode: according to
464 Guenter Geiger <geiger@xdv.org> on the linux-audio-dev list
465 (http://eca.cx/lad/2002/11/0380.html), OSS guarantees that
466 write() in blocking mode consumes the whole buffer. In blocking
467 mode, the behaviour of write() and writeall() from Python is
468 indistinguishable. */
469
Charles-François Natalia5293082011-06-11 18:58:24 +0200470 if (!_is_fd_valid(self->fd))
471 return NULL;
472
Neal Norwitz8abd2e62007-08-26 04:57:08 +0000473 if (!PyArg_ParseTuple(args, "y#:write", &cp, &size))
Greg Ward131bce02002-11-30 22:56:44 +0000474 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000475
Charles-François Nataliaa26b272011-08-28 17:51:43 +0200476 if (!_PyIsSelectable_fd(self->fd)) {
477 PyErr_SetString(PyExc_ValueError,
478 "file descriptor out of range for select");
479 return NULL;
480 }
Greg Ward04613a92002-11-30 22:47:45 +0000481 /* use select to wait for audio device to be available */
482 FD_ZERO(&write_set_fds);
Greg Ward7b43c682002-12-30 02:29:28 +0000483 FD_SET(self->fd, &write_set_fds);
Greg Ward04613a92002-11-30 22:47:45 +0000484
485 while (size > 0) {
Greg Ward64927852003-05-23 01:50:37 +0000486 Py_BEGIN_ALLOW_THREADS
Greg Ward7b43c682002-12-30 02:29:28 +0000487 select_rv = select(self->fd+1, NULL, &write_set_fds, NULL, NULL);
Greg Ward64927852003-05-23 01:50:37 +0000488 Py_END_ALLOW_THREADS
Greg Ward131bce02002-11-30 22:56:44 +0000489 assert(select_rv != 0); /* no timeout, can't expire */
490 if (select_rv == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000491 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000492
Greg Ward64927852003-05-23 01:50:37 +0000493 Py_BEGIN_ALLOW_THREADS
Greg Ward7b43c682002-12-30 02:29:28 +0000494 rv = write(self->fd, cp, size);
Greg Ward64927852003-05-23 01:50:37 +0000495 Py_END_ALLOW_THREADS
Greg Ward131bce02002-11-30 22:56:44 +0000496 if (rv == -1) {
497 if (errno == EAGAIN) { /* buffer is full, try again */
498 errno = 0;
499 continue;
500 } else /* it's a real error */
Greg Ward97708bc2002-11-30 23:17:10 +0000501 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000502 } else { /* wrote rv bytes */
Greg Ward7b43c682002-12-30 02:29:28 +0000503 self->ocount += rv;
Greg Ward131bce02002-11-30 22:56:44 +0000504 size -= rv;
505 cp += rv;
506 }
Greg Ward04613a92002-11-30 22:47:45 +0000507 }
508 Py_INCREF(Py_None);
509 return Py_None;
510}
511
512static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000513oss_close(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000514{
Greg Ward7b43c682002-12-30 02:29:28 +0000515 if (self->fd >= 0) {
Greg Wardb8043902003-05-26 22:47:30 +0000516 Py_BEGIN_ALLOW_THREADS
Greg Ward7b43c682002-12-30 02:29:28 +0000517 close(self->fd);
Greg Wardb8043902003-05-26 22:47:30 +0000518 Py_END_ALLOW_THREADS
Greg Ward7b43c682002-12-30 02:29:28 +0000519 self->fd = -1;
Greg Ward04613a92002-11-30 22:47:45 +0000520 }
521 Py_INCREF(Py_None);
522 return Py_None;
523}
524
525static PyObject *
Georg Brandl770a2be2010-10-24 20:47:32 +0000526oss_self(PyObject *self, PyObject *unused)
Georg Brandl1e908af2010-10-23 17:31:52 +0000527{
528 Py_INCREF(self);
529 return self;
530}
531
Victor Stinner63941882011-09-29 00:42:28 +0200532static PyObject *
Georg Brandl1e908af2010-10-23 17:31:52 +0000533oss_exit(PyObject *self, PyObject *unused)
534{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200535 _Py_IDENTIFIER(close);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200536
537 PyObject *ret = _PyObject_CallMethodId(self, &PyId_close, NULL);
Georg Brandl1e908af2010-10-23 17:31:52 +0000538 if (!ret)
539 return NULL;
540 Py_DECREF(ret);
541 Py_RETURN_NONE;
542}
543
544static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000545oss_fileno(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000546{
Charles-François Natalia5293082011-06-11 18:58:24 +0200547 if (!_is_fd_valid(self->fd))
548 return NULL;
549
Christian Heimes217cfd12007-12-02 14:31:20 +0000550 return PyLong_FromLong(self->fd);
Greg Ward04613a92002-11-30 22:47:45 +0000551}
552
Greg Ward131bce02002-11-30 22:56:44 +0000553
554/* Convenience methods: these generally wrap a couple of ioctls into one
555 common task. */
556
Greg Ward04613a92002-11-30 22:47:45 +0000557static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000558oss_setparameters(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000559{
Greg Wardd6769062003-05-29 21:53:06 +0000560 int wanted_fmt, wanted_channels, wanted_rate, strict=0;
561 int 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. */
Serhiy Storchaka48d761e2013-12-17 15:11:24 +0200606 return Py_BuildValue("(iii)", fmt, channels, rate);
Greg Ward04613a92002-11-30 22:47:45 +0000607}
608
609static int
Greg Ward499b73e2002-12-31 03:04:52 +0000610_ssize(oss_audio_t *self, int *nchannels, int *ssize)
Greg Ward04613a92002-11-30 22:47:45 +0000611{
612 int fmt;
613
614 fmt = 0;
Guido van Rossum0741f802003-06-02 14:15:34 +0000615 if (ioctl(self->fd, SNDCTL_DSP_SETFMT, &fmt) < 0)
Greg Ward04613a92002-11-30 22:47:45 +0000616 return -errno;
617
618 switch (fmt) {
619 case AFMT_MU_LAW:
620 case AFMT_A_LAW:
621 case AFMT_U8:
622 case AFMT_S8:
Greg Ward38c92662003-05-29 21:55:41 +0000623 *ssize = 1; /* 8 bit formats: 1 byte */
Greg Ward04613a92002-11-30 22:47:45 +0000624 break;
625 case AFMT_S16_LE:
626 case AFMT_S16_BE:
627 case AFMT_U16_LE:
628 case AFMT_U16_BE:
Greg Ward84f8ecd2003-05-29 23:44:44 +0000629 *ssize = 2; /* 16 bit formats: 2 byte */
Greg Ward04613a92002-11-30 22:47:45 +0000630 break;
631 case AFMT_MPEG:
632 case AFMT_IMA_ADPCM:
633 default:
634 return -EOPNOTSUPP;
635 }
Greg Ward7b43c682002-12-30 02:29:28 +0000636 if (ioctl(self->fd, SNDCTL_DSP_CHANNELS, nchannels) < 0)
Greg Ward04613a92002-11-30 22:47:45 +0000637 return -errno;
638 return 0;
639}
640
641
Guido van Rossum0741f802003-06-02 14:15:34 +0000642/* bufsize returns the size of the hardware audio buffer in number
Greg Ward04613a92002-11-30 22:47:45 +0000643 of samples */
644static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000645oss_bufsize(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000646{
647 audio_buf_info ai;
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000648 int nchannels=0, ssize=0;
Greg Ward04613a92002-11-30 22:47:45 +0000649
Charles-François Natalia5293082011-06-11 18:58:24 +0200650 if (!_is_fd_valid(self->fd))
651 return NULL;
652
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000653 if (_ssize(self, &nchannels, &ssize) < 0 || !nchannels || !ssize) {
Greg Ward97708bc2002-11-30 23:17:10 +0000654 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000655 return NULL;
656 }
Greg Ward7b43c682002-12-30 02:29:28 +0000657 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000658 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000659 return NULL;
660 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000661 return PyLong_FromLong((ai.fragstotal * ai.fragsize) / (nchannels * ssize));
Greg Ward04613a92002-11-30 22:47:45 +0000662}
663
Guido van Rossum0741f802003-06-02 14:15:34 +0000664/* obufcount returns the number of samples that are available in the
Greg Ward04613a92002-11-30 22:47:45 +0000665 hardware for playing */
666static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000667oss_obufcount(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000668{
669 audio_buf_info ai;
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000670 int nchannels=0, ssize=0;
Greg Ward04613a92002-11-30 22:47:45 +0000671
Charles-François Natalia5293082011-06-11 18:58:24 +0200672 if (!_is_fd_valid(self->fd))
673 return NULL;
674
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000675 if (_ssize(self, &nchannels, &ssize) < 0 || !nchannels || !ssize) {
Greg Ward97708bc2002-11-30 23:17:10 +0000676 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000677 return NULL;
678 }
Greg Ward7b43c682002-12-30 02:29:28 +0000679 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000680 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000681 return NULL;
682 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000683 return PyLong_FromLong((ai.fragstotal * ai.fragsize - ai.bytes) /
Greg Ward04613a92002-11-30 22:47:45 +0000684 (ssize * nchannels));
685}
686
687/* obufcount returns the number of samples that can be played without
688 blocking */
689static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000690oss_obuffree(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000691{
692 audio_buf_info ai;
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000693 int nchannels=0, ssize=0;
Greg Ward04613a92002-11-30 22:47:45 +0000694
Charles-François Natalia5293082011-06-11 18:58:24 +0200695 if (!_is_fd_valid(self->fd))
696 return NULL;
697
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000698 if (_ssize(self, &nchannels, &ssize) < 0 || !nchannels || !ssize) {
Greg Ward97708bc2002-11-30 23:17:10 +0000699 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000700 return NULL;
701 }
Greg Ward7b43c682002-12-30 02:29:28 +0000702 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000703 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000704 return NULL;
705 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000706 return PyLong_FromLong(ai.bytes / (ssize * nchannels));
Greg Ward04613a92002-11-30 22:47:45 +0000707}
708
Greg Ward04613a92002-11-30 22:47:45 +0000709static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000710oss_getptr(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000711{
712 count_info info;
713 int req;
714
Charles-François Natalia5293082011-06-11 18:58:24 +0200715 if (!_is_fd_valid(self->fd))
716 return NULL;
717
Greg Ward7b43c682002-12-30 02:29:28 +0000718 if (self->mode == O_RDONLY)
Greg Wardad4d9b92002-12-30 03:02:22 +0000719 req = SNDCTL_DSP_GETIPTR;
Greg Ward04613a92002-11-30 22:47:45 +0000720 else
Greg Wardad4d9b92002-12-30 03:02:22 +0000721 req = SNDCTL_DSP_GETOPTR;
Greg Ward7b43c682002-12-30 02:29:28 +0000722 if (ioctl(self->fd, req, &info) == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000723 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000724 return NULL;
725 }
726 return Py_BuildValue("iii", info.bytes, info.blocks, info.ptr);
727}
728
Greg Wardda1cacb2002-12-31 03:02:23 +0000729
730/* ----------------------------------------------------------------------
731 * Methods of mixer objects (OSSMixerType)
732 */
733
Greg Ward3d9994d2002-12-11 15:12:01 +0000734static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000735oss_mixer_close(oss_mixer_t *self, PyObject *unused)
Greg Ward3d9994d2002-12-11 15:12:01 +0000736{
Greg Ward7b43c682002-12-30 02:29:28 +0000737 if (self->fd >= 0) {
738 close(self->fd);
739 self->fd = -1;
Greg Ward3d9994d2002-12-11 15:12:01 +0000740 }
741 Py_INCREF(Py_None);
742 return Py_None;
743}
744
745static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000746oss_mixer_fileno(oss_mixer_t *self, PyObject *unused)
Greg Ward3d9994d2002-12-11 15:12:01 +0000747{
Charles-François Natalia5293082011-06-11 18:58:24 +0200748 if (!_is_fd_valid(self->fd))
749 return NULL;
750
Christian Heimes217cfd12007-12-02 14:31:20 +0000751 return PyLong_FromLong(self->fd);
Greg Ward3d9994d2002-12-11 15:12:01 +0000752}
753
754/* Simple mixer interface methods */
755
756static PyObject *
Greg Ward2d6f9a92002-12-31 02:54:43 +0000757oss_mixer_controls(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000758{
Charles-François Natalia5293082011-06-11 18:58:24 +0200759 if (!_is_fd_valid(self->fd))
760 return NULL;
761
Greg Ward2d6f9a92002-12-31 02:54:43 +0000762 return _do_ioctl_1_internal(self->fd, args, "controls",
Greg Ward3d9994d2002-12-11 15:12:01 +0000763 SOUND_MIXER_READ_DEVMASK);
764}
765
766static PyObject *
Greg Ward2d6f9a92002-12-31 02:54:43 +0000767oss_mixer_stereocontrols(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000768{
Charles-François Natalia5293082011-06-11 18:58:24 +0200769 if (!_is_fd_valid(self->fd))
770 return NULL;
771
Greg Ward2d6f9a92002-12-31 02:54:43 +0000772 return _do_ioctl_1_internal(self->fd, args, "stereocontrols",
Greg Ward3d9994d2002-12-11 15:12:01 +0000773 SOUND_MIXER_READ_STEREODEVS);
774}
775
776static PyObject *
Greg Ward2d6f9a92002-12-31 02:54:43 +0000777oss_mixer_reccontrols(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000778{
Charles-François Natalia5293082011-06-11 18:58:24 +0200779 if (!_is_fd_valid(self->fd))
780 return NULL;
781
Greg Ward2d6f9a92002-12-31 02:54:43 +0000782 return _do_ioctl_1_internal(self->fd, args, "reccontrols",
Greg Wardb69bb3d2002-12-12 17:35:45 +0000783 SOUND_MIXER_READ_RECMASK);
Greg Ward3d9994d2002-12-11 15:12:01 +0000784}
785
786static PyObject *
Greg Warde7037662002-12-30 03:01:48 +0000787oss_mixer_get(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000788{
789 int channel, volume;
Guido van Rossum0741f802003-06-02 14:15:34 +0000790
Charles-François Natalia5293082011-06-11 18:58:24 +0200791 if (!_is_fd_valid(self->fd))
792 return NULL;
793
Greg Ward3d9994d2002-12-11 15:12:01 +0000794 /* Can't use _do_ioctl_1 because of encoded arg thingy. */
Greg Warde7037662002-12-30 03:01:48 +0000795 if (!PyArg_ParseTuple(args, "i:get", &channel))
Greg Wardad4d9b92002-12-30 03:02:22 +0000796 return NULL;
Guido van Rossum0741f802003-06-02 14:15:34 +0000797
Greg Ward3d9994d2002-12-11 15:12:01 +0000798 if (channel < 0 || channel > SOUND_MIXER_NRDEVICES) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000799 PyErr_SetString(OSSAudioError, "Invalid mixer channel specified.");
800 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000801 }
Guido van Rossum0741f802003-06-02 14:15:34 +0000802
Greg Warde7037662002-12-30 03:01:48 +0000803 if (ioctl(self->fd, MIXER_READ(channel), &volume) == -1)
Greg Wardad4d9b92002-12-30 03:02:22 +0000804 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum0741f802003-06-02 14:15:34 +0000805
Greg Warde7037662002-12-30 03:01:48 +0000806 return Py_BuildValue("(ii)", volume & 0xff, (volume & 0xff00) >> 8);
Greg Ward3d9994d2002-12-11 15:12:01 +0000807}
808
809static PyObject *
Greg Warde7037662002-12-30 03:01:48 +0000810oss_mixer_set(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000811{
812 int channel, volume, leftVol, rightVol;
Guido van Rossum0741f802003-06-02 14:15:34 +0000813
Charles-François Natalia5293082011-06-11 18:58:24 +0200814 if (!_is_fd_valid(self->fd))
815 return NULL;
816
Greg Ward3d9994d2002-12-11 15:12:01 +0000817 /* Can't use _do_ioctl_1 because of encoded arg thingy. */
Greg Warde7037662002-12-30 03:01:48 +0000818 if (!PyArg_ParseTuple(args, "i(ii):set", &channel, &leftVol, &rightVol))
Greg Wardad4d9b92002-12-30 03:02:22 +0000819 return NULL;
Guido van Rossum0741f802003-06-02 14:15:34 +0000820
Greg Ward3d9994d2002-12-11 15:12:01 +0000821 if (channel < 0 || channel > SOUND_MIXER_NRDEVICES) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000822 PyErr_SetString(OSSAudioError, "Invalid mixer channel specified.");
823 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000824 }
Guido van Rossum0741f802003-06-02 14:15:34 +0000825
Greg Ward3d9994d2002-12-11 15:12:01 +0000826 if (leftVol < 0 || rightVol < 0 || leftVol > 100 || rightVol > 100) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000827 PyErr_SetString(OSSAudioError, "Volumes must be between 0 and 100.");
828 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000829 }
830
831 volume = (rightVol << 8) | leftVol;
Guido van Rossum0741f802003-06-02 14:15:34 +0000832
Greg Warde7037662002-12-30 03:01:48 +0000833 if (ioctl(self->fd, MIXER_WRITE(channel), &volume) == -1)
Greg Wardad4d9b92002-12-30 03:02:22 +0000834 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum0741f802003-06-02 14:15:34 +0000835
Greg Warde7037662002-12-30 03:01:48 +0000836 return Py_BuildValue("(ii)", volume & 0xff, (volume & 0xff00) >> 8);
Greg Ward3d9994d2002-12-11 15:12:01 +0000837}
838
839static PyObject *
Greg Wardf05aa102002-12-30 23:19:32 +0000840oss_mixer_get_recsrc(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000841{
Charles-François Natalia5293082011-06-11 18:58:24 +0200842 if (!_is_fd_valid(self->fd))
843 return NULL;
844
Greg Wardf05aa102002-12-30 23:19:32 +0000845 return _do_ioctl_1_internal(self->fd, args, "get_recsrc",
Greg Ward3d9994d2002-12-11 15:12:01 +0000846 SOUND_MIXER_READ_RECSRC);
847}
848
849static PyObject *
Greg Wardf05aa102002-12-30 23:19:32 +0000850oss_mixer_set_recsrc(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000851{
Charles-François Natalia5293082011-06-11 18:58:24 +0200852 if (!_is_fd_valid(self->fd))
853 return NULL;
854
Greg Wardf05aa102002-12-30 23:19:32 +0000855 return _do_ioctl_1(self->fd, args, "set_recsrc",
Greg Ward3d9994d2002-12-11 15:12:01 +0000856 SOUND_MIXER_WRITE_RECSRC);
857}
858
859
Greg Wardda1cacb2002-12-31 03:02:23 +0000860/* ----------------------------------------------------------------------
861 * Method tables and other bureaucracy
862 */
863
Greg Ward8c6b6a92002-12-11 14:43:13 +0000864static PyMethodDef oss_methods[] = {
Greg Ward131bce02002-11-30 22:56:44 +0000865 /* Regular file methods */
Greg Wardad4d9b92002-12-30 03:02:22 +0000866 { "read", (PyCFunction)oss_read, METH_VARARGS },
867 { "write", (PyCFunction)oss_write, METH_VARARGS },
868 { "writeall", (PyCFunction)oss_writeall, METH_VARARGS },
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000869 { "close", (PyCFunction)oss_close, METH_NOARGS },
870 { "fileno", (PyCFunction)oss_fileno, METH_NOARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000871
872 /* Simple ioctl wrappers */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000873 { "nonblock", (PyCFunction)oss_nonblock, METH_NOARGS },
Greg Wardad4d9b92002-12-30 03:02:22 +0000874 { "setfmt", (PyCFunction)oss_setfmt, METH_VARARGS },
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000875 { "getfmts", (PyCFunction)oss_getfmts, METH_NOARGS },
Greg Ward8c6b6a92002-12-11 14:43:13 +0000876 { "channels", (PyCFunction)oss_channels, METH_VARARGS },
877 { "speed", (PyCFunction)oss_speed, METH_VARARGS },
Greg Wardad4d9b92002-12-30 03:02:22 +0000878 { "sync", (PyCFunction)oss_sync, METH_VARARGS },
879 { "reset", (PyCFunction)oss_reset, METH_VARARGS },
880 { "post", (PyCFunction)oss_post, METH_VARARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000881
882 /* Convenience methods -- wrap a couple of ioctls together */
Greg Wardad4d9b92002-12-30 03:02:22 +0000883 { "setparameters", (PyCFunction)oss_setparameters, METH_VARARGS },
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000884 { "bufsize", (PyCFunction)oss_bufsize, METH_NOARGS },
885 { "obufcount", (PyCFunction)oss_obufcount, METH_NOARGS },
886 { "obuffree", (PyCFunction)oss_obuffree, METH_NOARGS },
887 { "getptr", (PyCFunction)oss_getptr, METH_NOARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000888
889 /* Aliases for backwards compatibility */
Greg Wardad4d9b92002-12-30 03:02:22 +0000890 { "flush", (PyCFunction)oss_sync, METH_VARARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000891
Serhiy Storchaka14867992014-09-10 23:43:41 +0300892 /* Support for the context management protocol */
Georg Brandl770a2be2010-10-24 20:47:32 +0000893 { "__enter__", oss_self, METH_NOARGS },
894 { "__exit__", oss_exit, METH_VARARGS },
Georg Brandl1e908af2010-10-23 17:31:52 +0000895
Greg Wardad4d9b92002-12-30 03:02:22 +0000896 { NULL, NULL} /* sentinel */
Greg Ward04613a92002-11-30 22:47:45 +0000897};
898
Greg Ward3d9994d2002-12-11 15:12:01 +0000899static PyMethodDef oss_mixer_methods[] = {
900 /* Regular file method - OSS mixers are ioctl-only interface */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000901 { "close", (PyCFunction)oss_mixer_close, METH_NOARGS },
902 { "fileno", (PyCFunction)oss_mixer_fileno, METH_NOARGS },
Greg Ward3d9994d2002-12-11 15:12:01 +0000903
Serhiy Storchaka14867992014-09-10 23:43:41 +0300904 /* Support for the context management protocol */
Georg Brandl770a2be2010-10-24 20:47:32 +0000905 { "__enter__", oss_self, METH_NOARGS },
906 { "__exit__", oss_exit, METH_VARARGS },
Georg Brandl1e908af2010-10-23 17:31:52 +0000907
Greg Ward3d9994d2002-12-11 15:12:01 +0000908 /* Simple ioctl wrappers */
Guido van Rossum0741f802003-06-02 14:15:34 +0000909 { "controls", (PyCFunction)oss_mixer_controls, METH_VARARGS },
Greg Ward2d6f9a92002-12-31 02:54:43 +0000910 { "stereocontrols", (PyCFunction)oss_mixer_stereocontrols, METH_VARARGS},
Guido van Rossum0741f802003-06-02 14:15:34 +0000911 { "reccontrols", (PyCFunction)oss_mixer_reccontrols, METH_VARARGS},
Greg Wardad4d9b92002-12-30 03:02:22 +0000912 { "get", (PyCFunction)oss_mixer_get, METH_VARARGS },
913 { "set", (PyCFunction)oss_mixer_set, METH_VARARGS },
Greg Wardf05aa102002-12-30 23:19:32 +0000914 { "get_recsrc", (PyCFunction)oss_mixer_get_recsrc, METH_VARARGS },
915 { "set_recsrc", (PyCFunction)oss_mixer_set_recsrc, METH_VARARGS },
Guido van Rossum0741f802003-06-02 14:15:34 +0000916
Greg Wardad4d9b92002-12-30 03:02:22 +0000917 { NULL, NULL}
Greg Ward3d9994d2002-12-11 15:12:01 +0000918};
919
Greg Ward04613a92002-11-30 22:47:45 +0000920static PyObject *
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000921oss_getattro(oss_audio_t *self, PyObject *nameobj)
Greg Ward04613a92002-11-30 22:47:45 +0000922{
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000923 char *name = "";
Greg Ward50682d02005-03-07 01:41:11 +0000924 PyObject * rval = NULL;
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000925
926 if (PyUnicode_Check(nameobj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 name = _PyUnicode_AsString(nameobj);
928
Greg Ward50682d02005-03-07 01:41:11 +0000929 if (strcmp(name, "closed") == 0) {
930 rval = (self->fd == -1) ? Py_True : Py_False;
931 Py_INCREF(rval);
932 }
933 else if (strcmp(name, "name") == 0) {
Neal Norwitz8abd2e62007-08-26 04:57:08 +0000934 rval = PyUnicode_FromString(self->devicename);
Greg Ward50682d02005-03-07 01:41:11 +0000935 }
936 else if (strcmp(name, "mode") == 0) {
937 /* No need for a "default" in this switch: from newossobject(),
938 self->mode can only be one of these three values. */
939 switch(self->mode) {
940 case O_RDONLY:
Neal Norwitz8abd2e62007-08-26 04:57:08 +0000941 rval = PyUnicode_FromString("r");
Greg Ward50682d02005-03-07 01:41:11 +0000942 break;
943 case O_RDWR:
Neal Norwitz8abd2e62007-08-26 04:57:08 +0000944 rval = PyUnicode_FromString("rw");
Greg Ward50682d02005-03-07 01:41:11 +0000945 break;
946 case O_WRONLY:
Neal Norwitz8abd2e62007-08-26 04:57:08 +0000947 rval = PyUnicode_FromString("w");
Greg Ward50682d02005-03-07 01:41:11 +0000948 break;
949 }
950 }
951 else {
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000952 rval = PyObject_GenericGetAttr((PyObject *)self, nameobj);
Greg Ward50682d02005-03-07 01:41:11 +0000953 }
954 return rval;
Greg Ward04613a92002-11-30 22:47:45 +0000955}
956
Greg Ward499b73e2002-12-31 03:04:52 +0000957static PyTypeObject OSSAudioType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000958 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Greg Ward9a568eb2002-11-30 23:20:09 +0000959 "ossaudiodev.oss_audio_device", /*tp_name*/
Greg Ward499b73e2002-12-31 03:04:52 +0000960 sizeof(oss_audio_t), /*tp_size*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000961 0, /*tp_itemsize*/
Greg Ward04613a92002-11-30 22:47:45 +0000962 /* methods */
Greg Wardad4d9b92002-12-30 03:02:22 +0000963 (destructor)oss_dealloc, /*tp_dealloc*/
964 0, /*tp_print*/
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000965 0, /*tp_getattr*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000966 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000967 0, /*tp_reserved*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000968 0, /*tp_repr*/
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000969 0, /*tp_as_number*/
970 0, /*tp_as_sequence*/
971 0, /*tp_as_mapping*/
972 0, /*tp_hash*/
973 0, /*tp_call*/
974 0, /*tp_str*/
975 (getattrofunc)oss_getattro, /*tp_getattro*/
976 0, /*tp_setattro*/
977 0, /*tp_as_buffer*/
978 Py_TPFLAGS_DEFAULT, /*tp_flags*/
979 0, /*tp_doc*/
980 0, /*tp_traverse*/
981 0, /*tp_clear*/
982 0, /*tp_richcompare*/
983 0, /*tp_weaklistoffset*/
984 0, /*tp_iter*/
985 0, /*tp_iternext*/
986 oss_methods, /*tp_methods*/
Greg Ward04613a92002-11-30 22:47:45 +0000987};
988
Greg Ward3d9994d2002-12-11 15:12:01 +0000989static PyTypeObject OSSMixerType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000990 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Greg Ward3d9994d2002-12-11 15:12:01 +0000991 "ossaudiodev.oss_mixer_device", /*tp_name*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000992 sizeof(oss_mixer_t), /*tp_size*/
993 0, /*tp_itemsize*/
Greg Ward3d9994d2002-12-11 15:12:01 +0000994 /* methods */
995 (destructor)oss_mixer_dealloc, /*tp_dealloc*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000996 0, /*tp_print*/
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000997 0, /*tp_getattr*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000998 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000999 0, /*tp_reserved*/
Greg Wardad4d9b92002-12-30 03:02:22 +00001000 0, /*tp_repr*/
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +00001001 0, /*tp_as_number*/
1002 0, /*tp_as_sequence*/
1003 0, /*tp_as_mapping*/
1004 0, /*tp_hash*/
1005 0, /*tp_call*/
1006 0, /*tp_str*/
1007 0, /*tp_getattro*/
1008 0, /*tp_setattro*/
1009 0, /*tp_as_buffer*/
1010 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1011 0, /*tp_doc*/
1012 0, /*tp_traverse*/
1013 0, /*tp_clear*/
1014 0, /*tp_richcompare*/
1015 0, /*tp_weaklistoffset*/
1016 0, /*tp_iter*/
1017 0, /*tp_iternext*/
1018 oss_mixer_methods, /*tp_methods*/
Greg Ward3d9994d2002-12-11 15:12:01 +00001019};
1020
1021
Greg Ward04613a92002-11-30 22:47:45 +00001022static PyObject *
Greg Ward9a568eb2002-11-30 23:20:09 +00001023ossopen(PyObject *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +00001024{
Greg Ward8c6b6a92002-12-11 14:43:13 +00001025 return (PyObject *)newossobject(args);
Greg Ward04613a92002-11-30 22:47:45 +00001026}
1027
Greg Ward3d9994d2002-12-11 15:12:01 +00001028static PyObject *
1029ossopenmixer(PyObject *self, PyObject *args)
1030{
1031 return (PyObject *)newossmixerobject(args);
1032}
1033
Greg Ward9a568eb2002-11-30 23:20:09 +00001034static PyMethodDef ossaudiodev_methods[] = {
1035 { "open", ossopen, METH_VARARGS },
Greg Ward3d9994d2002-12-11 15:12:01 +00001036 { "openmixer", ossopenmixer, METH_VARARGS },
Greg Ward04613a92002-11-30 22:47:45 +00001037 { 0, 0 },
1038};
1039
Greg Ward1e0f57d2002-11-30 23:05:26 +00001040
1041#define _EXPORT_INT(mod, name) \
Martin v. Löwis1a214512008-06-11 05:26:20 +00001042 if (PyModule_AddIntConstant(mod, #name, (long) (name)) == -1) return NULL;
Greg Ward1e0f57d2002-11-30 23:05:26 +00001043
Greg Ward744f0fd2002-12-31 03:23:59 +00001044
1045static char *control_labels[] = SOUND_DEVICE_LABELS;
1046static char *control_names[] = SOUND_DEVICE_NAMES;
1047
1048
1049static int
1050build_namelists (PyObject *module)
1051{
1052 PyObject *labels;
1053 PyObject *names;
1054 PyObject *s;
1055 int num_controls;
1056 int i;
1057
Victor Stinner63941882011-09-29 00:42:28 +02001058 num_controls = Py_ARRAY_LENGTH(control_labels);
1059 assert(num_controls == Py_ARRAY_LENGTH(control_names));
Greg Ward744f0fd2002-12-31 03:23:59 +00001060
1061 labels = PyList_New(num_controls);
1062 names = PyList_New(num_controls);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001063 if (labels == NULL || names == NULL)
1064 goto error2;
Greg Ward744f0fd2002-12-31 03:23:59 +00001065 for (i = 0; i < num_controls; i++) {
Neal Norwitz8abd2e62007-08-26 04:57:08 +00001066 s = PyUnicode_FromString(control_labels[i]);
Greg Ward744f0fd2002-12-31 03:23:59 +00001067 if (s == NULL)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001068 goto error2;
Greg Ward744f0fd2002-12-31 03:23:59 +00001069 PyList_SET_ITEM(labels, i, s);
Guido van Rossum0741f802003-06-02 14:15:34 +00001070
Neal Norwitz8abd2e62007-08-26 04:57:08 +00001071 s = PyUnicode_FromString(control_names[i]);
Greg Ward744f0fd2002-12-31 03:23:59 +00001072 if (s == NULL)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001073 goto error2;
Greg Ward744f0fd2002-12-31 03:23:59 +00001074 PyList_SET_ITEM(names, i, s);
1075 }
1076
1077 if (PyModule_AddObject(module, "control_labels", labels) == -1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001078 goto error2;
Greg Ward744f0fd2002-12-31 03:23:59 +00001079 if (PyModule_AddObject(module, "control_names", names) == -1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001080 goto error1;
Greg Ward744f0fd2002-12-31 03:23:59 +00001081
1082 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001083
1084error2:
1085 Py_XDECREF(labels);
1086error1:
1087 Py_XDECREF(names);
1088 return -1;
Guido van Rossum0741f802003-06-02 14:15:34 +00001089}
Greg Ward744f0fd2002-12-31 03:23:59 +00001090
1091
Martin v. Löwis1a214512008-06-11 05:26:20 +00001092static struct PyModuleDef ossaudiodevmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 PyModuleDef_HEAD_INIT,
1094 "ossaudiodev",
1095 NULL,
1096 -1,
1097 ossaudiodev_methods,
1098 NULL,
1099 NULL,
1100 NULL,
1101 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001102};
1103
Antoine Pitrou39b35432010-03-23 00:25:54 +00001104PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001105PyInit_ossaudiodev(void)
Greg Ward04613a92002-11-30 22:47:45 +00001106{
1107 PyObject *m;
Guido van Rossum0741f802003-06-02 14:15:34 +00001108
Antoine Pitrou39b35432010-03-23 00:25:54 +00001109 if (PyType_Ready(&OSSAudioType) < 0)
1110 return NULL;
1111
1112 if (PyType_Ready(&OSSMixerType) < 0)
1113 return NULL;
1114
Martin v. Löwis1a214512008-06-11 05:26:20 +00001115 m = PyModule_Create(&ossaudiodevmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001116 if (m == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +00001118
Guido van Rossum0741f802003-06-02 14:15:34 +00001119 OSSAudioError = PyErr_NewException("ossaudiodev.OSSAudioError",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 NULL, NULL);
Greg Wardd6769062003-05-29 21:53:06 +00001121 if (OSSAudioError) {
Guido van Rossum0741f802003-06-02 14:15:34 +00001122 /* Each call to PyModule_AddObject decrefs it; compensate: */
1123 Py_INCREF(OSSAudioError);
1124 Py_INCREF(OSSAudioError);
Greg Wardad4d9b92002-12-30 03:02:22 +00001125 PyModule_AddObject(m, "error", OSSAudioError);
Greg Wardd6769062003-05-29 21:53:06 +00001126 PyModule_AddObject(m, "OSSAudioError", OSSAudioError);
1127 }
Greg Ward04613a92002-11-30 22:47:45 +00001128
Greg Ward744f0fd2002-12-31 03:23:59 +00001129 /* Build 'control_labels' and 'control_names' lists and add them
1130 to the module. */
1131 if (build_namelists(m) == -1) /* XXX what to do here? */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001132 return NULL;
Greg Ward744f0fd2002-12-31 03:23:59 +00001133
Greg Ward1e0f57d2002-11-30 23:05:26 +00001134 /* Expose the audio format numbers -- essential! */
1135 _EXPORT_INT(m, AFMT_QUERY);
1136 _EXPORT_INT(m, AFMT_MU_LAW);
1137 _EXPORT_INT(m, AFMT_A_LAW);
1138 _EXPORT_INT(m, AFMT_IMA_ADPCM);
1139 _EXPORT_INT(m, AFMT_U8);
1140 _EXPORT_INT(m, AFMT_S16_LE);
1141 _EXPORT_INT(m, AFMT_S16_BE);
1142 _EXPORT_INT(m, AFMT_S8);
1143 _EXPORT_INT(m, AFMT_U16_LE);
1144 _EXPORT_INT(m, AFMT_U16_BE);
1145 _EXPORT_INT(m, AFMT_MPEG);
Andrew M. Kuchlingfe62bc92003-02-13 13:27:07 +00001146#ifdef AFMT_AC3
Greg Ward1e0f57d2002-11-30 23:05:26 +00001147 _EXPORT_INT(m, AFMT_AC3);
Andrew M. Kuchlingfe62bc92003-02-13 13:27:07 +00001148#endif
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001149#ifdef AFMT_S16_NE
Greg Ward1e0f57d2002-11-30 23:05:26 +00001150 _EXPORT_INT(m, AFMT_S16_NE);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001151#endif
Greg Ward0f260542005-03-28 02:40:46 +00001152#ifdef AFMT_U16_NE
1153 _EXPORT_INT(m, AFMT_U16_NE);
1154#endif
1155#ifdef AFMT_S32_LE
1156 _EXPORT_INT(m, AFMT_S32_LE);
1157#endif
1158#ifdef AFMT_S32_BE
1159 _EXPORT_INT(m, AFMT_S32_BE);
1160#endif
1161#ifdef AFMT_MPEG
1162 _EXPORT_INT(m, AFMT_MPEG);
1163#endif
Guido van Rossum0741f802003-06-02 14:15:34 +00001164
Greg Ward37897c22002-12-30 02:43:36 +00001165 /* Expose the sound mixer device numbers. */
Greg Ward7b43c682002-12-30 02:29:28 +00001166 _EXPORT_INT(m, SOUND_MIXER_NRDEVICES);
Greg Ward3d9994d2002-12-11 15:12:01 +00001167 _EXPORT_INT(m, SOUND_MIXER_VOLUME);
1168 _EXPORT_INT(m, SOUND_MIXER_BASS);
1169 _EXPORT_INT(m, SOUND_MIXER_TREBLE);
1170 _EXPORT_INT(m, SOUND_MIXER_SYNTH);
1171 _EXPORT_INT(m, SOUND_MIXER_PCM);
1172 _EXPORT_INT(m, SOUND_MIXER_SPEAKER);
1173 _EXPORT_INT(m, SOUND_MIXER_LINE);
1174 _EXPORT_INT(m, SOUND_MIXER_MIC);
1175 _EXPORT_INT(m, SOUND_MIXER_CD);
1176 _EXPORT_INT(m, SOUND_MIXER_IMIX);
1177 _EXPORT_INT(m, SOUND_MIXER_ALTPCM);
1178 _EXPORT_INT(m, SOUND_MIXER_RECLEV);
1179 _EXPORT_INT(m, SOUND_MIXER_IGAIN);
1180 _EXPORT_INT(m, SOUND_MIXER_OGAIN);
1181 _EXPORT_INT(m, SOUND_MIXER_LINE1);
1182 _EXPORT_INT(m, SOUND_MIXER_LINE2);
1183 _EXPORT_INT(m, SOUND_MIXER_LINE3);
Greg Wardfd0283e2004-05-11 01:34:55 +00001184#ifdef SOUND_MIXER_DIGITAL1
Greg Ward3d9994d2002-12-11 15:12:01 +00001185 _EXPORT_INT(m, SOUND_MIXER_DIGITAL1);
Greg Wardfd0283e2004-05-11 01:34:55 +00001186#endif
1187#ifdef SOUND_MIXER_DIGITAL2
Greg Ward3d9994d2002-12-11 15:12:01 +00001188 _EXPORT_INT(m, SOUND_MIXER_DIGITAL2);
Greg Wardfd0283e2004-05-11 01:34:55 +00001189#endif
1190#ifdef SOUND_MIXER_DIGITAL3
Greg Ward3d9994d2002-12-11 15:12:01 +00001191 _EXPORT_INT(m, SOUND_MIXER_DIGITAL3);
Greg Wardfd0283e2004-05-11 01:34:55 +00001192#endif
1193#ifdef SOUND_MIXER_PHONEIN
Greg Ward3d9994d2002-12-11 15:12:01 +00001194 _EXPORT_INT(m, SOUND_MIXER_PHONEIN);
Greg Wardfd0283e2004-05-11 01:34:55 +00001195#endif
1196#ifdef SOUND_MIXER_PHONEOUT
Greg Ward3d9994d2002-12-11 15:12:01 +00001197 _EXPORT_INT(m, SOUND_MIXER_PHONEOUT);
Greg Wardfd0283e2004-05-11 01:34:55 +00001198#endif
1199#ifdef SOUND_MIXER_VIDEO
Greg Ward3d9994d2002-12-11 15:12:01 +00001200 _EXPORT_INT(m, SOUND_MIXER_VIDEO);
Greg Wardfd0283e2004-05-11 01:34:55 +00001201#endif
1202#ifdef SOUND_MIXER_RADIO
Greg Ward3d9994d2002-12-11 15:12:01 +00001203 _EXPORT_INT(m, SOUND_MIXER_RADIO);
Greg Wardfd0283e2004-05-11 01:34:55 +00001204#endif
1205#ifdef SOUND_MIXER_MONITOR
Greg Ward3d9994d2002-12-11 15:12:01 +00001206 _EXPORT_INT(m, SOUND_MIXER_MONITOR);
Greg Wardfd0283e2004-05-11 01:34:55 +00001207#endif
Greg Ward04613a92002-11-30 22:47:45 +00001208
Greg Ward1e0f57d2002-11-30 23:05:26 +00001209 /* Expose all the ioctl numbers for masochists who like to do this
1210 stuff directly. */
1211 _EXPORT_INT(m, SNDCTL_COPR_HALT);
1212 _EXPORT_INT(m, SNDCTL_COPR_LOAD);
1213 _EXPORT_INT(m, SNDCTL_COPR_RCODE);
1214 _EXPORT_INT(m, SNDCTL_COPR_RCVMSG);
1215 _EXPORT_INT(m, SNDCTL_COPR_RDATA);
1216 _EXPORT_INT(m, SNDCTL_COPR_RESET);
1217 _EXPORT_INT(m, SNDCTL_COPR_RUN);
1218 _EXPORT_INT(m, SNDCTL_COPR_SENDMSG);
1219 _EXPORT_INT(m, SNDCTL_COPR_WCODE);
1220 _EXPORT_INT(m, SNDCTL_COPR_WDATA);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001221#ifdef SNDCTL_DSP_BIND_CHANNEL
Greg Ward1e0f57d2002-11-30 23:05:26 +00001222 _EXPORT_INT(m, SNDCTL_DSP_BIND_CHANNEL);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001223#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001224 _EXPORT_INT(m, SNDCTL_DSP_CHANNELS);
1225 _EXPORT_INT(m, SNDCTL_DSP_GETBLKSIZE);
1226 _EXPORT_INT(m, SNDCTL_DSP_GETCAPS);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001227#ifdef SNDCTL_DSP_GETCHANNELMASK
Greg Ward1e0f57d2002-11-30 23:05:26 +00001228 _EXPORT_INT(m, SNDCTL_DSP_GETCHANNELMASK);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001229#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001230 _EXPORT_INT(m, SNDCTL_DSP_GETFMTS);
1231 _EXPORT_INT(m, SNDCTL_DSP_GETIPTR);
1232 _EXPORT_INT(m, SNDCTL_DSP_GETISPACE);
Greg Wardfd0283e2004-05-11 01:34:55 +00001233#ifdef SNDCTL_DSP_GETODELAY
Greg Ward1e0f57d2002-11-30 23:05:26 +00001234 _EXPORT_INT(m, SNDCTL_DSP_GETODELAY);
Greg Wardfd0283e2004-05-11 01:34:55 +00001235#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001236 _EXPORT_INT(m, SNDCTL_DSP_GETOPTR);
1237 _EXPORT_INT(m, SNDCTL_DSP_GETOSPACE);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001238#ifdef SNDCTL_DSP_GETSPDIF
Greg Ward1e0f57d2002-11-30 23:05:26 +00001239 _EXPORT_INT(m, SNDCTL_DSP_GETSPDIF);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001240#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001241 _EXPORT_INT(m, SNDCTL_DSP_GETTRIGGER);
1242 _EXPORT_INT(m, SNDCTL_DSP_MAPINBUF);
1243 _EXPORT_INT(m, SNDCTL_DSP_MAPOUTBUF);
1244 _EXPORT_INT(m, SNDCTL_DSP_NONBLOCK);
1245 _EXPORT_INT(m, SNDCTL_DSP_POST);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001246#ifdef SNDCTL_DSP_PROFILE
Greg Ward1e0f57d2002-11-30 23:05:26 +00001247 _EXPORT_INT(m, SNDCTL_DSP_PROFILE);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001248#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001249 _EXPORT_INT(m, SNDCTL_DSP_RESET);
1250 _EXPORT_INT(m, SNDCTL_DSP_SAMPLESIZE);
1251 _EXPORT_INT(m, SNDCTL_DSP_SETDUPLEX);
1252 _EXPORT_INT(m, SNDCTL_DSP_SETFMT);
1253 _EXPORT_INT(m, SNDCTL_DSP_SETFRAGMENT);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001254#ifdef SNDCTL_DSP_SETSPDIF
Greg Ward1e0f57d2002-11-30 23:05:26 +00001255 _EXPORT_INT(m, SNDCTL_DSP_SETSPDIF);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001256#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001257 _EXPORT_INT(m, SNDCTL_DSP_SETSYNCRO);
1258 _EXPORT_INT(m, SNDCTL_DSP_SETTRIGGER);
1259 _EXPORT_INT(m, SNDCTL_DSP_SPEED);
1260 _EXPORT_INT(m, SNDCTL_DSP_STEREO);
1261 _EXPORT_INT(m, SNDCTL_DSP_SUBDIVIDE);
1262 _EXPORT_INT(m, SNDCTL_DSP_SYNC);
1263 _EXPORT_INT(m, SNDCTL_FM_4OP_ENABLE);
1264 _EXPORT_INT(m, SNDCTL_FM_LOAD_INSTR);
1265 _EXPORT_INT(m, SNDCTL_MIDI_INFO);
1266 _EXPORT_INT(m, SNDCTL_MIDI_MPUCMD);
1267 _EXPORT_INT(m, SNDCTL_MIDI_MPUMODE);
1268 _EXPORT_INT(m, SNDCTL_MIDI_PRETIME);
1269 _EXPORT_INT(m, SNDCTL_SEQ_CTRLRATE);
1270 _EXPORT_INT(m, SNDCTL_SEQ_GETINCOUNT);
1271 _EXPORT_INT(m, SNDCTL_SEQ_GETOUTCOUNT);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001272#ifdef SNDCTL_SEQ_GETTIME
Greg Ward1e0f57d2002-11-30 23:05:26 +00001273 _EXPORT_INT(m, SNDCTL_SEQ_GETTIME);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001274#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001275 _EXPORT_INT(m, SNDCTL_SEQ_NRMIDIS);
1276 _EXPORT_INT(m, SNDCTL_SEQ_NRSYNTHS);
1277 _EXPORT_INT(m, SNDCTL_SEQ_OUTOFBAND);
1278 _EXPORT_INT(m, SNDCTL_SEQ_PANIC);
1279 _EXPORT_INT(m, SNDCTL_SEQ_PERCMODE);
1280 _EXPORT_INT(m, SNDCTL_SEQ_RESET);
1281 _EXPORT_INT(m, SNDCTL_SEQ_RESETSAMPLES);
1282 _EXPORT_INT(m, SNDCTL_SEQ_SYNC);
1283 _EXPORT_INT(m, SNDCTL_SEQ_TESTMIDI);
1284 _EXPORT_INT(m, SNDCTL_SEQ_THRESHOLD);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001285#ifdef SNDCTL_SYNTH_CONTROL
Greg Ward1e0f57d2002-11-30 23:05:26 +00001286 _EXPORT_INT(m, SNDCTL_SYNTH_CONTROL);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001287#endif
1288#ifdef SNDCTL_SYNTH_ID
Greg Ward1e0f57d2002-11-30 23:05:26 +00001289 _EXPORT_INT(m, SNDCTL_SYNTH_ID);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001290#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001291 _EXPORT_INT(m, SNDCTL_SYNTH_INFO);
1292 _EXPORT_INT(m, SNDCTL_SYNTH_MEMAVL);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001293#ifdef SNDCTL_SYNTH_REMOVESAMPLE
Greg Ward1e0f57d2002-11-30 23:05:26 +00001294 _EXPORT_INT(m, SNDCTL_SYNTH_REMOVESAMPLE);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001295#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001296 _EXPORT_INT(m, SNDCTL_TMR_CONTINUE);
1297 _EXPORT_INT(m, SNDCTL_TMR_METRONOME);
1298 _EXPORT_INT(m, SNDCTL_TMR_SELECT);
1299 _EXPORT_INT(m, SNDCTL_TMR_SOURCE);
1300 _EXPORT_INT(m, SNDCTL_TMR_START);
1301 _EXPORT_INT(m, SNDCTL_TMR_STOP);
1302 _EXPORT_INT(m, SNDCTL_TMR_TEMPO);
1303 _EXPORT_INT(m, SNDCTL_TMR_TIMEBASE);
Martin v. Löwis1a214512008-06-11 05:26:20 +00001304 return m;
Greg Ward04613a92002-11-30 22:47:45 +00001305}