blob: 5aac51a6d923e689702b464c0900bf27c0efdf7a [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
Jeremy Hylton938ace62002-07-17 16:30:39 +000073static PyTypeObject Ladtype;
Guido van Rossumb130dc72000-03-30 23:25:49 +000074
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;
Greg Warda34b1a02002-11-27 22:19:15 +000082 char *basedev = NULL;
83 char *mode = NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +000084
Greg Warda34b1a02002-11-27 22:19:15 +000085 /* Two ways to call linuxaudiodev.open():
86 open(device, mode) (for consistency with builtin open())
87 open(mode) (for backwards compatibility)
88 because the *first* argument is optional, parsing args is
89 a wee bit tricky. */
90 if (!PyArg_ParseTuple(arg, "s|s:open", &basedev, &mode))
91 return NULL;
92 if (mode == NULL) { /* only one arg supplied */
93 mode = basedev;
94 basedev = NULL;
95 }
96
Fred Drakeda940d82000-07-08 06:05:58 +000097 if (strcmp(mode, "r") == 0)
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +000098 imode = O_RDONLY;
Fred Drakeda940d82000-07-08 06:05:58 +000099 else if (strcmp(mode, "w") == 0)
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000100 imode = O_WRONLY;
Fred Drakeda940d82000-07-08 06:05:58 +0000101 else {
Ka-Ping Yee27ac0d12001-01-15 22:21:39 +0000102 PyErr_SetString(LinuxAudioError, "mode should be 'r' or 'w'");
Fred Drakeda940d82000-07-08 06:05:58 +0000103 return NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000104 }
Guido van Rossumb130dc72000-03-30 23:25:49 +0000105
Fred Drakeda940d82000-07-08 06:05:58 +0000106 /* Open the correct device. The base device name comes from the
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000107 * AUDIODEV environment variable first, then /dev/dsp. The
Fred Drakeda940d82000-07-08 06:05:58 +0000108 * control device tacks "ctl" onto the base device name.
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000109 *
110 * Note that the only difference between /dev/audio and /dev/dsp
111 * is that the former uses logarithmic mu-law encoding and the
112 * latter uses 8-bit unsigned encoding.
Fred Drakeda940d82000-07-08 06:05:58 +0000113 */
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000114
Greg Warda34b1a02002-11-27 22:19:15 +0000115 if (basedev == NULL) { /* called with one arg */
116 basedev = getenv("AUDIODEV");
117 if (basedev == NULL) /* $AUDIODEV not set */
118 basedev = "/dev/dsp";
119 }
Guido van Rossumb130dc72000-03-30 23:25:49 +0000120
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000121 if ((fd = open(basedev, imode)) == -1) {
Fred Drakeda940d82000-07-08 06:05:58 +0000122 PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev);
123 return NULL;
124 }
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000125 if (imode == O_WRONLY && ioctl(fd, SNDCTL_DSP_NONBLOCK, NULL) == -1) {
Fred Drakeda940d82000-07-08 06:05:58 +0000126 PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev);
127 return NULL;
128 }
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000129 if (ioctl(fd, SNDCTL_DSP_GETFMTS, &afmts) == -1) {
Fred Drakeda940d82000-07-08 06:05:58 +0000130 PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev);
131 return NULL;
132 }
133 /* Create and initialize the object */
134 if ((xp = PyObject_New(lad_t, &Ladtype)) == NULL) {
135 close(fd);
136 return NULL;
137 }
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000138 xp->x_fd = fd;
139 xp->x_mode = imode;
Fred Drakeda940d82000-07-08 06:05:58 +0000140 xp->x_icount = xp->x_ocount = 0;
141 xp->x_afmts = afmts;
142 return xp;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000143}
144
145static void
146lad_dealloc(lad_t *xp)
147{
Barry Warsaw4ddd8202000-08-18 05:10:45 +0000148 /* if already closed, don't reclose it */
149 if (xp->x_fd != -1)
150 close(xp->x_fd);
Fred Drakeda940d82000-07-08 06:05:58 +0000151 PyObject_Del(xp);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000152}
153
154static PyObject *
155lad_read(lad_t *self, PyObject *args)
156{
Fred Drakeda940d82000-07-08 06:05:58 +0000157 int size, count;
158 char *cp;
159 PyObject *rv;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000160
Fred Drakeda940d82000-07-08 06:05:58 +0000161 if (!PyArg_ParseTuple(args, "i:read", &size))
162 return NULL;
163 rv = PyString_FromStringAndSize(NULL, size);
164 if (rv == NULL)
165 return NULL;
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000166 cp = PyString_AS_STRING(rv);
Fred Drakeda940d82000-07-08 06:05:58 +0000167 if ((count = read(self->x_fd, cp, size)) < 0) {
168 PyErr_SetFromErrno(LinuxAudioError);
169 Py_DECREF(rv);
170 return NULL;
171 }
172 self->x_icount += count;
Tim Peters5de98422002-04-27 18:44:32 +0000173 _PyString_Resize(&rv, count);
Fred Drakeda940d82000-07-08 06:05:58 +0000174 return rv;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000175}
176
177static PyObject *
178lad_write(lad_t *self, PyObject *args)
179{
Fred Drakeda940d82000-07-08 06:05:58 +0000180 char *cp;
181 int rv, size;
Guido van Rossumf5bd6842001-04-02 17:59:02 +0000182 fd_set write_set_fds;
183 struct timeval tv;
184 int select_retval;
185
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000186 if (!PyArg_ParseTuple(args, "s#:write", &cp, &size))
187 return NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000188
Guido van Rossumf5bd6842001-04-02 17:59:02 +0000189 /* use select to wait for audio device to be available */
190 FD_ZERO(&write_set_fds);
191 FD_SET(self->x_fd, &write_set_fds);
192 tv.tv_sec = 4; /* timeout values */
193 tv.tv_usec = 0;
194
Fred Drakeda940d82000-07-08 06:05:58 +0000195 while (size > 0) {
Guido van Rossumf5bd6842001-04-02 17:59:02 +0000196 select_retval = select(self->x_fd+1, NULL, &write_set_fds, NULL, &tv);
197 tv.tv_sec = 1; tv.tv_usec = 0; /* willing to wait this long next time*/
198 if (select_retval) {
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000199 if ((rv = write(self->x_fd, cp, size)) == -1) {
Guido van Rossumf5bd6842001-04-02 17:59:02 +0000200 if (errno != EAGAIN) {
201 PyErr_SetFromErrno(LinuxAudioError);
202 return NULL;
203 } else {
204 errno = 0; /* EAGAIN: buffer is full, try again */
205 }
206 } else {
207 self->x_ocount += rv;
208 size -= rv;
209 cp += rv;
210 }
211 } else {
212 /* printf("Not able to write to linux audio device within %ld seconds\n", tv.tv_sec); */
213 PyErr_SetFromErrno(LinuxAudioError);
214 return NULL;
215 }
Guido van Rossumb130dc72000-03-30 23:25:49 +0000216 }
Fred Drakeda940d82000-07-08 06:05:58 +0000217 Py_INCREF(Py_None);
218 return Py_None;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000219}
220
221static PyObject *
222lad_close(lad_t *self, PyObject *args)
223{
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000224 if (!PyArg_ParseTuple(args, ":close"))
225 return NULL;
226
Fred Drakeda940d82000-07-08 06:05:58 +0000227 if (self->x_fd >= 0) {
228 close(self->x_fd);
229 self->x_fd = -1;
230 }
231 Py_INCREF(Py_None);
232 return Py_None;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000233}
234
235static PyObject *
236lad_fileno(lad_t *self, PyObject *args)
237{
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000238 if (!PyArg_ParseTuple(args, ":fileno"))
239 return NULL;
Fred Drakeda940d82000-07-08 06:05:58 +0000240 return PyInt_FromLong(self->x_fd);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000241}
242
243static PyObject *
244lad_setparameters(lad_t *self, PyObject *args)
245{
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000246 int rate, ssize, nchannels, n, fmt, emulate=0;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000247
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000248 if (!PyArg_ParseTuple(args, "iiii|i:setparameters",
249 &rate, &ssize, &nchannels, &fmt, &emulate))
Fred Drakeda940d82000-07-08 06:05:58 +0000250 return NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000251
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000252 if (rate < 0) {
253 PyErr_Format(PyExc_ValueError, "expected rate >= 0, not %d",
254 rate);
255 return NULL;
256 }
257 if (ssize < 0) {
258 PyErr_Format(PyExc_ValueError, "expected sample size >= 0, not %d",
259 ssize);
260 return NULL;
261 }
262 if (nchannels != 1 && nchannels != 2) {
263 PyErr_Format(PyExc_ValueError, "nchannels must be 1 or 2, not %d",
264 nchannels);
265 return NULL;
266 }
267
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000268 for (n = 0; n < n_audio_types; n++)
Fred Drakeda940d82000-07-08 06:05:58 +0000269 if (fmt == audio_types[n].a_fmt)
270 break;
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000271 if (n == n_audio_types) {
272 PyErr_Format(PyExc_ValueError, "unknown audio encoding: %d", fmt);
273 return NULL;
274 }
275 if (audio_types[n].a_bps != ssize) {
276 PyErr_Format(PyExc_ValueError,
Ka-Ping Yee27ac0d12001-01-15 22:21:39 +0000277 "for %s, expected sample size %d, not %d",
278 audio_types[n].a_name, audio_types[n].a_bps, ssize);
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000279 return NULL;
280 }
Guido van Rossumb130dc72000-03-30 23:25:49 +0000281
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000282 if (emulate == 0) {
283 if ((self->x_afmts & audio_types[n].a_fmt) == 0) {
284 PyErr_Format(PyExc_ValueError,
Ka-Ping Yee27ac0d12001-01-15 22:21:39 +0000285 "%s format not supported by device",
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000286 audio_types[n].a_name);
287 return NULL;
288 }
289 }
290 if (ioctl(self->x_fd, SNDCTL_DSP_SETFMT,
291 &audio_types[n].a_fmt) == -1) {
Fred Drakeda940d82000-07-08 06:05:58 +0000292 PyErr_SetFromErrno(LinuxAudioError);
293 return NULL;
294 }
Guido van Rossum77452182001-12-08 17:13:45 +0000295 if (ioctl(self->x_fd, SNDCTL_DSP_CHANNELS, &nchannels) == -1) {
296 PyErr_SetFromErrno(LinuxAudioError);
297 return NULL;
298 }
299 if (ioctl(self->x_fd, SNDCTL_DSP_SPEED, &rate) == -1) {
300 PyErr_SetFromErrno(LinuxAudioError);
301 return NULL;
302 }
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000303
Fred Drakeda940d82000-07-08 06:05:58 +0000304 Py_INCREF(Py_None);
305 return Py_None;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000306}
307
308static int
309_ssize(lad_t *self, int *nchannels, int *ssize)
310{
Fred Drakeda940d82000-07-08 06:05:58 +0000311 int fmt;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000312
Fred Drakeda940d82000-07-08 06:05:58 +0000313 fmt = 0;
314 if (ioctl(self->x_fd, SNDCTL_DSP_SETFMT, &fmt) < 0)
315 return -errno;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000316
Fred Drakeda940d82000-07-08 06:05:58 +0000317 switch (fmt) {
318 case AFMT_MU_LAW:
319 case AFMT_A_LAW:
320 case AFMT_U8:
321 case AFMT_S8:
322 *ssize = sizeof(char);
323 break;
324 case AFMT_S16_LE:
325 case AFMT_S16_BE:
326 case AFMT_U16_LE:
327 case AFMT_U16_BE:
328 *ssize = sizeof(short);
329 break;
330 case AFMT_MPEG:
331 case AFMT_IMA_ADPCM:
332 default:
333 return -EOPNOTSUPP;
334 }
335 *nchannels = 0;
336 if (ioctl(self->x_fd, SNDCTL_DSP_CHANNELS, nchannels) < 0)
337 return -errno;
338 return 0;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000339}
340
341
342/* bufsize returns the size of the hardware audio buffer in number
Fred Drakeda940d82000-07-08 06:05:58 +0000343 of samples */
Guido van Rossumb130dc72000-03-30 23:25:49 +0000344static PyObject *
345lad_bufsize(lad_t *self, PyObject *args)
346{
Fred Drakeda940d82000-07-08 06:05:58 +0000347 audio_buf_info ai;
348 int nchannels, ssize;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000349
Fred Drakeda940d82000-07-08 06:05:58 +0000350 if (!PyArg_ParseTuple(args, ":bufsize")) return NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000351
Fred Drakeda940d82000-07-08 06:05:58 +0000352 if (_ssize(self, &nchannels, &ssize) < 0) {
353 PyErr_SetFromErrno(LinuxAudioError);
354 return NULL;
355 }
356 if (ioctl(self->x_fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
357 PyErr_SetFromErrno(LinuxAudioError);
358 return NULL;
359 }
360 return PyInt_FromLong((ai.fragstotal * ai.fragsize) / (nchannels * ssize));
Guido van Rossumb130dc72000-03-30 23:25:49 +0000361}
362
363/* obufcount returns the number of samples that are available in the
364 hardware for playing */
365static PyObject *
366lad_obufcount(lad_t *self, PyObject *args)
367{
Fred Drakeda940d82000-07-08 06:05:58 +0000368 audio_buf_info ai;
369 int nchannels, ssize;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000370
Fred Drakeda940d82000-07-08 06:05:58 +0000371 if (!PyArg_ParseTuple(args, ":obufcount"))
372 return NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000373
Fred Drakeda940d82000-07-08 06:05:58 +0000374 if (_ssize(self, &nchannels, &ssize) < 0) {
375 PyErr_SetFromErrno(LinuxAudioError);
376 return NULL;
377 }
378 if (ioctl(self->x_fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
379 PyErr_SetFromErrno(LinuxAudioError);
380 return NULL;
381 }
382 return PyInt_FromLong((ai.fragstotal * ai.fragsize - ai.bytes) /
383 (ssize * nchannels));
Guido van Rossumb130dc72000-03-30 23:25:49 +0000384}
385
386/* obufcount returns the number of samples that can be played without
Fred Drakeda940d82000-07-08 06:05:58 +0000387 blocking */
Guido van Rossumb130dc72000-03-30 23:25:49 +0000388static PyObject *
389lad_obuffree(lad_t *self, PyObject *args)
390{
Fred Drakeda940d82000-07-08 06:05:58 +0000391 audio_buf_info ai;
392 int nchannels, ssize;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000393
Fred Drakeda940d82000-07-08 06:05:58 +0000394 if (!PyArg_ParseTuple(args, ":obuffree"))
395 return NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000396
Fred Drakeda940d82000-07-08 06:05:58 +0000397 if (_ssize(self, &nchannels, &ssize) < 0) {
398 PyErr_SetFromErrno(LinuxAudioError);
399 return NULL;
400 }
401 if (ioctl(self->x_fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
402 PyErr_SetFromErrno(LinuxAudioError);
403 return NULL;
404 }
405 return PyInt_FromLong(ai.bytes / (ssize * nchannels));
Guido van Rossumb130dc72000-03-30 23:25:49 +0000406}
407
408/* Flush the device */
409static PyObject *
410lad_flush(lad_t *self, PyObject *args)
411{
Fred Drakeda940d82000-07-08 06:05:58 +0000412 if (!PyArg_ParseTuple(args, ":flush")) return NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000413
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000414 if (ioctl(self->x_fd, SNDCTL_DSP_SYNC, NULL) == -1) {
Fred Drakeda940d82000-07-08 06:05:58 +0000415 PyErr_SetFromErrno(LinuxAudioError);
416 return NULL;
417 }
418 Py_INCREF(Py_None);
419 return Py_None;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000420}
421
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000422static PyObject *
423lad_getptr(lad_t *self, PyObject *args)
424{
425 count_info info;
426 int req;
427
428 if (!PyArg_ParseTuple(args, ":getptr"))
429 return NULL;
430
431 if (self->x_mode == O_RDONLY)
432 req = SNDCTL_DSP_GETIPTR;
433 else
434 req = SNDCTL_DSP_GETOPTR;
435 if (ioctl(self->x_fd, req, &info) == -1) {
436 PyErr_SetFromErrno(LinuxAudioError);
437 return NULL;
438 }
439 return Py_BuildValue("iii", info.bytes, info.blocks, info.ptr);
440}
441
Guido van Rossumb130dc72000-03-30 23:25:49 +0000442static PyMethodDef lad_methods[] = {
Fred Drakeda940d82000-07-08 06:05:58 +0000443 { "read", (PyCFunction)lad_read, METH_VARARGS },
444 { "write", (PyCFunction)lad_write, METH_VARARGS },
445 { "setparameters", (PyCFunction)lad_setparameters, METH_VARARGS },
446 { "bufsize", (PyCFunction)lad_bufsize, METH_VARARGS },
447 { "obufcount", (PyCFunction)lad_obufcount, METH_VARARGS },
448 { "obuffree", (PyCFunction)lad_obuffree, METH_VARARGS },
449 { "flush", (PyCFunction)lad_flush, METH_VARARGS },
450 { "close", (PyCFunction)lad_close, METH_VARARGS },
451 { "fileno", (PyCFunction)lad_fileno, METH_VARARGS },
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000452 { "getptr", (PyCFunction)lad_getptr, METH_VARARGS },
Fred Drakeda940d82000-07-08 06:05:58 +0000453 { NULL, NULL} /* sentinel */
Guido van Rossumb130dc72000-03-30 23:25:49 +0000454};
455
456static PyObject *
457lad_getattr(lad_t *xp, char *name)
458{
Fred Drakeda940d82000-07-08 06:05:58 +0000459 return Py_FindMethod(lad_methods, (PyObject *)xp, name);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000460}
461
462static PyTypeObject Ladtype = {
Fred Drakeda940d82000-07-08 06:05:58 +0000463 PyObject_HEAD_INIT(&PyType_Type)
464 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +0000465 "linuxaudiodev.linux_audio_device", /*tp_name*/
Fred Drakeda940d82000-07-08 06:05:58 +0000466 sizeof(lad_t), /*tp_size*/
467 0, /*tp_itemsize*/
468 /* methods */
469 (destructor)lad_dealloc, /*tp_dealloc*/
470 0, /*tp_print*/
471 (getattrfunc)lad_getattr, /*tp_getattr*/
472 0, /*tp_setattr*/
473 0, /*tp_compare*/
474 0, /*tp_repr*/
Guido van Rossumb130dc72000-03-30 23:25:49 +0000475};
476
477static PyObject *
478ladopen(PyObject *self, PyObject *args)
479{
Fred Drakeda940d82000-07-08 06:05:58 +0000480 return (PyObject *)newladobject(args);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000481}
482
483static PyMethodDef linuxaudiodev_methods[] = {
Fred Drakeda940d82000-07-08 06:05:58 +0000484 { "open", ladopen, METH_VARARGS },
485 { 0, 0 },
Guido van Rossumb130dc72000-03-30 23:25:49 +0000486};
487
Guido van Rossumb130dc72000-03-30 23:25:49 +0000488void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000489initlinuxaudiodev(void)
Guido van Rossumb130dc72000-03-30 23:25:49 +0000490{
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000491 PyObject *m;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000492
Fred Drakeda940d82000-07-08 06:05:58 +0000493 m = Py_InitModule("linuxaudiodev", linuxaudiodev_methods);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000494
Fred Drakeda940d82000-07-08 06:05:58 +0000495 LinuxAudioError = PyErr_NewException("linuxaudiodev.error", NULL, NULL);
496 if (LinuxAudioError)
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000497 PyModule_AddObject(m, "error", LinuxAudioError);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000498
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000499 if (PyModule_AddIntConstant(m, "AFMT_MU_LAW", (long)AFMT_MU_LAW) == -1)
500 return;
501 if (PyModule_AddIntConstant(m, "AFMT_A_LAW", (long)AFMT_A_LAW) == -1)
502 return;
503 if (PyModule_AddIntConstant(m, "AFMT_U8", (long)AFMT_U8) == -1)
504 return;
505 if (PyModule_AddIntConstant(m, "AFMT_S8", (long)AFMT_S8) == -1)
506 return;
507 if (PyModule_AddIntConstant(m, "AFMT_U16_BE", (long)AFMT_U16_BE) == -1)
508 return;
509 if (PyModule_AddIntConstant(m, "AFMT_U16_LE", (long)AFMT_U16_LE) == -1)
510 return;
511 if (PyModule_AddIntConstant(m, "AFMT_S16_BE", (long)AFMT_S16_BE) == -1)
512 return;
513 if (PyModule_AddIntConstant(m, "AFMT_S16_LE", (long)AFMT_S16_LE) == -1)
514 return;
Guido van Rossum59316672000-10-08 19:47:47 +0000515 if (PyModule_AddIntConstant(m, "AFMT_S16_NE", (long)AFMT_S16_NE) == -1)
516 return;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000517
Barry Warsaw9bfd2bf2000-09-01 09:01:32 +0000518 return;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000519}