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)
35extern "C" {
36#endif
37
38/*
39 * PCM API
40 */
41
42struct pcm;
43
44#define PCM_OUT        0x00000000
45#define PCM_IN         0x10000000
46#define PCM_MMAP       0x00000001
47#define PCM_NOIRQ      0x00000002
48#define PCM_NORESTART  0x00000004 /* PCM_NORESTART - when set, calls to
49                                   * pcm_write for a playback stream will not
50                                   * attempt to restart the stream in the case
51                                   * of an underflow, but will return -EPIPE
52                                   * instead.  After the first -EPIPE error, the
53                                   * stream is considered to be stopped, and a
54                                   * second call to pcm_write will attempt to
55                                   * restart the stream.
56                                   */
57
58/* PCM runtime states */
59#define	PCM_STATE_OPEN		0
60#define	PCM_STATE_SETUP		1
61#define	PCM_STATE_PREPARED	2
62#define	PCM_STATE_RUNNING		3
63#define	PCM_STATE_XRUN		4
64#define	PCM_STATE_DRAINING	5
65#define	PCM_STATE_PAUSED		6
66#define	PCM_STATE_SUSPENDED	7
67#define	PCM_STATE_DISCONNECTED	8
68
69/* Bit formats */
70enum pcm_format {
71    PCM_FORMAT_S16_LE = 0,
72    PCM_FORMAT_S32_LE,
73
74    PCM_FORMAT_MAX,
75};
76
77/* Configuration for a stream */
78struct pcm_config {
79    unsigned int channels;
80    unsigned int rate;
81    unsigned int period_size;
82    unsigned int period_count;
83    enum pcm_format format;
84
85    /* Values to use for the ALSA start, stop and silence thresholds.  Setting
86     * any one of these values to 0 will cause the default tinyalsa values to be
87     * used instead.  Tinyalsa defaults are as follows.
88     *
89     * start_threshold   : period_count * period_size
90     * stop_threshold    : period_count * period_size
91     * silence_threshold : 0
92     */
93    unsigned int start_threshold;
94    unsigned int stop_threshold;
95    unsigned int silence_threshold;
96
97    /* Minimum number of frames available before pcm_mmap_write() will actually
98     * write into the kernel buffer. Only used if the stream is opened in mmap mode
99     * (pcm_open() called with PCM_MMAP flag set).   Use 0 for default.
100     */
101    int avail_min;
102};
103
104/* Mixer control types */
105enum mixer_ctl_type {
106    MIXER_CTL_TYPE_BOOL,
107    MIXER_CTL_TYPE_INT,
108    MIXER_CTL_TYPE_ENUM,
109    MIXER_CTL_TYPE_BYTE,
110    MIXER_CTL_TYPE_IEC958,
111    MIXER_CTL_TYPE_INT64,
112    MIXER_CTL_TYPE_UNKNOWN,
113
114    MIXER_CTL_TYPE_MAX,
115};
116
117/* Open and close a stream */
118struct pcm *pcm_open(unsigned int card, unsigned int device,
119                     unsigned int flags, struct pcm_config *config);
120int pcm_close(struct pcm *pcm);
121int pcm_is_ready(struct pcm *pcm);
122
123/* Set and get config */
124int pcm_get_config(struct pcm *pcm, struct pcm_config *config);
125int pcm_set_config(struct pcm *pcm, struct pcm_config *config);
126
127/* Returns a human readable reason for the last error */
128const char *pcm_get_error(struct pcm *pcm);
129
130/* Returns the buffer size (int frames) that should be used for pcm_write. */
131unsigned int pcm_get_buffer_size(struct pcm *pcm);
132unsigned int pcm_frames_to_bytes(struct pcm *pcm, unsigned int frames);
133unsigned int pcm_bytes_to_frames(struct pcm *pcm, unsigned int bytes);
134
135/* Returns the pcm latency in ms */
136unsigned int pcm_get_latency(struct pcm *pcm);
137
138/* Returns available frames in pcm buffer and corresponding time stamp.
139 * For an input stream, frames available are frames ready for the
140 * application to read.
141 * For an output stream, frames available are the number of empty frames available
142 * for the application to write.
143 */
144int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
145                       struct timespec *tstamp);
146
147/* Write data to the fifo.
148 * Will start playback on the first write or on a write that
149 * occurs after a fifo underrun.
150 */
151int pcm_write(struct pcm *pcm, const void *data, unsigned int count);
152int pcm_read(struct pcm *pcm, void *data, unsigned int count);
153
154/*
155 * mmap() support.
156 */
157int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count);
158int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
159                   unsigned int *frames);
160int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames);
161
162/* Start and stop a PCM channel that doesn't transfer data */
163int pcm_start(struct pcm *pcm);
164int pcm_stop(struct pcm *pcm);
165
166/* Change avail_min after the stream has been opened with no need to stop the stream.
167 * Only accepted if opened with PCM_MMAP and PCM_NOIRQ flags
168 */
169int pcm_set_avail_min(struct pcm *pcm, int avail_min);
170
171/*
172 * MIXER API
173 */
174
175struct mixer;
176struct mixer_ctl;
177
178/* Open and close a mixer */
179struct mixer *mixer_open(unsigned int card);
180void mixer_close(struct mixer *mixer);
181
182/* Obtain mixer controls */
183unsigned int mixer_get_num_ctls(struct mixer *mixer);
184struct mixer_ctl *mixer_get_ctl(struct mixer *mixer, unsigned int id);
185struct mixer_ctl *mixer_get_ctl_by_name(struct mixer *mixer, const char *name);
186
187/* Get info about mixer controls */
188const char *mixer_ctl_get_name(struct mixer_ctl *ctl);
189enum mixer_ctl_type mixer_ctl_get_type(struct mixer_ctl *ctl);
190const char *mixer_ctl_get_type_string(struct mixer_ctl *ctl);
191unsigned int mixer_ctl_get_num_values(struct mixer_ctl *ctl);
192unsigned int mixer_ctl_get_num_enums(struct mixer_ctl *ctl);
193const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl,
194                                      unsigned int enum_id);
195
196/* Set and get mixer controls */
197int mixer_ctl_get_percent(struct mixer_ctl *ctl, unsigned int id);
198int mixer_ctl_set_percent(struct mixer_ctl *ctl, unsigned int id, int percent);
199
200int mixer_ctl_get_value(struct mixer_ctl *ctl, unsigned int id);
201int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value);
202int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string);
203
204/* Determe range of integer mixer controls */
205int mixer_ctl_get_range_min(struct mixer_ctl *ctl);
206int mixer_ctl_get_range_max(struct mixer_ctl *ctl);
207
208#if defined(__cplusplus)
209}  /* extern "C" */
210#endif
211
212#endif
213