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> |
| 27 | #endif |
| 28 | |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 29 | #include <sys/ioctl.h> |
Jeremy Hylton | a3895c0 | 2000-08-31 18:11:07 +0000 | [diff] [blame] | 30 | #if defined(linux) |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 31 | #include <linux/soundcard.h> |
| 32 | |
| 33 | typedef unsigned long uint32_t; |
| 34 | |
Jeremy Hylton | a3895c0 | 2000-08-31 18:11:07 +0000 | [diff] [blame] | 35 | #elif defined(__FreeBSD__) |
| 36 | #include <machine/soundcard.h> |
| 37 | |
| 38 | #ifndef SNDCTL_DSP_CHANNELS |
| 39 | #define SNDCTL_DSP_CHANNELS SOUND_PCM_WRITE_CHANNELS |
| 40 | #endif |
| 41 | |
| 42 | #endif |
| 43 | |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 44 | typedef struct { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 45 | PyObject_HEAD; |
| 46 | int x_fd; /* The open file */ |
| 47 | int x_icount; /* Input count */ |
| 48 | int x_ocount; /* Output count */ |
| 49 | uint32_t x_afmts; /* Supported audio formats */ |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 50 | } lad_t; |
| 51 | |
| 52 | static struct { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 53 | int a_bps; |
| 54 | uint32_t a_fmt; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 55 | } audio_types[] = { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 56 | { 8, AFMT_MU_LAW }, |
| 57 | { 8, AFMT_U8 }, |
| 58 | { 8, AFMT_S8 }, |
| 59 | { 16, AFMT_U16_BE }, |
| 60 | { 16, AFMT_U16_LE }, |
| 61 | { 16, AFMT_S16_BE }, |
| 62 | { 16, AFMT_S16_LE }, |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | |
| 66 | staticforward PyTypeObject Ladtype; |
| 67 | |
| 68 | static PyObject *LinuxAudioError; |
| 69 | |
| 70 | static lad_t * |
| 71 | newladobject(PyObject *arg) |
| 72 | { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 73 | lad_t *xp; |
| 74 | int fd, afmts, imode; |
| 75 | char *mode; |
| 76 | char *basedev; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 77 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 78 | /* Check arg for r/w/rw */ |
| 79 | if (!PyArg_ParseTuple(arg, "s:open", &mode)) return NULL; |
| 80 | if (strcmp(mode, "r") == 0) |
| 81 | imode = 0; |
| 82 | else if (strcmp(mode, "w") == 0) |
| 83 | imode = 1; |
| 84 | else { |
| 85 | PyErr_SetString(LinuxAudioError, "Mode should be one of 'r', or 'w'"); |
| 86 | return NULL; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 87 | } |
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 | /* Open the correct device. The base device name comes from the |
| 90 | * AUDIODEV environment variable first, then /dev/audio. The |
| 91 | * control device tacks "ctl" onto the base device name. |
| 92 | */ |
| 93 | basedev = getenv("AUDIODEV"); |
| 94 | if (!basedev) |
| 95 | basedev = "/dev/dsp"; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 96 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 97 | if ((fd = open(basedev, imode)) < 0) { |
| 98 | PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev); |
| 99 | return NULL; |
| 100 | } |
| 101 | if (imode && ioctl(fd, SNDCTL_DSP_NONBLOCK, NULL) < 0) { |
| 102 | PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev); |
| 103 | return NULL; |
| 104 | } |
| 105 | if (ioctl(fd, SNDCTL_DSP_GETFMTS, &afmts) < 0) { |
| 106 | PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev); |
| 107 | return NULL; |
| 108 | } |
| 109 | /* Create and initialize the object */ |
| 110 | if ((xp = PyObject_New(lad_t, &Ladtype)) == NULL) { |
| 111 | close(fd); |
| 112 | return NULL; |
| 113 | } |
| 114 | xp->x_fd = fd; |
| 115 | xp->x_icount = xp->x_ocount = 0; |
| 116 | xp->x_afmts = afmts; |
| 117 | return xp; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static void |
| 121 | lad_dealloc(lad_t *xp) |
| 122 | { |
Barry Warsaw | 4ddd820 | 2000-08-18 05:10:45 +0000 | [diff] [blame] | 123 | /* if already closed, don't reclose it */ |
| 124 | if (xp->x_fd != -1) |
| 125 | close(xp->x_fd); |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 126 | PyObject_Del(xp); |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | static PyObject * |
| 130 | lad_read(lad_t *self, PyObject *args) |
| 131 | { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 132 | int size, count; |
| 133 | char *cp; |
| 134 | PyObject *rv; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 135 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 136 | if (!PyArg_ParseTuple(args, "i:read", &size)) |
| 137 | return NULL; |
| 138 | rv = PyString_FromStringAndSize(NULL, size); |
| 139 | if (rv == NULL) |
| 140 | return NULL; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 141 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 142 | if (!(cp = PyString_AsString(rv))) { |
| 143 | Py_DECREF(rv); |
| 144 | return NULL; |
| 145 | } |
| 146 | if ((count = read(self->x_fd, cp, size)) < 0) { |
| 147 | PyErr_SetFromErrno(LinuxAudioError); |
| 148 | Py_DECREF(rv); |
| 149 | return NULL; |
| 150 | } |
| 151 | self->x_icount += count; |
| 152 | return rv; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static PyObject * |
| 156 | lad_write(lad_t *self, PyObject *args) |
| 157 | { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 158 | char *cp; |
| 159 | int rv, size; |
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, "s#:write", &cp, &size)) return NULL; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 162 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 163 | while (size > 0) { |
| 164 | if ((rv = write(self->x_fd, cp, size)) < 0) { |
| 165 | PyErr_SetFromErrno(LinuxAudioError); |
| 166 | return NULL; |
| 167 | } |
| 168 | self->x_ocount += rv; |
| 169 | size -= rv; |
| 170 | cp += rv; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 171 | } |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 172 | Py_INCREF(Py_None); |
| 173 | return Py_None; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | static PyObject * |
| 177 | lad_close(lad_t *self, PyObject *args) |
| 178 | { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 179 | if (!PyArg_ParseTuple(args, ":close")) return NULL; |
| 180 | if (self->x_fd >= 0) { |
| 181 | close(self->x_fd); |
| 182 | self->x_fd = -1; |
| 183 | } |
| 184 | Py_INCREF(Py_None); |
| 185 | return Py_None; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | static PyObject * |
| 189 | lad_fileno(lad_t *self, PyObject *args) |
| 190 | { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 191 | if (!PyArg_ParseTuple(args, ":fileno")) return NULL; |
| 192 | return PyInt_FromLong(self->x_fd); |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | static PyObject * |
| 196 | lad_setparameters(lad_t *self, PyObject *args) |
| 197 | { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 198 | int rate, ssize, nchannels, stereo, n, fmt; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 199 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 200 | if (!PyArg_ParseTuple(args, "iiii:setparameters", |
| 201 | &rate, &ssize, &nchannels, &fmt)) |
| 202 | return NULL; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 203 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 204 | if (rate < 0 || ssize < 0 || (nchannels != 1 && nchannels != 2)) { |
| 205 | PyErr_SetFromErrno(LinuxAudioError); |
| 206 | return NULL; |
| 207 | } |
| 208 | if (ioctl(self->x_fd, SOUND_PCM_WRITE_RATE, &rate) < 0) { |
| 209 | PyErr_SetFromErrno(LinuxAudioError); |
| 210 | return NULL; |
| 211 | } |
| 212 | if (ioctl(self->x_fd, SNDCTL_DSP_SAMPLESIZE, &ssize) < 0) { |
| 213 | PyErr_SetFromErrno(LinuxAudioError); |
| 214 | return NULL; |
| 215 | } |
| 216 | stereo = (nchannels == 1)? 0: (nchannels == 2)? 1: -1; |
| 217 | if (ioctl(self->x_fd, SNDCTL_DSP_STEREO, &stereo) < 0) { |
| 218 | PyErr_SetFromErrno(LinuxAudioError); |
| 219 | return NULL; |
| 220 | } |
| 221 | for (n = 0; n != sizeof(audio_types) / sizeof(audio_types[0]); n++) |
| 222 | if (fmt == audio_types[n].a_fmt) |
| 223 | break; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 224 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 225 | if (n == sizeof(audio_types) / sizeof(audio_types[0]) || |
| 226 | audio_types[n].a_bps != ssize || |
| 227 | (self->x_afmts & audio_types[n].a_fmt) == 0) { |
| 228 | PyErr_SetFromErrno(LinuxAudioError); |
| 229 | return NULL; |
| 230 | } |
| 231 | if (ioctl(self->x_fd, SNDCTL_DSP_SETFMT, &audio_types[n].a_fmt) < 0) { |
| 232 | PyErr_SetFromErrno(LinuxAudioError); |
| 233 | return NULL; |
| 234 | } |
| 235 | Py_INCREF(Py_None); |
| 236 | return Py_None; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | static int |
| 240 | _ssize(lad_t *self, int *nchannels, int *ssize) |
| 241 | { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 242 | int fmt; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 243 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 244 | fmt = 0; |
| 245 | if (ioctl(self->x_fd, SNDCTL_DSP_SETFMT, &fmt) < 0) |
| 246 | return -errno; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 247 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 248 | switch (fmt) { |
| 249 | case AFMT_MU_LAW: |
| 250 | case AFMT_A_LAW: |
| 251 | case AFMT_U8: |
| 252 | case AFMT_S8: |
| 253 | *ssize = sizeof(char); |
| 254 | break; |
| 255 | case AFMT_S16_LE: |
| 256 | case AFMT_S16_BE: |
| 257 | case AFMT_U16_LE: |
| 258 | case AFMT_U16_BE: |
| 259 | *ssize = sizeof(short); |
| 260 | break; |
| 261 | case AFMT_MPEG: |
| 262 | case AFMT_IMA_ADPCM: |
| 263 | default: |
| 264 | return -EOPNOTSUPP; |
| 265 | } |
| 266 | *nchannels = 0; |
| 267 | if (ioctl(self->x_fd, SNDCTL_DSP_CHANNELS, nchannels) < 0) |
| 268 | return -errno; |
| 269 | return 0; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | |
| 273 | /* bufsize returns the size of the hardware audio buffer in number |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 274 | of samples */ |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 275 | static PyObject * |
| 276 | lad_bufsize(lad_t *self, PyObject *args) |
| 277 | { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 278 | audio_buf_info ai; |
| 279 | int nchannels, ssize; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 280 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 281 | if (!PyArg_ParseTuple(args, ":bufsize")) return NULL; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 282 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 283 | if (_ssize(self, &nchannels, &ssize) < 0) { |
| 284 | PyErr_SetFromErrno(LinuxAudioError); |
| 285 | return NULL; |
| 286 | } |
| 287 | if (ioctl(self->x_fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) { |
| 288 | PyErr_SetFromErrno(LinuxAudioError); |
| 289 | return NULL; |
| 290 | } |
| 291 | return PyInt_FromLong((ai.fragstotal * ai.fragsize) / (nchannels * ssize)); |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /* obufcount returns the number of samples that are available in the |
| 295 | hardware for playing */ |
| 296 | static PyObject * |
| 297 | lad_obufcount(lad_t *self, PyObject *args) |
| 298 | { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 299 | audio_buf_info ai; |
| 300 | int nchannels, ssize; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 301 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 302 | if (!PyArg_ParseTuple(args, ":obufcount")) |
| 303 | return NULL; |
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 | if (_ssize(self, &nchannels, &ssize) < 0) { |
| 306 | PyErr_SetFromErrno(LinuxAudioError); |
| 307 | return NULL; |
| 308 | } |
| 309 | if (ioctl(self->x_fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) { |
| 310 | PyErr_SetFromErrno(LinuxAudioError); |
| 311 | return NULL; |
| 312 | } |
| 313 | return PyInt_FromLong((ai.fragstotal * ai.fragsize - ai.bytes) / |
| 314 | (ssize * nchannels)); |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | /* obufcount returns the number of samples that can be played without |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 318 | blocking */ |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 319 | static PyObject * |
| 320 | lad_obuffree(lad_t *self, PyObject *args) |
| 321 | { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 322 | audio_buf_info ai; |
| 323 | int nchannels, ssize; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 324 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 325 | if (!PyArg_ParseTuple(args, ":obuffree")) |
| 326 | return NULL; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 327 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 328 | if (_ssize(self, &nchannels, &ssize) < 0) { |
| 329 | PyErr_SetFromErrno(LinuxAudioError); |
| 330 | return NULL; |
| 331 | } |
| 332 | if (ioctl(self->x_fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) { |
| 333 | PyErr_SetFromErrno(LinuxAudioError); |
| 334 | return NULL; |
| 335 | } |
| 336 | return PyInt_FromLong(ai.bytes / (ssize * nchannels)); |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | /* Flush the device */ |
| 340 | static PyObject * |
| 341 | lad_flush(lad_t *self, PyObject *args) |
| 342 | { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 343 | if (!PyArg_ParseTuple(args, ":flush")) 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 (ioctl(self->x_fd, SNDCTL_DSP_SYNC, NULL) < 0) { |
| 346 | PyErr_SetFromErrno(LinuxAudioError); |
| 347 | return NULL; |
| 348 | } |
| 349 | Py_INCREF(Py_None); |
| 350 | return Py_None; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | static PyMethodDef lad_methods[] = { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 354 | { "read", (PyCFunction)lad_read, METH_VARARGS }, |
| 355 | { "write", (PyCFunction)lad_write, METH_VARARGS }, |
| 356 | { "setparameters", (PyCFunction)lad_setparameters, METH_VARARGS }, |
| 357 | { "bufsize", (PyCFunction)lad_bufsize, METH_VARARGS }, |
| 358 | { "obufcount", (PyCFunction)lad_obufcount, METH_VARARGS }, |
| 359 | { "obuffree", (PyCFunction)lad_obuffree, METH_VARARGS }, |
| 360 | { "flush", (PyCFunction)lad_flush, METH_VARARGS }, |
| 361 | { "close", (PyCFunction)lad_close, METH_VARARGS }, |
| 362 | { "fileno", (PyCFunction)lad_fileno, METH_VARARGS }, |
| 363 | { NULL, NULL} /* sentinel */ |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 364 | }; |
| 365 | |
| 366 | static PyObject * |
| 367 | lad_getattr(lad_t *xp, char *name) |
| 368 | { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 369 | return Py_FindMethod(lad_methods, (PyObject *)xp, name); |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | static PyTypeObject Ladtype = { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 373 | PyObject_HEAD_INIT(&PyType_Type) |
| 374 | 0, /*ob_size*/ |
| 375 | "linux_audio_device", /*tp_name*/ |
| 376 | sizeof(lad_t), /*tp_size*/ |
| 377 | 0, /*tp_itemsize*/ |
| 378 | /* methods */ |
| 379 | (destructor)lad_dealloc, /*tp_dealloc*/ |
| 380 | 0, /*tp_print*/ |
| 381 | (getattrfunc)lad_getattr, /*tp_getattr*/ |
| 382 | 0, /*tp_setattr*/ |
| 383 | 0, /*tp_compare*/ |
| 384 | 0, /*tp_repr*/ |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 385 | }; |
| 386 | |
| 387 | static PyObject * |
| 388 | ladopen(PyObject *self, PyObject *args) |
| 389 | { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 390 | return (PyObject *)newladobject(args); |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | static PyMethodDef linuxaudiodev_methods[] = { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 394 | { "open", ladopen, METH_VARARGS }, |
| 395 | { 0, 0 }, |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 396 | }; |
| 397 | |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 398 | void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 399 | initlinuxaudiodev(void) |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 400 | { |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 401 | PyObject *m, *d, *x; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 402 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 403 | m = Py_InitModule("linuxaudiodev", linuxaudiodev_methods); |
| 404 | d = PyModule_GetDict(m); |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 405 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 406 | LinuxAudioError = PyErr_NewException("linuxaudiodev.error", NULL, NULL); |
| 407 | if (LinuxAudioError) |
| 408 | PyDict_SetItemString(d, "error", LinuxAudioError); |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 409 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 410 | x = PyInt_FromLong((long) AFMT_MU_LAW); |
| 411 | if (x == NULL || PyDict_SetItemString(d, "AFMT_MU_LAW", x) < 0) |
| 412 | goto error; |
| 413 | Py_DECREF(x); |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 414 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 415 | x = PyInt_FromLong((long) AFMT_U8); |
| 416 | if (x == NULL || PyDict_SetItemString(d, "AFMT_U8", x) < 0) |
| 417 | goto error; |
| 418 | Py_DECREF(x); |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 419 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 420 | x = PyInt_FromLong((long) AFMT_S8); |
| 421 | if (x == NULL || PyDict_SetItemString(d, "AFMT_S8", x) < 0) |
| 422 | goto error; |
| 423 | Py_DECREF(x); |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 424 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 425 | x = PyInt_FromLong((long) AFMT_U16_BE); |
| 426 | if (x == NULL || PyDict_SetItemString(d, "AFMT_U16_BE", x) < 0) |
| 427 | goto error; |
| 428 | Py_DECREF(x); |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 429 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 430 | x = PyInt_FromLong((long) AFMT_U16_LE); |
| 431 | if (x == NULL || PyDict_SetItemString(d, "AFMT_U16_LE", x) < 0) |
| 432 | goto error; |
| 433 | Py_DECREF(x); |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 434 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 435 | x = PyInt_FromLong((long) AFMT_S16_BE); |
| 436 | if (x == NULL || PyDict_SetItemString(d, "AFMT_S16_BE", x) < 0) |
| 437 | goto error; |
| 438 | Py_DECREF(x); |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 439 | |
Fred Drake | da940d8 | 2000-07-08 06:05:58 +0000 | [diff] [blame] | 440 | x = PyInt_FromLong((long) AFMT_S16_LE); |
| 441 | if (x == NULL || PyDict_SetItemString(d, "AFMT_S16_LE", x) < 0) |
| 442 | goto error; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 443 | |
Barry Warsaw | 9bfd2bf | 2000-09-01 09:01:32 +0000 | [diff] [blame] | 444 | error: |
Guido van Rossum | 7c55404 | 2000-09-01 15:35:12 +0000 | [diff] [blame] | 445 | Py_DECREF(x); |
Barry Warsaw | 9bfd2bf | 2000-09-01 09:01:32 +0000 | [diff] [blame] | 446 | return; |
Guido van Rossum | b130dc7 | 2000-03-30 23:25:49 +0000 | [diff] [blame] | 447 | } |