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