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