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 * Last Modified By: Peter Bosch
8 * Last Modified On: Fri Mar 24 11:27:00 2000
9 * Status : Unknown, Use with caution!
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 ******************************************************************************
19 #include "structmember.h"
33 #include <sys/ioctl.h>
35 #include <linux/soundcard.h>
37 typedef unsigned long uint32_t;
39 #elif defined(__FreeBSD__)
40 #include <machine/soundcard.h>
42 #ifndef SNDCTL_DSP_CHANNELS
43 #define SNDCTL_DSP_CHANNELS SOUND_PCM_WRITE_CHANNELS
50 int x_fd
; /* The open file */
51 int x_mode
; /* file mode */
52 int x_icount
; /* Input count */
53 int x_ocount
; /* Output count */
54 uint32_t x_afmts
; /* Audio formats supported by hardware*/
57 /* XXX several format defined in soundcard.h are not supported,
58 including _NE (native endian) options and S32 options
66 { 8, AFMT_MU_LAW
, "logarithmic mu-law 8-bit audio" },
67 { 8, AFMT_A_LAW
, "logarithmic A-law 8-bit audio" },
68 { 8, AFMT_U8
, "linear unsigned 8-bit audio" },
69 { 8, AFMT_S8
, "linear signed 8-bit audio" },
70 { 16, AFMT_U16_BE
, "linear unsigned 16-bit big-endian audio" },
71 { 16, AFMT_U16_LE
, "linear unsigned 16-bit little-endian audio" },
72 { 16, AFMT_S16_BE
, "linear signed 16-bit big-endian audio" },
73 { 16, AFMT_S16_LE
, "linear signed 16-bit little-endian audio" },
74 { 16, AFMT_S16_NE
, "linear signed 16-bit native-endian audio" },
77 static int n_audio_types
= sizeof(audio_types
) / sizeof(audio_types
[0]);
79 staticforward PyTypeObject Ladtype
;
81 static PyObject
*LinuxAudioError
;
84 newladobject(PyObject
*arg
)
91 /* Check arg for r/w/rw */
92 if (!PyArg_ParseTuple(arg
, "s:open", &mode
)) return NULL
;
93 if (strcmp(mode
, "r") == 0)
95 else if (strcmp(mode
, "w") == 0)
98 PyErr_SetString(LinuxAudioError
, "mode should be 'r' or 'w'");
102 /* Open the correct device. The base device name comes from the
103 * AUDIODEV environment variable first, then /dev/dsp. The
104 * control device tacks "ctl" onto the base device name.
106 * Note that the only difference between /dev/audio and /dev/dsp
107 * is that the former uses logarithmic mu-law encoding and the
108 * latter uses 8-bit unsigned encoding.
111 basedev
= getenv("AUDIODEV");
113 basedev
= "/dev/dsp";
115 if ((fd
= open(basedev
, imode
)) == -1) {
116 PyErr_SetFromErrnoWithFilename(LinuxAudioError
, basedev
);
119 if (imode
== O_WRONLY
&& ioctl(fd
, SNDCTL_DSP_NONBLOCK
, NULL
) == -1) {
120 PyErr_SetFromErrnoWithFilename(LinuxAudioError
, basedev
);
123 if (ioctl(fd
, SNDCTL_DSP_GETFMTS
, &afmts
) == -1) {
124 PyErr_SetFromErrnoWithFilename(LinuxAudioError
, basedev
);
127 /* Create and initialize the object */
128 if ((xp
= PyObject_New(lad_t
, &Ladtype
)) == NULL
) {
134 xp
->x_icount
= xp
->x_ocount
= 0;
140 lad_dealloc(lad_t
*xp
)
142 /* if already closed, don't reclose it */
149 lad_read(lad_t
*self
, PyObject
*args
)
155 if (!PyArg_ParseTuple(args
, "i:read", &size
))
157 rv
= PyString_FromStringAndSize(NULL
, size
);
160 cp
= PyString_AS_STRING(rv
);
161 if ((count
= read(self
->x_fd
, cp
, size
)) < 0) {
162 PyErr_SetFromErrno(LinuxAudioError
);
166 self
->x_icount
+= count
;
167 if (_PyString_Resize(&rv
, count
) == -1)
173 lad_write(lad_t
*self
, PyObject
*args
)
178 if (!PyArg_ParseTuple(args
, "s#:write", &cp
, &size
))
182 if ((rv
= write(self
->x_fd
, cp
, size
)) == -1) {
183 PyErr_SetFromErrno(LinuxAudioError
);
186 self
->x_ocount
+= rv
;
195 lad_close(lad_t
*self
, PyObject
*args
)
197 if (!PyArg_ParseTuple(args
, ":close"))
200 if (self
->x_fd
>= 0) {
209 lad_fileno(lad_t
*self
, PyObject
*args
)
211 if (!PyArg_ParseTuple(args
, ":fileno"))
213 return PyInt_FromLong(self
->x_fd
);
217 lad_setparameters(lad_t
*self
, PyObject
*args
)
219 int rate
, ssize
, nchannels
, n
, fmt
, emulate
=0;
221 if (!PyArg_ParseTuple(args
, "iiii|i:setparameters",
222 &rate
, &ssize
, &nchannels
, &fmt
, &emulate
))
226 PyErr_Format(PyExc_ValueError
, "expected rate >= 0, not %d",
231 PyErr_Format(PyExc_ValueError
, "expected sample size >= 0, not %d",
235 if (nchannels
!= 1 && nchannels
!= 2) {
236 PyErr_Format(PyExc_ValueError
, "nchannels must be 1 or 2, not %d",
241 if (ioctl(self
->x_fd
, SNDCTL_DSP_SPEED
, &rate
) == -1) {
242 PyErr_SetFromErrno(LinuxAudioError
);
245 if (ioctl(self
->x_fd
, SNDCTL_DSP_CHANNELS
, &nchannels
) == -1) {
246 PyErr_SetFromErrno(LinuxAudioError
);
250 for (n
= 0; n
< n_audio_types
; n
++)
251 if (fmt
== audio_types
[n
].a_fmt
)
253 if (n
== n_audio_types
) {
254 PyErr_Format(PyExc_ValueError
, "unknown audio encoding: %d", fmt
);
257 if (audio_types
[n
].a_bps
!= ssize
) {
258 PyErr_Format(PyExc_ValueError
,
259 "for %s, expected sample size %d, not %d",
260 audio_types
[n
].a_name
, audio_types
[n
].a_bps
, ssize
);
265 if ((self
->x_afmts
& audio_types
[n
].a_fmt
) == 0) {
266 PyErr_Format(PyExc_ValueError
,
267 "%s format not supported by device",
268 audio_types
[n
].a_name
);
272 if (ioctl(self
->x_fd
, SNDCTL_DSP_SETFMT
,
273 &audio_types
[n
].a_fmt
) == -1) {
274 PyErr_SetFromErrno(LinuxAudioError
);
283 _ssize(lad_t
*self
, int *nchannels
, int *ssize
)
288 if (ioctl(self
->x_fd
, SNDCTL_DSP_SETFMT
, &fmt
) < 0)
296 *ssize
= sizeof(char);
302 *ssize
= sizeof(short);
310 if (ioctl(self
->x_fd
, SNDCTL_DSP_CHANNELS
, nchannels
) < 0)
316 /* bufsize returns the size of the hardware audio buffer in number
319 lad_bufsize(lad_t
*self
, PyObject
*args
)
322 int nchannels
, ssize
;
324 if (!PyArg_ParseTuple(args
, ":bufsize")) return NULL
;
326 if (_ssize(self
, &nchannels
, &ssize
) < 0) {
327 PyErr_SetFromErrno(LinuxAudioError
);
330 if (ioctl(self
->x_fd
, SNDCTL_DSP_GETOSPACE
, &ai
) < 0) {
331 PyErr_SetFromErrno(LinuxAudioError
);
334 return PyInt_FromLong((ai
.fragstotal
* ai
.fragsize
) / (nchannels
* ssize
));
337 /* obufcount returns the number of samples that are available in the
338 hardware for playing */
340 lad_obufcount(lad_t
*self
, PyObject
*args
)
343 int nchannels
, ssize
;
345 if (!PyArg_ParseTuple(args
, ":obufcount"))
348 if (_ssize(self
, &nchannels
, &ssize
) < 0) {
349 PyErr_SetFromErrno(LinuxAudioError
);
352 if (ioctl(self
->x_fd
, SNDCTL_DSP_GETOSPACE
, &ai
) < 0) {
353 PyErr_SetFromErrno(LinuxAudioError
);
356 return PyInt_FromLong((ai
.fragstotal
* ai
.fragsize
- ai
.bytes
) /
357 (ssize
* nchannels
));
360 /* obufcount returns the number of samples that can be played without
363 lad_obuffree(lad_t
*self
, PyObject
*args
)
366 int nchannels
, ssize
;
368 if (!PyArg_ParseTuple(args
, ":obuffree"))
371 if (_ssize(self
, &nchannels
, &ssize
) < 0) {
372 PyErr_SetFromErrno(LinuxAudioError
);
375 if (ioctl(self
->x_fd
, SNDCTL_DSP_GETOSPACE
, &ai
) < 0) {
376 PyErr_SetFromErrno(LinuxAudioError
);
379 return PyInt_FromLong(ai
.bytes
/ (ssize
* nchannels
));
382 /* Flush the device */
384 lad_flush(lad_t
*self
, PyObject
*args
)
386 if (!PyArg_ParseTuple(args
, ":flush")) return NULL
;
388 if (ioctl(self
->x_fd
, SNDCTL_DSP_SYNC
, NULL
) == -1) {
389 PyErr_SetFromErrno(LinuxAudioError
);
397 lad_getptr(lad_t
*self
, PyObject
*args
)
402 if (!PyArg_ParseTuple(args
, ":getptr"))
405 if (self
->x_mode
== O_RDONLY
)
406 req
= SNDCTL_DSP_GETIPTR
;
408 req
= SNDCTL_DSP_GETOPTR
;
409 if (ioctl(self
->x_fd
, req
, &info
) == -1) {
410 PyErr_SetFromErrno(LinuxAudioError
);
413 return Py_BuildValue("iii", info
.bytes
, info
.blocks
, info
.ptr
);
416 static PyMethodDef lad_methods
[] = {
417 { "read", (PyCFunction
)lad_read
, METH_VARARGS
},
418 { "write", (PyCFunction
)lad_write
, METH_VARARGS
},
419 { "setparameters", (PyCFunction
)lad_setparameters
, METH_VARARGS
},
420 { "bufsize", (PyCFunction
)lad_bufsize
, METH_VARARGS
},
421 { "obufcount", (PyCFunction
)lad_obufcount
, METH_VARARGS
},
422 { "obuffree", (PyCFunction
)lad_obuffree
, METH_VARARGS
},
423 { "flush", (PyCFunction
)lad_flush
, METH_VARARGS
},
424 { "close", (PyCFunction
)lad_close
, METH_VARARGS
},
425 { "fileno", (PyCFunction
)lad_fileno
, METH_VARARGS
},
426 { "getptr", (PyCFunction
)lad_getptr
, METH_VARARGS
},
427 { NULL
, NULL
} /* sentinel */
431 lad_getattr(lad_t
*xp
, char *name
)
433 return Py_FindMethod(lad_methods
, (PyObject
*)xp
, name
);
436 static PyTypeObject Ladtype
= {
437 PyObject_HEAD_INIT(&PyType_Type
)
439 "linux_audio_device", /*tp_name*/
440 sizeof(lad_t
), /*tp_size*/
443 (destructor
)lad_dealloc
, /*tp_dealloc*/
445 (getattrfunc
)lad_getattr
, /*tp_getattr*/
452 ladopen(PyObject
*self
, PyObject
*args
)
454 return (PyObject
*)newladobject(args
);
457 static PyMethodDef linuxaudiodev_methods
[] = {
458 { "open", ladopen
, METH_VARARGS
},
463 initlinuxaudiodev(void)
467 m
= Py_InitModule("linuxaudiodev", linuxaudiodev_methods
);
469 LinuxAudioError
= PyErr_NewException("linuxaudiodev.error", NULL
, NULL
);
471 PyModule_AddObject(m
, "error", LinuxAudioError
);
473 if (PyModule_AddIntConstant(m
, "AFMT_MU_LAW", (long)AFMT_MU_LAW
) == -1)
475 if (PyModule_AddIntConstant(m
, "AFMT_A_LAW", (long)AFMT_A_LAW
) == -1)
477 if (PyModule_AddIntConstant(m
, "AFMT_U8", (long)AFMT_U8
) == -1)
479 if (PyModule_AddIntConstant(m
, "AFMT_S8", (long)AFMT_S8
) == -1)
481 if (PyModule_AddIntConstant(m
, "AFMT_U16_BE", (long)AFMT_U16_BE
) == -1)
483 if (PyModule_AddIntConstant(m
, "AFMT_U16_LE", (long)AFMT_U16_LE
) == -1)
485 if (PyModule_AddIntConstant(m
, "AFMT_S16_BE", (long)AFMT_S16_BE
) == -1)
487 if (PyModule_AddIntConstant(m
, "AFMT_S16_LE", (long)AFMT_S16_LE
) == -1)
489 if (PyModule_AddIntConstant(m
, "AFMT_S16_NE", (long)AFMT_S16_NE
) == -1)