O(log(n)) port and connection lookup for ALSA
[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  play_nfrags,
50                         unsigned int  capt_nfrags,
51                         unsigned int  debug = 0);
52
53         ~Alsa_pcmi (void);
54
55         enum
56         {
57                 DEBUG_INIT = 1,
58                 DEBUG_STAT = 2,
59                 DEBUG_WAIT = 4,
60                 DEBUG_DATA = 8,
61                 DEBUG_ALL  = 15,
62                 FORCE_16B  = 256,
63                 FORCE_2CH  = 512
64         };
65
66         void printinfo (void);
67
68         int pcm_start (void);
69         int pcm_stop (void);
70         snd_pcm_sframes_t pcm_wait (void);
71         int pcm_idle (int len);
72
73         int play_init (snd_pcm_uframes_t len);
74         void clear_chan (int chan, int len);
75         void play_chan (int chan, const float *src, int len, int step = 1);
76         int play_done (int len);
77
78         int capt_init (snd_pcm_uframes_t len);
79         void capt_chan (int chan, float *dst, int len, int step = 1);
80         int capt_done (int len);
81
82         int play_avail (void)
83         {
84                 return snd_pcm_avail (_play_handle);
85         }
86
87         int capt_avail (void)
88         {
89                 return snd_pcm_avail (_capt_handle);
90         }
91
92         int play_delay (void)
93         {
94                 long k;
95                 snd_pcm_delay (_play_handle, &k);
96                 return k;
97         }
98
99         int capt_delay (void)
100         {
101                 long k;
102                 snd_pcm_delay (_capt_handle, &k);
103                 return k;
104         }
105
106         float play_xrun (void) const { return _play_xrun; }
107         float capt_xrun (void) const { return _capt_xrun; }
108
109         int      state (void) const { return _state; }
110         size_t   fsize (void) const { return _fsize; }
111         uint32_t fsamp (void) const { return _fsamp; }
112         uint32_t play_nfrag (void) const { return _play_nfrag; }
113         uint32_t capt_nfrag (void) const { return _capt_nfrag; }
114         uint32_t nplay (void) const { return _play_nchan; }
115         uint32_t ncapt (void) const { return _capt_nchan; }
116         snd_pcm_t *play_handle (void) const { return _play_handle; }
117         snd_pcm_t *capt_handle (void) const { return _capt_handle; }
118
119
120 private:
121
122         typedef char *(Alsa_pcmi::*clear_function)(char *, int);
123         typedef char *(Alsa_pcmi::*play_function)(const float *, char *, int, int);
124         typedef const char *(Alsa_pcmi::*capt_function) (const char *, float *, int, int);
125
126         enum { MAXPFD = 16, MAXCHAN = 64 };
127
128         void initialise (const char *play_name, const char *capt_name, const char *ctrl_name);
129         int set_hwpar (snd_pcm_t *handle, snd_pcm_hw_params_t *hwpar, const char *sname, unsigned int nfrag, unsigned int *nchan);
130         int set_swpar (snd_pcm_t *handle, snd_pcm_sw_params_t *swpar, const char *sname);
131         int recover (void);
132         float xruncheck (snd_pcm_status_t *stat);
133
134         char *clear_32 (char *dst, int nfrm);
135         char *clear_24 (char *dst, int nfrm);
136         char *clear_16 (char *dst, int nfrm);
137
138         char *play_float (const float *src, char *dst, int nfrm, int step);
139         char *play_32 (const float *src, char *dst, int nfrm, int step);
140         char *play_24 (const float *src, char *dst, int nfrm, int step);
141         char *play_16 (const float *src, char *dst, int nfrm, int step);
142         char *play_32swap (const float *src, char *dst, int nfrm, int step);
143         char *play_24swap (const float *src, char *dst, int nfrm, int step);
144         char *play_16swap (const float *src, char *dst, int nfrm, int step);
145
146         const char *capt_float (const char *src, float *dst, int nfrm, int step);
147         const char *capt_32 (const char *src, float *dst, int nfrm, int step);
148         const char *capt_24 (const char *src, float *dst, int nfrm, int step);
149         const char *capt_16 (const char *src, float *dst, int nfrm, int step);
150         const char *capt_32swap (const char *src, float *dst, int nfrm, int step);
151         const char *capt_24swap (const char *src, float *dst, int nfrm, int step);
152         const char *capt_16swap (const char *src, float *dst, int nfrm, int step);
153
154         unsigned int           _fsamp;
155         snd_pcm_uframes_t      _fsize;
156         unsigned int           _play_nfrag;
157         unsigned int           _capt_nfrag;
158         unsigned int           _debug;
159         int                    _state;
160         snd_pcm_t             *_play_handle;
161         snd_pcm_t             *_capt_handle;
162         snd_ctl_t             *_ctrl_handle;
163         snd_pcm_hw_params_t   *_play_hwpar;
164         snd_pcm_sw_params_t   *_play_swpar;
165         snd_pcm_hw_params_t   *_capt_hwpar;
166         snd_pcm_sw_params_t   *_capt_swpar;
167         snd_pcm_format_t       _play_format;
168         snd_pcm_format_t       _capt_format;
169         snd_pcm_access_t       _play_access;
170         snd_pcm_access_t       _capt_access;
171         unsigned int           _play_nchan;
172         unsigned int           _capt_nchan;
173         float                  _play_xrun;
174         float                  _capt_xrun;
175         bool                   _synced;
176         int                    _play_npfd;
177         int                    _capt_npfd;
178         struct pollfd          _poll_fd [MAXPFD];
179         snd_pcm_uframes_t      _capt_offs;
180         snd_pcm_uframes_t      _play_offs;
181         int                    _play_step;
182         int                    _capt_step;
183         char                  *_play_ptr [MAXCHAN];
184         const char            *_capt_ptr [MAXCHAN];
185         clear_function         _clear_func;
186         play_function          _play_func;
187         capt_function          _capt_func;
188         void                  *_dummy [16];
189 };
190
191 #endif