]>
Commit | Line | Data |
---|---|---|
1d14ffa9 FB |
1 | /* |
2 | * QEMU ALSA audio driver | |
3 | * | |
4 | * Copyright (c) 2005 Vassili Karpov (malc) | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
24 | #include <alsa/asoundlib.h> | |
749bc4bf PB |
25 | #include "qemu-common.h" |
26 | #include "audio.h" | |
1d14ffa9 FB |
27 | |
28 | #define AUDIO_CAP "alsa" | |
29 | #include "audio_int.h" | |
30 | ||
31 | typedef struct ALSAVoiceOut { | |
32 | HWVoiceOut hw; | |
33 | void *pcm_buf; | |
34 | snd_pcm_t *handle; | |
1d14ffa9 FB |
35 | } ALSAVoiceOut; |
36 | ||
37 | typedef struct ALSAVoiceIn { | |
38 | HWVoiceIn hw; | |
39 | snd_pcm_t *handle; | |
40 | void *pcm_buf; | |
1d14ffa9 FB |
41 | } ALSAVoiceIn; |
42 | ||
43 | static struct { | |
44 | int size_in_usec_in; | |
45 | int size_in_usec_out; | |
46 | const char *pcm_name_in; | |
47 | const char *pcm_name_out; | |
48 | unsigned int buffer_size_in; | |
49 | unsigned int period_size_in; | |
50 | unsigned int buffer_size_out; | |
51 | unsigned int period_size_out; | |
52 | unsigned int threshold; | |
53 | ||
fe8f096b TS |
54 | int buffer_size_in_overridden; |
55 | int period_size_in_overridden; | |
1d14ffa9 | 56 | |
fe8f096b TS |
57 | int buffer_size_out_overridden; |
58 | int period_size_out_overridden; | |
571ec3d6 | 59 | int verbose; |
1d14ffa9 | 60 | } conf = { |
8ead62cf FB |
61 | .pcm_name_out = "default", |
62 | .pcm_name_in = "default", | |
1d14ffa9 FB |
63 | }; |
64 | ||
65 | struct alsa_params_req { | |
ca9cc28c AZ |
66 | int freq; |
67 | snd_pcm_format_t fmt; | |
68 | int nchannels; | |
7a24c800 | 69 | int size_in_usec; |
1d14ffa9 FB |
70 | unsigned int buffer_size; |
71 | unsigned int period_size; | |
72 | }; | |
73 | ||
74 | struct alsa_params_obt { | |
75 | int freq; | |
76 | audfmt_e fmt; | |
ca9cc28c | 77 | int endianness; |
1d14ffa9 | 78 | int nchannels; |
c0fe3827 | 79 | snd_pcm_uframes_t samples; |
1d14ffa9 FB |
80 | }; |
81 | ||
82 | static void GCC_FMT_ATTR (2, 3) alsa_logerr (int err, const char *fmt, ...) | |
83 | { | |
84 | va_list ap; | |
85 | ||
86 | va_start (ap, fmt); | |
87 | AUD_vlog (AUDIO_CAP, fmt, ap); | |
88 | va_end (ap); | |
89 | ||
90 | AUD_log (AUDIO_CAP, "Reason: %s\n", snd_strerror (err)); | |
91 | } | |
92 | ||
93 | static void GCC_FMT_ATTR (3, 4) alsa_logerr2 ( | |
94 | int err, | |
95 | const char *typ, | |
96 | const char *fmt, | |
97 | ... | |
98 | ) | |
99 | { | |
100 | va_list ap; | |
101 | ||
c0fe3827 | 102 | AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ); |
1d14ffa9 FB |
103 | |
104 | va_start (ap, fmt); | |
105 | AUD_vlog (AUDIO_CAP, fmt, ap); | |
106 | va_end (ap); | |
107 | ||
108 | AUD_log (AUDIO_CAP, "Reason: %s\n", snd_strerror (err)); | |
109 | } | |
110 | ||
111 | static void alsa_anal_close (snd_pcm_t **handlep) | |
112 | { | |
113 | int err = snd_pcm_close (*handlep); | |
114 | if (err) { | |
115 | alsa_logerr (err, "Failed to close PCM handle %p\n", *handlep); | |
116 | } | |
117 | *handlep = NULL; | |
118 | } | |
119 | ||
120 | static int alsa_write (SWVoiceOut *sw, void *buf, int len) | |
121 | { | |
122 | return audio_pcm_sw_write (sw, buf, len); | |
123 | } | |
124 | ||
ca9cc28c | 125 | static snd_pcm_format_t aud_to_alsafmt (audfmt_e fmt) |
1d14ffa9 FB |
126 | { |
127 | switch (fmt) { | |
128 | case AUD_FMT_S8: | |
129 | return SND_PCM_FORMAT_S8; | |
130 | ||
131 | case AUD_FMT_U8: | |
132 | return SND_PCM_FORMAT_U8; | |
133 | ||
134 | case AUD_FMT_S16: | |
135 | return SND_PCM_FORMAT_S16_LE; | |
136 | ||
137 | case AUD_FMT_U16: | |
138 | return SND_PCM_FORMAT_U16_LE; | |
139 | ||
f941aa25 TS |
140 | case AUD_FMT_S32: |
141 | return SND_PCM_FORMAT_S32_LE; | |
142 | ||
143 | case AUD_FMT_U32: | |
144 | return SND_PCM_FORMAT_U32_LE; | |
145 | ||
1d14ffa9 FB |
146 | default: |
147 | dolog ("Internal logic error: Bad audio format %d\n", fmt); | |
148 | #ifdef DEBUG_AUDIO | |
149 | abort (); | |
150 | #endif | |
151 | return SND_PCM_FORMAT_U8; | |
152 | } | |
153 | } | |
154 | ||
ca9cc28c AZ |
155 | static int alsa_to_audfmt (snd_pcm_format_t alsafmt, audfmt_e *fmt, |
156 | int *endianness) | |
1d14ffa9 FB |
157 | { |
158 | switch (alsafmt) { | |
159 | case SND_PCM_FORMAT_S8: | |
160 | *endianness = 0; | |
161 | *fmt = AUD_FMT_S8; | |
162 | break; | |
163 | ||
164 | case SND_PCM_FORMAT_U8: | |
165 | *endianness = 0; | |
166 | *fmt = AUD_FMT_U8; | |
167 | break; | |
168 | ||
169 | case SND_PCM_FORMAT_S16_LE: | |
170 | *endianness = 0; | |
171 | *fmt = AUD_FMT_S16; | |
172 | break; | |
173 | ||
174 | case SND_PCM_FORMAT_U16_LE: | |
175 | *endianness = 0; | |
176 | *fmt = AUD_FMT_U16; | |
177 | break; | |
178 | ||
179 | case SND_PCM_FORMAT_S16_BE: | |
180 | *endianness = 1; | |
181 | *fmt = AUD_FMT_S16; | |
182 | break; | |
183 | ||
184 | case SND_PCM_FORMAT_U16_BE: | |
185 | *endianness = 1; | |
186 | *fmt = AUD_FMT_U16; | |
187 | break; | |
188 | ||
f941aa25 TS |
189 | case SND_PCM_FORMAT_S32_LE: |
190 | *endianness = 0; | |
191 | *fmt = AUD_FMT_S32; | |
192 | break; | |
193 | ||
194 | case SND_PCM_FORMAT_U32_LE: | |
195 | *endianness = 0; | |
196 | *fmt = AUD_FMT_U32; | |
197 | break; | |
198 | ||
199 | case SND_PCM_FORMAT_S32_BE: | |
200 | *endianness = 1; | |
201 | *fmt = AUD_FMT_S32; | |
202 | break; | |
203 | ||
204 | case SND_PCM_FORMAT_U32_BE: | |
205 | *endianness = 1; | |
206 | *fmt = AUD_FMT_U32; | |
207 | break; | |
208 | ||
1d14ffa9 FB |
209 | default: |
210 | dolog ("Unrecognized audio format %d\n", alsafmt); | |
211 | return -1; | |
212 | } | |
213 | ||
214 | return 0; | |
215 | } | |
216 | ||
1d14ffa9 FB |
217 | static void alsa_dump_info (struct alsa_params_req *req, |
218 | struct alsa_params_obt *obt) | |
219 | { | |
220 | dolog ("parameter | requested value | obtained value\n"); | |
221 | dolog ("format | %10d | %10d\n", req->fmt, obt->fmt); | |
222 | dolog ("channels | %10d | %10d\n", | |
223 | req->nchannels, obt->nchannels); | |
224 | dolog ("frequency | %10d | %10d\n", req->freq, obt->freq); | |
225 | dolog ("============================================\n"); | |
226 | dolog ("requested: buffer size %d period size %d\n", | |
227 | req->buffer_size, req->period_size); | |
c0fe3827 | 228 | dolog ("obtained: samples %ld\n", obt->samples); |
1d14ffa9 | 229 | } |
1d14ffa9 FB |
230 | |
231 | static void alsa_set_threshold (snd_pcm_t *handle, snd_pcm_uframes_t threshold) | |
232 | { | |
233 | int err; | |
234 | snd_pcm_sw_params_t *sw_params; | |
235 | ||
236 | snd_pcm_sw_params_alloca (&sw_params); | |
237 | ||
238 | err = snd_pcm_sw_params_current (handle, sw_params); | |
239 | if (err < 0) { | |
c0fe3827 | 240 | dolog ("Could not fully initialize DAC\n"); |
1d14ffa9 FB |
241 | alsa_logerr (err, "Failed to get current software parameters\n"); |
242 | return; | |
243 | } | |
244 | ||
245 | err = snd_pcm_sw_params_set_start_threshold (handle, sw_params, threshold); | |
246 | if (err < 0) { | |
c0fe3827 | 247 | dolog ("Could not fully initialize DAC\n"); |
1d14ffa9 FB |
248 | alsa_logerr (err, "Failed to set software threshold to %ld\n", |
249 | threshold); | |
250 | return; | |
251 | } | |
252 | ||
253 | err = snd_pcm_sw_params (handle, sw_params); | |
254 | if (err < 0) { | |
c0fe3827 | 255 | dolog ("Could not fully initialize DAC\n"); |
1d14ffa9 FB |
256 | alsa_logerr (err, "Failed to set software parameters\n"); |
257 | return; | |
258 | } | |
259 | } | |
260 | ||
261 | static int alsa_open (int in, struct alsa_params_req *req, | |
262 | struct alsa_params_obt *obt, snd_pcm_t **handlep) | |
263 | { | |
264 | snd_pcm_t *handle; | |
265 | snd_pcm_hw_params_t *hw_params; | |
60fe76f3 | 266 | int err; |
7a24c800 | 267 | int size_in_usec; |
60fe76f3 | 268 | unsigned int freq, nchannels; |
1d14ffa9 | 269 | const char *pcm_name = in ? conf.pcm_name_in : conf.pcm_name_out; |
1d14ffa9 FB |
270 | snd_pcm_uframes_t obt_buffer_size; |
271 | const char *typ = in ? "ADC" : "DAC"; | |
ca9cc28c | 272 | snd_pcm_format_t obtfmt; |
1d14ffa9 FB |
273 | |
274 | freq = req->freq; | |
1d14ffa9 | 275 | nchannels = req->nchannels; |
7a24c800 | 276 | size_in_usec = req->size_in_usec; |
1d14ffa9 FB |
277 | |
278 | snd_pcm_hw_params_alloca (&hw_params); | |
279 | ||
280 | err = snd_pcm_open ( | |
281 | &handle, | |
282 | pcm_name, | |
283 | in ? SND_PCM_STREAM_CAPTURE : SND_PCM_STREAM_PLAYBACK, | |
284 | SND_PCM_NONBLOCK | |
285 | ); | |
286 | if (err < 0) { | |
287 | alsa_logerr2 (err, typ, "Failed to open `%s':\n", pcm_name); | |
288 | return -1; | |
289 | } | |
290 | ||
291 | err = snd_pcm_hw_params_any (handle, hw_params); | |
292 | if (err < 0) { | |
293 | alsa_logerr2 (err, typ, "Failed to initialize hardware parameters\n"); | |
294 | goto err; | |
295 | } | |
296 | ||
297 | err = snd_pcm_hw_params_set_access ( | |
298 | handle, | |
299 | hw_params, | |
300 | SND_PCM_ACCESS_RW_INTERLEAVED | |
301 | ); | |
302 | if (err < 0) { | |
303 | alsa_logerr2 (err, typ, "Failed to set access type\n"); | |
304 | goto err; | |
305 | } | |
306 | ||
307 | err = snd_pcm_hw_params_set_format (handle, hw_params, req->fmt); | |
ca9cc28c | 308 | if (err < 0 && conf.verbose) { |
1d14ffa9 | 309 | alsa_logerr2 (err, typ, "Failed to set format %d\n", req->fmt); |
1d14ffa9 FB |
310 | } |
311 | ||
312 | err = snd_pcm_hw_params_set_rate_near (handle, hw_params, &freq, 0); | |
313 | if (err < 0) { | |
314 | alsa_logerr2 (err, typ, "Failed to set frequency %d\n", req->freq); | |
315 | goto err; | |
316 | } | |
317 | ||
318 | err = snd_pcm_hw_params_set_channels_near ( | |
319 | handle, | |
320 | hw_params, | |
321 | &nchannels | |
322 | ); | |
323 | if (err < 0) { | |
324 | alsa_logerr2 (err, typ, "Failed to set number of channels %d\n", | |
325 | req->nchannels); | |
326 | goto err; | |
327 | } | |
328 | ||
329 | if (nchannels != 1 && nchannels != 2) { | |
330 | alsa_logerr2 (err, typ, | |
331 | "Can not handle obtained number of channels %d\n", | |
332 | nchannels); | |
333 | goto err; | |
334 | } | |
335 | ||
7a24c800 | 336 | if (req->buffer_size) { |
337 | if (size_in_usec) { | |
338 | int dir = 0; | |
339 | unsigned int btime = req->buffer_size; | |
1d14ffa9 FB |
340 | |
341 | err = snd_pcm_hw_params_set_buffer_time_near ( | |
342 | handle, | |
343 | hw_params, | |
7a24c800 | 344 | &btime, |
345 | &dir | |
c0fe3827 | 346 | ); |
1d14ffa9 FB |
347 | } |
348 | else { | |
7a24c800 | 349 | snd_pcm_uframes_t bsize = req->buffer_size; |
1d14ffa9 | 350 | |
7a24c800 | 351 | err = snd_pcm_hw_params_set_buffer_size_near ( |
352 | handle, | |
353 | hw_params, | |
354 | &bsize | |
355 | ); | |
356 | } | |
357 | if (err < 0) { | |
358 | alsa_logerr2 (err, typ, "Failed to set buffer %s to %d\n", | |
359 | size_in_usec ? "time" : "size", req->buffer_size); | |
360 | goto err; | |
361 | } | |
362 | } | |
363 | ||
364 | if (req->period_size) { | |
365 | if (size_in_usec) { | |
366 | int dir = 0; | |
367 | unsigned int ptime = req->period_size; | |
1d14ffa9 | 368 | |
7a24c800 | 369 | err = snd_pcm_hw_params_set_period_time_near ( |
370 | handle, | |
1d14ffa9 | 371 | hw_params, |
7a24c800 | 372 | &ptime, |
373 | &dir | |
1d14ffa9 | 374 | ); |
7a24c800 | 375 | } |
376 | else { | |
377 | snd_pcm_uframes_t psize = req->period_size; | |
1d14ffa9 | 378 | |
7a24c800 | 379 | err = snd_pcm_hw_params_set_buffer_size_near ( |
1d14ffa9 FB |
380 | handle, |
381 | hw_params, | |
7a24c800 | 382 | &psize |
1d14ffa9 | 383 | ); |
1d14ffa9 | 384 | } |
7a24c800 | 385 | |
386 | if (err < 0) { | |
387 | alsa_logerr2 (err, typ, "Failed to set period %s to %d\n", | |
388 | size_in_usec ? "time" : "size", req->period_size); | |
389 | goto err; | |
390 | } | |
1d14ffa9 FB |
391 | } |
392 | ||
393 | err = snd_pcm_hw_params (handle, hw_params); | |
394 | if (err < 0) { | |
395 | alsa_logerr2 (err, typ, "Failed to apply audio parameters\n"); | |
396 | goto err; | |
397 | } | |
398 | ||
399 | err = snd_pcm_hw_params_get_buffer_size (hw_params, &obt_buffer_size); | |
400 | if (err < 0) { | |
401 | alsa_logerr2 (err, typ, "Failed to get buffer size\n"); | |
402 | goto err; | |
403 | } | |
404 | ||
ca9cc28c AZ |
405 | err = snd_pcm_hw_params_get_format (hw_params, &obtfmt); |
406 | if (err < 0) { | |
407 | alsa_logerr2 (err, typ, "Failed to get format\n"); | |
408 | goto err; | |
409 | } | |
410 | ||
411 | if (alsa_to_audfmt (obtfmt, &obt->fmt, &obt->endianness)) { | |
412 | dolog ("Invalid format was returned %d\n", obtfmt); | |
413 | goto err; | |
414 | } | |
415 | ||
1d14ffa9 FB |
416 | err = snd_pcm_prepare (handle); |
417 | if (err < 0) { | |
c0fe3827 | 418 | alsa_logerr2 (err, typ, "Could not prepare handle %p\n", handle); |
1d14ffa9 FB |
419 | goto err; |
420 | } | |
421 | ||
1d14ffa9 FB |
422 | if (!in && conf.threshold) { |
423 | snd_pcm_uframes_t threshold; | |
424 | int bytes_per_sec; | |
425 | ||
ca9cc28c AZ |
426 | bytes_per_sec = freq << (nchannels == 2); |
427 | ||
428 | switch (obt->fmt) { | |
429 | case AUD_FMT_S8: | |
430 | case AUD_FMT_U8: | |
431 | break; | |
432 | ||
433 | case AUD_FMT_S16: | |
434 | case AUD_FMT_U16: | |
435 | bytes_per_sec <<= 1; | |
436 | break; | |
437 | ||
438 | case AUD_FMT_S32: | |
439 | case AUD_FMT_U32: | |
440 | bytes_per_sec <<= 2; | |
441 | break; | |
442 | } | |
1d14ffa9 FB |
443 | |
444 | threshold = (conf.threshold * bytes_per_sec) / 1000; | |
445 | alsa_set_threshold (handle, threshold); | |
446 | } | |
447 | ||
1d14ffa9 FB |
448 | obt->nchannels = nchannels; |
449 | obt->freq = freq; | |
c0fe3827 | 450 | obt->samples = obt_buffer_size; |
ca9cc28c | 451 | |
1d14ffa9 FB |
452 | *handlep = handle; |
453 | ||
ca9cc28c AZ |
454 | if (conf.verbose && |
455 | (obt->fmt != req->fmt || | |
456 | obt->nchannels != req->nchannels || | |
457 | obt->freq != req->freq)) { | |
458 | dolog ("Audio paramters for %s\n", typ); | |
1d14ffa9 | 459 | alsa_dump_info (req, obt); |
1d14ffa9 FB |
460 | } |
461 | ||
462 | #ifdef DEBUG | |
463 | alsa_dump_info (req, obt); | |
464 | #endif | |
465 | return 0; | |
466 | ||
467 | err: | |
468 | alsa_anal_close (&handle); | |
469 | return -1; | |
470 | } | |
471 | ||
472 | static int alsa_recover (snd_pcm_t *handle) | |
473 | { | |
474 | int err = snd_pcm_prepare (handle); | |
475 | if (err < 0) { | |
476 | alsa_logerr (err, "Failed to prepare handle %p\n", handle); | |
477 | return -1; | |
478 | } | |
479 | return 0; | |
480 | } | |
481 | ||
571ec3d6 FB |
482 | static snd_pcm_sframes_t alsa_get_avail (snd_pcm_t *handle) |
483 | { | |
484 | snd_pcm_sframes_t avail; | |
485 | ||
486 | avail = snd_pcm_avail_update (handle); | |
487 | if (avail < 0) { | |
488 | if (avail == -EPIPE) { | |
489 | if (!alsa_recover (handle)) { | |
490 | avail = snd_pcm_avail_update (handle); | |
491 | } | |
492 | } | |
493 | ||
494 | if (avail < 0) { | |
495 | alsa_logerr (avail, | |
496 | "Could not obtain number of available frames\n"); | |
497 | return -1; | |
498 | } | |
499 | } | |
500 | ||
501 | return avail; | |
502 | } | |
503 | ||
1d14ffa9 FB |
504 | static int alsa_run_out (HWVoiceOut *hw) |
505 | { | |
506 | ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw; | |
507 | int rpos, live, decr; | |
508 | int samples; | |
509 | uint8_t *dst; | |
510 | st_sample_t *src; | |
511 | snd_pcm_sframes_t avail; | |
512 | ||
513 | live = audio_pcm_hw_get_live_out (hw); | |
514 | if (!live) { | |
515 | return 0; | |
516 | } | |
517 | ||
571ec3d6 | 518 | avail = alsa_get_avail (alsa->handle); |
1d14ffa9 | 519 | if (avail < 0) { |
571ec3d6 | 520 | dolog ("Could not get number of available playback frames\n"); |
1d14ffa9 FB |
521 | return 0; |
522 | } | |
523 | ||
1d14ffa9 FB |
524 | decr = audio_MIN (live, avail); |
525 | samples = decr; | |
526 | rpos = hw->rpos; | |
527 | while (samples) { | |
528 | int left_till_end_samples = hw->samples - rpos; | |
571ec3d6 | 529 | int len = audio_MIN (samples, left_till_end_samples); |
1d14ffa9 FB |
530 | snd_pcm_sframes_t written; |
531 | ||
532 | src = hw->mix_buf + rpos; | |
533 | dst = advance (alsa->pcm_buf, rpos << hw->info.shift); | |
534 | ||
571ec3d6 | 535 | hw->clip (dst, src, len); |
1d14ffa9 | 536 | |
571ec3d6 FB |
537 | while (len) { |
538 | written = snd_pcm_writei (alsa->handle, dst, len); | |
4787c71d | 539 | |
571ec3d6 | 540 | if (written <= 0) { |
4787c71d | 541 | switch (written) { |
571ec3d6 FB |
542 | case 0: |
543 | if (conf.verbose) { | |
544 | dolog ("Failed to write %d frames (wrote zero)\n", len); | |
4787c71d | 545 | } |
4787c71d FB |
546 | goto exit; |
547 | ||
571ec3d6 FB |
548 | case -EPIPE: |
549 | if (alsa_recover (alsa->handle)) { | |
550 | alsa_logerr (written, "Failed to write %d frames\n", | |
551 | len); | |
552 | goto exit; | |
553 | } | |
554 | if (conf.verbose) { | |
555 | dolog ("Recovering from playback xrun\n"); | |
556 | } | |
4787c71d FB |
557 | continue; |
558 | ||
571ec3d6 FB |
559 | case -EAGAIN: |
560 | goto exit; | |
561 | ||
4787c71d FB |
562 | default: |
563 | alsa_logerr (written, "Failed to write %d frames to %p\n", | |
571ec3d6 | 564 | len, dst); |
4787c71d | 565 | goto exit; |
1d14ffa9 | 566 | } |
1d14ffa9 | 567 | } |
1d14ffa9 | 568 | |
4787c71d FB |
569 | rpos = (rpos + written) % hw->samples; |
570 | samples -= written; | |
571ec3d6 | 571 | len -= written; |
4787c71d FB |
572 | dst = advance (dst, written << hw->info.shift); |
573 | src += written; | |
574 | } | |
1d14ffa9 FB |
575 | } |
576 | ||
577 | exit: | |
578 | hw->rpos = rpos; | |
579 | return decr; | |
580 | } | |
581 | ||
582 | static void alsa_fini_out (HWVoiceOut *hw) | |
583 | { | |
584 | ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw; | |
585 | ||
586 | ldebug ("alsa_fini\n"); | |
587 | alsa_anal_close (&alsa->handle); | |
588 | ||
589 | if (alsa->pcm_buf) { | |
590 | qemu_free (alsa->pcm_buf); | |
591 | alsa->pcm_buf = NULL; | |
592 | } | |
593 | } | |
594 | ||
c0fe3827 | 595 | static int alsa_init_out (HWVoiceOut *hw, audsettings_t *as) |
1d14ffa9 FB |
596 | { |
597 | ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw; | |
598 | struct alsa_params_req req; | |
599 | struct alsa_params_obt obt; | |
1d14ffa9 | 600 | snd_pcm_t *handle; |
c0fe3827 | 601 | audsettings_t obt_as; |
1d14ffa9 | 602 | |
c0fe3827 FB |
603 | req.fmt = aud_to_alsafmt (as->fmt); |
604 | req.freq = as->freq; | |
605 | req.nchannels = as->nchannels; | |
1d14ffa9 FB |
606 | req.period_size = conf.period_size_out; |
607 | req.buffer_size = conf.buffer_size_out; | |
7a24c800 | 608 | req.size_in_usec = conf.size_in_usec_in; |
1d14ffa9 FB |
609 | |
610 | if (alsa_open (0, &req, &obt, &handle)) { | |
611 | return -1; | |
612 | } | |
613 | ||
c0fe3827 FB |
614 | obt_as.freq = obt.freq; |
615 | obt_as.nchannels = obt.nchannels; | |
ca9cc28c AZ |
616 | obt_as.fmt = obt.fmt; |
617 | obt_as.endianness = obt.endianness; | |
c0fe3827 | 618 | |
d929eba5 | 619 | audio_pcm_init_info (&hw->info, &obt_as); |
c0fe3827 | 620 | hw->samples = obt.samples; |
1d14ffa9 | 621 | |
c0fe3827 | 622 | alsa->pcm_buf = audio_calloc (AUDIO_FUNC, obt.samples, 1 << hw->info.shift); |
1d14ffa9 | 623 | if (!alsa->pcm_buf) { |
4787c71d FB |
624 | dolog ("Could not allocate DAC buffer (%d samples, each %d bytes)\n", |
625 | hw->samples, 1 << hw->info.shift); | |
1d14ffa9 FB |
626 | alsa_anal_close (&handle); |
627 | return -1; | |
628 | } | |
629 | ||
630 | alsa->handle = handle; | |
1d14ffa9 FB |
631 | return 0; |
632 | } | |
633 | ||
571ec3d6 | 634 | static int alsa_voice_ctl (snd_pcm_t *handle, const char *typ, int pause) |
1d14ffa9 FB |
635 | { |
636 | int err; | |
571ec3d6 FB |
637 | |
638 | if (pause) { | |
639 | err = snd_pcm_drop (handle); | |
640 | if (err < 0) { | |
32d448c4 | 641 | alsa_logerr (err, "Could not stop %s\n", typ); |
571ec3d6 FB |
642 | return -1; |
643 | } | |
644 | } | |
645 | else { | |
646 | err = snd_pcm_prepare (handle); | |
647 | if (err < 0) { | |
32d448c4 | 648 | alsa_logerr (err, "Could not prepare handle for %s\n", typ); |
571ec3d6 FB |
649 | return -1; |
650 | } | |
651 | } | |
652 | ||
653 | return 0; | |
654 | } | |
655 | ||
656 | static int alsa_ctl_out (HWVoiceOut *hw, int cmd, ...) | |
657 | { | |
1d14ffa9 FB |
658 | ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw; |
659 | ||
660 | switch (cmd) { | |
661 | case VOICE_ENABLE: | |
662 | ldebug ("enabling voice\n"); | |
571ec3d6 | 663 | return alsa_voice_ctl (alsa->handle, "playback", 0); |
1d14ffa9 FB |
664 | |
665 | case VOICE_DISABLE: | |
666 | ldebug ("disabling voice\n"); | |
571ec3d6 | 667 | return alsa_voice_ctl (alsa->handle, "playback", 1); |
1d14ffa9 | 668 | } |
571ec3d6 FB |
669 | |
670 | return -1; | |
1d14ffa9 FB |
671 | } |
672 | ||
c0fe3827 | 673 | static int alsa_init_in (HWVoiceIn *hw, audsettings_t *as) |
1d14ffa9 FB |
674 | { |
675 | ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw; | |
676 | struct alsa_params_req req; | |
677 | struct alsa_params_obt obt; | |
1d14ffa9 | 678 | snd_pcm_t *handle; |
c0fe3827 | 679 | audsettings_t obt_as; |
1d14ffa9 | 680 | |
c0fe3827 FB |
681 | req.fmt = aud_to_alsafmt (as->fmt); |
682 | req.freq = as->freq; | |
683 | req.nchannels = as->nchannels; | |
1d14ffa9 FB |
684 | req.period_size = conf.period_size_in; |
685 | req.buffer_size = conf.buffer_size_in; | |
7a24c800 | 686 | req.size_in_usec = conf.size_in_usec_in; |
1d14ffa9 FB |
687 | |
688 | if (alsa_open (1, &req, &obt, &handle)) { | |
689 | return -1; | |
690 | } | |
691 | ||
c0fe3827 FB |
692 | obt_as.freq = obt.freq; |
693 | obt_as.nchannels = obt.nchannels; | |
ca9cc28c AZ |
694 | obt_as.fmt = obt.fmt; |
695 | obt_as.endianness = obt.endianness; | |
c0fe3827 | 696 | |
d929eba5 | 697 | audio_pcm_init_info (&hw->info, &obt_as); |
c0fe3827 FB |
698 | hw->samples = obt.samples; |
699 | ||
700 | alsa->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift); | |
1d14ffa9 | 701 | if (!alsa->pcm_buf) { |
4787c71d FB |
702 | dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n", |
703 | hw->samples, 1 << hw->info.shift); | |
1d14ffa9 FB |
704 | alsa_anal_close (&handle); |
705 | return -1; | |
706 | } | |
707 | ||
708 | alsa->handle = handle; | |
709 | return 0; | |
710 | } | |
711 | ||
712 | static void alsa_fini_in (HWVoiceIn *hw) | |
713 | { | |
714 | ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw; | |
715 | ||
716 | alsa_anal_close (&alsa->handle); | |
717 | ||
718 | if (alsa->pcm_buf) { | |
719 | qemu_free (alsa->pcm_buf); | |
720 | alsa->pcm_buf = NULL; | |
721 | } | |
722 | } | |
723 | ||
724 | static int alsa_run_in (HWVoiceIn *hw) | |
725 | { | |
726 | ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw; | |
727 | int hwshift = hw->info.shift; | |
728 | int i; | |
729 | int live = audio_pcm_hw_get_live_in (hw); | |
730 | int dead = hw->samples - live; | |
571ec3d6 | 731 | int decr; |
1d14ffa9 FB |
732 | struct { |
733 | int add; | |
734 | int len; | |
735 | } bufs[2] = { | |
736 | { hw->wpos, 0 }, | |
737 | { 0, 0 } | |
738 | }; | |
571ec3d6 | 739 | snd_pcm_sframes_t avail; |
1d14ffa9 FB |
740 | snd_pcm_uframes_t read_samples = 0; |
741 | ||
742 | if (!dead) { | |
743 | return 0; | |
744 | } | |
745 | ||
571ec3d6 FB |
746 | avail = alsa_get_avail (alsa->handle); |
747 | if (avail < 0) { | |
748 | dolog ("Could not get number of captured frames\n"); | |
749 | return 0; | |
750 | } | |
751 | ||
752 | if (!avail && (snd_pcm_state (alsa->handle) == SND_PCM_STATE_PREPARED)) { | |
753 | avail = hw->samples; | |
754 | } | |
755 | ||
756 | decr = audio_MIN (dead, avail); | |
757 | if (!decr) { | |
758 | return 0; | |
759 | } | |
760 | ||
761 | if (hw->wpos + decr > hw->samples) { | |
1d14ffa9 | 762 | bufs[0].len = (hw->samples - hw->wpos); |
571ec3d6 | 763 | bufs[1].len = (decr - (hw->samples - hw->wpos)); |
1d14ffa9 FB |
764 | } |
765 | else { | |
571ec3d6 | 766 | bufs[0].len = decr; |
1d14ffa9 FB |
767 | } |
768 | ||
1d14ffa9 FB |
769 | for (i = 0; i < 2; ++i) { |
770 | void *src; | |
771 | st_sample_t *dst; | |
772 | snd_pcm_sframes_t nread; | |
773 | snd_pcm_uframes_t len; | |
774 | ||
775 | len = bufs[i].len; | |
776 | ||
777 | src = advance (alsa->pcm_buf, bufs[i].add << hwshift); | |
778 | dst = hw->conv_buf + bufs[i].add; | |
779 | ||
780 | while (len) { | |
781 | nread = snd_pcm_readi (alsa->handle, src, len); | |
782 | ||
571ec3d6 | 783 | if (nread <= 0) { |
1d14ffa9 | 784 | switch (nread) { |
571ec3d6 FB |
785 | case 0: |
786 | if (conf.verbose) { | |
787 | dolog ("Failed to read %ld frames (read zero)\n", len); | |
1d14ffa9 | 788 | } |
1d14ffa9 FB |
789 | goto exit; |
790 | ||
571ec3d6 FB |
791 | case -EPIPE: |
792 | if (alsa_recover (alsa->handle)) { | |
793 | alsa_logerr (nread, "Failed to read %ld frames\n", len); | |
794 | goto exit; | |
795 | } | |
796 | if (conf.verbose) { | |
797 | dolog ("Recovering from capture xrun\n"); | |
798 | } | |
1d14ffa9 FB |
799 | continue; |
800 | ||
571ec3d6 FB |
801 | case -EAGAIN: |
802 | goto exit; | |
803 | ||
1d14ffa9 FB |
804 | default: |
805 | alsa_logerr ( | |
806 | nread, | |
807 | "Failed to read %ld frames from %p\n", | |
808 | len, | |
809 | src | |
810 | ); | |
811 | goto exit; | |
812 | } | |
813 | } | |
814 | ||
815 | hw->conv (dst, src, nread, &nominal_volume); | |
816 | ||
817 | src = advance (src, nread << hwshift); | |
818 | dst += nread; | |
819 | ||
820 | read_samples += nread; | |
821 | len -= nread; | |
822 | } | |
823 | } | |
824 | ||
825 | exit: | |
826 | hw->wpos = (hw->wpos + read_samples) % hw->samples; | |
827 | return read_samples; | |
828 | } | |
829 | ||
830 | static int alsa_read (SWVoiceIn *sw, void *buf, int size) | |
831 | { | |
832 | return audio_pcm_sw_read (sw, buf, size); | |
833 | } | |
834 | ||
835 | static int alsa_ctl_in (HWVoiceIn *hw, int cmd, ...) | |
836 | { | |
571ec3d6 FB |
837 | ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw; |
838 | ||
839 | switch (cmd) { | |
840 | case VOICE_ENABLE: | |
841 | ldebug ("enabling voice\n"); | |
842 | return alsa_voice_ctl (alsa->handle, "capture", 0); | |
843 | ||
844 | case VOICE_DISABLE: | |
845 | ldebug ("disabling voice\n"); | |
846 | return alsa_voice_ctl (alsa->handle, "capture", 1); | |
847 | } | |
848 | ||
849 | return -1; | |
1d14ffa9 FB |
850 | } |
851 | ||
852 | static void *alsa_audio_init (void) | |
853 | { | |
854 | return &conf; | |
855 | } | |
856 | ||
857 | static void alsa_audio_fini (void *opaque) | |
858 | { | |
859 | (void) opaque; | |
860 | } | |
861 | ||
862 | static struct audio_option alsa_options[] = { | |
863 | {"DAC_SIZE_IN_USEC", AUD_OPT_BOOL, &conf.size_in_usec_out, | |
864 | "DAC period/buffer size in microseconds (otherwise in frames)", NULL, 0}, | |
865 | {"DAC_PERIOD_SIZE", AUD_OPT_INT, &conf.period_size_out, | |
7a24c800 | 866 | "DAC period size (0 to go with system default)", |
867 | &conf.period_size_out_overridden, 0}, | |
1d14ffa9 | 868 | {"DAC_BUFFER_SIZE", AUD_OPT_INT, &conf.buffer_size_out, |
7a24c800 | 869 | "DAC buffer size (0 to go with system default)", |
870 | &conf.buffer_size_out_overridden, 0}, | |
1d14ffa9 FB |
871 | |
872 | {"ADC_SIZE_IN_USEC", AUD_OPT_BOOL, &conf.size_in_usec_in, | |
873 | "ADC period/buffer size in microseconds (otherwise in frames)", NULL, 0}, | |
874 | {"ADC_PERIOD_SIZE", AUD_OPT_INT, &conf.period_size_in, | |
7a24c800 | 875 | "ADC period size (0 to go with system default)", |
876 | &conf.period_size_in_overridden, 0}, | |
1d14ffa9 | 877 | {"ADC_BUFFER_SIZE", AUD_OPT_INT, &conf.buffer_size_in, |
7a24c800 | 878 | "ADC buffer size (0 to go with system default)", |
879 | &conf.buffer_size_in_overridden, 0}, | |
1d14ffa9 FB |
880 | |
881 | {"THRESHOLD", AUD_OPT_INT, &conf.threshold, | |
882 | "(undocumented)", NULL, 0}, | |
883 | ||
884 | {"DAC_DEV", AUD_OPT_STR, &conf.pcm_name_out, | |
885 | "DAC device name (for instance dmix)", NULL, 0}, | |
886 | ||
887 | {"ADC_DEV", AUD_OPT_STR, &conf.pcm_name_in, | |
888 | "ADC device name", NULL, 0}, | |
571ec3d6 FB |
889 | |
890 | {"VERBOSE", AUD_OPT_BOOL, &conf.verbose, | |
891 | "Behave in a more verbose way", NULL, 0}, | |
892 | ||
1d14ffa9 FB |
893 | {NULL, 0, NULL, NULL, NULL, 0} |
894 | }; | |
895 | ||
896 | static struct audio_pcm_ops alsa_pcm_ops = { | |
897 | alsa_init_out, | |
898 | alsa_fini_out, | |
899 | alsa_run_out, | |
900 | alsa_write, | |
901 | alsa_ctl_out, | |
902 | ||
903 | alsa_init_in, | |
904 | alsa_fini_in, | |
905 | alsa_run_in, | |
906 | alsa_read, | |
907 | alsa_ctl_in | |
908 | }; | |
909 | ||
910 | struct audio_driver alsa_audio_driver = { | |
911 | INIT_FIELD (name = ) "alsa", | |
912 | INIT_FIELD (descr = ) "ALSA http://www.alsa-project.org", | |
913 | INIT_FIELD (options = ) alsa_options, | |
914 | INIT_FIELD (init = ) alsa_audio_init, | |
915 | INIT_FIELD (fini = ) alsa_audio_fini, | |
916 | INIT_FIELD (pcm_ops = ) &alsa_pcm_ops, | |
917 | INIT_FIELD (can_be_default = ) 1, | |
918 | INIT_FIELD (max_voices_out = ) INT_MAX, | |
919 | INIT_FIELD (max_voices_in = ) INT_MAX, | |
920 | INIT_FIELD (voice_size_out = ) sizeof (ALSAVoiceOut), | |
921 | INIT_FIELD (voice_size_in = ) sizeof (ALSAVoiceIn) | |
922 | }; |