Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 1 | /* |
| 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. |
| 6 | * |
| 7 | * Originally written by Peter Bosch, March 2000, as linuxaudiodev. |
| 8 | * |
| 9 | * Renamed to ossaudiodev and rearranged/revised/hacked up |
| 10 | * by Greg Ward <gward@python.net>, November 2002. |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 11 | * Mixer interface by Nicholas FitzRoy-Dale <wzdd@lardcave.net>, Dec 2002. |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 12 | * |
| 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. |
| 16 | * |
| 17 | * XXX need a license statement |
| 18 | * |
| 19 | * $Id$ |
| 20 | */ |
| 21 | |
| 22 | #include "Python.h" |
| 23 | #include "structmember.h" |
| 24 | |
| 25 | #ifdef HAVE_FCNTL_H |
| 26 | #include <fcntl.h> |
| 27 | #else |
| 28 | #define O_RDONLY 00 |
| 29 | #define O_WRONLY 01 |
| 30 | #endif |
| 31 | |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 32 | #include <sys/ioctl.h> |
Greg Ward | 0b6dfb8 | 2003-03-10 03:17:06 +0000 | [diff] [blame] | 33 | #include <sys/soundcard.h> |
| 34 | |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 35 | #if defined(linux) |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 36 | |
| 37 | typedef unsigned long uint32_t; |
| 38 | |
| 39 | #elif defined(__FreeBSD__) |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 40 | |
Greg Ward | 0b6dfb8 | 2003-03-10 03:17:06 +0000 | [diff] [blame] | 41 | # ifndef SNDCTL_DSP_CHANNELS |
| 42 | # define SNDCTL_DSP_CHANNELS SOUND_PCM_WRITE_CHANNELS |
| 43 | # endif |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 44 | |
| 45 | #endif |
| 46 | |
| 47 | typedef struct { |
| 48 | PyObject_HEAD; |
Greg Ward | 7b43c68 | 2002-12-30 02:29:28 +0000 | [diff] [blame] | 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 */ |
Greg Ward | 499b73e | 2002-12-31 03:04:52 +0000 | [diff] [blame] | 54 | } oss_audio_t; |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 55 | |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 56 | typedef struct { |
| 57 | PyObject_HEAD; |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 58 | int fd; /* The open mixer device */ |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 59 | } oss_mixer_t; |
| 60 | |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 61 | /* XXX several format defined in soundcard.h are not supported, |
| 62 | including _NE (native endian) options and S32 options |
| 63 | */ |
| 64 | |
| 65 | static struct { |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 66 | int a_bps; |
| 67 | uint32_t a_fmt; |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 68 | char *a_name; |
| 69 | } audio_types[] = { |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 70 | { 8, AFMT_MU_LAW, "logarithmic mu-law 8-bit audio" }, |
| 71 | { 8, AFMT_A_LAW, "logarithmic A-law 8-bit audio" }, |
| 72 | { 8, AFMT_U8, "linear unsigned 8-bit audio" }, |
| 73 | { 8, AFMT_S8, "linear signed 8-bit audio" }, |
| 74 | { 16, AFMT_U16_BE, "linear unsigned 16-bit big-endian audio" }, |
| 75 | { 16, AFMT_U16_LE, "linear unsigned 16-bit little-endian audio" }, |
| 76 | { 16, AFMT_S16_BE, "linear signed 16-bit big-endian audio" }, |
| 77 | { 16, AFMT_S16_LE, "linear signed 16-bit little-endian audio" }, |
Neal Norwitz | d156c2d | 2003-02-02 17:59:06 +0000 | [diff] [blame] | 78 | #ifdef AFMT_S16_NE |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 79 | { 16, AFMT_S16_NE, "linear signed 16-bit native-endian audio" }, |
Neal Norwitz | d156c2d | 2003-02-02 17:59:06 +0000 | [diff] [blame] | 80 | #endif |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | static int n_audio_types = sizeof(audio_types) / sizeof(audio_types[0]); |
| 84 | |
Greg Ward | 499b73e | 2002-12-31 03:04:52 +0000 | [diff] [blame] | 85 | static PyTypeObject OSSAudioType; |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 86 | static PyTypeObject OSSMixerType; |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 87 | |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 88 | static PyObject *OSSAudioError; |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 89 | |
Greg Ward | da1cacb | 2002-12-31 03:02:23 +0000 | [diff] [blame] | 90 | |
| 91 | /* ---------------------------------------------------------------------- |
| 92 | * DSP object initialization/deallocation |
| 93 | */ |
| 94 | |
Greg Ward | 499b73e | 2002-12-31 03:04:52 +0000 | [diff] [blame] | 95 | static oss_audio_t * |
Greg Ward | 8c6b6a9 | 2002-12-11 14:43:13 +0000 | [diff] [blame] | 96 | newossobject(PyObject *arg) |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 97 | { |
Greg Ward | 58ae13c | 2002-12-31 03:07:21 +0000 | [diff] [blame] | 98 | oss_audio_t *self; |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 99 | int fd, afmts, imode; |
| 100 | char *basedev = NULL; |
| 101 | char *mode = NULL; |
| 102 | |
Greg Ward | 9a568eb | 2002-11-30 23:20:09 +0000 | [diff] [blame] | 103 | /* Two ways to call open(): |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 104 | open(device, mode) (for consistency with builtin open()) |
| 105 | open(mode) (for backwards compatibility) |
| 106 | because the *first* argument is optional, parsing args is |
| 107 | a wee bit tricky. */ |
| 108 | if (!PyArg_ParseTuple(arg, "s|s:open", &basedev, &mode)) |
| 109 | return NULL; |
| 110 | if (mode == NULL) { /* only one arg supplied */ |
| 111 | mode = basedev; |
| 112 | basedev = NULL; |
| 113 | } |
| 114 | |
| 115 | if (strcmp(mode, "r") == 0) |
| 116 | imode = O_RDONLY; |
| 117 | else if (strcmp(mode, "w") == 0) |
| 118 | imode = O_WRONLY; |
Greg Ward | 1e0f57d | 2002-11-30 23:05:26 +0000 | [diff] [blame] | 119 | else if (strcmp(mode, "rw") == 0) |
| 120 | imode = O_RDWR; |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 121 | else { |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 122 | PyErr_SetString(OSSAudioError, "mode must be 'r', 'w', or 'rw'"); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 123 | return NULL; |
| 124 | } |
| 125 | |
Greg Ward | 5c5c577 | 2002-12-30 02:58:04 +0000 | [diff] [blame] | 126 | /* Open the correct device: either the 'device' argument, |
| 127 | or the AUDIODEV environment variable, or "/dev/dsp". */ |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 128 | if (basedev == NULL) { /* called with one arg */ |
| 129 | basedev = getenv("AUDIODEV"); |
| 130 | if (basedev == NULL) /* $AUDIODEV not set */ |
| 131 | basedev = "/dev/dsp"; |
| 132 | } |
| 133 | |
Greg Ward | 5c49ef2 | 2003-03-11 16:53:13 +0000 | [diff] [blame^] | 134 | /* Open with O_NONBLOCK to avoid hanging on devices that only allow |
| 135 | one open at a time. This does *not* affect later I/O; OSS |
| 136 | provides a special ioctl() for non-blocking read/write, which is |
| 137 | exposed via oss_nonblock() below. */ |
| 138 | if ((fd = open(basedev, imode|O_NONBLOCK)) == -1) { |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 139 | PyErr_SetFromErrnoWithFilename(PyExc_IOError, basedev); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 140 | return NULL; |
| 141 | } |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 142 | if (ioctl(fd, SNDCTL_DSP_GETFMTS, &afmts) == -1) { |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 143 | PyErr_SetFromErrnoWithFilename(PyExc_IOError, basedev); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 144 | return NULL; |
| 145 | } |
| 146 | /* Create and initialize the object */ |
Greg Ward | 58ae13c | 2002-12-31 03:07:21 +0000 | [diff] [blame] | 147 | if ((self = PyObject_New(oss_audio_t, &OSSAudioType)) == NULL) { |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 148 | close(fd); |
| 149 | return NULL; |
| 150 | } |
Greg Ward | 58ae13c | 2002-12-31 03:07:21 +0000 | [diff] [blame] | 151 | self->fd = fd; |
| 152 | self->mode = imode; |
| 153 | self->icount = self->ocount = 0; |
| 154 | self->afmts = afmts; |
| 155 | return self; |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static void |
Greg Ward | 58ae13c | 2002-12-31 03:07:21 +0000 | [diff] [blame] | 159 | oss_dealloc(oss_audio_t *self) |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 160 | { |
| 161 | /* if already closed, don't reclose it */ |
Greg Ward | 58ae13c | 2002-12-31 03:07:21 +0000 | [diff] [blame] | 162 | if (self->fd != -1) |
| 163 | close(self->fd); |
| 164 | PyObject_Del(self); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Greg Ward | da1cacb | 2002-12-31 03:02:23 +0000 | [diff] [blame] | 167 | |
| 168 | /* ---------------------------------------------------------------------- |
| 169 | * Mixer object initialization/deallocation |
| 170 | */ |
| 171 | |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 172 | static oss_mixer_t * |
| 173 | newossmixerobject(PyObject *arg) |
| 174 | { |
Greg Ward | 0b6dfb8 | 2003-03-10 03:17:06 +0000 | [diff] [blame] | 175 | char *basedev = NULL; |
| 176 | int fd; |
Greg Ward | 58ae13c | 2002-12-31 03:07:21 +0000 | [diff] [blame] | 177 | oss_mixer_t *self; |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 178 | |
Greg Ward | 0b6dfb8 | 2003-03-10 03:17:06 +0000 | [diff] [blame] | 179 | if (!PyArg_ParseTuple(arg, "|s", &basedev)) { |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 180 | return NULL; |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | if (basedev == NULL) { |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 184 | basedev = getenv("MIXERDEV"); |
| 185 | if (basedev == NULL) /* MIXERDEV not set */ |
| 186 | basedev = "/dev/mixer"; |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Greg Ward | 0b6dfb8 | 2003-03-10 03:17:06 +0000 | [diff] [blame] | 189 | if ((fd = open(basedev, O_RDWR)) == -1) { |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 190 | PyErr_SetFromErrnoWithFilename(PyExc_IOError, basedev); |
| 191 | return NULL; |
| 192 | } |
Greg Ward | 0b6dfb8 | 2003-03-10 03:17:06 +0000 | [diff] [blame] | 193 | |
Greg Ward | 58ae13c | 2002-12-31 03:07:21 +0000 | [diff] [blame] | 194 | if ((self = PyObject_New(oss_mixer_t, &OSSMixerType)) == NULL) { |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 195 | close(fd); |
| 196 | return NULL; |
| 197 | } |
| 198 | |
Greg Ward | 58ae13c | 2002-12-31 03:07:21 +0000 | [diff] [blame] | 199 | self->fd = fd; |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 200 | |
Greg Ward | 58ae13c | 2002-12-31 03:07:21 +0000 | [diff] [blame] | 201 | return self; |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | static void |
Greg Ward | 58ae13c | 2002-12-31 03:07:21 +0000 | [diff] [blame] | 205 | oss_mixer_dealloc(oss_mixer_t *self) |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 206 | { |
| 207 | /* if already closed, don't reclose it */ |
Greg Ward | 58ae13c | 2002-12-31 03:07:21 +0000 | [diff] [blame] | 208 | if (self->fd != -1) |
| 209 | close(self->fd); |
| 210 | PyObject_Del(self); |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 213 | |
| 214 | /* Methods to wrap the OSS ioctls. The calling convention is pretty |
| 215 | simple: |
| 216 | nonblock() -> ioctl(fd, SNDCTL_DSP_NONBLOCK) |
| 217 | fmt = setfmt(fmt) -> ioctl(fd, SNDCTL_DSP_SETFMT, &fmt) |
| 218 | etc. |
| 219 | */ |
| 220 | |
| 221 | |
| 222 | /* _do_ioctl_1() is a private helper function used for the OSS ioctls -- |
| 223 | SNDCTL_DSP_{SETFMT,CHANNELS,SPEED} -- that that are called from C |
| 224 | like this: |
| 225 | ioctl(fd, SNDCTL_DSP_cmd, &arg) |
| 226 | |
| 227 | where arg is the value to set, and on return the driver sets arg to |
| 228 | the value that was actually set. Mapping this to Python is obvious: |
| 229 | arg = dsp.xxx(arg) |
| 230 | */ |
| 231 | static PyObject * |
Greg Ward | da9f853 | 2002-12-11 14:49:59 +0000 | [diff] [blame] | 232 | _do_ioctl_1(int fd, PyObject *args, char *fname, int cmd) |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 233 | { |
Greg Ward | da9f853 | 2002-12-11 14:49:59 +0000 | [diff] [blame] | 234 | char argfmt[33] = "i:"; |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 235 | int arg; |
| 236 | |
Greg Ward | da9f853 | 2002-12-11 14:49:59 +0000 | [diff] [blame] | 237 | assert(strlen(fname) <= 30); |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 238 | strcat(argfmt, fname); |
| 239 | if (!PyArg_ParseTuple(args, argfmt, &arg)) |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 240 | return NULL; |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 241 | |
Greg Ward | da9f853 | 2002-12-11 14:49:59 +0000 | [diff] [blame] | 242 | if (ioctl(fd, cmd, &arg) == -1) |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 243 | return PyErr_SetFromErrno(PyExc_IOError); |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 244 | return PyInt_FromLong(arg); |
| 245 | } |
| 246 | |
Greg Ward | da1cacb | 2002-12-31 03:02:23 +0000 | [diff] [blame] | 247 | |
| 248 | /* ---------------------------------------------------------------------- |
| 249 | * Helper functions |
| 250 | */ |
| 251 | |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 252 | /* _do_ioctl_1_internal() is a wrapper for ioctls that take no inputs |
| 253 | but return an output -- ie. we need to pass a pointer to a local C |
| 254 | variable so the driver can write its output there, but from Python |
| 255 | all we see is the return value. For example, |
| 256 | SOUND_MIXER_READ_DEVMASK returns a bitmask of available mixer |
| 257 | devices, but does not use the value of the parameter passed-in in any |
| 258 | way. |
| 259 | */ |
| 260 | |
| 261 | static PyObject * |
| 262 | _do_ioctl_1_internal(int fd, PyObject *args, char *fname, int cmd) |
| 263 | { |
| 264 | char argfmt[32] = ":"; |
| 265 | int arg = 0; |
| 266 | |
| 267 | assert(strlen(fname) <= 30); |
| 268 | strcat(argfmt, fname); |
| 269 | if (!PyArg_ParseTuple(args, argfmt, &arg)) |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 270 | return NULL; |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 271 | |
| 272 | if (ioctl(fd, cmd, &arg) == -1) |
| 273 | return PyErr_SetFromErrno(PyExc_IOError); |
| 274 | return PyInt_FromLong(arg); |
| 275 | } |
| 276 | |
| 277 | |
| 278 | |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 279 | /* _do_ioctl_0() is a private helper for the no-argument ioctls: |
| 280 | SNDCTL_DSP_{SYNC,RESET,POST}. */ |
| 281 | static PyObject * |
Greg Ward | da9f853 | 2002-12-11 14:49:59 +0000 | [diff] [blame] | 282 | _do_ioctl_0(int fd, PyObject *args, char *fname, int cmd) |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 283 | { |
Greg Ward | da9f853 | 2002-12-11 14:49:59 +0000 | [diff] [blame] | 284 | char argfmt[32] = ":"; |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 285 | |
Greg Ward | da9f853 | 2002-12-11 14:49:59 +0000 | [diff] [blame] | 286 | assert(strlen(fname) <= 30); |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 287 | strcat(argfmt, fname); |
| 288 | if (!PyArg_ParseTuple(args, argfmt)) |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 289 | return NULL; |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 290 | |
Greg Ward | da9f853 | 2002-12-11 14:49:59 +0000 | [diff] [blame] | 291 | if (ioctl(fd, cmd, 0) == -1) |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 292 | return PyErr_SetFromErrno(PyExc_IOError); |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 293 | Py_INCREF(Py_None); |
| 294 | return Py_None; |
| 295 | } |
| 296 | |
| 297 | |
Greg Ward | da1cacb | 2002-12-31 03:02:23 +0000 | [diff] [blame] | 298 | /* ---------------------------------------------------------------------- |
Greg Ward | 499b73e | 2002-12-31 03:04:52 +0000 | [diff] [blame] | 299 | * Methods of DSP objects (OSSAudioType) |
Greg Ward | da1cacb | 2002-12-31 03:02:23 +0000 | [diff] [blame] | 300 | */ |
| 301 | |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 302 | static PyObject * |
Greg Ward | 499b73e | 2002-12-31 03:04:52 +0000 | [diff] [blame] | 303 | oss_nonblock(oss_audio_t *self, PyObject *args) |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 304 | { |
| 305 | /* Hmmm: it doesn't appear to be possible to return to blocking |
| 306 | mode once we're in non-blocking mode! */ |
| 307 | if (!PyArg_ParseTuple(args, ":nonblock")) |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 308 | return NULL; |
Greg Ward | 7b43c68 | 2002-12-30 02:29:28 +0000 | [diff] [blame] | 309 | if (ioctl(self->fd, SNDCTL_DSP_NONBLOCK, NULL) == -1) |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 310 | return PyErr_SetFromErrno(PyExc_IOError); |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 311 | Py_INCREF(Py_None); |
| 312 | return Py_None; |
| 313 | } |
| 314 | |
| 315 | static PyObject * |
Greg Ward | 499b73e | 2002-12-31 03:04:52 +0000 | [diff] [blame] | 316 | oss_setfmt(oss_audio_t *self, PyObject *args) |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 317 | { |
Greg Ward | 7b43c68 | 2002-12-30 02:29:28 +0000 | [diff] [blame] | 318 | return _do_ioctl_1(self->fd, args, "setfmt", SNDCTL_DSP_SETFMT); |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | static PyObject * |
Greg Ward | 499b73e | 2002-12-31 03:04:52 +0000 | [diff] [blame] | 322 | oss_getfmts(oss_audio_t *self, PyObject *args) |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 323 | { |
| 324 | int mask; |
| 325 | if (!PyArg_ParseTuple(args, ":getfmts")) |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 326 | return NULL; |
Greg Ward | 7b43c68 | 2002-12-30 02:29:28 +0000 | [diff] [blame] | 327 | if (ioctl(self->fd, SNDCTL_DSP_GETFMTS, &mask) == -1) |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 328 | return PyErr_SetFromErrno(PyExc_IOError); |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 329 | return PyInt_FromLong(mask); |
| 330 | } |
| 331 | |
| 332 | static PyObject * |
Greg Ward | 499b73e | 2002-12-31 03:04:52 +0000 | [diff] [blame] | 333 | oss_channels(oss_audio_t *self, PyObject *args) |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 334 | { |
Greg Ward | 7b43c68 | 2002-12-30 02:29:28 +0000 | [diff] [blame] | 335 | return _do_ioctl_1(self->fd, args, "channels", SNDCTL_DSP_CHANNELS); |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | static PyObject * |
Greg Ward | 499b73e | 2002-12-31 03:04:52 +0000 | [diff] [blame] | 339 | oss_speed(oss_audio_t *self, PyObject *args) |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 340 | { |
Greg Ward | 7b43c68 | 2002-12-30 02:29:28 +0000 | [diff] [blame] | 341 | return _do_ioctl_1(self->fd, args, "speed", SNDCTL_DSP_SPEED); |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | static PyObject * |
Greg Ward | 499b73e | 2002-12-31 03:04:52 +0000 | [diff] [blame] | 345 | oss_sync(oss_audio_t *self, PyObject *args) |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 346 | { |
Greg Ward | 7b43c68 | 2002-12-30 02:29:28 +0000 | [diff] [blame] | 347 | return _do_ioctl_0(self->fd, args, "sync", SNDCTL_DSP_SYNC); |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | static PyObject * |
Greg Ward | 499b73e | 2002-12-31 03:04:52 +0000 | [diff] [blame] | 351 | oss_reset(oss_audio_t *self, PyObject *args) |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 352 | { |
Greg Ward | 7b43c68 | 2002-12-30 02:29:28 +0000 | [diff] [blame] | 353 | return _do_ioctl_0(self->fd, args, "reset", SNDCTL_DSP_RESET); |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | static PyObject * |
Greg Ward | 499b73e | 2002-12-31 03:04:52 +0000 | [diff] [blame] | 357 | oss_post(oss_audio_t *self, PyObject *args) |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 358 | { |
Greg Ward | 7b43c68 | 2002-12-30 02:29:28 +0000 | [diff] [blame] | 359 | return _do_ioctl_0(self->fd, args, "post", SNDCTL_DSP_POST); |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | |
| 363 | /* Regular file methods: read(), write(), close(), etc. as well |
| 364 | as one convenience method, writeall(). */ |
| 365 | |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 366 | static PyObject * |
Greg Ward | 499b73e | 2002-12-31 03:04:52 +0000 | [diff] [blame] | 367 | oss_read(oss_audio_t *self, PyObject *args) |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 368 | { |
| 369 | int size, count; |
| 370 | char *cp; |
| 371 | PyObject *rv; |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 372 | |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 373 | if (!PyArg_ParseTuple(args, "i:read", &size)) |
| 374 | return NULL; |
| 375 | rv = PyString_FromStringAndSize(NULL, size); |
| 376 | if (rv == NULL) |
| 377 | return NULL; |
| 378 | cp = PyString_AS_STRING(rv); |
Greg Ward | 7b43c68 | 2002-12-30 02:29:28 +0000 | [diff] [blame] | 379 | if ((count = read(self->fd, cp, size)) < 0) { |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 380 | PyErr_SetFromErrno(PyExc_IOError); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 381 | Py_DECREF(rv); |
| 382 | return NULL; |
| 383 | } |
Greg Ward | 7b43c68 | 2002-12-30 02:29:28 +0000 | [diff] [blame] | 384 | self->icount += count; |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 385 | _PyString_Resize(&rv, count); |
| 386 | return rv; |
| 387 | } |
| 388 | |
| 389 | static PyObject * |
Greg Ward | 499b73e | 2002-12-31 03:04:52 +0000 | [diff] [blame] | 390 | oss_write(oss_audio_t *self, PyObject *args) |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 391 | { |
| 392 | char *cp; |
| 393 | int rv, size; |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 394 | |
| 395 | if (!PyArg_ParseTuple(args, "s#:write", &cp, &size)) { |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 396 | return NULL; |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 397 | } |
Greg Ward | 7b43c68 | 2002-12-30 02:29:28 +0000 | [diff] [blame] | 398 | if ((rv = write(self->fd, cp, size)) == -1) { |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 399 | return PyErr_SetFromErrno(PyExc_IOError); |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 400 | } else { |
Greg Ward | 7b43c68 | 2002-12-30 02:29:28 +0000 | [diff] [blame] | 401 | self->ocount += rv; |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 402 | } |
| 403 | return PyInt_FromLong(rv); |
| 404 | } |
| 405 | |
| 406 | static PyObject * |
Greg Ward | 499b73e | 2002-12-31 03:04:52 +0000 | [diff] [blame] | 407 | oss_writeall(oss_audio_t *self, PyObject *args) |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 408 | { |
| 409 | char *cp; |
| 410 | int rv, size; |
| 411 | fd_set write_set_fds; |
| 412 | int select_rv; |
| 413 | |
| 414 | /* NB. writeall() is only useful in non-blocking mode: according to |
| 415 | Guenter Geiger <geiger@xdv.org> on the linux-audio-dev list |
| 416 | (http://eca.cx/lad/2002/11/0380.html), OSS guarantees that |
| 417 | write() in blocking mode consumes the whole buffer. In blocking |
| 418 | mode, the behaviour of write() and writeall() from Python is |
| 419 | indistinguishable. */ |
| 420 | |
| 421 | if (!PyArg_ParseTuple(args, "s#:write", &cp, &size)) |
| 422 | return NULL; |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 423 | |
| 424 | /* use select to wait for audio device to be available */ |
| 425 | FD_ZERO(&write_set_fds); |
Greg Ward | 7b43c68 | 2002-12-30 02:29:28 +0000 | [diff] [blame] | 426 | FD_SET(self->fd, &write_set_fds); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 427 | |
| 428 | while (size > 0) { |
Greg Ward | 7b43c68 | 2002-12-30 02:29:28 +0000 | [diff] [blame] | 429 | select_rv = select(self->fd+1, NULL, &write_set_fds, NULL, NULL); |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 430 | assert(select_rv != 0); /* no timeout, can't expire */ |
| 431 | if (select_rv == -1) |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 432 | return PyErr_SetFromErrno(PyExc_IOError); |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 433 | |
Greg Ward | 7b43c68 | 2002-12-30 02:29:28 +0000 | [diff] [blame] | 434 | rv = write(self->fd, cp, size); |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 435 | if (rv == -1) { |
| 436 | if (errno == EAGAIN) { /* buffer is full, try again */ |
| 437 | errno = 0; |
| 438 | continue; |
| 439 | } else /* it's a real error */ |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 440 | return PyErr_SetFromErrno(PyExc_IOError); |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 441 | } else { /* wrote rv bytes */ |
Greg Ward | 7b43c68 | 2002-12-30 02:29:28 +0000 | [diff] [blame] | 442 | self->ocount += rv; |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 443 | size -= rv; |
| 444 | cp += rv; |
| 445 | } |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 446 | } |
| 447 | Py_INCREF(Py_None); |
| 448 | return Py_None; |
| 449 | } |
| 450 | |
| 451 | static PyObject * |
Greg Ward | 499b73e | 2002-12-31 03:04:52 +0000 | [diff] [blame] | 452 | oss_close(oss_audio_t *self, PyObject *args) |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 453 | { |
| 454 | if (!PyArg_ParseTuple(args, ":close")) |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 455 | return NULL; |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 456 | |
Greg Ward | 7b43c68 | 2002-12-30 02:29:28 +0000 | [diff] [blame] | 457 | if (self->fd >= 0) { |
| 458 | close(self->fd); |
| 459 | self->fd = -1; |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 460 | } |
| 461 | Py_INCREF(Py_None); |
| 462 | return Py_None; |
| 463 | } |
| 464 | |
| 465 | static PyObject * |
Greg Ward | 499b73e | 2002-12-31 03:04:52 +0000 | [diff] [blame] | 466 | oss_fileno(oss_audio_t *self, PyObject *args) |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 467 | { |
| 468 | if (!PyArg_ParseTuple(args, ":fileno")) |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 469 | return NULL; |
Greg Ward | 7b43c68 | 2002-12-30 02:29:28 +0000 | [diff] [blame] | 470 | return PyInt_FromLong(self->fd); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 473 | |
| 474 | /* Convenience methods: these generally wrap a couple of ioctls into one |
| 475 | common task. */ |
| 476 | |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 477 | static PyObject * |
Greg Ward | 499b73e | 2002-12-31 03:04:52 +0000 | [diff] [blame] | 478 | oss_setparameters(oss_audio_t *self, PyObject *args) |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 479 | { |
| 480 | int rate, ssize, nchannels, n, fmt, emulate=0; |
| 481 | |
| 482 | if (!PyArg_ParseTuple(args, "iiii|i:setparameters", |
| 483 | &rate, &ssize, &nchannels, &fmt, &emulate)) |
| 484 | return NULL; |
| 485 | |
| 486 | if (rate < 0) { |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 487 | PyErr_Format(PyExc_ValueError, "expected rate >= 0, not %d", |
| 488 | rate); |
| 489 | return NULL; |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 490 | } |
| 491 | if (ssize < 0) { |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 492 | PyErr_Format(PyExc_ValueError, "expected sample size >= 0, not %d", |
| 493 | ssize); |
| 494 | return NULL; |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 495 | } |
| 496 | if (nchannels != 1 && nchannels != 2) { |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 497 | PyErr_Format(PyExc_ValueError, "nchannels must be 1 or 2, not %d", |
| 498 | nchannels); |
| 499 | return NULL; |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | for (n = 0; n < n_audio_types; n++) |
| 503 | if (fmt == audio_types[n].a_fmt) |
| 504 | break; |
| 505 | if (n == n_audio_types) { |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 506 | PyErr_Format(PyExc_ValueError, "unknown audio encoding: %d", fmt); |
| 507 | return NULL; |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 508 | } |
| 509 | if (audio_types[n].a_bps != ssize) { |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 510 | PyErr_Format(PyExc_ValueError, |
| 511 | "for %s, expected sample size %d, not %d", |
| 512 | audio_types[n].a_name, audio_types[n].a_bps, ssize); |
| 513 | return NULL; |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | if (emulate == 0) { |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 517 | if ((self->afmts & audio_types[n].a_fmt) == 0) { |
| 518 | PyErr_Format(PyExc_ValueError, |
| 519 | "%s format not supported by device", |
| 520 | audio_types[n].a_name); |
| 521 | return NULL; |
| 522 | } |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 523 | } |
Greg Ward | 7b43c68 | 2002-12-30 02:29:28 +0000 | [diff] [blame] | 524 | if (ioctl(self->fd, SNDCTL_DSP_SETFMT, |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 525 | &audio_types[n].a_fmt) == -1) { |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 526 | PyErr_SetFromErrno(PyExc_IOError); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 527 | return NULL; |
| 528 | } |
Greg Ward | 7b43c68 | 2002-12-30 02:29:28 +0000 | [diff] [blame] | 529 | if (ioctl(self->fd, SNDCTL_DSP_CHANNELS, &nchannels) == -1) { |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 530 | PyErr_SetFromErrno(PyExc_IOError); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 531 | return NULL; |
| 532 | } |
Greg Ward | 7b43c68 | 2002-12-30 02:29:28 +0000 | [diff] [blame] | 533 | if (ioctl(self->fd, SNDCTL_DSP_SPEED, &rate) == -1) { |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 534 | PyErr_SetFromErrno(PyExc_IOError); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 535 | return NULL; |
| 536 | } |
| 537 | |
| 538 | Py_INCREF(Py_None); |
| 539 | return Py_None; |
| 540 | } |
| 541 | |
| 542 | static int |
Greg Ward | 499b73e | 2002-12-31 03:04:52 +0000 | [diff] [blame] | 543 | _ssize(oss_audio_t *self, int *nchannels, int *ssize) |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 544 | { |
| 545 | int fmt; |
| 546 | |
| 547 | fmt = 0; |
Greg Ward | 7b43c68 | 2002-12-30 02:29:28 +0000 | [diff] [blame] | 548 | if (ioctl(self->fd, SNDCTL_DSP_SETFMT, &fmt) < 0) |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 549 | return -errno; |
| 550 | |
| 551 | switch (fmt) { |
| 552 | case AFMT_MU_LAW: |
| 553 | case AFMT_A_LAW: |
| 554 | case AFMT_U8: |
| 555 | case AFMT_S8: |
| 556 | *ssize = sizeof(char); |
| 557 | break; |
| 558 | case AFMT_S16_LE: |
| 559 | case AFMT_S16_BE: |
| 560 | case AFMT_U16_LE: |
| 561 | case AFMT_U16_BE: |
| 562 | *ssize = sizeof(short); |
| 563 | break; |
| 564 | case AFMT_MPEG: |
| 565 | case AFMT_IMA_ADPCM: |
| 566 | default: |
| 567 | return -EOPNOTSUPP; |
| 568 | } |
| 569 | *nchannels = 0; |
Greg Ward | 7b43c68 | 2002-12-30 02:29:28 +0000 | [diff] [blame] | 570 | if (ioctl(self->fd, SNDCTL_DSP_CHANNELS, nchannels) < 0) |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 571 | return -errno; |
| 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | |
| 576 | /* bufsize returns the size of the hardware audio buffer in number |
| 577 | of samples */ |
| 578 | static PyObject * |
Greg Ward | 499b73e | 2002-12-31 03:04:52 +0000 | [diff] [blame] | 579 | oss_bufsize(oss_audio_t *self, PyObject *args) |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 580 | { |
| 581 | audio_buf_info ai; |
| 582 | int nchannels, ssize; |
| 583 | |
| 584 | if (!PyArg_ParseTuple(args, ":bufsize")) return NULL; |
| 585 | |
| 586 | if (_ssize(self, &nchannels, &ssize) < 0) { |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 587 | PyErr_SetFromErrno(PyExc_IOError); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 588 | return NULL; |
| 589 | } |
Greg Ward | 7b43c68 | 2002-12-30 02:29:28 +0000 | [diff] [blame] | 590 | if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) { |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 591 | PyErr_SetFromErrno(PyExc_IOError); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 592 | return NULL; |
| 593 | } |
| 594 | return PyInt_FromLong((ai.fragstotal * ai.fragsize) / (nchannels * ssize)); |
| 595 | } |
| 596 | |
| 597 | /* obufcount returns the number of samples that are available in the |
| 598 | hardware for playing */ |
| 599 | static PyObject * |
Greg Ward | 499b73e | 2002-12-31 03:04:52 +0000 | [diff] [blame] | 600 | oss_obufcount(oss_audio_t *self, PyObject *args) |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 601 | { |
| 602 | audio_buf_info ai; |
| 603 | int nchannels, ssize; |
| 604 | |
| 605 | if (!PyArg_ParseTuple(args, ":obufcount")) |
| 606 | return NULL; |
| 607 | |
| 608 | if (_ssize(self, &nchannels, &ssize) < 0) { |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 609 | PyErr_SetFromErrno(PyExc_IOError); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 610 | return NULL; |
| 611 | } |
Greg Ward | 7b43c68 | 2002-12-30 02:29:28 +0000 | [diff] [blame] | 612 | if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) { |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 613 | PyErr_SetFromErrno(PyExc_IOError); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 614 | return NULL; |
| 615 | } |
| 616 | return PyInt_FromLong((ai.fragstotal * ai.fragsize - ai.bytes) / |
| 617 | (ssize * nchannels)); |
| 618 | } |
| 619 | |
| 620 | /* obufcount returns the number of samples that can be played without |
| 621 | blocking */ |
| 622 | static PyObject * |
Greg Ward | 499b73e | 2002-12-31 03:04:52 +0000 | [diff] [blame] | 623 | oss_obuffree(oss_audio_t *self, PyObject *args) |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 624 | { |
| 625 | audio_buf_info ai; |
| 626 | int nchannels, ssize; |
| 627 | |
| 628 | if (!PyArg_ParseTuple(args, ":obuffree")) |
| 629 | return NULL; |
| 630 | |
| 631 | if (_ssize(self, &nchannels, &ssize) < 0) { |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 632 | PyErr_SetFromErrno(PyExc_IOError); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 633 | return NULL; |
| 634 | } |
Greg Ward | 7b43c68 | 2002-12-30 02:29:28 +0000 | [diff] [blame] | 635 | if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) { |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 636 | PyErr_SetFromErrno(PyExc_IOError); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 637 | return NULL; |
| 638 | } |
| 639 | return PyInt_FromLong(ai.bytes / (ssize * nchannels)); |
| 640 | } |
| 641 | |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 642 | static PyObject * |
Greg Ward | 499b73e | 2002-12-31 03:04:52 +0000 | [diff] [blame] | 643 | oss_getptr(oss_audio_t *self, PyObject *args) |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 644 | { |
| 645 | count_info info; |
| 646 | int req; |
| 647 | |
| 648 | if (!PyArg_ParseTuple(args, ":getptr")) |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 649 | return NULL; |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 650 | |
Greg Ward | 7b43c68 | 2002-12-30 02:29:28 +0000 | [diff] [blame] | 651 | if (self->mode == O_RDONLY) |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 652 | req = SNDCTL_DSP_GETIPTR; |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 653 | else |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 654 | req = SNDCTL_DSP_GETOPTR; |
Greg Ward | 7b43c68 | 2002-12-30 02:29:28 +0000 | [diff] [blame] | 655 | if (ioctl(self->fd, req, &info) == -1) { |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 656 | PyErr_SetFromErrno(PyExc_IOError); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 657 | return NULL; |
| 658 | } |
| 659 | return Py_BuildValue("iii", info.bytes, info.blocks, info.ptr); |
| 660 | } |
| 661 | |
Greg Ward | da1cacb | 2002-12-31 03:02:23 +0000 | [diff] [blame] | 662 | |
| 663 | /* ---------------------------------------------------------------------- |
| 664 | * Methods of mixer objects (OSSMixerType) |
| 665 | */ |
| 666 | |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 667 | static PyObject * |
| 668 | oss_mixer_close(oss_mixer_t *self, PyObject *args) |
| 669 | { |
| 670 | if (!PyArg_ParseTuple(args, ":close")) |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 671 | return NULL; |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 672 | |
Greg Ward | 7b43c68 | 2002-12-30 02:29:28 +0000 | [diff] [blame] | 673 | if (self->fd >= 0) { |
| 674 | close(self->fd); |
| 675 | self->fd = -1; |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 676 | } |
| 677 | Py_INCREF(Py_None); |
| 678 | return Py_None; |
| 679 | } |
| 680 | |
| 681 | static PyObject * |
| 682 | oss_mixer_fileno(oss_mixer_t *self, PyObject *args) |
| 683 | { |
| 684 | if (!PyArg_ParseTuple(args, ":fileno")) |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 685 | return NULL; |
Greg Ward | 7b43c68 | 2002-12-30 02:29:28 +0000 | [diff] [blame] | 686 | return PyInt_FromLong(self->fd); |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | /* Simple mixer interface methods */ |
| 690 | |
| 691 | static PyObject * |
Greg Ward | 2d6f9a9 | 2002-12-31 02:54:43 +0000 | [diff] [blame] | 692 | oss_mixer_controls(oss_mixer_t *self, PyObject *args) |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 693 | { |
Greg Ward | 2d6f9a9 | 2002-12-31 02:54:43 +0000 | [diff] [blame] | 694 | return _do_ioctl_1_internal(self->fd, args, "controls", |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 695 | SOUND_MIXER_READ_DEVMASK); |
| 696 | } |
| 697 | |
| 698 | static PyObject * |
Greg Ward | 2d6f9a9 | 2002-12-31 02:54:43 +0000 | [diff] [blame] | 699 | oss_mixer_stereocontrols(oss_mixer_t *self, PyObject *args) |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 700 | { |
Greg Ward | 2d6f9a9 | 2002-12-31 02:54:43 +0000 | [diff] [blame] | 701 | return _do_ioctl_1_internal(self->fd, args, "stereocontrols", |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 702 | SOUND_MIXER_READ_STEREODEVS); |
| 703 | } |
| 704 | |
| 705 | static PyObject * |
Greg Ward | 2d6f9a9 | 2002-12-31 02:54:43 +0000 | [diff] [blame] | 706 | oss_mixer_reccontrols(oss_mixer_t *self, PyObject *args) |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 707 | { |
Greg Ward | 2d6f9a9 | 2002-12-31 02:54:43 +0000 | [diff] [blame] | 708 | return _do_ioctl_1_internal(self->fd, args, "reccontrols", |
Greg Ward | b69bb3d | 2002-12-12 17:35:45 +0000 | [diff] [blame] | 709 | SOUND_MIXER_READ_RECMASK); |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | static PyObject * |
Greg Ward | e703766 | 2002-12-30 03:01:48 +0000 | [diff] [blame] | 713 | oss_mixer_get(oss_mixer_t *self, PyObject *args) |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 714 | { |
| 715 | int channel, volume; |
| 716 | |
| 717 | /* Can't use _do_ioctl_1 because of encoded arg thingy. */ |
Greg Ward | e703766 | 2002-12-30 03:01:48 +0000 | [diff] [blame] | 718 | if (!PyArg_ParseTuple(args, "i:get", &channel)) |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 719 | return NULL; |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 720 | |
| 721 | if (channel < 0 || channel > SOUND_MIXER_NRDEVICES) { |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 722 | PyErr_SetString(OSSAudioError, "Invalid mixer channel specified."); |
| 723 | return NULL; |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Greg Ward | e703766 | 2002-12-30 03:01:48 +0000 | [diff] [blame] | 726 | if (ioctl(self->fd, MIXER_READ(channel), &volume) == -1) |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 727 | return PyErr_SetFromErrno(PyExc_IOError); |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 728 | |
Greg Ward | e703766 | 2002-12-30 03:01:48 +0000 | [diff] [blame] | 729 | return Py_BuildValue("(ii)", volume & 0xff, (volume & 0xff00) >> 8); |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | static PyObject * |
Greg Ward | e703766 | 2002-12-30 03:01:48 +0000 | [diff] [blame] | 733 | oss_mixer_set(oss_mixer_t *self, PyObject *args) |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 734 | { |
| 735 | int channel, volume, leftVol, rightVol; |
| 736 | |
| 737 | /* Can't use _do_ioctl_1 because of encoded arg thingy. */ |
Greg Ward | e703766 | 2002-12-30 03:01:48 +0000 | [diff] [blame] | 738 | if (!PyArg_ParseTuple(args, "i(ii):set", &channel, &leftVol, &rightVol)) |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 739 | return NULL; |
| 740 | |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 741 | if (channel < 0 || channel > SOUND_MIXER_NRDEVICES) { |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 742 | PyErr_SetString(OSSAudioError, "Invalid mixer channel specified."); |
| 743 | return NULL; |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | if (leftVol < 0 || rightVol < 0 || leftVol > 100 || rightVol > 100) { |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 747 | PyErr_SetString(OSSAudioError, "Volumes must be between 0 and 100."); |
| 748 | return NULL; |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | volume = (rightVol << 8) | leftVol; |
| 752 | |
Greg Ward | e703766 | 2002-12-30 03:01:48 +0000 | [diff] [blame] | 753 | if (ioctl(self->fd, MIXER_WRITE(channel), &volume) == -1) |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 754 | return PyErr_SetFromErrno(PyExc_IOError); |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 755 | |
Greg Ward | e703766 | 2002-12-30 03:01:48 +0000 | [diff] [blame] | 756 | return Py_BuildValue("(ii)", volume & 0xff, (volume & 0xff00) >> 8); |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | static PyObject * |
Greg Ward | f05aa10 | 2002-12-30 23:19:32 +0000 | [diff] [blame] | 760 | oss_mixer_get_recsrc(oss_mixer_t *self, PyObject *args) |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 761 | { |
Greg Ward | f05aa10 | 2002-12-30 23:19:32 +0000 | [diff] [blame] | 762 | return _do_ioctl_1_internal(self->fd, args, "get_recsrc", |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 763 | SOUND_MIXER_READ_RECSRC); |
| 764 | } |
| 765 | |
| 766 | static PyObject * |
Greg Ward | f05aa10 | 2002-12-30 23:19:32 +0000 | [diff] [blame] | 767 | oss_mixer_set_recsrc(oss_mixer_t *self, PyObject *args) |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 768 | { |
Greg Ward | f05aa10 | 2002-12-30 23:19:32 +0000 | [diff] [blame] | 769 | return _do_ioctl_1(self->fd, args, "set_recsrc", |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 770 | SOUND_MIXER_WRITE_RECSRC); |
| 771 | } |
| 772 | |
| 773 | |
Greg Ward | da1cacb | 2002-12-31 03:02:23 +0000 | [diff] [blame] | 774 | /* ---------------------------------------------------------------------- |
| 775 | * Method tables and other bureaucracy |
| 776 | */ |
| 777 | |
Greg Ward | 8c6b6a9 | 2002-12-11 14:43:13 +0000 | [diff] [blame] | 778 | static PyMethodDef oss_methods[] = { |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 779 | /* Regular file methods */ |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 780 | { "read", (PyCFunction)oss_read, METH_VARARGS }, |
| 781 | { "write", (PyCFunction)oss_write, METH_VARARGS }, |
| 782 | { "writeall", (PyCFunction)oss_writeall, METH_VARARGS }, |
| 783 | { "close", (PyCFunction)oss_close, METH_VARARGS }, |
| 784 | { "fileno", (PyCFunction)oss_fileno, METH_VARARGS }, |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 785 | |
| 786 | /* Simple ioctl wrappers */ |
Greg Ward | 8c6b6a9 | 2002-12-11 14:43:13 +0000 | [diff] [blame] | 787 | { "nonblock", (PyCFunction)oss_nonblock, METH_VARARGS }, |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 788 | { "setfmt", (PyCFunction)oss_setfmt, METH_VARARGS }, |
Greg Ward | 8c6b6a9 | 2002-12-11 14:43:13 +0000 | [diff] [blame] | 789 | { "getfmts", (PyCFunction)oss_getfmts, METH_VARARGS }, |
| 790 | { "channels", (PyCFunction)oss_channels, METH_VARARGS }, |
| 791 | { "speed", (PyCFunction)oss_speed, METH_VARARGS }, |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 792 | { "sync", (PyCFunction)oss_sync, METH_VARARGS }, |
| 793 | { "reset", (PyCFunction)oss_reset, METH_VARARGS }, |
| 794 | { "post", (PyCFunction)oss_post, METH_VARARGS }, |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 795 | |
| 796 | /* Convenience methods -- wrap a couple of ioctls together */ |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 797 | { "setparameters", (PyCFunction)oss_setparameters, METH_VARARGS }, |
| 798 | { "bufsize", (PyCFunction)oss_bufsize, METH_VARARGS }, |
| 799 | { "obufcount", (PyCFunction)oss_obufcount, METH_VARARGS }, |
| 800 | { "obuffree", (PyCFunction)oss_obuffree, METH_VARARGS }, |
Greg Ward | 8c6b6a9 | 2002-12-11 14:43:13 +0000 | [diff] [blame] | 801 | { "getptr", (PyCFunction)oss_getptr, METH_VARARGS }, |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 802 | |
| 803 | /* Aliases for backwards compatibility */ |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 804 | { "flush", (PyCFunction)oss_sync, METH_VARARGS }, |
Greg Ward | 131bce0 | 2002-11-30 22:56:44 +0000 | [diff] [blame] | 805 | |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 806 | { NULL, NULL} /* sentinel */ |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 807 | }; |
| 808 | |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 809 | static PyMethodDef oss_mixer_methods[] = { |
| 810 | /* Regular file method - OSS mixers are ioctl-only interface */ |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 811 | { "close", (PyCFunction)oss_mixer_close, METH_VARARGS }, |
| 812 | { "fileno", (PyCFunction)oss_mixer_fileno, METH_VARARGS }, |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 813 | |
| 814 | /* Simple ioctl wrappers */ |
Greg Ward | 2d6f9a9 | 2002-12-31 02:54:43 +0000 | [diff] [blame] | 815 | { "controls", (PyCFunction)oss_mixer_controls, METH_VARARGS }, |
| 816 | { "stereocontrols", (PyCFunction)oss_mixer_stereocontrols, METH_VARARGS}, |
| 817 | { "reccontrols", (PyCFunction)oss_mixer_reccontrols, METH_VARARGS}, |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 818 | { "get", (PyCFunction)oss_mixer_get, METH_VARARGS }, |
| 819 | { "set", (PyCFunction)oss_mixer_set, METH_VARARGS }, |
Greg Ward | f05aa10 | 2002-12-30 23:19:32 +0000 | [diff] [blame] | 820 | { "get_recsrc", (PyCFunction)oss_mixer_get_recsrc, METH_VARARGS }, |
| 821 | { "set_recsrc", (PyCFunction)oss_mixer_set_recsrc, METH_VARARGS }, |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 822 | |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 823 | { NULL, NULL} |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 824 | }; |
| 825 | |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 826 | static PyObject * |
Greg Ward | 58ae13c | 2002-12-31 03:07:21 +0000 | [diff] [blame] | 827 | oss_getattr(oss_audio_t *self, char *name) |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 828 | { |
Greg Ward | 58ae13c | 2002-12-31 03:07:21 +0000 | [diff] [blame] | 829 | return Py_FindMethod(oss_methods, (PyObject *)self, name); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 832 | static PyObject * |
Greg Ward | 58ae13c | 2002-12-31 03:07:21 +0000 | [diff] [blame] | 833 | oss_mixer_getattr(oss_mixer_t *self, char *name) |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 834 | { |
Greg Ward | 58ae13c | 2002-12-31 03:07:21 +0000 | [diff] [blame] | 835 | return Py_FindMethod(oss_mixer_methods, (PyObject *)self, name); |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 836 | } |
| 837 | |
Greg Ward | 499b73e | 2002-12-31 03:04:52 +0000 | [diff] [blame] | 838 | static PyTypeObject OSSAudioType = { |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 839 | PyObject_HEAD_INIT(&PyType_Type) |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 840 | 0, /*ob_size*/ |
Greg Ward | 9a568eb | 2002-11-30 23:20:09 +0000 | [diff] [blame] | 841 | "ossaudiodev.oss_audio_device", /*tp_name*/ |
Greg Ward | 499b73e | 2002-12-31 03:04:52 +0000 | [diff] [blame] | 842 | sizeof(oss_audio_t), /*tp_size*/ |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 843 | 0, /*tp_itemsize*/ |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 844 | /* methods */ |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 845 | (destructor)oss_dealloc, /*tp_dealloc*/ |
| 846 | 0, /*tp_print*/ |
| 847 | (getattrfunc)oss_getattr, /*tp_getattr*/ |
| 848 | 0, /*tp_setattr*/ |
| 849 | 0, /*tp_compare*/ |
| 850 | 0, /*tp_repr*/ |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 851 | }; |
| 852 | |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 853 | static PyTypeObject OSSMixerType = { |
| 854 | PyObject_HEAD_INIT(&PyType_Type) |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 855 | 0, /*ob_size*/ |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 856 | "ossaudiodev.oss_mixer_device", /*tp_name*/ |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 857 | sizeof(oss_mixer_t), /*tp_size*/ |
| 858 | 0, /*tp_itemsize*/ |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 859 | /* methods */ |
| 860 | (destructor)oss_mixer_dealloc, /*tp_dealloc*/ |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 861 | 0, /*tp_print*/ |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 862 | (getattrfunc)oss_mixer_getattr, /*tp_getattr*/ |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 863 | 0, /*tp_setattr*/ |
| 864 | 0, /*tp_compare*/ |
| 865 | 0, /*tp_repr*/ |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 866 | }; |
| 867 | |
| 868 | |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 869 | static PyObject * |
Greg Ward | 9a568eb | 2002-11-30 23:20:09 +0000 | [diff] [blame] | 870 | ossopen(PyObject *self, PyObject *args) |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 871 | { |
Greg Ward | 8c6b6a9 | 2002-12-11 14:43:13 +0000 | [diff] [blame] | 872 | return (PyObject *)newossobject(args); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 873 | } |
| 874 | |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 875 | static PyObject * |
| 876 | ossopenmixer(PyObject *self, PyObject *args) |
| 877 | { |
| 878 | return (PyObject *)newossmixerobject(args); |
| 879 | } |
| 880 | |
Greg Ward | 9a568eb | 2002-11-30 23:20:09 +0000 | [diff] [blame] | 881 | static PyMethodDef ossaudiodev_methods[] = { |
| 882 | { "open", ossopen, METH_VARARGS }, |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 883 | { "openmixer", ossopenmixer, METH_VARARGS }, |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 884 | { 0, 0 }, |
| 885 | }; |
| 886 | |
Greg Ward | 1e0f57d | 2002-11-30 23:05:26 +0000 | [diff] [blame] | 887 | |
| 888 | #define _EXPORT_INT(mod, name) \ |
| 889 | if (PyModule_AddIntConstant(mod, #name, (long) (name)) == -1) return; |
| 890 | |
Greg Ward | 744f0fd | 2002-12-31 03:23:59 +0000 | [diff] [blame] | 891 | |
| 892 | static char *control_labels[] = SOUND_DEVICE_LABELS; |
| 893 | static char *control_names[] = SOUND_DEVICE_NAMES; |
| 894 | |
| 895 | |
| 896 | static int |
| 897 | build_namelists (PyObject *module) |
| 898 | { |
| 899 | PyObject *labels; |
| 900 | PyObject *names; |
| 901 | PyObject *s; |
| 902 | int num_controls; |
| 903 | int i; |
| 904 | |
| 905 | num_controls = sizeof(control_labels) / sizeof(control_labels[0]); |
| 906 | assert(num_controls == sizeof(control_names) / sizeof(control_names[0])); |
| 907 | |
| 908 | labels = PyList_New(num_controls); |
| 909 | names = PyList_New(num_controls); |
| 910 | for (i = 0; i < num_controls; i++) { |
| 911 | s = PyString_FromString(control_labels[i]); |
| 912 | if (s == NULL) |
| 913 | return -1; |
| 914 | PyList_SET_ITEM(labels, i, s); |
| 915 | |
| 916 | s = PyString_FromString(control_names[i]); |
| 917 | if (s == NULL) |
| 918 | return -1; |
| 919 | PyList_SET_ITEM(names, i, s); |
| 920 | } |
| 921 | |
| 922 | if (PyModule_AddObject(module, "control_labels", labels) == -1) |
| 923 | return -1; |
| 924 | if (PyModule_AddObject(module, "control_names", names) == -1) |
| 925 | return -1; |
| 926 | |
| 927 | return 0; |
| 928 | } |
| 929 | |
| 930 | |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 931 | void |
Greg Ward | 9a568eb | 2002-11-30 23:20:09 +0000 | [diff] [blame] | 932 | initossaudiodev(void) |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 933 | { |
| 934 | PyObject *m; |
| 935 | |
Greg Ward | 9a568eb | 2002-11-30 23:20:09 +0000 | [diff] [blame] | 936 | m = Py_InitModule("ossaudiodev", ossaudiodev_methods); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 937 | |
Greg Ward | 97708bc | 2002-11-30 23:17:10 +0000 | [diff] [blame] | 938 | OSSAudioError = PyErr_NewException("ossaudiodev.error", NULL, NULL); |
| 939 | if (OSSAudioError) |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 940 | PyModule_AddObject(m, "error", OSSAudioError); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 941 | |
Greg Ward | 744f0fd | 2002-12-31 03:23:59 +0000 | [diff] [blame] | 942 | /* Build 'control_labels' and 'control_names' lists and add them |
| 943 | to the module. */ |
| 944 | if (build_namelists(m) == -1) /* XXX what to do here? */ |
| 945 | return; |
| 946 | |
Greg Ward | 1e0f57d | 2002-11-30 23:05:26 +0000 | [diff] [blame] | 947 | /* Expose the audio format numbers -- essential! */ |
| 948 | _EXPORT_INT(m, AFMT_QUERY); |
| 949 | _EXPORT_INT(m, AFMT_MU_LAW); |
| 950 | _EXPORT_INT(m, AFMT_A_LAW); |
| 951 | _EXPORT_INT(m, AFMT_IMA_ADPCM); |
| 952 | _EXPORT_INT(m, AFMT_U8); |
| 953 | _EXPORT_INT(m, AFMT_S16_LE); |
| 954 | _EXPORT_INT(m, AFMT_S16_BE); |
| 955 | _EXPORT_INT(m, AFMT_S8); |
| 956 | _EXPORT_INT(m, AFMT_U16_LE); |
| 957 | _EXPORT_INT(m, AFMT_U16_BE); |
| 958 | _EXPORT_INT(m, AFMT_MPEG); |
Andrew M. Kuchling | fe62bc9 | 2003-02-13 13:27:07 +0000 | [diff] [blame] | 959 | #ifdef AFMT_AC3 |
Greg Ward | 1e0f57d | 2002-11-30 23:05:26 +0000 | [diff] [blame] | 960 | _EXPORT_INT(m, AFMT_AC3); |
Andrew M. Kuchling | fe62bc9 | 2003-02-13 13:27:07 +0000 | [diff] [blame] | 961 | #endif |
Neal Norwitz | d156c2d | 2003-02-02 17:59:06 +0000 | [diff] [blame] | 962 | #ifdef AFMT_S16_NE |
Greg Ward | 1e0f57d | 2002-11-30 23:05:26 +0000 | [diff] [blame] | 963 | _EXPORT_INT(m, AFMT_S16_NE); |
Neal Norwitz | d156c2d | 2003-02-02 17:59:06 +0000 | [diff] [blame] | 964 | #endif |
Greg Ward | ad4d9b9 | 2002-12-30 03:02:22 +0000 | [diff] [blame] | 965 | |
Greg Ward | 37897c2 | 2002-12-30 02:43:36 +0000 | [diff] [blame] | 966 | /* Expose the sound mixer device numbers. */ |
Greg Ward | 7b43c68 | 2002-12-30 02:29:28 +0000 | [diff] [blame] | 967 | _EXPORT_INT(m, SOUND_MIXER_NRDEVICES); |
Greg Ward | 3d9994d | 2002-12-11 15:12:01 +0000 | [diff] [blame] | 968 | _EXPORT_INT(m, SOUND_MIXER_VOLUME); |
| 969 | _EXPORT_INT(m, SOUND_MIXER_BASS); |
| 970 | _EXPORT_INT(m, SOUND_MIXER_TREBLE); |
| 971 | _EXPORT_INT(m, SOUND_MIXER_SYNTH); |
| 972 | _EXPORT_INT(m, SOUND_MIXER_PCM); |
| 973 | _EXPORT_INT(m, SOUND_MIXER_SPEAKER); |
| 974 | _EXPORT_INT(m, SOUND_MIXER_LINE); |
| 975 | _EXPORT_INT(m, SOUND_MIXER_MIC); |
| 976 | _EXPORT_INT(m, SOUND_MIXER_CD); |
| 977 | _EXPORT_INT(m, SOUND_MIXER_IMIX); |
| 978 | _EXPORT_INT(m, SOUND_MIXER_ALTPCM); |
| 979 | _EXPORT_INT(m, SOUND_MIXER_RECLEV); |
| 980 | _EXPORT_INT(m, SOUND_MIXER_IGAIN); |
| 981 | _EXPORT_INT(m, SOUND_MIXER_OGAIN); |
| 982 | _EXPORT_INT(m, SOUND_MIXER_LINE1); |
| 983 | _EXPORT_INT(m, SOUND_MIXER_LINE2); |
| 984 | _EXPORT_INT(m, SOUND_MIXER_LINE3); |
| 985 | _EXPORT_INT(m, SOUND_MIXER_DIGITAL1); |
| 986 | _EXPORT_INT(m, SOUND_MIXER_DIGITAL2); |
| 987 | _EXPORT_INT(m, SOUND_MIXER_DIGITAL3); |
| 988 | _EXPORT_INT(m, SOUND_MIXER_PHONEIN); |
| 989 | _EXPORT_INT(m, SOUND_MIXER_PHONEOUT); |
| 990 | _EXPORT_INT(m, SOUND_MIXER_VIDEO); |
| 991 | _EXPORT_INT(m, SOUND_MIXER_RADIO); |
| 992 | _EXPORT_INT(m, SOUND_MIXER_MONITOR); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 993 | |
Greg Ward | 1e0f57d | 2002-11-30 23:05:26 +0000 | [diff] [blame] | 994 | /* Expose all the ioctl numbers for masochists who like to do this |
| 995 | stuff directly. */ |
| 996 | _EXPORT_INT(m, SNDCTL_COPR_HALT); |
| 997 | _EXPORT_INT(m, SNDCTL_COPR_LOAD); |
| 998 | _EXPORT_INT(m, SNDCTL_COPR_RCODE); |
| 999 | _EXPORT_INT(m, SNDCTL_COPR_RCVMSG); |
| 1000 | _EXPORT_INT(m, SNDCTL_COPR_RDATA); |
| 1001 | _EXPORT_INT(m, SNDCTL_COPR_RESET); |
| 1002 | _EXPORT_INT(m, SNDCTL_COPR_RUN); |
| 1003 | _EXPORT_INT(m, SNDCTL_COPR_SENDMSG); |
| 1004 | _EXPORT_INT(m, SNDCTL_COPR_WCODE); |
| 1005 | _EXPORT_INT(m, SNDCTL_COPR_WDATA); |
Neal Norwitz | d156c2d | 2003-02-02 17:59:06 +0000 | [diff] [blame] | 1006 | #ifdef SNDCTL_DSP_BIND_CHANNEL |
Greg Ward | 1e0f57d | 2002-11-30 23:05:26 +0000 | [diff] [blame] | 1007 | _EXPORT_INT(m, SNDCTL_DSP_BIND_CHANNEL); |
Neal Norwitz | d156c2d | 2003-02-02 17:59:06 +0000 | [diff] [blame] | 1008 | #endif |
Greg Ward | 1e0f57d | 2002-11-30 23:05:26 +0000 | [diff] [blame] | 1009 | _EXPORT_INT(m, SNDCTL_DSP_CHANNELS); |
| 1010 | _EXPORT_INT(m, SNDCTL_DSP_GETBLKSIZE); |
| 1011 | _EXPORT_INT(m, SNDCTL_DSP_GETCAPS); |
Neal Norwitz | d156c2d | 2003-02-02 17:59:06 +0000 | [diff] [blame] | 1012 | #ifdef SNDCTL_DSP_GETCHANNELMASK |
Greg Ward | 1e0f57d | 2002-11-30 23:05:26 +0000 | [diff] [blame] | 1013 | _EXPORT_INT(m, SNDCTL_DSP_GETCHANNELMASK); |
Neal Norwitz | d156c2d | 2003-02-02 17:59:06 +0000 | [diff] [blame] | 1014 | #endif |
Greg Ward | 1e0f57d | 2002-11-30 23:05:26 +0000 | [diff] [blame] | 1015 | _EXPORT_INT(m, SNDCTL_DSP_GETFMTS); |
| 1016 | _EXPORT_INT(m, SNDCTL_DSP_GETIPTR); |
| 1017 | _EXPORT_INT(m, SNDCTL_DSP_GETISPACE); |
| 1018 | _EXPORT_INT(m, SNDCTL_DSP_GETODELAY); |
| 1019 | _EXPORT_INT(m, SNDCTL_DSP_GETOPTR); |
| 1020 | _EXPORT_INT(m, SNDCTL_DSP_GETOSPACE); |
Neal Norwitz | faa7b9b | 2003-01-10 21:27:54 +0000 | [diff] [blame] | 1021 | #ifdef SNDCTL_DSP_GETSPDIF |
Greg Ward | 1e0f57d | 2002-11-30 23:05:26 +0000 | [diff] [blame] | 1022 | _EXPORT_INT(m, SNDCTL_DSP_GETSPDIF); |
Neal Norwitz | faa7b9b | 2003-01-10 21:27:54 +0000 | [diff] [blame] | 1023 | #endif |
Greg Ward | 1e0f57d | 2002-11-30 23:05:26 +0000 | [diff] [blame] | 1024 | _EXPORT_INT(m, SNDCTL_DSP_GETTRIGGER); |
| 1025 | _EXPORT_INT(m, SNDCTL_DSP_MAPINBUF); |
| 1026 | _EXPORT_INT(m, SNDCTL_DSP_MAPOUTBUF); |
| 1027 | _EXPORT_INT(m, SNDCTL_DSP_NONBLOCK); |
| 1028 | _EXPORT_INT(m, SNDCTL_DSP_POST); |
Neal Norwitz | d156c2d | 2003-02-02 17:59:06 +0000 | [diff] [blame] | 1029 | #ifdef SNDCTL_DSP_PROFILE |
Greg Ward | 1e0f57d | 2002-11-30 23:05:26 +0000 | [diff] [blame] | 1030 | _EXPORT_INT(m, SNDCTL_DSP_PROFILE); |
Neal Norwitz | d156c2d | 2003-02-02 17:59:06 +0000 | [diff] [blame] | 1031 | #endif |
Greg Ward | 1e0f57d | 2002-11-30 23:05:26 +0000 | [diff] [blame] | 1032 | _EXPORT_INT(m, SNDCTL_DSP_RESET); |
| 1033 | _EXPORT_INT(m, SNDCTL_DSP_SAMPLESIZE); |
| 1034 | _EXPORT_INT(m, SNDCTL_DSP_SETDUPLEX); |
| 1035 | _EXPORT_INT(m, SNDCTL_DSP_SETFMT); |
| 1036 | _EXPORT_INT(m, SNDCTL_DSP_SETFRAGMENT); |
Neal Norwitz | faa7b9b | 2003-01-10 21:27:54 +0000 | [diff] [blame] | 1037 | #ifdef SNDCTL_DSP_SETSPDIF |
Greg Ward | 1e0f57d | 2002-11-30 23:05:26 +0000 | [diff] [blame] | 1038 | _EXPORT_INT(m, SNDCTL_DSP_SETSPDIF); |
Neal Norwitz | faa7b9b | 2003-01-10 21:27:54 +0000 | [diff] [blame] | 1039 | #endif |
Greg Ward | 1e0f57d | 2002-11-30 23:05:26 +0000 | [diff] [blame] | 1040 | _EXPORT_INT(m, SNDCTL_DSP_SETSYNCRO); |
| 1041 | _EXPORT_INT(m, SNDCTL_DSP_SETTRIGGER); |
| 1042 | _EXPORT_INT(m, SNDCTL_DSP_SPEED); |
| 1043 | _EXPORT_INT(m, SNDCTL_DSP_STEREO); |
| 1044 | _EXPORT_INT(m, SNDCTL_DSP_SUBDIVIDE); |
| 1045 | _EXPORT_INT(m, SNDCTL_DSP_SYNC); |
| 1046 | _EXPORT_INT(m, SNDCTL_FM_4OP_ENABLE); |
| 1047 | _EXPORT_INT(m, SNDCTL_FM_LOAD_INSTR); |
| 1048 | _EXPORT_INT(m, SNDCTL_MIDI_INFO); |
| 1049 | _EXPORT_INT(m, SNDCTL_MIDI_MPUCMD); |
| 1050 | _EXPORT_INT(m, SNDCTL_MIDI_MPUMODE); |
| 1051 | _EXPORT_INT(m, SNDCTL_MIDI_PRETIME); |
| 1052 | _EXPORT_INT(m, SNDCTL_SEQ_CTRLRATE); |
| 1053 | _EXPORT_INT(m, SNDCTL_SEQ_GETINCOUNT); |
| 1054 | _EXPORT_INT(m, SNDCTL_SEQ_GETOUTCOUNT); |
Neal Norwitz | d156c2d | 2003-02-02 17:59:06 +0000 | [diff] [blame] | 1055 | #ifdef SNDCTL_SEQ_GETTIME |
Greg Ward | 1e0f57d | 2002-11-30 23:05:26 +0000 | [diff] [blame] | 1056 | _EXPORT_INT(m, SNDCTL_SEQ_GETTIME); |
Neal Norwitz | d156c2d | 2003-02-02 17:59:06 +0000 | [diff] [blame] | 1057 | #endif |
Greg Ward | 1e0f57d | 2002-11-30 23:05:26 +0000 | [diff] [blame] | 1058 | _EXPORT_INT(m, SNDCTL_SEQ_NRMIDIS); |
| 1059 | _EXPORT_INT(m, SNDCTL_SEQ_NRSYNTHS); |
| 1060 | _EXPORT_INT(m, SNDCTL_SEQ_OUTOFBAND); |
| 1061 | _EXPORT_INT(m, SNDCTL_SEQ_PANIC); |
| 1062 | _EXPORT_INT(m, SNDCTL_SEQ_PERCMODE); |
| 1063 | _EXPORT_INT(m, SNDCTL_SEQ_RESET); |
| 1064 | _EXPORT_INT(m, SNDCTL_SEQ_RESETSAMPLES); |
| 1065 | _EXPORT_INT(m, SNDCTL_SEQ_SYNC); |
| 1066 | _EXPORT_INT(m, SNDCTL_SEQ_TESTMIDI); |
| 1067 | _EXPORT_INT(m, SNDCTL_SEQ_THRESHOLD); |
Neal Norwitz | d156c2d | 2003-02-02 17:59:06 +0000 | [diff] [blame] | 1068 | #ifdef SNDCTL_SYNTH_CONTROL |
Greg Ward | 1e0f57d | 2002-11-30 23:05:26 +0000 | [diff] [blame] | 1069 | _EXPORT_INT(m, SNDCTL_SYNTH_CONTROL); |
Neal Norwitz | d156c2d | 2003-02-02 17:59:06 +0000 | [diff] [blame] | 1070 | #endif |
| 1071 | #ifdef SNDCTL_SYNTH_ID |
Greg Ward | 1e0f57d | 2002-11-30 23:05:26 +0000 | [diff] [blame] | 1072 | _EXPORT_INT(m, SNDCTL_SYNTH_ID); |
Neal Norwitz | d156c2d | 2003-02-02 17:59:06 +0000 | [diff] [blame] | 1073 | #endif |
Greg Ward | 1e0f57d | 2002-11-30 23:05:26 +0000 | [diff] [blame] | 1074 | _EXPORT_INT(m, SNDCTL_SYNTH_INFO); |
| 1075 | _EXPORT_INT(m, SNDCTL_SYNTH_MEMAVL); |
Neal Norwitz | d156c2d | 2003-02-02 17:59:06 +0000 | [diff] [blame] | 1076 | #ifdef SNDCTL_SYNTH_REMOVESAMPLE |
Greg Ward | 1e0f57d | 2002-11-30 23:05:26 +0000 | [diff] [blame] | 1077 | _EXPORT_INT(m, SNDCTL_SYNTH_REMOVESAMPLE); |
Neal Norwitz | d156c2d | 2003-02-02 17:59:06 +0000 | [diff] [blame] | 1078 | #endif |
Greg Ward | 1e0f57d | 2002-11-30 23:05:26 +0000 | [diff] [blame] | 1079 | _EXPORT_INT(m, SNDCTL_TMR_CONTINUE); |
| 1080 | _EXPORT_INT(m, SNDCTL_TMR_METRONOME); |
| 1081 | _EXPORT_INT(m, SNDCTL_TMR_SELECT); |
| 1082 | _EXPORT_INT(m, SNDCTL_TMR_SOURCE); |
| 1083 | _EXPORT_INT(m, SNDCTL_TMR_START); |
| 1084 | _EXPORT_INT(m, SNDCTL_TMR_STOP); |
| 1085 | _EXPORT_INT(m, SNDCTL_TMR_TEMPO); |
| 1086 | _EXPORT_INT(m, SNDCTL_TMR_TIMEBASE); |
Greg Ward | 04613a9 | 2002-11-30 22:47:45 +0000 | [diff] [blame] | 1087 | } |