asoundlib.h revision c98da79067d89d934c4eb7de6a03e412eb421a5c
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#include <stddef.h>
34
35#if defined(__cplusplus)
36extern "C" {
37#endif
38
39/*
40 * PCM API
41 */
42
43struct pcm;
44
45#define PCM_OUT        0x00000000
46#define PCM_IN         0x10000000
47#define PCM_MMAP       0x00000001
48#define PCM_NOIRQ      0x00000002
49#define PCM_NORESTART  0x00000004 /* PCM_NORESTART - when set, calls to
50                                   * pcm_write for a playback stream will not
51                                   * attempt to restart the stream in the case
52                                   * of an underflow, but will return -EPIPE
53                                   * instead.  After the first -EPIPE error, the
54                                   * stream is considered to be stopped, and a
55                                   * second call to pcm_write will attempt to
56                                   * restart the stream.
57                                   */
58#define PCM_MONOTONIC  0x00000008 /* see pcm_get_htimestamp */
59
60/* PCM runtime states */
61#define	PCM_STATE_OPEN		0
62#define	PCM_STATE_SETUP		1
63#define	PCM_STATE_PREPARED	2
64#define	PCM_STATE_RUNNING		3
65#define	PCM_STATE_XRUN		4
66#define	PCM_STATE_DRAINING	5
67#define	PCM_STATE_PAUSED		6
68#define	PCM_STATE_SUSPENDED	7
69#define	PCM_STATE_DISCONNECTED	8
70
71/* Bit formats */
72enum pcm_format {
73    PCM_FORMAT_S16_LE = 0,
74    PCM_FORMAT_S32_LE,
75    PCM_FORMAT_S8,
76    PCM_FORMAT_S24_LE,
77
78    PCM_FORMAT_MAX,
79};
80
81/* Configuration for a stream */
82struct pcm_config {
83    unsigned int channels;
84    unsigned int rate;
85    unsigned int period_size;
86    unsigned int period_count;
87    enum pcm_format format;
88
89    /* Values to use for the ALSA start, stop and silence thresholds.  Setting
90     * any one of these values to 0 will cause the default tinyalsa values to be
91     * used instead.  Tinyalsa defaults are as follows.
92     *
93     * start_threshold   : period_count * period_size
94     * stop_threshold    : period_count * period_size
95     * silence_threshold : 0
96     */
97    unsigned int start_threshold;
98    unsigned int stop_threshold;
99    unsigned int silence_threshold;
100
101    /* Minimum number of frames available before pcm_mmap_write() will actually
102     * write into the kernel buffer. Only used if the stream is opened in mmap mode
103     * (pcm_open() called with PCM_MMAP flag set).   Use 0 for default.
104     */
105    int avail_min;
106};
107
108/* PCM parameters */
109enum pcm_param
110{
111    PCM_PARAM_SAMPLE_BITS,
112    PCM_PARAM_FRAME_BITS,
113    PCM_PARAM_CHANNELS,
114    PCM_PARAM_RATE,
115    PCM_PARAM_PERIOD_TIME,
116    PCM_PARAM_PERIOD_SIZE,
117    PCM_PARAM_PERIOD_BYTES,
118    PCM_PARAM_PERIODS,
119    PCM_PARAM_BUFFER_TIME,
120    PCM_PARAM_BUFFER_SIZE,
121    PCM_PARAM_BUFFER_BYTES,
122    PCM_PARAM_TICK_TIME,
123};
124
125/* Mixer control types */
126enum mixer_ctl_type {
127    MIXER_CTL_TYPE_BOOL,
128    MIXER_CTL_TYPE_INT,
129    MIXER_CTL_TYPE_ENUM,
130    MIXER_CTL_TYPE_BYTE,
131    MIXER_CTL_TYPE_IEC958,
132    MIXER_CTL_TYPE_INT64,
133    MIXER_CTL_TYPE_UNKNOWN,
134
135    MIXER_CTL_TYPE_MAX,
136};
137
138/* Open and close a stream */
139struct pcm *pcm_open(unsigned int card, unsigned int device,
140                     unsigned int flags, struct pcm_config *config);
141int pcm_close(struct pcm *pcm);
142int pcm_is_ready(struct pcm *pcm);
143
144/* Obtain the parameters for a PCM */
145struct pcm_params *pcm_params_get(unsigned int card, unsigned int device,
146                                  unsigned int flags);
147void pcm_params_free(struct pcm_params *pcm_params);
148unsigned int pcm_params_get_min(struct pcm_params *pcm_params,
149                                enum pcm_param param);
150unsigned int pcm_params_get_max(struct pcm_params *pcm_params,
151                                enum pcm_param param);
152
153/* Set and get config */
154int pcm_get_config(struct pcm *pcm, struct pcm_config *config);
155int pcm_set_config(struct pcm *pcm, struct pcm_config *config);
156
157/* Returns a human readable reason for the last error */
158const char *pcm_get_error(struct pcm *pcm);
159
160/* Returns the sample size in bits for a PCM format.
161 * As with ALSA formats, this is the storage size for the format, whereas the
162 * format represents the number of significant bits. For example,
163 * PCM_FORMAT_S24_LE uses 32 bits of storage.
164 */
165unsigned int pcm_format_to_bits(enum pcm_format format);
166
167/* Returns the buffer size (int frames) that should be used for pcm_write. */
168unsigned int pcm_get_buffer_size(struct pcm *pcm);
169unsigned int pcm_frames_to_bytes(struct pcm *pcm, unsigned int frames);
170unsigned int pcm_bytes_to_frames(struct pcm *pcm, unsigned int bytes);
171
172/* Returns the pcm latency in ms */
173unsigned int pcm_get_latency(struct pcm *pcm);
174
175/* Returns available frames in pcm buffer and corresponding time stamp.
176 * The clock is CLOCK_MONOTONIC if flag PCM_MONOTONIC was specified in pcm_open,
177 * otherwise the clock is CLOCK_REALTIME.
178 * For an input stream, frames available are frames ready for the
179 * application to read.
180 * For an output stream, frames available are the number of empty frames available
181 * for the application to write.
182 */
183int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
184                       struct timespec *tstamp);
185
186/* Write data to the fifo.
187 * Will start playback on the first write or on a write that
188 * occurs after a fifo underrun.
189 */
190int pcm_write(struct pcm *pcm, const void *data, unsigned int count);
191int pcm_read(struct pcm *pcm, void *data, unsigned int count);
192
193/*
194 * mmap() support.
195 */
196int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count);
197int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count);
198int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
199                   unsigned int *frames);
200int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames);
201
202/* Start and stop a PCM channel that doesn't transfer data */
203int pcm_start(struct pcm *pcm);
204int pcm_stop(struct pcm *pcm);
205
206/* Interrupt driven API */
207int pcm_wait(struct pcm *pcm, int timeout);
208
209/* Change avail_min after the stream has been opened with no need to stop the stream.
210 * Only accepted if opened with PCM_MMAP and PCM_NOIRQ flags
211 */
212int pcm_set_avail_min(struct pcm *pcm, int avail_min);
213
214/*
215 * MIXER API
216 */
217
218struct mixer;
219struct mixer_ctl;
220
221/* Open and close a mixer */
222struct mixer *mixer_open(unsigned int card);
223void mixer_close(struct mixer *mixer);
224
225/* Get info about a mixer */
226const char *mixer_get_name(struct mixer *mixer);
227
228/* Obtain mixer controls */
229unsigned int mixer_get_num_ctls(struct mixer *mixer);
230struct mixer_ctl *mixer_get_ctl(struct mixer *mixer, unsigned int id);
231struct mixer_ctl *mixer_get_ctl_by_name(struct mixer *mixer, const char *name);
232
233/* Get info about mixer controls */
234const char *mixer_ctl_get_name(struct mixer_ctl *ctl);
235enum mixer_ctl_type mixer_ctl_get_type(struct mixer_ctl *ctl);
236const char *mixer_ctl_get_type_string(struct mixer_ctl *ctl);
237unsigned int mixer_ctl_get_num_values(struct mixer_ctl *ctl);
238unsigned int mixer_ctl_get_num_enums(struct mixer_ctl *ctl);
239const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl,
240                                      unsigned int enum_id);
241
242/* Some sound cards update their controls due to external events,
243 * such as HDMI EDID byte data changing when an HDMI cable is
244 * connected. This API allows the count of elements to be updated.
245 */
246void mixer_ctl_update(struct mixer_ctl *ctl);
247
248/* Set and get mixer controls */
249int mixer_ctl_get_percent(struct mixer_ctl *ctl, unsigned int id);
250int mixer_ctl_set_percent(struct mixer_ctl *ctl, unsigned int id, int percent);
251
252int mixer_ctl_get_value(struct mixer_ctl *ctl, unsigned int id);
253int mixer_ctl_get_array(struct mixer_ctl *ctl, void *array, size_t count);
254int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value);
255int mixer_ctl_set_array(struct mixer_ctl *ctl, const void *array, size_t count);
256int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string);
257
258/* Determe range of integer mixer controls */
259int mixer_ctl_get_range_min(struct mixer_ctl *ctl);
260int mixer_ctl_get_range_max(struct mixer_ctl *ctl);
261
262#if defined(__cplusplus)
263}  /* extern "C" */
264#endif
265
266#endif
267