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