Changeset 388 for python/vendor/current/Modules/linuxaudiodev.c
- Timestamp:
- Mar 19, 2014, 11:11:30 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/vendor/current/Modules/linuxaudiodev.c
r2 r388 1 /* Hey Emacs, this is -*-C-*- 1 /* Hey Emacs, this is -*-C-*- 2 2 ****************************************************************************** 3 3 * linuxaudiodev.c -- Linux audio device for python. 4 * 4 * 5 5 * Author : Peter Bosch 6 6 * Created On : Thu Mar 2 21:10:33 2000 7 7 * Status : Unknown, Use with caution! 8 * 8 * 9 9 * Unless other notices are present in any part of this file 10 * explicitly claiming copyrights for other people and/or 11 * organizations, the contents of this file is fully copyright 10 * explicitly claiming copyrights for other people and/or 11 * organizations, the contents of this file is fully copyright 12 12 * (C) 2000 Peter Bosch, all rights reserved. 13 13 ****************************************************************************** … … 44 44 typedef struct { 45 45 PyObject_HEAD 46 int x_fd;/* The open file */46 int x_fd; /* The open file */ 47 47 int x_mode; /* file mode */ 48 int x_icount;/* Input count */49 int x_ocount;/* Output count */50 uint32_t x_afmts;/* Audio formats supported by hardware*/48 int x_icount; /* Input count */ 49 int x_ocount; /* Output count */ 50 uint32_t x_afmts; /* Audio formats supported by hardware*/ 51 51 } lad_t; 52 52 … … 56 56 57 57 static struct { 58 int 59 uint32_t 58 int a_bps; 59 uint32_t a_fmt; 60 60 char *a_name; 61 61 } audio_types[] = { 62 { 8, 63 { 8, 64 { 8, 65 { 8, 66 { 16, 67 { 16, 68 { 16, 69 { 16, 70 { 16, 62 { 8, AFMT_MU_LAW, "logarithmic mu-law 8-bit audio" }, 63 { 8, AFMT_A_LAW, "logarithmic A-law 8-bit audio" }, 64 { 8, AFMT_U8, "linear unsigned 8-bit audio" }, 65 { 8, AFMT_S8, "linear signed 8-bit audio" }, 66 { 16, AFMT_U16_BE, "linear unsigned 16-bit big-endian audio" }, 67 { 16, AFMT_U16_LE, "linear unsigned 16-bit little-endian audio" }, 68 { 16, AFMT_S16_BE, "linear signed 16-bit big-endian audio" }, 69 { 16, AFMT_S16_LE, "linear signed 16-bit little-endian audio" }, 70 { 16, AFMT_S16_NE, "linear signed 16-bit native-endian audio" }, 71 71 }; 72 72 … … 109 109 * AUDIODEV environment variable first, then /dev/dsp. The 110 110 * control device tacks "ctl" onto the base device name. 111 * 111 * 112 112 * Note that the only difference between /dev/audio and /dev/dsp 113 113 * is that the former uses logarithmic mu-law encoding and the … … 150 150 /* if already closed, don't reclose it */ 151 151 if (xp->x_fd != -1) 152 152 close(xp->x_fd); 153 153 PyObject_Del(xp); 154 154 } … … 160 160 char *cp; 161 161 PyObject *rv; 162 162 163 163 if (!PyArg_ParseTuple(args, "i:read", &size)) 164 164 return NULL; … … 185 185 struct timeval tv; 186 186 int select_retval; 187 188 if (!PyArg_ParseTuple(args, "s#:write", &cp, &size)) 189 187 188 if (!PyArg_ParseTuple(args, "s#:write", &cp, &size)) 189 return NULL; 190 190 191 191 /* use select to wait for audio device to be available */ … … 193 193 FD_SET(self->x_fd, &write_set_fds); 194 194 tv.tv_sec = 4; /* timeout values */ 195 tv.tv_usec = 0; 195 tv.tv_usec = 0; 196 196 197 197 while (size > 0) { … … 200 200 if (select_retval) { 201 201 if ((rv = write(self->x_fd, cp, size)) == -1) { 202 203 204 205 206 207 202 if (errno != EAGAIN) { 203 PyErr_SetFromErrno(LinuxAudioError); 204 return NULL; 205 } else { 206 errno = 0; /* EAGAIN: buffer is full, try again */ 207 } 208 208 } else { 209 210 211 212 209 self->x_ocount += rv; 210 size -= rv; 211 cp += rv; 212 } 213 213 } else { 214 215 216 214 /* printf("Not able to write to linux audio device within %ld seconds\n", tv.tv_sec); */ 215 PyErr_SetFromErrno(LinuxAudioError); 216 return NULL; 217 217 } 218 218 } … … 245 245 &rate, &ssize, &nchannels, &fmt, &emulate)) 246 246 return NULL; 247 247 248 248 if (rate < 0) { 249 250 rate); 251 249 PyErr_Format(PyExc_ValueError, "expected rate >= 0, not %d", 250 rate); 251 return NULL; 252 252 } 253 253 if (ssize < 0) { 254 255 256 254 PyErr_Format(PyExc_ValueError, "expected sample size >= 0, not %d", 255 ssize); 256 return NULL; 257 257 } 258 258 if (nchannels != 1 && nchannels != 2) { 259 260 261 259 PyErr_Format(PyExc_ValueError, "nchannels must be 1 or 2, not %d", 260 nchannels); 261 return NULL; 262 262 } 263 263 … … 266 266 break; 267 267 if (n == n_audio_types) { 268 269 268 PyErr_Format(PyExc_ValueError, "unknown audio encoding: %d", fmt); 269 return NULL; 270 270 } 271 271 if (audio_types[n].a_bps != ssize) { 272 PyErr_Format(PyExc_ValueError, 273 274 275 272 PyErr_Format(PyExc_ValueError, 273 "for %s, expected sample size %d, not %d", 274 audio_types[n].a_name, audio_types[n].a_bps, ssize); 275 return NULL; 276 276 } 277 277 278 278 if (emulate == 0) { 279 280 PyErr_Format(PyExc_ValueError, 281 282 283 284 285 } 286 if (ioctl(self->x_fd, SNDCTL_DSP_SETFMT, 287 279 if ((self->x_afmts & audio_types[n].a_fmt) == 0) { 280 PyErr_Format(PyExc_ValueError, 281 "%s format not supported by device", 282 audio_types[n].a_name); 283 return NULL; 284 } 285 } 286 if (ioctl(self->x_fd, SNDCTL_DSP_SETFMT, 287 &audio_types[n].a_fmt) == -1) { 288 288 PyErr_SetFromErrno(LinuxAudioError); 289 289 return NULL; … … 308 308 309 309 fmt = 0; 310 if (ioctl(self->x_fd, SNDCTL_DSP_SETFMT, &fmt) < 0) 310 if (ioctl(self->x_fd, SNDCTL_DSP_SETFMT, &fmt) < 0) 311 311 return -errno; 312 312 … … 335 335 336 336 337 /* bufsize returns the size of the hardware audio buffer in number 337 /* bufsize returns the size of the hardware audio buffer in number 338 338 of samples */ 339 339 static PyObject * … … 354 354 } 355 355 356 /* obufcount returns the number of samples that are available in the 356 /* obufcount returns the number of samples that are available in the 357 357 hardware for playing */ 358 358 static PyObject * … … 370 370 return NULL; 371 371 } 372 return PyInt_FromLong((ai.fragstotal * ai.fragsize - ai.bytes) / 372 return PyInt_FromLong((ai.fragstotal * ai.fragsize - ai.bytes) / 373 373 (ssize * nchannels)); 374 374 } … … 411 411 412 412 if (self->x_mode == O_RDONLY) 413 413 req = SNDCTL_DSP_GETIPTR; 414 414 else 415 415 req = SNDCTL_DSP_GETOPTR; 416 416 if (ioctl(self->x_fd, req, &info) == -1) { 417 417 PyErr_SetFromErrno(LinuxAudioError); … … 422 422 423 423 static PyMethodDef lad_methods[] = { 424 { "read", 425 { "write", 426 { "setparameters", 427 { "bufsize", 428 { "obufcount", 429 { "obuffree", 430 { "flush", 431 { "close", 432 { "fileno", 424 { "read", (PyCFunction)lad_read, METH_VARARGS }, 425 { "write", (PyCFunction)lad_write, METH_VARARGS }, 426 { "setparameters", (PyCFunction)lad_setparameters, METH_VARARGS }, 427 { "bufsize", (PyCFunction)lad_bufsize, METH_VARARGS }, 428 { "obufcount", (PyCFunction)lad_obufcount, METH_NOARGS }, 429 { "obuffree", (PyCFunction)lad_obuffree, METH_NOARGS }, 430 { "flush", (PyCFunction)lad_flush, METH_NOARGS }, 431 { "close", (PyCFunction)lad_close, METH_NOARGS }, 432 { "fileno", (PyCFunction)lad_fileno, METH_NOARGS }, 433 433 { "getptr", (PyCFunction)lad_getptr, METH_NOARGS }, 434 { NULL, NULL}/* sentinel */434 { NULL, NULL} /* sentinel */ 435 435 }; 436 436 … … 444 444 PyVarObject_HEAD_INIT(&PyType_Type, 0) 445 445 "linuxaudiodev.linux_audio_device", /*tp_name*/ 446 sizeof(lad_t), 447 0, 446 sizeof(lad_t), /*tp_size*/ 447 0, /*tp_itemsize*/ 448 448 /* methods */ 449 (destructor)lad_dealloc, 450 0, 451 (getattrfunc)lad_getattr, 452 0, 453 0, 454 0, 449 (destructor)lad_dealloc, /*tp_dealloc*/ 450 0, /*tp_print*/ 451 (getattrfunc)lad_getattr, /*tp_getattr*/ 452 0, /*tp_setattr*/ 453 0, /*tp_compare*/ 454 0, /*tp_repr*/ 455 455 }; 456 456 … … 470 470 { 471 471 PyObject *m; 472 472 473 473 if (PyErr_WarnPy3k("the linuxaudiodev module has been removed in " 474 474 "Python 3.0; use the ossaudiodev module instead", 2) < 0) 475 475 return; 476 476 477 477 m = Py_InitModule("linuxaudiodev", linuxaudiodev_methods); 478 478 if (m == NULL) 479 479 return; 480 480 481 481 LinuxAudioError = PyErr_NewException("linuxaudiodev.error", NULL, NULL); 482 482 if (LinuxAudioError) 483 483 PyModule_AddObject(m, "error", LinuxAudioError); 484 484 485 485 if (PyModule_AddIntConstant(m, "AFMT_MU_LAW", (long)AFMT_MU_LAW) == -1) 486 486 return; 487 487 if (PyModule_AddIntConstant(m, "AFMT_A_LAW", (long)AFMT_A_LAW) == -1) 488 488 return; 489 489 if (PyModule_AddIntConstant(m, "AFMT_U8", (long)AFMT_U8) == -1) 490 490 return; 491 491 if (PyModule_AddIntConstant(m, "AFMT_S8", (long)AFMT_S8) == -1) 492 492 return; 493 493 if (PyModule_AddIntConstant(m, "AFMT_U16_BE", (long)AFMT_U16_BE) == -1) 494 494 return; 495 495 if (PyModule_AddIntConstant(m, "AFMT_U16_LE", (long)AFMT_U16_LE) == -1) 496 496 return; 497 497 if (PyModule_AddIntConstant(m, "AFMT_S16_BE", (long)AFMT_S16_BE) == -1) 498 498 return; 499 499 if (PyModule_AddIntConstant(m, "AFMT_S16_LE", (long)AFMT_S16_LE) == -1) 500 500 return; 501 501 if (PyModule_AddIntConstant(m, "AFMT_S16_NE", (long)AFMT_S16_NE) == -1) 502 502 return; 503 503 504 504 return;
Note:
See TracChangeset
for help on using the changeset viewer.