


AUDIO(4)         DEVICES AND NETWORK INTERFACES          AUDIO(4)



NAME
     audio - telephone quality audio device

CONFIG
     device-driver audio

AVAILABILITY
     This device is available with Desktop SPARCsystems only.

DESCRIPTION
     The audio device plays and records a single channel of sound
     using  the AM79C30A Digital Subscriber Controller chip.  The
     chip has a built-in analog to digital  converter  (ADC)  and
     digital  to analog converter (DAC) that can drive either the
     built-in speaker or an external headphone  jack,  selectable
     under  software control.  Digital audio data is sampled at a
     rate of 8000  samples  per  second  with  12-bit  precision,
     though  the  data is compressed, using _u-law encoding, to 8-
     bit samples.  The resulting audio data quality is equivalent
     to that of standard telephone service.

     The audio driver is implemented as  a  STREAMS  device.   In
     order  to  record  audio  input,  applications  open(2V) the
     /dev/audio device and read data from it using  the  read(2V)
     system  call.   Similarly, sound data is queued to the audio
     output port by using the write(2V) system call.

  Opening the Audio Device
     The audio device is treated as an exclusive  resource:  only
     one  process  may typically open the device at a time.  How-
     ever, two processes may simultaneously access the device  if
     one opens it read-only and the other opens it write-only.

     When a process cannot open /dev/audio because the  requested
     access mode is busy:
          o+  if the O_NDELAY flag is  set  in  the  open()  _f_l_a_g_s
             argument,  then  open() returns -1 immediately, with
             _e_r_r_n_o set to EBUSY.
          o+  if O_NDELAY is not set, then open() hangs until  the
             device  is available or a signal is delivered to the
             process, in which case open() returns -1 with  _e_r_r_n_o
             set to EINTR.

     Since the audio device grants exclusive read or write access
     to a single process at a time, long-lived audio applications
     may choose to close the  device  when  they  enter  an  idle
     state,  reopening  it  when  required.  The _p_l_a_y._w_a_i_t_i_n_g and
     _r_e_c_o_r_d._w_a_i_t_i_n_g flags in the audio information structure (see
     below)  provide  an  indication  that  another  process  has
     requested  access  to  the  device.   This  information   is
     advisory  only; background audio output processes, for exam-
     ple, may choose to  relinquish  the  audio  device  whenever



Sun Release 4.1    Last change: 7 October 1990                  1






AUDIO(4)         DEVICES AND NETWORK INTERFACES          AUDIO(4)



     another process requests write access.

  Recording Audio Data
     The read() system call copies data from the  system  buffers
     to  the  application.   Ordinarily,  read() blocks until the
     user buffer is filled.  The FIONREAD  ioctl  (see  filio(4))
     may be used to determine the amount of data that may be read
     without blocking.  The device may alternatively be set to  a
     non-blocking  mode,  in  which case read() completes immedi-
     ately, but may return fewer bytes than requested.  Refer  to
     the  read(2V) manual page for a complete description of this
     behavior.

     When the audio device is opened with read access, the device
     driver immediately starts buffering audio input data.  Since
     this consumes system resources, processes that do not record
     audio data should open the device write-only (O_WRONLY).

     The transfer of input data to STREAMS buffers may be  paused
     (or  resumed)  by  using  the AUDIO_SETINFO ioctl to set (or
     clear) the _r_e_c_o_r_d._p_a_u_s_e flag in the audio information struc-
     ture  (see  below).   All  unread  input data in the STREAMS
     queue may be discarded by using the  I_FLUSH  STREAMS  ioctl
     (see streamio(4)).

     Input data accumulates in STREAMS buffers at a rate of  8000
     bytes per second.  If the application that consumes the data
     cannot keep up with this data rate, the  STREAMS  queue  may
     become full.  When this occurs, the _r_e_c_o_r_d._e_r_r_o_r flag is set
     in the audio information structure and input sampling ceases
     until  there is room in the input queue for additional data.
     In such cases, the input  data  stream  contains  a  discon-
     tinuity.   For  this  reason,  audio  recording applications
     should open the audio device when they are prepared to begin
     reading data, rather than at the start of extensive initial-
     ization.

  Playing Audio Data
     The write() system call copies  data  from  an  applications
     buffer  to  the  STREAMS  output queue.  Ordinarily, write()
     blocks until the entire user  buffer  is  transferred.   The
     device  may  alternatively be set to a non-blocking mode, in
     which case  write()  completes  immediately,  but  may  have
     transferred fewer bytes than requested (see write(2V)).

     Although write()  returns  when  the  data  is  successfully
     queued,  the actual completion of audio output may take con-
     siderably longer.  The AUDIO_DRAIN ioctl may  be  issued  to
     allow an application to block until all of the queued output
     data has been played.  Alternatively, a process may  request
     asynchronous  notification of output completion by writing a
     zero-length  buffer  (end-of-file  record)  to  the   output



