fix edge-case in Dummy-backend random number generator initialization
[ardour.git] / libs / backends / alsa / zita-alsa-pcmi.h
1 // ----------------------------------------------------------------------------
2 //
3 //  Copyright (C) 2006-2012 Fons Adriaensen <fons@linuxaudio.org>
4 //
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.
9 //
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.
14 //
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/>.
17 //
18 // ----------------------------------------------------------------------------
19
20
21 #ifndef _ZITA_ALSA_PCMI_H_
22 #define _ZITA_ALSA_PCMI_H_
23
24
25 #define ALSA_PCM_NEW_HW_PARAMS_API
26 #define ALSA_PCM_NEW_SW_PARAMS_API
27 #include <alsa/asoundlib.h>
28
29
30 #define ZITA_ALSA_PCMI_MAJOR_VERSION 0
31 #define ZITA_ALSA_PCMI_MINOR_VERSION 2
32
33 #include <stdint.h>
34
35 extern int zita_alsa_pcmi_major_version (void);
36 extern int zita_alsa_pcmi_minor_version (void);
37
38
39 class Alsa_pcmi
40 {
41 public:
42
43         Alsa_pcmi (
44                         const char   *play_name,
45                         const char   *capt_name,
46                         const char   *ctrl_name,
47                         unsigned int  rate,
48                         unsigned int  frsize,
49                         unsigned int  nfrags,
50                         unsigned int  debug = 0);
51
52         ~Alsa_pcmi (void);
53
54         enum
55         {
56                 DEBUG_INIT = 1,
57                 DEBUG_STAT = 2,
58                 DEBUG_WAIT = 4,
59                 DEBUG_DATA = 8,
60                 DEBUG_ALL  = 15,
61                 FORCE_16B  = 256,
62                 FORCE_2CH  = 512
63         };
64
65         void printinfo (void);
66
67         int pcm_start (void);
68         int pcm_stop (void);
69         snd_pcm_sframes_t pcm_wait (void);
70         int pcm_idle (int len);
71
72         int play_init (snd_pcm_uframes_t len);
73         void clear_chan (int chan, int len);
74         void play_chan (int chan, const float *src, int len, int step = 1);
75         int play_done (int len);
76
77         int capt_init (snd_pcm_uframes_t len);
78         void capt_chan (int chan, float *dst, int len, int step = 1);
79         int capt_done (int len);
80
81         int play_avail (void)
82         {
83                 return snd_pcm_avail (_play_handle);
84         }
85
86         int capt_avail (void)
87         {
88                 return snd_pcm_avail (_capt_handle);
89         }
90
91         int play_delay (void)
92         {
93                 long k;
94                 snd_pcm_delay (_play_handle, &k);
95                 return k;
96         }
97
98         int capt_delay (void)
99         {
100                 long k;
101                 snd_pcm_delay (_capt_handle, &k);
102                 return k;
103         }
104
105         float play_xrun (void) const { return _play_xrun; }
106         float capt_xrun (void) const { return _capt_xrun; }
107
108         int      state (void) const { return _state; }
109         size_t   fsize (void) const { return _fsize; }
110         uint32_t fsamp (void) const { return _fsamp; }
111         uint32_t nfrag (void) const { return _nfrag; }
112         uint32_t nplay (void) const { return _play_nchan; }
113         uint32_t ncapt (void) const { return _capt_nchan; }
114         snd_pcm_t *play_handle (void) const { return _play_handle; }
115         snd_pcm_t *capt_handle (void) const { return _capt_handle; }
116
117
118 private:
119
120         typedef char *(Alsa_pcmi::*clear_function)(char *, int);
121         typedef char *(Alsa_pcmi::*play_function)(const float *, char *, int, int);
122         typedef const char *(Alsa_pcmi::*capt_function) (const char *, float *, int, int);
123
124         enum { MAXPFD = 16, MAXCHAN = 64 };
125
126         void initialise (const char *play_name, const char *capt_name, const char *ctrl_name);
127         int set_hwpar (snd_pcm_t *handle, snd_pcm_hw_params_t *hwpar, const char *sname, unsigned int *nchan);
128         int set_swpar (snd_pcm_t *handle, snd_pcm_sw_params_t *swpar, const char *sname);
129         int recover (void);
130         float xruncheck (snd_pcm_status_t *stat);
131
132         char *clear_32 (char *dst, int nfrm);
133         char *clear_24 (char *dst, int nfrm);
134         char *clear_16 (char *dst, int nfrm);
135
136         char *play_float (const float *src, char *dst, int nfrm, int step);
137         char *play_32 (const float *src, char *dst, int nfrm, int step);
138         char *play_24 (const float *src, char *dst, int nfrm, int step);
139         char *play_16 (const float *src, char *dst, int nfrm, int step);
140         char *play_32swap (const float *src, char *dst, int nfrm, int step);
141         char *play_24swap (const float *src, char *dst, int nfrm, int step);
142         char *play_16swap (const float *src, char *dst, int nfrm, int step);
143
144         const char *capt_float (const char *src, float *dst, int nfrm, int step);
145         const char *capt_32 (const char *src, float *dst, int nfrm, int step);
146         const char *capt_24 (const char *src, float *dst, int nfrm, int step);
147         const char *capt_16 (const char *src, float *dst, int nfrm, int step);
148         const char *capt_32swap (const char *src, float *dst, int nfrm, int step);
149         const char *capt_24swap (const char *src, float *dst, int nfrm, int step);
150         const char *capt_16swap (const char *src, float *dst, int nfrm, int step);
151
152         unsigned int           _fsamp;
153         snd_pcm_uframes_t      _fsize;
154         unsigned int           _nfrag;
155         unsigned int           _debug;
156         int                    _state;
157         snd_pcm_t             *_play_handle;
158         snd_pcm_t             *_capt_handle;
159         snd_ctl_t             *_ctrl_handle;
160         snd_pcm_hw_params_t   *_play_hwpar;
161         snd_pcm_sw_params_t   *_play_swpar;
162         snd_pcm_hw_params_t   *_capt_hwpar;
163         snd_pcm_sw_params_t   *_capt_swpar;
164         snd_pcm_format_t       _play_format;
165         snd_pcm_format_t       _capt_format;
166         snd_pcm_access_t       _play_access;
167         snd_pcm_access_t       _capt_access;
168         unsigned int           _play_nchan;
169         unsigned int           _capt_nchan;
170         float                  _play_xrun;
171         float                  _capt_xrun;
172         bool                   _synced;
173         int                    _play_npfd;
174         int                    _capt_npfd;
175         struct pollfd          _poll_fd [MAXPFD];
176         snd_pcm_uframes_t      _capt_offs;
177         snd_pcm_uframes_t      _play_offs;
178         int                    _play_step;
179         int                    _capt_step;
180         char                  *_play_ptr [MAXCHAN];
181         const char            *_capt_ptr [MAXCHAN];
182         clear_function         _clear_func;
183         play_function          _play_func;
184         capt_function          _capt_func;
185         void                  *_dummy [16];
186 };
187
188 #endif