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 | |
| 80 | static PyObject *LinuxAudioError; |
| 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 | |
| 90 | /* Two ways to call linuxaudiodev.open(): |
| 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; |
| 106 | else { |
| 107 | PyErr_SetString(LinuxAudioError, "mode should be 'r' or 'w'"); |
| 108 | return NULL; |
| 109 | } |
| 110 | |
| 111 | /* Open the correct device. The base device name comes from the |
| 112 | * AUDIODEV environment variable first, then /dev/dsp. The |
| 113 | * control device tacks "ctl" onto the base device name. |
| 114 | * |
| 115 | * Note that the only difference between /dev/audio and /dev/dsp |
| 116 | * is that the former uses logarithmic mu-law encoding and the |
| 117 | * latter uses 8-bit unsigned encoding. |
| 118 | */ |
| 119 | |
| 120 | if (basedev == NULL) { /* called with one arg */ |
| 121 | basedev = getenv("AUDIODEV"); |
| 122 | if (basedev == NULL) /* $AUDIODEV not set */ |
| 123 | basedev = "/dev/dsp"; |
| 124 | } |
| 125 | |
| 126 | if ((fd = open(basedev, imode)) == -1) { |
| 127 | PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev); |
| 128 | return NULL; |
| 129 | } |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 130 | if (ioctl(fd, SNDCTL_DSP_GETFMTS, &afmts) == -1) { |
| 131 | PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev); |
| 132 | return NULL; |
| 133 | } |
| 134 | /* Create and initialize the object */ |
| 135 | if ((xp = PyObject_New(lad_t, &Ladtype)) == NULL) { |
| 136 | close(fd); |
| 137 | return NULL; |
| 138 | } |
| 139 | xp->x_fd = fd; |
| 140 | xp->x_mode = imode; |
| 141 | xp->x_icount = xp->x_ocount = 0; |
| 142 | xp->x_afmts = afmts; |
| 143 | return xp; |
| 144 | } |
| 145 | |
| 146 | static void |
| 147 | lad_dealloc(lad_t *xp) |
| 148 | { |
| 149 | /* if already closed, don't reclose it */ |
| 150 | if (xp->x_fd != -1) |
| 151 | close(xp->x_fd); |
| 152 | PyObject_Del(xp); |
| 153 | } |
| 154 | |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame^] | 155 | |
| 156 | /* Methods to wrap the OSS ioctls. The calling convention is pretty |
| 157 | simple: |
| 158 | nonblock() -> ioctl(fd, SNDCTL_DSP_NONBLOCK) |
| 159 | fmt = setfmt(fmt) -> ioctl(fd, SNDCTL_DSP_SETFMT, &fmt) |
| 160 | etc. |
| 161 | */ |
| 162 | |
| 163 | |
| 164 | /* _do_ioctl_1() is a private helper function used for the OSS ioctls -- |
| 165 | SNDCTL_DSP_{SETFMT,CHANNELS,SPEED} -- that that are called from C |
| 166 | like this: |
| 167 | ioctl(fd, SNDCTL_DSP_cmd, &arg) |
| 168 | |
| 169 | where arg is the value to set, and on return the driver sets arg to |
| 170 | the value that was actually set. Mapping this to Python is obvious: |
| 171 | arg = dsp.xxx(arg) |
| 172 | */ |
| 173 | static PyObject * |
| 174 | _do_ioctl_1(lad_t *self, PyObject *args, char *fname, int cmd) |
| 175 | { |
| 176 | char argfmt[13] = "i:"; |
| 177 | int arg; |
| 178 | |
| 179 | assert(strlen(fname) <= 10); |
| 180 | strcat(argfmt, fname); |
| 181 | if (!PyArg_ParseTuple(args, argfmt, &arg)) |
| 182 | return NULL; |
| 183 | |
| 184 | if (ioctl(self->x_fd, cmd, &arg) == -1) |
| 185 | return PyErr_SetFromErrno(LinuxAudioError); |
| 186 | return PyInt_FromLong(arg); |
| 187 | } |
| 188 | |
| 189 | /* _do_ioctl_0() is a private helper for the no-argument ioctls: |
| 190 | SNDCTL_DSP_{SYNC,RESET,POST}. */ |
| 191 | static PyObject * |
| 192 | _do_ioctl_0(lad_t *self, PyObject *args, char *fname, int cmd) |
| 193 | { |
| 194 | char argfmt[12] = ":"; |
| 195 | |
| 196 | assert(strlen(fname) <= 10); |
| 197 | strcat(argfmt, fname); |
| 198 | if (!PyArg_ParseTuple(args, argfmt)) |
| 199 | return NULL; |
| 200 | |
| 201 | if (ioctl(self->x_fd, cmd, 0) == -1) |
| 202 | return PyErr_SetFromErrno(LinuxAudioError); |
| 203 | Py_INCREF(Py_None); |
| 204 | return Py_None; |
| 205 | } |
| 206 | |
| 207 | |
| 208 | static PyObject * |
| 209 | lad_nonblock(lad_t *self, PyObject *args) |
| 210 | { |
| 211 | /* Hmmm: it doesn't appear to be possible to return to blocking |
| 212 | mode once we're in non-blocking mode! */ |
| 213 | if (!PyArg_ParseTuple(args, ":nonblock")) |
| 214 | return NULL; |
| 215 | if (ioctl(self->x_fd, SNDCTL_DSP_NONBLOCK, NULL) == -1) |
| 216 | return PyErr_SetFromErrno(LinuxAudioError); |
| 217 | Py_INCREF(Py_None); |
| 218 | return Py_None; |
| 219 | } |
| 220 | |
| 221 | static PyObject * |
| 222 | lad_setfmt(lad_t *self, PyObject *args) |
| 223 | { |
| 224 | return _do_ioctl_1(self, args, "setfmt", SNDCTL_DSP_SETFMT); |
| 225 | } |
| 226 | |
| 227 | static PyObject * |
| 228 | lad_getfmts(lad_t *self, PyObject *args) |
| 229 | { |
| 230 | int mask; |
| 231 | if (!PyArg_ParseTuple(args, ":getfmts")) |
| 232 | return NULL; |
| 233 | if (ioctl(self->x_fd, SNDCTL_DSP_GETFMTS, &mask) == -1) |
| 234 | return PyErr_SetFromErrno(LinuxAudioError); |
| 235 | return PyInt_FromLong(mask); |
| 236 | } |
| 237 | |
| 238 | static PyObject * |
| 239 | lad_channels(lad_t *self, PyObject *args) |
| 240 | { |
| 241 | return _do_ioctl_1(self, args, "channels", SNDCTL_DSP_CHANNELS); |
| 242 | } |
| 243 | |
| 244 | static PyObject * |
| 245 | lad_speed(lad_t *self, PyObject *args) |
| 246 | { |
| 247 | return _do_ioctl_1(self, args, "speed", SNDCTL_DSP_SPEED); |
| 248 | } |
| 249 | |
| 250 | static PyObject * |
| 251 | lad_sync(lad_t *self, PyObject *args) |
| 252 | { |
| 253 | return _do_ioctl_0(self, args, "sync", SNDCTL_DSP_SYNC); |
| 254 | } |
| 255 | |
| 256 | static PyObject * |
| 257 | lad_reset(lad_t *self, PyObject *args) |
| 258 | { |
| 259 | return _do_ioctl_0(self, args, "reset", SNDCTL_DSP_RESET); |
| 260 | } |
| 261 | |
| 262 | static PyObject * |
| 263 | lad_post(lad_t *self, PyObject *args) |
| 264 | { |
| 265 | return _do_ioctl_0(self, args, "post", SNDCTL_DSP_POST); |
| 266 | } |
| 267 | |
| 268 | |
| 269 | /* Regular file methods: read(), write(), close(), etc. as well |
| 270 | as one convenience method, writeall(). */ |
| 271 | |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 272 | static PyObject * |
| 273 | lad_read(lad_t *self, PyObject *args) |
| 274 | { |
| 275 | int size, count; |
| 276 | char *cp; |
| 277 | PyObject *rv; |
| 278 | |
| 279 | if (!PyArg_ParseTuple(args, "i:read", &size)) |
| 280 | return NULL; |
| 281 | rv = PyString_FromStringAndSize(NULL, size); |
| 282 | if (rv == NULL) |
| 283 | return NULL; |
| 284 | cp = PyString_AS_STRING(rv); |
| 285 | if ((count = read(self->x_fd, cp, size)) < 0) { |
| 286 | PyErr_SetFromErrno(LinuxAudioError); |
| 287 | Py_DECREF(rv); |
| 288 | return NULL; |
| 289 | } |
| 290 | self->x_icount += count; |
| 291 | _PyString_Resize(&rv, count); |
| 292 | return rv; |
| 293 | } |
| 294 | |
| 295 | static PyObject * |
| 296 | lad_write(lad_t *self, PyObject *args) |
| 297 | { |
| 298 | char *cp; |
| 299 | int rv, size; |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame^] | 300 | |
| 301 | if (!PyArg_ParseTuple(args, "s#:write", &cp, &size)) { |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 302 | return NULL; |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame^] | 303 | } |
| 304 | if ((rv = write(self->x_fd, cp, size)) == -1) { |
| 305 | return PyErr_SetFromErrno(LinuxAudioError); |
| 306 | } else { |
| 307 | self->x_ocount += rv; |
| 308 | } |
| 309 | return PyInt_FromLong(rv); |
| 310 | } |
| 311 | |
| 312 | static PyObject * |
| 313 | lad_writeall(lad_t *self, PyObject *args) |
| 314 | { |
| 315 | char *cp; |
| 316 | int rv, size; |
| 317 | fd_set write_set_fds; |
| 318 | int select_rv; |
| 319 | |
| 320 | /* NB. writeall() is only useful in non-blocking mode: according to |
| 321 | Guenter Geiger <geiger@xdv.org> on the linux-audio-dev list |
| 322 | (http://eca.cx/lad/2002/11/0380.html), OSS guarantees that |
| 323 | write() in blocking mode consumes the whole buffer. In blocking |
| 324 | mode, the behaviour of write() and writeall() from Python is |
| 325 | indistinguishable. */ |
| 326 | |
| 327 | if (!PyArg_ParseTuple(args, "s#:write", &cp, &size)) |
| 328 | return NULL; |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 329 | |
| 330 | /* use select to wait for audio device to be available */ |
| 331 | FD_ZERO(&write_set_fds); |
| 332 | FD_SET(self->x_fd, &write_set_fds); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 333 | |
| 334 | while (size > 0) { |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame^] | 335 | select_rv = select(self->x_fd+1, NULL, &write_set_fds, NULL, NULL); |
| 336 | assert(select_rv != 0); /* no timeout, can't expire */ |
| 337 | if (select_rv == -1) |
| 338 | return PyErr_SetFromErrno(LinuxAudioError); |
| 339 | |
| 340 | rv = write(self->x_fd, cp, size); |
| 341 | if (rv == -1) { |
| 342 | if (errno == EAGAIN) { /* buffer is full, try again */ |
| 343 | errno = 0; |
| 344 | continue; |
| 345 | } else /* it's a real error */ |
| 346 | return PyErr_SetFromErrno(LinuxAudioError); |
| 347 | } else { /* wrote rv bytes */ |
| 348 | self->x_ocount += rv; |
| 349 | size -= rv; |
| 350 | cp += rv; |
| 351 | } |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 352 | } |
| 353 | Py_INCREF(Py_None); |
| 354 | return Py_None; |
| 355 | } |
| 356 | |
| 357 | static PyObject * |
| 358 | lad_close(lad_t *self, PyObject *args) |
| 359 | { |
| 360 | if (!PyArg_ParseTuple(args, ":close")) |
| 361 | return NULL; |
| 362 | |
| 363 | if (self->x_fd >= 0) { |
| 364 | close(self->x_fd); |
| 365 | self->x_fd = -1; |
| 366 | } |
| 367 | Py_INCREF(Py_None); |
| 368 | return Py_None; |
| 369 | } |
| 370 | |
| 371 | static PyObject * |
| 372 | lad_fileno(lad_t *self, PyObject *args) |
| 373 | { |
| 374 | if (!PyArg_ParseTuple(args, ":fileno")) |
| 375 | return NULL; |
| 376 | return PyInt_FromLong(self->x_fd); |
| 377 | } |
| 378 | |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame^] | 379 | |
| 380 | /* Convenience methods: these generally wrap a couple of ioctls into one |
| 381 | common task. */ |
| 382 | |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 383 | static PyObject * |
| 384 | lad_setparameters(lad_t *self, PyObject *args) |
| 385 | { |
| 386 | int rate, ssize, nchannels, n, fmt, emulate=0; |
| 387 | |
| 388 | if (!PyArg_ParseTuple(args, "iiii|i:setparameters", |
| 389 | &rate, &ssize, &nchannels, &fmt, &emulate)) |
| 390 | return NULL; |
| 391 | |
| 392 | if (rate < 0) { |
| 393 | PyErr_Format(PyExc_ValueError, "expected rate >= 0, not %d", |
| 394 | rate); |
| 395 | return NULL; |
| 396 | } |
| 397 | if (ssize < 0) { |
| 398 | PyErr_Format(PyExc_ValueError, "expected sample size >= 0, not %d", |
| 399 | ssize); |
| 400 | return NULL; |
| 401 | } |
| 402 | if (nchannels != 1 && nchannels != 2) { |
| 403 | PyErr_Format(PyExc_ValueError, "nchannels must be 1 or 2, not %d", |
| 404 | nchannels); |
| 405 | return NULL; |
| 406 | } |
| 407 | |
| 408 | for (n = 0; n < n_audio_types; n++) |
| 409 | if (fmt == audio_types[n].a_fmt) |
| 410 | break; |
| 411 | if (n == n_audio_types) { |
| 412 | PyErr_Format(PyExc_ValueError, "unknown audio encoding: %d", fmt); |
| 413 | return NULL; |
| 414 | } |
| 415 | if (audio_types[n].a_bps != ssize) { |
| 416 | PyErr_Format(PyExc_ValueError, |
| 417 | "for %s, expected sample size %d, not %d", |
| 418 | audio_types[n].a_name, audio_types[n].a_bps, ssize); |
| 419 | return NULL; |
| 420 | } |
| 421 | |
| 422 | if (emulate == 0) { |
| 423 | if ((self->x_afmts & audio_types[n].a_fmt) == 0) { |
| 424 | PyErr_Format(PyExc_ValueError, |
| 425 | "%s format not supported by device", |
| 426 | audio_types[n].a_name); |
| 427 | return NULL; |
| 428 | } |
| 429 | } |
| 430 | if (ioctl(self->x_fd, SNDCTL_DSP_SETFMT, |
| 431 | &audio_types[n].a_fmt) == -1) { |
| 432 | PyErr_SetFromErrno(LinuxAudioError); |
| 433 | return NULL; |
| 434 | } |
| 435 | if (ioctl(self->x_fd, SNDCTL_DSP_CHANNELS, &nchannels) == -1) { |
| 436 | PyErr_SetFromErrno(LinuxAudioError); |
| 437 | return NULL; |
| 438 | } |
| 439 | if (ioctl(self->x_fd, SNDCTL_DSP_SPEED, &rate) == -1) { |
| 440 | PyErr_SetFromErrno(LinuxAudioError); |
| 441 | return NULL; |
| 442 | } |
| 443 | |
| 444 | Py_INCREF(Py_None); |
| 445 | return Py_None; |
| 446 | } |
| 447 | |
| 448 | static int |
| 449 | _ssize(lad_t *self, int *nchannels, int *ssize) |
| 450 | { |
| 451 | int fmt; |
| 452 | |
| 453 | fmt = 0; |
| 454 | if (ioctl(self->x_fd, SNDCTL_DSP_SETFMT, &fmt) < 0) |
| 455 | return -errno; |
| 456 | |
| 457 | switch (fmt) { |
| 458 | case AFMT_MU_LAW: |
| 459 | case AFMT_A_LAW: |
| 460 | case AFMT_U8: |
| 461 | case AFMT_S8: |
| 462 | *ssize = sizeof(char); |
| 463 | break; |
| 464 | case AFMT_S16_LE: |
| 465 | case AFMT_S16_BE: |
| 466 | case AFMT_U16_LE: |
| 467 | case AFMT_U16_BE: |
| 468 | *ssize = sizeof(short); |
| 469 | break; |
| 470 | case AFMT_MPEG: |
| 471 | case AFMT_IMA_ADPCM: |
| 472 | default: |
| 473 | return -EOPNOTSUPP; |
| 474 | } |
| 475 | *nchannels = 0; |
| 476 | if (ioctl(self->x_fd, SNDCTL_DSP_CHANNELS, nchannels) < 0) |
| 477 | return -errno; |
| 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | |
| 482 | /* bufsize returns the size of the hardware audio buffer in number |
| 483 | of samples */ |
| 484 | static PyObject * |
| 485 | lad_bufsize(lad_t *self, PyObject *args) |
| 486 | { |
| 487 | audio_buf_info ai; |
| 488 | int nchannels, ssize; |
| 489 | |
| 490 | if (!PyArg_ParseTuple(args, ":bufsize")) return NULL; |
| 491 | |
| 492 | if (_ssize(self, &nchannels, &ssize) < 0) { |
| 493 | PyErr_SetFromErrno(LinuxAudioError); |
| 494 | return NULL; |
| 495 | } |
| 496 | if (ioctl(self->x_fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) { |
| 497 | PyErr_SetFromErrno(LinuxAudioError); |
| 498 | return NULL; |
| 499 | } |
| 500 | return PyInt_FromLong((ai.fragstotal * ai.fragsize) / (nchannels * ssize)); |
| 501 | } |
| 502 | |
| 503 | /* obufcount returns the number of samples that are available in the |
| 504 | hardware for playing */ |
| 505 | static PyObject * |
| 506 | lad_obufcount(lad_t *self, PyObject *args) |
| 507 | { |
| 508 | audio_buf_info ai; |
| 509 | int nchannels, ssize; |
| 510 | |
| 511 | if (!PyArg_ParseTuple(args, ":obufcount")) |
| 512 | return NULL; |
| 513 | |
| 514 | if (_ssize(self, &nchannels, &ssize) < 0) { |
| 515 | PyErr_SetFromErrno(LinuxAudioError); |
| 516 | return NULL; |
| 517 | } |
| 518 | if (ioctl(self->x_fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) { |
| 519 | PyErr_SetFromErrno(LinuxAudioError); |
| 520 | return NULL; |
| 521 | } |
| 522 | return PyInt_FromLong((ai.fragstotal * ai.fragsize - ai.bytes) / |
| 523 | (ssize * nchannels)); |
| 524 | } |
| 525 | |
| 526 | /* obufcount returns the number of samples that can be played without |
| 527 | blocking */ |
| 528 | static PyObject * |
| 529 | lad_obuffree(lad_t *self, PyObject *args) |
| 530 | { |
| 531 | audio_buf_info ai; |
| 532 | int nchannels, ssize; |
| 533 | |
| 534 | if (!PyArg_ParseTuple(args, ":obuffree")) |
| 535 | return NULL; |
| 536 | |
| 537 | if (_ssize(self, &nchannels, &ssize) < 0) { |
| 538 | PyErr_SetFromErrno(LinuxAudioError); |
| 539 | return NULL; |
| 540 | } |
| 541 | if (ioctl(self->x_fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) { |
| 542 | PyErr_SetFromErrno(LinuxAudioError); |
| 543 | return NULL; |
| 544 | } |
| 545 | return PyInt_FromLong(ai.bytes / (ssize * nchannels)); |
| 546 | } |
| 547 | |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 548 | static PyObject * |
| 549 | lad_getptr(lad_t *self, PyObject *args) |
| 550 | { |
| 551 | count_info info; |
| 552 | int req; |
| 553 | |
| 554 | if (!PyArg_ParseTuple(args, ":getptr")) |
| 555 | return NULL; |
| 556 | |
| 557 | if (self->x_mode == O_RDONLY) |
| 558 | req = SNDCTL_DSP_GETIPTR; |
| 559 | else |
| 560 | req = SNDCTL_DSP_GETOPTR; |
| 561 | if (ioctl(self->x_fd, req, &info) == -1) { |
| 562 | PyErr_SetFromErrno(LinuxAudioError); |
| 563 | return NULL; |
| 564 | } |
| 565 | return Py_BuildValue("iii", info.bytes, info.blocks, info.ptr); |
| 566 | } |
| 567 | |
| 568 | static PyMethodDef lad_methods[] = { |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame^] | 569 | /* Regular file methods */ |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 570 | { "read", (PyCFunction)lad_read, METH_VARARGS }, |
| 571 | { "write", (PyCFunction)lad_write, METH_VARARGS }, |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame^] | 572 | { "writeall", (PyCFunction)lad_writeall, METH_VARARGS }, |
| 573 | { "close", (PyCFunction)lad_close, METH_VARARGS }, |
| 574 | { "fileno", (PyCFunction)lad_fileno, METH_VARARGS }, |
| 575 | |
| 576 | /* Simple ioctl wrappers */ |
| 577 | { "nonblock", (PyCFunction)lad_nonblock, METH_VARARGS }, |
| 578 | { "setfmt", (PyCFunction)lad_setfmt, METH_VARARGS }, |
| 579 | { "getfmts", (PyCFunction)lad_getfmts, METH_VARARGS }, |
| 580 | { "channels", (PyCFunction)lad_channels, METH_VARARGS }, |
| 581 | { "speed", (PyCFunction)lad_speed, METH_VARARGS }, |
| 582 | { "sync", (PyCFunction)lad_sync, METH_VARARGS }, |
| 583 | { "reset", (PyCFunction)lad_reset, METH_VARARGS }, |
| 584 | { "post", (PyCFunction)lad_post, METH_VARARGS }, |
| 585 | |
| 586 | /* Convenience methods -- wrap a couple of ioctls together */ |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 587 | { "setparameters", (PyCFunction)lad_setparameters, METH_VARARGS }, |
| 588 | { "bufsize", (PyCFunction)lad_bufsize, METH_VARARGS }, |
| 589 | { "obufcount", (PyCFunction)lad_obufcount, METH_VARARGS }, |
| 590 | { "obuffree", (PyCFunction)lad_obuffree, METH_VARARGS }, |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 591 | { "getptr", (PyCFunction)lad_getptr, METH_VARARGS }, |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame^] | 592 | |
| 593 | /* Aliases for backwards compatibility */ |
| 594 | { "flush", (PyCFunction)lad_sync, METH_VARARGS }, |
| 595 | |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 596 | { NULL, NULL} /* sentinel */ |
| 597 | }; |
| 598 | |
| 599 | static PyObject * |
| 600 | lad_getattr(lad_t *xp, char *name) |
| 601 | { |
| 602 | return Py_FindMethod(lad_methods, (PyObject *)xp, name); |
| 603 | } |
| 604 | |
| 605 | static PyTypeObject Ladtype = { |
| 606 | PyObject_HEAD_INIT(&PyType_Type) |
| 607 | 0, /*ob_size*/ |
| 608 | "linuxaudiodev.linux_audio_device", /*tp_name*/ |
| 609 | sizeof(lad_t), /*tp_size*/ |
| 610 | 0, /*tp_itemsize*/ |
| 611 | /* methods */ |
| 612 | (destructor)lad_dealloc, /*tp_dealloc*/ |
| 613 | 0, /*tp_print*/ |
| 614 | (getattrfunc)lad_getattr, /*tp_getattr*/ |
| 615 | 0, /*tp_setattr*/ |
| 616 | 0, /*tp_compare*/ |
| 617 | 0, /*tp_repr*/ |
| 618 | }; |
| 619 | |
| 620 | static PyObject * |
| 621 | ladopen(PyObject *self, PyObject *args) |
| 622 | { |
| 623 | return (PyObject *)newladobject(args); |
| 624 | } |
| 625 | |
| 626 | static PyMethodDef linuxaudiodev_methods[] = { |
| 627 | { "open", ladopen, METH_VARARGS }, |
| 628 | { 0, 0 }, |
| 629 | }; |
| 630 | |
| 631 | void |
| 632 | initlinuxaudiodev(void) |
| 633 | { |
| 634 | PyObject *m; |
| 635 | |
| 636 | m = Py_InitModule("linuxaudiodev", linuxaudiodev_methods); |
| 637 | |
| 638 | LinuxAudioError = PyErr_NewException("linuxaudiodev.error", NULL, NULL); |
| 639 | if (LinuxAudioError) |
| 640 | PyModule_AddObject(m, "error", LinuxAudioError); |
| 641 | |
| 642 | if (PyModule_AddIntConstant(m, "AFMT_MU_LAW", (long)AFMT_MU_LAW) == -1) |
| 643 | return; |
| 644 | if (PyModule_AddIntConstant(m, "AFMT_A_LAW", (long)AFMT_A_LAW) == -1) |
| 645 | return; |
| 646 | if (PyModule_AddIntConstant(m, "AFMT_U8", (long)AFMT_U8) == -1) |
| 647 | return; |
| 648 | if (PyModule_AddIntConstant(m, "AFMT_S8", (long)AFMT_S8) == -1) |
| 649 | return; |
| 650 | if (PyModule_AddIntConstant(m, "AFMT_U16_BE", (long)AFMT_U16_BE) == -1) |
| 651 | return; |
| 652 | if (PyModule_AddIntConstant(m, "AFMT_U16_LE", (long)AFMT_U16_LE) == -1) |
| 653 | return; |
| 654 | if (PyModule_AddIntConstant(m, "AFMT_S16_BE", (long)AFMT_S16_BE) == -1) |
| 655 | return; |
| 656 | if (PyModule_AddIntConstant(m, "AFMT_S16_LE", (long)AFMT_S16_LE) == -1) |
| 657 | return; |
| 658 | if (PyModule_AddIntConstant(m, "AFMT_S16_NE", (long)AFMT_S16_NE) == -1) |
| 659 | return; |
| 660 | |
| 661 | return; |
| 662 | } |