Sun Release 4.1    Last change: 7 October 1990                  2






AUDIO(4)         DEVICES AND NETWORK INTERFACES          AUDIO(4)



     stream.  When such a buffer has been processed, the _p_l_a_y._e_o_f
     flag in the  audio  information  structure  (see  below)  is
     incremented.

     The final close() of the file descriptor hangs  until  audio
     output has drained.  If a signal interrupts the close() , or
     if the process exits without closing the device, any remain-
     ing  data  queued for audio output is flushed and the device
     is closed immediately.

     The conversion of output data may be paused (or resumed)  by
     using   the  AUDIO_SETINFO  ioctl  to  set  (or  clear)  the
     _p_l_a_y._p_a_u_s_e flag in the audio information structure.   Queued
     output  data  may  be discarded by using the I_FLUSH STREAMS
     ioctl.

     Output data is played from the STREAMS buffers at a rate  of
     8000  bytes  per second.  If the output queue becomes empty,
     the _p_l_a_y._e_r_r_o_r flag is set in the audio  information  struc-
     ture and output ceases until additional data is written.

  Asynchronous I/O
     The I_SETSIG STREAMS ioctl may be used to  enable  asynchro-
     nous notification, via the SIGPOLL signal, of input and out-
     put  ready  conditions.   This,  in  conjunction  with  non-
     blocking read() and write() requests, is normally sufficient
     for applications to maintain an audio stream  in  the  back-
     ground.  Alternatively, asynchronous reads and writes may be
     initiated using the aioread(3) functions.

  Audio Data Encoding
     The data samples processed by the audio device  are  encoded
     in  8  bits.  The high-order bit is a sign bit: 1 represents
     positive data and 0 represents negative data.  The low-order
     7 bits represent signal magnitude and are inverted (1's com-
     plement).  The magnitude is encoded  according  to  a  _u-law
     transfer  function;   such  an encoding provides an improved
     signal-to-noise ratio at low amplitude levels.  In order  to
     achieve best results, the audio recording gain should be set
     so that typical amplitude levels  lie  within  approximately
     three-fourths of the full dynamic range.

  Audio Control Pseudo-Device
     It is sometimes convenient to have an application, such as a
     volume  control panel, modify certain characteristics of the
     audio device while it is being used by an unrelated process.
     The /dev/audioctl minor device is provided for this purpose.
     Any number of processes may  open  /dev/audioctl  simultane-
     ously.  However, read() and write() system calls are ignored
     by /dev/audioctl.  The AUDIO_GETINFO and AUDIO_SETINFO ioctl
     commands  may  be issued to /dev/audioctl in order to deter-
     mine the status or alter the behavior of /dev/audio.



Sun Release 4.1    Last change: 7 October 1990                  3






