gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
Stream.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 STREAM_H
18 #define STREAM_H
19 
20 #include <ALSA/ALSA.H>
21 
22 namespace ALSA {
25  class Stream : public Software {
26 
27  protected:
28  bool block;
29 
30  public:
32  Stream() {
33  block=1;
34  }
35 
36  int init(const char *device, snd_pcm_stream_t streamType, bool blockIn) {
37  block=blockIn;
38  int ret=open(device, streamType, block ? 0 : SND_PCM_NONBLOCK);
39  if (ret<0){
40  std::cerr<<"Couldn't open device"<<std::endl;
41  return ret;
42  }
43 
44  // if ((ret=rateResample(1))<0){
45  // std::cerr<<"Couldn't rateResample"<<std::endl;
46  // return ret;
47  // }
48  if ((ret=setAccess(SND_PCM_ACCESS_RW_INTERLEAVED))<0){
49  std::cerr<<"Couldn't setAccess"<<std::endl;
50  return ret;
51  }
52  if ((ret=setFormat(ALSA_DEFAULT_FORMAT))<0){
53  std::cerr<<"Couldn't setFormat"<<std::endl;
54  return ret;
55  }
56  if ((ret=setChannels(ALSA_DEFAULT_CHANNELS))<0){
57  std::cerr<<"Couldn't setChannels"<<std::endl;
58  return ret;
59  }
60 
61  unsigned int startRate=ALSA_DEFAULT_START_FS;
62  if ((ret=setSampleRate(startRate))<0){
63  std::cerr<<"Couldn't setSampleRate"<<std::endl;
64  return ret;
65  }
66  return 0;
67  }
68 
70  virtual ~Stream(){
71  }
72 
78  const char *getFormatName(const snd_pcm_format_t format){
79  return snd_pcm_format_name(format);
80  }
81 
88  int open(const char *device, snd_pcm_stream_t streamType, const int block) {
89  std::cout<<"opening the device "<<device<<std::endl;
90  int ret;
91  if ((ret=PCM::open(device, streamType, block))<0)
92  return ALSADebug().evaluateError(ret);
93  if ((ret=fillParams())<0)
94  std::cerr<<"Couldn't fillParams "<<snd_strerror(ret)<<std::endl;
95  return ret;
96  }
97 
101  int setParams() {
102  int err=setHWParams();
103  if (err < 0) {
104  return ALSADebug().evaluateError(err, "Stream:: setParams : Unable to set hw params ");
105  return err;
106  }
107 
108  if ((err = getSWParams()) < 0)
109  return ALSADebug().evaluateError(err, "Stream:: setParams : Unable to determine current sParams ");
110 
111  if ((err = setSWThreshold(0x7fffffff)) < 0)
112  return ALSADebug().evaluateError(err, "Stream:: setParams : Unable to set start threshold mode ");
113 
114  snd_pcm_uframes_t val = 4;
115  if (block)
116  getPeriodSize(&val);
117 
118  if ((err=setAvailMin(val))< 0)
119  return ALSADebug().evaluateError(err, "Stream:: setParams : Unable to set avail min ");
120 
121  if ((err=setSWParams()) < 0)
122  return ALSADebug().evaluateError(err, "Stream:: setParams : Unable to set sw params ");
123  return err;
124  }
125 
132  int setSilence(void *data, unsigned int samples){
133  snd_pcm_format_t format;
134  int ret=getFormat(format);
135  if (ret<0)
136  return ret;
137  return snd_pcm_format_set_silence(format, data, samples);
138  }
139 
144  int link(Stream &s){
145  PCM_NOT_OPEN_CHECK(getPCM()) // check pcm is open
146  return snd_pcm_link(getPCM(), *s.getPCMP());
147  }
148 
158  int wait(int timeOut=1000) {
159  PCM_NOT_OPEN_CHECK(getPCM()) // check pcm is open
160  return snd_pcm_wait(getPCM(), timeOut);
161  }
162 
167  snd_pcm_format_t format;
168  int ret=getFormat(format);
169  if (ret>=0)
170  return snd_pcm_format_width(format);
171  return ret;
172  }
173  };
174 }
175 #endif //STREAM_H
int setSampleRate(unsigned int rrate, int dir=0)
Definition: Hardware.H:248
int getPeriodSize(snd_pcm_uframes_t *p, int *dir=NULL)
Definition: Hardware.H:275
int open(const char *device, snd_pcm_stream_t streamType, const int block)
Definition: Stream.H:88
int setSWParams()
Definition: Software.H:51
bool block
Whether to block or use NONBLOCK.
Definition: Stream.H:28
#define ALSA_DEFAULT_CHANNELS
Definition: ALSA.H:39
snd_pcm_t ** getPCMP()
Definition: PCM.H:49
virtual snd_pcm_t * getPCM()
Definition: PCM.H:42
Stream()
Constructor.
Definition: Stream.H:32
int setHWParams()
Definition: Hardware.H:33
int init(const char *device, snd_pcm_stream_t streamType, bool blockIn)
Definition: Stream.H:36
#define ALSA_DEFAULT_FORMAT
Definition: ALSA.H:38
Definition: ALSA.H:26
int setAccess(snd_pcm_access_t access)
Definition: Hardware.H:105
int getFormat(snd_pcm_format_t &format)
Definition: Hardware.H:195
virtual int evaluateError(int errorNum)
Definition: ALSADebug.H:61
int setParams()
Definition: Stream.H:101
#define ALSA_DEFAULT_START_FS
Definition: ALSA.H:37
int fillParams()
Definition: Hardware.H:51
#define PCM_NOT_OPEN_CHECK(pcm)
Definition: ALSA.H:30
int open(const char *device, snd_pcm_stream_t streamType, const int block)
Definition: PCM.H:59
int setSilence(void *data, unsigned int samples)
Silence a PCM samples buffer.
Definition: Stream.H:132
int setSWThreshold(snd_pcm_uframes_t thresh)
Definition: Software.H:60
int link(Stream &s)
Definition: Stream.H:144
int wait(int timeOut=1000)
Definition: Stream.H:158
int getSWParams()
Definition: Software.H:43
const char * getFormatName(const snd_pcm_format_t format)
get name of PCM sample format
Definition: Stream.H:78
int setFormat(snd_pcm_format_t format)
Definition: Hardware.H:182
int setChannels(unsigned int cnt)
Definition: Hardware.H:214
virtual ~Stream()
Destructor.
Definition: Stream.H:70
int setAvailMin(snd_pcm_uframes_t cnt)
Definition: Software.H:69
int getFormatBits()
Definition: Stream.H:166
gtkIOStream: /tmp/gtkiostream/include/ALSA/Stream.H Source File
GTK+ IOStream  Beta