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