Home | History | Annotate | Download | only in tinyalsa
      1 /* asoundlib.h
      2 **
      3 ** Copyright 2011, The Android Open Source Project
      4 **
      5 ** Redistribution and use in source and binary forms, with or without
      6 ** modification, are permitted provided that the following conditions are met:
      7 **     * Redistributions of source code must retain the above copyright
      8 **       notice, this list of conditions and the following disclaimer.
      9 **     * Redistributions in binary form must reproduce the above copyright
     10 **       notice, this list of conditions and the following disclaimer in the
     11 **       documentation and/or other materials provided with the distribution.
     12 **     * Neither the name of The Android Open Source Project nor the names of
     13 **       its contributors may be used to endorse or promote products derived
     14 **       from this software without specific prior written permission.
     15 **
     16 ** THIS SOFTWARE IS PROVIDED BY The Android Open Source Project ``AS IS'' AND
     17 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19 ** ARE DISCLAIMED. IN NO EVENT SHALL The Android Open Source Project BE LIABLE
     20 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     22 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     23 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
     26 ** DAMAGE.
     27 */
     28 
     29 #ifndef ASOUNDLIB_H
     30 #define ASOUNDLIB_H
     31 
     32 #include <sys/time.h>
     33 
     34 #if defined(__cplusplus)
     35 extern "C" {
     36 #endif
     37 
     38 /*
     39  * PCM API
     40  */
     41 
     42 struct pcm;
     43 
     44 #define PCM_OUT        0x00000000
     45 #define PCM_IN         0x10000000
     46 #define PCM_MMAP       0x00000001
     47 #define PCM_NOIRQ      0x00000002
     48 
     49 /* PCM runtime states */
     50 #define	PCM_STATE_OPEN		0
     51 #define	PCM_STATE_SETUP		1
     52 #define	PCM_STATE_PREPARED	2
     53 #define	PCM_STATE_RUNNING		3
     54 #define	PCM_STATE_XRUN		4
     55 #define	PCM_STATE_DRAINING	5
     56 #define	PCM_STATE_PAUSED		6
     57 #define	PCM_STATE_SUSPENDED	7
     58 #define	PCM_STATE_DISCONNECTED	8
     59 
     60 /* Bit formats */
     61 enum pcm_format {
     62     PCM_FORMAT_S16_LE = 0,
     63     PCM_FORMAT_S32_LE,
     64 
     65     PCM_FORMAT_MAX,
     66 };
     67 
     68 /* Configuration for a stream */
     69 struct pcm_config {
     70     unsigned int channels;
     71     unsigned int rate;
     72     unsigned int period_size;
     73     unsigned int period_count;
     74     enum pcm_format format;
     75 
     76     /* Values to use for the ALSA start, stop and silence thresholds.  Setting
     77      * any one of these values to 0 will cause the default tinyalsa values to be
     78      * used instead.  Tinyalsa defaults are as follows.
     79      *
     80      * start_threshold   : period_count * period_size
     81      * stop_threshold    : period_count * period_size
     82      * silence_threshold : 0
     83      */
     84     unsigned int start_threshold;
     85     unsigned int stop_threshold;
     86     unsigned int silence_threshold;
     87     int avail_min;
     88 };
     89 
     90 /* Mixer control types */
     91 enum mixer_ctl_type {
     92     MIXER_CTL_TYPE_BOOL,
     93     MIXER_CTL_TYPE_INT,
     94     MIXER_CTL_TYPE_ENUM,
     95     MIXER_CTL_TYPE_BYTE,
     96     MIXER_CTL_TYPE_IEC958,
     97     MIXER_CTL_TYPE_INT64,
     98     MIXER_CTL_TYPE_UNKNOWN,
     99 
    100     MIXER_CTL_TYPE_MAX,
    101 };
    102 
    103 /* Open and close a stream */
    104 struct pcm *pcm_open(unsigned int card, unsigned int device,
    105                      unsigned int flags, struct pcm_config *config);
    106 int pcm_close(struct pcm *pcm);
    107 int pcm_is_ready(struct pcm *pcm);
    108 
    109 /* Set and get config */
    110 int pcm_get_config(struct pcm *pcm, struct pcm_config *config);
    111 int pcm_set_config(struct pcm *pcm, struct pcm_config *config);
    112 
    113 /* Returns a human readable reason for the last error */
    114 const char *pcm_get_error(struct pcm *pcm);
    115 
    116 /* Returns the buffer size (int frames) that should be used for pcm_write. */
    117 unsigned int pcm_get_buffer_size(struct pcm *pcm);
    118 unsigned int pcm_frames_to_bytes(struct pcm *pcm, unsigned int frames);
    119 unsigned int pcm_bytes_to_frames(struct pcm *pcm, unsigned int bytes);
    120 
    121 /* Returns the pcm latency in ms */
    122 unsigned int pcm_get_latency(struct pcm *pcm);
    123 
    124 /* Returns available frames in pcm buffer and corresponding time stamp.
    125  * For an input stream, frames available are frames ready for the
    126  * application to read.
    127  * For an output stream, frames available are the number of empty frames available
    128  * for the application to write.
    129  */
    130 int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
    131                        struct timespec *tstamp);
    132 
    133 /* Write data to the fifo.
    134  * Will start playback on the first write or on a write that
    135  * occurs after a fifo underrun.
    136  */
    137 int pcm_write(struct pcm *pcm, void *data, unsigned int count);
    138 int pcm_read(struct pcm *pcm, void *data, unsigned int count);
    139 
    140 /*
    141  * mmap() support.
    142  */
    143 int pcm_mmap_write(struct pcm *pcm, void *data, unsigned int count);
    144 int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
    145                    unsigned int *frames);
    146 int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames);
    147 
    148 /* Start and stop a PCM channel that doesn't transfer data */
    149 int pcm_start(struct pcm *pcm);
    150 int pcm_stop(struct pcm *pcm);
    151 
    152 /* Change avail_min after the stream has been opened with no need to stop the stream.
    153  * Only accepted if opened with PCM_MMAP and PCM_NOIRQ flags
    154  */
    155 int pcm_set_avail_min(struct pcm *pcm, int avail_min);
    156 
    157 /*
    158  * MIXER API
    159  */
    160 
    161 struct mixer;
    162 struct mixer_ctl;
    163 
    164 /* Open and close a mixer */
    165 struct mixer *mixer_open(unsigned int card);
    166 void mixer_close(struct mixer *mixer);
    167 
    168 /* Obtain mixer controls */
    169 unsigned int mixer_get_num_ctls(struct mixer *mixer);
    170 struct mixer_ctl *mixer_get_ctl(struct mixer *mixer, unsigned int id);
    171 struct mixer_ctl *mixer_get_ctl_by_name(struct mixer *mixer, const char *name);
    172 
    173 /* Get info about mixer controls */
    174 int mixer_ctl_get_name(struct mixer_ctl *ctl, char *name, unsigned int size);
    175 enum mixer_ctl_type mixer_ctl_get_type(struct mixer_ctl *ctl);
    176 const char *mixer_ctl_get_type_string(struct mixer_ctl *ctl);
    177 unsigned int mixer_ctl_get_num_values(struct mixer_ctl *ctl);
    178 unsigned int mixer_ctl_get_num_enums(struct mixer_ctl *ctl);
    179 int mixer_ctl_get_enum_string(struct mixer_ctl *ctl, unsigned int enum_id,
    180                               char *string, unsigned int size);
    181 
    182 /* Set and get mixer controls */
    183 int mixer_ctl_get_percent(struct mixer_ctl *ctl, unsigned int id);
    184 int mixer_ctl_set_percent(struct mixer_ctl *ctl, unsigned int id, int percent);
    185 
    186 int mixer_ctl_get_value(struct mixer_ctl *ctl, unsigned int id);
    187 int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value);
    188 int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string);
    189 
    190 /* Determe range of integer mixer controls */
    191 int mixer_ctl_get_range_min(struct mixer_ctl *ctl);
    192 int mixer_ctl_get_range_max(struct mixer_ctl *ctl);
    193 
    194 #if defined(__cplusplus)
    195 }  /* extern "C" */
    196 #endif
    197 
    198 #endif
    199