use primary, not secondary, clock mode to drive other clock modes, and dynamically...
[ardour.git] / libs / backends / alsa / zita-alsa-pcmi.cc
index 5aba08c786c203001870ed05d8b6922be1da5c13..4c5298f0d9b65070a0a376ac0f671f67bd979aab 100644 (file)
 //
 // ----------------------------------------------------------------------------
 
-
+#if defined(__NetBSD__)
+#include <sys/endian.h>
+#else
 #include <endian.h>
+#endif
 #include <sys/time.h>
 #include "zita-alsa-pcmi.h"
 
@@ -44,11 +47,13 @@ Alsa_pcmi::Alsa_pcmi (
                const char        *ctrl_name,
                unsigned int       fsamp,
                unsigned int       fsize,
-               unsigned int       nfrag,
+               unsigned int       play_nfrag,
+               unsigned int       capt_nfrag,
                unsigned int       debug)
        : _fsamp (fsamp)
        , _fsize (fsize)
-       , _nfrag (nfrag)
+       , _play_nfrag (play_nfrag)
+       , _capt_nfrag (capt_nfrag)
        , _debug (debug)
        , _state (-1)
        , _play_handle (0)
@@ -95,12 +100,12 @@ int Alsa_pcmi::pcm_start (void)
        if (_play_handle)
        {
                n = snd_pcm_avail_update (_play_handle);
-               if (n != _fsize * _nfrag)
+               if (n != _fsize * _play_nfrag)
                {
                        if (_debug & DEBUG_STAT) fprintf  (stderr, "Alsa_pcmi: full buffer not available at start.\n");
                        return -1;
                }
-               for (i = 0; i < _nfrag; i++)
+               for (i = 0; i < _play_nfrag; i++)
                {
                        play_init (_fsize);
                        for (j = 0; j < _play_nchan; j++) clear_chan (j, _fsize);
@@ -170,7 +175,16 @@ snd_pcm_sframes_t Alsa_pcmi::pcm_wait (void)
                }
                for (i = 0; i < n2; i++) _poll_fd [i].events |= POLLERR;
 
-               r = poll (_poll_fd, n2, 1000);
+               timespec timeout;
+               timeout.tv_sec = 1;
+               timeout.tv_nsec = 0;
+#if defined(__NetBSD__)
+               r = pollts (_poll_fd, n2, &timeout, NULL);
+#else
+               r = ppoll (_poll_fd, n2, &timeout, NULL);
+#endif
+
+
                if (r < 0)
                {
                        if (errno == EINTR) return 0;
@@ -266,6 +280,9 @@ int Alsa_pcmi::play_init (snd_pcm_uframes_t len)
        const snd_pcm_channel_area_t   *a;
        int                            err;
 
+       if (!_play_handle) {
+               return 0;
+       }
        if ((err = snd_pcm_mmap_begin (_play_handle, &a, &_play_offs, &len)) < 0)
        {
                if (_debug & DEBUG_DATA) fprintf (stderr, "Alsa_pcmi: snd_pcm_mmap_begin(play): %s.\n", snd_strerror (err));
@@ -287,6 +304,10 @@ int Alsa_pcmi::capt_init (snd_pcm_uframes_t len)
        const snd_pcm_channel_area_t  *a;
        int                           err;
 
+       if (!_capt_handle) {
+               return 0;
+       }
+
        if ((err = snd_pcm_mmap_begin (_capt_handle, &a, &_capt_offs, &len)) < 0)
        {
                if (_debug & DEBUG_DATA) fprintf (stderr, "Alsa_pcmi: snd_pcm_mmap_begin(capt): %s.\n", snd_strerror (err));
@@ -322,12 +343,14 @@ void Alsa_pcmi::capt_chan  (int chan, float *dst, int len, int step)
 
 int Alsa_pcmi::play_done (int len)
 {
+       if (!_play_handle) return 0;
        return snd_pcm_mmap_commit (_play_handle, _play_offs, len);
 }
 
 
 int Alsa_pcmi::capt_done (int len)
 {
+       if (!_capt_handle) return 0;
        return snd_pcm_mmap_commit (_capt_handle, _capt_offs, len);
 }
 
@@ -340,7 +363,7 @@ void Alsa_pcmi::printinfo (void)
                fprintf (stdout, "\n  nchan  : %d\n", _play_nchan);
                fprintf (stdout, "  fsamp  : %d\n", _fsamp);
                fprintf (stdout, "  fsize  : %ld\n", _fsize);
-               fprintf (stdout, "  nfrag  : %d\n", _nfrag);
+               fprintf (stdout, "  nfrag  : %d\n", _play_nfrag);
                fprintf (stdout, "  format : %s\n", snd_pcm_format_name (_play_format));
        }
        else fprintf (stdout, " not enabled\n");
@@ -350,7 +373,7 @@ void Alsa_pcmi::printinfo (void)
                fprintf (stdout, "\n  nchan  : %d\n", _capt_nchan);
                fprintf (stdout, "  fsamp  : %d\n", _fsamp);
                fprintf (stdout, "  fsize  : %ld\n", _fsize);
-               fprintf (stdout, "  nfrag  : %d\n", _nfrag);
+               fprintf (stdout, "  nfrag  : %d\n", _capt_nfrag);
                fprintf (stdout, "  format : %s\n", snd_pcm_format_name (_capt_format));
                if (_play_handle) fprintf (stdout, "%s\n", _synced ? "synced" : "not synced");
        }
@@ -390,7 +413,7 @@ void Alsa_pcmi::initialise (const char *play_name, const char *capt_name, const
                }
        }
 
-       if (! _play_handle || ! _capt_handle) return;
+       if (! _play_handle && ! _capt_handle) return;
 
        if (ctrl_name)
        {
@@ -423,7 +446,7 @@ void Alsa_pcmi::initialise (const char *play_name, const char *capt_name, const
                        if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't allocate playback sw params\n");
                        return;
                }
-               if (set_hwpar (_play_handle, _play_hwpar, "playback", &_play_nchan) < 0) return;
+               if (set_hwpar (_play_handle, _play_hwpar, "playback", _play_nfrag, &_play_nchan) < 0) return;
                if (set_swpar (_play_handle, _play_swpar, "playback") < 0) return;
        }
 
@@ -439,7 +462,7 @@ void Alsa_pcmi::initialise (const char *play_name, const char *capt_name, const
                        if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't allocate capture sw params\n");
                        return;
                }
-               if (set_hwpar (_capt_handle, _capt_hwpar, "capture", &_capt_nchan) < 0) return;
+               if (set_hwpar (_capt_handle, _capt_hwpar, "capture", _capt_nfrag, &_capt_nchan) < 0) return;
                if (set_swpar (_capt_handle, _capt_swpar, "capture") < 0) return;
        }
 
