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