1 // ----------------------------------------------------------------------------
3 // Copyright (C) 2006-2012 Fons Adriaensen <fons@linuxaudio.org>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 // ----------------------------------------------------------------------------
20 #if defined(__NetBSD__)
21 #include <sys/endian.h>
26 #include "zita-alsa-pcmi.h"
29 // Public members ----------------------------------------------------------------------
32 int zita_alsa_pcmi_major_version (void)
34 return ZITA_ALSA_PCMI_MAJOR_VERSION;
38 int zita_alsa_pcmi_minor_version (void)
40 return ZITA_ALSA_PCMI_MINOR_VERSION;
44 Alsa_pcmi::Alsa_pcmi (
45 const char *play_name,
46 const char *capt_name,
47 const char *ctrl_name,
50 unsigned int play_nfrag,
51 unsigned int capt_nfrag,
55 , _play_nfrag (play_nfrag)
56 , _capt_nfrag (capt_nfrag)
76 p = getenv ("ZITA_ALSA_PCMI_DEBUG");
77 if (p && *p) _debug = atoi (p);
78 initialise (play_name, capt_name, ctrl_name);
82 Alsa_pcmi::~Alsa_pcmi (void)
84 if (_play_handle) snd_pcm_close (_play_handle);
85 if (_capt_handle) snd_pcm_close (_capt_handle);
86 if (_ctrl_handle) snd_ctl_close (_ctrl_handle);
88 snd_pcm_sw_params_free (_capt_swpar);
89 snd_pcm_hw_params_free (_capt_hwpar);
90 snd_pcm_sw_params_free (_play_swpar);
91 snd_pcm_hw_params_free (_play_hwpar);
95 int Alsa_pcmi::pcm_start (void)
102 n = snd_pcm_avail_update (_play_handle);
103 if (n != _fsize * _play_nfrag)
105 if (_debug & DEBUG_STAT) fprintf (stderr, "Alsa_pcmi: full buffer not available at start.\n");
108 for (i = 0; i < _play_nfrag; i++)
111 for (j = 0; j < _play_nchan; j++) clear_chan (j, _fsize);
114 if ((err = snd_pcm_start (_play_handle)) < 0)
116 if (_debug & DEBUG_STAT) fprintf (stderr, "Alsa_pcmi: pcm_start(play): %s.\n", snd_strerror (err));
120 if (_capt_handle && !_synced && ((err = snd_pcm_start (_capt_handle)) < 0))
122 if (_debug & DEBUG_STAT) fprintf (stderr, "Alsa_pcmi: pcm_start(capt): %s.\n", snd_strerror (err));
130 int Alsa_pcmi::pcm_stop (void)
134 if (_play_handle && ((err = snd_pcm_drop (_play_handle)) < 0))
136 if (_debug & DEBUG_STAT) fprintf (stderr, "Alsa_pcmi: pcm_drop(play): %s.\n", snd_strerror (err));
139 if (_capt_handle && !_synced && ((err = snd_pcm_drop (_capt_handle)) < 0))
141 if (_debug & DEBUG_STAT) fprintf (stderr, "Alsa_pcmi: pcm_drop(capt): %s.\n", snd_strerror (err));
149 snd_pcm_sframes_t Alsa_pcmi::pcm_wait (void)
153 snd_pcm_sframes_t capt_av;
154 snd_pcm_sframes_t play_av;
159 need_capt = _capt_handle ? true : false;
160 need_play = _play_handle ? true : false;
162 while (need_play || need_capt)
167 snd_pcm_poll_descriptors (_play_handle, _poll_fd, _play_npfd);
173 snd_pcm_poll_descriptors (_capt_handle, _poll_fd + n1, _capt_npfd);
176 for (i = 0; i < n2; i++) _poll_fd [i].events |= POLLERR;
181 r = ppoll (_poll_fd, n2, &timeout, NULL);
185 if (errno == EINTR) return 0;
186 if (_debug & DEBUG_WAIT) fprintf (stderr, "Alsa_pcmi: poll(): %s\n.", strerror (errno));
192 if (_debug & DEBUG_WAIT) fprintf (stderr, "Alsa_pcmi: poll timed out.\n");
199 snd_pcm_poll_descriptors_revents (_play_handle, _poll_fd, n1, &rev);
202 if (_debug & DEBUG_WAIT) fprintf (stderr, "Alsa_pcmi: error on playback pollfd.\n");
207 if (rev & POLLOUT) need_play = false;
211 snd_pcm_poll_descriptors_revents (_capt_handle, _poll_fd + n1, n2 - n1, &rev);
214 if (_debug & DEBUG_WAIT) fprintf (stderr, "Alsa_pcmi: error on capture pollfd.\n");
219 if (rev & POLLIN) need_capt = false;
224 if (_play_handle && (play_av = snd_pcm_avail_update (_play_handle)) < 0)
231 if (_capt_handle && (capt_av = snd_pcm_avail_update (_capt_handle)) < 0)
238 return (capt_av < play_av) ? capt_av : play_av;
242 int Alsa_pcmi::pcm_idle (int len)
245 snd_pcm_uframes_t n, k;
263 for (i = 0; i < _play_nchan; i++) clear_chan (i, k);
272 int Alsa_pcmi::play_init (snd_pcm_uframes_t len)
275 const snd_pcm_channel_area_t *a;
281 if ((err = snd_pcm_mmap_begin (_play_handle, &a, &_play_offs, &len)) < 0)
283 if (_debug & DEBUG_DATA) fprintf (stderr, "Alsa_pcmi: snd_pcm_mmap_begin(play): %s.\n", snd_strerror (err));
286 _play_step = (a->step) >> 3;
287 for (i = 0; i < _play_nchan; i++, a++)
289 _play_ptr [i] = (char *) a->addr + ((a->first + a->step * _play_offs) >> 3);
296 int Alsa_pcmi::capt_init (snd_pcm_uframes_t len)
299 const snd_pcm_channel_area_t *a;
306 if ((err = snd_pcm_mmap_begin (_capt_handle, &a, &_capt_offs, &len)) < 0)
308 if (_debug & DEBUG_DATA) fprintf (stderr, "Alsa_pcmi: snd_pcm_mmap_begin(capt): %s.\n", snd_strerror (err));
311 _capt_step = (a->step) >> 3;
312 for (i = 0; i < _capt_nchan; i++, a++)
314 _capt_ptr [i] = (char *) a->addr + ((a->first + a->step * _capt_offs) >> 3);
321 void Alsa_pcmi::clear_chan (int chan, int len)
323 _play_ptr [chan] = (this->*Alsa_pcmi::_clear_func)(_play_ptr [chan], len);
327 void Alsa_pcmi::play_chan (int chan, const float *src, int len, int step)
329 _play_ptr [chan] = (this->*Alsa_pcmi::_play_func)(src, _play_ptr [chan], len, step);
333 void Alsa_pcmi::capt_chan (int chan, float *dst, int len, int step)
335 _capt_ptr [chan] = (this->*Alsa_pcmi::_capt_func)(_capt_ptr [chan], dst, len, step);
339 int Alsa_pcmi::play_done (int len)
341 if (!_play_handle) return 0;
342 return snd_pcm_mmap_commit (_play_handle, _play_offs, len);
346 int Alsa_pcmi::capt_done (int len)
348 if (!_capt_handle) return 0;
349 return snd_pcm_mmap_commit (_capt_handle, _capt_offs, len);
353 void Alsa_pcmi::printinfo (void)
355 fprintf (stdout, "playback :");
358 fprintf (stdout, "\n nchan : %d\n", _play_nchan);
359 fprintf (stdout, " fsamp : %d\n", _fsamp);
360 fprintf (stdout, " fsize : %ld\n", _fsize);
361 fprintf (stdout, " nfrag : %d\n", _play_nfrag);
362 fprintf (stdout, " format : %s\n", snd_pcm_format_name (_play_format));
364 else fprintf (stdout, " not enabled\n");
365 fprintf (stdout, "capture :");
368 fprintf (stdout, "\n nchan : %d\n", _capt_nchan);
369 fprintf (stdout, " fsamp : %d\n", _fsamp);
370 fprintf (stdout, " fsize : %ld\n", _fsize);
371 fprintf (stdout, " nfrag : %d\n", _capt_nfrag);
372 fprintf (stdout, " format : %s\n", snd_pcm_format_name (_capt_format));
373 if (_play_handle) fprintf (stdout, "%s\n", _synced ? "synced" : "not synced");
375 else fprintf (stdout, " not enabled\n");
379 // Private members ---------------------------------------------------------------------
382 void Alsa_pcmi::initialise (const char *play_name, const char *capt_name, const char *ctrl_name)
385 snd_pcm_uframes_t fsize;
389 snd_ctl_card_info_t *card;
393 if (snd_pcm_open (&_play_handle, play_name, SND_PCM_STREAM_PLAYBACK, 0) < 0)
396 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: Cannot open PCM device %s for playback.\n",
403 if (snd_pcm_open (&_capt_handle, capt_name, SND_PCM_STREAM_CAPTURE, 0) < 0)
406 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: Cannot open PCM device %s for capture.\n",
411 if (! _play_handle && ! _capt_handle) return;
415 snd_ctl_card_info_alloca (&card);
417 if ((err = snd_ctl_open (&_ctrl_handle, ctrl_name, 0)) < 0)
419 if (_debug & DEBUG_INIT) fprintf (stderr, "Alse_driver: ctl_open(): %s\n",
423 if ((err = snd_ctl_card_info (_ctrl_handle, card)) < 0)
425 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: ctl_card_info(): %s\n",
434 if (snd_pcm_hw_params_malloc (&_play_hwpar) < 0)
436 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't allocate playback hw params\n");
439 if (snd_pcm_sw_params_malloc (&_play_swpar) < 0)
441 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't allocate playback sw params\n");
444 if (set_hwpar (_play_handle, _play_hwpar, "playback", _play_nfrag, &_play_nchan) < 0) return;
445 if (set_swpar (_play_handle, _play_swpar, "playback") < 0) return;
450 if (snd_pcm_hw_params_malloc (&_capt_hwpar) < 0)
452 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't allocate capture hw params\n");
455 if (snd_pcm_sw_params_malloc (&_capt_swpar) < 0)
457 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't allocate capture sw params\n");
460 if (set_hwpar (_capt_handle, _capt_hwpar, "capture", _capt_nfrag, &_capt_nchan) < 0) return;
461 if (set_swpar (_capt_handle, _capt_swpar, "capture") < 0) return;
466 if (snd_pcm_hw_params_get_rate (_play_hwpar, &fsamp, &dir) || (fsamp != _fsamp) || dir)
468 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't get requested sample rate for playback.\n");
472 if (snd_pcm_hw_params_get_period_size (_play_hwpar, &fsize, &dir) || (fsize != _fsize) || dir)
474 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't get requested period size for playback.\n");
478 if (snd_pcm_hw_params_get_periods (_play_hwpar, &nfrag, &dir) || (nfrag != _play_nfrag) || dir)
480 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't get requested number of periods for playback.\n");
485 snd_pcm_hw_params_get_format (_play_hwpar, &_play_format);
486 snd_pcm_hw_params_get_access (_play_hwpar, &_play_access);
488 #if __BYTE_ORDER == __LITTLE_ENDIAN
489 switch (_play_format)
491 case SND_PCM_FORMAT_FLOAT_LE:
492 _clear_func = &Alsa_pcmi::clear_32;
493 _play_func = &Alsa_pcmi::play_float;
496 case SND_PCM_FORMAT_S32_LE:
497 _clear_func = &Alsa_pcmi::clear_32;
498 _play_func = &Alsa_pcmi::play_32;
501 case SND_PCM_FORMAT_S32_BE:
502 _clear_func = &Alsa_pcmi::clear_32;
503 _play_func = &Alsa_pcmi::play_32swap;
506 case SND_PCM_FORMAT_S24_3LE:
507 _clear_func = &Alsa_pcmi::clear_24;
508 _play_func = &Alsa_pcmi::play_24;
511 case SND_PCM_FORMAT_S24_3BE:
512 _clear_func = &Alsa_pcmi::clear_24;
513 _play_func = &Alsa_pcmi::play_24swap;
516 case SND_PCM_FORMAT_S16_LE:
517 _clear_func = &Alsa_pcmi::clear_16;
518 _play_func = &Alsa_pcmi::play_16;
521 case SND_PCM_FORMAT_S16_BE:
522 _clear_func = &Alsa_pcmi::clear_16;
523 _play_func = &Alsa_pcmi::play_16swap;
527 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't handle playback sample format.\n");
531 #elif __BYTE_ORDER == __BIG_ENDIAN
532 switch (_play_format)
534 case SND_PCM_FORMAT_S32_LE:
535 _clear_func = &Alsa_pcmi::clear_32;
536 _play_func = &Alsa_pcmi::play_32swap;
539 case SND_PCM_FORMAT_S32_BE:
540 _clear_func = &Alsa_pcmi::clear_32;
541 _play_func = &Alsa_pcmi::play_32;
544 case SND_PCM_FORMAT_S24_3LE:
545 _clear_func = &Alsa_pcmi::clear_24;
546 _play_func = &Alsa_pcmi::play_24swap;
549 case SND_PCM_FORMAT_S24_3BE:
550 _clear_func = &Alsa_pcmi::clear_24;
551 _play_func = &Alsa_pcmi::play_24;
554 case SND_PCM_FORMAT_S16_LE:
555 _clear_func = &Alsa_pcmi::clear_16;
556 _play_func = &Alsa_pcmi::play_16swap;
559 case SND_PCM_FORMAT_S16_BE:
560 _clear_func = &Alsa_pcmi::clear_16;
561 _play_func = &Alsa_pcmi::play_16;
565 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't handle playback sample format.\n");
570 #error "System byte order is undefined or not supported"
573 _play_npfd = snd_pcm_poll_descriptors_count (_play_handle);
578 if (snd_pcm_hw_params_get_rate (_capt_hwpar, &fsamp, &dir) || (fsamp != _fsamp) || dir)
580 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't get requested sample rate for capture.\n");
584 if (snd_pcm_hw_params_get_period_size (_capt_hwpar, &fsize, &dir) || (fsize != _fsize) || dir)
586 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't get requested period size for capture.\n");
590 if (snd_pcm_hw_params_get_periods (_capt_hwpar, &nfrag, &dir) || (nfrag != _capt_nfrag) || dir)
592 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't get requested number of periods for capture.\n");
597 if (_play_handle) _synced = ! snd_pcm_link (_play_handle, _capt_handle);
599 snd_pcm_hw_params_get_format (_capt_hwpar, &_capt_format);
600 snd_pcm_hw_params_get_access (_capt_hwpar, &_capt_access);
602 #if __BYTE_ORDER == __LITTLE_ENDIAN
603 switch (_capt_format)
605 case SND_PCM_FORMAT_FLOAT_LE:
606 _capt_func = &Alsa_pcmi::capt_float;
609 case SND_PCM_FORMAT_S32_LE:
610 _capt_func = &Alsa_pcmi::capt_32;
613 case SND_PCM_FORMAT_S32_BE:
614 _capt_func = &Alsa_pcmi::capt_32swap;
617 case SND_PCM_FORMAT_S24_3LE:
618 _capt_func = &Alsa_pcmi::capt_24;
621 case SND_PCM_FORMAT_S24_3BE:
622 _capt_func = &Alsa_pcmi::capt_24swap;
625 case SND_PCM_FORMAT_S16_LE:
626 _capt_func = &Alsa_pcmi::capt_16;
629 case SND_PCM_FORMAT_S16_BE:
630 _capt_func = &Alsa_pcmi::capt_16swap;
634 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't handle capture sample format.\n");
638 #elif __BYTE_ORDER == __BIG_ENDIAN
639 switch (_capt_format)
641 case SND_PCM_FORMAT_S32_LE:
642 _capt_func = &Alsa_pcmi::capt_32swap;
645 case SND_PCM_FORMAT_S32_BE:
646 _capt_func = &Alsa_pcmi::capt_32;
649 case SND_PCM_FORMAT_S24_3LE:
650 _capt_func = &Alsa_pcmi::capt_24swap;
653 case SND_PCM_FORMAT_S24_3BE:
654 _capt_func = &Alsa_pcmi::capt_24;
657 case SND_PCM_FORMAT_S16_LE:
658 _capt_func = &Alsa_pcmi::capt_16swap;
661 case SND_PCM_FORMAT_S16_BE:
662 _capt_func = &Alsa_pcmi::capt_16;
666 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't handle capture sample format.\n");
671 #error "System byte order is undefined or not supported"
674 _capt_npfd = snd_pcm_poll_descriptors_count (_capt_handle);
677 if (_play_npfd + _capt_npfd > MAXPFD)
679 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: interface requires more than %d pollfd\n", MAXPFD);
687 int Alsa_pcmi::set_hwpar (snd_pcm_t *handle, snd_pcm_hw_params_t *hwpar, const char *sname, unsigned int nfrag, unsigned int *nchan)
691 if (snd_pcm_hw_params_any (handle, hwpar) < 0)
693 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: no %s hw configurations available.\n",
697 if (snd_pcm_hw_params_set_periods_integer (handle, hwpar) < 0)
699 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't set %s period size to integral value.\n",
703 if ( (snd_pcm_hw_params_set_access (handle, hwpar, SND_PCM_ACCESS_MMAP_NONINTERLEAVED) < 0)
704 && (snd_pcm_hw_params_set_access (handle, hwpar, SND_PCM_ACCESS_MMAP_INTERLEAVED) < 0)
705 && (snd_pcm_hw_params_set_access (handle, hwpar, SND_PCM_ACCESS_MMAP_COMPLEX) < 0))
707 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: the %s interface doesn't support mmap-based access.\n",
711 if (_debug & FORCE_16B)
713 err = (snd_pcm_hw_params_set_format (handle, hwpar, SND_PCM_FORMAT_S16_LE) < 0)
714 && (snd_pcm_hw_params_set_format (handle, hwpar, SND_PCM_FORMAT_S16_BE) < 0);
718 err = (snd_pcm_hw_params_set_format (handle, hwpar, SND_PCM_FORMAT_FLOAT_LE) < 0)
719 && (snd_pcm_hw_params_set_format (handle, hwpar, SND_PCM_FORMAT_S32_LE) < 0)
720 && (snd_pcm_hw_params_set_format (handle, hwpar, SND_PCM_FORMAT_S32_BE) < 0)
721 && (snd_pcm_hw_params_set_format (handle, hwpar, SND_PCM_FORMAT_S24_3LE) < 0)
722 && (snd_pcm_hw_params_set_format (handle, hwpar, SND_PCM_FORMAT_S24_3BE) < 0)
723 && (snd_pcm_hw_params_set_format (handle, hwpar, SND_PCM_FORMAT_S16_LE) < 0)
724 && (snd_pcm_hw_params_set_format (handle, hwpar, SND_PCM_FORMAT_S16_BE) < 0);
728 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: no supported sample format on %s interface.\n.",
732 if (snd_pcm_hw_params_set_rate (handle, hwpar, _fsamp, 0) < 0)
734 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't set %s sample rate to %u.\n",
738 snd_pcm_hw_params_get_channels_max (hwpar, nchan);
741 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: detected more than 1024 %s channnels, reset to 2.\n",
745 if (_debug & FORCE_2CH)
749 if (*nchan > MAXCHAN)
751 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: number of %s channels reduced to %d.\n",
756 if (snd_pcm_hw_params_set_channels (handle, hwpar, *nchan) < 0)
758 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't set %s channel count to %u.\n",
762 if (snd_pcm_hw_params_set_period_size (handle, hwpar, _fsize, 0) < 0)
764 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't set %s period size to %lu.\n",
768 if (snd_pcm_hw_params_set_periods (handle, hwpar, nfrag, 0) < 0)
770 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't set %s periods to %u.\n",
774 if (snd_pcm_hw_params_set_buffer_size (handle, hwpar, _fsize * nfrag) < 0)
776 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't set %s buffer length to %lu.\n",
777 sname, _fsize * nfrag);
780 if (snd_pcm_hw_params (handle, hwpar) < 0)
782 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't set %s hardware parameters.\n",
791 int Alsa_pcmi::set_swpar (snd_pcm_t *handle, snd_pcm_sw_params_t *swpar, const char *sname)
795 snd_pcm_sw_params_current (handle, swpar);
797 if ((err = snd_pcm_sw_params_set_tstamp_mode (handle, swpar, SND_PCM_TSTAMP_MMAP)) < 0)
799 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't set %s timestamp mode to %u.\n",
800 sname, SND_PCM_TSTAMP_MMAP);
803 if ((err = snd_pcm_sw_params_set_avail_min (handle, swpar, _fsize)) < 0)
805 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't set %s avail_min to %lu.\n",
809 if ((err = snd_pcm_sw_params (handle, swpar)) < 0)
811 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't set %s software parameters.\n",
820 int Alsa_pcmi::recover (void)
823 snd_pcm_status_t *stat;
825 snd_pcm_status_alloca (&stat);
829 if ((err = snd_pcm_status (_play_handle, stat)) < 0)
831 if (_debug & DEBUG_STAT) fprintf (stderr, "Alsa_pcmi: pcm_status(play): %s\n",
834 _play_xrun = xruncheck (stat);
838 if ((err = snd_pcm_status (_capt_handle, stat)) < 0)
840 if (_debug & DEBUG_STAT) fprintf (stderr, "Alsa_pcmi: pcm_status(capt): %s\n",
843 _capt_xrun = xruncheck (stat);
846 if (pcm_stop ()) return -1;
847 if (_play_handle && ((err = snd_pcm_prepare (_play_handle)) < 0))
849 if (_debug & DEBUG_STAT) fprintf (stderr, "Alsa_pcmi: pcm_prepare(play): %s\n",
853 if (_capt_handle && !_synced && ((err = snd_pcm_prepare (_capt_handle)) < 0))
855 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: pcm_prepare(capt): %s\n",
859 if (pcm_start ()) return -1;
865 float Alsa_pcmi::xruncheck (snd_pcm_status_t *stat)
867 struct timeval tupd, trig;
870 if (snd_pcm_status_get_state (stat) == SND_PCM_STATE_XRUN)
872 snd_pcm_status_get_tstamp (stat, &tupd);
873 snd_pcm_status_get_trigger_tstamp (stat, &trig);
874 ds = tupd.tv_sec - trig.tv_sec;
875 du = tupd.tv_usec - trig.tv_usec;
881 return ds + 1e-6f * du;
887 char *Alsa_pcmi::clear_16 (char *dst, int nfrm)
891 *((short int *) dst) = 0;
897 char *Alsa_pcmi::clear_24 (char *dst, int nfrm)
909 char *Alsa_pcmi::clear_32 (char *dst, int nfrm)
920 char *Alsa_pcmi::play_16 (const float *src, char *dst, int nfrm, int step)
928 if (s > 1) d = 0x7fff;
929 else if (s < -1) d = 0x8001;
930 else d = (short int)((float) 0x7fff * s);
931 *((short int *) dst) = d;
938 char *Alsa_pcmi::play_16swap (const float *src, char *dst, int nfrm, int step)
946 if (s > 1) d = 0x7fff;
947 else if (s < -1) d = 0x8001;
948 else d = (short int)((float) 0x7fff * s);
957 char *Alsa_pcmi::play_24 (const float *src, char *dst, int nfrm, int step)
965 if (s > 1) d = 0x007fffff;
966 else if (s < -1) d = 0x00800001;
967 else d = (int)((float) 0x007fffff * s);
977 char *Alsa_pcmi::play_24swap (const float *src, char *dst, int nfrm, int step)
985 if (s > 1) d = 0x007fffff;
986 else if (s < -1) d = 0x00800001;
987 else d = (int)((float) 0x007fffff * s);
997 char *Alsa_pcmi::play_32 (const float *src, char *dst, int nfrm, int step)
1005 if (s > 1) d = 0x007fffff;
1006 else if (s < -1) d = 0x00800001;
1007 else d = (int)((float) 0x007fffff * s);
1008 *((int *) dst) = d << 8;
1015 char *Alsa_pcmi::play_32swap (const float *src, char *dst, int nfrm, int step)
1023 if (s > 1) d = 0x007fffff;
1024 else if (s < -1) d = 0x00800001;
1025 else d = (int)((float) 0x007fffff * s);
1036 char *Alsa_pcmi::play_float (const float *src, char *dst, int nfrm, int step)
1040 *((float *) dst) = *src;
1048 const char *Alsa_pcmi::capt_16 (const char *src, float *dst, int nfrm, int step)
1052 const short int s = *((short int const *) src);
1053 const float d = (float) s / (float) 0x7fff;
1061 const char *Alsa_pcmi::capt_16swap (const char *src, float *dst, int nfrm, int step)
1068 s = (src [0] & 0xFF) << 8;
1069 s += (src [1] & 0xFF);
1070 d = (float) s / (float) 0x7fff;
1078 const char *Alsa_pcmi::capt_24 (const char *src, float *dst, int nfrm, int step)
1085 s = (src [0] & 0xFF);
1086 s += (src [1] & 0xFF) << 8;
1087 s += (src [2] & 0xFF) << 16;
1088 if (s & 0x00800000) s-= 0x01000000;
1089 d = (float) s / (float) 0x007fffff;
1097 const char *Alsa_pcmi::capt_24swap (const char *src, float *dst, int nfrm, int step)
1104 s = (src [0] & 0xFF) << 16;
1105 s += (src [1] & 0xFF) << 8;
1106 s += (src [2] & 0xFF);
1107 if (s & 0x00800000) s-= 0x01000000;
1108 d = (float) s / (float) 0x007fffff;
1116 const char *Alsa_pcmi::capt_32 (const char *src, float *dst, int nfrm, int step)
1120 const int s = *((int const *) src);
1121 const float d = (float) s / (float) 0x7fffff00;
1129 const char *Alsa_pcmi::capt_32swap (const char *src, float *dst, int nfrm, int step)
1136 s = (src [0] & 0xFF) << 24;
1137 s += (src [1] & 0xFF) << 16;
1138 s += (src [2] & 0xFF) << 8;
1139 d = (float) s / (float) 0x7fffff00;
1147 const char *Alsa_pcmi::capt_float (const char *src, float *dst, int nfrm, int step)
1151 *dst = *((float const *) src);