gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
PCM.H
Go to the documentation of this file.
1 /* Copyright 2000-2018 Matt Flax <flatmax@flatmax.org>
2  This file is part of GTK+ IOStream class set
3 
4  GTK+ IOStream is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  GTK+ IOStream is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You have received a copy of the GNU General Public License
15  along with GTK+ IOStream
16 */
17 #ifndef PCM_H
18 #define PCM_H
19 
20 #include <ALSA/ALSA.H>
21 
22 namespace ALSA {
23 
26  class PCM {
27  protected:
28  snd_output_t *log;
29  snd_pcm_t *handle;
30  public:
31  PCM(){
32  log=NULL;
33  handle=NULL;
34  }
35 
36  virtual ~PCM(){
37  if (log)
38  snd_output_close(log);
39  close();
40  }
41 
42  virtual snd_pcm_t *getPCM(){
43  return handle;
44  }
45 
49  snd_pcm_t **getPCMP(){
50  return &handle;
51  }
52 
59  int open(const char *device, snd_pcm_stream_t streamType, const int block) {
60  std::cout<<"opening the device "<<device<<std::endl;
61  int ret=snd_pcm_open(getPCMP(), device, streamType, block);
62  if (ret<0)
63  std::cerr<<"Couldn't open the device "<<snd_strerror(ret)<<std::endl;
64  return ret;
65  }
66 
70  int close(){
71  PCM_NOT_OPEN_CHECK(getPCM()) // check pcm is open
72  int ret=0;
73  drop();
74  snd_pcm_hw_free(getPCM());
75  ret=snd_pcm_close(getPCM());
76  *getPCMP()=NULL;
77  return ret;
78  }
79 
83  int drop(){
84  PCM_NOT_OPEN_CHECK(getPCM()) // check pcm is open
85  int ret=0;
86  if (running())
87  ret=snd_pcm_drop(getPCM());
88  else
89  std::cout<<"PCM::drop can't drop, not running"<<std::endl;
90  return ret;
91  }
92 
96  int drain(){
97  PCM_NOT_OPEN_CHECK(getPCM()) // check pcm is open
98  return snd_pcm_drain(getPCM());
99  }
100 
104  int reset(){
105  PCM_NOT_OPEN_CHECK(getPCM()) // check pcm is open
106  return snd_pcm_reset(getPCM());
107  }
108 
109  void enableLog(){
110  snd_output_stdio_attach(&log, stdout, 0);
111  }
112 
113  int logEnabled(){
114  if (log)
115  return 1;
116  else
117  printf("errorlogging not enabled, call enableLog first.\n");
118  return 0;
119  }
120 
121  int dumpStatus(){
122  int err=0;
123  if (logEnabled()){
124  snd_pcm_status_t *status;
125  snd_pcm_status_alloca(&status);
126  if ((err = snd_pcm_status(getPCM(), status)) < 0)
127  printf("Stream status error: %s\n", snd_strerror(err));
128  else
129  snd_pcm_status_dump(status, log);
130  }
131  return err;
132  }
133 
134  int dumpPCM(){
135  PCM_NOT_OPEN_CHECK(getPCM()) // check pcm is open
136  int ret=0;
137  if (!logEnabled())
138  return ret;
139  return snd_pcm_dump(getPCM(), log);
140  }
141 
142  int dumpSetup(){
143  PCM_NOT_OPEN_CHECK(getPCM()) // check pcm is open
144  int ret=0;
145  if (!logEnabled())
146  return ret;
147  return snd_pcm_dump_setup(getPCM(), log);
148  }
149 
150  int dumpHWSetup(){
151  PCM_NOT_OPEN_CHECK(getPCM()) // check pcm is open
152  int ret=0;
153  if (!logEnabled())
154  return ret;
155  return snd_pcm_dump_hw_setup(getPCM(), log);
156  }
157 
158  int dumpSWSetup(){
159  PCM_NOT_OPEN_CHECK(getPCM()) // check pcm is open
160  int ret=0;
161  if (!logEnabled())
162  return ret;
163  return snd_pcm_dump_sw_setup(getPCM(), log);
164  }
165 
166  int start(){
167  PCM_NOT_OPEN_CHECK_NO_PRINT(getPCM(), int) // check pcm is open
168  return snd_pcm_start(getPCM());
169 
170  }
171  snd_pcm_state_t getState(){
172  PCM_NOT_OPEN_CHECK_NO_PRINT(getPCM(), snd_pcm_state_t) // check pcm is open
173  return snd_pcm_state(getPCM());
174  }
175 
176  const char *getStateName(){
177  PCM_NOT_OPEN_CHECK_STRING(getPCM()) // check pcm is open
178  return snd_pcm_state_name(getState());
179  }
182  bool prepared(){
183  PCM_NOT_OPEN_CHECK_NO_PRINT(getPCM(), bool) // check pcm is open
184  return getState()==SND_PCM_STATE_PREPARED;
185  }
186 
189  bool opened(){
190  PCM_NOT_OPEN_CHECK(getPCM()) // check pcm is open
191  return getState()==SND_PCM_STATE_OPEN;
192  }
193 
196  bool isSetup(){
197  PCM_NOT_OPEN_CHECK(getPCM()) // check pcm is open
198  return getState()==SND_PCM_STATE_SETUP;
199  }
200 
203  bool running(){
204  PCM_NOT_OPEN_CHECK(getPCM()) // check pcm is open
205  return getState()==SND_PCM_STATE_RUNNING;
206  }
207 
210  bool hasXrun(){
211  PCM_NOT_OPEN_CHECK(getPCM()) // check pcm is open
212  return getState()==SND_PCM_STATE_XRUN;
213  }
214 
217  bool draining(){
218  PCM_NOT_OPEN_CHECK(getPCM()) // check pcm is open
219  return getState()==SND_PCM_STATE_DRAINING;
220  }
221 
224  bool paused(){
225  PCM_NOT_OPEN_CHECK(getPCM()) // check pcm is open
226  return getState()==SND_PCM_STATE_PAUSED;
227  }
228 
231  bool suspended(){
232  PCM_NOT_OPEN_CHECK(getPCM()) // check pcm is open
233  return getState()==SND_PCM_STATE_SUSPENDED;
234  }
235 
238  bool disconnected(){
239  PCM_NOT_OPEN_CHECK(getPCM()) // check pcm is open
240  return getState()==SND_PCM_STATE_DISCONNECTED;
241  }
242  };
243 }
244 #endif //PCM_H
bool disconnected()
Definition: PCM.H:238
int drain()
Definition: PCM.H:96
int dumpHWSetup()
Definition: PCM.H:150
#define PCM_NOT_OPEN_CHECK_STRING(pcm)
Definition: ALSA.H:31
snd_pcm_t ** getPCMP()
Definition: PCM.H:49
#define PCM_NOT_OPEN_CHECK_NO_PRINT(pcm, type)
Definition: ALSA.H:28
virtual snd_pcm_t * getPCM()
Definition: PCM.H:42
bool hasXrun()
Definition: PCM.H:210
bool opened()
Definition: PCM.H:189
const char * getStateName()
Definition: PCM.H:176
Definition: ALSA.H:26
PCM()
Definition: PCM.H:31
Definition: PCM.H:26
bool prepared()
Definition: PCM.H:182
bool paused()
Definition: PCM.H:224
int drop()
Definition: PCM.H:83
#define PCM_NOT_OPEN_CHECK(pcm)
Definition: ALSA.H:30
int dumpStatus()
Definition: PCM.H:121
void enableLog()
Definition: PCM.H:109
int open(const char *device, snd_pcm_stream_t streamType, const int block)
Definition: PCM.H:59
bool suspended()
Definition: PCM.H:231
snd_output_t * log
The log stream if enabled.
Definition: PCM.H:28
int start()
Definition: PCM.H:166
bool draining()
Definition: PCM.H:217
int dumpPCM()
Definition: PCM.H:134
Definition: IIOMMap.H:56
int dumpSWSetup()
Definition: PCM.H:158
bool running()
Definition: PCM.H:203
virtual ~PCM()
Definition: PCM.H:36
int reset()
Definition: PCM.H:104
int dumpSetup()
Definition: PCM.H:142
int close()
Definition: PCM.H:70
bool isSetup()
Definition: PCM.H:196
int logEnabled()
Definition: PCM.H:113
snd_pcm_t * handle
PCM handle.
Definition: PCM.H:29
snd_pcm_state_t getState()
Definition: PCM.H:171
gtkIOStream: /tmp/gtkiostream/include/ALSA/PCM.H Source File
GTK+ IOStream  Beta