CLAM-Development  1.4.0
SndPcm.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 /* alsa 0.9 backwards compatibility for alsa 1.0 */
23 #define ALSA_PCM_OLD_HW_PARAMS_API
24 #define ALSA_PCM_OLD_SW_PARAMS_API
25 
26 #include <alsa/asoundlib.h>
27 #include "Err.hxx"
28 
29 class SndPcmError : public CLAM::Err
30 {
31 public:
32  SndPcmError(const char* _str)
33  :Err(_str){
34 
35  }
36 };
37 
38 class SndPcm
39 {
40 public:
41  snd_pcm_format_t format;
42  int rate;
45  int latency;
46  int latency_min; /* in frames / 2 */
47  int latency_max; /* in frames / 2 */
48  int block; /* block mode */
49  int tick_time; /* disabled, otherwise in us */
51 
52  snd_pcm_t *phandle, *chandle;
53 
54  char error_str[1024];
55  void cat_error(const char* fmt,...);
56 
57  void ReadBuf(short* data)
58  {
59  int res = readbuf(chandle,(char*) data,latency);
60  if (res>=0) return;
61  if (res == -EPIPE)
62 // throw(SndPcmError("SndPcm::ReadBuf() Buffer Overrun!\n"));
63  RecoverXRun(data);
64  }
65  void WriteBuf(short* data)
66  {
67  int res = writebuf(phandle,(char*) data,latency);
68  if (res>=0) return;
69  if (res == -EPIPE)
70 // throw(SndPcmError("SndPcm::WriteBuf() Buffer Underrun!\n"));
71  RecoverXRun(data);
72  }
73  void ReadBuf(short* data,int len)
74  {
75  int res = readbuf(chandle,(char*) data,len);
76  if (res>=0) return;
77  if (res == -EPIPE)
78 // throw(SndPcmError("SndPcm::ReadBuf() Buffer Overrun!\n"));
79  RecoverXRun(data);
80  }
81  void WriteBuf(short* data,int len)
82  {
83  int res = writebuf(phandle,(char*) data,len);
84  if (res>=0) return;
85  if (res == -EPIPE)
86 // throw(SndPcmError("SndPcm::WriteBuf() Buffer Underrun!\n"));
87  RecoverXRun(data);
88  }
89  SndPcm(int irate,int ichannels_in,int ichannels_out,int ilatency,
90  const char* pdevice,const char* cdevice);
91  ~SndPcm();
92 
93  void Start(void);
94  void Stop(void);
95  void RecoverXRun(short* data);
96 
97  void Poll(void);
98 
99 private:
100 /*
101  * The functions which follow are taken from the latency test included
102  * in the ALSA source distribution, with the following copyright note:
103  *
104  * Latency test program
105  *
106  * Author: Jaroslav Kysela <perex@suse.cz>
107  *
108  * Author of bandpass filter sweep effect:
109  * Maarten de Boer <mdeboer@iua.upf.es>
110  *
111  * This small demo program can be used for measuring latency between
112  * capture and playback. This latency is measured from driver (diff when
113  * playback and capture was started). Scheduler is set to SCHED_RR.
114  *
115  *
116  * This program is free software; you can redistribute it and/or modify
117  * it under the terms of the GNU General Public License as published by
118  * the Free Software Foundation; either version 2 of the License, or
119  * (at your option) any later version.
120  *
121  * This program is distributed in the hope that it will be useful,
122  * but WITHOUT ANY WARRANTY; without even the implied warranty of
123  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
124  * GNU General Public License for more details.
125  *
126  * You should have received a copy of the GNU General Public License
127  * along with this program; if not, write to the Free Software
128  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
129  *
130  */
131  int setparams_stream(snd_pcm_t *handle,
132  snd_pcm_hw_params_t *params,
133  int channels,
134  const char *id);
135  int setparams_bufsize(snd_pcm_t *handle,
136  snd_pcm_hw_params_t *params,
137  snd_pcm_hw_params_t *tparams,
138  snd_pcm_uframes_t bufsize,
139  const char *id);
140  int setparams_set(snd_pcm_t *handle,
141  snd_pcm_hw_params_t *params,
142  snd_pcm_sw_params_t *swparams,
143  const char *id);
144  int setparams(snd_pcm_t *phandle, snd_pcm_t *chandle, int *bufsize);
145  long readbuf(snd_pcm_t *handle, char *buf, long len);
146  long writebuf(snd_pcm_t *handle, char *buf, long len);
147 };
148