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
This commit is contained in:
Jeremy Hylton 2000-10-06 19:39:55 +00:00
parent d88d0a1d5b
commit e2b7c4dea3
3 changed files with 195 additions and 91 deletions

View File

@ -1 +1,7 @@
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

View File

@ -1,23 +1,78 @@
from test_support import verbose, findfile, TestFailed, TestSkipped
import linuxaudiodev
import errno
import fcntl
import linuxaudiodev
import os
import select
import sunaudio
import time
SND_FORMAT_MULAW_8 = 1
def play_sound_file(path):
fp = open(path, 'r')
size, enc, rate, nchannels, extra = sunaudio.gethdr(fp)
data = fp.read()
fp.close()
if enc != SND_FORMAT_MULAW_8:
print "Expect .au file with 8-bit mu-law samples"
return
try:
a = linuxaudiodev.open('w')
except linuxaudiodev.error, msg:
if msg[0] in (errno.EACCES, errno.ENODEV):
raise TestSkipped, msg
raise TestFailed, msg
else:
a.write(data)
a.close()
# 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.flush()
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():
play_sound_file(findfile('audiotest.au'))
test_errors()
test()

View File

@ -24,8 +24,12 @@
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#else
#define O_RDONLY 00
#define O_WRONLY 01
#endif
#include <sys/ioctl.h>
#if defined(linux)
#include <linux/soundcard.h>
@ -44,24 +48,32 @@ typedef unsigned long uint32_t;
typedef struct {
PyObject_HEAD;
int x_fd; /* The open file */
int x_mode; /* file mode */
int x_icount; /* Input count */
int x_ocount; /* Output count */
uint32_t x_afmts; /* Supported audio formats */
uint32_t x_afmts; /* Audio formats supported by hardware*/
} lad_t;
/* XXX several format defined in soundcard.h are not supported,
including _NE (native endian) options and S32 options
*/
static struct {
int a_bps;
uint32_t a_fmt;
char *a_name;
} audio_types[] = {
{ 8, AFMT_MU_LAW },
{ 8, AFMT_U8 },
{ 8, AFMT_S8 },
{ 16, AFMT_U16_BE },
{ 16, AFMT_U16_LE },
{ 16, AFMT_S16_BE },
{ 16, AFMT_S16_LE },
{ 8, AFMT_MU_LAW, "Logarithmic mu-law audio" },
{ 8, AFMT_A_LAW, "Logarithmic A-law audio" },
{ 8, AFMT_U8, "Standard unsigned 8-bit audio" },
{ 8, AFMT_S8, "Standard signed 8-bit audio" },
{ 16, AFMT_U16_BE, "Big-endian 16-bit unsigned format" },
{ 16, AFMT_U16_LE, "Little-endian 16-bit unsigned format" },
{ 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;
@ -78,31 +90,36 @@ newladobject(PyObject *arg)
/* Check arg for r/w/rw */
if (!PyArg_ParseTuple(arg, "s:open", &mode)) return NULL;
if (strcmp(mode, "r") == 0)
imode = 0;
imode = O_RDONLY;
else if (strcmp(mode, "w") == 0)
imode = 1;
imode = O_WRONLY;
else {
PyErr_SetString(LinuxAudioError, "Mode should be one of 'r', or 'w'");
return NULL;
}
/* 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.
*
* 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");
if (!basedev)
basedev = "/dev/dsp";
if ((fd = open(basedev, imode)) < 0) {
if ((fd = open(basedev, imode)) == -1) {
PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev);
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);
return NULL;
}
if (ioctl(fd, SNDCTL_DSP_GETFMTS, &afmts) < 0) {
if (ioctl(fd, SNDCTL_DSP_GETFMTS, &afmts) == -1) {
PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev);
return NULL;
}
@ -111,7 +128,8 @@ newladobject(PyObject *arg)
close(fd);
return NULL;
}
xp->x_fd = fd;
xp->x_fd = fd;
xp->x_mode = imode;
xp->x_icount = xp->x_ocount = 0;
xp->x_afmts = afmts;
return xp;
@ -138,17 +156,15 @@ lad_read(lad_t *self, PyObject *args)
rv = PyString_FromStringAndSize(NULL, size);
if (rv == NULL)
return NULL;
if (!(cp = PyString_AsString(rv))) {
Py_DECREF(rv);
return NULL;
}
cp = PyString_AS_STRING(rv);
if ((count = read(self->x_fd, cp, size)) < 0) {
PyErr_SetFromErrno(LinuxAudioError);
Py_DECREF(rv);
return NULL;
}
self->x_icount += count;
if (_PyString_Resize(&rv, count) == -1)
return NULL;
return rv;
}
@ -158,16 +174,17 @@ lad_write(lad_t *self, PyObject *args)
char *cp;
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) {
if ((rv = write(self->x_fd, cp, size)) < 0) {
if ((rv = write(self->x_fd, cp, size)) == -1) {
PyErr_SetFromErrno(LinuxAudioError);
return NULL;
}
self->x_ocount += rv;
size -= rv;
cp += rv;
size -= rv;
cp += rv;
}
Py_INCREF(Py_None);
return Py_None;
@ -176,7 +193,9 @@ lad_write(lad_t *self, PyObject *args)
static PyObject *
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) {
close(self->x_fd);
self->x_fd = -1;
@ -188,50 +207,73 @@ lad_close(lad_t *self, PyObject *args)
static PyObject *
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);
}
static PyObject *
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",
&rate, &ssize, &nchannels, &fmt))
if (!PyArg_ParseTuple(args, "iiii|i:setparameters",
&rate, &ssize, &nchannels, &fmt, &emulate))
return NULL;
if (rate < 0 || ssize < 0 || (nchannels != 1 && nchannels != 2)) {
if (rate < 0) {
PyErr_Format(PyExc_ValueError, "expected rate >= 0, not %d",
rate);
return NULL;
}
if (ssize < 0) {
PyErr_Format(PyExc_ValueError, "expected sample size >= 0, not %d",
ssize);
return NULL;
}
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);
return NULL;
}
if (ioctl(self->x_fd, SOUND_PCM_WRITE_RATE, &rate) < 0) {
if (ioctl(self->x_fd, SNDCTL_DSP_CHANNELS, &nchannels) == -1) {
PyErr_SetFromErrno(LinuxAudioError);
return NULL;
}
if (ioctl(self->x_fd, SNDCTL_DSP_SAMPLESIZE, &ssize) < 0) {
PyErr_SetFromErrno(LinuxAudioError);
return NULL;
}
stereo = (nchannels == 1)? 0: (nchannels == 2)? 1: -1;
if (ioctl(self->x_fd, SNDCTL_DSP_STEREO, &stereo) < 0) {
PyErr_SetFromErrno(LinuxAudioError);
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)
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]) ||
audio_types[n].a_bps != ssize ||
(self->x_afmts & audio_types[n].a_fmt) == 0) {
PyErr_SetFromErrno(LinuxAudioError);
return NULL;
}
if (ioctl(self->x_fd, SNDCTL_DSP_SETFMT, &audio_types[n].a_fmt) < 0) {
if (emulate == 0) {
if ((self->x_afmts & audio_types[n].a_fmt) == 0) {
PyErr_Format(PyExc_ValueError,
"format not supported by device: %s",
audio_types[n].a_name);
return NULL;
}
}
if (ioctl(self->x_fd, SNDCTL_DSP_SETFMT,
&audio_types[n].a_fmt) == -1) {
PyErr_SetFromErrno(LinuxAudioError);
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
@ -342,7 +384,7 @@ lad_flush(lad_t *self, PyObject *args)
{
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);
return NULL;
}
@ -350,6 +392,26 @@ lad_flush(lad_t *self, PyObject *args)
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[] = {
{ "read", (PyCFunction)lad_read, METH_VARARGS },
{ "write", (PyCFunction)lad_write, METH_VARARGS },
@ -360,6 +422,7 @@ static PyMethodDef lad_methods[] = {
{ "flush", (PyCFunction)lad_flush, METH_VARARGS },
{ "close", (PyCFunction)lad_close, METH_VARARGS },
{ "fileno", (PyCFunction)lad_fileno, METH_VARARGS },
{ "getptr", (PyCFunction)lad_getptr, METH_VARARGS },
{ NULL, NULL} /* sentinel */
};
@ -398,50 +461,30 @@ static PyMethodDef linuxaudiodev_methods[] = {
void
initlinuxaudiodev(void)
{
PyObject *m, *d, *x;
PyObject *m;
m = Py_InitModule("linuxaudiodev", linuxaudiodev_methods);
d = PyModule_GetDict(m);
LinuxAudioError = PyErr_NewException("linuxaudiodev.error", NULL, NULL);
if (LinuxAudioError)
PyDict_SetItemString(d, "error", LinuxAudioError);
PyModule_AddObject(m, "error", LinuxAudioError);
x = PyInt_FromLong((long) AFMT_MU_LAW);
if (x == NULL || PyDict_SetItemString(d, "AFMT_MU_LAW", x) < 0)
goto error;
Py_DECREF(x);
if (PyModule_AddIntConstant(m, "AFMT_MU_LAW", (long)AFMT_MU_LAW) == -1)
return;
if (PyModule_AddIntConstant(m, "AFMT_A_LAW", (long)AFMT_A_LAW) == -1)
return;
if (PyModule_AddIntConstant(m, "AFMT_U8", (long)AFMT_U8) == -1)
return;
if (PyModule_AddIntConstant(m, "AFMT_S8", (long)AFMT_S8) == -1)
return;
if (PyModule_AddIntConstant(m, "AFMT_U16_BE", (long)AFMT_U16_BE) == -1)
return;
if (PyModule_AddIntConstant(m, "AFMT_U16_LE", (long)AFMT_U16_LE) == -1)
return;
if (PyModule_AddIntConstant(m, "AFMT_S16_BE", (long)AFMT_S16_BE) == -1)
return;
if (PyModule_AddIntConstant(m, "AFMT_S16_LE", (long)AFMT_S16_LE) == -1)
return;
x = PyInt_FromLong((long) AFMT_U8);
if (x == NULL || PyDict_SetItemString(d, "AFMT_U8", x) < 0)
goto error;
Py_DECREF(x);
x = PyInt_FromLong((long) AFMT_S8);
if (x == NULL || PyDict_SetItemString(d, "AFMT_S8", x) < 0)
goto error;
Py_DECREF(x);
x = PyInt_FromLong((long) AFMT_U16_BE);
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;
}