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"
31 #include <sys/ioctl.h>
33 #include <linux/soundcard.h>
35 typedef unsigned long uint32_t;
37 #elif defined(__FreeBSD__)
38 #include <machine/soundcard.h>
40 #ifndef SNDCTL_DSP_CHANNELS
41 #define SNDCTL_DSP_CHANNELS SOUND_PCM_WRITE_CHANNELS
48 int x_fd
; /* The open file */
49 int x_mode
; /* file mode */
50 int x_icount
; /* Input count */
51 int x_ocount
; /* Output count */
52 uint32_t x_afmts
; /* Audio formats supported by hardware*/
55 /* XXX several format defined in soundcard.h are not supported,
56 including _NE (native endian) options and S32 options
64 { 8, AFMT_MU_LAW
, "logarithmic mu-law 8-bit audio" },
65 { 8, AFMT_A_LAW
, "logarithmic A-law 8-bit audio" },
66 { 8, AFMT_U8
, "linear unsigned 8-bit audio" },
67 { 8, AFMT_S8
, "linear signed 8-bit audio" },
68 { 16, AFMT_U16_BE
, "linear unsigned 16-bit big-endian audio" },
69 { 16, AFMT_U16_LE
, "linear unsigned 16-bit little-endian audio" },
70 { 16, AFMT_S16_BE
, "linear signed 16-bit big-endian audio" },
71 { 16, AFMT_S16_LE
, "linear signed 16-bit little-endian audio" },
72 { 16, AFMT_S16_NE
, "linear signed 16-bit native-endian audio" },
75 static int n_audio_types
= sizeof(audio_types
) / sizeof(audio_types
[0]);
77 staticforward PyTypeObject Ladtype
;
79 static PyObject
*LinuxAudioError
;
82 newladobject(PyObject
*arg
)
89 /* Check arg for r/w/rw */
90 if (!PyArg_ParseTuple(arg
, "s:open", &mode
)) return NULL
;
91 if (strcmp(mode
, "r") == 0)
93 else if (strcmp(mode
, "w") == 0)
96 PyErr_SetString(LinuxAudioError
, "mode should be 'r' or 'w'");
100 /* Open the correct device. The base device name comes from the
101 * AUDIODEV environment variable first, then /dev/dsp. The
102 * control device tacks "ctl" onto the base device name.
104 * Note that the only difference between /dev/audio and /dev/dsp
105 * is that the former uses logarithmic mu-law encoding and the
106 * latter uses 8-bit unsigned encoding.
109 basedev
= getenv("AUDIODEV");
111 basedev
= "/dev/dsp";
113 if ((fd
= open(basedev
, imode
)) == -1) {
114 PyErr_SetFromErrnoWithFilename(LinuxAudioError
, basedev
);
117 if (imode
== O_WRONLY
&& ioctl(fd
, SNDCTL_DSP_NONBLOCK
, NULL
) == -1) {
118 PyErr_SetFromErrnoWithFilename(LinuxAudioError
, basedev
);
121 if (ioctl(fd
, SNDCTL_DSP_GETFMTS
, &afmts
) == -1) {
122 PyErr_SetFromErrnoWithFilename(LinuxAudioError
, basedev
);
125 /* Create and initialize the object */
126 if ((xp
= PyObject_New(lad_t
, &Ladtype
)) == NULL
) {
132 xp
->x_icount
= xp
->x_ocount
= 0;
138 lad_dealloc(lad_t
*xp
)
140 /* if already closed, don't reclose it */
147 lad_read(lad_t
*self
, PyObject
*args
)
153 if (!PyArg_ParseTuple(args
, "i:read", &size
))
155 rv
= PyString_FromStringAndSize(NULL
, size
);
158 cp
= PyString_AS_STRING(rv
);
159 if ((count
= read(self
->x_fd
, cp
, size
)) < 0) {
160 PyErr_SetFromErrno(LinuxAudioError
);
164 self
->x_icount
+= count
;
165 if (_PyString_Resize(&rv
, count
) == -1)
171 lad_write(lad_t
*self
, PyObject
*args
)
175 fd_set write_set_fds
;
179 if (!PyArg_ParseTuple(args
, "s#:write", &cp
, &size
))
182 /* use select to wait for audio device to be available */
183 FD_ZERO(&write_set_fds
);
184 FD_SET(self
->x_fd
, &write_set_fds
);
185 tv
.tv_sec
= 4; /* timeout values */
189 select_retval
= select(self
->x_fd
+1, NULL
, &write_set_fds
, NULL
, &tv
);
190 tv
.tv_sec
= 1; tv
.tv_usec
= 0; /* willing to wait this long next time*/
192 if ((rv
= write(self
->x_fd
, cp
, size
)) == -1) {
193 if (errno
!= EAGAIN
) {
194 PyErr_SetFromErrno(LinuxAudioError
);
197 errno
= 0; /* EAGAIN: buffer is full, try again */
200 self
->x_ocount
+= rv
;
205 /* printf("Not able to write to linux audio device within %ld seconds\n", tv.tv_sec); */
206 PyErr_SetFromErrno(LinuxAudioError
);
215 lad_close(lad_t
*self
, PyObject
*args
)
217 if (!PyArg_ParseTuple(args
, ":close"))
220 if (self
->x_fd
>= 0) {
229 lad_fileno(lad_t
*self
, PyObject
*args
)
231 if (!PyArg_ParseTuple(args
, ":fileno"))
233 return PyInt_FromLong(self
->x_fd
);
237 lad_setparameters(lad_t
*self
, PyObject
*args
)
239 int rate
, ssize
, nchannels
, n
, fmt
, emulate
=0;
241 if (!PyArg_ParseTuple(args
, "iiii|i:setparameters",
242 &rate
, &ssize
, &nchannels
, &fmt
, &emulate
))
246 PyErr_Format(PyExc_ValueError
, "expected rate >= 0, not %d",
251 PyErr_Format(PyExc_ValueError
, "expected sample size >= 0, not %d",
255 if (nchannels
!= 1 && nchannels
!= 2) {
256 PyErr_Format(PyExc_ValueError
, "nchannels must be 1 or 2, not %d",
261 if (ioctl(self
->x_fd
, SNDCTL_DSP_SPEED
, &rate
) == -1) {
262 PyErr_SetFromErrno(LinuxAudioError
);
265 if (ioctl(self
->x_fd
, SNDCTL_DSP_CHANNELS
, &nchannels
) == -1) {
266 PyErr_SetFromErrno(LinuxAudioError
);
270 for (n
= 0; n
< n_audio_types
; n
++)
271 if (fmt
== audio_types
[n
].a_fmt
)
273 if (n
== n_audio_types
) {
274 PyErr_Format(PyExc_ValueError
, "unknown audio encoding: %d", fmt
);
277 if (audio_types
[n
].a_bps
!= ssize
) {
278 PyErr_Format(PyExc_ValueError
,
279 "for %s, expected sample size %d, not %d",
280 audio_types
[n
].a_name
, audio_types
[n
].a_bps
, ssize
);
285 if ((self
->x_afmts
& audio_types
[n
].a_fmt
) == 0) {
286 PyErr_Format(PyExc_ValueError
,
287 "%s format not supported by device",
288 audio_types
[n
].a_name
);
292 if (ioctl(self
->x_fd
, SNDCTL_DSP_SETFMT
,
293 &audio_types
[n
].a_fmt
) == -1) {
294 PyErr_SetFromErrno(LinuxAudioError
);
303 _ssize(lad_t
*self
, int *nchannels
, int *ssize
)
308 if (ioctl(self
->x_fd
, SNDCTL_DSP_SETFMT
, &fmt
) < 0)
316 *ssize
= sizeof(char);
322 *ssize
= sizeof(short);
330 if (ioctl(self
->x_fd
, SNDCTL_DSP_CHANNELS
, nchannels
) < 0)
336 /* bufsize returns the size of the hardware audio buffer in number
339 lad_bufsize(lad_t
*self
, PyObject
*args
)
342 int nchannels
, ssize
;
344 if (!PyArg_ParseTuple(args
, ":bufsize")) return NULL
;
346 if (_ssize(self
, &nchannels
, &ssize
) < 0) {
347 PyErr_SetFromErrno(LinuxAudioError
);
350 if (ioctl(self
->x_fd
, SNDCTL_DSP_GETOSPACE
, &ai
) < 0) {
351 PyErr_SetFromErrno(LinuxAudioError
);
354 return PyInt_FromLong((ai
.fragstotal
* ai
.fragsize
) / (nchannels
* ssize
));
357 /* obufcount returns the number of samples that are available in the
358 hardware for playing */
360 lad_obufcount(lad_t
*self
, PyObject
*args
)
363 int nchannels
, ssize
;
365 if (!PyArg_ParseTuple(args
, ":obufcount"))
368 if (_ssize(self
, &nchannels
, &ssize
) < 0) {
369 PyErr_SetFromErrno(LinuxAudioError
);
372 if (ioctl(self
->x_fd
, SNDCTL_DSP_GETOSPACE
, &ai
) < 0) {
373 PyErr_SetFromErrno(LinuxAudioError
);
376 return PyInt_FromLong((ai
.fragstotal
* ai
.fragsize
- ai
.bytes
) /
377 (ssize
* nchannels
));
380 /* obufcount returns the number of samples that can be played without
383 lad_obuffree(lad_t
*self
, PyObject
*args
)
386 int nchannels
, ssize
;
388 if (!PyArg_ParseTuple(args
, ":obuffree"))
391 if (_ssize(self
, &nchannels
, &ssize
) < 0) {
392 PyErr_SetFromErrno(LinuxAudioError
);
395 if (ioctl(self
->x_fd
, SNDCTL_DSP_GETOSPACE
, &ai
) < 0) {
396 PyErr_SetFromErrno(LinuxAudioError
);
399 return PyInt_FromLong(ai
.bytes
/ (ssize
* nchannels
));
402 /* Flush the device */
404 lad_flush(lad_t
*self
, PyObject
*args
)
406 if (!PyArg_ParseTuple(args
, ":flush")) return NULL
;
408 if (ioctl(self
->x_fd
, SNDCTL_DSP_SYNC
, NULL
) == -1) {
409 PyErr_SetFromErrno(LinuxAudioError
);
417 lad_getptr(lad_t
*self
, PyObject
*args
)
422 if (!PyArg_ParseTuple(args
, ":getptr"))
425 if (self
->x_mode
== O_RDONLY
)
426 req
= SNDCTL_DSP_GETIPTR
;
428 req
= SNDCTL_DSP_GETOPTR
;
429 if (ioctl(self
->x_fd
, req
, &info
) == -1) {
430 PyErr_SetFromErrno(LinuxAudioError
);
433 return Py_BuildValue("iii", info
.bytes
, info
.blocks
, info
.ptr
);
436 static PyMethodDef lad_methods
[] = {
437 { "read", (PyCFunction
)lad_read
, METH_VARARGS
},
438 { "write", (PyCFunction
)lad_write
, METH_VARARGS
},
439 { "setparameters", (PyCFunction
)lad_setparameters
, METH_VARARGS
},
440 { "bufsize", (PyCFunction
)lad_bufsize
, METH_VARARGS
},
441 { "obufcount", (PyCFunction
)lad_obufcount
, METH_VARARGS
},
442 { "obuffree", (PyCFunction
)lad_obuffree
, METH_VARARGS
},
443 { "flush", (PyCFunction
)lad_flush
, METH_VARARGS
},
444 { "close", (PyCFunction
)lad_close
, METH_VARARGS
},
445 { "fileno", (PyCFunction
)lad_fileno
, METH_VARARGS
},
446 { "getptr", (PyCFunction
)lad_getptr
, METH_VARARGS
},
447 { NULL
, NULL
} /* sentinel */
451 lad_getattr(lad_t
*xp
, char *name
)
453 return Py_FindMethod(lad_methods
, (PyObject
*)xp
, name
);
456 static PyTypeObject Ladtype
= {
457 PyObject_HEAD_INIT(&PyType_Type
)
459 "linux_audio_device", /*tp_name*/
460 sizeof(lad_t
), /*tp_size*/
463 (destructor
)lad_dealloc
, /*tp_dealloc*/
465 (getattrfunc
)lad_getattr
, /*tp_getattr*/
472 ladopen(PyObject
*self
, PyObject
*args
)
474 return (PyObject
*)newladobject(args
);
477 static PyMethodDef linuxaudiodev_methods
[] = {
478 { "open", ladopen
, METH_VARARGS
},
483 initlinuxaudiodev(void)
487 m
= Py_InitModule("linuxaudiodev", linuxaudiodev_methods
);
489 LinuxAudioError
= PyErr_NewException("linuxaudiodev.error", NULL
, NULL
);
491 PyModule_AddObject(m
, "error", LinuxAudioError
);
493 if (PyModule_AddIntConstant(m
, "AFMT_MU_LAW", (long)AFMT_MU_LAW
) == -1)
495 if (PyModule_AddIntConstant(m
, "AFMT_A_LAW", (long)AFMT_A_LAW
) == -1)
497 if (PyModule_AddIntConstant(m
, "AFMT_U8", (long)AFMT_U8
) == -1)
499 if (PyModule_AddIntConstant(m
, "AFMT_S8", (long)AFMT_S8
) == -1)
501 if (PyModule_AddIntConstant(m
, "AFMT_U16_BE", (long)AFMT_U16_BE
) == -1)
503 if (PyModule_AddIntConstant(m
, "AFMT_U16_LE", (long)AFMT_U16_LE
) == -1)
505 if (PyModule_AddIntConstant(m
, "AFMT_S16_BE", (long)AFMT_S16_BE
) == -1)
507 if (PyModule_AddIntConstant(m
, "AFMT_S16_LE", (long)AFMT_S16_LE
) == -1)
509 if (PyModule_AddIntConstant(m
, "AFMT_S16_NE", (long)AFMT_S16_NE
) == -1)