blob: be9001634707402d521978a96f106461ca7212b3 [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);
119
120 if (fd == -1) {
Greg Ward50682d02005-03-07 01:41:11 +0000121 PyErr_SetFromErrnoWithFilename(PyExc_IOError, devicename);
Greg Ward04613a92002-11-30 22:47:45 +0000122 return NULL;
123 }
Greg Ward76ffb192003-04-04 01:47:42 +0000124
125 /* And (try to) put it back in blocking mode so we get the
126 expected write() semantics. */
127 if (fcntl(fd, F_SETFL, 0) == -1) {
128 close(fd);
Greg Ward50682d02005-03-07 01:41:11 +0000129 PyErr_SetFromErrnoWithFilename(PyExc_IOError, devicename);
Greg Ward76ffb192003-04-04 01:47:42 +0000130 return NULL;
131 }
132
Greg Ward04613a92002-11-30 22:47:45 +0000133 if (ioctl(fd, SNDCTL_DSP_GETFMTS, &afmts) == -1) {
Charles-François Natali5a4a1092011-09-29 19:46:37 +0200134 close(fd);
Greg Ward50682d02005-03-07 01:41:11 +0000135 PyErr_SetFromErrnoWithFilename(PyExc_IOError, devicename);
Greg Ward04613a92002-11-30 22:47:45 +0000136 return NULL;
137 }
138 /* Create and initialize the object */
Greg Ward58ae13c2002-12-31 03:07:21 +0000139 if ((self = PyObject_New(oss_audio_t, &OSSAudioType)) == NULL) {
Greg Ward04613a92002-11-30 22:47:45 +0000140 close(fd);
141 return NULL;
142 }
Greg Ward50682d02005-03-07 01:41:11 +0000143 self->devicename = devicename;
Greg Ward58ae13c2002-12-31 03:07:21 +0000144 self->fd = fd;
145 self->mode = imode;
146 self->icount = self->ocount = 0;
147 self->afmts = afmts;
148 return self;
Greg Ward04613a92002-11-30 22:47:45 +0000149}
150
151static void
Greg Ward58ae13c2002-12-31 03:07:21 +0000152oss_dealloc(oss_audio_t *self)
Greg Ward04613a92002-11-30 22:47:45 +0000153{
154 /* if already closed, don't reclose it */
Greg Ward58ae13c2002-12-31 03:07:21 +0000155 if (self->fd != -1)
156 close(self->fd);
157 PyObject_Del(self);
Greg Ward04613a92002-11-30 22:47:45 +0000158}
159
Greg Wardda1cacb2002-12-31 03:02:23 +0000160
161/* ----------------------------------------------------------------------
162 * Mixer object initialization/deallocation
163 */
164
Greg Ward3d9994d2002-12-11 15:12:01 +0000165static oss_mixer_t *
166newossmixerobject(PyObject *arg)
167{
Greg Ward50682d02005-03-07 01:41:11 +0000168 char *devicename = NULL;
Greg Ward0b6dfb82003-03-10 03:17:06 +0000169 int fd;
Greg Ward58ae13c2002-12-31 03:07:21 +0000170 oss_mixer_t *self;
Guido van Rossum0741f802003-06-02 14:15:34 +0000171
Greg Ward50682d02005-03-07 01:41:11 +0000172 if (!PyArg_ParseTuple(arg, "|s", &devicename)) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000173 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000174 }
Guido van Rossum0741f802003-06-02 14:15:34 +0000175
Greg Ward50682d02005-03-07 01:41:11 +0000176 if (devicename == NULL) {
177 devicename = getenv("MIXERDEV");
178 if (devicename == NULL) /* MIXERDEV not set */
179 devicename = "/dev/mixer";
Greg Ward3d9994d2002-12-11 15:12:01 +0000180 }
181
Victor Stinnerdaf45552013-08-28 00:53:59 +0200182 fd = _Py_open(devicename, O_RDWR);
183 if (fd == -1) {
Greg Ward50682d02005-03-07 01:41:11 +0000184 PyErr_SetFromErrnoWithFilename(PyExc_IOError, devicename);
Greg Ward3d9994d2002-12-11 15:12:01 +0000185 return NULL;
186 }
Greg Ward0b6dfb82003-03-10 03:17:06 +0000187
Greg Ward58ae13c2002-12-31 03:07:21 +0000188 if ((self = PyObject_New(oss_mixer_t, &OSSMixerType)) == NULL) {
Greg Ward3d9994d2002-12-11 15:12:01 +0000189 close(fd);
190 return NULL;
191 }
Guido van Rossum0741f802003-06-02 14:15:34 +0000192
Greg Ward58ae13c2002-12-31 03:07:21 +0000193 self->fd = fd;
Guido van Rossum0741f802003-06-02 14:15:34 +0000194
Greg Ward58ae13c2002-12-31 03:07:21 +0000195 return self;
Greg Ward3d9994d2002-12-11 15:12:01 +0000196}
197
198static void
Greg Ward58ae13c2002-12-31 03:07:21 +0000199oss_mixer_dealloc(oss_mixer_t *self)
Greg Ward3d9994d2002-12-11 15:12:01 +0000200{
201 /* if already closed, don't reclose it */
Greg Ward58ae13c2002-12-31 03:07:21 +0000202 if (self->fd != -1)
203 close(self->fd);
204 PyObject_Del(self);
Greg Ward3d9994d2002-12-11 15:12:01 +0000205}
206
Greg Ward131bce02002-11-30 22:56:44 +0000207
208/* Methods to wrap the OSS ioctls. The calling convention is pretty
209 simple:
210 nonblock() -> ioctl(fd, SNDCTL_DSP_NONBLOCK)
211 fmt = setfmt(fmt) -> ioctl(fd, SNDCTL_DSP_SETFMT, &fmt)
212 etc.
213*/
214
215
Greg Wardd6769062003-05-29 21:53:06 +0000216/* ----------------------------------------------------------------------
217 * Helper functions
218 */
219
Charles-François Natalia5293082011-06-11 18:58:24 +0200220/* Check if a given file descriptor is valid (i.e. hasn't been closed).
221 * If true, return 1. Otherwise, raise ValueError and return 0.
222 */
223static int _is_fd_valid(int fd)
224{
225 /* the FD is set to -1 in oss_close()/oss_mixer_close() */
226 if (fd >= 0) {
227 return 1;
228 } else {
229 PyErr_SetString(PyExc_ValueError,
230 "Operation on closed OSS device.");
231 return 0;
232 }
233}
234
Greg Ward131bce02002-11-30 22:56:44 +0000235/* _do_ioctl_1() is a private helper function used for the OSS ioctls --
Terry Jan Reedy0158af32013-03-11 17:42:46 -0400236 SNDCTL_DSP_{SETFMT,CHANNELS,SPEED} -- that are called from C
Greg Ward131bce02002-11-30 22:56:44 +0000237 like this:
238 ioctl(fd, SNDCTL_DSP_cmd, &arg)
239
240 where arg is the value to set, and on return the driver sets arg to
241 the value that was actually set. Mapping this to Python is obvious:
242 arg = dsp.xxx(arg)
243*/
244static PyObject *
Greg Wardda9f8532002-12-11 14:49:59 +0000245_do_ioctl_1(int fd, PyObject *args, char *fname, int cmd)
Greg Ward131bce02002-11-30 22:56:44 +0000246{
Greg Wardda9f8532002-12-11 14:49:59 +0000247 char argfmt[33] = "i:";
Greg Ward131bce02002-11-30 22:56:44 +0000248 int arg;
249
Greg Wardda9f8532002-12-11 14:49:59 +0000250 assert(strlen(fname) <= 30);
Christian Heimese91ad502013-07-20 14:11:28 +0200251 strncat(argfmt, fname, 30);
Greg Ward131bce02002-11-30 22:56:44 +0000252 if (!PyArg_ParseTuple(args, argfmt, &arg))
Greg Wardad4d9b92002-12-30 03:02:22 +0000253 return NULL;
Greg Ward131bce02002-11-30 22:56:44 +0000254
Greg Wardda9f8532002-12-11 14:49:59 +0000255 if (ioctl(fd, cmd, &arg) == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000256 return PyErr_SetFromErrno(PyExc_IOError);
Christian Heimes217cfd12007-12-02 14:31:20 +0000257 return PyLong_FromLong(arg);
Greg Ward131bce02002-11-30 22:56:44 +0000258}
259
Greg Wardda1cacb2002-12-31 03:02:23 +0000260
Greg Ward3d9994d2002-12-11 15:12:01 +0000261/* _do_ioctl_1_internal() is a wrapper for ioctls that take no inputs
262 but return an output -- ie. we need to pass a pointer to a local C
263 variable so the driver can write its output there, but from Python
264 all we see is the return value. For example,
265 SOUND_MIXER_READ_DEVMASK returns a bitmask of available mixer
266 devices, but does not use the value of the parameter passed-in in any
267 way.
268*/
Greg Ward3d9994d2002-12-11 15:12:01 +0000269static PyObject *
270_do_ioctl_1_internal(int fd, PyObject *args, char *fname, int cmd)
271{
272 char argfmt[32] = ":";
273 int arg = 0;
274
275 assert(strlen(fname) <= 30);
Christian Heimese91ad502013-07-20 14:11:28 +0200276 strncat(argfmt, fname, 30);
Greg Ward3d9994d2002-12-11 15:12:01 +0000277 if (!PyArg_ParseTuple(args, argfmt, &arg))
Greg Wardad4d9b92002-12-30 03:02:22 +0000278 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000279
280 if (ioctl(fd, cmd, &arg) == -1)
281 return PyErr_SetFromErrno(PyExc_IOError);
Christian Heimes217cfd12007-12-02 14:31:20 +0000282 return PyLong_FromLong(arg);
Greg Ward3d9994d2002-12-11 15:12:01 +0000283}
284
285
286
Greg Ward131bce02002-11-30 22:56:44 +0000287/* _do_ioctl_0() is a private helper for the no-argument ioctls:
288 SNDCTL_DSP_{SYNC,RESET,POST}. */
289static PyObject *
Greg Wardda9f8532002-12-11 14:49:59 +0000290_do_ioctl_0(int fd, PyObject *args, char *fname, int cmd)
Greg Ward131bce02002-11-30 22:56:44 +0000291{
Greg Wardda9f8532002-12-11 14:49:59 +0000292 char argfmt[32] = ":";
Greg Wardd0d592f2003-05-27 01:57:21 +0000293 int rv;
Greg Ward131bce02002-11-30 22:56:44 +0000294
Greg Wardda9f8532002-12-11 14:49:59 +0000295 assert(strlen(fname) <= 30);
Christian Heimese91ad502013-07-20 14:11:28 +0200296 strncat(argfmt, fname, 30);
Greg Ward131bce02002-11-30 22:56:44 +0000297 if (!PyArg_ParseTuple(args, argfmt))
Greg Wardad4d9b92002-12-30 03:02:22 +0000298 return NULL;
Greg Ward131bce02002-11-30 22:56:44 +0000299
Greg Wardd0d592f2003-05-27 01:57:21 +0000300 /* According to hannu@opensound.com, all three of the ioctls that
301 use this function can block, so release the GIL. This is
302 especially important for SYNC, which can block for several
303 seconds. */
304 Py_BEGIN_ALLOW_THREADS
305 rv = ioctl(fd, cmd, 0);
306 Py_END_ALLOW_THREADS
307
308 if (rv == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000309 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000310 Py_INCREF(Py_None);
311 return Py_None;
312}
313
314
Greg Wardda1cacb2002-12-31 03:02:23 +0000315/* ----------------------------------------------------------------------
Greg Ward499b73e2002-12-31 03:04:52 +0000316 * Methods of DSP objects (OSSAudioType)
Greg Wardda1cacb2002-12-31 03:02:23 +0000317 */
318
Greg Ward131bce02002-11-30 22:56:44 +0000319static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000320oss_nonblock(oss_audio_t *self, PyObject *unused)
Greg Ward131bce02002-11-30 22:56:44 +0000321{
Charles-François Natalia5293082011-06-11 18:58:24 +0200322 if (!_is_fd_valid(self->fd))
323 return NULL;
324
Greg Ward131bce02002-11-30 22:56:44 +0000325 /* Hmmm: it doesn't appear to be possible to return to blocking
326 mode once we're in non-blocking mode! */
Greg Ward7b43c682002-12-30 02:29:28 +0000327 if (ioctl(self->fd, SNDCTL_DSP_NONBLOCK, NULL) == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000328 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000329 Py_INCREF(Py_None);
330 return Py_None;
331}
332
333static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000334oss_setfmt(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000335{
Charles-François Natalia5293082011-06-11 18:58:24 +0200336 if (!_is_fd_valid(self->fd))
337 return NULL;
338
Greg Ward7b43c682002-12-30 02:29:28 +0000339 return _do_ioctl_1(self->fd, args, "setfmt", SNDCTL_DSP_SETFMT);
Greg Ward131bce02002-11-30 22:56:44 +0000340}
341
342static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000343oss_getfmts(oss_audio_t *self, PyObject *unused)
Greg Ward131bce02002-11-30 22:56:44 +0000344{
345 int mask;
Charles-François Natalia5293082011-06-11 18:58:24 +0200346
347 if (!_is_fd_valid(self->fd))
348 return NULL;
349
Greg Ward7b43c682002-12-30 02:29:28 +0000350 if (ioctl(self->fd, SNDCTL_DSP_GETFMTS, &mask) == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000351 return PyErr_SetFromErrno(PyExc_IOError);
Christian Heimes217cfd12007-12-02 14:31:20 +0000352 return PyLong_FromLong(mask);
Greg Ward131bce02002-11-30 22:56:44 +0000353}
354
355static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000356oss_channels(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000357{
Charles-François Natalia5293082011-06-11 18:58:24 +0200358 if (!_is_fd_valid(self->fd))
359 return NULL;
360
Greg Ward7b43c682002-12-30 02:29:28 +0000361 return _do_ioctl_1(self->fd, args, "channels", SNDCTL_DSP_CHANNELS);
Greg Ward131bce02002-11-30 22:56:44 +0000362}
363
364static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000365oss_speed(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000366{
Charles-François Natalia5293082011-06-11 18:58:24 +0200367 if (!_is_fd_valid(self->fd))
368 return NULL;
369
Greg Ward7b43c682002-12-30 02:29:28 +0000370 return _do_ioctl_1(self->fd, args, "speed", SNDCTL_DSP_SPEED);
Greg Ward131bce02002-11-30 22:56:44 +0000371}
372
373static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000374oss_sync(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000375{
Charles-François Natalia5293082011-06-11 18:58:24 +0200376 if (!_is_fd_valid(self->fd))
377 return NULL;
378
Greg Wardd0d592f2003-05-27 01:57:21 +0000379 return _do_ioctl_0(self->fd, args, "sync", SNDCTL_DSP_SYNC);
Greg Ward131bce02002-11-30 22:56:44 +0000380}
Guido van Rossum0741f802003-06-02 14:15:34 +0000381
Greg Ward131bce02002-11-30 22:56:44 +0000382static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000383oss_reset(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000384{
Charles-François Natalia5293082011-06-11 18:58:24 +0200385 if (!_is_fd_valid(self->fd))
386 return NULL;
387
Greg Ward7b43c682002-12-30 02:29:28 +0000388 return _do_ioctl_0(self->fd, args, "reset", SNDCTL_DSP_RESET);
Greg Ward131bce02002-11-30 22:56:44 +0000389}
Guido van Rossum0741f802003-06-02 14:15:34 +0000390
Greg Ward131bce02002-11-30 22:56:44 +0000391static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000392oss_post(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000393{
Charles-François Natalia5293082011-06-11 18:58:24 +0200394 if (!_is_fd_valid(self->fd))
395 return NULL;
396
Greg Ward7b43c682002-12-30 02:29:28 +0000397 return _do_ioctl_0(self->fd, args, "post", SNDCTL_DSP_POST);
Greg Ward131bce02002-11-30 22:56:44 +0000398}
399
400
401/* Regular file methods: read(), write(), close(), etc. as well
402 as one convenience method, writeall(). */
403
Greg Ward04613a92002-11-30 22:47:45 +0000404static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000405oss_read(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000406{
407 int size, count;
408 char *cp;
409 PyObject *rv;
Guido van Rossum0741f802003-06-02 14:15:34 +0000410
Charles-François Natalia5293082011-06-11 18:58:24 +0200411 if (!_is_fd_valid(self->fd))
412 return NULL;
413
Greg Ward04613a92002-11-30 22:47:45 +0000414 if (!PyArg_ParseTuple(args, "i:read", &size))
415 return NULL;
Gregory P. Smith0a608fd2008-09-06 21:34:51 +0000416 rv = PyBytes_FromStringAndSize(NULL, size);
Greg Ward04613a92002-11-30 22:47:45 +0000417 if (rv == NULL)
418 return NULL;
Gregory P. Smith0a608fd2008-09-06 21:34:51 +0000419 cp = PyBytes_AS_STRING(rv);
Greg Ward64927852003-05-23 01:50:37 +0000420
421 Py_BEGIN_ALLOW_THREADS
422 count = read(self->fd, cp, size);
423 Py_END_ALLOW_THREADS
424
425 if (count < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000426 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000427 Py_DECREF(rv);
428 return NULL;
429 }
Greg Ward7b43c682002-12-30 02:29:28 +0000430 self->icount += count;
Gregory P. Smith0a608fd2008-09-06 21:34:51 +0000431 _PyBytes_Resize(&rv, count);
Greg Ward04613a92002-11-30 22:47:45 +0000432 return rv;
433}
434
435static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000436oss_write(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000437{
438 char *cp;
439 int rv, size;
Greg Ward131bce02002-11-30 22:56:44 +0000440
Charles-François Natalia5293082011-06-11 18:58:24 +0200441 if (!_is_fd_valid(self->fd))
442 return NULL;
443
Neal Norwitz8abd2e62007-08-26 04:57:08 +0000444 if (!PyArg_ParseTuple(args, "y#:write", &cp, &size)) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000445 return NULL;
Greg Ward131bce02002-11-30 22:56:44 +0000446 }
Greg Ward64927852003-05-23 01:50:37 +0000447
448 Py_BEGIN_ALLOW_THREADS
449 rv = write(self->fd, cp, size);
450 Py_END_ALLOW_THREADS
451
452 if (rv == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000453 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000454 } else {
Greg Ward7b43c682002-12-30 02:29:28 +0000455 self->ocount += rv;
Greg Ward131bce02002-11-30 22:56:44 +0000456 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000457 return PyLong_FromLong(rv);
Greg Ward131bce02002-11-30 22:56:44 +0000458}
459
460static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000461oss_writeall(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000462{
463 char *cp;
464 int rv, size;
465 fd_set write_set_fds;
466 int select_rv;
Guido van Rossum0741f802003-06-02 14:15:34 +0000467
Greg Ward131bce02002-11-30 22:56:44 +0000468 /* NB. writeall() is only useful in non-blocking mode: according to
469 Guenter Geiger <geiger@xdv.org> on the linux-audio-dev list
470 (http://eca.cx/lad/2002/11/0380.html), OSS guarantees that
471 write() in blocking mode consumes the whole buffer. In blocking
472 mode, the behaviour of write() and writeall() from Python is
473 indistinguishable. */
474
Charles-François Natalia5293082011-06-11 18:58:24 +0200475 if (!_is_fd_valid(self->fd))
476 return NULL;
477
Neal Norwitz8abd2e62007-08-26 04:57:08 +0000478 if (!PyArg_ParseTuple(args, "y#:write", &cp, &size))
Greg Ward131bce02002-11-30 22:56:44 +0000479 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000480
Charles-François Nataliaa26b272011-08-28 17:51:43 +0200481 if (!_PyIsSelectable_fd(self->fd)) {
482 PyErr_SetString(PyExc_ValueError,
483 "file descriptor out of range for select");
484 return NULL;
485 }
Greg Ward04613a92002-11-30 22:47:45 +0000486 /* use select to wait for audio device to be available */
487 FD_ZERO(&write_set_fds);
Greg Ward7b43c682002-12-30 02:29:28 +0000488 FD_SET(self->fd, &write_set_fds);
Greg Ward04613a92002-11-30 22:47:45 +0000489
490 while (size > 0) {
Greg Ward64927852003-05-23 01:50:37 +0000491 Py_BEGIN_ALLOW_THREADS
Greg Ward7b43c682002-12-30 02:29:28 +0000492 select_rv = select(self->fd+1, NULL, &write_set_fds, NULL, NULL);
Greg Ward64927852003-05-23 01:50:37 +0000493 Py_END_ALLOW_THREADS
Greg Ward131bce02002-11-30 22:56:44 +0000494 assert(select_rv != 0); /* no timeout, can't expire */
495 if (select_rv == -1)
Greg Ward97708bc2002-11-30 23:17:10 +0000496 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000497
Greg Ward64927852003-05-23 01:50:37 +0000498 Py_BEGIN_ALLOW_THREADS
Greg Ward7b43c682002-12-30 02:29:28 +0000499 rv = write(self->fd, cp, size);
Greg Ward64927852003-05-23 01:50:37 +0000500 Py_END_ALLOW_THREADS
Greg Ward131bce02002-11-30 22:56:44 +0000501 if (rv == -1) {
502 if (errno == EAGAIN) { /* buffer is full, try again */
503 errno = 0;
504 continue;
505 } else /* it's a real error */
Greg Ward97708bc2002-11-30 23:17:10 +0000506 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward131bce02002-11-30 22:56:44 +0000507 } else { /* wrote rv bytes */
Greg Ward7b43c682002-12-30 02:29:28 +0000508 self->ocount += rv;
Greg Ward131bce02002-11-30 22:56:44 +0000509 size -= rv;
510 cp += rv;
511 }
Greg Ward04613a92002-11-30 22:47:45 +0000512 }
513 Py_INCREF(Py_None);
514 return Py_None;
515}
516
517static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000518oss_close(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000519{
Greg Ward7b43c682002-12-30 02:29:28 +0000520 if (self->fd >= 0) {
Greg Wardb8043902003-05-26 22:47:30 +0000521 Py_BEGIN_ALLOW_THREADS
Greg Ward7b43c682002-12-30 02:29:28 +0000522 close(self->fd);
Greg Wardb8043902003-05-26 22:47:30 +0000523 Py_END_ALLOW_THREADS
Greg Ward7b43c682002-12-30 02:29:28 +0000524 self->fd = -1;
Greg Ward04613a92002-11-30 22:47:45 +0000525 }
526 Py_INCREF(Py_None);
527 return Py_None;
528}
529
530static PyObject *
Georg Brandl770a2be2010-10-24 20:47:32 +0000531oss_self(PyObject *self, PyObject *unused)
Georg Brandl1e908af2010-10-23 17:31:52 +0000532{
533 Py_INCREF(self);
534 return self;
535}
536
Victor Stinner63941882011-09-29 00:42:28 +0200537static PyObject *
Georg Brandl1e908af2010-10-23 17:31:52 +0000538oss_exit(PyObject *self, PyObject *unused)
539{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200540 _Py_IDENTIFIER(close);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200541
542 PyObject *ret = _PyObject_CallMethodId(self, &PyId_close, NULL);
Georg Brandl1e908af2010-10-23 17:31:52 +0000543 if (!ret)
544 return NULL;
545 Py_DECREF(ret);
546 Py_RETURN_NONE;
547}
548
549static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000550oss_fileno(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000551{
Charles-François Natalia5293082011-06-11 18:58:24 +0200552 if (!_is_fd_valid(self->fd))
553 return NULL;
554
Christian Heimes217cfd12007-12-02 14:31:20 +0000555 return PyLong_FromLong(self->fd);
Greg Ward04613a92002-11-30 22:47:45 +0000556}
557
Greg Ward131bce02002-11-30 22:56:44 +0000558
559/* Convenience methods: these generally wrap a couple of ioctls into one
560 common task. */
561
Greg Ward04613a92002-11-30 22:47:45 +0000562static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000563oss_setparameters(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000564{
Greg Wardd6769062003-05-29 21:53:06 +0000565 int wanted_fmt, wanted_channels, wanted_rate, strict=0;
566 int fmt, channels, rate;
Greg Ward04613a92002-11-30 22:47:45 +0000567
Charles-François Natalia5293082011-06-11 18:58:24 +0200568 if (!_is_fd_valid(self->fd))
569 return NULL;
570
Greg Wardd6769062003-05-29 21:53:06 +0000571 if (!PyArg_ParseTuple(args, "iii|i:setparameters",
572 &wanted_fmt, &wanted_channels, &wanted_rate,
573 &strict))
Greg Ward04613a92002-11-30 22:47:45 +0000574 return NULL;
Greg Wardd6769062003-05-29 21:53:06 +0000575
576 fmt = wanted_fmt;
577 if (ioctl(self->fd, SNDCTL_DSP_SETFMT, &fmt) == -1) {
578 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000579 }
Greg Wardd6769062003-05-29 21:53:06 +0000580 if (strict && fmt != wanted_fmt) {
581 return PyErr_Format
582 (OSSAudioError,
583 "unable to set requested format (wanted %d, got %d)",
584 wanted_fmt, fmt);
Greg Ward04613a92002-11-30 22:47:45 +0000585 }
586
Greg Wardd6769062003-05-29 21:53:06 +0000587 channels = wanted_channels;
588 if (ioctl(self->fd, SNDCTL_DSP_CHANNELS, &channels) == -1) {
589 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000590 }
Greg Wardd6769062003-05-29 21:53:06 +0000591 if (strict && channels != wanted_channels) {
592 return PyErr_Format
593 (OSSAudioError,
594 "unable to set requested channels (wanted %d, got %d)",
595 wanted_channels, channels);
Greg Ward04613a92002-11-30 22:47:45 +0000596 }
597
Greg Wardd6769062003-05-29 21:53:06 +0000598 rate = wanted_rate;
Greg Ward7b43c682002-12-30 02:29:28 +0000599 if (ioctl(self->fd, SNDCTL_DSP_SPEED, &rate) == -1) {
Greg Wardd6769062003-05-29 21:53:06 +0000600 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000601 }
Greg Wardd6769062003-05-29 21:53:06 +0000602 if (strict && rate != wanted_rate) {
603 return PyErr_Format
604 (OSSAudioError,
605 "unable to set requested rate (wanted %d, got %d)",
606 wanted_rate, rate);
607 }
Guido van Rossum0741f802003-06-02 14:15:34 +0000608
Greg Wardd6769062003-05-29 21:53:06 +0000609 /* Construct the return value: a (fmt, channels, rate) tuple that
610 tells what the audio hardware was actually set to. */
Serhiy Storchaka48d761e2013-12-17 15:11:24 +0200611 return Py_BuildValue("(iii)", fmt, channels, rate);
Greg Ward04613a92002-11-30 22:47:45 +0000612}
613
614static int
Greg Ward499b73e2002-12-31 03:04:52 +0000615_ssize(oss_audio_t *self, int *nchannels, int *ssize)
Greg Ward04613a92002-11-30 22:47:45 +0000616{
617 int fmt;
618
619 fmt = 0;
Guido van Rossum0741f802003-06-02 14:15:34 +0000620 if (ioctl(self->fd, SNDCTL_DSP_SETFMT, &fmt) < 0)
Greg Ward04613a92002-11-30 22:47:45 +0000621 return -errno;
622
623 switch (fmt) {
624 case AFMT_MU_LAW:
625 case AFMT_A_LAW:
626 case AFMT_U8:
627 case AFMT_S8:
Greg Ward38c92662003-05-29 21:55:41 +0000628 *ssize = 1; /* 8 bit formats: 1 byte */
Greg Ward04613a92002-11-30 22:47:45 +0000629 break;
630 case AFMT_S16_LE:
631 case AFMT_S16_BE:
632 case AFMT_U16_LE:
633 case AFMT_U16_BE:
Greg Ward84f8ecd2003-05-29 23:44:44 +0000634 *ssize = 2; /* 16 bit formats: 2 byte */
Greg Ward04613a92002-11-30 22:47:45 +0000635 break;
636 case AFMT_MPEG:
637 case AFMT_IMA_ADPCM:
638 default:
639 return -EOPNOTSUPP;
640 }
Greg Ward7b43c682002-12-30 02:29:28 +0000641 if (ioctl(self->fd, SNDCTL_DSP_CHANNELS, nchannels) < 0)
Greg Ward04613a92002-11-30 22:47:45 +0000642 return -errno;
643 return 0;
644}
645
646
Guido van Rossum0741f802003-06-02 14:15:34 +0000647/* bufsize returns the size of the hardware audio buffer in number
Greg Ward04613a92002-11-30 22:47:45 +0000648 of samples */
649static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000650oss_bufsize(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000651{
652 audio_buf_info ai;
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000653 int nchannels=0, ssize=0;
Greg Ward04613a92002-11-30 22:47:45 +0000654
Charles-François Natalia5293082011-06-11 18:58:24 +0200655 if (!_is_fd_valid(self->fd))
656 return NULL;
657
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000658 if (_ssize(self, &nchannels, &ssize) < 0 || !nchannels || !ssize) {
Greg Ward97708bc2002-11-30 23:17:10 +0000659 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000660 return NULL;
661 }
Greg Ward7b43c682002-12-30 02:29:28 +0000662 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000663 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000664 return NULL;
665 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000666 return PyLong_FromLong((ai.fragstotal * ai.fragsize) / (nchannels * ssize));
Greg Ward04613a92002-11-30 22:47:45 +0000667}
668
Guido van Rossum0741f802003-06-02 14:15:34 +0000669/* obufcount returns the number of samples that are available in the
Greg Ward04613a92002-11-30 22:47:45 +0000670 hardware for playing */
671static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000672oss_obufcount(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000673{
674 audio_buf_info ai;
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000675 int nchannels=0, ssize=0;
Greg Ward04613a92002-11-30 22:47:45 +0000676
Charles-François Natalia5293082011-06-11 18:58:24 +0200677 if (!_is_fd_valid(self->fd))
678 return NULL;
679
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000680 if (_ssize(self, &nchannels, &ssize) < 0 || !nchannels || !ssize) {
Greg Ward97708bc2002-11-30 23:17:10 +0000681 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000682 return NULL;
683 }
Greg Ward7b43c682002-12-30 02:29:28 +0000684 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000685 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000686 return NULL;
687 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000688 return PyLong_FromLong((ai.fragstotal * ai.fragsize - ai.bytes) /
Greg Ward04613a92002-11-30 22:47:45 +0000689 (ssize * nchannels));
690}
691
692/* obufcount returns the number of samples that can be played without
693 blocking */
694static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000695oss_obuffree(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000696{
697 audio_buf_info ai;
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000698 int nchannels=0, ssize=0;
Greg Ward04613a92002-11-30 22:47:45 +0000699
Charles-François Natalia5293082011-06-11 18:58:24 +0200700 if (!_is_fd_valid(self->fd))
701 return NULL;
702
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000703 if (_ssize(self, &nchannels, &ssize) < 0 || !nchannels || !ssize) {
Greg Ward97708bc2002-11-30 23:17:10 +0000704 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000705 return NULL;
706 }
Greg Ward7b43c682002-12-30 02:29:28 +0000707 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000708 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000709 return NULL;
710 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000711 return PyLong_FromLong(ai.bytes / (ssize * nchannels));
Greg Ward04613a92002-11-30 22:47:45 +0000712}
713
Greg Ward04613a92002-11-30 22:47:45 +0000714static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000715oss_getptr(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000716{
717 count_info info;
718 int req;
719
Charles-François Natalia5293082011-06-11 18:58:24 +0200720 if (!_is_fd_valid(self->fd))
721 return NULL;
722
Greg Ward7b43c682002-12-30 02:29:28 +0000723 if (self->mode == O_RDONLY)
Greg Wardad4d9b92002-12-30 03:02:22 +0000724 req = SNDCTL_DSP_GETIPTR;
Greg Ward04613a92002-11-30 22:47:45 +0000725 else
Greg Wardad4d9b92002-12-30 03:02:22 +0000726 req = SNDCTL_DSP_GETOPTR;
Greg Ward7b43c682002-12-30 02:29:28 +0000727 if (ioctl(self->fd, req, &info) == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000728 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000729 return NULL;
730 }
731 return Py_BuildValue("iii", info.bytes, info.blocks, info.ptr);
732}
733
Greg Wardda1cacb2002-12-31 03:02:23 +0000734
735/* ----------------------------------------------------------------------
736 * Methods of mixer objects (OSSMixerType)
737 */
738
Greg Ward3d9994d2002-12-11 15:12:01 +0000739static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000740oss_mixer_close(oss_mixer_t *self, PyObject *unused)
Greg Ward3d9994d2002-12-11 15:12:01 +0000741{
Greg Ward7b43c682002-12-30 02:29:28 +0000742 if (self->fd >= 0) {
743 close(self->fd);
744 self->fd = -1;
Greg Ward3d9994d2002-12-11 15:12:01 +0000745 }
746 Py_INCREF(Py_None);
747 return Py_None;
748}
749
750static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000751oss_mixer_fileno(oss_mixer_t *self, PyObject *unused)
Greg Ward3d9994d2002-12-11 15:12:01 +0000752{
Charles-François Natalia5293082011-06-11 18:58:24 +0200753 if (!_is_fd_valid(self->fd))
754 return NULL;
755
Christian Heimes217cfd12007-12-02 14:31:20 +0000756 return PyLong_FromLong(self->fd);
Greg Ward3d9994d2002-12-11 15:12:01 +0000757}
758
759/* Simple mixer interface methods */
760
761static PyObject *
Greg Ward2d6f9a92002-12-31 02:54:43 +0000762oss_mixer_controls(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000763{
Charles-François Natalia5293082011-06-11 18:58:24 +0200764 if (!_is_fd_valid(self->fd))
765 return NULL;
766
Greg Ward2d6f9a92002-12-31 02:54:43 +0000767 return _do_ioctl_1_internal(self->fd, args, "controls",
Greg Ward3d9994d2002-12-11 15:12:01 +0000768 SOUND_MIXER_READ_DEVMASK);
769}
770
771static PyObject *
Greg Ward2d6f9a92002-12-31 02:54:43 +0000772oss_mixer_stereocontrols(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000773{
Charles-François Natalia5293082011-06-11 18:58:24 +0200774 if (!_is_fd_valid(self->fd))
775 return NULL;
776
Greg Ward2d6f9a92002-12-31 02:54:43 +0000777 return _do_ioctl_1_internal(self->fd, args, "stereocontrols",
Greg Ward3d9994d2002-12-11 15:12:01 +0000778 SOUND_MIXER_READ_STEREODEVS);
779}
780
781static PyObject *
Greg Ward2d6f9a92002-12-31 02:54:43 +0000782oss_mixer_reccontrols(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000783{
Charles-François Natalia5293082011-06-11 18:58:24 +0200784 if (!_is_fd_valid(self->fd))
785 return NULL;
786
Greg Ward2d6f9a92002-12-31 02:54:43 +0000787 return _do_ioctl_1_internal(self->fd, args, "reccontrols",
Greg Wardb69bb3d2002-12-12 17:35:45 +0000788 SOUND_MIXER_READ_RECMASK);
Greg Ward3d9994d2002-12-11 15:12:01 +0000789}
790
791static PyObject *
Greg Warde7037662002-12-30 03:01:48 +0000792oss_mixer_get(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000793{
794 int channel, volume;
Guido van Rossum0741f802003-06-02 14:15:34 +0000795
Charles-François Natalia5293082011-06-11 18:58:24 +0200796 if (!_is_fd_valid(self->fd))
797 return NULL;
798
Greg Ward3d9994d2002-12-11 15:12:01 +0000799 /* Can't use _do_ioctl_1 because of encoded arg thingy. */
Greg Warde7037662002-12-30 03:01:48 +0000800 if (!PyArg_ParseTuple(args, "i:get", &channel))
Greg Wardad4d9b92002-12-30 03:02:22 +0000801 return NULL;
Guido van Rossum0741f802003-06-02 14:15:34 +0000802
Greg Ward3d9994d2002-12-11 15:12:01 +0000803 if (channel < 0 || channel > SOUND_MIXER_NRDEVICES) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000804 PyErr_SetString(OSSAudioError, "Invalid mixer channel specified.");
805 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000806 }
Guido van Rossum0741f802003-06-02 14:15:34 +0000807
Greg Warde7037662002-12-30 03:01:48 +0000808 if (ioctl(self->fd, MIXER_READ(channel), &volume) == -1)
Greg Wardad4d9b92002-12-30 03:02:22 +0000809 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum0741f802003-06-02 14:15:34 +0000810
Greg Warde7037662002-12-30 03:01:48 +0000811 return Py_BuildValue("(ii)", volume & 0xff, (volume & 0xff00) >> 8);
Greg Ward3d9994d2002-12-11 15:12:01 +0000812}
813
814static PyObject *
Greg Warde7037662002-12-30 03:01:48 +0000815oss_mixer_set(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000816{
817 int channel, volume, leftVol, rightVol;
Guido van Rossum0741f802003-06-02 14:15:34 +0000818
Charles-François Natalia5293082011-06-11 18:58:24 +0200819 if (!_is_fd_valid(self->fd))
820 return NULL;
821
Greg Ward3d9994d2002-12-11 15:12:01 +0000822 /* Can't use _do_ioctl_1 because of encoded arg thingy. */
Greg Warde7037662002-12-30 03:01:48 +0000823 if (!PyArg_ParseTuple(args, "i(ii):set", &channel, &leftVol, &rightVol))
Greg Wardad4d9b92002-12-30 03:02:22 +0000824 return NULL;
Guido van Rossum0741f802003-06-02 14:15:34 +0000825
Greg Ward3d9994d2002-12-11 15:12:01 +0000826 if (channel < 0 || channel > SOUND_MIXER_NRDEVICES) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000827 PyErr_SetString(OSSAudioError, "Invalid mixer channel specified.");
828 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000829 }
Guido van Rossum0741f802003-06-02 14:15:34 +0000830
Greg Ward3d9994d2002-12-11 15:12:01 +0000831 if (leftVol < 0 || rightVol < 0 || leftVol > 100 || rightVol > 100) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000832 PyErr_SetString(OSSAudioError, "Volumes must be between 0 and 100.");
833 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000834 }
835
836 volume = (rightVol << 8) | leftVol;
Guido van Rossum0741f802003-06-02 14:15:34 +0000837
Greg Warde7037662002-12-30 03:01:48 +0000838 if (ioctl(self->fd, MIXER_WRITE(channel), &volume) == -1)
Greg Wardad4d9b92002-12-30 03:02:22 +0000839 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum0741f802003-06-02 14:15:34 +0000840
Greg Warde7037662002-12-30 03:01:48 +0000841 return Py_BuildValue("(ii)", volume & 0xff, (volume & 0xff00) >> 8);
Greg Ward3d9994d2002-12-11 15:12:01 +0000842}
843
844static PyObject *
Greg Wardf05aa102002-12-30 23:19:32 +0000845oss_mixer_get_recsrc(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000846{
Charles-François Natalia5293082011-06-11 18:58:24 +0200847 if (!_is_fd_valid(self->fd))
848 return NULL;
849
Greg Wardf05aa102002-12-30 23:19:32 +0000850 return _do_ioctl_1_internal(self->fd, args, "get_recsrc",
Greg Ward3d9994d2002-12-11 15:12:01 +0000851 SOUND_MIXER_READ_RECSRC);
852}
853
854static PyObject *
Greg Wardf05aa102002-12-30 23:19:32 +0000855oss_mixer_set_recsrc(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000856{
Charles-François Natalia5293082011-06-11 18:58:24 +0200857 if (!_is_fd_valid(self->fd))
858 return NULL;
859
Greg Wardf05aa102002-12-30 23:19:32 +0000860 return _do_ioctl_1(self->fd, args, "set_recsrc",
Greg Ward3d9994d2002-12-11 15:12:01 +0000861 SOUND_MIXER_WRITE_RECSRC);
862}
863
864
Greg Wardda1cacb2002-12-31 03:02:23 +0000865/* ----------------------------------------------------------------------
866 * Method tables and other bureaucracy
867 */
868
Greg Ward8c6b6a92002-12-11 14:43:13 +0000869static PyMethodDef oss_methods[] = {
Greg Ward131bce02002-11-30 22:56:44 +0000870 /* Regular file methods */
Greg Wardad4d9b92002-12-30 03:02:22 +0000871 { "read", (PyCFunction)oss_read, METH_VARARGS },
872 { "write", (PyCFunction)oss_write, METH_VARARGS },
873 { "writeall", (PyCFunction)oss_writeall, METH_VARARGS },
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000874 { "close", (PyCFunction)oss_close, METH_NOARGS },
875 { "fileno", (PyCFunction)oss_fileno, METH_NOARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000876
877 /* Simple ioctl wrappers */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000878 { "nonblock", (PyCFunction)oss_nonblock, METH_NOARGS },
Greg Wardad4d9b92002-12-30 03:02:22 +0000879 { "setfmt", (PyCFunction)oss_setfmt, METH_VARARGS },
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000880 { "getfmts", (PyCFunction)oss_getfmts, METH_NOARGS },
Greg Ward8c6b6a92002-12-11 14:43:13 +0000881 { "channels", (PyCFunction)oss_channels, METH_VARARGS },
882 { "speed", (PyCFunction)oss_speed, METH_VARARGS },
Greg Wardad4d9b92002-12-30 03:02:22 +0000883 { "sync", (PyCFunction)oss_sync, METH_VARARGS },
884 { "reset", (PyCFunction)oss_reset, METH_VARARGS },
885 { "post", (PyCFunction)oss_post, METH_VARARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000886
887 /* Convenience methods -- wrap a couple of ioctls together */
Greg Wardad4d9b92002-12-30 03:02:22 +0000888 { "setparameters", (PyCFunction)oss_setparameters, METH_VARARGS },
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000889 { "bufsize", (PyCFunction)oss_bufsize, METH_NOARGS },
890 { "obufcount", (PyCFunction)oss_obufcount, METH_NOARGS },
891 { "obuffree", (PyCFunction)oss_obuffree, METH_NOARGS },
892 { "getptr", (PyCFunction)oss_getptr, METH_NOARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000893
894 /* Aliases for backwards compatibility */
Greg Wardad4d9b92002-12-30 03:02:22 +0000895 { "flush", (PyCFunction)oss_sync, METH_VARARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000896
Georg Brandl1e908af2010-10-23 17:31:52 +0000897 /* Support for the context manager protocol */
Georg Brandl770a2be2010-10-24 20:47:32 +0000898 { "__enter__", oss_self, METH_NOARGS },
899 { "__exit__", oss_exit, METH_VARARGS },
Georg Brandl1e908af2010-10-23 17:31:52 +0000900
Greg Wardad4d9b92002-12-30 03:02:22 +0000901 { NULL, NULL} /* sentinel */
Greg Ward04613a92002-11-30 22:47:45 +0000902};
903
Greg Ward3d9994d2002-12-11 15:12:01 +0000904static PyMethodDef oss_mixer_methods[] = {
905 /* Regular file method - OSS mixers are ioctl-only interface */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000906 { "close", (PyCFunction)oss_mixer_close, METH_NOARGS },
907 { "fileno", (PyCFunction)oss_mixer_fileno, METH_NOARGS },
Greg Ward3d9994d2002-12-11 15:12:01 +0000908
Georg Brandl1e908af2010-10-23 17:31:52 +0000909 /* Support for the context manager protocol */
Georg Brandl770a2be2010-10-24 20:47:32 +0000910 { "__enter__", oss_self, METH_NOARGS },
911 { "__exit__", oss_exit, METH_VARARGS },
Georg Brandl1e908af2010-10-23 17:31:52 +0000912
Greg Ward3d9994d2002-12-11 15:12:01 +0000913 /* Simple ioctl wrappers */
Guido van Rossum0741f802003-06-02 14:15:34 +0000914 { "controls", (PyCFunction)oss_mixer_controls, METH_VARARGS },
Greg Ward2d6f9a92002-12-31 02:54:43 +0000915 { "stereocontrols", (PyCFunction)oss_mixer_stereocontrols, METH_VARARGS},
Guido van Rossum0741f802003-06-02 14:15:34 +0000916 { "reccontrols", (PyCFunction)oss_mixer_reccontrols, METH_VARARGS},
Greg Wardad4d9b92002-12-30 03:02:22 +0000917 { "get", (PyCFunction)oss_mixer_get, METH_VARARGS },
918 { "set", (PyCFunction)oss_mixer_set, METH_VARARGS },
Greg Wardf05aa102002-12-30 23:19:32 +0000919 { "get_recsrc", (PyCFunction)oss_mixer_get_recsrc, METH_VARARGS },
920 { "set_recsrc", (PyCFunction)oss_mixer_set_recsrc, METH_VARARGS },
Guido van Rossum0741f802003-06-02 14:15:34 +0000921
Greg Wardad4d9b92002-12-30 03:02:22 +0000922 { NULL, NULL}
Greg Ward3d9994d2002-12-11 15:12:01 +0000923};
924
Greg Ward04613a92002-11-30 22:47:45 +0000925static PyObject *
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000926oss_getattro(oss_audio_t *self, PyObject *nameobj)
Greg Ward04613a92002-11-30 22:47:45 +0000927{
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000928 char *name = "";
Greg Ward50682d02005-03-07 01:41:11 +0000929 PyObject * rval = NULL;
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000930
931 if (PyUnicode_Check(nameobj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 name = _PyUnicode_AsString(nameobj);
933
Greg Ward50682d02005-03-07 01:41:11 +0000934 if (strcmp(name, "closed") == 0) {
935 rval = (self->fd == -1) ? Py_True : Py_False;
936 Py_INCREF(rval);
937 }
938 else if (strcmp(name, "name") == 0) {
Neal Norwitz8abd2e62007-08-26 04:57:08 +0000939 rval = PyUnicode_FromString(self->devicename);
Greg Ward50682d02005-03-07 01:41:11 +0000940 }
941 else if (strcmp(name, "mode") == 0) {
942 /* No need for a "default" in this switch: from newossobject(),
943 self->mode can only be one of these three values. */
944 switch(self->mode) {
945 case O_RDONLY:
Neal Norwitz8abd2e62007-08-26 04:57:08 +0000946 rval = PyUnicode_FromString("r");
Greg Ward50682d02005-03-07 01:41:11 +0000947 break;
948 case O_RDWR:
Neal Norwitz8abd2e62007-08-26 04:57:08 +0000949 rval = PyUnicode_FromString("rw");
Greg Ward50682d02005-03-07 01:41:11 +0000950 break;
951 case O_WRONLY:
Neal Norwitz8abd2e62007-08-26 04:57:08 +0000952 rval = PyUnicode_FromString("w");
Greg Ward50682d02005-03-07 01:41:11 +0000953 break;
954 }
955 }
956 else {
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000957 rval = PyObject_GenericGetAttr((PyObject *)self, nameobj);
Greg Ward50682d02005-03-07 01:41:11 +0000958 }
959 return rval;
Greg Ward04613a92002-11-30 22:47:45 +0000960}
961
Greg Ward499b73e2002-12-31 03:04:52 +0000962static PyTypeObject OSSAudioType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000963 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Greg Ward9a568eb2002-11-30 23:20:09 +0000964 "ossaudiodev.oss_audio_device", /*tp_name*/
Greg Ward499b73e2002-12-31 03:04:52 +0000965 sizeof(oss_audio_t), /*tp_size*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000966 0, /*tp_itemsize*/
Greg Ward04613a92002-11-30 22:47:45 +0000967 /* methods */
Greg Wardad4d9b92002-12-30 03:02:22 +0000968 (destructor)oss_dealloc, /*tp_dealloc*/
969 0, /*tp_print*/
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000970 0, /*tp_getattr*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000971 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000972 0, /*tp_reserved*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000973 0, /*tp_repr*/
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000974 0, /*tp_as_number*/
975 0, /*tp_as_sequence*/
976 0, /*tp_as_mapping*/
977 0, /*tp_hash*/
978 0, /*tp_call*/
979 0, /*tp_str*/
980 (getattrofunc)oss_getattro, /*tp_getattro*/
981 0, /*tp_setattro*/
982 0, /*tp_as_buffer*/
983 Py_TPFLAGS_DEFAULT, /*tp_flags*/
984 0, /*tp_doc*/
985 0, /*tp_traverse*/
986 0, /*tp_clear*/
987 0, /*tp_richcompare*/
988 0, /*tp_weaklistoffset*/
989 0, /*tp_iter*/
990 0, /*tp_iternext*/
991 oss_methods, /*tp_methods*/
Greg Ward04613a92002-11-30 22:47:45 +0000992};
993
Greg Ward3d9994d2002-12-11 15:12:01 +0000994static PyTypeObject OSSMixerType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000995 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Greg Ward3d9994d2002-12-11 15:12:01 +0000996 "ossaudiodev.oss_mixer_device", /*tp_name*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000997 sizeof(oss_mixer_t), /*tp_size*/
998 0, /*tp_itemsize*/
Greg Ward3d9994d2002-12-11 15:12:01 +0000999 /* methods */
1000 (destructor)oss_mixer_dealloc, /*tp_dealloc*/
Greg Wardad4d9b92002-12-30 03:02:22 +00001001 0, /*tp_print*/
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +00001002 0, /*tp_getattr*/
Greg Wardad4d9b92002-12-30 03:02:22 +00001003 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001004 0, /*tp_reserved*/
Greg Wardad4d9b92002-12-30 03:02:22 +00001005 0, /*tp_repr*/
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +00001006 0, /*tp_as_number*/
1007 0, /*tp_as_sequence*/
1008 0, /*tp_as_mapping*/
1009 0, /*tp_hash*/
1010 0, /*tp_call*/
1011 0, /*tp_str*/
1012 0, /*tp_getattro*/
1013 0, /*tp_setattro*/
1014 0, /*tp_as_buffer*/
1015 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1016 0, /*tp_doc*/
1017 0, /*tp_traverse*/
1018 0, /*tp_clear*/
1019 0, /*tp_richcompare*/
1020 0, /*tp_weaklistoffset*/
1021 0, /*tp_iter*/
1022 0, /*tp_iternext*/
1023 oss_mixer_methods, /*tp_methods*/
Greg Ward3d9994d2002-12-11 15:12:01 +00001024};
1025
1026
Greg Ward04613a92002-11-30 22:47:45 +00001027static PyObject *
Greg Ward9a568eb2002-11-30 23:20:09 +00001028ossopen(PyObject *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +00001029{
Greg Ward8c6b6a92002-12-11 14:43:13 +00001030 return (PyObject *)newossobject(args);
Greg Ward04613a92002-11-30 22:47:45 +00001031}
1032
Greg Ward3d9994d2002-12-11 15:12:01 +00001033static PyObject *
1034ossopenmixer(PyObject *self, PyObject *args)
1035{
1036 return (PyObject *)newossmixerobject(args);
1037}
1038
Greg Ward9a568eb2002-11-30 23:20:09 +00001039static PyMethodDef ossaudiodev_methods[] = {
1040 { "open", ossopen, METH_VARARGS },
Greg Ward3d9994d2002-12-11 15:12:01 +00001041 { "openmixer", ossopenmixer, METH_VARARGS },
Greg Ward04613a92002-11-30 22:47:45 +00001042 { 0, 0 },
1043};
1044
Greg Ward1e0f57d2002-11-30 23:05:26 +00001045
1046#define _EXPORT_INT(mod, name) \
Martin v. Löwis1a214512008-06-11 05:26:20 +00001047 if (PyModule_AddIntConstant(mod, #name, (long) (name)) == -1) return NULL;
Greg Ward1e0f57d2002-11-30 23:05:26 +00001048
Greg Ward744f0fd2002-12-31 03:23:59 +00001049
1050static char *control_labels[] = SOUND_DEVICE_LABELS;
1051static char *control_names[] = SOUND_DEVICE_NAMES;
1052
1053
1054static int
1055build_namelists (PyObject *module)
1056{
1057 PyObject *labels;
1058 PyObject *names;
1059 PyObject *s;
1060 int num_controls;
1061 int i;
1062
Victor Stinner63941882011-09-29 00:42:28 +02001063 num_controls = Py_ARRAY_LENGTH(control_labels);
1064 assert(num_controls == Py_ARRAY_LENGTH(control_names));
Greg Ward744f0fd2002-12-31 03:23:59 +00001065
1066 labels = PyList_New(num_controls);
1067 names = PyList_New(num_controls);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001068 if (labels == NULL || names == NULL)
1069 goto error2;
Greg Ward744f0fd2002-12-31 03:23:59 +00001070 for (i = 0; i < num_controls; i++) {
Neal Norwitz8abd2e62007-08-26 04:57:08 +00001071 s = PyUnicode_FromString(control_labels[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(labels, i, s);
Guido van Rossum0741f802003-06-02 14:15:34 +00001075
Neal Norwitz8abd2e62007-08-26 04:57:08 +00001076 s = PyUnicode_FromString(control_names[i]);
Greg Ward744f0fd2002-12-31 03:23:59 +00001077 if (s == NULL)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001078 goto error2;
Greg Ward744f0fd2002-12-31 03:23:59 +00001079 PyList_SET_ITEM(names, i, s);
1080 }
1081
1082 if (PyModule_AddObject(module, "control_labels", labels) == -1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001083 goto error2;
Greg Ward744f0fd2002-12-31 03:23:59 +00001084 if (PyModule_AddObject(module, "control_names", names) == -1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001085 goto error1;
Greg Ward744f0fd2002-12-31 03:23:59 +00001086
1087 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001088
1089error2:
1090 Py_XDECREF(labels);
1091error1:
1092 Py_XDECREF(names);
1093 return -1;
Guido van Rossum0741f802003-06-02 14:15:34 +00001094}
Greg Ward744f0fd2002-12-31 03:23:59 +00001095
1096
Martin v. Löwis1a214512008-06-11 05:26:20 +00001097static struct PyModuleDef ossaudiodevmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 PyModuleDef_HEAD_INIT,
1099 "ossaudiodev",
1100 NULL,
1101 -1,
1102 ossaudiodev_methods,
1103 NULL,
1104 NULL,
1105 NULL,
1106 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001107};
1108
Antoine Pitrou39b35432010-03-23 00:25:54 +00001109PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001110PyInit_ossaudiodev(void)
Greg Ward04613a92002-11-30 22:47:45 +00001111{
1112 PyObject *m;
Guido van Rossum0741f802003-06-02 14:15:34 +00001113
Antoine Pitrou39b35432010-03-23 00:25:54 +00001114 if (PyType_Ready(&OSSAudioType) < 0)
1115 return NULL;
1116
1117 if (PyType_Ready(&OSSMixerType) < 0)
1118 return NULL;
1119
Martin v. Löwis1a214512008-06-11 05:26:20 +00001120 m = PyModule_Create(&ossaudiodevmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001121 if (m == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +00001123
Guido van Rossum0741f802003-06-02 14:15:34 +00001124 OSSAudioError = PyErr_NewException("ossaudiodev.OSSAudioError",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 NULL, NULL);
Greg Wardd6769062003-05-29 21:53:06 +00001126 if (OSSAudioError) {
Guido van Rossum0741f802003-06-02 14:15:34 +00001127 /* Each call to PyModule_AddObject decrefs it; compensate: */
1128 Py_INCREF(OSSAudioError);
1129 Py_INCREF(OSSAudioError);
Greg Wardad4d9b92002-12-30 03:02:22 +00001130 PyModule_AddObject(m, "error", OSSAudioError);
Greg Wardd6769062003-05-29 21:53:06 +00001131 PyModule_AddObject(m, "OSSAudioError", OSSAudioError);
1132 }
Greg Ward04613a92002-11-30 22:47:45 +00001133
Greg Ward744f0fd2002-12-31 03:23:59 +00001134 /* Build 'control_labels' and 'control_names' lists and add them
1135 to the module. */
1136 if (build_namelists(m) == -1) /* XXX what to do here? */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001137 return NULL;
Greg Ward744f0fd2002-12-31 03:23:59 +00001138
Greg Ward1e0f57d2002-11-30 23:05:26 +00001139 /* Expose the audio format numbers -- essential! */
1140 _EXPORT_INT(m, AFMT_QUERY);
1141 _EXPORT_INT(m, AFMT_MU_LAW);
1142 _EXPORT_INT(m, AFMT_A_LAW);
1143 _EXPORT_INT(m, AFMT_IMA_ADPCM);
1144 _EXPORT_INT(m, AFMT_U8);
1145 _EXPORT_INT(m, AFMT_S16_LE);
1146 _EXPORT_INT(m, AFMT_S16_BE);
1147 _EXPORT_INT(m, AFMT_S8);
1148 _EXPORT_INT(m, AFMT_U16_LE);
1149 _EXPORT_INT(m, AFMT_U16_BE);
1150 _EXPORT_INT(m, AFMT_MPEG);
Andrew M. Kuchlingfe62bc92003-02-13 13:27:07 +00001151#ifdef AFMT_AC3
Greg Ward1e0f57d2002-11-30 23:05:26 +00001152 _EXPORT_INT(m, AFMT_AC3);
Andrew M. Kuchlingfe62bc92003-02-13 13:27:07 +00001153#endif
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001154#ifdef AFMT_S16_NE
Greg Ward1e0f57d2002-11-30 23:05:26 +00001155 _EXPORT_INT(m, AFMT_S16_NE);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001156#endif
Greg Ward0f260542005-03-28 02:40:46 +00001157#ifdef AFMT_U16_NE
1158 _EXPORT_INT(m, AFMT_U16_NE);
1159#endif
1160#ifdef AFMT_S32_LE
1161 _EXPORT_INT(m, AFMT_S32_LE);
1162#endif
1163#ifdef AFMT_S32_BE
1164 _EXPORT_INT(m, AFMT_S32_BE);
1165#endif
1166#ifdef AFMT_MPEG
1167 _EXPORT_INT(m, AFMT_MPEG);
1168#endif
Guido van Rossum0741f802003-06-02 14:15:34 +00001169
Greg Ward37897c22002-12-30 02:43:36 +00001170 /* Expose the sound mixer device numbers. */
Greg Ward7b43c682002-12-30 02:29:28 +00001171 _EXPORT_INT(m, SOUND_MIXER_NRDEVICES);
Greg Ward3d9994d2002-12-11 15:12:01 +00001172 _EXPORT_INT(m, SOUND_MIXER_VOLUME);
1173 _EXPORT_INT(m, SOUND_MIXER_BASS);
1174 _EXPORT_INT(m, SOUND_MIXER_TREBLE);
1175 _EXPORT_INT(m, SOUND_MIXER_SYNTH);
1176 _EXPORT_INT(m, SOUND_MIXER_PCM);
1177 _EXPORT_INT(m, SOUND_MIXER_SPEAKER);
1178 _EXPORT_INT(m, SOUND_MIXER_LINE);
1179 _EXPORT_INT(m, SOUND_MIXER_MIC);
1180 _EXPORT_INT(m, SOUND_MIXER_CD);
1181 _EXPORT_INT(m, SOUND_MIXER_IMIX);
1182 _EXPORT_INT(m, SOUND_MIXER_ALTPCM);
1183 _EXPORT_INT(m, SOUND_MIXER_RECLEV);
1184 _EXPORT_INT(m, SOUND_MIXER_IGAIN);
1185 _EXPORT_INT(m, SOUND_MIXER_OGAIN);
1186 _EXPORT_INT(m, SOUND_MIXER_LINE1);
1187 _EXPORT_INT(m, SOUND_MIXER_LINE2);
1188 _EXPORT_INT(m, SOUND_MIXER_LINE3);
Greg Wardfd0283e2004-05-11 01:34:55 +00001189#ifdef SOUND_MIXER_DIGITAL1
Greg Ward3d9994d2002-12-11 15:12:01 +00001190 _EXPORT_INT(m, SOUND_MIXER_DIGITAL1);
Greg Wardfd0283e2004-05-11 01:34:55 +00001191#endif
1192#ifdef SOUND_MIXER_DIGITAL2
Greg Ward3d9994d2002-12-11 15:12:01 +00001193 _EXPORT_INT(m, SOUND_MIXER_DIGITAL2);
Greg Wardfd0283e2004-05-11 01:34:55 +00001194#endif
1195#ifdef SOUND_MIXER_DIGITAL3
Greg Ward3d9994d2002-12-11 15:12:01 +00001196 _EXPORT_INT(m, SOUND_MIXER_DIGITAL3);
Greg Wardfd0283e2004-05-11 01:34:55 +00001197#endif
1198#ifdef SOUND_MIXER_PHONEIN
Greg Ward3d9994d2002-12-11 15:12:01 +00001199 _EXPORT_INT(m, SOUND_MIXER_PHONEIN);
Greg Wardfd0283e2004-05-11 01:34:55 +00001200#endif
1201#ifdef SOUND_MIXER_PHONEOUT
Greg Ward3d9994d2002-12-11 15:12:01 +00001202 _EXPORT_INT(m, SOUND_MIXER_PHONEOUT);
Greg Wardfd0283e2004-05-11 01:34:55 +00001203#endif
1204#ifdef SOUND_MIXER_VIDEO
Greg Ward3d9994d2002-12-11 15:12:01 +00001205 _EXPORT_INT(m, SOUND_MIXER_VIDEO);
Greg Wardfd0283e2004-05-11 01:34:55 +00001206#endif
1207#ifdef SOUND_MIXER_RADIO
Greg Ward3d9994d2002-12-11 15:12:01 +00001208 _EXPORT_INT(m, SOUND_MIXER_RADIO);
Greg Wardfd0283e2004-05-11 01:34:55 +00001209#endif
1210#ifdef SOUND_MIXER_MONITOR
Greg Ward3d9994d2002-12-11 15:12:01 +00001211 _EXPORT_INT(m, SOUND_MIXER_MONITOR);
Greg Wardfd0283e2004-05-11 01:34:55 +00001212#endif
Greg Ward04613a92002-11-30 22:47:45 +00001213
Greg Ward1e0f57d2002-11-30 23:05:26 +00001214 /* Expose all the ioctl numbers for masochists who like to do this
1215 stuff directly. */
1216 _EXPORT_INT(m, SNDCTL_COPR_HALT);
1217 _EXPORT_INT(m, SNDCTL_COPR_LOAD);
1218 _EXPORT_INT(m, SNDCTL_COPR_RCODE);
1219 _EXPORT_INT(m, SNDCTL_COPR_RCVMSG);
1220 _EXPORT_INT(m, SNDCTL_COPR_RDATA);
1221 _EXPORT_INT(m, SNDCTL_COPR_RESET);
1222 _EXPORT_INT(m, SNDCTL_COPR_RUN);
1223 _EXPORT_INT(m, SNDCTL_COPR_SENDMSG);
1224 _EXPORT_INT(m, SNDCTL_COPR_WCODE);
1225 _EXPORT_INT(m, SNDCTL_COPR_WDATA);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001226#ifdef SNDCTL_DSP_BIND_CHANNEL
Greg Ward1e0f57d2002-11-30 23:05:26 +00001227 _EXPORT_INT(m, SNDCTL_DSP_BIND_CHANNEL);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001228#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001229 _EXPORT_INT(m, SNDCTL_DSP_CHANNELS);
1230 _EXPORT_INT(m, SNDCTL_DSP_GETBLKSIZE);
1231 _EXPORT_INT(m, SNDCTL_DSP_GETCAPS);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001232#ifdef SNDCTL_DSP_GETCHANNELMASK
Greg Ward1e0f57d2002-11-30 23:05:26 +00001233 _EXPORT_INT(m, SNDCTL_DSP_GETCHANNELMASK);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001234#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001235 _EXPORT_INT(m, SNDCTL_DSP_GETFMTS);
1236 _EXPORT_INT(m, SNDCTL_DSP_GETIPTR);
1237 _EXPORT_INT(m, SNDCTL_DSP_GETISPACE);
Greg Wardfd0283e2004-05-11 01:34:55 +00001238#ifdef SNDCTL_DSP_GETODELAY
Greg Ward1e0f57d2002-11-30 23:05:26 +00001239 _EXPORT_INT(m, SNDCTL_DSP_GETODELAY);
Greg Wardfd0283e2004-05-11 01:34:55 +00001240#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001241 _EXPORT_INT(m, SNDCTL_DSP_GETOPTR);
1242 _EXPORT_INT(m, SNDCTL_DSP_GETOSPACE);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001243#ifdef SNDCTL_DSP_GETSPDIF
Greg Ward1e0f57d2002-11-30 23:05:26 +00001244 _EXPORT_INT(m, SNDCTL_DSP_GETSPDIF);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001245#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001246 _EXPORT_INT(m, SNDCTL_DSP_GETTRIGGER);
1247 _EXPORT_INT(m, SNDCTL_DSP_MAPINBUF);
1248 _EXPORT_INT(m, SNDCTL_DSP_MAPOUTBUF);
1249 _EXPORT_INT(m, SNDCTL_DSP_NONBLOCK);
1250 _EXPORT_INT(m, SNDCTL_DSP_POST);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001251#ifdef SNDCTL_DSP_PROFILE
Greg Ward1e0f57d2002-11-30 23:05:26 +00001252 _EXPORT_INT(m, SNDCTL_DSP_PROFILE);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001253#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001254 _EXPORT_INT(m, SNDCTL_DSP_RESET);
1255 _EXPORT_INT(m, SNDCTL_DSP_SAMPLESIZE);
1256 _EXPORT_INT(m, SNDCTL_DSP_SETDUPLEX);
1257 _EXPORT_INT(m, SNDCTL_DSP_SETFMT);
1258 _EXPORT_INT(m, SNDCTL_DSP_SETFRAGMENT);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001259#ifdef SNDCTL_DSP_SETSPDIF
Greg Ward1e0f57d2002-11-30 23:05:26 +00001260 _EXPORT_INT(m, SNDCTL_DSP_SETSPDIF);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001261#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001262 _EXPORT_INT(m, SNDCTL_DSP_SETSYNCRO);
1263 _EXPORT_INT(m, SNDCTL_DSP_SETTRIGGER);
1264 _EXPORT_INT(m, SNDCTL_DSP_SPEED);
1265 _EXPORT_INT(m, SNDCTL_DSP_STEREO);
1266 _EXPORT_INT(m, SNDCTL_DSP_SUBDIVIDE);
1267 _EXPORT_INT(m, SNDCTL_DSP_SYNC);
1268 _EXPORT_INT(m, SNDCTL_FM_4OP_ENABLE);
1269 _EXPORT_INT(m, SNDCTL_FM_LOAD_INSTR);
1270 _EXPORT_INT(m, SNDCTL_MIDI_INFO);
1271 _EXPORT_INT(m, SNDCTL_MIDI_MPUCMD);
1272 _EXPORT_INT(m, SNDCTL_MIDI_MPUMODE);
1273 _EXPORT_INT(m, SNDCTL_MIDI_PRETIME);
1274 _EXPORT_INT(m, SNDCTL_SEQ_CTRLRATE);
1275 _EXPORT_INT(m, SNDCTL_SEQ_GETINCOUNT);
1276 _EXPORT_INT(m, SNDCTL_SEQ_GETOUTCOUNT);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001277#ifdef SNDCTL_SEQ_GETTIME
Greg Ward1e0f57d2002-11-30 23:05:26 +00001278 _EXPORT_INT(m, SNDCTL_SEQ_GETTIME);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001279#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001280 _EXPORT_INT(m, SNDCTL_SEQ_NRMIDIS);
1281 _EXPORT_INT(m, SNDCTL_SEQ_NRSYNTHS);
1282 _EXPORT_INT(m, SNDCTL_SEQ_OUTOFBAND);
1283 _EXPORT_INT(m, SNDCTL_SEQ_PANIC);
1284 _EXPORT_INT(m, SNDCTL_SEQ_PERCMODE);
1285 _EXPORT_INT(m, SNDCTL_SEQ_RESET);
1286 _EXPORT_INT(m, SNDCTL_SEQ_RESETSAMPLES);
1287 _EXPORT_INT(m, SNDCTL_SEQ_SYNC);
1288 _EXPORT_INT(m, SNDCTL_SEQ_TESTMIDI);
1289 _EXPORT_INT(m, SNDCTL_SEQ_THRESHOLD);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001290#ifdef SNDCTL_SYNTH_CONTROL
Greg Ward1e0f57d2002-11-30 23:05:26 +00001291 _EXPORT_INT(m, SNDCTL_SYNTH_CONTROL);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001292#endif
1293#ifdef SNDCTL_SYNTH_ID
Greg Ward1e0f57d2002-11-30 23:05:26 +00001294 _EXPORT_INT(m, SNDCTL_SYNTH_ID);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001295#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001296 _EXPORT_INT(m, SNDCTL_SYNTH_INFO);
1297 _EXPORT_INT(m, SNDCTL_SYNTH_MEMAVL);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001298#ifdef SNDCTL_SYNTH_REMOVESAMPLE
Greg Ward1e0f57d2002-11-30 23:05:26 +00001299 _EXPORT_INT(m, SNDCTL_SYNTH_REMOVESAMPLE);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001300#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001301 _EXPORT_INT(m, SNDCTL_TMR_CONTINUE);
1302 _EXPORT_INT(m, SNDCTL_TMR_METRONOME);
1303 _EXPORT_INT(m, SNDCTL_TMR_SELECT);
1304 _EXPORT_INT(m, SNDCTL_TMR_SOURCE);
1305 _EXPORT_INT(m, SNDCTL_TMR_START);
1306 _EXPORT_INT(m, SNDCTL_TMR_STOP);
1307 _EXPORT_INT(m, SNDCTL_TMR_TEMPO);
1308 _EXPORT_INT(m, SNDCTL_TMR_TIMEBASE);
Martin v. Löwis1a214512008-06-11 05:26:20 +00001309 return m;
Greg Ward04613a92002-11-30 22:47:45 +00001310}