blob: dd462efbf12370ae3f2e8b2e617deee005cf7f5f [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
Guido van Rossumb130dc72000-03-30 23:25:49 +00007 * 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 Hyltone2b7c4d2000-10-06 19:39:55 +000025#else
26#define O_RDONLY 00
27#define O_WRONLY 01
Guido van Rossumb130dc72000-03-30 23:25:49 +000028#endif
29
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +000030
Guido van Rossumb130dc72000-03-30 23:25:49 +000031#include <sys/ioctl.h>
Jeremy Hyltona3895c02000-08-31 18:11:07 +000032#if defined(linux)
Guido van Rossumb130dc72000-03-30 23:25:49 +000033#include <linux/soundcard.h>
34
35typedef unsigned long uint32_t;
36
Jeremy Hyltona3895c02000-08-31 18:11:07 +000037#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 Rossumb130dc72000-03-30 23:25:49 +000046typedef struct {
Fred Drakeda940d82000-07-08 06:05:58 +000047 PyObject_HEAD;
48 int x_fd; /* The open file */
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +000049 int x_mode; /* file mode */
Fred Drakeda940d82000-07-08 06:05:58 +000050 int x_icount; /* Input count */
51 int x_ocount; /* Output count */
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +000052 uint32_t x_afmts; /* Audio formats supported by hardware*/
Guido van Rossumb130dc72000-03-30 23:25:49 +000053} lad_t;
54
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +000055/* XXX several format defined in soundcard.h are not supported,
56 including _NE (native endian) options and S32 options
57*/
58
Guido van Rossumb130dc72000-03-30 23:25:49 +000059static struct {
Fred Drakeda940d82000-07-08 06:05:58 +000060 int a_bps;
61 uint32_t a_fmt;
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +000062 char *a_name;
Guido van Rossumb130dc72000-03-30 23:25:49 +000063} audio_types[] = {
Ka-Ping Yee27ac0d12001-01-15 22:21:39 +000064 { 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 Yee7dfe6e32001-01-17 19:31:29 +000072 { 16, AFMT_S16_NE, "linear signed 16-bit native-endian audio" },
Guido van Rossumb130dc72000-03-30 23:25:49 +000073};
74
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +000075static int n_audio_types = sizeof(audio_types) / sizeof(audio_types[0]);
Guido van Rossumb130dc72000-03-30 23:25:49 +000076
77staticforward PyTypeObject Ladtype;
78
79static PyObject *LinuxAudioError;
80
81static lad_t *
82newladobject(PyObject *arg)
83{
Fred Drakeda940d82000-07-08 06:05:58 +000084 lad_t *xp;
85 int fd, afmts, imode;
86 char *mode;
87 char *basedev;
Guido van Rossumb130dc72000-03-30 23:25:49 +000088
Fred Drakeda940d82000-07-08 06:05:58 +000089 /* Check arg for r/w/rw */
90 if (!PyArg_ParseTuple(arg, "s:open", &mode)) return NULL;
91 if (strcmp(mode, "r") == 0)
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +000092 imode = O_RDONLY;
Fred Drakeda940d82000-07-08 06:05:58 +000093 else if (strcmp(mode, "w") == 0)
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +000094 imode = O_WRONLY;
Fred Drakeda940d82000-07-08 06:05:58 +000095 else {
Ka-Ping Yee27ac0d12001-01-15 22:21:39 +000096 PyErr_SetString(LinuxAudioError, "mode should be 'r' or 'w'");
Fred Drakeda940d82000-07-08 06:05:58 +000097 return NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +000098 }
Guido van Rossumb130dc72000-03-30 23:25:49 +000099
Fred Drakeda940d82000-07-08 06:05:58 +0000100 /* Open the correct device. The base device name comes from the
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000101 * AUDIODEV environment variable first, then /dev/dsp. The
Fred Drakeda940d82000-07-08 06:05:58 +0000102 * control device tacks "ctl" onto the base device name.
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000103 *
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 Drakeda940d82000-07-08 06:05:58 +0000107 */
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000108
Fred Drakeda940d82000-07-08 06:05:58 +0000109 basedev = getenv("AUDIODEV");
110 if (!basedev)
111 basedev = "/dev/dsp";
Guido van Rossumb130dc72000-03-30 23:25:49 +0000112
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000113 if ((fd = open(basedev, imode)) == -1) {
Fred Drakeda940d82000-07-08 06:05:58 +0000114 PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev);
115 return NULL;
116 }
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000117 if (imode == O_WRONLY && ioctl(fd, SNDCTL_DSP_NONBLOCK, NULL) == -1) {
Fred Drakeda940d82000-07-08 06:05:58 +0000118 PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev);
119 return NULL;
120 }
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000121 if (ioctl(fd, SNDCTL_DSP_GETFMTS, &afmts) == -1) {
Fred Drakeda940d82000-07-08 06:05:58 +0000122 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 Hyltone2b7c4d2000-10-06 19:39:55 +0000130 xp->x_fd = fd;
131 xp->x_mode = imode;
Fred Drakeda940d82000-07-08 06:05:58 +0000132 xp->x_icount = xp->x_ocount = 0;
133 xp->x_afmts = afmts;
134 return xp;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000135}
136
137static void
138lad_dealloc(lad_t *xp)
139{
Barry Warsaw4ddd8202000-08-18 05:10:45 +0000140 /* if already closed, don't reclose it */
141 if (xp->x_fd != -1)
142 close(xp->x_fd);
Fred Drakeda940d82000-07-08 06:05:58 +0000143 PyObject_Del(xp);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000144}
145
146static PyObject *
147lad_read(lad_t *self, PyObject *args)
148{
Fred Drakeda940d82000-07-08 06:05:58 +0000149 int size, count;
150 char *cp;
151 PyObject *rv;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000152
Fred Drakeda940d82000-07-08 06:05:58 +0000153 if (!PyArg_ParseTuple(args, "i:read", &size))
154 return NULL;
155 rv = PyString_FromStringAndSize(NULL, size);
156 if (rv == NULL)
157 return NULL;
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000158 cp = PyString_AS_STRING(rv);
Fred Drakeda940d82000-07-08 06:05:58 +0000159 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 Hyltone2b7c4d2000-10-06 19:39:55 +0000165 if (_PyString_Resize(&rv, count) == -1)
166 return NULL;
Fred Drakeda940d82000-07-08 06:05:58 +0000167 return rv;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000168}
169
170static PyObject *
171lad_write(lad_t *self, PyObject *args)
172{
Fred Drakeda940d82000-07-08 06:05:58 +0000173 char *cp;
174 int rv, size;
Guido van Rossumf5bd6842001-04-02 17:59:02 +0000175 fd_set write_set_fds;
176 struct timeval tv;
177 int select_retval;
178
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000179 if (!PyArg_ParseTuple(args, "s#:write", &cp, &size))
180 return NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000181
Guido van Rossumf5bd6842001-04-02 17:59:02 +0000182 /* 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 Drakeda940d82000-07-08 06:05:58 +0000188 while (size > 0) {
Guido van Rossumf5bd6842001-04-02 17:59:02 +0000189 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 Hyltone2b7c4d2000-10-06 19:39:55 +0000192 if ((rv = write(self->x_fd, cp, size)) == -1) {
Guido van Rossumf5bd6842001-04-02 17:59:02 +0000193 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 Rossumb130dc72000-03-30 23:25:49 +0000209 }
Fred Drakeda940d82000-07-08 06:05:58 +0000210 Py_INCREF(Py_None);
211 return Py_None;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000212}
213
214static PyObject *
215lad_close(lad_t *self, PyObject *args)
216{
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000217 if (!PyArg_ParseTuple(args, ":close"))
218 return NULL;
219
Fred Drakeda940d82000-07-08 06:05:58 +0000220 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 Rossumb130dc72000-03-30 23:25:49 +0000226}
227
228static PyObject *
229lad_fileno(lad_t *self, PyObject *args)
230{
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000231 if (!PyArg_ParseTuple(args, ":fileno"))
232 return NULL;
Fred Drakeda940d82000-07-08 06:05:58 +0000233 return PyInt_FromLong(self->x_fd);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000234}
235
236static PyObject *
237lad_setparameters(lad_t *self, PyObject *args)
238{
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000239 int rate, ssize, nchannels, n, fmt, emulate=0;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000240
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000241 if (!PyArg_ParseTuple(args, "iiii|i:setparameters",
242 &rate, &ssize, &nchannels, &fmt, &emulate))
Fred Drakeda940d82000-07-08 06:05:58 +0000243 return NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000244
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000245 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 Hyltone2b7c4d2000-10-06 19:39:55 +0000261 for (n = 0; n < n_audio_types; n++)
Fred Drakeda940d82000-07-08 06:05:58 +0000262 if (fmt == audio_types[n].a_fmt)
263 break;
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000264 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 Yee27ac0d12001-01-15 22:21:39 +0000270 "for %s, expected sample size %d, not %d",
271 audio_types[n].a_name, audio_types[n].a_bps, ssize);
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000272 return NULL;
273 }
Guido van Rossumb130dc72000-03-30 23:25:49 +0000274
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000275 if (emulate == 0) {
276 if ((self->x_afmts & audio_types[n].a_fmt) == 0) {
277 PyErr_Format(PyExc_ValueError,
Ka-Ping Yee27ac0d12001-01-15 22:21:39 +0000278 "%s format not supported by device",
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000279 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 Drakeda940d82000-07-08 06:05:58 +0000285 PyErr_SetFromErrno(LinuxAudioError);
286 return NULL;
287 }
Guido van Rossum77452182001-12-08 17:13:45 +0000288 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 Hyltone2b7c4d2000-10-06 19:39:55 +0000296
Fred Drakeda940d82000-07-08 06:05:58 +0000297 Py_INCREF(Py_None);
298 return Py_None;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000299}
300
301static int
302_ssize(lad_t *self, int *nchannels, int *ssize)
303{
Fred Drakeda940d82000-07-08 06:05:58 +0000304 int fmt;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000305
Fred Drakeda940d82000-07-08 06:05:58 +0000306 fmt = 0;
307 if (ioctl(self->x_fd, SNDCTL_DSP_SETFMT, &fmt) < 0)
308 return -errno;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000309
Fred Drakeda940d82000-07-08 06:05:58 +0000310 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 Rossumb130dc72000-03-30 23:25:49 +0000332}
333
334
335/* bufsize returns the size of the hardware audio buffer in number
Fred Drakeda940d82000-07-08 06:05:58 +0000336 of samples */
Guido van Rossumb130dc72000-03-30 23:25:49 +0000337static PyObject *
338lad_bufsize(lad_t *self, PyObject *args)
339{
Fred Drakeda940d82000-07-08 06:05:58 +0000340 audio_buf_info ai;
341 int nchannels, ssize;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000342
Fred Drakeda940d82000-07-08 06:05:58 +0000343 if (!PyArg_ParseTuple(args, ":bufsize")) return NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000344
Fred Drakeda940d82000-07-08 06:05:58 +0000345 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 Rossumb130dc72000-03-30 23:25:49 +0000354}
355
356/* obufcount returns the number of samples that are available in the
357 hardware for playing */
358static PyObject *
359lad_obufcount(lad_t *self, PyObject *args)
360{
Fred Drakeda940d82000-07-08 06:05:58 +0000361 audio_buf_info ai;
362 int nchannels, ssize;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000363
Fred Drakeda940d82000-07-08 06:05:58 +0000364 if (!PyArg_ParseTuple(args, ":obufcount"))
365 return NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000366
Fred Drakeda940d82000-07-08 06:05:58 +0000367 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 Rossumb130dc72000-03-30 23:25:49 +0000377}
378
379/* obufcount returns the number of samples that can be played without
Fred Drakeda940d82000-07-08 06:05:58 +0000380 blocking */
Guido van Rossumb130dc72000-03-30 23:25:49 +0000381static PyObject *
382lad_obuffree(lad_t *self, PyObject *args)
383{
Fred Drakeda940d82000-07-08 06:05:58 +0000384 audio_buf_info ai;
385 int nchannels, ssize;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000386
Fred Drakeda940d82000-07-08 06:05:58 +0000387 if (!PyArg_ParseTuple(args, ":obuffree"))
388 return NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000389
Fred Drakeda940d82000-07-08 06:05:58 +0000390 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 Rossumb130dc72000-03-30 23:25:49 +0000399}
400
401/* Flush the device */
402static PyObject *
403lad_flush(lad_t *self, PyObject *args)
404{
Fred Drakeda940d82000-07-08 06:05:58 +0000405 if (!PyArg_ParseTuple(args, ":flush")) return NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000406
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000407 if (ioctl(self->x_fd, SNDCTL_DSP_SYNC, NULL) == -1) {
Fred Drakeda940d82000-07-08 06:05:58 +0000408 PyErr_SetFromErrno(LinuxAudioError);
409 return NULL;
410 }
411 Py_INCREF(Py_None);
412 return Py_None;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000413}
414
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000415static PyObject *
416lad_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 Rossumb130dc72000-03-30 23:25:49 +0000435static PyMethodDef lad_methods[] = {
Fred Drakeda940d82000-07-08 06:05:58 +0000436 { "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 Hyltone2b7c4d2000-10-06 19:39:55 +0000445 { "getptr", (PyCFunction)lad_getptr, METH_VARARGS },
Fred Drakeda940d82000-07-08 06:05:58 +0000446 { NULL, NULL} /* sentinel */
Guido van Rossumb130dc72000-03-30 23:25:49 +0000447};
448
449static PyObject *
450lad_getattr(lad_t *xp, char *name)
451{
Fred Drakeda940d82000-07-08 06:05:58 +0000452 return Py_FindMethod(lad_methods, (PyObject *)xp, name);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000453}
454
455static PyTypeObject Ladtype = {
Fred Drakeda940d82000-07-08 06:05:58 +0000456 PyObject_HEAD_INIT(&PyType_Type)
457 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +0000458 "linuxaudiodev.linux_audio_device", /*tp_name*/
Fred Drakeda940d82000-07-08 06:05:58 +0000459 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 Rossumb130dc72000-03-30 23:25:49 +0000468};
469
470static PyObject *
471ladopen(PyObject *self, PyObject *args)
472{
Fred Drakeda940d82000-07-08 06:05:58 +0000473 return (PyObject *)newladobject(args);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000474}
475
476static PyMethodDef linuxaudiodev_methods[] = {
Fred Drakeda940d82000-07-08 06:05:58 +0000477 { "open", ladopen, METH_VARARGS },
478 { 0, 0 },
Guido van Rossumb130dc72000-03-30 23:25:49 +0000479};
480
Guido van Rossumb130dc72000-03-30 23:25:49 +0000481void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000482initlinuxaudiodev(void)
Guido van Rossumb130dc72000-03-30 23:25:49 +0000483{
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000484 PyObject *m;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000485
Fred Drakeda940d82000-07-08 06:05:58 +0000486 m = Py_InitModule("linuxaudiodev", linuxaudiodev_methods);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000487
Fred Drakeda940d82000-07-08 06:05:58 +0000488 LinuxAudioError = PyErr_NewException("linuxaudiodev.error", NULL, NULL);
489 if (LinuxAudioError)
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000490 PyModule_AddObject(m, "error", LinuxAudioError);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000491
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000492 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 Rossum59316672000-10-08 19:47:47 +0000508 if (PyModule_AddIntConstant(m, "AFMT_S16_NE", (long)AFMT_S16_NE) == -1)
509 return;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000510
Barry Warsaw9bfd2bf2000-09-01 09:01:32 +0000511 return;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000512}