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