blob: a32871ed3dda426c226758bdc2e4ae4c6a66a7ec [file] [log] [blame]
Simon Wilson79d39652011-05-25 13:44:23 -07001/* pcm.c
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#include <stdio.h>
30#include <stdlib.h>
31#include <fcntl.h>
32#include <stdarg.h>
33#include <string.h>
34#include <errno.h>
35#include <unistd.h>
Liam Girdwood6be28f12011-10-13 12:59:51 -070036#include <poll.h>
Simon Wilson79d39652011-05-25 13:44:23 -070037
38#include <sys/ioctl.h>
39#include <sys/mman.h>
40#include <sys/time.h>
Dima Krasner696c4482016-03-05 19:50:02 +020041#include <time.h>
Liam Girdwood6be28f12011-10-13 12:59:51 -070042#include <limits.h>
Simon Wilson79d39652011-05-25 13:44:23 -070043
44#include <linux/ioctl.h>
Taylor Holberton4c5a11d2018-11-28 14:29:52 -050045
46#ifndef __force
Simon Wilson79d39652011-05-25 13:44:23 -070047#define __force
Taylor Holberton4c5a11d2018-11-28 14:29:52 -050048#endif
49
50#ifndef __bitwise
Simon Wilson79d39652011-05-25 13:44:23 -070051#define __bitwise
Taylor Holberton4c5a11d2018-11-28 14:29:52 -050052#endif
53
54#ifndef __user
Simon Wilson79d39652011-05-25 13:44:23 -070055#define __user
Taylor Holberton4c5a11d2018-11-28 14:29:52 -050056#endif
57
Simon Wilson79d39652011-05-25 13:44:23 -070058#include <sound/asound.h>
59
Ricardo Biehl Pasquali04952ee2016-10-05 20:32:09 -030060#include <tinyalsa/pcm.h>
Taylor Holbertonea06b972017-04-06 23:14:14 -070061#include <tinyalsa/limits.h>
Simon Wilson79d39652011-05-25 13:44:23 -070062
Taylor Holberton1f741562018-11-28 16:33:24 -050063#ifndef PARAM_MAX
Simon Wilson79d39652011-05-25 13:44:23 -070064#define PARAM_MAX SNDRV_PCM_HW_PARAM_LAST_INTERVAL
Taylor Holberton1f741562018-11-28 16:33:24 -050065#endif /* PARAM_MAX */
66
67#ifndef SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP
Liam Girdwood6be28f12011-10-13 12:59:51 -070068#define SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP (1<<2)
Taylor Holberton1f741562018-11-28 16:33:24 -050069#endif /* SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP */
Simon Wilson79d39652011-05-25 13:44:23 -070070
71static inline int param_is_mask(int p)
72{
73 return (p >= SNDRV_PCM_HW_PARAM_FIRST_MASK) &&
74 (p <= SNDRV_PCM_HW_PARAM_LAST_MASK);
75}
76
77static inline int param_is_interval(int p)
78{
79 return (p >= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL) &&
80 (p <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL);
81}
82
Taylor Holberton2f387d22016-12-01 15:58:16 -080083static inline const struct snd_interval *param_get_interval(const struct snd_pcm_hw_params *p, int n)
84{
Taylor Holberton25976dc2017-04-10 11:46:40 -070085 return &(p->intervals[n - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL]);
Taylor Holberton2f387d22016-12-01 15:58:16 -080086}
87
Simon Wilson79d39652011-05-25 13:44:23 -070088static inline struct snd_interval *param_to_interval(struct snd_pcm_hw_params *p, int n)
89{
90 return &(p->intervals[n - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL]);
91}
92
93static inline struct snd_mask *param_to_mask(struct snd_pcm_hw_params *p, int n)
94{
95 return &(p->masks[n - SNDRV_PCM_HW_PARAM_FIRST_MASK]);
96}
97
98static void param_set_mask(struct snd_pcm_hw_params *p, int n, unsigned int bit)
99{
100 if (bit >= SNDRV_MASK_MAX)
101 return;
102 if (param_is_mask(n)) {
103 struct snd_mask *m = param_to_mask(p, n);
104 m->bits[0] = 0;
105 m->bits[1] = 0;
106 m->bits[bit >> 5] |= (1 << (bit & 31));
107 }
108}
109
110static void param_set_min(struct snd_pcm_hw_params *p, int n, unsigned int val)
111{
112 if (param_is_interval(n)) {
113 struct snd_interval *i = param_to_interval(p, n);
114 i->min = val;
115 }
116}
117
Taylor Holberton2f387d22016-12-01 15:58:16 -0800118static unsigned int param_get_min(const struct snd_pcm_hw_params *p, int n)
Simon Wilson43544882012-10-31 12:52:39 -0700119{
120 if (param_is_interval(n)) {
Taylor Holberton2f387d22016-12-01 15:58:16 -0800121 const struct snd_interval *i = param_get_interval(p, n);
Simon Wilson43544882012-10-31 12:52:39 -0700122 return i->min;
123 }
124 return 0;
125}
126
Taylor Holberton2f387d22016-12-01 15:58:16 -0800127static unsigned int param_get_max(const struct snd_pcm_hw_params *p, int n)
Simon Wilson43544882012-10-31 12:52:39 -0700128{
129 if (param_is_interval(n)) {
Taylor Holberton2f387d22016-12-01 15:58:16 -0800130 const struct snd_interval *i = param_get_interval(p, n);
Simon Wilson43544882012-10-31 12:52:39 -0700131 return i->max;
132 }
133 return 0;
134}
135
Simon Wilson79d39652011-05-25 13:44:23 -0700136static void param_set_int(struct snd_pcm_hw_params *p, int n, unsigned int val)
137{
138 if (param_is_interval(n)) {
139 struct snd_interval *i = param_to_interval(p, n);
140 i->min = val;
141 i->max = val;
142 i->integer = 1;
143 }
144}
145
Liam Girdwood6be28f12011-10-13 12:59:51 -0700146static unsigned int param_get_int(struct snd_pcm_hw_params *p, int n)
147{
148 if (param_is_interval(n)) {
149 struct snd_interval *i = param_to_interval(p, n);
150 if (i->integer)
151 return i->max;
152 }
153 return 0;
154}
155
Simon Wilson79d39652011-05-25 13:44:23 -0700156static void param_init(struct snd_pcm_hw_params *p)
157{
158 int n;
Simon Wilson98c1f162011-06-07 16:12:32 -0700159
Simon Wilson79d39652011-05-25 13:44:23 -0700160 memset(p, 0, sizeof(*p));
161 for (n = SNDRV_PCM_HW_PARAM_FIRST_MASK;
162 n <= SNDRV_PCM_HW_PARAM_LAST_MASK; n++) {
163 struct snd_mask *m = param_to_mask(p, n);
164 m->bits[0] = ~0;
165 m->bits[1] = ~0;
166 }
167 for (n = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL;
168 n <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; n++) {
169 struct snd_interval *i = param_to_interval(p, n);
170 i->min = 0;
171 i->max = ~0;
172 }
Simon Wilson43544882012-10-31 12:52:39 -0700173 p->rmask = ~0U;
174 p->cmask = 0;
175 p->info = ~0U;
Simon Wilson79d39652011-05-25 13:44:23 -0700176}
177
Taylor Holberton861da7a2017-04-10 12:05:51 -0700178static unsigned int pcm_format_to_alsa(enum pcm_format format)
179{
180 switch (format) {
181
182 case PCM_FORMAT_S8:
183 return SNDRV_PCM_FORMAT_S8;
184
185 default:
186 case PCM_FORMAT_S16_LE:
187 return SNDRV_PCM_FORMAT_S16_LE;
188 case PCM_FORMAT_S16_BE:
189 return SNDRV_PCM_FORMAT_S16_BE;
190
191 case PCM_FORMAT_S24_LE:
192 return SNDRV_PCM_FORMAT_S24_LE;
193 case PCM_FORMAT_S24_BE:
194 return SNDRV_PCM_FORMAT_S24_BE;
195
196 case PCM_FORMAT_S24_3LE:
197 return SNDRV_PCM_FORMAT_S24_3LE;
198 case PCM_FORMAT_S24_3BE:
199 return SNDRV_PCM_FORMAT_S24_3BE;
200
201 case PCM_FORMAT_S32_LE:
202 return SNDRV_PCM_FORMAT_S32_LE;
203 case PCM_FORMAT_S32_BE:
204 return SNDRV_PCM_FORMAT_S32_BE;
205 };
206}
207
Simon Wilson79d39652011-05-25 13:44:23 -0700208#define PCM_ERROR_MAX 128
209
Taylor Holberton6d58e012016-10-01 18:32:30 -0400210/** A PCM handle.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800211 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400212 */
Simon Wilson79d39652011-05-25 13:44:23 -0700213struct pcm {
Taylor Holberton6d58e012016-10-01 18:32:30 -0400214 /** The PCM's file descriptor */
Simon Wilson79d39652011-05-25 13:44:23 -0700215 int fd;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400216 /** Flags that were passed to @ref pcm_open */
Simon Wilson79d39652011-05-25 13:44:23 -0700217 unsigned int flags;
Ricardo Biehl Pasquali1804d152019-01-04 00:32:40 -0200218 /** The number of (under/over)runs that have occured */
219 int xruns;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400220 /** Size of the buffer */
Simon Wilson79d39652011-05-25 13:44:23 -0700221 unsigned int buffer_size;
Taylor Holberton17a10242016-11-23 13:18:24 -0800222 /** The boundary for ring buffer pointers */
Liam Girdwood6be28f12011-10-13 12:59:51 -0700223 unsigned int boundary;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400224 /** Description of the last error that occured */
Simon Wilson79d39652011-05-25 13:44:23 -0700225 char error[PCM_ERROR_MAX];
Taylor Holberton6d58e012016-10-01 18:32:30 -0400226 /** Configuration that was passed to @ref pcm_open */
Simon Wilson79d39652011-05-25 13:44:23 -0700227 struct pcm_config config;
Eric Laurent40b018e2011-06-18 10:10:23 -0700228 struct snd_pcm_mmap_status *mmap_status;
229 struct snd_pcm_mmap_control *mmap_control;
230 struct snd_pcm_sync_ptr *sync_ptr;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700231 void *mmap_buffer;
232 unsigned int noirq_frames_per_msec;
Taylor Holberton17a10242016-11-23 13:18:24 -0800233 /** The delay of the PCM, in terms of frames */
Hardik T Shah9ecb93f2014-04-10 18:03:52 +0530234 long pcm_delay;
Taylor Holberton17a10242016-11-23 13:18:24 -0800235 /** The subdevice corresponding to the PCM */
David Wagner4cddf192014-04-02 15:12:54 +0200236 unsigned int subdevice;
Simon Wilson79d39652011-05-25 13:44:23 -0700237};
238
Taylor Holberton861da7a2017-04-10 12:05:51 -0700239static int oops(struct pcm *pcm, int e, const char *fmt, ...)
240{
241 va_list ap;
242 int sz;
243
244 va_start(ap, fmt);
245 vsnprintf(pcm->error, PCM_ERROR_MAX, fmt, ap);
246 va_end(ap);
247 sz = strlen(pcm->error);
248
249 if (errno)
250 snprintf(pcm->error + sz, PCM_ERROR_MAX - sz,
251 ": %s", strerror(e));
252 return -1;
253}
254
Taylor Holberton6d58e012016-10-01 18:32:30 -0400255/** Gets the buffer size of the PCM.
256 * @param pcm A PCM handle.
257 * @return The buffer size of the PCM.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800258 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400259 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800260unsigned int pcm_get_buffer_size(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -0700261{
262 return pcm->buffer_size;
263}
264
Taylor Holberton77979a82016-12-01 20:04:04 -0800265/** Gets the channel count of the PCM.
266 * @param pcm A PCM handle.
267 * @return The channel count of the PCM.
268 * @ingroup libtinyalsa-pcm
269 */
270unsigned int pcm_get_channels(const struct pcm *pcm)
271{
Taylor Holberton25976dc2017-04-10 11:46:40 -0700272 return pcm->config.channels;
Taylor Holberton77979a82016-12-01 20:04:04 -0800273}
274
Taylor Holberton08bb5902017-04-10 11:45:44 -0700275/** Gets the PCM configuration.
276 * @param pcm A PCM handle.
277 * @return The PCM configuration.
278 * This function only returns NULL if
279 * @p pcm is NULL.
280 * @ingroup libtinyalsa-pcm
281 * */
282const struct pcm_config * pcm_get_config(const struct pcm *pcm)
283{
284 if (pcm == NULL)
285 return NULL;
286 return &pcm->config;
287}
288
Taylor Holberton77979a82016-12-01 20:04:04 -0800289/** Gets the rate of the PCM.
290 * The rate is given in frames per second.
291 * @param pcm A PCM handle.
292 * @return The rate of the PCM.
293 * @ingroup libtinyalsa-pcm
294 */
295unsigned int pcm_get_rate(const struct pcm *pcm)
296{
Taylor Holberton25976dc2017-04-10 11:46:40 -0700297 return pcm->config.rate;
Taylor Holberton77979a82016-12-01 20:04:04 -0800298}
299
300/** Gets the format of the PCM.
301 * @param pcm A PCM handle.
302 * @return The format of the PCM.
303 * @ingroup libtinyalsa-pcm
304 */
305enum pcm_format pcm_get_format(const struct pcm *pcm)
306{
Taylor Holberton25976dc2017-04-10 11:46:40 -0700307 return pcm->config.format;
Taylor Holberton77979a82016-12-01 20:04:04 -0800308}
309
Taylor Holberton6d58e012016-10-01 18:32:30 -0400310/** Gets the file descriptor of the PCM.
311 * Useful for extending functionality of the PCM when needed.
Taylor Holbertond265c272016-11-23 13:22:56 -0800312 * @param pcm A PCM handle.
Taylor Holberton6d58e012016-10-01 18:32:30 -0400313 * @return The file descriptor of the PCM.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800314 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400315 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800316int pcm_get_file_descriptor(const struct pcm *pcm)
Taylor Holbertonbb402602016-08-03 10:15:46 -0400317{
318 return pcm->fd;
319}
320
Taylor Holberton6d58e012016-10-01 18:32:30 -0400321/** Gets the error message for the last error that occured.
322 * If no error occured and this function is called, the results are undefined.
323 * @param pcm A PCM handle.
324 * @return The error message of the last error that occured.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800325 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400326 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800327const char* pcm_get_error(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -0700328{
329 return pcm->error;
330}
331
Taylor Holberton861da7a2017-04-10 12:05:51 -0700332/** Sets the PCM configuration.
333 * @param pcm A PCM handle.
334 * @param config The configuration to use for the
335 * PCM. This parameter may be NULL, in which case
336 * the default configuration is used.
337 * @returns Zero on success, a negative errno value
338 * on failure.
339 * @ingroup libtinyalsa-pcm
340 * */
341int pcm_set_config(struct pcm *pcm, const struct pcm_config *config)
342{
343 if (pcm == NULL)
344 return -EFAULT;
345 else if (config == NULL) {
346 config = &pcm->config;
347 pcm->config.channels = 2;
348 pcm->config.rate = 48000;
349 pcm->config.period_size = 1024;
350 pcm->config.period_count = 4;
351 pcm->config.format = PCM_FORMAT_S16_LE;
352 pcm->config.start_threshold = config->period_count * config->period_size;
353 pcm->config.stop_threshold = config->period_count * config->period_size;
354 pcm->config.silence_threshold = 0;
355 } else
356 pcm->config = *config;
357
358 struct snd_pcm_hw_params params;
359 param_init(&params);
360 param_set_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT,
361 pcm_format_to_alsa(config->format));
Taylor Holberton861da7a2017-04-10 12:05:51 -0700362 param_set_min(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size);
Taylor Holberton861da7a2017-04-10 12:05:51 -0700363 param_set_int(&params, SNDRV_PCM_HW_PARAM_CHANNELS,
364 config->channels);
365 param_set_int(&params, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count);
366 param_set_int(&params, SNDRV_PCM_HW_PARAM_RATE, config->rate);
367
368 if (pcm->flags & PCM_NOIRQ) {
369
370 if (!(pcm->flags & PCM_MMAP)) {
371 oops(pcm, -EINVAL, "noirq only currently supported with mmap().");
372 return -EINVAL;
373 }
374
375 params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
376 pcm->noirq_frames_per_msec = config->rate / 1000;
377 }
378
379 if (pcm->flags & PCM_MMAP)
380 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
381 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
382 else
383 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
384 SNDRV_PCM_ACCESS_RW_INTERLEAVED);
385
386 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, &params)) {
387 int errno_copy = errno;
388 oops(pcm, -errno, "cannot set hw params");
389 return -errno_copy;
390 }
391
392 /* get our refined hw_params */
393 pcm->config.period_size = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
394 pcm->config.period_count = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIODS);
395 pcm->buffer_size = config->period_count * config->period_size;
396
397 if (pcm->flags & PCM_MMAP) {
398 pcm->mmap_buffer = mmap(NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size),
399 PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, pcm->fd, 0);
400 if (pcm->mmap_buffer == MAP_FAILED) {
401 int errno_copy = errno;
402 oops(pcm, -errno, "failed to mmap buffer %d bytes\n",
403 pcm_frames_to_bytes(pcm, pcm->buffer_size));
404 return -errno_copy;
405 }
406 }
407
408 struct snd_pcm_sw_params sparams;
409 memset(&sparams, 0, sizeof(sparams));
410 sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE;
411 sparams.period_step = 1;
412 sparams.avail_min = 1;
413
414 if (!config->start_threshold) {
415 if (pcm->flags & PCM_IN)
416 pcm->config.start_threshold = sparams.start_threshold = 1;
417 else
418 pcm->config.start_threshold = sparams.start_threshold =
419 config->period_count * config->period_size / 2;
420 } else
421 sparams.start_threshold = config->start_threshold;
422
423 /* pick a high stop threshold - todo: does this need further tuning */
424 if (!config->stop_threshold) {
425 if (pcm->flags & PCM_IN)
426 pcm->config.stop_threshold = sparams.stop_threshold =
427 config->period_count * config->period_size * 10;
428 else
429 pcm->config.stop_threshold = sparams.stop_threshold =
430 config->period_count * config->period_size;
431 }
432 else
433 sparams.stop_threshold = config->stop_threshold;
434
435 sparams.xfer_align = config->period_size / 2; /* needed for old kernels */
436 sparams.silence_size = 0;
437 sparams.silence_threshold = config->silence_threshold;
438 pcm->boundary = sparams.boundary = pcm->buffer_size;
439
440 while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size)
441 pcm->boundary *= 2;
442
443 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
444 int errno_copy = errno;
445 oops(pcm, -errno, "cannot set sw params");
446 return -errno_copy;
447 }
448
449 return 0;
450}
451
Taylor Holberton6d58e012016-10-01 18:32:30 -0400452/** Gets the subdevice on which the pcm has been opened.
453 * @param pcm A PCM handle.
454 * @return The subdevice on which the pcm has been opened */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800455unsigned int pcm_get_subdevice(const struct pcm *pcm)
David Wagner4cddf192014-04-02 15:12:54 +0200456{
457 return pcm->subdevice;
458}
459
Taylor Holberton6d58e012016-10-01 18:32:30 -0400460/** Determines the number of bits occupied by a @ref pcm_format.
461 * @param format A PCM format.
462 * @return The number of bits associated with @p format
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800463 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400464 */
Simon Wilson7136cf72013-07-17 10:30:35 -0700465unsigned int pcm_format_to_bits(enum pcm_format format)
Simon Wilsonbc03b622011-06-15 17:19:01 -0700466{
467 switch (format) {
468 case PCM_FORMAT_S32_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400469 case PCM_FORMAT_S32_BE:
Simon Wilson7136cf72013-07-17 10:30:35 -0700470 case PCM_FORMAT_S24_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400471 case PCM_FORMAT_S24_BE:
Simon Wilsonbc03b622011-06-15 17:19:01 -0700472 return 32;
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400473 case PCM_FORMAT_S24_3LE:
474 case PCM_FORMAT_S24_3BE:
475 return 24;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700476 default:
477 case PCM_FORMAT_S16_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400478 case PCM_FORMAT_S16_BE:
Simon Wilsonbc03b622011-06-15 17:19:01 -0700479 return 16;
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400480 case PCM_FORMAT_S8:
481 return 8;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700482 };
483}
484
Taylor Holberton6d58e012016-10-01 18:32:30 -0400485/** Determines how many frames of a PCM can fit into a number of bytes.
486 * @param pcm A PCM handle.
487 * @param bytes The number of bytes.
488 * @return The number of frames that may fit into @p bytes
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800489 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400490 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800491unsigned int pcm_bytes_to_frames(const struct pcm *pcm, unsigned int bytes)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700492{
493 return bytes / (pcm->config.channels *
494 (pcm_format_to_bits(pcm->config.format) >> 3));
495}
496
Taylor Holberton6d58e012016-10-01 18:32:30 -0400497/** Determines how many bytes are occupied by a number of frames of a PCM.
498 * @param pcm A PCM handle.
499 * @param frames The number of frames of a PCM.
500 * @return The bytes occupied by @p frames.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800501 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400502 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800503unsigned int pcm_frames_to_bytes(const struct pcm *pcm, unsigned int frames)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700504{
505 return frames * pcm->config.channels *
506 (pcm_format_to_bits(pcm->config.format) >> 3);
507}
508
Taylor Holberton4f556062016-09-16 09:54:36 -0400509static int pcm_sync_ptr(struct pcm *pcm, int flags)
510{
Ricardo Biehl Pasqualic1446752018-12-18 23:13:05 -0200511 if (pcm->sync_ptr == NULL) {
512 /* status and control are mmaped */
513
514 if (flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
515 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HWSYNC) == -1) {
516 oops(pcm, errno, "failed to sync hardware pointer");
517 return -1;
518 }
519 }
520 } else {
Eric Laurent40b018e2011-06-18 10:10:23 -0700521 pcm->sync_ptr->flags = flags;
Taylor Holbertone123a652017-01-13 21:39:48 -0800522 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SYNC_PTR, pcm->sync_ptr) < 0) {
523 oops(pcm, errno, "failed to sync mmap ptr");
Eric Laurent40b018e2011-06-18 10:10:23 -0700524 return -1;
Taylor Holbertone123a652017-01-13 21:39:48 -0800525 }
Eric Laurent40b018e2011-06-18 10:10:23 -0700526 }
Ricardo Biehl Pasqualic1446752018-12-18 23:13:05 -0200527
Miguel Gaioc9f97da2018-07-17 10:05:54 +0200528 return 0;
Eric Laurent40b018e2011-06-18 10:10:23 -0700529}
530
Taylor Holberton4f556062016-09-16 09:54:36 -0400531static int pcm_hw_mmap_status(struct pcm *pcm)
532{
Eric Laurent40b018e2011-06-18 10:10:23 -0700533 if (pcm->sync_ptr)
534 return 0;
535
536 int page_size = sysconf(_SC_PAGE_SIZE);
537 pcm->mmap_status = mmap(NULL, page_size, PROT_READ, MAP_FILE | MAP_SHARED,
538 pcm->fd, SNDRV_PCM_MMAP_OFFSET_STATUS);
539 if (pcm->mmap_status == MAP_FAILED)
540 pcm->mmap_status = NULL;
541 if (!pcm->mmap_status)
542 goto mmap_error;
543
544 pcm->mmap_control = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
545 MAP_FILE | MAP_SHARED, pcm->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL);
546 if (pcm->mmap_control == MAP_FAILED)
547 pcm->mmap_control = NULL;
548 if (!pcm->mmap_control) {
549 munmap(pcm->mmap_status, page_size);
550 pcm->mmap_status = NULL;
551 goto mmap_error;
552 }
Eric Laurent40b018e2011-06-18 10:10:23 -0700553
554 return 0;
555
556mmap_error:
557
558 pcm->sync_ptr = calloc(1, sizeof(*pcm->sync_ptr));
559 if (!pcm->sync_ptr)
560 return -ENOMEM;
561 pcm->mmap_status = &pcm->sync_ptr->s.status;
562 pcm->mmap_control = &pcm->sync_ptr->c.control;
Eric Laurent40b018e2011-06-18 10:10:23 -0700563
564 return 0;
565}
566
567static void pcm_hw_munmap_status(struct pcm *pcm) {
568 if (pcm->sync_ptr) {
569 free(pcm->sync_ptr);
570 pcm->sync_ptr = NULL;
571 } else {
572 int page_size = sysconf(_SC_PAGE_SIZE);
573 if (pcm->mmap_status)
574 munmap(pcm->mmap_status, page_size);
575 if (pcm->mmap_control)
576 munmap(pcm->mmap_control, page_size);
577 }
578 pcm->mmap_status = NULL;
579 pcm->mmap_control = NULL;
580}
581
Simon Wilson79d39652011-05-25 13:44:23 -0700582static struct pcm bad_pcm = {
583 .fd = -1,
584};
585
Taylor Holberton6d58e012016-10-01 18:32:30 -0400586/** Gets the hardware parameters of a PCM, without created a PCM handle.
587 * @param card The card of the PCM.
588 * The default card is zero.
589 * @param device The device of the PCM.
590 * The default device is zero.
591 * @param flags Specifies whether the PCM is an input or output.
592 * May be one of the following:
593 * - @ref PCM_IN
594 * - @ref PCM_OUT
595 * @return On success, the hardware parameters of the PCM; on failure, NULL.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800596 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400597 */
Simon Wilson43544882012-10-31 12:52:39 -0700598struct pcm_params *pcm_params_get(unsigned int card, unsigned int device,
599 unsigned int flags)
600{
601 struct snd_pcm_hw_params *params;
602 char fn[256];
603 int fd;
604
605 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
606 flags & PCM_IN ? 'c' : 'p');
607
Taylor Holberton093b8782017-10-12 20:28:30 -0400608 if (flags & PCM_NONBLOCK)
609 fd = open(fn, O_RDWR | O_NONBLOCK);
610 else
611 fd = open(fn, O_RDWR);
612
Simon Wilson43544882012-10-31 12:52:39 -0700613 if (fd < 0) {
Taylor Holberton093b8782017-10-12 20:28:30 -0400614 fprintf(stderr, "cannot open device '%s': %s\n", fn, strerror(errno));
Simon Wilson43544882012-10-31 12:52:39 -0700615 goto err_open;
616 }
617
618 params = calloc(1, sizeof(struct snd_pcm_hw_params));
619 if (!params)
620 goto err_calloc;
621
622 param_init(params);
623 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, params)) {
624 fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno);
625 goto err_hw_refine;
626 }
627
628 close(fd);
629
630 return (struct pcm_params *)params;
631
632err_hw_refine:
633 free(params);
634err_calloc:
635 close(fd);
636err_open:
637 return NULL;
638}
639
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500640/** Frees the hardware parameters returned by @ref pcm_params_get.
Taylor Holberton6d58e012016-10-01 18:32:30 -0400641 * @param pcm_params Hardware parameters of a PCM.
642 * May be NULL.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800643 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400644 */
Simon Wilson43544882012-10-31 12:52:39 -0700645void pcm_params_free(struct pcm_params *pcm_params)
646{
647 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
648
649 if (params)
650 free(params);
651}
652
653static int pcm_param_to_alsa(enum pcm_param param)
654{
655 switch (param) {
Andy Hungad807622014-03-10 18:08:15 -0700656 case PCM_PARAM_ACCESS:
657 return SNDRV_PCM_HW_PARAM_ACCESS;
658 case PCM_PARAM_FORMAT:
659 return SNDRV_PCM_HW_PARAM_FORMAT;
660 case PCM_PARAM_SUBFORMAT:
661 return SNDRV_PCM_HW_PARAM_SUBFORMAT;
Simon Wilson43544882012-10-31 12:52:39 -0700662 case PCM_PARAM_SAMPLE_BITS:
663 return SNDRV_PCM_HW_PARAM_SAMPLE_BITS;
664 break;
665 case PCM_PARAM_FRAME_BITS:
666 return SNDRV_PCM_HW_PARAM_FRAME_BITS;
667 break;
668 case PCM_PARAM_CHANNELS:
669 return SNDRV_PCM_HW_PARAM_CHANNELS;
670 break;
671 case PCM_PARAM_RATE:
672 return SNDRV_PCM_HW_PARAM_RATE;
673 break;
674 case PCM_PARAM_PERIOD_TIME:
675 return SNDRV_PCM_HW_PARAM_PERIOD_TIME;
676 break;
677 case PCM_PARAM_PERIOD_SIZE:
678 return SNDRV_PCM_HW_PARAM_PERIOD_SIZE;
679 break;
680 case PCM_PARAM_PERIOD_BYTES:
681 return SNDRV_PCM_HW_PARAM_PERIOD_BYTES;
682 break;
683 case PCM_PARAM_PERIODS:
684 return SNDRV_PCM_HW_PARAM_PERIODS;
685 break;
686 case PCM_PARAM_BUFFER_TIME:
687 return SNDRV_PCM_HW_PARAM_BUFFER_TIME;
688 break;
689 case PCM_PARAM_BUFFER_SIZE:
690 return SNDRV_PCM_HW_PARAM_BUFFER_SIZE;
691 break;
692 case PCM_PARAM_BUFFER_BYTES:
693 return SNDRV_PCM_HW_PARAM_BUFFER_BYTES;
694 break;
695 case PCM_PARAM_TICK_TIME:
696 return SNDRV_PCM_HW_PARAM_TICK_TIME;
697 break;
698
699 default:
700 return -1;
701 }
702}
703
Taylor Holberton6d58e012016-10-01 18:32:30 -0400704/** Gets a mask from a PCM's hardware parameters.
705 * @param pcm_params A PCM's hardware parameters.
706 * @param param The parameter to get.
707 * @return If @p pcm_params is NULL or @p param is not a mask, NULL is returned.
708 * Otherwise, the mask associated with @p param is returned.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800709 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400710 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800711const struct pcm_mask *pcm_params_get_mask(const struct pcm_params *pcm_params,
Andy Hungad807622014-03-10 18:08:15 -0700712 enum pcm_param param)
713{
714 int p;
715 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
716 if (params == NULL) {
717 return NULL;
718 }
719
720 p = pcm_param_to_alsa(param);
721 if (p < 0 || !param_is_mask(p)) {
722 return NULL;
723 }
724
Taylor Holberton2f387d22016-12-01 15:58:16 -0800725 return (const struct pcm_mask *)param_to_mask(params, p);
Andy Hungad807622014-03-10 18:08:15 -0700726}
727
Taylor Holberton17a10242016-11-23 13:18:24 -0800728/** Get the minimum of a specified PCM parameter.
729 * @param pcm_params A PCM parameters structure.
730 * @param param The specified parameter to get the minimum of.
731 * @returns On success, the parameter minimum.
732 * On failure, zero.
733 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800734unsigned int pcm_params_get_min(const struct pcm_params *pcm_params,
Simon Wilson43544882012-10-31 12:52:39 -0700735 enum pcm_param param)
736{
737 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
738 int p;
739
740 if (!params)
741 return 0;
742
743 p = pcm_param_to_alsa(param);
744 if (p < 0)
745 return 0;
746
747 return param_get_min(params, p);
748}
749
Taylor Holberton17a10242016-11-23 13:18:24 -0800750/** Get the maximum of a specified PCM parameter.
751 * @param pcm_params A PCM parameters structure.
752 * @param param The specified parameter to get the maximum of.
753 * @returns On success, the parameter maximum.
754 * On failure, zero.
755 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800756unsigned int pcm_params_get_max(const struct pcm_params *pcm_params,
Simon Wilson43544882012-10-31 12:52:39 -0700757 enum pcm_param param)
758{
Taylor Holberton2f387d22016-12-01 15:58:16 -0800759 const struct snd_pcm_hw_params *params = (const struct snd_pcm_hw_params *)pcm_params;
Simon Wilson43544882012-10-31 12:52:39 -0700760 int p;
761
762 if (!params)
763 return 0;
764
765 p = pcm_param_to_alsa(param);
766 if (p < 0)
767 return 0;
768
769 return param_get_max(params, p);
770}
771
Taylor Holberton6d58e012016-10-01 18:32:30 -0400772/** Closes a PCM returned by @ref pcm_open.
773 * @param pcm A PCM returned by @ref pcm_open.
774 * May not be NULL.
775 * @return Always returns zero.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800776 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400777 */
Simon Wilson79d39652011-05-25 13:44:23 -0700778int pcm_close(struct pcm *pcm)
779{
780 if (pcm == &bad_pcm)
781 return 0;
782
Eric Laurent40b018e2011-06-18 10:10:23 -0700783 pcm_hw_munmap_status(pcm);
784
Liam Girdwood6be28f12011-10-13 12:59:51 -0700785 if (pcm->flags & PCM_MMAP) {
786 pcm_stop(pcm);
787 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
788 }
789
Simon Wilson79d39652011-05-25 13:44:23 -0700790 if (pcm->fd >= 0)
791 close(pcm->fd);
Simon Wilson79d39652011-05-25 13:44:23 -0700792 pcm->buffer_size = 0;
793 pcm->fd = -1;
Eric Laurent40b018e2011-06-18 10:10:23 -0700794 free(pcm);
Simon Wilson79d39652011-05-25 13:44:23 -0700795 return 0;
796}
797
Taylor Holbertonc6f908e2016-12-24 20:33:33 -0800798/** Opens a PCM by it's name.
799 * @param name The name of the PCM.
800 * The name is given in the format: <i>hw</i>:<b>card</b>,<b>device</b>
801 * @param flags Specify characteristics and functionality about the pcm.
802 * May be a bitwise AND of the following:
803 * - @ref PCM_IN
804 * - @ref PCM_OUT
805 * - @ref PCM_MMAP
806 * - @ref PCM_NOIRQ
807 * - @ref PCM_MONOTONIC
808 * @param config The hardware and software parameters to open the PCM with.
Taylor Holbertone123a652017-01-13 21:39:48 -0800809 * @returns A PCM structure.
810 * If an error occurs allocating memory for the PCM, NULL is returned.
811 * Otherwise, client code should check that the PCM opened properly by calling @ref pcm_is_ready.
812 * If @ref pcm_is_ready, check @ref pcm_get_error for more information.
Taylor Holbertonc6f908e2016-12-24 20:33:33 -0800813 * @ingroup libtinyalsa-pcm
814 */
815struct pcm *pcm_open_by_name(const char *name,
816 unsigned int flags,
817 const struct pcm_config *config)
818{
819 unsigned int card, device;
820 if ((name[0] != 'h')
821 || (name[1] != 'w')
822 || (name[2] != ':')) {
823 return NULL;
824 } else if (sscanf(&name[3], "%u,%u", &card, &device) != 2) {
825 return NULL;
826 }
827 return pcm_open(card, device, flags, config);
828}
829
Taylor Holberton6d58e012016-10-01 18:32:30 -0400830/** Opens a PCM.
831 * @param card The card that the pcm belongs to.
832 * The default card is zero.
833 * @param device The device that the pcm belongs to.
834 * The default device is zero.
835 * @param flags Specify characteristics and functionality about the pcm.
836 * May be a bitwise AND of the following:
837 * - @ref PCM_IN
838 * - @ref PCM_OUT
839 * - @ref PCM_MMAP
840 * - @ref PCM_NOIRQ
841 * - @ref PCM_MONOTONIC
842 * @param config The hardware and software parameters to open the PCM with.
Taylor Holbertone123a652017-01-13 21:39:48 -0800843 * @returns A PCM structure.
844 * If an error occurs allocating memory for the PCM, NULL is returned.
845 * Otherwise, client code should check that the PCM opened properly by calling @ref pcm_is_ready.
846 * If @ref pcm_is_ready, check @ref pcm_get_error for more information.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800847 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400848 */
Simon Wilson1bd580f2011-06-02 15:58:41 -0700849struct pcm *pcm_open(unsigned int card, unsigned int device,
Taylor Holberton94803b02016-12-01 16:07:14 -0800850 unsigned int flags, const struct pcm_config *config)
Simon Wilson79d39652011-05-25 13:44:23 -0700851{
Simon Wilson79d39652011-05-25 13:44:23 -0700852 struct pcm *pcm;
853 struct snd_pcm_info info;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700854 char fn[256];
Eric Laurent40b018e2011-06-18 10:10:23 -0700855 int rc;
Simon Wilson79d39652011-05-25 13:44:23 -0700856
857 pcm = calloc(1, sizeof(struct pcm));
Taylor Holbertonf319eb02016-10-14 20:05:30 -0400858 if (!pcm)
859 return &bad_pcm;
Simon Wilson79d39652011-05-25 13:44:23 -0700860
Simon Wilson1bd580f2011-06-02 15:58:41 -0700861 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
862 flags & PCM_IN ? 'c' : 'p');
Simon Wilson79d39652011-05-25 13:44:23 -0700863
864 pcm->flags = flags;
Taylor Holberton093b8782017-10-12 20:28:30 -0400865
866 if (flags & PCM_NONBLOCK)
867 pcm->fd = open(fn, O_RDWR | O_NONBLOCK);
868 else
869 pcm->fd = open(fn, O_RDWR);
870
Simon Wilson79d39652011-05-25 13:44:23 -0700871 if (pcm->fd < 0) {
Simon Wilson1bd580f2011-06-02 15:58:41 -0700872 oops(pcm, errno, "cannot open device '%s'", fn);
Simon Wilson79d39652011-05-25 13:44:23 -0700873 return pcm;
874 }
875
876 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) {
Simon Wilson851aa5c2011-05-30 21:18:26 -0700877 oops(pcm, errno, "cannot get info");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700878 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700879 }
David Wagner4cddf192014-04-02 15:12:54 +0200880 pcm->subdevice = info.subdevice;
Simon Wilson79d39652011-05-25 13:44:23 -0700881
Taylor Holberton861da7a2017-04-10 12:05:51 -0700882 if (pcm_set_config(pcm, config) != 0)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700883 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700884
Eric Laurent40b018e2011-06-18 10:10:23 -0700885 rc = pcm_hw_mmap_status(pcm);
886 if (rc < 0) {
887 oops(pcm, rc, "mmap status failed");
888 goto fail;
889 }
890
Glenn Kasten81012402013-08-22 15:11:48 -0700891#ifdef SNDRV_PCM_IOCTL_TTSTAMP
892 if (pcm->flags & PCM_MONOTONIC) {
893 int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
894 rc = ioctl(pcm->fd, SNDRV_PCM_IOCTL_TTSTAMP, &arg);
895 if (rc < 0) {
896 oops(pcm, rc, "cannot set timestamp type");
897 goto fail;
898 }
899 }
900#endif
901
Ricardo Biehl Pasquali13bf6292018-08-22 16:10:45 -0300902 /* prepare here so the user does not need to do this later */
903 if (pcm_prepare(pcm))
904 goto fail;
905
Ricardo Biehl Pasquali1804d152019-01-04 00:32:40 -0200906 pcm->xruns = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700907 return pcm;
908
909fail:
Ricardo Biehl Pasquali0bfac892019-01-04 14:45:35 -0200910 pcm_hw_munmap_status(pcm);
Liam Girdwood6be28f12011-10-13 12:59:51 -0700911 if (flags & PCM_MMAP)
912 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
913fail_close:
Simon Wilson79d39652011-05-25 13:44:23 -0700914 close(pcm->fd);
915 pcm->fd = -1;
916 return pcm;
917}
918
Taylor Holberton6d58e012016-10-01 18:32:30 -0400919/** Checks if a PCM file has been opened without error.
920 * @param pcm A PCM handle.
Taylor Holbertone123a652017-01-13 21:39:48 -0800921 * May be NULL.
922 * @return If a PCM's file descriptor is not valid or the pointer is NULL, it returns zero.
Taylor Holberton6d58e012016-10-01 18:32:30 -0400923 * Otherwise, the function returns one.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800924 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400925 */
Taylor Holberton15d58482016-12-01 17:46:29 -0800926int pcm_is_ready(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -0700927{
Taylor Holbertone123a652017-01-13 21:39:48 -0800928 if (pcm != NULL) {
929 return pcm->fd >= 0;
930 }
931 return 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700932}
Simon Wilsond6458e62011-06-21 14:58:11 -0700933
Taylor Holberton558e5942016-12-04 13:42:28 -0800934/** Links two PCMs.
935 * After this function is called, the two PCMs will prepare, start and stop in sync (at the same time).
936 * If an error occurs, the error message will be written to @p pcm1.
937 * @param pcm1 A PCM handle.
938 * @param pcm2 Another PCM handle.
939 * @return On success, zero; on failure, a negative number.
940 * @ingroup libtinyalsa-pcm
941 */
942int pcm_link(struct pcm *pcm1, struct pcm *pcm2)
943{
944 int err = ioctl(pcm1->fd, SNDRV_PCM_IOCTL_LINK, pcm2->fd);
945 if (err == -1) {
946 return oops(pcm1, errno, "cannot link PCM");
947 }
948 return 0;
949}
950
951/** Unlinks a PCM.
952 * @see @ref pcm_link
953 * @param pcm A PCM handle.
954 * @return On success, zero; on failure, a negative number.
955 * @ingroup libtinyalsa-pcm
956 */
957int pcm_unlink(struct pcm *pcm)
958{
959 int err = ioctl(pcm->fd, SNDRV_PCM_IOCTL_UNLINK);
960 if (err == -1) {
961 return oops(pcm, errno, "cannot unlink PCM");
962 }
963 return 0;
964}
965
Taylor Holberton6d58e012016-10-01 18:32:30 -0400966/** Prepares a PCM, if it has not been prepared already.
967 * @param pcm A PCM handle.
968 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800969 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400970 */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530971int pcm_prepare(struct pcm *pcm)
Simon Wilsond6458e62011-06-21 14:58:11 -0700972{
973 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0)
974 return oops(pcm, errno, "cannot prepare channel");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700975
Ricardo Biehl Pasquali535a6ba2018-12-30 11:53:54 -0200976 /* get appl_ptr and avail_min from kernel */
977 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_AVAIL_MIN);
978
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530979 return 0;
980}
981
Taylor Holberton6d58e012016-10-01 18:32:30 -0400982/** Starts a PCM.
Taylor Holberton6d58e012016-10-01 18:32:30 -0400983 * @param pcm A PCM handle.
984 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800985 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400986 */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530987int pcm_start(struct pcm *pcm)
988{
Ricardo Biehl Pasquali7ff9cde2018-12-31 17:23:39 -0200989 /* set appl_ptr and avail_min in kernel */
Ricardo Biehl Pasqualib38b6a62019-01-07 11:54:47 -0200990 if (pcm_sync_ptr(pcm, 0) < 0)
991 return -1;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700992
Miguel Gaiocf5f0632018-07-17 13:30:24 +0200993 if (pcm->mmap_status->state != PCM_STATE_RUNNING) {
994 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0)
995 return oops(pcm, errno, "cannot start channel");
996 }
Simon Wilsond6458e62011-06-21 14:58:11 -0700997
998 return 0;
999}
1000
Taylor Holberton6d58e012016-10-01 18:32:30 -04001001/** Stops a PCM.
1002 * @param pcm A PCM handle.
1003 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001004 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001005 */
Simon Wilsond6458e62011-06-21 14:58:11 -07001006int pcm_stop(struct pcm *pcm)
1007{
1008 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0)
1009 return oops(pcm, errno, "cannot stop channel");
1010
1011 return 0;
1012}
1013
Liam Girdwood6be28f12011-10-13 12:59:51 -07001014static inline int pcm_mmap_playback_avail(struct pcm *pcm)
1015{
1016 int avail;
1017
1018 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
1019
1020 if (avail < 0)
1021 avail += pcm->boundary;
StevenNANb0fc3e92014-03-17 11:14:49 +08001022 else if (avail >= (int)pcm->boundary)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001023 avail -= pcm->boundary;
1024
1025 return avail;
1026}
1027
1028static inline int pcm_mmap_capture_avail(struct pcm *pcm)
1029{
1030 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
1031 if (avail < 0)
1032 avail += pcm->boundary;
1033 return avail;
1034}
1035
1036static inline int pcm_mmap_avail(struct pcm *pcm)
1037{
Liam Girdwood6be28f12011-10-13 12:59:51 -07001038 if (pcm->flags & PCM_IN)
1039 return pcm_mmap_capture_avail(pcm);
1040 else
1041 return pcm_mmap_playback_avail(pcm);
1042}
1043
1044static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
1045{
1046 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
1047 appl_ptr += frames;
1048
1049 /* check for boundary wrap */
1050 if (appl_ptr > pcm->boundary)
1051 appl_ptr -= pcm->boundary;
1052 pcm->mmap_control->appl_ptr = appl_ptr;
1053}
1054
1055int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
1056 unsigned int *frames)
1057{
1058 unsigned int continuous, copy_frames, avail;
1059
1060 /* return the mmap buffer */
1061 *areas = pcm->mmap_buffer;
1062
1063 /* and the application offset in frames */
1064 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
1065
1066 avail = pcm_mmap_avail(pcm);
1067 if (avail > pcm->buffer_size)
1068 avail = pcm->buffer_size;
1069 continuous = pcm->buffer_size - *offset;
1070
1071 /* we can only copy frames if the are availabale and continuos */
1072 copy_frames = *frames;
1073 if (copy_frames > avail)
1074 copy_frames = avail;
1075 if (copy_frames > continuous)
1076 copy_frames = continuous;
1077 *frames = copy_frames;
1078
1079 return 0;
1080}
1081
Ricardo Biehl Pasqualid759f482018-12-22 11:17:32 -02001082static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset,
1083 char *buf, unsigned int src_offset,
1084 unsigned int frames)
1085{
1086 int size_bytes = pcm_frames_to_bytes(pcm, frames);
1087 int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset);
1088 int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset);
1089
1090 /* interleaved only atm */
1091 if (pcm->flags & PCM_IN)
1092 memcpy(buf + src_offset_bytes,
1093 (char*)pcm->mmap_buffer + pcm_offset_bytes,
1094 size_bytes);
1095 else
1096 memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes,
1097 buf + src_offset_bytes,
1098 size_bytes);
1099 return 0;
1100}
1101
Liam Girdwood6be28f12011-10-13 12:59:51 -07001102int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames)
1103{
Taylor Holberton72e44222016-11-22 09:54:47 -08001104 int ret;
1105
Taylor Holberton73466c02016-10-01 12:51:59 -04001106 /* not used */
1107 (void) offset;
1108
Liam Girdwood6be28f12011-10-13 12:59:51 -07001109 /* update the application pointer in userspace and kernel */
1110 pcm_mmap_appl_forward(pcm, frames);
Taylor Holberton72e44222016-11-22 09:54:47 -08001111 ret = pcm_sync_ptr(pcm, 0);
Taylor Holbertone123a652017-01-13 21:39:48 -08001112 if (ret != 0){
1113 printf("%d\n", ret);
Taylor Holberton72e44222016-11-22 09:54:47 -08001114 return ret;
Taylor Holbertone123a652017-01-13 21:39:48 -08001115 }
Liam Girdwood6be28f12011-10-13 12:59:51 -07001116
1117 return frames;
1118}
1119
Ricardo Biehl Pasqualid759f482018-12-22 11:17:32 -02001120static int pcm_mmap_transfer_areas(struct pcm *pcm, char *buf,
1121 unsigned int offset, unsigned int size)
1122{
1123 void *pcm_areas;
1124 int commit;
1125 unsigned int pcm_offset, frames, count = 0;
1126
Ricardo Biehl Pasquali7aacaed2018-12-18 23:24:19 -02001127 while (pcm_mmap_avail(pcm) && size) {
Ricardo Biehl Pasqualid759f482018-12-22 11:17:32 -02001128 frames = size;
1129 pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
1130 pcm_areas_copy(pcm, pcm_offset, buf, offset, frames);
1131 commit = pcm_mmap_commit(pcm, pcm_offset, frames);
1132 if (commit < 0) {
1133 oops(pcm, commit, "failed to commit %d frames\n", frames);
1134 return commit;
1135 }
1136
1137 offset += commit;
1138 count += commit;
1139 size -= commit;
1140 }
1141 return count;
1142}
1143
Liam Girdwood6be28f12011-10-13 12:59:51 -07001144int pcm_avail_update(struct pcm *pcm)
1145{
Ricardo Biehl Pasqualib00f90a2018-12-22 12:51:21 -02001146 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_AVAIL_MIN);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001147 return pcm_mmap_avail(pcm);
1148}
1149
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001150/** Returns available frames in pcm buffer and corresponding time stamp.
1151 * The clock is CLOCK_MONOTONIC if flag @ref PCM_MONOTONIC was specified in @ref pcm_open,
1152 * otherwise the clock is CLOCK_REALTIME.
1153 * For an input stream, frames available are frames ready for the application to read.
1154 * For an output stream, frames available are the number of empty frames available for the application to write.
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001155 * @param pcm A PCM handle.
1156 * @param avail The number of available frames
1157 * @param tstamp The timestamp
1158 * @return On success, zero is returned; on failure, negative one.
1159 */
1160int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
1161 struct timespec *tstamp)
1162{
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001163 int checking;
1164 int tmp;
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001165
1166 if (!pcm_is_ready(pcm))
1167 return -1;
1168
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001169 checking = 0;
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001170
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001171again:
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001172
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001173 tmp = pcm_avail_update(pcm);
1174 if (tmp < 0)
1175 return tmp; /* error */
1176
1177 if (checking && (unsigned int) tmp == *avail)
1178 return 0;
1179
1180 *avail = (unsigned int) tmp;
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001181 *tstamp = pcm->mmap_status->tstamp;
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001182
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001183 /*
1184 * When status is mmaped, get avail again to ensure
1185 * valid timestamp.
1186 */
1187 if (!pcm->sync_ptr) {
1188 checking = 1;
1189 goto again;
1190 }
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001191
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001192 /* SYNC_PTR ioctl was used, no need to check avail */
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001193 return 0;
1194}
1195
Liam Girdwood6be28f12011-10-13 12:59:51 -07001196int pcm_state(struct pcm *pcm)
1197{
1198 int err = pcm_sync_ptr(pcm, 0);
1199 if (err < 0)
1200 return err;
1201
1202 return pcm->mmap_status->state;
1203}
1204
Taylor Holberton17a10242016-11-23 13:18:24 -08001205/** Waits for frames to be available for read or write operations.
1206 * @param pcm A PCM handle.
1207 * @param timeout The maximum amount of time to wait for, in terms of milliseconds.
1208 * @returns If frames became available, one is returned.
1209 * If a timeout occured, zero is returned.
1210 * If an error occured, a negative number is returned.
1211 * @ingroup libtinyalsa-pcm
1212 */
Liam Girdwood6be28f12011-10-13 12:59:51 -07001213int pcm_wait(struct pcm *pcm, int timeout)
1214{
1215 struct pollfd pfd;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001216 int err;
1217
1218 pfd.fd = pcm->fd;
Apelete Seketeli84889d02014-02-14 14:34:32 +01001219 pfd.events = POLLIN | POLLOUT | POLLERR | POLLNVAL;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001220
1221 do {
1222 /* let's wait for avail or timeout */
1223 err = poll(&pfd, 1, timeout);
1224 if (err < 0)
1225 return -errno;
1226
1227 /* timeout ? */
1228 if (err == 0)
1229 return 0;
1230
1231 /* have we been interrupted ? */
1232 if (errno == -EINTR)
1233 continue;
1234
1235 /* check for any errors */
1236 if (pfd.revents & (POLLERR | POLLNVAL)) {
1237 switch (pcm_state(pcm)) {
1238 case PCM_STATE_XRUN:
1239 return -EPIPE;
1240 case PCM_STATE_SUSPENDED:
1241 return -ESTRPIPE;
1242 case PCM_STATE_DISCONNECTED:
1243 return -ENODEV;
1244 default:
1245 return -EIO;
1246 }
1247 }
1248 /* poll again if fd not ready for IO */
1249 } while (!(pfd.revents & (POLLIN | POLLOUT)));
1250
1251 return 1;
1252}
1253
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001254/*
1255 * Transfer data to/from mmaped buffer. This imitates the
1256 * behavior of read/write system calls.
1257 *
1258 * However, this doesn't seems to offer any advantage over
1259 * the read/write syscalls. Should it be removed?
1260 */
1261int pcm_mmap_transfer(struct pcm *pcm, void *buffer, unsigned int frames)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001262{
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001263 int is_playback;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001264
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001265 int state;
1266 unsigned int avail;
1267 unsigned int user_offset;
1268
1269 int err;
1270 int tmp;
1271
1272 is_playback = !(pcm->flags & PCM_IN);
1273
1274 if (frames == 0)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001275 return 0;
1276
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001277 /* update hardware pointer and get state */
1278 err = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC |
1279 SNDRV_PCM_SYNC_PTR_APPL |
1280 SNDRV_PCM_SYNC_PTR_AVAIL_MIN);
1281 if (err == -1)
1282 return -1;
1283 state = pcm->mmap_status->state;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001284
Ricardo Biehl Pasqualic6c0d782019-03-12 16:31:04 -03001285 /*
1286 * If frames < start_threshold, wait indefinitely.
1287 * Another thread may start capture
1288 */
1289 if (!is_playback && state == PCM_STATE_PREPARED &&
1290 frames >= pcm->config.start_threshold) {
1291 err = ioctl(pcm->fd, SNDRV_PCM_IOCTL_START);
1292 if (err == -1)
1293 return -1;
1294 /* state = PCM_STATE_RUNNING */
Liam Girdwood6be28f12011-10-13 12:59:51 -07001295 }
1296
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001297 avail = pcm_mmap_avail(pcm);
1298 user_offset = 0;
1299
1300 while (frames) {
1301 if (!avail) {
1302 if (pcm->flags & PCM_NONBLOCK) {
1303 errno = EAGAIN;
1304 break;
1305 }
1306
1307 /* wait for interrupt */
1308 err = pcm_wait(pcm, -1);
1309 if (err < 0) {
1310 errno = -err;
1311 break;
1312 }
1313
1314 /* get hardware pointer */
1315 avail = pcm_avail_update(pcm);
1316 }
1317
1318 tmp = pcm_mmap_transfer_areas(pcm, buffer, user_offset, frames);
1319 if (tmp < 0)
1320 break;
1321
1322 user_offset += tmp;
1323 frames -= tmp;
1324 avail -= tmp;
1325
1326 /* start playback if written >= start_threshold */
1327 if (is_playback && state == PCM_STATE_PREPARED &&
1328 pcm->buffer_size - avail >= pcm->config.start_threshold) {
1329 err = ioctl(pcm->fd, SNDRV_PCM_IOCTL_START);
1330 if (err == -1)
1331 break;
1332 }
1333 }
1334
1335 return user_offset ? (int) user_offset : -1;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001336}
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001337
1338int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count)
1339{
1340 if ((~pcm->flags) & (PCM_OUT | PCM_MMAP))
1341 return -ENOSYS;
1342
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001343 return pcm_mmap_transfer(pcm, (void *)data,
1344 pcm_bytes_to_frames(pcm, count));
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001345}
1346
1347int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count)
1348{
1349 if ((~pcm->flags) & (PCM_IN | PCM_MMAP))
1350 return -ENOSYS;
1351
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001352 return pcm_mmap_transfer(pcm, data, pcm_bytes_to_frames(pcm, count));
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001353}
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301354
Ricardo Biehl Pasquali03cc7c52019-01-04 00:20:14 -02001355static int pcm_rw_transfer(struct pcm *pcm, void *data, unsigned int frames)
1356{
1357 int is_playback;
1358
1359 struct snd_xferi transfer;
1360 int res;
1361
1362 is_playback = !(pcm->flags & PCM_IN);
1363
1364 transfer.buf = data;
1365 transfer.frames = frames;
1366 transfer.result = 0;
1367
1368 res = ioctl(pcm->fd, is_playback
1369 ? SNDRV_PCM_IOCTL_WRITEI_FRAMES
1370 : SNDRV_PCM_IOCTL_READI_FRAMES, &transfer);
1371
1372 return res == 0 ? (int) transfer.result : -1;
1373}
1374
1375static int pcm_generic_transfer(struct pcm *pcm, void *data,
1376 unsigned int frames)
1377{
1378 int res;
1379
1380#if UINT_MAX > TINYALSA_FRAMES_MAX
1381 if (frames > TINYALSA_FRAMES_MAX)
1382 return -EINVAL;
1383#endif
1384 if (frames > INT_MAX)
1385 return -EINVAL;
1386
1387again:
1388
1389 if (pcm->flags & PCM_MMAP)
1390 res = pcm_mmap_transfer(pcm, data, frames);
1391 else
1392 res = pcm_rw_transfer(pcm, data, frames);
1393
1394 if (res < 0) {
1395 switch (errno) {
1396 case EPIPE:
Ricardo Biehl Pasquali1804d152019-01-04 00:32:40 -02001397 pcm->xruns++;
Ricardo Biehl Pasquali03cc7c52019-01-04 00:20:14 -02001398 /* fallthrough */
1399 case ESTRPIPE:
1400 /*
1401 * Try to restart if we are allowed to do so.
1402 * Otherwise, return error.
1403 */
1404 if (pcm->flags & PCM_NORESTART || pcm_prepare(pcm))
1405 return -1;
1406 goto again;
1407 case EAGAIN:
1408 if (pcm->flags & PCM_NONBLOCK)
1409 return -1;
1410 /* fallthrough */
1411 default:
1412 return oops(pcm, errno, "cannot read/write stream data");
1413 }
1414 }
1415
1416 return res;
1417}
1418
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001419/** Writes audio samples to PCM.
1420 * If the PCM has not been started, it is started in this function.
1421 * This function is only valid for PCMs opened with the @ref PCM_OUT flag.
1422 * @param pcm A PCM handle.
1423 * @param data The audio sample array
1424 * @param frame_count The number of frames occupied by the sample array.
1425 * This value should not be greater than @ref TINYALSA_FRAMES_MAX
1426 * or INT_MAX.
1427 * @return On success, this function returns the number of frames written; otherwise, a negative number.
1428 * @ingroup libtinyalsa-pcm
1429 */
1430int pcm_writei(struct pcm *pcm, const void *data, unsigned int frame_count)
1431{
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001432 if (pcm->flags & PCM_IN)
1433 return -EINVAL;
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001434
Ricardo Biehl Pasquali03cc7c52019-01-04 00:20:14 -02001435 return pcm_generic_transfer(pcm, (void*) data, frame_count);
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001436}
1437
1438/** Reads audio samples from PCM.
1439 * If the PCM has not been started, it is started in this function.
1440 * This function is only valid for PCMs opened with the @ref PCM_IN flag.
1441 * @param pcm A PCM handle.
1442 * @param data The audio sample array
1443 * @param frame_count The number of frames occupied by the sample array.
1444 * This value should not be greater than @ref TINYALSA_FRAMES_MAX
1445 * or INT_MAX.
1446 * @return On success, this function returns the number of frames written; otherwise, a negative number.
1447 * @ingroup libtinyalsa-pcm
1448 */
1449int pcm_readi(struct pcm *pcm, void *data, unsigned int frame_count)
1450{
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001451 if (!(pcm->flags & PCM_IN))
1452 return -EINVAL;
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001453
Ricardo Biehl Pasquali03cc7c52019-01-04 00:20:14 -02001454 return pcm_generic_transfer(pcm, data, frame_count);
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001455}
1456
1457/** Writes audio samples to PCM.
1458 * If the PCM has not been started, it is started in this function.
1459 * This function is only valid for PCMs opened with the @ref PCM_OUT flag.
1460 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
1461 * @param pcm A PCM handle.
1462 * @param data The audio sample array
1463 * @param count The number of bytes occupied by the sample array.
1464 * @return On success, this function returns zero; otherwise, a negative number.
1465 * @deprecated
1466 * @ingroup libtinyalsa-pcm
1467 */
1468int pcm_write(struct pcm *pcm, const void *data, unsigned int count)
1469{
1470 return pcm_writei(pcm, data, pcm_bytes_to_frames(pcm, count));
1471}
1472
1473/** Reads audio samples from PCM.
1474 * If the PCM has not been started, it is started in this function.
1475 * This function is only valid for PCMs opened with the @ref PCM_IN flag.
1476 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
1477 * @param pcm A PCM handle.
1478 * @param data The audio sample array
1479 * @param count The number of bytes occupied by the sample array.
1480 * @return On success, this function returns zero; otherwise, a negative number.
1481 * @deprecated
1482 * @ingroup libtinyalsa-pcm
1483 */
1484int pcm_read(struct pcm *pcm, void *data, unsigned int count)
1485{
1486 return pcm_readi(pcm, data, pcm_bytes_to_frames(pcm, count));
1487}
1488
Taylor Holberton17a10242016-11-23 13:18:24 -08001489/** Gets the delay of the PCM, in terms of frames.
1490 * @param pcm A PCM handle.
1491 * @returns On success, the delay of the PCM.
1492 * On failure, a negative number.
1493 * @ingroup libtinyalsa-pcm
1494 */
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301495long pcm_get_delay(struct pcm *pcm)
1496{
1497 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DELAY, &pcm->pcm_delay) < 0)
1498 return -1;
1499
1500 return pcm->pcm_delay;
1501}
Taylor Holberton6d58e012016-10-01 18:32:30 -04001502