blob: fa2e441f6812e335c823e6e403bbb10aad14be19 [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
Guido van Rossumb130dc72000-03-30 23:25:49 +000019#ifdef HAVE_FCNTL_H
20#include <fcntl.h>
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +000021#else
22#define O_RDONLY 00
23#define O_WRONLY 01
Guido van Rossumb130dc72000-03-30 23:25:49 +000024#endif
25
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +000026
Guido van Rossumb130dc72000-03-30 23:25:49 +000027#include <sys/ioctl.h>
Jeremy Hyltona3895c02000-08-31 18:11:07 +000028#if defined(linux)
Guido van Rossumb130dc72000-03-30 23:25:49 +000029#include <linux/soundcard.h>
30
31typedef unsigned long uint32_t;
32
Jeremy Hyltona3895c02000-08-31 18:11:07 +000033#elif defined(__FreeBSD__)
34#include <machine/soundcard.h>
35
36#ifndef SNDCTL_DSP_CHANNELS
37#define SNDCTL_DSP_CHANNELS SOUND_PCM_WRITE_CHANNELS
38#endif
39
40#endif
41
Guido van Rossumb130dc72000-03-30 23:25:49 +000042typedef struct {
Fred Drakeda940d82000-07-08 06:05:58 +000043 PyObject_HEAD;
44 int x_fd; /* The open file */
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +000045 int x_mode; /* file mode */
Fred Drakeda940d82000-07-08 06:05:58 +000046 int x_icount; /* Input count */
47 int x_ocount; /* Output count */
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +000048 uint32_t x_afmts; /* Audio formats supported by hardware*/
Guido van Rossumb130dc72000-03-30 23:25:49 +000049} lad_t;
50
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +000051/* XXX several format defined in soundcard.h are not supported,
52 including _NE (native endian) options and S32 options
53*/
54
Guido van Rossumb130dc72000-03-30 23:25:49 +000055static struct {
Fred Drakeda940d82000-07-08 06:05:58 +000056 int a_bps;
57 uint32_t a_fmt;
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +000058 char *a_name;
Guido van Rossumb130dc72000-03-30 23:25:49 +000059} audio_types[] = {
Ka-Ping Yee27ac0d12001-01-15 22:21:39 +000060 { 8, AFMT_MU_LAW, "logarithmic mu-law 8-bit audio" },
61 { 8, AFMT_A_LAW, "logarithmic A-law 8-bit audio" },
62 { 8, AFMT_U8, "linear unsigned 8-bit audio" },
63 { 8, AFMT_S8, "linear signed 8-bit audio" },
64 { 16, AFMT_U16_BE, "linear unsigned 16-bit big-endian audio" },
65 { 16, AFMT_U16_LE, "linear unsigned 16-bit little-endian audio" },
66 { 16, AFMT_S16_BE, "linear signed 16-bit big-endian audio" },
67 { 16, AFMT_S16_LE, "linear signed 16-bit little-endian audio" },
Ka-Ping Yee7dfe6e32001-01-17 19:31:29 +000068 { 16, AFMT_S16_NE, "linear signed 16-bit native-endian audio" },
Guido van Rossumb130dc72000-03-30 23:25:49 +000069};
70
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +000071static int n_audio_types = sizeof(audio_types) / sizeof(audio_types[0]);
Guido van Rossumb130dc72000-03-30 23:25:49 +000072
73staticforward PyTypeObject Ladtype;
74
75static PyObject *LinuxAudioError;
76
77static lad_t *
78newladobject(PyObject *arg)
79{
Fred Drakeda940d82000-07-08 06:05:58 +000080 lad_t *xp;
81 int fd, afmts, imode;
82 char *mode;
83 char *basedev;
Guido van Rossumb130dc72000-03-30 23:25:49 +000084
Fred Drakeda940d82000-07-08 06:05:58 +000085 /* Check arg for r/w/rw */
86 if (!PyArg_ParseTuple(arg, "s:open", &mode)) return NULL;
87 if (strcmp(mode, "r") == 0)
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +000088 imode = O_RDONLY;
Fred Drakeda940d82000-07-08 06:05:58 +000089 else if (strcmp(mode, "w") == 0)
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +000090 imode = O_WRONLY;
Fred Drakeda940d82000-07-08 06:05:58 +000091 else {
Ka-Ping Yee27ac0d12001-01-15 22:21:39 +000092 PyErr_SetString(LinuxAudioError, "mode should be 'r' or 'w'");
Fred Drakeda940d82000-07-08 06:05:58 +000093 return NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +000094 }
Guido van Rossumb130dc72000-03-30 23:25:49 +000095
Fred Drakeda940d82000-07-08 06:05:58 +000096 /* Open the correct device. The base device name comes from the
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +000097 * AUDIODEV environment variable first, then /dev/dsp. The
Fred Drakeda940d82000-07-08 06:05:58 +000098 * control device tacks "ctl" onto the base device name.
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +000099 *
100 * Note that the only difference between /dev/audio and /dev/dsp
101 * is that the former uses logarithmic mu-law encoding and the
102 * latter uses 8-bit unsigned encoding.
Fred Drakeda940d82000-07-08 06:05:58 +0000103 */
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000104
Fred Drakeda940d82000-07-08 06:05:58 +0000105 basedev = getenv("AUDIODEV");
106 if (!basedev)
107 basedev = "/dev/dsp";
Guido van Rossumb130dc72000-03-30 23:25:49 +0000108
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000109 if ((fd = open(basedev, imode)) == -1) {
Fred Drakeda940d82000-07-08 06:05:58 +0000110 PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev);
111 return NULL;
112 }
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000113 if (imode == O_WRONLY && ioctl(fd, SNDCTL_DSP_NONBLOCK, NULL) == -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 (ioctl(fd, SNDCTL_DSP_GETFMTS, &afmts) == -1) {
Fred Drakeda940d82000-07-08 06:05:58 +0000118 PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev);
119 return NULL;
120 }
121 /* Create and initialize the object */
122 if ((xp = PyObject_New(lad_t, &Ladtype)) == NULL) {
123 close(fd);
124 return NULL;
125 }
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000126 xp->x_fd = fd;
127 xp->x_mode = imode;
Fred Drakeda940d82000-07-08 06:05:58 +0000128 xp->x_icount = xp->x_ocount = 0;
129 xp->x_afmts = afmts;
130 return xp;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000131}
132
133static void
134lad_dealloc(lad_t *xp)
135{
Barry Warsaw4ddd8202000-08-18 05:10:45 +0000136 /* if already closed, don't reclose it */
137 if (xp->x_fd != -1)
138 close(xp->x_fd);
Fred Drakeda940d82000-07-08 06:05:58 +0000139 PyObject_Del(xp);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000140}
141
142static PyObject *
143lad_read(lad_t *self, PyObject *args)
144{
Fred Drakeda940d82000-07-08 06:05:58 +0000145 int size, count;
146 char *cp;
147 PyObject *rv;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000148
Fred Drakeda940d82000-07-08 06:05:58 +0000149 if (!PyArg_ParseTuple(args, "i:read", &size))
150 return NULL;
151 rv = PyString_FromStringAndSize(NULL, size);
152 if (rv == NULL)
153 return NULL;
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000154 cp = PyString_AS_STRING(rv);
Fred Drakeda940d82000-07-08 06:05:58 +0000155 if ((count = read(self->x_fd, cp, size)) < 0) {
156 PyErr_SetFromErrno(LinuxAudioError);
157 Py_DECREF(rv);
158 return NULL;
159 }
160 self->x_icount += count;
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000161 if (_PyString_Resize(&rv, count) == -1)
162 return NULL;
Fred Drakeda940d82000-07-08 06:05:58 +0000163 return rv;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000164}
165
166static PyObject *
167lad_write(lad_t *self, PyObject *args)
168{
Fred Drakeda940d82000-07-08 06:05:58 +0000169 char *cp;
170 int rv, size;
Guido van Rossumf5bd6842001-04-02 17:59:02 +0000171 fd_set write_set_fds;
172 struct timeval tv;
173 int select_retval;
174
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000175 if (!PyArg_ParseTuple(args, "s#:write", &cp, &size))
176 return NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000177
Guido van Rossumf5bd6842001-04-02 17:59:02 +0000178 /* use select to wait for audio device to be available */
179 FD_ZERO(&write_set_fds);
180 FD_SET(self->x_fd, &write_set_fds);
181 tv.tv_sec = 4; /* timeout values */
182 tv.tv_usec = 0;
183
Fred Drakeda940d82000-07-08 06:05:58 +0000184 while (size > 0) {
Guido van Rossumf5bd6842001-04-02 17:59:02 +0000185 select_retval = select(self->x_fd+1, NULL, &write_set_fds, NULL, &tv);
186 tv.tv_sec = 1; tv.tv_usec = 0; /* willing to wait this long next time*/
187 if (select_retval) {
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000188 if ((rv = write(self->x_fd, cp, size)) == -1) {
Guido van Rossumf5bd6842001-04-02 17:59:02 +0000189 if (errno != EAGAIN) {
190 PyErr_SetFromErrno(LinuxAudioError);
191 return NULL;
192 } else {
193 errno = 0; /* EAGAIN: buffer is full, try again */
194 }
195 } else {
196 self->x_ocount += rv;
197 size -= rv;
198 cp += rv;
199 }
200 } else {
201 /* printf("Not able to write to linux audio device within %ld seconds\n", tv.tv_sec); */
202 PyErr_SetFromErrno(LinuxAudioError);
203 return NULL;
204 }
Guido van Rossumb130dc72000-03-30 23:25:49 +0000205 }
Fred Drakeda940d82000-07-08 06:05:58 +0000206 Py_INCREF(Py_None);
207 return Py_None;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000208}
209
210static PyObject *
211lad_close(lad_t *self, PyObject *args)
212{
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000213 if (!PyArg_ParseTuple(args, ":close"))
214 return NULL;
215
Fred Drakeda940d82000-07-08 06:05:58 +0000216 if (self->x_fd >= 0) {
217 close(self->x_fd);
218 self->x_fd = -1;
219 }
220 Py_INCREF(Py_None);
221 return Py_None;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000222}
223
224static PyObject *
225lad_fileno(lad_t *self, PyObject *args)
226{
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000227 if (!PyArg_ParseTuple(args, ":fileno"))
228 return NULL;
Fred Drakeda940d82000-07-08 06:05:58 +0000229 return PyInt_FromLong(self->x_fd);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000230}
231
232static PyObject *
233lad_setparameters(lad_t *self, PyObject *args)
234{
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000235 int rate, ssize, nchannels, n, fmt, emulate=0;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000236
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000237 if (!PyArg_ParseTuple(args, "iiii|i:setparameters",
238 &rate, &ssize, &nchannels, &fmt, &emulate))
Fred Drakeda940d82000-07-08 06:05:58 +0000239 return NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000240
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000241 if (rate < 0) {
242 PyErr_Format(PyExc_ValueError, "expected rate >= 0, not %d",
243 rate);
244 return NULL;
245 }
246 if (ssize < 0) {
247 PyErr_Format(PyExc_ValueError, "expected sample size >= 0, not %d",
248 ssize);
249 return NULL;
250 }
251 if (nchannels != 1 && nchannels != 2) {
252 PyErr_Format(PyExc_ValueError, "nchannels must be 1 or 2, not %d",
253 nchannels);
254 return NULL;
255 }
256
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000257 for (n = 0; n < n_audio_types; n++)
Fred Drakeda940d82000-07-08 06:05:58 +0000258 if (fmt == audio_types[n].a_fmt)
259 break;
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000260 if (n == n_audio_types) {
261 PyErr_Format(PyExc_ValueError, "unknown audio encoding: %d", fmt);
262 return NULL;
263 }
264 if (audio_types[n].a_bps != ssize) {
265 PyErr_Format(PyExc_ValueError,
Ka-Ping Yee27ac0d12001-01-15 22:21:39 +0000266 "for %s, expected sample size %d, not %d",
267 audio_types[n].a_name, audio_types[n].a_bps, ssize);
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000268 return NULL;
269 }
Guido van Rossumb130dc72000-03-30 23:25:49 +0000270
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000271 if (emulate == 0) {
272 if ((self->x_afmts & audio_types[n].a_fmt) == 0) {
273 PyErr_Format(PyExc_ValueError,
Ka-Ping Yee27ac0d12001-01-15 22:21:39 +0000274 "%s format not supported by device",
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000275 audio_types[n].a_name);
276 return NULL;
277 }
278 }
279 if (ioctl(self->x_fd, SNDCTL_DSP_SETFMT,
280 &audio_types[n].a_fmt) == -1) {
Fred Drakeda940d82000-07-08 06:05:58 +0000281 PyErr_SetFromErrno(LinuxAudioError);
282 return NULL;
283 }
Guido van Rossum77452182001-12-08 17:13:45 +0000284 if (ioctl(self->x_fd, SNDCTL_DSP_CHANNELS, &nchannels) == -1) {
285 PyErr_SetFromErrno(LinuxAudioError);
286 return NULL;
287 }
288 if (ioctl(self->x_fd, SNDCTL_DSP_SPEED, &rate) == -1) {
289 PyErr_SetFromErrno(LinuxAudioError);
290 return NULL;
291 }
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000292
Fred Drakeda940d82000-07-08 06:05:58 +0000293 Py_INCREF(Py_None);
294 return Py_None;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000295}
296
297static int
298_ssize(lad_t *self, int *nchannels, int *ssize)
299{
Fred Drakeda940d82000-07-08 06:05:58 +0000300 int fmt;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000301
Fred Drakeda940d82000-07-08 06:05:58 +0000302 fmt = 0;
303 if (ioctl(self->x_fd, SNDCTL_DSP_SETFMT, &fmt) < 0)
304 return -errno;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000305
Fred Drakeda940d82000-07-08 06:05:58 +0000306 switch (fmt) {
307 case AFMT_MU_LAW:
308 case AFMT_A_LAW:
309 case AFMT_U8:
310 case AFMT_S8:
311 *ssize = sizeof(char);
312 break;
313 case AFMT_S16_LE:
314 case AFMT_S16_BE:
315 case AFMT_U16_LE:
316 case AFMT_U16_BE:
317 *ssize = sizeof(short);
318 break;
319 case AFMT_MPEG:
320 case AFMT_IMA_ADPCM:
321 default:
322 return -EOPNOTSUPP;
323 }
324 *nchannels = 0;
325 if (ioctl(self->x_fd, SNDCTL_DSP_CHANNELS, nchannels) < 0)
326 return -errno;
327 return 0;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000328}
329
330
331/* bufsize returns the size of the hardware audio buffer in number
Fred Drakeda940d82000-07-08 06:05:58 +0000332 of samples */
Guido van Rossumb130dc72000-03-30 23:25:49 +0000333static PyObject *
334lad_bufsize(lad_t *self, PyObject *args)
335{
Fred Drakeda940d82000-07-08 06:05:58 +0000336 audio_buf_info ai;
337 int nchannels, ssize;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000338
Fred Drakeda940d82000-07-08 06:05:58 +0000339 if (!PyArg_ParseTuple(args, ":bufsize")) return NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000340
Fred Drakeda940d82000-07-08 06:05:58 +0000341 if (_ssize(self, &nchannels, &ssize) < 0) {
342 PyErr_SetFromErrno(LinuxAudioError);
343 return NULL;
344 }
345 if (ioctl(self->x_fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
346 PyErr_SetFromErrno(LinuxAudioError);
347 return NULL;
348 }
349 return PyInt_FromLong((ai.fragstotal * ai.fragsize) / (nchannels * ssize));
Guido van Rossumb130dc72000-03-30 23:25:49 +0000350}
351
352/* obufcount returns the number of samples that are available in the
353 hardware for playing */
354static PyObject *
355lad_obufcount(lad_t *self, PyObject *args)
356{
Fred Drakeda940d82000-07-08 06:05:58 +0000357 audio_buf_info ai;
358 int nchannels, ssize;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000359
Fred Drakeda940d82000-07-08 06:05:58 +0000360 if (!PyArg_ParseTuple(args, ":obufcount"))
361 return NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000362
Fred Drakeda940d82000-07-08 06:05:58 +0000363 if (_ssize(self, &nchannels, &ssize) < 0) {
364 PyErr_SetFromErrno(LinuxAudioError);
365 return NULL;
366 }
367 if (ioctl(self->x_fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
368 PyErr_SetFromErrno(LinuxAudioError);
369 return NULL;
370 }
371 return PyInt_FromLong((ai.fragstotal * ai.fragsize - ai.bytes) /
372 (ssize * nchannels));
Guido van Rossumb130dc72000-03-30 23:25:49 +0000373}
374
375/* obufcount returns the number of samples that can be played without
Fred Drakeda940d82000-07-08 06:05:58 +0000376 blocking */
Guido van Rossumb130dc72000-03-30 23:25:49 +0000377static PyObject *
378lad_obuffree(lad_t *self, PyObject *args)
379{
Fred Drakeda940d82000-07-08 06:05:58 +0000380 audio_buf_info ai;
381 int nchannels, ssize;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000382
Fred Drakeda940d82000-07-08 06:05:58 +0000383 if (!PyArg_ParseTuple(args, ":obuffree"))
384 return NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000385
Fred Drakeda940d82000-07-08 06:05:58 +0000386 if (_ssize(self, &nchannels, &ssize) < 0) {
387 PyErr_SetFromErrno(LinuxAudioError);
388 return NULL;
389 }
390 if (ioctl(self->x_fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
391 PyErr_SetFromErrno(LinuxAudioError);
392 return NULL;
393 }
394 return PyInt_FromLong(ai.bytes / (ssize * nchannels));
Guido van Rossumb130dc72000-03-30 23:25:49 +0000395}
396
397/* Flush the device */
398static PyObject *
399lad_flush(lad_t *self, PyObject *args)
400{
Fred Drakeda940d82000-07-08 06:05:58 +0000401 if (!PyArg_ParseTuple(args, ":flush")) return NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000402
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000403 if (ioctl(self->x_fd, SNDCTL_DSP_SYNC, NULL) == -1) {
Fred Drakeda940d82000-07-08 06:05:58 +0000404 PyErr_SetFromErrno(LinuxAudioError);
405 return NULL;
406 }
407 Py_INCREF(Py_None);
408 return Py_None;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000409}
410
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000411static PyObject *
412lad_getptr(lad_t *self, PyObject *args)
413{
414 count_info info;
415 int req;
416
417 if (!PyArg_ParseTuple(args, ":getptr"))
418 return NULL;
419
420 if (self->x_mode == O_RDONLY)
421 req = SNDCTL_DSP_GETIPTR;
422 else
423 req = SNDCTL_DSP_GETOPTR;
424 if (ioctl(self->x_fd, req, &info) == -1) {
425 PyErr_SetFromErrno(LinuxAudioError);
426 return NULL;
427 }
428 return Py_BuildValue("iii", info.bytes, info.blocks, info.ptr);
429}
430
Guido van Rossumb130dc72000-03-30 23:25:49 +0000431static PyMethodDef lad_methods[] = {
Fred Drakeda940d82000-07-08 06:05:58 +0000432 { "read", (PyCFunction)lad_read, METH_VARARGS },
433 { "write", (PyCFunction)lad_write, METH_VARARGS },
434 { "setparameters", (PyCFunction)lad_setparameters, METH_VARARGS },
435 { "bufsize", (PyCFunction)lad_bufsize, METH_VARARGS },
436 { "obufcount", (PyCFunction)lad_obufcount, METH_VARARGS },
437 { "obuffree", (PyCFunction)lad_obuffree, METH_VARARGS },
438 { "flush", (PyCFunction)lad_flush, METH_VARARGS },
439 { "close", (PyCFunction)lad_close, METH_VARARGS },
440 { "fileno", (PyCFunction)lad_fileno, METH_VARARGS },
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000441 { "getptr", (PyCFunction)lad_getptr, METH_VARARGS },
Fred Drakeda940d82000-07-08 06:05:58 +0000442 { NULL, NULL} /* sentinel */
Guido van Rossumb130dc72000-03-30 23:25:49 +0000443};
444
445static PyObject *
446lad_getattr(lad_t *xp, char *name)
447{
Fred Drakeda940d82000-07-08 06:05:58 +0000448 return Py_FindMethod(lad_methods, (PyObject *)xp, name);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000449}
450
451static PyTypeObject Ladtype = {
Fred Drakeda940d82000-07-08 06:05:58 +0000452 PyObject_HEAD_INIT(&PyType_Type)
453 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +0000454 "linuxaudiodev.linux_audio_device", /*tp_name*/
Fred Drakeda940d82000-07-08 06:05:58 +0000455 sizeof(lad_t), /*tp_size*/
456 0, /*tp_itemsize*/
457 /* methods */
458 (destructor)lad_dealloc, /*tp_dealloc*/
459 0, /*tp_print*/
460 (getattrfunc)lad_getattr, /*tp_getattr*/
461 0, /*tp_setattr*/
462 0, /*tp_compare*/
463 0, /*tp_repr*/
Guido van Rossumb130dc72000-03-30 23:25:49 +0000464};
465
466static PyObject *
467ladopen(PyObject *self, PyObject *args)
468{
Fred Drakeda940d82000-07-08 06:05:58 +0000469 return (PyObject *)newladobject(args);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000470}
471
472static PyMethodDef linuxaudiodev_methods[] = {
Fred Drakeda940d82000-07-08 06:05:58 +0000473 { "open", ladopen, METH_VARARGS },
474 { 0, 0 },
Guido van Rossumb130dc72000-03-30 23:25:49 +0000475};
476
Guido van Rossumb130dc72000-03-30 23:25:49 +0000477void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000478initlinuxaudiodev(void)
Guido van Rossumb130dc72000-03-30 23:25:49 +0000479{
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000480 PyObject *m;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000481
Fred Drakeda940d82000-07-08 06:05:58 +0000482 m = Py_InitModule("linuxaudiodev", linuxaudiodev_methods);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000483
Fred Drakeda940d82000-07-08 06:05:58 +0000484 LinuxAudioError = PyErr_NewException("linuxaudiodev.error", NULL, NULL);
485 if (LinuxAudioError)
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000486 PyModule_AddObject(m, "error", LinuxAudioError);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000487
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000488 if (PyModule_AddIntConstant(m, "AFMT_MU_LAW", (long)AFMT_MU_LAW) == -1)
489 return;
490 if (PyModule_AddIntConstant(m, "AFMT_A_LAW", (long)AFMT_A_LAW) == -1)
491 return;
492 if (PyModule_AddIntConstant(m, "AFMT_U8", (long)AFMT_U8) == -1)
493 return;
494 if (PyModule_AddIntConstant(m, "AFMT_S8", (long)AFMT_S8) == -1)
495 return;
496 if (PyModule_AddIntConstant(m, "AFMT_U16_BE", (long)AFMT_U16_BE) == -1)
497 return;
498 if (PyModule_AddIntConstant(m, "AFMT_U16_LE", (long)AFMT_U16_LE) == -1)
499 return;
500 if (PyModule_AddIntConstant(m, "AFMT_S16_BE", (long)AFMT_S16_BE) == -1)
501 return;
502 if (PyModule_AddIntConstant(m, "AFMT_S16_LE", (long)AFMT_S16_LE) == -1)
503 return;
Guido van Rossum59316672000-10-08 19:47:47 +0000504 if (PyModule_AddIntConstant(m, "AFMT_S16_NE", (long)AFMT_S16_NE) == -1)
505 return;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000506
Barry Warsaw9bfd2bf2000-09-01 09:01:32 +0000507 return;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000508}