blob: a0661c5ac7f24ea13d565ab4681e67a45346192b [file] [log] [blame]
Guido van Rossumb130dc72000-03-30 23:25:49 +00001/* 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>
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +000027#else
28#define O_RDONLY 00
29#define O_WRONLY 01
Guido van Rossumb130dc72000-03-30 23:25:49 +000030#endif
31
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +000032
Guido van Rossumb130dc72000-03-30 23:25:49 +000033#include <sys/ioctl.h>
Jeremy Hyltona3895c02000-08-31 18:11:07 +000034#if defined(linux)
Guido van Rossumb130dc72000-03-30 23:25:49 +000035#include <linux/soundcard.h>
36
37typedef unsigned long uint32_t;
38
Jeremy Hyltona3895c02000-08-31 18:11:07 +000039#elif defined(__FreeBSD__)
40#include <machine/soundcard.h>
41
42#ifndef SNDCTL_DSP_CHANNELS
43#define SNDCTL_DSP_CHANNELS SOUND_PCM_WRITE_CHANNELS
44#endif
45
46#endif
47
Guido van Rossumb130dc72000-03-30 23:25:49 +000048typedef struct {
Fred Drakeda940d82000-07-08 06:05:58 +000049 PyObject_HEAD;
50 int x_fd; /* The open file */
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +000051 int x_mode; /* file mode */
Fred Drakeda940d82000-07-08 06:05:58 +000052 int x_icount; /* Input count */
53 int x_ocount; /* Output count */
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +000054 uint32_t x_afmts; /* Audio formats supported by hardware*/
Guido van Rossumb130dc72000-03-30 23:25:49 +000055} lad_t;
56
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +000057/* XXX several format defined in soundcard.h are not supported,
58 including _NE (native endian) options and S32 options
59*/
60
Guido van Rossumb130dc72000-03-30 23:25:49 +000061static struct {
Fred Drakeda940d82000-07-08 06:05:58 +000062 int a_bps;
63 uint32_t a_fmt;
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +000064 char *a_name;
Guido van Rossumb130dc72000-03-30 23:25:49 +000065} audio_types[] = {
Ka-Ping Yee27ac0d12001-01-15 22:21:39 +000066 { 8, AFMT_MU_LAW, "logarithmic mu-law 8-bit audio" },
67 { 8, AFMT_A_LAW, "logarithmic A-law 8-bit audio" },
68 { 8, AFMT_U8, "linear unsigned 8-bit audio" },
69 { 8, AFMT_S8, "linear signed 8-bit audio" },
70 { 16, AFMT_U16_BE, "linear unsigned 16-bit big-endian audio" },
71 { 16, AFMT_U16_LE, "linear unsigned 16-bit little-endian audio" },
72 { 16, AFMT_S16_BE, "linear signed 16-bit big-endian audio" },
73 { 16, AFMT_S16_LE, "linear signed 16-bit little-endian audio" },
Ka-Ping Yee7dfe6e32001-01-17 19:31:29 +000074 { 16, AFMT_S16_NE, "linear signed 16-bit native-endian audio" },
Guido van Rossumb130dc72000-03-30 23:25:49 +000075};
76
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +000077static int n_audio_types = sizeof(audio_types) / sizeof(audio_types[0]);
Guido van Rossumb130dc72000-03-30 23:25:49 +000078
79staticforward PyTypeObject Ladtype;
80
81static PyObject *LinuxAudioError;
82
83static lad_t *
84newladobject(PyObject *arg)
85{
Fred Drakeda940d82000-07-08 06:05:58 +000086 lad_t *xp;
87 int fd, afmts, imode;
88 char *mode;
89 char *basedev;
Guido van Rossumb130dc72000-03-30 23:25:49 +000090
Fred Drakeda940d82000-07-08 06:05:58 +000091 /* Check arg for r/w/rw */
92 if (!PyArg_ParseTuple(arg, "s:open", &mode)) return NULL;
93 if (strcmp(mode, "r") == 0)
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +000094 imode = O_RDONLY;
Fred Drakeda940d82000-07-08 06:05:58 +000095 else if (strcmp(mode, "w") == 0)
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +000096 imode = O_WRONLY;
Fred Drakeda940d82000-07-08 06:05:58 +000097 else {
Ka-Ping Yee27ac0d12001-01-15 22:21:39 +000098 PyErr_SetString(LinuxAudioError, "mode should be 'r' or 'w'");
Fred Drakeda940d82000-07-08 06:05:58 +000099 return NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000100 }
Guido van Rossumb130dc72000-03-30 23:25:49 +0000101
Fred Drakeda940d82000-07-08 06:05:58 +0000102 /* Open the correct device. The base device name comes from the
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000103 * AUDIODEV environment variable first, then /dev/dsp. The
Fred Drakeda940d82000-07-08 06:05:58 +0000104 * control device tacks "ctl" onto the base device name.
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000105 *
106 * Note that the only difference between /dev/audio and /dev/dsp
107 * is that the former uses logarithmic mu-law encoding and the
108 * latter uses 8-bit unsigned encoding.
Fred Drakeda940d82000-07-08 06:05:58 +0000109 */
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000110
Fred Drakeda940d82000-07-08 06:05:58 +0000111 basedev = getenv("AUDIODEV");
112 if (!basedev)
113 basedev = "/dev/dsp";
Guido van Rossumb130dc72000-03-30 23:25:49 +0000114
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000115 if ((fd = open(basedev, imode)) == -1) {
Fred Drakeda940d82000-07-08 06:05:58 +0000116 PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev);
117 return NULL;
118 }
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000119 if (imode == O_WRONLY && ioctl(fd, SNDCTL_DSP_NONBLOCK, NULL) == -1) {
Fred Drakeda940d82000-07-08 06:05:58 +0000120 PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev);
121 return NULL;
122 }
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000123 if (ioctl(fd, SNDCTL_DSP_GETFMTS, &afmts) == -1) {
Fred Drakeda940d82000-07-08 06:05:58 +0000124 PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev);
125 return NULL;
126 }
127 /* Create and initialize the object */
128 if ((xp = PyObject_New(lad_t, &Ladtype)) == NULL) {
129 close(fd);
130 return NULL;
131 }
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000132 xp->x_fd = fd;
133 xp->x_mode = imode;
Fred Drakeda940d82000-07-08 06:05:58 +0000134 xp->x_icount = xp->x_ocount = 0;
135 xp->x_afmts = afmts;
136 return xp;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000137}
138
139static void
140lad_dealloc(lad_t *xp)
141{
Barry Warsaw4ddd8202000-08-18 05:10:45 +0000142 /* if already closed, don't reclose it */
143 if (xp->x_fd != -1)
144 close(xp->x_fd);
Fred Drakeda940d82000-07-08 06:05:58 +0000145 PyObject_Del(xp);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000146}
147
148static PyObject *
149lad_read(lad_t *self, PyObject *args)
150{
Fred Drakeda940d82000-07-08 06:05:58 +0000151 int size, count;
152 char *cp;
153 PyObject *rv;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000154
Fred Drakeda940d82000-07-08 06:05:58 +0000155 if (!PyArg_ParseTuple(args, "i:read", &size))
156 return NULL;
157 rv = PyString_FromStringAndSize(NULL, size);
158 if (rv == NULL)
159 return NULL;
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000160 cp = PyString_AS_STRING(rv);
Fred Drakeda940d82000-07-08 06:05:58 +0000161 if ((count = read(self->x_fd, cp, size)) < 0) {
162 PyErr_SetFromErrno(LinuxAudioError);
163 Py_DECREF(rv);
164 return NULL;
165 }
166 self->x_icount += count;
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000167 if (_PyString_Resize(&rv, count) == -1)
168 return NULL;
Fred Drakeda940d82000-07-08 06:05:58 +0000169 return rv;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000170}
171
172static PyObject *
173lad_write(lad_t *self, PyObject *args)
174{
Fred Drakeda940d82000-07-08 06:05:58 +0000175 char *cp;
176 int rv, size;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000177
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000178 if (!PyArg_ParseTuple(args, "s#:write", &cp, &size))
179 return NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000180
Fred Drakeda940d82000-07-08 06:05:58 +0000181 while (size > 0) {
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000182 if ((rv = write(self->x_fd, cp, size)) == -1) {
Fred Drakeda940d82000-07-08 06:05:58 +0000183 PyErr_SetFromErrno(LinuxAudioError);
184 return NULL;
185 }
186 self->x_ocount += rv;
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000187 size -= rv;
188 cp += rv;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000189 }
Fred Drakeda940d82000-07-08 06:05:58 +0000190 Py_INCREF(Py_None);
191 return Py_None;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000192}
193
194static PyObject *
195lad_close(lad_t *self, PyObject *args)
196{
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000197 if (!PyArg_ParseTuple(args, ":close"))
198 return NULL;
199
Fred Drakeda940d82000-07-08 06:05:58 +0000200 if (self->x_fd >= 0) {
201 close(self->x_fd);
202 self->x_fd = -1;
203 }
204 Py_INCREF(Py_None);
205 return Py_None;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000206}
207
208static PyObject *
209lad_fileno(lad_t *self, PyObject *args)
210{
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000211 if (!PyArg_ParseTuple(args, ":fileno"))
212 return NULL;
Fred Drakeda940d82000-07-08 06:05:58 +0000213 return PyInt_FromLong(self->x_fd);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000214}
215
216static PyObject *
217lad_setparameters(lad_t *self, PyObject *args)
218{
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000219 int rate, ssize, nchannels, n, fmt, emulate=0;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000220
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000221 if (!PyArg_ParseTuple(args, "iiii|i:setparameters",
222 &rate, &ssize, &nchannels, &fmt, &emulate))
Fred Drakeda940d82000-07-08 06:05:58 +0000223 return NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000224
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000225 if (rate < 0) {
226 PyErr_Format(PyExc_ValueError, "expected rate >= 0, not %d",
227 rate);
228 return NULL;
229 }
230 if (ssize < 0) {
231 PyErr_Format(PyExc_ValueError, "expected sample size >= 0, not %d",
232 ssize);
233 return NULL;
234 }
235 if (nchannels != 1 && nchannels != 2) {
236 PyErr_Format(PyExc_ValueError, "nchannels must be 1 or 2, not %d",
237 nchannels);
238 return NULL;
239 }
240
241 if (ioctl(self->x_fd, SNDCTL_DSP_SPEED, &rate) == -1) {
Fred Drakeda940d82000-07-08 06:05:58 +0000242 PyErr_SetFromErrno(LinuxAudioError);
243 return NULL;
244 }
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000245 if (ioctl(self->x_fd, SNDCTL_DSP_CHANNELS, &nchannels) == -1) {
Fred Drakeda940d82000-07-08 06:05:58 +0000246 PyErr_SetFromErrno(LinuxAudioError);
247 return NULL;
248 }
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000249
250 for (n = 0; n < n_audio_types; n++)
Fred Drakeda940d82000-07-08 06:05:58 +0000251 if (fmt == audio_types[n].a_fmt)
252 break;
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000253 if (n == n_audio_types) {
254 PyErr_Format(PyExc_ValueError, "unknown audio encoding: %d", fmt);
255 return NULL;
256 }
257 if (audio_types[n].a_bps != ssize) {
258 PyErr_Format(PyExc_ValueError,
Ka-Ping Yee27ac0d12001-01-15 22:21:39 +0000259 "for %s, expected sample size %d, not %d",
260 audio_types[n].a_name, audio_types[n].a_bps, ssize);
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000261 return NULL;
262 }
Guido van Rossumb130dc72000-03-30 23:25:49 +0000263
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000264 if (emulate == 0) {
265 if ((self->x_afmts & audio_types[n].a_fmt) == 0) {
266 PyErr_Format(PyExc_ValueError,
Ka-Ping Yee27ac0d12001-01-15 22:21:39 +0000267 "%s format not supported by device",
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000268 audio_types[n].a_name);
269 return NULL;
270 }
271 }
272 if (ioctl(self->x_fd, SNDCTL_DSP_SETFMT,
273 &audio_types[n].a_fmt) == -1) {
Fred Drakeda940d82000-07-08 06:05:58 +0000274 PyErr_SetFromErrno(LinuxAudioError);
275 return NULL;
276 }
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000277
Fred Drakeda940d82000-07-08 06:05:58 +0000278 Py_INCREF(Py_None);
279 return Py_None;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000280}
281
282static int
283_ssize(lad_t *self, int *nchannels, int *ssize)
284{
Fred Drakeda940d82000-07-08 06:05:58 +0000285 int fmt;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000286
Fred Drakeda940d82000-07-08 06:05:58 +0000287 fmt = 0;
288 if (ioctl(self->x_fd, SNDCTL_DSP_SETFMT, &fmt) < 0)
289 return -errno;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000290
Fred Drakeda940d82000-07-08 06:05:58 +0000291 switch (fmt) {
292 case AFMT_MU_LAW:
293 case AFMT_A_LAW:
294 case AFMT_U8:
295 case AFMT_S8:
296 *ssize = sizeof(char);
297 break;
298 case AFMT_S16_LE:
299 case AFMT_S16_BE:
300 case AFMT_U16_LE:
301 case AFMT_U16_BE:
302 *ssize = sizeof(short);
303 break;
304 case AFMT_MPEG:
305 case AFMT_IMA_ADPCM:
306 default:
307 return -EOPNOTSUPP;
308 }
309 *nchannels = 0;
310 if (ioctl(self->x_fd, SNDCTL_DSP_CHANNELS, nchannels) < 0)
311 return -errno;
312 return 0;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000313}
314
315
316/* bufsize returns the size of the hardware audio buffer in number
Fred Drakeda940d82000-07-08 06:05:58 +0000317 of samples */
Guido van Rossumb130dc72000-03-30 23:25:49 +0000318static PyObject *
319lad_bufsize(lad_t *self, PyObject *args)
320{
Fred Drakeda940d82000-07-08 06:05:58 +0000321 audio_buf_info ai;
322 int nchannels, ssize;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000323
Fred Drakeda940d82000-07-08 06:05:58 +0000324 if (!PyArg_ParseTuple(args, ":bufsize")) return NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000325
Fred Drakeda940d82000-07-08 06:05:58 +0000326 if (_ssize(self, &nchannels, &ssize) < 0) {
327 PyErr_SetFromErrno(LinuxAudioError);
328 return NULL;
329 }
330 if (ioctl(self->x_fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
331 PyErr_SetFromErrno(LinuxAudioError);
332 return NULL;
333 }
334 return PyInt_FromLong((ai.fragstotal * ai.fragsize) / (nchannels * ssize));
Guido van Rossumb130dc72000-03-30 23:25:49 +0000335}
336
337/* obufcount returns the number of samples that are available in the
338 hardware for playing */
339static PyObject *
340lad_obufcount(lad_t *self, PyObject *args)
341{
Fred Drakeda940d82000-07-08 06:05:58 +0000342 audio_buf_info ai;
343 int nchannels, ssize;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000344
Fred Drakeda940d82000-07-08 06:05:58 +0000345 if (!PyArg_ParseTuple(args, ":obufcount"))
346 return NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000347
Fred Drakeda940d82000-07-08 06:05:58 +0000348 if (_ssize(self, &nchannels, &ssize) < 0) {
349 PyErr_SetFromErrno(LinuxAudioError);
350 return NULL;
351 }
352 if (ioctl(self->x_fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
353 PyErr_SetFromErrno(LinuxAudioError);
354 return NULL;
355 }
356 return PyInt_FromLong((ai.fragstotal * ai.fragsize - ai.bytes) /
357 (ssize * nchannels));
Guido van Rossumb130dc72000-03-30 23:25:49 +0000358}
359
360/* obufcount returns the number of samples that can be played without
Fred Drakeda940d82000-07-08 06:05:58 +0000361 blocking */
Guido van Rossumb130dc72000-03-30 23:25:49 +0000362static PyObject *
363lad_obuffree(lad_t *self, PyObject *args)
364{
Fred Drakeda940d82000-07-08 06:05:58 +0000365 audio_buf_info ai;
366 int nchannels, ssize;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000367
Fred Drakeda940d82000-07-08 06:05:58 +0000368 if (!PyArg_ParseTuple(args, ":obuffree"))
369 return NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000370
Fred Drakeda940d82000-07-08 06:05:58 +0000371 if (_ssize(self, &nchannels, &ssize) < 0) {
372 PyErr_SetFromErrno(LinuxAudioError);
373 return NULL;
374 }
375 if (ioctl(self->x_fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
376 PyErr_SetFromErrno(LinuxAudioError);
377 return NULL;
378 }
379 return PyInt_FromLong(ai.bytes / (ssize * nchannels));
Guido van Rossumb130dc72000-03-30 23:25:49 +0000380}
381
382/* Flush the device */
383static PyObject *
384lad_flush(lad_t *self, PyObject *args)
385{
Fred Drakeda940d82000-07-08 06:05:58 +0000386 if (!PyArg_ParseTuple(args, ":flush")) return NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000387
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000388 if (ioctl(self->x_fd, SNDCTL_DSP_SYNC, NULL) == -1) {
Fred Drakeda940d82000-07-08 06:05:58 +0000389 PyErr_SetFromErrno(LinuxAudioError);
390 return NULL;
391 }
392 Py_INCREF(Py_None);
393 return Py_None;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000394}
395
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000396static PyObject *
397lad_getptr(lad_t *self, PyObject *args)
398{
399 count_info info;
400 int req;
401
402 if (!PyArg_ParseTuple(args, ":getptr"))
403 return NULL;
404
405 if (self->x_mode == O_RDONLY)
406 req = SNDCTL_DSP_GETIPTR;
407 else
408 req = SNDCTL_DSP_GETOPTR;
409 if (ioctl(self->x_fd, req, &info) == -1) {
410 PyErr_SetFromErrno(LinuxAudioError);
411 return NULL;
412 }
413 return Py_BuildValue("iii", info.bytes, info.blocks, info.ptr);
414}
415
Guido van Rossumb130dc72000-03-30 23:25:49 +0000416static PyMethodDef lad_methods[] = {
Fred Drakeda940d82000-07-08 06:05:58 +0000417 { "read", (PyCFunction)lad_read, METH_VARARGS },
418 { "write", (PyCFunction)lad_write, METH_VARARGS },
419 { "setparameters", (PyCFunction)lad_setparameters, METH_VARARGS },
420 { "bufsize", (PyCFunction)lad_bufsize, METH_VARARGS },
421 { "obufcount", (PyCFunction)lad_obufcount, METH_VARARGS },
422 { "obuffree", (PyCFunction)lad_obuffree, METH_VARARGS },
423 { "flush", (PyCFunction)lad_flush, METH_VARARGS },
424 { "close", (PyCFunction)lad_close, METH_VARARGS },
425 { "fileno", (PyCFunction)lad_fileno, METH_VARARGS },
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000426 { "getptr", (PyCFunction)lad_getptr, METH_VARARGS },
Fred Drakeda940d82000-07-08 06:05:58 +0000427 { NULL, NULL} /* sentinel */
Guido van Rossumb130dc72000-03-30 23:25:49 +0000428};
429
430static PyObject *
431lad_getattr(lad_t *xp, char *name)
432{
Fred Drakeda940d82000-07-08 06:05:58 +0000433 return Py_FindMethod(lad_methods, (PyObject *)xp, name);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000434}
435
436static PyTypeObject Ladtype = {
Fred Drakeda940d82000-07-08 06:05:58 +0000437 PyObject_HEAD_INIT(&PyType_Type)
438 0, /*ob_size*/
439 "linux_audio_device", /*tp_name*/
440 sizeof(lad_t), /*tp_size*/
441 0, /*tp_itemsize*/
442 /* methods */
443 (destructor)lad_dealloc, /*tp_dealloc*/
444 0, /*tp_print*/
445 (getattrfunc)lad_getattr, /*tp_getattr*/
446 0, /*tp_setattr*/
447 0, /*tp_compare*/
448 0, /*tp_repr*/
Guido van Rossumb130dc72000-03-30 23:25:49 +0000449};
450
451static PyObject *
452ladopen(PyObject *self, PyObject *args)
453{
Fred Drakeda940d82000-07-08 06:05:58 +0000454 return (PyObject *)newladobject(args);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000455}
456
457static PyMethodDef linuxaudiodev_methods[] = {
Fred Drakeda940d82000-07-08 06:05:58 +0000458 { "open", ladopen, METH_VARARGS },
459 { 0, 0 },
Guido van Rossumb130dc72000-03-30 23:25:49 +0000460};
461
Guido van Rossumb130dc72000-03-30 23:25:49 +0000462void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000463initlinuxaudiodev(void)
Guido van Rossumb130dc72000-03-30 23:25:49 +0000464{
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000465 PyObject *m;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000466
Fred Drakeda940d82000-07-08 06:05:58 +0000467 m = Py_InitModule("linuxaudiodev", linuxaudiodev_methods);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000468
Fred Drakeda940d82000-07-08 06:05:58 +0000469 LinuxAudioError = PyErr_NewException("linuxaudiodev.error", NULL, NULL);
470 if (LinuxAudioError)
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000471 PyModule_AddObject(m, "error", LinuxAudioError);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000472
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000473 if (PyModule_AddIntConstant(m, "AFMT_MU_LAW", (long)AFMT_MU_LAW) == -1)
474 return;
475 if (PyModule_AddIntConstant(m, "AFMT_A_LAW", (long)AFMT_A_LAW) == -1)
476 return;
477 if (PyModule_AddIntConstant(m, "AFMT_U8", (long)AFMT_U8) == -1)
478 return;
479 if (PyModule_AddIntConstant(m, "AFMT_S8", (long)AFMT_S8) == -1)
480 return;
481 if (PyModule_AddIntConstant(m, "AFMT_U16_BE", (long)AFMT_U16_BE) == -1)
482 return;
483 if (PyModule_AddIntConstant(m, "AFMT_U16_LE", (long)AFMT_U16_LE) == -1)
484 return;
485 if (PyModule_AddIntConstant(m, "AFMT_S16_BE", (long)AFMT_S16_BE) == -1)
486 return;
487 if (PyModule_AddIntConstant(m, "AFMT_S16_LE", (long)AFMT_S16_LE) == -1)
488 return;
Guido van Rossum59316672000-10-08 19:47:47 +0000489 if (PyModule_AddIntConstant(m, "AFMT_S16_NE", (long)AFMT_S16_NE) == -1)
490 return;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000491
Barry Warsaw9bfd2bf2000-09-01 09:01:32 +0000492 return;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000493}