1 /* Hey Emacs, this is -*-C-*-
2 ******************************************************************************
3 * linuxaudiodev.c -- Linux audio device for python.
6 * Created On : Thu Mar 2 21:10:33 2000
7 * Status : Unknown, Use with caution!
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 ******************************************************************************
17 #include "structmember.h"
27 #include <sys/ioctl.h>
29 #include <linux/soundcard.h>
31 typedef unsigned long uint32_t;
33 #elif defined(__FreeBSD__)
34 #include <machine/soundcard.h>
36 #ifndef SNDCTL_DSP_CHANNELS
37 #define SNDCTL_DSP_CHANNELS SOUND_PCM_WRITE_CHANNELS
44 int x_fd
; /* The open file */
45 int x_mode
; /* file mode */
46 int x_icount
; /* Input count */
47 int x_ocount
; /* Output count */
48 uint32_t x_afmts
; /* Audio formats supported by hardware*/
51 /* XXX several format defined in soundcard.h are not supported,
52 including _NE (native endian) options and S32 options
60 { 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" },
68 { 16, AFMT_S16_NE
, "linear signed 16-bit native-endian audio" },
71 static int n_audio_types
= sizeof(audio_types
) / sizeof(audio_types
[0]);
73 staticforward PyTypeObject Ladtype
;
75 static PyObject
*LinuxAudioError
;
78 newladobject(PyObject
*arg
)
85 /* Check arg for r/w/rw */
86 if (!PyArg_ParseTuple(arg
, "s:open", &mode
)) return NULL
;
87 if (strcmp(mode
, "r") == 0)
89 else if (strcmp(mode
, "w") == 0)
92 PyErr_SetString(LinuxAudioError
, "mode should be 'r' or 'w'");
96 /* Open the correct device. The base device name comes from the
97 * AUDIODEV environment variable first, then /dev/dsp. The
98 * control device tacks "ctl" onto the base device name.
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.
105 basedev
= getenv("AUDIODEV");
107 basedev
= "/dev/dsp";
109 if ((fd
= open(basedev
, imode
)) == -1) {
110 PyErr_SetFromErrnoWithFilename(LinuxAudioError
, basedev
);
113 if (imode
== O_WRONLY
&& ioctl(fd
, SNDCTL_DSP_NONBLOCK
, NULL
) == -1) {
114 PyErr_SetFromErrnoWithFilename(LinuxAudioError
, basedev
);
117 if (ioctl(fd
, SNDCTL_DSP_GETFMTS
, &afmts
) == -1) {
118 PyErr_SetFromErrnoWithFilename(LinuxAudioError
, basedev
);
121 /* Create and initialize the object */
122 if ((xp
= PyObject_New(lad_t
, &Ladtype
)) == NULL
) {
128 xp
->x_icount
= xp
->x_ocount
= 0;
134 lad_dealloc(lad_t
*xp
)
136 /* if already closed, don't reclose it */
143 lad_read(lad_t
*self
, PyObject
*args
)
149 if (!PyArg_ParseTuple(args
, "i:read", &size
))
151 rv
= PyString_FromStringAndSize(NULL
, size
);
154 cp
= PyString_AS_STRING(rv
);
155 if ((count
= read(self
->x_fd
, cp
, size
)) < 0) {
156 PyErr_SetFromErrno(LinuxAudioError
);
160 self
->x_icount
+= count
;
161 if (_PyString_Resize(&rv
, count
) == -1)
167 lad_write(lad_t
*self
, PyObject
*args
)
171 fd_set write_set_fds
;
175 if (!PyArg_ParseTuple(args
, "s#:write", &cp
, &size
))
178 /* 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 */
185 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*/
188 if ((rv
= write(self
->x_fd
, cp
, size
)) == -1) {
189 if (errno
!= EAGAIN
) {
190 PyErr_SetFromErrno(LinuxAudioError
);
193 errno
= 0; /* EAGAIN: buffer is full, try again */
196 self
->x_ocount
+= rv
;
201 /* printf("Not able to write to linux audio device within %ld seconds\n", tv.tv_sec); */
202 PyErr_SetFromErrno(LinuxAudioError
);
211 lad_close(lad_t
*self
, PyObject
*args
)
213 if (!PyArg_ParseTuple(args
, ":close"))
216 if (self
->x_fd
>= 0) {
225 lad_fileno(lad_t
*self
, PyObject
*args
)
227 if (!PyArg_ParseTuple(args
, ":fileno"))
229 return PyInt_FromLong(self
->x_fd
);
233 lad_setparameters(lad_t
*self
, PyObject
*args
)
235 int rate
, ssize
, nchannels
, n
, fmt
, emulate
=0;
237 if (!PyArg_ParseTuple(args
, "iiii|i:setparameters",
238 &rate
, &ssize
, &nchannels
, &fmt
, &emulate
))
242 PyErr_Format(PyExc_ValueError
, "expected rate >= 0, not %d",
247 PyErr_Format(PyExc_ValueError
, "expected sample size >= 0, not %d",
251 if (nchannels
!= 1 && nchannels
!= 2) {
252 PyErr_Format(PyExc_ValueError
, "nchannels must be 1 or 2, not %d",
257 for (n
= 0; n
< n_audio_types
; n
++)
258 if (fmt
== audio_types
[n
].a_fmt
)
260 if (n
== n_audio_types
) {
261 PyErr_Format(PyExc_ValueError
, "unknown audio encoding: %d", fmt
);
264 if (audio_types
[n
].a_bps
!= ssize
) {
265 PyErr_Format(PyExc_ValueError
,
266 "for %s, expected sample size %d, not %d",
267 audio_types
[n
].a_name
, audio_types
[n
].a_bps
, ssize
);
272 if ((self
->x_afmts
& audio_types
[n
].a_fmt
) == 0) {
273 PyErr_Format(PyExc_ValueError
,
274 "%s format not supported by device",
275 audio_types
[n
].a_name
);
279 if (ioctl(self
->x_fd
, SNDCTL_DSP_SETFMT
,
280 &audio_types
[n
].a_fmt
) == -1) {
281 PyErr_SetFromErrno(LinuxAudioError
);
284 if (ioctl(self
->x_fd
, SNDCTL_DSP_CHANNELS
, &nchannels
) == -1) {
285 PyErr_SetFromErrno(LinuxAudioError
);
288 if (ioctl(self
->x_fd
, SNDCTL_DSP_SPEED
, &rate
) == -1) {
289 PyErr_SetFromErrno(LinuxAudioError
);
298 _ssize(lad_t
*self
, int *nchannels
, int *ssize
)
303 if (ioctl(self
->x_fd
, SNDCTL_DSP_SETFMT
, &fmt
) < 0)
311 *ssize
= sizeof(char);
317 *ssize
= sizeof(short);
325 if (ioctl(self
->x_fd
, SNDCTL_DSP_CHANNELS
, nchannels
) < 0)
331 /* bufsize returns the size of the hardware audio buffer in number
334 lad_bufsize(lad_t
*self
, PyObject
*args
)
337 int nchannels
, ssize
;
339 if (!PyArg_ParseTuple(args
, ":bufsize")) return NULL
;
341 if (_ssize(self
, &nchannels
, &ssize
) < 0) {
342 PyErr_SetFromErrno(LinuxAudioError
);
345 if (ioctl(self
->x_fd
, SNDCTL_DSP_GETOSPACE
, &ai
) < 0) {
346 PyErr_SetFromErrno(LinuxAudioError
);
349 return PyInt_FromLong((ai
.fragstotal
* ai
.fragsize
) / (nchannels
* ssize
));
352 /* obufcount returns the number of samples that are available in the
353 hardware for playing */
355 lad_obufcount(lad_t
*self
, PyObject
*args
)
358 int nchannels
, ssize
;
360 if (!PyArg_ParseTuple(args
, ":obufcount"))
363 if (_ssize(self
, &nchannels
, &ssize
) < 0) {
364 PyErr_SetFromErrno(LinuxAudioError
);
367 if (ioctl(self
->x_fd
, SNDCTL_DSP_GETOSPACE
, &ai
) < 0) {
368 PyErr_SetFromErrno(LinuxAudioError
);
371 return PyInt_FromLong((ai
.fragstotal
* ai
.fragsize
- ai
.bytes
) /
372 (ssize
* nchannels
));
375 /* obufcount returns the number of samples that can be played without
378 lad_obuffree(lad_t
*self
, PyObject
*args
)
381 int nchannels
, ssize
;
383 if (!PyArg_ParseTuple(args
, ":obuffree"))
386 if (_ssize(self
, &nchannels
, &ssize
) < 0) {
387 PyErr_SetFromErrno(LinuxAudioError
);
390 if (ioctl(self
->x_fd
, SNDCTL_DSP_GETOSPACE
, &ai
) < 0) {
391 PyErr_SetFromErrno(LinuxAudioError
);
394 return PyInt_FromLong(ai
.bytes
/ (ssize
* nchannels
));
397 /* Flush the device */
399 lad_flush(lad_t
*self
, PyObject
*args
)
401 if (!PyArg_ParseTuple(args
, ":flush")) return NULL
;
403 if (ioctl(self
->x_fd
, SNDCTL_DSP_SYNC
, NULL
) == -1) {
404 PyErr_SetFromErrno(LinuxAudioError
);
412 lad_getptr(lad_t
*self
, PyObject
*args
)
417 if (!PyArg_ParseTuple(args
, ":getptr"))
420 if (self
->x_mode
== O_RDONLY
)
421 req
= SNDCTL_DSP_GETIPTR
;
423 req
= SNDCTL_DSP_GETOPTR
;
424 if (ioctl(self
->x_fd
, req
, &info
) == -1) {
425 PyErr_SetFromErrno(LinuxAudioError
);
428 return Py_BuildValue("iii", info
.bytes
, info
.blocks
, info
.ptr
);
431 static PyMethodDef lad_methods
[] = {
432 { "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
},
441 { "getptr", (PyCFunction
)lad_getptr
, METH_VARARGS
},
442 { NULL
, NULL
} /* sentinel */
446 lad_getattr(lad_t
*xp
, char *name
)
448 return Py_FindMethod(lad_methods
, (PyObject
*)xp
, name
);
451 static PyTypeObject Ladtype
= {
452 PyObject_HEAD_INIT(&PyType_Type
)
454 "linuxaudiodev.linux_audio_device", /*tp_name*/
455 sizeof(lad_t
), /*tp_size*/
458 (destructor
)lad_dealloc
, /*tp_dealloc*/
460 (getattrfunc
)lad_getattr
, /*tp_getattr*/
467 ladopen(PyObject
*self
, PyObject
*args
)
469 return (PyObject
*)newladobject(args
);
472 static PyMethodDef linuxaudiodev_methods
[] = {
473 { "open", ladopen
, METH_VARARGS
},
478 initlinuxaudiodev(void)
482 m
= Py_InitModule("linuxaudiodev", linuxaudiodev_methods
);
484 LinuxAudioError
= PyErr_NewException("linuxaudiodev.error", NULL
, NULL
);
486 PyModule_AddObject(m
, "error", LinuxAudioError
);
488 if (PyModule_AddIntConstant(m
, "AFMT_MU_LAW", (long)AFMT_MU_LAW
) == -1)
490 if (PyModule_AddIntConstant(m
, "AFMT_A_LAW", (long)AFMT_A_LAW
) == -1)
492 if (PyModule_AddIntConstant(m
, "AFMT_U8", (long)AFMT_U8
) == -1)
494 if (PyModule_AddIntConstant(m
, "AFMT_S8", (long)AFMT_S8
) == -1)
496 if (PyModule_AddIntConstant(m
, "AFMT_U16_BE", (long)AFMT_U16_BE
) == -1)
498 if (PyModule_AddIntConstant(m
, "AFMT_U16_LE", (long)AFMT_U16_LE
) == -1)
500 if (PyModule_AddIntConstant(m
, "AFMT_S16_BE", (long)AFMT_S16_BE
) == -1)
502 if (PyModule_AddIntConstant(m
, "AFMT_S16_LE", (long)AFMT_S16_LE
) == -1)
504 if (PyModule_AddIntConstant(m
, "AFMT_S16_NE", (long)AFMT_S16_NE
) == -1)