blob: 509823ed32d4fb3614d07333c2f5785fc208b3ad [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>
27#endif
28
Guido van Rossumb130dc72000-03-30 23:25:49 +000029#include <sys/ioctl.h>
Jeremy Hyltona3895c02000-08-31 18:11:07 +000030#if defined(linux)
Guido van Rossumb130dc72000-03-30 23:25:49 +000031#include <linux/soundcard.h>
32
33typedef unsigned long uint32_t;
34
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 {
Fred Drakeda940d82000-07-08 06:05:58 +000045 PyObject_HEAD;
46 int x_fd; /* The open file */
47 int x_icount; /* Input count */
48 int x_ocount; /* Output count */
49 uint32_t x_afmts; /* Supported audio formats */
Guido van Rossumb130dc72000-03-30 23:25:49 +000050} lad_t;
51
52static struct {
Fred Drakeda940d82000-07-08 06:05:58 +000053 int a_bps;
54 uint32_t a_fmt;
Guido van Rossumb130dc72000-03-30 23:25:49 +000055} audio_types[] = {
Fred Drakeda940d82000-07-08 06:05:58 +000056 { 8, AFMT_MU_LAW },
57 { 8, AFMT_U8 },
58 { 8, AFMT_S8 },
59 { 16, AFMT_U16_BE },
60 { 16, AFMT_U16_LE },
61 { 16, AFMT_S16_BE },
62 { 16, AFMT_S16_LE },
Guido van Rossumb130dc72000-03-30 23:25:49 +000063};
64
65
66staticforward PyTypeObject Ladtype;
67
68static PyObject *LinuxAudioError;
69
70static lad_t *
71newladobject(PyObject *arg)
72{
Fred Drakeda940d82000-07-08 06:05:58 +000073 lad_t *xp;
74 int fd, afmts, imode;
75 char *mode;
76 char *basedev;
Guido van Rossumb130dc72000-03-30 23:25:49 +000077
Fred Drakeda940d82000-07-08 06:05:58 +000078 /* Check arg for r/w/rw */
79 if (!PyArg_ParseTuple(arg, "s:open", &mode)) return NULL;
80 if (strcmp(mode, "r") == 0)
81 imode = 0;
82 else if (strcmp(mode, "w") == 0)
83 imode = 1;
84 else {
85 PyErr_SetString(LinuxAudioError, "Mode should be one of 'r', or 'w'");
86 return NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +000087 }
Guido van Rossumb130dc72000-03-30 23:25:49 +000088
Fred Drakeda940d82000-07-08 06:05:58 +000089 /* Open the correct device. The base device name comes from the
90 * AUDIODEV environment variable first, then /dev/audio. The
91 * control device tacks "ctl" onto the base device name.
92 */
93 basedev = getenv("AUDIODEV");
94 if (!basedev)
95 basedev = "/dev/dsp";
Guido van Rossumb130dc72000-03-30 23:25:49 +000096
Fred Drakeda940d82000-07-08 06:05:58 +000097 if ((fd = open(basedev, imode)) < 0) {
98 PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev);
99 return NULL;
100 }
101 if (imode && ioctl(fd, SNDCTL_DSP_NONBLOCK, NULL) < 0) {
102 PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev);
103 return NULL;
104 }
105 if (ioctl(fd, SNDCTL_DSP_GETFMTS, &afmts) < 0) {
106 PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev);
107 return NULL;
108 }
109 /* Create and initialize the object */
110 if ((xp = PyObject_New(lad_t, &Ladtype)) == NULL) {
111 close(fd);
112 return NULL;
113 }
114 xp->x_fd = fd;
115 xp->x_icount = xp->x_ocount = 0;
116 xp->x_afmts = afmts;
117 return xp;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000118}
119
120static void
121lad_dealloc(lad_t *xp)
122{
Barry Warsaw4ddd8202000-08-18 05:10:45 +0000123 /* if already closed, don't reclose it */
124 if (xp->x_fd != -1)
125 close(xp->x_fd);
Fred Drakeda940d82000-07-08 06:05:58 +0000126 PyObject_Del(xp);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000127}
128
129static PyObject *
130lad_read(lad_t *self, PyObject *args)
131{
Fred Drakeda940d82000-07-08 06:05:58 +0000132 int size, count;
133 char *cp;
134 PyObject *rv;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000135
Fred Drakeda940d82000-07-08 06:05:58 +0000136 if (!PyArg_ParseTuple(args, "i:read", &size))
137 return NULL;
138 rv = PyString_FromStringAndSize(NULL, size);
139 if (rv == NULL)
140 return NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000141
Fred Drakeda940d82000-07-08 06:05:58 +0000142 if (!(cp = PyString_AsString(rv))) {
143 Py_DECREF(rv);
144 return NULL;
145 }
146 if ((count = read(self->x_fd, cp, size)) < 0) {
147 PyErr_SetFromErrno(LinuxAudioError);
148 Py_DECREF(rv);
149 return NULL;
150 }
151 self->x_icount += count;
152 return rv;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000153}
154
155static PyObject *
156lad_write(lad_t *self, PyObject *args)
157{
Fred Drakeda940d82000-07-08 06:05:58 +0000158 char *cp;
159 int rv, size;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000160
Fred Drakeda940d82000-07-08 06:05:58 +0000161 if (!PyArg_ParseTuple(args, "s#:write", &cp, &size)) return NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000162
Fred Drakeda940d82000-07-08 06:05:58 +0000163 while (size > 0) {
164 if ((rv = write(self->x_fd, cp, size)) < 0) {
165 PyErr_SetFromErrno(LinuxAudioError);
166 return NULL;
167 }
168 self->x_ocount += rv;
169 size -= rv;
170 cp += rv;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000171 }
Fred Drakeda940d82000-07-08 06:05:58 +0000172 Py_INCREF(Py_None);
173 return Py_None;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000174}
175
176static PyObject *
177lad_close(lad_t *self, PyObject *args)
178{
Fred Drakeda940d82000-07-08 06:05:58 +0000179 if (!PyArg_ParseTuple(args, ":close")) return NULL;
180 if (self->x_fd >= 0) {
181 close(self->x_fd);
182 self->x_fd = -1;
183 }
184 Py_INCREF(Py_None);
185 return Py_None;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000186}
187
188static PyObject *
189lad_fileno(lad_t *self, PyObject *args)
190{
Fred Drakeda940d82000-07-08 06:05:58 +0000191 if (!PyArg_ParseTuple(args, ":fileno")) return NULL;
192 return PyInt_FromLong(self->x_fd);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000193}
194
195static PyObject *
196lad_setparameters(lad_t *self, PyObject *args)
197{
Fred Drakeda940d82000-07-08 06:05:58 +0000198 int rate, ssize, nchannels, stereo, n, fmt;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000199
Fred Drakeda940d82000-07-08 06:05:58 +0000200 if (!PyArg_ParseTuple(args, "iiii:setparameters",
201 &rate, &ssize, &nchannels, &fmt))
202 return NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000203
Fred Drakeda940d82000-07-08 06:05:58 +0000204 if (rate < 0 || ssize < 0 || (nchannels != 1 && nchannels != 2)) {
205 PyErr_SetFromErrno(LinuxAudioError);
206 return NULL;
207 }
208 if (ioctl(self->x_fd, SOUND_PCM_WRITE_RATE, &rate) < 0) {
209 PyErr_SetFromErrno(LinuxAudioError);
210 return NULL;
211 }
212 if (ioctl(self->x_fd, SNDCTL_DSP_SAMPLESIZE, &ssize) < 0) {
213 PyErr_SetFromErrno(LinuxAudioError);
214 return NULL;
215 }
216 stereo = (nchannels == 1)? 0: (nchannels == 2)? 1: -1;
217 if (ioctl(self->x_fd, SNDCTL_DSP_STEREO, &stereo) < 0) {
218 PyErr_SetFromErrno(LinuxAudioError);
219 return NULL;
220 }
221 for (n = 0; n != sizeof(audio_types) / sizeof(audio_types[0]); n++)
222 if (fmt == audio_types[n].a_fmt)
223 break;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000224
Fred Drakeda940d82000-07-08 06:05:58 +0000225 if (n == sizeof(audio_types) / sizeof(audio_types[0]) ||
226 audio_types[n].a_bps != ssize ||
227 (self->x_afmts & audio_types[n].a_fmt) == 0) {
228 PyErr_SetFromErrno(LinuxAudioError);
229 return NULL;
230 }
231 if (ioctl(self->x_fd, SNDCTL_DSP_SETFMT, &audio_types[n].a_fmt) < 0) {
232 PyErr_SetFromErrno(LinuxAudioError);
233 return NULL;
234 }
235 Py_INCREF(Py_None);
236 return Py_None;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000237}
238
239static int
240_ssize(lad_t *self, int *nchannels, int *ssize)
241{
Fred Drakeda940d82000-07-08 06:05:58 +0000242 int fmt;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000243
Fred Drakeda940d82000-07-08 06:05:58 +0000244 fmt = 0;
245 if (ioctl(self->x_fd, SNDCTL_DSP_SETFMT, &fmt) < 0)
246 return -errno;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000247
Fred Drakeda940d82000-07-08 06:05:58 +0000248 switch (fmt) {
249 case AFMT_MU_LAW:
250 case AFMT_A_LAW:
251 case AFMT_U8:
252 case AFMT_S8:
253 *ssize = sizeof(char);
254 break;
255 case AFMT_S16_LE:
256 case AFMT_S16_BE:
257 case AFMT_U16_LE:
258 case AFMT_U16_BE:
259 *ssize = sizeof(short);
260 break;
261 case AFMT_MPEG:
262 case AFMT_IMA_ADPCM:
263 default:
264 return -EOPNOTSUPP;
265 }
266 *nchannels = 0;
267 if (ioctl(self->x_fd, SNDCTL_DSP_CHANNELS, nchannels) < 0)
268 return -errno;
269 return 0;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000270}
271
272
273/* bufsize returns the size of the hardware audio buffer in number
Fred Drakeda940d82000-07-08 06:05:58 +0000274 of samples */
Guido van Rossumb130dc72000-03-30 23:25:49 +0000275static PyObject *
276lad_bufsize(lad_t *self, PyObject *args)
277{
Fred Drakeda940d82000-07-08 06:05:58 +0000278 audio_buf_info ai;
279 int nchannels, ssize;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000280
Fred Drakeda940d82000-07-08 06:05:58 +0000281 if (!PyArg_ParseTuple(args, ":bufsize")) return NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000282
Fred Drakeda940d82000-07-08 06:05:58 +0000283 if (_ssize(self, &nchannels, &ssize) < 0) {
284 PyErr_SetFromErrno(LinuxAudioError);
285 return NULL;
286 }
287 if (ioctl(self->x_fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
288 PyErr_SetFromErrno(LinuxAudioError);
289 return NULL;
290 }
291 return PyInt_FromLong((ai.fragstotal * ai.fragsize) / (nchannels * ssize));
Guido van Rossumb130dc72000-03-30 23:25:49 +0000292}
293
294/* obufcount returns the number of samples that are available in the
295 hardware for playing */
296static PyObject *
297lad_obufcount(lad_t *self, PyObject *args)
298{
Fred Drakeda940d82000-07-08 06:05:58 +0000299 audio_buf_info ai;
300 int nchannels, ssize;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000301
Fred Drakeda940d82000-07-08 06:05:58 +0000302 if (!PyArg_ParseTuple(args, ":obufcount"))
303 return NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000304
Fred Drakeda940d82000-07-08 06:05:58 +0000305 if (_ssize(self, &nchannels, &ssize) < 0) {
306 PyErr_SetFromErrno(LinuxAudioError);
307 return NULL;
308 }
309 if (ioctl(self->x_fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
310 PyErr_SetFromErrno(LinuxAudioError);
311 return NULL;
312 }
313 return PyInt_FromLong((ai.fragstotal * ai.fragsize - ai.bytes) /
314 (ssize * nchannels));
Guido van Rossumb130dc72000-03-30 23:25:49 +0000315}
316
317/* obufcount returns the number of samples that can be played without
Fred Drakeda940d82000-07-08 06:05:58 +0000318 blocking */
Guido van Rossumb130dc72000-03-30 23:25:49 +0000319static PyObject *
320lad_obuffree(lad_t *self, PyObject *args)
321{
Fred Drakeda940d82000-07-08 06:05:58 +0000322 audio_buf_info ai;
323 int nchannels, ssize;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000324
Fred Drakeda940d82000-07-08 06:05:58 +0000325 if (!PyArg_ParseTuple(args, ":obuffree"))
326 return NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000327
Fred Drakeda940d82000-07-08 06:05:58 +0000328 if (_ssize(self, &nchannels, &ssize) < 0) {
329 PyErr_SetFromErrno(LinuxAudioError);
330 return NULL;
331 }
332 if (ioctl(self->x_fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
333 PyErr_SetFromErrno(LinuxAudioError);
334 return NULL;
335 }
336 return PyInt_FromLong(ai.bytes / (ssize * nchannels));
Guido van Rossumb130dc72000-03-30 23:25:49 +0000337}
338
339/* Flush the device */
340static PyObject *
341lad_flush(lad_t *self, PyObject *args)
342{
Fred Drakeda940d82000-07-08 06:05:58 +0000343 if (!PyArg_ParseTuple(args, ":flush")) return NULL;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000344
Fred Drakeda940d82000-07-08 06:05:58 +0000345 if (ioctl(self->x_fd, SNDCTL_DSP_SYNC, NULL) < 0) {
346 PyErr_SetFromErrno(LinuxAudioError);
347 return NULL;
348 }
349 Py_INCREF(Py_None);
350 return Py_None;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000351}
352
353static PyMethodDef lad_methods[] = {
Fred Drakeda940d82000-07-08 06:05:58 +0000354 { "read", (PyCFunction)lad_read, METH_VARARGS },
355 { "write", (PyCFunction)lad_write, METH_VARARGS },
356 { "setparameters", (PyCFunction)lad_setparameters, METH_VARARGS },
357 { "bufsize", (PyCFunction)lad_bufsize, METH_VARARGS },
358 { "obufcount", (PyCFunction)lad_obufcount, METH_VARARGS },
359 { "obuffree", (PyCFunction)lad_obuffree, METH_VARARGS },
360 { "flush", (PyCFunction)lad_flush, METH_VARARGS },
361 { "close", (PyCFunction)lad_close, METH_VARARGS },
362 { "fileno", (PyCFunction)lad_fileno, METH_VARARGS },
363 { NULL, NULL} /* sentinel */
Guido van Rossumb130dc72000-03-30 23:25:49 +0000364};
365
366static PyObject *
367lad_getattr(lad_t *xp, char *name)
368{
Fred Drakeda940d82000-07-08 06:05:58 +0000369 return Py_FindMethod(lad_methods, (PyObject *)xp, name);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000370}
371
372static PyTypeObject Ladtype = {
Fred Drakeda940d82000-07-08 06:05:58 +0000373 PyObject_HEAD_INIT(&PyType_Type)
374 0, /*ob_size*/
375 "linux_audio_device", /*tp_name*/
376 sizeof(lad_t), /*tp_size*/
377 0, /*tp_itemsize*/
378 /* methods */
379 (destructor)lad_dealloc, /*tp_dealloc*/
380 0, /*tp_print*/
381 (getattrfunc)lad_getattr, /*tp_getattr*/
382 0, /*tp_setattr*/
383 0, /*tp_compare*/
384 0, /*tp_repr*/
Guido van Rossumb130dc72000-03-30 23:25:49 +0000385};
386
387static PyObject *
388ladopen(PyObject *self, PyObject *args)
389{
Fred Drakeda940d82000-07-08 06:05:58 +0000390 return (PyObject *)newladobject(args);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000391}
392
393static PyMethodDef linuxaudiodev_methods[] = {
Fred Drakeda940d82000-07-08 06:05:58 +0000394 { "open", ladopen, METH_VARARGS },
395 { 0, 0 },
Guido van Rossumb130dc72000-03-30 23:25:49 +0000396};
397
Guido van Rossumb130dc72000-03-30 23:25:49 +0000398void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000399initlinuxaudiodev(void)
Guido van Rossumb130dc72000-03-30 23:25:49 +0000400{
Fred Drakeda940d82000-07-08 06:05:58 +0000401 PyObject *m, *d, *x;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000402
Fred Drakeda940d82000-07-08 06:05:58 +0000403 m = Py_InitModule("linuxaudiodev", linuxaudiodev_methods);
404 d = PyModule_GetDict(m);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000405
Fred Drakeda940d82000-07-08 06:05:58 +0000406 LinuxAudioError = PyErr_NewException("linuxaudiodev.error", NULL, NULL);
407 if (LinuxAudioError)
408 PyDict_SetItemString(d, "error", LinuxAudioError);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000409
Fred Drakeda940d82000-07-08 06:05:58 +0000410 x = PyInt_FromLong((long) AFMT_MU_LAW);
411 if (x == NULL || PyDict_SetItemString(d, "AFMT_MU_LAW", x) < 0)
412 goto error;
413 Py_DECREF(x);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000414
Fred Drakeda940d82000-07-08 06:05:58 +0000415 x = PyInt_FromLong((long) AFMT_U8);
416 if (x == NULL || PyDict_SetItemString(d, "AFMT_U8", x) < 0)
417 goto error;
418 Py_DECREF(x);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000419
Fred Drakeda940d82000-07-08 06:05:58 +0000420 x = PyInt_FromLong((long) AFMT_S8);
421 if (x == NULL || PyDict_SetItemString(d, "AFMT_S8", x) < 0)
422 goto error;
423 Py_DECREF(x);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000424
Fred Drakeda940d82000-07-08 06:05:58 +0000425 x = PyInt_FromLong((long) AFMT_U16_BE);
426 if (x == NULL || PyDict_SetItemString(d, "AFMT_U16_BE", x) < 0)
427 goto error;
428 Py_DECREF(x);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000429
Fred Drakeda940d82000-07-08 06:05:58 +0000430 x = PyInt_FromLong((long) AFMT_U16_LE);
431 if (x == NULL || PyDict_SetItemString(d, "AFMT_U16_LE", x) < 0)
432 goto error;
433 Py_DECREF(x);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000434
Fred Drakeda940d82000-07-08 06:05:58 +0000435 x = PyInt_FromLong((long) AFMT_S16_BE);
436 if (x == NULL || PyDict_SetItemString(d, "AFMT_S16_BE", x) < 0)
437 goto error;
438 Py_DECREF(x);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000439
Fred Drakeda940d82000-07-08 06:05:58 +0000440 x = PyInt_FromLong((long) AFMT_S16_LE);
441 if (x == NULL || PyDict_SetItemString(d, "AFMT_S16_LE", x) < 0)
442 goto error;
443 Py_DECREF(x);
Guido van Rossumb130dc72000-03-30 23:25:49 +0000444
Barry Warsaw9bfd2bf2000-09-01 09:01:32 +0000445 error:
446 return;
Guido van Rossumb130dc72000-03-30 23:25:49 +0000447}