AUDIO(4)         DEVICES AND NETWORK INTERFACES          AUDIO(4)



  Audio Status Change Notification
     Applications that open the audio control  pseudo-device  may
     request asynchronous notification of changes in the state of
     the audio device by setting the S_MSG flag  in  an  I_SETSIG
     STREAMS ioctl.  Such processes receive a SIGPOLL signal when
     any of the following events occurs:
          o+    An AUDIO_SETINFO  ioctl  has  altered  the  device
               state.
          o+    An  input  overflow  or   output   underflow   has
               occurred.
          o+    An end-of-file  record  (zero-length  buffer)  has
               been processed on output.
          o+    An open() or close() of /dev/audio has altered the
               device state.

  Audio Information Structure
     The state of the audio device  may  be  polled  or  modified
     using  the  AUDIO_GETINFO  and AUDIO_SETINFO ioctl commands.
     These commands operate on the audio_info structure,  defined
     in <sun/audioio.h> as follows:

          /* Data encoding values, used below in the encoding field */
          #define AUDIO_ENCODING_ULAW   (1)  /* _u-law encoding */
          #define AUDIO_ENCODING_ALAW   (2)  /* A-law encoding */

          /* These ranges apply to record, play, and monitor gain values */
          #define AUDIO_MIN_GAIN   (0)       /* minimum gain value */
          #define AUDIO_MAX_GAIN   (255)          /* maximum gain value */

          /* Audio I/O channel status, used below in the audio_info structure */
          struct audio_prinfo {
               /* The following values describe the audio data encoding */
               unsigned            sample_rate;   /* samples per second */
               unsigned            channels; /* number of interleaved channels */
               unsigned            precision;     /* number of bits per sample */
               unsigned            encoding; /* data encoding method */

               /* The following values control audio device configuration */
               unsigned            gain;          /* gain level */
               unsigned            port;          /* selected I/O port */

               /* The following values describe the current device state */
               unsigned            samples;       /* number of samples converted */
               unsigned            eof;      /* End Of File counter (play only) */
               unsigned char       pause;         /* non-zero if paused, zero to resume */
               unsigned char       error;         /* non-zero if overflow/underflow */
               unsigned char       waiting;       /* non-zero if a process wants access */

               /* The following values are read-only device state flags */
               unsigned char       open;          /* non-zero if open access granted */
               unsigned char       active;        /* non-zero if I/O active */
          };



Sun Release 4.1    Last change: 7 October 1990                  4






