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