Commit e2b7c4de authored by Jeremy Hylton's avatar Jeremy Hylton

test_linuxaudio:

    read the header from the .au file and do a sanity check
    pass only the data to the audio device
    call flush() so that program does not exit until playback is complete
    call all the other methods to verify that they work minimally
    call setparameters with a bunch of bugs arguments

linuxaudiodev.c:
    use explicit O_WRONLY and O_RDONLY instead of 1 and 0
    add a string name to each of the entries in audio_types[]
    add AFMT_A_LAW to the list of known formats
    add x_mode attribute to lad object, stores imode from open call
    test ioctl return value as == -1, not < 0
    in read() method, resize string before return
    add getptr() method, that calls does ioctl on GETIPTR or GETOPTR
        depending on x_mode
    in setparameters() method, do better error checking and raise
        ValueErrors; also use ioctl calls recommended by Open Sound
        System Programmer's Guido (www.opensound.com)
    use PyModule_AddXXX to define names in module
parent d88d0a1d
test_linuxaudiodev test_linuxaudiodev
expected rate >= 0, not -1
expected sample size >= 0, not -2
nchannels must be 1 or 2, not 3
unknown audio encoding: 177
sample size 16 expected for Little-endian 16-bit unsigned format: 8 received
sample size 8 expected for Logarithmic mu-law audio: 16 received
from test_support import verbose, findfile, TestFailed, TestSkipped from test_support import verbose, findfile, TestFailed, TestSkipped
import linuxaudiodev
import errno import errno
import fcntl
import linuxaudiodev
import os import os
import select
import sunaudio
import time
SND_FORMAT_MULAW_8 = 1
def play_sound_file(path): def play_sound_file(path):
fp = open(path, 'r') fp = open(path, 'r')
size, enc, rate, nchannels, extra = sunaudio.gethdr(fp)
data = fp.read() data = fp.read()
fp.close() fp.close()
if enc != SND_FORMAT_MULAW_8:
print "Expect .au file with 8-bit mu-law samples"
return
try: try:
a = linuxaudiodev.open('w') a = linuxaudiodev.open('w')
except linuxaudiodev.error, msg: except linuxaudiodev.error, msg:
if msg[0] in (errno.EACCES, errno.ENODEV): if msg[0] in (errno.EACCES, errno.ENODEV):
raise TestSkipped, msg raise TestSkipped, msg
raise TestFailed, msg raise TestFailed, msg
else:
# at least check that these methods can be invoked
a.bufsize()
a.obufcount()
a.obuffree()
a.getptr()
a.fileno()
# set parameters based on .au file headers
a.setparameters(rate, 8, nchannels, linuxaudiodev.AFMT_MU_LAW, 1)
a.write(data) a.write(data)
a.flush()
a.close() a.close()
def test_errors():
a = linuxaudiodev.open("w")
size = 8
fmt = linuxaudiodev.AFMT_U8
rate = 8000
nchannels = 1
try:
a.setparameters(-1, size, nchannels, fmt)
except ValueError, msg:
print msg
try:
a.setparameters(rate, -2, nchannels, fmt)
except ValueError, msg:
print msg
try:
a.setparameters(rate, size, 3, fmt)
except ValueError, msg:
print msg
try:
a.setparameters(rate, size, nchannels, 177)
except ValueError, msg:
print msg
try:
a.setparameters(rate, size, nchannels, linuxaudiodev.AFMT_U16_LE)
except ValueError, msg:
print msg
try:
a.setparameters(rate, 16, nchannels, fmt)
except ValueError, msg:
print msg
def test(): def test():
play_sound_file(findfile('audiotest.au')) play_sound_file(findfile('audiotest.au'))
test_errors()
test() test()
...@@ -24,8 +24,12 @@ ...@@ -24,8 +24,12 @@
#ifdef HAVE_FCNTL_H #ifdef HAVE_FCNTL_H
#include <fcntl.h> #include <fcntl.h>
#else
#define O_RDONLY 00
#define O_WRONLY 01
#endif #endif
#include <sys/ioctl.h> #include <sys/ioctl.h>
#if defined(linux) #if defined(linux)
#include <linux/soundcard.h> #include <linux/soundcard.h>
...@@ -44,24 +48,32 @@ typedef unsigned long uint32_t; ...@@ -44,24 +48,32 @@ typedef unsigned long uint32_t;
typedef struct { typedef struct {
PyObject_HEAD; PyObject_HEAD;
int x_fd; /* The open file */ int x_fd; /* The open file */
int x_mode; /* file mode */
int x_icount; /* Input count */ int x_icount; /* Input count */
int x_ocount; /* Output count */ int x_ocount; /* Output count */
uint32_t x_afmts; /* Supported audio formats */ uint32_t x_afmts; /* Audio formats supported by hardware*/
} lad_t; } lad_t;
/* XXX several format defined in soundcard.h are not supported,
including _NE (native endian) options and S32 options
*/
static struct { static struct {
int a_bps; int a_bps;
uint32_t a_fmt; uint32_t a_fmt;
char *a_name;
} audio_types[] = { } audio_types[] = {
{ 8, AFMT_MU_LAW }, { 8, AFMT_MU_LAW, "Logarithmic mu-law audio" },
{ 8, AFMT_U8 }, { 8, AFMT_A_LAW, "Logarithmic A-law audio" },
{ 8, AFMT_S8 }, { 8, AFMT_U8, "Standard unsigned 8-bit audio" },
{ 16, AFMT_U16_BE }, { 8, AFMT_S8, "Standard signed 8-bit audio" },
{ 16, AFMT_U16_LE }, { 16, AFMT_U16_BE, "Big-endian 16-bit unsigned format" },
{ 16, AFMT_S16_BE }, { 16, AFMT_U16_LE, "Little-endian 16-bit unsigned format" },
{ 16, AFMT_S16_LE }, { 16, AFMT_S16_BE, "Big-endian 16-bit signed format" },
{ 16, AFMT_S16_LE, "Little-endian 16-bit signed format" },
}; };
static int n_audio_types = sizeof(audio_types) / sizeof(audio_types[0]);
staticforward PyTypeObject Ladtype; staticforward PyTypeObject Ladtype;
...@@ -78,31 +90,36 @@ newladobject(PyObject *arg) ...@@ -78,31 +90,36 @@ newladobject(PyObject *arg)
/* Check arg for r/w/rw */ /* Check arg for r/w/rw */
if (!PyArg_ParseTuple(arg, "s:open", &mode)) return NULL; if (!PyArg_ParseTuple(arg, "s:open", &mode)) return NULL;
if (strcmp(mode, "r") == 0) if (strcmp(mode, "r") == 0)
imode = 0; imode = O_RDONLY;
else if (strcmp(mode, "w") == 0) else if (strcmp(mode, "w") == 0)
imode = 1; imode = O_WRONLY;
else { else {
PyErr_SetString(LinuxAudioError, "Mode should be one of 'r', or 'w'"); PyErr_SetString(LinuxAudioError, "Mode should be one of 'r', or 'w'");
return NULL; return NULL;
} }
/* Open the correct device. The base device name comes from the /* Open the correct device. The base device name comes from the
* AUDIODEV environment variable first, then /dev/audio. The * AUDIODEV environment variable first, then /dev/dsp. The
* control device tacks "ctl" onto the base device name. * control device tacks "ctl" onto the base device name.
*
* Note that the only difference between /dev/audio and /dev/dsp
* is that the former uses logarithmic mu-law encoding and the
* latter uses 8-bit unsigned encoding.
*/ */
basedev = getenv("AUDIODEV"); basedev = getenv("AUDIODEV");
if (!basedev) if (!basedev)
basedev = "/dev/dsp"; basedev = "/dev/dsp";
if ((fd = open(basedev, imode)) < 0) { if ((fd = open(basedev, imode)) == -1) {
PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev); PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev);
return NULL; return NULL;
} }
if (imode && ioctl(fd, SNDCTL_DSP_NONBLOCK, NULL) < 0) { if (imode == O_WRONLY && ioctl(fd, SNDCTL_DSP_NONBLOCK, NULL) == -1) {
PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev); PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev);
return NULL; return NULL;
} }
if (ioctl(fd, SNDCTL_DSP_GETFMTS, &afmts) < 0) { if (ioctl(fd, SNDCTL_DSP_GETFMTS, &afmts) == -1) {
PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev); PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev);
return NULL; return NULL;
} }
...@@ -112,6 +129,7 @@ newladobject(PyObject *arg) ...@@ -112,6 +129,7 @@ newladobject(PyObject *arg)
return NULL; return NULL;
} }
xp->x_fd = fd; xp->x_fd = fd;
xp->x_mode = imode;
xp->x_icount = xp->x_ocount = 0; xp->x_icount = xp->x_ocount = 0;
xp->x_afmts = afmts; xp->x_afmts = afmts;
return xp; return xp;
...@@ -138,17 +156,15 @@ lad_read(lad_t *self, PyObject *args) ...@@ -138,17 +156,15 @@ lad_read(lad_t *self, PyObject *args)
rv = PyString_FromStringAndSize(NULL, size); rv = PyString_FromStringAndSize(NULL, size);
if (rv == NULL) if (rv == NULL)
return NULL; return NULL;
cp = PyString_AS_STRING(rv);
if (!(cp = PyString_AsString(rv))) {
Py_DECREF(rv);
return NULL;
}
if ((count = read(self->x_fd, cp, size)) < 0) { if ((count = read(self->x_fd, cp, size)) < 0) {
PyErr_SetFromErrno(LinuxAudioError); PyErr_SetFromErrno(LinuxAudioError);
Py_DECREF(rv); Py_DECREF(rv);
return NULL; return NULL;
} }
self->x_icount += count; self->x_icount += count;
if (_PyString_Resize(&rv, count) == -1)
return NULL;
return rv; return rv;
} }
...@@ -158,10 +174,11 @@ lad_write(lad_t *self, PyObject *args) ...@@ -158,10 +174,11 @@ lad_write(lad_t *self, PyObject *args)
char *cp; char *cp;
int rv, size; int rv, size;
if (!PyArg_ParseTuple(args, "s#:write", &cp, &size)) return NULL; if (!PyArg_ParseTuple(args, "s#:write", &cp, &size))
return NULL;
while (size > 0) { while (size > 0) {
if ((rv = write(self->x_fd, cp, size)) < 0) { if ((rv = write(self->x_fd, cp, size)) == -1) {
PyErr_SetFromErrno(LinuxAudioError); PyErr_SetFromErrno(LinuxAudioError);
return NULL; return NULL;
} }
...@@ -176,7 +193,9 @@ lad_write(lad_t *self, PyObject *args) ...@@ -176,7 +193,9 @@ lad_write(lad_t *self, PyObject *args)
static PyObject * static PyObject *
lad_close(lad_t *self, PyObject *args) lad_close(lad_t *self, PyObject *args)
{ {
if (!PyArg_ParseTuple(args, ":close")) return NULL; if (!PyArg_ParseTuple(args, ":close"))
return NULL;
if (self->x_fd >= 0) { if (self->x_fd >= 0) {
close(self->x_fd); close(self->x_fd);
self->x_fd = -1; self->x_fd = -1;
...@@ -188,50 +207,73 @@ lad_close(lad_t *self, PyObject *args) ...@@ -188,50 +207,73 @@ lad_close(lad_t *self, PyObject *args)
static PyObject * static PyObject *
lad_fileno(lad_t *self, PyObject *args) lad_fileno(lad_t *self, PyObject *args)
{ {
if (!PyArg_ParseTuple(args, ":fileno")) return NULL; if (!PyArg_ParseTuple(args, ":fileno"))
return NULL;
return PyInt_FromLong(self->x_fd); return PyInt_FromLong(self->x_fd);
} }
static PyObject * static PyObject *
lad_setparameters(lad_t *self, PyObject *args) lad_setparameters(lad_t *self, PyObject *args)
{ {
int rate, ssize, nchannels, stereo, n, fmt; int rate, ssize, nchannels, n, fmt, emulate=0;
if (!PyArg_ParseTuple(args, "iiii:setparameters", if (!PyArg_ParseTuple(args, "iiii|i:setparameters",
&rate, &ssize, &nchannels, &fmt)) &rate, &ssize, &nchannels, &fmt, &emulate))
return NULL; return NULL;
if (rate < 0 || ssize < 0 || (nchannels != 1 && nchannels != 2)) { if (rate < 0) {
PyErr_SetFromErrno(LinuxAudioError); PyErr_Format(PyExc_ValueError, "expected rate >= 0, not %d",
rate);
return NULL; return NULL;
} }
if (ioctl(self->x_fd, SOUND_PCM_WRITE_RATE, &rate) < 0) { if (ssize < 0) {
PyErr_SetFromErrno(LinuxAudioError); PyErr_Format(PyExc_ValueError, "expected sample size >= 0, not %d",
ssize);
return NULL; return NULL;
} }
if (ioctl(self->x_fd, SNDCTL_DSP_SAMPLESIZE, &ssize) < 0) { if (nchannels != 1 && nchannels != 2) {
PyErr_Format(PyExc_ValueError, "nchannels must be 1 or 2, not %d",
nchannels);
return NULL;
}
if (ioctl(self->x_fd, SNDCTL_DSP_SPEED, &rate) == -1) {
PyErr_SetFromErrno(LinuxAudioError); PyErr_SetFromErrno(LinuxAudioError);
return NULL; return NULL;
} }
stereo = (nchannels == 1)? 0: (nchannels == 2)? 1: -1; if (ioctl(self->x_fd, SNDCTL_DSP_CHANNELS, &nchannels) == -1) {
if (ioctl(self->x_fd, SNDCTL_DSP_STEREO, &stereo) < 0) {
PyErr_SetFromErrno(LinuxAudioError); PyErr_SetFromErrno(LinuxAudioError);
return NULL; return NULL;
} }
for (n = 0; n != sizeof(audio_types) / sizeof(audio_types[0]); n++)
for (n = 0; n < n_audio_types; n++)
if (fmt == audio_types[n].a_fmt) if (fmt == audio_types[n].a_fmt)
break; break;
if (n == n_audio_types) {
PyErr_Format(PyExc_ValueError, "unknown audio encoding: %d", fmt);
return NULL;
}
if (audio_types[n].a_bps != ssize) {
PyErr_Format(PyExc_ValueError,
"sample size %d expected for %s: %d received",
audio_types[n].a_bps, audio_types[n].a_name, ssize);
return NULL;
}
if (n == sizeof(audio_types) / sizeof(audio_types[0]) || if (emulate == 0) {
audio_types[n].a_bps != ssize || if ((self->x_afmts & audio_types[n].a_fmt) == 0) {
(self->x_afmts & audio_types[n].a_fmt) == 0) { PyErr_Format(PyExc_ValueError,
PyErr_SetFromErrno(LinuxAudioError); "format not supported by device: %s",
audio_types[n].a_name);
return NULL; return NULL;
} }
if (ioctl(self->x_fd, SNDCTL_DSP_SETFMT, &audio_types[n].a_fmt) < 0) { }
if (ioctl(self->x_fd, SNDCTL_DSP_SETFMT,
&audio_types[n].a_fmt) == -1) {
PyErr_SetFromErrno(LinuxAudioError); PyErr_SetFromErrno(LinuxAudioError);
return NULL; return NULL;
} }
Py_INCREF(Py_None); Py_INCREF(Py_None);
return Py_None; return Py_None;
} }
...@@ -342,7 +384,7 @@ lad_flush(lad_t *self, PyObject *args) ...@@ -342,7 +384,7 @@ lad_flush(lad_t *self, PyObject *args)
{ {
if (!PyArg_ParseTuple(args, ":flush")) return NULL; if (!PyArg_ParseTuple(args, ":flush")) return NULL;
if (ioctl(self->x_fd, SNDCTL_DSP_SYNC, NULL) < 0) { if (ioctl(self->x_fd, SNDCTL_DSP_SYNC, NULL) == -1) {
PyErr_SetFromErrno(LinuxAudioError); PyErr_SetFromErrno(LinuxAudioError);
return NULL; return NULL;
} }
...@@ -350,6 +392,26 @@ lad_flush(lad_t *self, PyObject *args) ...@@ -350,6 +392,26 @@ lad_flush(lad_t *self, PyObject *args)
return Py_None; return Py_None;
} }
static PyObject *
lad_getptr(lad_t *self, PyObject *args)
{
count_info info;
int req;
if (!PyArg_ParseTuple(args, ":getptr"))
return NULL;
if (self->x_mode == O_RDONLY)
req = SNDCTL_DSP_GETIPTR;
else
req = SNDCTL_DSP_GETOPTR;
if (ioctl(self->x_fd, req, &info) == -1) {
PyErr_SetFromErrno(LinuxAudioError);
return NULL;
}
return Py_BuildValue("iii", info.bytes, info.blocks, info.ptr);
}
static PyMethodDef lad_methods[] = { static PyMethodDef lad_methods[] = {
{ "read", (PyCFunction)lad_read, METH_VARARGS }, { "read", (PyCFunction)lad_read, METH_VARARGS },
{ "write", (PyCFunction)lad_write, METH_VARARGS }, { "write", (PyCFunction)lad_write, METH_VARARGS },
...@@ -360,6 +422,7 @@ static PyMethodDef lad_methods[] = { ...@@ -360,6 +422,7 @@ static PyMethodDef lad_methods[] = {
{ "flush", (PyCFunction)lad_flush, METH_VARARGS }, { "flush", (PyCFunction)lad_flush, METH_VARARGS },
{ "close", (PyCFunction)lad_close, METH_VARARGS }, { "close", (PyCFunction)lad_close, METH_VARARGS },
{ "fileno", (PyCFunction)lad_fileno, METH_VARARGS }, { "fileno", (PyCFunction)lad_fileno, METH_VARARGS },
{ "getptr", (PyCFunction)lad_getptr, METH_VARARGS },
{ NULL, NULL} /* sentinel */ { NULL, NULL} /* sentinel */
}; };
...@@ -398,50 +461,30 @@ static PyMethodDef linuxaudiodev_methods[] = { ...@@ -398,50 +461,30 @@ static PyMethodDef linuxaudiodev_methods[] = {
void void
initlinuxaudiodev(void) initlinuxaudiodev(void)
{ {
PyObject *m, *d, *x; PyObject *m;
m = Py_InitModule("linuxaudiodev", linuxaudiodev_methods); m = Py_InitModule("linuxaudiodev", linuxaudiodev_methods);
d = PyModule_GetDict(m);
LinuxAudioError = PyErr_NewException("linuxaudiodev.error", NULL, NULL); LinuxAudioError = PyErr_NewException("linuxaudiodev.error", NULL, NULL);
if (LinuxAudioError) if (LinuxAudioError)
PyDict_SetItemString(d, "error", LinuxAudioError); PyModule_AddObject(m, "error", LinuxAudioError);
x = PyInt_FromLong((long) AFMT_MU_LAW); if (PyModule_AddIntConstant(m, "AFMT_MU_LAW", (long)AFMT_MU_LAW) == -1)
if (x == NULL || PyDict_SetItemString(d, "AFMT_MU_LAW", x) < 0) return;
goto error; if (PyModule_AddIntConstant(m, "AFMT_A_LAW", (long)AFMT_A_LAW) == -1)
Py_DECREF(x); return;
if (PyModule_AddIntConstant(m, "AFMT_U8", (long)AFMT_U8) == -1)
x = PyInt_FromLong((long) AFMT_U8); return;
if (x == NULL || PyDict_SetItemString(d, "AFMT_U8", x) < 0) if (PyModule_AddIntConstant(m, "AFMT_S8", (long)AFMT_S8) == -1)
goto error; return;
Py_DECREF(x); if (PyModule_AddIntConstant(m, "AFMT_U16_BE", (long)AFMT_U16_BE) == -1)
return;
x = PyInt_FromLong((long) AFMT_S8); if (PyModule_AddIntConstant(m, "AFMT_U16_LE", (long)AFMT_U16_LE) == -1)
if (x == NULL || PyDict_SetItemString(d, "AFMT_S8", x) < 0) return;
goto error; if (PyModule_AddIntConstant(m, "AFMT_S16_BE", (long)AFMT_S16_BE) == -1)
Py_DECREF(x); return;
if (PyModule_AddIntConstant(m, "AFMT_S16_LE", (long)AFMT_S16_LE) == -1)
x = PyInt_FromLong((long) AFMT_U16_BE); return;
if (x == NULL || PyDict_SetItemString(d, "AFMT_U16_BE", x) < 0)
goto error;
Py_DECREF(x);
x = PyInt_FromLong((long) AFMT_U16_LE);
if (x == NULL || PyDict_SetItemString(d, "AFMT_U16_LE", x) < 0)
goto error;
Py_DECREF(x);
x = PyInt_FromLong((long) AFMT_S16_BE);
if (x == NULL || PyDict_SetItemString(d, "AFMT_S16_BE", x) < 0)
goto error;
Py_DECREF(x);
x = PyInt_FromLong((long) AFMT_S16_LE);
if (x == NULL || PyDict_SetItemString(d, "AFMT_S16_LE", x) < 0)
goto error;
error:
Py_DECREF(x);
return; return;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment