2 * ossaudiodev -- Python interface to the OSS (Open Sound System) API.
3 * This is the standard audio API for Linux and some
4 * flavours of BSD [XXX which ones?]; it is also available
5 * for a wide range of commercial Unices.
7 * Originally written by Peter Bosch, March 2000, as linuxaudiodev.
9 * Renamed to ossaudiodev and rearranged/revised/hacked up
10 * by Greg Ward <gward@python.net>, November 2002.
11 * Mixer interface by Nicholas FitzRoy-Dale <wzdd@lardcave.net>, Dec 2002.
13 * (c) 2000 Peter Bosch. All Rights Reserved.
14 * (c) 2002 Gregory P. Ward. All Rights Reserved.
15 * (c) 2002 Python Software Foundation. All Rights Reserved.
17 * XXX need a license statement
23 #include "structmember.h"
32 #include <sys/ioctl.h>
33 #include <sys/soundcard.h>
37 typedef unsigned long uint32_t;
39 #elif defined(__FreeBSD__)
41 # ifndef SNDCTL_DSP_CHANNELS
42 # define SNDCTL_DSP_CHANNELS SOUND_PCM_WRITE_CHANNELS
49 char *devicename
; /* name of the device file */
50 int fd
; /* file descriptor */
51 int mode
; /* file mode (O_RDONLY, etc.) */
52 int icount
; /* input count */
53 int ocount
; /* output count */
54 uint32_t afmts
; /* audio formats supported by hardware */
59 int fd
; /* The open mixer device */
63 static PyTypeObject OSSAudioType
;
64 static PyTypeObject OSSMixerType
;
66 static PyObject
*OSSAudioError
;
69 /* ----------------------------------------------------------------------
70 * DSP object initialization/deallocation
74 newossobject(PyObject
*arg
)
78 char *devicename
= NULL
;
81 /* Two ways to call open():
82 open(device, mode) (for consistency with builtin open())
83 open(mode) (for backwards compatibility)
84 because the *first* argument is optional, parsing args is
86 if (!PyArg_ParseTuple(arg
, "s|s:open", &devicename
, &mode
))
88 if (mode
== NULL
) { /* only one arg supplied */
93 if (strcmp(mode
, "r") == 0)
95 else if (strcmp(mode
, "w") == 0)
97 else if (strcmp(mode
, "rw") == 0)
100 PyErr_SetString(OSSAudioError
, "mode must be 'r', 'w', or 'rw'");
104 /* Open the correct device: either the 'device' argument,
105 or the AUDIODEV environment variable, or "/dev/dsp". */
106 if (devicename
== NULL
) { /* called with one arg */
107 devicename
= getenv("AUDIODEV");
108 if (devicename
== NULL
) /* $AUDIODEV not set */
109 devicename
= "/dev/dsp";
112 /* Open with O_NONBLOCK to avoid hanging on devices that only allow
113 one open at a time. This does *not* affect later I/O; OSS
114 provides a special ioctl() for non-blocking read/write, which is
115 exposed via oss_nonblock() below. */
116 if ((fd
= open(devicename
, imode
|O_NONBLOCK
)) == -1) {
117 PyErr_SetFromErrnoWithFilename(PyExc_IOError
, devicename
);
121 /* And (try to) put it back in blocking mode so we get the
122 expected write() semantics. */
123 if (fcntl(fd
, F_SETFL
, 0) == -1) {
125 PyErr_SetFromErrnoWithFilename(PyExc_IOError
, devicename
);
129 if (ioctl(fd
, SNDCTL_DSP_GETFMTS
, &afmts
) == -1) {
130 PyErr_SetFromErrnoWithFilename(PyExc_IOError
, devicename
);
133 /* Create and initialize the object */
134 if ((self
= PyObject_New(oss_audio_t
, &OSSAudioType
)) == NULL
) {
138 self
->devicename
= devicename
;
141 self
->icount
= self
->ocount
= 0;
147 oss_dealloc(oss_audio_t
*self
)
149 /* if already closed, don't reclose it */
156 /* ----------------------------------------------------------------------
157 * Mixer object initialization/deallocation
161 newossmixerobject(PyObject
*arg
)
163 char *devicename
= NULL
;
167 if (!PyArg_ParseTuple(arg
, "|s", &devicename
)) {
171 if (devicename
== NULL
) {
172 devicename
= getenv("MIXERDEV");
173 if (devicename
== NULL
) /* MIXERDEV not set */
174 devicename
= "/dev/mixer";
177 if ((fd
= open(devicename
, O_RDWR
)) == -1) {
178 PyErr_SetFromErrnoWithFilename(PyExc_IOError
, devicename
);
182 if ((self
= PyObject_New(oss_mixer_t
, &OSSMixerType
)) == NULL
) {
193 oss_mixer_dealloc(oss_mixer_t
*self
)
195 /* if already closed, don't reclose it */
202 /* Methods to wrap the OSS ioctls. The calling convention is pretty
204 nonblock() -> ioctl(fd, SNDCTL_DSP_NONBLOCK)
205 fmt = setfmt(fmt) -> ioctl(fd, SNDCTL_DSP_SETFMT, &fmt)
210 /* ----------------------------------------------------------------------
214 /* _do_ioctl_1() is a private helper function used for the OSS ioctls --
215 SNDCTL_DSP_{SETFMT,CHANNELS,SPEED} -- that that are called from C
217 ioctl(fd, SNDCTL_DSP_cmd, &arg)
219 where arg is the value to set, and on return the driver sets arg to
220 the value that was actually set. Mapping this to Python is obvious:
224 _do_ioctl_1(int fd
, PyObject
*args
, char *fname
, int cmd
)
226 char argfmt
[33] = "i:";
229 assert(strlen(fname
) <= 30);
230 strcat(argfmt
, fname
);
231 if (!PyArg_ParseTuple(args
, argfmt
, &arg
))
234 if (ioctl(fd
, cmd
, &arg
) == -1)
235 return PyErr_SetFromErrno(PyExc_IOError
);
236 return PyInt_FromLong(arg
);
240 /* _do_ioctl_1_internal() is a wrapper for ioctls that take no inputs
241 but return an output -- ie. we need to pass a pointer to a local C
242 variable so the driver can write its output there, but from Python
243 all we see is the return value. For example,
244 SOUND_MIXER_READ_DEVMASK returns a bitmask of available mixer
245 devices, but does not use the value of the parameter passed-in in any
249 _do_ioctl_1_internal(int fd
, PyObject
*args
, char *fname
, int cmd
)
251 char argfmt
[32] = ":";
254 assert(strlen(fname
) <= 30);
255 strcat(argfmt
, fname
);
256 if (!PyArg_ParseTuple(args
, argfmt
, &arg
))
259 if (ioctl(fd
, cmd
, &arg
) == -1)
260 return PyErr_SetFromErrno(PyExc_IOError
);
261 return PyInt_FromLong(arg
);
266 /* _do_ioctl_0() is a private helper for the no-argument ioctls:
267 SNDCTL_DSP_{SYNC,RESET,POST}. */
269 _do_ioctl_0(int fd
, PyObject
*args
, char *fname
, int cmd
)
271 char argfmt
[32] = ":";
274 assert(strlen(fname
) <= 30);
275 strcat(argfmt
, fname
);
276 if (!PyArg_ParseTuple(args
, argfmt
))
279 /* According to hannu@opensound.com, all three of the ioctls that
280 use this function can block, so release the GIL. This is
281 especially important for SYNC, which can block for several
283 Py_BEGIN_ALLOW_THREADS
284 rv
= ioctl(fd
, cmd
, 0);
288 return PyErr_SetFromErrno(PyExc_IOError
);
294 /* ----------------------------------------------------------------------
295 * Methods of DSP objects (OSSAudioType)
299 oss_nonblock(oss_audio_t
*self
, PyObject
*args
)
301 /* Hmmm: it doesn't appear to be possible to return to blocking
302 mode once we're in non-blocking mode! */
303 if (!PyArg_ParseTuple(args
, ":nonblock"))
305 if (ioctl(self
->fd
, SNDCTL_DSP_NONBLOCK
, NULL
) == -1)
306 return PyErr_SetFromErrno(PyExc_IOError
);
312 oss_setfmt(oss_audio_t
*self
, PyObject
*args
)
314 return _do_ioctl_1(self
->fd
, args
, "setfmt", SNDCTL_DSP_SETFMT
);
318 oss_getfmts(oss_audio_t
*self
, PyObject
*args
)
321 if (!PyArg_ParseTuple(args
, ":getfmts"))
323 if (ioctl(self
->fd
, SNDCTL_DSP_GETFMTS
, &mask
) == -1)
324 return PyErr_SetFromErrno(PyExc_IOError
);
325 return PyInt_FromLong(mask
);
329 oss_channels(oss_audio_t
*self
, PyObject
*args
)
331 return _do_ioctl_1(self
->fd
, args
, "channels", SNDCTL_DSP_CHANNELS
);
335 oss_speed(oss_audio_t
*self
, PyObject
*args
)
337 return _do_ioctl_1(self
->fd
, args
, "speed", SNDCTL_DSP_SPEED
);
341 oss_sync(oss_audio_t
*self
, PyObject
*args
)
343 return _do_ioctl_0(self
->fd
, args
, "sync", SNDCTL_DSP_SYNC
);
347 oss_reset(oss_audio_t
*self
, PyObject
*args
)
349 return _do_ioctl_0(self
->fd
, args
, "reset", SNDCTL_DSP_RESET
);
353 oss_post(oss_audio_t
*self
, PyObject
*args
)
355 return _do_ioctl_0(self
->fd
, args
, "post", SNDCTL_DSP_POST
);
359 /* Regular file methods: read(), write(), close(), etc. as well
360 as one convenience method, writeall(). */
363 oss_read(oss_audio_t
*self
, PyObject
*args
)
369 if (!PyArg_ParseTuple(args
, "i:read", &size
))
371 rv
= PyString_FromStringAndSize(NULL
, size
);
374 cp
= PyString_AS_STRING(rv
);
376 Py_BEGIN_ALLOW_THREADS
377 count
= read(self
->fd
, cp
, size
);
381 PyErr_SetFromErrno(PyExc_IOError
);
385 self
->icount
+= count
;
386 _PyString_Resize(&rv
, count
);
391 oss_write(oss_audio_t
*self
, PyObject
*args
)
396 if (!PyArg_ParseTuple(args
, "s#:write", &cp
, &size
)) {
400 Py_BEGIN_ALLOW_THREADS
401 rv
= write(self
->fd
, cp
, size
);
405 return PyErr_SetFromErrno(PyExc_IOError
);
409 return PyInt_FromLong(rv
);
413 oss_writeall(oss_audio_t
*self
, PyObject
*args
)
417 fd_set write_set_fds
;
420 /* NB. writeall() is only useful in non-blocking mode: according to
421 Guenter Geiger <geiger@xdv.org> on the linux-audio-dev list
422 (http://eca.cx/lad/2002/11/0380.html), OSS guarantees that
423 write() in blocking mode consumes the whole buffer. In blocking
424 mode, the behaviour of write() and writeall() from Python is
425 indistinguishable. */
427 if (!PyArg_ParseTuple(args
, "s#:write", &cp
, &size
))
430 /* use select to wait for audio device to be available */
431 FD_ZERO(&write_set_fds
);
432 FD_SET(self
->fd
, &write_set_fds
);
435 Py_BEGIN_ALLOW_THREADS
436 select_rv
= select(self
->fd
+1, NULL
, &write_set_fds
, NULL
, NULL
);
438 assert(select_rv
!= 0); /* no timeout, can't expire */
440 return PyErr_SetFromErrno(PyExc_IOError
);
442 Py_BEGIN_ALLOW_THREADS
443 rv
= write(self
->fd
, cp
, size
);
446 if (errno
== EAGAIN
) { /* buffer is full, try again */
449 } else /* it's a real error */
450 return PyErr_SetFromErrno(PyExc_IOError
);
451 } else { /* wrote rv bytes */
462 oss_close(oss_audio_t
*self
, PyObject
*args
)
464 if (!PyArg_ParseTuple(args
, ":close"))
468 Py_BEGIN_ALLOW_THREADS
478 oss_fileno(oss_audio_t
*self
, PyObject
*args
)
480 if (!PyArg_ParseTuple(args
, ":fileno"))
482 return PyInt_FromLong(self
->fd
);
486 /* Convenience methods: these generally wrap a couple of ioctls into one
490 oss_setparameters(oss_audio_t
*self
, PyObject
*args
)
492 int wanted_fmt
, wanted_channels
, wanted_rate
, strict
=0;
493 int fmt
, channels
, rate
;
494 PyObject
* rv
; /* return tuple (fmt, channels, rate) */
496 if (!PyArg_ParseTuple(args
, "iii|i:setparameters",
497 &wanted_fmt
, &wanted_channels
, &wanted_rate
,
502 if (ioctl(self
->fd
, SNDCTL_DSP_SETFMT
, &fmt
) == -1) {
503 return PyErr_SetFromErrno(PyExc_IOError
);
505 if (strict
&& fmt
!= wanted_fmt
) {
508 "unable to set requested format (wanted %d, got %d)",
512 channels
= wanted_channels
;
513 if (ioctl(self
->fd
, SNDCTL_DSP_CHANNELS
, &channels
) == -1) {
514 return PyErr_SetFromErrno(PyExc_IOError
);
516 if (strict
&& channels
!= wanted_channels
) {
519 "unable to set requested channels (wanted %d, got %d)",
520 wanted_channels
, channels
);
524 if (ioctl(self
->fd
, SNDCTL_DSP_SPEED
, &rate
) == -1) {
525 return PyErr_SetFromErrno(PyExc_IOError
);
527 if (strict
&& rate
!= wanted_rate
) {
530 "unable to set requested rate (wanted %d, got %d)",
534 /* Construct the return value: a (fmt, channels, rate) tuple that
535 tells what the audio hardware was actually set to. */
539 PyTuple_SET_ITEM(rv
, 0, PyInt_FromLong(fmt
));
540 PyTuple_SET_ITEM(rv
, 1, PyInt_FromLong(channels
));
541 PyTuple_SET_ITEM(rv
, 2, PyInt_FromLong(rate
));
546 _ssize(oss_audio_t
*self
, int *nchannels
, int *ssize
)
551 if (ioctl(self
->fd
, SNDCTL_DSP_SETFMT
, &fmt
) < 0)
559 *ssize
= 1; /* 8 bit formats: 1 byte */
565 *ssize
= 2; /* 16 bit formats: 2 byte */
573 if (ioctl(self
->fd
, SNDCTL_DSP_CHANNELS
, nchannels
) < 0)
579 /* bufsize returns the size of the hardware audio buffer in number
582 oss_bufsize(oss_audio_t
*self
, PyObject
*args
)
585 int nchannels
, ssize
;
587 if (!PyArg_ParseTuple(args
, ":bufsize")) return NULL
;
589 if (_ssize(self
, &nchannels
, &ssize
) < 0) {
590 PyErr_SetFromErrno(PyExc_IOError
);
593 if (ioctl(self
->fd
, SNDCTL_DSP_GETOSPACE
, &ai
) < 0) {
594 PyErr_SetFromErrno(PyExc_IOError
);
597 return PyInt_FromLong((ai
.fragstotal
* ai
.fragsize
) / (nchannels
* ssize
));
600 /* obufcount returns the number of samples that are available in the
601 hardware for playing */
603 oss_obufcount(oss_audio_t
*self
, PyObject
*args
)
606 int nchannels
, ssize
;
608 if (!PyArg_ParseTuple(args
, ":obufcount"))
611 if (_ssize(self
, &nchannels
, &ssize
) < 0) {
612 PyErr_SetFromErrno(PyExc_IOError
);
615 if (ioctl(self
->fd
, SNDCTL_DSP_GETOSPACE
, &ai
) < 0) {
616 PyErr_SetFromErrno(PyExc_IOError
);
619 return PyInt_FromLong((ai
.fragstotal
* ai
.fragsize
- ai
.bytes
) /
620 (ssize
* nchannels
));
623 /* obufcount returns the number of samples that can be played without
626 oss_obuffree(oss_audio_t
*self
, PyObject
*args
)
629 int nchannels
, ssize
;
631 if (!PyArg_ParseTuple(args
, ":obuffree"))
634 if (_ssize(self
, &nchannels
, &ssize
) < 0) {
635 PyErr_SetFromErrno(PyExc_IOError
);
638 if (ioctl(self
->fd
, SNDCTL_DSP_GETOSPACE
, &ai
) < 0) {
639 PyErr_SetFromErrno(PyExc_IOError
);
642 return PyInt_FromLong(ai
.bytes
/ (ssize
* nchannels
));
646 oss_getptr(oss_audio_t
*self
, PyObject
*args
)
651 if (!PyArg_ParseTuple(args
, ":getptr"))
654 if (self
->mode
== O_RDONLY
)
655 req
= SNDCTL_DSP_GETIPTR
;
657 req
= SNDCTL_DSP_GETOPTR
;
658 if (ioctl(self
->fd
, req
, &info
) == -1) {
659 PyErr_SetFromErrno(PyExc_IOError
);
662 return Py_BuildValue("iii", info
.bytes
, info
.blocks
, info
.ptr
);
666 /* ----------------------------------------------------------------------
667 * Methods of mixer objects (OSSMixerType)
671 oss_mixer_close(oss_mixer_t
*self
, PyObject
*args
)
673 if (!PyArg_ParseTuple(args
, ":close"))
685 oss_mixer_fileno(oss_mixer_t
*self
, PyObject
*args
)
687 if (!PyArg_ParseTuple(args
, ":fileno"))
689 return PyInt_FromLong(self
->fd
);
692 /* Simple mixer interface methods */
695 oss_mixer_controls(oss_mixer_t
*self
, PyObject
*args
)
697 return _do_ioctl_1_internal(self
->fd
, args
, "controls",
698 SOUND_MIXER_READ_DEVMASK
);
702 oss_mixer_stereocontrols(oss_mixer_t
*self
, PyObject
*args
)
704 return _do_ioctl_1_internal(self
->fd
, args
, "stereocontrols",
705 SOUND_MIXER_READ_STEREODEVS
);
709 oss_mixer_reccontrols(oss_mixer_t
*self
, PyObject
*args
)
711 return _do_ioctl_1_internal(self
->fd
, args
, "reccontrols",
712 SOUND_MIXER_READ_RECMASK
);
716 oss_mixer_get(oss_mixer_t
*self
, PyObject
*args
)
720 /* Can't use _do_ioctl_1 because of encoded arg thingy. */
721 if (!PyArg_ParseTuple(args
, "i:get", &channel
))
724 if (channel
< 0 || channel
> SOUND_MIXER_NRDEVICES
) {
725 PyErr_SetString(OSSAudioError
, "Invalid mixer channel specified.");
729 if (ioctl(self
->fd
, MIXER_READ(channel
), &volume
) == -1)
730 return PyErr_SetFromErrno(PyExc_IOError
);
732 return Py_BuildValue("(ii)", volume
& 0xff, (volume
& 0xff00) >> 8);
736 oss_mixer_set(oss_mixer_t
*self
, PyObject
*args
)
738 int channel
, volume
, leftVol
, rightVol
;
740 /* Can't use _do_ioctl_1 because of encoded arg thingy. */
741 if (!PyArg_ParseTuple(args
, "i(ii):set", &channel
, &leftVol
, &rightVol
))
744 if (channel
< 0 || channel
> SOUND_MIXER_NRDEVICES
) {
745 PyErr_SetString(OSSAudioError
, "Invalid mixer channel specified.");
749 if (leftVol
< 0 || rightVol
< 0 || leftVol
> 100 || rightVol
> 100) {
750 PyErr_SetString(OSSAudioError
, "Volumes must be between 0 and 100.");
754 volume
= (rightVol
<< 8) | leftVol
;
756 if (ioctl(self
->fd
, MIXER_WRITE(channel
), &volume
) == -1)
757 return PyErr_SetFromErrno(PyExc_IOError
);
759 return Py_BuildValue("(ii)", volume
& 0xff, (volume
& 0xff00) >> 8);
763 oss_mixer_get_recsrc(oss_mixer_t
*self
, PyObject
*args
)
765 return _do_ioctl_1_internal(self
->fd
, args
, "get_recsrc",
766 SOUND_MIXER_READ_RECSRC
);
770 oss_mixer_set_recsrc(oss_mixer_t
*self
, PyObject
*args
)
772 return _do_ioctl_1(self
->fd
, args
, "set_recsrc",
773 SOUND_MIXER_WRITE_RECSRC
);
777 /* ----------------------------------------------------------------------
778 * Method tables and other bureaucracy
781 static PyMethodDef oss_methods
[] = {
782 /* Regular file methods */
783 { "read", (PyCFunction
)oss_read
, METH_VARARGS
},
784 { "write", (PyCFunction
)oss_write
, METH_VARARGS
},
785 { "writeall", (PyCFunction
)oss_writeall
, METH_VARARGS
},
786 { "close", (PyCFunction
)oss_close
, METH_VARARGS
},
787 { "fileno", (PyCFunction
)oss_fileno
, METH_VARARGS
},
789 /* Simple ioctl wrappers */
790 { "nonblock", (PyCFunction
)oss_nonblock
, METH_VARARGS
},
791 { "setfmt", (PyCFunction
)oss_setfmt
, METH_VARARGS
},
792 { "getfmts", (PyCFunction
)oss_getfmts
, METH_VARARGS
},
793 { "channels", (PyCFunction
)oss_channels
, METH_VARARGS
},
794 { "speed", (PyCFunction
)oss_speed
, METH_VARARGS
},
795 { "sync", (PyCFunction
)oss_sync
, METH_VARARGS
},
796 { "reset", (PyCFunction
)oss_reset
, METH_VARARGS
},
797 { "post", (PyCFunction
)oss_post
, METH_VARARGS
},
799 /* Convenience methods -- wrap a couple of ioctls together */
800 { "setparameters", (PyCFunction
)oss_setparameters
, METH_VARARGS
},
801 { "bufsize", (PyCFunction
)oss_bufsize
, METH_VARARGS
},
802 { "obufcount", (PyCFunction
)oss_obufcount
, METH_VARARGS
},
803 { "obuffree", (PyCFunction
)oss_obuffree
, METH_VARARGS
},
804 { "getptr", (PyCFunction
)oss_getptr
, METH_VARARGS
},
806 /* Aliases for backwards compatibility */
807 { "flush", (PyCFunction
)oss_sync
, METH_VARARGS
},
809 { NULL
, NULL
} /* sentinel */
812 static PyMethodDef oss_mixer_methods
[] = {
813 /* Regular file method - OSS mixers are ioctl-only interface */
814 { "close", (PyCFunction
)oss_mixer_close
, METH_VARARGS
},
815 { "fileno", (PyCFunction
)oss_mixer_fileno
, METH_VARARGS
},
817 /* Simple ioctl wrappers */
818 { "controls", (PyCFunction
)oss_mixer_controls
, METH_VARARGS
},
819 { "stereocontrols", (PyCFunction
)oss_mixer_stereocontrols
, METH_VARARGS
},
820 { "reccontrols", (PyCFunction
)oss_mixer_reccontrols
, METH_VARARGS
},
821 { "get", (PyCFunction
)oss_mixer_get
, METH_VARARGS
},
822 { "set", (PyCFunction
)oss_mixer_set
, METH_VARARGS
},
823 { "get_recsrc", (PyCFunction
)oss_mixer_get_recsrc
, METH_VARARGS
},
824 { "set_recsrc", (PyCFunction
)oss_mixer_set_recsrc
, METH_VARARGS
},
830 oss_getattr(oss_audio_t
*self
, char *name
)
832 PyObject
* rval
= NULL
;
833 if (strcmp(name
, "closed") == 0) {
834 rval
= (self
->fd
== -1) ? Py_True
: Py_False
;
837 else if (strcmp(name
, "name") == 0) {
838 rval
= PyString_FromString(self
->devicename
);
840 else if (strcmp(name
, "mode") == 0) {
841 /* No need for a "default" in this switch: from newossobject(),
842 self->mode can only be one of these three values. */
845 rval
= PyString_FromString("r");
848 rval
= PyString_FromString("rw");
851 rval
= PyString_FromString("w");
856 rval
= Py_FindMethod(oss_methods
, (PyObject
*)self
, name
);
862 oss_mixer_getattr(oss_mixer_t
*self
, char *name
)
864 return Py_FindMethod(oss_mixer_methods
, (PyObject
*)self
, name
);
867 static PyTypeObject OSSAudioType
= {
868 PyObject_HEAD_INIT(&PyType_Type
)
870 "ossaudiodev.oss_audio_device", /*tp_name*/
871 sizeof(oss_audio_t
), /*tp_size*/
874 (destructor
)oss_dealloc
, /*tp_dealloc*/
876 (getattrfunc
)oss_getattr
, /*tp_getattr*/
882 static PyTypeObject OSSMixerType
= {
883 PyObject_HEAD_INIT(&PyType_Type
)
885 "ossaudiodev.oss_mixer_device", /*tp_name*/
886 sizeof(oss_mixer_t
), /*tp_size*/
889 (destructor
)oss_mixer_dealloc
, /*tp_dealloc*/
891 (getattrfunc
)oss_mixer_getattr
, /*tp_getattr*/
899 ossopen(PyObject
*self
, PyObject
*args
)
901 return (PyObject
*)newossobject(args
);
905 ossopenmixer(PyObject
*self
, PyObject
*args
)
907 return (PyObject
*)newossmixerobject(args
);
910 static PyMethodDef ossaudiodev_methods
[] = {
911 { "open", ossopen
, METH_VARARGS
},
912 { "openmixer", ossopenmixer
, METH_VARARGS
},
917 #define _EXPORT_INT(mod, name) \
918 if (PyModule_AddIntConstant(mod, #name, (long) (name)) == -1) return;
921 static char *control_labels
[] = SOUND_DEVICE_LABELS
;
922 static char *control_names
[] = SOUND_DEVICE_NAMES
;
926 build_namelists (PyObject
*module
)
934 num_controls
= sizeof(control_labels
) / sizeof(control_labels
[0]);
935 assert(num_controls
== sizeof(control_names
) / sizeof(control_names
[0]));
937 labels
= PyList_New(num_controls
);
938 names
= PyList_New(num_controls
);
939 for (i
= 0; i
< num_controls
; i
++) {
940 s
= PyString_FromString(control_labels
[i
]);
943 PyList_SET_ITEM(labels
, i
, s
);
945 s
= PyString_FromString(control_names
[i
]);
948 PyList_SET_ITEM(names
, i
, s
);
951 if (PyModule_AddObject(module
, "control_labels", labels
) == -1)
953 if (PyModule_AddObject(module
, "control_names", names
) == -1)
961 initossaudiodev(void)
965 m
= Py_InitModule("ossaudiodev", ossaudiodev_methods
);
967 OSSAudioError
= PyErr_NewException("ossaudiodev.OSSAudioError",
970 /* Each call to PyModule_AddObject decrefs it; compensate: */
971 Py_INCREF(OSSAudioError
);
972 Py_INCREF(OSSAudioError
);
973 PyModule_AddObject(m
, "error", OSSAudioError
);
974 PyModule_AddObject(m
, "OSSAudioError", OSSAudioError
);
977 /* Build 'control_labels' and 'control_names' lists and add them
979 if (build_namelists(m
) == -1) /* XXX what to do here? */
982 /* Expose the audio format numbers -- essential! */
983 _EXPORT_INT(m
, AFMT_QUERY
);
984 _EXPORT_INT(m
, AFMT_MU_LAW
);
985 _EXPORT_INT(m
, AFMT_A_LAW
);
986 _EXPORT_INT(m
, AFMT_IMA_ADPCM
);
987 _EXPORT_INT(m
, AFMT_U8
);
988 _EXPORT_INT(m
, AFMT_S16_LE
);
989 _EXPORT_INT(m
, AFMT_S16_BE
);
990 _EXPORT_INT(m
, AFMT_S8
);
991 _EXPORT_INT(m
, AFMT_U16_LE
);
992 _EXPORT_INT(m
, AFMT_U16_BE
);
993 _EXPORT_INT(m
, AFMT_MPEG
);
995 _EXPORT_INT(m
, AFMT_AC3
);
998 _EXPORT_INT(m
, AFMT_S16_NE
);
1001 _EXPORT_INT(m
, AFMT_U16_NE
);
1004 _EXPORT_INT(m
, AFMT_S32_LE
);
1007 _EXPORT_INT(m
, AFMT_S32_BE
);
1010 _EXPORT_INT(m
, AFMT_MPEG
);
1013 /* Expose the sound mixer device numbers. */
1014 _EXPORT_INT(m
, SOUND_MIXER_NRDEVICES
);
1015 _EXPORT_INT(m
, SOUND_MIXER_VOLUME
);
1016 _EXPORT_INT(m
, SOUND_MIXER_BASS
);
1017 _EXPORT_INT(m
, SOUND_MIXER_TREBLE
);
1018 _EXPORT_INT(m
, SOUND_MIXER_SYNTH
);
1019 _EXPORT_INT(m
, SOUND_MIXER_PCM
);
1020 _EXPORT_INT(m
, SOUND_MIXER_SPEAKER
);
1021 _EXPORT_INT(m
, SOUND_MIXER_LINE
);
1022 _EXPORT_INT(m
, SOUND_MIXER_MIC
);
1023 _EXPORT_INT(m
, SOUND_MIXER_CD
);
1024 _EXPORT_INT(m
, SOUND_MIXER_IMIX
);
1025 _EXPORT_INT(m
, SOUND_MIXER_ALTPCM
);
1026 _EXPORT_INT(m
, SOUND_MIXER_RECLEV
);
1027 _EXPORT_INT(m
, SOUND_MIXER_IGAIN
);
1028 _EXPORT_INT(m
, SOUND_MIXER_OGAIN
);
1029 _EXPORT_INT(m
, SOUND_MIXER_LINE1
);
1030 _EXPORT_INT(m
, SOUND_MIXER_LINE2
);
1031 _EXPORT_INT(m
, SOUND_MIXER_LINE3
);
1032 #ifdef SOUND_MIXER_DIGITAL1
1033 _EXPORT_INT(m
, SOUND_MIXER_DIGITAL1
);
1035 #ifdef SOUND_MIXER_DIGITAL2
1036 _EXPORT_INT(m
, SOUND_MIXER_DIGITAL2
);
1038 #ifdef SOUND_MIXER_DIGITAL3
1039 _EXPORT_INT(m
, SOUND_MIXER_DIGITAL3
);
1041 #ifdef SOUND_MIXER_PHONEIN
1042 _EXPORT_INT(m
, SOUND_MIXER_PHONEIN
);
1044 #ifdef SOUND_MIXER_PHONEOUT
1045 _EXPORT_INT(m
, SOUND_MIXER_PHONEOUT
);
1047 #ifdef SOUND_MIXER_VIDEO
1048 _EXPORT_INT(m
, SOUND_MIXER_VIDEO
);
1050 #ifdef SOUND_MIXER_RADIO
1051 _EXPORT_INT(m
, SOUND_MIXER_RADIO
);
1053 #ifdef SOUND_MIXER_MONITOR
1054 _EXPORT_INT(m
, SOUND_MIXER_MONITOR
);
1057 /* Expose all the ioctl numbers for masochists who like to do this
1059 _EXPORT_INT(m
, SNDCTL_COPR_HALT
);
1060 _EXPORT_INT(m
, SNDCTL_COPR_LOAD
);
1061 _EXPORT_INT(m
, SNDCTL_COPR_RCODE
);
1062 _EXPORT_INT(m
, SNDCTL_COPR_RCVMSG
);
1063 _EXPORT_INT(m
, SNDCTL_COPR_RDATA
);
1064 _EXPORT_INT(m
, SNDCTL_COPR_RESET
);
1065 _EXPORT_INT(m
, SNDCTL_COPR_RUN
);
1066 _EXPORT_INT(m
, SNDCTL_COPR_SENDMSG
);
1067 _EXPORT_INT(m
, SNDCTL_COPR_WCODE
);
1068 _EXPORT_INT(m
, SNDCTL_COPR_WDATA
);
1069 #ifdef SNDCTL_DSP_BIND_CHANNEL
1070 _EXPORT_INT(m
, SNDCTL_DSP_BIND_CHANNEL
);
1072 _EXPORT_INT(m
, SNDCTL_DSP_CHANNELS
);
1073 _EXPORT_INT(m
, SNDCTL_DSP_GETBLKSIZE
);
1074 _EXPORT_INT(m
, SNDCTL_DSP_GETCAPS
);
1075 #ifdef SNDCTL_DSP_GETCHANNELMASK
1076 _EXPORT_INT(m
, SNDCTL_DSP_GETCHANNELMASK
);
1078 _EXPORT_INT(m
, SNDCTL_DSP_GETFMTS
);
1079 _EXPORT_INT(m
, SNDCTL_DSP_GETIPTR
);
1080 _EXPORT_INT(m
, SNDCTL_DSP_GETISPACE
);
1081 #ifdef SNDCTL_DSP_GETODELAY
1082 _EXPORT_INT(m
, SNDCTL_DSP_GETODELAY
);
1084 _EXPORT_INT(m
, SNDCTL_DSP_GETOPTR
);
1085 _EXPORT_INT(m
, SNDCTL_DSP_GETOSPACE
);
1086 #ifdef SNDCTL_DSP_GETSPDIF
1087 _EXPORT_INT(m
, SNDCTL_DSP_GETSPDIF
);
1089 _EXPORT_INT(m
, SNDCTL_DSP_GETTRIGGER
);
1090 _EXPORT_INT(m
, SNDCTL_DSP_MAPINBUF
);
1091 _EXPORT_INT(m
, SNDCTL_DSP_MAPOUTBUF
);
1092 _EXPORT_INT(m
, SNDCTL_DSP_NONBLOCK
);
1093 _EXPORT_INT(m
, SNDCTL_DSP_POST
);
1094 #ifdef SNDCTL_DSP_PROFILE
1095 _EXPORT_INT(m
, SNDCTL_DSP_PROFILE
);
1097 _EXPORT_INT(m
, SNDCTL_DSP_RESET
);
1098 _EXPORT_INT(m
, SNDCTL_DSP_SAMPLESIZE
);
1099 _EXPORT_INT(m
, SNDCTL_DSP_SETDUPLEX
);
1100 _EXPORT_INT(m
, SNDCTL_DSP_SETFMT
);
1101 _EXPORT_INT(m
, SNDCTL_DSP_SETFRAGMENT
);
1102 #ifdef SNDCTL_DSP_SETSPDIF
1103 _EXPORT_INT(m
, SNDCTL_DSP_SETSPDIF
);
1105 _EXPORT_INT(m
, SNDCTL_DSP_SETSYNCRO
);
1106 _EXPORT_INT(m
, SNDCTL_DSP_SETTRIGGER
);
1107 _EXPORT_INT(m
, SNDCTL_DSP_SPEED
);
1108 _EXPORT_INT(m
, SNDCTL_DSP_STEREO
);
1109 _EXPORT_INT(m
, SNDCTL_DSP_SUBDIVIDE
);
1110 _EXPORT_INT(m
, SNDCTL_DSP_SYNC
);
1111 _EXPORT_INT(m
, SNDCTL_FM_4OP_ENABLE
);
1112 _EXPORT_INT(m
, SNDCTL_FM_LOAD_INSTR
);
1113 _EXPORT_INT(m
, SNDCTL_MIDI_INFO
);
1114 _EXPORT_INT(m
, SNDCTL_MIDI_MPUCMD
);
1115 _EXPORT_INT(m
, SNDCTL_MIDI_MPUMODE
);
1116 _EXPORT_INT(m
, SNDCTL_MIDI_PRETIME
);
1117 _EXPORT_INT(m
, SNDCTL_SEQ_CTRLRATE
);
1118 _EXPORT_INT(m
, SNDCTL_SEQ_GETINCOUNT
);
1119 _EXPORT_INT(m
, SNDCTL_SEQ_GETOUTCOUNT
);
1120 #ifdef SNDCTL_SEQ_GETTIME
1121 _EXPORT_INT(m
, SNDCTL_SEQ_GETTIME
);
1123 _EXPORT_INT(m
, SNDCTL_SEQ_NRMIDIS
);
1124 _EXPORT_INT(m
, SNDCTL_SEQ_NRSYNTHS
);
1125 _EXPORT_INT(m
, SNDCTL_SEQ_OUTOFBAND
);
1126 _EXPORT_INT(m
, SNDCTL_SEQ_PANIC
);
1127 _EXPORT_INT(m
, SNDCTL_SEQ_PERCMODE
);
1128 _EXPORT_INT(m
, SNDCTL_SEQ_RESET
);
1129 _EXPORT_INT(m
, SNDCTL_SEQ_RESETSAMPLES
);
1130 _EXPORT_INT(m
, SNDCTL_SEQ_SYNC
);
1131 _EXPORT_INT(m
, SNDCTL_SEQ_TESTMIDI
);
1132 _EXPORT_INT(m
, SNDCTL_SEQ_THRESHOLD
);
1133 #ifdef SNDCTL_SYNTH_CONTROL
1134 _EXPORT_INT(m
, SNDCTL_SYNTH_CONTROL
);
1136 #ifdef SNDCTL_SYNTH_ID
1137 _EXPORT_INT(m
, SNDCTL_SYNTH_ID
);
1139 _EXPORT_INT(m
, SNDCTL_SYNTH_INFO
);
1140 _EXPORT_INT(m
, SNDCTL_SYNTH_MEMAVL
);
1141 #ifdef SNDCTL_SYNTH_REMOVESAMPLE
1142 _EXPORT_INT(m
, SNDCTL_SYNTH_REMOVESAMPLE
);
1144 _EXPORT_INT(m
, SNDCTL_TMR_CONTINUE
);
1145 _EXPORT_INT(m
, SNDCTL_TMR_METRONOME
);
1146 _EXPORT_INT(m
, SNDCTL_TMR_SELECT
);
1147 _EXPORT_INT(m
, SNDCTL_TMR_SOURCE
);
1148 _EXPORT_INT(m
, SNDCTL_TMR_START
);
1149 _EXPORT_INT(m
, SNDCTL_TMR_STOP
);
1150 _EXPORT_INT(m
, SNDCTL_TMR_TEMPO
);
1151 _EXPORT_INT(m
, SNDCTL_TMR_TIMEBASE
);