blob: 5f0505c8a7b4b43323edf03ed3617acae41ec05d [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
Victor Stinner99953002015-03-20 10:37:34 +010022#define PY_SSIZE_T_CLEAN
Greg Ward04613a92002-11-30 22:47:45 +000023#include "Python.h"
24#include "structmember.h"
25
26#ifdef HAVE_FCNTL_H
27#include <fcntl.h>
28#else
29#define O_RDONLY 00
30#define O_WRONLY 01
31#endif
32
Greg Ward04613a92002-11-30 22:47:45 +000033#include <sys/ioctl.h>
Stefan Krahfa935c42016-04-26 16:48:48 +020034#ifdef __ANDROID__
35#include <linux/soundcard.h>
36#else
Greg Ward0b6dfb82003-03-10 03:17:06 +000037#include <sys/soundcard.h>
Stefan Krahfa935c42016-04-26 16:48:48 +020038#endif
Greg Ward0b6dfb82003-03-10 03:17:06 +000039
Benjamin Peterson840ef8f2016-09-07 14:45:10 -070040#ifdef __linux__
Greg Ward04613a92002-11-30 22:47:45 +000041
Thomas Wouters89f507f2006-12-13 04:49:30 +000042#ifndef HAVE_STDINT_H
Greg Ward04613a92002-11-30 22:47:45 +000043typedef unsigned long uint32_t;
Thomas Wouters89f507f2006-12-13 04:49:30 +000044#endif
Greg Ward04613a92002-11-30 22:47:45 +000045
46#elif defined(__FreeBSD__)
Greg Ward04613a92002-11-30 22:47:45 +000047
Greg Ward0b6dfb82003-03-10 03:17:06 +000048# ifndef SNDCTL_DSP_CHANNELS
49# define SNDCTL_DSP_CHANNELS SOUND_PCM_WRITE_CHANNELS
50# endif
Greg Ward04613a92002-11-30 22:47:45 +000051
52#endif
53
54typedef struct {
Neal Norwitz3f046482006-01-07 21:19:49 +000055 PyObject_HEAD
Greg Ward50682d02005-03-07 01:41:11 +000056 char *devicename; /* name of the device file */
57 int fd; /* file descriptor */
58 int mode; /* file mode (O_RDONLY, etc.) */
Victor Stinner99953002015-03-20 10:37:34 +010059 Py_ssize_t icount; /* input count */
60 Py_ssize_t ocount; /* output count */
Greg Ward50682d02005-03-07 01:41:11 +000061 uint32_t afmts; /* audio formats supported by hardware */
Greg Ward499b73e2002-12-31 03:04:52 +000062} oss_audio_t;
Greg Ward04613a92002-11-30 22:47:45 +000063
Greg Ward3d9994d2002-12-11 15:12:01 +000064typedef struct {
Neal Norwitz3f046482006-01-07 21:19:49 +000065 PyObject_HEAD
Greg Wardad4d9b92002-12-30 03:02:22 +000066 int fd; /* The open mixer device */
Greg Ward3d9994d2002-12-11 15:12:01 +000067} oss_mixer_t;
68
Greg Ward04613a92002-11-30 22:47:45 +000069
Greg Ward499b73e2002-12-31 03:04:52 +000070static PyTypeObject OSSAudioType;
Greg Ward3d9994d2002-12-11 15:12:01 +000071static PyTypeObject OSSMixerType;
Greg Ward04613a92002-11-30 22:47:45 +000072
Greg Ward97708bc2002-11-30 23:17:10 +000073static PyObject *OSSAudioError;
Greg Ward04613a92002-11-30 22:47:45 +000074
Greg Wardda1cacb2002-12-31 03:02:23 +000075
76/* ----------------------------------------------------------------------
77 * DSP object initialization/deallocation
78 */
79
Greg Ward499b73e2002-12-31 03:04:52 +000080static oss_audio_t *
Greg Ward8c6b6a92002-12-11 14:43:13 +000081newossobject(PyObject *arg)
Greg Ward04613a92002-11-30 22:47:45 +000082{
Greg Ward58ae13c2002-12-31 03:07:21 +000083 oss_audio_t *self;
Greg Ward04613a92002-11-30 22:47:45 +000084 int fd, afmts, imode;
Greg Ward50682d02005-03-07 01:41:11 +000085 char *devicename = NULL;
Greg Ward04613a92002-11-30 22:47:45 +000086 char *mode = NULL;
87
Greg Ward9a568eb2002-11-30 23:20:09 +000088 /* Two ways to call open():
Greg Ward04613a92002-11-30 22:47:45 +000089 open(device, mode) (for consistency with builtin open())
90 open(mode) (for backwards compatibility)
91 because the *first* argument is optional, parsing args is
92 a wee bit tricky. */
Greg Ward50682d02005-03-07 01:41:11 +000093 if (!PyArg_ParseTuple(arg, "s|s:open", &devicename, &mode))
Greg Ward04613a92002-11-30 22:47:45 +000094 return NULL;
95 if (mode == NULL) { /* only one arg supplied */
Greg Ward50682d02005-03-07 01:41:11 +000096 mode = devicename;
97 devicename = NULL;
Greg Ward04613a92002-11-30 22:47:45 +000098 }
99
100 if (strcmp(mode, "r") == 0)
101 imode = O_RDONLY;
102 else if (strcmp(mode, "w") == 0)
103 imode = O_WRONLY;
Greg Ward1e0f57d2002-11-30 23:05:26 +0000104 else if (strcmp(mode, "rw") == 0)
105 imode = O_RDWR;
Greg Ward04613a92002-11-30 22:47:45 +0000106 else {
Greg Ward97708bc2002-11-30 23:17:10 +0000107 PyErr_SetString(OSSAudioError, "mode must be 'r', 'w', or 'rw'");
Greg Ward04613a92002-11-30 22:47:45 +0000108 return NULL;
109 }
110
Greg Ward5c5c5772002-12-30 02:58:04 +0000111 /* Open the correct device: either the 'device' argument,
112 or the AUDIODEV environment variable, or "/dev/dsp". */
Greg Ward50682d02005-03-07 01:41:11 +0000113 if (devicename == NULL) { /* called with one arg */
114 devicename = getenv("AUDIODEV");
115 if (devicename == NULL) /* $AUDIODEV not set */
116 devicename = "/dev/dsp";
Greg Ward04613a92002-11-30 22:47:45 +0000117 }
118
Greg Ward5c49ef22003-03-11 16:53:13 +0000119 /* Open with O_NONBLOCK to avoid hanging on devices that only allow
120 one open at a time. This does *not* affect later I/O; OSS
121 provides a special ioctl() for non-blocking read/write, which is
122 exposed via oss_nonblock() below. */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200123 fd = _Py_open(devicename, imode|O_NONBLOCK);
Victor Stinnera555cfc2015-03-18 00:22:14 +0100124 if (fd == -1)
Greg Ward04613a92002-11-30 22:47:45 +0000125 return NULL;
Greg Ward76ffb192003-04-04 01:47:42 +0000126
127 /* And (try to) put it back in blocking mode so we get the
128 expected write() semantics. */
129 if (fcntl(fd, F_SETFL, 0) == -1) {
130 close(fd);
Greg Ward50682d02005-03-07 01:41:11 +0000131 PyErr_SetFromErrnoWithFilename(PyExc_IOError, devicename);
Greg Ward76ffb192003-04-04 01:47:42 +0000132 return NULL;
133 }
134
Greg Ward04613a92002-11-30 22:47:45 +0000135 if (ioctl(fd, SNDCTL_DSP_GETFMTS, &afmts) == -1) {
Charles-François Natali5a4a1092011-09-29 19:46:37 +0200136 close(fd);
Greg Ward50682d02005-03-07 01:41:11 +0000137 PyErr_SetFromErrnoWithFilename(PyExc_IOError, devicename);
Greg Ward04613a92002-11-30 22:47:45 +0000138 return NULL;
139 }
140 /* Create and initialize the object */
Greg Ward58ae13c2002-12-31 03:07:21 +0000141 if ((self = PyObject_New(oss_audio_t, &OSSAudioType)) == NULL) {
Greg Ward04613a92002-11-30 22:47:45 +0000142 close(fd);
143 return NULL;
144 }
Greg Ward50682d02005-03-07 01:41:11 +0000145 self->devicename = devicename;
Greg Ward58ae13c2002-12-31 03:07:21 +0000146 self->fd = fd;
147 self->mode = imode;
148 self->icount = self->ocount = 0;
149 self->afmts = afmts;
150 return self;
Greg Ward04613a92002-11-30 22:47:45 +0000151}
152
153static void
Greg Ward58ae13c2002-12-31 03:07:21 +0000154oss_dealloc(oss_audio_t *self)
Greg Ward04613a92002-11-30 22:47:45 +0000155{
156 /* if already closed, don't reclose it */
Greg Ward58ae13c2002-12-31 03:07:21 +0000157 if (self->fd != -1)
158 close(self->fd);
159 PyObject_Del(self);
Greg Ward04613a92002-11-30 22:47:45 +0000160}
161
Greg Wardda1cacb2002-12-31 03:02:23 +0000162
163/* ----------------------------------------------------------------------
164 * Mixer object initialization/deallocation
165 */
166
Greg Ward3d9994d2002-12-11 15:12:01 +0000167static oss_mixer_t *
168newossmixerobject(PyObject *arg)
169{
Greg Ward50682d02005-03-07 01:41:11 +0000170 char *devicename = NULL;
Greg Ward0b6dfb82003-03-10 03:17:06 +0000171 int fd;
Greg Ward58ae13c2002-12-31 03:07:21 +0000172 oss_mixer_t *self;
Guido van Rossum0741f802003-06-02 14:15:34 +0000173
Greg Ward50682d02005-03-07 01:41:11 +0000174 if (!PyArg_ParseTuple(arg, "|s", &devicename)) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000175 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000176 }
Guido van Rossum0741f802003-06-02 14:15:34 +0000177
Greg Ward50682d02005-03-07 01:41:11 +0000178 if (devicename == NULL) {
179 devicename = getenv("MIXERDEV");
180 if (devicename == NULL) /* MIXERDEV not set */
181 devicename = "/dev/mixer";
Greg Ward3d9994d2002-12-11 15:12:01 +0000182 }
183
Victor Stinnerdaf45552013-08-28 00:53:59 +0200184 fd = _Py_open(devicename, O_RDWR);
Victor Stinnera555cfc2015-03-18 00:22:14 +0100185 if (fd == -1)
Greg Ward3d9994d2002-12-11 15:12:01 +0000186 return NULL;
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{
Victor Stinner99953002015-03-20 10:37:34 +0100407 Py_ssize_t size, count;
Greg Ward04613a92002-11-30 22:47:45 +0000408 PyObject *rv;
Guido van Rossum0741f802003-06-02 14:15:34 +0000409
Charles-François Natalia5293082011-06-11 18:58:24 +0200410 if (!_is_fd_valid(self->fd))
411 return NULL;
412
Victor Stinner99953002015-03-20 10:37:34 +0100413 if (!PyArg_ParseTuple(args, "n:read", &size))
Greg Ward04613a92002-11-30 22:47:45 +0000414 return NULL;
Victor Stinner4fa99cd2015-03-20 00:27:28 +0100415
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;
Greg Ward64927852003-05-23 01:50:37 +0000419
Victor Stinner4fa99cd2015-03-20 00:27:28 +0100420 count = _Py_read(self->fd, PyBytes_AS_STRING(rv), size);
421 if (count == -1) {
Greg Ward04613a92002-11-30 22:47:45 +0000422 Py_DECREF(rv);
423 return NULL;
424 }
Victor Stinner4fa99cd2015-03-20 00:27:28 +0100425
Greg Ward7b43c682002-12-30 02:29:28 +0000426 self->icount += count;
Gregory P. Smith0a608fd2008-09-06 21:34:51 +0000427 _PyBytes_Resize(&rv, count);
Greg Ward04613a92002-11-30 22:47:45 +0000428 return rv;
429}
430
431static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000432oss_write(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000433{
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +0200434 Py_buffer data;
Victor Stinner99953002015-03-20 10:37:34 +0100435 Py_ssize_t rv;
Greg Ward131bce02002-11-30 22:56:44 +0000436
Charles-François Natalia5293082011-06-11 18:58:24 +0200437 if (!_is_fd_valid(self->fd))
438 return NULL;
439
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +0200440 if (!PyArg_ParseTuple(args, "y*:write", &data)) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000441 return NULL;
Greg Ward131bce02002-11-30 22:56:44 +0000442 }
Greg Ward64927852003-05-23 01:50:37 +0000443
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +0200444 rv = _Py_write(self->fd, data.buf, data.len);
445 PyBuffer_Release(&data);
Victor Stinner4fa99cd2015-03-20 00:27:28 +0100446 if (rv == -1)
447 return NULL;
Greg Ward64927852003-05-23 01:50:37 +0000448
Victor Stinner4fa99cd2015-03-20 00:27:28 +0100449 self->ocount += rv;
Christian Heimes217cfd12007-12-02 14:31:20 +0000450 return PyLong_FromLong(rv);
Greg Ward131bce02002-11-30 22:56:44 +0000451}
452
453static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000454oss_writeall(oss_audio_t *self, PyObject *args)
Greg Ward131bce02002-11-30 22:56:44 +0000455{
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +0200456 Py_buffer data;
457 const char *cp;
458 Py_ssize_t size;
Victor Stinner99953002015-03-20 10:37:34 +0100459 Py_ssize_t rv;
Greg Ward131bce02002-11-30 22:56:44 +0000460 fd_set write_set_fds;
461 int select_rv;
Guido van Rossum0741f802003-06-02 14:15:34 +0000462
Greg Ward131bce02002-11-30 22:56:44 +0000463 /* NB. writeall() is only useful in non-blocking mode: according to
464 Guenter Geiger <geiger@xdv.org> on the linux-audio-dev list
465 (http://eca.cx/lad/2002/11/0380.html), OSS guarantees that
466 write() in blocking mode consumes the whole buffer. In blocking
467 mode, the behaviour of write() and writeall() from Python is
468 indistinguishable. */
469
Charles-François Natalia5293082011-06-11 18:58:24 +0200470 if (!_is_fd_valid(self->fd))
471 return NULL;
472
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +0200473 if (!PyArg_ParseTuple(args, "y*:writeall", &data))
Greg Ward131bce02002-11-30 22:56:44 +0000474 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +0000475
Charles-François Nataliaa26b272011-08-28 17:51:43 +0200476 if (!_PyIsSelectable_fd(self->fd)) {
477 PyErr_SetString(PyExc_ValueError,
478 "file descriptor out of range for select");
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +0200479 PyBuffer_Release(&data);
Charles-François Nataliaa26b272011-08-28 17:51:43 +0200480 return NULL;
481 }
Greg Ward04613a92002-11-30 22:47:45 +0000482 /* use select to wait for audio device to be available */
483 FD_ZERO(&write_set_fds);
Greg Ward7b43c682002-12-30 02:29:28 +0000484 FD_SET(self->fd, &write_set_fds);
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +0200485 cp = (const char *)data.buf;
486 size = data.len;
Greg Ward04613a92002-11-30 22:47:45 +0000487
488 while (size > 0) {
Greg Ward64927852003-05-23 01:50:37 +0000489 Py_BEGIN_ALLOW_THREADS
Greg Ward7b43c682002-12-30 02:29:28 +0000490 select_rv = select(self->fd+1, NULL, &write_set_fds, NULL, NULL);
Greg Ward64927852003-05-23 01:50:37 +0000491 Py_END_ALLOW_THREADS
Victor Stinner4fa99cd2015-03-20 00:27:28 +0100492
493 assert(select_rv != 0); /* no timeout, can't expire */
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +0200494 if (select_rv == -1) {
495 PyBuffer_Release(&data);
Greg Ward97708bc2002-11-30 23:17:10 +0000496 return PyErr_SetFromErrno(PyExc_IOError);
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +0200497 }
Greg Ward131bce02002-11-30 22:56:44 +0000498
Victor Stinner716a74e2015-03-20 10:24:18 +0100499 rv = _Py_write(self->fd, cp, Py_MIN(size, INT_MAX));
Greg Ward131bce02002-11-30 22:56:44 +0000500 if (rv == -1) {
Victor Stinner4fa99cd2015-03-20 00:27:28 +0100501 /* buffer is full, try again */
502 if (errno == EAGAIN) {
503 PyErr_Clear();
Greg Ward131bce02002-11-30 22:56:44 +0000504 continue;
Victor Stinner4fa99cd2015-03-20 00:27:28 +0100505 }
506 /* it's a real error */
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +0200507 PyBuffer_Release(&data);
Victor Stinner4fa99cd2015-03-20 00:27:28 +0100508 return NULL;
Greg Ward131bce02002-11-30 22:56:44 +0000509 }
Victor Stinner4fa99cd2015-03-20 00:27:28 +0100510
511 /* wrote rv bytes */
512 self->ocount += rv;
513 size -= rv;
514 cp += rv;
Greg Ward04613a92002-11-30 22:47:45 +0000515 }
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +0200516 PyBuffer_Release(&data);
Greg Ward04613a92002-11-30 22:47:45 +0000517 Py_INCREF(Py_None);
518 return Py_None;
519}
520
521static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000522oss_close(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000523{
Greg Ward7b43c682002-12-30 02:29:28 +0000524 if (self->fd >= 0) {
Greg Wardb8043902003-05-26 22:47:30 +0000525 Py_BEGIN_ALLOW_THREADS
Greg Ward7b43c682002-12-30 02:29:28 +0000526 close(self->fd);
Greg Wardb8043902003-05-26 22:47:30 +0000527 Py_END_ALLOW_THREADS
Greg Ward7b43c682002-12-30 02:29:28 +0000528 self->fd = -1;
Greg Ward04613a92002-11-30 22:47:45 +0000529 }
530 Py_INCREF(Py_None);
531 return Py_None;
532}
533
534static PyObject *
Georg Brandl770a2be2010-10-24 20:47:32 +0000535oss_self(PyObject *self, PyObject *unused)
Georg Brandl1e908af2010-10-23 17:31:52 +0000536{
537 Py_INCREF(self);
538 return self;
539}
540
Victor Stinner63941882011-09-29 00:42:28 +0200541static PyObject *
Georg Brandl1e908af2010-10-23 17:31:52 +0000542oss_exit(PyObject *self, PyObject *unused)
543{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200544 _Py_IDENTIFIER(close);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200545
546 PyObject *ret = _PyObject_CallMethodId(self, &PyId_close, NULL);
Georg Brandl1e908af2010-10-23 17:31:52 +0000547 if (!ret)
548 return NULL;
549 Py_DECREF(ret);
550 Py_RETURN_NONE;
551}
552
553static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000554oss_fileno(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000555{
Charles-François Natalia5293082011-06-11 18:58:24 +0200556 if (!_is_fd_valid(self->fd))
557 return NULL;
558
Christian Heimes217cfd12007-12-02 14:31:20 +0000559 return PyLong_FromLong(self->fd);
Greg Ward04613a92002-11-30 22:47:45 +0000560}
561
Greg Ward131bce02002-11-30 22:56:44 +0000562
563/* Convenience methods: these generally wrap a couple of ioctls into one
564 common task. */
565
Greg Ward04613a92002-11-30 22:47:45 +0000566static PyObject *
Greg Ward499b73e2002-12-31 03:04:52 +0000567oss_setparameters(oss_audio_t *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +0000568{
Greg Wardd6769062003-05-29 21:53:06 +0000569 int wanted_fmt, wanted_channels, wanted_rate, strict=0;
570 int fmt, channels, rate;
Greg Ward04613a92002-11-30 22:47:45 +0000571
Charles-François Natalia5293082011-06-11 18:58:24 +0200572 if (!_is_fd_valid(self->fd))
573 return NULL;
574
Greg Wardd6769062003-05-29 21:53:06 +0000575 if (!PyArg_ParseTuple(args, "iii|i:setparameters",
576 &wanted_fmt, &wanted_channels, &wanted_rate,
577 &strict))
Greg Ward04613a92002-11-30 22:47:45 +0000578 return NULL;
Greg Wardd6769062003-05-29 21:53:06 +0000579
580 fmt = wanted_fmt;
581 if (ioctl(self->fd, SNDCTL_DSP_SETFMT, &fmt) == -1) {
582 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000583 }
Greg Wardd6769062003-05-29 21:53:06 +0000584 if (strict && fmt != wanted_fmt) {
585 return PyErr_Format
586 (OSSAudioError,
587 "unable to set requested format (wanted %d, got %d)",
588 wanted_fmt, fmt);
Greg Ward04613a92002-11-30 22:47:45 +0000589 }
590
Greg Wardd6769062003-05-29 21:53:06 +0000591 channels = wanted_channels;
592 if (ioctl(self->fd, SNDCTL_DSP_CHANNELS, &channels) == -1) {
593 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000594 }
Greg Wardd6769062003-05-29 21:53:06 +0000595 if (strict && channels != wanted_channels) {
596 return PyErr_Format
597 (OSSAudioError,
598 "unable to set requested channels (wanted %d, got %d)",
599 wanted_channels, channels);
Greg Ward04613a92002-11-30 22:47:45 +0000600 }
601
Greg Wardd6769062003-05-29 21:53:06 +0000602 rate = wanted_rate;
Greg Ward7b43c682002-12-30 02:29:28 +0000603 if (ioctl(self->fd, SNDCTL_DSP_SPEED, &rate) == -1) {
Greg Wardd6769062003-05-29 21:53:06 +0000604 return PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000605 }
Greg Wardd6769062003-05-29 21:53:06 +0000606 if (strict && rate != wanted_rate) {
607 return PyErr_Format
608 (OSSAudioError,
609 "unable to set requested rate (wanted %d, got %d)",
610 wanted_rate, rate);
611 }
Guido van Rossum0741f802003-06-02 14:15:34 +0000612
Greg Wardd6769062003-05-29 21:53:06 +0000613 /* Construct the return value: a (fmt, channels, rate) tuple that
614 tells what the audio hardware was actually set to. */
Serhiy Storchaka48d761e2013-12-17 15:11:24 +0200615 return Py_BuildValue("(iii)", fmt, channels, rate);
Greg Ward04613a92002-11-30 22:47:45 +0000616}
617
618static int
Greg Ward499b73e2002-12-31 03:04:52 +0000619_ssize(oss_audio_t *self, int *nchannels, int *ssize)
Greg Ward04613a92002-11-30 22:47:45 +0000620{
621 int fmt;
622
623 fmt = 0;
Guido van Rossum0741f802003-06-02 14:15:34 +0000624 if (ioctl(self->fd, SNDCTL_DSP_SETFMT, &fmt) < 0)
Greg Ward04613a92002-11-30 22:47:45 +0000625 return -errno;
626
627 switch (fmt) {
628 case AFMT_MU_LAW:
629 case AFMT_A_LAW:
630 case AFMT_U8:
631 case AFMT_S8:
Greg Ward38c92662003-05-29 21:55:41 +0000632 *ssize = 1; /* 8 bit formats: 1 byte */
Greg Ward04613a92002-11-30 22:47:45 +0000633 break;
634 case AFMT_S16_LE:
635 case AFMT_S16_BE:
636 case AFMT_U16_LE:
637 case AFMT_U16_BE:
Greg Ward84f8ecd2003-05-29 23:44:44 +0000638 *ssize = 2; /* 16 bit formats: 2 byte */
Greg Ward04613a92002-11-30 22:47:45 +0000639 break;
640 case AFMT_MPEG:
641 case AFMT_IMA_ADPCM:
642 default:
643 return -EOPNOTSUPP;
644 }
Greg Ward7b43c682002-12-30 02:29:28 +0000645 if (ioctl(self->fd, SNDCTL_DSP_CHANNELS, nchannels) < 0)
Greg Ward04613a92002-11-30 22:47:45 +0000646 return -errno;
647 return 0;
648}
649
650
Guido van Rossum0741f802003-06-02 14:15:34 +0000651/* bufsize returns the size of the hardware audio buffer in number
Greg Ward04613a92002-11-30 22:47:45 +0000652 of samples */
653static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000654oss_bufsize(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000655{
656 audio_buf_info ai;
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000657 int nchannels=0, ssize=0;
Greg Ward04613a92002-11-30 22:47:45 +0000658
Charles-François Natalia5293082011-06-11 18:58:24 +0200659 if (!_is_fd_valid(self->fd))
660 return NULL;
661
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000662 if (_ssize(self, &nchannels, &ssize) < 0 || !nchannels || !ssize) {
Greg Ward97708bc2002-11-30 23:17:10 +0000663 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000664 return NULL;
665 }
Greg Ward7b43c682002-12-30 02:29:28 +0000666 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000667 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000668 return NULL;
669 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000670 return PyLong_FromLong((ai.fragstotal * ai.fragsize) / (nchannels * ssize));
Greg Ward04613a92002-11-30 22:47:45 +0000671}
672
Guido van Rossum0741f802003-06-02 14:15:34 +0000673/* obufcount returns the number of samples that are available in the
Greg Ward04613a92002-11-30 22:47:45 +0000674 hardware for playing */
675static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000676oss_obufcount(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000677{
678 audio_buf_info ai;
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000679 int nchannels=0, ssize=0;
Greg Ward04613a92002-11-30 22:47:45 +0000680
Charles-François Natalia5293082011-06-11 18:58:24 +0200681 if (!_is_fd_valid(self->fd))
682 return NULL;
683
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000684 if (_ssize(self, &nchannels, &ssize) < 0 || !nchannels || !ssize) {
Greg Ward97708bc2002-11-30 23:17:10 +0000685 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000686 return NULL;
687 }
Greg Ward7b43c682002-12-30 02:29:28 +0000688 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000689 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000690 return NULL;
691 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000692 return PyLong_FromLong((ai.fragstotal * ai.fragsize - ai.bytes) /
Greg Ward04613a92002-11-30 22:47:45 +0000693 (ssize * nchannels));
694}
695
696/* obufcount returns the number of samples that can be played without
697 blocking */
698static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000699oss_obuffree(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000700{
701 audio_buf_info ai;
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000702 int nchannels=0, ssize=0;
Greg Ward04613a92002-11-30 22:47:45 +0000703
Charles-François Natalia5293082011-06-11 18:58:24 +0200704 if (!_is_fd_valid(self->fd))
705 return NULL;
706
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000707 if (_ssize(self, &nchannels, &ssize) < 0 || !nchannels || !ssize) {
Greg Ward97708bc2002-11-30 23:17:10 +0000708 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000709 return NULL;
710 }
Greg Ward7b43c682002-12-30 02:29:28 +0000711 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
Greg Ward97708bc2002-11-30 23:17:10 +0000712 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000713 return NULL;
714 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000715 return PyLong_FromLong(ai.bytes / (ssize * nchannels));
Greg Ward04613a92002-11-30 22:47:45 +0000716}
717
Greg Ward04613a92002-11-30 22:47:45 +0000718static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000719oss_getptr(oss_audio_t *self, PyObject *unused)
Greg Ward04613a92002-11-30 22:47:45 +0000720{
721 count_info info;
722 int req;
723
Charles-François Natalia5293082011-06-11 18:58:24 +0200724 if (!_is_fd_valid(self->fd))
725 return NULL;
726
Greg Ward7b43c682002-12-30 02:29:28 +0000727 if (self->mode == O_RDONLY)
Greg Wardad4d9b92002-12-30 03:02:22 +0000728 req = SNDCTL_DSP_GETIPTR;
Greg Ward04613a92002-11-30 22:47:45 +0000729 else
Greg Wardad4d9b92002-12-30 03:02:22 +0000730 req = SNDCTL_DSP_GETOPTR;
Greg Ward7b43c682002-12-30 02:29:28 +0000731 if (ioctl(self->fd, req, &info) == -1) {
Greg Ward97708bc2002-11-30 23:17:10 +0000732 PyErr_SetFromErrno(PyExc_IOError);
Greg Ward04613a92002-11-30 22:47:45 +0000733 return NULL;
734 }
735 return Py_BuildValue("iii", info.bytes, info.blocks, info.ptr);
736}
737
Greg Wardda1cacb2002-12-31 03:02:23 +0000738
739/* ----------------------------------------------------------------------
740 * Methods of mixer objects (OSSMixerType)
741 */
742
Greg Ward3d9994d2002-12-11 15:12:01 +0000743static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000744oss_mixer_close(oss_mixer_t *self, PyObject *unused)
Greg Ward3d9994d2002-12-11 15:12:01 +0000745{
Greg Ward7b43c682002-12-30 02:29:28 +0000746 if (self->fd >= 0) {
747 close(self->fd);
748 self->fd = -1;
Greg Ward3d9994d2002-12-11 15:12:01 +0000749 }
750 Py_INCREF(Py_None);
751 return Py_None;
752}
753
754static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000755oss_mixer_fileno(oss_mixer_t *self, PyObject *unused)
Greg Ward3d9994d2002-12-11 15:12:01 +0000756{
Charles-François Natalia5293082011-06-11 18:58:24 +0200757 if (!_is_fd_valid(self->fd))
758 return NULL;
759
Christian Heimes217cfd12007-12-02 14:31:20 +0000760 return PyLong_FromLong(self->fd);
Greg Ward3d9994d2002-12-11 15:12:01 +0000761}
762
763/* Simple mixer interface methods */
764
765static PyObject *
Greg Ward2d6f9a92002-12-31 02:54:43 +0000766oss_mixer_controls(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000767{
Charles-François Natalia5293082011-06-11 18:58:24 +0200768 if (!_is_fd_valid(self->fd))
769 return NULL;
770
Greg Ward2d6f9a92002-12-31 02:54:43 +0000771 return _do_ioctl_1_internal(self->fd, args, "controls",
Greg Ward3d9994d2002-12-11 15:12:01 +0000772 SOUND_MIXER_READ_DEVMASK);
773}
774
775static PyObject *
Greg Ward2d6f9a92002-12-31 02:54:43 +0000776oss_mixer_stereocontrols(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000777{
Charles-François Natalia5293082011-06-11 18:58:24 +0200778 if (!_is_fd_valid(self->fd))
779 return NULL;
780
Greg Ward2d6f9a92002-12-31 02:54:43 +0000781 return _do_ioctl_1_internal(self->fd, args, "stereocontrols",
Greg Ward3d9994d2002-12-11 15:12:01 +0000782 SOUND_MIXER_READ_STEREODEVS);
783}
784
785static PyObject *
Greg Ward2d6f9a92002-12-31 02:54:43 +0000786oss_mixer_reccontrols(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000787{
Charles-François Natalia5293082011-06-11 18:58:24 +0200788 if (!_is_fd_valid(self->fd))
789 return NULL;
790
Greg Ward2d6f9a92002-12-31 02:54:43 +0000791 return _do_ioctl_1_internal(self->fd, args, "reccontrols",
Greg Wardb69bb3d2002-12-12 17:35:45 +0000792 SOUND_MIXER_READ_RECMASK);
Greg Ward3d9994d2002-12-11 15:12:01 +0000793}
794
795static PyObject *
Greg Warde7037662002-12-30 03:01:48 +0000796oss_mixer_get(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000797{
798 int channel, volume;
Guido van Rossum0741f802003-06-02 14:15:34 +0000799
Charles-François Natalia5293082011-06-11 18:58:24 +0200800 if (!_is_fd_valid(self->fd))
801 return NULL;
802
Greg Ward3d9994d2002-12-11 15:12:01 +0000803 /* Can't use _do_ioctl_1 because of encoded arg thingy. */
Greg Warde7037662002-12-30 03:01:48 +0000804 if (!PyArg_ParseTuple(args, "i:get", &channel))
Greg Wardad4d9b92002-12-30 03:02:22 +0000805 return NULL;
Guido van Rossum0741f802003-06-02 14:15:34 +0000806
Greg Ward3d9994d2002-12-11 15:12:01 +0000807 if (channel < 0 || channel > SOUND_MIXER_NRDEVICES) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000808 PyErr_SetString(OSSAudioError, "Invalid mixer channel specified.");
809 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000810 }
Guido van Rossum0741f802003-06-02 14:15:34 +0000811
Greg Warde7037662002-12-30 03:01:48 +0000812 if (ioctl(self->fd, MIXER_READ(channel), &volume) == -1)
Greg Wardad4d9b92002-12-30 03:02:22 +0000813 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum0741f802003-06-02 14:15:34 +0000814
Greg Warde7037662002-12-30 03:01:48 +0000815 return Py_BuildValue("(ii)", volume & 0xff, (volume & 0xff00) >> 8);
Greg Ward3d9994d2002-12-11 15:12:01 +0000816}
817
818static PyObject *
Greg Warde7037662002-12-30 03:01:48 +0000819oss_mixer_set(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000820{
821 int channel, volume, leftVol, rightVol;
Guido van Rossum0741f802003-06-02 14:15:34 +0000822
Charles-François Natalia5293082011-06-11 18:58:24 +0200823 if (!_is_fd_valid(self->fd))
824 return NULL;
825
Greg Ward3d9994d2002-12-11 15:12:01 +0000826 /* Can't use _do_ioctl_1 because of encoded arg thingy. */
Greg Warde7037662002-12-30 03:01:48 +0000827 if (!PyArg_ParseTuple(args, "i(ii):set", &channel, &leftVol, &rightVol))
Greg Wardad4d9b92002-12-30 03:02:22 +0000828 return NULL;
Guido van Rossum0741f802003-06-02 14:15:34 +0000829
Greg Ward3d9994d2002-12-11 15:12:01 +0000830 if (channel < 0 || channel > SOUND_MIXER_NRDEVICES) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000831 PyErr_SetString(OSSAudioError, "Invalid mixer channel specified.");
832 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000833 }
Guido van Rossum0741f802003-06-02 14:15:34 +0000834
Greg Ward3d9994d2002-12-11 15:12:01 +0000835 if (leftVol < 0 || rightVol < 0 || leftVol > 100 || rightVol > 100) {
Greg Wardad4d9b92002-12-30 03:02:22 +0000836 PyErr_SetString(OSSAudioError, "Volumes must be between 0 and 100.");
837 return NULL;
Greg Ward3d9994d2002-12-11 15:12:01 +0000838 }
839
840 volume = (rightVol << 8) | leftVol;
Guido van Rossum0741f802003-06-02 14:15:34 +0000841
Greg Warde7037662002-12-30 03:01:48 +0000842 if (ioctl(self->fd, MIXER_WRITE(channel), &volume) == -1)
Greg Wardad4d9b92002-12-30 03:02:22 +0000843 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum0741f802003-06-02 14:15:34 +0000844
Greg Warde7037662002-12-30 03:01:48 +0000845 return Py_BuildValue("(ii)", volume & 0xff, (volume & 0xff00) >> 8);
Greg Ward3d9994d2002-12-11 15:12:01 +0000846}
847
848static PyObject *
Greg Wardf05aa102002-12-30 23:19:32 +0000849oss_mixer_get_recsrc(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000850{
Charles-François Natalia5293082011-06-11 18:58:24 +0200851 if (!_is_fd_valid(self->fd))
852 return NULL;
853
Greg Wardf05aa102002-12-30 23:19:32 +0000854 return _do_ioctl_1_internal(self->fd, args, "get_recsrc",
Greg Ward3d9994d2002-12-11 15:12:01 +0000855 SOUND_MIXER_READ_RECSRC);
856}
857
858static PyObject *
Greg Wardf05aa102002-12-30 23:19:32 +0000859oss_mixer_set_recsrc(oss_mixer_t *self, PyObject *args)
Greg Ward3d9994d2002-12-11 15:12:01 +0000860{
Charles-François Natalia5293082011-06-11 18:58:24 +0200861 if (!_is_fd_valid(self->fd))
862 return NULL;
863
Greg Wardf05aa102002-12-30 23:19:32 +0000864 return _do_ioctl_1(self->fd, args, "set_recsrc",
Greg Ward3d9994d2002-12-11 15:12:01 +0000865 SOUND_MIXER_WRITE_RECSRC);
866}
867
868
Greg Wardda1cacb2002-12-31 03:02:23 +0000869/* ----------------------------------------------------------------------
870 * Method tables and other bureaucracy
871 */
872
Greg Ward8c6b6a92002-12-11 14:43:13 +0000873static PyMethodDef oss_methods[] = {
Greg Ward131bce02002-11-30 22:56:44 +0000874 /* Regular file methods */
Greg Wardad4d9b92002-12-30 03:02:22 +0000875 { "read", (PyCFunction)oss_read, METH_VARARGS },
876 { "write", (PyCFunction)oss_write, METH_VARARGS },
877 { "writeall", (PyCFunction)oss_writeall, METH_VARARGS },
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000878 { "close", (PyCFunction)oss_close, METH_NOARGS },
879 { "fileno", (PyCFunction)oss_fileno, METH_NOARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000880
881 /* Simple ioctl wrappers */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000882 { "nonblock", (PyCFunction)oss_nonblock, METH_NOARGS },
Greg Wardad4d9b92002-12-30 03:02:22 +0000883 { "setfmt", (PyCFunction)oss_setfmt, METH_VARARGS },
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000884 { "getfmts", (PyCFunction)oss_getfmts, METH_NOARGS },
Greg Ward8c6b6a92002-12-11 14:43:13 +0000885 { "channels", (PyCFunction)oss_channels, METH_VARARGS },
886 { "speed", (PyCFunction)oss_speed, METH_VARARGS },
Greg Wardad4d9b92002-12-30 03:02:22 +0000887 { "sync", (PyCFunction)oss_sync, METH_VARARGS },
888 { "reset", (PyCFunction)oss_reset, METH_VARARGS },
889 { "post", (PyCFunction)oss_post, METH_VARARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000890
891 /* Convenience methods -- wrap a couple of ioctls together */
Greg Wardad4d9b92002-12-30 03:02:22 +0000892 { "setparameters", (PyCFunction)oss_setparameters, METH_VARARGS },
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000893 { "bufsize", (PyCFunction)oss_bufsize, METH_NOARGS },
894 { "obufcount", (PyCFunction)oss_obufcount, METH_NOARGS },
895 { "obuffree", (PyCFunction)oss_obuffree, METH_NOARGS },
896 { "getptr", (PyCFunction)oss_getptr, METH_NOARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000897
898 /* Aliases for backwards compatibility */
Greg Wardad4d9b92002-12-30 03:02:22 +0000899 { "flush", (PyCFunction)oss_sync, METH_VARARGS },
Greg Ward131bce02002-11-30 22:56:44 +0000900
Serhiy Storchaka14867992014-09-10 23:43:41 +0300901 /* Support for the context management protocol */
Georg Brandl770a2be2010-10-24 20:47:32 +0000902 { "__enter__", oss_self, METH_NOARGS },
903 { "__exit__", oss_exit, METH_VARARGS },
Georg Brandl1e908af2010-10-23 17:31:52 +0000904
Greg Wardad4d9b92002-12-30 03:02:22 +0000905 { NULL, NULL} /* sentinel */
Greg Ward04613a92002-11-30 22:47:45 +0000906};
907
Greg Ward3d9994d2002-12-11 15:12:01 +0000908static PyMethodDef oss_mixer_methods[] = {
909 /* Regular file method - OSS mixers are ioctl-only interface */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000910 { "close", (PyCFunction)oss_mixer_close, METH_NOARGS },
911 { "fileno", (PyCFunction)oss_mixer_fileno, METH_NOARGS },
Greg Ward3d9994d2002-12-11 15:12:01 +0000912
Serhiy Storchaka14867992014-09-10 23:43:41 +0300913 /* Support for the context management protocol */
Georg Brandl770a2be2010-10-24 20:47:32 +0000914 { "__enter__", oss_self, METH_NOARGS },
915 { "__exit__", oss_exit, METH_VARARGS },
Georg Brandl1e908af2010-10-23 17:31:52 +0000916
Greg Ward3d9994d2002-12-11 15:12:01 +0000917 /* Simple ioctl wrappers */
Guido van Rossum0741f802003-06-02 14:15:34 +0000918 { "controls", (PyCFunction)oss_mixer_controls, METH_VARARGS },
Greg Ward2d6f9a92002-12-31 02:54:43 +0000919 { "stereocontrols", (PyCFunction)oss_mixer_stereocontrols, METH_VARARGS},
Guido van Rossum0741f802003-06-02 14:15:34 +0000920 { "reccontrols", (PyCFunction)oss_mixer_reccontrols, METH_VARARGS},
Greg Wardad4d9b92002-12-30 03:02:22 +0000921 { "get", (PyCFunction)oss_mixer_get, METH_VARARGS },
922 { "set", (PyCFunction)oss_mixer_set, METH_VARARGS },
Greg Wardf05aa102002-12-30 23:19:32 +0000923 { "get_recsrc", (PyCFunction)oss_mixer_get_recsrc, METH_VARARGS },
924 { "set_recsrc", (PyCFunction)oss_mixer_set_recsrc, METH_VARARGS },
Guido van Rossum0741f802003-06-02 14:15:34 +0000925
Greg Wardad4d9b92002-12-30 03:02:22 +0000926 { NULL, NULL}
Greg Ward3d9994d2002-12-11 15:12:01 +0000927};
928
Greg Ward04613a92002-11-30 22:47:45 +0000929static PyObject *
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000930oss_getattro(oss_audio_t *self, PyObject *nameobj)
Greg Ward04613a92002-11-30 22:47:45 +0000931{
Serhiy Storchaka144f77a2016-11-20 08:47:21 +0200932 const char *name = "";
Greg Ward50682d02005-03-07 01:41:11 +0000933 PyObject * rval = NULL;
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000934
Serhiy Storchaka144f77a2016-11-20 08:47:21 +0200935 if (PyUnicode_Check(nameobj)) {
936 name = PyUnicode_AsUTF8(nameobj);
937 if (name == NULL)
938 return NULL;
939 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940
Greg Ward50682d02005-03-07 01:41:11 +0000941 if (strcmp(name, "closed") == 0) {
942 rval = (self->fd == -1) ? Py_True : Py_False;
943 Py_INCREF(rval);
944 }
945 else if (strcmp(name, "name") == 0) {
Neal Norwitz8abd2e62007-08-26 04:57:08 +0000946 rval = PyUnicode_FromString(self->devicename);
Greg Ward50682d02005-03-07 01:41:11 +0000947 }
948 else if (strcmp(name, "mode") == 0) {
949 /* No need for a "default" in this switch: from newossobject(),
950 self->mode can only be one of these three values. */
951 switch(self->mode) {
952 case O_RDONLY:
Neal Norwitz8abd2e62007-08-26 04:57:08 +0000953 rval = PyUnicode_FromString("r");
Greg Ward50682d02005-03-07 01:41:11 +0000954 break;
955 case O_RDWR:
Neal Norwitz8abd2e62007-08-26 04:57:08 +0000956 rval = PyUnicode_FromString("rw");
Greg Ward50682d02005-03-07 01:41:11 +0000957 break;
958 case O_WRONLY:
Neal Norwitz8abd2e62007-08-26 04:57:08 +0000959 rval = PyUnicode_FromString("w");
Greg Ward50682d02005-03-07 01:41:11 +0000960 break;
961 }
962 }
963 else {
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000964 rval = PyObject_GenericGetAttr((PyObject *)self, nameobj);
Greg Ward50682d02005-03-07 01:41:11 +0000965 }
966 return rval;
Greg Ward04613a92002-11-30 22:47:45 +0000967}
968
Greg Ward499b73e2002-12-31 03:04:52 +0000969static PyTypeObject OSSAudioType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000970 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Greg Ward9a568eb2002-11-30 23:20:09 +0000971 "ossaudiodev.oss_audio_device", /*tp_name*/
Greg Ward499b73e2002-12-31 03:04:52 +0000972 sizeof(oss_audio_t), /*tp_size*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000973 0, /*tp_itemsize*/
Greg Ward04613a92002-11-30 22:47:45 +0000974 /* methods */
Greg Wardad4d9b92002-12-30 03:02:22 +0000975 (destructor)oss_dealloc, /*tp_dealloc*/
976 0, /*tp_print*/
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000977 0, /*tp_getattr*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000978 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000979 0, /*tp_reserved*/
Greg Wardad4d9b92002-12-30 03:02:22 +0000980 0, /*tp_repr*/
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000981 0, /*tp_as_number*/
982 0, /*tp_as_sequence*/
983 0, /*tp_as_mapping*/
984 0, /*tp_hash*/
985 0, /*tp_call*/
986 0, /*tp_str*/
987 (getattrofunc)oss_getattro, /*tp_getattro*/
988 0, /*tp_setattro*/
989 0, /*tp_as_buffer*/
990 Py_TPFLAGS_DEFAULT, /*tp_flags*/
991 0, /*tp_doc*/
992 0, /*tp_traverse*/
993 0, /*tp_clear*/
994 0, /*tp_richcompare*/
995 0, /*tp_weaklistoffset*/
996 0, /*tp_iter*/
997 0, /*tp_iternext*/
998 oss_methods, /*tp_methods*/
Greg Ward04613a92002-11-30 22:47:45 +0000999};
1000
Greg Ward3d9994d2002-12-11 15:12:01 +00001001static PyTypeObject OSSMixerType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001002 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Greg Ward3d9994d2002-12-11 15:12:01 +00001003 "ossaudiodev.oss_mixer_device", /*tp_name*/
Greg Wardad4d9b92002-12-30 03:02:22 +00001004 sizeof(oss_mixer_t), /*tp_size*/
1005 0, /*tp_itemsize*/
Greg Ward3d9994d2002-12-11 15:12:01 +00001006 /* methods */
1007 (destructor)oss_mixer_dealloc, /*tp_dealloc*/
Greg Wardad4d9b92002-12-30 03:02:22 +00001008 0, /*tp_print*/
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +00001009 0, /*tp_getattr*/
Greg Wardad4d9b92002-12-30 03:02:22 +00001010 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001011 0, /*tp_reserved*/
Greg Wardad4d9b92002-12-30 03:02:22 +00001012 0, /*tp_repr*/
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +00001013 0, /*tp_as_number*/
1014 0, /*tp_as_sequence*/
1015 0, /*tp_as_mapping*/
1016 0, /*tp_hash*/
1017 0, /*tp_call*/
1018 0, /*tp_str*/
1019 0, /*tp_getattro*/
1020 0, /*tp_setattro*/
1021 0, /*tp_as_buffer*/
1022 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1023 0, /*tp_doc*/
1024 0, /*tp_traverse*/
1025 0, /*tp_clear*/
1026 0, /*tp_richcompare*/
1027 0, /*tp_weaklistoffset*/
1028 0, /*tp_iter*/
1029 0, /*tp_iternext*/
1030 oss_mixer_methods, /*tp_methods*/
Greg Ward3d9994d2002-12-11 15:12:01 +00001031};
1032
1033
Greg Ward04613a92002-11-30 22:47:45 +00001034static PyObject *
Greg Ward9a568eb2002-11-30 23:20:09 +00001035ossopen(PyObject *self, PyObject *args)
Greg Ward04613a92002-11-30 22:47:45 +00001036{
Greg Ward8c6b6a92002-12-11 14:43:13 +00001037 return (PyObject *)newossobject(args);
Greg Ward04613a92002-11-30 22:47:45 +00001038}
1039
Greg Ward3d9994d2002-12-11 15:12:01 +00001040static PyObject *
1041ossopenmixer(PyObject *self, PyObject *args)
1042{
1043 return (PyObject *)newossmixerobject(args);
1044}
1045
Greg Ward9a568eb2002-11-30 23:20:09 +00001046static PyMethodDef ossaudiodev_methods[] = {
1047 { "open", ossopen, METH_VARARGS },
Greg Ward3d9994d2002-12-11 15:12:01 +00001048 { "openmixer", ossopenmixer, METH_VARARGS },
Greg Ward04613a92002-11-30 22:47:45 +00001049 { 0, 0 },
1050};
1051
Greg Ward1e0f57d2002-11-30 23:05:26 +00001052
1053#define _EXPORT_INT(mod, name) \
Martin v. Löwis1a214512008-06-11 05:26:20 +00001054 if (PyModule_AddIntConstant(mod, #name, (long) (name)) == -1) return NULL;
Greg Ward1e0f57d2002-11-30 23:05:26 +00001055
Greg Ward744f0fd2002-12-31 03:23:59 +00001056
1057static char *control_labels[] = SOUND_DEVICE_LABELS;
1058static char *control_names[] = SOUND_DEVICE_NAMES;
1059
1060
1061static int
1062build_namelists (PyObject *module)
1063{
1064 PyObject *labels;
1065 PyObject *names;
1066 PyObject *s;
1067 int num_controls;
1068 int i;
1069
Victor Stinner63941882011-09-29 00:42:28 +02001070 num_controls = Py_ARRAY_LENGTH(control_labels);
1071 assert(num_controls == Py_ARRAY_LENGTH(control_names));
Greg Ward744f0fd2002-12-31 03:23:59 +00001072
1073 labels = PyList_New(num_controls);
1074 names = PyList_New(num_controls);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001075 if (labels == NULL || names == NULL)
1076 goto error2;
Greg Ward744f0fd2002-12-31 03:23:59 +00001077 for (i = 0; i < num_controls; i++) {
Neal Norwitz8abd2e62007-08-26 04:57:08 +00001078 s = PyUnicode_FromString(control_labels[i]);
Greg Ward744f0fd2002-12-31 03:23:59 +00001079 if (s == NULL)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001080 goto error2;
Greg Ward744f0fd2002-12-31 03:23:59 +00001081 PyList_SET_ITEM(labels, i, s);
Guido van Rossum0741f802003-06-02 14:15:34 +00001082
Neal Norwitz8abd2e62007-08-26 04:57:08 +00001083 s = PyUnicode_FromString(control_names[i]);
Greg Ward744f0fd2002-12-31 03:23:59 +00001084 if (s == NULL)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001085 goto error2;
Greg Ward744f0fd2002-12-31 03:23:59 +00001086 PyList_SET_ITEM(names, i, s);
1087 }
1088
1089 if (PyModule_AddObject(module, "control_labels", labels) == -1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001090 goto error2;
Greg Ward744f0fd2002-12-31 03:23:59 +00001091 if (PyModule_AddObject(module, "control_names", names) == -1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001092 goto error1;
Greg Ward744f0fd2002-12-31 03:23:59 +00001093
1094 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001095
1096error2:
1097 Py_XDECREF(labels);
1098error1:
1099 Py_XDECREF(names);
1100 return -1;
Guido van Rossum0741f802003-06-02 14:15:34 +00001101}
Greg Ward744f0fd2002-12-31 03:23:59 +00001102
1103
Martin v. Löwis1a214512008-06-11 05:26:20 +00001104static struct PyModuleDef ossaudiodevmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 PyModuleDef_HEAD_INIT,
1106 "ossaudiodev",
1107 NULL,
1108 -1,
1109 ossaudiodev_methods,
1110 NULL,
1111 NULL,
1112 NULL,
1113 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001114};
1115
Antoine Pitrou39b35432010-03-23 00:25:54 +00001116PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001117PyInit_ossaudiodev(void)
Greg Ward04613a92002-11-30 22:47:45 +00001118{
1119 PyObject *m;
Guido van Rossum0741f802003-06-02 14:15:34 +00001120
Antoine Pitrou39b35432010-03-23 00:25:54 +00001121 if (PyType_Ready(&OSSAudioType) < 0)
1122 return NULL;
1123
1124 if (PyType_Ready(&OSSMixerType) < 0)
1125 return NULL;
1126
Martin v. Löwis1a214512008-06-11 05:26:20 +00001127 m = PyModule_Create(&ossaudiodevmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001128 if (m == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 return NULL;
Greg Ward04613a92002-11-30 22:47:45 +00001130
Guido van Rossum0741f802003-06-02 14:15:34 +00001131 OSSAudioError = PyErr_NewException("ossaudiodev.OSSAudioError",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 NULL, NULL);
Greg Wardd6769062003-05-29 21:53:06 +00001133 if (OSSAudioError) {
Guido van Rossum0741f802003-06-02 14:15:34 +00001134 /* Each call to PyModule_AddObject decrefs it; compensate: */
1135 Py_INCREF(OSSAudioError);
1136 Py_INCREF(OSSAudioError);
Greg Wardad4d9b92002-12-30 03:02:22 +00001137 PyModule_AddObject(m, "error", OSSAudioError);
Greg Wardd6769062003-05-29 21:53:06 +00001138 PyModule_AddObject(m, "OSSAudioError", OSSAudioError);
1139 }
Greg Ward04613a92002-11-30 22:47:45 +00001140
Greg Ward744f0fd2002-12-31 03:23:59 +00001141 /* Build 'control_labels' and 'control_names' lists and add them
1142 to the module. */
1143 if (build_namelists(m) == -1) /* XXX what to do here? */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001144 return NULL;
Greg Ward744f0fd2002-12-31 03:23:59 +00001145
Greg Ward1e0f57d2002-11-30 23:05:26 +00001146 /* Expose the audio format numbers -- essential! */
1147 _EXPORT_INT(m, AFMT_QUERY);
1148 _EXPORT_INT(m, AFMT_MU_LAW);
1149 _EXPORT_INT(m, AFMT_A_LAW);
1150 _EXPORT_INT(m, AFMT_IMA_ADPCM);
1151 _EXPORT_INT(m, AFMT_U8);
1152 _EXPORT_INT(m, AFMT_S16_LE);
1153 _EXPORT_INT(m, AFMT_S16_BE);
1154 _EXPORT_INT(m, AFMT_S8);
1155 _EXPORT_INT(m, AFMT_U16_LE);
1156 _EXPORT_INT(m, AFMT_U16_BE);
1157 _EXPORT_INT(m, AFMT_MPEG);
Andrew M. Kuchlingfe62bc92003-02-13 13:27:07 +00001158#ifdef AFMT_AC3
Greg Ward1e0f57d2002-11-30 23:05:26 +00001159 _EXPORT_INT(m, AFMT_AC3);
Andrew M. Kuchlingfe62bc92003-02-13 13:27:07 +00001160#endif
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001161#ifdef AFMT_S16_NE
Greg Ward1e0f57d2002-11-30 23:05:26 +00001162 _EXPORT_INT(m, AFMT_S16_NE);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001163#endif
Greg Ward0f260542005-03-28 02:40:46 +00001164#ifdef AFMT_U16_NE
1165 _EXPORT_INT(m, AFMT_U16_NE);
1166#endif
1167#ifdef AFMT_S32_LE
1168 _EXPORT_INT(m, AFMT_S32_LE);
1169#endif
1170#ifdef AFMT_S32_BE
1171 _EXPORT_INT(m, AFMT_S32_BE);
1172#endif
1173#ifdef AFMT_MPEG
1174 _EXPORT_INT(m, AFMT_MPEG);
1175#endif
Guido van Rossum0741f802003-06-02 14:15:34 +00001176
Greg Ward37897c22002-12-30 02:43:36 +00001177 /* Expose the sound mixer device numbers. */
Greg Ward7b43c682002-12-30 02:29:28 +00001178 _EXPORT_INT(m, SOUND_MIXER_NRDEVICES);
Greg Ward3d9994d2002-12-11 15:12:01 +00001179 _EXPORT_INT(m, SOUND_MIXER_VOLUME);
1180 _EXPORT_INT(m, SOUND_MIXER_BASS);
1181 _EXPORT_INT(m, SOUND_MIXER_TREBLE);
1182 _EXPORT_INT(m, SOUND_MIXER_SYNTH);
1183 _EXPORT_INT(m, SOUND_MIXER_PCM);
1184 _EXPORT_INT(m, SOUND_MIXER_SPEAKER);
1185 _EXPORT_INT(m, SOUND_MIXER_LINE);
1186 _EXPORT_INT(m, SOUND_MIXER_MIC);
1187 _EXPORT_INT(m, SOUND_MIXER_CD);
1188 _EXPORT_INT(m, SOUND_MIXER_IMIX);
1189 _EXPORT_INT(m, SOUND_MIXER_ALTPCM);
1190 _EXPORT_INT(m, SOUND_MIXER_RECLEV);
1191 _EXPORT_INT(m, SOUND_MIXER_IGAIN);
1192 _EXPORT_INT(m, SOUND_MIXER_OGAIN);
1193 _EXPORT_INT(m, SOUND_MIXER_LINE1);
1194 _EXPORT_INT(m, SOUND_MIXER_LINE2);
1195 _EXPORT_INT(m, SOUND_MIXER_LINE3);
Greg Wardfd0283e2004-05-11 01:34:55 +00001196#ifdef SOUND_MIXER_DIGITAL1
Greg Ward3d9994d2002-12-11 15:12:01 +00001197 _EXPORT_INT(m, SOUND_MIXER_DIGITAL1);
Greg Wardfd0283e2004-05-11 01:34:55 +00001198#endif
1199#ifdef SOUND_MIXER_DIGITAL2
Greg Ward3d9994d2002-12-11 15:12:01 +00001200 _EXPORT_INT(m, SOUND_MIXER_DIGITAL2);
Greg Wardfd0283e2004-05-11 01:34:55 +00001201#endif
1202#ifdef SOUND_MIXER_DIGITAL3
Greg Ward3d9994d2002-12-11 15:12:01 +00001203 _EXPORT_INT(m, SOUND_MIXER_DIGITAL3);
Greg Wardfd0283e2004-05-11 01:34:55 +00001204#endif
1205#ifdef SOUND_MIXER_PHONEIN
Greg Ward3d9994d2002-12-11 15:12:01 +00001206 _EXPORT_INT(m, SOUND_MIXER_PHONEIN);
Greg Wardfd0283e2004-05-11 01:34:55 +00001207#endif
1208#ifdef SOUND_MIXER_PHONEOUT
Greg Ward3d9994d2002-12-11 15:12:01 +00001209 _EXPORT_INT(m, SOUND_MIXER_PHONEOUT);
Greg Wardfd0283e2004-05-11 01:34:55 +00001210#endif
1211#ifdef SOUND_MIXER_VIDEO
Greg Ward3d9994d2002-12-11 15:12:01 +00001212 _EXPORT_INT(m, SOUND_MIXER_VIDEO);
Greg Wardfd0283e2004-05-11 01:34:55 +00001213#endif
1214#ifdef SOUND_MIXER_RADIO
Greg Ward3d9994d2002-12-11 15:12:01 +00001215 _EXPORT_INT(m, SOUND_MIXER_RADIO);
Greg Wardfd0283e2004-05-11 01:34:55 +00001216#endif
1217#ifdef SOUND_MIXER_MONITOR
Greg Ward3d9994d2002-12-11 15:12:01 +00001218 _EXPORT_INT(m, SOUND_MIXER_MONITOR);
Greg Wardfd0283e2004-05-11 01:34:55 +00001219#endif
Greg Ward04613a92002-11-30 22:47:45 +00001220
Greg Ward1e0f57d2002-11-30 23:05:26 +00001221 /* Expose all the ioctl numbers for masochists who like to do this
1222 stuff directly. */
1223 _EXPORT_INT(m, SNDCTL_COPR_HALT);
1224 _EXPORT_INT(m, SNDCTL_COPR_LOAD);
1225 _EXPORT_INT(m, SNDCTL_COPR_RCODE);
1226 _EXPORT_INT(m, SNDCTL_COPR_RCVMSG);
1227 _EXPORT_INT(m, SNDCTL_COPR_RDATA);
1228 _EXPORT_INT(m, SNDCTL_COPR_RESET);
1229 _EXPORT_INT(m, SNDCTL_COPR_RUN);
1230 _EXPORT_INT(m, SNDCTL_COPR_SENDMSG);
1231 _EXPORT_INT(m, SNDCTL_COPR_WCODE);
1232 _EXPORT_INT(m, SNDCTL_COPR_WDATA);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001233#ifdef SNDCTL_DSP_BIND_CHANNEL
Greg Ward1e0f57d2002-11-30 23:05:26 +00001234 _EXPORT_INT(m, SNDCTL_DSP_BIND_CHANNEL);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001235#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001236 _EXPORT_INT(m, SNDCTL_DSP_CHANNELS);
1237 _EXPORT_INT(m, SNDCTL_DSP_GETBLKSIZE);
1238 _EXPORT_INT(m, SNDCTL_DSP_GETCAPS);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001239#ifdef SNDCTL_DSP_GETCHANNELMASK
Greg Ward1e0f57d2002-11-30 23:05:26 +00001240 _EXPORT_INT(m, SNDCTL_DSP_GETCHANNELMASK);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001241#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001242 _EXPORT_INT(m, SNDCTL_DSP_GETFMTS);
1243 _EXPORT_INT(m, SNDCTL_DSP_GETIPTR);
1244 _EXPORT_INT(m, SNDCTL_DSP_GETISPACE);
Greg Wardfd0283e2004-05-11 01:34:55 +00001245#ifdef SNDCTL_DSP_GETODELAY
Greg Ward1e0f57d2002-11-30 23:05:26 +00001246 _EXPORT_INT(m, SNDCTL_DSP_GETODELAY);
Greg Wardfd0283e2004-05-11 01:34:55 +00001247#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001248 _EXPORT_INT(m, SNDCTL_DSP_GETOPTR);
1249 _EXPORT_INT(m, SNDCTL_DSP_GETOSPACE);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001250#ifdef SNDCTL_DSP_GETSPDIF
Greg Ward1e0f57d2002-11-30 23:05:26 +00001251 _EXPORT_INT(m, SNDCTL_DSP_GETSPDIF);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001252#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001253 _EXPORT_INT(m, SNDCTL_DSP_GETTRIGGER);
1254 _EXPORT_INT(m, SNDCTL_DSP_MAPINBUF);
1255 _EXPORT_INT(m, SNDCTL_DSP_MAPOUTBUF);
1256 _EXPORT_INT(m, SNDCTL_DSP_NONBLOCK);
1257 _EXPORT_INT(m, SNDCTL_DSP_POST);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001258#ifdef SNDCTL_DSP_PROFILE
Greg Ward1e0f57d2002-11-30 23:05:26 +00001259 _EXPORT_INT(m, SNDCTL_DSP_PROFILE);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001260#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001261 _EXPORT_INT(m, SNDCTL_DSP_RESET);
1262 _EXPORT_INT(m, SNDCTL_DSP_SAMPLESIZE);
1263 _EXPORT_INT(m, SNDCTL_DSP_SETDUPLEX);
1264 _EXPORT_INT(m, SNDCTL_DSP_SETFMT);
1265 _EXPORT_INT(m, SNDCTL_DSP_SETFRAGMENT);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001266#ifdef SNDCTL_DSP_SETSPDIF
Greg Ward1e0f57d2002-11-30 23:05:26 +00001267 _EXPORT_INT(m, SNDCTL_DSP_SETSPDIF);
Neal Norwitzfaa7b9b2003-01-10 21:27:54 +00001268#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001269 _EXPORT_INT(m, SNDCTL_DSP_SETSYNCRO);
1270 _EXPORT_INT(m, SNDCTL_DSP_SETTRIGGER);
1271 _EXPORT_INT(m, SNDCTL_DSP_SPEED);
1272 _EXPORT_INT(m, SNDCTL_DSP_STEREO);
1273 _EXPORT_INT(m, SNDCTL_DSP_SUBDIVIDE);
1274 _EXPORT_INT(m, SNDCTL_DSP_SYNC);
1275 _EXPORT_INT(m, SNDCTL_FM_4OP_ENABLE);
1276 _EXPORT_INT(m, SNDCTL_FM_LOAD_INSTR);
1277 _EXPORT_INT(m, SNDCTL_MIDI_INFO);
1278 _EXPORT_INT(m, SNDCTL_MIDI_MPUCMD);
1279 _EXPORT_INT(m, SNDCTL_MIDI_MPUMODE);
1280 _EXPORT_INT(m, SNDCTL_MIDI_PRETIME);
1281 _EXPORT_INT(m, SNDCTL_SEQ_CTRLRATE);
1282 _EXPORT_INT(m, SNDCTL_SEQ_GETINCOUNT);
1283 _EXPORT_INT(m, SNDCTL_SEQ_GETOUTCOUNT);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001284#ifdef SNDCTL_SEQ_GETTIME
Greg Ward1e0f57d2002-11-30 23:05:26 +00001285 _EXPORT_INT(m, SNDCTL_SEQ_GETTIME);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001286#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001287 _EXPORT_INT(m, SNDCTL_SEQ_NRMIDIS);
1288 _EXPORT_INT(m, SNDCTL_SEQ_NRSYNTHS);
1289 _EXPORT_INT(m, SNDCTL_SEQ_OUTOFBAND);
1290 _EXPORT_INT(m, SNDCTL_SEQ_PANIC);
1291 _EXPORT_INT(m, SNDCTL_SEQ_PERCMODE);
1292 _EXPORT_INT(m, SNDCTL_SEQ_RESET);
1293 _EXPORT_INT(m, SNDCTL_SEQ_RESETSAMPLES);
1294 _EXPORT_INT(m, SNDCTL_SEQ_SYNC);
1295 _EXPORT_INT(m, SNDCTL_SEQ_TESTMIDI);
1296 _EXPORT_INT(m, SNDCTL_SEQ_THRESHOLD);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001297#ifdef SNDCTL_SYNTH_CONTROL
Greg Ward1e0f57d2002-11-30 23:05:26 +00001298 _EXPORT_INT(m, SNDCTL_SYNTH_CONTROL);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001299#endif
1300#ifdef SNDCTL_SYNTH_ID
Greg Ward1e0f57d2002-11-30 23:05:26 +00001301 _EXPORT_INT(m, SNDCTL_SYNTH_ID);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001302#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001303 _EXPORT_INT(m, SNDCTL_SYNTH_INFO);
1304 _EXPORT_INT(m, SNDCTL_SYNTH_MEMAVL);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001305#ifdef SNDCTL_SYNTH_REMOVESAMPLE
Greg Ward1e0f57d2002-11-30 23:05:26 +00001306 _EXPORT_INT(m, SNDCTL_SYNTH_REMOVESAMPLE);
Neal Norwitzd156c2d2003-02-02 17:59:06 +00001307#endif
Greg Ward1e0f57d2002-11-30 23:05:26 +00001308 _EXPORT_INT(m, SNDCTL_TMR_CONTINUE);
1309 _EXPORT_INT(m, SNDCTL_TMR_METRONOME);
1310 _EXPORT_INT(m, SNDCTL_TMR_SELECT);
1311 _EXPORT_INT(m, SNDCTL_TMR_SOURCE);
1312 _EXPORT_INT(m, SNDCTL_TMR_START);
1313 _EXPORT_INT(m, SNDCTL_TMR_STOP);
1314 _EXPORT_INT(m, SNDCTL_TMR_TEMPO);
1315 _EXPORT_INT(m, SNDCTL_TMR_TIMEBASE);
Martin v. Löwis1a214512008-06-11 05:26:20 +00001316 return m;
Greg Ward04613a92002-11-30 22:47:45 +00001317}