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 int fd
; /* The open file */
50 int mode
; /* file mode */
51 int icount
; /* Input count */
52 int ocount
; /* Output count */
53 uint32_t afmts
; /* Audio formats supported by hardware */
58 int fd
; /* The open mixer device */
62 static PyTypeObject OSSAudioType
;
63 static PyTypeObject OSSMixerType
;
65 static PyObject
*OSSAudioError
;
68 /* ----------------------------------------------------------------------
69 * DSP object initialization/deallocation
73 newossobject(PyObject
*arg
)
80 /* Two ways to call open():
81 open(device, mode) (for consistency with builtin open())
82 open(mode) (for backwards compatibility)
83 because the *first* argument is optional, parsing args is
85 if (!PyArg_ParseTuple(arg
, "s|s:open", &basedev
, &mode
))
87 if (mode
== NULL
) { /* only one arg supplied */
92 if (strcmp(mode
, "r") == 0)
94 else if (strcmp(mode
, "w") == 0)
96 else if (strcmp(mode
, "rw") == 0)
99 PyErr_SetString(OSSAudioError
, "mode must be 'r', 'w', or 'rw'");
103 /* Open the correct device: either the 'device' argument,
104 or the AUDIODEV environment variable, or "/dev/dsp". */
105 if (basedev
== NULL
) { /* called with one arg */
106 basedev
= getenv("AUDIODEV");
107 if (basedev
== NULL
) /* $AUDIODEV not set */
108 basedev
= "/dev/dsp";
111 /* Open with O_NONBLOCK to avoid hanging on devices that only allow
112 one open at a time. This does *not* affect later I/O; OSS
113 provides a special ioctl() for non-blocking read/write, which is
114 exposed via oss_nonblock() below. */
115 if ((fd
= open(basedev
, imode
|O_NONBLOCK
)) == -1) {
116 PyErr_SetFromErrnoWithFilename(PyExc_IOError
, basedev
);
120 /* And (try to) put it back in blocking mode so we get the
121 expected write() semantics. */
122 if (fcntl(fd
, F_SETFL
, 0) == -1) {
124 PyErr_SetFromErrnoWithFilename(PyExc_IOError
, basedev
);
128 if (ioctl(fd
, SNDCTL_DSP_GETFMTS
, &afmts
) == -1) {
129 PyErr_SetFromErrnoWithFilename(PyExc_IOError
, basedev
);
132 /* Create and initialize the object */
133 if ((self
= PyObject_New(oss_audio_t
, &OSSAudioType
)) == NULL
) {
139 self
->icount
= self
->ocount
= 0;
145 oss_dealloc(oss_audio_t
*self
)
147 /* if already closed, don't reclose it */
154 /* ----------------------------------------------------------------------
155 * Mixer object initialization/deallocation
159 newossmixerobject(PyObject
*arg
)
161 char *basedev
= NULL
;
165 if (!PyArg_ParseTuple(arg
, "|s", &basedev
)) {
169 if (basedev
== NULL
) {
170 basedev
= getenv("MIXERDEV");
171 if (basedev
== NULL
) /* MIXERDEV not set */
172 basedev
= "/dev/mixer";
175 if ((fd
= open(basedev
, O_RDWR
)) == -1) {
176 PyErr_SetFromErrnoWithFilename(PyExc_IOError
, basedev
);
180 if ((self
= PyObject_New(oss_mixer_t
, &OSSMixerType
)) == NULL
) {
191 oss_mixer_dealloc(oss_mixer_t
*self
)
193 /* if already closed, don't reclose it */
200 /* Methods to wrap the OSS ioctls. The calling convention is pretty
202 nonblock() -> ioctl(fd, SNDCTL_DSP_NONBLOCK)
203 fmt = setfmt(fmt) -> ioctl(fd, SNDCTL_DSP_SETFMT, &fmt)
208 /* ----------------------------------------------------------------------
212 /* _do_ioctl_1() is a private helper function used for the OSS ioctls --
213 SNDCTL_DSP_{SETFMT,CHANNELS,SPEED} -- that that are called from C
215 ioctl(fd, SNDCTL_DSP_cmd, &arg)
217 where arg is the value to set, and on return the driver sets arg to
218 the value that was actually set. Mapping this to Python is obvious:
222 _do_ioctl_1(int fd
, PyObject
*args
, char *fname
, int cmd
)
224 char argfmt
[33] = "i:";
227 assert(strlen(fname
) <= 30);
228 strcat(argfmt
, fname
);
229 if (!PyArg_ParseTuple(args
, argfmt
, &arg
))
232 if (ioctl(fd
, cmd
, &arg
) == -1)
233 return PyErr_SetFromErrno(PyExc_IOError
);
234 return PyInt_FromLong(arg
);
238 /* _do_ioctl_1_internal() is a wrapper for ioctls that take no inputs
239 but return an output -- ie. we need to pass a pointer to a local C
240 variable so the driver can write its output there, but from Python
241 all we see is the return value. For example,
242 SOUND_MIXER_READ_DEVMASK returns a bitmask of available mixer
243 devices, but does not use the value of the parameter passed-in in any
247 _do_ioctl_1_internal(int fd
, PyObject
*args
, char *fname
, int cmd
)
249 char argfmt
[32] = ":";
252 assert(strlen(fname
) <= 30);
253 strcat(argfmt
, fname
);
254 if (!PyArg_ParseTuple(args
, argfmt
, &arg
))
257 if (ioctl(fd
, cmd
, &arg
) == -1)
258 return PyErr_SetFromErrno(PyExc_IOError
);
259 return PyInt_FromLong(arg
);
264 /* _do_ioctl_0() is a private helper for the no-argument ioctls:
265 SNDCTL_DSP_{SYNC,RESET,POST}. */
267 _do_ioctl_0(int fd
, PyObject
*args
, char *fname
, int cmd
)
269 char argfmt
[32] = ":";
272 assert(strlen(fname
) <= 30);
273 strcat(argfmt
, fname
);
274 if (!PyArg_ParseTuple(args
, argfmt
))
277 /* According to hannu@opensound.com, all three of the ioctls that
278 use this function can block, so release the GIL. This is
279 especially important for SYNC, which can block for several
281 Py_BEGIN_ALLOW_THREADS
282 rv
= ioctl(fd
, cmd
, 0);
286 return PyErr_SetFromErrno(PyExc_IOError
);
292 /* ----------------------------------------------------------------------
293 * Methods of DSP objects (OSSAudioType)
297 oss_nonblock(oss_audio_t
*self
, PyObject
*args
)
299 /* Hmmm: it doesn't appear to be possible to return to blocking
300 mode once we're in non-blocking mode! */
301 if (!PyArg_ParseTuple(args
, ":nonblock"))
303 if (ioctl(self
->fd
, SNDCTL_DSP_NONBLOCK
, NULL
) == -1)
304 return PyErr_SetFromErrno(PyExc_IOError
);
310 oss_setfmt(oss_audio_t
*self
, PyObject
*args
)
312 return _do_ioctl_1(self
->fd
, args
, "setfmt", SNDCTL_DSP_SETFMT
);
316 oss_getfmts(oss_audio_t
*self
, PyObject
*args
)
319 if (!PyArg_ParseTuple(args
, ":getfmts"))
321 if (ioctl(self
->fd
, SNDCTL_DSP_GETFMTS
, &mask
) == -1)
322 return PyErr_SetFromErrno(PyExc_IOError
);
323 return PyInt_FromLong(mask
);
327 oss_channels(oss_audio_t
*self
, PyObject
*args
)
329 return _do_ioctl_1(self
->fd
, args
, "channels", SNDCTL_DSP_CHANNELS
);
333 oss_speed(oss_audio_t
*self
, PyObject
*args
)
335 return _do_ioctl_1(self
->fd
, args
, "speed", SNDCTL_DSP_SPEED
);
339 oss_sync(oss_audio_t
*self
, PyObject
*args
)
341 return _do_ioctl_0(self
->fd
, args
, "sync", SNDCTL_DSP_SYNC
);
345 oss_reset(oss_audio_t
*self
, PyObject
*args
)
347 return _do_ioctl_0(self
->fd
, args
, "reset", SNDCTL_DSP_RESET
);
351 oss_post(oss_audio_t
*self
, PyObject
*args
)
353 return _do_ioctl_0(self
->fd
, args
, "post", SNDCTL_DSP_POST
);
357 /* Regular file methods: read(), write(), close(), etc. as well
358 as one convenience method, writeall(). */
361 oss_read(oss_audio_t
*self
, PyObject
*args
)
367 if (!PyArg_ParseTuple(args
, "i:read", &size
))
369 rv
= PyString_FromStringAndSize(NULL
, size
);
372 cp
= PyString_AS_STRING(rv
);
374 Py_BEGIN_ALLOW_THREADS
375 count
= read(self
->fd
, cp
, size
);
379 PyErr_SetFromErrno(PyExc_IOError
);
383 self
->icount
+= count
;
384 _PyString_Resize(&rv
, count
);
389 oss_write(oss_audio_t
*self
, PyObject
*args
)
394 if (!PyArg_ParseTuple(args
, "s#:write", &cp
, &size
)) {
398 Py_BEGIN_ALLOW_THREADS
399 rv
= write(self
->fd
, cp
, size
);
403 return PyErr_SetFromErrno(PyExc_IOError
);
407 return PyInt_FromLong(rv
);
411 oss_writeall(oss_audio_t
*self
, PyObject
*args
)
415 fd_set write_set_fds
;
418 /* NB. writeall() is only useful in non-blocking mode: according to
419 Guenter Geiger <geiger@xdv.org> on the linux-audio-dev list
420 (http://eca.cx/lad/2002/11/0380.html), OSS guarantees that
421 write() in blocking mode consumes the whole buffer. In blocking
422 mode, the behaviour of write() and writeall() from Python is
423 indistinguishable. */
425 if (!PyArg_ParseTuple(args
, "s#:write", &cp
, &size
))
428 /* use select to wait for audio device to be available */
429 FD_ZERO(&write_set_fds
);
430 FD_SET(self
->fd
, &write_set_fds
);
433 Py_BEGIN_ALLOW_THREADS
434 select_rv
= select(self
->fd
+1, NULL
, &write_set_fds
, NULL
, NULL
);
436 assert(select_rv
!= 0); /* no timeout, can't expire */
438 return PyErr_SetFromErrno(PyExc_IOError
);
440 Py_BEGIN_ALLOW_THREADS
441 rv
= write(self
->fd
, cp
, size
);
444 if (errno
== EAGAIN
) { /* buffer is full, try again */
447 } else /* it's a real error */
448 return PyErr_SetFromErrno(PyExc_IOError
);
449 } else { /* wrote rv bytes */
460 oss_close(oss_audio_t
*self
, PyObject
*args
)
462 if (!PyArg_ParseTuple(args
, ":close"))
466 Py_BEGIN_ALLOW_THREADS
476 oss_fileno(oss_audio_t
*self
, PyObject
*args
)
478 if (!PyArg_ParseTuple(args
, ":fileno"))
480 return PyInt_FromLong(self
->fd
);
484 /* Convenience methods: these generally wrap a couple of ioctls into one
488 oss_setparameters(oss_audio_t
*self
, PyObject
*args
)
490 int wanted_fmt
, wanted_channels
, wanted_rate
, strict
=0;
491 int fmt
, channels
, rate
;
492 PyObject
* rv
; /* return tuple (fmt, channels, rate) */
494 if (!PyArg_ParseTuple(args
, "iii|i:setparameters",
495 &wanted_fmt
, &wanted_channels
, &wanted_rate
,
500 if (ioctl(self
->fd
, SNDCTL_DSP_SETFMT
, &fmt
) == -1) {
501 return PyErr_SetFromErrno(PyExc_IOError
);
503 if (strict
&& fmt
!= wanted_fmt
) {
506 "unable to set requested format (wanted %d, got %d)",
510 channels
= wanted_channels
;
511 if (ioctl(self
->fd
, SNDCTL_DSP_CHANNELS
, &channels
) == -1) {
512 return PyErr_SetFromErrno(PyExc_IOError
);
514 if (strict
&& channels
!= wanted_channels
) {
517 "unable to set requested channels (wanted %d, got %d)",
518 wanted_channels
, channels
);
522 if (ioctl(self
->fd
, SNDCTL_DSP_SPEED
, &rate
) == -1) {
523 return PyErr_SetFromErrno(PyExc_IOError
);
525 if (strict
&& rate
!= wanted_rate
) {
528 "unable to set requested rate (wanted %d, got %d)",
532 /* Construct the return value: a (fmt, channels, rate) tuple that
533 tells what the audio hardware was actually set to. */
537 PyTuple_SET_ITEM(rv
, 0, PyInt_FromLong(fmt
));
538 PyTuple_SET_ITEM(rv
, 1, PyInt_FromLong(channels
));
539 PyTuple_SET_ITEM(rv
, 2, PyInt_FromLong(rate
));
544 _ssize(oss_audio_t
*self
, int *nchannels
, int *ssize
)
549 if (ioctl(self
->fd
, SNDCTL_DSP_SETFMT
, &fmt
) < 0)
557 *ssize
= 1; /* 8 bit formats: 1 byte */
563 *ssize
= 2; /* 16 bit formats: 2 byte */
571 if (ioctl(self
->fd
, SNDCTL_DSP_CHANNELS
, nchannels
) < 0)
577 /* bufsize returns the size of the hardware audio buffer in number
580 oss_bufsize(oss_audio_t
*self
, PyObject
*args
)
583 int nchannels
, ssize
;
585 if (!PyArg_ParseTuple(args
, ":bufsize")) return NULL
;
587 if (_ssize(self
, &nchannels
, &ssize
) < 0) {
588 PyErr_SetFromErrno(PyExc_IOError
);
591 if (ioctl(self
->fd
, SNDCTL_DSP_GETOSPACE
, &ai
) < 0) {
592 PyErr_SetFromErrno(PyExc_IOError
);
595 return PyInt_FromLong((ai
.fragstotal
* ai
.fragsize
) / (nchannels
* ssize
));
598 /* obufcount returns the number of samples that are available in the
599 hardware for playing */
601 oss_obufcount(oss_audio_t
*self
, PyObject
*args
)
604 int nchannels
, ssize
;
606 if (!PyArg_ParseTuple(args
, ":obufcount"))
609 if (_ssize(self
, &nchannels
, &ssize
) < 0) {
610 PyErr_SetFromErrno(PyExc_IOError
);
613 if (ioctl(self
->fd
, SNDCTL_DSP_GETOSPACE
, &ai
) < 0) {
614 PyErr_SetFromErrno(PyExc_IOError
);
617 return PyInt_FromLong((ai
.fragstotal
* ai
.fragsize
- ai
.bytes
) /
618 (ssize
* nchannels
));
621 /* obufcount returns the number of samples that can be played without
624 oss_obuffree(oss_audio_t
*self
, PyObject
*args
)
627 int nchannels
, ssize
;
629 if (!PyArg_ParseTuple(args
, ":obuffree"))
632 if (_ssize(self
, &nchannels
, &ssize
) < 0) {
633 PyErr_SetFromErrno(PyExc_IOError
);
636 if (ioctl(self
->fd
, SNDCTL_DSP_GETOSPACE
, &ai
) < 0) {
637 PyErr_SetFromErrno(PyExc_IOError
);
640 return PyInt_FromLong(ai
.bytes
/ (ssize
* nchannels
));
644 oss_getptr(oss_audio_t
*self
, PyObject
*args
)
649 if (!PyArg_ParseTuple(args
, ":getptr"))
652 if (self
->mode
== O_RDONLY
)
653 req
= SNDCTL_DSP_GETIPTR
;
655 req
= SNDCTL_DSP_GETOPTR
;
656 if (ioctl(self
->fd
, req
, &info
) == -1) {
657 PyErr_SetFromErrno(PyExc_IOError
);
660 return Py_BuildValue("iii", info
.bytes
, info
.blocks
, info
.ptr
);
664 /* ----------------------------------------------------------------------
665 * Methods of mixer objects (OSSMixerType)
669 oss_mixer_close(oss_mixer_t
*self
, PyObject
*args
)
671 if (!PyArg_ParseTuple(args
, ":close"))
683 oss_mixer_fileno(oss_mixer_t
*self
, PyObject
*args
)
685 if (!PyArg_ParseTuple(args
, ":fileno"))
687 return PyInt_FromLong(self
->fd
);
690 /* Simple mixer interface methods */
693 oss_mixer_controls(oss_mixer_t
*self
, PyObject
*args
)
695 return _do_ioctl_1_internal(self
->fd
, args
, "controls",
696 SOUND_MIXER_READ_DEVMASK
);
700 oss_mixer_stereocontrols(oss_mixer_t
*self
, PyObject
*args
)
702 return _do_ioctl_1_internal(self
->fd
, args
, "stereocontrols",
703 SOUND_MIXER_READ_STEREODEVS
);
707 oss_mixer_reccontrols(oss_mixer_t
*self
, PyObject
*args
)
709 return _do_ioctl_1_internal(self
->fd
, args
, "reccontrols",
710 SOUND_MIXER_READ_RECMASK
);
714 oss_mixer_get(oss_mixer_t
*self
, PyObject
*args
)
718 /* Can't use _do_ioctl_1 because of encoded arg thingy. */
719 if (!PyArg_ParseTuple(args
, "i:get", &channel
))
722 if (channel
< 0 || channel
> SOUND_MIXER_NRDEVICES
) {
723 PyErr_SetString(OSSAudioError
, "Invalid mixer channel specified.");
727 if (ioctl(self
->fd
, MIXER_READ(channel
), &volume
) == -1)
728 return PyErr_SetFromErrno(PyExc_IOError
);
730 return Py_BuildValue("(ii)", volume
& 0xff, (volume
& 0xff00) >> 8);
734 oss_mixer_set(oss_mixer_t
*self
, PyObject
*args
)
736 int channel
, volume
, leftVol
, rightVol
;
738 /* Can't use _do_ioctl_1 because of encoded arg thingy. */
739 if (!PyArg_ParseTuple(args
, "i(ii):set", &channel
, &leftVol
, &rightVol
))
742 if (channel
< 0 || channel
> SOUND_MIXER_NRDEVICES
) {
743 PyErr_SetString(OSSAudioError
, "Invalid mixer channel specified.");
747 if (leftVol
< 0 || rightVol
< 0 || leftVol
> 100 || rightVol
> 100) {
748 PyErr_SetString(OSSAudioError
, "Volumes must be between 0 and 100.");
752 volume
= (rightVol
<< 8) | leftVol
;
754 if (ioctl(self
->fd
, MIXER_WRITE(channel
), &volume
) == -1)
755 return PyErr_SetFromErrno(PyExc_IOError
);
757 return Py_BuildValue("(ii)", volume
& 0xff, (volume
& 0xff00) >> 8);
761 oss_mixer_get_recsrc(oss_mixer_t
*self
, PyObject
*args
)
763 return _do_ioctl_1_internal(self
->fd
, args
, "get_recsrc",
764 SOUND_MIXER_READ_RECSRC
);
768 oss_mixer_set_recsrc(oss_mixer_t
*self
, PyObject
*args
)
770 return _do_ioctl_1(self
->fd
, args
, "set_recsrc",
771 SOUND_MIXER_WRITE_RECSRC
);
775 /* ----------------------------------------------------------------------
776 * Method tables and other bureaucracy
779 static PyMethodDef oss_methods
[] = {
780 /* Regular file methods */
781 { "read", (PyCFunction
)oss_read
, METH_VARARGS
},
782 { "write", (PyCFunction
)oss_write
, METH_VARARGS
},
783 { "writeall", (PyCFunction
)oss_writeall
, METH_VARARGS
},
784 { "close", (PyCFunction
)oss_close
, METH_VARARGS
},
785 { "fileno", (PyCFunction
)oss_fileno
, METH_VARARGS
},
787 /* Simple ioctl wrappers */
788 { "nonblock", (PyCFunction
)oss_nonblock
, METH_VARARGS
},
789 { "setfmt", (PyCFunction
)oss_setfmt
, METH_VARARGS
},
790 { "getfmts", (PyCFunction
)oss_getfmts
, METH_VARARGS
},
791 { "channels", (PyCFunction
)oss_channels
, METH_VARARGS
},
792 { "speed", (PyCFunction
)oss_speed
, METH_VARARGS
},
793 { "sync", (PyCFunction
)oss_sync
, METH_VARARGS
},
794 { "reset", (PyCFunction
)oss_reset
, METH_VARARGS
},
795 { "post", (PyCFunction
)oss_post
, METH_VARARGS
},
797 /* Convenience methods -- wrap a couple of ioctls together */
798 { "setparameters", (PyCFunction
)oss_setparameters
, METH_VARARGS
},
799 { "bufsize", (PyCFunction
)oss_bufsize
, METH_VARARGS
},
800 { "obufcount", (PyCFunction
)oss_obufcount
, METH_VARARGS
},
801 { "obuffree", (PyCFunction
)oss_obuffree
, METH_VARARGS
},
802 { "getptr", (PyCFunction
)oss_getptr
, METH_VARARGS
},
804 /* Aliases for backwards compatibility */
805 { "flush", (PyCFunction
)oss_sync
, METH_VARARGS
},
807 { NULL
, NULL
} /* sentinel */
810 static PyMethodDef oss_mixer_methods
[] = {
811 /* Regular file method - OSS mixers are ioctl-only interface */
812 { "close", (PyCFunction
)oss_mixer_close
, METH_VARARGS
},
813 { "fileno", (PyCFunction
)oss_mixer_fileno
, METH_VARARGS
},
815 /* Simple ioctl wrappers */
816 { "controls", (PyCFunction
)oss_mixer_controls
, METH_VARARGS
},
817 { "stereocontrols", (PyCFunction
)oss_mixer_stereocontrols
, METH_VARARGS
},
818 { "reccontrols", (PyCFunction
)oss_mixer_reccontrols
, METH_VARARGS
},
819 { "get", (PyCFunction
)oss_mixer_get
, METH_VARARGS
},
820 { "set", (PyCFunction
)oss_mixer_set
, METH_VARARGS
},
821 { "get_recsrc", (PyCFunction
)oss_mixer_get_recsrc
, METH_VARARGS
},
822 { "set_recsrc", (PyCFunction
)oss_mixer_set_recsrc
, METH_VARARGS
},
828 oss_getattr(oss_audio_t
*self
, char *name
)
830 return Py_FindMethod(oss_methods
, (PyObject
*)self
, name
);
834 oss_mixer_getattr(oss_mixer_t
*self
, char *name
)
836 return Py_FindMethod(oss_mixer_methods
, (PyObject
*)self
, name
);
839 static PyTypeObject OSSAudioType
= {
840 PyObject_HEAD_INIT(&PyType_Type
)
842 "ossaudiodev.oss_audio_device", /*tp_name*/
843 sizeof(oss_audio_t
), /*tp_size*/
846 (destructor
)oss_dealloc
, /*tp_dealloc*/
848 (getattrfunc
)oss_getattr
, /*tp_getattr*/
854 static PyTypeObject OSSMixerType
= {
855 PyObject_HEAD_INIT(&PyType_Type
)
857 "ossaudiodev.oss_mixer_device", /*tp_name*/
858 sizeof(oss_mixer_t
), /*tp_size*/
861 (destructor
)oss_mixer_dealloc
, /*tp_dealloc*/
863 (getattrfunc
)oss_mixer_getattr
, /*tp_getattr*/
871 ossopen(PyObject
*self
, PyObject
*args
)
873 return (PyObject
*)newossobject(args
);
877 ossopenmixer(PyObject
*self
, PyObject
*args
)
879 return (PyObject
*)newossmixerobject(args
);
882 static PyMethodDef ossaudiodev_methods
[] = {
883 { "open", ossopen
, METH_VARARGS
},
884 { "openmixer", ossopenmixer
, METH_VARARGS
},
889 #define _EXPORT_INT(mod, name) \
890 if (PyModule_AddIntConstant(mod, #name, (long) (name)) == -1) return;
893 static char *control_labels
[] = SOUND_DEVICE_LABELS
;
894 static char *control_names
[] = SOUND_DEVICE_NAMES
;
898 build_namelists (PyObject
*module
)
906 num_controls
= sizeof(control_labels
) / sizeof(control_labels
[0]);
907 assert(num_controls
== sizeof(control_names
) / sizeof(control_names
[0]));
909 labels
= PyList_New(num_controls
);
910 names
= PyList_New(num_controls
);
911 for (i
= 0; i
< num_controls
; i
++) {
912 s
= PyString_FromString(control_labels
[i
]);
915 PyList_SET_ITEM(labels
, i
, s
);
917 s
= PyString_FromString(control_names
[i
]);
920 PyList_SET_ITEM(names
, i
, s
);
923 if (PyModule_AddObject(module
, "control_labels", labels
) == -1)
925 if (PyModule_AddObject(module
, "control_names", names
) == -1)
933 initossaudiodev(void)
937 m
= Py_InitModule("ossaudiodev", ossaudiodev_methods
);
939 OSSAudioError
= PyErr_NewException("ossaudiodev.OSSAudioError",
942 /* Each call to PyModule_AddObject decrefs it; compensate: */
943 Py_INCREF(OSSAudioError
);
944 Py_INCREF(OSSAudioError
);
945 PyModule_AddObject(m
, "error", OSSAudioError
);
946 PyModule_AddObject(m
, "OSSAudioError", OSSAudioError
);
949 /* Build 'control_labels' and 'control_names' lists and add them
951 if (build_namelists(m
) == -1) /* XXX what to do here? */
954 /* Expose the audio format numbers -- essential! */
955 _EXPORT_INT(m
, AFMT_QUERY
);
956 _EXPORT_INT(m
, AFMT_MU_LAW
);
957 _EXPORT_INT(m
, AFMT_A_LAW
);
958 _EXPORT_INT(m
, AFMT_IMA_ADPCM
);
959 _EXPORT_INT(m
, AFMT_U8
);
960 _EXPORT_INT(m
, AFMT_S16_LE
);
961 _EXPORT_INT(m
, AFMT_S16_BE
);
962 _EXPORT_INT(m
, AFMT_S8
);
963 _EXPORT_INT(m
, AFMT_U16_LE
);
964 _EXPORT_INT(m
, AFMT_U16_BE
);
965 _EXPORT_INT(m
, AFMT_MPEG
);
967 _EXPORT_INT(m
, AFMT_AC3
);
970 _EXPORT_INT(m
, AFMT_S16_NE
);
973 /* Expose the sound mixer device numbers. */
974 _EXPORT_INT(m
, SOUND_MIXER_NRDEVICES
);
975 _EXPORT_INT(m
, SOUND_MIXER_VOLUME
);
976 _EXPORT_INT(m
, SOUND_MIXER_BASS
);
977 _EXPORT_INT(m
, SOUND_MIXER_TREBLE
);
978 _EXPORT_INT(m
, SOUND_MIXER_SYNTH
);
979 _EXPORT_INT(m
, SOUND_MIXER_PCM
);
980 _EXPORT_INT(m
, SOUND_MIXER_SPEAKER
);
981 _EXPORT_INT(m
, SOUND_MIXER_LINE
);
982 _EXPORT_INT(m
, SOUND_MIXER_MIC
);
983 _EXPORT_INT(m
, SOUND_MIXER_CD
);
984 _EXPORT_INT(m
, SOUND_MIXER_IMIX
);
985 _EXPORT_INT(m
, SOUND_MIXER_ALTPCM
);
986 _EXPORT_INT(m
, SOUND_MIXER_RECLEV
);
987 _EXPORT_INT(m
, SOUND_MIXER_IGAIN
);
988 _EXPORT_INT(m
, SOUND_MIXER_OGAIN
);
989 _EXPORT_INT(m
, SOUND_MIXER_LINE1
);
990 _EXPORT_INT(m
, SOUND_MIXER_LINE2
);
991 _EXPORT_INT(m
, SOUND_MIXER_LINE3
);
992 #ifdef SOUND_MIXER_DIGITAL1
993 _EXPORT_INT(m
, SOUND_MIXER_DIGITAL1
);
995 #ifdef SOUND_MIXER_DIGITAL2
996 _EXPORT_INT(m
, SOUND_MIXER_DIGITAL2
);
998 #ifdef SOUND_MIXER_DIGITAL3
999 _EXPORT_INT(m
, SOUND_MIXER_DIGITAL3
);
1001 #ifdef SOUND_MIXER_PHONEIN
1002 _EXPORT_INT(m
, SOUND_MIXER_PHONEIN
);
1004 #ifdef SOUND_MIXER_PHONEOUT
1005 _EXPORT_INT(m
, SOUND_MIXER_PHONEOUT
);
1007 #ifdef SOUND_MIXER_VIDEO
1008 _EXPORT_INT(m
, SOUND_MIXER_VIDEO
);
1010 #ifdef SOUND_MIXER_RADIO
1011 _EXPORT_INT(m
, SOUND_MIXER_RADIO
);
1013 #ifdef SOUND_MIXER_MONITOR
1014 _EXPORT_INT(m
, SOUND_MIXER_MONITOR
);
1017 /* Expose all the ioctl numbers for masochists who like to do this
1019 _EXPORT_INT(m
, SNDCTL_COPR_HALT
);
1020 _EXPORT_INT(m
, SNDCTL_COPR_LOAD
);
1021 _EXPORT_INT(m
, SNDCTL_COPR_RCODE
);
1022 _EXPORT_INT(m
, SNDCTL_COPR_RCVMSG
);
1023 _EXPORT_INT(m
, SNDCTL_COPR_RDATA
);
1024 _EXPORT_INT(m
, SNDCTL_COPR_RESET
);
1025 _EXPORT_INT(m
, SNDCTL_COPR_RUN
);
1026 _EXPORT_INT(m
, SNDCTL_COPR_SENDMSG
);
1027 _EXPORT_INT(m
, SNDCTL_COPR_WCODE
);
1028 _EXPORT_INT(m
, SNDCTL_COPR_WDATA
);
1029 #ifdef SNDCTL_DSP_BIND_CHANNEL
1030 _EXPORT_INT(m
, SNDCTL_DSP_BIND_CHANNEL
);
1032 _EXPORT_INT(m
, SNDCTL_DSP_CHANNELS
);
1033 _EXPORT_INT(m
, SNDCTL_DSP_GETBLKSIZE
);
1034 _EXPORT_INT(m
, SNDCTL_DSP_GETCAPS
);
1035 #ifdef SNDCTL_DSP_GETCHANNELMASK
1036 _EXPORT_INT(m
, SNDCTL_DSP_GETCHANNELMASK
);
1038 _EXPORT_INT(m
, SNDCTL_DSP_GETFMTS
);
1039 _EXPORT_INT(m
, SNDCTL_DSP_GETIPTR
);
1040 _EXPORT_INT(m
, SNDCTL_DSP_GETISPACE
);
1041 #ifdef SNDCTL_DSP_GETODELAY
1042 _EXPORT_INT(m
, SNDCTL_DSP_GETODELAY
);
1044 _EXPORT_INT(m
, SNDCTL_DSP_GETOPTR
);
1045 _EXPORT_INT(m
, SNDCTL_DSP_GETOSPACE
);
1046 #ifdef SNDCTL_DSP_GETSPDIF
1047 _EXPORT_INT(m
, SNDCTL_DSP_GETSPDIF
);
1049 _EXPORT_INT(m
, SNDCTL_DSP_GETTRIGGER
);
1050 _EXPORT_INT(m
, SNDCTL_DSP_MAPINBUF
);
1051 _EXPORT_INT(m
, SNDCTL_DSP_MAPOUTBUF
);
1052 _EXPORT_INT(m
, SNDCTL_DSP_NONBLOCK
);
1053 _EXPORT_INT(m
, SNDCTL_DSP_POST
);
1054 #ifdef SNDCTL_DSP_PROFILE
1055 _EXPORT_INT(m
, SNDCTL_DSP_PROFILE
);
1057 _EXPORT_INT(m
, SNDCTL_DSP_RESET
);
1058 _EXPORT_INT(m
, SNDCTL_DSP_SAMPLESIZE
);
1059 _EXPORT_INT(m
, SNDCTL_DSP_SETDUPLEX
);
1060 _EXPORT_INT(m
, SNDCTL_DSP_SETFMT
);
1061 _EXPORT_INT(m
, SNDCTL_DSP_SETFRAGMENT
);
1062 #ifdef SNDCTL_DSP_SETSPDIF
1063 _EXPORT_INT(m
, SNDCTL_DSP_SETSPDIF
);
1065 _EXPORT_INT(m
, SNDCTL_DSP_SETSYNCRO
);
1066 _EXPORT_INT(m
, SNDCTL_DSP_SETTRIGGER
);
1067 _EXPORT_INT(m
, SNDCTL_DSP_SPEED
);
1068 _EXPORT_INT(m
, SNDCTL_DSP_STEREO
);
1069 _EXPORT_INT(m
, SNDCTL_DSP_SUBDIVIDE
);
1070 _EXPORT_INT(m
, SNDCTL_DSP_SYNC
);
1071 _EXPORT_INT(m
, SNDCTL_FM_4OP_ENABLE
);
1072 _EXPORT_INT(m
, SNDCTL_FM_LOAD_INSTR
);
1073 _EXPORT_INT(m
, SNDCTL_MIDI_INFO
);
1074 _EXPORT_INT(m
, SNDCTL_MIDI_MPUCMD
);
1075 _EXPORT_INT(m
, SNDCTL_MIDI_MPUMODE
);
1076 _EXPORT_INT(m
, SNDCTL_MIDI_PRETIME
);
1077 _EXPORT_INT(m
, SNDCTL_SEQ_CTRLRATE
);
1078 _EXPORT_INT(m
, SNDCTL_SEQ_GETINCOUNT
);
1079 _EXPORT_INT(m
, SNDCTL_SEQ_GETOUTCOUNT
);
1080 #ifdef SNDCTL_SEQ_GETTIME
1081 _EXPORT_INT(m
, SNDCTL_SEQ_GETTIME
);
1083 _EXPORT_INT(m
, SNDCTL_SEQ_NRMIDIS
);
1084 _EXPORT_INT(m
, SNDCTL_SEQ_NRSYNTHS
);
1085 _EXPORT_INT(m
, SNDCTL_SEQ_OUTOFBAND
);
1086 _EXPORT_INT(m
, SNDCTL_SEQ_PANIC
);
1087 _EXPORT_INT(m
, SNDCTL_SEQ_PERCMODE
);
1088 _EXPORT_INT(m
, SNDCTL_SEQ_RESET
);
1089 _EXPORT_INT(m
, SNDCTL_SEQ_RESETSAMPLES
);
1090 _EXPORT_INT(m
, SNDCTL_SEQ_SYNC
);
1091 _EXPORT_INT(m
, SNDCTL_SEQ_TESTMIDI
);
1092 _EXPORT_INT(m
, SNDCTL_SEQ_THRESHOLD
);
1093 #ifdef SNDCTL_SYNTH_CONTROL
1094 _EXPORT_INT(m
, SNDCTL_SYNTH_CONTROL
);
1096 #ifdef SNDCTL_SYNTH_ID
1097 _EXPORT_INT(m
, SNDCTL_SYNTH_ID
);
1099 _EXPORT_INT(m
, SNDCTL_SYNTH_INFO
);
1100 _EXPORT_INT(m
, SNDCTL_SYNTH_MEMAVL
);
1101 #ifdef SNDCTL_SYNTH_REMOVESAMPLE
1102 _EXPORT_INT(m
, SNDCTL_SYNTH_REMOVESAMPLE
);
1104 _EXPORT_INT(m
, SNDCTL_TMR_CONTINUE
);
1105 _EXPORT_INT(m
, SNDCTL_TMR_METRONOME
);
1106 _EXPORT_INT(m
, SNDCTL_TMR_SELECT
);
1107 _EXPORT_INT(m
, SNDCTL_TMR_SOURCE
);
1108 _EXPORT_INT(m
, SNDCTL_TMR_START
);
1109 _EXPORT_INT(m
, SNDCTL_TMR_STOP
);
1110 _EXPORT_INT(m
, SNDCTL_TMR_TEMPO
);
1111 _EXPORT_INT(m
, SNDCTL_TMR_TIMEBASE
);