Debian Codegraph @

utils/aplay/alsa-pcm.c

raw
1 /*
2 * BlueALSA - alsa-pcm.c
3 * Copyright (c) 2016-2023 Arkadiusz Bokowy
4 *
5 * This file is a part of bluez-alsa.
6 *
7 * This project is licensed under the terms of the MIT license.
8 *
9 */
10
11 #include "alsa-pcm.h"
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16
17 static int alsa_pcm_set_hw_params(snd_pcm_t *pcm, snd_pcm_format_t format, int channels,
18 int rate, unsigned int *buffer_time, unsigned int *period_time, char **msg) {
19
20 const snd_pcm_access_t access = SND_PCM_ACCESS_RW_INTERLEAVED;
21 snd_pcm_hw_params_t *params;
22 char buf[256];
23 int dir;
24 int err;
25
26 snd_pcm_hw_params_alloca(&params);
27
28 if ((err = snd_pcm_hw_params_any(pcm, params)) < 0) {
29 snprintf(buf, sizeof(buf), "Set all possible ranges: %s", snd_strerror(err));
30 goto fail;
31 }
32
33 if ((err = snd_pcm_hw_params_set_access(pcm, params, access)) != 0) {
34 snprintf(buf, sizeof(buf), "Set assess type: %s: %s", snd_strerror(err), snd_pcm_access_name(access));
35 goto fail;
36 }
37
38 if ((err = snd_pcm_hw_params_set_format(pcm, params, format)) != 0) {
39 snprintf(buf, sizeof(buf), "Set format: %s: %s", snd_strerror(err), snd_pcm_format_name(format));
40 goto fail;
41 }
42
43 if ((err = snd_pcm_hw_params_set_channels(pcm, params, channels)) != 0) {
44 snprintf(buf, sizeof(buf), "Set channels: %s: %d", snd_strerror(err), channels);
45 goto fail;
46 }
47
48 if ((err = snd_pcm_hw_params_set_rate(pcm, params, rate, 0)) != 0) {
49 snprintf(buf, sizeof(buf), "Set sampling rate: %s: %d", snd_strerror(err), rate);
50 goto fail;
51 }
52
53 dir = 0;
54 if ((err = snd_pcm_hw_params_set_period_time_near(pcm, params, period_time, &dir)) != 0) {
55 snprintf(buf, sizeof(buf), "Set period time: %s: %u", snd_strerror(err), *period_time);
56 goto fail;
57 }
58
59 dir = 0;
60 if ((err = snd_pcm_hw_params_set_buffer_time_near(pcm, params, buffer_time, &dir)) != 0) {
61 snprintf(buf, sizeof(buf), "Set buffer time: %s: %u", snd_strerror(err), *buffer_time);
62 goto fail;
63 }
64
65 if ((err = snd_pcm_hw_params(pcm, params)) != 0) {
66 snprintf(buf, sizeof(buf), "%s", snd_strerror(err));
67 goto fail;
68 }
69
70 return 0;
71
72 fail:
73 if (msg != NULL)
74 *msg = strdup(buf);
75 return err;
76 }
77
78 static int alsa_pcm_set_sw_params(snd_pcm_t *pcm, snd_pcm_uframes_t buffer_size,
79 snd_pcm_uframes_t period_size, char **msg) {
80
81 snd_pcm_sw_params_t *params;
82 char buf[256];
83 int err;
84
85 snd_pcm_sw_params_alloca(&params);
86
87 if ((err = snd_pcm_sw_params_current(pcm, params)) != 0) {
88 snprintf(buf, sizeof(buf), "Get current params: %s", snd_strerror(err));
89 goto fail;
90 }
91
92 /* start the transfer when the buffer is full (or almost full) */
93 snd_pcm_uframes_t threshold = (buffer_size / period_size) * period_size;
94 if ((err = snd_pcm_sw_params_set_start_threshold(pcm, params, threshold)) != 0) {
95 snprintf(buf, sizeof(buf), "Set start threshold: %s: %lu", snd_strerror(err), threshold);
96 goto fail;
97 }
98
99 /* allow the transfer when at least period_size samples can be processed */
100 if ((err = snd_pcm_sw_params_set_avail_min(pcm, params, period_size)) != 0) {
101 snprintf(buf, sizeof(buf), "Set avail min: %s: %lu", snd_strerror(err), period_size);
102 goto fail;
103 }
104
105 if ((err = snd_pcm_sw_params(pcm, params)) != 0) {
106 snprintf(buf, sizeof(buf), "%s", snd_strerror(err));
107 goto fail;
108 }
109
110 return 0;
111
112 fail:
113 if (msg != NULL)
114 *msg = strdup(buf);
115 return err;
116 }
117
118 int alsa_pcm_open(snd_pcm_t **pcm, const char *name,
119 snd_pcm_format_t format, int channels, int rate,
120 unsigned int *buffer_time, unsigned int *period_time,
121 char **msg) {
122
123 snd_pcm_t *_pcm = NULL;
124 char *tmp = NULL;
125 char buf[256];
126 int err;
127
128 if ((err = snd_pcm_open(&_pcm, name, SND_PCM_STREAM_PLAYBACK, 0)) != 0) {
129 snprintf(buf, sizeof(buf), "Open PCM: %s", snd_strerror(err));
130 goto fail;
131 }
132
133 if ((err = alsa_pcm_set_hw_params(_pcm, format, channels, rate, buffer_time, period_time, &tmp)) != 0) {
134 snprintf(buf, sizeof(buf), "Set HW params: %s", tmp);
135 goto fail;
136 }
137
138 snd_pcm_uframes_t buffer_size, period_size;
139 if ((err = snd_pcm_get_params(_pcm, &buffer_size, &period_size)) != 0) {
140 snprintf(buf, sizeof(buf), "Get params: %s", snd_strerror(err));
141 goto fail;
142 }
143
144 if ((err = alsa_pcm_set_sw_params(_pcm, buffer_size, period_size, &tmp)) != 0) {
145 snprintf(buf, sizeof(buf), "Set SW params: %s", tmp);
146 goto fail;
147 }
148
149 if ((err = snd_pcm_prepare(_pcm)) != 0) {
150 snprintf(buf, sizeof(buf), "Prepare: %s", snd_strerror(err));
151 goto fail;
152 }
153
154 *pcm = _pcm;
155 return 0;
156
157 fail:
158 if (_pcm != NULL)
159 snd_pcm_close(_pcm);
160 if (msg != NULL)
161 *msg = strdup(buf);
162 if (tmp != NULL)
163 free(tmp);
164 return err;
165 }
166
167 void alsa_pcm_dump(snd_pcm_t *pcm, FILE *fp) {
168 snd_output_t *out;
169 snd_output_stdio_attach(&out, fp, 0);
170 snd_pcm_dump(pcm, out);
171 snd_output_close(out);
172 }