AUDIO(4)         DEVICES AND NETWORK INTERFACES          AUDIO(4)



          /* This structure is used in AUDIO_GETINFO and AUDIO_SETINFO ioctl commands */
          typedef struct audio_info {
               struct audio_prinfo record;        /* input status information */
               struct audio_prinfo play;          /* output status information */
               unsigned            monitor_gain;  /* input to output mix */
          }  audio_info_t;

     The _p_l_a_y._g_a_i_n and _r_e_c_o_r_d._g_a_i_n fields specify the output  and
     input  volume  levels.   A value of AUDIO_MAX_GAIN indicates
     maximum gain.  The device also allows input data to be moni-
     tored  by  mixing  audio input onto the output channel.  The
     _m_o_n_i_t_o_r__g_a_i_n field controls the level of this feedback path.
     The  _p_l_a_y._p_o_r_t  field controls the output path for the audio
     device.   It  may  be  set  to   either   AUDIO_SPEAKER   or
     AUDIO_HEADPHONE  to direct output to the built-in speaker or
     the headphone jack, respectively.

     The _p_l_a_y._p_a_u_s_e and _r_e_c_o_r_d._p_a_u_s_e flags may be used  to  pause
     and resume the transfer of data between the audio device and
     the STREAMS buffers.  The _p_l_a_y._e_r_r_o_r and _r_e_c_o_r_d._e_r_r_o_r  flags
     indicate  that data underflow or overflow has occurred.  The
     _p_l_a_y._a_c_t_i_v_e  and  _r_e_c_o_r_d._a_c_t_i_v_e  flags  indicate  that  data
     transfer is currently active in the corresponding direction.

     The _p_l_a_y._o_p_e_n and _r_e_c_o_r_d._o_p_e_n flags indicate that the device
     is  currently open with the corresponding access permission.
     The _p_l_a_y._w_a_i_t_i_n_g and _r_e_c_o_r_d._w_a_i_t_i_n_g flags provide an indica-
     tion  that  a  process  may be waiting to access the device.
     These flags are set automatically when a process  blocks  on
     open(),  though they may also be set using the AUDIO_SETINFO
     ioctl command.  They are cleared only when a process  relin-
     quishes access by closing the device.

     The _p_l_a_y._s_a_m_p_l_e_s and _r_e_c_o_r_d._s_a_m_p_l_e_s fields are  initialized,
     at  open(), to zero and increment each time a data sample is
     copied to or from the associated  STREAMS  queue.   Applica-
     tions that keep track of the number of samples read or writ-
     ten may use these fields to determine exactly how many  sam-
     ples  remain  in  the  STREAMS  buffers.  The _p_l_a_y._e_o_f field
     increments whenever a zero-length output buffer is  synchro-
     nously processed.  Applications may use this field to detect
     the completion of particular segments of audio output.

     The _s_a_m_p_l_e__r_a_t_e, _c_h_a_n_n_e_l_s, _p_r_e_c_i_s_i_o_n,  and  _e_n_c_o_d_i_n_g  fields
     report the audio data format in use by the device.  For now,
     these values are read-only;  however,  future  audio  device
     implementations may support more than one data encoding for-
     mat, in which case applications  might  be  able  to  modify
     these fields.

  Filio and STREAMS IOCTLS




Sun Release 4.1    Last change: 7 October 1990                  5






AUDIO(4)         DEVICES AND NETWORK INTERFACES          AUDIO(4)



     All of the filio(4) and streamio(4) ioctl  commands  may  be
     issued for the /dev/audio device.  Because the /dev/audioctl
     device has its own STREAMS queues, most  of  these  commands
     neither  modify nor report the state of /dev/audio if issued
     for the /dev/audioctl device.  The  I_SETSIG  ioctl  may  be
     issued for /dev/audioctl to enable the notification of audio
     status changes, as described above.

  Audio IOCTLS
     The audio device additionally supports the  following  ioctl
     commands:

     AUDIO_DRAIN
          The argument is ignored.   This  command  suspends  the
          calling  process  until  the  output  STREAMS  queue is
          empty, or until a signal is delivered  to  the  calling
          process.  It may only be issued for the /dev/audio dev-
          ice.  An implicit AUDIO_DRAIN is performed on the final
          close() of /dev/audio.

     AUDIO_GETINFO
          The argument is a pointer to an  audio_info  structure.
          This  command  may  be  issued for either /dev/audio or
          /dev/audioctl.  The current  state  of  the  /dev/audio
          device is returned in the structure.

     AUDIO_SETINFO
          The argument is a pointer to an  audio_info  structure.
          This  command  may  be  issued for either /dev/audio or
          /dev/audioctl.  This command configures the audio  dev-
          ice  according to the structure supplied and overwrites
          the structure with the new state of the device.  [Note:
          The     _p_l_a_y._s_a_m_p_l_e_s,    _r_e_c_o_r_d._s_a_m_p_l_e_s,    _p_l_a_y._e_r_r_o_r,
          _r_e_c_o_r_d._e_r_r_o_r,  and  _p_l_a_y._e_o_f  fields  are  modified  to
          reflect  the state of the device when the AUDIO_SETINFO
          was issued.  This allows programs to atomically  modify
          these fields while retrieving the previous value.]

          Certain fields in the information  structure,  such  as
          the   _p_a_u_s_e   flags,  are  treated  as  read-only  when
          /dev/audio is not open with  the  corresponding  access
          permission.   Other fields, such as the gain levels and
          encoding information, may  have  a  restricted  set  of
          acceptable values.  Applications that attempt to modify
          such fields should check the returned values to be sure
          that the corresponding change took effect.

          Once set, the following values persist  through  subse-
          quent   open()   and   close()  calls  of  the  device:
          _p_l_a_y._g_a_i_n, _r_e_c_o_r_d._g_a_i_n,  _m_o_n_i_t_o_r__g_a_i_n,  _p_l_a_y._p_o_r_t,  and
          _r_e_c_o_r_d._p_o_r_t.    All  other  state  is  reset  when  the
          corresponding I/O stream of /dev/audio is closed.



