blob: 7fe20ae19544c74527d20deed38c6bc193397fad [file] [log] [blame]
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001/* Hey Emacs, this is -*-C-*-
Guido van Rossumb130dc72000-03-30 23:25:49 +00002 ******************************************************************************
3 * linuxaudiodev.c -- Linux audio device for python.
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004 *
Guido van Rossumb130dc72000-03-30 23:25:49 +00005 * 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!
Antoine Pitrouc83ea132010-05-09 14:46:46 +00008 *
Guido van Rossumb130dc72000-03-30 23:25:49 +00009 * Unless other notices are present in any part of this file
Antoine Pitrouc83ea132010-05-09 14:46:46 +000010 * explicitly claiming copyrights for other people and/or
11 * organizations, the contents of this file is fully copyright
Guido van Rossumb130dc72000-03-30 23:25:49 +000012 * (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
Armin Rigo0d2f4982006-10-04 10:23:57 +000031#ifndef HAVE_STDINT_H
Guido van Rossumb130dc72000-03-30 23:25:49 +000032typedef unsigned long uint32_t;
Armin Rigo0d2f4982006-10-04 10:23:57 +000033#endif
Guido van Rossumb130dc72000-03-30 23:25:49 +000034
Jeremy Hyltona3895c02000-08-31 18:11:07 +000035#elif defined(__FreeBSD__)
36#include <machine/soundcard.h>
37
38#ifndef SNDCTL_DSP_CHANNELS
39#define SNDCTL_DSP_CHANNELS SOUND_PCM_WRITE_CHANNELS
40#endif
41
42#endif
43
Guido van Rossumb130dc72000-03-30 23:25:49 +000044typedef struct {
Neal Norwitz3f046482006-01-07 21:19:49 +000045 PyObject_HEAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +000046 int x_fd; /* The open file */
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +000047 int x_mode; /* file mode */
Antoine Pitrouc83ea132010-05-09 14:46:46 +000048 int x_icount; /* Input count */
49 int x_ocount; /* Output count */
50 uint32_t x_afmts; /* Audio formats supported by hardware*/
Guido van Rossumb130dc72000-03-30 23:25:49 +000051} lad_t;
52
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +000053/* XXX several format defined in soundcard.h are not supported,
54 including _NE (native endian) options and S32 options
55*/
56
Guido van Rossumb130dc72000-03-30 23:25:49 +000057static struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +000058 int a_bps;
59 uint32_t a_fmt;
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +000060 char *a_name;
Guido van Rossumb130dc72000-03-30 23:25:49 +000061} audio_types[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +000062 { 8, AFMT_MU_LAW, "logarithmic mu-law 8-bit audio" },
63 { 8, AFMT_A_LAW, "logarithmic A-law 8-bit audio" },
64 { 8, AFMT_U8, "linear unsigned 8-bit audio" },
65 { 8, AFMT_S8, "linear signed 8-bit audio" },
66 { 16, AFMT_U16_BE, "linear unsigned 16-bit big-endian audio" },
67 { 16, AFMT_U16_LE, "linear unsigned 16-bit little-endian audio" },
68 { 16, AFMT_S16_BE, "linear signed 16-bit big-endian audio" },
69 { 16, AFMT_S16_LE, "linear signed 16-bit little-endian audio" },
70 { 16, AFMT_S16_NE, "linear signed 16-bit native-endian audio" },
Guido van Rossumb130dc72000-03-30 23:25:49 +000071};
72
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +000073static int n_audio_types = sizeof(audio_types) / sizeof(audio_types[0]);
Guido van Rossumb130dc72000-03-30 23:25:49 +000074
Jeremy Hylton938ace62002-07-17 16:30:39 +000075static PyTypeObject Ladtype;
Guido van Rossumb130dc72000-03-30 23:25:49 +000076
77static PyObject *LinuxAudioError;
78
79static lad_t *
80newladobject(PyObject *arg)
81{
Fred Drakeda940d82000-07-08 06:05:58 +000082 lad_t *xp;
83 int fd, afmts, imode;
Greg Warda34b1a02002-11-27 22:19:15 +000084 char *basedev = NULL;
85 char *mode = NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +000086
Greg Warda34b1a02002-11-27 22:19:15 +000087 /* Two ways to call linuxaudiodev.open():
88 open(device, mode) (for consistency with builtin open())
89 open(mode) (for backwards compatibility)
90 because the *first* argument is optional, parsing args is
91 a wee bit tricky. */
92 if (!PyArg_ParseTuple(arg, "s|s:open", &basedev, &mode))
93 return NULL;
94 if (mode == NULL) { /* only one arg supplied */
95 mode = basedev;
96 basedev = NULL;
97 }
98
Fred Drakeda940d82000-07-08 06:05:58 +000099 if (strcmp(mode, "r") == 0)
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000100 imode = O_RDONLY;
Fred Drakeda940d82000-07-08 06:05:58 +0000101 else if (strcmp(mode, "w") == 0)
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000102 imode = O_WRONLY;
Fred Drakeda940d82000-07-08 06:05:58 +0000103 else {
Ka-Ping Yee27ac0d12001-01-15 22:21:39 +0000104 PyErr_SetString(LinuxAudioError, "mode should be 'r' or 'w'");
Fred Drakeda940d82000-07-08 06:05:58 +0000105 return NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000106 }
Guido van Rossumb130dc72000-03-30 23:25:49 +0000107
Fred Drakeda940d82000-07-08 06:05:58 +0000108 /* Open the correct device. The base device name comes from the
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000109 * AUDIODEV environment variable first, then /dev/dsp. The
Fred Drakeda940d82000-07-08 06:05:58 +0000110 * control device tacks "ctl" onto the base device name.
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000111 *
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000112 * Note that the only difference between /dev/audio and /dev/dsp
113 * is that the former uses logarithmic mu-law encoding and the
114 * latter uses 8-bit unsigned encoding.
Fred Drakeda940d82000-07-08 06:05:58 +0000115 */
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000116
Greg Warda34b1a02002-11-27 22:19:15 +0000117 if (basedev == NULL) { /* called with one arg */
118 basedev = getenv("AUDIODEV");
119 if (basedev == NULL) /* $AUDIODEV not set */
120 basedev = "/dev/dsp";
121 }
Guido van Rossumb130dc72000-03-30 23:25:49 +0000122
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000123 if ((fd = open(basedev, imode)) == -1) {
Fred Drakeda940d82000-07-08 06:05:58 +0000124 PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev);
125 return NULL;
126 }
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000127 if (imode == O_WRONLY && ioctl(fd, SNDCTL_DSP_NONBLOCK, NULL) == -1) {
Fred Drakeda940d82000-07-08 06:05:58 +0000128 PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev);
129 return NULL;
130 }
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000131 if (ioctl(fd, SNDCTL_DSP_GETFMTS, &afmts) == -1) {
Fred Drakeda940d82000-07-08 06:05:58 +0000132 PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev);
133 return NULL;
134 }
135 /* Create and initialize the object */
136 if ((xp = PyObject_New(lad_t, &Ladtype)) == NULL) {
137 close(fd);
138 return NULL;
139 }
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000140 xp->x_fd = fd;
141 xp->x_mode = imode;
Fred Drakeda940d82000-07-08 06:05:58 +0000142 xp->x_icount = xp->x_ocount = 0;
143 xp->x_afmts = afmts;
144 return xp;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000145}
146
147static void
148lad_dealloc(lad_t *xp)
149{
Barry Warsaw4ddd8202000-08-18 05:10:45 +0000150 /* if already closed, don't reclose it */
151 if (xp->x_fd != -1)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000152 close(xp->x_fd);
Fred Drakeda940d82000-07-08 06:05:58 +0000153 PyObject_Del(xp);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000154}
155
156static PyObject *
157lad_read(lad_t *self, PyObject *args)
158{
Fred Drakeda940d82000-07-08 06:05:58 +0000159 int size, count;
160 char *cp;
161 PyObject *rv;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000162
Fred Drakeda940d82000-07-08 06:05:58 +0000163 if (!PyArg_ParseTuple(args, "i:read", &size))
164 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000165 rv = PyString_FromStringAndSize(NULL, size);
Fred Drakeda940d82000-07-08 06:05:58 +0000166 if (rv == NULL)
167 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000168 cp = PyString_AS_STRING(rv);
Fred Drakeda940d82000-07-08 06:05:58 +0000169 if ((count = read(self->x_fd, cp, size)) < 0) {
170 PyErr_SetFromErrno(LinuxAudioError);
171 Py_DECREF(rv);
172 return NULL;
173 }
174 self->x_icount += count;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000175 _PyString_Resize(&rv, count);
Fred Drakeda940d82000-07-08 06:05:58 +0000176 return rv;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000177}
178
179static PyObject *
180lad_write(lad_t *self, PyObject *args)
181{
Fred Drakeda940d82000-07-08 06:05:58 +0000182 char *cp;
183 int rv, size;
Guido van Rossumf5bd6842001-04-02 17:59:02 +0000184 fd_set write_set_fds;
185 struct timeval tv;
186 int select_retval;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000187
188 if (!PyArg_ParseTuple(args, "s#:write", &cp, &size))
189 return NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000190
Guido van Rossumf5bd6842001-04-02 17:59:02 +0000191 /* use select to wait for audio device to be available */
192 FD_ZERO(&write_set_fds);
193 FD_SET(self->x_fd, &write_set_fds);
194 tv.tv_sec = 4; /* timeout values */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000195 tv.tv_usec = 0;
Guido van Rossumf5bd6842001-04-02 17:59:02 +0000196
Fred Drakeda940d82000-07-08 06:05:58 +0000197 while (size > 0) {
Guido van Rossumf5bd6842001-04-02 17:59:02 +0000198 select_retval = select(self->x_fd+1, NULL, &write_set_fds, NULL, &tv);
199 tv.tv_sec = 1; tv.tv_usec = 0; /* willing to wait this long next time*/
200 if (select_retval) {
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000201 if ((rv = write(self->x_fd, cp, size)) == -1) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000202 if (errno != EAGAIN) {
203 PyErr_SetFromErrno(LinuxAudioError);
204 return NULL;
205 } else {
206 errno = 0; /* EAGAIN: buffer is full, try again */
207 }
Guido van Rossumf5bd6842001-04-02 17:59:02 +0000208 } else {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000209 self->x_ocount += rv;
210 size -= rv;
211 cp += rv;
212 }
Guido van Rossumf5bd6842001-04-02 17:59:02 +0000213 } else {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000214 /* printf("Not able to write to linux audio device within %ld seconds\n", tv.tv_sec); */
215 PyErr_SetFromErrno(LinuxAudioError);
216 return NULL;
Guido van Rossumf5bd6842001-04-02 17:59:02 +0000217 }
Guido van Rossumb130dc72000-03-30 23:25:49 +0000218 }
Fred Drakeda940d82000-07-08 06:05:58 +0000219 Py_INCREF(Py_None);
220 return Py_None;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000221}
222
223static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +0000224lad_close(lad_t *self, PyObject *unused)
Guido van Rossumb130dc72000-03-30 23:25:49 +0000225{
Fred Drakeda940d82000-07-08 06:05:58 +0000226 if (self->x_fd >= 0) {
227 close(self->x_fd);
228 self->x_fd = -1;
229 }
Georg Brandl96a8c392006-05-29 21:04:52 +0000230 Py_RETURN_NONE;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000231}
232
233static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +0000234lad_fileno(lad_t *self, PyObject *unused)
Guido van Rossumb130dc72000-03-30 23:25:49 +0000235{
Fred Drakeda940d82000-07-08 06:05:58 +0000236 return PyInt_FromLong(self->x_fd);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000237}
238
239static PyObject *
240lad_setparameters(lad_t *self, PyObject *args)
241{
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000242 int rate, ssize, nchannels, n, fmt, emulate=0;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000243
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000244 if (!PyArg_ParseTuple(args, "iiii|i:setparameters",
245 &rate, &ssize, &nchannels, &fmt, &emulate))
Fred Drakeda940d82000-07-08 06:05:58 +0000246 return NULL;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000247
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000248 if (rate < 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000249 PyErr_Format(PyExc_ValueError, "expected rate >= 0, not %d",
250 rate);
251 return NULL;
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000252 }
253 if (ssize < 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000254 PyErr_Format(PyExc_ValueError, "expected sample size >= 0, not %d",
255 ssize);
256 return NULL;
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000257 }
258 if (nchannels != 1 && nchannels != 2) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000259 PyErr_Format(PyExc_ValueError, "nchannels must be 1 or 2, not %d",
260 nchannels);
261 return NULL;
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000262 }
263
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000264 for (n = 0; n < n_audio_types; n++)
Fred Drakeda940d82000-07-08 06:05:58 +0000265 if (fmt == audio_types[n].a_fmt)
266 break;
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000267 if (n == n_audio_types) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000268 PyErr_Format(PyExc_ValueError, "unknown audio encoding: %d", fmt);
269 return NULL;
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000270 }
271 if (audio_types[n].a_bps != ssize) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000272 PyErr_Format(PyExc_ValueError,
273 "for %s, expected sample size %d, not %d",
274 audio_types[n].a_name, audio_types[n].a_bps, ssize);
275 return NULL;
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000276 }
Guido van Rossumb130dc72000-03-30 23:25:49 +0000277
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000278 if (emulate == 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000279 if ((self->x_afmts & audio_types[n].a_fmt) == 0) {
280 PyErr_Format(PyExc_ValueError,
281 "%s format not supported by device",
282 audio_types[n].a_name);
283 return NULL;
284 }
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000285 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000286 if (ioctl(self->x_fd, SNDCTL_DSP_SETFMT,
287 &audio_types[n].a_fmt) == -1) {
Fred Drakeda940d82000-07-08 06:05:58 +0000288 PyErr_SetFromErrno(LinuxAudioError);
289 return NULL;
290 }
Guido van Rossum77452182001-12-08 17:13:45 +0000291 if (ioctl(self->x_fd, SNDCTL_DSP_CHANNELS, &nchannels) == -1) {
292 PyErr_SetFromErrno(LinuxAudioError);
293 return NULL;
294 }
295 if (ioctl(self->x_fd, SNDCTL_DSP_SPEED, &rate) == -1) {
296 PyErr_SetFromErrno(LinuxAudioError);
297 return NULL;
298 }
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000299
Fred Drakeda940d82000-07-08 06:05:58 +0000300 Py_INCREF(Py_None);
301 return Py_None;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000302}
303
304static int
305_ssize(lad_t *self, int *nchannels, int *ssize)
306{
Fred Drakeda940d82000-07-08 06:05:58 +0000307 int fmt;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000308
Fred Drakeda940d82000-07-08 06:05:58 +0000309 fmt = 0;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000310 if (ioctl(self->x_fd, SNDCTL_DSP_SETFMT, &fmt) < 0)
Fred Drakeda940d82000-07-08 06:05:58 +0000311 return -errno;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000312
Fred Drakeda940d82000-07-08 06:05:58 +0000313 switch (fmt) {
314 case AFMT_MU_LAW:
315 case AFMT_A_LAW:
316 case AFMT_U8:
317 case AFMT_S8:
318 *ssize = sizeof(char);
319 break;
320 case AFMT_S16_LE:
321 case AFMT_S16_BE:
322 case AFMT_U16_LE:
323 case AFMT_U16_BE:
324 *ssize = sizeof(short);
325 break;
326 case AFMT_MPEG:
327 case AFMT_IMA_ADPCM:
328 default:
329 return -EOPNOTSUPP;
330 }
Fred Drakeda940d82000-07-08 06:05:58 +0000331 if (ioctl(self->x_fd, SNDCTL_DSP_CHANNELS, nchannels) < 0)
332 return -errno;
333 return 0;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000334}
335
336
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000337/* bufsize returns the size of the hardware audio buffer in number
Fred Drakeda940d82000-07-08 06:05:58 +0000338 of samples */
Guido van Rossumb130dc72000-03-30 23:25:49 +0000339static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +0000340lad_bufsize(lad_t *self, PyObject *unused)
Guido van Rossumb130dc72000-03-30 23:25:49 +0000341{
Fred Drakeda940d82000-07-08 06:05:58 +0000342 audio_buf_info ai;
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000343 int nchannels=0, ssize=0;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000344
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000345 if (_ssize(self, &nchannels, &ssize) < 0 || !ssize || !nchannels) {
Fred Drakeda940d82000-07-08 06:05:58 +0000346 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
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000356/* obufcount returns the number of samples that are available in the
Guido van Rossumb130dc72000-03-30 23:25:49 +0000357 hardware for playing */
358static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +0000359lad_obufcount(lad_t *self, PyObject *unused)
Guido van Rossumb130dc72000-03-30 23:25:49 +0000360{
Fred Drakeda940d82000-07-08 06:05:58 +0000361 audio_buf_info ai;
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000362 int nchannels=0, ssize=0;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000363
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000364 if (_ssize(self, &nchannels, &ssize) < 0 || !ssize || !nchannels) {
Fred Drakeda940d82000-07-08 06:05:58 +0000365 PyErr_SetFromErrno(LinuxAudioError);
366 return NULL;
367 }
368 if (ioctl(self->x_fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
369 PyErr_SetFromErrno(LinuxAudioError);
370 return NULL;
371 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000372 return PyInt_FromLong((ai.fragstotal * ai.fragsize - ai.bytes) /
Fred Drakeda940d82000-07-08 06:05:58 +0000373 (ssize * nchannels));
Guido van Rossumb130dc72000-03-30 23:25:49 +0000374}
375
376/* obufcount returns the number of samples that can be played without
Fred Drakeda940d82000-07-08 06:05:58 +0000377 blocking */
Guido van Rossumb130dc72000-03-30 23:25:49 +0000378static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +0000379lad_obuffree(lad_t *self, PyObject *unused)
Guido van Rossumb130dc72000-03-30 23:25:49 +0000380{
Fred Drakeda940d82000-07-08 06:05:58 +0000381 audio_buf_info ai;
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000382 int nchannels=0, ssize=0;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000383
Thomas Wouters3ffa59b2006-03-01 22:45:36 +0000384 if (_ssize(self, &nchannels, &ssize) < 0 || !ssize || !nchannels) {
Fred Drakeda940d82000-07-08 06:05:58 +0000385 PyErr_SetFromErrno(LinuxAudioError);
386 return NULL;
387 }
388 if (ioctl(self->x_fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
389 PyErr_SetFromErrno(LinuxAudioError);
390 return NULL;
391 }
392 return PyInt_FromLong(ai.bytes / (ssize * nchannels));
Guido van Rossumb130dc72000-03-30 23:25:49 +0000393}
394
395/* Flush the device */
396static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +0000397lad_flush(lad_t *self, PyObject *unused)
Guido van Rossumb130dc72000-03-30 23:25:49 +0000398{
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000399 if (ioctl(self->x_fd, SNDCTL_DSP_SYNC, NULL) == -1) {
Fred Drakeda940d82000-07-08 06:05:58 +0000400 PyErr_SetFromErrno(LinuxAudioError);
401 return NULL;
402 }
Georg Brandl96a8c392006-05-29 21:04:52 +0000403 Py_RETURN_NONE;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000404}
405
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000406static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +0000407lad_getptr(lad_t *self, PyObject *unused)
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000408{
409 count_info info;
410 int req;
411
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000412 if (self->x_mode == O_RDONLY)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000413 req = SNDCTL_DSP_GETIPTR;
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000414 else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000415 req = SNDCTL_DSP_GETOPTR;
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000416 if (ioctl(self->x_fd, req, &info) == -1) {
417 PyErr_SetFromErrno(LinuxAudioError);
418 return NULL;
419 }
420 return Py_BuildValue("iii", info.bytes, info.blocks, info.ptr);
421}
422
Guido van Rossumb130dc72000-03-30 23:25:49 +0000423static PyMethodDef lad_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000424 { "read", (PyCFunction)lad_read, METH_VARARGS },
425 { "write", (PyCFunction)lad_write, METH_VARARGS },
426 { "setparameters", (PyCFunction)lad_setparameters, METH_VARARGS },
427 { "bufsize", (PyCFunction)lad_bufsize, METH_VARARGS },
428 { "obufcount", (PyCFunction)lad_obufcount, METH_NOARGS },
429 { "obuffree", (PyCFunction)lad_obuffree, METH_NOARGS },
430 { "flush", (PyCFunction)lad_flush, METH_NOARGS },
431 { "close", (PyCFunction)lad_close, METH_NOARGS },
432 { "fileno", (PyCFunction)lad_fileno, METH_NOARGS },
Georg Brandl96a8c392006-05-29 21:04:52 +0000433 { "getptr", (PyCFunction)lad_getptr, METH_NOARGS },
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000434 { NULL, NULL} /* sentinel */
Guido van Rossumb130dc72000-03-30 23:25:49 +0000435};
436
437static PyObject *
438lad_getattr(lad_t *xp, char *name)
439{
Fred Drakeda940d82000-07-08 06:05:58 +0000440 return Py_FindMethod(lad_methods, (PyObject *)xp, name);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000441}
442
443static PyTypeObject Ladtype = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000444 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossum14648392001-12-08 18:02:58 +0000445 "linuxaudiodev.linux_audio_device", /*tp_name*/
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000446 sizeof(lad_t), /*tp_size*/
447 0, /*tp_itemsize*/
Fred Drakeda940d82000-07-08 06:05:58 +0000448 /* methods */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000449 (destructor)lad_dealloc, /*tp_dealloc*/
450 0, /*tp_print*/
451 (getattrfunc)lad_getattr, /*tp_getattr*/
452 0, /*tp_setattr*/
453 0, /*tp_compare*/
454 0, /*tp_repr*/
Guido van Rossumb130dc72000-03-30 23:25:49 +0000455};
456
457static PyObject *
458ladopen(PyObject *self, PyObject *args)
459{
Fred Drakeda940d82000-07-08 06:05:58 +0000460 return (PyObject *)newladobject(args);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000461}
462
463static PyMethodDef linuxaudiodev_methods[] = {
Fred Drakeda940d82000-07-08 06:05:58 +0000464 { "open", ladopen, METH_VARARGS },
465 { 0, 0 },
Guido van Rossumb130dc72000-03-30 23:25:49 +0000466};
467
Guido van Rossumb130dc72000-03-30 23:25:49 +0000468void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000469initlinuxaudiodev(void)
Guido van Rossumb130dc72000-03-30 23:25:49 +0000470{
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000471 PyObject *m;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000472
Brett Cannondf0a7172008-05-11 00:50:51 +0000473 if (PyErr_WarnPy3k("the linuxaudiodev module has been removed in "
474 "Python 3.0; use the ossaudiodev module instead", 2) < 0)
475 return;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000476
Fred Drakeda940d82000-07-08 06:05:58 +0000477 m = Py_InitModule("linuxaudiodev", linuxaudiodev_methods);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000478 if (m == NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000479 return;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000480
Fred Drakeda940d82000-07-08 06:05:58 +0000481 LinuxAudioError = PyErr_NewException("linuxaudiodev.error", NULL, NULL);
482 if (LinuxAudioError)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000483 PyModule_AddObject(m, "error", LinuxAudioError);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000484
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000485 if (PyModule_AddIntConstant(m, "AFMT_MU_LAW", (long)AFMT_MU_LAW) == -1)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000486 return;
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000487 if (PyModule_AddIntConstant(m, "AFMT_A_LAW", (long)AFMT_A_LAW) == -1)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000488 return;
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000489 if (PyModule_AddIntConstant(m, "AFMT_U8", (long)AFMT_U8) == -1)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000490 return;
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000491 if (PyModule_AddIntConstant(m, "AFMT_S8", (long)AFMT_S8) == -1)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000492 return;
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000493 if (PyModule_AddIntConstant(m, "AFMT_U16_BE", (long)AFMT_U16_BE) == -1)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000494 return;
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000495 if (PyModule_AddIntConstant(m, "AFMT_U16_LE", (long)AFMT_U16_LE) == -1)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000496 return;
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000497 if (PyModule_AddIntConstant(m, "AFMT_S16_BE", (long)AFMT_S16_BE) == -1)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000498 return;
Jeremy Hyltone2b7c4d2000-10-06 19:39:55 +0000499 if (PyModule_AddIntConstant(m, "AFMT_S16_LE", (long)AFMT_S16_LE) == -1)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000500 return;
Guido van Rossum59316672000-10-08 19:47:47 +0000501 if (PyModule_AddIntConstant(m, "AFMT_S16_NE", (long)AFMT_S16_NE) == -1)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000502 return;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000503
Barry Warsaw9bfd2bf2000-09-01 09:01:32 +0000504 return;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000505}