Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 1 | /* |
| 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. |
| 11 | * |
| 12 | * (c) 2000 Peter Bosch. All Rights Reserved. |
| 13 | * (c) 2002 Gregory P. Ward. All Rights Reserved. |
| 14 | * (c) 2002 Python Software Foundation. All Rights Reserved. |
| 15 | * |
| 16 | * XXX need a license statement |
| 17 | * |
| 18 | * $Id$ |
| 19 | */ |
| 20 | |
| 21 | #include "Python.h" |
| 22 | #include "structmember.h" |
| 23 | |
| 24 | #ifdef HAVE_FCNTL_H |
| 25 | #include <fcntl.h> |
| 26 | #else |
| 27 | #define O_RDONLY 00 |
| 28 | #define O_WRONLY 01 |
| 29 | #endif |
| 30 | |
| 31 | |
| 32 | #include <sys/ioctl.h> |
| 33 | #if defined(linux) |
| 34 | #include <linux/soundcard.h> |
| 35 | |
| 36 | typedef unsigned long uint32_t; |
| 37 | |
| 38 | #elif defined(__FreeBSD__) |
| 39 | #include <machine/soundcard.h> |
| 40 | |
| 41 | #ifndef SNDCTL_DSP_CHANNELS |
| 42 | #define SNDCTL_DSP_CHANNELS SOUND_PCM_WRITE_CHANNELS |
| 43 | #endif |
| 44 | |
| 45 | #endif |
| 46 | |
| 47 | typedef struct { |
| 48 | PyObject_HEAD; |
| 49 | int x_fd; /* The open file */ |
| 50 | int x_mode; /* file mode */ |
| 51 | int x_icount; /* Input count */ |
| 52 | int x_ocount; /* Output count */ |
| 53 | uint32_t x_afmts; /* Audio formats supported by hardware*/ |
| 54 | } lad_t; |
| 55 | |
| 56 | /* XXX several format defined in soundcard.h are not supported, |
| 57 | including _NE (native endian) options and S32 options |
| 58 | */ |
| 59 | |
| 60 | static struct { |
| 61 | int a_bps; |
| 62 | uint32_t a_fmt; |
| 63 | char *a_name; |
| 64 | } audio_types[] = { |
| 65 | { 8, AFMT_MU_LAW, "logarithmic mu-law 8-bit audio" }, |
| 66 | { 8, AFMT_A_LAW, "logarithmic A-law 8-bit audio" }, |
| 67 | { 8, AFMT_U8, "linear unsigned 8-bit audio" }, |
| 68 | { 8, AFMT_S8, "linear signed 8-bit audio" }, |
| 69 | { 16, AFMT_U16_BE, "linear unsigned 16-bit big-endian audio" }, |
| 70 | { 16, AFMT_U16_LE, "linear unsigned 16-bit little-endian audio" }, |
| 71 | { 16, AFMT_S16_BE, "linear signed 16-bit big-endian audio" }, |
| 72 | { 16, AFMT_S16_LE, "linear signed 16-bit little-endian audio" }, |
| 73 | { 16, AFMT_S16_NE, "linear signed 16-bit native-endian audio" }, |
| 74 | }; |
| 75 | |
| 76 | static int n_audio_types = sizeof(audio_types) / sizeof(audio_types[0]); |
| 77 | |
| 78 | static PyTypeObject Ladtype; |
| 79 | |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 80 | static PyObject *OSSAudioError; |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 81 | |
| 82 | static lad_t * |
| 83 | newladobject(PyObject *arg) |
| 84 | { |
| 85 | lad_t *xp; |
| 86 | int fd, afmts, imode; |
| 87 | char *basedev = NULL; |
| 88 | char *mode = NULL; |
| 89 | |
Greg Ward | 9a568eb | 2002-11-30 23:20:09 +0000 | [diff] [blame] | 90 | /* Two ways to call open(): |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 91 | open(device, mode) (for consistency with builtin open()) |
| 92 | open(mode) (for backwards compatibility) |
| 93 | because the *first* argument is optional, parsing args is |
| 94 | a wee bit tricky. */ |
| 95 | if (!PyArg_ParseTuple(arg, "s|s:open", &basedev, &mode)) |
| 96 | return NULL; |
| 97 | if (mode == NULL) { /* only one arg supplied */ |
| 98 | mode = basedev; |
| 99 | basedev = NULL; |
| 100 | } |
| 101 | |
| 102 | if (strcmp(mode, "r") == 0) |
| 103 | imode = O_RDONLY; |
| 104 | else if (strcmp(mode, "w") == 0) |
| 105 | imode = O_WRONLY; |
Greg Ward | 1e0f57d | 2002-11-30 23:05:26 +0000 | [diff] [blame] | 106 | else if (strcmp(mode, "rw") == 0) |
| 107 | imode = O_RDWR; |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 108 | else { |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 109 | PyErr_SetString(OSSAudioError, "mode must be 'r', 'w', or 'rw'"); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 110 | return NULL; |
| 111 | } |
| 112 | |
| 113 | /* Open the correct device. The base device name comes from the |
| 114 | * AUDIODEV environment variable first, then /dev/dsp. The |
| 115 | * control device tacks "ctl" onto the base device name. |
| 116 | * |
| 117 | * Note that the only difference between /dev/audio and /dev/dsp |
| 118 | * is that the former uses logarithmic mu-law encoding and the |
| 119 | * latter uses 8-bit unsigned encoding. |
| 120 | */ |
| 121 | |
| 122 | if (basedev == NULL) { /* called with one arg */ |
| 123 | basedev = getenv("AUDIODEV"); |
| 124 | if (basedev == NULL) /* $AUDIODEV not set */ |
| 125 | basedev = "/dev/dsp"; |
| 126 | } |
| 127 | |
| 128 | if ((fd = open(basedev, imode)) == -1) { |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 129 | PyErr_SetFromErrnoWithFilename(PyExc_IOError, basedev); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 130 | return NULL; |
| 131 | } |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 132 | if (ioctl(fd, SNDCTL_DSP_GETFMTS, &afmts) == -1) { |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 133 | PyErr_SetFromErrnoWithFilename(PyExc_IOError, basedev); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 134 | return NULL; |
| 135 | } |
| 136 | /* Create and initialize the object */ |
| 137 | if ((xp = PyObject_New(lad_t, &Ladtype)) == NULL) { |
| 138 | close(fd); |
| 139 | return NULL; |
| 140 | } |
| 141 | xp->x_fd = fd; |
| 142 | xp->x_mode = imode; |
| 143 | xp->x_icount = xp->x_ocount = 0; |
| 144 | xp->x_afmts = afmts; |
| 145 | return xp; |
| 146 | } |
| 147 | |
| 148 | static void |
| 149 | lad_dealloc(lad_t *xp) |
| 150 | { |
| 151 | /* if already closed, don't reclose it */ |
| 152 | if (xp->x_fd != -1) |
| 153 | close(xp->x_fd); |
| 154 | PyObject_Del(xp); |
| 155 | } |
| 156 | |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 157 | |
| 158 | /* Methods to wrap the OSS ioctls. The calling convention is pretty |
| 159 | simple: |
| 160 | nonblock() -> ioctl(fd, SNDCTL_DSP_NONBLOCK) |
| 161 | fmt = setfmt(fmt) -> ioctl(fd, SNDCTL_DSP_SETFMT, &fmt) |
| 162 | etc. |
| 163 | */ |
| 164 | |
| 165 | |
| 166 | /* _do_ioctl_1() is a private helper function used for the OSS ioctls -- |
| 167 | SNDCTL_DSP_{SETFMT,CHANNELS,SPEED} -- that that are called from C |
| 168 | like this: |
| 169 | ioctl(fd, SNDCTL_DSP_cmd, &arg) |
| 170 | |
| 171 | where arg is the value to set, and on return the driver sets arg to |
| 172 | the value that was actually set. Mapping this to Python is obvious: |
| 173 | arg = dsp.xxx(arg) |
| 174 | */ |
| 175 | static PyObject * |
| 176 | _do_ioctl_1(lad_t *self, PyObject *args, char *fname, int cmd) |
| 177 | { |
| 178 | char argfmt[13] = "i:"; |
| 179 | int arg; |
| 180 | |
| 181 | assert(strlen(fname) <= 10); |
| 182 | strcat(argfmt, fname); |
| 183 | if (!PyArg_ParseTuple(args, argfmt, &arg)) |
| 184 | return NULL; |
| 185 | |
| 186 | if (ioctl(self->x_fd, cmd, &arg) == -1) |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 187 | return PyErr_SetFromErrno(PyExc_IOError); |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 188 | return PyInt_FromLong(arg); |
| 189 | } |
| 190 | |
| 191 | /* _do_ioctl_0() is a private helper for the no-argument ioctls: |
| 192 | SNDCTL_DSP_{SYNC,RESET,POST}. */ |
| 193 | static PyObject * |
| 194 | _do_ioctl_0(lad_t *self, PyObject *args, char *fname, int cmd) |
| 195 | { |
| 196 | char argfmt[12] = ":"; |
| 197 | |
| 198 | assert(strlen(fname) <= 10); |
| 199 | strcat(argfmt, fname); |
| 200 | if (!PyArg_ParseTuple(args, argfmt)) |
| 201 | return NULL; |
| 202 | |
| 203 | if (ioctl(self->x_fd, cmd, 0) == -1) |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 204 | return PyErr_SetFromErrno(PyExc_IOError); |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 205 | Py_INCREF(Py_None); |
| 206 | return Py_None; |
| 207 | } |
| 208 | |
| 209 | |
| 210 | static PyObject * |
| 211 | lad_nonblock(lad_t *self, PyObject *args) |
| 212 | { |
| 213 | /* Hmmm: it doesn't appear to be possible to return to blocking |
| 214 | mode once we're in non-blocking mode! */ |
| 215 | if (!PyArg_ParseTuple(args, ":nonblock")) |
| 216 | return NULL; |
| 217 | if (ioctl(self->x_fd, SNDCTL_DSP_NONBLOCK, NULL) == -1) |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 218 | return PyErr_SetFromErrno(PyExc_IOError); |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 219 | Py_INCREF(Py_None); |
| 220 | return Py_None; |
| 221 | } |
| 222 | |
| 223 | static PyObject * |
| 224 | lad_setfmt(lad_t *self, PyObject *args) |
| 225 | { |
| 226 | return _do_ioctl_1(self, args, "setfmt", SNDCTL_DSP_SETFMT); |
| 227 | } |
| 228 | |
| 229 | static PyObject * |
| 230 | lad_getfmts(lad_t *self, PyObject *args) |
| 231 | { |
| 232 | int mask; |
| 233 | if (!PyArg_ParseTuple(args, ":getfmts")) |
| 234 | return NULL; |
| 235 | if (ioctl(self->x_fd, SNDCTL_DSP_GETFMTS, &mask) == -1) |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 236 | return PyErr_SetFromErrno(PyExc_IOError); |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 237 | return PyInt_FromLong(mask); |
| 238 | } |
| 239 | |
| 240 | static PyObject * |
| 241 | lad_channels(lad_t *self, PyObject *args) |
| 242 | { |
| 243 | return _do_ioctl_1(self, args, "channels", SNDCTL_DSP_CHANNELS); |
| 244 | } |
| 245 | |
| 246 | static PyObject * |
| 247 | lad_speed(lad_t *self, PyObject *args) |
| 248 | { |
| 249 | return _do_ioctl_1(self, args, "speed", SNDCTL_DSP_SPEED); |
| 250 | } |
| 251 | |
| 252 | static PyObject * |
| 253 | lad_sync(lad_t *self, PyObject *args) |
| 254 | { |
| 255 | return _do_ioctl_0(self, args, "sync", SNDCTL_DSP_SYNC); |
| 256 | } |
| 257 | |
| 258 | static PyObject * |
| 259 | lad_reset(lad_t *self, PyObject *args) |
| 260 | { |
| 261 | return _do_ioctl_0(self, args, "reset", SNDCTL_DSP_RESET); |
| 262 | } |
| 263 | |
| 264 | static PyObject * |
| 265 | lad_post(lad_t *self, PyObject *args) |
| 266 | { |
| 267 | return _do_ioctl_0(self, args, "post", SNDCTL_DSP_POST); |
| 268 | } |
| 269 | |
| 270 | |
| 271 | /* Regular file methods: read(), write(), close(), etc. as well |
| 272 | as one convenience method, writeall(). */ |
| 273 | |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 274 | static PyObject * |
| 275 | lad_read(lad_t *self, PyObject *args) |
| 276 | { |
| 277 | int size, count; |
| 278 | char *cp; |
| 279 | PyObject *rv; |
| 280 | |
| 281 | if (!PyArg_ParseTuple(args, "i:read", &size)) |
| 282 | return NULL; |
| 283 | rv = PyString_FromStringAndSize(NULL, size); |
| 284 | if (rv == NULL) |
| 285 | return NULL; |
| 286 | cp = PyString_AS_STRING(rv); |
| 287 | if ((count = read(self->x_fd, cp, size)) < 0) { |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 288 | PyErr_SetFromErrno(PyExc_IOError); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 289 | Py_DECREF(rv); |
| 290 | return NULL; |
| 291 | } |
| 292 | self->x_icount += count; |
| 293 | _PyString_Resize(&rv, count); |
| 294 | return rv; |
| 295 | } |
| 296 | |
| 297 | static PyObject * |
| 298 | lad_write(lad_t *self, PyObject *args) |
| 299 | { |
| 300 | char *cp; |
| 301 | int rv, size; |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 302 | |
| 303 | if (!PyArg_ParseTuple(args, "s#:write", &cp, &size)) { |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 304 | return NULL; |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 305 | } |
| 306 | if ((rv = write(self->x_fd, cp, size)) == -1) { |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 307 | return PyErr_SetFromErrno(PyExc_IOError); |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 308 | } else { |
| 309 | self->x_ocount += rv; |
| 310 | } |
| 311 | return PyInt_FromLong(rv); |
| 312 | } |
| 313 | |
| 314 | static PyObject * |
| 315 | lad_writeall(lad_t *self, PyObject *args) |
| 316 | { |
| 317 | char *cp; |
| 318 | int rv, size; |
| 319 | fd_set write_set_fds; |
| 320 | int select_rv; |
| 321 | |
| 322 | /* NB. writeall() is only useful in non-blocking mode: according to |
| 323 | Guenter Geiger <geiger@xdv.org> on the linux-audio-dev list |
| 324 | (http://eca.cx/lad/2002/11/0380.html), OSS guarantees that |
| 325 | write() in blocking mode consumes the whole buffer. In blocking |
| 326 | mode, the behaviour of write() and writeall() from Python is |
| 327 | indistinguishable. */ |
| 328 | |
| 329 | if (!PyArg_ParseTuple(args, "s#:write", &cp, &size)) |
| 330 | return NULL; |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 331 | |
| 332 | /* use select to wait for audio device to be available */ |
| 333 | FD_ZERO(&write_set_fds); |
| 334 | FD_SET(self->x_fd, &write_set_fds); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 335 | |
| 336 | while (size > 0) { |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 337 | select_rv = select(self->x_fd+1, NULL, &write_set_fds, NULL, NULL); |
| 338 | assert(select_rv != 0); /* no timeout, can't expire */ |
| 339 | if (select_rv == -1) |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 340 | return PyErr_SetFromErrno(PyExc_IOError); |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 341 | |
| 342 | rv = write(self->x_fd, cp, size); |
| 343 | if (rv == -1) { |
| 344 | if (errno == EAGAIN) { /* buffer is full, try again */ |
| 345 | errno = 0; |
| 346 | continue; |
| 347 | } else /* it's a real error */ |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 348 | return PyErr_SetFromErrno(PyExc_IOError); |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 349 | } else { /* wrote rv bytes */ |
| 350 | self->x_ocount += rv; |
| 351 | size -= rv; |
| 352 | cp += rv; |
| 353 | } |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 354 | } |
| 355 | Py_INCREF(Py_None); |
| 356 | return Py_None; |
| 357 | } |
| 358 | |
| 359 | static PyObject * |
| 360 | lad_close(lad_t *self, PyObject *args) |
| 361 | { |
| 362 | if (!PyArg_ParseTuple(args, ":close")) |
| 363 | return NULL; |
| 364 | |
| 365 | if (self->x_fd >= 0) { |
| 366 | close(self->x_fd); |
| 367 | self->x_fd = -1; |
| 368 | } |
| 369 | Py_INCREF(Py_None); |
| 370 | return Py_None; |
| 371 | } |
| 372 | |
| 373 | static PyObject * |
| 374 | lad_fileno(lad_t *self, PyObject *args) |
| 375 | { |
| 376 | if (!PyArg_ParseTuple(args, ":fileno")) |
| 377 | return NULL; |
| 378 | return PyInt_FromLong(self->x_fd); |
| 379 | } |
| 380 | |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 381 | |
| 382 | /* Convenience methods: these generally wrap a couple of ioctls into one |
| 383 | common task. */ |
| 384 | |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 385 | static PyObject * |
| 386 | lad_setparameters(lad_t *self, PyObject *args) |
| 387 | { |
| 388 | int rate, ssize, nchannels, n, fmt, emulate=0; |
| 389 | |
| 390 | if (!PyArg_ParseTuple(args, "iiii|i:setparameters", |
| 391 | &rate, &ssize, &nchannels, &fmt, &emulate)) |
| 392 | return NULL; |
| 393 | |
| 394 | if (rate < 0) { |
| 395 | PyErr_Format(PyExc_ValueError, "expected rate >= 0, not %d", |
| 396 | rate); |
| 397 | return NULL; |
| 398 | } |
| 399 | if (ssize < 0) { |
| 400 | PyErr_Format(PyExc_ValueError, "expected sample size >= 0, not %d", |
| 401 | ssize); |
| 402 | return NULL; |
| 403 | } |
| 404 | if (nchannels != 1 && nchannels != 2) { |
| 405 | PyErr_Format(PyExc_ValueError, "nchannels must be 1 or 2, not %d", |
| 406 | nchannels); |
| 407 | return NULL; |
| 408 | } |
| 409 | |
| 410 | for (n = 0; n < n_audio_types; n++) |
| 411 | if (fmt == audio_types[n].a_fmt) |
| 412 | break; |
| 413 | if (n == n_audio_types) { |
| 414 | PyErr_Format(PyExc_ValueError, "unknown audio encoding: %d", fmt); |
| 415 | return NULL; |
| 416 | } |
| 417 | if (audio_types[n].a_bps != ssize) { |
| 418 | PyErr_Format(PyExc_ValueError, |
| 419 | "for %s, expected sample size %d, not %d", |
| 420 | audio_types[n].a_name, audio_types[n].a_bps, ssize); |
| 421 | return NULL; |
| 422 | } |
| 423 | |
| 424 | if (emulate == 0) { |
| 425 | if ((self->x_afmts & audio_types[n].a_fmt) == 0) { |
| 426 | PyErr_Format(PyExc_ValueError, |
| 427 | "%s format not supported by device", |
| 428 | audio_types[n].a_name); |
| 429 | return NULL; |
| 430 | } |
| 431 | } |
| 432 | if (ioctl(self->x_fd, SNDCTL_DSP_SETFMT, |
| 433 | &audio_types[n].a_fmt) == -1) { |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 434 | PyErr_SetFromErrno(PyExc_IOError); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 435 | return NULL; |
| 436 | } |
| 437 | if (ioctl(self->x_fd, SNDCTL_DSP_CHANNELS, &nchannels) == -1) { |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 438 | PyErr_SetFromErrno(PyExc_IOError); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 439 | return NULL; |
| 440 | } |
| 441 | if (ioctl(self->x_fd, SNDCTL_DSP_SPEED, &rate) == -1) { |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 442 | PyErr_SetFromErrno(PyExc_IOError); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 443 | return NULL; |
| 444 | } |
| 445 | |
| 446 | Py_INCREF(Py_None); |
| 447 | return Py_None; |
| 448 | } |
| 449 | |
| 450 | static int |
| 451 | _ssize(lad_t *self, int *nchannels, int *ssize) |
| 452 | { |
| 453 | int fmt; |
| 454 | |
| 455 | fmt = 0; |
| 456 | if (ioctl(self->x_fd, SNDCTL_DSP_SETFMT, &fmt) < 0) |
| 457 | return -errno; |
| 458 | |
| 459 | switch (fmt) { |
| 460 | case AFMT_MU_LAW: |
| 461 | case AFMT_A_LAW: |
| 462 | case AFMT_U8: |
| 463 | case AFMT_S8: |
| 464 | *ssize = sizeof(char); |
| 465 | break; |
| 466 | case AFMT_S16_LE: |
| 467 | case AFMT_S16_BE: |
| 468 | case AFMT_U16_LE: |
| 469 | case AFMT_U16_BE: |
| 470 | *ssize = sizeof(short); |
| 471 | break; |
| 472 | case AFMT_MPEG: |
| 473 | case AFMT_IMA_ADPCM: |
| 474 | default: |
| 475 | return -EOPNOTSUPP; |
| 476 | } |
| 477 | *nchannels = 0; |
| 478 | if (ioctl(self->x_fd, SNDCTL_DSP_CHANNELS, nchannels) < 0) |
| 479 | return -errno; |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | |
| 484 | /* bufsize returns the size of the hardware audio buffer in number |
| 485 | of samples */ |
| 486 | static PyObject * |
| 487 | lad_bufsize(lad_t *self, PyObject *args) |
| 488 | { |
| 489 | audio_buf_info ai; |
| 490 | int nchannels, ssize; |
| 491 | |
| 492 | if (!PyArg_ParseTuple(args, ":bufsize")) return NULL; |
| 493 | |
| 494 | if (_ssize(self, &nchannels, &ssize) < 0) { |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 495 | PyErr_SetFromErrno(PyExc_IOError); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 496 | return NULL; |
| 497 | } |
| 498 | if (ioctl(self->x_fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) { |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 499 | PyErr_SetFromErrno(PyExc_IOError); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 500 | return NULL; |
| 501 | } |
| 502 | return PyInt_FromLong((ai.fragstotal * ai.fragsize) / (nchannels * ssize)); |
| 503 | } |
| 504 | |
| 505 | /* obufcount returns the number of samples that are available in the |
| 506 | hardware for playing */ |
| 507 | static PyObject * |
| 508 | lad_obufcount(lad_t *self, PyObject *args) |
| 509 | { |
| 510 | audio_buf_info ai; |
| 511 | int nchannels, ssize; |
| 512 | |
| 513 | if (!PyArg_ParseTuple(args, ":obufcount")) |
| 514 | return NULL; |
| 515 | |
| 516 | if (_ssize(self, &nchannels, &ssize) < 0) { |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 517 | PyErr_SetFromErrno(PyExc_IOError); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 518 | return NULL; |
| 519 | } |
| 520 | if (ioctl(self->x_fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) { |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 521 | PyErr_SetFromErrno(PyExc_IOError); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 522 | return NULL; |
| 523 | } |
| 524 | return PyInt_FromLong((ai.fragstotal * ai.fragsize - ai.bytes) / |
| 525 | (ssize * nchannels)); |
| 526 | } |
| 527 | |
| 528 | /* obufcount returns the number of samples that can be played without |
| 529 | blocking */ |
| 530 | static PyObject * |
| 531 | lad_obuffree(lad_t *self, PyObject *args) |
| 532 | { |
| 533 | audio_buf_info ai; |
| 534 | int nchannels, ssize; |
| 535 | |
| 536 | if (!PyArg_ParseTuple(args, ":obuffree")) |
| 537 | return NULL; |
| 538 | |
| 539 | if (_ssize(self, &nchannels, &ssize) < 0) { |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 540 | PyErr_SetFromErrno(PyExc_IOError); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 541 | return NULL; |
| 542 | } |
| 543 | if (ioctl(self->x_fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) { |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 544 | PyErr_SetFromErrno(PyExc_IOError); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 545 | return NULL; |
| 546 | } |
| 547 | return PyInt_FromLong(ai.bytes / (ssize * nchannels)); |
| 548 | } |
| 549 | |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 550 | static PyObject * |
| 551 | lad_getptr(lad_t *self, PyObject *args) |
| 552 | { |
| 553 | count_info info; |
| 554 | int req; |
| 555 | |
| 556 | if (!PyArg_ParseTuple(args, ":getptr")) |
| 557 | return NULL; |
| 558 | |
| 559 | if (self->x_mode == O_RDONLY) |
| 560 | req = SNDCTL_DSP_GETIPTR; |
| 561 | else |
| 562 | req = SNDCTL_DSP_GETOPTR; |
| 563 | if (ioctl(self->x_fd, req, &info) == -1) { |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 564 | PyErr_SetFromErrno(PyExc_IOError); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 565 | return NULL; |
| 566 | } |
| 567 | return Py_BuildValue("iii", info.bytes, info.blocks, info.ptr); |
| 568 | } |
| 569 | |
| 570 | static PyMethodDef lad_methods[] = { |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 571 | /* Regular file methods */ |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 572 | { "read", (PyCFunction)lad_read, METH_VARARGS }, |
| 573 | { "write", (PyCFunction)lad_write, METH_VARARGS }, |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 574 | { "writeall", (PyCFunction)lad_writeall, METH_VARARGS }, |
| 575 | { "close", (PyCFunction)lad_close, METH_VARARGS }, |
| 576 | { "fileno", (PyCFunction)lad_fileno, METH_VARARGS }, |
| 577 | |
| 578 | /* Simple ioctl wrappers */ |
| 579 | { "nonblock", (PyCFunction)lad_nonblock, METH_VARARGS }, |
| 580 | { "setfmt", (PyCFunction)lad_setfmt, METH_VARARGS }, |
| 581 | { "getfmts", (PyCFunction)lad_getfmts, METH_VARARGS }, |
| 582 | { "channels", (PyCFunction)lad_channels, METH_VARARGS }, |
| 583 | { "speed", (PyCFunction)lad_speed, METH_VARARGS }, |
| 584 | { "sync", (PyCFunction)lad_sync, METH_VARARGS }, |
| 585 | { "reset", (PyCFunction)lad_reset, METH_VARARGS }, |
| 586 | { "post", (PyCFunction)lad_post, METH_VARARGS }, |
| 587 | |
| 588 | /* Convenience methods -- wrap a couple of ioctls together */ |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 589 | { "setparameters", (PyCFunction)lad_setparameters, METH_VARARGS }, |
| 590 | { "bufsize", (PyCFunction)lad_bufsize, METH_VARARGS }, |
| 591 | { "obufcount", (PyCFunction)lad_obufcount, METH_VARARGS }, |
| 592 | { "obuffree", (PyCFunction)lad_obuffree, METH_VARARGS }, |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 593 | { "getptr", (PyCFunction)lad_getptr, METH_VARARGS }, |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 594 | |
| 595 | /* Aliases for backwards compatibility */ |
| 596 | { "flush", (PyCFunction)lad_sync, METH_VARARGS }, |
| 597 | |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 598 | { NULL, NULL} /* sentinel */ |
| 599 | }; |
| 600 | |
| 601 | static PyObject * |
| 602 | lad_getattr(lad_t *xp, char *name) |
| 603 | { |
| 604 | return Py_FindMethod(lad_methods, (PyObject *)xp, name); |
| 605 | } |
| 606 | |
| 607 | static PyTypeObject Ladtype = { |
| 608 | PyObject_HEAD_INIT(&PyType_Type) |
| 609 | 0, /*ob_size*/ |
Greg Ward | 9a568eb | 2002-11-30 23:20:09 +0000 | [diff] [blame] | 610 | "ossaudiodev.oss_audio_device", /*tp_name*/ |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 611 | sizeof(lad_t), /*tp_size*/ |
| 612 | 0, /*tp_itemsize*/ |
| 613 | /* methods */ |
| 614 | (destructor)lad_dealloc, /*tp_dealloc*/ |
| 615 | 0, /*tp_print*/ |
| 616 | (getattrfunc)lad_getattr, /*tp_getattr*/ |
| 617 | 0, /*tp_setattr*/ |
| 618 | 0, /*tp_compare*/ |
| 619 | 0, /*tp_repr*/ |
| 620 | }; |
| 621 | |
| 622 | static PyObject * |
Greg Ward | 9a568eb | 2002-11-30 23:20:09 +0000 | [diff] [blame] | 623 | ossopen(PyObject *self, PyObject *args) |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 624 | { |
| 625 | return (PyObject *)newladobject(args); |
| 626 | } |
| 627 | |
Greg Ward | 9a568eb | 2002-11-30 23:20:09 +0000 | [diff] [blame] | 628 | static PyMethodDef ossaudiodev_methods[] = { |
| 629 | { "open", ossopen, METH_VARARGS }, |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 630 | { 0, 0 }, |
| 631 | }; |
| 632 | |
Greg Ward | 1e0f57d | 2002-11-30 23:05:26 +0000 | [diff] [blame] | 633 | |
| 634 | #define _EXPORT_INT(mod, name) \ |
| 635 | if (PyModule_AddIntConstant(mod, #name, (long) (name)) == -1) return; |
| 636 | |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 637 | void |
Greg Ward | 9a568eb | 2002-11-30 23:20:09 +0000 | [diff] [blame] | 638 | initossaudiodev(void) |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 639 | { |
| 640 | PyObject *m; |
| 641 | |
Greg Ward | 9a568eb | 2002-11-30 23:20:09 +0000 | [diff] [blame] | 642 | m = Py_InitModule("ossaudiodev", ossaudiodev_methods); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 643 | |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 644 | OSSAudioError = PyErr_NewException("ossaudiodev.error", NULL, NULL); |
| 645 | if (OSSAudioError) |
| 646 | PyModule_AddObject(m, "error", OSSAudioError); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 647 | |
Greg Ward | 1e0f57d | 2002-11-30 23:05:26 +0000 | [diff] [blame] | 648 | /* Expose the audio format numbers -- essential! */ |
| 649 | _EXPORT_INT(m, AFMT_QUERY); |
| 650 | _EXPORT_INT(m, AFMT_MU_LAW); |
| 651 | _EXPORT_INT(m, AFMT_A_LAW); |
| 652 | _EXPORT_INT(m, AFMT_IMA_ADPCM); |
| 653 | _EXPORT_INT(m, AFMT_U8); |
| 654 | _EXPORT_INT(m, AFMT_S16_LE); |
| 655 | _EXPORT_INT(m, AFMT_S16_BE); |
| 656 | _EXPORT_INT(m, AFMT_S8); |
| 657 | _EXPORT_INT(m, AFMT_U16_LE); |
| 658 | _EXPORT_INT(m, AFMT_U16_BE); |
| 659 | _EXPORT_INT(m, AFMT_MPEG); |
| 660 | _EXPORT_INT(m, AFMT_AC3); |
| 661 | _EXPORT_INT(m, AFMT_S16_NE); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 662 | |
Greg Ward | 1e0f57d | 2002-11-30 23:05:26 +0000 | [diff] [blame] | 663 | /* Expose all the ioctl numbers for masochists who like to do this |
| 664 | stuff directly. */ |
| 665 | _EXPORT_INT(m, SNDCTL_COPR_HALT); |
| 666 | _EXPORT_INT(m, SNDCTL_COPR_LOAD); |
| 667 | _EXPORT_INT(m, SNDCTL_COPR_RCODE); |
| 668 | _EXPORT_INT(m, SNDCTL_COPR_RCVMSG); |
| 669 | _EXPORT_INT(m, SNDCTL_COPR_RDATA); |
| 670 | _EXPORT_INT(m, SNDCTL_COPR_RESET); |
| 671 | _EXPORT_INT(m, SNDCTL_COPR_RUN); |
| 672 | _EXPORT_INT(m, SNDCTL_COPR_SENDMSG); |
| 673 | _EXPORT_INT(m, SNDCTL_COPR_WCODE); |
| 674 | _EXPORT_INT(m, SNDCTL_COPR_WDATA); |
| 675 | _EXPORT_INT(m, SNDCTL_DSP_BIND_CHANNEL); |
| 676 | _EXPORT_INT(m, SNDCTL_DSP_CHANNELS); |
| 677 | _EXPORT_INT(m, SNDCTL_DSP_GETBLKSIZE); |
| 678 | _EXPORT_INT(m, SNDCTL_DSP_GETCAPS); |
| 679 | _EXPORT_INT(m, SNDCTL_DSP_GETCHANNELMASK); |
| 680 | _EXPORT_INT(m, SNDCTL_DSP_GETFMTS); |
| 681 | _EXPORT_INT(m, SNDCTL_DSP_GETIPTR); |
| 682 | _EXPORT_INT(m, SNDCTL_DSP_GETISPACE); |
| 683 | _EXPORT_INT(m, SNDCTL_DSP_GETODELAY); |
| 684 | _EXPORT_INT(m, SNDCTL_DSP_GETOPTR); |
| 685 | _EXPORT_INT(m, SNDCTL_DSP_GETOSPACE); |
| 686 | _EXPORT_INT(m, SNDCTL_DSP_GETSPDIF); |
| 687 | _EXPORT_INT(m, SNDCTL_DSP_GETTRIGGER); |
| 688 | _EXPORT_INT(m, SNDCTL_DSP_MAPINBUF); |
| 689 | _EXPORT_INT(m, SNDCTL_DSP_MAPOUTBUF); |
| 690 | _EXPORT_INT(m, SNDCTL_DSP_NONBLOCK); |
| 691 | _EXPORT_INT(m, SNDCTL_DSP_POST); |
| 692 | _EXPORT_INT(m, SNDCTL_DSP_PROFILE); |
| 693 | _EXPORT_INT(m, SNDCTL_DSP_RESET); |
| 694 | _EXPORT_INT(m, SNDCTL_DSP_SAMPLESIZE); |
| 695 | _EXPORT_INT(m, SNDCTL_DSP_SETDUPLEX); |
| 696 | _EXPORT_INT(m, SNDCTL_DSP_SETFMT); |
| 697 | _EXPORT_INT(m, SNDCTL_DSP_SETFRAGMENT); |
| 698 | _EXPORT_INT(m, SNDCTL_DSP_SETSPDIF); |
| 699 | _EXPORT_INT(m, SNDCTL_DSP_SETSYNCRO); |
| 700 | _EXPORT_INT(m, SNDCTL_DSP_SETTRIGGER); |
| 701 | _EXPORT_INT(m, SNDCTL_DSP_SPEED); |
| 702 | _EXPORT_INT(m, SNDCTL_DSP_STEREO); |
| 703 | _EXPORT_INT(m, SNDCTL_DSP_SUBDIVIDE); |
| 704 | _EXPORT_INT(m, SNDCTL_DSP_SYNC); |
| 705 | _EXPORT_INT(m, SNDCTL_FM_4OP_ENABLE); |
| 706 | _EXPORT_INT(m, SNDCTL_FM_LOAD_INSTR); |
| 707 | _EXPORT_INT(m, SNDCTL_MIDI_INFO); |
| 708 | _EXPORT_INT(m, SNDCTL_MIDI_MPUCMD); |
| 709 | _EXPORT_INT(m, SNDCTL_MIDI_MPUMODE); |
| 710 | _EXPORT_INT(m, SNDCTL_MIDI_PRETIME); |
| 711 | _EXPORT_INT(m, SNDCTL_SEQ_CTRLRATE); |
| 712 | _EXPORT_INT(m, SNDCTL_SEQ_GETINCOUNT); |
| 713 | _EXPORT_INT(m, SNDCTL_SEQ_GETOUTCOUNT); |
| 714 | _EXPORT_INT(m, SNDCTL_SEQ_GETTIME); |
| 715 | _EXPORT_INT(m, SNDCTL_SEQ_NRMIDIS); |
| 716 | _EXPORT_INT(m, SNDCTL_SEQ_NRSYNTHS); |
| 717 | _EXPORT_INT(m, SNDCTL_SEQ_OUTOFBAND); |
| 718 | _EXPORT_INT(m, SNDCTL_SEQ_PANIC); |
| 719 | _EXPORT_INT(m, SNDCTL_SEQ_PERCMODE); |
| 720 | _EXPORT_INT(m, SNDCTL_SEQ_RESET); |
| 721 | _EXPORT_INT(m, SNDCTL_SEQ_RESETSAMPLES); |
| 722 | _EXPORT_INT(m, SNDCTL_SEQ_SYNC); |
| 723 | _EXPORT_INT(m, SNDCTL_SEQ_TESTMIDI); |
| 724 | _EXPORT_INT(m, SNDCTL_SEQ_THRESHOLD); |
| 725 | _EXPORT_INT(m, SNDCTL_SYNTH_CONTROL); |
| 726 | _EXPORT_INT(m, SNDCTL_SYNTH_ID); |
| 727 | _EXPORT_INT(m, SNDCTL_SYNTH_INFO); |
| 728 | _EXPORT_INT(m, SNDCTL_SYNTH_MEMAVL); |
| 729 | _EXPORT_INT(m, SNDCTL_SYNTH_REMOVESAMPLE); |
| 730 | _EXPORT_INT(m, SNDCTL_TMR_CONTINUE); |
| 731 | _EXPORT_INT(m, SNDCTL_TMR_METRONOME); |
| 732 | _EXPORT_INT(m, SNDCTL_TMR_SELECT); |
| 733 | _EXPORT_INT(m, SNDCTL_TMR_SOURCE); |
| 734 | _EXPORT_INT(m, SNDCTL_TMR_START); |
| 735 | _EXPORT_INT(m, SNDCTL_TMR_STOP); |
| 736 | _EXPORT_INT(m, SNDCTL_TMR_TEMPO); |
| 737 | _EXPORT_INT(m, SNDCTL_TMR_TIMEBASE); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 738 | } |