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