@@ -457,7 +480,7 @@ void Alsa_pcmi::initialise (const char *play_name, const char *capt_name, const
                        _state = -4;
                        return;
                }
-               if (snd_pcm_hw_params_get_periods (_play_hwpar, &nfrag, &dir) || (nfrag != _nfrag) || dir)
+               if (snd_pcm_hw_params_get_periods (_play_hwpar, &nfrag, &dir) || (nfrag != _play_nfrag) || dir)
                {
                        if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't get requested number of periods for playback.\n");
                        _state = -5;
@@ -569,7 +592,7 @@ void Alsa_pcmi::initialise (const char *play_name, const char *capt_name, const
                        _state = -4;
                        return;
                }
-               if (snd_pcm_hw_params_get_periods (_capt_hwpar, &nfrag, &dir) || (nfrag != _nfrag) || dir)
+               if (snd_pcm_hw_params_get_periods (_capt_hwpar, &nfrag, &dir) || (nfrag != _capt_nfrag) || dir)
                {
                        if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't get requested number of periods for capture.\n");
                        _state = -5;
@@ -666,7 +689,7 @@ void Alsa_pcmi::initialise (const char *play_name, const char *capt_name, const
 }
 
 
-int Alsa_pcmi::set_hwpar (snd_pcm_t *handle,  snd_pcm_hw_params_t *hwpar, const char *sname, unsigned int *nchan)
+int Alsa_pcmi::set_hwpar (snd_pcm_t *handle,  snd_pcm_hw_params_t *hwpar, const char *sname, unsigned int nfrag, unsigned int *nchan)
 {
        bool err;
 
@@ -715,12 +738,12 @@ int Alsa_pcmi::set_hwpar (snd_pcm_t *handle,  snd_pcm_hw_params_t *hwpar, const
        {
                if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't set %s sample rate to %u.\n",
                                sname, _fsamp);
-               return -1;
+               return -3;
        }
        snd_pcm_hw_params_get_channels_max (hwpar, nchan);
        if (*nchan > 1024)
        {
-               if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: detected more than 1024 %s channnels, reset to 2.\n",
+               if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: detected more than 1024 %s channels, reset to 2.\n",
                                sname);
                *nchan = 2;
        }
@@ -745,19 +768,19 @@ int Alsa_pcmi::set_hwpar (snd_pcm_t *handle,  snd_pcm_hw_params_t *hwpar, const
        {
                if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't set %s period size to %lu.\n",
                                sname, _fsize);
-               return -1;
+               return -4;
        }
-       if (snd_pcm_hw_params_set_periods (handle, hwpar, _nfrag, 0) < 0)
+       if (snd_pcm_hw_params_set_periods (handle, hwpar, nfrag, 0) < 0)
        {
                if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't set %s periods to %u.\n",
-                               sname, _nfrag);
-               return -1;
+                               sname, nfrag);
+               return -5;
        }
-       if (snd_pcm_hw_params_set_buffer_size (handle, hwpar, _fsize * _nfrag) < 0)
+       if (snd_pcm_hw_params_set_buffer_size (handle, hwpar, _fsize * nfrag) < 0)
        {
                if (_debug & DEBUG_INIT) fprintf (stderr, "Alsa_pcmi: can't set %s buffer length to %lu.\n",
-                               sname, _fsize * _nfrag);
-               return -1;
+                               sname, _fsize * nfrag);
+               return -4;
        }
        if (snd_pcm_hw_params (handle, hwpar) < 0)
        {