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