Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 1 | /* Hey Emacs, this is -*-C-*- |
| 2 | ****************************************************************************** |
| 3 | * linuxaudiodev.c -- Linux audio device for python. |
| 4 | * |
| 5 | * Author : Peter Bosch |
| 6 | * Created On : Thu Mar 2 21:10:33 2000 |
| 7 | * Last Modified By: Peter Bosch |
| 8 | * Last Modified On: Fri Mar 24 11:27:00 2000 |
| 9 | * Status : Unknown, Use with caution! |
| 10 | * |
| 11 | * Unless other notices are present in any part of this file |
| 12 | * explicitly claiming copyrights for other people and/or |
| 13 | * organizations, the contents of this file is fully copyright |
| 14 | * (C) 2000 Peter Bosch, all rights reserved. |
| 15 | ****************************************************************************** |
| 16 | */ |
| 17 | |
| 18 | #include "Python.h" |
| 19 | #include "structmember.h" |
| 20 | |
| 21 | #ifdef HAVE_UNISTD_H |
| 22 | #include <unistd.h> |
| 23 | #endif |
| 24 | |
| 25 | #ifdef HAVE_FCNTL_H |
| 26 | #include <fcntl.h> |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame^] | 27 | #else |
| 28 | #define O_RDONLY 00 |
| 29 | #define O_WRONLY 01 |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 30 | #endif |
| 31 | |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame^] | 32 | |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 33 | #include <sys/ioctl.h> |
Jeremy Hylton | a3895c0 | 2000-08-31 18:11:07 +0000 | [diff] [blame] | 34 | #if defined(linux) |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 35 | #include <linux/soundcard.h> |
| 36 | |
| 37 | typedef unsigned long uint32_t; |
| 38 | |
Jeremy Hylton | a3895c0 | 2000-08-31 18:11:07 +0000 | [diff] [blame] | 39 | #elif defined(__FreeBSD__) |
| 40 | #include <machine/soundcard.h> |
| 41 | |
| 42 | #ifndef SNDCTL_DSP_CHANNELS |
| 43 | #define SNDCTL_DSP_CHANNELS SOUND_PCM_WRITE_CHANNELS |
| 44 | #endif |
| 45 | |
| 46 | #endif |
| 47 | |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 48 | typedef struct { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 49 | PyObject_HEAD; |
| 50 | int x_fd; /* The open file */ |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame^] | 51 | int x_mode; /* file mode */ |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 52 | int x_icount; /* Input count */ |
| 53 | int x_ocount; /* Output count */ |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame^] | 54 | uint32_t x_afmts; /* Audio formats supported by hardware*/ |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 55 | } lad_t; |
| 56 | |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame^] | 57 | /* XXX several format defined in soundcard.h are not supported, |
| 58 | including _NE (native endian) options and S32 options |
| 59 | */ |
| 60 | |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 61 | static struct { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 62 | int a_bps; |
| 63 | uint32_t a_fmt; |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame^] | 64 | char *a_name; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 65 | } audio_types[] = { |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame^] | 66 | { 8, AFMT_MU_LAW, "Logarithmic mu-law audio" }, |
| 67 | { 8, AFMT_A_LAW, "Logarithmic A-law audio" }, |
| 68 | { 8, AFMT_U8, "Standard unsigned 8-bit audio" }, |
| 69 | { 8, AFMT_S8, "Standard signed 8-bit audio" }, |
| 70 | { 16, AFMT_U16_BE, "Big-endian 16-bit unsigned format" }, |
| 71 | { 16, AFMT_U16_LE, "Little-endian 16-bit unsigned format" }, |
| 72 | { 16, AFMT_S16_BE, "Big-endian 16-bit signed format" }, |
| 73 | { 16, AFMT_S16_LE, "Little-endian 16-bit signed format" }, |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame^] | 76 | static int n_audio_types = sizeof(audio_types) / sizeof(audio_types[0]); |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 77 | |
| 78 | staticforward PyTypeObject Ladtype; |
| 79 | |
| 80 | static PyObject *LinuxAudioError; |
| 81 | |
| 82 | static lad_t * |
| 83 | newladobject(PyObject *arg) |
| 84 | { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 85 | lad_t *xp; |
| 86 | int fd, afmts, imode; |
| 87 | char *mode; |
| 88 | char *basedev; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 89 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 90 | /* Check arg for r/w/rw */ |
| 91 | if (!PyArg_ParseTuple(arg, "s:open", &mode)) return NULL; |
| 92 | if (strcmp(mode, "r") == 0) |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame^] | 93 | imode = O_RDONLY; |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 94 | else if (strcmp(mode, "w") == 0) |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame^] | 95 | imode = O_WRONLY; |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 96 | else { |
| 97 | PyErr_SetString(LinuxAudioError, "Mode should be one of 'r', or 'w'"); |
| 98 | return NULL; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 99 | } |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 100 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 101 | /* Open the correct device. The base device name comes from the |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame^] | 102 | * AUDIODEV environment variable first, then /dev/dsp. The |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 103 | * control device tacks "ctl" onto the base device name. |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame^] | 104 | * |
| 105 | * Note that the only difference between /dev/audio and /dev/dsp |
| 106 | * is that the former uses logarithmic mu-law encoding and the |
| 107 | * latter uses 8-bit unsigned encoding. |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 108 | */ |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame^] | 109 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 110 | basedev = getenv("AUDIODEV"); |
| 111 | if (!basedev) |
| 112 | basedev = "/dev/dsp"; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 113 | |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame^] | 114 | if ((fd = open(basedev, imode)) == -1) { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 115 | PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev); |
| 116 | return NULL; |
| 117 | } |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame^] | 118 | if (imode == O_WRONLY && ioctl(fd, SNDCTL_DSP_NONBLOCK, NULL) == -1) { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 119 | PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev); |
| 120 | return NULL; |
| 121 | } |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame^] | 122 | if (ioctl(fd, SNDCTL_DSP_GETFMTS, &afmts) == -1) { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 123 | PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev); |
| 124 | return NULL; |
| 125 | } |
| 126 | /* Create and initialize the object */ |
| 127 | if ((xp = PyObject_New(lad_t, &Ladtype)) == NULL) { |
| 128 | close(fd); |
| 129 | return NULL; |
| 130 | } |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame^] | 131 | xp->x_fd = fd; |
| 132 | xp->x_mode = imode; |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 133 | xp->x_icount = xp->x_ocount = 0; |
| 134 | xp->x_afmts = afmts; |
| 135 | return xp; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | static void |
| 139 | lad_dealloc(lad_t *xp) |
| 140 | { |
Barry Warsaw | 4ddd820 | 2000-08-18 05:10:45 +0000 | [diff] [blame] | 141 | /* if already closed, don't reclose it */ |
| 142 | if (xp->x_fd != -1) |
| 143 | close(xp->x_fd); |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 144 | PyObject_Del(xp); |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | static PyObject * |
| 148 | lad_read(lad_t *self, PyObject *args) |
| 149 | { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 150 | int size, count; |
| 151 | char *cp; |
| 152 | PyObject *rv; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 153 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 154 | if (!PyArg_ParseTuple(args, "i:read", &size)) |
| 155 | return NULL; |
| 156 | rv = PyString_FromStringAndSize(NULL, size); |
| 157 | if (rv == NULL) |
| 158 | return NULL; |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame^] | 159 | cp = PyString_AS_STRING(rv); |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 160 | if ((count = read(self->x_fd, cp, size)) < 0) { |
| 161 | PyErr_SetFromErrno(LinuxAudioError); |
| 162 | Py_DECREF(rv); |
| 163 | return NULL; |
| 164 | } |
| 165 | self->x_icount += count; |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame^] | 166 | if (_PyString_Resize(&rv, count) == -1) |
| 167 | return NULL; |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 168 | return rv; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | static PyObject * |
| 172 | lad_write(lad_t *self, PyObject *args) |
| 173 | { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 174 | char *cp; |
| 175 | int rv, size; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 176 | |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame^] | 177 | if (!PyArg_ParseTuple(args, "s#:write", &cp, &size)) |
| 178 | return NULL; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 179 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 180 | while (size > 0) { |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame^] | 181 | if ((rv = write(self->x_fd, cp, size)) == -1) { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 182 | PyErr_SetFromErrno(LinuxAudioError); |
| 183 | return NULL; |
| 184 | } |
| 185 | self->x_ocount += rv; |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame^] | 186 | size -= rv; |
| 187 | cp += rv; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 188 | } |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 189 | Py_INCREF(Py_None); |
| 190 | return Py_None; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | static PyObject * |
| 194 | lad_close(lad_t *self, PyObject *args) |
| 195 | { |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame^] | 196 | if (!PyArg_ParseTuple(args, ":close")) |
| 197 | return NULL; |
| 198 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 199 | if (self->x_fd >= 0) { |
| 200 | close(self->x_fd); |
| 201 | self->x_fd = -1; |
| 202 | } |
| 203 | Py_INCREF(Py_None); |
| 204 | return Py_None; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static PyObject * |
| 208 | lad_fileno(lad_t *self, PyObject *args) |
| 209 | { |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame^] | 210 | if (!PyArg_ParseTuple(args, ":fileno")) |
| 211 | return NULL; |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 212 | return PyInt_FromLong(self->x_fd); |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | static PyObject * |
| 216 | lad_setparameters(lad_t *self, PyObject *args) |
| 217 | { |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame^] | 218 | int rate, ssize, nchannels, n, fmt, emulate=0; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 219 | |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame^] | 220 | if (!PyArg_ParseTuple(args, "iiii|i:setparameters", |
| 221 | &rate, &ssize, &nchannels, &fmt, &emulate)) |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 222 | return NULL; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 223 | |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame^] | 224 | if (rate < 0) { |
| 225 | PyErr_Format(PyExc_ValueError, "expected rate >= 0, not %d", |
| 226 | rate); |
| 227 | return NULL; |
| 228 | } |
| 229 | if (ssize < 0) { |
| 230 | PyErr_Format(PyExc_ValueError, "expected sample size >= 0, not %d", |
| 231 | ssize); |
| 232 | return NULL; |
| 233 | } |
| 234 | if (nchannels != 1 && nchannels != 2) { |
| 235 | PyErr_Format(PyExc_ValueError, "nchannels must be 1 or 2, not %d", |
| 236 | nchannels); |
| 237 | return NULL; |
| 238 | } |
| 239 | |
| 240 | if (ioctl(self->x_fd, SNDCTL_DSP_SPEED, &rate) == -1) { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 241 | PyErr_SetFromErrno(LinuxAudioError); |
| 242 | return NULL; |
| 243 | } |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame^] | 244 | if (ioctl(self->x_fd, SNDCTL_DSP_CHANNELS, &nchannels) == -1) { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 245 | PyErr_SetFromErrno(LinuxAudioError); |
| 246 | return NULL; |
| 247 | } |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame^] | 248 | |
| 249 | for (n = 0; n < n_audio_types; n++) |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 250 | if (fmt == audio_types[n].a_fmt) |
| 251 | break; |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame^] | 252 | if (n == n_audio_types) { |
| 253 | PyErr_Format(PyExc_ValueError, "unknown audio encoding: %d", fmt); |
| 254 | return NULL; |
| 255 | } |
| 256 | if (audio_types[n].a_bps != ssize) { |
| 257 | PyErr_Format(PyExc_ValueError, |
| 258 | "sample size %d expected for %s: %d received", |
| 259 | audio_types[n].a_bps, audio_types[n].a_name, ssize); |
| 260 | return NULL; |
| 261 | } |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 262 | |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame^] | 263 | if (emulate == 0) { |
| 264 | if ((self->x_afmts & audio_types[n].a_fmt) == 0) { |
| 265 | PyErr_Format(PyExc_ValueError, |
| 266 | "format not supported by device: %s", |
| 267 | audio_types[n].a_name); |
| 268 | return NULL; |
| 269 | } |
| 270 | } |
| 271 | if (ioctl(self->x_fd, SNDCTL_DSP_SETFMT, |
| 272 | &audio_types[n].a_fmt) == -1) { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 273 | PyErr_SetFromErrno(LinuxAudioError); |
| 274 | return NULL; |
| 275 | } |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame^] | 276 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 277 | Py_INCREF(Py_None); |
| 278 | return Py_None; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | static int |
| 282 | _ssize(lad_t *self, int *nchannels, int *ssize) |
| 283 | { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 284 | int fmt; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 285 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 286 | fmt = 0; |
| 287 | if (ioctl(self->x_fd, SNDCTL_DSP_SETFMT, &fmt) < 0) |
| 288 | return -errno; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 289 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 290 | switch (fmt) { |
| 291 | case AFMT_MU_LAW: |
| 292 | case AFMT_A_LAW: |
| 293 | case AFMT_U8: |
| 294 | case AFMT_S8: |
| 295 | *ssize = sizeof(char); |
| 296 | break; |
| 297 | case AFMT_S16_LE: |
| 298 | case AFMT_S16_BE: |
| 299 | case AFMT_U16_LE: |
| 300 | case AFMT_U16_BE: |
| 301 | *ssize = sizeof(short); |
| 302 | break; |
| 303 | case AFMT_MPEG: |
| 304 | case AFMT_IMA_ADPCM: |
| 305 | default: |
| 306 | return -EOPNOTSUPP; |
| 307 | } |
| 308 | *nchannels = 0; |
| 309 | if (ioctl(self->x_fd, SNDCTL_DSP_CHANNELS, nchannels) < 0) |
| 310 | return -errno; |
| 311 | return 0; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | |
| 315 | /* bufsize returns the size of the hardware audio buffer in number |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 316 | of samples */ |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 317 | static PyObject * |
| 318 | lad_bufsize(lad_t *self, PyObject *args) |
| 319 | { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 320 | audio_buf_info ai; |
| 321 | int nchannels, ssize; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 322 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 323 | if (!PyArg_ParseTuple(args, ":bufsize")) return NULL; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 324 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 325 | if (_ssize(self, &nchannels, &ssize) < 0) { |
| 326 | PyErr_SetFromErrno(LinuxAudioError); |
| 327 | return NULL; |
| 328 | } |
| 329 | if (ioctl(self->x_fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) { |
| 330 | PyErr_SetFromErrno(LinuxAudioError); |
| 331 | return NULL; |
| 332 | } |
| 333 | return PyInt_FromLong((ai.fragstotal * ai.fragsize) / (nchannels * ssize)); |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | /* obufcount returns the number of samples that are available in the |
| 337 | hardware for playing */ |
| 338 | static PyObject * |
| 339 | lad_obufcount(lad_t *self, PyObject *args) |
| 340 | { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 341 | audio_buf_info ai; |
| 342 | int nchannels, ssize; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 343 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 344 | if (!PyArg_ParseTuple(args, ":obufcount")) |
| 345 | return NULL; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 346 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 347 | if (_ssize(self, &nchannels, &ssize) < 0) { |
| 348 | PyErr_SetFromErrno(LinuxAudioError); |
| 349 | return NULL; |
| 350 | } |
| 351 | if (ioctl(self->x_fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) { |
| 352 | PyErr_SetFromErrno(LinuxAudioError); |
| 353 | return NULL; |
| 354 | } |
| 355 | return PyInt_FromLong((ai.fragstotal * ai.fragsize - ai.bytes) / |
| 356 | (ssize * nchannels)); |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | /* obufcount returns the number of samples that can be played without |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 360 | blocking */ |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 361 | static PyObject * |
| 362 | lad_obuffree(lad_t *self, PyObject *args) |
| 363 | { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 364 | audio_buf_info ai; |
| 365 | int nchannels, ssize; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 366 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 367 | if (!PyArg_ParseTuple(args, ":obuffree")) |
| 368 | return NULL; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 369 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 370 | if (_ssize(self, &nchannels, &ssize) < 0) { |
| 371 | PyErr_SetFromErrno(LinuxAudioError); |
| 372 | return NULL; |
| 373 | } |
| 374 | if (ioctl(self->x_fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) { |
| 375 | PyErr_SetFromErrno(LinuxAudioError); |
| 376 | return NULL; |
| 377 | } |
| 378 | return PyInt_FromLong(ai.bytes / (ssize * nchannels)); |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | /* Flush the device */ |
| 382 | static PyObject * |
| 383 | lad_flush(lad_t *self, PyObject *args) |
| 384 | { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 385 | if (!PyArg_ParseTuple(args, ":flush")) return NULL; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 386 | |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame^] | 387 | if (ioctl(self->x_fd, SNDCTL_DSP_SYNC, NULL) == -1) { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 388 | PyErr_SetFromErrno(LinuxAudioError); |
| 389 | return NULL; |
| 390 | } |
| 391 | Py_INCREF(Py_None); |
| 392 | return Py_None; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame^] | 395 | static PyObject * |
| 396 | lad_getptr(lad_t *self, PyObject *args) |
| 397 | { |
| 398 | count_info info; |
| 399 | int req; |
| 400 | |
| 401 | if (!PyArg_ParseTuple(args, ":getptr")) |
| 402 | return NULL; |
| 403 | |
| 404 | if (self->x_mode == O_RDONLY) |
| 405 | req = SNDCTL_DSP_GETIPTR; |
| 406 | else |
| 407 | req = SNDCTL_DSP_GETOPTR; |
| 408 | if (ioctl(self->x_fd, req, &info) == -1) { |
| 409 | PyErr_SetFromErrno(LinuxAudioError); |
| 410 | return NULL; |
| 411 | } |
| 412 | return Py_BuildValue("iii", info.bytes, info.blocks, info.ptr); |
| 413 | } |
| 414 | |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 415 | static PyMethodDef lad_methods[] = { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 416 | { "read", (PyCFunction)lad_read, METH_VARARGS }, |
| 417 | { "write", (PyCFunction)lad_write, METH_VARARGS }, |
| 418 | { "setparameters", (PyCFunction)lad_setparameters, METH_VARARGS }, |
| 419 | { "bufsize", (PyCFunction)lad_bufsize, METH_VARARGS }, |
| 420 | { "obufcount", (PyCFunction)lad_obufcount, METH_VARARGS }, |
| 421 | { "obuffree", (PyCFunction)lad_obuffree, METH_VARARGS }, |
| 422 | { "flush", (PyCFunction)lad_flush, METH_VARARGS }, |
| 423 | { "close", (PyCFunction)lad_close, METH_VARARGS }, |
| 424 | { "fileno", (PyCFunction)lad_fileno, METH_VARARGS }, |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame^] | 425 | { "getptr", (PyCFunction)lad_getptr, METH_VARARGS }, |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 426 | { NULL, NULL} /* sentinel */ |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 427 | }; |
| 428 | |
| 429 | static PyObject * |
| 430 | lad_getattr(lad_t *xp, char *name) |
| 431 | { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 432 | return Py_FindMethod(lad_methods, (PyObject *)xp, name); |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | static PyTypeObject Ladtype = { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 436 | PyObject_HEAD_INIT(&PyType_Type) |
| 437 | 0, /*ob_size*/ |
| 438 | "linux_audio_device", /*tp_name*/ |
| 439 | sizeof(lad_t), /*tp_size*/ |
| 440 | 0, /*tp_itemsize*/ |
| 441 | /* methods */ |
| 442 | (destructor)lad_dealloc, /*tp_dealloc*/ |
| 443 | 0, /*tp_print*/ |
| 444 | (getattrfunc)lad_getattr, /*tp_getattr*/ |
| 445 | 0, /*tp_setattr*/ |
| 446 | 0, /*tp_compare*/ |
| 447 | 0, /*tp_repr*/ |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 448 | }; |
| 449 | |
| 450 | static PyObject * |
| 451 | ladopen(PyObject *self, PyObject *args) |
| 452 | { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 453 | return (PyObject *)newladobject(args); |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | static PyMethodDef linuxaudiodev_methods[] = { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 457 | { "open", ladopen, METH_VARARGS }, |
| 458 | { 0, 0 }, |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 459 | }; |
| 460 | |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 461 | void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 462 | initlinuxaudiodev(void) |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 463 | { |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame^] | 464 | PyObject *m; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 465 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 466 | m = Py_InitModule("linuxaudiodev", linuxaudiodev_methods); |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 467 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 468 | LinuxAudioError = PyErr_NewException("linuxaudiodev.error", NULL, NULL); |
| 469 | if (LinuxAudioError) |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame^] | 470 | PyModule_AddObject(m, "error", LinuxAudioError); |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 471 | |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame^] | 472 | if (PyModule_AddIntConstant(m, "AFMT_MU_LAW", (long)AFMT_MU_LAW) == -1) |
| 473 | return; |
| 474 | if (PyModule_AddIntConstant(m, "AFMT_A_LAW", (long)AFMT_A_LAW) == -1) |
| 475 | return; |
| 476 | if (PyModule_AddIntConstant(m, "AFMT_U8", (long)AFMT_U8) == -1) |
| 477 | return; |
| 478 | if (PyModule_AddIntConstant(m, "AFMT_S8", (long)AFMT_S8) == -1) |
| 479 | return; |
| 480 | if (PyModule_AddIntConstant(m, "AFMT_U16_BE", (long)AFMT_U16_BE) == -1) |
| 481 | return; |
| 482 | if (PyModule_AddIntConstant(m, "AFMT_U16_LE", (long)AFMT_U16_LE) == -1) |
| 483 | return; |
| 484 | if (PyModule_AddIntConstant(m, "AFMT_S16_BE", (long)AFMT_S16_BE) == -1) |
| 485 | return; |
| 486 | if (PyModule_AddIntConstant(m, "AFMT_S16_LE", (long)AFMT_S16_LE) == -1) |
| 487 | return; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 488 | |
Barry Warsaw | 9bfd2bf | 2000-09-01 09:01:32 +0000 | [diff] [blame] | 489 | return; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 490 | } |