[Python-Dev] Audio devices

Greg Ward gward@python.net
Tue, 11 Mar 2003 09:15:59 -0500


Back at work on the ossaudiodev docs for a few minutes.  Documenting an
API is always a great opportunity to clean it up, and the
ossaudiodev.open() function has a weird interface right now.  From the
current docs:

"""
open([device, ] mode)
    Open an audio device and return an OSS audio device object. This
    object supports many file-like methods, such as read(), write(), and
    fileno() (although there are subtle differences between conventional
    Unix read/write semantics and those of OSS audio devices). It also
    supports a number of audio-specific methods; see below for the
    complete list of methods.

    Note the unusual calling syntax: the first argument is optional, and
    the second is required. This is a historical artifact for
    compatibility with the older linuxaudiodev module which ossaudiodev
    supersedes.

    device is the audio device filename to use. If it is not specified,
    this module first looks in the environment variable AUDIODEV for a
    device to use. If not found, it falls back to /dev/dsp.

    mode is one of 'r' for read-only (record) access, 'w' for write-only
    (playback) access and 'rw' for both. Since many soundcards only
    allow one process to have the recorder or player open at a time it
    is a good idea to open the device only for the activity
    needed. Further, some soundcards are half-duplex: they can be opened
    for reading or writing, but not both at once.
"""

The historical background is that in linuxaudiodev prior to Python 2.3,
it was *impossible* to specify the device file to open -- you had to do
something like this:

  os.environ['AUDIODEV'] = "/dev/dsp2"
  dsp = linuxaudiodev.open("w")

Fixing that wart is what led me to create ossaudiodev in the first
place.  Cleaning up the remaining ugliness in ossaudiodev.open() brings
things nicely full-circle.  Anyways, since the module has been renamed,
who cares about backwards compatibility with linuxaudiodev?  I'd like to
change the open() interface to:

  open(device, mode)

where both are required.  (Most use of the audio device is for playback,
not recording.  But a default mode of "w" goes counter to expectations.
So I think 'mode' should be required.)

This would also mean getting rid of the $AUDIODEV check in
ossaudiodev.c.  Less C code is a good thing, unless of course it leads
to lots of redundant Python code all over the world.

Finally, for consistency I should also change openmixer() to require a
'device' argument (currently, it does the same thing, but hardcodes
"/dev/mixer" and checks $MIXERDEVICE).

Of course, this will lead people to hardcode "/dev/dsp" (and/or
"/dev/mixer") into their Python audio scripts.  That's bad if other
OSS-using operating systems have different names for the standard audio
devices.  Do they?

But it's certainly no *worse* than the situation for C programmers, who
have to assume "/dev/dsp" as a default -- the open(2) system call
certainly doesn't let you get away with leaving the filename out.  And
besides, "/dev/dsp" is already hard-coded into ossaudiodev.c, so if
that's inappropriate on certain operating systems, somebody's going to
lose already. 

Thoughts?

        Greg
-- 
Greg Ward <gward@python.net>                         http://www.gerg.ca/
Sure, I'm paranoid... but am I paranoid ENOUGH?