blob: b7895733e8db6bc59e62360648469d5ab669ccd5 [file] [log] [blame]
Simon Wilsonedff7082011-06-06 15:33:34 -07001/* 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
Simon Wilsondd88f132011-07-25 10:58:30 -070032#include <sys/time.h>
Simon Wilsonda39e0b2012-11-09 15:16:47 -080033#include <stddef.h>
Simon Wilsonedff7082011-06-06 15:33:34 -070034
Simon Wilsonc1239622011-07-27 15:08:34 -070035#if defined(__cplusplus)
36extern "C" {
37#endif
38
Simon Wilsonedff7082011-06-06 15:33:34 -070039/*
40 * PCM API
41 */
42
43struct pcm;
44
45#define PCM_OUT 0x00000000
46#define PCM_IN 0x10000000
Simon Wilsone9942c82011-10-13 13:57:25 -070047#define PCM_MMAP 0x00000001
48#define PCM_NOIRQ 0x00000002
John Grossman673253a2012-04-03 16:37:38 -070049#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 */
Glenn Kasten6b0a2062013-08-22 15:11:48 -070058#define PCM_MONOTONIC 0x00000008 /* see pcm_get_htimestamp */
Simon Wilsone9942c82011-10-13 13:57:25 -070059
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
Simon Wilsonedff7082011-06-06 15:33:34 -070070
71/* Bit formats */
72enum pcm_format {
Glenn Kasten88638162014-07-15 10:52:31 -070073 PCM_FORMAT_INVALID = -1,
Paul McLeanb25ece42014-03-21 15:27:34 -070074 PCM_FORMAT_S16_LE = 0, /* 16-bit signed */
75 PCM_FORMAT_S32_LE, /* 32-bit signed */
76 PCM_FORMAT_S8, /* 8-bit signed */
77 PCM_FORMAT_S24_LE, /* 24-bits in 4-bytes */
78 PCM_FORMAT_S24_3LE, /* 24-bits in 3-bytes */
Simon Wilsonedff7082011-06-06 15:33:34 -070079
80 PCM_FORMAT_MAX,
81};
82
Andy Hunga5b44d92014-03-10 18:08:15 -070083/* Bitmask has 256 bits (32 bytes) in asound.h */
84struct pcm_mask {
85 unsigned int bits[32 / sizeof(unsigned int)];
86};
87
Simon Wilsonedff7082011-06-06 15:33:34 -070088/* Configuration for a stream */
89struct pcm_config {
90 unsigned int channels;
91 unsigned int rate;
92 unsigned int period_size;
93 unsigned int period_count;
94 enum pcm_format format;
Simon Wilsonc1239622011-07-27 15:08:34 -070095
Maneet Singhe25fe0b2015-06-11 17:34:40 -070096 /* Values to use for the ALSA start, stop and silence thresholds, and
97 * silence size. Setting any one of these values to 0 will cause the
98 * default tinyalsa values to be used instead.
99 * Tinyalsa defaults are as follows.
Simon Wilsonc1239622011-07-27 15:08:34 -0700100 *
101 * start_threshold : period_count * period_size
102 * stop_threshold : period_count * period_size
103 * silence_threshold : 0
Maneet Singhe25fe0b2015-06-11 17:34:40 -0700104 * silence_size : 0
Simon Wilsonc1239622011-07-27 15:08:34 -0700105 */
106 unsigned int start_threshold;
107 unsigned int stop_threshold;
108 unsigned int silence_threshold;
Christopher R. Palmer6240f8d2015-11-28 07:32:13 -0500109#ifndef IGNORE_SILENCE_SIZE
Maneet Singhe25fe0b2015-06-11 17:34:40 -0700110 unsigned int silence_size;
Christopher R. Palmer6240f8d2015-11-28 07:32:13 -0500111#endif
Glenn Kasten4c0f7e82012-01-19 13:35:28 -0800112
113 /* Minimum number of frames available before pcm_mmap_write() will actually
114 * write into the kernel buffer. Only used if the stream is opened in mmap mode
115 * (pcm_open() called with PCM_MMAP flag set). Use 0 for default.
116 */
Eric Laurent73b9c672011-10-14 11:12:24 -0700117 int avail_min;
Simon Wilsonedff7082011-06-06 15:33:34 -0700118};
119
Simon Wilson42fc2d32012-12-03 11:18:57 -0800120/* PCM parameters */
121enum pcm_param
122{
Andy Hunga5b44d92014-03-10 18:08:15 -0700123 /* mask parameters */
124 PCM_PARAM_ACCESS,
125 PCM_PARAM_FORMAT,
126 PCM_PARAM_SUBFORMAT,
127 /* interval parameters */
Simon Wilson42fc2d32012-12-03 11:18:57 -0800128 PCM_PARAM_SAMPLE_BITS,
129 PCM_PARAM_FRAME_BITS,
130 PCM_PARAM_CHANNELS,
131 PCM_PARAM_RATE,
132 PCM_PARAM_PERIOD_TIME,
133 PCM_PARAM_PERIOD_SIZE,
134 PCM_PARAM_PERIOD_BYTES,
135 PCM_PARAM_PERIODS,
136 PCM_PARAM_BUFFER_TIME,
137 PCM_PARAM_BUFFER_SIZE,
138 PCM_PARAM_BUFFER_BYTES,
139 PCM_PARAM_TICK_TIME,
140};
141
Simon Wilsonedff7082011-06-06 15:33:34 -0700142/* Mixer control types */
143enum mixer_ctl_type {
144 MIXER_CTL_TYPE_BOOL,
145 MIXER_CTL_TYPE_INT,
146 MIXER_CTL_TYPE_ENUM,
147 MIXER_CTL_TYPE_BYTE,
148 MIXER_CTL_TYPE_IEC958,
149 MIXER_CTL_TYPE_INT64,
150 MIXER_CTL_TYPE_UNKNOWN,
151
152 MIXER_CTL_TYPE_MAX,
153};
154
155/* Open and close a stream */
156struct pcm *pcm_open(unsigned int card, unsigned int device,
157 unsigned int flags, struct pcm_config *config);
158int pcm_close(struct pcm *pcm);
159int pcm_is_ready(struct pcm *pcm);
160
Simon Wilson42fc2d32012-12-03 11:18:57 -0800161/* Obtain the parameters for a PCM */
162struct pcm_params *pcm_params_get(unsigned int card, unsigned int device,
163 unsigned int flags);
164void pcm_params_free(struct pcm_params *pcm_params);
Andy Hunga5b44d92014-03-10 18:08:15 -0700165
166struct pcm_mask *pcm_params_get_mask(struct pcm_params *pcm_params,
Andy Hung70530a62014-03-26 18:29:07 -0700167 enum pcm_param param);
Simon Wilson42fc2d32012-12-03 11:18:57 -0800168unsigned int pcm_params_get_min(struct pcm_params *pcm_params,
169 enum pcm_param param);
Paul McLeanb25ece42014-03-21 15:27:34 -0700170void pcm_params_set_min(struct pcm_params *pcm_params,
171 enum pcm_param param, unsigned int val);
Simon Wilson42fc2d32012-12-03 11:18:57 -0800172unsigned int pcm_params_get_max(struct pcm_params *pcm_params,
173 enum pcm_param param);
Paul McLeanb25ece42014-03-21 15:27:34 -0700174void pcm_params_set_max(struct pcm_params *pcm_params,
175 enum pcm_param param, unsigned int val);
Simon Wilson42fc2d32012-12-03 11:18:57 -0800176
Andy Hung70530a62014-03-26 18:29:07 -0700177/* Converts the pcm parameters to a human readable string.
178 * The string parameter is a caller allocated buffer of size bytes,
179 * which is then filled up to size - 1 and null terminated,
180 * if size is greater than zero.
181 * The return value is the number of bytes copied to string
182 * (not including null termination) if less than size; otherwise,
183 * the number of bytes required for the buffer.
184 */
185int pcm_params_to_string(struct pcm_params *params, char *string, unsigned int size);
186
187/* Returns 1 if the pcm_format is present (format bit set) in
188 * the pcm_params structure; 0 otherwise, or upon unrecognized format.
189 */
190int pcm_params_format_test(struct pcm_params *params, enum pcm_format format);
191
Simon Wilsonedff7082011-06-06 15:33:34 -0700192/* Set and get config */
193int pcm_get_config(struct pcm *pcm, struct pcm_config *config);
194int pcm_set_config(struct pcm *pcm, struct pcm_config *config);
195
196/* Returns a human readable reason for the last error */
197const char *pcm_get_error(struct pcm *pcm);
198
Simon Wilson36ea2d82013-07-17 11:10:45 -0700199/* Returns the sample size in bits for a PCM format.
200 * As with ALSA formats, this is the storage size for the format, whereas the
201 * format represents the number of significant bits. For example,
202 * PCM_FORMAT_S24_LE uses 32 bits of storage.
203 */
204unsigned int pcm_format_to_bits(enum pcm_format format);
205
Simon Wilson5aed71d2011-11-16 14:45:38 -0800206/* Returns the buffer size (int frames) that should be used for pcm_write. */
Simon Wilsonedff7082011-06-06 15:33:34 -0700207unsigned int pcm_get_buffer_size(struct pcm *pcm);
Simon Wilsone9942c82011-10-13 13:57:25 -0700208unsigned int pcm_frames_to_bytes(struct pcm *pcm, unsigned int frames);
Simon Wilson5aed71d2011-11-16 14:45:38 -0800209unsigned int pcm_bytes_to_frames(struct pcm *pcm, unsigned int bytes);
Simon Wilsonedff7082011-06-06 15:33:34 -0700210
211/* Returns the pcm latency in ms */
212unsigned int pcm_get_latency(struct pcm *pcm);
213
Simon Wilsondd88f132011-07-25 10:58:30 -0700214/* Returns available frames in pcm buffer and corresponding time stamp.
Glenn Kasten6b0a2062013-08-22 15:11:48 -0700215 * The clock is CLOCK_MONOTONIC if flag PCM_MONOTONIC was specified in pcm_open,
216 * otherwise the clock is CLOCK_REALTIME.
Simon Wilsondd88f132011-07-25 10:58:30 -0700217 * For an input stream, frames available are frames ready for the
218 * application to read.
219 * For an output stream, frames available are the number of empty frames available
220 * for the application to write.
221 */
222int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
223 struct timespec *tstamp);
224
Simon Wilsonedff7082011-06-06 15:33:34 -0700225/* Write data to the fifo.
226 * Will start playback on the first write or on a write that
227 * occurs after a fifo underrun.
228 */
Simon Wilsondaa83292012-02-28 15:26:02 -0800229int pcm_write(struct pcm *pcm, const void *data, unsigned int count);
Simon Wilsonedff7082011-06-06 15:33:34 -0700230int pcm_read(struct pcm *pcm, void *data, unsigned int count);
231
Simon Wilsone9942c82011-10-13 13:57:25 -0700232/*
233 * mmap() support.
234 */
Simon Wilsondaa83292012-02-28 15:26:02 -0800235int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count);
Eric Laurentc98da792013-09-16 14:31:17 -0700236int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count);
Simon Wilsone9942c82011-10-13 13:57:25 -0700237int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
238 unsigned int *frames);
239int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames);
Dylan Reid9074cfc2015-09-01 11:01:57 -0700240int pcm_mmap_avail(struct pcm *pcm);
Simon Wilsone9942c82011-10-13 13:57:25 -0700241
Omair Mohammed Abdullah7a0b9952013-01-31 16:35:39 +0530242/* Prepare the PCM substream to be triggerable */
243int pcm_prepare(struct pcm *pcm);
Simon Wilson70d77082011-06-24 11:08:10 -0700244/* Start and stop a PCM channel that doesn't transfer data */
245int pcm_start(struct pcm *pcm);
246int pcm_stop(struct pcm *pcm);
247
Shiv Maliyappanahalli892d6a52014-05-23 15:20:13 -0700248/* ioctl function for PCM driver */
249int pcm_ioctl(struct pcm *pcm, int request, ...);
250
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800251/* Interrupt driven API */
252int pcm_wait(struct pcm *pcm, int timeout);
Dylan Reidcefba1e2015-09-01 11:03:01 -0700253int pcm_get_poll_fd(struct pcm *pcm);
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800254
Eric Laurent73b9c672011-10-14 11:12:24 -0700255/* Change avail_min after the stream has been opened with no need to stop the stream.
256 * Only accepted if opened with PCM_MMAP and PCM_NOIRQ flags
257 */
258int pcm_set_avail_min(struct pcm *pcm, int avail_min);
Simon Wilsonedff7082011-06-06 15:33:34 -0700259
260/*
261 * MIXER API
262 */
263
264struct mixer;
265struct mixer_ctl;
266
267/* Open and close a mixer */
268struct mixer *mixer_open(unsigned int card);
269void mixer_close(struct mixer *mixer);
270
Simon Wilson7e8a6562013-06-28 16:47:16 -0700271/* Get info about a mixer */
272const char *mixer_get_name(struct mixer *mixer);
273
Simon Wilsonedff7082011-06-06 15:33:34 -0700274/* Obtain mixer controls */
275unsigned int mixer_get_num_ctls(struct mixer *mixer);
276struct mixer_ctl *mixer_get_ctl(struct mixer *mixer, unsigned int id);
277struct mixer_ctl *mixer_get_ctl_by_name(struct mixer *mixer, const char *name);
278
279/* Get info about mixer controls */
Simon Wilsone44e30a2012-03-08 10:45:31 -0800280const char *mixer_ctl_get_name(struct mixer_ctl *ctl);
Simon Wilsonedff7082011-06-06 15:33:34 -0700281enum mixer_ctl_type mixer_ctl_get_type(struct mixer_ctl *ctl);
282const char *mixer_ctl_get_type_string(struct mixer_ctl *ctl);
283unsigned int mixer_ctl_get_num_values(struct mixer_ctl *ctl);
284unsigned int mixer_ctl_get_num_enums(struct mixer_ctl *ctl);
Simon Wilsone44e30a2012-03-08 10:45:31 -0800285const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl,
286 unsigned int enum_id);
Simon Wilsonedff7082011-06-06 15:33:34 -0700287
Simon Wilson7e8a6562013-06-28 16:47:16 -0700288/* Some sound cards update their controls due to external events,
289 * such as HDMI EDID byte data changing when an HDMI cable is
290 * connected. This API allows the count of elements to be updated.
291 */
292void mixer_ctl_update(struct mixer_ctl *ctl);
293
Simon Wilsonedff7082011-06-06 15:33:34 -0700294/* Set and get mixer controls */
295int mixer_ctl_get_percent(struct mixer_ctl *ctl, unsigned int id);
296int mixer_ctl_set_percent(struct mixer_ctl *ctl, unsigned int id, int percent);
297
298int mixer_ctl_get_value(struct mixer_ctl *ctl, unsigned int id);
Simon Wilson8813fe82013-06-24 15:40:34 -0700299int mixer_ctl_get_array(struct mixer_ctl *ctl, void *array, size_t count);
Simon Wilsonedff7082011-06-06 15:33:34 -0700300int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value);
Simon Wilson8813fe82013-06-24 15:40:34 -0700301int mixer_ctl_set_array(struct mixer_ctl *ctl, const void *array, size_t count);
Simon Wilsonedff7082011-06-06 15:33:34 -0700302int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string);
303
304/* Determe range of integer mixer controls */
305int mixer_ctl_get_range_min(struct mixer_ctl *ctl);
306int mixer_ctl_get_range_max(struct mixer_ctl *ctl);
307
Simon Wilsonc1239622011-07-27 15:08:34 -0700308#if defined(__cplusplus)
309} /* extern "C" */
310#endif
311
Simon Wilsonedff7082011-06-06 15:33:34 -0700312#endif