Mon Apr 10 19:41:11 PDT 2006

audbuf library

overview
--------

The audbuf library allows the NC's audio hardware to be used in an
application-driven mode.  In this mode, the audio hardware is serviced
only when the audbuf library routines are called.  Thus, the calls
must be made frequently enough to avoid audio overflows (on microphone
input) and underflows (on headset output).

The audbuf library also buffers audio data between the user and the
hardware, allowing the user to access the data in arbitrary block
sizes.

Finally, the audbuf driver provides utility routines for getting and
setting the audio volume, detecting the presence of a headset, etc.


overflow and underflow
----------------------

There is a relationship between the calling frequency (dt, sec), the audio
sampling rate (Fs, samples/sec), and the buffer size (N, bytes).

If Fs is the input sample rate, then the following condition must be
met to avoid an input overflow --

    N > dt * Fs * (1 sample/4 bytes) / 2

Note that N must be a power of 2 between 32 and 4096, inclusive.  When
calling audbufOpen(), specify readSize = N.

If Fs is the output sample rate, then the following condition must be
met to avoid output underflow --

    N > dt * Fs * (1 sample/4 bytes)

Note that N must be a power of 2 between 32 and 4096, inclusive.  When
calling audbufWrite(), specify size = N.


data format
-----------

Audio data is always stereo (2 channels) with 16-bits/sample/channel.
Thus, there are 4 bytes of data for each sample time.  The data bytes
are layed out as follows --

 *    byte i*4+0 = sample i, left channel, high byte
 *    byte i*4+1 = sample i, left channel, low byte
 *    byte i*4+2 = sample i, right channel, high byte
 *    byte i*4+3 = sample i, right channel, low byte

Note that for microphone input, the left channel samples are the same
as the right channel samples.


usage
-----

First, open and configure the library by calling audbufOpen().  Use
the above calculations to compute readSize so as to avoid overflow.
Specify flags as O_RDONLY, O_WRONLY, or O_RDWR.  Set the input and
output sampling rates with the "config" parameter.  Determine the amount
of buffering you want between the hardware and your application (in
bytes), allocate the buffers, and supply them to the audbuf library
via the inbuf, inbufSize, outbuf, and outbufSize parameters.  Finally,
determine how you wish to deal with buffer overflows; you can specify
AUDBUF_OVERFLOW_DISCARD_NEW or AUDBUF_OVERFLOW_DISCARD_OLD for the
following parameters -- inOverflowConfig, outOverflowConfig.

After a successful audbufOpen(), you can perform audbufRead()'s and
audbufWrite()'s, provided that the open flags are consistent with the
read/write operation.  When performing audbufWrite(), be sure to
supply enough data to avoid an underflow (as detailed in the
"overflow" and "underflow" section).  Also, note that the data
supplied via "buf" is copied into an audbuf-internal buffer, so
"buf" can be reused by the application as soon as audbufWrite()
returns.

For convenience, the audbuf library provides routines for various
auxiliary audio functions --

* audbufSetVolume
* audbufGetVolume
* audbufGetHeadset

In addition, to help getting and setting the volume, the following
helper routines are provided --

* audbufGetVolumeMin
* audbufGetVolumeMax
* audbufGetVolumeMute

When all audio operations have been completed, call audbufClose()
to release system resources and shut down the audio hardware.
