[Python-Dev] Audio devices

Guido van Rossum guido@python.org
Tue, 11 Mar 2003 11:20:18 -0500


> On 11 March 2003, Guido van Rossum said:
> > Great!  I wonder if you have any thoughts on why running
> > test_ossaudiodev hangs when run on Linux Red Hat 7.3?  I'm currently
> > using a 2.4.18-24.7.x kernel.  I have no idea what other info would be
> > useful to debug this.

[Greg]
> The most obvious cause is that some other process has the audio device
> open, and your audio {hardware, device driver} only allows one at a
> time.

Hm, but I *do* hear some sound coming out of the speaker: a quiet,
sped-up squeaky version of the "nobody expects the spanish
inquisition" soundclip that test_linuxaudiodev also used to play.
(The latter now crashes for me with "linuxaudiodev.error: (0,
'Error')".)

> If you're running one of those newfangled GUI environments like KDE or
> GNOME, it's quite likely that the esound or aRTSd (however you spell it)
> daemon started when you logged in, and is thus blocking all access to
> your /dev/dsp.  This sucks, but IMHO it's not ossaudiodev's job to know
> about esound and similar.
> 
> One way to test this is to take your system down to single-user (or at
> least a console-only, no-X11 runlevel) and then try running
> test_ossaudiodev.

I tried this at runlevel 1, and the symptoms are identical: some
squeaks, then it hangs.

> Hmmm, it looks like calling open() with O_NONBLOCK helps.  I know this
> does *not* affect later read()/write() -- there's a special ioctl() for
> non-blocking read/write -- but it *does* appear to fix blocking open().
> At least for me it turned a second open() attempt on the same device
> from "hang" to "IOError: [Errno 16] Device or resource busy:
> '/dev/dsp2'".
> 
> Try this patch; if it works I'll check it in:
> 
> --- Modules/ossaudiodev.c       10 Mar 2003 03:17:06 -0000      1.24
> +++ Modules/ossaudiodev.c       11 Mar 2003 15:56:24 -0000
> @@ -131,7 +131,7 @@
>            basedev = "/dev/dsp";
>      }
>  
> -    if ((fd = open(basedev, imode)) == -1) {
> +    if ((fd = open(basedev, imode|O_NONBLOCK)) == -1) {
>          PyErr_SetFromErrnoWithFilename(PyExc_IOError, basedev);
>          return NULL;
>      }
> 
> test_ossaudiodev.py will still need fixing to handle the EBUSY error,
> but at least this should prevent hanging on open().

Yes, it fixes the hang.  Please check it in!

The sample is still played at too high a speed, but maybe that's
expected?

--Guido van Rossum (home page: http://www.python.org/~guido/)