  Advanced Linux Sound Architecture - Library API
  From the original work of
      Jaroslav Kysela <perex@jcu.cz> with assistance from Alan Robinson

  QNX Neutrino Version 1.0.0

  This document describes, in full detail, the Advanced Linux Sound
  Architecture library API.
  ______________________________________________________________________

  Table of Contents:

  1.      Introduction

  2.      Error Codes

  2.1.    Error Codes in Detail

  2.2.    Functions

  2.2.1.  const char *snd_strerror( int errnum )

  3.      Control Interface

  3.1.    Low-Level Layer

  3.1.1.  int snd_cards( void )

  3.1.2.  unsigned int snd_cards_mask( void )

  3.1.3.  int snd_card_name( const char *string )

  3.1.4.  int snd_ctl_open( void **handle, int card )

  3.1.5.  int snd_ctl_close( void *handle )

  3.1.6.  int snd_ctl_file_descriptor( void *handle )

  3.1.7.  int snd_ctl_hw_info( void *handle, snd_ctl_hw_info_t *info )

  3.1.8.  int snd_ctl_switches( void *handle )

  3.1.9.  int snd_ctl_switch( void *handle, const char *switch_id )

  3.1.10.  int snd_ctl_switch_read( void *handle, int switchn, 
                                                      snd_switch_t *data )

  3.1.11. int snd_ctl_switch_write( void *handle, int switchn, 
                                                      snd_switch_t *data )

  3.1.12. int snd_ctl_mixer_info( void *handle, int dev, 
                                                  snd_mixer_info_t *info )

  3.1.13. int snd_ctl_mixer_switches( void *handle, int dev )

  3.1.14. int snd_ctl_mixer_switch( void *handle, int dev, 
                                                   const char *switch_id )

  3.1.15. int snd_ctl_mixer_switch_read( void *handle, int dev, 
                                         int switchn, snd_switch_t *data )

  3.1.16. int snd_ctl_mixer_switch_write( void *handle, int dev, 
                                         int switchn, snd_switch_t *data )

  3.1.17. int snd_ctl_pcm_info( void *handle, int dev, 
                                                    snd_pcm_info_t *info )

  3.1.18. int snd_ctl_pcm_playback_info( void *handle, int dev, 
                                           snd_pcm_playback_info_t *info )

  3.1.19. int snd_ctl_pcm_record_info( void *handle, int dev, 
                                             snd_pcm_record_info_t *info )

  3.1.20. int snd_ctl_pcm_playback_switches( void *handle, int dev )

  3.1.21. int snd_ctl_pcm_playback_switch( void *handle, int dev, 
                                                   const char *switch_id )

  3.1.22. int snd_ctl_pcm_playback_switch_read( void *handle, 
                                int dev, int switchn, snd_switch_t *data )

  3.1.23. int snd_ctl_pcm_playback_switch_write( void *handle, 
                                int dev, int switchn, snd_switch_t *data )

  3.1.24. int snd_ctl_pcm_record_switches( void *handle, int dev )

  3.1.25. int snd_ctl_pcm_record_switch( void *handle, int dev, 
                                                   const char *switch_id )

  3.1.26. int snd_ctl_pcm_record_switch_read( void *handle, int dev, 
                                         int switchn, snd_switch_t *data )

  3.1.27. int snd_ctl_pcm_record_switch_write( void *handle, int dev, 
                                         int switchn, snd_switch_t *data )

  3.1.28. int snd_ctl_rawmidi_info( void *handle, int dev, 
                                                snd_rawmidi_info_t *info )

  3.1.29. int snd_ctl_rawmidi_output_info( void *handle, int dev, 
                                         snd_rawmidi_output_info_t *info )

  3.1.30. int snd_ctl_rawmidi_input_info( void *handle, int dev, 
                                          snd_rawmidi_input_info_t *info )

  3.1.31. int snd_ctl_rawmidi_output_switches( void *handle, int dev )

  3.1.32. int snd_ctl_rawmidi_output_switch( void *handle, int dev, 
                                                   const char *switch_id )

  3.1.33. int snd_ctl_rawmidi_output_switch_read( void *handle, 
                                int dev, int switchn, snd_switch_t *data )

  3.1.34. int snd_ctl_rawmidi_output_switch_write( void *handle, 
                                int dev, int switchn, snd_switch_t *data )

  3.1.35. int snd_ctl_rawmidi_input_switches( void *handle, int dev )

  3.1.36. int snd_ctl_rawmidi_input_switch( void *handle, int dev, 
                                                   const char *switch_id )

  3.1.37. int snd_ctl_rawmidi_input_switch_read( void *handle, 
                                int dev, int switchn, snd_switch_t *data )

  3.1.38. int snd_ctl_rawmidi_input_switch_write( void *handle, 
                                int dev, int switchn, snd_switch_t *data )
 
  3.2.    Examples

  4.      Mixer Interface

  4.1.    Low-Level Layer

  4.1.1.  int snd_mixer_open( void **handle, int card, int device )

  4.1.2.  int snd_mixer_close( void *handle )

  4.1.3.  int snd_mixer_file_descriptor( void *handle )

  4.1.4.  int snd_mixer_channels( void *handle )

  4.1.5.  int snd_mixer_info( void *handle, snd_mixer_info_t *info )

  4.1.6.  int snd_mixer_exact_mode( void *handle, int enable )

  4.1.7.  int snd_mixer_channel( void *handle, const char *channel_id )

  4.1.8.  int snd_mixer_channel_info( void *handle, int channel, 
                                           snd_mixer_channel_info_t *info )

  4.1.9.  int snd_mixer_channel_read( void *handle, int channel, 
                                                snd_mixer_channel_t *data )

  4.1.10. int snd_mixer_channel_write( void *handle, int channel, 
                                                snd_mixer_channel_t *data )

  4.1.11. int snd_mixer_channel_playback_info( void *handle,
                    int channel, snd_mixer_channel_direction_info_t *info )  

  4.1.12. int snd_mixer_channel_record_info( void *handle,
                    int channel, snd_mixer_channel_direction_info_t *info )

  4.1.13. int snd_mixer_channel_playback_read( void *handle, 
                         int channel, snd_mixer_channel_direction_t *data )
  
  4.1.14. int snd_mixer_channel_record_read( void *handle, 
                         int channel, snd_mixer_channel_direction_t *data )
 
  4.1.15. int snd_mixer_channel_playback_write( void *handle, 
                         int channel, snd_mixer_channel_direction_t *data ) 

  4.1.16. int snd_mixer_channel_record_write( void *handle, 
                         int channel, snd_mixer_channel_direction_t *data ) 
  
  4.1.17. int snd_mixer_switches( void *handle )

  4.1.18. int snd_mixer_switch( void *handle, const char *switch_id ) 

  4.1.19. int snd_mixer_switch_read( void *handle, int switchn, 
                                                       snd_switch_t *data )

  4.1.20. int snd_mixer_switch_write( void *handle, int switchn, 
                                                       snd_switch_t *data )

  4.1.21. int snd_mixer_read( void *handle, 
                                         snd_mixer_callbacks_t *callbacks ) 

  4.2.    Examples

  5.      Digital Audio (PCM) Interface

  5.1.    Low-Level Layer

  5.1.1.  int snd_pcm_open( void **handle, int card, int device, int mode )

  5.1.2.  int snd_pcm_close( void *handle )

  5.1.3.  int snd_pcm_file_descriptor( void *handle )

  5.1.4.  int snd_pcm_block_mode( void *handle, int enable )

  5.1.5.  int snd_pcm_info( void *handle, snd_pcm_info_t *info )

  5.1.6.  int snd_pcm_playback_info( void *handle, 
                                           snd_pcm_playback_info_t *info )

  5.1.7.  int snd_pcm_record_info( void *handle, 
                                             snd_pcm_record_info_t *info )

  5.1.8.  int snd_pcm_playback_switches( void *handle )
 
  5.1.9.  int snd_pcm_playback_switch( void *handle, 
                                                   const char *switch_id )

  5.1.10. int snd_pcm_playback_switch_read( void *handle, 
                                         int switchn, snd_switch_t *data )

  5.1.11. int snd_pcm_playback_switch_write( void *handle, 
                                         int switchn, snd_switch_t *data )

  5.1.12.  int snd_pcm_record_switches( void *handle )
 
  5.1.13.  int snd_pcm_record_switch( void *handle, const char *switch_id )

  5.1.14. int snd_pcm_record_switch_read( void *handle, 
                                         int switchn, snd_switch_t *data )

  5.1.15. int snd_pcm_record_switch_write( void *handle, 
                                         int switchn, snd_switch_t *data )

  5.1.16. int snd_pcm_playback_format( void *handle, 
                                                snd_pcm_format_t *format )

  5.1.17. int snd_pcm_record_format( void *handle, 
                                                snd_pcm_format_t *format )

  5.1.18. int snd_pcm_playback_params( void *handle, 
                                       snd_pcm_playback_params_t *params )

  5.1.19. int snd_pcm_record_params( void *handle, 
                                         snd_pcm_record_params_t *params )

  5.1.20. int snd_pcm_playback_status( void *handle, 
                                       snd_pcm_playback_status_t *status )

  5.1.21. int snd_pcm_record_status( void *handle, 
                                         snd_pcm_record_status_t *status )

  5.1.22. int snd_pcm_drain_playback( void *handle )

  5.1.23. int snd_pcm_flush_playback( void *handle )

  5.1.24. int snd_pcm_flush_record( void *handle )

  5.1.25. int snd_pcm_playback_pause( void *handle, int enable )
 
  5.1.26. int snd_pcm_playback_time( void *handle, int enable )

  5.1.27. int snd_pcm_record_time( void *handle, int enable )

  5.1.28. int snd_pcm_reset_playback_time( void *handle, struct timeval *time )

  5.1.29. int snd_pcm_reset_record_time( void *handle, struct timeval *time )

  5.1.30. ssize_t snd_pcm_write( void *handle, const void *buffer, 
                                                            size_t size )

  5.1.31. ssize_t snd_pcm_read( void *handle, void *buffer, size_t size ) 

  5.2.    Examples

  5.3.    PCM Loopback Interface

  5.3.1.  int snd_pcm_loopback_open( void **handle, 
                                        int card, int device, int mode )

  5.3.2.  int snd_pcm_loopback_close( void *handle )

  5.3.3.  int snd_pcm_loopback_file_descriptor( void *handle )

  5.3.4.  int snd_pcm_loopback_block_mode( void *handle, int enable )

  5.3.5.  int snd_pcm_loopback_stream_mode( void *handle, int mode )

  5.3.6.  int snd_pcm_loopback_format( void *handle, snd_pcm_format_t *format ) 
                                              
  5.3.7.  ssize_t snd_pcm_loopback_read( void *handle, void *buffer, 
                                                            size_t size ) 

  6.      Raw MIDI Interface

  6.1.    Low Level Interface

  6.1.1.  int snd_rawmidi_open( void **handle, int card, 
                                                  int device, int mode )

  6.1.2.  int snd_rawmidi_close( void *handle )

  6.1.3.  int snd_rawmidi_file_descriptor( void *handle )

  6.1.4.  int snd_rawmidi_block_mode( void *handle, int enable )

  6.1.5.  int snd_rawmidi_info( void *handle, 
                                               snd_rawmidi_info_t *info )

  6.1.6.  int snd_rawmidi_output_info( void *handle, 
                                        snd_rawmidi_output_info_t *info )
 
  6.1.7.  int snd_rawmidi_input_info( void *handle, 
                                         snd_rawmidi_input_info_t *info )

  6.1.8.  int snd_rawmidi_output_switches( void *handle )

  6.1.9.  int snd_rawmidi_output_switch( void *handle, 
                                                  const char *switch_id )

  6.1.10. int snd_rawmidi_output_switch_read( void *handle, 
                                        int switchn, snd_switch_t *data )

  6.1.11. int snd_rawmidi_output_switch_write( void *handle, 
                                        int switchn, snd_switch_t *data )

  6.1.12. int snd_rawmidi_input_switches( void *handle )

  6.1.13. int snd_rawmidi_input_switch( void *handle, 
                                                  const char *switch_id )

  6.1.14. int snd_rawmidi_input_switch_read( void *handle, 
                                        int switchn, snd_switch_t *data )

  6.1.15. int snd_rawmidi_input_switch_write( void *handle, 
                                        int switchn, snd_switch_t *data )

  6.1.16. int snd_rawmidi_output_params( void *handle, 
                                    snd_rawmidi_output_params_t *params )

  6.1.17. int snd_rawmidi_input_params( void *handle, 
                                     snd_rawmidi_input_params_t *params )

  6.1.18. int snd_rawmidi_output_status( void *handle,  
                                    snd_rawmidi_output_status_t *status )

  6.1.19. int snd_rawmidi_input_status( void *handle, 
                                     snd_rawmidi_input_status_t *status )

  6.1.20. int snd_rawmidi_drain_output( void *handle )

  6.1.21. int snd_rawmidi_flush_output( void *handle )

  6.1.22. int snd_rawmidi_flush_input( void *handle )

  6.1.23. ssize_t snd_rawmidi_read( void *handle, void *buffer, size_t size )
                                                            
  6.1.24. ssize_t snd_rawmidi_write( void *handle, 
                                        const void *buffer, size_t size )

  6.2.    Examples

  7.      Timer Interface
  
  7.1.    Low Level Interface

  7.1.1.  int snd_timer_open( void **handle )  
                                                   
  7.1.2.  int snd_timer_close( void *handle )

  7.1.3.  int snd_timer_file_descriptor( void *handle )

  7.1.4.  int snd_timer_general_info( void *handle, 
                                         snd_timer_general_info_t *info )

  7.1.5.  int snd_timer_select( void *handle, snd_timer_select_t *tselect )

  7.1.6.  int snd_timer_info( void *handle, snd_timer_info_t *info )

  7.1.7.  int snd_timer_params( void *handle, snd_timer_params_t *params )

  7.1.11. int snd_timer_status( void *handle, snd_timer_status_t *status )

  7.1.8.  int snd_timer_start( void *handle )

  7.1.9.  int snd_timer_stop( void *handle )

  7.1.10. int snd_timer_continue( void *handle )

  7.1.12. ssize_t snd_timer_read( void *handle, void *buffer, size_t size )  
                                                            
  7.2.    Examples

  ______________________________________________________________________

  1.  Introduction

  The Advanced Linux Sound Architecture comes with a driver API &
  library API.  This document describes the library API and how it
  interfaces with the driver API.  The kernal API will probably never be
  documented in standalone form.

  Application programmers should use the library API rather than driver
  API.  The Library offers 100% of the functionally of the driver API,
  but add next major improvements in usability, making the application
  code simpler and better looking. In addition, some of the some
  fixes/compatibility code in, may be placed in the library code instead
  of the driver.

  For a complete list of all variables and functions in the API you
  should look at the following header files:

  1. /usr/include/sys/asoundlib.h

  2. /usr/include/linux/asound.h

  3. /usr/include/linux/asoundid.h

  2.  Error Codes

  All functions return int (or some sort of signed value). If this value
  is negative it represents an error code. Codes up to SND_ERROR_BEGIN
  (500000) represents standard system errors. Codes equal or greather
  than this value represents sound library API errors. All error codes
  begin with the prefix _SND_ERROR_.

  2.1.  Error Codes in Detail

     SND_ERROR_UNCOMPATIBLE_VERSION (500000)
        This error is caused if the driver uses an incompatible API
        for this interface and hence the library doesn't know how
        this API can be used.

  2.2. Functions

  2.2.1.

  const char *snd_strerror( int errnum )

  This functions converts error code to a string. Its functionality is
  the same as the strerror function from the standard C library, but
  this function returns correct strings for sound error codes, too.

  3.  Control Interface

  The control interfaces gives application various information about the
  currently installed sound driver in the system. The interface should
  be used to detect if another sound interface is present for selected
  soundcard or, for example, to create a list of devices (MIXER, PCM
  etc) from which the user can select.

  3.1.  Low-Level Layer

  3.1.1.

  int snd_cards( void )

  Returns the number of soundcards present in the system, if any.
  Otherwise it returns a negative value, which maps to an error code.
  This function will return 0 if no soundcards are detected.

  3.1.2.

  unsigned int snd_cards_mask( void )

  Returns the bitmap of soundcards present in the system, if any.
  Otherwise it returns a negative value, which maps to an error code.
  This function will return 0 if no soundcards are detected. First
  soundcard is represented with bit 0.

  3.1.3.

  int snd_card_name( const char *string )

  Returns the index of the sound card with the id of "string". If
  no card with that id is detected, then a negative value, which
  maps to an error code is returned.

  3.1.4.

  int snd_ctl_open( void **handle, int card )

  Creates a new handle and opens communication with the control
  interface for soundcard number "card" (0-N). The function also
  checks if protocol is compatible, so as to prevent the use of old
  programs with a new driver API. Function returns zero if successful,
  otherwise an error code is returned.

  3.1.5.

  int snd_ctl_close( void *handle )

  Function frees all resources allocated with control handle and closes
  the control interface. Function returns zero if successful, otherwise 
  it returns an error code.
  

  3.1.6.

  int snd_ctl_file_descriptor( void *handle )

  Function returns file descriptor for the control interface.
  This function should be used in very special cases.
  Function returns a negative error code if some error was encountered.

  3.1.7.

  int snd_ctl_hw_info( void *handle, snd_ctl_hw_info_t *info )

  Fills the info structure with data about the sound hardware referenced
  by handle. Function returns zero if successful, otherwise it returns
  an error code.

  ______________________________________________________________________

    struct snd_ctl_hw_info {
      unsigned int type;            /* type of card - see SND_CARD_TYPE_XXXX */
      unsigned int pcmdevs;         /* count of PCM devices (0 to N) */
      unsigned int mixerdevs;       /* count of MIXER devices (0 to N) */
      unsigned int mididevs;        /* count of raw MIDI devices (0 to N) */
      unsigned int timerdevs;       /* count of timer devices (0 to N) */
      char id[16];                  /* ID of card (user selectable) */
      char abbreviation[16];        /* Abbreviation for soundcard */
      char name[32];                /* Short name of soundcard */
      char longname[80];            /* name + info text about soundcard */
      unsigned int switches;        /* count of switches */
      unsigned char reserved[128];  /* reserved for future use */
    };

  ______________________________________________________________________

  3.1.8

  int snd_ctl_switches( void *handle )

  Returns the number of control switches.  In this context, 'switch'
  means a universal control interface between the driver and application
  which allows various types of control.  Function returns count if 
  successfull, otherwise it returns an error code.  Return value should
  be zero if the soundcard doesn't have any control switches.

  3.1.9

  int snd_ctl_switch( void *handle, const char *switch_id )

  Returns the index for the switch with the name switch_id. This function
  returns the switch's index if successfull, otherwise it returns an
  error.

  ______________________________________________________________________

  /* well known (named) switches */
  #define SND_CTL_SW_JOYSTICK             "Joystick"
  #define SND_CTL_SW_JOYSTICK_ADDRESS     "Joystick Address"
  #define SND_CTL_SW_JOYSTICK_SPEED       "Joystick Speed Compensation"

  ______________________________________________________________________

  3.1.10

  snd_ctl_switch_read( void *handle, int switchn, snd_switch_t *data )

  Fills the "*data" structure with data about the switch with index 
  "switchn".  This function returns zero if successfull, otherwise it
  returns an error code.

  ______________________________________________________________________

  /*
   *  Universal switch interface
   */

  #define SND_SW_TYPE_NONE       0       /* invalid */
  #define SND_SW_TYPE_BOOLEAN    1       /* 0 or 1 (enable) */
  #define SND_SW_TYPE_BYTE       2       /* 0 to 255 (low to high) */
  #define SND_SW_TYPE_WORD       3       /* 0 to 65535 (low to high) */
  #define SND_SW_TYPE_DWORD      4       /* 0 to 4294967296 (low to high) */
  #define SND_SW_TYPE_LIST       5       /* list type */
  #define SND_SW_TYPE_LIST_ITEM  6       /* list item */
  #define SND_SW_TYPE_USER       (~0)    /* user type */

  struct snd_switch_list_item {
          unsigned char name[32];
  };

  struct snd_switch_list {
          int switches_size;                 /* size of switches in array */
          int switches;                      /* filled switches in array */
          int switches_over;                 /* missing switches in array */
          snd_switch_list_item_t *pswitches; /* pointer to list item array */
  };

  struct snd_switch {
          int switchn;
          unsigned char name[32]; /* unique identification of switch (from driver) */
          unsigned int type;      /* look to SND_SW_TYPE_* */
          unsigned int low;       /* low range value */
          unsigned int high;      /* high range value */
          union {
                  unsigned int enable: 1;         /* 0 = off, 1 = on */
                  unsigned char data8[32];        /* 8-bit data */
                  unsigned short data16[16];      /* 16-bit data */
                  unsigned int data32[8];         /* 32-bit data */
                  int item_number;                /* active list item number */
                  char item[32];                  /* list item, low=high -> item number */
          } value;
          unsigned char reserved[32];
  };

  ______________________________________________________________________

  3.1.11

  snd_ctl_switch_write( void*handle, int switchn, snd_switch_t *data )

  Writes the "*data" structure with data concerning the switch with index
  "switchn" to the driver. This function returns zero if successful, 
  otherwise it returns an error code.

  Note: See snd_ctl_switch_read() for more information on the switches
        interface in general.

  3.1.12

  int snd_ctl_mixer_info( void *handle, int dev, snd_mixer_info_t *info )

  Fills the "*info" structure with data about the mixer device. Returns
  zero if successful, otherwise it returns an error code. Details about
  the snd_mixer_info_t structure are in the Mixer Interface section.
  The argument "dev" specifies the device number for the appropriate
  soundcard. Its range is 0 to N where N found from struct
  snd_ctl_hw_info -> mixerdevs - 1.  It should be used to collect
  information about mixer devices.

  3.1.13.
  
  int snd_ctl_mixer_switches( void *handle, int dev )

  Returns count of mixer switches.  In this context 'switch' means 
  universal control interface between driver and application which 
  allows various types control.
  This function returns count if successful, otherwise it returns an error
  code.  Return value should be zero if soundcard doesn't have any 
  mixer switches.

  3.1.14.

  int snd_ctl_mixer_switch( void *handle, int dev,
						 const char *switch_id )

  Returns the index for the switch with the name "switch_id". 
  This function returns switch index if successful, otherwise it returns
  an error code.

  3.1.15.

  int snd_ctl_mixer_switch_read( void *handle, int dev,
                                        int switchn, snd_switch_t *data )
 
  Fills the "*data" structure with data from the switch with index "switchn".
  This function returns zero if successful, otherwise it returns an error
  code.

  Note: See snd_ctl_switch_read() for more information on the switches
        interface in general.

  3.1.16.

  int snd_ctl_mixer_switch_write( void *handle, int dev, int switchn, 
                                                          snd_switch_t *data )

  Writes the "*data" structure to the switch with index "switchn".
  This function returns zero if successful, otherwise it returns an error
  code.

  Note: See snd_ctl_switch_read() for more information on the switches
        interface in general.

  3.1.17.

  int snd_ctl_pcm_info( void *handle, int dev, snd_pcm_info_t *info )

  Fills the *info structure with data about the PCM device. Function
  returns zero if successful, otherwise it returns an error code.
  Details about the snd_pcm_info_t structure are in the Digital Audio
  (PCM) Interface section. The argument "dev" selects the device number
  for the soundcard referenced by "*handle". Its range is 0 to N where N
  is struct snd_ctl_hw_info -> pcmdevs - 1. This function will work if
  the selected PCM device is busy, too. It should be used to collect
  information about PCM devices without exclusive lock.

  3.1.18.

  int snd_ctl_pcm_playback_info( void *handle, int dev, 
                                           snd_pcm_playback_info_t *info )

  Fills the *info structure with data about the PCM device and playback
  direction.  Function returns zero if successful, otherwise it returns
  an error code.  Details about the snd_pcm_playback_info_t structure
  are in the Digital Audio (PCM) Interface section. The argument "dev"
  selects the device number for the soundcard referenced by "*handle". Its
  range is 0 to N where N is struct snd_ctl_hw_info -> pcmdevs - 1.
  This function will work if the selected PCM device is busy, too. It
  should be used to collect information about PCM devices without
  exclusive lock.

  3.1.19.

  int snd_ctl_pcm_record_info( void *handle, int dev, 
                                             snd_pcm_record_info_t *info )

  Fills the *info structure with data about the PCM device and record
  direction.  Function returns zero if successful, otherwise it returns
  an error code.  Details about the snd_pcm_record_info_t structure are
  in the Digital Audio (PCM) Interface section. The argument "dev" selects
  the device number for the soundcard referenced by "*handle". Its range
  is 0 to N where N is struct snd_ctl_hw_info -> pcmdevs - 1.  This
  function will work if the selected PCM device is busy, too. It should
  be used to collect information about PCM devices without exclusive
  lock.

  3.1.20.

  int snd_ctl_pcm_playback_switches( void *handle, int dev )

  Returns count of PCM playback switches.  In this context 'switch' means
  universal control interface between driver and application which
  allows various types control.
  This function returns count if successful, otherwise it returns an error
  code.  Return value should be zero if soundcard doesn't have any
  mixer switches.

  3.1.21. 

  int snd_ctl_pcm_playback_switch( void *handle, int dev, 
                                                  const char *switch_id )

  Returns the index for the PCM playback switch with the name "switch_id". 
  This function returns the switch's index if successfull, otherwise it 
  returns an error.

  3.1.22.

  int snd_ctl_pcm_playback_switch_read( void *handle, int dev, 
                                        int switchn, snd_switch_t *data )

  Fills the "*data" structure with data from the PCM playback switch 
  with index "switchn".  This function returns zero if successful, 
  otherwise it returns an error code.

  Note: See snd_ctl_switch_read() for more information on the switches
        interface in general. 

  3.1.23.

  int snd_ctl_pcm_playback_switch_write( void *handle, int dev, 
                                       int switchn, snd_switch_t *data )

  Writes the "*data" structure to the PCM playback switch with 
  index "switchn".
  This function returns zero if successful, otherwise it returns an error
  code.

  Note: See snd_ctl_switch_read() for more information on the switches
        interface in general.

  3.1.24.

  int snd_ctl_pcm_record_switches( void *handle, int dev )

  Returns count of PCM recording switches.  In this context 'switch' means
  universal control interface between driver and application which
  allows various types control.
  This function returns count if successful, otherwise it returns an error
  code.  Return value should be zero if soundcard doesn't have any
  mixer switches.

  3.1.25.

  int snd_ctl_pcm_record_switch( void *handle, int dev, 
                                                    const char *switch_id )

  Returns the index for the PCM recording switch with the name "switch_id". 
  This function returns the switch's index if successfull, otherwise it 
  returns an error.

  3.1.26.

  int snd_ctl_pcm_record_switch_read( void *handle, int dev, 
                                        int switchn, snd_switch_t *data )

  Fills the "*data" structure with data from the PCM recording switch 
  with index "switchn".
  This function returns zero if successful, otherwise it returns an error
  code.

  Note: See snd_ctl_switch_read() for more information on the switches
        interface in general.

  3.1.27.

  int snd_ctl_pcm_record_switch_write( void *handle, int dev, 
                                        int switchn, snd_switch_t *data )

  Writes the "*data" structure to the PCM recording switch with 
  index "switchn".
  This function returns zero if successful, otherwise it returns an error
  code.

  Note: See snd_ctl_switch_read() for more information on the switches
        interface in general.

  3.1.28.

  int snd_ctl_rawmidi_info( void *handle, int dev, 
                                              snd_rawmidi_info_t *info )

  Fills the "*info" structure with data about the raw MIDI device. 
  Function returns zero if successful, otherwise it returns an 
  error code. Details about the snd_rawmidi_info_t structure are 
  in the RawMidi Interface section. 
  The argument "dev" selects the device number for the sound card 
  referenced by "*handle". Its range is 0 to N where N is 
  struct snd_ctl_hw_info->mididevs-1. This function will work 
  even if the selected rawmidi device is busy. It should be used 
  to collect information about rawmidi devices without an exclusive lock.

  3.1.29.

  int snd_ctl_rawmidi_output_info( void *handle, int dev, 
                                       snd_rawmidi_output_info_t *info )

  Fills the "*info" structure with data about the raw MIDI device and output
  direction. Function returns zero if successful, otherwise it returns an 
  error code. Details about the snd_rawmidi_output_info_t structure are in 
  the RawMidi Interface section. The argument "dev" selects the device number 
  for the sound card referenced by "*handle". Its range is 0 to N where N 
  is struct snd_ctl_hw_info->mididevs-1. This function will work if the 
  selected rawmidi device is busy, too. It should be used to collect
  information about rawmidi devices without an exclusive lock.

  3.1.30.

  int snd_ctl_rawmidi_input_info( void *handle, int dev, 
                                        snd_rawmidi_input_info_t *info )

  Fills the "*info" structure with data about the raw MIDI device and input
  direction. Function returns zero if successful, otherwise it returns an 
  error code. Details about the snd_rawmidi_input_info_t structure are in 
  the RawMidi Interface section. The argument "dev" selects the device number 
  for the sound card referenced by "*handle". Its range is 0 to N where N 
  is struct snd_ctl_hw_info->mididevs-1. This function will work if the 
  selected rawmidi device is busy, too. It should be used to collect
  information about rawmidi devices without an exclusive lock.

  3.1.31.

  int snd_ctl_rawmidi_output_switches( void *handle, int dev )

  Returns count of mixer switches.  In this context 'switch' means
  universal control interface between driver and application which
  allows various types control.
  This function returns count if successful, otherwise it returns an error
  code.  Return value should be zero if soundcard doesn't have any
  mixer switches.

  3.1.32.

  int snd_ctl_rawmidi_output_switch( void *handle, int dev, 
                                                    const char *switch_id )

  Returns the index for the switch with the name "switch_id". This function
  returns the switch's index if successfull, otherwise it returns an
  error.

  3.1.33.

  int snd_ctl_rawmidi_output_switch_read( void *handle, int dev, 
                                          int switchn, snd_switch_t *data )

  Fills the "*data" structure with data from the switch with index "switchn".
  This function returns zero if successful, otherwise it returns an error
  code.

  Note: See snd_ctl_switch_read() for more information on the switches
        interface in general.

  3.1.34.

  int snd_ctl_rawmidi_output_switch_write( void *handle, int dev, 
                                         int switchn, snd_switch_t *data )

  Writes the "*data" structure to the switch with index "switchn".
  This function returns zero if successful, otherwise it returns an error
  code.

  Note: See snd_ctl_switch_read() for more information on the switches
        interface in general.

  3.1.35.

  int snd_ctl_rawmidi_input_switches( void *handle, int dev )

  Returns count of mixer switches.  In this context 'switch' means
  universal control interface between driver and application which
  allows various types control.
  This function returns count if successful, otherwise it returns an error
  code.  Return value should be zero if soundcard doesn't have any
  mixer switches.

  3.1.36.

  int snd_ctl_rawmidi_input_switch( void *handle, int dev, 
                                                    const char *switch_id )

  Returns the index for the switch with the name "switch_id". This function
  returns the switch's index if successfull, otherwise it returns an
  error.

  3.1.37.

  int snd_ctl_rawmidi_input_switch_read( void *handle, int dev, 
                                           int switchn, snd_switch_t *data )

  Fills the "*data" structure with data from the switch with index "switchn".
  This function returns zero if successful, otherwise it returns an error
  code.

  Note: See snd_ctl_switch_read() for more information on the switches
        interface in general.

  3.1.38.

  int snd_ctl_rawmidi_input_switch_write( void *handle, int dev, 
                                           int switchn, snd_switch_t *data )

  Writes the "*data" structure to the switch with index "switchn".
  This function returns zero if successful, otherwise it returns an error
  code.

  Note: See snd_ctl_switch_read() for more information on the switches
        interface in general.

  3.2.  Examples

  The following example shows how all PCM devices can be detected for
  the first soundcard (#0) in the system.

       ______________________________________________________________________

       int card = 0, err;
       void *handle;
       snd_ctl_hw_info_t info;

       if ( (err = snd_ctl_open( &handle, card )) < 0 ) {
         fprintf( stderr, "open failed: %s\n", snd_strerror( err ) );
         return;
       }
       if ( (err = snd_ctl_hw_info( handle, &info )) < 0 ) {
         fprintf( stderr, "hw info failed: %s\n", snd_strerror( err ) );
         snd_ctl_close( handle );
         return;
       }
       printf( "Installed PCM devices for card \#%i: %i\n", card + 1, 
                                                            info.pcmdevs );
       snd_ctl_close( handle );

       ______________________________________________________________________

  4.  Mixer Interface

  The Mixer Interface allows applications to change the volume level of
  a soundcard's input/output channels in both the linear range (0-100)
  and in decibels. It also supports features like hardware mute, input
  sound source, etc.

  4.1.  Low-Level Layer

  Mixer devices aren't opened exclusively. This allows applications to
  open a device multiple times with one or more processes.

  4.1.1.

  int snd_mixer_open( void **handle, int card, int device )

  Creates new handle and opens a connection to the mixer interface
  for soundcard number "card" (0-N) and mixer device number "device".  
  Also checks if protocol is compatible to prevent use of old
  programs with new driver API. Function returns zero if successful,
  otherwise it returns an error code.

  4.1.2.

  int snd_mixer_close( void *handle )

  Frees all resources allocated to the mixer handle and closes its
  connection to the mixer interface. Function returns zero if
  successful, otherwise it returns an error code.

  4.1.3.

  int snd_mixer_file_descriptor( void *handle )

  Returns the file descriptor for the connection to the mixer
  interface. This function should be used only in very special
  cases. Function returns a negative error code if an error was
  encountered.

  The file descriptor should be used for the select() synchronous
  multiplexer function for determining read direction. Applications
  should call snd_mixer_read() function if some data is waiting to be
  read.  It is recommended that you do this, since it leaves place for
  this function to handle some new driver API specifications.

  4.1.4.

  int snd_mixer_channels( void *handle )

  Returns the count of mixer channels for appropriate mixer device,
  otherwise the return value is negative, and signifies an error code.
  Never returns zero.

  4.1.5.

  int snd_mixer_info( void *handle, snd_mixer_info_t *info )

  Fills the "*info" structure with information about the mixer associated
  with "*handle". Returns zero if successful, otherwise it returns an
  error code.

  ______________________________________________________________________
    /* Channel general capabilities -- SND_MIXER_INFO_CAP_XXXX */
    /* No SND_MIXER_INFO_CAP_XXXX flags currently defined */

    struct snd_mixer_info {
      unsigned int type;        /* type of soundcard, see SND_CARD_TYPE_XXXX */
      unsigned int channels;    /* count of mixer devices */
      unsigned int caps;        /* capabilities, see SND_MIXER_INFO_CAP_XXXX */
      unsigned char id[32];     /* ID of this mixer */
      unsigned char name[80];   /* name of this device */
      unsigned int switches;    /* count of switches */
      char reserved[28];        /* reserved for future use */
    };

  ______________________________________________________________________

  4.1.6.

  int snd_mixer_exact_mode( void *handle, int enable )

  Turns on or off (by default) exact mode. This mode allows to
  application set/get volume values in exact range which uses hardware.
  In non-exact mode is range always from 0 to 100 and conversion to
  hardware range does driver. Function returns zero if successful,
  otherwise it returns an error code.


  4.1.7.

  int snd_mixer_channel( void *handle, const char *channel_id )

  Returns the channel number (index) associated with "*channel_id" (channel
  name), or returns an error code.

  ______________________________________________________________________

    #define SND_MIXER_ID_MASTER             "Master"
    #define SND_MIXER_ID_MASTER1            "Master 1"
    #define SND_MIXER_ID_MASTERD            "Master D"
    #define SND_MIXER_ID_MASTERD1           "Master D1"
    #define SND_MIXER_ID_HEADPHONE          "Headphone"
    #define SND_MIXER_ID_MASTER_MONO        "Master Mono"
    #define SND_MIXER_ID_3D                 "3D Wide"
    #define SND_MIXER_ID_3D_VOLUME          "3D Volume"
    #define SND_MIXER_ID_3D_CENTER          "3D Center"
    #define SND_MIXER_ID_3D_SPACE           "3D Space"
    #define SND_MIXER_ID_3D_DEPTH           "3D Depth"
    #define SND_MIXER_ID_BASS               "Bass"
    #define SND_MIXER_ID_TREBLE             "Treble"
    #define SND_MIXER_ID_FADER              "Fader"
    #define SND_MIXER_ID_SYNTHESIZER        "Synth"
    #define SND_MIXER_ID_SYNTHESIZER1       "Synth 1"
    #define SND_MIXER_ID_FM                 "FM"
    #define SND_MIXER_ID_EFFECT             "Effect"
    #define SND_MIXER_ID_DSP                "DSP"
    #define SND_MIXER_ID_PCM                "PCM"
    #define SND_MIXER_ID_PCM1               "PCM 1"
    #define SND_MIXER_ID_LINE               "Line-In"
    #define SND_MIXER_ID_MIC                "MIC"
    #define SND_MIXER_ID_CD                 "CD"
    #define SND_MIXER_ID_VIDEO              "Video"
    #define SND_MIXER_ID_PHONE              "Phone"
    #define SND_MIXER_ID_GAIN               "Record-Gain"
    #define SND_MIXER_ID_MIC_GAIN           "Mic-Gain"
    #define SND_MIXER_ID_IGAIN              "In-Gain"
    #define SND_MIXER_ID_OGAIN              "Out-Gain"
    #define SND_MIXER_ID_LOOPBACK           "Loopback"
    #define SND_MIXER_ID_SPEAKER            "PC Speaker"
    #define SND_MIXER_ID_MONO               "Mono"
    #define SND_MIXER_ID_MONO1              "Mono 1"
    #define SND_MIXER_ID_MONO2              "Mono 2"
    #define SND_MIXER_ID_AUXA               "Aux A"
    #define SND_MIXER_ID_AUXB               "Aux B"
    #define SND_MIXER_ID_AUXC               "Aux C"

  ______________________________________________________________________

  4.1.8.

  int snd_mixer_channel_info( void *handle, int channel, 
                                          snd_mixer_channel_info_t *info )
  
  Fills the "*info" structure. The argument "channel" specifies channel 
  (0 to N) for which is the info requested. Function returns zero if
  successful, otherwise it returns an error code.

  ______________________________________________________________________
    /* Channel general capabilities -- SND_MIXER_CINFO_CAP_XXXX */
    #define SND_MIXER_CINFO_CAP_OUTPUT   0x00000001 /* has output direction */
    #define SND_MIXER_CINFO_CAP_INPUT    0x00000002 /* has input direction */
    #define SND_MIXER_CINFO_CAP_EXTINPUT 0x00000010 /* external input channel */
    #define SND_MIXER_CINFO_CAP_EFFECT   0x00000020 /* effect channel */

    struct snd_mixer_channel_info {
      unsigned int channel;   /* channel # (filled by application) */
      unsigned int parent;    /* parent channel # or SND_MIXER_PARENT */
      unsigned char name[12]; /* name of this device */
      unsigned int caps;      /* capabilities, see SND_MIXER_CINFO_CAP_XXXX */
      unsigned char reserved[20];
    };

  ______________________________________________________________________

  4.1.9.

  int snd_mixer_channel_read( void *handle, int channel, 
                                                snd_mixer_channel_t *data )

  Fills the "*data" structure. The argument "channel" specifies the channel
  (0 to N) for which is data requested. Function returns zero if
  successful, otherwise it returns an error code.

  ______________________________________________________________________
    /* Channel general flags -- SND_MIXER_FLG_XXXX */
    /* No SND_MIXER_FLG_XXXX flags currently defined */

    struct snd_mixer_channel {
      unsigned int channel;   /* channel # (filled by application) */
      unsigned int flags;     /* flags, see SND_MIXER_FLG_XXXX */
      unsigned char reserved[16];
    };

  ______________________________________________________________________

  4.1.10.

  int snd_mixer_channel_write( void *handle, int channel, 
                                                snd_mixer_channel_t *data )

  Writes the "*data" structure to driver. The "channel" argument specifies
  the channel (0 to N) for which is data is to be applied.  Function
  returns zero if successful, otherwise it returns an error code.  This
  functions is the opposite of snd_mixer_channel_read().

  Note: see snd_mixer_channel_read() for more info.

  4.1.11.

  int snd_mixer_channel_playback_info( void *handle, int channel, 
                                 snd_mixer_channel_direction_info_t *info )
 
  Fills the "*info" structure. The argument "channel" specifies channel
  (0 to N) for which is the info requested. Function returns zero if
  successful, otherwise it returns an error code.

  ______________________________________________________________________
    /* Channel direction dependant capabilities (bits 0-15) 
                                              -- SND_MIXER_CINFO_DCAP_XXXX */
    #define SND_MIXER_CINFO_DCAP_VOLUME    0x00000001 
                        /* volume setup is available */
    #define SND_MIXER_CINFO_DCAP_STEREO    0x00000002
    #define SND_MIXER_CINFO_DCAP_MUTE      0x00000010 
                        /* always set at this moment, driver emulates mute */
    #define SND_MIXER_CINFO_DCAP_HWMUTE    0x00000020 
                        /* direction supports hardware mute */
    #define SND_MIXER_CINFO_DCAP_JOINMUTE  0x00000040 i
                        /* join mute is supported only (left and right channel 
                           doesn't have separate mute control) */
    #define SND_MIXER_CINFO_DCAP_NOMUTEALL 0x00000080 
                       /* at least one of these channels must be unmuted */
    #define SND_MIXER_CINFO_DCAP_EXCLUSIVE 0x00000100 
                       /* exclude all others */
    #define SND_MIXER_CINFO_DCAP_ROUTE     0x00000200 
                       /* route left to right and/or vice versa is supported */
    #define SND_MIXER_CINFO_DCAP_SWAPROUTE 0x00000400 
                       /* route is only swap */
    #define SND_MIXER_CINFO_DCAP_DIGITAL   0x00001000 
                       /* direction does digital (not analog) mixing */

    /* Output direction only capabilities (bits 16-31) 
                                               -- SND_MIXER_CINFO_DCAP_XXXX */
    /* No SND_MIXER_CINFO_DCAP_XXXX output only (bits 16-31) flags 
                                               currently defined */

    struct snd_mixer_channel_direction_info {
      unsigned int channel;   /* channel # (filled by application) */
      unsigned int parent;    /* parent channel # or SND_MIXER_PARENT */
      unsigned int caps;      /* capabilities, see SND_MIXER_CINFO_DCAP_XXXX */
      int min;                /* min. value when exact mode (or always 0) */
      int max;                /* max. value when exact mode (or always 100) */
      int min_dB;             /* minimum decibel value (*100) */
      int max_dB;             /* maximum decibel value (*100) */
      unsigned char reserved[16];
    };

  ______________________________________________________________________

  4.1.12.

  int snd_mixer_channel_record_info( void *handle, int channel, 
                                 snd_mixer_channel_direction_info_t *info )
 
  Fills the "*info" structure. The argument "channel" specifies channel
  (0 to N) for which is the info requested. Function returns zero if
  successful, otherwise it returns an error code.

  Note: see snd_mixer_channel_playback_info() for more information.
  ______________________________________________________________________

     /* Input direction only capabilities (bits 16-31) 
                                               -- SND_MIXER_CINFO_DCAP_XXXX */
    #define SND_MIXER_CINFO_DCAP_RECORDBYMUTE 0x00010000 
                       /* we can record data even if output path is 
                          muted (to avoid loopback) */

 ______________________________________________________________________
 
  4.1.13.

  int snd_mixer_channel_playback_read( void *handle, int channel, 
                                     snd_mixer_channel_direction_t *data )
 
  Fills the "*data" structure. The argument "channel" specifies channel
  (0 to N) for which is the info requested. Function returns zero if
  successful, otherwise it returns an error code.
 
  ______________________________________________________________________
    /* Channel direction dependant flags (bits 0-15) -- SND_MIXER_DFLG_XXXX */
    #define SND_MIXER_DFLG_FORCE      0x00000001 /* force set - don't use */
    #define SND_MIXER_DFLG_DECIBEL    0x00000002 
                   /* if this bit is set, driver sets volume from 
                      dB variables (left_dB, right_dB) */
    #define SND_MIXER_DFLG_MUTE_LEFT  0x00000010
    #define SND_MIXER_DFLG_MUTE_RIGHT 0x00000020
    #define SND_MIXER_DFLG_MUTE       0x00000030
    #define SND_MIXER_DFLG_LTOR       0x00010000 /* route left to right */
    #define SND_MIXER_DFLG_RTOL       0x00020000 /* route right to left */
    #define SND_MIXER_DFLG_SWAP       0x00030000 /* swap left & right */

    /* Output direction only flags (bits 16-31) -- SND_MIXER_DFLG_XXXX */
    /* No SND_MIXER_DFLG_XXXX output only (bits 16-31) flags currently defined */

    struct snd_mixer_channel_direction {
      unsigned int channel; /* channel # (filled by application) */
      unsigned int flags;   /* some flags to read/write (SND_MIXER_DFLG_XXXX) */
      int left;             /* min - max when exact mode */
      int right;            /* min - max when exact mode */
      int left_dB;          /* dB * 100 */
      int right_dB;         /* dB * 100 */
      int volume;           /* used when not in exact mode (0-100) */
      int balance;          /* used when not in exact mode:
                               -100 (all left) -> 0 (equal) -> 100 (all right) */
      unsigned char reserved[16];
    };

  ______________________________________________________________________

  4.1.14.

  int snd_mixer_channel_record_read( void *handle, int channel, 
                                     snd_mixer_channel_direction_t *data )
 
  Fills the "*data" structure. The argument "channel" specifies channel
  (0 to N) for which is the info requested. Function returns zero if
  successful, otherwise it returns an error code.
 
  Note: see snd_mixer_channel_playback_read() for more information.
  ______________________________________________________________________

    /* Input direction only flags (bits 16-31) -- SND_MIXER_DFLG_XXXX */
    /* No SND_MIXER_DFLG_XXXX input only (bits 16-31) flags currently defined */
  ______________________________________________________________________
 
  4.1.15.

  int snd_mixer_channel_playback_write( void *handle, int channel, 
                                        snd_mixer_channel_direction_t *data )
 
  Writes the "*data" structure to the driver. The argument "channel" 
  specifies channel (0 to N) for which is the info requested. Function 
  returns zero if successful, otherwise it returns an error code.

  Note: see snd_mixer_channel_playback_read() for more information.
 
  4.1.16.

  int snd_mixer_channel_record_write( void *handle, int channel, 
                                        snd_mixer_channel_direction_t *data )
  
  Writes the "*data" structure to the driver. The argument "channel" 
  specifies channel (0 to N) for which is the info requested. Function 
  returns zero if successful, otherwise it returns an error code.

  Note: see snd_mixer_channel_playback_read() and
        snd_mixer_channel_record_read() for more information.
 
  4.1.17.

  int snd_mixer_switches( void *handle )

  Returns count of mixer switches.  In this context 'switch' means
  universal control interface between driver and application which
  allows various types control.
  This function returns count if successful, otherwise it returns an error
  code.  Return value should be zero if soundcard doesn't have any
  mixer switches.

  Returns the index for the switch with the name "switch_id".
  This function returns switch index if successful, otherwise it returns
  an error code.

  4.1.18.

  int snd_mixer_switch( void *handle, const char *switch_id )

  Returns the index for the switch with the name "switch_id". 
  This function returns switch index if successful, otherwise it returns
  an error code.

  4.1.19.

  int snd_mixer_switch_read( void *handle, int switchn, 
                                                        snd_switch_t *data )

  Fills the "*data" structure with data from the switch with index "switchn".
  This function returns zero if successful, otherwise it returns an error
  code.

  Note: See snd_ctl_switch_read() for more information on the switches
        interface in general.

  4.1.20.

  int snd_mixer_switch_write( void *handle, int switchn, 
                                                     snd_switch_t *data )

  Writes the "*data" structure to the switch with index "switchn".
  This function returns zero if successful, otherwise it returns an error
  code.

  Note: See snd_ctl_switch_read() for more information on the switches
        interface in general.

  4.1.21.

  int snd_mixer_read( void *handle, snd_mixer_callbacks_t *callbacks )

  This function reads and parses data from driver. Parsed actions are
  returned back to the application using the "callbacks" structure.
  Applications should not parse data from the driver in standard cases.
  This function returns immediately after all data is read from driver.
  Does not block process.

  ______________________________________________________________________
    typedef struct snd_mixer_callbacks {
      void *private_data;        /* should be used by application */
      void (*channel_was_changed)( void *private_data, int channel );
      void *reserved[15];        /* reserved for future use - must be NULL!!! */
    } snd_mixer_callbacks_t;

  ______________________________________________________________________

  4.2.  Examples

  The following example shows installed mixer channels for soundcard #0
  and mixer device #0 in the system, and also sets the master volume (if
  present) to 50.

  ______________________________________________________________________
  int card = 0, device = 0, err;
  void *handle;
  snd_mixer_info_t info;
  snd_mixer_channel_t channel;

  if ( (err = snd_mixer_open( &handle, card, device )) < 0 ) {
    fprintf( stderr, "open failed: %s\n", snd_strerror( err ) );
    return;
  }
  if ( (err = snd_mixer_info( handle, &info )) < 0 ) {
    fprintf( stderr, "info failed: %s\n", snd_strerror( err ) );
    snd_mixer_close( handle );
    return;
  }
  printf( "Installed MIXER channels for card \#%i and device %i: %i\n",
                                          card + 1, device, info.channels );
  master = snd_mixer_channel( handle, SND_MIXER_ID_MASTER );
  if ( master >= 0 ) {
    if ( (err = snd_mixer_read( handle, master, &channel )) < 0 ) {
      fprintf( stderr, "master read failed: %s\n", snd_strerror( err ) );
      snd_mixer_close( handle );
      return;
    }
    channel -> left = channel -> right = 50;
    if ( (err = snd_mixer_write( handle, master, &channel )) < 0 ) {
      fprintf( stderr, "master write failed: %s\n", snd_strerror( err ) );
      snd_mixer_close( handle );
      return;
    }
  }
  snd_mixer_close( handle );
  ______________________________________________________________________

  5.  Digital Audio (PCM) Interface

  Digital audio is the most commonly used method of representing sound
  inside a computer. In this method sound is stored as a sequence of
  samples taken from the audio signal using constant time intervals. A
  sample represents volume of the signal at the moment when it was
  measured. In uncompressed digital audio each sample require one or
  more bytes of storage. The number of bytes required depends on number
  of channels (mono, stereo) and sample format (8 or 16 bits, mu-Law,
  etc.). The length of this interval determines the sampling rate.
  Commonly used sampling rates are between 8 kHz (telephone quality) and
  48 kHz (DAT tapes).

  The physical devices used in digital audio are called the ADC (Analog
  to Digital Converter) and DAC (Digital to Analog Converter). A device
  containing both ADC and DAC is commonly known as a codec. The codec
  device used in a Sound Blaster cards is called a DSP which is somewhat
  misleading since DSP also stands for Digital Signal Processor (the SB
  DSP chip is very limited when compared to "true" DSP chips).

  Sampling parameters affect the quality of sound which can be
  reproduced from the recorded signal. The most fundamental parameter is
  sampling rate which limits the highest frequency than can be stored.
  It is well known (Nyquist's Sampling Theorem) that the highest
  frequency that can be stored in a sampled signal is at most 1/2 of the
  sampling frequency. For example, a 8 kHz sampling rate permits the
  recording of a signal in which the highest frequency is less than 4
  kHz. Higher frequency signals must be filtered out before feeding them
  to DAC.

  Sample encoding limits the dynamic range of recorded signal
  (difference between the faintest and the loudest signal that can be
  recorded). In theory the maximum dynamic range of signal is
  number_of_bits * 6 dB . This means that 8 bits sampling resolution
  gives dynamic range of 48 dB and 16 bit resolution gives 96 dB.

  Quality has price. The number of bytes required to store an audio
  sequence depends on sampling rate, number of channels and sampling
  resolution. For example just 8000 bytes of memory is required to store
  one second of sound using 8 kHz/8 bits/mono but 48 kHz/16bit/stereo
  takes 192 kilobytes. A 64 kbps ISDN channel is required to transfer a
  8kHz/8bit/mono audio stream in real time, and about 1.5 Mbps is
  required for DAT quality (48kHz/16bit/stereo).  On the other hand it
  is possible to store just 5.46 seconds of sound in a megabyte of
  memory when using 48kHz/16bit/stereo sampling. With 8kHz/8bits/mono it
  is possible to store 131 seconds of sound using the same amount of
  memory. It is possible to reduce memory and communication costs by
  compressing the recorded signal but this is out of the scope of this
  document.

  5.1.  Low-Level Layer

  Audio devices are opened exclusively for a selected direction. This
  doesn't allow open from more than one processes for the same audio
  device in the same direction, but does allow one open call to each
  playback direction and second open call to record direction
  independently. Audio devices return EBUSY error to applications when
  other applications have already opened the requested direction.

  Low-Level layer supports these formats:

  ______________________________________________________________________

    #define SND_PCM_SFMT_MU_LAW     0
    #define SND_PCM_SFMT_A_LAW      1
    #define SND_PCM_SFMT_IMA_ADPCM  2
    #define SND_PCM_SFMT_U8         3
    #define SND_PCM_SFMT_S16_LE     4
    #define SND_PCM_SFMT_S16_BE     5
    #define SND_PCM_SFMT_S8         6
    #define SND_PCM_SFMT_U16_LE     7
    #define SND_PCM_SFMT_U16_BE     8
    #define SND_PCM_SFMT_MPEG       9
    #define SND_PCM_SFMT_GSM        10
    #define SND_PCM_SFMT_S24_LE     11
    #define SND_PCM_SFMT_S24_BE     12
    #define SND_PCM_SFMT_U24_LE     13
    #define SND_PCM_SFMT_U24_BE     14
    #define SND_PCM_SFMT_S32_LE     15
    #define SND_PCM_SFMT_S32_BE     16
    #define SND_PCM_SFMT_U32_LE     17
    #define SND_PCM_SFMT_U32_BE     18
    #define SND_PCM_SFMT_FLOAT      19  /* 4-byte float, need specification!! */
    #define SND_PCM_SFMT_FLOAT64    20  /* 8-byte float, need specification!! */
    #define SND_PCM_SFMT_CAMBRIDGE  21
    #define SND_PCM_SFMT_SPECIAL    31

    #define SND_PCM_FMT_QUERY       0
    #define SND_PCM_FMT_MU_LAW      (1 << SND_PCM_SFMT_MU_LAW)
    #define SND_PCM_FMT_A_LAW       (1 << SND_PCM_SFMT_A_LAW)
    #define SND_PCM_FMT_IMA_ADPCM   (1 << SND_PCM_SFMT_IMA_ADPCM)
    #define SND_PCM_FMT_U8          (1 << SND_PCM_SFMT_U8)
    #define SND_PCM_FMT_S16_LE      (1 << SND_PCM_SFMT_S16_LE)
    #define SND_PCM_FMT_S16_BE      (1 << SND_PCM_SFMT_S16_BE)
    #define SND_PCM_FMT_S8          (1 << SND_PCM_SFMT_S8)
    #define SND_PCM_FMT_U16_LE      (1 << SND_PCM_SFMT_U16_LE)
    #define SND_PCM_FMT_U16_BE      (1 << SND_PCM_SFMT_U16_BE)
    #define SND_PCM_FMT_MPEG        (1 << SND_PCM_SFMT_MPEG)
    #define SND_PCM_FMT_GSM         (1 << SND_PCM_SFMT_GSM)
    #define SND_PCM_FMT_S24_LE      (1 << SND_PCM_SFMT_S32_LE)
    #define SND_PCM_FMT_S24_BE      (1 << SND_PCM_SFMT_S32_BE)
    #define SND_PCM_FMT_U24_LE      (1 << SND_PCM_SFMT_U32_LE)
    #define SND_PCM_FMT_U24_BE      (1 << SND_PCM_SFMT_U32_BE)
    #define SND_PCM_FMT_S32_LE      (1 << SND_PCM_SFMT_S32_LE)
    #define SND_PCM_FMT_S32_BE      (1 << SND_PCM_SFMT_S32_BE)
    #define SND_PCM_FMT_U32_LE      (1 << SND_PCM_SFMT_U32_LE)
    #define SND_PCM_FMT_U32_BE      (1 << SND_PCM_SFMT_U32_BE)
    #define SND_PCM_FMT_FLOAT       (1 << SND_PCM_SFMT_FLOAT)
    #define SND_PCM_FMT_FLOAT64     (1 << SND_PCM_SFMT_FLOAT64)
    #define SND_PCM_FMT_CAMBRIDGE   (1 << SND_PCM_SFMT_CAMBRIDGE)
    #define SND_PCM_FMT_SPECIAL     (1 << SND_PCM_SFMT_SPECIAL)

  ______________________________________________________________________

  Constants with prefix SND_PCM_FMT_ are used in info structures and
  constants with prefix SND_PCM_SFMT_ are used in format structures.

  5.1.1.

  int snd_pcm_open( void **handle, int card, int device, int mode )

  Creates a new handle and opens a connection to audio interface
  for soundcard number "card" (0-N) and audio device number "device". 
  Function also checks if protocol is compatible to prevent use
  of old programs with a new driver API. Function returns zero if
  successful, otherwise it returns an error code.  Error code -EBUSY
  is returned when some process ownes the selected direction.

  Default format after opening is mono mu-Law at 8000Hz. This device can
  be used directly for playback of standard .au (Sparc) files.

  The following modes should be used for the "mode" argument:

  ______________________________________________________________________

    #define SND_PCM_OPEN_PLAYBACK   (O_WRONLY)
    #define SND_PCM_OPEN_RECORD     (O_RDONLY)
    #define SND_PCM_OPEN_DUPLEX     (O_RDWR)

  ______________________________________________________________________

  5.1.2.

  int snd_pcm_close( void *handle )

  Frees all resources allocated with audio handle and closes the
  connection to the audio interface. Function returns zero if
  successful, otherwise it returns an error code.

  5.1.3.

  int snd_pcm_file_descriptor( void *handle )

  Returns the file descriptor of the connection to the audio interface. 
  Function returns an error code if an error was
  encountered.

  The file descriptor should be used for the select() synchronous
  multiplexer function for setting the read direction. Application
  should call snd_pcm_read() or snd_pcm_write() functions if some data is
  waiting for reading or a write can be performed. Calling this function
  is highly recomended, as it leaves a place for the API to things like
  data conversions, if needed.

  5.1.4.

  int snd_pcm_block_mode( void *handle, int enable )

  Sets up block (default) or nonblock mode for a handle. Block mode
  suspends execution of a program when snd_pcm_read() or snd_pcm_write()
  is called for the time which is needed for the actual playback or record
  over of the entire buffer. In nonblock mode, programs aren't suspended
  and the above functions returns immediately with the count of bytes
  which were read or written by the driver. When used in this way, don't
  try to use the entire buffer after the call, but instead process the
  number of bytes returned, and call the function again.

  5.1.5.

  int snd_pcm_info( void *handle, snd_pcm_info_t *info )

  Fills the "*info" structure with data about the PCM device selected by
  "*handle". Function returns zero if successful, otherwise it returns an
  error code.

  ______________________________________________________________________
    #define SND_PCM_INFO_CODEC        0x00000001
    #define SND_PCM_INFO_DSP          SND_PCM_INFO_CODEC
    #define SND_PCM_INFO_MMAP         0x00000002 
                    /* for compatibility with OSS, this flag shouldn't 
                       be used with native applications */
    #define SND_PCM_INFO_PLAYBACK     0x00000100
    #define SND_PCM_INFO_RECORD       0x00000200
    #define SND_PCM_INFO_DUPLEX       0x00000400
    #define SND_PCM_INFO_DUPLEX_LIMIT 0x00000800 
                   /* rate for playback & record channels must be same!!! */
    #define SND_PCM_INFO_DUPLEX_MONO  0x00001000 
                   /* in duplex mode - only mono (one channel) is supported */

        struct snd_pcm_info {
      unsigned int type;              /* soundcard type */
      unsigned int flags;             /* see to SND_PCM_INFO_XXXX */
      unsigned char id[32];           /* ID of this PCM device */
      unsigned char name[80];         /* name of this device */
      unsigned char reserved[64];     /* reserved for future... */
    };

  ______________________________________________________________________

     SND_PCM_INFO_MMAP
        This flag is reserved and should be never used. It remains for
        compatibility with Open Sound System driver.

     SND_PCM_INFO_DUPLEX_LIMIT
        If this bit is set, rate must be same for playback and record
        direction.

  5.1.6.

  int snd_pcm_playback_info( void *handle, snd_pcm_playback_info_t *info )

  Fills the "*info" structure with data about PCM playback. Function
  returns zero if successful, otherwise it returns an error code.

  ______________________________________________________________________
    #define SND_PCM_PINFO_BATCH             0x00000001
    #define SND_PCM_PINFO_8BITONLY          0x00000002
    #define SND_PCM_PINFO_16BITONLY         0x00000004

        struct snd_pcm_playback_info {
      unsigned int flags;             /* see to SND_PCM_PINFO_XXXX */
      unsigned int formats;           /* supported formats */
      unsigned int min_rate;          /* min rate (in Hz) */
      unsigned int max_rate;          /* max rate (in Hz) */
      unsigned int min_channels;      /* min channels (probably always 1) */
      unsigned int max_channels;      /* max channels */
      unsigned int buffer_size;       /* playback buffer size */
      unsigned int min_fragment_size; /* min fragment size in bytes */
      unsigned int max_fragment_size; /* max fragment size in bytes */
      unsigned int fragment_align;    /* align fragment value */
      unsigned int hw_formats;        /* formats supported by hardware */
      unsigned int switches;          /* count of switches */
      unsigned char reserved[56];     /* reserved for future... */
    };

  ______________________________________________________________________

     SND_PCM_PINFO_BATCH
        Driver implements double buffering with this device. This means
        that the chip used for data processing has its own memory, and
        output should be more delayed than if a traditional codec chip
        is used.

     SND_PCM_PINFO_8BITONLY
        If this bit is set, the driver uses 8-bit format for 16-bit
        samples and does software conversion. This bit is set on broken
        SoundBlaster 16/AWE soundcards which can't do full 16-bit
        duplex. If this bit is set, the application or a higher digital
        audio layer should do the conversion from 16-bit samples to 8-bit
        samples rather than making the driver to do it.

     SND_PCM_PINFO_16BITONLY
        If this bit is set, driver uses 16-bit format for 8-bit samples
        and does software conversion. This bit is set on broken
        SoundBlaster 16/AWE soundcards which can't do full 8-bit duplex.
        If this bit is set, the application or a higher digital audio
        layer should do conversion from 8-bit samples to 16-bit samples
        rather than making the driver to do it.

  5.1.7.

  int snd_pcm_record_info( void *handle, snd_pcm_record_info_t *info )

  Fills the "*info" structure. Returns zero if successful, otherwise it
  returns an error code.

  ______________________________________________________________________
    #define SND_PCM_RINFO_BATCH     0x00000001
    #define SND_PCM_RINFO_8BITONLY  0x00000002
    #define SND_PCM_RINFO_16BITONLY 0x00000004
    #define SND_PCM_RINFO_OVERRANGE 0x00010000 
                         /* hardware supports ADC overrange detection */

    struct snd_pcm_record_info {
      unsigned int flags;             /* see to SND_PCM_RINFO_XXXX */
      unsigned int formats;           /* supported formats */
      unsigned int min_rate;          /* min rate (in Hz) */
      unsigned int max_rate;          /* max rate (in Hz) */
      unsigned int min_channels;      /* min channels (probably always 1) */
      unsigned int max_channels;      /* max channels */
      unsigned int buffer_size;       /* record buffer size */
      unsigned int min_fragment_size; /* min fragment size in bytes */
      unsigned int max_fragment_size; /* max fragment size in bytes */
      unsigned int fragment_align;    /* align fragment value */
      unsigned int hw_formats;        /* formats supported by hardware */
      unsigned int switches;          /* count of switches */
      unsigned char reserved[56];     /* reserved for future... */
    };
  ______________________________________________________________________

     SND_PCM_RINFO_BATCH
        Driver implements buffering for this device. This means that the
        chip used for data processing has its own memory and output
        should be more delayed than if a traditional codec chip is used.

     SND_PCM_RINFO_8BITONLY
        If this bit is set, the device uses 8-bit format for 16-bit
        samples and does software conversion. This bit is set on broken
        SoundBlaster 16/AWE soundcards which can't do full 16-bit
        duplex. If this bit is set, the application or a higher digital
        audio layer should do conversion from 16-bit samples to 8-bit
        samples rather than making the driver to do it.

     SND_PCM_RINFO_16BITONLY
        If this bit is set, the device uses a 16-bit format for 8-bit
        samples and does software conversion. This bit is set on broken
        SoundBlaster 16/AWE soundcards which can't do full 8-bit duplex.
        If this bit is set, the application or a higher digital audio
        layer should do the conversion from 8-bit samples to 16-bit
        samples rather than making the driver to do it.

  5.1.8.

  int snd_pcm_playback_switches( void *handle )

  Returns count of PCM playback switches.  In this context 'switch' means
  universal control interface between driver and application which
  allows various types control.
  This function returns count if successful, otherwise it returns an error
  code.  Return value should be zero if soundcard doesn't have any
  mixer switches.

  5.1.9.

  int snd_pcm_playback_switch( void *handle, const char *switch_id )

  Returns the index for the switch with the name "switch_id".
  This function returns switch index if successful, otherwise it returns
  an error code.

  5.1.10.

  int snd_pcm_playback_switch_read( void *handle, int switchn,
                                                        snd_switch_t *data )

  Fills the "*data" structure with data from the switch with index "switchn".
  This function returns zero if successful, otherwise it returns an error
  code.

  Note: See snd_ctl_switch_read() for more information on the switches
        interface in general.

  5.1.11.

  int snd_pcm_playback_switch_write( void *handle, int switchn,
                                                     snd_switch_t *data )

  Writes the "*data" structure to the switch with index "switchn".
  This function returns zero if successful, otherwise it returns an error
  code.

  Note: See snd_ctl_switch_read() for more information on the switches
        interface in general.


  5.1.12.

  int snd_pcm_record_switches( void *handle )

  Returns count of PCM recording switches.  In this context 'switch' means
  universal control interface between driver and application which
  allows various types control.
  This function returns count if successful, otherwise it returns an error
  code.  Return value should be zero if soundcard doesn't have any
  mixer switches.

  Returns the index for the switch with the name "switch_id".
  This function returns switch index if successful, otherwise it returns
  an error code.

  5.1.13.

  int snd_pcm_record_switch( void *handle, const char *switch_id )

  Returns the index for the switch with the name "switch_id".
  This function returns switch index if successful, otherwise it returns
  an error code.

  5.1.14.

  int snd_pcm_record_switch_read( void *handle, int switchn,
                                                        snd_switch_t *data )

  Fills the "*data" structure with data from the switch with index "switchn".
  This function returns zero if successful, otherwise it returns an error
  code.

  Note: See snd_ctl_switch_read() for more information on the switches
        interface in general.

  5.1.15.

  int snd_pcm_record_switch_write( void *handle, int switchn,
                                                     snd_switch_t *data )

  Writes the "*data" structure to the switch with index "switchn".
  This function returns zero if successful, otherwise it returns an error
  code.

  Note: See snd_ctl_switch_read() for more information on the switches
        interface in general.

  5.1.16.

  int snd_pcm_playback_format( void *handle, snd_pcm_format_t *format )

  Sets up format, rate (in Hz) and number of channels for playback, in
  the desired direction. Function returns zero if successful, otherwise
  it returns an error code.

  ______________________________________________________________________

    struct snd_pcm_format {
      unsigned int format;            /* SND_PCM_SFMT_XXXX */
      unsigned int rate;              /* rate in Hz */
      unsigned int channels;          /* channels (voices) */
      unsigned int special;           /* special description of format */
      unsigned char reserved[12];
    };

  ______________________________________________________________________

  5.1.17.

  int snd_pcm_record_format( void *handle, snd_pcm_format_t *format )

  Sets up format, rate (in Hz) and number of channels for used for
  recording in the specified direction. Function returns zero if
  successful, otherwise it returns an error code.

  Note: see snd_pcm_playback_format for more info.

  5.1.18.

  int snd_pcm_playback_params( void *handle, 
                                      snd_pcm_playback_params_t *params )
  

  Sets various parameters for playback direction. Function returns zero
  if successful, otherwise it returns an error code.

  ______________________________________________________________________

    struct snd_pcm_playback_params {
      int fragment_size;
      int fragments_max;
      int fragments_room;
      unsigned char reserved[16];           /* must be filled with zero */
    };

  ______________________________________________________________________

     fragment_size
        Requested size of fragment. This value should be aligned for
        current format (for example to 4 if stereo 16-bit samples are
        used) or with the "fragment_align" variable from the
        snd_pcm_playback_info_t structure. Its range can be from
        "min_fragment_size" to "max_fragment_size".

     fragments_max
        Maximum number of fragments in queue for wakeup. This number
        doesn't count partly used fragments. If current count of filled
        playback fragments is greater than this value driver block
        application or return immediately back if nonblock mode is
        active.

     fragments_room
        Minumum number of fragments writeable for wakeup. This value
        should be in most cases 1 which means return back to application
        if at least one fragment is free for playback. This value
        includes partly used fragments, too.

  5.1.19.

  int snd_pcm_record_params( void *handle, 
                                       snd_pcm_record_params_t *params )
  
  Function sets various parameters for the recording direction. Function
  returns zero if successful, otherwise it returns an error code.

  ______________________________________________________________________

    struct snd_pcm_record_params {
      int fragment_size;
      int fragments_min;
      unsigned char reserved[16];
    };

  ______________________________________________________________________

     fragment_size
        Requested size of fragment. This value should be aligned for
        current format (for example to 4 if stereo 16-bit samples are
        used) or set to the "fragment_align" variable from
        snd_pcm_record_info_t structure. Its range can be from
        "min_fragment_size" to "max_fragment_size".

     fragments_min
        Minimum filled fragments for wakeup. Driver blocks the
        application (if block mode is selected) until it isn't filled
        with number of fragments specified with this value.

  5.1.20.

  int snd_pcm_playback_status( void *handle, 
                                       snd_pcm_playback_status_t *status )
  

  Fills the "*status" structure. Function returns zero if successful,
  otherwise it returns an error code.

  ______________________________________________________________________
    struct snd_pcm_playback_status {
      unsigned int rate;      /* real used rate */
      int fragments;          /* allocated fragments */
      int fragment_size;      /* current fragment size in bytes */
      int count;              /* number of bytes writeable without blocking */
      int queue;              /* number of bytes in queue */
      int underrun;           /* count of underruns from last status */
      struct timeval time;    /* time the next write is going to play */
      struct timeval stime;   /* time when playback was started */
      int scount;             /* number of bytes processed from 
                                 playback start */
      unsigned char reserved[16];
    };

  ______________________________________________________________________

     rate
        Real playback rate. This value reflects hardware limitations.

     fragments
        Currently allocated fragments by the driver for playback
        direction.

     fragment_size
        Current fragment size used by driver for the playback direction.

     count
        Count of bytes writeable without blocking.

     queue
        Count of bytes in queue. Note: ((fragments * fragment_size) - queue )
        should not be equal to "count".

     underrun
        This value tells the application the number of underruns since
        the last call of snd_pcm_playback_status().

     time
        Delay till playing of the first sample from next write. This
        value should be used for time synchronization. Returned value is
        in the same format as returned from the standard C function
        gettimeofday(&time, NULL ).  This variable contains right value
        only if playback time mode is enabled (look to
        snd_pcm_playback_time() function).

     stime
        Time when playback was started.  This variable contains right
        value only if playback time mode is enabled (look to
        snd_pcm_playback_time() function).

     scount
        number of bytes processed (actually played) since playback start (stime).
        Note: This value is not necessarily the same as byte count written by 
              the application.

  5.1.21.

  int snd_pcm_record_status( void *handle, snd_pcm_record_status_t *status )
                                       
  Fills the "*status" structure. Function returns zero if successful,
  otherwise it returns an error code.

  ______________________________________________________________________
    
    struct snd_pcm_record_status {
      unsigned int rate;    /* real used rate */
      int fragments;        /* allocated fragments */
      int fragment_size;    /* current fragment size in bytes */
      int count;            /* number of bytes readable without blocking */
      int free;             /* bytes in buffer still free */
      int overrun;          /* count of overruns from last status */
      struct timeval time;  /* time the next read was taken */
      struct timeval stime; /* time when record was started */
      int scount;           /* number of bytes processed from record start */
      int overrange;        /* ADC overrange detection */
      unsigned char reserved[12];
    };
  ______________________________________________________________________

     rate
        Real record rate. This value reflects hardware limitations.

     fragments
        Currently allocated fragments by driver for the record
        direction.

     fragment_size
        Current fragment size used by driver for the record direction.

     count
        Count of bytes readable without blocking.

     free
        Count of bytes in buffer still free. 
	Note: ((fragments * fragment_size) - free )
	      should not be equal to "count".

     overrun
        This value tells application the count of overruns since the
        last call to snd_pcm_record_status.

     time
        Lag since the next sample read was recorded. This value should
        be used for time synchronization. Returned value is in the same
        format as returned by the from standard C function 
	gettimeofday( &time, NULL ). This variable contains right 
	value only if record time mode is enabled 
	(look to snd_pcm_record_time() function).
        
     stime
        Time when record was started. This variable contains right value
        only if record time mode is enabled (look to snd_pcm_record_time()
        function).

     scount
        number of bytes processed (actually recorded) since record start (stime).
        Note: This value is not necessarily the same as byte count written by 
              the application.

     overrange
        ADC overrange count. 
        Note: This value is used only if the SND_PCM_RINFO_OVERRANGE bit
              in struct snd_pcm_record_info_t->flags is set and the hardware
              supports ADC overrange detection. ( see snd_pcm_record_info() )
              
  5.1.22.

  int snd_pcm_drain_playback( void *handle )

  This function drain playback buffers immediately. Function returns
  zero if successful, otherwise it returns an error code.

  5.1.23.

  int snd_pcm_flush_playback( void *handle )

  This function flushes the playback buffers. It blocks the program
  while the all the waiting samples in driver playback buffers are
  processed. Function returns zero if successful, otherwise it returns
  an error code.

  5.1.24.

  int snd_pcm_flush_record( void *handle )

  This function flushes (destroys) record buffers. Function returns
  zero if successful, otherwise it returns a negative error code.

  5.1.25.

  int snd_pcm_playback_pause( void *handle, int enable ) 

  This function pauses (enables) or resumes (disables) playback.
  Function returns zero if successful, otherwise it returns a
  negative error code.

  5.1.26.

  int snd_pcm_playback_time( void *handle, int enable )

  This function enables or disables time mode for playback direction.
  Time mode allows to application better time synchronization. Function
  returns zero if successful, otherwise it returns a negative error code.

  5.1.27.

  int snd_pcm_record_time( void *handle, int enable )

  This function enables or disables time mode for record direction. Time
  mode allows to application better time synchronization. Function
  returns zero if successful, otherwise it returns a negative error code.


  5.1.28.

  int snd_pcm_reset_playback_time( void *handle, struct timeval *time )

  This function is useful for resynchonizing the drivers internal 
  playback counter with an external reference (ex. a video stream)
  This function should be used with snd_pcm_playback_time() enabled.
  The argument, *time, becomes your new start time (stime) on 
  subsequent calls of snd_pcm_playback_status(). Note that scount
  is automatically updated to match the new start time. Function
  returns zero if successful, otherwise it returns a negative error code.

  5.1.29.

  int snd_pcm_reset_record_time( void *handle, struct timeval *time )
 
  This function is useful for resynchonizing the drivers internal 
  record counter with an external reference (ex. a video stream)
  This function should be used with snd_pcm_playback_time() enabled.
  The argument, *time, becomes your new start time (stime) on 
  subsequent calls of snd_pcm_record_status(). Note that scount
  is automatically updated to match the new start time. Function
  returns zero if successful, otherwise it returns a negative error code.


  5.1.30.

  ssize_t snd_pcm_write( void *handle, const void *buffer, size_t size )

  Writes samples to the device which must be in the proper format
  specified by the snd_pcm_playback_format() function. Function returns
  zero or positive value if playback was successful (value represents
  count of bytes which was successfuly written to device) or an error
  value if error occured. Function should suspend process if block mode
  is active.

  5.1.31.

  ssize_t snd_pcm_read( void *handle, void *buffer, size_t size )

  Function reads samples from driver. Samples are in format specified by
  snd_pcm_record_format() function. Function returns zero or positive
  value if record was success (value represents count of bytes which was
  successfuly read from device) or negative error value if error
  occured. Function should suspend process if block mode is active.

  5.2. Examples

  The following example shows how to play the first 512kB from the
  /tmp/test.au file with soundcard #0 and PCM device #0:

       ______________________________________________________________________
       int card = 0, device = 0, err, fd, count, size, idx;
       void *handle;
       snd_pcm_format_t format;
       char *buffer;

       buffer = (char *)malloc( 512 * 1024 );
       if ( !buffer ) return;
       if ( (err = snd_pcm_open( &handle, card, device, 
                                          SND_PCM_OPEN_PLAYBACK )) < 0 ) {
         fprintf( stderr, "open failed: %s\n", snd_strerror( err ) );
         return;
       }
       format.format = SND_PCM_SFMT_MU_LAW;
       format.rate = 8000;
       format.channels = 1;
       if ( (err = snd_pcm_playback_format( handle, &format )) < 0 ) {
         fprintf( stderr, "format setup failed: %s\n", snd_strerror( err ) );
         snd_pcm_close( handle );
         return;
       }
       fd = open( "/tmp/test.au", O_RDONLY );
       if ( fd < 0 ) {
         perror( "open file" );
         snd_pcm_close( handle );
         return;
       }
       idx = 0;
       count = read( fd, buffer, 512 * 1024 );
       if ( count <= 0 ) {
         perror( "read from file" );
         snd_pcm_close( handle );
         return;
       }
       close( fd );
       if ( !memcmp( buffer, ".snd", 4 ) ) {
         idx = (buffer[4]<<24)|(buffer[5]<<16)|(buffer[6]<<8)|(buffer[7]);
         if ( idx > 128 ) idx = 128;
         if ( idx > count ) idx = count;
       }
       size = snd_pcm_write( handle, &buffer[ idx ], count - idx );
       printf( "Bytes written %i from %i...\n", size, count - idx );
       snd_pcm_close( handle );
       free( buffer );
       ______________________________________________________________________

  5.3.    PCM Loopback Interface

  5.3.1.  

  int snd_pcm_loopback_open( void **handle, int card, int device, int mode )

  Creates a new handle and opens a connection to the driver audio loopback
  interface for sound card number "card" (0-N) and audio device number 
  "device".  Function also checks if protocol is compatible to prevent 
  use of old programs with a new API.
  Function returns zero if successful otherwise it returns an error code.
  Error code -EBUSY is returned when another process owns the selected 
  direction.

  The following modes should be used for the "mode" argument:
  #define SND_PCM_LB_OPEN_PLAYBACK 0
  #define SND_PCM_LB_OPEN_RECORD   1

  5.3.2.  
  
  int snd_pcm_loopback_close( void *handle )

  Frees all resources allocated with audio handle and closes the connection
  to the driver audio loopback interface.
  Function returns zero if successful, otherwise it returns an error code.

  5.3.3.  

  int snd_pcm_loopback_file_descriptor( void *handle )

  Returns the file descriptor of the connection to the audio interface.
  Function returns an error code if an error was encountered.
  The file descriptor should be used for the select() synchronous 
  multiplexer function for setting the read direction.
  Application should call snd_pcm_loopback_read() function if data 
  is waiting to be read.
 

  5.3.4.  

  int snd_pcm_loopback_block_mode( void *handle, int enable )

  Sets up block (default) or non-block mode for a handle.
  Block mode suspends execution of a program when snd_pcm_loopback_read()
  is called for the time until some data arrives for file descriptor.
  In non-block mode, programs aren't suspended and the above function returns
  immediately with the count of bytes which were read by the driver.
  When used in this way, don't try to use the entire buffer after the call,
  but instead process the number of bytes returned, and call the function
  again.

  5.3.5.  

  int snd_pcm_loopback_stream_mode( void *handle, int mode )

  Sets up stream mode which should be one of these values:
  #define SND_PCM_LB_STREAM_MODE_RAW    0
  #define SND_PCM_LB_STREAM_MODE_PACKET 1

  Mode raw (default mode) means that the stream contains only PCM samples.
  Packet mode is more complicated.
  The stream contains a header at the begining of the packet.
  Information like data type and data size is contain in this header.
  #define SND_PCM_LB_TYPE_DATA   0  /* sample data */
  #define SND_PCM_LB_TYPE_FORMAT 1  /* format change */

  struct snd_pcm_loopback_header {
    unsigned int size;   /* block size */
    unsigned int type;   /* block type (SND_PCM_LB_TYPE_XXXX) */
  };

  5.3.6.  

  int snd_pcm_loopback_format( void *handle, snd_pcm_format_t *format )

  Get current format for PCM stream.

  struct snd_pcm_format {
    unsigned int format;        /* SND_PCM_SFMT_XXXX */
    unsigned int rate;          /* rate in Hz */
    unsigned int channels;      /* number of channels (voices) */
    unsigned char reserved[16];
  };

  5.3.7.  

  ssize_t snd_pcm_loopback_read( void *handle, void *buffer, size_t size ) 

  This function reads samples or loopback packets from the stream.
  Data depends on stream mode which should be set with 
  snd_pcm_loopback_stream_mode() function.  Function returns 
  zero or positive value if record was success (value represents 
  count of bytes which were successfully read from device) or negative error
  value if error occurred.
  Function will suspend process if block mode is active.

  6.      Raw MIDI Interface

  RawMidi Interface is designed to write or read raw (unchanged) MIDI data
  over the MIDI line.
  MIDI stands Musical Instrument Digital Interface and more informations
  about this standard can be found at http://www.midi.org

  Note: Do not confuse Raw MIDI with a synthesizer. This is a communications
  protocol that allows you to "talk" to musical instruments. This will not
  (directly) play out sound through your speakers.


  6.1.    Low Level Interface

  RawMidi devices are opened exclusively for a selected direction.
  While more than one process may not open a given MIDI device in the same
  direction simultaniously, seperate processes may open a single MIDI device
  in different directions (i.e.
  process one opens a MIDI device in playback direction and process two opens
  the same device in record direction).
  Audio devices (with MIDI ports) return EBUSY error to applications when
  other applications have already opened the requested direction.

  6.1.1. 
 
  int snd_rawmidi_open( void **handle, int card, int device, int mode )

  Creates a new handle and opens a connection to the driver's rawmidi interface
  for sound card number "card" (0-N) and rawmidi device number "device".
  Function also checks if protocol is compatible to prevent use of old programs
  with a new API.
  Function returns zero if successful, otherwise it returns an error code.
  Error code -EBUSY is returned when another process owns the selected 
  direction.

  The following modes should be used for the "mode" argument:
  #define SND_RAWMIDI_OPEN_OUTPUT (O_WRONLY)
  #define SND_RAWMIDI_OPEN_INPUT  (O_RDONLY) 
  #define SND_RAWMIDI_OPEN_DUPLEX (O_RDWR)

  6.1.2.  

  int snd_rawmidi_close( void *handle )

  Frees all resources allocated with audio handle and closes the connection
  to the driver rawmidi interface.
  Function returns zero if successful, otherwise it returns an error code.

  6.1.3.  

  int snd_rawmidi_file_descriptor( void *handle )

  Returns the file descriptor of the connection to the driver rawmidi
  interface.
  Function returns an error code if an error was encountered.

  The file descriptor should be used for the select() synchronous 
  multiplexer function for setting the read direction.
  Application should call snd_rawmidi_read() or snd_rawmidi_write()
  functions if data is waiting to be read or a write can be performed.
  Calling these functions is highly recommended.

  6.1.4.  

  int snd_rawmidi_block_mode( void *handle, int enable )

  Sets up block (default) or non-block mode for a handle.
  Block mode suspends execution of a program when
  snd_rawmidi_read() or snd_rawmidi_write() is called 
  for the time which is needed for the actual output 
  or input over of the selected limit.
  In non-block mode, programs aren't suspended and the above functions return
  immediately with the count of bytes which were read or written by the driver.
  When used in this way, don't try to use the entire buffer after the call,
  but instead process the number of bytes returned, and call the function
  again.

  6.1.5.  

  int snd_rawmidi_info( void *handle, snd_rawmidi_info_t *info )

  Fills the "*info" structure with data about the rawmidi device selected by  
  "*handle".
  Function returns zero if successful, otherwise it returns an error code.

  ______________________________________________________________________

  #define SND_RAWMIDI_INFO_OUTPUT 0x00000001 /* capable of rawmidi output */
  #define SND_RAWMIDI_INFO_INPUT  0x00000002 /* capable of rawmidi input */
  #define SND_RAWMIDI_INFO_DUPLEX 0x00000004 /* capable of duplex mode */

  struct snd_rawmidi_info {
    unsigned int type;              /* soundcard type */
    unsigned int flags;             /* SND_RAWMIDI_INFO_XXXX */
    unsigned char id[32];           /* ID of this raw midi device */
    unsigned char name[80];         /* name of this raw midi device */
    unsigned char reserved[64];     /* reserved for future use */
  };

  ______________________________________________________________________

  6.1.6.  

  int snd_rawmidi_output_info( void *handle, 
                                         snd_rawmidi_output_info_t *info )
 
  Fills the "*info" structure with data about rawmidi output. 
  Function returns zero if successful, otherwise it returns an error code.

  ______________________________________________________________________

  struct snd_rawmidi_output_info {
    unsigned int switches;  /* count of switches */
    unsigned char reserved[64];
  };

  ______________________________________________________________________

  6.1.7.  

  int snd_rawmidi_input_info( void *handle, 
                                         snd_rawmidi_input_info_t *info )
  Fills the "*info" structure with data about rawmidi input. 
  Function returns zero if successful, otherwise it returns an error code.
  

  ______________________________________________________________________

  struct snd_rawmidi_input_info {
    unsigned int switches;  /* count of switches */
    unsigned char reserved[64];
  };

  ______________________________________________________________________

  6.1.8.  

  int snd_rawmidi_output_switches( void *handle )

  Returns count of rawmidi output switches.
  In this context 'switch' means universal control interface between driver
  and application which allows various types of control.
  Function returns count if successful, otherwise it returns an error code.
  Return value should be zero if sound card doesn't have any rawmidi output
  switch.

  6.1.9.  

  int snd_rawmidi_output_switch( void *handle, const char *switch_id )

  Returns index for rawmidi output switch with name "switch_id".
  Function returns switch index if successful, otherwise it returns an error
  code.

  6.1.10. 

  int snd_rawmidi_output_switch_read( void *handle, 
                                          int switchn, snd_switch_t *data )

  Fills the "*data" structure with data about switch with index "switchn".
  Function returns zero if successful, otherwise it returns an error code.

  Note: See snd_ctl_switch_read() for more information on the switches
        interface in general.

  6.1.11. 

  int snd_rawmidi_output_switch_write( void *handle, 
                                          int switchn, snd_switch_t *data )

  Writes the "*data" structure with data about switch with index "switchn"
  to the driver.
  Function returns zero if successful, otherwise it returns an error code.

  Note: See snd_ctl_switch_read() for more information on the switches
        interface in general.

  6.1.12. 

  int snd_rawmidi_input_switches( void *handle )

  Returns count of rawmidi input switches.
  In this context 'switch' means universal control interface between driver 
  and application which allows various types of control.
  Function returns count if successful, otherwise it returns an error code.
  Return value should be zero if sound card doesn't have any rawmidi input
  switch.

  6.1.13. 

  int snd_rawmidi_input_switch( void *handle, const char *switch_id )

  Returns index for switch with name "switch_id".
  Function returns switch index if successful, otherwise it returns an error
  code.

  6.1.14. 

  int snd_rawmidi_input_switch_read( void *handle, 
                                          int switchn, snd_switch_t *data )

  Fills the "*data" structure with data about switch with index "switchn".
  Function returns zero if successful, otherwise it returns an error code.

  Note: See snd_ctl_switch_read() for more information on the switches
        interface in general.

  6.1.15. 

  int snd_rawmidi_input_switch_write( void *handle, 
                                        int switchn, snd_switch_t *data )

  Writes the "*data" structure with data about switch with index "switchn"
  to the driver.
  Function returns zero if successful, otherwise it returns an error code.

  Note: See snd_ctl_switch_read() for more information on the switches
        interface in general.

  6.1.16. 

  int snd_rawmidi_output_params( void *handle, 
                                    snd_rawmidi_output_params_t *params )

  Sets various parameters for output direction.
  Function returns zero if successful, otherwise it returns an error code.

  ______________________________________________________________________
 
  struct snd_rawmidi_output_params {
    int size;
    int max;
    int room; 
    unsigned char reserved[16];     /* reserved for future use */
  };

  ______________________________________________________________________

  size 
    Requested queue size of output buffer in bytes 
    (default setup is 4096 [i386] or 8192 [alpha] bytes - this is system 
     architecture dependent).

  max 
    Maximum number of bytes in queue for wakeup.
    If the current byte count of filled portion of output buffer is greater
    than this value the driver will block an application or return immediately
    if non block mode is active.

  room 
    Minimum number of bytes writable for wakeup.
    This value should be in most cases 1 which means return back to application
    if at least one byte is free in output buffer.

  6.1.17. 
 
  int snd_rawmidi_input_params( void *handle, 
                                      snd_rawmidi_input_params_t *params )

  Function sets various parameters for the recording direction.
  Function returns zero if successful, otherwise it returns an error code.

  ______________________________________________________________________

  struct snd_rawmidi_input_params {
    int size;   
    int min;   
    unsigned char reserved[16];
  };
  ______________________________________________________________________

  size 
    Requested queue size of input buffer in bytes 
    (default setup is 4096 [i386] or 8192 [alpha] bytes - this is system 
     architecture dependent).

  min 
    Minimum filled bytes in queue for wakeup.
    Driver blocks the application (if block mode is selected) until 
    input buffer is filled with fewer than the number of bytes specified 
    with this value.
    

  6.1.18. 

  int snd_rawmidi_output_status( void *handle, 
                                      snd_rawmidi_output_status_t *status )

  Fills the "*status" structure.
  Function returns zero if successful, otherwise it returns an error code.

  ______________________________________________________________________

  struct snd_rawmidi_output_status {
    int size;      
    int count;    
    int queue;   
    unsigned char reserved[16];
  };

  ______________________________________________________________________

  size 
    Size of currently allocated queue in bytes.

  count 
    Count of bytes writable without blocking.

  queue 
    Count of bytes in queue (number of bytes waiting to be output).

  6.1.19. 

  int snd_rawmidi_input_status( void *handle, 
                                      snd_rawmidi_input_status_t *status )

  Fills the "*status" structure.
  Function returns zero if successful, otherwise it returns an error code.

  ______________________________________________________________________

  struct snd_rawmidi_input_status {
    int size;    
    int count;  
    int free;  
    int overrun;
    unsigned char reserved[16];
  };

  ______________________________________________________________________

  size 
    Size of currently allocated queue in bytes.

  count 
    Count of bytes readable without blocking.

  free 
    Count of bytes in queue still free.

  overrun 
    This value tells the application the count of overruns since the
    last call to snd_rawmidi_input_status().

  6.1.20. 

  int snd_rawmidi_drain_output( void *handle )

  This function stops and drains (destroys) the output queue immediately.
  Function returns zero if successful, otherwise it returns an error code.

  6.1.21. 

  int snd_rawmidi_flush_output( void *handle )

  This function flushes the output queue.  It blocks the program 
  while the all the waiting bytes in driver's output queue
  are processed.
  Function returns zero if successful, otherwise it returns an error code.

  6.1.22. 

  int snd_rawmidi_flush_input( void *handle )

  This function flushes (destroys) input queue immediately.
  Function returns zero if successful, otherwise it returns an error code.

  6.1.23. 

  ssize_t snd_rawmidi_read( void *handle, void *buffer, size_t size )

  Reads bytes from input queue.  Function returns zero or positive value 
  if the read was successful (value represents count of bytes which were 
  successfully read from device) or negative error value if error occurred.
  Function will suspend the process if block mode is active.

  6.1.24. 

  ssize_t snd_rawmidi_write( void *handle, const void *buffer, size_t size )
                                          
  Writes bytes to the output queue.  Function returns zero or positive value
  if the write was successful (value represents count of bytes which were 
  successfully written to the device) or an error value if error occurred.
  Function will suspend the process if block mode is active.

  6.2.    Examples

  The following example shows how to send a control sequence (such as SysEx)
  to a MIDI device.
  Sound card #0 and rawmidi device #0 are used here:

  ______________________________________________________________________
  int card = 0, device = 0, err, fd, count, size;
  void *handle;
  snd_pcm_format_t format;
  char *buffer;

  buffer = (char *)malloc(64 * 1024);

  if (!buffer) return;

  if ((err = snd_rawmidi_open(&handle, card, device, 
                                       SND_RAWMIDI_OPEN_OUTPUT)) < 0) {
    fprintf(stderr, "open failed: %s\n", snd_strerror( err ));
    return;
  }

  if ((err = snd_rawmidi_block_mode(handle, 1)) < 0) {
    fprintf(stderr, "block failed: %s\n", snd_strerror( err ));
    snd_rawmidi_close(handle);
    return;
  }

  fd = open("/tmp/test.sysex", O_RDONLY);

  if (fd < 0) {
    perror("open file");
    snd_rawmidi_close(handle);
    return;

  }

  idx = 0;
  count = read(fd, buffer, 64 * 1024);
  if (count <= 0) {
    perror("read from file");
    snd_rawmidi_close(handle);
    return;
  }
  close(fd);

  size = snd_rawmidi_write(handle, &buffer, count);

  printf("Bytes written %i from %i...\n", size, count);

  snd_rawmidi_close(handle);
  free(buffer);

  ______________________________________________________________________


  7.      Timer Interface

  The timer interface description is currently on standby. When I get the
  chance to confirm that it is enabled in the current driver, I will 
  flesh this section out.
  
  7.1.    Low Level Interface

  7.1.1.  

  int snd_timer_open( void **handle )

  ////TODO

  7.1.2.  

  int snd_timer_close( void *handle )

  ////TODO

  7.1.3.  

  int snd_timer_file_descriptor( void *handle )

  ////TODO

  7.1.4.  

  int snd_timer_general_info( void *handle, snd_timer_general_info_t *info ) 
                                          
  ////TODO

  7.1.5.  

  int snd_timer_select( void *handle, snd_timer_select_t *tselect )

  ////TODO

  7.1.6.  

  int snd_timer_info( void *handle, snd_timer_info_t *info )

  ////TODO

  7.1.7.  

  int snd_timer_params( void *handle, snd_timer_params_t *params )

  ////TODO

  7.1.11. 

  int snd_timer_status( void *handle, snd_timer_status_t *status )

  ////TODO

  7.1.8.  

  int snd_timer_start( void *handle )

  ////TODO

  7.1.9.  

  int snd_timer_stop( void *handle )

  ////TODO

  7.1.10. 

  int snd_timer_continue( void *handle )

  ////TODO

  7.1.12. 

  ssize_t snd_timer_read( void *handle, void *buffer, size_t size ) 

  ////TODO

  7.2.    Examples

  ////TODO
