Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 2 | Copyright (c) 2000, BeOpen.com. |
| 3 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 4 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 5 | All rights reserved. |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 7 | See the file "Misc/COPYRIGHT" for information on usage and |
| 8 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 9 | ******************************************************************/ |
| 10 | |
| 11 | /* Sad objects */ |
| 12 | |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 13 | #include "Python.h" |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 14 | #include "structmember.h" |
| 15 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 16 | #ifdef HAVE_SYS_AUDIOIO_H |
| 17 | #define SOLARIS |
| 18 | #endif |
| 19 | |
Guido van Rossum | e29ed8f | 1996-12-10 15:21:30 +0000 | [diff] [blame] | 20 | #ifdef HAVE_UNISTD_H |
| 21 | #include <unistd.h> |
| 22 | #endif |
| 23 | |
| 24 | #ifdef HAVE_FCNTL_H |
| 25 | #include <fcntl.h> |
| 26 | #endif |
| 27 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 28 | #include <stropts.h> |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 29 | #include <sys/ioctl.h> |
Sjoerd Mullender | 66bca32 | 1993-12-03 16:54:45 +0000 | [diff] [blame] | 30 | #ifdef SOLARIS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 31 | #include <sys/audioio.h> |
Sjoerd Mullender | 66bca32 | 1993-12-03 16:54:45 +0000 | [diff] [blame] | 32 | #else |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 33 | #include <sun/audioio.h> |
Sjoerd Mullender | 66bca32 | 1993-12-03 16:54:45 +0000 | [diff] [blame] | 34 | #endif |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 35 | |
| 36 | /* #define offsetof(str,mem) ((int)(((str *)0)->mem)) */ |
| 37 | |
| 38 | typedef struct { |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 39 | PyObject_HEAD |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 40 | int x_fd; /* The open file */ |
| 41 | int x_icount; /* # samples read */ |
| 42 | int x_ocount; /* # samples written */ |
| 43 | int x_isctl; /* True if control device */ |
| 44 | |
| 45 | } sadobject; |
| 46 | |
| 47 | typedef struct { |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 48 | PyObject_HEAD |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 49 | audio_info_t ai; |
| 50 | } sadstatusobject; |
| 51 | |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 52 | staticforward PyTypeObject Sadtype; |
| 53 | staticforward PyTypeObject Sadstatustype; |
Sjoerd Mullender | 66bca32 | 1993-12-03 16:54:45 +0000 | [diff] [blame] | 54 | static sadstatusobject *sads_alloc(); /* Forward */ |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 55 | |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 56 | static PyObject *SunAudioError; |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 57 | |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 58 | #define is_sadobject(v) ((v)->ob_type == &Sadtype) |
| 59 | #define is_sadstatusobject(v) ((v)->ob_type == &Sadstatustype) |
| 60 | |
Barry Warsaw | b0d1b06 | 1998-10-31 22:52:54 +0000 | [diff] [blame] | 61 | |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 62 | static sadobject * |
Peter Schneider-Kamp | 3e3175b | 2000-07-10 17:25:37 +0000 | [diff] [blame] | 63 | newsadobject(PyObject *args) |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 64 | { |
| 65 | sadobject *xp; |
| 66 | int fd; |
| 67 | char *mode; |
| 68 | int imode; |
Barry Warsaw | b0d1b06 | 1998-10-31 22:52:54 +0000 | [diff] [blame] | 69 | char* basedev; |
| 70 | char* ctldev; |
| 71 | char* opendev; |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 72 | |
| 73 | /* Check arg for r/w/rw */ |
Peter Schneider-Kamp | 3e3175b | 2000-07-10 17:25:37 +0000 | [diff] [blame] | 74 | if (!PyArg_Parse(args, "s", &mode)) |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 75 | return NULL; |
| 76 | if (strcmp(mode, "r") == 0) |
| 77 | imode = 0; |
| 78 | else if (strcmp(mode, "w") == 0) |
| 79 | imode = 1; |
| 80 | else if (strcmp(mode, "rw") == 0) |
| 81 | imode = 2; |
| 82 | else if (strcmp(mode, "control") == 0) |
| 83 | imode = -1; |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 84 | else { |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 85 | PyErr_SetString(SunAudioError, |
| 86 | "Mode should be one of 'r', 'w', 'rw' or 'control'"); |
| 87 | return NULL; |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Barry Warsaw | b0d1b06 | 1998-10-31 22:52:54 +0000 | [diff] [blame] | 90 | /* Open the correct device. The base device name comes from the |
| 91 | * AUDIODEV environment variable first, then /dev/audio. The |
| 92 | * control device tacks "ctl" onto the base device name. |
| 93 | */ |
| 94 | basedev = getenv("AUDIODEV"); |
| 95 | if (!basedev) |
| 96 | basedev = "/dev/audio"; |
| 97 | ctldev = PyMem_NEW(char, strlen(basedev) + 4); |
| 98 | if (!ctldev) { |
| 99 | PyErr_NoMemory(); |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 100 | return NULL; |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 101 | } |
Barry Warsaw | b0d1b06 | 1998-10-31 22:52:54 +0000 | [diff] [blame] | 102 | strcpy(ctldev, basedev); |
| 103 | strcat(ctldev, "ctl"); |
| 104 | |
| 105 | if (imode < 0) { |
| 106 | opendev = ctldev; |
| 107 | fd = open(ctldev, 2); |
| 108 | } |
| 109 | else { |
| 110 | opendev = basedev; |
| 111 | fd = open(basedev, imode); |
| 112 | } |
| 113 | if (fd < 0) { |
| 114 | PyErr_SetFromErrnoWithFilename(SunAudioError, opendev); |
| 115 | return NULL; |
| 116 | } |
| 117 | PyMem_DEL(ctldev); |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 118 | |
| 119 | /* Create and initialize the object */ |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 120 | xp = PyObject_New(sadobject, &Sadtype); |
Barry Warsaw | 42185a2 | 1997-01-09 22:29:57 +0000 | [diff] [blame] | 121 | if (xp == NULL) { |
| 122 | close(fd); |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 123 | return NULL; |
Barry Warsaw | 42185a2 | 1997-01-09 22:29:57 +0000 | [diff] [blame] | 124 | } |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 125 | xp->x_fd = fd; |
| 126 | xp->x_icount = xp->x_ocount = 0; |
| 127 | xp->x_isctl = (imode < 0); |
| 128 | |
| 129 | return xp; |
| 130 | } |
| 131 | |
| 132 | /* Sad methods */ |
| 133 | |
| 134 | static void |
Peter Schneider-Kamp | 3e3175b | 2000-07-10 17:25:37 +0000 | [diff] [blame] | 135 | sad_dealloc(sadobject *xp) |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 136 | { |
| 137 | close(xp->x_fd); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 138 | PyObject_Del(xp); |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 141 | static PyObject * |
Peter Schneider-Kamp | 3e3175b | 2000-07-10 17:25:37 +0000 | [diff] [blame] | 142 | sad_read(sadobject *self, PyObject *args) |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 143 | { |
| 144 | int size, count; |
| 145 | char *cp; |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 146 | PyObject *rv; |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 147 | |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 148 | if (!PyArg_Parse(args, "i", &size)) |
| 149 | return NULL; |
| 150 | rv = PyString_FromStringAndSize(NULL, size); |
| 151 | if (rv == NULL) |
| 152 | return NULL; |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 153 | |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 154 | if (!(cp = PyString_AsString(rv))) |
| 155 | goto finally; |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 156 | |
| 157 | count = read(self->x_fd, cp, size); |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 158 | if (count < 0) { |
| 159 | PyErr_SetFromErrno(SunAudioError); |
| 160 | goto finally; |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 161 | } |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 162 | #if 0 |
| 163 | /* TBD: why print this message if you can handle the condition? |
Barry Warsaw | 42185a2 | 1997-01-09 22:29:57 +0000 | [diff] [blame] | 164 | * assume it's debugging info which we can just as well get rid |
| 165 | * of. in any case this message should *not* be using printf! |
| 166 | */ |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 167 | if (count != size) |
| 168 | printf("sunaudio: funny read rv %d wtd %d\n", count, size); |
| 169 | #endif |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 170 | self->x_icount += count; |
| 171 | return rv; |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 172 | |
| 173 | finally: |
| 174 | Py_DECREF(rv); |
| 175 | return NULL; |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 178 | static PyObject * |
Peter Schneider-Kamp | 3e3175b | 2000-07-10 17:25:37 +0000 | [diff] [blame] | 179 | sad_write(sadobject *self, PyObject *args) |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 180 | { |
| 181 | char *cp; |
| 182 | int count, size; |
| 183 | |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 184 | if (!PyArg_Parse(args, "s#", &cp, &size)) |
| 185 | return NULL; |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 186 | |
| 187 | count = write(self->x_fd, cp, size); |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 188 | if (count < 0) { |
| 189 | PyErr_SetFromErrno(SunAudioError); |
| 190 | return NULL; |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 191 | } |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 192 | #if 0 |
| 193 | if (count != size) |
| 194 | printf("sunaudio: funny write rv %d wanted %d\n", count, size); |
| 195 | #endif |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 196 | self->x_ocount += count; |
| 197 | |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 198 | Py_INCREF(Py_None); |
| 199 | return Py_None; |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 202 | static PyObject * |
Peter Schneider-Kamp | 3e3175b | 2000-07-10 17:25:37 +0000 | [diff] [blame] | 203 | sad_getinfo(sadobject *self, PyObject *args) |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 204 | { |
| 205 | sadstatusobject *rv; |
| 206 | |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 207 | if (!PyArg_Parse(args, "")) |
| 208 | return NULL; |
Barry Warsaw | 42185a2 | 1997-01-09 22:29:57 +0000 | [diff] [blame] | 209 | if (!(rv = sads_alloc())) |
| 210 | return NULL; |
| 211 | |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 212 | if (ioctl(self->x_fd, AUDIO_GETINFO, &rv->ai) < 0) { |
| 213 | PyErr_SetFromErrno(SunAudioError); |
| 214 | Py_DECREF(rv); |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 215 | return NULL; |
| 216 | } |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 217 | return (PyObject *)rv; |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 220 | static PyObject * |
Peter Schneider-Kamp | 3e3175b | 2000-07-10 17:25:37 +0000 | [diff] [blame] | 221 | sad_setinfo(sadobject *self, sadstatusobject *arg) |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 222 | { |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 223 | if (!is_sadstatusobject(arg)) { |
| 224 | PyErr_SetString(PyExc_TypeError, |
| 225 | "Must be sun audio status object"); |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 226 | return NULL; |
| 227 | } |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 228 | if (ioctl(self->x_fd, AUDIO_SETINFO, &arg->ai) < 0) { |
| 229 | PyErr_SetFromErrno(SunAudioError); |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 230 | return NULL; |
| 231 | } |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 232 | Py_INCREF(Py_None); |
| 233 | return Py_None; |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 236 | static PyObject * |
Peter Schneider-Kamp | 3e3175b | 2000-07-10 17:25:37 +0000 | [diff] [blame] | 237 | sad_ibufcount(sadobject *self, PyObject *args) |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 238 | { |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 239 | audio_info_t ai; |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 240 | |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 241 | if (!PyArg_Parse(args, "")) |
| 242 | return NULL; |
| 243 | if (ioctl(self->x_fd, AUDIO_GETINFO, &ai) < 0) { |
| 244 | PyErr_SetFromErrno(SunAudioError); |
| 245 | return NULL; |
| 246 | } |
| 247 | return PyInt_FromLong(ai.record.samples - self->x_icount); |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 250 | static PyObject * |
Peter Schneider-Kamp | 3e3175b | 2000-07-10 17:25:37 +0000 | [diff] [blame] | 251 | sad_obufcount(sadobject *self, PyObject *args) |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 252 | { |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 253 | audio_info_t ai; |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 254 | |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 255 | if (!PyArg_Parse(args, "")) |
| 256 | return NULL; |
| 257 | if (ioctl(self->x_fd, AUDIO_GETINFO, &ai) < 0) { |
| 258 | PyErr_SetFromErrno(SunAudioError); |
| 259 | return NULL; |
| 260 | } |
Guido van Rossum | 1e0b19e | 1997-04-11 15:23:52 +0000 | [diff] [blame] | 261 | /* x_ocount is in bytes, wheras play.samples is in frames */ |
| 262 | /* we want frames */ |
| 263 | return PyInt_FromLong(self->x_ocount / (ai.play.channels * |
| 264 | ai.play.precision / 8) - |
| 265 | ai.play.samples); |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 268 | static PyObject * |
Peter Schneider-Kamp | 3e3175b | 2000-07-10 17:25:37 +0000 | [diff] [blame] | 269 | sad_drain(sadobject *self, PyObject *args) |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 270 | { |
| 271 | |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 272 | if (!PyArg_Parse(args, "")) |
| 273 | return NULL; |
| 274 | if (ioctl(self->x_fd, AUDIO_DRAIN, 0) < 0) { |
| 275 | PyErr_SetFromErrno(SunAudioError); |
| 276 | return NULL; |
| 277 | } |
| 278 | Py_INCREF(Py_None); |
| 279 | return Py_None; |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Sjoerd Mullender | 66bca32 | 1993-12-03 16:54:45 +0000 | [diff] [blame] | 282 | #ifdef SOLARIS |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 283 | static PyObject * |
Peter Schneider-Kamp | 3e3175b | 2000-07-10 17:25:37 +0000 | [diff] [blame] | 284 | sad_getdev(sadobject *self, PyObject *args) |
Sjoerd Mullender | 66bca32 | 1993-12-03 16:54:45 +0000 | [diff] [blame] | 285 | { |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 286 | struct audio_device ad; |
Sjoerd Mullender | 66bca32 | 1993-12-03 16:54:45 +0000 | [diff] [blame] | 287 | |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 288 | if (!PyArg_Parse(args, "")) |
| 289 | return NULL; |
| 290 | if (ioctl(self->x_fd, AUDIO_GETDEV, &ad) < 0) { |
| 291 | PyErr_SetFromErrno(SunAudioError); |
| 292 | return NULL; |
| 293 | } |
| 294 | return Py_BuildValue("(sss)", ad.name, ad.version, ad.config); |
Sjoerd Mullender | 66bca32 | 1993-12-03 16:54:45 +0000 | [diff] [blame] | 295 | } |
| 296 | #endif |
| 297 | |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 298 | static PyObject * |
Peter Schneider-Kamp | 3e3175b | 2000-07-10 17:25:37 +0000 | [diff] [blame] | 299 | sad_flush(sadobject *self, PyObject *args) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 300 | { |
| 301 | |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 302 | if (!PyArg_Parse(args, "")) |
| 303 | return NULL; |
| 304 | if (ioctl(self->x_fd, I_FLUSH, FLUSHW) < 0) { |
| 305 | PyErr_SetFromErrno(SunAudioError); |
| 306 | return NULL; |
| 307 | } |
| 308 | Py_INCREF(Py_None); |
| 309 | return Py_None; |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 312 | static PyObject * |
Peter Schneider-Kamp | 3e3175b | 2000-07-10 17:25:37 +0000 | [diff] [blame] | 313 | sad_close(sadobject *self, PyObject *args) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 314 | { |
| 315 | |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 316 | if (!PyArg_Parse(args, "")) |
| 317 | return NULL; |
| 318 | if (self->x_fd >= 0) { |
| 319 | close(self->x_fd); |
| 320 | self->x_fd = -1; |
| 321 | } |
| 322 | Py_INCREF(Py_None); |
| 323 | return Py_None; |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Barry Warsaw | b0d1b06 | 1998-10-31 22:52:54 +0000 | [diff] [blame] | 326 | static PyObject * |
Peter Schneider-Kamp | 3e3175b | 2000-07-10 17:25:37 +0000 | [diff] [blame] | 327 | sad_fileno(sadobject *self, PyObject *args) |
Barry Warsaw | b0d1b06 | 1998-10-31 22:52:54 +0000 | [diff] [blame] | 328 | { |
| 329 | if (!PyArg_Parse(args, "")) |
| 330 | return NULL; |
| 331 | |
| 332 | return PyInt_FromLong(self->x_fd); |
| 333 | } |
| 334 | |
| 335 | |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 336 | static PyMethodDef sad_methods[] = { |
| 337 | { "read", (PyCFunction)sad_read }, |
| 338 | { "write", (PyCFunction)sad_write }, |
| 339 | { "ibufcount", (PyCFunction)sad_ibufcount }, |
| 340 | { "obufcount", (PyCFunction)sad_obufcount }, |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 341 | #define CTL_METHODS 4 |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 342 | { "getinfo", (PyCFunction)sad_getinfo }, |
| 343 | { "setinfo", (PyCFunction)sad_setinfo }, |
| 344 | { "drain", (PyCFunction)sad_drain }, |
| 345 | { "flush", (PyCFunction)sad_flush }, |
Sjoerd Mullender | 66bca32 | 1993-12-03 16:54:45 +0000 | [diff] [blame] | 346 | #ifdef SOLARIS |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 347 | { "getdev", (PyCFunction)sad_getdev }, |
Sjoerd Mullender | 66bca32 | 1993-12-03 16:54:45 +0000 | [diff] [blame] | 348 | #endif |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 349 | { "close", (PyCFunction)sad_close }, |
Barry Warsaw | b0d1b06 | 1998-10-31 22:52:54 +0000 | [diff] [blame] | 350 | { "fileno", (PyCFunction)sad_fileno }, |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 351 | {NULL, NULL} /* sentinel */ |
| 352 | }; |
| 353 | |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 354 | static PyObject * |
Peter Schneider-Kamp | 3e3175b | 2000-07-10 17:25:37 +0000 | [diff] [blame] | 355 | sad_getattr(sadobject *xp, char *name) |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 356 | { |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 357 | if (xp->x_isctl) |
| 358 | return Py_FindMethod(sad_methods+CTL_METHODS, |
| 359 | (PyObject *)xp, name); |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 360 | else |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 361 | return Py_FindMethod(sad_methods, (PyObject *)xp, name); |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | /* ----------------------------------------------------------------- */ |
| 365 | |
| 366 | static sadstatusobject * |
| 367 | sads_alloc() { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 368 | return PyObject_New(sadstatusobject, &Sadstatustype); |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | static void |
Peter Schneider-Kamp | 3e3175b | 2000-07-10 17:25:37 +0000 | [diff] [blame] | 372 | sads_dealloc(sadstatusobject *xp) |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 373 | { |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 374 | PyMem_DEL(xp); |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | #define OFF(x) offsetof(audio_info_t,x) |
| 378 | static struct memberlist sads_ml[] = { |
| 379 | { "i_sample_rate", T_UINT, OFF(record.sample_rate) }, |
| 380 | { "i_channels", T_UINT, OFF(record.channels) }, |
| 381 | { "i_precision", T_UINT, OFF(record.precision) }, |
| 382 | { "i_encoding", T_UINT, OFF(record.encoding) }, |
| 383 | { "i_gain", T_UINT, OFF(record.gain) }, |
| 384 | { "i_port", T_UINT, OFF(record.port) }, |
| 385 | { "i_samples", T_UINT, OFF(record.samples) }, |
| 386 | { "i_eof", T_UINT, OFF(record.eof) }, |
| 387 | { "i_pause", T_UBYTE, OFF(record.pause) }, |
| 388 | { "i_error", T_UBYTE, OFF(record.error) }, |
| 389 | { "i_waiting", T_UBYTE, OFF(record.waiting) }, |
| 390 | { "i_open", T_UBYTE, OFF(record.open) , RO}, |
| 391 | { "i_active", T_UBYTE, OFF(record.active) , RO}, |
Sjoerd Mullender | 66bca32 | 1993-12-03 16:54:45 +0000 | [diff] [blame] | 392 | #ifdef SOLARIS |
| 393 | { "i_buffer_size", T_UINT, OFF(record.buffer_size) }, |
| 394 | { "i_balance", T_UBYTE, OFF(record.balance) }, |
| 395 | { "i_avail_ports", T_UINT, OFF(record.avail_ports) }, |
| 396 | #endif |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 397 | |
| 398 | { "o_sample_rate", T_UINT, OFF(play.sample_rate) }, |
| 399 | { "o_channels", T_UINT, OFF(play.channels) }, |
| 400 | { "o_precision", T_UINT, OFF(play.precision) }, |
| 401 | { "o_encoding", T_UINT, OFF(play.encoding) }, |
| 402 | { "o_gain", T_UINT, OFF(play.gain) }, |
| 403 | { "o_port", T_UINT, OFF(play.port) }, |
| 404 | { "o_samples", T_UINT, OFF(play.samples) }, |
| 405 | { "o_eof", T_UINT, OFF(play.eof) }, |
| 406 | { "o_pause", T_UBYTE, OFF(play.pause) }, |
| 407 | { "o_error", T_UBYTE, OFF(play.error) }, |
| 408 | { "o_waiting", T_UBYTE, OFF(play.waiting) }, |
| 409 | { "o_open", T_UBYTE, OFF(play.open) , RO}, |
| 410 | { "o_active", T_UBYTE, OFF(play.active) , RO}, |
Sjoerd Mullender | 66bca32 | 1993-12-03 16:54:45 +0000 | [diff] [blame] | 411 | #ifdef SOLARIS |
| 412 | { "o_buffer_size", T_UINT, OFF(play.buffer_size) }, |
| 413 | { "o_balance", T_UBYTE, OFF(play.balance) }, |
| 414 | { "o_avail_ports", T_UINT, OFF(play.avail_ports) }, |
| 415 | #endif |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 416 | |
| 417 | { "monitor_gain", T_UINT, OFF(monitor_gain) }, |
| 418 | { NULL, 0, 0}, |
| 419 | }; |
| 420 | |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 421 | static PyObject * |
Peter Schneider-Kamp | 3e3175b | 2000-07-10 17:25:37 +0000 | [diff] [blame] | 422 | sads_getattr(sadstatusobject *xp, char *name) |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 423 | { |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 424 | return PyMember_Get((char *)&xp->ai, sads_ml, name); |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | static int |
Peter Schneider-Kamp | 3e3175b | 2000-07-10 17:25:37 +0000 | [diff] [blame] | 428 | sads_setattr(sadstatusobject *xp, char *name, PyObject *v) |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 429 | { |
Guido van Rossum | 94472a0 | 1992-09-04 09:45:18 +0000 | [diff] [blame] | 430 | |
| 431 | if (v == NULL) { |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 432 | PyErr_SetString(PyExc_TypeError, |
| 433 | "can't delete sun audio status attributes"); |
Guido van Rossum | f156f50 | 1996-09-11 23:31:23 +0000 | [diff] [blame] | 434 | return -1; |
Guido van Rossum | 94472a0 | 1992-09-04 09:45:18 +0000 | [diff] [blame] | 435 | } |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 436 | return PyMember_Set((char *)&xp->ai, sads_ml, name, v); |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | /* ------------------------------------------------------------------- */ |
| 440 | |
| 441 | |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 442 | static PyTypeObject Sadtype = { |
| 443 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 444 | 0, /*ob_size*/ |
| 445 | "sun_audio_device", /*tp_name*/ |
| 446 | sizeof(sadobject), /*tp_size*/ |
| 447 | 0, /*tp_itemsize*/ |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 448 | /* methods */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 449 | (destructor)sad_dealloc, /*tp_dealloc*/ |
| 450 | 0, /*tp_print*/ |
| 451 | (getattrfunc)sad_getattr, /*tp_getattr*/ |
| 452 | 0, /*tp_setattr*/ |
| 453 | 0, /*tp_compare*/ |
| 454 | 0, /*tp_repr*/ |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 455 | }; |
| 456 | |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 457 | static PyTypeObject Sadstatustype = { |
| 458 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 459 | 0, /*ob_size*/ |
| 460 | "sun_audio_device_status", /*tp_name*/ |
| 461 | sizeof(sadstatusobject), /*tp_size*/ |
| 462 | 0, /*tp_itemsize*/ |
| 463 | /* methods */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 464 | (destructor)sads_dealloc, /*tp_dealloc*/ |
| 465 | 0, /*tp_print*/ |
| 466 | (getattrfunc)sads_getattr, /*tp_getattr*/ |
| 467 | (setattrfunc)sads_setattr, /*tp_setattr*/ |
| 468 | 0, /*tp_compare*/ |
| 469 | 0, /*tp_repr*/ |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 470 | }; |
| 471 | /* ------------------------------------------------------------------- */ |
| 472 | |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 473 | static PyObject * |
Peter Schneider-Kamp | 3e3175b | 2000-07-10 17:25:37 +0000 | [diff] [blame] | 474 | sadopen(PyObject *self, PyObject *args) |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 475 | { |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 476 | return (PyObject *)newsadobject(args); |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 479 | static PyMethodDef sunaudiodev_methods[] = { |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 480 | { "open", sadopen }, |
| 481 | { 0, 0 }, |
| 482 | }; |
| 483 | |
| 484 | void |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 485 | initsunaudiodev() |
| 486 | { |
| 487 | PyObject *m, *d; |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 488 | |
Barry Warsaw | 8496d39 | 1996-12-13 00:11:04 +0000 | [diff] [blame] | 489 | m = Py_InitModule("sunaudiodev", sunaudiodev_methods); |
| 490 | d = PyModule_GetDict(m); |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 491 | SunAudioError = PyErr_NewException("sunaudiodev.error", NULL, NULL); |
Barry Warsaw | 42185a2 | 1997-01-09 22:29:57 +0000 | [diff] [blame] | 492 | if (SunAudioError) |
| 493 | PyDict_SetItemString(d, "error", SunAudioError); |
Guido van Rossum | 9c52e0f | 1992-06-03 17:06:14 +0000 | [diff] [blame] | 494 | } |