| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "Python.h" |
| 17 |
#include "structmember.h" |
| 18 |
|
| 19 |
#ifdef HAVE_FCNTL_H |
| 20 |
#include <fcntl.h> |
| 21 |
#else |
| 22 |
#define O_RDONLY 00 |
| 23 |
#define O_WRONLY 01 |
| 24 |
#endif |
| 25 |
|
| 26 |
|
| 27 |
#include <sys/ioctl.h> |
| 28 |
#if defined(linux) |
| 29 |
#include <linux/soundcard.h> |
| 30 |
|
| 31 |
#ifndef HAVE_STDINT_H |
| 32 |
typedef unsigned long uint32_t; |
| 33 |
#endif |
| 34 |
|
| 35 |
#elif defined(__FreeBSD__) |
| 36 |
#include <machine/soundcard.h> |
| 37 |
|
| 38 |
#ifndef SNDCTL_DSP_CHANNELS |
| 39 |
#define SNDCTL_DSP_CHANNELS SOUND_PCM_WRITE_CHANNELS |
| 40 |
#endif |
| 41 |
|
| 42 |
#endif |
| 43 |
|
| 44 |
typedef struct { |
| 45 |
PyObject_HEAD |
| 46 |
int x_fd; |
| 47 |
int x_mode; |
| 48 |
int x_icount; |
| 49 |
int x_ocount; |
| 50 |
uint32_t x_afmts; |
| 51 |
} lad_t; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
static struct { |
| 58 |
int a_bps; |
| 59 |
uint32_t a_fmt; |
| 60 |
char *a_name; |
| 61 |
} audio_types[] = { |
| 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 |
}; |
| 72 |
|
| 73 |
static int n_audio_types = sizeof(audio_types) / sizeof(audio_types[0]); |
| 74 |
|
| 75 |
static PyTypeObject Ladtype; |
| 76 |
|
| 77 |
static PyObject *LinuxAudioError; |
| 78 |
|
| 79 |
static lad_t * |
| 80 |
newladobject(PyObject *arg) |
| 81 |
{ |
| 82 |
lad_t *xp; |
| 83 |
int fd, afmts, imode; |
| 84 |
char *basedev = NULL; |
| 85 |
char *mode = NULL; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
if (!PyArg_ParseTuple(arg, "s|s:open", &basedev, &mode)) |
| 93 |
return NULL; |
| 94 |
if (mode == NULL) { |
| 95 |
mode = basedev; |
| 96 |
basedev = NULL; |
| 97 |
} |
| 98 |
|
| 99 |
if (strcmp(mode, "r") == 0) |
| 100 |
imode = O_RDONLY; |
| 101 |
else if (strcmp(mode, "w") == 0) |
| 102 |
imode = O_WRONLY; |
| 103 |
else { |
| 104 |
PyErr_SetString(LinuxAudioError, "mode should be 'r' or 'w'"); |
| 105 |
return NULL; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
if (basedev == NULL) { |
| 118 |
basedev = getenv("AUDIODEV"); |
| 119 |
if (basedev == NULL) |
| 120 |
basedev = "/dev/dsp"; |
| 121 |
} |
| 122 |
|
| 123 |
if ((fd = open(basedev, imode)) == -1) { |
| 124 |
PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev); |
| 125 |
return NULL; |
| 126 |
} |
| 127 |
if (imode == O_WRONLY && ioctl(fd, SNDCTL_DSP_NONBLOCK, NULL) == -1) { |
| 128 |
PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev); |
| 129 |
close(fd); |
| 130 |
return NULL; |
| 131 |
} |
| 132 |
if (ioctl(fd, SNDCTL_DSP_GETFMTS, &afmts) == -1) { |
| 133 |
PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev); |
| 134 |
close(fd); |
| 135 |
return NULL; |
| 136 |
} |
| 137 |
|
| 138 |
if ((xp = PyObject_New(lad_t, &Ladtype)) == NULL) { |
| 139 |
close(fd); |
| 140 |
return NULL; |
| 141 |
} |
| 142 |
xp->x_fd = fd; |
| 143 |
xp->x_mode = imode; |
| 144 |
xp->x_icount = xp->x_ocount = 0; |
| 145 |
xp->x_afmts = afmts; |
| 146 |
return xp; |
| 147 |
} |
| 148 |
|
| 149 |
static void |
| 150 |
lad_dealloc(lad_t *xp) |
| 151 |
{ |
| 152 |
|
| 153 |
if (xp->x_fd != -1) |
| 154 |
close(xp->x_fd); |
| 155 |
PyObject_Del(xp); |
| 156 |
} |
| 157 |
|
| 158 |
static PyObject * |
| 159 |
lad_read(lad_t *self, PyObject *args) |
| 160 |
{ |
| 161 |
int size, count; |
| 162 |
char *cp; |
| 163 |
PyObject *rv; |
| 164 |
|
| 165 |
if (!PyArg_ParseTuple(args, "i:read", &size)) |
| 166 |
return NULL; |
| 167 |
rv = PyString_FromStringAndSize(NULL, size); |
| 168 |
if (rv == NULL) |
| 169 |
return NULL; |
| 170 |
cp = PyString_AS_STRING(rv); |
| 171 |
if ((count = read(self->x_fd, cp, size)) < 0) { |
| 172 |
PyErr_SetFromErrno(LinuxAudioError); |
| 173 |
Py_DECREF(rv); |
| 174 |
return NULL; |
| 175 |
} |
| 176 |
self->x_icount += count; |
| 177 |
_PyString_Resize(&rv, count); |
| 178 |
return rv; |
| 179 |
} |
| 180 |
|
| 181 |
static PyObject * |
| 182 |
lad_write(lad_t *self, PyObject *args) |
| 183 |
{ |
| 184 |
char *cp; |
| 185 |
int rv, size; |
| 186 |
fd_set write_set_fds; |
| 187 |
struct timeval tv; |
| 188 |
int select_retval; |
| 189 |
|
| 190 |
if (!PyArg_ParseTuple(args, "s#:write", &cp, &size)) |
| 191 |
return NULL; |
| 192 |
|
| 193 |
|
| 194 |
FD_ZERO(&write_set_fds); |
| 195 |
FD_SET(self->x_fd, &write_set_fds); |
| 196 |
tv.tv_sec = 4; |
| 197 |
tv.tv_usec = 0; |
| 198 |
|
| 199 |
while (size > 0) { |
| 200 |
select_retval = select(self->x_fd+1, NULL, &write_set_fds, NULL, &tv); |
| 201 |
tv.tv_sec = 1; tv.tv_usec = 0; |
| 202 |
if (select_retval) { |
| 203 |
if ((rv = write(self->x_fd, cp, size)) == -1) { |
| 204 |
if (errno != EAGAIN) { |
| 205 |
PyErr_SetFromErrno(LinuxAudioError); |
| 206 |
return NULL; |
| 207 |
} else { |
| 208 |
errno = 0; |
| 209 |
} |
| 210 |
} else { |
| 211 |
self->x_ocount += rv; |
| 212 |
size -= rv; |
| 213 |
cp += rv; |
| 214 |
} |
| 215 |
} else { |
| 216 |
|
| 217 |
PyErr_SetFromErrno(LinuxAudioError); |
| 218 |
return NULL; |
| 219 |
} |
| 220 |
} |
| 221 |
Py_INCREF(Py_None); |
| 222 |
return Py_None; |
| 223 |
} |
| 224 |
|
| 225 |
static PyObject * |
| 226 |
lad_close(lad_t *self, PyObject *unused) |
| 227 |
{ |
| 228 |
if (self->x_fd >= 0) { |
| 229 |
close(self->x_fd); |
| 230 |
self->x_fd = -1; |
| 231 |
} |
| 232 |
Py_RETURN_NONE; |
| 233 |
} |
| 234 |
|
| 235 |
static PyObject * |
| 236 |
lad_fileno(lad_t *self, PyObject *unused) |
| 237 |
{ |
| 238 |
return PyInt_FromLong(self->x_fd); |
| 239 |
} |
| 240 |
|
| 241 |
static PyObject * |
| 242 |
lad_setparameters(lad_t *self, PyObject *args) |
| 243 |
{ |
| 244 |
int rate, ssize, nchannels, n, fmt, emulate=0; |
| 245 |
|
| 246 |
if (!PyArg_ParseTuple(args, "iiii|i:setparameters", |
| 247 |
&rate, &ssize, &nchannels, &fmt, &emulate)) |
| 248 |
return NULL; |
| 249 |
|
| 250 |
if (rate < 0) { |
| 251 |
PyErr_Format(PyExc_ValueError, "expected rate >= 0, not %d", |
| 252 |
rate); |
| 253 |
return NULL; |
| 254 |
} |
| 255 |
if (ssize < 0) { |
| 256 |
PyErr_Format(PyExc_ValueError, "expected sample size >= 0, not %d", |
| 257 |
ssize); |
| 258 |
return NULL; |
| 259 |
} |
| 260 |
if (nchannels != 1 && nchannels != 2) { |
| 261 |
PyErr_Format(PyExc_ValueError, "nchannels must be 1 or 2, not %d", |
| 262 |
nchannels); |
| 263 |
return NULL; |
| 264 |
} |
| 265 |
|
| 266 |
for (n = 0; n < n_audio_types; n++) |
| 267 |
if (fmt == audio_types[n].a_fmt) |
| 268 |
break; |
| 269 |
if (n == n_audio_types) { |
| 270 |
PyErr_Format(PyExc_ValueError, "unknown audio encoding: %d", fmt); |
| 271 |
return NULL; |
| 272 |
} |
| 273 |
if (audio_types[n].a_bps != ssize) { |
| 274 |
PyErr_Format(PyExc_ValueError, |
| 275 |
"for %s, expected sample size %d, not %d", |
| 276 |
audio_types[n].a_name, audio_types[n].a_bps, ssize); |
| 277 |
return NULL; |
| 278 |
} |
| 279 |
|
| 280 |
if (emulate == 0) { |
| 281 |
if ((self->x_afmts & audio_types[n].a_fmt) == 0) { |
| 282 |
PyErr_Format(PyExc_ValueError, |
| 283 |
"%s format not supported by device", |
| 284 |
audio_types[n].a_name); |
| 285 |
return NULL; |
| 286 |
} |
| 287 |
} |
| 288 |
if (ioctl(self->x_fd, SNDCTL_DSP_SETFMT, |
| 289 |
&audio_types[n].a_fmt) == -1) { |
| 290 |
PyErr_SetFromErrno(LinuxAudioError); |
| 291 |
return NULL; |
| 292 |
} |
| 293 |
if (ioctl(self->x_fd, SNDCTL_DSP_CHANNELS, &nchannels) == -1) { |
| 294 |
PyErr_SetFromErrno(LinuxAudioError); |
| 295 |
return NULL; |
| 296 |
} |
| 297 |
if (ioctl(self->x_fd, SNDCTL_DSP_SPEED, &rate) == -1) { |
| 298 |
PyErr_SetFromErrno(LinuxAudioError); |
| 299 |
return NULL; |
| 300 |
} |
| 301 |
|
| 302 |
Py_INCREF(Py_None); |
| 303 |
return Py_None; |
| 304 |
} |
| 305 |
|
| 306 |
static int |
| 307 |
_ssize(lad_t *self, int *nchannels, int *ssize) |
| 308 |
{ |
| 309 |
int fmt; |
| 310 |
|
| 311 |
fmt = 0; |
| 312 |
if (ioctl(self->x_fd, SNDCTL_DSP_SETFMT, &fmt) < 0) |
| 313 |
return -errno; |
| 314 |
|
| 315 |
switch (fmt) { |
| 316 |
case AFMT_MU_LAW: |
| 317 |
case AFMT_A_LAW: |
| 318 |
case AFMT_U8: |
| 319 |
case AFMT_S8: |
| 320 |
*ssize = sizeof(char); |
| 321 |
break; |
| 322 |
case AFMT_S16_LE: |
| 323 |
case AFMT_S16_BE: |
| 324 |
case AFMT_U16_LE: |
| 325 |
case AFMT_U16_BE: |
| 326 |
*ssize = sizeof(short); |
| 327 |
break; |
| 328 |
case AFMT_MPEG: |
| 329 |
case AFMT_IMA_ADPCM: |
| 330 |
default: |
| 331 |
return -EOPNOTSUPP; |
| 332 |
} |
| 333 |
if (ioctl(self->x_fd, SNDCTL_DSP_CHANNELS, nchannels) < 0) |
| 334 |
return -errno; |
| 335 |
return 0; |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
static PyObject * |
| 342 |
lad_bufsize(lad_t *self, PyObject *unused) |
| 343 |
{ |
| 344 |
audio_buf_info ai; |
| 345 |
int nchannels=0, ssize=0; |
| 346 |
|
| 347 |
if (_ssize(self, &nchannels, &ssize) < 0 || !ssize || !nchannels) { |
| 348 |
PyErr_SetFromErrno(LinuxAudioError); |
| 349 |
return NULL; |
| 350 |
} |
| 351 |
if (ioctl(self->x_fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) { |
| 352 |
PyErr_SetFromErrno(LinuxAudioError); |
| 353 |
return NULL; |
| 354 |
} |
| 355 |
return PyInt_FromLong((ai.fragstotal * ai.fragsize) / (nchannels * ssize)); |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
static PyObject * |
| 361 |
lad_obufcount(lad_t *self, PyObject *unused) |
| 362 |
{ |
| 363 |
audio_buf_info ai; |
| 364 |
int nchannels=0, ssize=0; |
| 365 |
|
| 366 |
if (_ssize(self, &nchannels, &ssize) < 0 || !ssize || !nchannels) { |
| 367 |
PyErr_SetFromErrno(LinuxAudioError); |
| 368 |
return NULL; |
| 369 |
} |
| 370 |
if (ioctl(self->x_fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) { |
| 371 |
PyErr_SetFromErrno(LinuxAudioError); |
| 372 |
return NULL; |
| 373 |
} |
| 374 |
return PyInt_FromLong((ai.fragstotal * ai.fragsize - ai.bytes) / |
| 375 |
(ssize * nchannels)); |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
static PyObject * |
| 381 |
lad_obuffree(lad_t *self, PyObject *unused) |
| 382 |
{ |
| 383 |
audio_buf_info ai; |
| 384 |
int nchannels=0, ssize=0; |
| 385 |
|
| 386 |
if (_ssize(self, &nchannels, &ssize) < 0 || !ssize || !nchannels) { |
| 387 |
PyErr_SetFromErrno(LinuxAudioError); |
| 388 |
return NULL; |
| 389 |
} |
| 390 |
if (ioctl(self->x_fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) { |
| 391 |
PyErr_SetFromErrno(LinuxAudioError); |
| 392 |
return NULL; |
| 393 |
} |
| 394 |
return PyInt_FromLong(ai.bytes / (ssize * nchannels)); |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
static PyObject * |
| 399 |
lad_flush(lad_t *self, PyObject *unused) |
| 400 |
{ |
| 401 |
if (ioctl(self->x_fd, SNDCTL_DSP_SYNC, NULL) == -1) { |
| 402 |
PyErr_SetFromErrno(LinuxAudioError); |
| 403 |
return NULL; |
| 404 |
} |
| 405 |
Py_RETURN_NONE; |
| 406 |
} |
| 407 |
|
| 408 |
static PyObject * |
| 409 |
lad_getptr(lad_t *self, PyObject *unused) |
| 410 |
{ |
| 411 |
count_info info; |
| 412 |
int req; |
| 413 |
|
| 414 |
if (self->x_mode == O_RDONLY) |
| 415 |
req = SNDCTL_DSP_GETIPTR; |
| 416 |
else |
| 417 |
req = SNDCTL_DSP_GETOPTR; |
| 418 |
if (ioctl(self->x_fd, req, &info) == -1) { |
| 419 |
PyErr_SetFromErrno(LinuxAudioError); |
| 420 |
return NULL; |
| 421 |
} |
| 422 |
return Py_BuildValue("iii", info.bytes, info.blocks, info.ptr); |
| 423 |
} |
| 424 |
|
| 425 |
static PyMethodDef lad_methods[] = { |
| 426 |
{ "read", (PyCFunction)lad_read, METH_VARARGS }, |
| 427 |
{ "write", (PyCFunction)lad_write, METH_VARARGS }, |
| 428 |
{ "setparameters", (PyCFunction)lad_setparameters, METH_VARARGS }, |
| 429 |
{ "bufsize", (PyCFunction)lad_bufsize, METH_VARARGS }, |
| 430 |
{ "obufcount", (PyCFunction)lad_obufcount, METH_NOARGS }, |
| 431 |
{ "obuffree", (PyCFunction)lad_obuffree, METH_NOARGS }, |
| 432 |
{ "flush", (PyCFunction)lad_flush, METH_NOARGS }, |
| 433 |
{ "close", (PyCFunction)lad_close, METH_NOARGS }, |
| 434 |
{ "fileno", (PyCFunction)lad_fileno, METH_NOARGS }, |
| 435 |
{ "getptr", (PyCFunction)lad_getptr, METH_NOARGS }, |
| 436 |
{ NULL, NULL} |
| 437 |
}; |
| 438 |
|
| 439 |
static PyObject * |
| 440 |
lad_getattr(lad_t *xp, char *name) |
| 441 |
{ |
| 442 |
return Py_FindMethod(lad_methods, (PyObject *)xp, name); |
| 443 |
} |
| 444 |
|
| 445 |
static PyTypeObject Ladtype = { |
| 446 |
PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 447 |
"linuxaudiodev.linux_audio_device", |
| 448 |
sizeof(lad_t), |
| 449 |
0, |
| 450 |
|
| 451 |
(destructor)lad_dealloc, |
| 452 |
0, |
| 453 |
(getattrfunc)lad_getattr, |
| 454 |
0, |
| 455 |
0, |
| 456 |
0, |
| 457 |
}; |
| 458 |
|
| 459 |
static PyObject * |
| 460 |
ladopen(PyObject *self, PyObject *args) |
| 461 |
{ |
| 462 |
return (PyObject *)newladobject(args); |
| 463 |
} |
| 464 |
|
| 465 |
static PyMethodDef linuxaudiodev_methods[] = { |
| 466 |
{ "open", ladopen, METH_VARARGS }, |
| 467 |
{ 0, 0 }, |
| 468 |
}; |
| 469 |
|
| 470 |
void |
| 471 |
initlinuxaudiodev(void) |
| 472 |
{ |
| 473 |
PyObject *m; |
| 474 |
|
| 475 |
if (PyErr_WarnPy3k("the linuxaudiodev module has been removed in " |
| 476 |
"Python 3.0; use the ossaudiodev module instead", 2) < 0) |
| 477 |
return; |
| 478 |
|
| 479 |
m = Py_InitModule("linuxaudiodev", linuxaudiodev_methods); |
| 480 |
if (m == NULL) |
| 481 |
return; |
| 482 |
|
| 483 |
LinuxAudioError = PyErr_NewException("linuxaudiodev.error", NULL, NULL); |
| 484 |
if (LinuxAudioError) |
| 485 |
PyModule_AddObject(m, "error", LinuxAudioError); |
| 486 |
|
| 487 |
if (PyModule_AddIntConstant(m, "AFMT_MU_LAW", (long)AFMT_MU_LAW) == -1) |
| 488 |
return; |
| 489 |
if (PyModule_AddIntConstant(m, "AFMT_A_LAW", (long)AFMT_A_LAW) == -1) |
| 490 |
return; |
| 491 |
if (PyModule_AddIntConstant(m, "AFMT_U8", (long)AFMT_U8) == -1) |
| 492 |
return; |
| 493 |
if (PyModule_AddIntConstant(m, "AFMT_S8", (long)AFMT_S8) == -1) |
| 494 |
return; |
| 495 |
if (PyModule_AddIntConstant(m, "AFMT_U16_BE", (long)AFMT_U16_BE) == -1) |
| 496 |
return; |
| 497 |
if (PyModule_AddIntConstant(m, "AFMT_U16_LE", (long)AFMT_U16_LE) == -1) |
| 498 |
return; |
| 499 |
if (PyModule_AddIntConstant(m, "AFMT_S16_BE", (long)AFMT_S16_BE) == -1) |
| 500 |
return; |
| 501 |
if (PyModule_AddIntConstant(m, "AFMT_S16_LE", (long)AFMT_S16_LE) == -1) |
| 502 |
return; |
| 503 |
if (PyModule_AddIntConstant(m, "AFMT_S16_NE", (long)AFMT_S16_NE) == -1) |
| 504 |
return; |
| 505 |
|
| 506 |
return; |
| 507 |
} |