Debian Codegraph @

src/audio/au_alsa.c

raw
1 /*************************************************************************/
2 /* */
3 /* Language Technologies Institute */
4 /* Carnegie Mellon University */
5 /* Copyright (c) 2005 */
6 /* All Rights Reserved. */
7 /* */
8 /* Permission is hereby granted, free of charge, to use and distribute */
9 /* this software and its documentation without restriction, including */
10 /* without limitation the rights to use, copy, modify, merge, publish, */
11 /* distribute, sublicense, and/or sell copies of this work, and to */
12 /* permit persons to whom this work is furnished to do so, subject to */
13 /* the following conditions: */
14 /* 1. The code must retain the above copyright notice, this list of */
15 /* conditions and the following disclaimer. */
16 /* 2. Any modifications must be clearly marked as such. */
17 /* 3. Original authors' names are not deleted. */
18 /* 4. The authors' names are not used to endorse or promote products */
19 /* derived from this software without specific prior written */
20 /* permission. */
21 /* */
22 /* CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK */
23 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25 /* SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE */
26 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30 /* THIS SOFTWARE. */
31 /* */
32 /*********************************************************************** */
33 /* Author: Lukas Loehrer () */
34 /* Date: January 2005 */
35 /*************************************************************************/
36 /* */
37 /* Native access to alsa audio devices on Linux */
38 /* Tested with libasound version 1.0.10 */
39 /* */
40 /* Added snd_config_update_free_global(); after every close to stop */
41 /* (apparent?) memory leaks */
42 /*************************************************************************/
43
44 #include <stdlib.h>
45 #include <unistd.h>
46 #include <sys/types.h>
47 #include <assert.h>
48 #include <errno.h>
49
50 #include "cst_string.h"
51 #include "cst_wave.h"
52 #include "cst_audio.h"
53
54 #include <alsa/asoundlib.h>
55
56 /*static char *pcm_dev_name = "hw:0,0"; */
57 static const char *pcm_dev_name ="default";
58
59 static inline void print_pcm_state(snd_pcm_t *handle, char *msg)
60 {
61 fprintf(stderr, "PCM state at %s = %s\n", msg,
62 snd_pcm_state_name(snd_pcm_state(handle)));
63 }
64
65 cst_audiodev *audio_open_alsa(unsigned int sps, int channels, cst_audiofmt fmt)
66 {
67 cst_audiodev *ad;
68 unsigned int real_rate;
69 int err;
70
71 /* alsa specific stuff */
72 snd_pcm_t *pcm_handle;
73 snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK;
74 snd_pcm_hw_params_t *hwparams;
75 snd_pcm_format_t format;
76 snd_pcm_access_t access = SND_PCM_ACCESS_RW_INTERLEAVED;
77
78 /* Allocate the snd_pcm_hw_params_t structure on the stack. */
79 snd_pcm_hw_params_alloca(&hwparams);
80
81 /* Open pcm device */
82 err = snd_pcm_open(&pcm_handle, pcm_dev_name, stream, 0);
83 if (err < 0)
84 {
85 cst_errmsg("audio_open_alsa: failed to open audio device %s. %s\n",
86 pcm_dev_name, snd_strerror(err));
87 return NULL;
88 }
89
90 /* Init hwparams with full configuration space */
91 err = snd_pcm_hw_params_any(pcm_handle, hwparams);
92 if (err < 0)
93 {
94 snd_pcm_close(pcm_handle);
95 snd_config_update_free_global();
96 cst_errmsg("audio_open_alsa: failed to get hardware parameters from audio device. %s\n", snd_strerror(err));
97 return NULL;
98 }
99
100 /* Set access mode */
101 err = snd_pcm_hw_params_set_access(pcm_handle, hwparams, access);
102 if (err < 0)
103 {
104 snd_pcm_close(pcm_handle);
105 snd_config_update_free_global();
106 cst_errmsg("audio_open_alsa: failed to set access mode. %s.\n", snd_strerror(err));
107 return NULL;
108 }
109
110 /* Determine matching alsa sample format */
111 /* This could be implemented in a more */
112 /* flexible way (byte order conversion). */
113 switch (fmt)
114 {
115 case CST_AUDIO_LINEAR16:
116 if (CST_LITTLE_ENDIAN)
117 format = SND_PCM_FORMAT_S16_LE;
118 else
119 format = SND_PCM_FORMAT_S16_BE;
120 break;
121 case CST_AUDIO_LINEAR8:
122 format = SND_PCM_FORMAT_U8;
123 break;
124 case CST_AUDIO_MULAW:
125 format = SND_PCM_FORMAT_MU_LAW;
126 break;
127 default:
128 snd_pcm_close(pcm_handle);
129 snd_config_update_free_global();
130 cst_errmsg("audio_open_alsa: failed to find suitable format.\n");
131 return NULL;
132 break;
133 }
134
135 /* Set samble format */
136 err = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format);
137 if (err <0)
138 {
139 snd_pcm_close(pcm_handle);
140 snd_config_update_free_global();
141 cst_errmsg("audio_open_alsa: failed to set format. %s.\n", snd_strerror(err));
142 return NULL;
143 }
144
145 /* Set sample rate near the desired rate */
146 real_rate = sps;
147 err = snd_pcm_hw_params_set_rate(pcm_handle, hwparams, real_rate, 0);
148 if (err < 0)
149 {
150 snd_pcm_close(pcm_handle);
151 snd_config_update_free_global();
152 cst_errmsg("audio_open_alsa: failed to set sample rate near %d. %s.\n", sps, snd_strerror(err));
153 return NULL;
154 }
155 /*FIXME: This is probably too strict */
156 assert(sps == real_rate);
157
158 /* Set number of channels */
159 assert(channels >0);
160 err = snd_pcm_hw_params_set_channels(pcm_handle, hwparams, channels);
161 if (err < 0)
162 {
163 snd_pcm_close(pcm_handle);
164 snd_config_update_free_global();
165 cst_errmsg("audio_open_alsa: failed to set number of channels to %d. %s.\n", channels, snd_strerror(err));
166 return NULL;
167 }
168
169 /* Commit hardware parameters */
170 err = snd_pcm_hw_params(pcm_handle, hwparams);
171 if (err < 0)
172 {
173 snd_pcm_close(pcm_handle);
174 snd_config_update_free_global();
175 cst_errmsg("audio_open_alsa: failed to set hw parameters. %s.\n", snd_strerror(err));
176 return NULL;
177 }
178
179 /* Make sure the device is ready to accept data */
180 assert(snd_pcm_state(pcm_handle) == SND_PCM_STATE_PREPARED);
181
182 /* snd_pcm_hw_params_free(hwparams); */
183 /* There doesn't seem to be another way to set the latency -- if done
184 here, it works, if not, it looses the first 2s or so */
185 snd_pcm_set_params(pcm_handle,
186 format,
187 SND_PCM_ACCESS_RW_INTERLEAVED,
188 1,
189 real_rate,
190 1,
191 50000);
192
193 /* Write hardware parameters to flite audio device data structure */
194 ad = cst_alloc(cst_audiodev, 1);
195 assert(ad != NULL);
196 ad->real_sps = ad->sps = sps;
197 ad->real_channels = ad->channels = channels;
198 ad->real_fmt = ad->fmt = fmt;
199 ad->platform_data = (void *) pcm_handle;
200
201 return ad;
202 }
203
204 int audio_close_alsa(cst_audiodev *ad)
205 {
206 int result;
207 snd_pcm_t *pcm_handle;
208
209 if (ad == NULL)
210 return 0;
211
212 pcm_handle = (snd_pcm_t *) ad->platform_data;
213
214 snd_pcm_drain(pcm_handle); /* wait for current stuff in buffer to finish */
215
216 result = snd_pcm_close(pcm_handle);
217 snd_config_update_free_global();
218 if (result < 0)
219 {
220 cst_errmsg("audio_close_alsa: Error: %s.\n", snd_strerror(result));
221 }
222 cst_free(ad);
223 return result;
224 }
225
226 /* Returns zero if recovery was successful. */
227 static int recover_from_error(snd_pcm_t *pcm_handle, ssize_t res)
228 {
229 if (res == -EPIPE) /* xrun */
230 {
231 res = snd_pcm_prepare(pcm_handle);
232 if (res < 0)
233 {
234 /* Failed to recover from xrun */
235 cst_errmsg("recover_from_write_error: failed to recover from xrun. %s\n.", snd_strerror(res));
236 return res;
237 }
238 }
239 else if (res == -ESTRPIPE) /* Suspend */
240 {
241 while ((res = snd_pcm_resume(pcm_handle)) == -EAGAIN)
242 {
243 snd_pcm_wait(pcm_handle, 1000);
244 }
245 if (res < 0)
246 {
247 res = snd_pcm_prepare(pcm_handle);
248 if (res <0)
249 {
250 /* Resume failed */
251 cst_errmsg("audio_recover_from_write_error: failed to resume after suspend. %s\n.", snd_strerror(res));
252 return res;
253 }
254 }
255 }
256 else if (res < 0)
257 {
258 /* Unknown failure */
259 cst_errmsg("audio_recover_from_write_error: %s.\n", snd_strerror(res));
260 return res;
261 }
262 return 0;
263 }
264
265 int audio_write_alsa(cst_audiodev *ad, void *samples, int num_bytes)
266 {
267 size_t frame_size;
268 ssize_t num_frames, res;
269 snd_pcm_t *pcm_handle;
270 char *buf = (char *) samples;
271
272 /* Determine frame size in bytes */
273 frame_size = audio_bps(ad->real_fmt) * ad->real_channels;
274 /* Require that only complete frames are handed in */
275 assert((num_bytes % frame_size) == 0);
276 num_frames = num_bytes / frame_size;
277 pcm_handle = (snd_pcm_t *) ad->platform_data;
278
279 while (num_frames > 0)
280 {
281 res = snd_pcm_writei(pcm_handle, buf, num_frames);
282
283 if (res != num_frames)
284 {
285 if (res == -EAGAIN || (res > 0 && res < num_frames))
286 {
287 snd_pcm_wait(pcm_handle, 100);
288 }
289 else if (recover_from_error(pcm_handle, res) < 0)
290 {
291 return -1;
292 }
293 }
294
295 if (res >0)
296 {
297 num_frames -= res;
298 buf += res * frame_size;
299 }
300 }
301
302 return num_bytes;
303 }
304
305 int audio_flush_alsa(cst_audiodev *ad)
306 {
307 int result;
308 result = snd_pcm_drain((snd_pcm_t *) ad->platform_data);
309 if (result < 0)
310 {
311 cst_errmsg("audio_flush_alsa: Error: %s.\n", snd_strerror(result));
312 }
313 /* Prepare device for more data */
314 result = snd_pcm_prepare((snd_pcm_t *) ad->platform_data);
315 if (result < 0)
316 {
317 cst_errmsg("audio_flush_alsa: Error: %s.\n", snd_strerror(result));
318 }
319 return result;
320 }
321
322 int audio_drain_alsa(cst_audiodev *ad)
323 {
324 int result;
325 result = snd_pcm_drop((snd_pcm_t *) ad->platform_data);
326 if (result < 0)
327 {
328 cst_errmsg("audio_drain_alsa: Error: %s.\n", snd_strerror(result));
329 }
330 /* Prepare device for more data */
331 result = snd_pcm_prepare((snd_pcm_t *) ad->platform_data);
332 if (result < 0)
333 {
334 cst_errmsg("audio_drain_alsa: Error: %s.\n", snd_strerror(result));
335 }
336 return result;
337 }