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 // ----------------------------------------------------------------------------
23 #include "zita-alsa-pcmi.h"
26 // Public members ----------------------------------------------------------------------
29 int zita_alsa_pcmi_major_version (void)
31 return ZITA_ALSA_PCMI_MAJOR_VERSION;
35 int zita_alsa_pcmi_minor_version (void)
37 return ZITA_ALSA_PCMI_MINOR_VERSION;
41 Alsa_pcmi::Alsa_pcmi (
42 const char *play_name,
43 const char *capt_name,
44 const char *ctrl_name,
47 unsigned int play_nfrag,
48 unsigned int capt_nfrag,
52 , _play_nfrag (play_nfrag)
53 , _capt_nfrag (capt_nfrag)
73 p = getenv ("ZITA_ALSA_PCMI_DEBUG");
74 if (p && *p) _debug = atoi (p);
75 initialise (play_name, capt_name, ctrl_name);
79 Alsa_pcmi::~Alsa_pcmi (void)
81 if (_play_handle) snd_pcm_close (_play_handle);
82 if (_capt_handle) snd_pcm_close (_capt_handle);
83 if (_ctrl_handle) snd_ctl_close (_ctrl_handle);
85 snd_pcm_sw_params_free (_capt_swpar);
86 snd_pcm_hw_params_free (_capt_hwpar);
87 snd_pcm_sw_params_free (_play_swpar);
88 snd_pcm_hw_params_free (_play_hwpar);
92 int Alsa_pcmi::pcm_start (void)
99 n = snd_pcm_avail_update (_play_handle);
100 if (n != _fsize * _play_nfrag)
102 if (_debug & DEBUG_STAT) fprintf (stderr, "Alsa_pcmi: full buffer not available at start.\n");
105 for (i = 0; i < _play_nfrag; i++)
108 for (j = 0; j < _play_nchan; j++) clear_chan (j, _fsize);
111 if ((err = snd_pcm_start (_play_handle)) < 0)
113 if (_debug & DEBUG_STAT) fprintf (stderr, "Alsa_pcmi: pcm_start(play): %s.\n", snd_strerror (err));
117 if (_capt_handle && !_synced && ((err = snd_pcm_start (_capt_handle)) < 0))
119 if (_debug & DEBUG_STAT) fprintf (stderr, "Alsa_pcmi: pcm_start(capt): %s.\n", snd_strerror (err));
127 int Alsa_pcmi::pcm_stop (void)
131 if (_play_handle && ((err = snd_pcm_drop (_play_handle)) < 0))
133 if (_debug & DEBUG_STAT) fprintf (stderr, "Alsa_pcmi: pcm_drop(play): %s.\n", snd_strerror (err));
136 if (_capt_handle && !_synced && ((err = snd_pcm_drop (_capt_handle)) < 0))
138 if (_debug & DEBUG_STAT) fprintf (stderr, "Alsa_pcmi: pcm_drop(capt): %s.\n", snd_strerror (err));
146 snd_pcm_sframes_t Alsa_pcmi::pcm_wait (void)
150 snd_pcm_sframes_t capt_av;
151 snd_pcm_sframes_t play_av;
156 need_capt = _capt_handle ? true : false;
157 need_play = _play_handle ? true : false;
159 while (need_play || need_capt)
164 snd_pcm_poll_descriptors (_play_handle, _poll_fd, _play_npfd);
170 snd_pcm_poll_descriptors (_capt_handle, _poll_fd + n1, _capt_npfd);
173 for (i = 0; i < n2; i++) _poll_fd [i].events |= POLLERR;
178 r = ppoll (_poll_fd, n2, &timeout, NULL);
182 if (errno == EINTR) return 0;
183 if (_debug & DEBUG_WAIT) fprintf (stderr, "Alsa_pcmi: poll(): %s\n.", strerror (errno));
189 if (_debug & DEBUG_WAIT) fprintf (stderr, "Alsa_pcmi: poll timed out.\n");
196 snd_pcm_poll_descriptors_revents (_play_handle, _poll_fd, n1, &rev);
199 if (_debug & DEBUG_WAIT) fprintf (stderr, "Alsa_pcmi: error on playback pollfd.\n");
204 if (rev & POLLOUT) need_play = false;
208 snd_pcm_poll_descriptors_revents (_capt_handle, _poll_fd + n1, n2 - n1, &rev);
211 if (_debug & DEBUG_WAIT) fprintf (stderr, "Alsa_pcmi: error on capture pollfd.\n");
216 if (rev & POLLIN) need_capt = false;
221 if (_play_handle && (play_av = snd_pcm_avail_update (_play_handle)) < 0)
228 if (_capt_handle && (capt_av = snd_pcm_avail_update (_capt_handle)) < 0)
235 return (capt_av < play_av) ? capt_av : play_av;
239 int Alsa_pcmi::pcm_idle (int len)
242 snd_pcm_uframes_t n, k;
260 for (i = 0; i < _play_nchan; i++) clear_chan (i, k);
269 int Alsa_pcmi::play_init (snd_pcm_uframes_t len)
272 const snd_pcm_channel_area_t *a;
278 if ((err = snd_pcm_mmap_begin (_play_handle, &a, &_play_offs, &len)) < 0)
280 if (_debug & DEBUG_DATA) fprintf (stderr, "Alsa_pcmi: snd_pcm_mmap_begin(play): %s.\n", snd_strerror (err));
283 _play_step = (a->step) >> 3;
284 for (i = 0; i < _play_nchan; i++, a++)
286 _play_ptr [i] = (char *) a->addr + ((a->first + a->step * _play_offs) >> 3);
293 int Alsa_pcmi::capt_init (snd_pcm_uframes_t len)
296 const snd_pcm_channel_area_t *a;
303 if ((err = snd_pcm_mmap_begin (_capt_handle, &a, &_capt_offs, &len)) < 0)
305 if (_debug & DEBUG_DATA) fprintf (stderr, "Alsa_pcmi: snd_pcm_mmap_begin(capt): %s.\n", snd_strerror (err));
308 _capt_step = (a->step) >> 3;
309 for (i = 0; i < _capt_nchan; i++, a++)
311 _capt_ptr [i] = (char *) a->addr + ((a->first + a->step * _capt_offs) >> 3);
318 void Alsa_pcmi::clear_chan (int chan, int len)
320 _play_ptr [chan] = (this->*Alsa_pcmi::_clear_func)(_play_ptr [chan], len);
324 void Alsa_pcmi::play_chan (int chan, const float *src, int len, int step)
326 _play_ptr [chan] = (this->*Alsa_pcmi::_play_func)(src, _play_ptr [chan], len, step);
330 void Alsa_pcmi::capt_chan (int chan, float *dst, int len, int step)
332 _capt_ptr [chan] = (this->*Alsa_pcmi::_capt_func)(_capt_ptr [chan], dst, len, step);
336 int Alsa_pcmi::play_done (int len)
338 if (!_play_handle) return 0;
339 return snd_pcm_mmap_commit (_play_handle, _play_offs, len);
343 int Alsa_pcmi::capt_done (int len)
345 if (!_capt_handle) return 0;
346 return snd_pcm_mmap_commit (_capt_handle, _capt_offs, len);
350 void Alsa_pcmi::printinfo (void)
352 fprintf (stdout, "playback :");
355 fprintf (stdout, "\n nchan : %d\n", _play_nchan);
356 fprintf (stdout, " fsamp : %d\n", _fsamp);
357 fprintf (stdout, " fsize : %ld\n", _fsize);
358 fprintf (stdout, " nfrag : %d\n", _play_nfrag);
359 fprintf (stdout, " format : %s\n", snd_pcm_format_name (_play_format));
361 else fprintf (stdout, " not enabled\n");
362 fprintf (stdout, "capture :");
365 fprintf (stdout, "\n nchan : %d\n", _capt_nchan);
366 fprintf (stdout, " fsamp : %d\n", _fsamp);
367 fprintf (stdout, " fsize : %ld\n", _fsize);
368 fprintf (stdout, " nfrag : %d\n", _capt_nfrag);
369 fprintf (stdout, " format : %s\n", snd_pcm_format_name (_capt_format));
370 if (_play_handle) fprintf (stdout, "%s\n", _synced ? "synced" : "not synced");
372 else fprintf (stdout, " not enabled\n");
376 // Private members ---------------------------------------------------------------------
379 void Alsa_pcmi::initialise (const char *play_name, const char *capt_name, const char *ctrl_name)
382 snd_pcm_uframes_t fsize;
386 snd_ctl_card_info_t *card;
390 if (snd_pcm_open (&_play_handle, play_name, SND_PCM_STREAM_PLAYBACK, 0) < 0)
393 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: Cannot open PCM device %s for playback.\n",
400 if (snd_pcm_open (&_capt_handle, capt_name, SND_PCM_STREAM_CAPTURE, 0) < 0)
403 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: Cannot open PCM device %s for capture.\n",
408 if (! _play_handle && ! _capt_handle) return;
412 snd_ctl_card_info_alloca (&card);
414 if ((err = snd_ctl_open (&_ctrl_handle, ctrl_name, 0)) < 0)
416 if (_debug & DEBUG_INIT) fprintf (stderr, "Alse_driver: ctl_open(): %s\n",
420 if ((err = snd_ctl_card_info (_ctrl_handle, card)) < 0)
422 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: ctl_card_info(): %s\n",
431 if (snd_pcm_hw_params_malloc (&_play_hwpar) < 0)
433 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't allocate playback hw params\n");
436 if (snd_pcm_sw_params_malloc (&_play_swpar) < 0)
438 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't allocate playback sw params\n");
441 if (set_hwpar (_play_handle, _play_hwpar, "playback", _play_nfrag, &_play_nchan) < 0) return;
442 if (set_swpar (_play_handle, _play_swpar, "playback") < 0) return;
447 if (snd_pcm_hw_params_malloc (&_capt_hwpar) < 0)
449 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't allocate capture hw params\n");
452 if (snd_pcm_sw_params_malloc (&_capt_swpar) < 0)
454 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't allocate capture sw params\n");
457 if (set_hwpar (_capt_handle, _capt_hwpar, "capture", _capt_nfrag, &_capt_nchan) < 0) return;
458 if (set_swpar (_capt_handle, _capt_swpar, "capture") < 0) return;
463 if (snd_pcm_hw_params_get_rate (_play_hwpar, &fsamp, &dir) || (fsamp != _fsamp) || dir)
465 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't get requested sample rate for playback.\n");
469 if (snd_pcm_hw_params_get_period_size (_play_hwpar, &fsize, &dir) || (fsize != _fsize) || dir)
471 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't get requested period size for playback.\n");
475 if (snd_pcm_hw_params_get_periods (_play_hwpar, &nfrag, &dir) || (nfrag != _play_nfrag) || dir)
477 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't get requested number of periods for playback.\n");
482 snd_pcm_hw_params_get_format (_play_hwpar, &_play_format);
483 snd_pcm_hw_params_get_access (_play_hwpar, &_play_access);
485 #if __BYTE_ORDER == __LITTLE_ENDIAN
486 switch (_play_format)
488 case SND_PCM_FORMAT_FLOAT_LE:
489 _clear_func = &Alsa_pcmi::clear_32;
490 _play_func = &Alsa_pcmi::play_float;
493 case SND_PCM_FORMAT_S32_LE:
494 _clear_func = &Alsa_pcmi::clear_32;
495 _play_func = &Alsa_pcmi::play_32;
498 case SND_PCM_FORMAT_S32_BE:
499 _clear_func = &Alsa_pcmi::clear_32;
500 _play_func = &Alsa_pcmi::play_32swap;
503 case SND_PCM_FORMAT_S24_3LE:
504 _clear_func = &Alsa_pcmi::clear_24;
505 _play_func = &Alsa_pcmi::play_24;
508 case SND_PCM_FORMAT_S24_3BE:
509 _clear_func = &Alsa_pcmi::clear_24;
510 _play_func = &Alsa_pcmi::play_24swap;
513 case SND_PCM_FORMAT_S16_LE:
514 _clear_func = &Alsa_pcmi::clear_16;
515 _play_func = &Alsa_pcmi::play_16;
518 case SND_PCM_FORMAT_S16_BE:
519 _clear_func = &Alsa_pcmi::clear_16;
520 _play_func = &Alsa_pcmi::play_16swap;
524 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't handle playback sample format.\n");
528 #elif __BYTE_ORDER == __BIG_ENDIAN
529 switch (_play_format)
531 case SND_PCM_FORMAT_S32_LE:
532 _clear_func = &Alsa_pcmi::clear_32;
533 _play_func = &Alsa_pcmi::play_32swap;
536 case SND_PCM_FORMAT_S32_BE:
537 _clear_func = &Alsa_pcmi::clear_32;
538 _play_func = &Alsa_pcmi::play_32;
541 case SND_PCM_FORMAT_S24_3LE:
542 _clear_func = &Alsa_pcmi::clear_24;
543 _play_func = &Alsa_pcmi::play_24swap;
546 case SND_PCM_FORMAT_S24_3BE:
547 _clear_func = &Alsa_pcmi::clear_24;
548 _play_func = &Alsa_pcmi::play_24;
551 case SND_PCM_FORMAT_S16_LE:
552 _clear_func = &Alsa_pcmi::clear_16;
553 _play_func = &Alsa_pcmi::play_16swap;
556 case SND_PCM_FORMAT_S16_BE:
557 _clear_func = &Alsa_pcmi::clear_16;
558 _play_func = &Alsa_pcmi::play_16;
562 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't handle playback sample format.\n");
567 #error "System byte order is undefined or not supported"
570 _play_npfd = snd_pcm_poll_descriptors_count (_play_handle);
575 if (snd_pcm_hw_params_get_rate (_capt_hwpar, &fsamp, &dir) || (fsamp != _fsamp) || dir)
577 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't get requested sample rate for capture.\n");
581 if (snd_pcm_hw_params_get_period_size (_capt_hwpar, &fsize, &dir) || (fsize != _fsize) || dir)
583 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't get requested period size for capture.\n");
587 if (snd_pcm_hw_params_get_periods (_capt_hwpar, &nfrag, &dir) || (nfrag != _capt_nfrag) || dir)
589 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't get requested number of periods for capture.\n");
594 if (_play_handle) _synced = ! snd_pcm_link (_play_handle, _capt_handle);
596 snd_pcm_hw_params_get_format (_capt_hwpar, &_capt_format);
597 snd_pcm_hw_params_get_access (_capt_hwpar, &_capt_access);
599 #if __BYTE_ORDER == __LITTLE_ENDIAN
600 switch (_capt_format)
602 case SND_PCM_FORMAT_FLOAT_LE:
603 _capt_func = &Alsa_pcmi::capt_float;
606 case SND_PCM_FORMAT_S32_LE:
607 _capt_func = &Alsa_pcmi::capt_32;
610 case SND_PCM_FORMAT_S32_BE:
611 _capt_func = &Alsa_pcmi::capt_32swap;
614 case SND_PCM_FORMAT_S24_3LE:
615 _capt_func = &Alsa_pcmi::capt_24;
618 case SND_PCM_FORMAT_S24_3BE:
619 _capt_func = &Alsa_pcmi::capt_24swap;
622 case SND_PCM_FORMAT_S16_LE:
623 _capt_func = &Alsa_pcmi::capt_16;
626 case SND_PCM_FORMAT_S16_BE:
627 _capt_func = &Alsa_pcmi::capt_16swap;
631 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't handle capture sample format.\n");
635 #elif __BYTE_ORDER == __BIG_ENDIAN
636 switch (_capt_format)
638 case SND_PCM_FORMAT_S32_LE:
639 _capt_func = &Alsa_pcmi::capt_32swap;
642 case SND_PCM_FORMAT_S32_BE:
643 _capt_func = &Alsa_pcmi::capt_32;
646 case SND_PCM_FORMAT_S24_3LE:
647 _capt_func = &Alsa_pcmi::capt_24swap;
650 case SND_PCM_FORMAT_S24_3BE:
651 _capt_func = &Alsa_pcmi::capt_24;
654 case SND_PCM_FORMAT_S16_LE:
655 _capt_func = &Alsa_pcmi::capt_16swap;
658 case SND_PCM_FORMAT_S16_BE:
659 _capt_func = &Alsa_pcmi::capt_16;
663 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't handle capture sample format.\n");
668 #error "System byte order is undefined or not supported"
671 _capt_npfd = snd_pcm_poll_descriptors_count (_capt_handle);
674 if (_play_npfd + _capt_npfd > MAXPFD)
676 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: interface requires more than %d pollfd\n", MAXPFD);
684 int Alsa_pcmi::set_hwpar (snd_pcm_t *handle, snd_pcm_hw_params_t *hwpar, const char *sname, unsigned int nfrag, unsigned int *nchan)
688 if (snd_pcm_hw_params_any (handle, hwpar) < 0)
690 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: no %s hw configurations available.\n",
694 if (snd_pcm_hw_params_set_periods_integer (handle, hwpar) < 0)
696 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't set %s period size to integral value.\n",
700 if ( (snd_pcm_hw_params_set_access (handle, hwpar, SND_PCM_ACCESS_MMAP_NONINTERLEAVED) < 0)
701 && (snd_pcm_hw_params_set_access (handle, hwpar, SND_PCM_ACCESS_MMAP_INTERLEAVED) < 0)
702 && (snd_pcm_hw_params_set_access (handle, hwpar, SND_PCM_ACCESS_MMAP_COMPLEX) < 0))
704 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: the %s interface doesn't support mmap-based access.\n",
708 if (_debug & FORCE_16B)
710 err = (snd_pcm_hw_params_set_format (handle, hwpar, SND_PCM_FORMAT_S16_LE) < 0)
711 && (snd_pcm_hw_params_set_format (handle, hwpar, SND_PCM_FORMAT_S16_BE) < 0);
715 err = (snd_pcm_hw_params_set_format (handle, hwpar, SND_PCM_FORMAT_FLOAT_LE) < 0)
716 && (snd_pcm_hw_params_set_format (handle, hwpar, SND_PCM_FORMAT_S32_LE) < 0)
717 && (snd_pcm_hw_params_set_format (handle, hwpar, SND_PCM_FORMAT_S32_BE) < 0)
718 && (snd_pcm_hw_params_set_format (handle, hwpar, SND_PCM_FORMAT_S24_3LE) < 0)
719 && (snd_pcm_hw_params_set_format (handle, hwpar, SND_PCM_FORMAT_S24_3BE) < 0)
720 && (snd_pcm_hw_params_set_format (handle, hwpar, SND_PCM_FORMAT_S16_LE) < 0)
721 && (snd_pcm_hw_params_set_format (handle, hwpar, SND_PCM_FORMAT_S16_BE) < 0);
725 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: no supported sample format on %s interface.\n.",
729 if (snd_pcm_hw_params_set_rate (handle, hwpar, _fsamp, 0) < 0)
731 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't set %s sample rate to %u.\n",
735 snd_pcm_hw_params_get_channels_max (hwpar, nchan);
738 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: detected more than 1024 %s channnels, reset to 2.\n",
742 if (_debug & FORCE_2CH)
746 if (*nchan > MAXCHAN)
748 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: number of %s channels reduced to %d.\n",
753 if (snd_pcm_hw_params_set_channels (handle, hwpar, *nchan) < 0)
755 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't set %s channel count to %u.\n",
759 if (snd_pcm_hw_params_set_period_size (handle, hwpar, _fsize, 0) < 0)
761 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't set %s period size to %lu.\n",
765 if (snd_pcm_hw_params_set_periods (handle, hwpar, nfrag, 0) < 0)
767 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't set %s periods to %u.\n",
771 if (snd_pcm_hw_params_set_buffer_size (handle, hwpar, _fsize * nfrag) < 0)
773 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't set %s buffer length to %lu.\n",
774 sname, _fsize * nfrag);
777 if (snd_pcm_hw_params (handle, hwpar) < 0)
779 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't set %s hardware parameters.\n",
788 int Alsa_pcmi::set_swpar (snd_pcm_t *handle, snd_pcm_sw_params_t *swpar, const char *sname)
792 snd_pcm_sw_params_current (handle, swpar);
794 if ((err = snd_pcm_sw_params_set_tstamp_mode (handle, swpar, SND_PCM_TSTAMP_MMAP)) < 0)
796 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't set %s timestamp mode to %u.\n",
797 sname, SND_PCM_TSTAMP_MMAP);
800 if ((err = snd_pcm_sw_params_set_avail_min (handle, swpar, _fsize)) < 0)
802 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't set %s avail_min to %lu.\n",
806 if ((err = snd_pcm_sw_params (handle, swpar)) < 0)
808 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't set %s software parameters.\n",
817 int Alsa_pcmi::recover (void)
820 snd_pcm_status_t *stat;
822 snd_pcm_status_alloca (&stat);
826 if ((err = snd_pcm_status (_play_handle, stat)) < 0)
828 if (_debug & DEBUG_STAT) fprintf (stderr, "Alsa_pcmi: pcm_status(play): %s\n",
831 _play_xrun = xruncheck (stat);
835 if ((err = snd_pcm_status (_capt_handle, stat)) < 0)
837 if (_debug & DEBUG_STAT) fprintf (stderr, "Alsa_pcmi: pcm_status(capt): %s\n",
840 _capt_xrun = xruncheck (stat);
843 if (pcm_stop ()) return -1;
844 if (_play_handle && ((err = snd_pcm_prepare (_play_handle)) < 0))
846 if (_debug & DEBUG_STAT) fprintf (stderr, "Alsa_pcmi: pcm_prepare(play): %s\n",
850 if (_capt_handle && !_synced && ((err = snd_pcm_prepare (_capt_handle)) < 0))
852 if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: pcm_prepare(capt): %s\n",
856 if (pcm_start ()) return -1;
862 float Alsa_pcmi::xruncheck (snd_pcm_status_t *stat)
864 struct timeval tupd, trig;
867 if (snd_pcm_status_get_state (stat) == SND_PCM_STATE_XRUN)
869 snd_pcm_status_get_tstamp (stat, &tupd);
870 snd_pcm_status_get_trigger_tstamp (stat, &trig);
871 ds = tupd.tv_sec - trig.tv_sec;
872 du = tupd.tv_usec - trig.tv_usec;
878 return ds + 1e-6f * du;
884 char *Alsa_pcmi::clear_16 (char *dst, int nfrm)
888 *((short int *) dst) = 0;
894 char *Alsa_pcmi::clear_24 (char *dst, int nfrm)
906 char *Alsa_pcmi::clear_32 (char *dst, int nfrm)
917 char *Alsa_pcmi::play_16 (const float *src, char *dst, int nfrm, int step)
925 if (s > 1) d = 0x7fff;
926 else if (s < -1) d = 0x8001;
927 else d = (short int)((float) 0x7fff * s);
928 *((short int *) dst) = d;
935 char *Alsa_pcmi::play_16swap (const float *src, char *dst, int nfrm, int step)
943 if (s > 1) d = 0x7fff;
944 else if (s < -1) d = 0x8001;
945 else d = (short int)((float) 0x7fff * s);
954 char *Alsa_pcmi::play_24 (const float *src, char *dst, int nfrm, int step)
962 if (s > 1) d = 0x007fffff;
963 else if (s < -1) d = 0x00800001;
964 else d = (int)((float) 0x007fffff * s);
974 char *Alsa_pcmi::play_24swap (const float *src, char *dst, int nfrm, int step)
982 if (s > 1) d = 0x007fffff;
983 else if (s < -1) d = 0x00800001;
984 else d = (int)((float) 0x007fffff * s);
994 char *Alsa_pcmi::play_32 (const float *src, char *dst, int nfrm, int step)
1002 if (s > 1) d = 0x007fffff;
1003 else if (s < -1) d = 0x00800001;
1004 else d = (int)((float) 0x007fffff * s);
1005 *((int *) dst) = d << 8;
1012 char *Alsa_pcmi::play_32swap (const float *src, char *dst, int nfrm, int step)
1020 if (s > 1) d = 0x007fffff;
1021 else if (s < -1) d = 0x00800001;
1022 else d = (int)((float) 0x007fffff * s);
1033 char *Alsa_pcmi::play_float (const float *src, char *dst, int nfrm, int step)
1037 *((float *) dst) = *src;
1045 const char *Alsa_pcmi::capt_16 (const char *src, float *dst, int nfrm, int step)
1049 const short int s = *((short int const *) src);
1050 const float d = (float) s / (float) 0x7fff;
1058 const char *Alsa_pcmi::capt_16swap (const char *src, float *dst, int nfrm, int step)
1065 s = (src [0] & 0xFF) << 8;
1066 s += (src [1] & 0xFF);
1067 d = (float) s / (float) 0x7fff;
1075 const char *Alsa_pcmi::capt_24 (const char *src, float *dst, int nfrm, int step)
1082 s = (src [0] & 0xFF);
1083 s += (src [1] & 0xFF) << 8;
1084 s += (src [2] & 0xFF) << 16;
1085 if (s & 0x00800000) s-= 0x01000000;
1086 d = (float) s / (float) 0x007fffff;
1094 const char *Alsa_pcmi::capt_24swap (const char *src, float *dst, int nfrm, int step)
1101 s = (src [0] & 0xFF) << 16;
1102 s += (src [1] & 0xFF) << 8;
1103 s += (src [2] & 0xFF);
1104 if (s & 0x00800000) s-= 0x01000000;
1105 d = (float) s / (float) 0x007fffff;
1113 const char *Alsa_pcmi::capt_32 (const char *src, float *dst, int nfrm, int step)
1117 const int s = *((int const *) src);
1118 const float d = (float) s / (float) 0x7fffff00;
1126 const char *Alsa_pcmi::capt_32swap (const char *src, float *dst, int nfrm, int step)
1133 s = (src [0] & 0xFF) << 24;
1134 s += (src [1] & 0xFF) << 16;
1135 s += (src [2] & 0xFF) << 8;
1136 d = (float) s / (float) 0x7fffff00;
1144 const char *Alsa_pcmi::capt_float (const char *src, float *dst, int nfrm, int step)
1148 *dst = *((float const *) src);