blob: 3d1f18083dc09212039cc81e26532f4dda0b820c [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;
Greg Ward04613a92002-11-30 22:47:45 +0000403 PyObject *rv;
Guido van Rossum0741f802003-06-02 14:15:34 +0000404
Charles-François Natalia5293082011-06-11 18:58:24 +0200405 if (!_is_fd_valid(self->fd))
406 return NULL;
407
Greg Ward04613a92002-11-30 22:47:45 +0000408 if (!PyArg_ParseTuple(args, "i:read", &size))
409 return NULL;
Victor Stinner4fa99cd2015-03-20 00:27:28 +0100410
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;
Greg Ward64927852003-05-23 01:50:37 +0000414
Victor Stinner4fa99cd2015-03-20 00:27:28 +0100415 count = _Py_read(self->fd, PyBytes_AS_STRING(rv), size);
416 if (count == -1) {
Greg Ward04613a92002-11-30 22:47:45 +0000417 Py_DECREF(rv);
418 return NULL;
419 }
Victor Stinner4fa99cd2015-03-20 00:27:28 +0100420
Greg Ward7b43c682002-12-30 02:29:28 +0000421 self->icount += count;
Gregory P. Smith0a608fd2008-09-06 21:34:51 +0000422 _PyBytes_Resize(&rv, count);
Greg Ward04613a92002-11-30 22:47:45 +0000423 return rv;
424}
425
426static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000427oss_write(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000428{
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +0200429 Py_buffer data;
430 int rv;
Greg Ward131bce02002-11-30 22:56:44 +0000431
Charles-François Natalia5293082011-06-11 18:58:24 +0200432 if (!_is_fd_valid(self->fd))
433 return NULL;
434
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +0200435 if (!PyArg_ParseTuple(args, "y*:write", &data)) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000436 return NULL;
Greg Ward131bce02002-11-30 22:56:44 +0000437 }
Greg Ward64927852003-05-23 01:50:37 +0000438
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +0200439 rv = _Py_write(self->fd, data.buf, data.len);
440 PyBuffer_Release(&data);
Victor Stinner4fa99cd2015-03-20 00:27:28 +0100441 if (rv == -1)
442 return NULL;
Greg Ward64927852003-05-23 01:50:37 +0000443
Victor Stinner4fa99cd2015-03-20 00:27:28 +0100444 self->ocount += rv;
Christian Heimes217cfd12007-12-02 14:31:20 +0000445 return PyLong_FromLong(rv);
Greg Ward131bce02002-11-30 22:56:44 +0000446}
447
448static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000449oss_writeall(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000450{
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +0200451 Py_buffer data;
452 const char *cp;
453 Py_ssize_t size;
454 int rv;
Greg Ward131bce02002-11-30 22:56:44 +0000455 fd_set write_set_fds;
456 int select_rv;
Guido van Rossum0741f802003-06-02 14:15:34 +0000457
Greg Ward131bce02002-11-30 22:56:44 +0000458 /* NB. writeall() is only useful in non-blocking mode: according to
459 Guenter Geiger <geiger@xdv.org> on the linux-audio-dev list
460 (http://eca.cx/lad/2002/11/0380.html), OSS guarantees that
461 write() in blocking mode consumes the whole buffer. In blocking
462 mode, the behaviour of write() and writeall() from Python is
463 indistinguishable. */
464
Charles-François Natalia5293082011-06-11 18:58:24 +0200465 if (!_is_fd_valid(self->fd))
466 return NULL;
467
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +0200468 if (!PyArg_ParseTuple(args, "y*:writeall", &data))
Greg Ward131bce02002-11-30 22:56:44 +0000469 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000470
Charles-François Nataliaa26b272011-08-28 17:51:43 +0200471 if (!_PyIsSelectable_fd(self->fd)) {
472 PyErr_SetString(PyExc_ValueError,
473 "file descriptor out of range for select");
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +0200474 PyBuffer_Release(&data);
Charles-François Nataliaa26b272011-08-28 17:51:43 +0200475 return NULL;
476 }
Greg Ward04613a92002-11-30 22:47:45 +0000477 /* use select to wait for audio device to be available */
478 FD_ZERO(&write_set_fds);
Greg Ward7b43c682002-12-30 02:29:28 +0000479 FD_SET(self->fd, &write_set_fds);
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +0200480 cp = (const char *)data.buf;
481 size = data.len;
Greg Ward04613a92002-11-30 22:47:45 +0000482
483 while (size > 0) {
Greg Ward64927852003-05-23 01:50:37 +0000484 Py_BEGIN_ALLOW_THREADS
Greg Ward7b43c682002-12-30 02:29:28 +0000485 select_rv = select(self->fd+1, NULL, &write_set_fds, NULL, NULL);
Greg Ward64927852003-05-23 01:50:37 +0000486 Py_END_ALLOW_THREADS
Victor Stinner4fa99cd2015-03-20 00:27:28 +0100487
488 assert(select_rv != 0); /* no timeout, can't expire */
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +0200489 if (select_rv == -1) {
490 PyBuffer_Release(&data);
Greg Ward97708bc2002-11-30 23:17:10 +0000491 return PyErr_SetFromErrno(PyExc_IOError);
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +0200492 }
Greg Ward131bce02002-11-30 22:56:44 +0000493
Victor Stinner716a74e2015-03-20 10:24:18 +0100494 rv = _Py_write(self->fd, cp, Py_MIN(size, INT_MAX));
Greg Ward131bce02002-11-30 22:56:44 +0000495 if (rv == -1) {
Victor Stinner4fa99cd2015-03-20 00:27:28 +0100496 /* buffer is full, try again */
497 if (errno == EAGAIN) {
498 PyErr_Clear();
Greg Ward131bce02002-11-30 22:56:44 +0000499 continue;
Victor Stinner4fa99cd2015-03-20 00:27:28 +0100500 }
501 /* it's a real error */
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +0200502 PyBuffer_Release(&data);
Victor Stinner4fa99cd2015-03-20 00:27:28 +0100503 return NULL;
Greg Ward131bce02002-11-30 22:56:44 +0000504 }
Victor Stinner4fa99cd2015-03-20 00:27:28 +0100505
506 /* wrote rv bytes */
507 self->ocount += rv;
508 size -= rv;
509 cp += rv;
Greg Ward04613a92002-11-30 22:47:45 +0000510 }
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +0200511 PyBuffer_Release(&data);
Greg Ward04613a92002-11-30 22:47:45 +0000512 Py_INCREF(Py_None);
513 return Py_None;
514}
515
516static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000517oss_close(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000518{
Greg Ward7b43c682002-12-30 02:29:28 +0000519 if (self->fd >= 0) {
Greg Wardb8043902003-05-26 22:47:30 +0000520 Py_BEGIN_ALLOW_THREADS
Greg Ward7b43c682002-12-30 02:29:28 +0000521 close(self->fd);
Greg Wardb8043902003-05-26 22:47:30 +0000522 Py_END_ALLOW_THREADS
Greg Ward7b43c682002-12-30 02:29:28 +0000523 self->fd = -1;
Greg Ward04613a92002-11-30 22:47:45 +0000524 }
525 Py_INCREF(Py_None);
526 return Py_None;
527}
528
529static PyObject *
Georg Brandl770a2be2010-10-24 20:47:32 +0000530oss_self(PyObject *self, PyObject *unused)
Georg Brandl1e908af2010-10-23 17:31:52 +0000531{
532 Py_INCREF(self);
533 return self;
534}
535
Victor Stinner63941882011-09-29 00:42:28 +0200536static PyObject *
Georg Brandl1e908af2010-10-23 17:31:52 +0000537oss_exit(PyObject *self, PyObject *unused)
538{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200539 _Py_IDENTIFIER(close);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200540
541 PyObject *ret = _PyObject_CallMethodId(self, &PyId_close, NULL);
Georg Brandl1e908af2010-10-23 17:31:52 +0000542 if (!ret)
543 return NULL;
544 Py_DECREF(ret);
545 Py_RETURN_NONE;
546}
547
548static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000549oss_fileno(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000550{
Charles-François Natalia5293082011-06-11 18:58:24 +0200551 if (!_is_fd_valid(self->fd))
552 return NULL;
553
Christian Heimes217cfd12007-12-02 14:31:20 +0000554 return PyLong_FromLong(self->fd);
Greg Ward04613a92002-11-30 22:47:45 +0000555}
556
Greg Ward131bce02002-11-30 22:56:44 +0000557
558/* Convenience methods: these generally wrap a couple of ioctls into one
559 common task. */
560
Greg Ward04613a92002-11-30 22:47:45 +0000561static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000562oss_setparameters(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000563{
Greg Wardd6769062003-05-29 21:53:06 +0000564 int wanted_fmt, wanted_channels, wanted_rate, strict=0;
565 int fmt, channels, rate;
Greg Ward04613a92002-11-30 22:47:45 +0000566
Charles-François Natalia5293082011-06-11 18:58:24 +0200567 if (!_is_fd_valid(self->fd))
568 return NULL;
569
Greg Wardd6769062003-05-29 21:53:06 +0000570 if (!PyArg_ParseTuple(args, "iii|i:setparameters",
571 &wanted_fmt, &wanted_channels, &wanted_rate,
572 &strict))
Greg Ward04613a92002-11-30 22:47:45 +0000573 return NULL;
Greg Wardd6769062003-05-29 21:53:06 +0000574
575 fmt = wanted_fmt;
576 if (ioctl(self->fd, SNDCTL_DSP_SETFMT, &fmt) == -1) {
577 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000578 }
Greg Wardd6769062003-05-29 21:53:06 +0000579 if (strict && fmt != wanted_fmt) {
580 return PyErr_Format
581 (OSSAudioError,
582 "unable to set requested format (wanted %d, got %d)",
583 wanted_fmt, fmt);
Greg Ward04613a92002-11-30 22:47:45 +0000584 }
585
Greg Wardd6769062003-05-29 21:53:06 +0000586 channels = wanted_channels;
587 if (ioctl(self->fd, SNDCTL_DSP_CHANNELS, &channels) == -1) {
588 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000589 }
Greg Wardd6769062003-05-29 21:53:06 +0000590 if (strict && channels != wanted_channels) {
591 return PyErr_Format
592 (OSSAudioError,
593 "unable to set requested channels (wanted %d, got %d)",
594 wanted_channels, channels);
Greg Ward04613a92002-11-30 22:47:45 +0000595 }
596
Greg Wardd6769062003-05-29 21:53:06 +0000597 rate = wanted_rate;
Greg Ward7b43c682002-12-30 02:29:28 +0000598 if (ioctl(self->fd, SNDCTL_DSP_SPEED, &rate) == -1) {
Greg Wardd6769062003-05-29 21:53:06 +0000599 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000600 }
Greg Wardd6769062003-05-29 21:53:06 +0000601 if (strict && rate != wanted_rate) {
602 return PyErr_Format
603 (OSSAudioError,
604 "unable to set requested rate (wanted %d, got %d)",
605 wanted_rate, rate);
606 }
Guido van Rossum0741f802003-06-02 14:15:34 +0000607
Greg Wardd6769062003-05-29 21:53:06 +0000608 /* Construct the return value: a (fmt, channels, rate) tuple that
609 tells what the audio hardware was actually set to. */
Serhiy Storchaka48d761e2013-12-17 15:11:24 +0200610 return Py_BuildValue("(iii)", fmt, channels, rate);
Greg Ward04613a92002-11-30 22:47:45 +0000611}
612
613static int
Greg Ward499b73e2002-12-31 03:04:52 +0000614_ssize(oss_audio_t *self, int *nchannels, int *ssize)
Greg Ward04613a92002-11-30 22:47:45 +0000615{
616 int fmt;
617
618 fmt = 0;
Guido van Rossum0741f802003-06-02 14:15:34 +0000619 if (ioctl(self->fd, SNDCTL_DSP_SETFMT, &fmt) < 0)
Greg Ward04613a92002-11-30 22:47:45 +0000620 return -errno;
621
622 switch (fmt) {
623 case AFMT_MU_LAW:
624 case AFMT_A_LAW:
625 case AFMT_U8:
626 case AFMT_S8:
Greg Ward38c92662003-05-29 21:55:41 +0000627 *ssize = 1; /* 8 bit formats: 1 byte */
Greg Ward04613a92002-11-30 22:47:45 +0000628 break;
629 case AFMT_S16_LE:
630 case AFMT_S16_BE:
631 case AFMT_U16_LE:
632 case AFMT_U16_BE:
Greg Ward84f8ecd2003-05-29 23:44:44 +0000633 *ssize = 2; /* 16 bit formats: 2 byte */
Greg Ward04613a92002-11-30 22:47:45 +0000634 break;
635 case AFMT_MPEG:
636 case AFMT_IMA_ADPCM:
637 default:
638 return -EOPNOTSUPP;
639 }
Greg Ward7b43c682002-12-30 02:29:28 +0000640 if (ioctl(self->fd, SNDCTL_DSP_CHANNELS, nchannels) < 0)
Greg Ward04613a92002-11-30 22:47:45 +0000641 return -errno;
642 return 0;
643}
644
645
Guido van Rossum0741f802003-06-02 14:15:34 +0000646/* bufsize returns the size of the hardware audio buffer in number
Greg Ward04613a92002-11-30 22:47:45 +0000647 of samples */
648static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000649oss_bufsize(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000650{
651 audio_buf_info ai;
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000652 int nchannels=0, ssize=0;
Greg Ward04613a92002-11-30 22:47:45 +0000653
Charles-François Natalia5293082011-06-11 18:58:24 +0200654 if (!_is_fd_valid(self->fd))
655 return NULL;
656
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000657 if (_ssize(self, &nchannels, &ssize) < 0 || !nchannels || !ssize) {
Greg Ward97708bc2002-11-30 23:17:10 +0000658 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000659 return NULL;
660 }
Greg Ward7b43c682002-12-30 02:29:28 +0000661 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000662 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000663 return NULL;
664 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000665 return PyLong_FromLong((ai.fragstotal * ai.fragsize) / (nchannels * ssize));
Greg Ward04613a92002-11-30 22:47:45 +0000666}
667
Guido van Rossum0741f802003-06-02 14:15:34 +0000668/* obufcount returns the number of samples that are available in the
Greg Ward04613a92002-11-30 22:47:45 +0000669 hardware for playing */
670static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000671oss_obufcount(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000672{
673 audio_buf_info ai;
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000674 int nchannels=0, ssize=0;
Greg Ward04613a92002-11-30 22:47:45 +0000675
Charles-François Natalia5293082011-06-11 18:58:24 +0200676 if (!_is_fd_valid(self->fd))
677 return NULL;
678
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000679 if (_ssize(self, &nchannels, &ssize) < 0 || !nchannels || !ssize) {
Greg Ward97708bc2002-11-30 23:17:10 +0000680 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000681 return NULL;
682 }
Greg Ward7b43c682002-12-30 02:29:28 +0000683 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000684 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000685 return NULL;
686 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000687 return PyLong_FromLong((ai.fragstotal * ai.fragsize - ai.bytes) /
Greg Ward04613a92002-11-30 22:47:45 +0000688 (ssize * nchannels));
689}
690
691/* obufcount returns the number of samples that can be played without
692 blocking */
693static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000694oss_obuffree(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000695{
696 audio_buf_info ai;
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000697 int nchannels=0, ssize=0;
Greg Ward04613a92002-11-30 22:47:45 +0000698
Charles-François Natalia5293082011-06-11 18:58:24 +0200699 if (!_is_fd_valid(self->fd))
700 return NULL;
701
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000702 if (_ssize(self, &nchannels, &ssize) < 0 || !nchannels || !ssize) {
Greg Ward97708bc2002-11-30 23:17:10 +0000703 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000704 return NULL;
705 }
Greg Ward7b43c682002-12-30 02:29:28 +0000706 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000707 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000708 return NULL;
709 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000710 return PyLong_FromLong(ai.bytes / (ssize * nchannels));
Greg Ward04613a92002-11-30 22:47:45 +0000711}
712
Greg Ward04613a92002-11-30 22:47:45 +0000713static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000714oss_getptr(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000715{
716 count_info info;
717 int req;
718
Charles-François Natalia5293082011-06-11 18:58:24 +0200719 if (!_is_fd_valid(self->fd))
720 return NULL;
721
Greg Ward7b43c682002-12-30 02:29:28 +0000722 if (self->mode == O_RDONLY)
Greg Wardad4d9b92002-12-30 03:02:22 +0000723 req = SNDCTL_DSP_GETIPTR;
Greg Ward04613a92002-11-30 22:47:45 +0000724 else
Greg Wardad4d9b92002-12-30 03:02:22 +0000725 req = SNDCTL_DSP_GETOPTR;
Greg Ward7b43c682002-12-30 02:29:28 +0000726 if (ioctl(self->fd, req, &info) == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000727 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000728 return NULL;
729 }
730 return Py_BuildValue("iii", info.bytes, info.blocks, info.ptr);
731}
732
Greg Wardda1cacb2002-12-31 03:02:23 +0000733
734/* ----------------------------------------------------------------------
735 * Methods of mixer objects (OSSMixerType)
736 */
737
Greg Ward3d9994d2002-12-11 15:12:01 +0000738static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000739oss_mixer_close(oss_mixer_t *self, PyObject *unused)
Greg Ward3d9994d2002-12-11 15:12:01 +0000740{
Greg Ward7b43c682002-12-30 02:29:28 +0000741 if (self->fd >= 0) {
742 close(self->fd);
743 self->fd = -1;
Greg Ward3d9994d2002-12-11 15:12:01 +0000744 }
745 Py_INCREF(Py_None);
746 return Py_None;
747}
748
749static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000750oss_mixer_fileno(oss_mixer_t *self, PyObject *unused)
Greg Ward3d9994d2002-12-11 15:12:01 +0000751{
Charles-François Natalia5293082011-06-11 18:58:24 +0200752 if (!_is_fd_valid(self->fd))
753 return NULL;
754
Christian Heimes217cfd12007-12-02 14:31:20 +0000755 return PyLong_FromLong(self->fd);
Greg Ward3d9994d2002-12-11 15:12:01 +0000756}
757
758/* Simple mixer interface methods */
759
760static PyObject *
Greg Ward2d6f9a92002-12-31 02:54:43 +0000761oss_mixer_controls(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000762{
Charles-François Natalia5293082011-06-11 18:58:24 +0200763 if (!_is_fd_valid(self->fd))
764 return NULL;
765
Greg Ward2d6f9a92002-12-31 02:54:43 +0000766 return _do_ioctl_1_internal(self->fd, args, "controls",
Greg Ward3d9994d2002-12-11 15:12:01 +0000767 SOUND_MIXER_READ_DEVMASK);
768}
769
770static PyObject *
Greg Ward2d6f9a92002-12-31 02:54:43 +0000771oss_mixer_stereocontrols(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000772{
Charles-François Natalia5293082011-06-11 18:58:24 +0200773 if (!_is_fd_valid(self->fd))
774 return NULL;
775
Greg Ward2d6f9a92002-12-31 02:54:43 +0000776 return _do_ioctl_1_internal(self->fd, args, "stereocontrols",
Greg Ward3d9994d2002-12-11 15:12:01 +0000777 SOUND_MIXER_READ_STEREODEVS);
778}
779
780static PyObject *
Greg Ward2d6f9a92002-12-31 02:54:43 +0000781oss_mixer_reccontrols(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000782{
Charles-François Natalia5293082011-06-11 18:58:24 +0200783 if (!_is_fd_valid(self->fd))
784 return NULL;
785
Greg Ward2d6f9a92002-12-31 02:54:43 +0000786 return _do_ioctl_1_internal(self->fd, args, "reccontrols",
Greg Wardb69bb3d2002-12-12 17:35:45 +0000787 SOUND_MIXER_READ_RECMASK);
Greg Ward3d9994d2002-12-11 15:12:01 +0000788}
789
790static PyObject *
Greg Warde7037662002-12-30 03:01:48 +0000791oss_mixer_get(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000792{
793 int channel, volume;
Guido van Rossum0741f802003-06-02 14:15:34 +0000794
Charles-François Natalia5293082011-06-11 18:58:24 +0200795 if (!_is_fd_valid(self->fd))
796 return NULL;
797
Greg Ward3d9994d2002-12-11 15:12:01 +0000798 /* Can't use _do_ioctl_1 because of encoded arg thingy. */
Greg Warde7037662002-12-30 03:01:48 +0000799 if (!PyArg_ParseTuple(args, "i:get", &channel))
Greg Wardad4d9b92002-12-30 03:02:22 +0000800 return NULL;
Guido van Rossum0741f802003-06-02 14:15:34 +0000801
Greg Ward3d9994d2002-12-11 15:12:01 +0000802 if (channel < 0 || channel > SOUND_MIXER_NRDEVICES) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000803 PyErr_SetString(OSSAudioError, "Invalid mixer channel specified.");
804 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000805 }
Guido van Rossum0741f802003-06-02 14:15:34 +0000806
Greg Warde7037662002-12-30 03:01:48 +0000807 if (ioctl(self->fd, MIXER_READ(channel), &volume) == -1)
Greg Wardad4d9b92002-12-30 03:02:22 +0000808 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum0741f802003-06-02 14:15:34 +0000809
Greg Warde7037662002-12-30 03:01:48 +0000810 return Py_BuildValue("(ii)", volume & 0xff, (volume & 0xff00) >> 8);
Greg Ward3d9994d2002-12-11 15:12:01 +0000811}
812
813static PyObject *
Greg Warde7037662002-12-30 03:01:48 +0000814oss_mixer_set(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000815{
816 int channel, volume, leftVol, rightVol;
Guido van Rossum0741f802003-06-02 14:15:34 +0000817
Charles-François Natalia5293082011-06-11 18:58:24 +0200818 if (!_is_fd_valid(self->fd))
819 return NULL;
820
Greg Ward3d9994d2002-12-11 15:12:01 +0000821 /* Can't use _do_ioctl_1 because of encoded arg thingy. */
Greg Warde7037662002-12-30 03:01:48 +0000822 if (!PyArg_ParseTuple(args, "i(ii):set", &channel, &leftVol, &rightVol))
Greg Wardad4d9b92002-12-30 03:02:22 +0000823 return NULL;
Guido van Rossum0741f802003-06-02 14:15:34 +0000824
Greg Ward3d9994d2002-12-11 15:12:01 +0000825 if (channel < 0 || channel > SOUND_MIXER_NRDEVICES) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000826 PyErr_SetString(OSSAudioError, "Invalid mixer channel specified.");
827 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000828 }
Guido van Rossum0741f802003-06-02 14:15:34 +0000829
Greg Ward3d9994d2002-12-11 15:12:01 +0000830 if (leftVol < 0 || rightVol < 0 || leftVol > 100 || rightVol > 100) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000831 PyErr_SetString(OSSAudioError, "Volumes must be between 0 and 100.");
832 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000833 }
834
835 volume = (rightVol << 8) | leftVol;
Guido van Rossum0741f802003-06-02 14:15:34 +0000836
Greg Warde7037662002-12-30 03:01:48 +0000837 if (ioctl(self->fd, MIXER_WRITE(channel), &volume) == -1)
Greg Wardad4d9b92002-12-30 03:02:22 +0000838 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum0741f802003-06-02 14:15:34 +0000839
Greg Warde7037662002-12-30 03:01:48 +0000840 return Py_BuildValue("(ii)", volume & 0xff, (volume & 0xff00) >> 8);
Greg Ward3d9994d2002-12-11 15:12:01 +0000841}
842
843static PyObject *
Greg Wardf05aa102002-12-30 23:19:32 +0000844oss_mixer_get_recsrc(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000845{
Charles-François Natalia5293082011-06-11 18:58:24 +0200846 if (!_is_fd_valid(self->fd))
847 return NULL;
848
Greg Wardf05aa102002-12-30 23:19:32 +0000849 return _do_ioctl_1_internal(self->fd, args, "get_recsrc",
Greg Ward3d9994d2002-12-11 15:12:01 +0000850 SOUND_MIXER_READ_RECSRC);
851}
852
853static PyObject *
Greg Wardf05aa102002-12-30 23:19:32 +0000854oss_mixer_set_recsrc(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000855{
Charles-François Natalia5293082011-06-11 18:58:24 +0200856 if (!_is_fd_valid(self->fd))
857 return NULL;
858
Greg Wardf05aa102002-12-30 23:19:32 +0000859 return _do_ioctl_1(self->fd, args, "set_recsrc",
Greg Ward3d9994d2002-12-11 15:12:01 +0000860 SOUND_MIXER_WRITE_RECSRC);
861}
862
863
Greg Wardda1cacb2002-12-31 03:02:23 +0000864/* ----------------------------------------------------------------------
865 * Method tables and other bureaucracy
866 */
867
Greg Ward8c6b6a92002-12-11 14:43:13 +0000868static PyMethodDef oss_methods[] = {
Greg Ward131bce02002-11-30 22:56:44 +0000869 /* Regular file methods */
Greg Wardad4d9b92002-12-30 03:02:22 +0000870 { "read", (PyCFunction)oss_read, METH_VARARGS },
871 { "write", (PyCFunction)oss_write, METH_VARARGS },
872 { "writeall", (PyCFunction)oss_writeall, METH_VARARGS },
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000873 { "close", (PyCFunction)oss_close, METH_NOARGS },
874 { "fileno", (PyCFunction)oss_fileno, METH_NOARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000875
876 /* Simple ioctl wrappers */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000877 { "nonblock", (PyCFunction)oss_nonblock, METH_NOARGS },
Greg Wardad4d9b92002-12-30 03:02:22 +0000878 { "setfmt", (PyCFunction)oss_setfmt, METH_VARARGS },
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000879 { "getfmts", (PyCFunction)oss_getfmts, METH_NOARGS },
Greg Ward8c6b6a92002-12-11 14:43:13 +0000880 { "channels", (PyCFunction)oss_channels, METH_VARARGS },
881 { "speed", (PyCFunction)oss_speed, METH_VARARGS },
Greg Wardad4d9b92002-12-30 03:02:22 +0000882 { "sync", (PyCFunction)oss_sync, METH_VARARGS },
883 { "reset", (PyCFunction)oss_reset, METH_VARARGS },
884 { "post", (PyCFunction)oss_post, METH_VARARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000885
886 /* Convenience methods -- wrap a couple of ioctls together */
Greg Wardad4d9b92002-12-30 03:02:22 +0000887 { "setparameters", (PyCFunction)oss_setparameters, METH_VARARGS },
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000888 { "bufsize", (PyCFunction)oss_bufsize, METH_NOARGS },
889 { "obufcount", (PyCFunction)oss_obufcount, METH_NOARGS },
890 { "obuffree", (PyCFunction)oss_obuffree, METH_NOARGS },
891 { "getptr", (PyCFunction)oss_getptr, METH_NOARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000892
893 /* Aliases for backwards compatibility */
Greg Wardad4d9b92002-12-30 03:02:22 +0000894 { "flush", (PyCFunction)oss_sync, METH_VARARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000895
Serhiy Storchaka14867992014-09-10 23:43:41 +0300896 /* Support for the context management protocol */
Georg Brandl770a2be2010-10-24 20:47:32 +0000897 { "__enter__", oss_self, METH_NOARGS },
898 { "__exit__", oss_exit, METH_VARARGS },
Georg Brandl1e908af2010-10-23 17:31:52 +0000899
Greg Wardad4d9b92002-12-30 03:02:22 +0000900 { NULL, NULL} /* sentinel */
Greg Ward04613a92002-11-30 22:47:45 +0000901};
902
Greg Ward3d9994d2002-12-11 15:12:01 +0000903static PyMethodDef oss_mixer_methods[] = {
904 /* Regular file method - OSS mixers are ioctl-only interface */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000905 { "close", (PyCFunction)oss_mixer_close, METH_NOARGS },
906 { "fileno", (PyCFunction)oss_mixer_fileno, METH_NOARGS },
Greg Ward3d9994d2002-12-11 15:12:01 +0000907
Serhiy Storchaka14867992014-09-10 23:43:41 +0300908 /* Support for the context management protocol */
Georg Brandl770a2be2010-10-24 20:47:32 +0000909 { "__enter__", oss_self, METH_NOARGS },
910 { "__exit__", oss_exit, METH_VARARGS },
Georg Brandl1e908af2010-10-23 17:31:52 +0000911
Greg Ward3d9994d2002-12-11 15:12:01 +0000912 /* Simple ioctl wrappers */
Guido van Rossum0741f802003-06-02 14:15:34 +0000913 { "controls", (PyCFunction)oss_mixer_controls, METH_VARARGS },
Greg Ward2d6f9a92002-12-31 02:54:43 +0000914 { "stereocontrols", (PyCFunction)oss_mixer_stereocontrols, METH_VARARGS},
Guido van Rossum0741f802003-06-02 14:15:34 +0000915 { "reccontrols", (PyCFunction)oss_mixer_reccontrols, METH_VARARGS},
Greg Wardad4d9b92002-12-30 03:02:22 +0000916 { "get", (PyCFunction)oss_mixer_get, METH_VARARGS },
917 { "set", (PyCFunction)oss_mixer_set, METH_VARARGS },
Greg Wardf05aa102002-12-30 23:19:32 +0000918 { "get_recsrc", (PyCFunction)oss_mixer_get_recsrc, METH_VARARGS },
919 { "set_recsrc", (PyCFunction)oss_mixer_set_recsrc, METH_VARARGS },
Guido van Rossum0741f802003-06-02 14:15:34 +0000920
Greg Wardad4d9b92002-12-30 03:02:22 +0000921 { NULL, NULL}
Greg Ward3d9994d2002-12-11 15:12:01 +0000922};
923
Greg Ward04613a92002-11-30 22:47:45 +0000924static PyObject *
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000925oss_getattro(oss_audio_t *self, PyObject *nameobj)
Greg Ward04613a92002-11-30 22:47:45 +0000926{
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000927 char *name = "";
Greg Ward50682d02005-03-07 01:41:11 +0000928 PyObject * rval = NULL;
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000929
930 if (PyUnicode_Check(nameobj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 name = _PyUnicode_AsString(nameobj);
932
Greg Ward50682d02005-03-07 01:41:11 +0000933 if (strcmp(name, "closed") == 0) {
934 rval = (self->fd == -1) ? Py_True : Py_False;
935 Py_INCREF(rval);
936 }
937 else if (strcmp(name, "name") == 0) {
Neal Norwitz8abd2e62007-08-26 04:57:08 +0000938 rval = PyUnicode_FromString(self->devicename);
Greg Ward50682d02005-03-07 01:41:11 +0000939 }
940 else if (strcmp(name, "mode") == 0) {
941 /* No need for a "default" in this switch: from newossobject(),
942 self->mode can only be one of these three values. */
943 switch(self->mode) {
944 case O_RDONLY:
Neal Norwitz8abd2e62007-08-26 04:57:08 +0000945 rval = PyUnicode_FromString("r");
Greg Ward50682d02005-03-07 01:41:11 +0000946 break;
947 case O_RDWR:
Neal Norwitz8abd2e62007-08-26 04:57:08 +0000948 rval = PyUnicode_FromString("rw");
Greg Ward50682d02005-03-07 01:41:11 +0000949 break;
950 case O_WRONLY:
Neal Norwitz8abd2e62007-08-26 04:57:08 +0000951 rval = PyUnicode_FromString("w");
Greg Ward50682d02005-03-07 01:41:11 +0000952 break;
953 }
954 }
955 else {
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000956 rval = PyObject_GenericGetAttr((PyObject *)self, nameobj);
Greg Ward50682d02005-03-07 01:41:11 +0000957 }
958 return rval;
Greg Ward04613a92002-11-30 22:47:45 +0000959}
960
Greg Ward499b73e2002-12-31 03:04:52 +0000961static PyTypeObject OSSAudioType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000962 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Greg Ward9a568eb2002-11-30 23:20:09 +0000963 "ossaudiodev.oss_audio_device", /*tp_name*/
Greg Ward499b73e2002-12-31 03:04:52 +0000964 sizeof(oss_audio_t), /*tp_size*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000965 0, /*tp_itemsize*/
Greg Ward04613a92002-11-30 22:47:45 +0000966 /* methods */
Greg Wardad4d9b92002-12-30 03:02:22 +0000967 (destructor)oss_dealloc, /*tp_dealloc*/
968 0, /*tp_print*/
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000969 0, /*tp_getattr*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000970 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000971 0, /*tp_reserved*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000972 0, /*tp_repr*/
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000973 0, /*tp_as_number*/
974 0, /*tp_as_sequence*/
975 0, /*tp_as_mapping*/
976 0, /*tp_hash*/
977 0, /*tp_call*/
978 0, /*tp_str*/
979 (getattrofunc)oss_getattro, /*tp_getattro*/
980 0, /*tp_setattro*/
981 0, /*tp_as_buffer*/
982 Py_TPFLAGS_DEFAULT, /*tp_flags*/
983 0, /*tp_doc*/
984 0, /*tp_traverse*/
985 0, /*tp_clear*/
986 0, /*tp_richcompare*/
987 0, /*tp_weaklistoffset*/
988 0, /*tp_iter*/
989 0, /*tp_iternext*/
990 oss_methods, /*tp_methods*/
Greg Ward04613a92002-11-30 22:47:45 +0000991};
992
Greg Ward3d9994d2002-12-11 15:12:01 +0000993static PyTypeObject OSSMixerType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000994 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Greg Ward3d9994d2002-12-11 15:12:01 +0000995 "ossaudiodev.oss_mixer_device", /*tp_name*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000996 sizeof(oss_mixer_t), /*tp_size*/
997 0, /*tp_itemsize*/
Greg Ward3d9994d2002-12-11 15:12:01 +0000998 /* methods */
999 (destructor)oss_mixer_dealloc, /*tp_dealloc*/
Greg Wardad4d9b92002-12-30 03:02:22 +00001000 0, /*tp_print*/
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +00001001 0, /*tp_getattr*/
Greg Wardad4d9b92002-12-30 03:02:22 +00001002 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001003 0, /*tp_reserved*/
Greg Wardad4d9b92002-12-30 03:02:22 +00001004 0, /*tp_repr*/
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +00001005 0, /*tp_as_number*/
1006 0, /*tp_as_sequence*/
1007 0, /*tp_as_mapping*/
1008 0, /*tp_hash*/
1009 0, /*tp_call*/
1010 0, /*tp_str*/
1011 0, /*tp_getattro*/
1012 0, /*tp_setattro*/
1013 0, /*tp_as_buffer*/
1014 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1015 0, /*tp_doc*/
1016 0, /*tp_traverse*/
1017 0, /*tp_clear*/
1018 0, /*tp_richcompare*/
1019 0, /*tp_weaklistoffset*/
1020 0, /*tp_iter*/
1021 0, /*tp_iternext*/
1022 oss_mixer_methods, /*tp_methods*/
Greg Ward3d9994d2002-12-11 15:12:01 +00001023};
1024
1025
Greg Ward04613a92002-11-30 22:47:45 +00001026static PyObject *
Greg Ward9a568eb2002-11-30 23:20:09 +00001027ossopen(PyObject *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +00001028{
Greg Ward8c6b6a92002-12-11 14:43:13 +00001029 return (PyObject *)newossobject(args);
Greg Ward04613a92002-11-30 22:47:45 +00001030}
1031
Greg Ward3d9994d2002-12-11 15:12:01 +00001032static PyObject *
1033ossopenmixer(PyObject *self, PyObject *args)
1034{
1035 return (PyObject *)newossmixerobject(args);
1036}
1037
Greg Ward9a568eb2002-11-30 23:20:09 +00001038static PyMethodDef ossaudiodev_methods[] = {
1039 { "open", ossopen, METH_VARARGS },
Greg Ward3d9994d2002-12-11 15:12:01 +00001040 { "openmixer", ossopenmixer, METH_VARARGS },
Greg Ward04613a92002-11-30 22:47:45 +00001041 { 0, 0 },
1042};
1043
Greg Ward1e0f57d2002-11-30 23:05:26 +00001044
1045#define _EXPORT_INT(mod, name) \
Martin v. Löwis1a214512008-06-11 05:26:20 +00001046 if (PyModule_AddIntConstant(mod, #name, (long) (name)) == -1) return NULL;
Greg Ward1e0f57d2002-11-30 23:05:26 +00001047
Greg Ward744f0fd2002-12-31 03:23:59 +00001048
1049static char *control_labels[] = SOUND_DEVICE_LABELS;
1050static char *control_names[] = SOUND_DEVICE_NAMES;
1051
1052
1053static int
1054build_namelists (PyObject *module)
1055{
1056 PyObject *labels;
1057 PyObject *names;
1058 PyObject *s;
1059 int num_controls;
1060 int i;
1061
Victor Stinner63941882011-09-29 00:42:28 +02001062 num_controls = Py_ARRAY_LENGTH(control_labels);
1063 assert(num_controls == Py_ARRAY_LENGTH(control_names));
Greg Ward744f0fd2002-12-31 03:23:59 +00001064
1065 labels = PyList_New(num_controls);
1066 names = PyList_New(num_controls);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001067 if (labels == NULL || names == NULL)
1068 goto error2;
Greg Ward744f0fd2002-12-31 03:23:59 +00001069 for (i = 0; i < num_controls; i++) {
Neal Norwitz8abd2e62007-08-26 04:57:08 +00001070 s = PyUnicode_FromString(control_labels[i]);
Greg Ward744f0fd2002-12-31 03:23:59 +00001071 if (s == NULL)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001072 goto error2;
Greg Ward744f0fd2002-12-31 03:23:59 +00001073 PyList_SET_ITEM(labels, i, s);
Guido van Rossum0741f802003-06-02 14:15:34 +00001074
Neal Norwitz8abd2e62007-08-26 04:57:08 +00001075 s = PyUnicode_FromString(control_names[i]);
Greg Ward744f0fd2002-12-31 03:23:59 +00001076 if (s == NULL)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001077 goto error2;
Greg Ward744f0fd2002-12-31 03:23:59 +00001078 PyList_SET_ITEM(names, i, s);
1079 }
1080
1081 if (PyModule_AddObject(module, "control_labels", labels) == -1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001082 goto error2;
Greg Ward744f0fd2002-12-31 03:23:59 +00001083 if (PyModule_AddObject(module, "control_names", names) == -1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001084 goto error1;
Greg Ward744f0fd2002-12-31 03:23:59 +00001085
1086 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001087
1088error2:
1089 Py_XDECREF(labels);
1090error1:
1091 Py_XDECREF(names);
1092 return -1;
Guido van Rossum0741f802003-06-02 14:15:34 +00001093}
Greg Ward744f0fd2002-12-31 03:23:59 +00001094
1095
Martin v. Löwis1a214512008-06-11 05:26:20 +00001096static struct PyModuleDef ossaudiodevmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 PyModuleDef_HEAD_INIT,
1098 "ossaudiodev",
1099 NULL,
1100 -1,
1101 ossaudiodev_methods,
1102 NULL,
1103 NULL,
1104 NULL,
1105 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001106};
1107
Antoine Pitrou39b35432010-03-23 00:25:54 +00001108PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001109PyInit_ossaudiodev(void)
Greg Ward04613a92002-11-30 22:47:45 +00001110{
1111 PyObject *m;
Guido van Rossum0741f802003-06-02 14:15:34 +00001112
Antoine Pitrou39b35432010-03-23 00:25:54 +00001113 if (PyType_Ready(&OSSAudioType) < 0)
1114 return NULL;
1115
1116 if (PyType_Ready(&OSSMixerType) < 0)
1117 return NULL;
1118
Martin v. Löwis1a214512008-06-11 05:26:20 +00001119 m = PyModule_Create(&ossaudiodevmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001120 if (m == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +00001122
Guido van Rossum0741f802003-06-02 14:15:34 +00001123 OSSAudioError = PyErr_NewException("ossaudiodev.OSSAudioError",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 NULL, NULL);
Greg Wardd6769062003-05-29 21:53:06 +00001125 if (OSSAudioError) {
Guido van Rossum0741f802003-06-02 14:15:34 +00001126 /* Each call to PyModule_AddObject decrefs it; compensate: */
1127 Py_INCREF(OSSAudioError);
1128 Py_INCREF(OSSAudioError);
Greg Wardad4d9b92002-12-30 03:02:22 +00001129 PyModule_AddObject(m, "error", OSSAudioError);
Greg Wardd6769062003-05-29 21:53:06 +00001130 PyModule_AddObject(m, "OSSAudioError", OSSAudioError);
1131 }
Greg Ward04613a92002-11-30 22:47:45 +00001132
Greg Ward744f0fd2002-12-31 03:23:59 +00001133 /* Build 'control_labels' and 'control_names' lists and add them
1134 to the module. */
1135 if (build_namelists(m) == -1) /* XXX what to do here? */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001136 return NULL;
Greg Ward744f0fd2002-12-31 03:23:59 +00001137
Greg Ward1e0f57d2002-11-30 23:05:26 +00001138 /* Expose the audio format numbers -- essential! */
1139 _EXPORT_INT(m, AFMT_QUERY);
1140 _EXPORT_INT(m, AFMT_MU_LAW);
1141 _EXPORT_INT(m, AFMT_A_LAW);
1142 _EXPORT_INT(m, AFMT_IMA_ADPCM);
1143 _EXPORT_INT(m, AFMT_U8);
1144 _EXPORT_INT(m, AFMT_S16_LE);
1145 _EXPORT_INT(m, AFMT_S16_BE);
1146 _EXPORT_INT(m, AFMT_S8);
1147 _EXPORT_INT(m, AFMT_U16_LE);
1148 _EXPORT_INT(m, AFMT_U16_BE);
1149 _EXPORT_INT(m, AFMT_MPEG);
Andrew M. Kuchlingfe62bc92003-02-13 13:27:07 +00001150#ifdef AFMT_AC3
Greg Ward1e0f57d2002-11-30 23:05:26 +00001151 _EXPORT_INT(m, AFMT_AC3);
Andrew M. Kuchlingfe62bc92003-02-13 13:27:07 +00001152#endif
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001153#ifdef AFMT_S16_NE
Greg Ward1e0f57d2002-11-30 23:05:26 +00001154 _EXPORT_INT(m, AFMT_S16_NE);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001155#endif
Greg Ward0f260542005-03-28 02:40:46 +00001156#ifdef AFMT_U16_NE
1157 _EXPORT_INT(m, AFMT_U16_NE);
1158#endif
1159#ifdef AFMT_S32_LE
1160 _EXPORT_INT(m, AFMT_S32_LE);
1161#endif
1162#ifdef AFMT_S32_BE
1163 _EXPORT_INT(m, AFMT_S32_BE);
1164#endif
1165#ifdef AFMT_MPEG
1166 _EXPORT_INT(m, AFMT_MPEG);
1167#endif
Guido van Rossum0741f802003-06-02 14:15:34 +00001168
Greg Ward37897c22002-12-30 02:43:36 +00001169 /* Expose the sound mixer device numbers. */
Greg Ward7b43c682002-12-30 02:29:28 +00001170 _EXPORT_INT(m, SOUND_MIXER_NRDEVICES);
Greg Ward3d9994d2002-12-11 15:12:01 +00001171 _EXPORT_INT(m, SOUND_MIXER_VOLUME);
1172 _EXPORT_INT(m, SOUND_MIXER_BASS);
1173 _EXPORT_INT(m, SOUND_MIXER_TREBLE);
1174 _EXPORT_INT(m, SOUND_MIXER_SYNTH);
1175 _EXPORT_INT(m, SOUND_MIXER_PCM);
1176 _EXPORT_INT(m, SOUND_MIXER_SPEAKER);
1177 _EXPORT_INT(m, SOUND_MIXER_LINE);
1178 _EXPORT_INT(m, SOUND_MIXER_MIC);
1179 _EXPORT_INT(m, SOUND_MIXER_CD);
1180 _EXPORT_INT(m, SOUND_MIXER_IMIX);
1181 _EXPORT_INT(m, SOUND_MIXER_ALTPCM);
1182 _EXPORT_INT(m, SOUND_MIXER_RECLEV);
1183 _EXPORT_INT(m, SOUND_MIXER_IGAIN);
1184 _EXPORT_INT(m, SOUND_MIXER_OGAIN);
1185 _EXPORT_INT(m, SOUND_MIXER_LINE1);
1186 _EXPORT_INT(m, SOUND_MIXER_LINE2);
1187 _EXPORT_INT(m, SOUND_MIXER_LINE3);
Greg Wardfd0283e2004-05-11 01:34:55 +00001188#ifdef SOUND_MIXER_DIGITAL1
Greg Ward3d9994d2002-12-11 15:12:01 +00001189 _EXPORT_INT(m, SOUND_MIXER_DIGITAL1);
Greg Wardfd0283e2004-05-11 01:34:55 +00001190#endif
1191#ifdef SOUND_MIXER_DIGITAL2
Greg Ward3d9994d2002-12-11 15:12:01 +00001192 _EXPORT_INT(m, SOUND_MIXER_DIGITAL2);
Greg Wardfd0283e2004-05-11 01:34:55 +00001193#endif
1194#ifdef SOUND_MIXER_DIGITAL3
Greg Ward3d9994d2002-12-11 15:12:01 +00001195 _EXPORT_INT(m, SOUND_MIXER_DIGITAL3);
Greg Wardfd0283e2004-05-11 01:34:55 +00001196#endif
1197#ifdef SOUND_MIXER_PHONEIN
Greg Ward3d9994d2002-12-11 15:12:01 +00001198 _EXPORT_INT(m, SOUND_MIXER_PHONEIN);
Greg Wardfd0283e2004-05-11 01:34:55 +00001199#endif
1200#ifdef SOUND_MIXER_PHONEOUT
Greg Ward3d9994d2002-12-11 15:12:01 +00001201 _EXPORT_INT(m, SOUND_MIXER_PHONEOUT);
Greg Wardfd0283e2004-05-11 01:34:55 +00001202#endif
1203#ifdef SOUND_MIXER_VIDEO
Greg Ward3d9994d2002-12-11 15:12:01 +00001204 _EXPORT_INT(m, SOUND_MIXER_VIDEO);
Greg Wardfd0283e2004-05-11 01:34:55 +00001205#endif
1206#ifdef SOUND_MIXER_RADIO
Greg Ward3d9994d2002-12-11 15:12:01 +00001207 _EXPORT_INT(m, SOUND_MIXER_RADIO);
Greg Wardfd0283e2004-05-11 01:34:55 +00001208#endif
1209#ifdef SOUND_MIXER_MONITOR
Greg Ward3d9994d2002-12-11 15:12:01 +00001210 _EXPORT_INT(m, SOUND_MIXER_MONITOR);
Greg Wardfd0283e2004-05-11 01:34:55 +00001211#endif
Greg Ward04613a92002-11-30 22:47:45 +00001212
Greg Ward1e0f57d2002-11-30 23:05:26 +00001213 /* Expose all the ioctl numbers for masochists who like to do this
1214 stuff directly. */
1215 _EXPORT_INT(m, SNDCTL_COPR_HALT);
1216 _EXPORT_INT(m, SNDCTL_COPR_LOAD);
1217 _EXPORT_INT(m, SNDCTL_COPR_RCODE);
1218 _EXPORT_INT(m, SNDCTL_COPR_RCVMSG);
1219 _EXPORT_INT(m, SNDCTL_COPR_RDATA);
1220 _EXPORT_INT(m, SNDCTL_COPR_RESET);
1221 _EXPORT_INT(m, SNDCTL_COPR_RUN);
1222 _EXPORT_INT(m, SNDCTL_COPR_SENDMSG);
1223 _EXPORT_INT(m, SNDCTL_COPR_WCODE);
1224 _EXPORT_INT(m, SNDCTL_COPR_WDATA);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001225#ifdef SNDCTL_DSP_BIND_CHANNEL
Greg Ward1e0f57d2002-11-30 23:05:26 +00001226 _EXPORT_INT(m, SNDCTL_DSP_BIND_CHANNEL);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001227#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001228 _EXPORT_INT(m, SNDCTL_DSP_CHANNELS);
1229 _EXPORT_INT(m, SNDCTL_DSP_GETBLKSIZE);
1230 _EXPORT_INT(m, SNDCTL_DSP_GETCAPS);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001231#ifdef SNDCTL_DSP_GETCHANNELMASK
Greg Ward1e0f57d2002-11-30 23:05:26 +00001232 _EXPORT_INT(m, SNDCTL_DSP_GETCHANNELMASK);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001233#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001234 _EXPORT_INT(m, SNDCTL_DSP_GETFMTS);
1235 _EXPORT_INT(m, SNDCTL_DSP_GETIPTR);
1236 _EXPORT_INT(m, SNDCTL_DSP_GETISPACE);
Greg Wardfd0283e2004-05-11 01:34:55 +00001237#ifdef SNDCTL_DSP_GETODELAY
Greg Ward1e0f57d2002-11-30 23:05:26 +00001238 _EXPORT_INT(m, SNDCTL_DSP_GETODELAY);
Greg Wardfd0283e2004-05-11 01:34:55 +00001239#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001240 _EXPORT_INT(m, SNDCTL_DSP_GETOPTR);
1241 _EXPORT_INT(m, SNDCTL_DSP_GETOSPACE);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001242#ifdef SNDCTL_DSP_GETSPDIF
Greg Ward1e0f57d2002-11-30 23:05:26 +00001243 _EXPORT_INT(m, SNDCTL_DSP_GETSPDIF);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001244#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001245 _EXPORT_INT(m, SNDCTL_DSP_GETTRIGGER);
1246 _EXPORT_INT(m, SNDCTL_DSP_MAPINBUF);
1247 _EXPORT_INT(m, SNDCTL_DSP_MAPOUTBUF);
1248 _EXPORT_INT(m, SNDCTL_DSP_NONBLOCK);
1249 _EXPORT_INT(m, SNDCTL_DSP_POST);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001250#ifdef SNDCTL_DSP_PROFILE
Greg Ward1e0f57d2002-11-30 23:05:26 +00001251 _EXPORT_INT(m, SNDCTL_DSP_PROFILE);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001252#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001253 _EXPORT_INT(m, SNDCTL_DSP_RESET);
1254 _EXPORT_INT(m, SNDCTL_DSP_SAMPLESIZE);
1255 _EXPORT_INT(m, SNDCTL_DSP_SETDUPLEX);
1256 _EXPORT_INT(m, SNDCTL_DSP_SETFMT);
1257 _EXPORT_INT(m, SNDCTL_DSP_SETFRAGMENT);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001258#ifdef SNDCTL_DSP_SETSPDIF
Greg Ward1e0f57d2002-11-30 23:05:26 +00001259 _EXPORT_INT(m, SNDCTL_DSP_SETSPDIF);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001260#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001261 _EXPORT_INT(m, SNDCTL_DSP_SETSYNCRO);
1262 _EXPORT_INT(m, SNDCTL_DSP_SETTRIGGER);
1263 _EXPORT_INT(m, SNDCTL_DSP_SPEED);
1264 _EXPORT_INT(m, SNDCTL_DSP_STEREO);
1265 _EXPORT_INT(m, SNDCTL_DSP_SUBDIVIDE);
1266 _EXPORT_INT(m, SNDCTL_DSP_SYNC);
1267 _EXPORT_INT(m, SNDCTL_FM_4OP_ENABLE);
1268 _EXPORT_INT(m, SNDCTL_FM_LOAD_INSTR);
1269 _EXPORT_INT(m, SNDCTL_MIDI_INFO);
1270 _EXPORT_INT(m, SNDCTL_MIDI_MPUCMD);
1271 _EXPORT_INT(m, SNDCTL_MIDI_MPUMODE);
1272 _EXPORT_INT(m, SNDCTL_MIDI_PRETIME);
1273 _EXPORT_INT(m, SNDCTL_SEQ_CTRLRATE);
1274 _EXPORT_INT(m, SNDCTL_SEQ_GETINCOUNT);
1275 _EXPORT_INT(m, SNDCTL_SEQ_GETOUTCOUNT);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001276#ifdef SNDCTL_SEQ_GETTIME
Greg Ward1e0f57d2002-11-30 23:05:26 +00001277 _EXPORT_INT(m, SNDCTL_SEQ_GETTIME);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001278#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001279 _EXPORT_INT(m, SNDCTL_SEQ_NRMIDIS);
1280 _EXPORT_INT(m, SNDCTL_SEQ_NRSYNTHS);
1281 _EXPORT_INT(m, SNDCTL_SEQ_OUTOFBAND);
1282 _EXPORT_INT(m, SNDCTL_SEQ_PANIC);
1283 _EXPORT_INT(m, SNDCTL_SEQ_PERCMODE);
1284 _EXPORT_INT(m, SNDCTL_SEQ_RESET);
1285 _EXPORT_INT(m, SNDCTL_SEQ_RESETSAMPLES);
1286 _EXPORT_INT(m, SNDCTL_SEQ_SYNC);
1287 _EXPORT_INT(m, SNDCTL_SEQ_TESTMIDI);
1288 _EXPORT_INT(m, SNDCTL_SEQ_THRESHOLD);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001289#ifdef SNDCTL_SYNTH_CONTROL
Greg Ward1e0f57d2002-11-30 23:05:26 +00001290 _EXPORT_INT(m, SNDCTL_SYNTH_CONTROL);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001291#endif
1292#ifdef SNDCTL_SYNTH_ID
Greg Ward1e0f57d2002-11-30 23:05:26 +00001293 _EXPORT_INT(m, SNDCTL_SYNTH_ID);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001294#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001295 _EXPORT_INT(m, SNDCTL_SYNTH_INFO);
1296 _EXPORT_INT(m, SNDCTL_SYNTH_MEMAVL);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001297#ifdef SNDCTL_SYNTH_REMOVESAMPLE
Greg Ward1e0f57d2002-11-30 23:05:26 +00001298 _EXPORT_INT(m, SNDCTL_SYNTH_REMOVESAMPLE);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001299#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001300 _EXPORT_INT(m, SNDCTL_TMR_CONTINUE);
1301 _EXPORT_INT(m, SNDCTL_TMR_METRONOME);
1302 _EXPORT_INT(m, SNDCTL_TMR_SELECT);
1303 _EXPORT_INT(m, SNDCTL_TMR_SOURCE);
1304 _EXPORT_INT(m, SNDCTL_TMR_START);
1305 _EXPORT_INT(m, SNDCTL_TMR_STOP);
1306 _EXPORT_INT(m, SNDCTL_TMR_TEMPO);
1307 _EXPORT_INT(m, SNDCTL_TMR_TIMEBASE);
Martin v. Löwis1a214512008-06-11 05:26:20 +00001308 return m;
Greg Ward04613a92002-11-30 22:47:45 +00001309}