Fix some greedy sed changes in imported code. Also provide a sys/types.h for compatib...
[kugel-rb.git] / apps / plugins / pdbox / PDa / src / s_audio_alsa.c
blobb8e8535dc93e6805ed69bf3c165970783b9ffea7
1 /* Copyright (c) 1997-2003 Guenter Geiger, Miller Puckette, Larry Troxler,
2 * Winfried Ritsch, Karl MacMillan, and others.
3 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
4 * WARRANTIES, see the file, "LICENSE.txt," in this distribution. */
6 /* this file inputs and outputs audio using the ALSA API available on linux. */
8 #include <alsa/asoundlib.h>
10 #include "m_pd.h"
11 #include "s_stuff.h"
12 #include <errno.h>
13 #include <stdio.h>
14 #include <unistd.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <sys/types.h>
18 #include <sys/time.h>
19 #include <sys/stat.h>
20 #include <sys/ioctl.h>
21 #include <fcntl.h>
22 #include <sched.h>
23 #include <sys/mman.h>
25 typedef int16_t t_alsa_sample16;
26 typedef int32_t t_alsa_sample32;
27 #define ALSA_SAMPLEWIDTH_16 sizeof(t_alsa_sample16)
28 #define ALSA_SAMPLEWIDTH_32 sizeof(t_alsa_sample32)
29 #define ALSA_XFERSIZE16 (signed int)(sizeof(t_alsa_sample16) * DEFDACBLKSIZE)
30 #define ALSA_XFERSIZE32 (signed int)(sizeof(t_alsa_sample32) * DEFDACBLKSIZE)
31 #define ALSA_MAXDEV 1
32 #define ALSA_JITTER 1024
33 #define ALSA_EXTRABUFFER 2048
34 #define ALSA_DEFFRAGSIZE 64
35 #define ALSA_DEFNFRAG 12
37 #ifndef INT32_MAX
38 #define INT32_MAX 0x7fffffff
39 #endif
41 #if (SND_LIB_MAJOR < 1)
42 #define ALSAAPI9
43 #endif
45 typedef struct _alsa_dev
47 snd_pcm_t *inhandle;
48 snd_pcm_t *outhandle;
49 int innoninterleave; /* true if we're set for noninterleaved read */
50 int outnoninterleave; /* same for write */
51 } t_alsa_dev;
53 t_alsa_dev alsa_device;
54 static void *alsa_snd_buf = 0;
55 static void **alsa_buf_ptrs;
56 static int alsa_samplewidth;
57 static snd_pcm_status_t* in_status;
58 static snd_pcm_status_t* out_status;
60 static int alsa_mode;
61 static int alsa_buf_samps; /* believed actual ALSA bufsize in sample frames */
62 static int alsa_inchannels;
63 static int alsa_outchannels;
65 /* Defines */
66 #define DEBUG(x) x
67 #define DEBUG2(x) {x;}
69 static void alsa_checkiosync( void);
70 static void alsa_numbertoname(int devno, char *devname, int nchar);
72 /* don't assume we can turn all 31 bits when doing float-to-fix;
73 otherwise some audio drivers (e.g. Midiman/ALSA) wrap around. */
74 #define FMAX 0x7ffff000
75 #define CLIP32(x) (((x)>FMAX)?FMAX:((x) < -FMAX)?-FMAX:(x))
77 /* support for ALSA pcmv2 api by Karl MacMillan<karlmac@peabody.jhu.edu> */
79 static void check_error(int err, const char *why)
81 if (err < 0)
82 fprintf(stderr, "%s: %s\n", why, snd_strerror(err));
85 /* was: alsa_open_audio(int wantinchans, int wantoutchans, int srate) */
87 int alsa_open_audio(int naudioindev, int *audioindev, int nchindev,
88 int *chindev, int naudiooutdev, int *audiooutdev, int nchoutdev,
89 int *choutdev, int rate)
91 int err, inchans = 0, outchans = 0, subunitdir;
92 char devname[512];
93 snd_pcm_hw_params_t* hw_params;
94 snd_pcm_sw_params_t* sw_params;
95 snd_output_t* out;
96 int frag_size = (sys_blocksize ? sys_blocksize : ALSA_DEFFRAGSIZE);
97 int nfrags, i;
98 short* tmp_buf;
99 unsigned int tmp_uint;
100 snd_pcm_uframes_t tmp_snd_pcm_uframes;
101 int wantinchans, wantoutchans, devno;
103 if (naudioindev >= 2 || naudiooutdev >= 2)
104 post("alsa: only one input and output device allowed (extras ignored");
105 if (naudioindev >= 1 && naudiooutdev >= 1 &&
106 audioindev[0] != audiooutdev[0])
107 post("alsa: changing output device to agree with input device");
108 if (nchindev)
109 wantinchans = chindev[0];
110 else wantinchans = (naudioindev ? 2 : 0);
111 if (nchoutdev)
112 wantoutchans = choutdev[0];
113 else wantoutchans = (naudiooutdev ? 2 : 0);
114 devno = (naudioindev > 0 ? audioindev[0] :
115 (naudiooutdev > 0 ? audiooutdev[0] : 0));
117 alsa_numbertoname(devno, devname, 512);
119 if (sys_verbose)
120 post("device name %s; channels in %d, out %d", devname, wantinchans,
121 wantoutchans);
123 nfrags = sys_schedadvance * (float)rate / (1e6 * frag_size);
124 /* save our belief as to ALSA's buffer size for later */
125 alsa_buf_samps = nfrags * frag_size;
127 if (sys_verbose)
128 post("audio buffer set to %d", (int)(0.001 * sys_schedadvance));
130 alsa_device.innoninterleave = alsa_device.outnoninterleave = 0;
131 if (wantinchans)
133 err = snd_pcm_open(&alsa_device.inhandle, devname,
134 SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
136 check_error(err, "snd_pcm_open (input)");
137 if (err < 0)
138 inchans = 0;
139 else
141 inchans = wantinchans;
142 snd_pcm_nonblock(alsa_device.inhandle, 1);
145 if (wantoutchans)
147 err = snd_pcm_open(&alsa_device.outhandle, devname,
148 SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
150 check_error(err, "snd_pcm_open (output)");
151 if (err < 0)
152 outchans = 0;
153 else
155 outchans = wantoutchans;
156 snd_pcm_nonblock(alsa_device.outhandle, 1);
159 if (inchans)
161 if (sys_verbose)
162 post("opening sound input...");
163 err = snd_pcm_hw_params_malloc(&hw_params);
164 check_error(err, "snd_pcm_hw_params_malloc (input)");
166 // get the default params
167 err = snd_pcm_hw_params_any(alsa_device.inhandle, hw_params);
168 check_error(err, "snd_pcm_hw_params_any (input)");
170 /* try to set interleaved access */
171 err = snd_pcm_hw_params_set_access(alsa_device.inhandle,
172 hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
173 if (err < 0)
175 /* OK, so try non-interleaved */
176 err = snd_pcm_hw_params_set_access(alsa_device.inhandle,
177 hw_params, SND_PCM_ACCESS_RW_NONINTERLEAVED);
178 if (err >= 0)
180 post("using non-interleaved audio input");
181 alsa_device.innoninterleave = 1;
184 check_error(err, "snd_pcm_hw_params_set_access (input)");
185 // Try to set 32 bit format first
186 err = snd_pcm_hw_params_set_format(alsa_device.inhandle, hw_params,
187 SND_PCM_FORMAT_S32);
188 if (err < 0)
190 /* fprintf(stderr,
191 "PD-ALSA: 32 bit format not available - using 16\n"); */
192 err = snd_pcm_hw_params_set_format(alsa_device.inhandle, hw_params,
193 SND_PCM_FORMAT_S16);
194 check_error(err, "snd_pcm_hw_params_set_format (input)");
195 alsa_samplewidth = 2;
197 else
199 alsa_samplewidth = 4;
201 post("Sample width set to %d bytes", alsa_samplewidth);
202 // set the subformat
203 err = snd_pcm_hw_params_set_subformat(alsa_device.inhandle, hw_params,
204 SND_PCM_SUBFORMAT_STD);
205 check_error(err, "snd_pcm_hw_params_set_subformat (input)");
206 // set the number of channels
207 tmp_uint = inchans;
208 err = snd_pcm_hw_params_set_channels_min(alsa_device.inhandle,
209 hw_params, &tmp_uint);
210 check_error(err, "snd_pcm_hw_params_set_channels (input)");
211 if (tmp_uint != (unsigned)inchans)
212 post("ALSA: set input channels to %d", tmp_uint);
213 inchans = tmp_uint;
214 // set the sampling rate
215 err = snd_pcm_hw_params_set_rate_min(alsa_device.inhandle, hw_params,
216 &rate, 0);
217 check_error(err, "snd_pcm_hw_params_set_rate_min (input)");
218 #if 0
219 err = snd_pcm_hw_params_get_rate(hw_params, &subunitdir);
220 post("input sample rate %d", err);
221 #endif
222 // set the period - ie frag size
223 // post("fragsize a %d", frag_size);
225 /* LATER try this to get a recommended period size...
226 right now, it trips an assertion failure in ALSA lib */
227 #if 0
228 post("input period was %d, min %d, max %d\n",
229 snd_pcm_hw_params_get_period_size(hw_params, 0),
230 snd_pcm_hw_params_get_period_size_min(hw_params, 0),
231 snd_pcm_hw_params_get_period_size_max(hw_params, 0));
232 #endif
233 #ifdef ALSAAPI9
234 err = snd_pcm_hw_params_set_period_size_near(alsa_device.inhandle,
235 hw_params,
236 (snd_pcm_uframes_t)
237 frag_size, 0);
238 #else
239 tmp_snd_pcm_uframes = frag_size;
240 err = snd_pcm_hw_params_set_period_size_near(alsa_device.inhandle,
241 hw_params, &tmp_snd_pcm_uframes, 0);
242 #endif
243 check_error(err, "snd_pcm_hw_params_set_period_size_near (input)");
244 // post("fragsize b %d", frag_size);
245 // set the number of periods - ie numfrags
246 // post("nfrags a %d", nfrags);
247 #ifdef ALSAAPI9
248 err = snd_pcm_hw_params_set_periods_near(alsa_device.inhandle,
249 hw_params, nfrags, 0);
250 #else
251 tmp_uint = nfrags;
252 err = snd_pcm_hw_params_set_periods_near(alsa_device.inhandle,
253 hw_params, &tmp_uint, 0);
254 #endif
255 check_error(err, "snd_pcm_hw_params_set_periods_near (input)");
256 // set the buffer size
257 #ifdef ALSAAPI9
258 err = snd_pcm_hw_params_set_buffer_size_near(alsa_device.inhandle,
259 hw_params, nfrags * frag_size);
260 #else
261 tmp_snd_pcm_uframes = nfrags * frag_size;
262 err = snd_pcm_hw_params_set_buffer_size_near(alsa_device.inhandle,
263 hw_params, &tmp_snd_pcm_uframes);
264 #endif
265 check_error(err, "snd_pcm_hw_params_set_buffer_size_near (input)");
267 err = snd_pcm_hw_params(alsa_device.inhandle, hw_params);
268 check_error(err, "snd_pcm_hw_params (input)");
270 snd_pcm_hw_params_free(hw_params);
272 err = snd_pcm_sw_params_malloc(&sw_params);
273 check_error(err, "snd_pcm_sw_params_malloc (input)");
274 err = snd_pcm_sw_params_current(alsa_device.inhandle, sw_params);
275 check_error(err, "snd_pcm_sw_params_current (input)");
276 err = snd_pcm_sw_params_set_start_threshold(alsa_device.inhandle,
277 sw_params, nfrags * frag_size);
278 check_error(err, "snd_pcm_sw_params_set_start_threshold (input)");
279 err = snd_pcm_sw_params_set_stop_threshold(alsa_device.inhandle,
280 sw_params, 0x7fffffff);
281 check_error(err, "snd_pcm_sw_params_set_stop_threshold (input)");
282 err = snd_pcm_sw_params_set_avail_min(alsa_device.inhandle, sw_params,
283 frag_size);
284 check_error(err, "snd_pcm_sw_params_set_avail_min (input)");
285 err = snd_pcm_sw_params(alsa_device.inhandle, sw_params);
286 check_error(err, "snd_pcm_sw_params (input)");
288 snd_pcm_sw_params_free(sw_params);
290 snd_output_stdio_attach(&out, stderr, 0);
291 #if 0
292 if (sys_verbose)
294 snd_pcm_dump_hw_setup(alsa_device.inhandle, out);
295 snd_pcm_dump_sw_setup(alsa_device.inhandle, out);
297 #endif
300 if (outchans)
302 int foo;
303 if (sys_verbose)
304 post("opening sound output...");
305 err = snd_pcm_hw_params_malloc(&hw_params);
306 check_error(err, "snd_pcm_sw_params (output)");
308 // get the default params
309 err = snd_pcm_hw_params_any(alsa_device.outhandle, hw_params);
310 check_error(err, "snd_pcm_hw_params_any (output)");
311 // set interleaved access - FIXME deal with other access types
312 err = snd_pcm_hw_params_set_access(alsa_device.outhandle, hw_params,
313 SND_PCM_ACCESS_RW_INTERLEAVED);
314 check_error(err, "snd_pcm_hw_params_set_access (output)");
316 /* try to set interleaved access */
317 err = snd_pcm_hw_params_set_access(alsa_device.outhandle,
318 hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
319 if (err < 0)
321 /* OK, so try non-interleaved */
322 err = snd_pcm_hw_params_set_access(alsa_device.outhandle,
323 hw_params, SND_PCM_ACCESS_RW_NONINTERLEAVED);
324 if (err >= 0)
326 post("using non-interleaved audio");
327 alsa_device.outnoninterleave = 1;
330 check_error(err, "snd_pcm_hw_params_set_access (output)");
333 // Try to set 32 bit format first
334 err = snd_pcm_hw_params_set_format(alsa_device.outhandle, hw_params,
335 SND_PCM_FORMAT_S32);
336 if (err < 0)
338 err = snd_pcm_hw_params_set_format(alsa_device.outhandle,
339 hw_params,SND_PCM_FORMAT_S16);
340 check_error(err, "snd_pcm_hw_params_set_format (output)");
341 /* fprintf(stderr,
342 "PD-ALSA: 32 bit format not available - using 16\n"); */
343 alsa_samplewidth = 2;
345 else
347 alsa_samplewidth = 4;
349 // set the subformat
350 err = snd_pcm_hw_params_set_subformat(alsa_device.outhandle, hw_params,
351 SND_PCM_SUBFORMAT_STD);
352 check_error(err, "snd_pcm_hw_params_set_subformat (output)");
353 // set the number of channels
354 tmp_uint = outchans;
355 err = snd_pcm_hw_params_set_channels_min(alsa_device.outhandle,
356 hw_params, &tmp_uint);
357 check_error(err, "snd_pcm_hw_params_set_channels (output)");
358 if (tmp_uint != (unsigned)outchans)
359 post("alsa: set output channels to %d", tmp_uint);
360 outchans = tmp_uint;
361 // set the sampling rate
362 err = snd_pcm_hw_params_set_rate_min(alsa_device.outhandle, hw_params,
363 &rate, 0);
364 check_error(err, "snd_pcm_hw_params_set_rate_min (output)");
365 #if 0
366 err = snd_pcm_hw_params_get_rate(hw_params, &subunitdir);
367 post("output sample rate %d", err);
368 #endif
369 // set the period - ie frag size
370 #if 0
371 post("output period was %d, min %d, max %d\n",
372 snd_pcm_hw_params_get_period_size(hw_params, 0),
373 snd_pcm_hw_params_get_period_size_min(hw_params, 0),
374 snd_pcm_hw_params_get_period_size_max(hw_params, 0));
375 #endif
376 // post("fragsize c %d", frag_size);
377 #ifdef ALSAAPI9
378 err = snd_pcm_hw_params_set_period_size_near(alsa_device.outhandle,
379 hw_params,
380 (snd_pcm_uframes_t)
381 frag_size, 0);
382 #else
383 tmp_snd_pcm_uframes = frag_size;
384 err = snd_pcm_hw_params_set_period_size_near(alsa_device.outhandle,
385 hw_params, &tmp_snd_pcm_uframes, 0);
386 #endif
387 // post("fragsize d %d", frag_size);
388 check_error(err, "snd_pcm_hw_params_set_period_size_near (output)");
389 // set the number of periods - ie numfrags
390 #ifdef ALSAAPI9
391 err = snd_pcm_hw_params_set_periods_near(alsa_device.outhandle,
392 hw_params, nfrags, 0);
393 #else
394 tmp_uint = nfrags;
395 err = snd_pcm_hw_params_set_periods_near(alsa_device.outhandle,
396 hw_params, &tmp_uint, 0);
397 #endif
398 check_error(err, "snd_pcm_hw_params_set_periods_near (output)");
399 // set the buffer size
400 #ifdef ALSAAPI9
401 err = snd_pcm_hw_params_set_buffer_size_near(alsa_device.outhandle,
402 hw_params, nfrags * frag_size);
403 #else
404 tmp_snd_pcm_uframes = nfrags * frag_size;
405 err = snd_pcm_hw_params_set_buffer_size_near(alsa_device.outhandle,
406 hw_params, &tmp_snd_pcm_uframes);
407 #endif
408 check_error(err, "snd_pcm_hw_params_set_buffer_size_near (output)");
410 err = snd_pcm_hw_params(alsa_device.outhandle, hw_params);
411 check_error(err, "snd_pcm_hw_params (output)");
413 snd_pcm_hw_params_free(hw_params);
415 err = snd_pcm_sw_params_malloc(&sw_params);
416 check_error(err, "snd_pcm_sw_params_malloc (output)");
417 err = snd_pcm_sw_params_current(alsa_device.outhandle, sw_params);
418 check_error(err, "snd_pcm_sw_params_current (output)");
419 err = snd_pcm_sw_params_set_start_threshold(alsa_device.outhandle,
420 sw_params, nfrags * frag_size);
421 check_error(err, "snd_pcm_sw_params_set_start_threshold (output)");
422 err = snd_pcm_sw_params_set_stop_threshold(alsa_device.outhandle,
423 sw_params, 0x7fffffff);
424 check_error(err, "snd_pcm_sw_params_set_stop_threshold (output)");
425 err = snd_pcm_sw_params_set_avail_min(alsa_device.outhandle, sw_params,
426 frag_size);
427 check_error(err, "snd_pcm_sw_params_set_avail_min (output)");
428 err = snd_pcm_sw_params(alsa_device.outhandle, sw_params);
429 check_error(err, "snd_pcm_sw_params (output)");
430 snd_pcm_sw_params_free(sw_params);
432 snd_output_stdio_attach(&out, stderr, 0);
433 #if 0
434 if (sys_verbose)
436 snd_pcm_dump_hw_setup(alsa_device.outhandle, out);
437 snd_pcm_dump_sw_setup(alsa_device.outhandle, out);
439 #endif
442 if (inchans)
443 snd_pcm_prepare(alsa_device.inhandle);
444 if (outchans)
445 snd_pcm_prepare(alsa_device.outhandle);
447 // if duplex we can link the channels so they start together
448 if (inchans && outchans)
449 snd_pcm_link(alsa_device.inhandle, alsa_device.outhandle);
451 // set up the status variables
452 err = snd_pcm_status_malloc(&in_status);
453 check_error(err, "snd_pcm_status_malloc");
454 err = snd_pcm_status_malloc(&out_status);
455 check_error(err, "snd_pcm_status_malloc");
457 // set up the buffer
458 if (alsa_snd_buf)
459 free(alsa_snd_buf);
460 alsa_snd_buf = (void *)malloc(
461 sizeof(char) * alsa_samplewidth * DEFDACBLKSIZE *
462 (outchans > inchans ? outchans : inchans));
463 memset(alsa_snd_buf, 0, sizeof(char) * alsa_samplewidth * DEFDACBLKSIZE *
464 (outchans > inchans ? outchans : inchans));
465 /* make an array of pointers too in case we need them */
466 if (alsa_buf_ptrs)
467 free(alsa_buf_ptrs);
468 alsa_buf_ptrs = (void **)malloc(
469 sizeof(void *) * (outchans > inchans ? outchans : inchans));
470 for (i = 0; i < (outchans > inchans ? outchans : inchans); i++)
471 alsa_buf_ptrs[i] = (t_alsa_sample32 *)alsa_snd_buf + i * DEFDACBLKSIZE;
473 // fill the buffer with silence
474 if (outchans)
476 i = (frag_size * nfrags)/DEFDACBLKSIZE + 1;
477 while (i--)
479 if (alsa_device.outnoninterleave)
480 snd_pcm_writen(alsa_device.outhandle, alsa_buf_ptrs,
481 DEFDACBLKSIZE);
482 else snd_pcm_writei(alsa_device.outhandle, alsa_snd_buf,
483 DEFDACBLKSIZE);
485 /* confused about this: */
486 /* if ((err = snd_pcm_start(alsa_device.outhandle) < 0))
487 check_error(err, "output start failed\n"); */
489 else if (inchans)
491 if (snd_pcm_start(alsa_device.inhandle) < 0)
492 check_error(err, "input start failed\n");
494 alsa_outchannels = outchans;
495 alsa_inchannels = inchans;
497 return (!(inchans || outchans));
500 void alsa_close_audio(void)
502 int err;
503 if (alsa_inchannels)
505 err = snd_pcm_close(alsa_device.inhandle);
506 check_error(err, "snd_pcm_close (input)");
508 if (alsa_outchannels)
510 err = snd_pcm_close(alsa_device.outhandle);
511 check_error(err, "snd_pcm_close (output)");
515 // #define DEBUG_ALSA_XFER
517 int alsa_send_dacs(void)
519 static int16_t *sp;
520 static int xferno = 0;
521 static int callno = 0;
522 static double timenow;
523 double timelast;
524 t_sample *fp, *fp1, *fp2;
525 int i, j, k, err, devno = 0;
526 int inputcount = 0, outputcount = 0, inputlate = 0, outputlate = 0;
527 int result;
528 int inchannels = (sys_inchannels > alsa_inchannels ?
529 alsa_inchannels : sys_inchannels);
530 int outchannels = (sys_outchannels > alsa_outchannels ?
531 alsa_outchannels : sys_outchannels);
532 unsigned int intransfersize = DEFDACBLKSIZE;
533 unsigned int outtransfersize = DEFDACBLKSIZE;
535 // get the status
536 if (!inchannels && !outchannels)
538 return SENDDACS_NO;
541 timelast = timenow;
542 timenow = sys_getrealtime();
544 #ifdef DEBUG_ALSA_XFER
545 if (timenow - timelast > 0.050)
546 fprintf(stderr, "(%d)",
547 (int)(1000 * (timenow - timelast))), fflush(stderr);
548 #endif
550 callno++;
552 alsa_checkiosync(); /* check I/O are in sync and data not late */
554 if (alsa_inchannels)
556 snd_pcm_status(alsa_device.inhandle, in_status);
557 if (snd_pcm_status_get_avail(in_status) < intransfersize)
558 return SENDDACS_NO;
560 if (alsa_outchannels)
562 snd_pcm_status(alsa_device.outhandle, out_status);
563 if (snd_pcm_status_get_avail(out_status) < outtransfersize)
564 return SENDDACS_NO;
567 /* do output */
568 if (alsa_outchannels)
570 fp = sys_soundout;
571 if (alsa_samplewidth == 4)
573 if (alsa_device.outnoninterleave)
575 int n = outchannels * DEFDACBLKSIZE;
576 for (i = 0, fp1 = fp; i < n; i++)
578 float s1 = *fp1 * INT32_MAX;
579 ((t_alsa_sample32 *)alsa_snd_buf)[i] = CLIP32(s1);
581 n = alsa_outchannels * DEFDACBLKSIZE;
582 for (; i < n; i++)
583 ((t_alsa_sample32 *)alsa_snd_buf)[i] = 0;
585 else
587 for (i = 0, fp1 = fp; i < outchannels; i++,
588 fp1 += DEFDACBLKSIZE)
590 for (j = i, k = DEFDACBLKSIZE, fp2 = fp1; k--;
591 j += alsa_outchannels, fp2++)
593 float s1 = *fp2 * INT32_MAX;
594 ((t_alsa_sample32 *)alsa_snd_buf)[j] = CLIP32(s1);
599 else
601 for (i = 0, fp1 = fp; i < outchannels; i++, fp1 += DEFDACBLKSIZE)
603 for (j = i, k = DEFDACBLKSIZE, fp2 = fp1; k--;
604 j += alsa_outchannels, fp2++)
606 int s = *fp2 * 32767.;
607 if (s > 32767)
608 s = 32767;
609 else if (s < -32767)
610 s = -32767;
611 ((t_alsa_sample16 *)alsa_snd_buf)[j] = s;
616 if (alsa_device.outnoninterleave)
617 result = snd_pcm_writen(alsa_device.outhandle, alsa_buf_ptrs,
618 outtransfersize);
619 else result = snd_pcm_writei(alsa_device.outhandle, alsa_snd_buf,
620 outtransfersize);
622 if (result != (int)outtransfersize)
624 #ifdef DEBUG_ALSA_XFER
625 if (result >= 0 || errno == EAGAIN)
626 fprintf(stderr, "ALSA: write returned %d of %d\n",
627 result, outtransfersize);
628 else fprintf(stderr, "ALSA: write: %s\n",
629 snd_strerror(errno));
630 fprintf(stderr,
631 "inputcount %d, outputcount %d, outbufsize %d\n",
632 inputcount, outputcount,
633 (ALSA_EXTRABUFFER + sys_advance_samples)
634 * alsa_samplewidth * outchannels);
635 #endif
636 sys_log_error(ERR_DACSLEPT);
637 return (SENDDACS_NO);
640 /* zero out the output buffer */
641 memset(sys_soundout, 0, DEFDACBLKSIZE * sizeof(*sys_soundout) *
642 sys_outchannels);
643 if (sys_getrealtime() - timenow > 0.002)
645 #ifdef DEBUG_ALSA_XFER
646 fprintf(stderr, "output %d took %d msec\n",
647 callno, (int)(1000 * (timenow - timelast))), fflush(stderr);
648 #endif
649 timenow = sys_getrealtime();
650 sys_log_error(ERR_DACSLEPT);
653 /* do input */
654 if (alsa_inchannels)
656 if (alsa_device.innoninterleave)
657 result = snd_pcm_readn(alsa_device.inhandle, alsa_buf_ptrs,
658 intransfersize);
659 else result = snd_pcm_readi(alsa_device.inhandle, alsa_snd_buf,
660 intransfersize);
661 if (result < (int)intransfersize)
663 #ifdef DEBUG_ALSA_XFER
664 if (result < 0)
665 fprintf(stderr,
666 "snd_pcm_read %d %d: %s\n",
667 callno, xferno, snd_strerror(errno));
668 else fprintf(stderr,
669 "snd_pcm_read %d %d returned only %d\n",
670 callno, xferno, result);
671 fprintf(stderr,
672 "inputcount %d, outputcount %d, inbufsize %d\n",
673 inputcount, outputcount,
674 (ALSA_EXTRABUFFER + sys_advance_samples)
675 * alsa_samplewidth * inchannels);
676 #endif
677 sys_log_error(ERR_ADCSLEPT);
678 return (SENDDACS_NO);
680 fp = sys_soundin;
681 if (alsa_samplewidth == 4)
683 if (alsa_device.innoninterleave)
685 int n = inchannels * DEFDACBLKSIZE;
686 for (i = 0, fp1 = fp; i < n; i++)
687 *fp1 = (float) ((t_alsa_sample32 *)alsa_snd_buf)[i]
688 * (1./ INT32_MAX);
690 else
692 for (i = 0, fp1 = fp; i < inchannels;
693 i++, fp1 += DEFDACBLKSIZE)
695 for (j = i, k = DEFDACBLKSIZE, fp2 = fp1; k--;
696 j += alsa_inchannels, fp2++)
697 *fp2 = (float) ((t_alsa_sample32 *)alsa_snd_buf)[j]
698 * (1./ INT32_MAX);
702 else
704 for (i = 0, fp1 = fp; i < inchannels; i++, fp1 += DEFDACBLKSIZE)
706 for (j = i, k = DEFDACBLKSIZE, fp2 = fp1; k--;
707 j += alsa_inchannels, fp2++)
708 *fp2 = (float) ((t_alsa_sample16 *)alsa_snd_buf)[j]
709 * 3.051850e-05;
713 xferno++;
714 if (sys_getrealtime() - timenow > 0.002)
716 #ifdef DEBUG_ALSA_XFER
717 fprintf(stderr, "routine took %d msec\n",
718 (int)(1000 * (sys_getrealtime() - timenow)));
719 #endif
720 sys_log_error(ERR_ADCSLEPT);
722 return SENDDACS_YES;
725 void alsa_printstate( void)
727 int i, result;
728 snd_pcm_sframes_t indelay, outdelay;
729 if (sys_audioapi != API_ALSA)
731 error("restart-audio: implemented for ALSA only.");
732 return;
734 if (sys_inchannels)
736 result = snd_pcm_delay(alsa_device.inhandle, &indelay);
737 if (result < 0)
738 post("snd_pcm_delay 1 failed");
739 else post("in delay %d", indelay);
741 if (sys_outchannels)
743 result = snd_pcm_delay(alsa_device.outhandle, &outdelay);
744 if (result < 0)
745 post("snd_pcm_delay 2 failed");
746 else post("out delay %d", outdelay);
748 post("sum %d (%d mod 64)\n", indelay + outdelay, (indelay+outdelay)%64);
750 post("buf samples %d", alsa_buf_samps);
754 void alsa_resync( void)
756 int i, result;
757 if (sys_audioapi != API_ALSA)
759 error("restart-audio: implemented for ALSA only.");
760 return;
762 memset(alsa_snd_buf, 0,
763 sizeof(char) * alsa_samplewidth * DEFDACBLKSIZE * sys_outchannels);
764 for (i = 0; i < 1000000; i++)
766 if (alsa_device.outnoninterleave)
767 result = snd_pcm_writen(alsa_device.outhandle, alsa_buf_ptrs,
768 DEFDACBLKSIZE);
769 else result = snd_pcm_writei(alsa_device.outhandle, alsa_snd_buf,
770 DEFDACBLKSIZE);
771 if (result != (int)DEFDACBLKSIZE)
772 break;
774 post("%d written", i);
777 void alsa_putzeros(int n)
779 int i, result;
780 memset(alsa_snd_buf, 0,
781 sizeof(char) * alsa_samplewidth * DEFDACBLKSIZE * alsa_outchannels);
782 for (i = 0; i < n; i++)
784 if (alsa_device.outnoninterleave)
785 result = snd_pcm_writen(alsa_device.outhandle, alsa_buf_ptrs,
786 DEFDACBLKSIZE);
787 else result = snd_pcm_writei(alsa_device.outhandle, alsa_snd_buf,
788 DEFDACBLKSIZE);
789 #if 0
790 if (result != DEFDACBLKSIZE)
791 post("result %d", result);
792 #endif
794 /* post ("putzeros %d", n); */
797 void alsa_getzeros(int n)
799 int i, result;
800 for (i = 0; i < n; i++)
802 result = snd_pcm_readi(alsa_device.inhandle, alsa_snd_buf,
803 DEFDACBLKSIZE);
804 #if 0
805 if (result != DEFDACBLKSIZE)
806 post("result %d", result);
807 #endif
809 /* post ("getzeros %d", n); */
812 /* call this only if both input and output are open */
813 static void alsa_checkiosync( void)
815 int i, result, checkit = 1, giveup = 1000, alreadylogged = 0;
816 snd_pcm_sframes_t indelay, outdelay, defect;
818 if (!(alsa_outchannels && alsa_inchannels))
819 return;
820 while (checkit)
822 checkit = 0;
823 if (giveup-- <= 0)
824 return;
825 result = snd_pcm_delay(alsa_device.outhandle, &outdelay);
826 if (result < 0)
828 post("output snd_pcm_delay failed: %s", snd_strerror(result));
829 if (snd_pcm_status(alsa_device.outhandle, out_status) < 0)
830 post("output snd_pcm_status failed");
831 else post("astate %d",
832 snd_pcm_status_get_state(out_status));
833 return;
835 if (outdelay < 0)
836 sys_log_error(ERR_DATALATE), alreadylogged = 1;
838 if (sys_inchannels)
840 result = snd_pcm_delay(alsa_device.inhandle, &indelay);
841 if (result < 0)
843 post("input snd_pcm_delay failed");
844 return;
846 defect = indelay + outdelay - alsa_buf_samps;
847 if (defect < -(3 * DEFDACBLKSIZE / 2) )
849 checkit = 1;
850 alsa_putzeros(1);
851 if (!alreadylogged)
852 sys_log_error(ERR_RESYNC), alreadylogged = 1;
854 else if (defect > 0)
856 checkit = 1;
857 alsa_getzeros(1);
858 if (!alreadylogged)
859 sys_log_error(ERR_RESYNC), alreadylogged = 1;
861 /* if (alreadylogged)
862 post("in %d out %d defect %d", indelay, outdelay, defect); */
867 static int alsa_nnames = 0;
868 static char **alsa_names = 0;
870 void alsa_adddev(char *name)
872 if (alsa_nnames)
873 alsa_names = (char **)t_resizebytes(alsa_names,
874 alsa_nnames * sizeof(char *),
875 (alsa_nnames+1) * sizeof(char *));
876 else alsa_names = (char **)t_getbytes(sizeof(char *));
877 alsa_names[alsa_nnames] = gensym(name)->s_name;
878 alsa_nnames++;
881 static void alsa_numbertoname(int devno, char *devname, int nchar)
883 int ndev = 0, cardno = -1;
884 while (!snd_card_next(&cardno) && cardno >= 0)
885 ndev++;
886 if (devno < 2*ndev)
888 if (devno & 1)
889 snprintf(devname, nchar, "plughw:%d", devno/2);
890 else snprintf(devname, nchar, "hw:%d", devno/2);
892 else if (devno <2*ndev + alsa_nnames)
893 snprintf(devname, nchar, "%s", alsa_names[devno - 2*ndev]);
894 else snprintf(devname, nchar, "???");
897 /* For each hardware card found, we list two devices, the "hard" and
898 "plug" one. The card scan is derived from portaudio code. */
899 void alsa_getdevs(char *indevlist, int *nindevs,
900 char *outdevlist, int *noutdevs, int *canmulti,
901 int maxndev, int devdescsize)
903 int ndev = 0, cardno = -1, i, j;
904 *canmulti = 0; /* only one device; must be the same for input&output */
905 while (!snd_card_next(&cardno) && cardno >= 0)
907 snd_ctl_t *ctl;
908 snd_ctl_card_info_t *info;
909 char devname[80];
910 const char *desc;
911 if (2 * ndev + 2 > maxndev)
912 break;
913 /* apparently, "cardno" is just a counter; but check that here */
914 if (ndev != cardno)
915 fprintf(stderr, "oops: ALSA cards not reported in order?\n");
916 sprintf(devname, "hw:%d", cardno );
917 /* fprintf(stderr, "\ntry %s...\n", devname); */
918 if (snd_ctl_open(&ctl, devname, 0) >= 0)
920 snd_ctl_card_info_malloc(&info);
921 snd_ctl_card_info(ctl, info);
922 desc = snd_ctl_card_info_get_name(info);
923 snd_ctl_card_info_free(info);
925 else
927 fprintf(stderr, "ALSA card scan error\n");
928 desc = "???";
930 /* fprintf(stderr, "name: %s\n", snd_ctl_card_info_get_name(info)); */
931 sprintf(indevlist + 2*ndev * devdescsize, "%s (hardware)", desc);
932 sprintf(indevlist + (2*ndev + 1) * devdescsize, "%s (plug-in)", desc);
933 sprintf(outdevlist + 2*ndev * devdescsize, "%s (hardware)", desc);
934 sprintf(outdevlist + (2*ndev + 1) * devdescsize, "%s (plug-in)", desc);
935 ndev++;
937 for (i = 0, j = 2*ndev; i < alsa_nnames; i++, j++)
939 if (j >= maxndev)
940 break;
941 snprintf(indevlist + j * devdescsize, devdescsize, "%s",
942 alsa_names[i]);
944 *nindevs = *noutdevs = j;