CLAM-Development  1.4.0
AudioSink.hxx
Go to the documentation of this file.
1 #ifndef AudioSink_hxx
2 #define AudioSink_hxx
3 
4 #include "Processing.hxx"
5 #include "AudioInPort.hxx"
6 
7 #include <sstream>
8 
9 namespace CLAM
10 {
11  class AudioSink : public Processing
12  {
13  public:
14  struct Port
15  {
16  float* mFloatBuffer;
17  double* mDoubleBuffer;
18  unsigned mBufferSize;
20 
21  //resize needs a default constructor
22  Port()
24  {
25  }
26 
27  explicit Port(AudioInPort* p)
29  {
30  }
31  };
32  typedef std::vector<Port> Ports;
33 
34  private:
35  class Config : public ProcessingConfig
36  {
38  DYN_ATTRIBUTE( 0, public, int, NSinks);
39  protected:
40  void DefaultInit()
41  {
42  AddAll();
43  UpdateData();
44  SetNSinks(1);
45  };
46  void LoadFrom(Storage & storage)
47  {
49  if (not HasNSinks())
50  {
51  AddNSinks();
52  UpdateData();
53  SetNSinks(1);
54  }
55  }
56  };
57 
58  private:
59  Config _config;
60  Ports _ports;
61 
62  public:
63  AudioSink(const ProcessingConfig & config=Config())
64  {
65  //After being dropped it is ready to run as it does not need any configuration at all
66  //SetExecState(Ready);
67  Configure( config );
68  ResizePorts(1);
69  }
70 
72  {
73  for (unsigned port = 0; port < _ports.size(); ++port)
74  delete _ports[port].mPort;
75  }
76 
78  void SetFrameAndHopSize(const int val, unsigned index)
79  {
80  CLAM_ASSERT(index < _ports.size(), "AudioInPort index out of range");
81  Port& port = _ports[index];
82  port.mPort->SetSize(val);
83  port.mPort->SetHop(val);
84  }
85 
86  void SetExternalBuffer(float* buf, unsigned nframes, unsigned index);
87  void SetExternalBuffer(double* buf, unsigned nframes, unsigned index);
88 
89  bool Do();
90 
91  const char* GetClassName() const { return "AudioSink";}
92 
93  const ProcessingConfig & GetConfig() const
94  {
95  return _config;
96  }
97 
98  bool ConcreteConfigure(const ProcessingConfig& config)
99  {
100  CopyAsConcreteConfig(_config, config);
101  unsigned sinks = _config.GetNSinks();
102 
103  ResizePorts(sinks);
104 
105  return true;
106  }
107 
108  Ports& GetPorts() { return _ports; }
109 
110  private:
111  std::string const Portname(unsigned port) const
112  {
113  std::ostringstream os;
114  os << port + 1; //to make ports one based (when viewed in jack)
115  return os.str();
116  }
117 
118  void ResizePorts(unsigned sinks)
119  {
120  if (sinks == _ports.size())
121  return;
122 
123  for (unsigned port = sinks; port < _ports.size(); ++port)
124  delete _ports[port].mPort;
125 
126  unsigned oldSize = _ports.size();
127  _ports.resize(sinks);
128 
129  for (unsigned port = oldSize; port < sinks; ++port)
130  _ports[port] = Port(new AudioInPort(Portname(port), this));
131  }
132  };
133 
134 } //namespace CLAM
135 
136 #endif
137