gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
Capture.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 CAPTURE_H
18 #define CAPTURE_H
19 
20 #include <ALSA/ALSA.H>
21 
22 namespace ALSA {
25  class Capture : public Stream {
26  void init(const char *devName){
27  int err=Stream::init(devName, SND_PCM_STREAM_CAPTURE, block ? 0 : SND_PCM_NONBLOCK);
28  if (err < 0) {
29  std::cout<<err<<std::endl;
30  std::cerr<<"Capture :: Capture : open error: "<< snd_strerror(err)<<std::endl;
31  assert("open error");
32  }
33  }
34  public:
38  Capture(const char *devName) : Stream() {
39  init(devName);
40  };
41 
43  Capture() {
44  init("default");
45  };
46 
51  int readBuf(char *buffer, size_t len){
52  PCM_NOT_OPEN_CHECK_NO_PRINT(getPCM(), int) // check pcm is open
53  // PCM_NOT_OPEN_CHECK(getPCM()) // check pcm is open
54  int bytes_per_frame = getFormatPhysicalWidth() * getChannels()/8;
55  int ret=0;
56  while ((len-=ret)>0){
57  ret = snd_pcm_readi(getPCM(), buffer, len);
58  if (ret<0)
59  switch (ret){
60  case -EAGAIN: // try again
61  ret=0;
62  break;
63  case -EBADFD:
64  printf("Capture::readbuf : PCM state %s\n", getStateName());
65  return ALSADebug().evaluateError(ret, "reading failed because pcm is not in the correct state\n");
66  case -EPIPE:
67  ALSADebug().evaluateError(ret," writeBuf overrun\n");
68  return ALSADebug().evaluateError(ret);
69  case -ESTRPIPE:
70  return ALSADebug().evaluateError(ret);
71  default:
72  std::cout<<"read error "<<ret<<'\n';
74  }
75  if (ret==0 && block!=0)
76  wait();
77  buffer+= ret*bytes_per_frame;
78  }
79  return 0;
80  }
81 
86  template <typename Derived>
87  int readBuf(const Eigen::DenseBase<Derived> &audioData){
88  return readBuf((char*)&audioData(0,0), audioData.rows());
89  }
90 
95  template <typename Derived>
96  Capture& operator>>(const Eigen::DenseBase<Derived> &audioData){
97  if (readBuf(audioData)<0)
98  throw;
99  return *this;
100  }
101  };
102 }
103 #endif //CAPTURE_H
void init(const char *devName)
Definition: Capture.H:26
#define PCM_NOT_OPEN_CHECK_NO_PRINT(pcm, type)
Definition: ALSA.H:28
virtual snd_pcm_t * getPCM()
Definition: PCM.H:42
int readBuf(char *buffer, size_t len)
Definition: Capture.H:51
Capture & operator>>(const Eigen::DenseBase< Derived > &audioData)
Definition: Capture.H:96
const char * getStateName()
Definition: PCM.H:176
int init(const char *device, snd_pcm_stream_t streamType, bool blockIn)
Definition: Stream.H:36
Definition: ALSA.H:26
Capture(const char *devName)
Definition: Capture.H:38
int readBuf(const Eigen::DenseBase< Derived > &audioData)
Definition: Capture.H:87
virtual int evaluateError(int errorNum)
Definition: ALSADebug.H:61
int getFormatPhysicalWidth()
Definition: Hardware.H:202
Definition: IIOMMap.H:56
int wait(int timeOut=1000)
Definition: Stream.H:158
#define ALSA_UNKNOWN_READ_ERROR
error when reading and an unknown code is retured
Definition: ALSADebug.H:26
Capture()
Constructor.
Definition: Capture.H:43
int getChannels()
Definition: Hardware.H:222
gtkIOStream: /tmp/gtkiostream/include/ALSA/Capture.H Source File
GTK+ IOStream  Beta