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