Sun Release 4.1    Last change: 7 October 1990                  6






AUDIO(4)         DEVICES AND NETWORK INTERFACES          AUDIO(4)



          The audio_info structure may be initialized through the
          use  of  the AUDIO_INITINFO macro.  This macro sets all
          fields in the structure to values that are  ignored  by
          the AUDIO_SETINFO command.  For instance, the following
          code switches the output port from the built-in speaker
          to the headphone jack without modifying any other audio
          parameters:

               audio_info_t   info;

               AUDIO_INITINFO(&info);
               info.play.port = AUDIO_HEADPHONE;
               err = ioctl(audio_fd, AUDIO_SETINFO, &info);

          This technique is preferred over using  a  sequence  of
          AUDIO_GETINFO followed by AUDIO_SETINFO.

  Unsupported Device Control Features
     The AM79C30A chip is capable of a  performing  a  number  of
     functions  that  are  not  currently supported by the device
     driver, many of which were designed primarily for  telephony
     applications.   For  example,  the  chip can generate ringer
     tones and has a number of specialized filtering capabilities
     that  are  designed  to  compensate  for  different types of
     external speakers and microphones.

     Ordinarily, applications do not need to access  these  capa-
     bilities  and,  further, altering the chip's characteristics
     may interfere with its normal behavior.  However, knowledge-
     able  applications  may  use the unsupported AUDIOGETREG and
     AUDIOSETREG ioctl commands to read and write the chip regis-
     ters  directly.   The  description  of this interface may be
     found in <sbusdev/audio_79C30.h>.  Note: these commands  are
     supplied  for  prototyping  purposes  only  and  may  become
     obsolete in a future release of the audio driver.

FILES
     /dev/audio
     /dev/audioctl
     /usr/demo/SOUND

SEE ALSO
     ioctl(2),   poll(2),   read(2V),   write(2V),    aioread(3),
     filio(4), streamio(4)

     AMD data sheet for  the  AM79C30A  Digital  Subscriber  Con-
     troller, Publication number 09893.

BUGS
     Due to a _f_e_a_t_u_r_e of  the  STREAMS  implementation,  programs
     that are terminated or exit without closing the audio device
     may hang for a short period while audio output  drains.   In



Sun Release 4.1    Last change: 7 October 1990                  7






AUDIO(4)         DEVICES AND NETWORK INTERFACES          AUDIO(4)



     general, programs that produce audio output should catch the
     SIGINT signal and flush the output stream before exiting.

     The current driver implementation does not support the A-law
     encoding  mode of the AM79C30A chip.  Future implementations
     may  permit  the   AUDIO_SETINFO   ioctl   to   modify   the
     _p_l_a_y._e_n_c_o_d_i_n_g  and  _r_e_c_o_r_d._e_n_c_o_d_i_n_g  fields  of  the  device
     information structure to enable this mode.

FUTURE DIRECTIONS
     Workstation audio resources should be managed by a networked
     audio  server,  in  the  same  way that the video monitor is
     manipulated by a window system server.  For the time  being,
     we  encourage  you  to  write  your  programs  in  a modular
     fashion, isolating the audio device-specific  functions,  so
     that they may be easily ported to such an environment.







































Sun Release 4.1    Last change: 7 October 1990                  8



